1 /**
2 * Copyright (c) 2021 - 2023 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 "numberLiteral.h"
17 #include <string>
18
19 #include "checker/TSchecker.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 panda::es2panda::ir {
TransformChildren(const NodeTransformer & cb)26 void NumberLiteral::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {}
Iterate(const NodeTraverser & cb) const27 void NumberLiteral::Iterate([[maybe_unused]] const NodeTraverser &cb) const {}
28
Dump(ir::AstDumper * dumper) const29 void NumberLiteral::Dump(ir::AstDumper *dumper) const
30 {
31 dumper->Add({{"type", "NumberLiteral"}, {"value", number_}});
32 }
33
Dump(ir::SrcDumper * dumper) const34 void NumberLiteral::Dump(ir::SrcDumper *dumper) const
35 {
36 if (number_.IsInt()) {
37 dumper->Add(number_.GetInt());
38 return;
39 }
40
41 if (number_.IsLong()) {
42 dumper->Add(number_.GetLong());
43 return;
44 }
45
46 if (number_.IsFloat()) {
47 dumper->Add(number_.GetFloat());
48 return;
49 }
50
51 if (number_.IsDouble()) {
52 dumper->Add(number_.GetDouble());
53 return;
54 }
55 }
56
Compile(compiler::PandaGen * pg) const57 void NumberLiteral::Compile(compiler::PandaGen *pg) const
58 {
59 pg->GetAstCompiler()->Compile(this);
60 }
61
Compile(compiler::ETSGen * etsg) const62 void NumberLiteral::Compile(compiler::ETSGen *etsg) const
63 {
64 etsg->GetAstCompiler()->Compile(this);
65 }
66
Check(checker::TSChecker * checker)67 checker::Type *NumberLiteral::Check(checker::TSChecker *checker)
68 {
69 return checker->GetAnalyzer()->Check(this);
70 }
71
Check(checker::ETSChecker * checker)72 checker::Type *NumberLiteral::Check(checker::ETSChecker *checker)
73 {
74 return checker->GetAnalyzer()->Check(this);
75 }
76
77 // NOLINTNEXTLINE(google-default-arguments)
Clone(ArenaAllocator * const allocator,AstNode * const parent)78 NumberLiteral *NumberLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent)
79 {
80 if (auto *const clone = allocator->New<NumberLiteral>(number_); clone != nullptr) {
81 if (parent != nullptr) {
82 clone->SetParent(parent);
83 }
84 return clone;
85 }
86
87 throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
88 }
89 } // namespace panda::es2panda::ir
90