[a32b204] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
[a436947] | 7 | // CommonType.cc -- |
---|
[a32b204] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 06:59:27 2015 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[6f95000] | 12 | // Last Modified On : Thu Mar 16 16:24:31 2017 |
---|
| 13 | // Update Count : 7 |
---|
[a32b204] | 14 | // |
---|
[51b7345] | 15 | |
---|
| 16 | #include "typeops.h" |
---|
| 17 | #include "SynTree/Type.h" |
---|
| 18 | #include "Unify.h" |
---|
| 19 | |
---|
| 20 | /// #define DEBUG |
---|
| 21 | |
---|
| 22 | namespace ResolvExpr { |
---|
[a32b204] | 23 | class CommonType : public Visitor { |
---|
| 24 | public: |
---|
| 25 | CommonType( Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars ); |
---|
| 26 | Type *get_result() const { return result; } |
---|
| 27 | private: |
---|
| 28 | virtual void visit( VoidType *voidType ); |
---|
| 29 | virtual void visit( BasicType *basicType ); |
---|
| 30 | virtual void visit( PointerType *pointerType ); |
---|
| 31 | virtual void visit( ArrayType *arrayType ); |
---|
[65cec25] | 32 | virtual void visit( ReferenceType *refType ); |
---|
[a32b204] | 33 | virtual void visit( FunctionType *functionType ); |
---|
| 34 | virtual void visit( StructInstType *aggregateUseType ); |
---|
| 35 | virtual void visit( UnionInstType *aggregateUseType ); |
---|
| 36 | virtual void visit( EnumInstType *aggregateUseType ); |
---|
[4040425] | 37 | virtual void visit( TraitInstType *aggregateUseType ); |
---|
[a32b204] | 38 | virtual void visit( TypeInstType *aggregateUseType ); |
---|
| 39 | virtual void visit( TupleType *tupleType ); |
---|
[44b7088] | 40 | virtual void visit( VarArgsType *varArgsType ); |
---|
[89e6ffc] | 41 | virtual void visit( ZeroType *zeroType ); |
---|
| 42 | virtual void visit( OneType *oneType ); |
---|
[a32b204] | 43 | |
---|
[65cec25] | 44 | template< typename Pointer > void getCommonWithVoidPointer( Pointer* voidPointer, Pointer* otherPointer ); |
---|
| 45 | template< typename RefType > void handleRefType( RefType *inst, Type *other ); |
---|
[a32b204] | 46 | |
---|
| 47 | Type *result; |
---|
| 48 | Type *type2; // inherited |
---|
| 49 | bool widenFirst, widenSecond; |
---|
| 50 | const SymTab::Indexer &indexer; |
---|
| 51 | TypeEnvironment &env; |
---|
| 52 | const OpenVarSet &openVars; |
---|
| 53 | }; |
---|
| 54 | |
---|
[65cec25] | 55 | Type * handleReference( ReferenceType * refType, Type * other, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment & env, const OpenVarSet &openVars ) { |
---|
| 56 | Type * result = nullptr, * common = nullptr; |
---|
| 57 | AssertionSet have, need; |
---|
| 58 | OpenVarSet newOpen( openVars ); |
---|
| 59 | // need unify to bind type variables |
---|
| 60 | if ( unify( refType->get_base(), other, env, have, need, newOpen, indexer, common ) ) { |
---|
| 61 | // std::cerr << "unify success" << std::endl; |
---|
| 62 | if ( widenSecond ) { |
---|
| 63 | if ( widenFirst || other->get_qualifiers() <= refType->get_qualifiers() ) { |
---|
| 64 | result = new ReferenceType( refType->get_qualifiers(), common ); // refType->clone(); |
---|
| 65 | result->get_qualifiers() |= other->get_qualifiers(); |
---|
| 66 | } |
---|
| 67 | } else if ( widenFirst ) { |
---|
| 68 | if ( widenSecond || refType->get_qualifiers() <= other->get_qualifiers() ) { |
---|
| 69 | result = common; |
---|
| 70 | result->get_qualifiers() |= refType->get_qualifiers(); |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | } else { |
---|
| 74 | // std::cerr << "exact unify failed: " << refType << " " << other << std::endl; |
---|
| 75 | } |
---|
| 76 | // std::cerr << "common type of reference [" << refType << "] and non-reference [" << other << "] is [" << result << "]" << std::endl; |
---|
| 77 | return result; |
---|
| 78 | } |
---|
| 79 | |
---|
[a32b204] | 80 | Type *commonType( Type *type1, Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars ) { |
---|
| 81 | CommonType visitor( type2, widenFirst, widenSecond, indexer, env, openVars ); |
---|
[65cec25] | 82 | |
---|
| 83 | ReferenceType * refType1 = dynamic_cast< ReferenceType * >( type1 ); |
---|
| 84 | ReferenceType * refType2 = dynamic_cast< ReferenceType * >( type2 ); |
---|
| 85 | if ( (refType1 || refType2) && (! refType1 || ! refType2) ) { |
---|
| 86 | // handle the case where exactly one of the types is a reference type specially |
---|
| 87 | if ( refType1 ) { |
---|
| 88 | return handleReference( refType1, type2, widenFirst, widenSecond, indexer, env, openVars ); |
---|
| 89 | } else if ( refType2 ) { |
---|
| 90 | return handleReference( refType2, type1, widenSecond, widenFirst, indexer, env, openVars ); |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | |
---|
[a32b204] | 94 | type1->accept( visitor ); |
---|
| 95 | Type *result = visitor.get_result(); |
---|
| 96 | if ( ! result ) { |
---|
[2c57025] | 97 | // this appears to be handling for opaque type declarations |
---|
[a32b204] | 98 | if ( widenSecond ) { |
---|
[2c57025] | 99 | if ( TypeInstType *inst = dynamic_cast< TypeInstType* >( type2 ) ) { |
---|
| 100 | if ( NamedTypeDecl *nt = indexer.lookupType( inst->get_name() ) ) { |
---|
| 101 | TypeDecl *type = safe_dynamic_cast< TypeDecl* >( nt ); |
---|
[a32b204] | 102 | if ( type->get_base() ) { |
---|
| 103 | Type::Qualifiers tq1 = type1->get_qualifiers(), tq2 = type2->get_qualifiers(); |
---|
| 104 | AssertionSet have, need; |
---|
| 105 | OpenVarSet newOpen( openVars ); |
---|
| 106 | type1->get_qualifiers() = Type::Qualifiers(); |
---|
| 107 | type->get_base()->get_qualifiers() = tq1; |
---|
| 108 | if ( unifyExact( type1, type->get_base(), env, have, need, newOpen, indexer ) ) { |
---|
| 109 | result = type1->clone(); |
---|
[6f95000] | 110 | result->get_qualifiers() = tq1 | tq2; |
---|
[a32b204] | 111 | } // if |
---|
| 112 | type1->get_qualifiers() = tq1; |
---|
| 113 | type->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
| 114 | } // if |
---|
| 115 | } // if |
---|
| 116 | } // if |
---|
| 117 | } // if |
---|
| 118 | } // if |
---|
[51b7345] | 119 | #ifdef DEBUG |
---|
[a32b204] | 120 | std::cout << "============= commonType" << std::endl << "type1 is "; |
---|
| 121 | type1->print( std::cout ); |
---|
| 122 | std::cout << " type2 is "; |
---|
| 123 | type2->print( std::cout ); |
---|
| 124 | if ( result ) { |
---|
| 125 | std::cout << " common type is "; |
---|
| 126 | result->print( std::cout ); |
---|
| 127 | } else { |
---|
| 128 | std::cout << " no common type"; |
---|
| 129 | } // if |
---|
| 130 | std::cout << std::endl; |
---|
[51b7345] | 131 | #endif |
---|
[a32b204] | 132 | return result; |
---|
| 133 | } |
---|
[51b7345] | 134 | |
---|
[a32b204] | 135 | static const BasicType::Kind combinedType[ BasicType::NUMBER_OF_BASIC_TYPES ][ BasicType::NUMBER_OF_BASIC_TYPES ] = |
---|
| 136 | { |
---|
[51b7345] | 137 | /* Bool Char SignedChar UnsignedChar ShortSignedInt ShortUnsignedInt SignedInt UnsignedInt LongSignedInt LongUnsignedInt LongLongSignedInt LongLongUnsignedInt Float Double LongDouble FloatComplex DoubleComplex LongDoubleComplex FloatImaginary DoubleImaginary LongDoubleImaginary */ |
---|
[a32b204] | 138 | /* Bool */ { BasicType::Bool, BasicType::Char, BasicType::SignedChar, BasicType::UnsignedChar, BasicType::ShortSignedInt, BasicType::ShortUnsignedInt, BasicType::SignedInt, BasicType::UnsignedInt, BasicType::LongSignedInt, BasicType::LongUnsignedInt, BasicType::LongLongSignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
| 139 | /* Char */ { BasicType::Char, BasicType::Char, BasicType::UnsignedChar, BasicType::UnsignedChar, BasicType::ShortSignedInt, BasicType::ShortUnsignedInt, BasicType::SignedInt, BasicType::UnsignedInt, BasicType::LongSignedInt, BasicType::LongUnsignedInt, BasicType::LongLongSignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
| 140 | /* SignedChar */ { BasicType::SignedChar, BasicType::UnsignedChar, BasicType::SignedChar, BasicType::UnsignedChar, BasicType::ShortSignedInt, BasicType::ShortUnsignedInt, BasicType::SignedInt, BasicType::UnsignedInt, BasicType::LongSignedInt, BasicType::LongUnsignedInt, BasicType::LongLongSignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
| 141 | /* UnsignedChar */ { BasicType::UnsignedChar, BasicType::UnsignedChar, BasicType::UnsignedChar, BasicType::UnsignedChar, BasicType::ShortSignedInt, BasicType::ShortUnsignedInt, BasicType::SignedInt, BasicType::UnsignedInt, BasicType::LongSignedInt, BasicType::LongUnsignedInt, BasicType::LongLongSignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
| 142 | /* ShortSignedInt */ { BasicType::ShortSignedInt, BasicType::ShortSignedInt, BasicType::ShortSignedInt, BasicType::ShortSignedInt, BasicType::ShortSignedInt, BasicType::ShortUnsignedInt, BasicType::SignedInt, BasicType::UnsignedInt, BasicType::LongSignedInt, BasicType::LongUnsignedInt, BasicType::LongLongSignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
| 143 | /* ShortUnsignedInt */ { BasicType::ShortUnsignedInt, BasicType::ShortUnsignedInt, BasicType::ShortUnsignedInt, BasicType::ShortUnsignedInt, BasicType::ShortUnsignedInt, BasicType::ShortUnsignedInt, BasicType::SignedInt, BasicType::UnsignedInt, BasicType::LongSignedInt, BasicType::LongUnsignedInt, BasicType::LongLongSignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
| 144 | /* SignedInt */ { BasicType::SignedInt, BasicType::SignedInt, BasicType::SignedInt, BasicType::SignedInt, BasicType::SignedInt, BasicType::SignedInt, BasicType::SignedInt, BasicType::UnsignedInt, BasicType::LongSignedInt, BasicType::LongUnsignedInt, BasicType::LongLongSignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
| 145 | /* UnsignedInt */ { BasicType::UnsignedInt, BasicType::UnsignedInt, BasicType::UnsignedInt, BasicType::UnsignedInt, BasicType::UnsignedInt, BasicType::UnsignedInt, BasicType::UnsignedInt, BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongSignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
| 146 | /* LongSignedInt */ { BasicType::LongSignedInt, BasicType::LongSignedInt, BasicType::LongSignedInt, BasicType::LongSignedInt, BasicType::LongSignedInt, BasicType::LongSignedInt, BasicType::LongSignedInt, BasicType::LongUnsignedInt, BasicType::LongSignedInt, BasicType::LongUnsignedInt, BasicType::LongLongSignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
| 147 | /* LongUnsignedInt */ { BasicType::LongUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongSignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
| 148 | /* LongLongSignedInt */ { BasicType::LongLongSignedInt, BasicType::LongLongSignedInt, BasicType::LongLongSignedInt, BasicType::LongLongSignedInt, BasicType::LongLongSignedInt, BasicType::LongLongSignedInt, BasicType::LongLongSignedInt, BasicType::LongLongSignedInt, BasicType::LongLongSignedInt, BasicType::LongLongSignedInt, BasicType::LongLongSignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
| 149 | /* LongLongUnsignedInt */ { BasicType::LongLongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
| 150 | /* Float */ { BasicType::Float, BasicType::Float, BasicType::Float, BasicType::Float, BasicType::Float, BasicType::Float, BasicType::Float, BasicType::Float, BasicType::Float, BasicType::Float, BasicType::Float, BasicType::Float, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
| 151 | /* Double */ { BasicType::Double, BasicType::Double, BasicType::Double, BasicType::Double, BasicType::Double, BasicType::Double, BasicType::Double, BasicType::Double, BasicType::Double, BasicType::Double, BasicType::Double, BasicType::Double, BasicType::Double, BasicType::Double, BasicType::LongDouble, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
| 152 | /* LongDouble */ { BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex }, |
---|
| 153 | /* FloatComplex */ { BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
| 154 | /* DoubleComplex */ { BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
| 155 | /* LongDoubleComplex */ { BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex }, |
---|
| 156 | /* FloatImaginary */ { BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatImaginary, BasicType::DoubleImaginary, BasicType::LongDoubleImaginary }, |
---|
| 157 | /* DoubleImaginary */ { BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::DoubleImaginary, BasicType::DoubleImaginary, BasicType::LongDoubleImaginary }, |
---|
| 158 | /* LongDoubleImaginary */ { BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleImaginary, BasicType::LongDoubleImaginary, BasicType::LongDoubleImaginary } |
---|
| 159 | }; |
---|
| 160 | |
---|
| 161 | CommonType::CommonType( Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars ) |
---|
| 162 | : result( 0 ), type2( type2 ), widenFirst( widenFirst ), widenSecond( widenSecond ), indexer( indexer ), env( env ), openVars( openVars ) { |
---|
| 163 | } |
---|
| 164 | |
---|
[7e003011] | 165 | void CommonType::visit( __attribute((unused)) VoidType *voidType ) {} |
---|
[a32b204] | 166 | |
---|
| 167 | void CommonType::visit( BasicType *basicType ) { |
---|
| 168 | if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) { |
---|
| 169 | BasicType::Kind newType = combinedType[ basicType->get_kind() ][ otherBasic->get_kind() ]; |
---|
| 170 | if ( ( ( newType == basicType->get_kind() && basicType->get_qualifiers() >= otherBasic->get_qualifiers() ) || widenFirst ) && ( ( newType == otherBasic->get_kind() && basicType->get_qualifiers() <= otherBasic->get_qualifiers() ) || widenSecond ) ) { |
---|
[6f95000] | 171 | result = new BasicType( basicType->get_qualifiers() | otherBasic->get_qualifiers(), newType ); |
---|
[a32b204] | 172 | } // if |
---|
[89e6ffc] | 173 | } else if ( dynamic_cast< EnumInstType * > ( type2 ) || dynamic_cast< ZeroType* >( type2 ) || dynamic_cast< OneType* >( type2 ) ) { |
---|
| 174 | // use signed int in lieu of the enum/zero/one type |
---|
[a436947] | 175 | BasicType::Kind newType = combinedType[ basicType->get_kind() ][ BasicType::SignedInt ]; |
---|
[89e6ffc] | 176 | if ( ( ( newType == basicType->get_kind() && basicType->get_qualifiers() >= type2->get_qualifiers() ) || widenFirst ) && ( ( newType != basicType->get_kind() && basicType->get_qualifiers() <= type2->get_qualifiers() ) || widenSecond ) ) { |
---|
[6f95000] | 177 | result = new BasicType( basicType->get_qualifiers() | type2->get_qualifiers(), newType ); |
---|
[a436947] | 178 | } // if |
---|
[a32b204] | 179 | } // if |
---|
| 180 | } |
---|
| 181 | |
---|
[65cec25] | 182 | template< typename Pointer > |
---|
| 183 | void CommonType::getCommonWithVoidPointer( Pointer* voidPointer, Pointer* otherPointer ) { |
---|
[a2a77af] | 184 | if ( TypeInstType* var = dynamic_cast< TypeInstType* >( otherPointer->get_base() ) ) { |
---|
| 185 | OpenVarSet::const_iterator entry = openVars.find( var->get_name() ); |
---|
| 186 | if ( entry != openVars.end() ) { |
---|
| 187 | AssertionSet need, have; |
---|
| 188 | WidenMode widen( widenFirst, widenSecond ); |
---|
| 189 | if ( entry != openVars.end() && ! bindVar(var, voidPointer->get_base(), entry->second, env, need, have, openVars, widen, indexer ) ) return; |
---|
| 190 | } |
---|
[2c57025] | 191 | } |
---|
[a2a77af] | 192 | result = voidPointer->clone(); |
---|
[6f95000] | 193 | result->get_qualifiers() |= otherPointer->get_qualifiers(); |
---|
[2c57025] | 194 | } |
---|
| 195 | |
---|
[a32b204] | 196 | void CommonType::visit( PointerType *pointerType ) { |
---|
| 197 | if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) { |
---|
[d7dc824] | 198 | if ( widenFirst && dynamic_cast< VoidType* >( otherPointer->get_base() ) && ! isFtype(pointerType->get_base()) ) { |
---|
[a2a77af] | 199 | getCommonWithVoidPointer( otherPointer, pointerType ); |
---|
[d7dc824] | 200 | } else if ( widenSecond && dynamic_cast< VoidType* >( pointerType->get_base() ) && ! isFtype(otherPointer->get_base()) ) { |
---|
[a2a77af] | 201 | getCommonWithVoidPointer( pointerType, otherPointer ); |
---|
[a32b204] | 202 | } else if ( ( pointerType->get_base()->get_qualifiers() >= otherPointer->get_base()->get_qualifiers() || widenFirst ) |
---|
| 203 | && ( pointerType->get_base()->get_qualifiers() <= otherPointer->get_base()->get_qualifiers() || widenSecond ) ) { |
---|
| 204 | Type::Qualifiers tq1 = pointerType->get_base()->get_qualifiers(), tq2 = otherPointer->get_base()->get_qualifiers(); |
---|
| 205 | pointerType->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
| 206 | otherPointer->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
| 207 | AssertionSet have, need; |
---|
| 208 | OpenVarSet newOpen( openVars ); |
---|
| 209 | if ( unifyExact( pointerType->get_base(), otherPointer->get_base(), env, have, need, newOpen, indexer ) ) { |
---|
| 210 | if ( tq1 < tq2 ) { |
---|
| 211 | result = pointerType->clone(); |
---|
| 212 | } else { |
---|
| 213 | result = otherPointer->clone(); |
---|
| 214 | } // if |
---|
[6f95000] | 215 | result->get_qualifiers() = tq1 | tq2; |
---|
[a32b204] | 216 | } else { |
---|
| 217 | /// std::cout << "place for ptr-to-type" << std::endl; |
---|
| 218 | } // if |
---|
| 219 | pointerType->get_base()->get_qualifiers() = tq1; |
---|
| 220 | otherPointer->get_base()->get_qualifiers() = tq2; |
---|
| 221 | } // if |
---|
[89e6ffc] | 222 | } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) { |
---|
| 223 | result = pointerType->clone(); |
---|
[6f95000] | 224 | result->get_qualifiers() |= type2->get_qualifiers(); |
---|
[a32b204] | 225 | } // if |
---|
| 226 | } |
---|
| 227 | |
---|
[7e003011] | 228 | void CommonType::visit( __attribute((unused)) ArrayType *arrayType ) {} |
---|
[65cec25] | 229 | |
---|
| 230 | void CommonType::visit( ReferenceType *refType ) { |
---|
| 231 | if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) { |
---|
| 232 | if ( widenFirst && dynamic_cast< VoidType* >( otherRef->get_base() ) && ! isFtype(refType->get_base()) ) { |
---|
| 233 | getCommonWithVoidPointer( otherRef, refType ); |
---|
| 234 | } else if ( widenSecond && dynamic_cast< VoidType* >( refType->get_base() ) && ! isFtype(otherRef->get_base()) ) { |
---|
| 235 | getCommonWithVoidPointer( refType, otherRef ); |
---|
| 236 | } else if ( ( refType->get_base()->get_qualifiers() >= otherRef->get_base()->get_qualifiers() || widenFirst ) |
---|
| 237 | && ( refType->get_base()->get_qualifiers() <= otherRef->get_base()->get_qualifiers() || widenSecond ) ) { |
---|
| 238 | Type::Qualifiers tq1 = refType->get_base()->get_qualifiers(), tq2 = otherRef->get_base()->get_qualifiers(); |
---|
| 239 | refType->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
| 240 | otherRef->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
| 241 | AssertionSet have, need; |
---|
| 242 | OpenVarSet newOpen( openVars ); |
---|
| 243 | if ( unifyExact( refType->get_base(), otherRef->get_base(), env, have, need, newOpen, indexer ) ) { |
---|
| 244 | if ( tq1 < tq2 ) { |
---|
| 245 | result = refType->clone(); |
---|
| 246 | } else { |
---|
| 247 | result = otherRef->clone(); |
---|
| 248 | } // if |
---|
| 249 | result->get_qualifiers() = tq1 | tq2; |
---|
| 250 | } else { |
---|
| 251 | /// std::cout << "place for ptr-to-type" << std::endl; |
---|
| 252 | } // if |
---|
| 253 | refType->get_base()->get_qualifiers() = tq1; |
---|
| 254 | otherRef->get_base()->get_qualifiers() = tq2; |
---|
| 255 | } // if |
---|
| 256 | } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) { |
---|
| 257 | result = refType->clone(); |
---|
| 258 | result->get_qualifiers() |= type2->get_qualifiers(); |
---|
| 259 | } // if |
---|
| 260 | } |
---|
| 261 | |
---|
[7e003011] | 262 | void CommonType::visit( __attribute((unused)) FunctionType *functionType ) {} |
---|
| 263 | void CommonType::visit( __attribute((unused)) StructInstType *aggregateUseType ) {} |
---|
| 264 | void CommonType::visit( __attribute((unused)) UnionInstType *aggregateUseType ) {} |
---|
[a32b204] | 265 | |
---|
[a436947] | 266 | void CommonType::visit( EnumInstType *enumInstType ) { |
---|
[89e6ffc] | 267 | if ( dynamic_cast< BasicType * >( type2 ) || dynamic_cast< ZeroType* >( type2 ) || dynamic_cast< OneType* >( type2 ) ) { |
---|
[a436947] | 268 | // reuse BasicType, EnumInstType code by swapping type2 with enumInstType |
---|
[65cec25] | 269 | ValueGuard< Type * > temp( type2 ); |
---|
[a436947] | 270 | type2 = enumInstType; |
---|
[65cec25] | 271 | temp.old->accept( *this ); |
---|
[a436947] | 272 | } // if |
---|
[a32b204] | 273 | } |
---|
| 274 | |
---|
[7e003011] | 275 | void CommonType::visit( __attribute((unused)) TraitInstType *aggregateUseType ) { |
---|
[a32b204] | 276 | } |
---|
| 277 | |
---|
| 278 | void CommonType::visit( TypeInstType *inst ) { |
---|
| 279 | if ( widenFirst ) { |
---|
| 280 | NamedTypeDecl *nt = indexer.lookupType( inst->get_name() ); |
---|
| 281 | if ( nt ) { |
---|
[2c57025] | 282 | TypeDecl *type = safe_dynamic_cast< TypeDecl* >( nt ); |
---|
[a32b204] | 283 | if ( type->get_base() ) { |
---|
| 284 | Type::Qualifiers tq1 = inst->get_qualifiers(), tq2 = type2->get_qualifiers(); |
---|
| 285 | AssertionSet have, need; |
---|
| 286 | OpenVarSet newOpen( openVars ); |
---|
| 287 | type2->get_qualifiers() = Type::Qualifiers(); |
---|
| 288 | type->get_base()->get_qualifiers() = tq1; |
---|
| 289 | if ( unifyExact( type->get_base(), type2, env, have, need, newOpen, indexer ) ) { |
---|
| 290 | result = type2->clone(); |
---|
[6f95000] | 291 | result->get_qualifiers() = tq1 | tq2; |
---|
[a32b204] | 292 | } // if |
---|
| 293 | type2->get_qualifiers() = tq2; |
---|
| 294 | type->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
| 295 | } // if |
---|
| 296 | } // if |
---|
| 297 | } // if |
---|
| 298 | } |
---|
| 299 | |
---|
[7e003011] | 300 | void CommonType::visit( __attribute((unused)) TupleType *tupleType ) {} |
---|
| 301 | void CommonType::visit( __attribute((unused)) VarArgsType *varArgsType ) {} |
---|
[89e6ffc] | 302 | |
---|
| 303 | void CommonType::visit( ZeroType *zeroType ) { |
---|
| 304 | if ( widenFirst ) { |
---|
| 305 | if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< PointerType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) { |
---|
| 306 | if ( widenSecond || zeroType->get_qualifiers() <= type2->get_qualifiers() ) { |
---|
| 307 | result = type2->clone(); |
---|
[6f95000] | 308 | result->get_qualifiers() |= zeroType->get_qualifiers(); |
---|
[89e6ffc] | 309 | } |
---|
[4cb935e] | 310 | } else if ( widenSecond && dynamic_cast< OneType* >( type2 ) ) { |
---|
| 311 | result = new BasicType( zeroType->get_qualifiers(), BasicType::SignedInt ); |
---|
[6f95000] | 312 | result->get_qualifiers() |= type2->get_qualifiers(); |
---|
[89e6ffc] | 313 | } |
---|
| 314 | } |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | void CommonType::visit( OneType *oneType ) { |
---|
| 318 | if ( widenFirst ) { |
---|
| 319 | if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) { |
---|
| 320 | if ( widenSecond || oneType->get_qualifiers() <= type2->get_qualifiers() ) { |
---|
| 321 | result = type2->clone(); |
---|
[6f95000] | 322 | result->get_qualifiers() |= oneType->get_qualifiers(); |
---|
[89e6ffc] | 323 | } |
---|
[4cb935e] | 324 | } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) { |
---|
| 325 | result = new BasicType( oneType->get_qualifiers(), BasicType::SignedInt ); |
---|
[6f95000] | 326 | result->get_qualifiers() |= type2->get_qualifiers(); |
---|
[89e6ffc] | 327 | } |
---|
| 328 | } |
---|
| 329 | } |
---|
[51b7345] | 330 | } // namespace ResolvExpr |
---|
[a32b204] | 331 | |
---|
| 332 | // Local Variables: // |
---|
| 333 | // tab-width: 4 // |
---|
| 334 | // mode: c++ // |
---|
| 335 | // compile-command: "make install" // |
---|
| 336 | // End: // |
---|