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_GLOBALCLASSHANDLER_H 17 #define PANDA_GLOBALCLASSHANDLER_H 18 19 #include "compiler/lowering/ets/topLevelStmts/globalDeclTransformer.h" 20 #include "ir/ets/etsReExportDeclaration.h" 21 #include "parser/program/program.h" 22 #include "public/public.h" 23 #include "ir/astNode.h" 24 25 namespace ark::es2panda::compiler { 26 27 class GlobalClassHandler { 28 public: 29 using ModuleDependencies = ArenaUnorderedSet<parser::Program *>; 30 31 struct GlobalStmts { 32 parser::Program *program; 33 ArenaVector<ir::Statement *> statements; 34 }; GlobalClassHandler(parser::ETSParser * parser,ArenaAllocator * allocator)35 explicit GlobalClassHandler(parser::ETSParser *parser, ArenaAllocator *allocator) 36 : parser_(parser), allocator_(allocator), packageInitializerBlockCount_(allocator->Adapter()) {}; 37 38 /** 39 * Each "Module" has it's own global class, which contains all top level statements across "module" 40 * Result - creation of global class and _$init$_ method 41 * @param programs - vector of files in module 42 */ 43 void SetupGlobalClass(const ArenaVector<parser::Program *> &programs, const ModuleDependencies *moduleDependencies); 44 void static MergeNamespace(ArenaVector<ir::ETSModule *> &namespaces, parser::Program *program); 45 void CheckPackageMultiInitializerBlock(util::StringView packageName, 46 const ArenaVector<ArenaVector<ir::Statement *>> &initializerBlocks); 47 48 private: 49 /** 50 * Move top level statements to _$init$_ and 51 * @param program program of module 52 * @param init_statements statements which should be executed 53 */ 54 void SetupGlobalMethods(parser::Program *program, ArenaVector<ir::Statement *> &&statements); 55 void AddStaticBlockToClass(ir::AstNode *node); 56 void CollectProgramGlobalClasses(parser::Program *program, ArenaVector<ir::ETSModule *> namespaces); 57 ir::ClassDeclaration *TransformNamespace(ir::ETSModule *ns, parser::Program *program); 58 ir::ClassDeclaration *CreateTransformedClass(ir::ETSModule *ns); 59 template <class Node> 60 void CollectExportedClasses(parser::Program *program, ir::ClassDefinition *classDef, 61 const ArenaVector<Node *> &statements); 62 void CollectReExportedClasses(parser::Program *program, ir::ClassDefinition *classDef, 63 const ir::ETSReExportDeclaration *reExport); 64 void CollectNamespaceExportedClasses(parser::Program *program, ir::ClassDefinition *classDef); 65 void SetupGlobalMethods(parser::Program *program, ArenaVector<ir::Statement *> &&initStatements, 66 ir::ClassDefinition *globalClass, bool isDeclare); 67 void SetupInitializerBlock(parser::Program *program, ArenaVector<ArenaVector<ir::Statement *>> &&initializerBlock, 68 ir::ClassDefinition *globalClass); 69 ArenaVector<ir::ClassDeclaration *> TransformNamespaces(ArenaVector<ir::ETSModule *> &namespaces, 70 parser::Program *program); 71 72 ir::ClassDeclaration *CreateGlobalClass(const parser::Program *globalProgram); 73 ir::ClassStaticBlock *CreateStaticBlock(ir::ClassDefinition *classDef); 74 ir::MethodDefinition *CreateGlobalMethod(std::string_view name, ArenaVector<ir::Statement *> &&statements, 75 const parser::Program *program); 76 void AddInitCallToStaticBlock(ir::ClassDefinition *globalClass, ir::MethodDefinition *initMethod); 77 void AddInitializerBlockToStaticBlock(ir::ClassDefinition *globalClass, 78 ArenaVector<ir::Statement *> &&initializerBlocks); 79 80 ArenaVector<ArenaVector<ir::Statement *>> FormInitStaticBlockMethodStatements( 81 parser::Program *program, const ModuleDependencies *moduleDependencies, 82 ArenaVector<GlobalStmts> &&initStatements); 83 void TransformBrokenNamespace(ir::AstNode *node, parser::Program *program); 84 85 ArenaVector<ir::Statement *> FormInitMethodStatements(parser::Program *program, 86 const ModuleDependencies *moduleDependencies, 87 ArenaVector<GlobalStmts> &&initStatements); 88 89 void FormDependentInitTriggers(ArenaVector<ir::Statement *> &statements, 90 const ModuleDependencies *moduleDependencies); 91 92 GlobalDeclTransformer::ResultT CollectProgramGlobalStatements(ArenaVector<ir::Statement *> &stmts, 93 ir::ClassDefinition *classDef, 94 ir::Statement const *stmt); 95 96 ir::Identifier *RefIdent(const util::StringView &name); 97 98 parser::ETSParser *const parser_; 99 ArenaAllocator *const allocator_; 100 ArenaUnorderedSet<util::StringView> packageInitializerBlockCount_; 101 }; 102 } // namespace ark::es2panda::compiler 103 104 #endif // PANDA_GLOBALCLASSHANDLER_H 105