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 24 namespace panda::es2panda::ir { 25 GetBoolean() const26bool Literal::GetBoolean() const 27 { 28 ASSERT(IsBooleanLiteral()); 29 return AsBooleanLiteral()->Value(); 30 } 31 GetInt() const32uint32_t Literal::GetInt() const 33 { 34 ASSERT(IsNumberLiteral() && AsNumberLiteral()->IsInteger()); 35 return AsNumberLiteral()->Number<uint32_t>(); 36 } 37 GetDouble() const38double Literal::GetDouble() const 39 { 40 ASSERT(IsNumberLiteral()); 41 return AsNumberLiteral()->Number<double>(); 42 } 43 GetString() const44const util::StringView &Literal::GetString() const 45 { 46 if (IsStringLiteral()) { 47 return AsStringLiteral()->Str(); 48 } 49 ASSERT(IsNumberLiteral()); 50 return AsNumberLiteral()->Str(); 51 } 52 GetMethod() const53const util::StringView &Literal::GetMethod() const 54 { 55 ASSERT(IsTaggedLiteral()); 56 return AsTaggedLiteral()->Method(); 57 } 58 GetMethodAffiliate() const59uint16_t Literal::GetMethodAffiliate() const 60 { 61 ASSERT(IsTaggedLiteral()); 62 return AsTaggedLiteral()->MethodAffiliate(); 63 } 64 GetName() const65std::optional<util::StringView> Literal::GetName() const 66 { 67 if (IsStringLiteral()) { 68 return AsStringLiteral()->Str(); 69 } 70 if (IsNumberLiteral()) { 71 return AsNumberLiteral()->Str(); 72 } 73 if (IsBigIntLiteral()) { 74 return AsBigIntLiteral()->Str(); 75 } 76 if (IsTaggedLiteral()) { 77 return AsTaggedLiteral()->Str(); 78 } 79 return std::nullopt; 80 } 81 82 } // namespace panda::es2panda::ir 83