1 /*
2 * Copyright (c) 2024-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 "etsPartialTypeParameter.h"
17 #include "checker/ETSchecker.h"
18
19 namespace ark::es2panda::checker {
20
ETSPartialTypeParameter(ETSTypeParameter * const typeParam,ETSChecker * const checker)21 ETSPartialTypeParameter::ETSPartialTypeParameter(ETSTypeParameter *const typeParam, ETSChecker *const checker)
22 : Type(TypeFlag::ETS_PARTIAL_TYPE_PARAMETER), checker_(checker), typeParameter_(typeParam)
23 {
24 ES2PANDA_ASSERT(typeParameter_ != nullptr && checker_ != nullptr);
25 }
26
ToString(std::stringstream & ss,bool precise) const27 void ETSPartialTypeParameter::ToString(std::stringstream &ss, bool precise) const
28 {
29 ss << "Partial<";
30 GetUnderlying()->ToString(ss, precise);
31 ss << ">";
32 }
33
Identical(TypeRelation * relation,Type * other)34 void ETSPartialTypeParameter::Identical(TypeRelation *relation, Type *other)
35 {
36 if (other->IsETSPartialTypeParameter()) {
37 relation->IsIdenticalTo(GetUnderlying(), other->AsETSPartialTypeParameter()->GetUnderlying());
38 }
39 }
40
AssignmentSource(TypeRelation * relation,Type * target)41 bool ETSPartialTypeParameter::AssignmentSource(TypeRelation *relation, Type *target)
42 {
43 return relation->IsSupertypeOf(target, this);
44 }
45
AssignmentTarget(TypeRelation * relation,Type * source)46 void ETSPartialTypeParameter::AssignmentTarget(TypeRelation *relation, Type *source)
47 {
48 relation->IsSupertypeOf(this, source);
49 }
50
Cast(TypeRelation * relation,Type * target)51 void ETSPartialTypeParameter::Cast(TypeRelation *relation, Type *target)
52 {
53 if (relation->IsSupertypeOf(target, this)) {
54 relation->RemoveFlags(TypeRelationFlag::UNCHECKED_CAST);
55 return;
56 }
57 relation->Result(relation->InCastingContext());
58 }
59
CastTarget(TypeRelation * relation,Type * source)60 void ETSPartialTypeParameter::CastTarget(TypeRelation *relation, Type *source)
61 {
62 if (relation->IsSupertypeOf(this, source)) {
63 relation->RemoveFlags(TypeRelationFlag::UNCHECKED_CAST);
64 return;
65 }
66 relation->Result(relation->InCastingContext());
67 }
68
IsSupertypeOf(TypeRelation * relation,Type * source)69 void ETSPartialTypeParameter::IsSupertypeOf(TypeRelation *relation, [[maybe_unused]] Type *source)
70 {
71 relation->Result(false);
72 }
73
IsSubtypeOf(TypeRelation * relation,Type * target)74 void ETSPartialTypeParameter::IsSubtypeOf(TypeRelation *relation, Type *target)
75 {
76 relation->Result(false);
77 if (target->IsETSPartialTypeParameter()) {
78 relation->IsSupertypeOf(target->AsETSPartialTypeParameter()->GetUnderlying(), GetUnderlying());
79 }
80 }
81
Instantiate(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)82 ETSPartialTypeParameter *ETSPartialTypeParameter::Instantiate(ArenaAllocator *allocator, TypeRelation *relation,
83 GlobalTypesHolder *globalTypes)
84 {
85 auto *underlying = GetUnderlying();
86 ES2PANDA_ASSERT(underlying != nullptr);
87 auto *instantiated = underlying->Instantiate(allocator, relation, globalTypes);
88 ES2PANDA_ASSERT(instantiated != nullptr);
89 auto *typeParam = instantiated->AsETSTypeParameter();
90 ES2PANDA_ASSERT(typeParam != nullptr);
91 return allocator->New<ETSPartialTypeParameter>(typeParam, checker_);
92 }
93
Substitute(TypeRelation * relation,const Substitution * substitution)94 Type *ETSPartialTypeParameter::Substitute(TypeRelation *relation, const Substitution *substitution)
95 {
96 auto *substituted = GetUnderlying()->Substitute(relation, substitution);
97 if (substituted == GetUnderlying()) {
98 return this;
99 }
100 return checker_->CreatePartialType(substituted);
101 }
102
ToAssemblerType(std::stringstream & ss) const103 void ETSPartialTypeParameter::ToAssemblerType(std::stringstream &ss) const
104 {
105 checker_->CreatePartialType(GetUnderlying()->GetConstraintType())->ToAssemblerTypeWithRank(ss);
106 }
107
ToDebugInfoType(std::stringstream & ss) const108 void ETSPartialTypeParameter::ToDebugInfoType(std::stringstream &ss) const
109 {
110 checker_->CreatePartialType(GetUnderlying()->GetConstraintType())->ToDebugInfoType(ss);
111 }
112
CheckVarianceRecursively(TypeRelation * relation,VarianceFlag varianceFlag)113 void ETSPartialTypeParameter::CheckVarianceRecursively(TypeRelation *relation, VarianceFlag varianceFlag)
114 {
115 relation->CheckVarianceRecursively(GetUnderlying(),
116 relation->TransferVariant(varianceFlag, VarianceFlag::COVARIANT));
117 }
118
119 } // namespace ark::es2panda::checker
120