• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 - 2024 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_GLOBALCLASSHANDLER_H
17 #define PANDA_GLOBALCLASSHANDLER_H
18 
19 #include "parser/program/program.h"
20 #include "public/public.h"
21 #include "ir/astNode.h"
22 
23 namespace ark::es2panda::compiler {
24 
25 class GlobalClassHandler {
26 public:
27     struct GlobalStmts {
28         parser::Program *program;
29         ArenaVector<ir::Statement *> statements;
30     };
GlobalClassHandler(ArenaAllocator * allocator)31     explicit GlobalClassHandler(ArenaAllocator *allocator) : allocator_(allocator) {};
32 
33     /**
34      * Each "Module" has it's own global class, which contains all top level statements across "module"
35      * Result - creation of global class and _$init$_ method
36      * @param programs - vector of files in module
37      */
38     void InitGlobalClass(const ArenaVector<parser::Program *> &programs);
39 
40 private:
41     /**
42      * Move top level statements to _$init$_ and
43      * @param program program of module
44      * @param init_statements statements which should be executed
45      */
46     void InitCallToCCTOR(parser::Program *program, const ArenaVector<GlobalStmts> &initStatements,
47                          bool mainExists = false, bool topLevelStatementsExist = false);
48 
49 private:
50     void InitGlobalClass(ir::ClassDefinition *classDef, parser::ScriptKind scriptKind);
51     ir::ClassDeclaration *CreateGlobalClass();
52     ir::ClassStaticBlock *CreateCCtor(const ArenaVector<ir::AstNode *> &properties, const lexer::SourcePosition &loc,
53                                       bool allowEmptyCctor);
54 
55     /**
56      *
57      * @param global_stmts leave only declarations here
58      * @param class_def add new properties such as methods and fields
59      * @param addInitializer $init$ should contain global variable initializers
60      * @return Statements, which should be executed before the start
61      */
62     ArenaVector<ir::Statement *> MakeGlobalStatements(ir::BlockStatement *globalStmts, ir::ClassDefinition *classDef,
63                                                       bool addInitializer);
64 
65     void AddInitCall(ir::ClassDefinition *globalClass, ir::MethodDefinition *initMethod);
66 
67     ir::Identifier *RefIdent(const util::StringView &name);
68 
69 private:
70     constexpr static std::string_view INIT_NAME = compiler::Signatures::INIT_METHOD;
71     ArenaAllocator *const allocator_;
72 };
73 }  // namespace ark::es2panda::compiler
74 
75 #endif  // PANDA_GLOBALCLASSHANDLER_H
76