1 /**
2 * Copyright (c) 2021 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 <typescript/checker.h>
17
18 #include <algorithm>
19
20 namespace panda::es2panda::checker {
21
IsAllTypesAssignableTo(Type * source,Type * target)22 bool Checker::IsAllTypesAssignableTo(Type *source, Type *target)
23 {
24 if (source->TypeFlags() == TypeFlag::UNION) {
25 auto &types = source->AsUnionType()->ConstituentTypes();
26
27 return std::all_of(types.begin(), types.end(),
28 [this, target](auto *it) { return IsAllTypesAssignableTo(it, target); });
29 }
30
31 return relation_->IsAssignableTo(source, target);
32 }
33
IsTypeIdenticalTo(Type * source,Type * target)34 bool Checker::IsTypeIdenticalTo(Type *source, Type *target)
35 {
36 return relation_->IsIdenticalTo(source, target);
37 }
38
IsTypeIdenticalTo(Type * source,Type * target,const std::string & errMsg,const lexer::SourcePosition & errPos)39 bool Checker::IsTypeIdenticalTo(Type *source, Type *target, const std::string &errMsg,
40 const lexer::SourcePosition &errPos)
41 {
42 if (!IsTypeIdenticalTo(source, target)) {
43 relation_->RaiseError(errMsg, errPos);
44 }
45
46 return true;
47 }
48
IsTypeIdenticalTo(Type * source,Type * target,std::initializer_list<TypeErrorMessageElement> list,const lexer::SourcePosition & errPos)49 bool Checker::IsTypeIdenticalTo(Type *source, Type *target, std::initializer_list<TypeErrorMessageElement> list,
50 const lexer::SourcePosition &errPos)
51 {
52 if (!IsTypeIdenticalTo(source, target)) {
53 relation_->RaiseError(list, errPos);
54 }
55
56 return true;
57 }
58
IsTypeAssignableTo(Type * source,Type * target)59 bool Checker::IsTypeAssignableTo(Type *source, Type *target)
60 {
61 return relation_->IsAssignableTo(source, target);
62 }
63
IsTypeAssignableTo(Type * source,Type * target,const std::string & errMsg,const lexer::SourcePosition & errPos)64 bool Checker::IsTypeAssignableTo(Type *source, Type *target, const std::string &errMsg,
65 const lexer::SourcePosition &errPos)
66 {
67 if (!IsTypeAssignableTo(source, target)) {
68 relation_->RaiseError(errMsg, errPos);
69 }
70
71 return true;
72 }
73
IsTypeAssignableTo(Type * source,Type * target,std::initializer_list<TypeErrorMessageElement> list,const lexer::SourcePosition & errPos)74 bool Checker::IsTypeAssignableTo(Type *source, Type *target, std::initializer_list<TypeErrorMessageElement> list,
75 const lexer::SourcePosition &errPos)
76 {
77 if (!IsTypeAssignableTo(source, target)) {
78 relation_->RaiseError(list, errPos);
79 }
80
81 return true;
82 }
83
IsTypeComparableTo(Type * source,Type * target)84 bool Checker::IsTypeComparableTo(Type *source, Type *target)
85 {
86 return relation_->IsComparableTo(source, target);
87 }
88
IsTypeComparableTo(Type * source,Type * target,const std::string & errMsg,const lexer::SourcePosition & errPos)89 bool Checker::IsTypeComparableTo(Type *source, Type *target, const std::string &errMsg,
90 const lexer::SourcePosition &errPos)
91 {
92 if (!IsTypeComparableTo(source, target)) {
93 relation_->RaiseError(errMsg, errPos);
94 }
95
96 return true;
97 }
98
IsTypeComparableTo(Type * source,Type * target,std::initializer_list<TypeErrorMessageElement> list,const lexer::SourcePosition & errPos)99 bool Checker::IsTypeComparableTo(Type *source, Type *target, std::initializer_list<TypeErrorMessageElement> list,
100 const lexer::SourcePosition &errPos)
101 {
102 if (!IsTypeComparableTo(source, target)) {
103 relation_->RaiseError(list, errPos);
104 }
105
106 return true;
107 }
108
AreTypesComparable(Type * source,Type * target)109 bool Checker::AreTypesComparable(Type *source, Type *target)
110 {
111 return IsTypeComparableTo(source, target) || IsTypeComparableTo(target, source);
112 }
113
IsTypeEqualityComparableTo(Type * source,Type * target)114 bool Checker::IsTypeEqualityComparableTo(Type *source, Type *target)
115 {
116 return target->HasTypeFlag(TypeFlag::NULLABLE) || IsTypeComparableTo(source, target);
117 }
118
119 } // namespace panda::es2panda::checker
120