• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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     relation->Result(relation->InCastingContext());
60 }
61 
CastTarget(TypeRelation * relation,Type * source)62 void ETSNonNullishType::CastTarget(TypeRelation *relation, Type *source)
63 {
64     if (relation->IsSupertypeOf(this, source)) {
65         relation->RemoveFlags(TypeRelationFlag::UNCHECKED_CAST);
66         return;
67     }
68     if (relation->IsIdenticalTo(source, GetUnderlying())) {
69         relation->RemoveFlags(TypeRelationFlag::UNCHECKED_CAST);
70     }
71     relation->Result(relation->InCastingContext());
72 }
73 
IsSupertypeOf(TypeRelation * relation,Type * source)74 void ETSNonNullishType::IsSupertypeOf([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *source)
75 {
76     relation->Result(false);
77 }
78 
IsSubtypeOf(TypeRelation * relation,Type * target)79 void ETSNonNullishType::IsSubtypeOf([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *target)
80 {
81     if (relation->IsSupertypeOf(target, GetUnderlying())) {
82         return;
83     }
84 
85     relation->Result(false);
86 }
87 
Substitute(TypeRelation * relation,const Substitution * substitution)88 Type *ETSNonNullishType::Substitute([[maybe_unused]] TypeRelation *relation, const Substitution *substitution)
89 {
90     auto *substituted = GetUnderlying()->Substitute(relation, substitution);
91     if (substituted == GetUnderlying()) {
92         return this;
93     }
94     return relation->GetChecker()->AsETSChecker()->GetNonNullishType(substituted);
95 }
96 
ToAssemblerType(std::stringstream & ss) const97 void ETSNonNullishType::ToAssemblerType(std::stringstream &ss) const
98 {
99     GetUnderlying()->ToAssemblerTypeWithRank(ss);
100 }
101 
ToDebugInfoType(std::stringstream & ss) const102 void ETSNonNullishType::ToDebugInfoType(std::stringstream &ss) const
103 {
104     GetUnderlying()->ToDebugInfoType(ss);
105 }
106 
Instantiate(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)107 Type *ETSNonNullishType::Instantiate([[maybe_unused]] ArenaAllocator *allocator,
108                                      [[maybe_unused]] TypeRelation *relation,
109                                      [[maybe_unused]] GlobalTypesHolder *globalTypes)
110 {
111     return allocator->New<ETSNonNullishType>(GetUnderlying());
112 }
113 
114 }  // namespace ark::es2panda::checker
115