• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-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_UNION_NORMALISATION_TEST_H
17 #define PANDA_UNION_NORMALISATION_TEST_H
18 
19 namespace ark::es2panda::gtests {
20 
21 class UnionNormalizationTest : public testing::Test {
22 public:
UnionNormalizationTest()23     UnionNormalizationTest()
24         : allocator_(std::make_unique<ArenaAllocator>(SpaceType::SPACE_TYPE_COMPILER)),
25           publicContext_ {std::make_unique<public_lib::Context>()},
26           program_ {parser::Program::NewProgram<varbinder::ETSBinder>(Allocator())},
27           es2pandaPath_ {test::utils::PandaExecutablePathGetter {}.Get()}
28     {
29     }
30 
31     ~UnionNormalizationTest() override = default;
32 
SetUpTestCase()33     static void SetUpTestCase()
34     {
35         constexpr auto COMPILER_SIZE = operator""_MB(256ULL);
36         mem::MemConfig::Initialize(0, 0, COMPILER_SIZE, 0, 0, 0);
37         PoolManager::Initialize();
38     }
39 
Allocator()40     ArenaAllocator *Allocator()
41     {
42         return allocator_.get();
43     }
44 
Program()45     parser::Program *Program()
46     {
47         return &program_;
48     }
49 
Checker()50     checker::ETSChecker *Checker()
51     {
52         return &checker_;
53     }
54 
InitializeChecker(std::string_view fileName,std::string_view src)55     void InitializeChecker(std::string_view fileName, std::string_view src)
56     {
57         auto es2pandaPathPtr = es2pandaPath_.c_str();
58         ASSERT(es2pandaPathPtr);
59 
60         InitializeChecker<parser::ETSParser, varbinder::ETSBinder, checker::ETSChecker, checker::ETSAnalyzer,
61                           compiler::ETSCompiler, compiler::ETSGen, compiler::StaticRegSpiller,
62                           compiler::ETSFunctionEmitter, compiler::ETSEmitter>(&es2pandaPathPtr, fileName, src,
63                                                                               &checker_, &program_);
64     }
65 
66     template <typename CodeGen, typename RegSpiller, typename FunctionEmitter, typename Emitter, typename AstCompiler>
MakeCompileJob()67     public_lib::Context::CodeGenCb MakeCompileJob()
68     {
69         return [this](public_lib::Context *context, varbinder::FunctionScope *scope,
70                       compiler::ProgramElement *programElement) -> void {
71             RegSpiller regSpiller;
72             AstCompiler astcompiler;
73             CodeGen cg(allocator_.get(), &regSpiller, context, scope, programElement, &astcompiler);
74             FunctionEmitter funcEmitter(&cg, programElement);
75             funcEmitter.Generate();
76         };
77     }
78 
79     template <typename Parser, typename VarBinder, typename Checker, typename Analyzer, typename AstCompiler,
80               typename CodeGen, typename RegSpiller, typename FunctionEmitter, typename Emitter>
InitializeChecker(const char ** argv,std::string_view fileName,std::string_view src,checker::ETSChecker * checker,parser::Program * program)81     void InitializeChecker(const char **argv, std::string_view fileName, std::string_view src,
82                            checker::ETSChecker *checker, parser::Program *program)
83     {
84         auto options = std::make_unique<ark::es2panda::util::Options>();
85         if (!options->Parse(1, argv)) {
86             std::cerr << options->ErrorMsg() << std::endl;
87             return;
88         }
89 
90         ark::Logger::ComponentMask mask {};
91         mask.set(ark::Logger::Component::ES2PANDA);
92         ark::Logger::InitializeStdLogging(ark::Logger::LevelFromString(options->LogLevel()), mask);
93 
94         Compiler compiler(options->Extension(), options->ThreadCount());
95         SourceFile input(fileName, src, options->ParseModule());
96         compiler::CompilationUnit unit {input, *options, 0, options->Extension()};
97         auto getPhases = compiler::GetPhaseList(ScriptExtension::ETS);
98 
99         program->MarkEntry();
100         auto parser =
101             Parser(program, unit.options.CompilerOptions(), static_cast<parser::ParserStatus>(unit.rawParserStatus));
102         auto analyzer = Analyzer(checker);
103         checker->SetAnalyzer(&analyzer);
104 
105         auto *varbinder = program->VarBinder();
106         varbinder->SetProgram(program);
107 
108         varbinder->SetContext(publicContext_.get());
109 
110         auto emitter = Emitter(publicContext_.get());
111 
112         auto config = public_lib::ConfigImpl {};
113         publicContext_->config = &config;
114         publicContext_->config->options = &unit.options;
115         publicContext_->sourceFile = &unit.input;
116         publicContext_->allocator = allocator_.get();
117         publicContext_->parser = &parser;
118         publicContext_->checker = checker;
119         publicContext_->analyzer = publicContext_->checker->GetAnalyzer();
120         publicContext_->emitter = &emitter;
121         publicContext_->parserProgram = program;
122 
123         parser.ParseScript(unit.input, unit.options.CompilerOptions().compilationMode == CompilationMode::GEN_STD_LIB);
124         for (auto *phase : getPhases) {
125             if (!phase->Apply(publicContext_.get(), program)) {
126                 return;
127             }
128         }
129     }
130 
FindClassType(varbinder::ETSBinder * varbinder,std::string_view className)131     static checker::Type *FindClassType(varbinder::ETSBinder *varbinder, std::string_view className)
132     {
133         auto classDefs = varbinder->AsETSBinder()->GetRecordTable()->ClassDefinitions();
134         auto baseClass = std::find_if(classDefs.begin(), classDefs.end(), [className](ir::ClassDefinition *cdef) {
135             return cdef->Ident()->Name().Is(className);
136         });
137         if (baseClass == classDefs.end()) {
138             return nullptr;
139         }
140         return (*baseClass)->TsType();
141     }
142 
FindTypeAlias(checker::ETSChecker * checker,std::string_view aliasName)143     static checker::Type *FindTypeAlias(checker::ETSChecker *checker, std::string_view aliasName)
144     {
145         auto *foundVar =
146             checker->Scope()->FindLocal(aliasName, varbinder::ResolveBindingOptions::TYPE_ALIASES)->AsLocalVariable();
147         if (foundVar == nullptr) {
148             return nullptr;
149         }
150         return foundVar->Declaration()->Node()->AsTSTypeAliasDeclaration()->TypeAnnotation()->TsType();
151     }
152 
153     NO_COPY_SEMANTIC(UnionNormalizationTest);
154     NO_MOVE_SEMANTIC(UnionNormalizationTest);
155 
156 protected:
157     static constexpr uint8_t SIZE2 = 2;
158     static constexpr uint8_t SIZE3 = 3;
159     static constexpr uint8_t IDX0 = 0;
160     static constexpr uint8_t IDX1 = 1;
161     static constexpr uint8_t IDX2 = 2;
162 
163 private:
164     std::unique_ptr<ArenaAllocator> allocator_;
165     std::unique_ptr<public_lib::Context> publicContext_;
166     parser::Program program_;
167     std::string es2pandaPath_;
168     checker::ETSChecker checker_;
169 };
170 
171 }  // namespace ark::es2panda::gtests
172 #endif  // PANDA_UNION_NORMALISATION_TEST_H
173