From cfb8c591e50a86d752e39f6a1d81784016edd9df Mon Sep 17 00:00:00 2001 From: yehandong Date: Thu, 6 Jan 2022 15:12:01 -0800 Subject: [PATCH 1/5] Add 'break' as class function name. --- .../unit_tests/identifiers20.ts.result | 24 +++++++++++++++++++ src/MapleFE/typescript/stmt.spec | 1 + 2 files changed, 25 insertions(+) create mode 100644 src/MapleFE/test/typescript/unit_tests/identifiers20.ts.result diff --git a/src/MapleFE/test/typescript/unit_tests/identifiers20.ts.result b/src/MapleFE/test/typescript/unit_tests/identifiers20.ts.result new file mode 100644 index 0000000000..e9df6598ad --- /dev/null +++ b/src/MapleFE/test/typescript/unit_tests/identifiers20.ts.result @@ -0,0 +1,24 @@ +Matched 34 tokens. +Matched 44 tokens. +Matched 63 tokens. +Matched 69 tokens. +============= Module =========== +== Sub Tree == +class Klass + Fields: + items=[] + Instance Initializer: + Constructors: + Methods: + func break() throws: + for ( ) + console.log(i) + LocalClasses: + LocalInterfaces: + +== Sub Tree == +js_var Decl: obj=new Klass() +== Sub Tree == +obj.items.push(6,2,1,4,5,3) +== Sub Tree == +obj.break() diff --git a/src/MapleFE/typescript/stmt.spec b/src/MapleFE/typescript/stmt.spec index 16eef2fa74..e123336266 100644 --- a/src/MapleFE/typescript/stmt.spec +++ b/src/MapleFE/typescript/stmt.spec @@ -2250,6 +2250,7 @@ rule KeywordMemberFunctionName : ONEOF("return", "get", "set", "continue", + "break", "export") attr.action : BuildIdentifier() -- Gitee From e5e927d530ddb41f3096b8ddbc6bda08643d62f0 Mon Sep 17 00:00:00 2001 From: eching Date: Thu, 6 Jan 2022 20:28:24 -0800 Subject: [PATCH 2/5] Make typescript class obj contructor function a static member field in the corresponding C++ class. This simplifes constructor function naming and fixes preious problem in resolving the constructor function name of classes imported from other modules. --- .../ast2cpp/runtime/include/builtins.h | 18 +++++++---- src/MapleFE/ast2cpp/runtime/include/ts2cpp.h | 2 ++ src/MapleFE/ast2cpp/runtime/src/builtins.cpp | 6 ++-- src/MapleFE/ast2cpp/src/cpp_declaration.cpp | 8 ++--- src/MapleFE/ast2cpp/src/cpp_definition.cpp | 30 +++++++++---------- src/MapleFE/ast2cpp/src/helper.cpp | 2 +- 6 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/MapleFE/ast2cpp/runtime/include/builtins.h b/src/MapleFE/ast2cpp/runtime/include/builtins.h index 4d9fb0bf02..8a62655c67 100644 --- a/src/MapleFE/ast2cpp/runtime/include/builtins.h +++ b/src/MapleFE/ast2cpp/runtime/include/builtins.h @@ -18,10 +18,6 @@ namespace t2crt { -extern Function::Ctor Function_ctor; -extern Object::Ctor Object_ctor; -extern Function::Ctor Number_ctor; - template class Record : public Object { public: @@ -64,7 +60,7 @@ class Ctor_Array: public Function { // For creating array and array constructor instances #define ARR_OBJ(NM,CTOR) NM(&CTOR, CTOR.prototype) -#define ARR_CTOR(NM) NM(&Function_ctor, Function_ctor.prototype, Object_ctor.prototype) +#define ARR_CTOR(NM) NM(&Function::ctor, Function::ctor.prototype, Object::ctor.prototype) // Need all text type name for macros to generate names for predefined Array type constuctors. using ObjectP = t2crt::Object*; @@ -96,9 +92,19 @@ class RegExp : public Object { // TODO }; +class Number : public Object { +public: + // TODO + class Ctor : public t2crt::Function { + public: + Ctor(t2crt::Function* ctor, t2crt::Object* proto, t2crt::Object* prototype_proto) : t2crt::Function(ctor, proto, prototype_proto) { } + virtual const char* __GetClassName() const {return "Number ";} + }; + static Ctor ctor; +}; + } // namespace t2crt -using t2crt::Number_ctor; using t2crt::Record; using t2crt::JSON; using t2crt::RegExp; diff --git a/src/MapleFE/ast2cpp/runtime/include/ts2cpp.h b/src/MapleFE/ast2cpp/runtime/include/ts2cpp.h index 6cd956c2e1..863f83f57a 100644 --- a/src/MapleFE/ast2cpp/runtime/include/ts2cpp.h +++ b/src/MapleFE/ast2cpp/runtime/include/ts2cpp.h @@ -215,6 +215,7 @@ class Object { } virtual ~Object() {} class Ctor; + static Ctor ctor; JS_Val& operator[] (std::string key) { @@ -316,6 +317,7 @@ class Function : public Object { prototype->AddProp("constructor", val); } class Ctor; + static Ctor ctor; bool IsFuncObj() { return true; diff --git a/src/MapleFE/ast2cpp/runtime/src/builtins.cpp b/src/MapleFE/ast2cpp/runtime/src/builtins.cpp index bb11966d1a..8b64431814 100644 --- a/src/MapleFE/ast2cpp/runtime/src/builtins.cpp +++ b/src/MapleFE/ast2cpp/runtime/src/builtins.cpp @@ -2,9 +2,9 @@ namespace t2crt { -Object::Ctor Object_ctor (&Function_ctor, Function_ctor.prototype); -Function::Ctor Function_ctor(&Function_ctor, Function_ctor.prototype, Object_ctor.prototype); -Function::Ctor Number_ctor (&Function_ctor, Function_ctor.prototype, Object_ctor.prototype); +Object::Ctor Object::ctor = Object::Ctor (&Function::ctor, Function::ctor.prototype); +Function::Ctor Function::ctor = Function::Ctor(&Function::ctor, Function::ctor.prototype, Object::ctor.prototype); +Number::Ctor Number::ctor = Number::Ctor (&Function::ctor, Function::ctor.prototype, Object::ctor.prototype); ARRAY_CTOR_DEF(int) ARRAY_CTOR_DEF(long) diff --git a/src/MapleFE/ast2cpp/src/cpp_declaration.cpp b/src/MapleFE/ast2cpp/src/cpp_declaration.cpp index 4a53c953c5..43e0fa3c2d 100644 --- a/src/MapleFE/ast2cpp/src/cpp_declaration.cpp +++ b/src/MapleFE/ast2cpp/src/cpp_declaration.cpp @@ -57,7 +57,7 @@ class ImportExportModules : public AstVisitor { std::size_t found = mIncludes.find(incl); if (found == std::string::npos) { mIncludes += "#include \""s + filename + ".h\"\n"s; - mCppDecl->AddInit(mCppDecl->GetModuleName(filename.c_str()) + "::__init_func__();\n"s); + mCppDecl->AddInit(tab(1) + mCppDecl->GetModuleName(filename.c_str()) + "::__init_func__();\n"s); } } } @@ -830,8 +830,8 @@ std::string CppDecl::EmitClassNode(ClassNode *node) { str += indent + " "s+clsName+"* _new() {return new "s+clsName+"(this, this->prototype);}\n"s; str += indent + " virtual const char* __GetClassName() const {return \""s + clsName + " \";}\n"s; str += indent + "};\n"; + str += indent + "static Ctor ctor;\n"s; str += "};\n"; - str += "extern " + clsName + "::Ctor "s + clsName + "_ctor;\n"s; // emit declaration for JS class object constructor return str; } @@ -843,10 +843,10 @@ std::string CppDecl::EmitNewNode(NewNode *node) { MASSERT(node->GetId() && "No mId on NewNode"); if (node->GetId() && node->GetId()->IsTypeIdClass()) { // Generate code to create new obj and call constructor - str = node->GetId()->GetName() + "_ctor("s + node->GetId()->GetName() + "_ctor._new("s; + str = node->GetId()->GetName() + "::ctor("s + node->GetId()->GetName() + "::ctor._new("s; } else if (IsBuiltinObj(node->GetId()->GetName())) { // Check for builtin obejcts: t2crt::Object, t2crt::Function, etc. - str = node->GetId()->GetName() + "_ctor._new("s; + str = node->GetId()->GetName() + "::ctor._new("s; } else { str = "new "s + EmitTreeNode(node->GetId()); str += "("s; diff --git a/src/MapleFE/ast2cpp/src/cpp_definition.cpp b/src/MapleFE/ast2cpp/src/cpp_definition.cpp index 85f391f612..4adbda1b4e 100644 --- a/src/MapleFE/ast2cpp/src/cpp_definition.cpp +++ b/src/MapleFE/ast2cpp/src/cpp_definition.cpp @@ -20,18 +20,18 @@ namespace maplefe { std::string CppDef::EmitCtorInstance(ClassNode *c) { std::string str, thisClass, ctor, proto, prototypeProto; - ctor = "&t2crt::Function_ctor"; + ctor = "&t2crt::Function::ctor"; thisClass = c->GetName(); if (c->GetSuperClassesNum() == 0) { - proto = "t2crt::Function_ctor.prototype"; - prototypeProto = "t2crt::Object_ctor.prototype"; + proto = "t2crt::Function::ctor.prototype"; + prototypeProto = "t2crt::Object::ctor.prototype"; } else { - proto = c->GetSuperClass(0)->GetName() + "_ctor"s; + proto = c->GetSuperClass(0)->GetName() + "::ctor"s; prototypeProto = proto + ".prototype"s; proto.insert(0, "&"s, 0, std::string::npos); } - str = "\n// Instantiate constructor for class "+ thisClass+ "\n"s; - str += thisClass + "::Ctor "s + thisClass+"_ctor("s +ctor+","s+proto+","+prototypeProto+");\n\n"s; + str = "\n// Init class ctor as a static class field for "+ thisClass+ "\n"s; + str += thisClass + "::Ctor "s + thisClass+"::ctor = " + thisClass + "::Ctor("s +ctor+","s+proto+","+prototypeProto+");\n\n"s; // piggy back generation of static field definition for (unsigned i = 0; i < c->GetFieldsNum(); ++i) { @@ -388,7 +388,7 @@ std::string CppDef::EmitStructLiteralNode(StructLiteralNode* node) { // Handle embedded t2crt::ObjectLiterals recursively if (lit->IsStructLiteral()) { std::string props = EmitStructLiteralNode(static_cast(lit)); - str += "std::make_pair(\""s + fieldName + "\", t2crt::JS_Val(t2crt::Object_ctor._new("s + props + ")))"s; + str += "std::make_pair(\""s + fieldName + "\", t2crt::JS_Val(t2crt::Object::ctor._new("s + props + ")))"s; } break; } @@ -425,17 +425,17 @@ std::string CppDef::EmitObjPropInit(TreeNode* var, std::string varName, TreeNode if (userType == nullptr) { // no type info - create instance of builtin t2crt::Object with proplist - str = varName+ " = t2crt::Object_ctor._new("s + EmitTreeNode(node) + ")"s; + str = varName+ " = t2crt::Object::ctor._new("s + EmitTreeNode(node) + ")"s; } else if (IsVarTypeClass(var)) { // init var of type TS class // - create obj instance of user defined class and do direct field access init // - todo: handle class with generics - str = varName+ " = "s +userType->GetId()->GetName()+ "_ctor._new();\n"s; + str = varName+ " = "s +userType->GetId()->GetName()+ "::ctor._new();\n"s; str += EmitDirectFieldInit(varName, node); } else { // type is builtin (e.g. t2crt::Record) and StructNode types (e.g. TSInterface) // create instance of type but set constructor to the builtin t2crt::Object. - str = varName+ " = new "s +EmitUserTypeNode(userType)+ "(&t2crt::Object_ctor, t2crt::Object_ctor.prototype);\n"s; + str = varName+ " = new "s +EmitUserTypeNode(userType)+ "(&t2crt::Object::ctor, t2crt::Object::ctor.prototype);\n"s; auto n = mHandler->FindDecl(static_cast(userType->GetId())); if (n && n->IsStruct() && static_cast(n)->GetProp() == SProp_TSInterface) { str += EmitDirectFieldInit(varName, node); // do direct field init @@ -527,7 +527,7 @@ std::string CppDef::EmitDeclNode(DeclNode *node) { else if (n->IsStructLiteral()) str += EmitObjPropInit(node->GetVar(), varStr, idType, static_cast(n)); else if (node->GetVar()->IsIdentifier() && n->IsIdentifier() && n->IsTypeIdClass()) - str += varStr + "= &"s + n->GetName() + "_ctor"s; // init with ctor address + str += varStr + "= &"s + n->GetName() + "::ctor"s; // init with ctor address else if (n->IsFunction()) { if (hFuncTable.IsTopLevelFunc(n)) { str += varStr + " = new "s + "Cls_" + n->GetName() + "()"s; @@ -556,7 +556,7 @@ std::string EmitSuperCtorCall(TreeNode* node) { if (node && node->IsClass()) { std::string base, str; base = (static_cast(node)->GetSuperClassesNum() != 0)? static_cast(node)->GetSuperClass(0)->GetName() : "t2crt::Object"; - str = " "s + base + "_ctor"s; + str = " "s + base + "::ctor"s; return str; } return ""s; @@ -1008,7 +1008,7 @@ std::string CppDef::EmitBinOperatorNode(BinOperatorNode *node) { if (IsBracketNotationProp(n)) { rhs = EmitBracketNotationProp(static_cast(n), node->GetOprId(), false, rhsIsDynProp); } else if (IsClassId(n)) { - rhs = "&"s + n->GetName() + "_ctor"s; + rhs = "&"s + n->GetName() + "::ctor"s; } else rhs = EmitTreeNode(n); if(precd > mPrecedence || (precd == mPrecedence && !rl_assoc)) @@ -1225,7 +1225,7 @@ std::string CppDef::EmitNewNode(NewNode *node) { std::string clsName = EmitTreeNode(node->GetId()); if (IsBuiltinObj(clsName)) clsName = "t2crt::"s + clsName; - str = clsName + "_ctor("s + clsName + "_ctor._new()"s; + str = clsName + "::ctor("s + clsName + "::ctor._new()"s; } else if (id->IsTypeIdFunction()) { // TS: new () // When calling TS new() on constructor function: @@ -1425,7 +1425,7 @@ std::string CppDef::EmitInstanceOfNode(InstanceOfNode *node) { if (auto n = node->GetRight()) { if (IsClassId(n) || IsBuiltinObj(n->GetName())) - rhs = "&"s + n->GetName() + "_ctor"s; + rhs = "&t2crt::"s + n->GetName() + "::ctor"s; else rhs = EmitTreeNode(n); if(precd > mPrecedence || (precd == mPrecedence && !rl_assoc)) diff --git a/src/MapleFE/ast2cpp/src/helper.cpp b/src/MapleFE/ast2cpp/src/helper.cpp index 85403bac6c..809596cbe3 100644 --- a/src/MapleFE/ast2cpp/src/helper.cpp +++ b/src/MapleFE/ast2cpp/src/helper.cpp @@ -125,7 +125,7 @@ std::string GenFuncClass(std::string retType, std::string funcName, std::string str = R"""( class )""" + clsName + R"""( : public t2crt::Function { public: - )""" + clsName + R"""(() : t2crt::Function(&t2crt::Function_ctor,t2crt::Function_ctor.prototype,t2crt::Object_ctor.prototype) {} + )""" + clsName + R"""(() : t2crt::Function(&t2crt::Function::ctor,t2crt::Function::ctor.prototype,t2crt::Object::ctor.prototype) {} ~)""" + clsName + R"""(() {} )""" + retType + R"""( _body()""" + params + R"""(); -- Gitee From 8e37f7378640a9bfb89cf869179182b21923f1d2 Mon Sep 17 00:00:00 2001 From: eching Date: Fri, 7 Jan 2022 01:22:51 -0800 Subject: [PATCH 3/5] Temporary script change to generate build command for modules imported using the "require" keyword. --- src/MapleFE/test/typescript/ts2cxx-test.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/MapleFE/test/typescript/ts2cxx-test.sh b/src/MapleFE/test/typescript/ts2cxx-test.sh index 959d3121c7..cc5fd7add3 100755 --- a/src/MapleFE/test/typescript/ts2cxx-test.sh +++ b/src/MapleFE/test/typescript/ts2cxx-test.sh @@ -42,11 +42,21 @@ for f in $list; do while true; do $TS2AST $f || { echo "(ts2ast)$f" >> ts2cpp.failures.out; break; } dep=$(grep "^[ei][xm]port.* from " "$f" | sed "s/^ *[ei][xm]port.* from .\([^'\"]*\).*/\1.cpp/" | sort -u) + if [ -z $dep ]; then +# dep=$(grep "^[ei][xm]port.* require" "$f" | sed "s/^ *[ei][xm]port.* require *\([^();\"]*\)/\1.cpp/" | sort -u) + dep=$(grep "^[ei][xm]port.* require" "$f" | sed "s/^ *[ei][xm]port.* require(\"\(.*\)\");/\1.cpp/" | sort -u) + fi +echo $dep > xxx for cpp in $dep; do ts=$(sed 's/\.cpp/.ts/' <<< "$cpp") $TS2AST $ts dep="$dep "$(grep "^[ei][xm]port.* from " "$ts" | sed "s/^ *[ei][xm]port.* from .\([^'\"]*\).*/\1.cpp/" | sort -u) + if [ -z $dep ]; then +# dep=$(grep "^[ei][xm]port.* require" "$f" | sed "s/^ *[ei][xm]port.* require *\([^();\"]*\)/\1.cpp/" | sort -u) + dep=$(grep "^[ei][xm]port.* require" "$f" | sed "s/^ *[ei][xm]port.* require(\"\(.*\)\");/\1.cpp/" | sort -u) + fi done +echo $dep >> xxx dep=$(echo $dep | xargs -n1 | sort -u) $AST2CPP $f.ast || { echo "(ast2cpp)$f" >> ts2cpp.failures.out; break; } g++ -std=c++17 -g -I$RTINC -I$ASTINC $t.cpp $RTSRC/*.cpp $dep -o $t.out || { echo "(g++)$f" >> ts2cpp.failures2.out; break; } -- Gitee From 99b69e164864380c56d62b2d681c17b47ac10262 Mon Sep 17 00:00:00 2001 From: yehandong Date: Sat, 8 Jan 2022 20:45:09 -0800 Subject: [PATCH 4/5] Add 'import' to KeywordPropName. --- src/MapleFE/typescript/stmt.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MapleFE/typescript/stmt.spec b/src/MapleFE/typescript/stmt.spec index e123336266..fcfb2f362b 100644 --- a/src/MapleFE/typescript/stmt.spec +++ b/src/MapleFE/typescript/stmt.spec @@ -90,7 +90,6 @@ rule KeywordIdentifier : ONEOF("type", "infer", "asserts", "require", - "import", "global", "throw", "class", @@ -368,6 +367,7 @@ rule KeywordPropName : ONEOF("break", "let", "return", "extends", + "import", "get", "set", "var") -- Gitee From 08510f6476fa114fc92cde055b3032753fbfda37 Mon Sep 17 00:00:00 2001 From: eching Date: Sat, 8 Jan 2022 22:06:11 -0800 Subject: [PATCH 5/5] Nest array constructor class inside array class, and make array constructor function a static field of array class. Implement uniform naming of array constructor functions to support all array types. --- src/MapleFE/ast2cpp/include/helper.h | 1 + .../ast2cpp/runtime/include/builtins.h | 59 +++++++------------ src/MapleFE/ast2cpp/runtime/src/builtins.cpp | 12 ++-- src/MapleFE/ast2cpp/src/cpp_declaration.cpp | 4 +- src/MapleFE/ast2cpp/src/cpp_definition.cpp | 12 ++-- src/MapleFE/ast2cpp/src/helper.cpp | 18 ++++++ 6 files changed, 51 insertions(+), 55 deletions(-) diff --git a/src/MapleFE/ast2cpp/include/helper.h b/src/MapleFE/ast2cpp/include/helper.h index b8ef5e6e90..71ae151b5f 100644 --- a/src/MapleFE/ast2cpp/include/helper.h +++ b/src/MapleFE/ast2cpp/include/helper.h @@ -42,6 +42,7 @@ extern std::string GenAnonFuncName(TreeNode* node); inline std::string ClsName(std::string func) { return "Cls_"s + func; } extern void HandleThisParam(unsigned nParams, TreeNode* node, std::string& params, std::string&args); extern std::string hlpGetJSValTypeStr(TypeId typeId); +extern std::string ArrayCtorName(int dim, std::string type); class FuncTable { private: diff --git a/src/MapleFE/ast2cpp/runtime/include/builtins.h b/src/MapleFE/ast2cpp/runtime/include/builtins.h index 8a62655c67..24f6176af1 100644 --- a/src/MapleFE/ast2cpp/runtime/include/builtins.h +++ b/src/MapleFE/ast2cpp/runtime/include/builtins.h @@ -41,48 +41,29 @@ class Array : public Object { // Put JS Array.prototype props as static fields and methods in this class // and add to proplist of Array_ctor.prototype object on system init. -}; - -template -class Ctor_Array: public Function { - public: - Ctor_Array(Function* ctor, Object* proto, Object* prototype_proto) : Function(ctor, proto, prototype_proto) {} - - Array* _new() { - return new Array(this, this->prototype); - } - - Array* _new(std::initializer_listl) { - return new Array(this, this->prototype, l); - } + class Ctor: public Function { + public: + Ctor(Function* ctor, Object* proto, Object* prototype_proto) : Function(ctor, proto, prototype_proto) {} + Array* _new() { + return new Array(this, this->prototype); + } + Array* _new(std::initializer_listl) { + return new Array(this, this->prototype, l); + } + }; + static Ctor ctor; }; -// For creating array and array constructor instances -#define ARR_OBJ(NM,CTOR) NM(&CTOR, CTOR.prototype) -#define ARR_CTOR(NM) NM(&Function::ctor, Function::ctor.prototype, Object::ctor.prototype) - -// Need all text type name for macros to generate names for predefined Array type constuctors. -using ObjectP = t2crt::Object*; - -// predefined Array constructors for dimension 1-3 and basic types -#define ARRAY_CTOR_DECL(type) \ - extern Ctor_Array Array1D_##type; \ - extern Ctor_Array*> Array2D_##type; \ - extern Ctor_Array*>*> Array3D_##type; - -// predefined Array class constructors for dimensions 1-3 and basic types -#define ARRAY_CTOR_DEF(type) \ - Ctor_Array ARR_CTOR(Array1D_##type); \ - Ctor_Array*> ARR_CTOR(Array2D_##type); \ - Ctor_Array*>*> ARR_CTOR(Array3D_##type); - -ARRAY_CTOR_DECL(int) -ARRAY_CTOR_DECL(long) -ARRAY_CTOR_DECL(double) -ARRAY_CTOR_DECL(JS_Val) -ARRAY_CTOR_DECL(Object) -ARRAY_CTOR_DECL(ObjectP) +// Create ctor func for 1,2,3 dimension array of given type +// note: must be in sync with format generated by ArrayCtorName in helper.h +#define ARR_CTOR_DEF(type) \ + template <> \ + Array::Ctor Array::ctor = Array::Ctor(&Function::ctor, Function::ctor.prototype, Object::ctor.prototype); \ + template <> \ + Array*>::Ctor Array*>::ctor = Array*>::Ctor(&Function::ctor, Function::ctor.prototype, Object::ctor.prototype); \ + template <> \ + Array*>*>::Ctor Array*>*>::ctor = Array*>*>::Ctor(&Function::ctor, Function::ctor.prototype, Object::ctor.prototype); class JSON : public Object { // TODO diff --git a/src/MapleFE/ast2cpp/runtime/src/builtins.cpp b/src/MapleFE/ast2cpp/runtime/src/builtins.cpp index 8b64431814..d2bd4d4bb0 100644 --- a/src/MapleFE/ast2cpp/runtime/src/builtins.cpp +++ b/src/MapleFE/ast2cpp/runtime/src/builtins.cpp @@ -6,12 +6,12 @@ Object::Ctor Object::ctor = Object::Ctor (&Function::ctor, Function::ctor.p Function::Ctor Function::ctor = Function::Ctor(&Function::ctor, Function::ctor.prototype, Object::ctor.prototype); Number::Ctor Number::ctor = Number::Ctor (&Function::ctor, Function::ctor.prototype, Object::ctor.prototype); -ARRAY_CTOR_DEF(int) -ARRAY_CTOR_DEF(long) -ARRAY_CTOR_DEF(double) -ARRAY_CTOR_DEF(JS_Val) -ARRAY_CTOR_DEF(Object) -ARRAY_CTOR_DEF(ObjectP) +ARR_CTOR_DEF(int) +ARR_CTOR_DEF(long) +ARR_CTOR_DEF(double) +ARR_CTOR_DEF(JS_Val) +ARR_CTOR_DEF(Object) +ARR_CTOR_DEF(Object*) } // namepsace t2crt diff --git a/src/MapleFE/ast2cpp/src/cpp_declaration.cpp b/src/MapleFE/ast2cpp/src/cpp_declaration.cpp index 43e0fa3c2d..ab6ea0c89a 100644 --- a/src/MapleFE/ast2cpp/src/cpp_declaration.cpp +++ b/src/MapleFE/ast2cpp/src/cpp_declaration.cpp @@ -568,8 +568,8 @@ std::string CppDecl::EmitArrayLiteral(ArrayLiteralNode *node, int dim, std::stri if (node == nullptr) return std::string(); - // Generate ctor call to instantiate array, e.g. t2crt::Array1D_long._new(). See builtins.h - std::string str("t2crt::Array"s + std::to_string(dim) + "D_"s + type + "._new({"s ); + // Generate array ctor call to instantiate array + std::string str = ArrayCtorName(dim, type) + "._new({"s; for (unsigned i = 0; i < node->GetLiteralsNum(); ++i) { if (i) str += ", "s; diff --git a/src/MapleFE/ast2cpp/src/cpp_definition.cpp b/src/MapleFE/ast2cpp/src/cpp_definition.cpp index 4adbda1b4e..e35dea4fc2 100644 --- a/src/MapleFE/ast2cpp/src/cpp_definition.cpp +++ b/src/MapleFE/ast2cpp/src/cpp_definition.cpp @@ -451,8 +451,8 @@ std::string CppDef::EmitArrayLiterals(TreeNode *node, int dim, std::string type) if (type.back() == ' ') type.pop_back(); - // Generate ctor call to instantiate t2crt::Array, e.g. t2crt::Array1D_long._new(). See builtins.h - std::string str("t2crt::Array"s + std::to_string(dim) + "D_"s + type + "._new({"s ); + // Generate array ctor call to instantiate array + std::string str = ArrayCtorName(dim, type) + "._new({"s; for (unsigned i = 0; i < static_cast(node)->GetLiteralsNum(); ++i) { if (i) str += ", "s; @@ -477,13 +477,9 @@ std::string CppDef::EmitArrayLiteral(TreeNode* arrType, TreeNode* arrLiteral) { if (static_cast(arrType)->GetDims()) dims = static_cast(arrType)->GetDimsNum(); if (auto id = static_cast(arrType)->GetId()) { - // Get class name or TS builtin obj name for array of usertyp objects. - // ("Object" need to be translated into its type alias "ObjectP" - // see builtins.h). type = id->GetName(); - if (type.compare("t2crt::Object") == 0 || - type.compare("Object") == 0) - type = "ObjectP"; + if (type.compare("t2crt::Object") == 0 || type.compare("Object") == 0) + type = "t2crt::Object*"; } str = EmitArrayLiterals(arrLiteral, dims, type); } else if (arrType->IsPrimArrayType()) { // array of prim type diff --git a/src/MapleFE/ast2cpp/src/helper.cpp b/src/MapleFE/ast2cpp/src/helper.cpp index 809596cbe3..d44400fd34 100644 --- a/src/MapleFE/ast2cpp/src/helper.cpp +++ b/src/MapleFE/ast2cpp/src/helper.cpp @@ -181,4 +181,22 @@ void HandleThisParam(unsigned nParams, TreeNode* node, std::string& params, std: } } +// return array constructor name of given type +// format: +// 1D array: t2crt::Array::ctor +// 2D array: t2crt::Array*>::ctor +// 3D array: t2crt::Array*>*>::ctor +// ... +// note: must be in sycn with format generated by ARR_CTOR_DEF in builtins.h +std::string ArrayCtorName(int dim, std::string type) { + if (!dim) + return std::string(); + std::string str = "t2crt::Array<"s + type + ">"s; + for (int i=1; i"s; + } + str = str + "::ctor"s; + return str; +} + } // namespace maplefe -- Gitee