• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "type.h"
17 
18 #include "checker/types/typeFlag.h"
19 #include "checker/types/typeRelation.h"
20 #include "checker/types/ets/etsObjectType.h"
21 #include "checker/checker.h"
22 
23 namespace ark::es2panda::checker {
24 
IsETSStringType() const25 bool Type::IsETSStringType() const
26 {
27     return IsETSObjectType() && AsETSObjectType()->HasObjectFlag(ETSObjectFlags::STRING);
28 }
29 
IsETSBigIntType() const30 bool Type::IsETSBigIntType() const
31 {
32     return IsETSObjectType() && AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_BIGINT);
33 }
34 
IsETSAsyncFuncReturnType() const35 bool Type::IsETSAsyncFuncReturnType() const
36 {
37     return IsETSObjectType() && AsETSObjectType()->HasObjectFlag(ETSObjectFlags::ASYNC_FUNC_RETURN_TYPE);
38 }
39 
IsLambdaObject() const40 bool Type::IsLambdaObject() const
41 {
42     if (IsETSObjectType() && (AsETSObjectType()->HasObjectFlag(checker::ETSObjectFlags::FUNCTIONAL_INTERFACE) ||
43                               AsETSObjectType()->HasObjectFlag(checker::ETSObjectFlags::CLASS))) {
44         auto *invoke = AsETSObjectType()->GetOwnProperty<checker::PropertyType::INSTANCE_METHOD>("invoke");
45         if (invoke != nullptr && invoke->TsType() != nullptr && invoke->TsType()->IsETSFunctionType()) {
46             return true;
47         }
48     }
49     return false;
50 }
51 
ToString(std::stringstream & ss) const52 void Type::ToString(std::stringstream &ss) const
53 {
54     ToString(ss, false);
55 }
56 
ToStringAsSrc(std::stringstream & ss) const57 void Type::ToStringAsSrc(std::stringstream &ss) const
58 {
59     ToString(ss);
60 }
61 
ToString() const62 std::string Type::ToString() const
63 {
64     std::stringstream ss {};
65     ToString(ss);
66     return ss.str();
67 }
68 
ToStringAsSrc() const69 std::string Type::ToStringAsSrc() const
70 {
71     std::stringstream ss;
72     ToStringAsSrc(ss);
73     return ss.str();
74 }
75 
ToStringPrecise() const76 std::string Type::ToStringPrecise() const
77 {
78     std::stringstream ss;
79     ToString(ss, true);
80     return ss.str();
81 }
82 
Identical(TypeRelation * relation,Type * other)83 void Type::Identical(TypeRelation *relation, Type *other)
84 {
85     relation->Result(typeFlags_ == other->TypeFlags());
86 }
87 
AssignmentSource(TypeRelation * relation,Type * target)88 bool Type::AssignmentSource([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *target)
89 {
90     return false;
91 }
92 
GetTypeFacts() const93 TypeFacts Type::GetTypeFacts() const
94 {
95     return TypeFacts::NONE;
96 }
97 
Compare(TypeRelation * relation,Type * other)98 void Type::Compare([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *other) {}
99 
Cast(TypeRelation * const relation,Type * target)100 void Type::Cast(TypeRelation *const relation, [[maybe_unused]] Type *target)
101 {
102     relation->Result(false);
103 }
104 
CastTarget(TypeRelation * const relation,Type * source)105 void Type::CastTarget(TypeRelation *const relation, [[maybe_unused]] Type *source)
106 {
107     relation->Result(false);
108 }
109 
IsSupertypeOf(TypeRelation * const relation,Type * source)110 void Type::IsSupertypeOf(TypeRelation *const relation, [[maybe_unused]] Type *source)
111 {
112     relation->Result(false);
113 }
114 
IsSubtypeOf(TypeRelation * const relation,Type * target)115 void Type::IsSubtypeOf(TypeRelation *const relation, [[maybe_unused]] Type *target)
116 {
117     relation->Result(false);
118 }
119 
AsSuper(Checker * checker,varbinder::Variable * sourceVar)120 Type *Type::AsSuper([[maybe_unused]] Checker *checker, [[maybe_unused]] varbinder::Variable *sourceVar)
121 {
122     return nullptr;
123 }
124 
Instantiate(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)125 Type *Type::Instantiate([[maybe_unused]] ArenaAllocator *allocator, [[maybe_unused]] TypeRelation *relation,
126                         [[maybe_unused]] GlobalTypesHolder *globalTypes)
127 {
128     return nullptr;
129 }
130 
Clone(Checker * const checker)131 Type *Type::Clone(Checker *const checker)
132 {
133     return Instantiate(checker->Allocator(), checker->Relation(), checker->GetGlobalTypesHolder());
134 }
135 
Substitute(TypeRelation * relation,const Substitution * substitution)136 Type *Type::Substitute([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] const Substitution *substitution)
137 {
138     return this;
139 }
140 
GetPrecedence(Type const * type)141 std::uint32_t Type::GetPrecedence(Type const *type) noexcept
142 {
143     ASSERT(type != nullptr);
144     if (type->HasTypeFlag(TypeFlag::BYTE)) {
145         return 1U;
146     }
147     if (type->HasTypeFlag(TypeFlag::CHAR)) {
148         return 2U;
149     }
150     if (type->HasTypeFlag(TypeFlag::SHORT)) {
151         return 3U;
152     }
153     if (type->HasTypeFlag(TypeFlag::INT)) {
154         return 4U;
155     }
156     if (type->HasTypeFlag(TypeFlag::LONG)) {
157         return 5U;
158     }
159     if (type->HasTypeFlag(TypeFlag::FLOAT)) {
160         return 6U;
161     }
162     if (type->HasTypeFlag(TypeFlag::DOUBLE)) {
163         return 7U;
164     }
165     if (type->HasTypeFlag(TypeFlag::BIGINT)) {
166         return 8U;
167     }
168     return 0U;
169 }
170 
IsTypeError(Type const * tp)171 bool IsTypeError(Type const *tp)
172 {
173     return tp != nullptr && tp->IsTypeError();
174 }
175 
176 }  // namespace ark::es2panda::checker
177