• 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 
22 namespace panda::es2panda::checker {
IsETSNullType() const23 bool Type::IsETSNullType() const
24 {
25     return IsETSObjectType() && AsETSObjectType()->HasObjectFlag(ETSObjectFlags::NULL_TYPE);
26 }
27 
IsETSUndefinedType() const28 bool Type::IsETSUndefinedType() const
29 {
30     return IsETSObjectType() && AsETSObjectType()->HasObjectFlag(ETSObjectFlags::UNDEFINED_TYPE);
31 }
32 
IsETSNullLike() const33 bool Type::IsETSNullLike() const
34 {
35     // NOTE: vpukhov. should be true for 'null|undefined'
36     return IsETSUndefinedType() || IsETSNullType();
37 }
38 
IsNullish() const39 bool Type::IsNullish() const
40 {
41     return HasTypeFlag(TypeFlag::NULLISH);
42 }
43 
IsNullishOrNullLike() const44 bool Type::IsNullishOrNullLike() const
45 {
46     return IsNullish() || IsETSNullLike();
47 }
48 
ContainsNull() const49 bool Type::ContainsNull() const
50 {
51     return HasTypeFlag(TypeFlag::NULL_TYPE);
52 }
53 
ContainsUndefined() const54 bool Type::ContainsUndefined() const
55 {
56     return HasTypeFlag(TypeFlag::UNDEFINED);
57 }
58 
IsETSStringType() const59 bool Type::IsETSStringType() const
60 {
61     return IsETSObjectType() && AsETSObjectType()->HasObjectFlag(ETSObjectFlags::STRING);
62 }
63 
IsETSBigIntType() const64 bool Type::IsETSBigIntType() const
65 {
66     return IsETSObjectType() && AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_BIGINT);
67 }
68 
IsETSAsyncFuncReturnType() const69 bool Type::IsETSAsyncFuncReturnType() const
70 {
71     return IsETSObjectType() && AsETSObjectType()->HasObjectFlag(ETSObjectFlags::ASYNC_FUNC_RETURN_TYPE);
72 }
73 
IsLambdaObject() const74 bool Type::IsLambdaObject() const
75 {
76     if (IsETSObjectType() && (AsETSObjectType()->HasObjectFlag(checker::ETSObjectFlags::FUNCTIONAL_INTERFACE) ||
77                               AsETSObjectType()->HasObjectFlag(checker::ETSObjectFlags::CLASS))) {
78         auto *invoke = AsETSObjectType()->GetOwnProperty<checker::PropertyType::INSTANCE_METHOD>("invoke");
79         if (invoke != nullptr && invoke->TsType() != nullptr && invoke->TsType()->IsETSFunctionType()) {
80             return true;
81         }
82     }
83     return false;
84 }
85 
ToStringAsSrc(std::stringstream & ss) const86 void Type::ToStringAsSrc(std::stringstream &ss) const
87 {
88     ToString(ss);
89 }
90 
Identical(TypeRelation * relation,Type * other)91 void Type::Identical(TypeRelation *relation, Type *other)
92 {
93     relation->Result(typeFlags_ == other->TypeFlags());
94 }
95 
AssignmentSource(TypeRelation * relation,Type * target)96 bool Type::AssignmentSource([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *target)
97 {
98     return false;
99 }
100 
GetTypeFacts() const101 TypeFacts Type::GetTypeFacts() const
102 {
103     return TypeFacts::NONE;
104 }
105 
Compare(TypeRelation * relation,Type * other)106 void Type::Compare([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *other) {}
107 
Cast(TypeRelation * const relation,Type * target)108 void Type::Cast(TypeRelation *const relation, [[maybe_unused]] Type *target)
109 {
110     relation->Result(false);
111 }
112 
CastTarget(TypeRelation * const relation,Type * source)113 void Type::CastTarget(TypeRelation *const relation, [[maybe_unused]] Type *source)
114 {
115     relation->Result(false);
116 }
117 
IsSupertypeOf(TypeRelation * const relation,Type * source)118 void Type::IsSupertypeOf(TypeRelation *const relation, [[maybe_unused]] Type *source)
119 {
120     relation->Result(false);
121 }
122 
IsSubtypeOf(TypeRelation * const relation,Type * target)123 void Type::IsSubtypeOf(TypeRelation *const relation, [[maybe_unused]] Type *target)
124 {
125     relation->Result(false);
126 }
127 
AsSuper(Checker * checker,varbinder::Variable * sourceVar)128 Type *Type::AsSuper([[maybe_unused]] Checker *checker, [[maybe_unused]] varbinder::Variable *sourceVar)
129 {
130     return nullptr;
131 }
132 
Instantiate(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)133 Type *Type::Instantiate([[maybe_unused]] ArenaAllocator *allocator, [[maybe_unused]] TypeRelation *relation,
134                         [[maybe_unused]] GlobalTypesHolder *globalTypes)
135 {
136     return nullptr;
137 }
138 
Substitute(TypeRelation * relation,const Substitution * substitution)139 Type *Type::Substitute([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] const Substitution *substitution)
140 {
141     return this;
142 }
143 }  // namespace panda::es2panda::checker
144