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 "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->IsETSObjectType()) {
63 relation->RemoveFlags(TypeRelationFlag::UNCHECKED_CAST);
64 }
65 relation->Result(relation->InCastingContext());
66 }
67
CastTarget(TypeRelation * relation,Type * source)68 void ETSTypeParameter::CastTarget(TypeRelation *relation, Type *source)
69 {
70 if (relation->IsSupertypeOf(this, source)) {
71 relation->RemoveFlags(TypeRelationFlag::UNCHECKED_CAST);
72 return;
73 }
74
75 relation->Result(relation->InCastingContext());
76 }
77
IsSupertypeOf(TypeRelation * relation,Type * source)78 void ETSTypeParameter::IsSupertypeOf(TypeRelation *relation, [[maybe_unused]] Type *source)
79 {
80 relation->Result(false);
81 }
82
IsSubtypeOf(TypeRelation * relation,Type * target)83 void ETSTypeParameter::IsSubtypeOf(TypeRelation *relation, Type *target)
84 {
85 if (relation->IsSupertypeOf(target, GetConstraintType())) {
86 return;
87 }
88
89 relation->Result(false);
90 }
91
Instantiate(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)92 Type *ETSTypeParameter::Instantiate([[maybe_unused]] ArenaAllocator *allocator, [[maybe_unused]] TypeRelation *relation,
93 [[maybe_unused]] GlobalTypesHolder *globalTypes)
94 {
95 auto *const checker = relation->GetChecker()->AsETSChecker();
96
97 auto *const copiedType = checker->CreateTypeParameter();
98 copiedType->AddTypeFlag(TypeFlag::GENERIC);
99 copiedType->SetDeclNode(GetDeclNode());
100 copiedType->SetDefaultType(GetDefaultType());
101 copiedType->SetConstraintType(GetConstraintType());
102 copiedType->SetVariable(Variable());
103 return copiedType;
104 }
105
Substitute(TypeRelation * relation,const Substitution * substitution)106 Type *ETSTypeParameter::Substitute([[maybe_unused]] TypeRelation *relation, const Substitution *substitution)
107 {
108 if (substitution == nullptr || substitution->empty()) {
109 return this;
110 }
111 if (auto repl = substitution->find(GetOriginal()); repl != substitution->end()) {
112 return repl->second;
113 }
114 return this;
115 }
116
ToAssemblerType(std::stringstream & ss) const117 void ETSTypeParameter::ToAssemblerType(std::stringstream &ss) const
118 {
119 GetConstraintType()->ToAssemblerTypeWithRank(ss);
120 }
121
ToDebugInfoType(std::stringstream & ss) const122 void ETSTypeParameter::ToDebugInfoType(std::stringstream &ss) const
123 {
124 GetConstraintType()->ToDebugInfoType(ss);
125 }
126
GetOriginal() const127 ETSTypeParameter *ETSTypeParameter::GetOriginal() const noexcept
128 {
129 return GetDeclNode()->Name()->Variable()->TsType()->AsETSTypeParameter();
130 }
131
Name() const132 util::StringView const &ETSTypeParameter::Name() const noexcept
133 {
134 return GetDeclNode()->Name()->Name();
135 }
136 } // namespace ark::es2panda::checker
137