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