1 /**
2 * Copyright (c) 2021-2022 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 "numberLiteralType.h"
17
18 #include <binder/variable.h>
19 #include <typescript/types/enumType.h>
20
21 namespace panda::es2panda::checker {
22
ToString(std::stringstream & ss) const23 void NumberLiteralType::ToString(std::stringstream &ss) const
24 {
25 ss << util::Helpers::ToString(value_);
26 }
27
ToStringAsSrc(std::stringstream & ss) const28 void NumberLiteralType::ToStringAsSrc(std::stringstream &ss) const
29 {
30 ss << "number";
31 }
32
Identical(TypeRelation * relation,Type * other)33 void NumberLiteralType::Identical(TypeRelation *relation, Type *other)
34 {
35 if (other->IsNumberLiteralType()) {
36 relation->Result(value_ == other->AsNumberLiteralType()->Value());
37 }
38 }
39
AssignmentTarget(TypeRelation * relation,Type * source)40 void NumberLiteralType::AssignmentTarget(TypeRelation *relation, Type *source)
41 {
42 if (source->IsEnumType()) {
43 const EnumType *sourceEnumType = source->AsEnumType();
44 const binder::EnumVariable *enumVar = sourceEnumType->EnumVar();
45
46 if (std::holds_alternative<double>(enumVar->Value()) && value_ == std::get<double>(enumVar->Value())) {
47 relation->Result(true);
48 }
49 }
50 }
51
GetTypeFacts() const52 TypeFacts NumberLiteralType::GetTypeFacts() const
53 {
54 return value_ == 0 ? TypeFacts::ZERO_NUMBER_FACTS : TypeFacts::NON_ZERO_NUMBER_FACTS;
55 }
56
Instantiate(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)57 Type *NumberLiteralType::Instantiate([[maybe_unused]] ArenaAllocator *allocator,
58 [[maybe_unused]] TypeRelation *relation,
59 [[maybe_unused]] GlobalTypesHolder *globalTypes)
60 {
61 return this;
62 }
63
64 } // namespace panda::es2panda::checker
65