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 "etsNullishTypes.h"
18 #include "ir/expressions/identifier.h"
19 #include "ir/ts/tsTypeParameter.h"
20 #include "checker/ETSchecker.h"
21 #include "checker/ets/conversion.h"
22
23 namespace ark::es2panda::checker {
24
ToString(std::stringstream & ss,bool precise) const25 void ETSNonNullishType::ToString(std::stringstream &ss, bool precise) const
26 {
27 ss << "NonNullable<";
28 GetUnderlying()->ToString(ss, precise);
29 ss << ">";
30 }
31
Identical(TypeRelation * relation,Type * other)32 void ETSNonNullishType::Identical(TypeRelation *relation, Type *other)
33 {
34 if (other->IsETSNonNullishType()) {
35 relation->IsIdenticalTo(GetUnderlying(), other->AsETSNonNullishType()->GetUnderlying());
36 }
37 }
38
AssignmentSource(TypeRelation * relation,Type * target)39 bool ETSNonNullishType::AssignmentSource([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *target)
40 {
41 return relation->IsSupertypeOf(target, this);
42 }
43
AssignmentTarget(TypeRelation * relation,Type * source)44 void ETSNonNullishType::AssignmentTarget([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *source)
45 {
46 relation->IsSupertypeOf(this, source);
47 }
48
Cast(TypeRelation * relation,Type * target)49 void ETSNonNullishType::Cast(TypeRelation *relation, Type *target)
50 {
51 if (relation->IsSupertypeOf(target, this)) {
52 relation->RemoveFlags(TypeRelationFlag::UNCHECKED_CAST);
53 return;
54 }
55 if (relation->IsIdenticalTo(GetUnderlying(), target)) {
56 relation->RemoveFlags(TypeRelationFlag::UNCHECKED_CAST);
57 return;
58 }
59
60 if (target->IsETSReferenceType()) {
61 relation->Result(relation->InCastingContext());
62 }
63 }
64
CastTarget(TypeRelation * relation,Type * source)65 void ETSNonNullishType::CastTarget(TypeRelation *relation, Type *source)
66 {
67 if (relation->IsSupertypeOf(this, source)) {
68 relation->RemoveFlags(TypeRelationFlag::UNCHECKED_CAST);
69 return;
70 }
71 if (relation->IsIdenticalTo(source, GetUnderlying())) {
72 relation->RemoveFlags(TypeRelationFlag::UNCHECKED_CAST);
73 }
74 relation->Result(relation->InCastingContext());
75 }
76
IsSupertypeOf(TypeRelation * relation,Type * source)77 void ETSNonNullishType::IsSupertypeOf([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *source)
78 {
79 relation->Result(false);
80 }
81
IsSubtypeOf(TypeRelation * relation,Type * target)82 void ETSNonNullishType::IsSubtypeOf([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *target)
83 {
84 if (relation->IsSupertypeOf(target, GetUnderlying())) {
85 return;
86 }
87
88 relation->Result(false);
89 }
90
Substitute(TypeRelation * relation,const Substitution * substitution)91 Type *ETSNonNullishType::Substitute([[maybe_unused]] TypeRelation *relation, const Substitution *substitution)
92 {
93 auto *substituted = GetUnderlying()->Substitute(relation, substitution);
94 if (substituted == GetUnderlying()) {
95 return this;
96 }
97 return relation->GetChecker()->AsETSChecker()->GetNonNullishType(substituted);
98 }
99
ToAssemblerType(std::stringstream & ss) const100 void ETSNonNullishType::ToAssemblerType(std::stringstream &ss) const
101 {
102 GetUnderlying()->ToAssemblerTypeWithRank(ss);
103 }
104
ToDebugInfoType(std::stringstream & ss) const105 void ETSNonNullishType::ToDebugInfoType(std::stringstream &ss) const
106 {
107 GetUnderlying()->ToDebugInfoType(ss);
108 }
109
Instantiate(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)110 Type *ETSNonNullishType::Instantiate([[maybe_unused]] ArenaAllocator *allocator,
111 [[maybe_unused]] TypeRelation *relation,
112 [[maybe_unused]] GlobalTypesHolder *globalTypes)
113 {
114 return allocator->New<ETSNonNullishType>(GetUnderlying());
115 }
116
CheckVarianceRecursively(TypeRelation * relation,VarianceFlag varianceFlag)117 void ETSNonNullishType::CheckVarianceRecursively(TypeRelation *relation, VarianceFlag varianceFlag)
118 {
119 relation->CheckVarianceRecursively(GetUnderlying(),
120 relation->TransferVariant(varianceFlag, VarianceFlag::COVARIANT));
121 }
122
123 } // namespace ark::es2panda::checker
124