[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 |
---|
[d286cf68] | 26 | #include "Unify.h" // for unifyExact, WidenMode |
---|
[ea6332d] | 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 | |
---|
[4ee3b0c1] | 178 | static const BasicType::Kind combinedType[][ BasicType::NUMBER_OF_BASIC_TYPES ] = |
---|
[a32b204] | 179 | { |
---|
[4ee3b0c1] | 180 | /* Bool Char SignedChar UnsignedChar ShortSignedInt ShortUnsignedInt SignedInt UnsignedInt LongSignedInt LongUnsignedInt LongLongSignedInt LongLongUnsignedInt Float Double LongDouble FloatComplex DoubleComplex LongDoubleComplex FloatImaginary DoubleImaginary LongDoubleImaginary SignedInt128 UnsignedInt128 Float80 Float128 */ |
---|
| 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, BasicType::Float80, BasicType::Float128 }, |
---|
| 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, BasicType::Float80, BasicType::Float128 }, |
---|
| 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, BasicType::Float80, BasicType::Float128 }, |
---|
| 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, BasicType::Float80, BasicType::Float128 }, |
---|
| 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, BasicType::Float80, BasicType::Float128 }, |
---|
| 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, BasicType::Float80, BasicType::Float128 }, |
---|
| 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, BasicType::Float80, BasicType::Float128 }, |
---|
| 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, BasicType::Float80, BasicType::Float128 }, |
---|
| 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, BasicType::Float80, BasicType::Float128 }, |
---|
| 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, BasicType::Float80, BasicType::Float128 }, |
---|
| 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, BasicType::Float80, BasicType::Float128 }, |
---|
| 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, BasicType::Float80, BasicType::Float128 }, |
---|
| 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, BasicType::Float80, BasicType::Float128 }, |
---|
| 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, BasicType::Float80, BasicType::Float128 }, |
---|
| 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, BasicType::BasicType::LongDouble, BasicType::Float128 }, |
---|
| 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, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, }, |
---|
| 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, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex }, |
---|
| 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, 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, BasicType::LongDoubleImaginary, BasicType::LongDoubleImaginary, }, |
---|
| 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, BasicType::LongDoubleImaginary, BasicType::LongDoubleImaginary, }, |
---|
| 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, 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, BasicType::Float80, BasicType::Float128, }, |
---|
| 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, BasicType::Float80, BasicType::Float128, }, |
---|
| 204 | /* Float80 */ { BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::LongDouble, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float128 }, |
---|
| 205 | /* Float128 */ { BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128 }, |
---|
[a32b204] | 206 | }; |
---|
[4ee3b0c1] | 207 | static_assert( |
---|
| 208 | sizeof(combinedType)/sizeof(combinedType[0][0]) == BasicType::NUMBER_OF_BASIC_TYPES*BasicType::NUMBER_OF_BASIC_TYPES, |
---|
| 209 | "Each basic type kind should have a corresponding row in the combined type matrix" |
---|
| 210 | ); |
---|
[a32b204] | 211 | |
---|
| 212 | CommonType::CommonType( Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars ) |
---|
| 213 | : result( 0 ), type2( type2 ), widenFirst( widenFirst ), widenSecond( widenSecond ), indexer( indexer ), env( env ), openVars( openVars ) { |
---|
| 214 | } |
---|
| 215 | |
---|
[53452de] | 216 | void CommonType::postvisit( VoidType * ) {} |
---|
[a32b204] | 217 | |
---|
[53452de] | 218 | void CommonType::postvisit( BasicType *basicType ) { |
---|
[a32b204] | 219 | if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) { |
---|
| 220 | BasicType::Kind newType = combinedType[ basicType->get_kind() ][ otherBasic->get_kind() ]; |
---|
| 221 | if ( ( ( newType == basicType->get_kind() && basicType->get_qualifiers() >= otherBasic->get_qualifiers() ) || widenFirst ) && ( ( newType == otherBasic->get_kind() && basicType->get_qualifiers() <= otherBasic->get_qualifiers() ) || widenSecond ) ) { |
---|
[6f95000] | 222 | result = new BasicType( basicType->get_qualifiers() | otherBasic->get_qualifiers(), newType ); |
---|
[a32b204] | 223 | } // if |
---|
[89e6ffc] | 224 | } else if ( dynamic_cast< EnumInstType * > ( type2 ) || dynamic_cast< ZeroType* >( type2 ) || dynamic_cast< OneType* >( type2 ) ) { |
---|
| 225 | // use signed int in lieu of the enum/zero/one type |
---|
[a436947] | 226 | BasicType::Kind newType = combinedType[ basicType->get_kind() ][ BasicType::SignedInt ]; |
---|
[89e6ffc] | 227 | if ( ( ( newType == basicType->get_kind() && basicType->get_qualifiers() >= type2->get_qualifiers() ) || widenFirst ) && ( ( newType != basicType->get_kind() && basicType->get_qualifiers() <= type2->get_qualifiers() ) || widenSecond ) ) { |
---|
[6f95000] | 228 | result = new BasicType( basicType->get_qualifiers() | type2->get_qualifiers(), newType ); |
---|
[a436947] | 229 | } // if |
---|
[a32b204] | 230 | } // if |
---|
| 231 | } |
---|
| 232 | |
---|
[65cec25] | 233 | template< typename Pointer > |
---|
| 234 | void CommonType::getCommonWithVoidPointer( Pointer* voidPointer, Pointer* otherPointer ) { |
---|
[a2a77af] | 235 | if ( TypeInstType* var = dynamic_cast< TypeInstType* >( otherPointer->get_base() ) ) { |
---|
| 236 | OpenVarSet::const_iterator entry = openVars.find( var->get_name() ); |
---|
| 237 | if ( entry != openVars.end() ) { |
---|
| 238 | AssertionSet need, have; |
---|
| 239 | WidenMode widen( widenFirst, widenSecond ); |
---|
[d286cf68] | 240 | if ( entry != openVars.end() && ! env.bindVar(var, voidPointer->get_base(), entry->second, need, have, openVars, widen, indexer ) ) return; |
---|
[a2a77af] | 241 | } |
---|
[2c57025] | 242 | } |
---|
[a2a77af] | 243 | result = voidPointer->clone(); |
---|
[6f95000] | 244 | result->get_qualifiers() |= otherPointer->get_qualifiers(); |
---|
[2c57025] | 245 | } |
---|
| 246 | |
---|
[53452de] | 247 | void CommonType::postvisit( PointerType *pointerType ) { |
---|
[a32b204] | 248 | if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) { |
---|
[c93bc28] | 249 | // std::cerr << "commonType: two pointers: " << pointerType << " / " << otherPointer << std::endl; |
---|
[d7dc824] | 250 | if ( widenFirst && dynamic_cast< VoidType* >( otherPointer->get_base() ) && ! isFtype(pointerType->get_base()) ) { |
---|
[a2a77af] | 251 | getCommonWithVoidPointer( otherPointer, pointerType ); |
---|
[d7dc824] | 252 | } else if ( widenSecond && dynamic_cast< VoidType* >( pointerType->get_base() ) && ! isFtype(otherPointer->get_base()) ) { |
---|
[a2a77af] | 253 | getCommonWithVoidPointer( pointerType, otherPointer ); |
---|
[a32b204] | 254 | } else if ( ( pointerType->get_base()->get_qualifiers() >= otherPointer->get_base()->get_qualifiers() || widenFirst ) |
---|
| 255 | && ( pointerType->get_base()->get_qualifiers() <= otherPointer->get_base()->get_qualifiers() || widenSecond ) ) { |
---|
[c93bc28] | 256 | // std::cerr << "middle case" << std::endl; |
---|
[a32b204] | 257 | Type::Qualifiers tq1 = pointerType->get_base()->get_qualifiers(), tq2 = otherPointer->get_base()->get_qualifiers(); |
---|
| 258 | pointerType->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
| 259 | otherPointer->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
| 260 | AssertionSet have, need; |
---|
| 261 | OpenVarSet newOpen( openVars ); |
---|
| 262 | if ( unifyExact( pointerType->get_base(), otherPointer->get_base(), env, have, need, newOpen, indexer ) ) { |
---|
[c93bc28] | 263 | // std::cerr << "unifyExact success" << std::endl; |
---|
[a32b204] | 264 | if ( tq1 < tq2 ) { |
---|
| 265 | result = pointerType->clone(); |
---|
| 266 | } else { |
---|
| 267 | result = otherPointer->clone(); |
---|
| 268 | } // if |
---|
[825170d] | 269 | strict_dynamic_cast<PointerType*>(result)->base->get_qualifiers() = tq1 | tq2; |
---|
[a32b204] | 270 | } else { |
---|
[c93bc28] | 271 | /// std::cerr << "place for ptr-to-type" << std::endl; |
---|
[a32b204] | 272 | } // if |
---|
| 273 | pointerType->get_base()->get_qualifiers() = tq1; |
---|
| 274 | otherPointer->get_base()->get_qualifiers() = tq2; |
---|
| 275 | } // if |
---|
[89e6ffc] | 276 | } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) { |
---|
| 277 | result = pointerType->clone(); |
---|
[6f95000] | 278 | result->get_qualifiers() |= type2->get_qualifiers(); |
---|
[a32b204] | 279 | } // if |
---|
| 280 | } |
---|
| 281 | |
---|
[53452de] | 282 | void CommonType::postvisit( ArrayType * ) {} |
---|
[65cec25] | 283 | |
---|
[53452de] | 284 | void CommonType::postvisit( ReferenceType *refType ) { |
---|
[65cec25] | 285 | if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) { |
---|
[c93bc28] | 286 | // std::cerr << "commonType: both references: " << refType << " / " << otherRef << std::endl; |
---|
| 287 | // 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] | 288 | if ( widenFirst && dynamic_cast< VoidType* >( otherRef->get_base() ) && ! isFtype(refType->get_base()) ) { |
---|
| 289 | getCommonWithVoidPointer( otherRef, refType ); |
---|
| 290 | } else if ( widenSecond && dynamic_cast< VoidType* >( refType->get_base() ) && ! isFtype(otherRef->get_base()) ) { |
---|
| 291 | getCommonWithVoidPointer( refType, otherRef ); |
---|
| 292 | } else if ( ( refType->get_base()->get_qualifiers() >= otherRef->get_base()->get_qualifiers() || widenFirst ) |
---|
| 293 | && ( refType->get_base()->get_qualifiers() <= otherRef->get_base()->get_qualifiers() || widenSecond ) ) { |
---|
[c93bc28] | 294 | // std::cerr << "middle case" << std::endl; |
---|
[65cec25] | 295 | Type::Qualifiers tq1 = refType->get_base()->get_qualifiers(), tq2 = otherRef->get_base()->get_qualifiers(); |
---|
| 296 | refType->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
| 297 | otherRef->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
| 298 | AssertionSet have, need; |
---|
| 299 | OpenVarSet newOpen( openVars ); |
---|
| 300 | if ( unifyExact( refType->get_base(), otherRef->get_base(), env, have, need, newOpen, indexer ) ) { |
---|
| 301 | if ( tq1 < tq2 ) { |
---|
| 302 | result = refType->clone(); |
---|
| 303 | } else { |
---|
| 304 | result = otherRef->clone(); |
---|
| 305 | } // if |
---|
[825170d] | 306 | strict_dynamic_cast<ReferenceType*>(result)->base->get_qualifiers() = tq1 | tq2; |
---|
[65cec25] | 307 | } else { |
---|
[c93bc28] | 308 | /// std::cerr << "place for ptr-to-type" << std::endl; |
---|
[65cec25] | 309 | } // if |
---|
| 310 | refType->get_base()->get_qualifiers() = tq1; |
---|
| 311 | otherRef->get_base()->get_qualifiers() = tq2; |
---|
| 312 | } // if |
---|
| 313 | } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) { |
---|
| 314 | result = refType->clone(); |
---|
| 315 | result->get_qualifiers() |= type2->get_qualifiers(); |
---|
| 316 | } // if |
---|
| 317 | } |
---|
| 318 | |
---|
[53452de] | 319 | void CommonType::postvisit( FunctionType * ) {} |
---|
| 320 | void CommonType::postvisit( StructInstType * ) {} |
---|
| 321 | void CommonType::postvisit( UnionInstType * ) {} |
---|
[a32b204] | 322 | |
---|
[53452de] | 323 | void CommonType::postvisit( EnumInstType *enumInstType ) { |
---|
[89e6ffc] | 324 | if ( dynamic_cast< BasicType * >( type2 ) || dynamic_cast< ZeroType* >( type2 ) || dynamic_cast< OneType* >( type2 ) ) { |
---|
[a436947] | 325 | // reuse BasicType, EnumInstType code by swapping type2 with enumInstType |
---|
[53452de] | 326 | result = commonType( type2, enumInstType, widenSecond, widenFirst, indexer, env, openVars ); |
---|
[a436947] | 327 | } // if |
---|
[a32b204] | 328 | } |
---|
| 329 | |
---|
[53452de] | 330 | void CommonType::postvisit( TraitInstType * ) { |
---|
[a32b204] | 331 | } |
---|
| 332 | |
---|
[53452de] | 333 | void CommonType::postvisit( TypeInstType *inst ) { |
---|
[a32b204] | 334 | if ( widenFirst ) { |
---|
| 335 | NamedTypeDecl *nt = indexer.lookupType( inst->get_name() ); |
---|
| 336 | if ( nt ) { |
---|
[e3e16bc] | 337 | TypeDecl *type = strict_dynamic_cast< TypeDecl* >( nt ); |
---|
[a32b204] | 338 | if ( type->get_base() ) { |
---|
| 339 | Type::Qualifiers tq1 = inst->get_qualifiers(), tq2 = type2->get_qualifiers(); |
---|
| 340 | AssertionSet have, need; |
---|
| 341 | OpenVarSet newOpen( openVars ); |
---|
| 342 | type2->get_qualifiers() = Type::Qualifiers(); |
---|
| 343 | type->get_base()->get_qualifiers() = tq1; |
---|
| 344 | if ( unifyExact( type->get_base(), type2, env, have, need, newOpen, indexer ) ) { |
---|
| 345 | result = type2->clone(); |
---|
[6f95000] | 346 | result->get_qualifiers() = tq1 | tq2; |
---|
[a32b204] | 347 | } // if |
---|
| 348 | type2->get_qualifiers() = tq2; |
---|
| 349 | type->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
| 350 | } // if |
---|
| 351 | } // if |
---|
| 352 | } // if |
---|
| 353 | } |
---|
| 354 | |
---|
[53452de] | 355 | void CommonType::postvisit( TupleType * ) {} |
---|
| 356 | void CommonType::postvisit( VarArgsType * ) {} |
---|
[89e6ffc] | 357 | |
---|
[53452de] | 358 | void CommonType::postvisit( ZeroType *zeroType ) { |
---|
[89e6ffc] | 359 | if ( widenFirst ) { |
---|
| 360 | if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< PointerType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) { |
---|
| 361 | if ( widenSecond || zeroType->get_qualifiers() <= type2->get_qualifiers() ) { |
---|
| 362 | result = type2->clone(); |
---|
[6f95000] | 363 | result->get_qualifiers() |= zeroType->get_qualifiers(); |
---|
[89e6ffc] | 364 | } |
---|
[4cb935e] | 365 | } else if ( widenSecond && dynamic_cast< OneType* >( type2 ) ) { |
---|
| 366 | result = new BasicType( zeroType->get_qualifiers(), BasicType::SignedInt ); |
---|
[6f95000] | 367 | result->get_qualifiers() |= type2->get_qualifiers(); |
---|
[89e6ffc] | 368 | } |
---|
| 369 | } |
---|
| 370 | } |
---|
| 371 | |
---|
[53452de] | 372 | void CommonType::postvisit( OneType *oneType ) { |
---|
[89e6ffc] | 373 | if ( widenFirst ) { |
---|
| 374 | if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) { |
---|
| 375 | if ( widenSecond || oneType->get_qualifiers() <= type2->get_qualifiers() ) { |
---|
| 376 | result = type2->clone(); |
---|
[6f95000] | 377 | result->get_qualifiers() |= oneType->get_qualifiers(); |
---|
[89e6ffc] | 378 | } |
---|
[4cb935e] | 379 | } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) { |
---|
| 380 | result = new BasicType( oneType->get_qualifiers(), BasicType::SignedInt ); |
---|
[6f95000] | 381 | result->get_qualifiers() |= type2->get_qualifiers(); |
---|
[89e6ffc] | 382 | } |
---|
| 383 | } |
---|
| 384 | } |
---|
[51b7345] | 385 | } // namespace ResolvExpr |
---|
[a32b204] | 386 | |
---|
| 387 | // Local Variables: // |
---|
| 388 | // tab-width: 4 // |
---|
| 389 | // mode: c++ // |
---|
| 390 | // compile-command: "make install" // |
---|
| 391 | // End: // |
---|