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