From 8c0510e29db9166b2df223d5f43b25e79d5fcc8b Mon Sep 17 00:00:00 2001 From: Yan Zhang Date: Tue, 11 Jan 2022 13:37:07 -0800 Subject: [PATCH 1/7] Add label to each generated graph --- src/MapleFE/docs/utils/proto.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/MapleFE/docs/utils/proto.js b/src/MapleFE/docs/utils/proto.js index 80bf386e32..341305e1cf 100644 --- a/src/MapleFE/docs/utils/proto.js +++ b/src/MapleFE/docs/utils/proto.js @@ -33,12 +33,12 @@ const agpt = asyncGenerator.prototype.__proto__; // All data for generating graphs let graphData = { - Classes : ["Array", "arr", "myCar", "car"], + Class : ["Array", "arr", "myCar", "car"], Generator: ["generator", [generator(), "generator_instance"], [gpt, "GeneratorPrototype"], [gpt.__proto__, "IteratorPrototype"], [generator.__proto__, "Generator"]], - Builtins : ["Symbol", "Math", "JSON", "Promise"], + Builtin : ["Symbol", "Math", "JSON", "Promise"], Closure : ["makeClosure", "closure"], - Iterators: ["myMap", "myMapIterator", "MapIteratorPrototype", [gpt.__proto__, "IteratorPrototype"]], + Iterator : ["myMap", "myMapIterator", "MapIteratorPrototype", [gpt.__proto__, "IteratorPrototype"]], Async : ["asyncFunction", "asyncGenerator", [asyncGenerator(), "asyncGenerator_instance"], [agpt, "AsyncGeneratorPrototype"], [agpt.__proto__, "AsyncIteratorPrototype"], [asyncGenerator.__proto__, "AsyncGenerator"]], }; @@ -73,8 +73,9 @@ function generateGraph(data) { for (let prop in data) { let graph = new Map(); insert(graph, 0, "Function", "Object", ...data[prop]); - for (let ctor of ["", "_with_ctors"]) { - console.log("digraph JS_" + prop + ctor + " {\nrankdir = TB;\nranksep=0.6;\nnodesep=0.6;\n" + (ctor != "" ? "" : "newrank=true;")); + for (let ctor of ["", "_with_Constructor_Edges"]) { + console.log("digraph JS_" + prop + ctor + " {\nlabel=\"\\n" + prop + " Graph" + ctor.replace(/_/g, " ") + "\\n(Node in gray: function)" + + "\";\nrankdir = TB;\nranksep=0.6;\nnodesep=0.6;\n" + (ctor != "" ? "" : "newrank=true;")); for (let [index, [key, val]] of Array.from(graph).entries()) { let func = typeof key === "function"; // Add comments with detailed information of keys @@ -86,7 +87,7 @@ function generateGraph(data) { // Add edges for prototype, constructor and __proto__ properties of objects for (let [f, c] of [["prototype", "blue"], ["constructor", "darkgreen"], ["__proto__", "red"]]) if (typeof key[f] !== "undefined" && key[f] !== null && graph.has(key[f]) && (ctor != "" || f !== "constructor")) - console.log((ctor != "" || f !== "prototype" ? "" : "subgraph cluster_" + val + " {\nrank=same;\ncolor=white;\n" + console.log((ctor != "" || f !== "prototype" ? "" : "subgraph cluster_" + val + " {\nlabel=\"\";rank=same;color=white;\n" + val + ";\n" + graph.get(key.prototype) + " [shape=box];\n}\n") + val + " -> " + graph.get(key[f]) + " [label=\"" + (f === "constructor" ? "ctor" : f) + "\", color=" + c + ", fontcolor=" + c + "];"); } -- Gitee From b74f0189a8d0bedb9b03bc9daaa2a1d243abd46a Mon Sep 17 00:00:00 2001 From: Wen HU Date: Wed, 12 Jan 2022 16:28:27 -0500 Subject: [PATCH 2/7] update field search --- src/MapleFE/astopt/src/ast_ti.cpp | 13 +++++-------- src/MapleFE/typescript/include/lang_keywords.def | 1 + 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/MapleFE/astopt/src/ast_ti.cpp b/src/MapleFE/astopt/src/ast_ti.cpp index 1a21909423..e60c63ad59 100644 --- a/src/MapleFE/astopt/src/ast_ti.cpp +++ b/src/MapleFE/astopt/src/ast_ti.cpp @@ -1466,15 +1466,12 @@ IdentifierNode *TypeInferVisitor::VisitIdentifierNode(IdentifierNode *node) { decl = mHandler->FindDecl(node, true); } } else if (node == fld) { - TreeNode *cls = gTypeTable.GetTypeFromTypeIdx(upper->GetTypeIdx()); - if (cls) { - scope = cls->GetScope(); - - // for imported decl, need trace down the import/export chain - if (decl && mXXport->IsImportedDeclId(mHandler->GetHidx(), decl->GetNodeId())) { + TreeNode *uptype = gTypeTable.GetTypeFromTypeIdx(upper->GetTypeIdx()); + if (uptype) { + scope = uptype->GetScope(); + decl = scope->FindDeclOf(node->GetStrIdx()); + if (!decl) { decl = scope->FindExportedDeclOf(node->GetStrIdx()); - } else { - decl = scope->FindDeclOf(node->GetStrIdx()); } } } else { diff --git a/src/MapleFE/typescript/include/lang_keywords.def b/src/MapleFE/typescript/include/lang_keywords.def index b8e018414e..096966bd2b 100644 --- a/src/MapleFE/typescript/include/lang_keywords.def +++ b/src/MapleFE/typescript/include/lang_keywords.def @@ -112,6 +112,7 @@ LANGKEYWORD(Capitalize) LANGKEYWORD(Uncapitalize) LANGKEYWORD(Error) +LANGKEYWORD(toString) // extra -- Gitee From 725b731be32a12e3b3463f246a2d67f907c4c369 Mon Sep 17 00:00:00 2001 From: Wen HU Date: Wed, 12 Jan 2022 16:45:08 -0500 Subject: [PATCH 3/7] add Date to supported_types.def --- src/MapleFE/shared/include/supported_types.def | 1 + 1 file changed, 1 insertion(+) diff --git a/src/MapleFE/shared/include/supported_types.def b/src/MapleFE/shared/include/supported_types.def index ff932b0691..b2f6159eb8 100644 --- a/src/MapleFE/shared/include/supported_types.def +++ b/src/MapleFE/shared/include/supported_types.def @@ -39,6 +39,7 @@ TYPE(Never) // First come from TS TYPE(Undefined) // First come from JS TYPE(Symbol) // First come from JS TYPE(Any) // First come from JS +TYPE(Date) TYPE(Array) TYPE(Object) -- Gitee From b7710507f83f25ccbb29adedc69a3c5f20ea5778 Mon Sep 17 00:00:00 2001 From: yehandong Date: Thu, 13 Jan 2022 11:42:34 -0800 Subject: [PATCH 4/7] support 'import' as function property name. --- .../test/typescript/unit_tests/import-as-prop-name.ts.result | 5 +++++ src/MapleFE/typescript/stmt.spec | 1 + 2 files changed, 6 insertions(+) create mode 100644 src/MapleFE/test/typescript/unit_tests/import-as-prop-name.ts.result diff --git a/src/MapleFE/test/typescript/unit_tests/import-as-prop-name.ts.result b/src/MapleFE/test/typescript/unit_tests/import-as-prop-name.ts.result new file mode 100644 index 0000000000..3cb7c20598 --- /dev/null +++ b/src/MapleFE/test/typescript/unit_tests/import-as-prop-name.ts.result @@ -0,0 +1,5 @@ +Matched 22 tokens. +============= Module =========== +== Sub Tree == +declare ts_interface: Load {func import(n,o?) throws: + } diff --git a/src/MapleFE/typescript/stmt.spec b/src/MapleFE/typescript/stmt.spec index fcfb2f362b..cc03abd99c 100644 --- a/src/MapleFE/typescript/stmt.spec +++ b/src/MapleFE/typescript/stmt.spec @@ -1992,6 +1992,7 @@ rule IndexSignature: ONEOF( rule KeywordMethodName : ONEOF("return", "throw", "continue", + "import", "export") attr.action : BuildIdentifier() -- Gitee From b1f17b16be7626a96448ccd2df49e9499312db74 Mon Sep 17 00:00:00 2001 From: Wen HU Date: Thu, 13 Jan 2022 14:39:20 -0500 Subject: [PATCH 5/7] unify handling of type parameter nodes in different cases --- src/MapleFE/astopt/include/ast_scp.h | 1 + src/MapleFE/astopt/src/ast_scp.cpp | 33 ++++++++++++++-------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/MapleFE/astopt/include/ast_scp.h b/src/MapleFE/astopt/include/ast_scp.h index 8de224d437..9ceb8f05f6 100644 --- a/src/MapleFE/astopt/include/ast_scp.h +++ b/src/MapleFE/astopt/include/ast_scp.h @@ -117,6 +117,7 @@ class BuildScopeVisitor : public BuildScopeBaseVisitor { ForLoopNode *VisitForLoopNode(ForLoopNode *node); FieldNode *VisitFieldNode(FieldNode *node); + TypeParameterNode *VisitTypeParameterNode(TypeParameterNode *node); // related node with scope : decl, type DeclNode *VisitDeclNode(DeclNode *node); diff --git a/src/MapleFE/astopt/src/ast_scp.cpp b/src/MapleFE/astopt/src/ast_scp.cpp index 4c101784c7..1a6ffa8e76 100644 --- a/src/MapleFE/astopt/src/ast_scp.cpp +++ b/src/MapleFE/astopt/src/ast_scp.cpp @@ -199,6 +199,8 @@ FunctionNode *BuildScopeVisitor::VisitFunctionNode(FunctionNode *node) { // function is a decl AddDecl(parent, node); ASTScope *scope = NewScope(parent, node); + mScopeStack.push(scope); + mUserScopeStack.push(scope); // add parameters as decl for(unsigned i = 0; i < node->GetParamsNum(); i++) { @@ -223,13 +225,7 @@ FunctionNode *BuildScopeVisitor::VisitFunctionNode(FunctionNode *node) { // add type parameter as decl if (it->IsTypeParameter()) { - TypeParameterNode *tpn = static_cast(it); - TreeNode *id = tpn->GetId(); - if (id->IsIdentifier()) { - AddDecl(scope, id); - } else { - NOTYETIMPL("function type parameter not identifier"); - } + VisitTreeNode(it); continue; } @@ -250,8 +246,6 @@ FunctionNode *BuildScopeVisitor::VisitFunctionNode(FunctionNode *node) { AddType(scope, it); } } - mScopeStack.push(scope); - mUserScopeStack.push(scope); BuildScopeBaseVisitor::VisitFunctionNode(node); mUserScopeStack.pop(); mScopeStack.pop(); @@ -344,6 +338,7 @@ StructNode *BuildScopeVisitor::VisitStructNode(StructNode *node) { } ASTScope *scope = NewScope(parent, node); + mScopeStack.push(scope); if (node->GetStructId() && node->GetStructId()->GetStrIdx()) { mStrIdx2ScopeMap[node->GetStructId()->GetStrIdx()] = scope; } @@ -361,13 +356,7 @@ StructNode *BuildScopeVisitor::VisitStructNode(StructNode *node) { // add type parameter as decl for(unsigned i = 0; i < node->GetTypeParamsNum(); i++) { - TypeParameterNode *tpn = node->GetTypeParamAtIndex(i); - TreeNode *id = tpn->GetId(); - if (id->IsIdentifier()) { - AddDecl(scope, id); - } else { - NOTYETIMPL("function type parameter not identifier"); - } + VisitTreeNode(node->GetTypeParamAtIndex(i)); continue; } @@ -381,7 +370,6 @@ StructNode *BuildScopeVisitor::VisitStructNode(StructNode *node) { } } - mScopeStack.push(scope); BuildScopeBaseVisitor::VisitStructNode(node); mScopeStack.pop(); return node; @@ -566,6 +554,17 @@ FieldNode *BuildScopeVisitor::VisitFieldNode(FieldNode *node) { return node; } +TypeParameterNode *BuildScopeVisitor::VisitTypeParameterNode(TypeParameterNode *node) { + BuildScopeBaseVisitor::VisitTypeParameterNode(node); + TreeNode *id = node->GetId(); + if (id && id->IsIdentifier()) { + ASTScope *scope = mScopeStack.top(); + id->SetScope(scope); + AddDecl(scope, id); + } + return node; +} + ImportNode *BuildScopeVisitor::VisitImportNode(ImportNode *node) { (void) AstVisitor::VisitImportNode(node); ASTScope *scope = mScopeStack.top(); -- Gitee From 7e95b5ed41891e5dc1536b6e3b5a34116d1e2fa2 Mon Sep 17 00:00:00 2001 From: yehandong Date: Thu, 13 Jan 2022 12:09:35 -0800 Subject: [PATCH 6/7] Support special import(..).call() statement. --- .../test/typescript/unit_tests/import-promise.ts.result | 5 +++++ src/MapleFE/typescript/stmt.spec | 4 ++++ 2 files changed, 9 insertions(+) create mode 100644 src/MapleFE/test/typescript/unit_tests/import-promise.ts.result diff --git a/src/MapleFE/test/typescript/unit_tests/import-promise.ts.result b/src/MapleFE/test/typescript/unit_tests/import-promise.ts.result new file mode 100644 index 0000000000..e9813196aa --- /dev/null +++ b/src/MapleFE/test/typescript/unit_tests/import-promise.ts.result @@ -0,0 +1,5 @@ +Matched 20 tokens. +============= Module =========== +== Sub Tree == +import "./M".then(() -> console.log("Completed") +) diff --git a/src/MapleFE/typescript/stmt.spec b/src/MapleFE/typescript/stmt.spec index cc03abd99c..f9e0b2c4b5 100644 --- a/src/MapleFE/typescript/stmt.spec +++ b/src/MapleFE/typescript/stmt.spec @@ -801,6 +801,9 @@ rule Expression : ONEOF( ## TryStatement[?Yield, ?Return] ## DebuggerStatement +rule SpecialStatement : ONEOF( ImportedType + '.' + Expression + ASI(';') ) + attr.action.%1 : BuildField(%1, %3) + rule Statement : ONEOF( BlockStatement, VariableStatement, @@ -815,6 +818,7 @@ rule Statement : ONEOF( LabelledStatement, ThrowStatement, TryStatement, + SpecialStatement, ## I suppose to include CallExpression in ExpressionStatement, but due to the ## complexity of auto-insert-semicolon of TS, I put CallExpression here. CallExpression) -- Gitee From 2bb082b75e1b33539c829f57868e6dbca0ac0b6a Mon Sep 17 00:00:00 2001 From: yehandong Date: Thu, 13 Jan 2022 12:16:48 -0800 Subject: [PATCH 7/7] support export xxx as import. --- .../unit_tests/export-func-as-import.ts.result | 10 ++++++++++ src/MapleFE/typescript/stmt.spec | 8 ++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 src/MapleFE/test/typescript/unit_tests/export-func-as-import.ts.result diff --git a/src/MapleFE/test/typescript/unit_tests/export-func-as-import.ts.result b/src/MapleFE/test/typescript/unit_tests/export-func-as-import.ts.result new file mode 100644 index 0000000000..c42ee98681 --- /dev/null +++ b/src/MapleFE/test/typescript/unit_tests/export-func-as-import.ts.result @@ -0,0 +1,10 @@ +Matched 41 tokens. +Matched 48 tokens. +============= Module =========== +== Sub Tree == +func func(n,o?) throws: + return new Promise((resolve,reject) -> resolve("OK") +) + +== Sub Tree == +export {func as import} diff --git a/src/MapleFE/typescript/stmt.spec b/src/MapleFE/typescript/stmt.spec index f9e0b2c4b5..a1e3b72575 100644 --- a/src/MapleFE/typescript/stmt.spec +++ b/src/MapleFE/typescript/stmt.spec @@ -1635,13 +1635,17 @@ rule ExportsList : ONEOF(ExportSpecifier, ## ExportSpecifier : ## IdentifierName ## IdentifierName as IdentifierName +rule KeywordExportName : ONEOF("import") + attr.action : BuildIdentifier() + rule ExportSpecifier : ONEOF(JSIdentifier, JSIdentifier + "as" + BindingIdentifier, JSIdentifier + "as" + "default", "default" + "as" + JSIdentifier, JSIdentifier + "as" + "super", - JSIdentifier + "as" + "function") - attr.action.%2,%5,%6 : BuildXXportAsPair(%1, %3) + JSIdentifier + "as" + "function", + JSIdentifier + "as" + KeywordExportName) + attr.action.%2,%5,%6,%7 : BuildXXportAsPair(%1, %3) attr.action.%3 : BuildXXportAsPairDefault(%1) attr.action.%4 : BuildXXportAsPairDefault(%3) -- Gitee