• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 - 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 #ifndef PANDA_GLOBALDECLTRANSFORMER_H
17 #define PANDA_GLOBALDECLTRANSFORMER_H
18 
19 #include "util/helpers.h"
20 #include "compiler/lowering/phase.h"
21 #include "ir/visitor/IterateAstVisitor.h"
22 
23 namespace ark::es2panda::compiler {
24 
25 class GlobalDeclTransformer : public ir::visitor::CustomAstVisitor {
26     const std::unordered_set<ir::AstNodeType> typeDecl_ = {
27         ir::AstNodeType::CLASS_DECLARATION,
28         ir::AstNodeType::STRUCT_DECLARATION,
29         ir::AstNodeType::TS_ENUM_DECLARATION,
30         ir::AstNodeType::TS_INTERFACE_DECLARATION,
31         ir::AstNodeType::ETS_PACKAGE_DECLARATION,
32         ir::AstNodeType::ETS_IMPORT_DECLARATION,
33         ir::AstNodeType::TS_TYPE_ALIAS_DECLARATION,
34         ir::AstNodeType::EXPORT_ALL_DECLARATION,
35         ir::AstNodeType::EXPORT_NAMED_DECLARATION,
36         ir::AstNodeType::REEXPORT_STATEMENT,
37         ir::AstNodeType::ETS_MODULE,
38         ir::AstNodeType::ANNOTATION_DECLARATION,
39     };
40 
41     const std::unordered_set<ir::AstNodeType> propertiesDecl_ = {
42         ir::AstNodeType::FUNCTION_DECLARATION,
43         ir::AstNodeType::VARIABLE_DECLARATION,
44     };
45 
46 public:
47     struct ResultT {
ResultTResultT48         explicit ResultT(ArenaAllocator *alloc)
49             : classProperties(alloc->Adapter()), immediateInit(alloc->Adapter()), initializerBlocks(alloc->Adapter())
50         {
51         }
52 
53         // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
54         ArenaVector<ir::Statement *> classProperties;
55         // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
56         ArenaVector<ir::Statement *> immediateInit;
57         // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
58         ArenaVector<ArenaVector<ir::Statement *>> initializerBlocks;
59     };
60 
GlobalDeclTransformer(ArenaAllocator * allocator,ir::Statement const * currentModule,parser::ETSParser * const parser)61     explicit GlobalDeclTransformer(ArenaAllocator *allocator, ir::Statement const *currentModule,
62                                    parser::ETSParser *const parser)
63         : allocator_(allocator), result_(allocator), currentModule_(currentModule), parser_(parser)
64     {
65     }
66 
67     /**
68      * Removes top level statements, global variable declarations, global function declarations
69      * @param stmts
70      */
71     void FilterDeclarations(ArenaVector<ir::Statement *> &stmts);
72 
73     /**
74      * Creates ClassProperty for global variables and MethodFunction for global functions.
75      * Copy top level statements to vector.
76      * @param stmts top level statements
77      * @param addInitializer $init$ should contain global variable initializers
78      * @return pair (class properties, init statements)
79      */
80     ResultT TransformStatements(const ArenaVector<ir::Statement *> &stmts);
81 
82     void VisitFunctionDeclaration(ir::FunctionDeclaration *funcDecl) override;
83     void VisitVariableDeclaration(ir::VariableDeclaration *varDecl) override;
84     void VisitClassStaticBlock(ir::ClassStaticBlock *classStaticBlock) override;
85     void HandleNode(ir::AstNode *node) override;
86     bool CheckValidInitializer(ir::AstNode const *initializer) const;
87 
88     ir::Identifier *RefIdent(const util::StringView &name);
89 
90     ir::ExpressionStatement *InitTopLevelProperty(ir::ClassProperty *classProperty);
91 
IsMultiInitializer()92     [[nodiscard]] bool IsMultiInitializer() const
93     {
94         return initializerBlockCount_ > 1;
95     }
96 
97 private:
98     ArenaAllocator *allocator_;
99     ResultT result_;
100     ir::Statement const *currentModule_;
101     parser::ETSParser *const parser_;
102     size_t initializerBlockCount_ = 0;
103 };
104 
105 }  // namespace ark::es2panda::compiler
106 #endif  // PANDA_GLOBALDECLTRANSFORMER_H
107