• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ETSparser.h"
17 #include "ETSNolintParser.h"
18 #include <utility>
19 
20 #include "util/es2pandaMacros.h"
21 #include "parser/parserFlags.h"
22 #include "parser/parserStatusContext.h"
23 #include "util/helpers.h"
24 #include "util/language.h"
25 #include "utils/arena_containers.h"
26 #include "varbinder/varbinder.h"
27 #include "varbinder/ETSBinder.h"
28 #include "lexer/lexer.h"
29 #include "lexer/ETSLexer.h"
30 #include "ir/astNode.h"
31 #include "ir/base/classDefinition.h"
32 #include "ir/base/decorator.h"
33 #include "ir/base/catchClause.h"
34 #include "ir/base/classProperty.h"
35 #include "ir/base/scriptFunction.h"
36 #include "ir/base/methodDefinition.h"
37 #include "ir/base/classStaticBlock.h"
38 #include "ir/base/spreadElement.h"
39 #include "ir/expressions/identifier.h"
40 #include "ir/expressions/functionExpression.h"
41 #include "ir/statements/functionDeclaration.h"
42 #include "ir/statements/expressionStatement.h"
43 #include "ir/statements/classDeclaration.h"
44 #include "ir/statements/variableDeclarator.h"
45 #include "ir/statements/variableDeclaration.h"
46 #include "ir/expressions/dummyNode.h"
47 #include "ir/expressions/callExpression.h"
48 #include "ir/expressions/thisExpression.h"
49 #include "ir/expressions/typeofExpression.h"
50 #include "ir/expressions/memberExpression.h"
51 #include "ir/expressions/updateExpression.h"
52 #include "ir/expressions/arrowFunctionExpression.h"
53 #include "ir/expressions/unaryExpression.h"
54 #include "ir/expressions/yieldExpression.h"
55 #include "ir/expressions/awaitExpression.h"
56 #include "ir/expressions/literals/nullLiteral.h"
57 #include "ir/expressions/literals/numberLiteral.h"
58 #include "ir/expressions/literals/stringLiteral.h"
59 #include "ir/expressions/literals/undefinedLiteral.h"
60 #include "ir/module/importDeclaration.h"
61 #include "ir/module/importDefaultSpecifier.h"
62 #include "ir/module/importSpecifier.h"
63 #include "ir/module/exportSpecifier.h"
64 #include "ir/module/exportNamedDeclaration.h"
65 #include "ir/statements/blockStatement.h"
66 #include "ir/statements/ifStatement.h"
67 #include "ir/statements/labelledStatement.h"
68 #include "ir/statements/switchStatement.h"
69 #include "ir/statements/throwStatement.h"
70 #include "ir/statements/tryStatement.h"
71 #include "ir/statements/whileStatement.h"
72 #include "ir/statements/forOfStatement.h"
73 #include "ir/statements/doWhileStatement.h"
74 #include "ir/statements/breakStatement.h"
75 #include "ir/statements/debuggerStatement.h"
76 #include "ir/ets/etsClassLiteral.h"
77 #include "ir/ets/etsPrimitiveType.h"
78 #include "ir/ets/etsPackageDeclaration.h"
79 #include "ir/ets/etsReExportDeclaration.h"
80 #include "ir/ets/etsWildcardType.h"
81 #include "ir/ets/etsNewArrayInstanceExpression.h"
82 #include "ir/ets/etsTuple.h"
83 #include "ir/ets/etsFunctionType.h"
84 #include "ir/ets/etsNewClassInstanceExpression.h"
85 #include "ir/ets/etsNewMultiDimArrayInstanceExpression.h"
86 #include "ir/ets/etsModule.h"
87 #include "ir/ets/etsTypeReference.h"
88 #include "ir/ets/etsTypeReferencePart.h"
89 #include "ir/ets/etsNullishTypes.h"
90 #include "ir/ets/etsUnionType.h"
91 #include "ir/ets/etsImportDeclaration.h"
92 #include "ir/ets/etsStructDeclaration.h"
93 #include "ir/ets/etsParameterExpression.h"
94 #include "ir/module/importNamespaceSpecifier.h"
95 #include "ir/ts/tsAsExpression.h"
96 #include "ir/ts/tsInterfaceDeclaration.h"
97 #include "ir/ts/tsEnumDeclaration.h"
98 #include "ir/ts/tsTypeParameterInstantiation.h"
99 #include "ir/ts/tsInterfaceBody.h"
100 #include "ir/ts/tsImportEqualsDeclaration.h"
101 #include "ir/ts/tsArrayType.h"
102 #include "ir/ts/tsQualifiedName.h"
103 #include "ir/ts/tsTypeReference.h"
104 #include "ir/ts/tsTypeParameter.h"
105 #include "ir/ts/tsInterfaceHeritage.h"
106 #include "ir/ts/tsFunctionType.h"
107 #include "ir/ts/tsClassImplements.h"
108 #include "ir/ts/tsEnumMember.h"
109 #include "ir/ts/tsTypeAliasDeclaration.h"
110 #include "ir/ts/tsTypeParameterDeclaration.h"
111 #include "ir/ts/tsNonNullExpression.h"
112 #include "ir/ts/tsThisType.h"
113 #include "generated/signatures.h"
114 #include "generated/diagnostic.h"
115 
116 namespace ark::es2panda::parser {
117 class FunctionContext;
118 
119 using namespace std::literals::string_literals;
120 
121 // NOLINTNEXTLINE(google-default-arguments)
ParseEnumDeclaration(bool isConst,bool isStatic)122 ir::Statement *ETSParser::ParseEnumDeclaration(bool isConst, bool isStatic)
123 {
124     ES2PANDA_ASSERT(Lexer()->GetToken().Type() == lexer::TokenType::KEYW_ENUM);
125 
126     lexer::SourcePosition enumStart = Lexer()->GetToken().Start();
127     Lexer()->NextToken();  // eat enum keyword
128 
129     auto *key = ExpectIdentifier(false, true);
130 
131     auto *declNode = ParseEnumMembers(key, enumStart, isConst, isStatic);
132     if (declNode == nullptr) {  // Error processing.
133         // Error is logged inside ParseEnumMembers
134         return AllocBrokenStatement(enumStart);
135     }
136     return declNode;
137 }
138 
ParsePotentialConstEnum(VariableParsingFlags flags)139 ir::Statement *ETSParser::ParsePotentialConstEnum(VariableParsingFlags flags)
140 {
141     if ((flags & VariableParsingFlags::CONST) == 0) {
142         LogError(diagnostic::VAR_DEC_EXPECTED);
143     }
144 
145     // According to the ArkTS specification:
146     // const enum is supported for source-level compatibility with TypeScript,
147     // and const is skipped as it has no impact on enum semantics in ArkTS.
148     return ParseEnumDeclaration(false);
149 }
150 
151 // NOLINTBEGIN(cert-err58-cpp)
152 // NOLINTEND(cert-err58-cpp)
153 
ParseEnumMembers(ir::Identifier * const key,const lexer::SourcePosition & enumStart,const bool isConst,const bool isStatic)154 ir::TSEnumDeclaration *ETSParser::ParseEnumMembers(ir::Identifier *const key, const lexer::SourcePosition &enumStart,
155                                                    const bool isConst, const bool isStatic)
156 {
157     if (Lexer()->GetToken().Type() != lexer::TokenType::PUNCTUATOR_LEFT_BRACE) {
158         LogExpectedToken(lexer::TokenType::PUNCTUATOR_LEFT_BRACE);
159     }
160 
161     Lexer()->NextToken(lexer::NextTokenFlags::KEYWORD_TO_IDENT);  // eat '{'
162 
163     ArenaVector<ir::AstNode *> members(Allocator()->Adapter());
164 
165     lexer::SourcePosition enumEnd = ParseEnumMember(members);
166 
167     auto *const enumDeclaration = AllocNode<ir::TSEnumDeclaration>(
168         Allocator(), key, std::move(members),
169         ir::TSEnumDeclaration::ConstructorFlags {isConst, isStatic, InAmbientContext()});
170     ES2PANDA_ASSERT(enumDeclaration != nullptr);
171     if (InAmbientContext()) {
172         enumDeclaration->AddModifier(ir::ModifierFlags::DECLARE);
173     }
174     enumDeclaration->SetRange({enumStart, enumEnd});
175 
176     return enumDeclaration;
177 }
178 
ParseEnumExpression()179 ir::Expression *ETSParser::ParseEnumExpression()
180 {
181     ir::Expression *expression {};
182     auto endLoc = Lexer()->GetToken().Start();
183     expression = ParseExpression();
184     if (expression == nullptr) {
185         LogError(diagnostic::ENUM_INVALID_INIT, {}, endLoc);
186         // Continue to parse the rest of Enum.
187         return AllocNode<ir::NumberLiteral>(lexer::Number(0));
188     }
189     return expression;
190 }
191 
ParseNumberEnumHelper()192 bool ETSParser::ParseNumberEnumHelper()
193 {
194     bool minusSign = false;
195     if (Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_PLUS) {
196         Lexer()->NextToken();
197     } else if (Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_MINUS) {
198         minusSign = true;
199         Lexer()->NextToken();
200     }
201 
202     if (Lexer()->GetToken().Type() != lexer::TokenType::LITERAL_NUMBER) {
203         // enum15.ets; will be zero by default
204         LogError(diagnostic::INVALID_ENUM_TYPE, {}, Lexer()->GetToken().Start());
205         Lexer()->GetToken().SetTokenType(lexer::TokenType::LITERAL_NUMBER);
206         Lexer()->GetToken().SetTokenStr(ERROR_LITERAL);
207     }
208     return minusSign;
209 }
210 
ParseEnumMember(ArenaVector<ir::AstNode * > & members)211 lexer::SourcePosition ETSParser::ParseEnumMember(ArenaVector<ir::AstNode *> &members)
212 {
213     // Default enum number value
214     ir::Expression *currentNumberExpr = AllocNode<ir::NumberLiteral>(lexer::Number(0));
215 
216     // Lambda to parse enum member (maybe with initializer)
217     auto const parseMember = [this, &members, &currentNumberExpr]() {
218         HandleJsDocLikeComments();
219         auto *const ident = ExpectIdentifier(false, true);
220 
221         ir::Expression *ordinal;
222         lexer::SourcePosition endLoc;
223         bool isGenerated = false;
224 
225         if (Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_SUBSTITUTION) {
226             // Case when user explicitly set the value for enumeration constant
227             Lexer()->NextToken();
228 
229             ordinal = ParseEnumExpression();
230             currentNumberExpr = ordinal;
231 
232             endLoc = ordinal->End();
233         } else {
234             // Default enumeration constant value. Equal to 0 for the first item and = previous_value + 1 for all
235             // the others.
236             ordinal = currentNumberExpr;
237             ordinal->SetRange({ident->End(), ident->End()});
238             isGenerated = true;
239 
240             endLoc = ident->End();
241         }
242 
243         auto *const member = AllocNode<ir::TSEnumMember>(ident, ordinal, isGenerated);
244         member->SetRange({ident->Start(), endLoc});
245         members.emplace_back(member);
246 
247         // Increment the value by one
248         auto incrementNode = AllocNode<ir::NumberLiteral>(lexer::Number(1));
249         ir::Expression *dummyNode = currentNumberExpr->Clone(Allocator(), nullptr)->AsExpression();
250         currentNumberExpr =
251             AllocNode<ir::BinaryExpression>(dummyNode, incrementNode, lexer::TokenType::PUNCTUATOR_PLUS);
252         return true;
253     };
254 
255     lexer::SourcePosition enumEnd;
256     ParseList(lexer::TokenType::PUNCTUATOR_RIGHT_BRACE, lexer::NextTokenFlags::KEYWORD_TO_IDENT, parseMember, &enumEnd,
257               true);
258     return enumEnd;
259 }
260 
261 }  // namespace ark::es2panda::parser
262