• 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 #include "etsPrimitiveType.h"
17 
18 #include "checker/TSchecker.h"
19 #include "checker/ETSchecker.h"
20 #include "compiler/core/ETSGen.h"
21 #include "compiler/core/pandagen.h"
22 #include "ir/astDump.h"
23 #include "ir/srcDump.h"
24 
25 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)26 void ETSPrimitiveType::TransformChildren([[maybe_unused]] const NodeTransformer &cb,
27                                          [[maybe_unused]] std::string_view const transformationName)
28 {
29 }
30 
Iterate(const NodeTraverser & cb) const31 void ETSPrimitiveType::Iterate([[maybe_unused]] const NodeTraverser &cb) const {}
32 
Dump(ir::AstDumper * dumper) const33 void ETSPrimitiveType::Dump(ir::AstDumper *dumper) const
34 {
35     dumper->Add({{"type", "ETSPrimitiveType"}});
36 }
37 
Dump(ir::SrcDumper * dumper) const38 void ETSPrimitiveType::Dump(ir::SrcDumper *dumper) const
39 {
40     switch (GetPrimitiveType()) {
41         case PrimitiveType::BYTE:
42             dumper->Add("byte");
43             break;
44         case PrimitiveType::INT:
45             dumper->Add("int");
46             break;
47         case PrimitiveType::LONG:
48             dumper->Add("long");
49             break;
50         case PrimitiveType::SHORT:
51             dumper->Add("short");
52             break;
53         case PrimitiveType::FLOAT:
54             dumper->Add("float");
55             break;
56         case PrimitiveType::DOUBLE:
57             dumper->Add("double");
58             break;
59         case PrimitiveType::BOOLEAN:
60             dumper->Add("boolean");
61             break;
62         case PrimitiveType::CHAR:
63             dumper->Add("char");
64             break;
65         case PrimitiveType::VOID:
66             dumper->Add("void");
67             break;
68         default:
69             UNREACHABLE();
70     }
71 }
72 
Compile(compiler::PandaGen * pg) const73 void ETSPrimitiveType::Compile(compiler::PandaGen *pg) const
74 {
75     pg->GetAstCompiler()->Compile(this);
76 }
77 
Compile(compiler::ETSGen * etsg) const78 void ETSPrimitiveType::Compile(compiler::ETSGen *etsg) const
79 {
80     etsg->GetAstCompiler()->Compile(this);
81 }
82 
Check(checker::TSChecker * checker)83 checker::Type *ETSPrimitiveType::Check(checker::TSChecker *checker)
84 {
85     return checker->GetAnalyzer()->Check(this);
86 }
87 
GetType(checker::TSChecker * checker)88 checker::Type *ETSPrimitiveType::GetType([[maybe_unused]] checker::TSChecker *checker)
89 {
90     return checker->GlobalAnyType();
91 }
92 
Check(checker::ETSChecker * checker)93 checker::Type *ETSPrimitiveType::Check(checker::ETSChecker *checker)
94 {
95     return checker->GetAnalyzer()->Check(this);
96 }
97 
GetType(checker::ETSChecker * checker)98 checker::Type *ETSPrimitiveType::GetType([[maybe_unused]] checker::ETSChecker *checker)
99 {
100     switch (type_) {
101         case PrimitiveType::BYTE: {
102             SetTsType(checker->GlobalByteType());
103             return TsType();
104         }
105         case PrimitiveType::SHORT: {
106             SetTsType(checker->GlobalShortType());
107             return TsType();
108         }
109         case PrimitiveType::INT: {
110             SetTsType(checker->GlobalIntType());
111             return TsType();
112         }
113         case PrimitiveType::LONG: {
114             SetTsType(checker->GlobalLongType());
115             return TsType();
116         }
117         case PrimitiveType::FLOAT: {
118             SetTsType(checker->GlobalFloatType());
119             return TsType();
120         }
121         case PrimitiveType::DOUBLE: {
122             SetTsType(checker->GlobalDoubleType());
123             return TsType();
124         }
125         case PrimitiveType::BOOLEAN: {
126             SetTsType(checker->GlobalETSBooleanType());
127             return TsType();
128         }
129         case PrimitiveType::CHAR: {
130             SetTsType(checker->GlobalCharType());
131             return TsType();
132         }
133         case PrimitiveType::VOID: {
134             checker->CheckVoidAnnotation(this);
135             SetTsType(checker->GlobalVoidType());
136             return TsType();
137         }
138         default: {
139             UNREACHABLE();
140         }
141     }
142 }
143 
Clone(ArenaAllocator * const allocator,AstNode * const parent)144 ETSPrimitiveType *ETSPrimitiveType::Clone(ArenaAllocator *const allocator, AstNode *const parent)
145 {
146     if (auto *const clone = allocator->New<ETSPrimitiveType>(type_); clone != nullptr) {
147         if (parent != nullptr) {
148             clone->SetParent(parent);
149         }
150 
151         clone->SetRange(Range());
152         return clone;
153     }
154 
155     throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
156 }
157 
158 }  // namespace ark::es2panda::ir
159