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