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