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 panda::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 const ArenaVector<Type *> &ConstituentTypes() const 31 { 32 return constituentTypes_; 33 } 34 35 void ToString(std::stringstream &ss) 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 GetLeastUpperBoundType()51 Type *GetLeastUpperBoundType() const 52 { 53 ASSERT(lubType_ != nullptr); 54 return lubType_; 55 } 56 57 bool HasObjectType(ETSObjectFlags flag) const; 58 59 Type *FindExactOrBoxedType(ETSChecker *checker, Type *type) const; 60 61 static void NormalizeTypes(TypeRelation *relation, ArenaVector<Type *> &constituentTypes); 62 ResolveConditionExpr()63 std::tuple<bool, bool> ResolveConditionExpr() const override 64 { 65 for (auto const &tp : ConstituentTypes()) { 66 if (!tp->IsConditionalExprType()) { 67 return {true, false}; 68 } 69 } 70 return {true, true}; 71 } 72 73 private: 74 static bool EachTypeRelatedToSomeType(TypeRelation *relation, ETSUnionType *source, ETSUnionType *target); 75 static bool TypeRelatedToSomeType(TypeRelation *relation, Type *source, ETSUnionType *target); 76 77 static void LinearizeAndEraseIdentical(TypeRelation *relation, ArenaVector<Type *> &constituentTypes); 78 79 Type *ComputeLUB(ETSChecker *checker) const; 80 81 ArenaVector<Type *> const constituentTypes_; 82 Type *lubType_ {nullptr}; 83 }; 84 } // namespace panda::es2panda::checker 85 86 #endif /* ETS_TYPES_ETS_UNION_TYPE_H */ 87