• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 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 
IsETSResizableArrayType() const25 bool Type::IsETSResizableArrayType() const
26 {
27     return IsETSObjectType() && AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_ARRAY);
28 }
29 
IsETSStringType() const30 bool Type::IsETSStringType() const
31 {
32     return IsETSObjectType() && AsETSObjectType()->HasObjectFlag(ETSObjectFlags::STRING);
33 }
34 
IsETSCharType() const35 bool Type::IsETSCharType() const
36 {
37     return IsETSObjectType() && AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_CHAR);
38 }
39 
IsETSBigIntType() const40 bool Type::IsETSBigIntType() const
41 {
42     return IsETSObjectType() && AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_BIGINT);
43 }
44 
IsETSAsyncFuncReturnType() const45 bool Type::IsETSAsyncFuncReturnType() const
46 {
47     return IsETSObjectType() && AsETSObjectType()->HasObjectFlag(ETSObjectFlags::ASYNC_FUNC_RETURN_TYPE);
48 }
49 
IsLambdaObject() const50 bool Type::IsLambdaObject() const
51 {
52     if (IsETSObjectType() && (AsETSObjectType()->HasObjectFlag(checker::ETSObjectFlags::FUNCTIONAL_INTERFACE) ||
53                               AsETSObjectType()->HasObjectFlag(checker::ETSObjectFlags::CLASS))) {
54         auto *invoke = AsETSObjectType()->GetOwnProperty<checker::PropertyType::INSTANCE_METHOD>(
55             compiler::Signatures::STATIC_INVOKE_METHOD);
56         if (invoke != nullptr && invoke->TsType() != nullptr && invoke->TsType()->IsETSFunctionType()) {
57             return true;
58         }
59     }
60     return false;
61 }
62 
ToString(std::stringstream & ss) const63 void Type::ToString(std::stringstream &ss) const
64 {
65     ToString(ss, false);
66 }
67 
ToStringAsSrc(std::stringstream & ss) const68 void Type::ToStringAsSrc(std::stringstream &ss) const
69 {
70     ToString(ss);
71 }
72 
ToString() const73 std::string Type::ToString() const
74 {
75     std::stringstream ss {};
76     ToString(ss);
77     return ss.str();
78 }
79 
ToStringAsSrc() const80 std::string Type::ToStringAsSrc() const
81 {
82     std::stringstream ss;
83     ToStringAsSrc(ss);
84     return ss.str();
85 }
86 
ToStringPrecise() const87 std::string Type::ToStringPrecise() const
88 {
89     std::stringstream ss;
90     ToString(ss, true);
91     return ss.str();
92 }
93 
Identical(TypeRelation * relation,Type * other)94 void Type::Identical(TypeRelation *relation, Type *other)
95 {
96     relation->Result(typeFlags_ == other->TypeFlags());
97 }
98 
AssignmentSource(TypeRelation * relation,Type * target)99 bool Type::AssignmentSource([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *target)
100 {
101     return false;
102 }
103 
GetTypeFacts() const104 TypeFacts Type::GetTypeFacts() const
105 {
106     return TypeFacts::NONE;
107 }
108 
Compare(TypeRelation * relation,Type * other)109 void Type::Compare([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *other) {}
110 
Cast(TypeRelation * const relation,Type * target)111 void Type::Cast(TypeRelation *const relation, [[maybe_unused]] Type *target)
112 {
113     relation->Result(false);
114 }
115 
CastTarget(TypeRelation * const relation,Type * source)116 void Type::CastTarget(TypeRelation *const relation, [[maybe_unused]] Type *source)
117 {
118     relation->Result(false);
119 }
120 
IsSupertypeOf(TypeRelation * const relation,Type * source)121 void Type::IsSupertypeOf(TypeRelation *const relation, [[maybe_unused]] Type *source)
122 {
123     relation->Result(false);
124 }
125 
IsSubtypeOf(TypeRelation * const relation,Type * target)126 void Type::IsSubtypeOf(TypeRelation *const relation, [[maybe_unused]] Type *target)
127 {
128     relation->Result(false);
129 }
130 
AsSuper(Checker * checker,varbinder::Variable * sourceVar)131 Type *Type::AsSuper([[maybe_unused]] Checker *checker, [[maybe_unused]] varbinder::Variable *sourceVar)
132 {
133     return nullptr;
134 }
135 
Instantiate(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)136 Type *Type::Instantiate([[maybe_unused]] ArenaAllocator *allocator, [[maybe_unused]] TypeRelation *relation,
137                         [[maybe_unused]] GlobalTypesHolder *globalTypes)
138 {
139     return nullptr;
140 }
141 
Clone(Checker * const checker)142 Type *Type::Clone(Checker *const checker)
143 {
144     return Instantiate(checker->ProgramAllocator(), checker->Relation(), checker->GetGlobalTypesHolder());
145 }
146 
Substitute(TypeRelation * relation,const Substitution * substitution)147 Type *Type::Substitute([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] const Substitution *substitution)
148 {
149     return this;
150 }
151 
GetPrecedence(Type const * type)152 std::uint32_t Type::GetPrecedence(Type const *type) noexcept
153 {
154     ES2PANDA_ASSERT(type != nullptr);
155     if (type->HasTypeFlag(TypeFlag::BYTE)) {
156         return 1U;
157     }
158     if (type->HasTypeFlag(TypeFlag::CHAR)) {
159         return 2U;
160     }
161     if (type->HasTypeFlag(TypeFlag::SHORT)) {
162         return 3U;
163     }
164     if (type->HasTypeFlag(TypeFlag::INT)) {
165         return 4U;
166     }
167     if (type->HasTypeFlag(TypeFlag::LONG)) {
168         return 5U;
169     }
170     if (type->HasTypeFlag(TypeFlag::FLOAT)) {
171         return 6U;
172     }
173     if (type->HasTypeFlag(TypeFlag::DOUBLE)) {
174         return 7U;
175     }
176     if (type->HasTypeFlag(TypeFlag::BIGINT)) {
177         return 8U;
178     }
179     return 0U;
180 }
181 
IsTypeError(Type const * tp)182 bool IsTypeError(Type const *tp)
183 {
184     return tp != nullptr && tp->IsTypeError();
185 }
186 
187 }  // namespace ark::es2panda::checker
188