// // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // CommonType.cc -- // // Author : Richard C. Bilson // Created On : Sun May 17 06:59:27 2015 // Last Modified By : Peter A. Buhr // Last Modified On : Thu Mar 16 16:24:31 2017 // Update Count : 7 // #include "typeops.h" #include "SynTree/Type.h" #include "Unify.h" /// #define DEBUG namespace ResolvExpr { class CommonType : public Visitor { public: CommonType( Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars ); Type *get_result() const { return result; } private: virtual void visit( VoidType *voidType ); virtual void visit( BasicType *basicType ); virtual void visit( PointerType *pointerType ); virtual void visit( ArrayType *arrayType ); virtual void visit( ReferenceType *refType ); virtual void visit( FunctionType *functionType ); virtual void visit( StructInstType *aggregateUseType ); virtual void visit( UnionInstType *aggregateUseType ); virtual void visit( EnumInstType *aggregateUseType ); virtual void visit( TraitInstType *aggregateUseType ); virtual void visit( TypeInstType *aggregateUseType ); virtual void visit( TupleType *tupleType ); virtual void visit( VarArgsType *varArgsType ); virtual void visit( ZeroType *zeroType ); virtual void visit( OneType *oneType ); template< typename Pointer > void getCommonWithVoidPointer( Pointer* voidPointer, Pointer* otherPointer ); template< typename RefType > void handleRefType( RefType *inst, Type *other ); Type *result; Type *type2; // inherited bool widenFirst, widenSecond; const SymTab::Indexer &indexer; TypeEnvironment &env; const OpenVarSet &openVars; }; Type * handleReference( ReferenceType * refType, Type * other, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment & env, const OpenVarSet &openVars ) { Type * result = nullptr, * common = nullptr; AssertionSet have, need; OpenVarSet newOpen( openVars ); // need unify to bind type variables if ( unify( refType->get_base(), other, env, have, need, newOpen, indexer, common ) ) { // std::cerr << "unify success" << std::endl; if ( widenSecond ) { if ( widenFirst || other->get_qualifiers() <= refType->get_qualifiers() ) { result = new ReferenceType( refType->get_qualifiers(), common ); // refType->clone(); result->get_qualifiers() |= other->get_qualifiers(); } } else if ( widenFirst ) { if ( widenSecond || refType->get_qualifiers() <= other->get_qualifiers() ) { result = common; result->get_qualifiers() |= refType->get_qualifiers(); } } } else { // std::cerr << "exact unify failed: " << refType << " " << other << std::endl; } // std::cerr << "common type of reference [" << refType << "] and non-reference [" << other << "] is [" << result << "]" << std::endl; return result; } Type *commonType( Type *type1, Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars ) { CommonType visitor( type2, widenFirst, widenSecond, indexer, env, openVars ); ReferenceType * refType1 = dynamic_cast< ReferenceType * >( type1 ); ReferenceType * refType2 = dynamic_cast< ReferenceType * >( type2 ); if ( (refType1 || refType2) && (! refType1 || ! refType2) ) { // handle the case where exactly one of the types is a reference type specially if ( refType1 ) { return handleReference( refType1, type2, widenFirst, widenSecond, indexer, env, openVars ); } else if ( refType2 ) { return handleReference( refType2, type1, widenSecond, widenFirst, indexer, env, openVars ); } } type1->accept( visitor ); Type *result = visitor.get_result(); if ( ! result ) { // this appears to be handling for opaque type declarations if ( widenSecond ) { if ( TypeInstType *inst = dynamic_cast< TypeInstType* >( type2 ) ) { if ( NamedTypeDecl *nt = indexer.lookupType( inst->get_name() ) ) { TypeDecl *type = safe_dynamic_cast< TypeDecl* >( nt ); if ( type->get_base() ) { Type::Qualifiers tq1 = type1->get_qualifiers(), tq2 = type2->get_qualifiers(); AssertionSet have, need; OpenVarSet newOpen( openVars ); type1->get_qualifiers() = Type::Qualifiers(); type->get_base()->get_qualifiers() = tq1; if ( unifyExact( type1, type->get_base(), env, have, need, newOpen, indexer ) ) { result = type1->clone(); result->get_qualifiers() = tq1 | tq2; } // if type1->get_qualifiers() = tq1; type->get_base()->get_qualifiers() = Type::Qualifiers(); } // if } // if } // if } // if } // if #ifdef DEBUG std::cout << "============= commonType" << std::endl << "type1 is "; type1->print( std::cout ); std::cout << " type2 is "; type2->print( std::cout ); if ( result ) { std::cout << " common type is "; result->print( std::cout ); } else { std::cout << " no common type"; } // if std::cout << std::endl; #endif return result; } static const BasicType::Kind combinedType[ BasicType::NUMBER_OF_BASIC_TYPES ][ BasicType::NUMBER_OF_BASIC_TYPES ] = { /* Bool Char SignedChar UnsignedChar ShortSignedInt ShortUnsignedInt SignedInt UnsignedInt LongSignedInt LongUnsignedInt LongLongSignedInt LongLongUnsignedInt Float Double LongDouble FloatComplex DoubleComplex LongDoubleComplex FloatImaginary DoubleImaginary LongDoubleImaginary */ /* 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 }, /* 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 }, /* 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 }, /* 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 }, /* 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 }, /* 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 }, /* 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 }, /* 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 }, /* 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 }, /* 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 }, /* 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 }, /* 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 }, /* 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 }, /* 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 }, /* 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 }, /* 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 }, /* 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 }, /* 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 }, /* 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 }, /* 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 }, /* 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 } }; CommonType::CommonType( Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars ) : result( 0 ), type2( type2 ), widenFirst( widenFirst ), widenSecond( widenSecond ), indexer( indexer ), env( env ), openVars( openVars ) { } void CommonType::visit( __attribute((unused)) VoidType *voidType ) {} void CommonType::visit( BasicType *basicType ) { if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) { BasicType::Kind newType = combinedType[ basicType->get_kind() ][ otherBasic->get_kind() ]; if ( ( ( newType == basicType->get_kind() && basicType->get_qualifiers() >= otherBasic->get_qualifiers() ) || widenFirst ) && ( ( newType == otherBasic->get_kind() && basicType->get_qualifiers() <= otherBasic->get_qualifiers() ) || widenSecond ) ) { result = new BasicType( basicType->get_qualifiers() | otherBasic->get_qualifiers(), newType ); } // if } else if ( dynamic_cast< EnumInstType * > ( type2 ) || dynamic_cast< ZeroType* >( type2 ) || dynamic_cast< OneType* >( type2 ) ) { // use signed int in lieu of the enum/zero/one type BasicType::Kind newType = combinedType[ basicType->get_kind() ][ BasicType::SignedInt ]; if ( ( ( newType == basicType->get_kind() && basicType->get_qualifiers() >= type2->get_qualifiers() ) || widenFirst ) && ( ( newType != basicType->get_kind() && basicType->get_qualifiers() <= type2->get_qualifiers() ) || widenSecond ) ) { result = new BasicType( basicType->get_qualifiers() | type2->get_qualifiers(), newType ); } // if } // if } template< typename Pointer > void CommonType::getCommonWithVoidPointer( Pointer* voidPointer, Pointer* otherPointer ) { if ( TypeInstType* var = dynamic_cast< TypeInstType* >( otherPointer->get_base() ) ) { OpenVarSet::const_iterator entry = openVars.find( var->get_name() ); if ( entry != openVars.end() ) { AssertionSet need, have; WidenMode widen( widenFirst, widenSecond ); if ( entry != openVars.end() && ! bindVar(var, voidPointer->get_base(), entry->second, env, need, have, openVars, widen, indexer ) ) return; } } result = voidPointer->clone(); result->get_qualifiers() |= otherPointer->get_qualifiers(); } void CommonType::visit( PointerType *pointerType ) { if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) { if ( widenFirst && dynamic_cast< VoidType* >( otherPointer->get_base() ) && ! isFtype(pointerType->get_base()) ) { getCommonWithVoidPointer( otherPointer, pointerType ); } else if ( widenSecond && dynamic_cast< VoidType* >( pointerType->get_base() ) && ! isFtype(otherPointer->get_base()) ) { getCommonWithVoidPointer( pointerType, otherPointer ); } else if ( ( pointerType->get_base()->get_qualifiers() >= otherPointer->get_base()->get_qualifiers() || widenFirst ) && ( pointerType->get_base()->get_qualifiers() <= otherPointer->get_base()->get_qualifiers() || widenSecond ) ) { Type::Qualifiers tq1 = pointerType->get_base()->get_qualifiers(), tq2 = otherPointer->get_base()->get_qualifiers(); pointerType->get_base()->get_qualifiers() = Type::Qualifiers(); otherPointer->get_base()->get_qualifiers() = Type::Qualifiers(); AssertionSet have, need; OpenVarSet newOpen( openVars ); if ( unifyExact( pointerType->get_base(), otherPointer->get_base(), env, have, need, newOpen, indexer ) ) { if ( tq1 < tq2 ) { result = pointerType->clone(); } else { result = otherPointer->clone(); } // if result->get_qualifiers() = tq1 | tq2; } else { /// std::cout << "place for ptr-to-type" << std::endl; } // if pointerType->get_base()->get_qualifiers() = tq1; otherPointer->get_base()->get_qualifiers() = tq2; } // if } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) { result = pointerType->clone(); result->get_qualifiers() |= type2->get_qualifiers(); } // if } void CommonType::visit( __attribute((unused)) ArrayType *arrayType ) {} void CommonType::visit( ReferenceType *refType ) { if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) { if ( widenFirst && dynamic_cast< VoidType* >( otherRef->get_base() ) && ! isFtype(refType->get_base()) ) { getCommonWithVoidPointer( otherRef, refType ); } else if ( widenSecond && dynamic_cast< VoidType* >( refType->get_base() ) && ! isFtype(otherRef->get_base()) ) { getCommonWithVoidPointer( refType, otherRef ); } else if ( ( refType->get_base()->get_qualifiers() >= otherRef->get_base()->get_qualifiers() || widenFirst ) && ( refType->get_base()->get_qualifiers() <= otherRef->get_base()->get_qualifiers() || widenSecond ) ) { Type::Qualifiers tq1 = refType->get_base()->get_qualifiers(), tq2 = otherRef->get_base()->get_qualifiers(); refType->get_base()->get_qualifiers() = Type::Qualifiers(); otherRef->get_base()->get_qualifiers() = Type::Qualifiers(); AssertionSet have, need; OpenVarSet newOpen( openVars ); if ( unifyExact( refType->get_base(), otherRef->get_base(), env, have, need, newOpen, indexer ) ) { if ( tq1 < tq2 ) { result = refType->clone(); } else { result = otherRef->clone(); } // if result->get_qualifiers() = tq1 | tq2; } else { /// std::cout << "place for ptr-to-type" << std::endl; } // if refType->get_base()->get_qualifiers() = tq1; otherRef->get_base()->get_qualifiers() = tq2; } // if } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) { result = refType->clone(); result->get_qualifiers() |= type2->get_qualifiers(); } // if } void CommonType::visit( __attribute((unused)) FunctionType *functionType ) {} void CommonType::visit( __attribute((unused)) StructInstType *aggregateUseType ) {} void CommonType::visit( __attribute((unused)) UnionInstType *aggregateUseType ) {} void CommonType::visit( EnumInstType *enumInstType ) { if ( dynamic_cast< BasicType * >( type2 ) || dynamic_cast< ZeroType* >( type2 ) || dynamic_cast< OneType* >( type2 ) ) { // reuse BasicType, EnumInstType code by swapping type2 with enumInstType ValueGuard< Type * > temp( type2 ); type2 = enumInstType; temp.old->accept( *this ); } // if } void CommonType::visit( __attribute((unused)) TraitInstType *aggregateUseType ) { } void CommonType::visit( TypeInstType *inst ) { if ( widenFirst ) { NamedTypeDecl *nt = indexer.lookupType( inst->get_name() ); if ( nt ) { TypeDecl *type = safe_dynamic_cast< TypeDecl* >( nt ); if ( type->get_base() ) { Type::Qualifiers tq1 = inst->get_qualifiers(), tq2 = type2->get_qualifiers(); AssertionSet have, need; OpenVarSet newOpen( openVars ); type2->get_qualifiers() = Type::Qualifiers(); type->get_base()->get_qualifiers() = tq1; if ( unifyExact( type->get_base(), type2, env, have, need, newOpen, indexer ) ) { result = type2->clone(); result->get_qualifiers() = tq1 | tq2; } // if type2->get_qualifiers() = tq2; type->get_base()->get_qualifiers() = Type::Qualifiers(); } // if } // if } // if } void CommonType::visit( __attribute((unused)) TupleType *tupleType ) {} void CommonType::visit( __attribute((unused)) VarArgsType *varArgsType ) {} void CommonType::visit( ZeroType *zeroType ) { if ( widenFirst ) { if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< PointerType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) { if ( widenSecond || zeroType->get_qualifiers() <= type2->get_qualifiers() ) { result = type2->clone(); result->get_qualifiers() |= zeroType->get_qualifiers(); } } else if ( widenSecond && dynamic_cast< OneType* >( type2 ) ) { result = new BasicType( zeroType->get_qualifiers(), BasicType::SignedInt ); result->get_qualifiers() |= type2->get_qualifiers(); } } } void CommonType::visit( OneType *oneType ) { if ( widenFirst ) { if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) { if ( widenSecond || oneType->get_qualifiers() <= type2->get_qualifiers() ) { result = type2->clone(); result->get_qualifiers() |= oneType->get_qualifiers(); } } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) { result = new BasicType( oneType->get_qualifiers(), BasicType::SignedInt ); result->get_qualifiers() |= type2->get_qualifiers(); } } } } // namespace ResolvExpr // Local Variables: // // tab-width: 4 // // mode: c++ // // compile-command: "make install" // // End: //