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