[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 |
---|
[201aeb9] | 12 | // Last Modified On : Mon Sep 25 15:18:17 2017 |
---|
| 13 | // Update Count : 9 |
---|
[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 | |
---|
[000178a] | 63 | Type * handleReference( Type * t1, Type * t2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment & env, const OpenVarSet &openVars ) { |
---|
| 64 | Type * common = nullptr; |
---|
[65cec25] | 65 | AssertionSet have, need; |
---|
| 66 | OpenVarSet newOpen( openVars ); |
---|
| 67 | // need unify to bind type variables |
---|
[000178a] | 68 | if ( unify( t1, t2, env, have, need, newOpen, indexer, common ) ) { |
---|
| 69 | // std::cerr << "unify success: " << widenFirst << " " << widenSecond << std::endl; |
---|
| 70 | if ( (widenFirst || t2->get_qualifiers() <= t1->get_qualifiers()) && (widenSecond || t1->get_qualifiers() <= t2->get_qualifiers()) ) { |
---|
| 71 | // std::cerr << "widen okay" << std::endl; |
---|
| 72 | common->get_qualifiers() |= t1->get_qualifiers(); |
---|
| 73 | common->get_qualifiers() |= t2->get_qualifiers(); |
---|
| 74 | return common; |
---|
[65cec25] | 75 | } |
---|
| 76 | } |
---|
[000178a] | 77 | // std::cerr << "exact unify failed: " << t1 << " " << t2 << std::endl; |
---|
| 78 | return nullptr; |
---|
[65cec25] | 79 | } |
---|
| 80 | |
---|
[a32b204] | 81 | Type *commonType( Type *type1, Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars ) { |
---|
| 82 | CommonType visitor( type2, widenFirst, widenSecond, indexer, env, openVars ); |
---|
[65cec25] | 83 | |
---|
[e6cee92] | 84 | int depth1 = type1->referenceDepth(); |
---|
| 85 | int depth2 = type2->referenceDepth(); |
---|
| 86 | if ( depth1 > 0 || depth2 > 0 ) { |
---|
| 87 | int diff = depth1-depth2; |
---|
| 88 | // 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. |
---|
| 89 | if ( diff > 1 || diff < -1 ) return nullptr; |
---|
| 90 | |
---|
| 91 | // special case where one type has a reference depth of 1 larger than the other |
---|
[000178a] | 92 | if ( diff > 0 || diff < 0 ) { |
---|
[74cdfb1] | 93 | // std::cerr << "reference depth diff: " << diff << std::endl; |
---|
[000178a] | 94 | Type * result = nullptr; |
---|
[74cdfb1] | 95 | ReferenceType * ref1 = dynamic_cast< ReferenceType * >( type1 ); |
---|
| 96 | ReferenceType * ref2 = dynamic_cast< ReferenceType * >( type2 ); |
---|
| 97 | if ( diff > 0 ) { |
---|
| 98 | // deeper on the left |
---|
| 99 | assert( ref1 ); |
---|
[000178a] | 100 | result = handleReference( ref1->base, type2, widenFirst, widenSecond, indexer, env, openVars ); |
---|
| 101 | } else { |
---|
[74cdfb1] | 102 | // deeper on the right |
---|
| 103 | assert( ref2 ); |
---|
[000178a] | 104 | result = handleReference( type1, ref2->base, widenFirst, widenSecond, indexer, env, openVars ); |
---|
| 105 | } |
---|
[74cdfb1] | 106 | if ( result && ref1 ) { |
---|
| 107 | // formal is reference, so result should be reference |
---|
| 108 | // std::cerr << "formal is reference; result should be reference" << std::endl; |
---|
| 109 | result = new ReferenceType( ref1->get_qualifiers(), result ); |
---|
| 110 | } |
---|
[000178a] | 111 | // std::cerr << "common type of reference [" << type1 << "] and [" << type2 << "] is [" << result << "]" << std::endl; |
---|
| 112 | return result; |
---|
[65cec25] | 113 | } |
---|
[e6cee92] | 114 | // otherwise, both are reference types of the same depth and this is handled by the CommonType visitor. |
---|
[65cec25] | 115 | } |
---|
| 116 | |
---|
[a32b204] | 117 | type1->accept( visitor ); |
---|
| 118 | Type *result = visitor.get_result(); |
---|
| 119 | if ( ! result ) { |
---|
[2c57025] | 120 | // this appears to be handling for opaque type declarations |
---|
[a32b204] | 121 | if ( widenSecond ) { |
---|
[2c57025] | 122 | if ( TypeInstType *inst = dynamic_cast< TypeInstType* >( type2 ) ) { |
---|
| 123 | if ( NamedTypeDecl *nt = indexer.lookupType( inst->get_name() ) ) { |
---|
[e3e16bc] | 124 | TypeDecl *type = strict_dynamic_cast< TypeDecl* >( nt ); |
---|
[a32b204] | 125 | if ( type->get_base() ) { |
---|
| 126 | Type::Qualifiers tq1 = type1->get_qualifiers(), tq2 = type2->get_qualifiers(); |
---|
| 127 | AssertionSet have, need; |
---|
| 128 | OpenVarSet newOpen( openVars ); |
---|
| 129 | type1->get_qualifiers() = Type::Qualifiers(); |
---|
| 130 | type->get_base()->get_qualifiers() = tq1; |
---|
| 131 | if ( unifyExact( type1, type->get_base(), env, have, need, newOpen, indexer ) ) { |
---|
| 132 | result = type1->clone(); |
---|
[6f95000] | 133 | result->get_qualifiers() = tq1 | tq2; |
---|
[a32b204] | 134 | } // if |
---|
| 135 | type1->get_qualifiers() = tq1; |
---|
| 136 | type->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
| 137 | } // if |
---|
| 138 | } // if |
---|
| 139 | } // if |
---|
| 140 | } // if |
---|
| 141 | } // if |
---|
[51b7345] | 142 | #ifdef DEBUG |
---|
[c93bc28] | 143 | std::cerr << "============= commonType" << std::endl << "type1 is "; |
---|
| 144 | type1->print( std::cerr ); |
---|
| 145 | std::cerr << " type2 is "; |
---|
| 146 | type2->print( std::cerr ); |
---|
[a32b204] | 147 | if ( result ) { |
---|
[c93bc28] | 148 | std::cerr << " common type is "; |
---|
| 149 | result->print( std::cerr ); |
---|
[a32b204] | 150 | } else { |
---|
[c93bc28] | 151 | std::cerr << " no common type"; |
---|
[a32b204] | 152 | } // if |
---|
[c93bc28] | 153 | std::cerr << std::endl; |
---|
[51b7345] | 154 | #endif |
---|
[a32b204] | 155 | return result; |
---|
| 156 | } |
---|
[51b7345] | 157 | |
---|
[a32b204] | 158 | static const BasicType::Kind combinedType[ BasicType::NUMBER_OF_BASIC_TYPES ][ BasicType::NUMBER_OF_BASIC_TYPES ] = |
---|
| 159 | { |
---|
[201aeb9] | 160 | /* Bool Char SignedChar UnsignedChar ShortSignedInt ShortUnsignedInt SignedInt UnsignedInt LongSignedInt LongUnsignedInt LongLongSignedInt LongLongUnsignedInt Float Double LongDouble FloatComplex DoubleComplex LongDoubleComplex FloatImaginary DoubleImaginary LongDoubleImaginary SignedInt128 UnsignedInt128 */ |
---|
| 161 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, }, |
---|
| 162 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, }, |
---|
| 163 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, }, |
---|
| 164 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, }, |
---|
| 165 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, }, |
---|
| 166 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, }, |
---|
| 167 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, }, |
---|
| 168 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, }, |
---|
| 169 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, }, |
---|
| 170 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, }, |
---|
| 171 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, }, |
---|
| 172 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, }, |
---|
| 173 | /* 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, BasicType::Float, BasicType::Float, }, |
---|
| 174 | /* 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, BasicType::Double, BasicType::Double, }, |
---|
| 175 | /* 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, BasicType::LongDouble, BasicType::LongDouble, }, |
---|
| 176 | /* 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, BasicType::FloatComplex, BasicType::FloatComplex, }, |
---|
| 177 | /* 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, BasicType::DoubleComplex, BasicType::DoubleComplex, }, |
---|
| 178 | /* 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, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, }, |
---|
| 179 | /* 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, BasicType::FloatImaginary, BasicType::FloatImaginary, }, |
---|
| 180 | /* 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, BasicType::DoubleImaginary, BasicType::DoubleImaginary, }, |
---|
| 181 | /* 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 }, |
---|
| 182 | /* SignedInt128 */ { BasicType::SignedInt128, BasicType::SignedInt128, BasicType::SignedInt128, BasicType::SignedInt128, BasicType::SignedInt128, BasicType::SignedInt128, BasicType::SignedInt128, BasicType::SignedInt128, BasicType::SignedInt128, BasicType::SignedInt128, BasicType::SignedInt128, BasicType::SignedInt128, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::SignedInt128, BasicType::UnsignedInt128, }, |
---|
| 183 | /* UnsignedInt128 */ { BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::UnsignedInt128, BasicType::UnsignedInt128, }, |
---|
[a32b204] | 184 | }; |
---|
| 185 | |
---|
| 186 | CommonType::CommonType( Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars ) |
---|
| 187 | : result( 0 ), type2( type2 ), widenFirst( widenFirst ), widenSecond( widenSecond ), indexer( indexer ), env( env ), openVars( openVars ) { |
---|
| 188 | } |
---|
| 189 | |
---|
[ecd4923] | 190 | void CommonType::visit( VoidType * ) {} |
---|
[a32b204] | 191 | |
---|
| 192 | void CommonType::visit( BasicType *basicType ) { |
---|
| 193 | if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) { |
---|
| 194 | BasicType::Kind newType = combinedType[ basicType->get_kind() ][ otherBasic->get_kind() ]; |
---|
| 195 | if ( ( ( newType == basicType->get_kind() && basicType->get_qualifiers() >= otherBasic->get_qualifiers() ) || widenFirst ) && ( ( newType == otherBasic->get_kind() && basicType->get_qualifiers() <= otherBasic->get_qualifiers() ) || widenSecond ) ) { |
---|
[6f95000] | 196 | result = new BasicType( basicType->get_qualifiers() | otherBasic->get_qualifiers(), newType ); |
---|
[a32b204] | 197 | } // if |
---|
[89e6ffc] | 198 | } else if ( dynamic_cast< EnumInstType * > ( type2 ) || dynamic_cast< ZeroType* >( type2 ) || dynamic_cast< OneType* >( type2 ) ) { |
---|
| 199 | // use signed int in lieu of the enum/zero/one type |
---|
[a436947] | 200 | BasicType::Kind newType = combinedType[ basicType->get_kind() ][ BasicType::SignedInt ]; |
---|
[89e6ffc] | 201 | if ( ( ( newType == basicType->get_kind() && basicType->get_qualifiers() >= type2->get_qualifiers() ) || widenFirst ) && ( ( newType != basicType->get_kind() && basicType->get_qualifiers() <= type2->get_qualifiers() ) || widenSecond ) ) { |
---|
[6f95000] | 202 | result = new BasicType( basicType->get_qualifiers() | type2->get_qualifiers(), newType ); |
---|
[a436947] | 203 | } // if |
---|
[a32b204] | 204 | } // if |
---|
| 205 | } |
---|
| 206 | |
---|
[65cec25] | 207 | template< typename Pointer > |
---|
| 208 | void CommonType::getCommonWithVoidPointer( Pointer* voidPointer, Pointer* otherPointer ) { |
---|
[a2a77af] | 209 | if ( TypeInstType* var = dynamic_cast< TypeInstType* >( otherPointer->get_base() ) ) { |
---|
| 210 | OpenVarSet::const_iterator entry = openVars.find( var->get_name() ); |
---|
| 211 | if ( entry != openVars.end() ) { |
---|
| 212 | AssertionSet need, have; |
---|
| 213 | WidenMode widen( widenFirst, widenSecond ); |
---|
| 214 | if ( entry != openVars.end() && ! bindVar(var, voidPointer->get_base(), entry->second, env, need, have, openVars, widen, indexer ) ) return; |
---|
| 215 | } |
---|
[2c57025] | 216 | } |
---|
[a2a77af] | 217 | result = voidPointer->clone(); |
---|
[6f95000] | 218 | result->get_qualifiers() |= otherPointer->get_qualifiers(); |
---|
[2c57025] | 219 | } |
---|
| 220 | |
---|
[a32b204] | 221 | void CommonType::visit( PointerType *pointerType ) { |
---|
| 222 | if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) { |
---|
[c93bc28] | 223 | // std::cerr << "commonType: two pointers: " << pointerType << " / " << otherPointer << std::endl; |
---|
[d7dc824] | 224 | if ( widenFirst && dynamic_cast< VoidType* >( otherPointer->get_base() ) && ! isFtype(pointerType->get_base()) ) { |
---|
[a2a77af] | 225 | getCommonWithVoidPointer( otherPointer, pointerType ); |
---|
[d7dc824] | 226 | } else if ( widenSecond && dynamic_cast< VoidType* >( pointerType->get_base() ) && ! isFtype(otherPointer->get_base()) ) { |
---|
[a2a77af] | 227 | getCommonWithVoidPointer( pointerType, otherPointer ); |
---|
[a32b204] | 228 | } else if ( ( pointerType->get_base()->get_qualifiers() >= otherPointer->get_base()->get_qualifiers() || widenFirst ) |
---|
| 229 | && ( pointerType->get_base()->get_qualifiers() <= otherPointer->get_base()->get_qualifiers() || widenSecond ) ) { |
---|
[c93bc28] | 230 | // std::cerr << "middle case" << std::endl; |
---|
[a32b204] | 231 | Type::Qualifiers tq1 = pointerType->get_base()->get_qualifiers(), tq2 = otherPointer->get_base()->get_qualifiers(); |
---|
| 232 | pointerType->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
| 233 | otherPointer->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
| 234 | AssertionSet have, need; |
---|
| 235 | OpenVarSet newOpen( openVars ); |
---|
| 236 | if ( unifyExact( pointerType->get_base(), otherPointer->get_base(), env, have, need, newOpen, indexer ) ) { |
---|
[c93bc28] | 237 | // std::cerr << "unifyExact success" << std::endl; |
---|
[a32b204] | 238 | if ( tq1 < tq2 ) { |
---|
| 239 | result = pointerType->clone(); |
---|
| 240 | } else { |
---|
| 241 | result = otherPointer->clone(); |
---|
| 242 | } // if |
---|
[6f95000] | 243 | result->get_qualifiers() = tq1 | tq2; |
---|
[a32b204] | 244 | } else { |
---|
[c93bc28] | 245 | /// std::cerr << "place for ptr-to-type" << std::endl; |
---|
[a32b204] | 246 | } // if |
---|
| 247 | pointerType->get_base()->get_qualifiers() = tq1; |
---|
| 248 | otherPointer->get_base()->get_qualifiers() = tq2; |
---|
| 249 | } // if |
---|
[89e6ffc] | 250 | } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) { |
---|
| 251 | result = pointerType->clone(); |
---|
[6f95000] | 252 | result->get_qualifiers() |= type2->get_qualifiers(); |
---|
[a32b204] | 253 | } // if |
---|
| 254 | } |
---|
| 255 | |
---|
[ecd4923] | 256 | void CommonType::visit( ArrayType * ) {} |
---|
[65cec25] | 257 | |
---|
| 258 | void CommonType::visit( ReferenceType *refType ) { |
---|
| 259 | if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) { |
---|
[c93bc28] | 260 | // std::cerr << "commonType: both references: " << refType << " / " << otherRef << std::endl; |
---|
| 261 | // 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] | 262 | if ( widenFirst && dynamic_cast< VoidType* >( otherRef->get_base() ) && ! isFtype(refType->get_base()) ) { |
---|
| 263 | getCommonWithVoidPointer( otherRef, refType ); |
---|
| 264 | } else if ( widenSecond && dynamic_cast< VoidType* >( refType->get_base() ) && ! isFtype(otherRef->get_base()) ) { |
---|
| 265 | getCommonWithVoidPointer( refType, otherRef ); |
---|
| 266 | } else if ( ( refType->get_base()->get_qualifiers() >= otherRef->get_base()->get_qualifiers() || widenFirst ) |
---|
| 267 | && ( refType->get_base()->get_qualifiers() <= otherRef->get_base()->get_qualifiers() || widenSecond ) ) { |
---|
[c93bc28] | 268 | // std::cerr << "middle case" << std::endl; |
---|
[65cec25] | 269 | Type::Qualifiers tq1 = refType->get_base()->get_qualifiers(), tq2 = otherRef->get_base()->get_qualifiers(); |
---|
| 270 | refType->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
| 271 | otherRef->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
| 272 | AssertionSet have, need; |
---|
| 273 | OpenVarSet newOpen( openVars ); |
---|
| 274 | if ( unifyExact( refType->get_base(), otherRef->get_base(), env, have, need, newOpen, indexer ) ) { |
---|
| 275 | if ( tq1 < tq2 ) { |
---|
| 276 | result = refType->clone(); |
---|
| 277 | } else { |
---|
| 278 | result = otherRef->clone(); |
---|
| 279 | } // if |
---|
| 280 | result->get_qualifiers() = tq1 | tq2; |
---|
| 281 | } else { |
---|
[c93bc28] | 282 | /// std::cerr << "place for ptr-to-type" << std::endl; |
---|
[65cec25] | 283 | } // if |
---|
| 284 | refType->get_base()->get_qualifiers() = tq1; |
---|
| 285 | otherRef->get_base()->get_qualifiers() = tq2; |
---|
| 286 | } // if |
---|
| 287 | } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) { |
---|
| 288 | result = refType->clone(); |
---|
| 289 | result->get_qualifiers() |= type2->get_qualifiers(); |
---|
| 290 | } // if |
---|
| 291 | } |
---|
| 292 | |
---|
[ecd4923] | 293 | void CommonType::visit( FunctionType * ) {} |
---|
| 294 | void CommonType::visit( StructInstType * ) {} |
---|
| 295 | void CommonType::visit( UnionInstType * ) {} |
---|
[a32b204] | 296 | |
---|
[a436947] | 297 | void CommonType::visit( EnumInstType *enumInstType ) { |
---|
[89e6ffc] | 298 | if ( dynamic_cast< BasicType * >( type2 ) || dynamic_cast< ZeroType* >( type2 ) || dynamic_cast< OneType* >( type2 ) ) { |
---|
[a436947] | 299 | // reuse BasicType, EnumInstType code by swapping type2 with enumInstType |
---|
[65cec25] | 300 | ValueGuard< Type * > temp( type2 ); |
---|
[a436947] | 301 | type2 = enumInstType; |
---|
[65cec25] | 302 | temp.old->accept( *this ); |
---|
[a436947] | 303 | } // if |
---|
[a32b204] | 304 | } |
---|
| 305 | |
---|
[ecd4923] | 306 | void CommonType::visit( TraitInstType * ) { |
---|
[a32b204] | 307 | } |
---|
| 308 | |
---|
| 309 | void CommonType::visit( TypeInstType *inst ) { |
---|
| 310 | if ( widenFirst ) { |
---|
| 311 | NamedTypeDecl *nt = indexer.lookupType( inst->get_name() ); |
---|
| 312 | if ( nt ) { |
---|
[e3e16bc] | 313 | TypeDecl *type = strict_dynamic_cast< TypeDecl* >( nt ); |
---|
[a32b204] | 314 | if ( type->get_base() ) { |
---|
| 315 | Type::Qualifiers tq1 = inst->get_qualifiers(), tq2 = type2->get_qualifiers(); |
---|
| 316 | AssertionSet have, need; |
---|
| 317 | OpenVarSet newOpen( openVars ); |
---|
| 318 | type2->get_qualifiers() = Type::Qualifiers(); |
---|
| 319 | type->get_base()->get_qualifiers() = tq1; |
---|
| 320 | if ( unifyExact( type->get_base(), type2, env, have, need, newOpen, indexer ) ) { |
---|
| 321 | result = type2->clone(); |
---|
[6f95000] | 322 | result->get_qualifiers() = tq1 | tq2; |
---|
[a32b204] | 323 | } // if |
---|
| 324 | type2->get_qualifiers() = tq2; |
---|
| 325 | type->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
| 326 | } // if |
---|
| 327 | } // if |
---|
| 328 | } // if |
---|
| 329 | } |
---|
| 330 | |
---|
[ecd4923] | 331 | void CommonType::visit( TupleType * ) {} |
---|
| 332 | void CommonType::visit( VarArgsType * ) {} |
---|
[89e6ffc] | 333 | |
---|
| 334 | void CommonType::visit( ZeroType *zeroType ) { |
---|
| 335 | if ( widenFirst ) { |
---|
| 336 | if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< PointerType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) { |
---|
| 337 | if ( widenSecond || zeroType->get_qualifiers() <= type2->get_qualifiers() ) { |
---|
| 338 | result = type2->clone(); |
---|
[6f95000] | 339 | result->get_qualifiers() |= zeroType->get_qualifiers(); |
---|
[89e6ffc] | 340 | } |
---|
[4cb935e] | 341 | } else if ( widenSecond && dynamic_cast< OneType* >( type2 ) ) { |
---|
| 342 | result = new BasicType( zeroType->get_qualifiers(), BasicType::SignedInt ); |
---|
[6f95000] | 343 | result->get_qualifiers() |= type2->get_qualifiers(); |
---|
[89e6ffc] | 344 | } |
---|
| 345 | } |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | void CommonType::visit( OneType *oneType ) { |
---|
| 349 | if ( widenFirst ) { |
---|
| 350 | if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) { |
---|
| 351 | if ( widenSecond || oneType->get_qualifiers() <= type2->get_qualifiers() ) { |
---|
| 352 | result = type2->clone(); |
---|
[6f95000] | 353 | result->get_qualifiers() |= oneType->get_qualifiers(); |
---|
[89e6ffc] | 354 | } |
---|
[4cb935e] | 355 | } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) { |
---|
| 356 | result = new BasicType( oneType->get_qualifiers(), BasicType::SignedInt ); |
---|
[6f95000] | 357 | result->get_qualifiers() |= type2->get_qualifiers(); |
---|
[89e6ffc] | 358 | } |
---|
| 359 | } |
---|
| 360 | } |
---|
[51b7345] | 361 | } // namespace ResolvExpr |
---|
[a32b204] | 362 | |
---|
| 363 | // Local Variables: // |
---|
| 364 | // tab-width: 4 // |
---|
| 365 | // mode: c++ // |
---|
| 366 | // compile-command: "make install" // |
---|
| 367 | // End: // |
---|