• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "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     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     return allocator->New<ETSPartialTypeParameter>(
86         GetUnderlying()->Instantiate(allocator, relation, globalTypes)->AsETSTypeParameter(), checker_);
87 }
88 
Substitute(TypeRelation * relation,const Substitution * substitution)89 Type *ETSPartialTypeParameter::Substitute(TypeRelation *relation, const Substitution *substitution)
90 {
91     auto *substituted = GetUnderlying()->Substitute(relation, substitution);
92     if (substituted == GetUnderlying()) {
93         return this;
94     }
95     return checker_->CreatePartialType(substituted);
96 }
97 
ToAssemblerType(std::stringstream & ss) const98 void ETSPartialTypeParameter::ToAssemblerType(std::stringstream &ss) const
99 {
100     checker_->CreatePartialType(GetUnderlying()->GetConstraintType())->ToAssemblerTypeWithRank(ss);
101 }
102 
ToDebugInfoType(std::stringstream & ss) const103 void ETSPartialTypeParameter::ToDebugInfoType(std::stringstream &ss) const
104 {
105     checker_->CreatePartialType(GetUnderlying()->GetConstraintType())->ToDebugInfoType(ss);
106 }
107 }  // namespace ark::es2panda::checker
108