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