• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "etsAnyType.h"
17 
18 #include "checker/ETSchecker.h"
19 #include "checker/ets/conversion.h"
20 #include "etsTypeParameter.h"
21 
22 namespace ark::es2panda::checker {
Identical(TypeRelation * relation,Type * other)23 void ETSAnyType::Identical(TypeRelation *relation, Type *other)
24 {
25     relation->Result(other->IsAnyType());
26 }
27 
AssignmentTarget(TypeRelation * relation,Type * source)28 void ETSAnyType::AssignmentTarget(TypeRelation *relation, Type *source)
29 {
30     if (!source->IsETSPrimitiveType()) {
31         relation->Result(true);
32         return;
33     }
34 
35     if (relation->ApplyBoxing()) {
36         auto checker = relation->GetChecker()->AsETSChecker();
37         relation->GetNode()->AddBoxingUnboxingFlags(checker->GetBoxingFlag(checker->MaybeBoxType(source)));
38         relation->Result(true);
39     }
40 }
41 
AssignmentSource(TypeRelation * relation,Type * target)42 bool ETSAnyType::AssignmentSource(TypeRelation *relation, Type *target)
43 {
44     Identical(relation, target);
45     return relation->IsTrue();
46 }
47 
Compare(TypeRelation * relation,Type * other)48 void ETSAnyType::Compare([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *other)
49 {
50     ES2PANDA_UNREACHABLE();
51 }
52 
Cast(TypeRelation * relation,Type * target)53 void ETSAnyType::Cast(TypeRelation *relation, Type *target)
54 {
55     if (!relation->InCastingContext()) {
56         Identical(relation, target);
57         return;
58     }
59 
60     if (!target->IsETSPrimitiveType()) {
61         relation->Result(true);
62         return;
63     }
64 
65     if (relation->ApplyUnboxing()) {
66         auto *const boxedTarget = relation->GetChecker()->AsETSChecker()->MaybeBoxInRelation(target);
67         ES2PANDA_ASSERT(boxedTarget != nullptr);
68         conversion::Unboxing(relation, boxedTarget->AsETSObjectType());
69         relation->Result(true);
70     }
71 }
72 
CastTarget(TypeRelation * relation,Type * source)73 void ETSAnyType::CastTarget(TypeRelation *relation, [[maybe_unused]] Type *source)
74 {
75     AssignmentTarget(relation, source);
76 }
77 
IsSubtypeOf(TypeRelation * relation,Type * target)78 void ETSAnyType::IsSubtypeOf(TypeRelation *relation, Type *target)
79 {
80     Identical(relation, target);
81 }
82 
IsSupertypeOf(TypeRelation * relation,Type * source)83 void ETSAnyType::IsSupertypeOf(TypeRelation *relation, Type *source)
84 {
85     relation->Result(!source->IsETSPrimitiveType());
86 }
87 
ToString(std::stringstream & ss,bool precise) const88 void ETSAnyType::ToString(std::stringstream &ss, [[maybe_unused]] bool precise) const
89 {
90     ss << compiler::Signatures::ANY_TYPE_NAME;
91 }
92 
ToAssemblerType(std::stringstream & ss) const93 void ETSAnyType::ToAssemblerType(std::stringstream &ss) const
94 {
95     ss << compiler::Signatures::BUILTIN_OBJECT;
96 }
97 
GetTypeFacts() const98 TypeFacts ETSAnyType::GetTypeFacts() const
99 {
100     return TypeFacts::NONE;
101 }
102 
ToDebugInfoType(std::stringstream & ss) const103 void ETSAnyType::ToDebugInfoType(std::stringstream &ss) const
104 {
105     ss << ETSObjectType::NameToDescriptor(compiler::Signatures::BUILTIN_OBJECT);
106 }
107 
Instantiate(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)108 Type *ETSAnyType::Instantiate(ArenaAllocator *allocator, [[maybe_unused]] TypeRelation *relation,
109                               [[maybe_unused]] GlobalTypesHolder *globalTypes)
110 {
111     return allocator->New<ETSAnyType>();
112 }
113 }  // namespace ark::es2panda::checker