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 "etsStringType.h"
17
18 #include "varbinder/ETSBinder.h"
19
20 namespace ark::es2panda::checker {
Identical(TypeRelation * relation,Type * other)21 void ETSStringType::Identical(TypeRelation *relation, Type *other)
22 {
23 if (other->IsETSStringType()) {
24 relation->Result(true);
25 }
26 }
27
AssignmentSource(TypeRelation * relation,Type * target)28 bool ETSStringType::AssignmentSource(TypeRelation *relation, Type *target)
29 {
30 auto node = relation->GetNode();
31 if ((relation->InAssignmentContext() || relation->ApplyStringToChar()) && IsConvertibleTo(target)) {
32 node->AddBoxingUnboxingFlags(ir::BoxingUnboxingFlags::UNBOX_TO_CHAR);
33 relation->Result(true);
34 } else {
35 relation->Result(target->IsETSStringType());
36 }
37
38 return relation->IsTrue();
39 }
40
AssignmentTarget(TypeRelation * relation,Type * source)41 void ETSStringType::AssignmentTarget([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *source)
42 {
43 if (source->IsETSStringType()) {
44 relation->Result(true);
45 }
46 }
47
Instantiate(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)48 Type *ETSStringType::Instantiate([[maybe_unused]] ArenaAllocator *allocator, [[maybe_unused]] TypeRelation *relation,
49 [[maybe_unused]] GlobalTypesHolder *globalTypes)
50 {
51 return this;
52 }
53
IsConvertibleTo(Type const * to) const54 bool ETSStringType::IsConvertibleTo(Type const *to) const
55 {
56 const bool targetIsChar =
57 to->IsCharType() ||
58 // ETSObjectType is used in arrow function expression call.
59 (to->IsETSObjectType() && to->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_CHAR));
60
61 return targetIsChar && IsConstantType() && value_.IsConvertibleToChar();
62 }
63
64 } // namespace ark::es2panda::checker
65