BsonTreeModel::BsonTreeModel(const std::vector<MongoDocumentPtr> &documents, QObject *parent) : BaseClass(parent), _root(new BsonTreeItem(this)) { for (int i = 0; i < documents.size(); ++i) { MongoDocumentPtr doc = documents[i]; BsonTreeItem *child = new BsonTreeItem(doc->bsonObj(), _root); parseDocument(child, doc->bsonObj(), doc->bsonObj().isArray()); QString idValue; BsonTreeItem *idItem = child->childByKey("_id"); if (idItem) { idValue = idItem->value(); } child->setKey(QString("(%1) %2").arg(i + 1).arg(idValue)); int count = BsonUtils::elementsCount(doc->bsonObj()); if (doc->bsonObj().isArray()) { child->setValue(arrayValue(count)); child->setType(mongo::Array); } else { child->setValue(objectValue(count)); child->setType(mongo::Object); } _root->addChild(child); } }
void PrintHandler::startArray() { arrayValue(); _out << '[' << endLine(); _tab.append(indent(), ' '); ++_array; _objStart = true; }
void PrintHandler::startObject() { arrayValue(); _out << '{'; _out << endLine(); _tab.append(indent(), ' '); _objStart = true; }
void SuperastCPP::ensureSonIsArray() { if (!sonValue.IsArray()) { rapidjson::Value arrayValue(rapidjson::kArrayType); if (!sonValue.IsNull()) { arrayValue.PushBack(sonValue, allocator); } sonValue = arrayValue; } }
bool WriteStructuredCloneImageData(JSContext* aCx, JSStructuredCloneWriter* aWriter, ImageData* aImageData) { uint32_t width = aImageData->Width(); uint32_t height = aImageData->Height(); JS::Rooted<JSObject*> dataArray(aCx, aImageData->GetDataObject()); JSAutoCompartment ac(aCx, dataArray); JS::Rooted<JS::Value> arrayValue(aCx, JS::ObjectValue(*dataArray)); return JS_WriteUint32Pair(aWriter, SCTAG_DOM_IMAGEDATA, 0) && JS_WriteUint32Pair(aWriter, width, height) && JS_WriteTypedArray(aWriter, arrayValue); }
// COMPOUND STATEMENTS, A block of statements in clangAST bool SuperastCPP::TraverseCompoundStmt(clang::CompoundStmt* compoundStmt) { rapidjson::Value arrayValue(rapidjson::kArrayType); // Traverse each statement and append it to the array for (clang::Stmt* stmt : compoundStmt->body()) { TRY_TO(TraverseStmt(stmt)); if (sonValue.IsArray()) { addElemsToArray(arrayValue, sonValue); } else { arrayValue.PushBack(sonValue, allocator); } } sonValue = arrayValue; return true; }
// DECLSTMT Allows mix of decl and statements bool SuperastCPP::TraverseDeclStmt(clang::DeclStmt* declStmt) { if (declStmt->isSingleDecl()) { TRY_TO(TraverseDecl(declStmt->getSingleDecl())); } else { // Group of declStmt rapidjson::Value arrayValue(rapidjson::kArrayType); const clang::DeclGroupRef declGroup = declStmt->getDeclGroup(); for (auto iterator = declGroup.begin(); iterator != declGroup.end(); ++iterator) { TRY_TO(TraverseDecl(*iterator)); arrayValue.PushBack(sonValue, allocator); } sonValue = arrayValue; } return true; }
// CXX CLASS MEMBER CALL, with dot operator ('.') bool SuperastCPP::TraverseCXXMemberCallExpr( clang::CXXMemberCallExpr* memberCall) { // Method Decl clang::CXXMethodDecl* methodDecl = memberCall->getMethodDecl(); // Implicit obj clang::Expr* objectExpr = memberCall->getImplicitObjectArgument(); // If this is an implicitCast, get the subExpr if (clang::ImplicitCastExpr* implicitExpr = llvm::dyn_cast<clang::ImplicitCastExpr>(objectExpr)) { objectExpr = implicitExpr->getSubExpr(); } const std::string& methodName = methodDecl->getNameInfo().getAsString(); //const clang::Type* type = methodDecl->getReturnType().getTypePtr(); rapidjson::Value rightValue(rapidjson::kObjectType); addId(rightValue); addPos(rightValue, memberCall); rightValue.AddMember("type", "function-call", allocator); rightValue.AddMember("name", rapidjson::Value().SetString(methodName.c_str(), methodName.size(), allocator), allocator); // For each argument rapidjson::Value arrayValue(rapidjson::kArrayType); for (auto arg : memberCall->arguments()) { TRY_TO(TraverseStmt(arg)); arrayValue.PushBack(sonValue, allocator); } rightValue.AddMember("arguments", arrayValue, allocator); // Uncomment to add the type //rightValue.AddMember("return-type", createTypeValue(type), allocator); TRY_TO(TraverseStmt(objectExpr)); // Object from which the method is called rapidjson::Value memberCallValue = createBinOpValue(".", sonValue, rightValue); addPos(memberCallValue, memberCall); sonValue = memberCallValue; return true; }
int main(){ int value = 1; std::cout << "before passValue " << value << "\n"; passValue(value); std::cout << "after passValue " << value << "\n"; int prime[5] = {2,3,5,7,11}; std::cout << "before arrayValue " << prime[0] << " " << prime[1] << " " << prime[2] << " " << prime[3] << " " << prime[4] << "\n"; arrayValue(prime); std::cout << "after arrayValue " << prime[0] << " " << prime[1] << " " << prime[2] << " " << prime[3] << " " << prime[4] << "\n"; int dabba[5] = {1,2,3,4,5}; int total = 0; for (int i=0; i<5; ++i){ total = total + dabba[i]; } std::cout << "total sum " << total << "\n"; return 0; }
void PrintHandler::value(unsigned v) { arrayValue(); _out << v; _value = true; }
void PrintHandler::value(double d) { arrayValue(); _out << d; _objStart = false; }
void PrintHandler::value(const std::string& value) { arrayValue(); Stringifier::formatString(value, _out); _value = true; }
void PrintHandler::value(UInt64 v) { arrayValue(); _out << v; _value = true; }
bool SuperastCPP::TraverseCXXOperatorCallExpr(clang::CXXOperatorCallExpr* operatorCallExpr) { // IF THERE IS A PRINT auto decl = llvm::dyn_cast<clang::FunctionDecl>(operatorCallExpr->getCalleeDecl()); if (!decl) { std::cerr << "Unknown function in CXXOperatorCallExpr" << std::endl; return true; } const std::string functionName = decl->getNameInfo().getAsString(); if (functionName == PRINT_NAME || functionName == READ_NAME) { bool isFirst = false; if (!iofunctionStarted) { isFirst = true; iofunctionStarted = true; } rapidjson::Value arrayValue(rapidjson::kArrayType); for (unsigned i = 0; i < operatorCallExpr->getNumArgs(); ++i) { TRY_TO(TraverseStmt(operatorCallExpr->getArg(i))); if (i == 0) { // If sonValue is not an array, is because it reached the begin of // call, which is the cout/cerr/stream class if (sonValue.IsArray()) { arrayValue = sonValue; } } else { arrayValue.PushBack(sonValue, allocator); } } if (!isFirst) { sonValue = arrayValue; } else { iofunctionStarted = false; rapidjson::Value functionValue(rapidjson::kObjectType); addId(functionValue); addPos(functionValue, operatorCallExpr); functionValue.AddMember("type", "function-call", allocator); if (functionName == PRINT_NAME) functionValue.AddMember("name", "print", allocator); else functionValue.AddMember("name", "read", allocator); functionValue.AddMember("arguments", arrayValue, allocator); sonValue = functionValue; } } else if (functionName == VECTOR_POS_NAME) { // Operator [] TRY_TO(TraverseStmt(operatorCallExpr->getArg(0))); rapidjson::Value leftValue(rapidjson::kObjectType); leftValue = sonValue; TRY_TO(TraverseStmt(operatorCallExpr->getArg(1))); rapidjson::Value rightValue(rapidjson::kObjectType); rightValue = sonValue; sonValue = createBinOpValue("[]", leftValue, rightValue); } else { std::cerr << "Operator call not defined: " << functionName << std::endl; } return true; }
void PrintHandler::value(double d) { arrayValue(); _out << d; _value = true; }
void PrintHandler::null() { arrayValue(); _out << "null"; _objStart = false; }
void PrintHandler::value(bool b) { arrayValue(); _out << b; _value = true; }
void PrintHandler::value(unsigned v) { arrayValue(); _out << v; _objStart = false; }
void PrintHandler::value(bool b) { arrayValue(); _out << b; _objStart = false; }
void PrintHandler::null() { arrayValue(); _out << "null"; _value = true; }
void PrintHandler::value(const std::string& value) { arrayValue(); Stringifier::formatString(value, _out); _objStart = false; }
void PrintHandler::value(UInt64 v) { arrayValue(); _out << v; _objStart = false; }