| 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 : Wed Mar 2 17:35:34 2016
|
|---|
| 13 | // Update Count : 3
|
|---|
| 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 | template< typename RefType > void handleRefType( RefType *inst, Type *other );
|
|---|
| 45 |
|
|---|
| 46 | Type *result;
|
|---|
| 47 | Type *type2; // inherited
|
|---|
| 48 | bool widenFirst, widenSecond;
|
|---|
| 49 | const SymTab::Indexer &indexer;
|
|---|
| 50 | TypeEnvironment &env;
|
|---|
| 51 | const OpenVarSet &openVars;
|
|---|
| 52 | };
|
|---|
| 53 |
|
|---|
| 54 | Type *commonType( Type *type1, Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars ) {
|
|---|
| 55 | CommonType visitor( type2, widenFirst, widenSecond, indexer, env, openVars );
|
|---|
| 56 | type1->accept( visitor );
|
|---|
| 57 | Type *result = visitor.get_result();
|
|---|
| 58 | if ( ! result ) {
|
|---|
| 59 | // this appears to be handling for opaque type declarations
|
|---|
| 60 | if ( widenSecond ) {
|
|---|
| 61 | if ( TypeInstType *inst = dynamic_cast< TypeInstType* >( type2 ) ) {
|
|---|
| 62 | if ( NamedTypeDecl *nt = indexer.lookupType( inst->get_name() ) ) {
|
|---|
| 63 | TypeDecl *type = safe_dynamic_cast< TypeDecl* >( nt );
|
|---|
| 64 | if ( type->get_base() ) {
|
|---|
| 65 | Type::Qualifiers tq1 = type1->get_qualifiers(), tq2 = type2->get_qualifiers();
|
|---|
| 66 | AssertionSet have, need;
|
|---|
| 67 | OpenVarSet newOpen( openVars );
|
|---|
| 68 | type1->get_qualifiers() = Type::Qualifiers();
|
|---|
| 69 | type->get_base()->get_qualifiers() = tq1;
|
|---|
| 70 | if ( unifyExact( type1, type->get_base(), env, have, need, newOpen, indexer ) ) {
|
|---|
| 71 | result = type1->clone();
|
|---|
| 72 | result->get_qualifiers() = tq1 + tq2;
|
|---|
| 73 | } // if
|
|---|
| 74 | type1->get_qualifiers() = tq1;
|
|---|
| 75 | type->get_base()->get_qualifiers() = Type::Qualifiers();
|
|---|
| 76 | } // if
|
|---|
| 77 | } // if
|
|---|
| 78 | } // if
|
|---|
| 79 | } // if
|
|---|
| 80 | } // if
|
|---|
| 81 | #ifdef DEBUG
|
|---|
| 82 | std::cout << "============= commonType" << std::endl << "type1 is ";
|
|---|
| 83 | type1->print( std::cout );
|
|---|
| 84 | std::cout << " type2 is ";
|
|---|
| 85 | type2->print( std::cout );
|
|---|
| 86 | if ( result ) {
|
|---|
| 87 | std::cout << " common type is ";
|
|---|
| 88 | result->print( std::cout );
|
|---|
| 89 | } else {
|
|---|
| 90 | std::cout << " no common type";
|
|---|
| 91 | } // if
|
|---|
| 92 | std::cout << std::endl;
|
|---|
| 93 | #endif
|
|---|
| 94 | return result;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | static const BasicType::Kind combinedType[ BasicType::NUMBER_OF_BASIC_TYPES ][ BasicType::NUMBER_OF_BASIC_TYPES ] =
|
|---|
| 98 | {
|
|---|
| 99 | /* Bool Char SignedChar UnsignedChar ShortSignedInt ShortUnsignedInt SignedInt UnsignedInt LongSignedInt LongUnsignedInt LongLongSignedInt LongLongUnsignedInt Float Double LongDouble FloatComplex DoubleComplex LongDoubleComplex FloatImaginary DoubleImaginary LongDoubleImaginary */
|
|---|
| 100 | /* 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 },
|
|---|
| 101 | /* 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 },
|
|---|
| 102 | /* 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 },
|
|---|
| 103 | /* 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 },
|
|---|
| 104 | /* 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 },
|
|---|
| 105 | /* 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 },
|
|---|
| 106 | /* 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 },
|
|---|
| 107 | /* 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 },
|
|---|
| 108 | /* 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 },
|
|---|
| 109 | /* 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 },
|
|---|
| 110 | /* 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 },
|
|---|
| 111 | /* 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 },
|
|---|
| 112 | /* 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 },
|
|---|
| 113 | /* 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 },
|
|---|
| 114 | /* 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 },
|
|---|
| 115 | /* 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 },
|
|---|
| 116 | /* 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 },
|
|---|
| 117 | /* 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 },
|
|---|
| 118 | /* 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 },
|
|---|
| 119 | /* 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 },
|
|---|
| 120 | /* 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 }
|
|---|
| 121 | };
|
|---|
| 122 |
|
|---|
| 123 | CommonType::CommonType( Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars )
|
|---|
| 124 | : result( 0 ), type2( type2 ), widenFirst( widenFirst ), widenSecond( widenSecond ), indexer( indexer ), env( env ), openVars( openVars ) {
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | void CommonType::visit( VoidType *voidType ) {
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | void CommonType::visit( BasicType *basicType ) {
|
|---|
| 131 | if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) {
|
|---|
| 132 | BasicType::Kind newType = combinedType[ basicType->get_kind() ][ otherBasic->get_kind() ];
|
|---|
| 133 | if ( ( ( newType == basicType->get_kind() && basicType->get_qualifiers() >= otherBasic->get_qualifiers() ) || widenFirst ) && ( ( newType == otherBasic->get_kind() && basicType->get_qualifiers() <= otherBasic->get_qualifiers() ) || widenSecond ) ) {
|
|---|
| 134 | result = new BasicType( basicType->get_qualifiers() + otherBasic->get_qualifiers(), newType );
|
|---|
| 135 | } // if
|
|---|
| 136 | } else if ( dynamic_cast< EnumInstType * > ( type2 ) || dynamic_cast< ZeroType* >( type2 ) || dynamic_cast< OneType* >( type2 ) ) {
|
|---|
| 137 | // use signed int in lieu of the enum/zero/one type
|
|---|
| 138 | BasicType::Kind newType = combinedType[ basicType->get_kind() ][ BasicType::SignedInt ];
|
|---|
| 139 | if ( ( ( newType == basicType->get_kind() && basicType->get_qualifiers() >= type2->get_qualifiers() ) || widenFirst ) && ( ( newType != basicType->get_kind() && basicType->get_qualifiers() <= type2->get_qualifiers() ) || widenSecond ) ) {
|
|---|
| 140 | result = new BasicType( basicType->get_qualifiers() + type2->get_qualifiers(), newType );
|
|---|
| 141 | } // if
|
|---|
| 142 | } // if
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /// true if a common type for t must be a complete type
|
|---|
| 146 | bool requiredComplete( Type * t ) {
|
|---|
| 147 | if ( TypeInstType * inst = dynamic_cast< TypeInstType * > ( t ) ) {
|
|---|
| 148 | return inst->get_baseType()->isComplete();
|
|---|
| 149 | }
|
|---|
| 150 | return false;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | void CommonType::visit( PointerType *pointerType ) {
|
|---|
| 154 | if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) {
|
|---|
| 155 | // Note: relationship between formal and actual types is antisymmetric
|
|---|
| 156 | // void free(void *);
|
|---|
| 157 | // forall(otype T) void foo(T *);
|
|---|
| 158 | //
|
|---|
| 159 | // should be able to pass T* to free, but should not be able to pass a void* to foo.
|
|---|
| 160 | // because of this, the requiredComplete check occurs only on the first, since it corresponds
|
|---|
| 161 | // to the formal parameter type (though this may be incorrect in some cases, in which case
|
|---|
| 162 | // perhaps the widen mode needs to incorporate another bit for whether this is a formal or actual context)
|
|---|
| 163 | if ( widenFirst && dynamic_cast< VoidType* >( otherPointer->get_base() ) && ! isFtype(pointerType->get_base(), indexer) && ! requiredComplete( pointerType->get_base() ) ) {
|
|---|
| 164 | result = otherPointer->clone();
|
|---|
| 165 | result->get_qualifiers() += pointerType->get_qualifiers();
|
|---|
| 166 | } else if ( widenSecond && dynamic_cast< VoidType* >( pointerType->get_base() ) && ! isFtype(otherPointer->get_base(), indexer) ) {
|
|---|
| 167 | result = pointerType->clone();
|
|---|
| 168 | result->get_qualifiers() += otherPointer->get_qualifiers();
|
|---|
| 169 | } else if ( ( pointerType->get_base()->get_qualifiers() >= otherPointer->get_base()->get_qualifiers() || widenFirst )
|
|---|
| 170 | && ( pointerType->get_base()->get_qualifiers() <= otherPointer->get_base()->get_qualifiers() || widenSecond ) ) {
|
|---|
| 171 | Type::Qualifiers tq1 = pointerType->get_base()->get_qualifiers(), tq2 = otherPointer->get_base()->get_qualifiers();
|
|---|
| 172 | pointerType->get_base()->get_qualifiers() = Type::Qualifiers();
|
|---|
| 173 | otherPointer->get_base()->get_qualifiers() = Type::Qualifiers();
|
|---|
| 174 | AssertionSet have, need;
|
|---|
| 175 | OpenVarSet newOpen( openVars );
|
|---|
| 176 | if ( unifyExact( pointerType->get_base(), otherPointer->get_base(), env, have, need, newOpen, indexer ) ) {
|
|---|
| 177 | if ( tq1 < tq2 ) {
|
|---|
| 178 | result = pointerType->clone();
|
|---|
| 179 | } else {
|
|---|
| 180 | result = otherPointer->clone();
|
|---|
| 181 | } // if
|
|---|
| 182 | result->get_qualifiers() = tq1 + tq2;
|
|---|
| 183 | } else {
|
|---|
| 184 | /// std::cout << "place for ptr-to-type" << std::endl;
|
|---|
| 185 | } // if
|
|---|
| 186 | pointerType->get_base()->get_qualifiers() = tq1;
|
|---|
| 187 | otherPointer->get_base()->get_qualifiers() = tq2;
|
|---|
| 188 | } // if
|
|---|
| 189 | } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) {
|
|---|
| 190 | result = pointerType->clone();
|
|---|
| 191 | result->get_qualifiers() += type2->get_qualifiers();
|
|---|
| 192 | } // if
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | void CommonType::visit( ArrayType *arrayType ) {
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | void CommonType::visit( FunctionType *functionType ) {
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | template< typename RefType > void CommonType::handleRefType( RefType *inst, Type *other ) {
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | void CommonType::visit( StructInstType *aggregateUseType ) {
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | void CommonType::visit( UnionInstType *aggregateUseType ) {
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | void CommonType::visit( EnumInstType *enumInstType ) {
|
|---|
| 211 | if ( dynamic_cast< BasicType * >( type2 ) || dynamic_cast< ZeroType* >( type2 ) || dynamic_cast< OneType* >( type2 ) ) {
|
|---|
| 212 | // reuse BasicType, EnumInstType code by swapping type2 with enumInstType
|
|---|
| 213 | Type * temp = type2;
|
|---|
| 214 | type2 = enumInstType;
|
|---|
| 215 | temp->accept( *this );
|
|---|
| 216 | type2 = temp;
|
|---|
| 217 | } // if
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | void CommonType::visit( TraitInstType *aggregateUseType ) {
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | void CommonType::visit( TypeInstType *inst ) {
|
|---|
| 224 | if ( widenFirst ) {
|
|---|
| 225 | NamedTypeDecl *nt = indexer.lookupType( inst->get_name() );
|
|---|
| 226 | if ( nt ) {
|
|---|
| 227 | TypeDecl *type = safe_dynamic_cast< TypeDecl* >( nt );
|
|---|
| 228 | if ( type->get_base() ) {
|
|---|
| 229 | Type::Qualifiers tq1 = inst->get_qualifiers(), tq2 = type2->get_qualifiers();
|
|---|
| 230 | AssertionSet have, need;
|
|---|
| 231 | OpenVarSet newOpen( openVars );
|
|---|
| 232 | type2->get_qualifiers() = Type::Qualifiers();
|
|---|
| 233 | type->get_base()->get_qualifiers() = tq1;
|
|---|
| 234 | if ( unifyExact( type->get_base(), type2, env, have, need, newOpen, indexer ) ) {
|
|---|
| 235 | result = type2->clone();
|
|---|
| 236 | result->get_qualifiers() = tq1 + tq2;
|
|---|
| 237 | } // if
|
|---|
| 238 | type2->get_qualifiers() = tq2;
|
|---|
| 239 | type->get_base()->get_qualifiers() = Type::Qualifiers();
|
|---|
| 240 | } // if
|
|---|
| 241 | } // if
|
|---|
| 242 | } // if
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | void CommonType::visit( TupleType *tupleType ) {
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | void CommonType::visit( VarArgsType *varArgsType ) {
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | void CommonType::visit( ZeroType *zeroType ) {
|
|---|
| 252 | if ( widenFirst ) {
|
|---|
| 253 | if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< PointerType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) {
|
|---|
| 254 | if ( widenSecond || zeroType->get_qualifiers() <= type2->get_qualifiers() ) {
|
|---|
| 255 | result = type2->clone();
|
|---|
| 256 | result->get_qualifiers() += zeroType->get_qualifiers();
|
|---|
| 257 | }
|
|---|
| 258 | }
|
|---|
| 259 | }
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | void CommonType::visit( OneType *oneType ) {
|
|---|
| 263 | if ( widenFirst ) {
|
|---|
| 264 | if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) {
|
|---|
| 265 | if ( widenSecond || oneType->get_qualifiers() <= type2->get_qualifiers() ) {
|
|---|
| 266 | result = type2->clone();
|
|---|
| 267 | result->get_qualifiers() += oneType->get_qualifiers();
|
|---|
| 268 | }
|
|---|
| 269 | }
|
|---|
| 270 | }
|
|---|
| 271 | }
|
|---|
| 272 | } // namespace ResolvExpr
|
|---|
| 273 |
|
|---|
| 274 | // Local Variables: //
|
|---|
| 275 | // tab-width: 4 //
|
|---|
| 276 | // mode: c++ //
|
|---|
| 277 | // compile-command: "make install" //
|
|---|
| 278 | // End: //
|
|---|