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