• 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 "etsDynamicType.h"
17 #include "checker/ETSchecker.h"
18 #include "checker/ets/conversion.h"
19 #include "checker/types/ets/etsDynamicFunctionType.h"
20 
21 namespace ark::es2panda::checker {
22 
GetPropertyDynamic(const util::StringView & name,const ETSChecker * checker) const23 varbinder::LocalVariable *ETSDynamicType::GetPropertyDynamic(const util::StringView &name,
24                                                              const ETSChecker *checker) const
25 {
26     auto it = propertiesCache_.find(name);
27     if (it != propertiesCache_.end()) {
28         return it->second;
29     }
30 
31     varbinder::LocalVariable *var = varbinder::Scope::CreateVar<varbinder::PropertyDecl>(
32         Allocator(), name, varbinder::VariableFlags::BUILTIN_TYPE, nullptr);
33     var->SetTsType(checker->GlobalBuiltinDynamicType(lang_));
34     propertiesCache_.emplace(name, var);
35 
36     return var;
37 }
38 
AssignmentTarget(TypeRelation * relation,Type * source)39 void ETSDynamicType::AssignmentTarget(TypeRelation *relation, Type *source)
40 {
41     if (hasDecl_) {
42         return ETSObjectType::AssignmentTarget(relation, source);
43     }
44 
45     if (relation->ApplyBoxing() && !relation->IsTrue() && IsConvertible(source)) {
46         relation->Result(true);
47         return;
48     }
49 
50     if (source->IsETSDynamicType()) {
51         relation->Result(true);
52     }
53 }
54 
AssignmentSource(TypeRelation * relation,Type * target)55 bool ETSDynamicType::AssignmentSource(TypeRelation *relation, Type *target)
56 {
57     if (hasDecl_) {
58         return ETSObjectType::AssignmentSource(relation, target);
59     }
60 
61     if (relation->ApplyUnboxing() && IsConvertible(target)) {
62         relation->Result(true);
63         return true;
64     }
65 
66     if (target->IsETSDynamicType()) {
67         relation->Result(true);
68     }
69     return relation->IsTrue();
70 }
71 
Cast(TypeRelation * relation,Type * target)72 void ETSDynamicType::Cast(TypeRelation *relation, Type *target)
73 {
74     if (hasDecl_) {
75         return ETSObjectType::Cast(relation, target);
76     }
77 
78     if (relation->InCastingContext() || IsConvertible(target)) {
79         relation->Result(true);
80         return;
81     }
82 
83     conversion::Forbidden(relation);
84 }
85 
CastTarget(TypeRelation * relation,Type * source)86 void ETSDynamicType::CastTarget(TypeRelation *relation, Type *source)
87 {
88     if (hasDecl_) {
89         ETSObjectType::CastTarget(relation, source);
90         return;
91     }
92 
93     if (relation->InCastingContext() || IsConvertible(source)) {
94         relation->Result(true);
95         return;
96     }
97 
98     conversion::Forbidden(relation);
99 }
100 
IsConvertible(Type const * target)101 bool ETSDynamicType::IsConvertible(Type const *target)
102 {
103     return target->IsETSDynamicType() || target->IsETSObjectType() || target->IsETSArrayType() ||
104            target->IsETSFunctionType() ||
105            target->HasTypeFlag(checker::TypeFlag::ETS_CONVERTIBLE_TO_NUMERIC | checker::TypeFlag::ETS_BOOLEAN);
106 }
107 
CreateETSFunctionType(const util::StringView & name) const108 ETSFunctionType *ETSDynamicType::CreateETSFunctionType(const util::StringView &name) const
109 {
110     return Allocator()->New<ETSDynamicFunctionType>(name, Allocator(), lang_);
111 }
112 
ToAssemblerType(std::stringstream & ss) const113 void ETSDynamicType::ToAssemblerType(std::stringstream &ss) const
114 {
115     ss << compiler::Signatures::Dynamic::Type(lang_);
116 }
117 
118 }  // namespace ark::es2panda::checker
119