1/**
2 * Copyright (c) 2021 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 "literal.h"
17
18#include <ir/expressions/literals/bigIntLiteral.h>
19#include <ir/expressions/literals/booleanLiteral.h>
20#include <ir/expressions/literals/numberLiteral.h>
21#include <ir/expressions/literals/stringLiteral.h>
22#include <ir/expressions/literals/taggedLiteral.h>
23
24namespace panda::es2panda::ir {
25
26bool Literal::GetBoolean() const
27{
28    ASSERT(IsBooleanLiteral());
29    return AsBooleanLiteral()->Value();
30}
31
32uint32_t Literal::GetInt() const
33{
34    ASSERT(IsNumberLiteral() && AsNumberLiteral()->IsInteger());
35    return AsNumberLiteral()->Number<uint32_t>();
36}
37
38double Literal::GetDouble() const
39{
40    ASSERT(IsNumberLiteral());
41    return AsNumberLiteral()->Number<double>();
42}
43
44const util::StringView &Literal::GetString() const
45{
46    if (IsStringLiteral()) {
47        return AsStringLiteral()->Str();
48    }
49    if (IsNumberLiteral()) {
50        return AsNumberLiteral()->Str();
51    }
52    ASSERT(IsTaggedLiteral());
53    return AsTaggedLiteral()->Str();
54}
55
56const util::StringView &Literal::GetMethod() const
57{
58    ASSERT(IsTaggedLiteral());
59    return AsTaggedLiteral()->Method();
60}
61
62uint16_t Literal::GetMethodAffiliate() const
63{
64    ASSERT(IsTaggedLiteral());
65    return AsTaggedLiteral()->MethodAffiliate();
66}
67
68std::optional<util::StringView> Literal::GetName() const
69{
70    if (IsStringLiteral()) {
71        return AsStringLiteral()->Str();
72    }
73    if (IsNumberLiteral()) {
74        return AsNumberLiteral()->Str();
75    }
76    if (IsBigIntLiteral()) {
77        return AsBigIntLiteral()->Str();
78    }
79    if (IsTaggedLiteral()) {
80        return AsTaggedLiteral()->Str();
81    }
82    return std::nullopt;
83}
84
85}  // namespace panda::es2panda::ir
86