• 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 "etsTypeParameter.h"
17 #include "ir/expressions/identifier.h"
18 #include "ir/ts/tsTypeParameter.h"
19 #include "checker/ETSchecker.h"
20 
21 namespace ark::es2panda::checker {
22 
ToString(std::stringstream & ss,bool precise) const23 void ETSTypeParameter::ToString(std::stringstream &ss, bool precise) const
24 {
25     if (precise) {
26         ss << std::to_string(id_) << ".";
27     }
28     ss << declNode_->Name()->Name();
29 }
30 
Identical(TypeRelation * relation,Type * other)31 void ETSTypeParameter::Identical(TypeRelation *relation, Type *other)
32 {
33     relation->Result(false);
34     if (other->IsETSTypeParameter() && other->AsETSTypeParameter()->GetOriginal() == GetOriginal()) {
35         relation->Result(true);
36     }
37 }
38 
AssignmentSource(TypeRelation * relation,Type * target)39 bool ETSTypeParameter::AssignmentSource(TypeRelation *relation, [[maybe_unused]] Type *target)
40 {
41     return relation->IsAssignableTo(this->GetConstraintType(), target);
42 }
43 
AssignmentTarget(TypeRelation * relation,Type * source)44 void ETSTypeParameter::AssignmentTarget(TypeRelation *relation, Type *source)
45 {
46     if (source->IsETSTypeParameter() && source->AsETSTypeParameter()->GetOriginal() == GetOriginal()) {
47         relation->Result(true);
48         return;
49     }
50 
51     relation->IsSupertypeOf(this, source);
52 }
53 
Cast(TypeRelation * relation,Type * target)54 void ETSTypeParameter::Cast(TypeRelation *relation, Type *target)
55 {
56     if (relation->IsSupertypeOf(target, this)) {
57         relation->RemoveFlags(TypeRelationFlag::UNCHECKED_CAST);
58         return;
59     }
60 
61     // NOTE(vpukhov): adjust UNCHECKED_CAST flags
62     if (target->IsETSReferenceType()) {
63         relation->Result(relation->InCastingContext());
64     }
65 }
66 
CastTarget(TypeRelation * relation,Type * source)67 void ETSTypeParameter::CastTarget(TypeRelation *relation, Type *source)
68 {
69     if (relation->IsSupertypeOf(this, source)) {
70         relation->RemoveFlags(TypeRelationFlag::UNCHECKED_CAST);
71         return;
72     }
73 
74     relation->Result(relation->InCastingContext());
75 }
76 
IsSupertypeOf(TypeRelation * relation,Type * source)77 void ETSTypeParameter::IsSupertypeOf(TypeRelation *relation, [[maybe_unused]] Type *source)
78 {
79     relation->Result(false);
80 }
81 
IsSubtypeOf(TypeRelation * relation,Type * target)82 void ETSTypeParameter::IsSubtypeOf(TypeRelation *relation, Type *target)
83 {
84     if (relation->IsSupertypeOf(target, GetConstraintType())) {
85         return;
86     }
87 
88     relation->Result(false);
89 }
90 
CheckVarianceRecursively(TypeRelation * relation,VarianceFlag varianceFlag)91 void ETSTypeParameter::CheckVarianceRecursively([[maybe_unused]] TypeRelation *relation, VarianceFlag varianceFlag)
92 {
93     if (!declNode_->IsIn() && !declNode_->IsOut()) {
94         return;
95     }
96     auto classTypeParameters = relation->GetChecker()->Context().ContainingClass()->TypeArguments();
97     if (std::all_of(classTypeParameters.begin(), classTypeParameters.end(), [this](Type *param) {
98             return this->GetDeclNode()->Name()->Name() != param->AsETSTypeParameter()->Name();
99         })) {
100         return;
101     }
102 
103     if (varianceFlag == VarianceFlag::INVARIANT) {
104         relation->GetChecker()->LogError(diagnostic::VARIANT_PARAM_INVARIAN_USE,
105                                          {declNode_->Name()->Name(), declNode_->IsOut() ? " 'out'" : " 'in'"},
106                                          relation->GetNode()->Start());
107         relation->Result(false);
108         return;
109     }
110 
111     if (varianceFlag == VarianceFlag::COVARIANT && !declNode_->IsOut()) {
112         relation->GetChecker()->LogError(diagnostic::VARIANCE_TPARAM_IN_OUT, {declNode_->Name()->Name()},
113                                          relation->GetNode()->Start());
114         relation->Result(false);
115     }
116 
117     if (varianceFlag == VarianceFlag::CONTRAVARIANT && !declNode_->IsIn()) {
118         relation->GetChecker()->LogError(diagnostic::VARIANCE_TPARAM_OUT_IN, {declNode_->Name()->Name()},
119                                          relation->GetNode()->Start());
120         relation->Result(false);
121     }
122 }
123 
Instantiate(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)124 Type *ETSTypeParameter::Instantiate([[maybe_unused]] ArenaAllocator *allocator, [[maybe_unused]] TypeRelation *relation,
125                                     [[maybe_unused]] GlobalTypesHolder *globalTypes)
126 {
127     auto *const checker = relation->GetChecker()->AsETSChecker();
128 
129     auto *const copiedType = checker->CreateTypeParameter();
130     ES2PANDA_ASSERT(copiedType != nullptr);
131     copiedType->AddTypeFlag(TypeFlag::GENERIC);
132     copiedType->SetDeclNode(GetDeclNode());
133     copiedType->SetDefaultType(GetDefaultType());
134     copiedType->SetConstraintType(GetConstraintType());
135     copiedType->SetVariable(Variable());
136     return copiedType;
137 }
138 
Substitute(TypeRelation * relation,const Substitution * substitution)139 Type *ETSTypeParameter::Substitute([[maybe_unused]] TypeRelation *relation, const Substitution *substitution)
140 {
141     if (substitution == nullptr || substitution->empty()) {
142         return this;
143     }
144 
145     auto *type = GetDeclNode()->Name()->Variable()->TsType();
146     if (type->IsTypeError()) {
147         return this;
148     }
149 
150     if (auto repl = substitution->find(type->AsETSTypeParameter()); repl != substitution->end()) {
151         // 22955: The result is sometimes primitve. Can be reproduced for type aliases
152         return repl->second;
153     }
154     return this;
155 }
156 
ToAssemblerType(std::stringstream & ss) const157 void ETSTypeParameter::ToAssemblerType(std::stringstream &ss) const
158 {
159     GetConstraintType()->ToAssemblerTypeWithRank(ss);
160 }
161 
ToDebugInfoType(std::stringstream & ss) const162 void ETSTypeParameter::ToDebugInfoType(std::stringstream &ss) const
163 {
164     GetConstraintType()->ToDebugInfoType(ss);
165 }
166 
GetOriginal() const167 ETSTypeParameter *ETSTypeParameter::GetOriginal() const noexcept
168 {
169     return GetDeclNode()->Name()->Variable()->TsType()->AsETSTypeParameter();
170 }
171 
Name() const172 util::StringView const &ETSTypeParameter::Name() const noexcept
173 {
174     return GetDeclNode()->Name()->Name();
175 }
176 }  // namespace ark::es2panda::checker
177