• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021 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 #ifndef ES2PANDA_PARSER_CORE_PARSER_IMPL_H
17 #define ES2PANDA_PARSER_CORE_PARSER_IMPL_H
18 
19 #include <binder/binder.h>
20 #include <es2panda.h>
21 #include <ir/astNode.h>
22 #include <ir/base/methodDefinition.h>
23 #include <lexer/token/sourceLocation.h>
24 #include <macros.h>
25 #include <mem/arena_allocator.h>
26 #include <parser/context/parserContext.h>
27 #include <parser/module/sourceTextModuleRecord.h>
28 #include <parser/parserFlags.h>
29 #include <parser/program/program.h>
30 #include <util/enumbitops.h>
31 #include <util/ustring.h>
32 
33 #include <memory>
34 #include <sstream>
35 #include <unordered_map>
36 #include <unordered_set>
37 
38 namespace panda::es2panda::lexer {
39 enum class TokenFlags : uint8_t;
40 enum class TokenType;
41 class LexerPosition;
42 class Token;
43 class Lexer;
44 }  // namespace panda::es2panda::lexer
45 
46 namespace panda::es2panda::ir {
47 class ArrowFunctionExpression;
48 class AstNode;
49 class BlockStatement;
50 class BreakStatement;
51 class CallExpression;
52 class ClassDeclaration;
53 class ClassDefinition;
54 class ContinueStatement;
55 class DoWhileStatement;
56 class ExportAllDeclaration;
57 class ExportDefaultDeclaration;
58 class ExportNamedDeclaration;
59 class ExportNamedDeclaration;
60 class Expression;
61 class FunctionDeclaration;
62 class FunctionExpression;
63 class Identifier;
64 class IfStatement;
65 class ImportDeclaration;
66 class LabelledStatement;
67 class NewExpression;
68 class ObjectExpression;
69 class ReturnStatement;
70 class ScriptFunction;
71 class SequenceExpression;
72 class SpreadElement;
73 class Statement;
74 class StringLiteral;
75 class SwitchCaseStatement;
76 class SwitchStatement;
77 class TSArrayType;
78 class TSEnumDeclaration;
79 class TSFunctionType;
80 class TSInterfaceDeclaration;
81 class TSIntersectionType;
82 class TSTupleType;
83 class TSTypeAliasDeclaration;
84 class TSUnionType;
85 class TSImportType;
86 class TemplateLiteral;
87 class ThrowStatement;
88 class TryStatement;
89 class VariableDeclaration;
90 class WhileStatement;
91 class WithStatement;
92 class TSTypeParameter;
93 class TSTypeParameterDeclaration;
94 class MemberExpression;
95 class MethodDefinition;
96 class TSTypeReference;
97 class TSMappedType;
98 class TSTypePredicate;
99 class Decorator;
100 class TSIndexSignature;
101 class Property;
102 class TSTypeParameterInstantiation;
103 class TSParameterProperty;
104 class TSTypeAssertion;
105 class TSAsExpression;
106 class TSSatisfiesExpression;
107 class YieldExpression;
108 class MetaProperty;
109 class TSModuleDeclaration;
110 class TSImportEqualsDeclaration;
111 class TSModuleBlock;
112 class EmptyStatement;
113 class DebuggerStatement;
114 class CatchClause;
115 class VariableDeclarator;
116 
117 enum class PropertyKind;
118 enum class TSTupleKind;
119 enum class MethodDefinitionKind;
120 enum class ModifierFlags : uint16_t;
121 }  // namespace panda::es2panda::ir
122 
123 namespace panda::es2panda::parser {
124 
125 class Program;
126 class ParserContext;
127 
128 class ClassElmentDescriptor {
129 public:
130     ir::MethodDefinitionKind methodKind {};
131     ParserStatus newStatus {};
132     ir::ModifierFlags modifiers {};
133     lexer::SourcePosition methodStart {};
134     lexer::SourcePosition propStart {};
135     bool isPrivateIdent {};
136     bool hasSuperClass {};
137     bool isGenerator {};
138     bool invalidComputedProperty {};
139     bool isComputed {};
140     bool isIndexSignature {};
141     bool classMethod {};
142     bool classField {};
143 };
144 
145 class ArrowFunctionDescriptor {
146 public:
ArrowFunctionDescriptor(ArenaVector<ir::Expression * > && p,binder::FunctionParamScope * ps,lexer::SourcePosition sl,ParserStatus ns)147     explicit ArrowFunctionDescriptor(ArenaVector<ir::Expression *> &&p, binder::FunctionParamScope *ps,
148                                      lexer::SourcePosition sl, ParserStatus ns)
149         : params(p), paramScope(ps), startLoc(sl), newStatus(ns)
150     {
151     }
152 
153     ArenaVector<ir::Expression *> params;
154     binder::FunctionParamScope *paramScope;
155     lexer::SourcePosition startLoc;
156     ParserStatus newStatus;
157 };
158 
159 enum class TypeAnnotationParsingOptions : uint8_t {
160     NO_OPTS = 0,
161     IN_UNION = 1 << 0,
162     ALLOW_CONST = 1 << 1,
163     IN_INTERSECTION = 1 << 2,
164     RESTRICT_EXTENDS = 1 << 3,
165     THROW_ERROR = 1 << 4,
166     CAN_BE_TS_TYPE_PREDICATE = 1 << 5,
167     BREAK_AT_NEW_LINE = 1 << 6,
168     IN_MODIFIER = 1 << 7,
169 };
170 
DEFINE_BITOPS(TypeAnnotationParsingOptions)171 DEFINE_BITOPS(TypeAnnotationParsingOptions)
172 
173 class ArrowFunctionContext;
174 
175 enum class PrivateGetterSetterType : uint8_t {
176     GETTER = 0,
177     SETTER = 1 << 0,
178     STATIC = 1 << 1,
179 };
180 
DEFINE_BITOPS(PrivateGetterSetterType)181 DEFINE_BITOPS(PrivateGetterSetterType)
182 
183 class ParserImpl {
184 public:
185     explicit ParserImpl(es2panda::ScriptExtension extension);
186     NO_COPY_SEMANTIC(ParserImpl);
187     NO_MOVE_SEMANTIC(ParserImpl);
188     ~ParserImpl() = default;
189 
190     Program Parse(const SourceFile &sourceFile, const CompilerOptions &options);
191 
192     ScriptExtension Extension() const;
193 
194     void AddPatchFixHelper(util::PatchFix *patchFixHelper);
195 
196     ArenaAllocator *Allocator() const
197     {
198         return program_.Allocator();
199     }
200     bool IsDtsFile() const;
201 
202 private:
203     bool IsStartOfMappedType() const;
204     bool IsStartOfTsTypePredicate() const;
205     bool IsStartOfAbstractConstructorType() const;
206 
207     ir::Expression* SetupChainExpr(ir::Expression *const top, lexer::SourcePosition startLoc);
208 
209     bool CurrentTokenIsModifier(char32_t nextCp) const;
210     [[noreturn]] void ThrowParameterModifierError(ir::ModifierFlags status) const;
211     [[noreturn]] void ThrowSyntaxError(std::string_view errorMessage) const;
212     [[noreturn]] void ThrowSyntaxError(std::initializer_list<std::string_view> list) const;
213     [[noreturn]] void ThrowSyntaxError(std::initializer_list<std::string_view> list,
214                                        const lexer::SourcePosition &pos) const;
215 
216     [[noreturn]] void ThrowSyntaxError(std::string_view errorMessage, const lexer::SourcePosition &pos) const;
217 
218     template <typename T, typename... Args>
219     T *AllocNode(Args &&... args)
220     {
221         auto ret = program_.Allocator()->New<T>(std::forward<Args>(args)...);
222         if (ret == nullptr) {
223             throw Error(ErrorType::GENERIC, "Unsuccessful allocation during parsing");
224         }
225         return ret;
226     }
227 
228     [[nodiscard]] std::unique_ptr<lexer::Lexer> InitLexer(const std::string &fileName, const std::string &source);
229     void ParseScript();
230     void ParseModule();
231 
232     /*
233      * Transform the commonjs module's AST by wrap the sourceCode & use Reflect.apply to invoke this wrapper with [this]
234      * pointing to [exports] object
235      *
236      * Reflect.apply(function (exports, require, module, __filename, __dirname) {
237      *   [SourceCode]
238      * }, exports, [exports, require, module, __filename, __dirname]);
239      */
240     void ParseCommonjs();
241     void AddCommonjsParams(ArenaVector<ir::Expression *> &params);
242     void AddReflectApplyArgs(ArenaVector<ir::Expression *> &args, ir::FunctionExpression *wrapper);
243     void ParseProgram(ScriptKind kind);
244     bool CheckTopStatementsForRequiredDeclare(const ArenaVector<ir::Statement *> &statements);
245     static ExpressionParseFlags CarryExpressionParserFlag(ExpressionParseFlags origin, ExpressionParseFlags carry);
246     static ExpressionParseFlags CarryPatternFlags(ExpressionParseFlags flags);
247     static ExpressionParseFlags CarryAllowTsParamAndPatternFlags(ExpressionParseFlags flags);
248     bool CurrentIsBasicType();
249     bool CurrentLiteralIsBasicType();
250     static bool CheckTypeNameIsReserved(const util::StringView &paramName);
251     static bool IsPropertyKeysAreSame(const ir::Expression *exp1, const ir::Expression *exp2);
252     static bool IsMemberExpressionsAreSame(const ir::MemberExpression *mExp1, const ir::MemberExpression *mExp2);
253     static bool IsMethodDefinitionsAreSame(const ir::MethodDefinition *property, ir::MethodDefinition *overload);
254     ir::TSTypeReference *ParseTsConstExpression();
255     ir::Expression *ParseTsTypeOperatorOrTypeReference(bool throwError);
256     ir::Expression *ParseTsTypeOperator();
257     ir::Expression *ParseTsInferType();
258     ir::Expression *ParseTsIdentifierReference(TypeAnnotationParsingOptions options);
259     ir::Expression *ParseTsBasicType(TypeAnnotationParsingOptions options);
260     ir::TSIntersectionType *ParseTsIntersectionType(ir::Expression *type, bool inUnion, bool restrictExtends,
261                                                     bool throwError);
262     ir::TSUnionType *ParseTsUnionType(ir::Expression *type, bool restrictExtends, bool throwError);
263     ir::Expression *ParseTsParenthesizedOrFunctionType(ir::Expression *typeAnnotation, bool throwError);
264     ir::TSArrayType *ParseTsArrayType(ir::Expression *elementType);
265     bool IsTsFunctionType();
266     ir::Expression *ParseTsFunctionType(lexer::SourcePosition startLoc, bool isConstructionType, bool throwError,
267                                         bool abstractConstructor = false);
268     ir::TSTypeParameter *ParseTsMappedTypeParameter();
269     ir::MappedOption ParseMappedOption(lexer::TokenType tokenType);
270     ir::TSMappedType *ParseTsMappedType();
271     ir::TSTypePredicate *ParseTsTypePredicate();
272     void ParseTsTypeLiteralOrInterfaceKeyModifiers(bool *isGetAccessor, bool *isSetAccessor);
273     ir::Expression *ParseTsTypeLiteralOrInterfaceKey(bool *computed, bool *signature, bool *isIndexSignature);
274     void ValidateIndexSignatureParameterType(ir::Expression *typeAnnotation);
275     ir::Expression *ParseTsConditionalType(ir::Expression *checkType, bool restrictExtends);
276     ir::Expression *ParseTsTypeLiteralOrInterfaceMember();
277     ArenaVector<ir::Expression *> ParseTsTypeLiteralOrInterface();
278     ir::Expression *ParseTsThisType(bool throwError);
279     ir::Expression *ParseTsIndexAccessType(ir::Expression *typeName, bool throwError);
280     ir::Expression *ParseTsQualifiedReference(ir::Expression *typeName);
281     ir::Expression *ParseTsTypeReferenceOrQuery(TypeAnnotationParsingOptions options, bool parseQuery = false);
282     bool IsTSNamedTupleMember();
283     void HandleRestType(ir::AstNodeType elementType, bool *hasRestType) const;
284     ir::Expression *ParseTsTupleElement(ir::TSTupleKind *kind, bool *seenOptional, bool *hasRestType);
285     ir::TSTupleType *ParseTsTupleType();
286     ir::TSImportType *ParseTsImportType(const lexer::SourcePosition &startLoc, bool isTypeof = false);
287     ir::Expression *ParseTsTypeAnnotation(TypeAnnotationParsingOptions *options);
288     ir::Expression *ParseTsTypeLiteralOrTsMappedType(ir::Expression *typeAnnotation);
289     ir::Expression *ParseTsTypeReferenceOrTsTypePredicate(ir::Expression *typeAnnotation, bool canBeTsTypePredicate,
290                                                           bool throwError);
291     ir::Expression *ParseTsThisTypeOrTsTypePredicate(ir::Expression *typeAnnotation, bool canBeTsTypePredicate,
292                                                      bool throwError);
293     ir::Expression *ParseTsTemplateLiteralType(bool throwError);
294     ir::Expression *ParseTsTypeAnnotationElement(ir::Expression *typeAnnotation, TypeAnnotationParsingOptions *options);
295     ir::ModifierFlags ParseModifiers();
296 
297     void ThrowIfPrivateIdent(ClassElmentDescriptor *desc, const char *msg);
298     void ValidateClassKey(ClassElmentDescriptor *desc, bool isDeclare);
299 
300     void ValidateClassMethodStart(ClassElmentDescriptor *desc, ir::Expression *typeAnnotation);
301     ir::Expression *ParseClassKey(ClassElmentDescriptor *desc, bool isDeclare);
302 
303     void ValidateClassSetter(ClassElmentDescriptor *desc, const ArenaVector<ir::Statement *> &properties,
304                              ir::Expression *propName, ir::ScriptFunction *func, bool hasDecorator,
305                              lexer::SourcePosition errorInfo);
306     void ValidateClassGetter(ClassElmentDescriptor *desc, const ArenaVector<ir::Statement *> &properties,
307                              ir::Expression *propName, ir::ScriptFunction *func, bool hasDecorator,
308                              lexer::SourcePosition errorInfo);
309     void ValidatePrivateProperty(ir::Statement *stmt, std::unordered_set<util::StringView> &privateNames,
310         std::unordered_map<util::StringView, PrivateGetterSetterType> &unusedGetterSetterPairs);
311     ir::MethodDefinition *ParseClassMethod(ClassElmentDescriptor *desc, const ArenaVector<ir::Statement *> &properties,
312                                            ir::Expression *propName, lexer::SourcePosition *propEnd,
313                                            ArenaVector<ir::Decorator *> &&decorators,
314                                            ArenaVector<ir::Annotation *> &&annotations, bool isDeclare);
315     ir::ClassStaticBlock *ParseStaticBlock(ClassElmentDescriptor *desc);
316     ir::Statement *ParseClassProperty(ClassElmentDescriptor *desc, const ArenaVector<ir::Statement *> &properties,
317                                       ir::Expression *propName, ir::Expression *typeAnnotation,
318                                       ArenaVector<ir::Decorator *> &&decorators,
319                                       ArenaVector<ir::Annotation *> &&annotations, bool isDeclare,
320                                       std::pair<binder::FunctionScope *, binder::FunctionScope *> implicitScopes);
321     void ParseClassKeyModifiers(ClassElmentDescriptor *desc);
322     void CheckClassGeneratorMethod(ClassElmentDescriptor *desc);
323     void CheckClassPrivateIdentifier(ClassElmentDescriptor *desc);
324     void CheckFieldKey(ir::Expression *propName);
325     bool CheckAnnotationPrefix(const util::StringView &Ident);
326     void ThrowAnnotationNotEnable();
327 
328     ir::Expression *ParseClassKeyAnnotation();
329     ir::Statement *ParseAnnotationUsage(ir::Expression *expr, lexer::SourcePosition start);
330     ir::Statement *ParseDecoratorAndAnnotation();
331     std::pair<ArenaVector<ir::Decorator *>, ArenaVector<ir::Annotation *>> ParseDecoratorsAndAnnotations();
332     ir::Statement *ParseClassElement(const ArenaVector<ir::Statement *> &properties,
333                                      ArenaVector<ir::TSIndexSignature *> *indexSignatures, bool hasSuperClass,
334                                      bool isDeclare, bool isAbstractClass, bool isExtendsFromNull,
335                                      std::pair<binder::FunctionScope *, binder::FunctionScope *> implicitScopes);
336     ir::Identifier *GetKeyByFuncFlag(ir::ScriptFunctionFlags funcFlag);
337     ir::MethodDefinition *CreateImplicitMethod(ir::Expression *superClass, bool hasSuperClass,
338                                                ir::ScriptFunctionFlags funcFlag, bool isDeclare = false);
339     ir::MethodDefinition *CheckClassMethodOverload(ir::Statement *property, ir::MethodDefinition **ctor, bool isDeclare,
340                                                    lexer::SourcePosition errorInfo, ir::MethodDefinition *lastOverload,
341                                                    bool implExists, bool isAbstract = false);
342     ir::Identifier *SetIdentNodeInClassDefinition(bool isDeclare, binder::ConstDecl **decl, bool isAnnotation = false);
343     ir::ClassDefinition *ParseClassDefinition(bool isDeclaration, bool idRequired = true, bool isDeclare = false,
344                                               bool isAbstract = false, bool isAnnotation = false);
345     ir::Expression *ParseSuperClass(bool isDeclare, bool *hasSuperClass, bool *isExtendsFromNull);
346     ArenaVector<ir::TSClassImplements *> ParseTSClassImplements(bool isDeclare);
347     void ValidateClassConstructor(const ir::MethodDefinition *ctor,
348                                   const ArenaVector<ir::Statement *> &properties,
349                                   bool isDeclare, bool hasConstructorFuncBody,
350                                   bool hasSuperClass, bool isExtendsFromNull);
351     void FindSuperCall(const ir::AstNode *parent, bool *hasSuperCall);
352     void FindSuperCallInCtorChildNode(const ir::AstNode *childNode, bool *hasSuperCall);
353     bool SuperCallShouldBeRootLevel(const ir::MethodDefinition *ctor, const ArenaVector<ir::Statement *> &properties);
354     void ValidateSuperCallLocation(const ir::MethodDefinition *ctor, bool superCallShouldBeRootLevel);
355     void FindThisOrSuperReference(const ir::AstNode *parent, bool *hasThisOrSuperReference);
356     void FindThisOrSuperReferenceInChildNode(const ir::AstNode *childNode, bool *hasThisOrSuperReference);
357     void ValidateAccessor(ExpressionParseFlags flags, lexer::TokenFlags currentTokenFlags);
358     void CheckPropertyKeyAsycModifier(ParserStatus *methodStatus);
359     ir::Property *ParseShorthandProperty(const lexer::LexerPosition *startPos);
360     void ParseGeneratorPropertyModifier(ExpressionParseFlags flags, ParserStatus *methodStatus);
361     bool ParsePropertyModifiers(ExpressionParseFlags flags, ir::PropertyKind *propertyKind, ParserStatus *methodStatus);
362     ir::Expression *ParsePropertyKey(ExpressionParseFlags flags);
363     ir::Expression *ParsePropertyValue(const ir::PropertyKind *propertyKind, const ParserStatus *methodStatus,
364                                        ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS);
365     bool ParsePropertyEnd();
366 
367     ir::Expression *ParsePostfixTypeOrHigher(ir::Expression *typeAnnotation, TypeAnnotationParsingOptions *options);
368     ir::Expression *TryParseConstraintOfInferType(TypeAnnotationParsingOptions *options);
369     ir::Expression *ParsePropertyDefinition(ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS);
370     bool CheckOutIsIdentInTypeParameter();
371     void ParseTypeModifier(bool &isTypeIn, bool &isTypeOut, bool &isAllowInOut);
372     ir::TSTypeParameter *ParseTsTypeParameter(bool throwError, bool addBinding = false, bool isAllowInOut = false);
373     ir::TSTypeParameterDeclaration *ParseTsTypeParameterDeclaration(bool throwError = true, bool isAllowInOut = false);
374     ir::TSTypeParameterInstantiation *ParseTsTypeParameterInstantiation(bool throwError = true);
375     ir::ScriptFunction *ParseFunction(ParserStatus newStatus = ParserStatus::NO_OPTS,
376                                       bool isDeclare = false,
377                                       ArenaVector<ir::ParamDecorators> *paramDecorators = nullptr);
378     void ValidateFunctionParam(const ArenaVector<ir::Expression *> &params, const ir::Expression *parameter,
379                                bool *seenOptional);
380     void ValidateTsFunctionOverloadParams(const ArenaVector<ir::Expression *> &params);
381     void CheckAccessorPair(const ArenaVector<ir::Statement *> &properties, const ir::Expression *propName,
382                            ir::MethodDefinitionKind methodKind, ir::ModifierFlags access, bool hasDecorator,
383                            lexer::SourcePosition errorInfo);
384     ArenaVector<ir::Expression *> ParseFunctionParams(bool isDeclare = false,
385                                                       ArenaVector<ir::ParamDecorators> *paramDecorators = nullptr);
386     ir::SpreadElement *ParseSpreadElement(ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS);
387     ir::TSParameterProperty *CreateTsParameterProperty(ir::Expression *parameter, ir::ModifierFlags modifiers);
388     ir::Expression *ParseFunctionParameter(bool isDeclare);
389     void CreateTSVariableForProperty(ir::AstNode *node, const ir::Expression *key, binder::VariableFlags flags);
390     void CheckObjectTypeForDuplicatedProperties(ir::Expression *member, ArenaVector<ir::Expression *> const &members);
391 
392     // ExpressionParser.Cpp
393 
394     ir::Expression *ParseExpression(ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS);
395     ir::ArrowFunctionExpression *ParseTsGenericArrowFunction();
396     ir::TSTypeAssertion *ParseTsTypeAssertion(ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS);
397     ir::TSAsExpression *ParseTsAsExpression(ir::Expression *expr, ExpressionParseFlags flags);
398     ir::TSSatisfiesExpression *ParseTsSatisfiesExpression(ir::Expression *expr);
399     ir::Expression *ParseArrayExpression(ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS);
400     ir::YieldExpression *ParseYieldExpression();
401     ir::Expression *ParsePotentialExpressionSequence(ir::Expression *expr, ExpressionParseFlags flags);
402     ParserStatus ValidateArrowParameter(ir::Expression *expr);
403 
404     ArrowFunctionDescriptor ConvertToArrowParameter(ir::Expression *expr, bool isAsync,
405                                                     binder::FunctionParamScope *paramScope);
406     ir::ArrowFunctionExpression *ParseArrowFunctionExpressionBody(ArrowFunctionContext *arrowFunctionContext,
407                                                                   binder::FunctionScope *functionScope,
408                                                                   ArrowFunctionDescriptor *desc,
409                                                                   ir::TSTypeParameterDeclaration *typeParamDecl,
410                                                                   ir::Expression *returnTypeAnnotation);
411     ir::ArrowFunctionExpression *ParseArrowFunctionExpression(ir::Expression *expr,
412                                                               ir::TSTypeParameterDeclaration *typeParamDecl,
413                                                               ir::Expression *returnTypeAnnotation, bool isAsync);
414     ir::Expression *ParseCoverParenthesizedExpressionAndArrowParameterList();
415     ir::Expression *ParseKeywordExpression();
416     ir::Expression *ParseBinaryExpression(ir::Expression *left);
417     ir::CallExpression *ParseCallExpression(ir::Expression *callee, bool isOptionalChain = false, bool isAsync = false);
418     ir::ArrowFunctionExpression *ParsePotentialArrowExpression(ir::Expression **returnExpression,
419                                                                const lexer::SourcePosition &startLoc,
420                                                                bool ignoreCallExpression);
421 
422     void ValidateUpdateExpression(ir::Expression *returnExpression, bool isChainExpression);
423     bool IsGenericInstantiation();
424     bool ParsePotentialTsGenericFunctionCall(ir::Expression **returnExpression, const lexer::SourcePosition &startLoc,
425                                              bool ignoreCallExpression);
426     ir::Expression *ParsePostPrimaryExpression(ir::Expression *primaryExpr, lexer::SourcePosition startLoc,
427                                                bool ignoreCallExpression, bool *isChainExpression);
428     ir::Expression *ParseMemberExpression(bool ignoreCallExpression = false,
429                                           ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS);
430     ir::ObjectExpression *ParseObjectExpression(ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS);
431     ir::SequenceExpression *ParseSequenceExpression(ir::Expression *startExpr, bool acceptRest = false,
432                                                     bool acceptTsParam = false, bool acceptPattern = false);
433     ir::Expression *ParseUnaryOrPrefixUpdateExpression(ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS);
434     ir::Expression *ParseLeftHandSideExpression(ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS);
435     ir::MetaProperty *ParsePotentialNewTarget();
436     void CheckInvalidDestructuring(const ir::AstNode *object) const;
437     void ValidateParenthesizedExpression(ir::Expression *lhsExpression);
438     ir::Expression *ParseAssignmentExpression(ir::Expression *lhsExpression,
439                                               ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS);
440     ir::Expression *ParsePrimaryExpression(ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS);
441     ir::NewExpression *ParseNewExpression();
442     void ParsePotentialTsFunctionParameter(ExpressionParseFlags flags, ir::Expression *returnNode,
443                                            bool isDeclare = false);
444     ir::Expression *ParsePatternElement(ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS,
445                                         bool allowDefault = true, bool isDeclare = false);
446     ir::TemplateLiteral *ParseTemplateLiteral(bool isTaggedTemplate = false);
447     ir::Expression *ParseImportExpression();
448     ir::ObjectExpression *ParseImportAssertionForDynamicImport();
449     void ValidateImportAssertionForDynamicImport(ir::ObjectExpression *importAssertion);
450     ir::AssertClause *ParseAssertClause();
451     ir::AssertEntry *ParseAssertEntry();
452     ir::FunctionExpression *ParseFunctionExpression(ParserStatus newStatus = ParserStatus::NO_OPTS);
453     ir::Expression *ParseOptionalChain(ir::Expression *leftSideExpr);
454     ir::Expression *ParseOptionalMemberExpression(ir::Expression *object);
455     void ParseNameSpaceImport(ArenaVector<ir::AstNode *> *specifiers, bool isType);
456     ir::Identifier *ParseNamedImport(const lexer::Token &importedToken);
457     binder::Decl *AddImportDecl(bool isType,
458                                 util::StringView name,
459                                 lexer::SourcePosition startPos,
460                                 binder::DeclarationFlags flag);
461 
462     ir::StringLiteral *ParseFromClause(bool requireFrom = true);
463     void ParseNamedImportSpecifiers(ArenaVector<ir::AstNode *> *specifiers, bool isType, bool isLazy);
464     bool HandleTypeImportOrExportSpecifier();
465     ir::Expression *ParseModuleReference();
466     ir::AstNode *ParseImportDefaultSpecifier(ArenaVector<ir::AstNode *> *specifiers, bool isType, bool isLazy);
467     ir::AstNode *ParseImportSpecifiers(ArenaVector<ir::AstNode *> *specifiers, bool isType, bool isLazy);
468     void ValidateAssignmentTarget(ExpressionParseFlags flags, ir::Expression *node);
469     void ValidateLvalueAssignmentTarget(ir::Expression *node) const;
470     void ValidateArrowParameterBindings(const ir::Expression *node);
471 
472     ir::ExportDefaultDeclaration *ParseExportDefaultDeclaration(const lexer::SourcePosition &startLoc,
473                                                                 ArenaVector<ir::Decorator *> decorators,
474                                                                 ArenaVector<ir::Annotation *> annotations,
475                                                                 bool isExportEquals = false);
476     ir::ExportAllDeclaration *ParseExportAllDeclaration(const lexer::SourcePosition &startLoc);
477     ir::ExportNamedDeclaration *ParseExportNamedSpecifiers(const lexer::SourcePosition &startLoc, bool isType);
478     ir::ExportNamedDeclaration *ParseNamedExportDeclaration(const lexer::SourcePosition &startLoc,
479                                                             ArenaVector<ir::Decorator *> &&decorators,
480                                                             ArenaVector<ir::Annotation *> &&annotations);
481     ir::Identifier *ParseNamedExport(const lexer::Token &exportedToken);
482     void CheckStrictReservedWord() const;
483     ir::PrivateIdentifier *ParsePrivateIdentifier();
484 
485     // Discard the DISALLOW_CONDITIONAL_TYPES in current status to call function.
486     template<class Function,  typename... Args>
487     ir::Expression *DoOutsideOfDisallowConditinalTypesContext(Function func, Args &&... args);
488 
489     // Add the DISALLOW_CONDITIONAL_TYPES to current status to call function.
490     template<typename Function, typename... Args>
491     ir::Expression *DoInsideOfDisallowConditinalTypesContext(Function func, Args &&... args);
492 
493     bool InDisallowConditionalTypesContext();
494     bool InContext(ParserStatus status);
495     void AddFlagToStatus(ParserStatus status);
496     void RemoveFlagToStatus(ParserStatus status);
497     // StatementParser.Cpp
498 
499     void ConsumeSemicolon(ir::Statement *statement);
500     void CheckFunctionDeclaration(StatementParsingFlags flags);
501 
502     void CheckLabelledFunction(const ir::Statement *node);
503     bool CheckDeclare();
504 
505     bool IsLabelFollowedByIterationStatement();
506 
507     void AddImportEntryItem(const ir::StringLiteral *source, const ArenaVector<ir::AstNode *> *specifiers, bool isType,
508                             bool isLazy);
509     void AddImportEntryItemForImportSpecifier(const ir::StringLiteral *source, const ir::AstNode *specifier,
510                                               bool isLazy);
511     void AddImportEntryItemForImportDefaultSpecifier(const ir::StringLiteral *source, const ir::AstNode *specifier,
512                                                      bool isType, bool isLazy);
513     void AddImportEntryItemForNamespaceSpecifier(const ir::StringLiteral *source,
514                                                  const ir::AstNode *specifier, bool isType);
515     void AddExportNamedEntryItem(const ArenaVector<ir::ExportSpecifier *> &specifiers,
516                                  const ir::StringLiteral *source, bool isType);
517     void AddExportStarEntryItem(const lexer::SourcePosition &startLoc, const ir::StringLiteral *source,
518                                 const ir::Identifier *exported);
519     void AddExportDefaultEntryItem(const ir::AstNode *declNode);
520     void AddExportLocalEntryItem(const ir::Statement *declNode, bool isTsModule);
521     void AddTsTypeExportLocalEntryItem(const ir::Statement *declNode, bool isTsModule,
522                                        binder::TSModuleScope *tsModuleScope);
523     parser::SourceTextModuleRecord *GetSourceTextModuleRecord();
524     parser::SourceTextModuleRecord *GetSourceTextTypeModuleRecord();
525 
526     bool ParseDirective(ArenaVector<ir::Statement *> *statements);
527     void ParseDirectivePrologue(ArenaVector<ir::Statement *> *statements);
528     ArenaVector<ir::Statement *> ParseStatementList(StatementParsingFlags flags = StatementParsingFlags::ALLOW_LEXICAL);
529     bool IsTsDeclarationStatement() const;
530     ir::Statement *ParseStatement(StatementParsingFlags flags = StatementParsingFlags::NONE);
531     ir::TSModuleDeclaration *ParseTsModuleDeclaration(bool isDeclare, bool isExport = false);
532     ir::TSModuleDeclaration *ParseTsAmbientExternalModuleDeclaration(const lexer::SourcePosition &startLoc,
533                                                                      bool isDeclare);
534     ir::TSModuleDeclaration *ParseTsModuleOrNamespaceDelaration(const lexer::SourcePosition &startLoc,
535                                                                 bool isDeclare,
536                                                                 bool isExport);
537     bool IsInstantiatedInTsModuleBlock(ir::Statement **body);
538 
539     ir::TSImportEqualsDeclaration *ParseTsImportEqualsDeclaration(const lexer::SourcePosition &startLoc,
540                                                                   bool isExport = false);
541     ir::TSNamespaceExportDeclaration *ParseTsNamespaceExportDeclaration(const lexer::SourcePosition &startLoc);
542     ir::TSModuleBlock *ParseTsModuleBlock();
543     ir::BlockStatement *ParseFunctionBody();
544     ir::BlockStatement *ParseBlockStatement();
545     ir::BlockStatement *ParseBlockStatement(binder::Scope *scope);
546     ir::EmptyStatement *ParseEmptyStatement();
547     ir::DebuggerStatement *ParseDebuggerStatement();
548     ir::BreakStatement *ParseBreakStatement();
549     ir::ContinueStatement *ParseContinueStatement();
550     ir::DoWhileStatement *ParseDoWhileStatement();
551     ir::Statement *ParsePotentialExpressionStatement(StatementParsingFlags flags, bool isDeclare);
552     ir::Statement *ParseVarStatement(bool isDeclare);
553     ir::Statement *ParseLetStatement(StatementParsingFlags flags, bool isDeclare);
554     ir::Statement *ParseConstStatement(StatementParsingFlags flags, bool isDeclare);
555     ir::Statement *ParseExpressionStatement(StatementParsingFlags flags = StatementParsingFlags::NONE);
556     ir::Statement *ParseFunctionStatement(StatementParsingFlags flags, bool isDeclare);
557     ir::FunctionDeclaration *ParseFunctionDeclaration(bool canBeAnonymous = false,
558                                                       ParserStatus newStatus = ParserStatus::NO_OPTS,
559                                                       bool isDeclare = false);
560     void AddFunctionToBinder(ir::ScriptFunction *func, ParserStatus newStatus);
561     void CheckOptionalBindingPatternParameter(ir::ScriptFunction *func) const;
562     ir::Statement *ParseExportDeclaration(StatementParsingFlags flags, ArenaVector<ir::Decorator *> &&decorators,
563                                           ArenaVector<ir::Annotation *> &&annotations);
564     std::tuple<ForStatementKind, ir::AstNode *, ir::Expression *, ir::Expression *> ParseForInOf(
565         ir::Expression *leftNode, ExpressionParseFlags exprFlags, bool isAwait);
566     std::tuple<ForStatementKind, ir::Expression *, ir::Expression *> ParseForInOf(ir::AstNode *initNode,
567                                                                                   ExpressionParseFlags exprFlags,
568                                                                                   bool isAwait);
569     std::tuple<ir::Expression *, ir::Expression *> ParseForUpdate(bool isAwait);
570     ir::Statement *ParseForStatement();
571     ir::IfStatement *ParseIfStatement();
572 
573     ir::Statement *ParseImportDeclaration(StatementParsingFlags flags);
574     ir::LabelledStatement *ParseLabelledStatement(const lexer::LexerPosition &pos);
575     ir::ReturnStatement *ParseReturnStatement();
576     ir::ClassDeclaration *ParseClassStatement(StatementParsingFlags flags, bool isDeclare,
577                                               ArenaVector<ir::Decorator *> &&decorators,
578                                               ArenaVector<ir::Annotation *> &&annotations, bool isAbstract = false);
579     ir::ClassDeclaration *ParseClassDeclaration(bool idRequired, ArenaVector<ir::Decorator *> &&decorators,
580                                                 ArenaVector<ir::Annotation *> &&annotations, bool isDeclare = false,
581                                                 bool isAbstract = false, bool isExported = false,
582                                                 bool isAnnotation = false);
583     ir::TSTypeAliasDeclaration *ParseTsTypeAliasDeclaration(bool isDeclare);
584     ir::Expression *ParseEnumComputedPropertyKey(binder::EnumDecl *&decl, const lexer::SourcePosition &keyStartLoc,
585                                                  bool isDeclare);
586     ir::TSEnumDeclaration *ParseEnumMembers(ir::Identifier *key, const lexer::SourcePosition &enumStart,
587                                             bool isExport, bool isDeclare, bool isConst);
588     ir::TSEnumDeclaration *ParseEnumDeclaration(bool isExport = false, bool isDeclare = false, bool isConst = false);
589     ir::TSInterfaceDeclaration *ParseTsInterfaceDeclaration(bool isDeclare);
590     void ValidateTsInterfaceName(bool isDeclare);
591     ArenaVector<ir::TSInterfaceHeritage *> ParseTsInterfaceExtends();
592     ir::SwitchCaseStatement *ParseSwitchCaseStatement(bool *seenDefault);
593     ir::SwitchStatement *ParseSwitchStatement();
594     ir::ThrowStatement *ParseThrowStatement();
595     ir::Expression *ParseCatchParam();
596     ir::CatchClause *ParseCatchClause();
597     ir::TryStatement *ParseTryStatement();
598     void ValidateDeclaratorId(bool isDeclare);
599     ir::VariableDeclarator *ParseVariableDeclaratorInitializer(ir::Expression *init, VariableParsingFlags flags,
600                                                                const lexer::SourcePosition &startLoc, bool isDeclare);
601     ir::VariableDeclarator *ParseVariableDeclarator(VariableParsingFlags flags, bool isDeclare);
602     ir::Expression *ParseVariableDeclaratorKey(VariableParsingFlags flags, bool isDeclare, bool *isDefinite);
603     ir::Statement *ParseVariableDeclaration(VariableParsingFlags flags = VariableParsingFlags::NO_OPTS,
604                                             bool isDeclare = false, bool isExport = false);
605     ir::WhileStatement *ParseWhileStatement();
606     ir::VariableDeclaration *ParseContextualLet(VariableParsingFlags flags,
607                                                 StatementParsingFlags stmFlags = StatementParsingFlags::ALLOW_LEXICAL,
608                                                 bool isDeclare = false);
609     void VerifySupportLazyImportVersion();
610 
611     util::StringView GetNamespaceExportInternalName()
612     {
613         std::string name = std::string(parser::SourceTextModuleRecord::ANONY_NAMESPACE_NAME) +
614                            std::to_string(namespaceExportCount_++);
615         util::UString internalName(name, Allocator());
616         return internalName.View();
617     }
618 
619     binder::Binder *Binder()
620     {
621         return program_.Binder();
622     }
623     static constexpr unsigned MAX_RECURSION_DEPTH = 1024;
624 
625     inline void RecursiveDepthCheck()
626     {
627         if (recursiveDepth_ < MAX_RECURSION_DEPTH) {
628             return;
629         }
630         RecursiveDepthException();
631     }
632 
633     void RecursiveDepthException();
634     // RAII to recursive depth tracking.
635     class TrackRecursive {
636     public:
637         explicit TrackRecursive(ParserImpl *parser) : parser_(parser)
638         {
639             ++parser_->recursiveDepth_;
640         }
641         ~TrackRecursive()
642         {
643             --parser_->recursiveDepth_;
644         }
645     private:
646         ParserImpl *const parser_;
647     };
648 
649     friend class Lexer;
650     friend class SavedParserContext;
651     friend class ArrowFunctionContext;
652 
653     Program program_;
654     ParserContext context_;
655     lexer::Lexer *lexer_ {nullptr};
656     size_t namespaceExportCount_ {0};
657     size_t recursiveDepth_{0};
658 };
659 
660 // Declare a RAII recursive tracker. Check whether the recursion limit has
661 // been exceeded, if so, throw a generic error.
662 // The macro only works from inside parserImpl methods.
663 #define CHECK_PARSER_RECURSIVE_DEPTH                  \
664     TrackRecursive trackRecursive{this};       \
665     RecursiveDepthCheck()
666 
667 template <ParserStatus status>
668 class SavedStatusContext {
669 public:
SavedStatusContext(ParserContext * ctx)670     explicit SavedStatusContext(ParserContext *ctx)
671         // NOLINTNEXTLINE(readability-magic-numbers)
672         : ctx_(ctx), savedStatus_(static_cast<ParserStatus>(ctx->Status()))
673     {
674         // NOLINTNEXTLINE(readability-magic-numbers)
675         ctx->Status() |= status;
676     }
677 
678     NO_COPY_SEMANTIC(SavedStatusContext);
679     NO_MOVE_SEMANTIC(SavedStatusContext);
680 
~SavedStatusContext()681     ~SavedStatusContext()
682     {
683         ctx_->Status() = savedStatus_;
684     }
685 
686 private:
687     ParserContext *ctx_;
688     ParserStatus savedStatus_;
689 };
690 
691 class SwitchContext : public SavedStatusContext<ParserStatus::IN_SWITCH> {
692 public:
SwitchContext(ParserContext * ctx)693     explicit SwitchContext(ParserContext *ctx) : SavedStatusContext(ctx) {}
694     NO_COPY_SEMANTIC(SwitchContext);
695     NO_MOVE_SEMANTIC(SwitchContext);
696     ~SwitchContext() = default;
697 };
698 
699 template <typename T>
700 class IterationContext : public SavedStatusContext<ParserStatus::IN_ITERATION> {
701 public:
IterationContext(ParserContext * ctx,binder::Binder * binder)702     explicit IterationContext(ParserContext *ctx, binder::Binder *binder)
703         : SavedStatusContext(ctx), lexicalScope_(binder)
704     {
705     }
706 
707     NO_COPY_SEMANTIC(IterationContext);
708     NO_MOVE_SEMANTIC(IterationContext);
709     ~IterationContext() = default;
710 
LexicalScope()711     const auto &LexicalScope() const
712     {
713         return lexicalScope_;
714     }
715 
716 private:
717     binder::LexicalScope<T> lexicalScope_;
718 };
719 
720 class FunctionParameterContext : public SavedStatusContext<ParserStatus::FUNCTION_PARAM> {
721 public:
FunctionParameterContext(ParserContext * ctx,binder::Binder * binder)722     explicit FunctionParameterContext(ParserContext *ctx, binder::Binder *binder)
723         : SavedStatusContext(ctx), lexicalScope_(binder)
724     {
725     }
726 
LexicalScope()727     const auto &LexicalScope() const
728     {
729         return lexicalScope_;
730     }
731 
732     NO_COPY_SEMANTIC(FunctionParameterContext);
733     NO_MOVE_SEMANTIC(FunctionParameterContext);
734     ~FunctionParameterContext() = default;
735 
736 private:
737     binder::LexicalScope<binder::FunctionParamScope> lexicalScope_;
738 };
739 
740 class SavedParserContext {
741 public:
742     template <typename... Args>
SavedParserContext(ParserImpl * parser,Args &&...args)743     explicit SavedParserContext(ParserImpl *parser, Args &&... args) : parser_(parser), prev_(parser->context_)
744     {
745         parser_->context_ = ParserContext(&prev_, std::forward<Args>(args)...);
746     }
747 
748     NO_COPY_SEMANTIC(SavedParserContext);
749     DEFAULT_MOVE_SEMANTIC(SavedParserContext);
750 
~SavedParserContext()751     ~SavedParserContext()
752     {
753         parser_->context_ = prev_;
754     }
755 
756 protected:
Binder()757     binder::Binder *Binder()
758     {
759         return parser_->Binder();
760     }
761 
762     ParserImpl *parser_;
763     ParserContext prev_;
764 };
765 
766 class FunctionContext : public SavedParserContext {
767 public:
FunctionContext(ParserImpl * parser,ParserStatus newStatus)768     explicit FunctionContext(ParserImpl *parser, ParserStatus newStatus) : SavedParserContext(parser, newStatus)
769     {
770         if (newStatus & ParserStatus::GENERATOR_FUNCTION) {
771             flags_ |= ir::ScriptFunctionFlags::GENERATOR;
772         }
773 
774         if (newStatus & ParserStatus::ASYNC_FUNCTION) {
775             flags_ |= ir::ScriptFunctionFlags::ASYNC;
776         }
777 
778         if (newStatus & ParserStatus::CONSTRUCTOR_FUNCTION) {
779             flags_ |= ir::ScriptFunctionFlags::CONSTRUCTOR;
780         }
781     }
782 
Flags()783     ir::ScriptFunctionFlags Flags() const
784     {
785         return flags_;
786     }
787 
AddFlag(ir::ScriptFunctionFlags flags)788     void AddFlag(ir::ScriptFunctionFlags flags)
789     {
790         flags_ |= flags;
791     }
792 
793     NO_COPY_SEMANTIC(FunctionContext);
794     NO_MOVE_SEMANTIC(FunctionContext);
795     ~FunctionContext() = default;
796 
797 protected:
798     ir::ScriptFunctionFlags flags_ {ir::ScriptFunctionFlags::NONE};
799 };
800 
801 class ArrowFunctionContext : public FunctionContext {
802 public:
ArrowFunctionContext(ParserImpl * parser,bool isAsync)803     explicit ArrowFunctionContext(ParserImpl *parser, bool isAsync)
804         : FunctionContext(parser, InitialFlags(parser->context_.Status()))
805     {
806         if (isAsync) {
807             AddFlag(ir::ScriptFunctionFlags::ASYNC);
808         }
809 
810         AddFlag(ir::ScriptFunctionFlags::ARROW);
811     }
812 
813     NO_COPY_SEMANTIC(ArrowFunctionContext);
814     NO_MOVE_SEMANTIC(ArrowFunctionContext);
815     ~ArrowFunctionContext() = default;
816 
817 private:
InitialFlags(ParserStatus currentStatus)818     static ParserStatus InitialFlags(ParserStatus currentStatus)
819     {
820         return ParserStatus::FUNCTION | ParserStatus::ARROW_FUNCTION |
821                static_cast<ParserStatus>(currentStatus & (ParserStatus::ALLOW_SUPER | ParserStatus::ALLOW_SUPER_CALL |
822                                          ParserStatus::DISALLOW_ARGUMENTS));
823     }
824 };
825 
826 }  // namespace panda::es2panda::parser
827 
828 #endif
829