From 84b29f925e87681bc0cf3e60eda311e9d4f020c9 Mon Sep 17 00:00:00 2001 From: yehandong Date: Tue, 21 Dec 2021 15:07:22 -0800 Subject: [PATCH 01/16] Mark the first and last token in a line. --- src/MapleFE/shared/include/lexer.h | 1 + src/MapleFE/shared/src/parser.cpp | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/MapleFE/shared/include/lexer.h b/src/MapleFE/shared/include/lexer.h index 863e630e95..292cb2a97c 100644 --- a/src/MapleFE/shared/include/lexer.h +++ b/src/MapleFE/shared/include/lexer.h @@ -76,6 +76,7 @@ public: } void SetTrace() {mTrace = true;} + bool GetTrace() {return mTrace;} void SetLineMode() {mLineMode = true;} void ResetLineMode(){mLineMode = false;} diff --git a/src/MapleFE/shared/src/parser.cpp b/src/MapleFE/shared/src/parser.cpp index fc499967ef..e392c48851 100644 --- a/src/MapleFE/shared/src/parser.cpp +++ b/src/MapleFE/shared/src/parser.cpp @@ -393,6 +393,9 @@ unsigned Parser::LexOneLine() { unsigned token_num = 0; Token *t = NULL; + Token *last_token = NULL; + bool line_begin = true; + // Check if there are already pending tokens. if (mCurToken < mActiveTokens.GetNum()) return mActiveTokens.GetNum() - mCurToken; @@ -425,7 +428,14 @@ unsigned Parser::LexOneLine() { // 3. handle regular expression t = GetRegExpr(t); + if (line_begin) { + t->mLineBegin = true; + line_begin = false; + if (mLexer->GetTrace()) + DUMP0("Set as Line First."); + } mActiveTokens.PushBack(t); + last_token = t; token_num++; } } else { @@ -442,6 +452,13 @@ unsigned Parser::LexOneLine() { } } + // We are done with a meaningful line + if (token_num) { + last_token->mLineEnd = true; + if (mLexer->GetTrace()) + DUMP0("Set as Line End."); + } + return token_num; } -- Gitee From 7fcb780de4573baed1ddde09e1bb0ecef74dd25d Mon Sep 17 00:00:00 2001 From: yehandong Date: Tue, 21 Dec 2021 18:12:59 -0800 Subject: [PATCH 02/16] Update TraverseASI. --- src/MapleFE/shared/include/parser.h | 2 +- src/MapleFE/shared/src/parser.cpp | 10 ++++++++-- src/MapleFE/typescript/include/lang_spec.h | 2 +- src/MapleFE/typescript/src/lang_spec.cpp | 11 ++++++++--- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/MapleFE/shared/include/parser.h b/src/MapleFE/shared/include/parser.h index 1669e43d52..a7384f9d07 100644 --- a/src/MapleFE/shared/include/parser.h +++ b/src/MapleFE/shared/include/parser.h @@ -293,7 +293,7 @@ public: bool TraverseOneof(RuleTable*, AppealNode*); bool TraverseZeroormore(RuleTable*, AppealNode*); bool TraverseZeroorone(RuleTable*, AppealNode*); - virtual bool TraverseASI(RuleTable*, AppealNode*) {return false;} + virtual bool TraverseASI(RuleTable*, AppealNode*, AppealNode *&) {return false;} // There are some special cases we can speed up the traversal. // 1. If the target is a token, we just need compare mCurToken with it. diff --git a/src/MapleFE/shared/src/parser.cpp b/src/MapleFE/shared/src/parser.cpp index e392c48851..658bf248e2 100644 --- a/src/MapleFE/shared/src/parser.cpp +++ b/src/MapleFE/shared/src/parser.cpp @@ -956,6 +956,10 @@ bool Parser::TraverseRuleTable(RuleTable *rule_table, AppealNode *parent, Appeal DumpEnterTable(name, mIndentation); } + if (rule_table->mType == ET_ASI) { + return TraverseASI(rule_table, parent, child); + } + // Lookahead fail is fast to check, even faster than check WasFailed. if (LookAheadFail(rule_table, mCurToken) && (rule_table->mType != ET_Zeroormore) && @@ -1242,9 +1246,11 @@ bool Parser::TraverseRuleTableRegular(RuleTable *rule_table, AppealNode *appeal) case ET_Concatenate: matched = TraverseConcatenate(rule_table, appeal); break; - case ET_ASI: - matched = TraverseASI(rule_table, appeal); + case ET_ASI: { + AppealNode *child = NULL; + matched = TraverseASI(rule_table, appeal, child); break; + } case ET_Data: { // This is a rare case where a rule table contains only table, either a token // or a single child rule. In this case, we need merge the child's match into diff --git a/src/MapleFE/typescript/include/lang_spec.h b/src/MapleFE/typescript/include/lang_spec.h index aa302bfdef..f0ec422f1a 100644 --- a/src/MapleFE/typescript/include/lang_spec.h +++ b/src/MapleFE/typescript/include/lang_spec.h @@ -58,7 +58,7 @@ class TypescriptParser : public Parser { public: TypescriptParser(const char *f) : Parser(f) {} Token* GetRegExpr(Token *t); - bool TraverseASI(RuleTable*, AppealNode*); + bool TraverseASI(RuleTable*, AppealNode*, AppealNode *&); }; } diff --git a/src/MapleFE/typescript/src/lang_spec.cpp b/src/MapleFE/typescript/src/lang_spec.cpp index d73777758c..9a1b3c0412 100644 --- a/src/MapleFE/typescript/src/lang_spec.cpp +++ b/src/MapleFE/typescript/src/lang_spec.cpp @@ -418,13 +418,18 @@ Token* TypescriptParser::GetRegExpr(Token *t) { // 'appeal' is the node of 'rule_table'. -bool TypescriptParser::TraverseASI(RuleTable *rule_table, AppealNode *appeal) { +// 'child' was NULL when passed in. +bool TypescriptParser::TraverseASI(RuleTable *rule_table, + AppealNode *appeal, + AppealNode *&child) { MASSERT((rule_table->mNum == 1) && "ASI node has more than one elements?"); TableData *data = rule_table->mData; - AppealNode *child = NULL; bool found = TraverseTableData(data, appeal, child); - if (child) + if (child) { + child->SetChildIndex(0); appeal->CopyMatch(child); + } + return found; } -- Gitee From f2036a85ba925914ace7c0fe82f748f236bfa8c0 Mon Sep 17 00:00:00 2001 From: yehandong Date: Tue, 21 Dec 2021 18:48:17 -0800 Subject: [PATCH 03/16] Update the raw version of TraverseASI --- src/MapleFE/shared/include/parser.h | 2 +- src/MapleFE/typescript/src/lang_spec.cpp | 33 +++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/MapleFE/shared/include/parser.h b/src/MapleFE/shared/include/parser.h index a7384f9d07..9c0751d78e 100644 --- a/src/MapleFE/shared/include/parser.h +++ b/src/MapleFE/shared/include/parser.h @@ -227,7 +227,7 @@ struct RecStackEntry { //////////////////////////////////////////////////////////////////////////// class Parser { -private: +protected: friend class RecursionTraversal; // Matching on alternative tokens needs a state machine. diff --git a/src/MapleFE/typescript/src/lang_spec.cpp b/src/MapleFE/typescript/src/lang_spec.cpp index 9a1b3c0412..764a0f0149 100644 --- a/src/MapleFE/typescript/src/lang_spec.cpp +++ b/src/MapleFE/typescript/src/lang_spec.cpp @@ -422,9 +422,40 @@ Token* TypescriptParser::GetRegExpr(Token *t) { bool TypescriptParser::TraverseASI(RuleTable *rule_table, AppealNode *appeal, AppealNode *&child) { + // Usually mCurToken is a new token to be matched. So if it's end of file, we simply return false. + // However, (1) if mCurToken is actually an ATMToken, which means it needs to be matched + // multiple times, we are NOT at the end yet. + // (2) If we are traverse a Concatenate rule, and the previous sub-rule has multiple matches, + // and we are trying the current sub-rule, ie. 'data', using one of the matches. + // The lexer actually reaches the EndOfFile in previous matchings, but the mCurToken + // we are working on right now is not the last token. It's one of the previous matches. + // So we need check if we are matching the last token. + if (mEndOfFile && mCurToken >= mActiveTokens.GetNum()) { + if (!(mInAltTokensMatching && (mCurToken == mATMToken))) + return false; + } + + unsigned old_pos = mCurToken; + bool found = false; + Token *curr_token = GetActiveToken(mCurToken); + Token *prev_token = GetActiveToken(mCurToken - 1); + MASSERT((rule_table->mNum == 1) && "ASI node has more than one elements?"); + TableData *data = rule_table->mData; - bool found = TraverseTableData(data, appeal, child); + MASSERT(data->mType == DT_Token && "ASI data is not a token?"); + + Token *semicolon = &gSystemTokens[data->mData.mTokenId]; + MASSERT(semicolon->IsSeparator()); + MASSERT(semicolon->GetSepId() == SEP_Semicolon); + + if (curr_token == semicolon) { + // To simplify the code, I reused TraverseToken(). + found = TraverseToken(semicolon, appeal, child); + } else { + found = TraverseTableData(data, appeal, child); + } + if (child) { child->SetChildIndex(0); appeal->CopyMatch(child); -- Gitee From 3c2ca74aeb0b26291a06efdeaf00a116b60d49cf Mon Sep 17 00:00:00 2001 From: yehandong Date: Tue, 21 Dec 2021 23:26:03 -0800 Subject: [PATCH 04/16] ASI is working now. --- src/MapleFE/shared/include/parser.h | 3 + src/MapleFE/shared/src/parser.cpp | 61 ++++++++++++++----- .../unit_tests/semicolon-missing12.ts.result | 7 +++ src/MapleFE/typescript/src/lang_spec.cpp | 27 ++++++-- 4 files changed, 79 insertions(+), 19 deletions(-) create mode 100644 src/MapleFE/test/typescript/unit_tests/semicolon-missing12.ts.result diff --git a/src/MapleFE/shared/include/parser.h b/src/MapleFE/shared/include/parser.h index 9c0751d78e..14abcafe61 100644 --- a/src/MapleFE/shared/include/parser.h +++ b/src/MapleFE/shared/include/parser.h @@ -49,6 +49,7 @@ typedef enum AppealStatus { FailChildrenFailed, Fail2ndOf1st, FailLookAhead, + FailASI, // Succ : Really does the matching, will be saved in SuccMatch // SuccWasSucc : Was matched, not tried traversal for a second timewill, @@ -57,9 +58,11 @@ typedef enum AppealStatus { // in RecurionNodes where it does multiple instances of // traversal. But it doesn't make any change compared // to the last real Succ. It will NOT be saved in SuccMatch + // SuccASI: TS/JS auto-semicolon-insert Succ, SuccWasSucc, SuccStillWasSucc, + SuccASI, AppealStatus_NA }AppealStatus; diff --git a/src/MapleFE/shared/src/parser.cpp b/src/MapleFE/shared/src/parser.cpp index 658bf248e2..19a3da4f2d 100644 --- a/src/MapleFE/shared/src/parser.cpp +++ b/src/MapleFE/shared/src/parser.cpp @@ -706,8 +706,9 @@ bool Parser::TraverseTempLiteral() { AppealNode *child = NULL; succ_expr = TraverseRuleTable(t, mRootNode, child); if (succ_expr) { - MASSERT(child); - mRootNode->CopyMatch(child); + MASSERT(child || t->mType == ET_ASI); + if (child) + mRootNode->CopyMatch(child); // Need adjust the mCurToken. A rule could try multiple possible // children rules, although there is one and only one valid child // for a Top table. However, the mCurToken could deviate from @@ -724,8 +725,9 @@ bool Parser::TraverseTempLiteral() { child = NULL; succ_type = TraverseRuleTable(t, mRootNode, child); if (succ_type) { - MASSERT(child); - mRootNode->CopyMatch(child); + MASSERT(child || t->mType == ET_ASI); + if (child) + mRootNode->CopyMatch(child); // Need adjust the mCurToken. A rule could try multiple possible // children rules, although there is one and only one valid child // for a Top table. However, the mCurToken could deviate from @@ -766,8 +768,9 @@ bool Parser::TraverseStmt() { AppealNode *child = NULL; succ = TraverseRuleTable(t, mRootNode, child); if (succ) { - MASSERT(child); - mRootNode->CopyMatch(child); + MASSERT(child || t->mType == ET_ASI); + if (child) + mRootNode->CopyMatch(child); // Need adjust the mCurToken. A rule could try multiple possible // children rules, although there is one and only one valid child // for a Top table. However, the mCurToken could deviate from @@ -834,6 +837,9 @@ void Parser::DumpExitTable(const char *table_name, unsigned indent, std::cout << " succ" << "}"; DumpSuccTokens(appeal); std::cout << std::endl; + } else if (reason == SuccASI) { + std::cout << " succASI" << "}"; + std::cout << std::endl; } else if (reason == FailWasFailed) std::cout << " fail@WasFailed" << "}" << std::endl; else if (reason == FailNotRightToken) @@ -850,6 +856,8 @@ void Parser::DumpExitTable(const char *table_name, unsigned indent, std::cout << " fail@2ndOf1st" << "}" << std::endl; else if (reason == FailLookAhead) std::cout << " fail@LookAhead" << "}" << std::endl; + else if (reason == FailASI) + std::cout << " fail@ASI" << "}" << std::endl; else if (reason == AppealStatus_NA) std::cout << " fail@NA" << "}" << std::endl; } @@ -957,7 +965,15 @@ bool Parser::TraverseRuleTable(RuleTable *rule_table, AppealNode *parent, Appeal } if (rule_table->mType == ET_ASI) { - return TraverseASI(rule_table, parent, child); + bool found = TraverseASI(rule_table, parent, child); + if (mTraceTable) { + if (found) + DumpExitTable(name, mIndentation, SuccASI); + else + DumpExitTable(name, mIndentation, FailASI); + } + mIndentation -= 2; + return found; } // Lookahead fail is fast to check, even faster than check WasFailed. @@ -1691,6 +1707,7 @@ bool Parser::TraverseConcatenate(RuleTable *rule_table, AppealNode *appeal) { for (unsigned i = 0; i < rule_table->mNum; i++) { bool is_zeroxxx = false; + bool is_asi = false; bool is_token = false; bool old_mInAltTokensMatching = mInAltTokensMatching; @@ -1699,6 +1716,8 @@ bool Parser::TraverseConcatenate(RuleTable *rule_table, AppealNode *appeal) { RuleTable *zero_rt = data->mData.mEntry; if (zero_rt->mType == ET_Zeroormore || zero_rt->mType == ET_Zeroorone) is_zeroxxx = true; + if (zero_rt->mType == ET_ASI) + is_asi = true; } else if (data->mType == DT_Token) { is_token = true; } @@ -1724,11 +1743,17 @@ bool Parser::TraverseConcatenate(RuleTable *rule_table, AppealNode *appeal) { child->SetChildIndex(i); found_subtable |= temp_found; - if (temp_found && child) { - for (unsigned id = 0; id < child->GetMatchNum(); id++) { - unsigned match = child->GetMatch(id); - if (!subtable_succ_tokens.Find(match)) - subtable_succ_tokens.PushBack(match); + if (temp_found ) { + if (child) { + for (unsigned id = 0; id < child->GetMatchNum(); id++) { + unsigned match = child->GetMatch(id); + if (!subtable_succ_tokens.Find(match)) + subtable_succ_tokens.PushBack(match); + } + } else if (is_asi) { + // ASI succeeded, without child. It means semicolon is skipped. + // Keep prev. NO moving mCurToken. + subtable_succ_tokens.PushBack(prev); } } } @@ -1803,8 +1828,14 @@ bool Parser::TraverseTableData(TableData *data, AppealNode *appeal, AppealNode * // we are working on right now is not the last token. It's one of the previous matches. // So we need check if we are matching the last token. if (mEndOfFile && mCurToken >= mActiveTokens.GetNum()) { - if (!(mInAltTokensMatching && (mCurToken == mATMToken))) + if (!(mInAltTokensMatching && (mCurToken == mATMToken))) { + if (data->mType == DT_Subtable) { + RuleTable *t = data->mData.mEntry; + if (t->mType == ET_ASI) + return TraverseASI(t, appeal, child_node); + } return false; + } } unsigned old_pos = mCurToken; @@ -2245,13 +2276,15 @@ void Parser::SortOutConcatenate(AppealNode *parent) { for (int i = rule_table->mNum - 1; i >= 0; i--) { TableData *data = rule_table->mData + i; AppealNode *child = parent->FindIndexedChild(last_match, i); - // It's possible that we find NO child if 'data' is a ZEROORxxx table + // It's possible that we find NO child if 'data' is a ZEROORxxx table or ASI. bool good_child = false; if (!child) { if (data->mType == DT_Subtable) { RuleTable *table = data->mData.mEntry; if (table->mType == ET_Zeroorone || table->mType == ET_Zeroormore) good_child = true; + if (table->mType == ET_ASI) + good_child = true; } MASSERT(good_child); } else { diff --git a/src/MapleFE/test/typescript/unit_tests/semicolon-missing12.ts.result b/src/MapleFE/test/typescript/unit_tests/semicolon-missing12.ts.result new file mode 100644 index 0000000000..c499b0516d --- /dev/null +++ b/src/MapleFE/test/typescript/unit_tests/semicolon-missing12.ts.result @@ -0,0 +1,7 @@ +Matched 6 tokens. +Matched 9 tokens. +============= Module =========== +== Sub Tree == +console.log("Hello") +== Sub Tree == +console.log diff --git a/src/MapleFE/typescript/src/lang_spec.cpp b/src/MapleFE/typescript/src/lang_spec.cpp index 764a0f0149..7b5f1f12a7 100644 --- a/src/MapleFE/typescript/src/lang_spec.cpp +++ b/src/MapleFE/typescript/src/lang_spec.cpp @@ -430,10 +430,16 @@ bool TypescriptParser::TraverseASI(RuleTable *rule_table, // The lexer actually reaches the EndOfFile in previous matchings, but the mCurToken // we are working on right now is not the last token. It's one of the previous matches. // So we need check if we are matching the last token. - if (mEndOfFile && mCurToken >= mActiveTokens.GetNum()) { - if (!(mInAltTokensMatching && (mCurToken == mATMToken))) - return false; - } + //if (mEndOfFile && mCurToken >= mActiveTokens.GetNum()) { + // if (!(mInAltTokensMatching && (mCurToken == mATMToken))) + // return false; + //} + + if (mCurToken <= 1) + return false; + + if (mEndOfFile && mCurToken == mActiveTokens.GetNum()) + return true; unsigned old_pos = mCurToken; bool found = false; @@ -453,7 +459,18 @@ bool TypescriptParser::TraverseASI(RuleTable *rule_table, // To simplify the code, I reused TraverseToken(). found = TraverseToken(semicolon, appeal, child); } else { - found = TraverseTableData(data, appeal, child); + if (curr_token->mLineBegin && + prev_token->mLineEnd && + prev_token->IsSeparator()){ + if (prev_token->GetSepId() == SEP_Rbrace || + prev_token->GetSepId() == SEP_Rbrack || + prev_token->GetSepId() == SEP_Rparen) { + if (mTraceTable) { + std::cout << "TraverseASI, Auto-insert one semicolon." << std::endl; + } + return true; + } + } } if (child) { -- Gitee From d56dcd95733cc53704f7bec61c9d27cfda586a80 Mon Sep 17 00:00:00 2001 From: Yan Zhang Date: Wed, 22 Dec 2021 10:37:26 -0800 Subject: [PATCH 05/16] Add a test case with class decorator --- .../test/typescript/unit_tests/class-deco3.ts | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/MapleFE/test/typescript/unit_tests/class-deco3.ts diff --git a/src/MapleFE/test/typescript/unit_tests/class-deco3.ts b/src/MapleFE/test/typescript/unit_tests/class-deco3.ts new file mode 100644 index 0000000000..9e599b2155 --- /dev/null +++ b/src/MapleFE/test/typescript/unit_tests/class-deco3.ts @@ -0,0 +1,29 @@ +function class_deco(name: string): Function { + function deco(ctor: Function): void { + console.log("Class constructor is :", ctor, ", Name is: ", name); + } + return deco; +} + +@class_deco('Klass') +class Klass { + data: any = null; + public setData(value: any) { + this.data= [ + { + n: value, + }, + ]; + } + + public dump (value: number) { + switch (value) { + case 1: + console.log(value, this.data); + } + } +} + +let obj: Klass = new Klass(); +obj.setData(123); +obj.dump(1); -- Gitee From 293b603fdbdd936c2cf04d0d0013fa25f572a138 Mon Sep 17 00:00:00 2001 From: Wen HU Date: Wed, 22 Dec 2021 14:28:02 -0500 Subject: [PATCH 06/16] handle field decl for StrIndexSigNode in classes etc --- src/MapleFE/astopt/src/ast_scp.cpp | 32 +++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/MapleFE/astopt/src/ast_scp.cpp b/src/MapleFE/astopt/src/ast_scp.cpp index 1f49b955d1..fef5c1362a 100644 --- a/src/MapleFE/astopt/src/ast_scp.cpp +++ b/src/MapleFE/astopt/src/ast_scp.cpp @@ -278,9 +278,15 @@ ClassNode *BuildScopeVisitor::VisitClassNode(ClassNode *node) { // add fields as decl for(unsigned i = 0; i < node->GetFieldsNum(); i++) { - TreeNode *it = node->GetField(i); - if (it->IsIdentifier()) { - AddDecl(scope, it); + TreeNode *fld = node->GetField(i); + if (fld->IsStrIndexSig()) { + StrIndexSigNode *sn = static_cast(fld); + fld = sn->GetKey(); + } + if (fld->IsIdentifier()) { + AddDecl(scope, fld); + } else { + NOTYETIMPL("new type of class field"); } } mScopeStack.push(scope); @@ -304,9 +310,13 @@ InterfaceNode *BuildScopeVisitor::VisitInterfaceNode(InterfaceNode *node) { // add fields as decl for(unsigned i = 0; i < node->GetFieldsNum(); i++) { - TreeNode *it = node->GetField(i); - if (it->IsIdentifier()) { - AddDecl(scope, it); + TreeNode *fld = node->GetField(i); + if (fld->IsStrIndexSig()) { + StrIndexSigNode *sn = static_cast(fld); + fld = sn->GetKey(); + } + if (fld->IsIdentifier()) { + AddDecl(scope, fld); } } mScopeStack.push(scope); @@ -330,9 +340,13 @@ StructNode *BuildScopeVisitor::VisitStructNode(StructNode *node) { // add fields as decl for(unsigned i = 0; i < node->GetFieldsNum(); i++) { - TreeNode *it = node->GetField(i); - if (it->IsIdentifier()) { - AddDecl(scope, it); + TreeNode *fld = node->GetField(i); + if (fld->IsStrIndexSig()) { + StrIndexSigNode *sn = static_cast(fld); + fld = sn->GetKey(); + } + if (fld->IsIdentifier()) { + AddDecl(scope, fld); } } mScopeStack.push(scope); -- Gitee From c7531381914cf279165a6652bd2c098164722232 Mon Sep 17 00:00:00 2001 From: Wen HU Date: Wed, 22 Dec 2021 15:08:00 -0500 Subject: [PATCH 07/16] add dummy decl for some keywords --- src/MapleFE/astopt/src/ast_scp.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/MapleFE/astopt/src/ast_scp.cpp b/src/MapleFE/astopt/src/ast_scp.cpp index fef5c1362a..f16445f1e7 100644 --- a/src/MapleFE/astopt/src/ast_scp.cpp +++ b/src/MapleFE/astopt/src/ast_scp.cpp @@ -120,6 +120,13 @@ void BuildScopeVisitor::AddTypeAndDecl(ASTScope *scope, TreeNode *node) { AddDecl(scope, node); } +#define ADD_DECL(K) {\ + TreeNode *node = mHandler->NewTreeNode(); \ + unsigned idx = gStringPool.GetStrIdx(K); \ + node->SetStrIdx(idx); \ + AddDecl(scope, node); \ +} + void BuildScopeVisitor::InitInternalTypes() { // add primitive and builtin types to root scope ModuleNode *module = mHandler->GetASTModule(); @@ -145,6 +152,12 @@ void BuildScopeVisitor::InitInternalTypes() { console->AddMethod(log); log->SetScope(scp); AddDecl(scp, log); + + // add dummy decl for some keywords + ADD_DECL("undefined"); + ADD_DECL("null"); + ADD_DECL("Error"); + ADD_DECL("NonNullable"); } ClassNode *BuildScopeVisitor::AddClass(std::string name, unsigned tyidx) { -- Gitee From c345dc8157ceaee112071538490ab4ae060de0dd Mon Sep 17 00:00:00 2001 From: Wen HU Date: Wed, 22 Dec 2021 15:08:27 -0500 Subject: [PATCH 08/16] add function type parameter as decl --- src/MapleFE/astopt/src/ast_scp.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/MapleFE/astopt/src/ast_scp.cpp b/src/MapleFE/astopt/src/ast_scp.cpp index f16445f1e7..d4a566be92 100644 --- a/src/MapleFE/astopt/src/ast_scp.cpp +++ b/src/MapleFE/astopt/src/ast_scp.cpp @@ -234,6 +234,18 @@ FunctionNode *BuildScopeVisitor::VisitFunctionNode(FunctionNode *node) { for(unsigned i = 0; i < node->GetTypeParamsNum(); i++) { TreeNode *it = node->GetTypeParamAtIndex(i); + // 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"); + } + continue; + } + // add it to scope's mTypes only if it is a new type TreeNode *tn = it; if (it->IsUserType()) { -- Gitee From 674e67a54a309e3796243d3dfd25747e819f5aaf Mon Sep 17 00:00:00 2001 From: Yan Zhang Date: Thu, 23 Dec 2021 09:05:09 -0800 Subject: [PATCH 09/16] Add a test case with 'new Array()' in constructor --- .../typescript/unit_tests/array-in-ctor.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/MapleFE/test/typescript/unit_tests/array-in-ctor.ts diff --git a/src/MapleFE/test/typescript/unit_tests/array-in-ctor.ts b/src/MapleFE/test/typescript/unit_tests/array-in-ctor.ts new file mode 100644 index 0000000000..b07b07525b --- /dev/null +++ b/src/MapleFE/test/typescript/unit_tests/array-in-ctor.ts @@ -0,0 +1,19 @@ +class Klass { + data: any; + constructor() { + { + this.data = [ + new Array(123, 456) + ]; + } + } + public dump (value: number) { + switch (value) { + case 1: + console.log(value, this.data); + } + } +} + +let obj: Klass = new Klass(); +obj.dump(1); -- Gitee From 2f6468d6db1b548eedc360b72ce1a2351b93c8d3 Mon Sep 17 00:00:00 2001 From: Yan Zhang Date: Thu, 23 Dec 2021 09:46:57 -0800 Subject: [PATCH 10/16] Add a test case with class decorator --- .../test/typescript/unit_tests/class-deco4.ts | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/MapleFE/test/typescript/unit_tests/class-deco4.ts diff --git a/src/MapleFE/test/typescript/unit_tests/class-deco4.ts b/src/MapleFE/test/typescript/unit_tests/class-deco4.ts new file mode 100644 index 0000000000..063acd21db --- /dev/null +++ b/src/MapleFE/test/typescript/unit_tests/class-deco4.ts @@ -0,0 +1,22 @@ +function class_deco(name: string): Function { + function deco(ctor: Function): void { + console.log("Class constructor is :", ctor, ", Name is: ", name); + } + return deco; +} + +@class_deco('Klass') +class Klass { + data: + {} = {}; + + public dump (value: number) { + switch (value) { + case 1: + console.log(value, this.data); + } + } +} + +let obj: Klass = new Klass(); +obj.dump(1); -- Gitee From 7cb94a443ccb5d3fe0028c734ef5b07d2a6b3a9f Mon Sep 17 00:00:00 2001 From: Wen HU Date: Thu, 23 Dec 2021 09:43:08 -0500 Subject: [PATCH 11/16] handle cases where no deep search found anything new --- src/MapleFE/astopt/src/ast_handler.cpp | 3 +-- src/MapleFE/astopt/src/ast_xxport.cpp | 10 +++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/MapleFE/astopt/src/ast_handler.cpp b/src/MapleFE/astopt/src/ast_handler.cpp index 35dc32a627..4d647bbdee 100644 --- a/src/MapleFE/astopt/src/ast_handler.cpp +++ b/src/MapleFE/astopt/src/ast_handler.cpp @@ -195,8 +195,7 @@ TreeNode *Module_Handler::FindDecl(IdentifierNode *node, bool deep) { mNodeId2Decl[nid] = decl; } - if (deep && decl && !decl->IsTypeIdModule() && - GetASTXXport()->IsImportedDeclId(mHidx, decl->GetNodeId())) { + if (deep && decl && GetASTXXport()->IsImportedDeclId(mHidx, decl->GetNodeId())) { decl = GetASTXXport()->GetExportedNodeFromImportedNode(mHidx, decl->GetNodeId()); } diff --git a/src/MapleFE/astopt/src/ast_xxport.cpp b/src/MapleFE/astopt/src/ast_xxport.cpp index dc7a746d0b..448da00457 100644 --- a/src/MapleFE/astopt/src/ast_xxport.cpp +++ b/src/MapleFE/astopt/src/ast_xxport.cpp @@ -451,23 +451,23 @@ TreeNode *AST_XXport::GetExportedNamedNode(unsigned hidx, unsigned stridx) { // hidx is the index of handler, string is the string index of identifier TreeNode *AST_XXport::GetExportedNodeFromImportedNode(unsigned hidx, unsigned nid) { - TreeNode *node = NULL; + TreeNode *node = mAstOpt->GetNodeFromNodeId(nid); for (auto it : mImports[hidx]) { if (it->mDefaultNodeId == nid) { - TreeNode *node = GetExportedDefault(it->mModuleStrIdx); + node = GetExportedDefault(it->mModuleStrIdx); return node; } for (auto it1 : it->mNodeIdPairs) { unsigned nid2 = it1.second; if (nid2 == nid) { unsigned nid1 = it1.first; - TreeNode *node1 = mAstOpt->GetNodeFromNodeId(nid1); - return node1; + node = mAstOpt->GetNodeFromNodeId(nid1); + return node; } } } - return NULL; + return node; } ImportNode *XXportBasicVisitor::VisitImportNode(ImportNode *node) { -- Gitee From 1d51d2e9052fd24141b26258d647fd4c05532d99 Mon Sep 17 00:00:00 2001 From: Wen HU Date: Thu, 23 Dec 2021 15:24:34 -0500 Subject: [PATCH 12/16] refine exported node to extract identifiers; fix typeidx for import/export nodes --- src/MapleFE/astopt/include/ast_ti.h | 6 ++- src/MapleFE/astopt/include/ast_xxport.h | 3 ++ src/MapleFE/astopt/src/ast_ti.cpp | 27 +++++++------ src/MapleFE/astopt/src/ast_xxport.cpp | 52 +++++++++++++++++++------ 4 files changed, 65 insertions(+), 23 deletions(-) diff --git a/src/MapleFE/astopt/include/ast_ti.h b/src/MapleFE/astopt/include/ast_ti.h index 4e7897d345..e56b2a035e 100644 --- a/src/MapleFE/astopt/include/ast_ti.h +++ b/src/MapleFE/astopt/include/ast_ti.h @@ -136,11 +136,15 @@ class TypeInferVisitor : public TypeInferBaseVisitor { void SetUpdated(bool b = true) {mUpdated = b;} void SetTypeId(TreeNode *node, TypeId tid); - void SetTypeIdx(TreeNode *node, unsigned tidx); + void SetTypeId(TreeNode *node1, TreeNode *node2); void UpdateTypeId(TreeNode *node, TypeId tid); void UpdateTypeId(TreeNode *node1, TreeNode *node2); + + void SetTypeIdx(TreeNode *node, unsigned tidx); + void SetTypeIdx(TreeNode *node1, TreeNode *node2); void UpdateTypeIdx(TreeNode *node, unsigned tidx); void UpdateTypeIdx(TreeNode *node1, TreeNode *node2); + void UpdateFuncRetTypeId(FunctionNode *node, TypeId tid, unsigned tidx); void UpdateTypeUseNode(TreeNode *target, TreeNode *input); void UpdateArgArrayDecls(unsigned nid, TypeId tid); diff --git a/src/MapleFE/astopt/include/ast_xxport.h b/src/MapleFE/astopt/include/ast_xxport.h index 27b94f7529..bd5014f29a 100644 --- a/src/MapleFE/astopt/include/ast_xxport.h +++ b/src/MapleFE/astopt/include/ast_xxport.h @@ -139,6 +139,9 @@ class AST_XXport { // hidx is the handler index of node with index nid TreeNode *GetExportedNodeFromImportedNode(unsigned hidx, unsigned nid); + // get identifier from node + TreeNode *GetIdentifier(TreeNode *node); + void Dump(); }; diff --git a/src/MapleFE/astopt/src/ast_ti.cpp b/src/MapleFE/astopt/src/ast_ti.cpp index fa7b0c129c..89a1be3799 100644 --- a/src/MapleFE/astopt/src/ast_ti.cpp +++ b/src/MapleFE/astopt/src/ast_ti.cpp @@ -297,6 +297,15 @@ void TypeInferVisitor::SetTypeIdx(TreeNode *node, unsigned tidx) { } } +void TypeInferVisitor::SetTypeId(TreeNode *node1, TreeNode *node2) { + SetTypeId(node1, node2->GetTypeId()); + SetTypeIdx(node1, node2->GetTypeIdx()); +} + +void TypeInferVisitor::SetTypeIdx(TreeNode *node1, TreeNode *node2) { + SetTypeIdx(node1, node2->GetTypeIdx()); +} + void TypeInferVisitor::UpdateTypeId(TreeNode *node, TypeId tid) { if (tid == TY_None || !node || node->IsLiteral()) { return; @@ -1152,7 +1161,7 @@ DeclNode *TypeInferVisitor::VisitDeclNode(DeclNode *node) { } ImportNode *TypeInferVisitor::VisitImportNode(ImportNode *node) { - (void) AstVisitor::VisitImportNode(node); + //(void) AstVisitor::VisitImportNode(node); TreeNode *target = node->GetTarget(); unsigned hidx = DEFAULTVALUE; unsigned hstridx = 0; @@ -1203,8 +1212,7 @@ ImportNode *TypeInferVisitor::VisitImportNode(ImportNode *node) { } else { exported = mXXport->GetExportedNamedNode(hidx, bfnode->GetStrIdx()); if (exported) { - UpdateTypeId(bfnode, exported->GetTypeId()); - UpdateTypeIdx(bfnode, exported->GetTypeIdx()); + SetTypeId(bfnode, exported); } } if (!exported) { @@ -1212,11 +1220,9 @@ ImportNode *TypeInferVisitor::VisitImportNode(ImportNode *node) { } } - UpdateTypeId(p, bfnode->GetTypeId()); - UpdateTypeIdx(p, bfnode->GetTypeIdx()); + SetTypeId(p, bfnode); if (afnode) { - UpdateTypeId(afnode, bfnode->GetTypeId()); - UpdateTypeIdx(afnode, bfnode->GetTypeIdx()); + SetTypeId(afnode, bfnode); } } } @@ -1582,8 +1588,7 @@ TerOperatorNode *TypeInferVisitor::VisitTerOperatorNode(TerOperatorNode *node) { (void) VisitTreeNode(ta); (void) VisitTreeNode(tb); (void) VisitTreeNode(tc); - UpdateTypeId(node, tb->GetTypeId()); - UpdateTypeId(node, tc->GetTypeId()); + UpdateTypeId(node, tb); return node; } @@ -1598,8 +1603,8 @@ TypeAliasNode *TypeInferVisitor::VisitTypeAliasNode(TypeAliasNode *node) { UserTypeNode *id = node->GetId(); TreeNode *alias = node->GetAlias(); TypeId tid = alias->GetTypeId(); - UpdateTypeId(id, tid); - UpdateTypeId(node, tid); + unsigned tidx = alias->GetTypeIdx(); + UpdateTypeId(id, alias); return node; } diff --git a/src/MapleFE/astopt/src/ast_xxport.cpp b/src/MapleFE/astopt/src/ast_xxport.cpp index 448da00457..87290dbc2a 100644 --- a/src/MapleFE/astopt/src/ast_xxport.cpp +++ b/src/MapleFE/astopt/src/ast_xxport.cpp @@ -217,7 +217,8 @@ void AST_XXport::CollectImportInfo(unsigned hidx) { info->mNodeIdPairs.insert(pnid); TypeId tid = exported->GetTypeId(); bfnode->SetTypeId(tid); - bfnode->SetTypeIdx(tid); + unsigned tidx = exported->GetTypeIdx(); + bfnode->SetTypeIdx(tidx); } else { NOTYETIMPL("failed to find the exported - default"); } @@ -231,10 +232,11 @@ void AST_XXport::CollectImportInfo(unsigned hidx) { std::pair pnid(exported->GetNodeId(), afnode->GetNodeId()); info->mNodeIdPairs.insert(pnid); TypeId tid = exported->GetTypeId(); + unsigned tidx = exported->GetTypeIdx(); bfnode->SetTypeId(tid); - bfnode->SetTypeIdx(tid); + bfnode->SetTypeIdx(tidx); afnode->SetTypeId(tid); - afnode->SetTypeIdx(tid); + afnode->SetTypeIdx(tidx); } else if (bfnode) { // import bfnode TreeNode *exported = FindExportedDecl(targethidx, bfnode->GetStrIdx()); @@ -245,8 +247,9 @@ void AST_XXport::CollectImportInfo(unsigned hidx) { std::pair pnid(exported->GetNodeId(), bfnode->GetNodeId()); info->mNodeIdPairs.insert(pnid); TypeId tid = exported->GetTypeId(); + unsigned tidx = exported->GetTypeIdx(); bfnode->SetTypeId(tid); - bfnode->SetTypeIdx(tid); + bfnode->SetTypeIdx(tidx); } else { NOTYETIMPL("failed to find the exported"); } @@ -264,6 +267,27 @@ void AST_XXport::CollectImportInfo(unsigned hidx) { } } +TreeNode *AST_XXport::GetIdentifier(TreeNode *node) { + switch (node->GetKind()) { + case NK_Identifier: + break; + case NK_TypeAlias: { + TypeAliasNode *ta = static_cast(node); + node = GetIdentifier(ta->GetId()); + break; + } + case NK_UserType: { + UserTypeNode *ut = static_cast(node); + node = GetIdentifier(ut->GetId()); + break; + } + default: + NOTYETIMPL("need to extract identifier"); + break; + } + return node; +} + void AST_XXport::CollectExportInfo(unsigned hidx) { Module_Handler *handler = mASTHandler->GetModuleHandler(hidx); ModuleNode *module = handler->GetASTModule(); @@ -331,25 +355,31 @@ void AST_XXport::CollectExportInfo(unsigned hidx) { p->SetAfter(NULL); } - bfnode = p->GetBefore(); afnode = p->GetAfter(); + bfnode = p->GetBefore(); + if (!bfnode->IsIdentifier()) { + bfnode = GetIdentifier(bfnode); + } + unsigned exportednid = (afnode ? afnode->GetNodeId(): bfnode->GetNodeId()); if (p->IsDefault()) { info->mDefaultNodeId = exportednid; - AddExportedDeclIds(hidx, exportednid); } else if (mExportNodeSets[hidx].size() == 1 && node->GetPairsNum() == 1) { info->mDefaultNodeId = bfnode->GetNodeId(); - std::pair pnid(bfnode->GetNodeId(), afnode ? afnode->GetNodeId() : 0); + std::pair pnid(bfnode->GetNodeId(), exportednid); info->mNodeIdPairs.insert(pnid); - AddExportedDeclIds(hidx, exportednid); } else { - std::pair pnid(bfnode->GetNodeId(), afnode ? afnode->GetNodeId() : 0); + std::pair pnid(bfnode->GetNodeId(), exportednid); info->mNodeIdPairs.insert(pnid); - AddExportedDeclIds(hidx, exportednid); } + AddExportedDeclIds(hidx, exportednid); } - mExports[hidx].insert(info); + if (info->mDefaultNodeId || info->mNodeIdPairs.size()) { + mExports[hidx].insert(info); + } else { + delete info; + } } } -- Gitee From 757f0a47a67034ced1707566829972eca18c2e02 Mon Sep 17 00:00:00 2001 From: Wen HU Date: Thu, 23 Dec 2021 15:48:27 -0500 Subject: [PATCH 13/16] rename title in ast dump graph --- src/MapleFE/ast2cpp/src/ast2cpp.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MapleFE/ast2cpp/src/ast2cpp.cpp b/src/MapleFE/ast2cpp/src/ast2cpp.cpp index 9f503460c0..449e4b9c17 100644 --- a/src/MapleFE/ast2cpp/src/ast2cpp.cpp +++ b/src/MapleFE/ast2cpp/src/ast2cpp.cpp @@ -172,13 +172,13 @@ int A2C::ProcessAST() { if (mFlags & FLG_trace_2) { std::cout << "============= AstGraph ===========" << std::endl; AstGraph graph(module); - graph.DumpGraph("After BuildCFG()", &std::cout); + graph.DumpGraph("After TypeInference()", &std::cout); } if (mFlags & FLG_trace_2) { std::cout << "============= AstDump ===========" << std::endl; AstDump astdump(module); - astdump.Dump("After BuildCFG()", &std::cout); + astdump.Dump("After TypeInference()", &std::cout); } // data flow analysis -- Gitee From 777a5712fb204f9f93525051e926fbb68dc44f46 Mon Sep 17 00:00:00 2001 From: Wen HU Date: Thu, 23 Dec 2021 16:16:28 -0500 Subject: [PATCH 14/16] handle structliteral and fieldliteral for direct fields --- src/MapleFE/astopt/include/ast_scp.h | 1 + src/MapleFE/astopt/include/ast_ti.h | 1 + src/MapleFE/astopt/src/ast_scp.cpp | 24 ++++++++++++++++++++++++ src/MapleFE/astopt/src/ast_ti.cpp | 13 +++++++++++++ 4 files changed, 39 insertions(+) diff --git a/src/MapleFE/astopt/include/ast_scp.h b/src/MapleFE/astopt/include/ast_scp.h index df514cd4c4..46a4266dfa 100644 --- a/src/MapleFE/astopt/include/ast_scp.h +++ b/src/MapleFE/astopt/include/ast_scp.h @@ -114,6 +114,7 @@ class BuildScopeVisitor : public BuildScopeBaseVisitor { LambdaNode *VisitLambdaNode(LambdaNode *node); ClassNode *VisitClassNode(ClassNode *node); StructNode *VisitStructNode(StructNode *node); + StructLiteralNode *VisitStructLiteralNode(StructLiteralNode *node); InterfaceNode *VisitInterfaceNode(InterfaceNode *node); NamespaceNode *VisitNamespaceNode(NamespaceNode *node); ForLoopNode *VisitForLoopNode(ForLoopNode *node); diff --git a/src/MapleFE/astopt/include/ast_ti.h b/src/MapleFE/astopt/include/ast_ti.h index e56b2a035e..d686598243 100644 --- a/src/MapleFE/astopt/include/ast_ti.h +++ b/src/MapleFE/astopt/include/ast_ti.h @@ -63,6 +63,7 @@ class BuildIdDirectFieldVisitor : public AstVisitor { ~BuildIdDirectFieldVisitor() = default; FieldNode *VisitFieldNode(FieldNode *node); + FieldLiteralNode *VisitFieldLiteralNode(FieldLiteralNode *node); ArrayElementNode *VisitArrayElementNode(ArrayElementNode *node); void Dump(); }; diff --git a/src/MapleFE/astopt/src/ast_scp.cpp b/src/MapleFE/astopt/src/ast_scp.cpp index d4a566be92..946b18b195 100644 --- a/src/MapleFE/astopt/src/ast_scp.cpp +++ b/src/MapleFE/astopt/src/ast_scp.cpp @@ -380,6 +380,30 @@ StructNode *BuildScopeVisitor::VisitStructNode(StructNode *node) { return node; } +StructLiteralNode *BuildScopeVisitor::VisitStructLiteralNode(StructLiteralNode *node) { + ASTScope *parent = mScopeStack.top(); + // struct is a decl + if (parent) { + AddDecl(parent, node); + AddType(parent, node); + } + + ASTScope *scope = NewScope(parent, node); + + // add fields as decl + for(unsigned i = 0; i < node->GetFieldsNum(); i++) { + FieldLiteralNode *fld = node->GetField(i); + TreeNode *name = fld->GetFieldName(); + if (name->IsIdentifier()) { + AddDecl(scope, name); + } + } + mScopeStack.push(scope); + BuildScopeBaseVisitor::VisitStructLiteralNode(node); + mScopeStack.pop(); + return node; +} + NamespaceNode *BuildScopeVisitor::VisitNamespaceNode(NamespaceNode *node) { node->SetTypeId(TY_Namespace); TreeNode *id = node->GetId(); diff --git a/src/MapleFE/astopt/src/ast_ti.cpp b/src/MapleFE/astopt/src/ast_ti.cpp index 89a1be3799..758968f586 100644 --- a/src/MapleFE/astopt/src/ast_ti.cpp +++ b/src/MapleFE/astopt/src/ast_ti.cpp @@ -104,6 +104,19 @@ FieldNode *BuildIdDirectFieldVisitor::VisitFieldNode(FieldNode *node) { return node; } +FieldLiteralNode *BuildIdDirectFieldVisitor::VisitFieldLiteralNode(FieldLiteralNode *node) { + (void) AstVisitor::VisitFieldLiteralNode(node); + TreeNode *name = node->GetFieldName(); + IdentifierNode *field = static_cast(name); + TreeNode *decl = NULL; + decl = mHandler->FindDecl(field); + if (decl) { + mHandler->AddDirectField(field); + mHandler->AddDirectField(node); + } + return node; +} + ArrayElementNode *BuildIdDirectFieldVisitor::VisitArrayElementNode(ArrayElementNode *node) { (void) AstVisitor::VisitArrayElementNode(node); TreeNode *array = node->GetArray(); -- Gitee From 2bb09ea08dc33285069d43c4845cc3c19777f06a Mon Sep 17 00:00:00 2001 From: Wen HU Date: Thu, 23 Dec 2021 16:32:01 -0500 Subject: [PATCH 15/16] add guard --- src/MapleFE/astopt/src/ast_scp.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MapleFE/astopt/src/ast_scp.cpp b/src/MapleFE/astopt/src/ast_scp.cpp index 946b18b195..a11790d4b4 100644 --- a/src/MapleFE/astopt/src/ast_scp.cpp +++ b/src/MapleFE/astopt/src/ast_scp.cpp @@ -370,7 +370,7 @@ StructNode *BuildScopeVisitor::VisitStructNode(StructNode *node) { StrIndexSigNode *sn = static_cast(fld); fld = sn->GetKey(); } - if (fld->IsIdentifier()) { + if (fld && fld->IsIdentifier()) { AddDecl(scope, fld); } } @@ -394,7 +394,7 @@ StructLiteralNode *BuildScopeVisitor::VisitStructLiteralNode(StructLiteralNode * for(unsigned i = 0; i < node->GetFieldsNum(); i++) { FieldLiteralNode *fld = node->GetField(i); TreeNode *name = fld->GetFieldName(); - if (name->IsIdentifier()) { + if (name && name->IsIdentifier()) { AddDecl(scope, name); } } -- Gitee From c59e261e5440dac8f4046c8b613cdb202017aa03 Mon Sep 17 00:00:00 2001 From: Wen HU Date: Thu, 23 Dec 2021 17:23:40 -0500 Subject: [PATCH 16/16] move some shared macros to ast_common.h --- src/MapleFE/ast2cpp/runtime/include/ts2cpp.h | 3 --- src/MapleFE/ast2cpp/runtime/src/ts2cpp.cpp | 1 + src/MapleFE/ast2cpp/src/cpp_declaration.cpp | 2 +- src/MapleFE/astopt/include/ast_adj.h | 3 --- src/MapleFE/astopt/include/ast_common.h | 3 +++ src/MapleFE/astopt/include/ast_handler.h | 2 -- src/MapleFE/astopt/src/ast_ti.cpp | 4 ++-- 7 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/MapleFE/ast2cpp/runtime/include/ts2cpp.h b/src/MapleFE/ast2cpp/runtime/include/ts2cpp.h index 5a5cd376fd..6cd956c2e1 100644 --- a/src/MapleFE/ast2cpp/runtime/include/ts2cpp.h +++ b/src/MapleFE/ast2cpp/runtime/include/ts2cpp.h @@ -24,9 +24,6 @@ #include #include -// Copied from astopt/include/ast_adj.h -#define RENAMINGSUFFIX "__RENAMED" - using namespace std::string_literals; namespace t2crt { diff --git a/src/MapleFE/ast2cpp/runtime/src/ts2cpp.cpp b/src/MapleFE/ast2cpp/runtime/src/ts2cpp.cpp index 4820cf0929..1fa036b3af 100644 --- a/src/MapleFE/ast2cpp/runtime/src/ts2cpp.cpp +++ b/src/MapleFE/ast2cpp/runtime/src/ts2cpp.cpp @@ -1,6 +1,7 @@ #include #include #include "../include/ts2cpp.h" +#include "ast_common.h" std::ostream& operator<< (std::ostream& out, const t2crt::JS_Val& v) { switch(v.type) { diff --git a/src/MapleFE/ast2cpp/src/cpp_declaration.cpp b/src/MapleFE/ast2cpp/src/cpp_declaration.cpp index f2cc18da61..8272ffefe6 100644 --- a/src/MapleFE/ast2cpp/src/cpp_declaration.cpp +++ b/src/MapleFE/ast2cpp/src/cpp_declaration.cpp @@ -18,7 +18,7 @@ #include #include #include "helper.h" -#include "ast_adj.h" +#include "ast_common.h" namespace maplefe { diff --git a/src/MapleFE/astopt/include/ast_adj.h b/src/MapleFE/astopt/include/ast_adj.h index 025ae5d8c1..0f1f2bce98 100644 --- a/src/MapleFE/astopt/include/ast_adj.h +++ b/src/MapleFE/astopt/include/ast_adj.h @@ -40,9 +40,6 @@ class AST_ADJ { void AdjustAST(); }; -// If you change RENAMINGSUFFIX, you have to update ast2cpp/runtime/include/ts2cpp.h as well -#define RENAMINGSUFFIX "__RENAMED" - class AdjustASTVisitor : public AstVisitor { private: Module_Handler *mHandler; diff --git a/src/MapleFE/astopt/include/ast_common.h b/src/MapleFE/astopt/include/ast_common.h index d479cf5c66..7c25f1ca9f 100644 --- a/src/MapleFE/astopt/include/ast_common.h +++ b/src/MapleFE/astopt/include/ast_common.h @@ -18,6 +18,9 @@ namespace maplefe { +#define DEFAULTVALUE 0xdeadbeef +#define RENAMINGSUFFIX "__RENAMED" + #define NOTYETIMPL(M) { if (mFlags & FLG_trace) { MNYI(M); }} #define MSGNOLOC0(M) { if (mFlags & FLG_trace_3) { MMSGNOLOC0(M); }} #define MSGNOLOC(M,v) { if (mFlags & FLG_trace_3) { MMSGNOLOC(M,v); }} diff --git a/src/MapleFE/astopt/include/ast_handler.h b/src/MapleFE/astopt/include/ast_handler.h index a6d6dedec6..d6ca33098d 100644 --- a/src/MapleFE/astopt/include/ast_handler.h +++ b/src/MapleFE/astopt/include/ast_handler.h @@ -31,8 +31,6 @@ #include "ast_common.h" #include "gen_astvisitor.h" -#define DEFAULTVALUE 0xdeadbeef - namespace maplefe { class CfgBB; diff --git a/src/MapleFE/astopt/src/ast_ti.cpp b/src/MapleFE/astopt/src/ast_ti.cpp index 758968f586..205a0e0f81 100644 --- a/src/MapleFE/astopt/src/ast_ti.cpp +++ b/src/MapleFE/astopt/src/ast_ti.cpp @@ -1242,9 +1242,9 @@ ImportNode *TypeInferVisitor::VisitImportNode(ImportNode *node) { return node; } -// check if node is identifier with name "default__RENAMED" +// check if node is identifier with name "default"+RENAMINGSUFFIX static bool IsDefault(TreeNode *node) { - return node->GetStrIdx() == gStringPool.GetStrIdx("default__RENAMED"); + return node->GetStrIdx() == gStringPool.GetStrIdx(std::string("default") + RENAMINGSUFFIX); } ExportNode *TypeInferVisitor::VisitExportNode(ExportNode *node) { -- Gitee