• 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     bool IsOverlapWith(TypeRelation *relation, Type *type);
57 
58     Type *FindExactOrBoxedType(ETSChecker *checker, Type *type) const;
59 
60     static void NormalizeTypes(TypeRelation *relation, ArenaVector<Type *> &types);
61     static void ReduceSubtypes(TypeRelation *relation, ArenaVector<Type *> &types);
62 
63     static ArenaVector<Type *> GetNonConstantTypes(ETSChecker *checker, const ArenaVector<Type *> &types);
64 
65     std::tuple<bool, bool> ResolveConditionExpr() const override;
66 
67     // Do not use it anywhere except codegen
GetAssemblerLUB()68     Type *GetAssemblerLUB() const
69     {
70         return assemblerLub_;
71     }
72 
73     template <class UnaryPredicate>
AllOfConstituentTypes(UnaryPredicate p)74     bool AllOfConstituentTypes(UnaryPredicate p) const
75     {
76         return std::all_of(constituentTypes_.cbegin(), constituentTypes_.cend(), p);
77     }
78 
79     [[nodiscard]] checker::Type *GetAssignableType(ETSChecker *checker, checker::Type *sourceType) const noexcept;
80     [[nodiscard]] std::pair<checker::Type *, checker::Type *> GetComplimentaryType(ETSChecker *checker,
81                                                                                    checker::Type *sourceType);
82 
83 private:
84     static bool EachTypeRelatedToSomeType(TypeRelation *relation, ETSUnionType *source, ETSUnionType *target);
85     static bool TypeRelatedToSomeType(TypeRelation *relation, Type *source, ETSUnionType *target);
86 
87     template <typename RelFN>
88     void RelationTarget(TypeRelation *relation, Type *source, RelFN const &relFn);
89 
90     static void LinearizeAndEraseIdentical(TypeRelation *relation, ArenaVector<Type *> &types);
91     [[nodiscard]] static bool ExtractType(ETSChecker *checker, checker::ETSObjectType *sourceType,
92                                           ArenaVector<Type *> &unionTypes) noexcept;
93     [[nodiscard]] static bool ExtractType(ETSChecker *checker, checker::ETSArrayType *sourceType,
94                                           ArenaVector<Type *> &unionTypes) noexcept;
95 
96     [[nodiscard]] checker::Type *GetAssignableBuiltinType(
97         checker::ETSChecker *checker, checker::ETSObjectType *sourceType, bool isBool, bool isChar,
98         std::map<std::uint32_t, checker::Type *> &numericTypes) const noexcept;
99 
100     bool IsAssignableType(checker::Type *sourceType) const noexcept;
101 
102     static Type *ComputeAssemblerLUB(ETSChecker *checker, ETSUnionType *un);
103 
104     ArenaVector<Type *> const constituentTypes_;
105     Type *assemblerLub_ {nullptr};
106 };
107 }  // namespace ark::es2panda::checker
108 
109 #endif /* ETS_TYPES_ETS_UNION_TYPE_H */
110