13af6ab5fSopenharmony_ci/* 23af6ab5fSopenharmony_ci * Copyright (c) 2023 - 2024 Huawei Device Co., Ltd. 33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License. 53af6ab5fSopenharmony_ci * You may obtain a copy of the License at 63af6ab5fSopenharmony_ci * 73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83af6ab5fSopenharmony_ci * 93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and 133af6ab5fSopenharmony_ci * limitations under the License. 143af6ab5fSopenharmony_ci */ 153af6ab5fSopenharmony_ci 163af6ab5fSopenharmony_ci#ifndef PANDA_GLOBALCLASSHANDLER_H 173af6ab5fSopenharmony_ci#define PANDA_GLOBALCLASSHANDLER_H 183af6ab5fSopenharmony_ci 193af6ab5fSopenharmony_ci#include "parser/program/program.h" 203af6ab5fSopenharmony_ci#include "public/public.h" 213af6ab5fSopenharmony_ci#include "ir/astNode.h" 223af6ab5fSopenharmony_ci 233af6ab5fSopenharmony_cinamespace ark::es2panda::compiler { 243af6ab5fSopenharmony_ci 253af6ab5fSopenharmony_ciclass GlobalClassHandler { 263af6ab5fSopenharmony_cipublic: 273af6ab5fSopenharmony_ci using ModuleDependencies = ArenaUnorderedSet<parser::Program *>; 283af6ab5fSopenharmony_ci 293af6ab5fSopenharmony_ci struct GlobalStmts { 303af6ab5fSopenharmony_ci parser::Program *program; 313af6ab5fSopenharmony_ci ArenaVector<ir::Statement *> statements; 323af6ab5fSopenharmony_ci }; 333af6ab5fSopenharmony_ci explicit GlobalClassHandler(parser::ETSParser *parser, ArenaAllocator *allocator) 343af6ab5fSopenharmony_ci : parser_(parser), allocator_(allocator) {}; 353af6ab5fSopenharmony_ci 363af6ab5fSopenharmony_ci /** 373af6ab5fSopenharmony_ci * Each "Module" has it's own global class, which contains all top level statements across "module" 383af6ab5fSopenharmony_ci * Result - creation of global class and _$init$_ method 393af6ab5fSopenharmony_ci * @param programs - vector of files in module 403af6ab5fSopenharmony_ci */ 413af6ab5fSopenharmony_ci void SetupGlobalClass(const ArenaVector<parser::Program *> &programs, const ModuleDependencies *moduleDependencies); 423af6ab5fSopenharmony_ci 433af6ab5fSopenharmony_ciprivate: 443af6ab5fSopenharmony_ci /** 453af6ab5fSopenharmony_ci * Move top level statements to _$init$_ and 463af6ab5fSopenharmony_ci * @param program program of module 473af6ab5fSopenharmony_ci * @param init_statements statements which should be executed 483af6ab5fSopenharmony_ci */ 493af6ab5fSopenharmony_ci void SetupGlobalMethods(parser::Program *program, ArenaVector<ir::Statement *> &&statements, 503af6ab5fSopenharmony_ci bool mainExists = false, bool topLevelStatementsExist = false); 513af6ab5fSopenharmony_ci 523af6ab5fSopenharmony_ci ir::ClassDeclaration *CreateGlobalClass(); 533af6ab5fSopenharmony_ci ir::ClassStaticBlock *CreateStaticBlock(ir::ClassDefinition *classDef); 543af6ab5fSopenharmony_ci ir::MethodDefinition *CreateGlobalMethod(const std::string_view name, ArenaVector<ir::Statement *> &&statements); 553af6ab5fSopenharmony_ci void AddInitCallFromStaticBlock(ir::ClassDefinition *globalClass, ir::MethodDefinition *initMethod); 563af6ab5fSopenharmony_ci 573af6ab5fSopenharmony_ci ArenaVector<ir::Statement *> FormInitMethodStatements(parser::Program *program, 583af6ab5fSopenharmony_ci const ModuleDependencies *moduleDependencies, 593af6ab5fSopenharmony_ci ArenaVector<GlobalStmts> &&initStatements); 603af6ab5fSopenharmony_ci 613af6ab5fSopenharmony_ci void FormDependentInitTriggers(ArenaVector<ir::Statement *> &statements, 623af6ab5fSopenharmony_ci const ModuleDependencies *moduleDependencies); 633af6ab5fSopenharmony_ci 643af6ab5fSopenharmony_ci /** 653af6ab5fSopenharmony_ci * 663af6ab5fSopenharmony_ci * @param program leave only declarations here 673af6ab5fSopenharmony_ci * @param class_def add new properties such as methods and fields 683af6ab5fSopenharmony_ci * @param addInitializer $init$ should contain global variable initializers 693af6ab5fSopenharmony_ci * @return Statements, which should be executed before the start 703af6ab5fSopenharmony_ci */ 713af6ab5fSopenharmony_ci ArenaVector<ir::Statement *> CollectProgramGlobalStatements(parser::Program *program, ir::ClassDefinition *classDef, 723af6ab5fSopenharmony_ci bool addInitializer); 733af6ab5fSopenharmony_ci 743af6ab5fSopenharmony_ci ir::Identifier *RefIdent(const util::StringView &name); 753af6ab5fSopenharmony_ci util::UString ReplaceSpecialCharacters(util::UString *word) const; 763af6ab5fSopenharmony_ci 773af6ab5fSopenharmony_ci parser::ETSParser *const parser_; 783af6ab5fSopenharmony_ci ArenaAllocator *const allocator_; 793af6ab5fSopenharmony_ci}; 803af6ab5fSopenharmony_ci} // namespace ark::es2panda::compiler 813af6ab5fSopenharmony_ci 823af6ab5fSopenharmony_ci#endif // PANDA_GLOBALCLASSHANDLER_H 83