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 "numberLiteral.h"
17
18#include "checker/TSchecker.h"
19#include "compiler/core/ETSGen.h"
20#include "compiler/core/pandagen.h"
21#include "ir/astDump.h"
22#include "ir/srcDump.h"
23
24namespace ark::es2panda::ir {
25void NumberLiteral::TransformChildren([[maybe_unused]] const NodeTransformer &cb,
26                                      [[maybe_unused]] std::string_view const transformationName)
27{
28}
29
30void NumberLiteral::Iterate([[maybe_unused]] const NodeTraverser &cb) const {}
31
32void NumberLiteral::Dump(ir::AstDumper *dumper) const
33{
34    dumper->Add({{"type", "NumberLiteral"}, {"value", number_}});
35}
36
37void NumberLiteral::Dump(ir::SrcDumper *dumper) const
38{
39    if (std::string(number_.Str()).empty() || (parent_ != nullptr && parent_->IsTSEnumMember())) {
40        if (number_.IsInt()) {
41            dumper->Add(number_.GetInt());
42            return;
43        }
44
45        if (number_.IsLong()) {
46            dumper->Add(number_.GetLong());
47            return;
48        }
49
50        if (number_.IsFloat()) {
51            dumper->Add(number_.GetFloat());
52            return;
53        }
54
55        if (number_.IsDouble()) {
56            dumper->Add(number_.GetDouble());
57            return;
58        }
59    }
60    dumper->Add(std::string(number_.Str()));
61}
62
63void NumberLiteral::Compile(compiler::PandaGen *pg) const
64{
65    pg->GetAstCompiler()->Compile(this);
66}
67
68void NumberLiteral::Compile(compiler::ETSGen *etsg) const
69{
70    etsg->GetAstCompiler()->Compile(this);
71}
72
73checker::Type *NumberLiteral::Check(checker::TSChecker *checker)
74{
75    return checker->GetAnalyzer()->Check(this);
76}
77
78checker::Type *NumberLiteral::Check(checker::ETSChecker *checker)
79{
80    return checker->GetAnalyzer()->Check(this);
81}
82
83NumberLiteral *NumberLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent)
84{
85    if (auto *const clone = allocator->New<NumberLiteral>(number_); clone != nullptr) {
86        if (parent != nullptr) {
87            clone->SetParent(parent);
88        }
89        clone->SetRange(Range());
90        return clone;
91    }
92
93    throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
94}
95}  // namespace ark::es2panda::ir
96