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