• 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 "etsTypeParameter.h"
17 #include "etsNullishTypes.h"
18 #include "etsReadonlyType.h"
19 #include "ir/expressions/identifier.h"
20 #include "ir/ts/tsTypeParameter.h"
21 #include "checker/ETSchecker.h"
22 #include "checker/ets/conversion.h"
23 
24 namespace ark::es2panda::checker {
25 
ToString(std::stringstream & ss,bool precise) const26 void ETSReadonlyType::ToString(std::stringstream &ss, bool precise) const
27 {
28     ss << "Readonly<";
29     GetUnderlying()->ToString(ss, precise);
30     ss << ">";
31 }
32 
Identical(TypeRelation * relation,Type * other)33 void ETSReadonlyType::Identical(TypeRelation *relation, Type *other)
34 {
35     relation->Result(false);
36     if (other->IsETSReadonlyType()) {
37         relation->IsIdenticalTo(GetUnderlying(), other->AsETSReadonlyType()->GetUnderlying());
38     }
39 }
40 
AssignmentSource(TypeRelation * relation,Type * target)41 bool ETSReadonlyType::AssignmentSource([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *target)
42 {
43     return relation->Result(false);
44 }
45 
AssignmentTarget(TypeRelation * relation,Type * source)46 void ETSReadonlyType::AssignmentTarget([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *source)
47 {
48     if (source->IsETSTypeParameter() && source->AsETSTypeParameter()->GetOriginal() == GetUnderlying()) {
49         relation->Result(true);
50         return;
51     }
52     relation->Result(false);
53 }
54 
Cast(TypeRelation * relation,Type * target)55 void ETSReadonlyType::Cast(TypeRelation *relation, [[maybe_unused]] Type *target)
56 {
57     if (relation->IsSupertypeOf(target, this)) {
58         relation->RemoveFlags(TypeRelationFlag::UNCHECKED_CAST);
59         return;
60     }
61     if (target->IsETSObjectType() && target->HasTypeFlag(TypeFlag::READONLY)) {
62         relation->RemoveFlags(TypeRelationFlag::UNCHECKED_CAST);
63     }
64     relation->Result(relation->InCastingContext());
65 }
66 
CastTarget(TypeRelation * relation,Type * source)67 void ETSReadonlyType::CastTarget(TypeRelation *relation, [[maybe_unused]] Type *source)
68 {
69     this->AssignmentTarget(relation, source);
70     if (relation->IsTrue()) {
71         relation->RemoveFlags(TypeRelationFlag::UNCHECKED_CAST);
72         return;
73     }
74 
75     relation->Result(relation->InCastingContext());
76 }
77 
IsSupertypeOf(TypeRelation * relation,Type * source)78 void ETSReadonlyType::IsSupertypeOf([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *source)
79 {
80     relation->Result(false);
81 }
82 
IsSubtypeOf(TypeRelation * relation,Type * target)83 void ETSReadonlyType::IsSubtypeOf([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *target)
84 {
85     if (relation->IsSupertypeOf(target, GetUnderlying())) {
86         return;
87     }
88 
89     relation->Result(false);
90 }
91 
Substitute(TypeRelation * relation,const Substitution * substitution)92 Type *ETSReadonlyType::Substitute([[maybe_unused]] TypeRelation *relation, const Substitution *substitution)
93 {
94     auto *substituted = GetUnderlying()->Substitute(relation, substitution);
95     if (substituted == GetUnderlying()) {
96         return this;
97     }
98     return relation->GetChecker()->AsETSChecker()->GetReadonlyType(substituted);
99 }
100 
ToAssemblerType(std::stringstream & ss) const101 void ETSReadonlyType::ToAssemblerType(std::stringstream &ss) const
102 {
103     GetUnderlying()->ToAssemblerTypeWithRank(ss);
104 }
105 
ToDebugInfoType(std::stringstream & ss) const106 void ETSReadonlyType::ToDebugInfoType(std::stringstream &ss) const
107 {
108     GetUnderlying()->ToDebugInfoType(ss);
109 }
110 
Instantiate(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)111 Type *ETSReadonlyType::Instantiate([[maybe_unused]] ArenaAllocator *allocator, [[maybe_unused]] TypeRelation *relation,
112                                    [[maybe_unused]] GlobalTypesHolder *globalTypes)
113 {
114     return allocator->New<ETSReadonlyType>(GetUnderlying());
115 }
116 
117 }  // namespace ark::es2panda::checker
118