• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ES2PANDA_COMPILER_CHECKER_TYPES_ETS_UNION_TYPE_H
17 #define ES2PANDA_COMPILER_CHECKER_TYPES_ETS_UNION_TYPE_H
18 
19 #include "checker/types/type.h"
20 #include "checker/types/ets/etsObjectType.h"
21 
22 namespace ark::es2panda::checker {
23 class GlobalTypesHolder;
24 
25 class ETSUnionType : public Type {
26 public:
27     // constituentTypes must be normalized
28     explicit ETSUnionType(ETSChecker *checker, ArenaVector<Type *> &&constituentTypes);
29 
ConstituentTypes()30     [[nodiscard]] const ArenaVector<Type *> &ConstituentTypes() const noexcept
31     {
32         return constituentTypes_;
33     }
34 
35     void ToString(std::stringstream &ss, bool precise) const override;
36     void ToAssemblerType(std::stringstream &ss) const override;
37     void ToDebugInfoType(std::stringstream &ss) const override;
38     void Identical(TypeRelation *relation, Type *other) override;
39     void AssignmentTarget(TypeRelation *relation, Type *source) override;
40     bool AssignmentSource(TypeRelation *relation, Type *target) override;
41     Type *Instantiate(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *globalTypes) override;
42     Type *Substitute(TypeRelation *relation, const Substitution *substitution) override;
43     void Cast(TypeRelation *relation, Type *target) override;
44     void CastTarget(TypeRelation *relation, Type *source) override;
45     void IsSupertypeOf(TypeRelation *relation, Type *source) override;
46     void IsSubtypeOf(TypeRelation *relation, Type *target) override;
47     Type *FindTypeIsCastableToThis(ir::Expression *node, TypeRelation *relation, Type *source) const;
48     Type *FindTypeIsCastableToSomeType(ir::Expression *node, TypeRelation *relation, Type *target) const;
49     Type *FindUnboxableType() const;
50 
51     bool HasObjectType(ETSObjectFlags flag) const;
52     bool HasUndefinedType() const;
53     bool HasType(Type *type) const;
54     bool HasNullishType(const ETSChecker *checker) const;
55 
56     Type *FindExactOrBoxedType(ETSChecker *checker, Type *type) const;
57 
58     static void NormalizeTypes(TypeRelation *relation, ArenaVector<Type *> &types);
59 
60     std::tuple<bool, bool> ResolveConditionExpr() const override;
61 
62     // Do not use it anywhere except codegen
GetAssemblerLUB()63     Type *GetAssemblerLUB() const
64     {
65         return assemblerLub_;
66     }
67 
68     template <class UnaryPredicate>
AllOfConstituentTypes(UnaryPredicate p)69     bool AllOfConstituentTypes(UnaryPredicate p) const
70     {
71         return std::all_of(constituentTypes_.cbegin(), constituentTypes_.cend(), p);
72     }
73 
74     [[nodiscard]] checker::Type *GetAssignableType(ETSChecker *checker, checker::Type *sourceType) const noexcept;
75     [[nodiscard]] std::pair<checker::Type *, checker::Type *> GetComplimentaryType(ETSChecker *checker,
76                                                                                    checker::Type *sourceType);
77 
78 private:
79     static bool EachTypeRelatedToSomeType(TypeRelation *relation, ETSUnionType *source, ETSUnionType *target);
80     static bool TypeRelatedToSomeType(TypeRelation *relation, Type *source, ETSUnionType *target);
81 
82     template <typename RelFN>
83     void RelationTarget(TypeRelation *relation, Type *source, RelFN const &relFn);
84 
85     static void LinearizeAndEraseIdentical(TypeRelation *relation, ArenaVector<Type *> &types);
86     [[nodiscard]] static bool ExtractType(ETSChecker *checker, checker::ETSObjectType *sourceType,
87                                           ArenaVector<Type *> &unionTypes) noexcept;
88     [[nodiscard]] static bool ExtractType(ETSChecker *checker, checker::ETSArrayType *sourceType,
89                                           ArenaVector<Type *> &unionTypes) noexcept;
90 
91     [[nodiscard]] checker::Type *GetAssignableBuiltinType(
92         checker::ETSChecker *checker, checker::ETSObjectType *sourceType, bool isBool, bool isChar,
93         std::map<std::uint32_t, checker::Type *> &numericTypes) const noexcept;
94 
95     static Type *ComputeAssemblerLUB(ETSChecker *checker, ETSUnionType *un);
96 
97     ArenaVector<Type *> const constituentTypes_;
98     Type *assemblerLub_ {nullptr};
99 };
100 }  // namespace ark::es2panda::checker
101 
102 #endif /* ETS_TYPES_ETS_UNION_TYPE_H */
103