[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 | //
|
---|
[41a2620] | 7 | // Unify.cc --
|
---|
[a32b204] | 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Sun May 17 12:27:10 2015
|
---|
[4040425] | 11 | // Last Modified By : Peter A. Buhr
|
---|
| 12 | // Last Modified On : Wed Mar 2 17:37:05 2016
|
---|
| 13 | // Update Count : 37
|
---|
[a32b204] | 14 | //
|
---|
[51b73452] | 15 |
|
---|
| 16 | #include <set>
|
---|
| 17 | #include <memory>
|
---|
| 18 |
|
---|
| 19 | #include "Unify.h"
|
---|
| 20 | #include "TypeEnvironment.h"
|
---|
| 21 | #include "typeops.h"
|
---|
| 22 | #include "FindOpenVars.h"
|
---|
| 23 | #include "SynTree/Visitor.h"
|
---|
| 24 | #include "SynTree/Type.h"
|
---|
| 25 | #include "SynTree/Declaration.h"
|
---|
| 26 | #include "SymTab/Indexer.h"
|
---|
[d3b7937] | 27 | #include "Common/utility.h"
|
---|
[51b73452] | 28 |
|
---|
| 29 |
|
---|
[1cbca6e] | 30 | // #define DEBUG
|
---|
[51b73452] | 31 |
|
---|
| 32 | namespace ResolvExpr {
|
---|
[a32b204] | 33 | struct WidenMode {
|
---|
| 34 | WidenMode( bool widenFirst, bool widenSecond ): widenFirst( widenFirst ), widenSecond( widenSecond ) {}
|
---|
| 35 | WidenMode &operator|=( const WidenMode &other ) { widenFirst |= other.widenFirst; widenSecond |= other.widenSecond; return *this; }
|
---|
| 36 | WidenMode &operator&=( const WidenMode &other ) { widenFirst &= other.widenFirst; widenSecond &= other.widenSecond; return *this; }
|
---|
| 37 | WidenMode operator|( const WidenMode &other ) { WidenMode newWM( *this ); newWM |= other; return newWM; }
|
---|
| 38 | WidenMode operator&( const WidenMode &other ) { WidenMode newWM( *this ); newWM &= other; return newWM; }
|
---|
| 39 | operator bool() { return widenFirst && widenSecond; }
|
---|
[41a2620] | 40 |
|
---|
[a32b204] | 41 | bool widenFirst : 1, widenSecond : 1;
|
---|
| 42 | };
|
---|
[51b73452] | 43 |
|
---|
[a32b204] | 44 | class Unify : public Visitor {
|
---|
| 45 | public:
|
---|
| 46 | Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
|
---|
[41a2620] | 47 |
|
---|
[a32b204] | 48 | bool get_result() const { return result; }
|
---|
| 49 | private:
|
---|
| 50 | virtual void visit(VoidType *voidType);
|
---|
| 51 | virtual void visit(BasicType *basicType);
|
---|
| 52 | virtual void visit(PointerType *pointerType);
|
---|
| 53 | virtual void visit(ArrayType *arrayType);
|
---|
| 54 | virtual void visit(FunctionType *functionType);
|
---|
| 55 | virtual void visit(StructInstType *aggregateUseType);
|
---|
| 56 | virtual void visit(UnionInstType *aggregateUseType);
|
---|
| 57 | virtual void visit(EnumInstType *aggregateUseType);
|
---|
[4040425] | 58 | virtual void visit(TraitInstType *aggregateUseType);
|
---|
[a32b204] | 59 | virtual void visit(TypeInstType *aggregateUseType);
|
---|
| 60 | virtual void visit(TupleType *tupleType);
|
---|
[44b7088] | 61 | virtual void visit(VarArgsType *varArgsType);
|
---|
[a32b204] | 62 |
|
---|
| 63 | template< typename RefType > void handleRefType( RefType *inst, Type *other );
|
---|
[02ec390] | 64 | template< typename RefType > void handleGenericRefType( RefType *inst, Type *other );
|
---|
[a32b204] | 65 |
|
---|
| 66 | bool result;
|
---|
| 67 | Type *type2; // inherited
|
---|
| 68 | TypeEnvironment &env;
|
---|
| 69 | AssertionSet &needAssertions;
|
---|
| 70 | AssertionSet &haveAssertions;
|
---|
| 71 | const OpenVarSet &openVars;
|
---|
| 72 | WidenMode widenMode;
|
---|
| 73 | Type *commonType;
|
---|
| 74 | const SymTab::Indexer &indexer;
|
---|
| 75 | };
|
---|
| 76 |
|
---|
[eb50842] | 77 | /// Attempts an inexact unification of type1 and type2.
|
---|
| 78 | /// Returns false if no such unification; if the types can be unified, sets common (unless they unify exactly and have identical type qualifiers)
|
---|
[a32b204] | 79 | bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common );
|
---|
| 80 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
|
---|
[41a2620] | 81 |
|
---|
[a32b204] | 82 | bool typesCompatible( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
|
---|
| 83 | TypeEnvironment newEnv;
|
---|
[1cbca6e] | 84 | OpenVarSet openVars, closedVars; // added closedVars
|
---|
[a32b204] | 85 | AssertionSet needAssertions, haveAssertions;
|
---|
| 86 | Type *newFirst = first->clone(), *newSecond = second->clone();
|
---|
| 87 | env.apply( newFirst );
|
---|
| 88 | env.apply( newSecond );
|
---|
[1cbca6e] | 89 |
|
---|
| 90 | // do we need to do this? Seems like we do, types should be able to be compatible if they
|
---|
| 91 | // have free variables that can unify
|
---|
| 92 | findOpenVars( newFirst, openVars, closedVars, needAssertions, haveAssertions, false );
|
---|
| 93 | findOpenVars( newSecond, openVars, closedVars, needAssertions, haveAssertions, true );
|
---|
| 94 |
|
---|
[a32b204] | 95 | bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 96 | delete newFirst;
|
---|
| 97 | delete newSecond;
|
---|
| 98 | return result;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | bool typesCompatibleIgnoreQualifiers( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
|
---|
| 102 | TypeEnvironment newEnv;
|
---|
| 103 | OpenVarSet openVars;
|
---|
| 104 | AssertionSet needAssertions, haveAssertions;
|
---|
| 105 | Type *newFirst = first->clone(), *newSecond = second->clone();
|
---|
| 106 | env.apply( newFirst );
|
---|
| 107 | env.apply( newSecond );
|
---|
| 108 | newFirst->get_qualifiers() = Type::Qualifiers();
|
---|
| 109 | newSecond->get_qualifiers() = Type::Qualifiers();
|
---|
[51b73452] | 110 | /// std::cout << "first is ";
|
---|
| 111 | /// first->print( std::cout );
|
---|
| 112 | /// std::cout << std::endl << "second is ";
|
---|
| 113 | /// second->print( std::cout );
|
---|
| 114 | /// std::cout << std::endl << "newFirst is ";
|
---|
| 115 | /// newFirst->print( std::cout );
|
---|
| 116 | /// std::cout << std::endl << "newSecond is ";
|
---|
| 117 | /// newSecond->print( std::cout );
|
---|
| 118 | /// std::cout << std::endl;
|
---|
[a32b204] | 119 | bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 120 | delete newFirst;
|
---|
| 121 | delete newSecond;
|
---|
| 122 | return result;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | bool isFtype( Type *type, const SymTab::Indexer &indexer ) {
|
---|
| 126 | if ( dynamic_cast< FunctionType* >( type ) ) {
|
---|
| 127 | return true;
|
---|
| 128 | } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) {
|
---|
| 129 | return typeInst->get_isFtype();
|
---|
| 130 | } // if
|
---|
| 131 | return false;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | bool tyVarCompatible( TypeDecl::Kind kind, Type *type, const SymTab::Indexer &indexer ) {
|
---|
| 135 | switch ( kind ) {
|
---|
| 136 | case TypeDecl::Any:
|
---|
| 137 | case TypeDecl::Dtype:
|
---|
| 138 | return ! isFtype( type, indexer );
|
---|
[41a2620] | 139 |
|
---|
[a32b204] | 140 | case TypeDecl::Ftype:
|
---|
| 141 | return isFtype( type, indexer );
|
---|
| 142 | } // switch
|
---|
| 143 | assert( false );
|
---|
| 144 | return false;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | bool bindVar( TypeInstType *typeInst, Type *other, TypeDecl::Kind kind, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
|
---|
| 148 | OpenVarSet::const_iterator tyvar = openVars.find( typeInst->get_name() );
|
---|
| 149 | assert( tyvar != openVars.end() );
|
---|
| 150 | if ( ! tyVarCompatible( tyvar->second, other, indexer ) ) {
|
---|
| 151 | return false;
|
---|
| 152 | } // if
|
---|
| 153 | if ( occurs( other, typeInst->get_name(), env ) ) {
|
---|
| 154 | return false;
|
---|
| 155 | } // if
|
---|
| 156 | EqvClass curClass;
|
---|
| 157 | if ( env.lookup( typeInst->get_name(), curClass ) ) {
|
---|
| 158 | if ( curClass.type ) {
|
---|
| 159 | Type *common = 0;
|
---|
[eb50842] | 160 | // attempt to unify equivalence class type (which has qualifiers stripped, so they must be restored) with the type to bind to
|
---|
[a32b204] | 161 | std::auto_ptr< Type > newType( curClass.type->clone() );
|
---|
[721f17a] | 162 | newType->get_qualifiers() = typeInst->get_qualifiers();
|
---|
[a32b204] | 163 | if ( unifyInexact( newType.get(), other, env, needAssertions, haveAssertions, openVars, widenMode & WidenMode( curClass.allowWidening, true ), indexer, common ) ) {
|
---|
| 164 | if ( common ) {
|
---|
| 165 | common->get_qualifiers() = Type::Qualifiers();
|
---|
| 166 | delete curClass.type;
|
---|
| 167 | curClass.type = common;
|
---|
| 168 | env.add( curClass );
|
---|
| 169 | } // if
|
---|
| 170 | return true;
|
---|
| 171 | } else {
|
---|
| 172 | return false;
|
---|
| 173 | } // if
|
---|
| 174 | } else {
|
---|
| 175 | curClass.type = other->clone();
|
---|
| 176 | curClass.type->get_qualifiers() = Type::Qualifiers();
|
---|
| 177 | curClass.allowWidening = widenMode.widenFirst && widenMode.widenSecond;
|
---|
| 178 | env.add( curClass );
|
---|
| 179 | } // if
|
---|
| 180 | } else {
|
---|
| 181 | EqvClass newClass;
|
---|
| 182 | newClass.vars.insert( typeInst->get_name() );
|
---|
| 183 | newClass.type = other->clone();
|
---|
| 184 | newClass.type->get_qualifiers() = Type::Qualifiers();
|
---|
| 185 | newClass.allowWidening = widenMode.widenFirst && widenMode.widenSecond;
|
---|
| 186 | newClass.kind = kind;
|
---|
| 187 | env.add( newClass );
|
---|
| 188 | } // if
|
---|
| 189 | return true;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | bool bindVarToVar( TypeInstType *var1, TypeInstType *var2, TypeDecl::Kind kind, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
|
---|
| 193 | bool result = true;
|
---|
| 194 | EqvClass class1, class2;
|
---|
| 195 | bool hasClass1 = false, hasClass2 = false;
|
---|
| 196 | bool widen1 = false, widen2 = false;
|
---|
| 197 | Type *type1 = 0, *type2 = 0;
|
---|
[41a2620] | 198 |
|
---|
[a32b204] | 199 | if ( env.lookup( var1->get_name(), class1 ) ) {
|
---|
| 200 | hasClass1 = true;
|
---|
| 201 | if ( class1.type ) {
|
---|
| 202 | if ( occurs( class1.type, var2->get_name(), env ) ) {
|
---|
| 203 | return false;
|
---|
| 204 | } // if
|
---|
| 205 | type1 = class1.type->clone();
|
---|
| 206 | } // if
|
---|
| 207 | widen1 = widenMode.widenFirst && class1.allowWidening;
|
---|
| 208 | } // if
|
---|
| 209 | if ( env.lookup( var2->get_name(), class2 ) ) {
|
---|
| 210 | hasClass2 = true;
|
---|
| 211 | if ( class2.type ) {
|
---|
| 212 | if ( occurs( class2.type, var1->get_name(), env ) ) {
|
---|
| 213 | return false;
|
---|
| 214 | } // if
|
---|
| 215 | type2 = class2.type->clone();
|
---|
| 216 | } // if
|
---|
| 217 | widen2 = widenMode.widenSecond && class2.allowWidening;
|
---|
| 218 | } // if
|
---|
[41a2620] | 219 |
|
---|
[a32b204] | 220 | if ( type1 && type2 ) {
|
---|
[51b73452] | 221 | // std::cout << "has type1 && type2" << std::endl;
|
---|
[a32b204] | 222 | WidenMode newWidenMode ( widen1, widen2 );
|
---|
| 223 | Type *common = 0;
|
---|
| 224 | if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, newWidenMode, indexer, common ) ) {
|
---|
| 225 | class1.vars.insert( class2.vars.begin(), class2.vars.end() );
|
---|
| 226 | class1.allowWidening = widen1 && widen2;
|
---|
| 227 | if ( common ) {
|
---|
| 228 | common->get_qualifiers() = Type::Qualifiers();
|
---|
| 229 | delete class1.type;
|
---|
| 230 | class1.type = common;
|
---|
| 231 | } // if
|
---|
| 232 | env.add( class1 );
|
---|
| 233 | } else {
|
---|
| 234 | result = false;
|
---|
| 235 | } // if
|
---|
| 236 | } else if ( hasClass1 && hasClass2 ) {
|
---|
| 237 | if ( type1 ) {
|
---|
| 238 | class1.vars.insert( class2.vars.begin(), class2.vars.end() );
|
---|
| 239 | class1.allowWidening = widen1;
|
---|
| 240 | env.add( class1 );
|
---|
| 241 | } else {
|
---|
| 242 | class2.vars.insert( class1.vars.begin(), class1.vars.end() );
|
---|
| 243 | class2.allowWidening = widen2;
|
---|
| 244 | env.add( class2 );
|
---|
| 245 | } // if
|
---|
| 246 | } else if ( hasClass1 ) {
|
---|
| 247 | class1.vars.insert( var2->get_name() );
|
---|
| 248 | class1.allowWidening = widen1;
|
---|
| 249 | env.add( class1 );
|
---|
| 250 | } else if ( hasClass2 ) {
|
---|
| 251 | class2.vars.insert( var1->get_name() );
|
---|
| 252 | class2.allowWidening = widen2;
|
---|
| 253 | env.add( class2 );
|
---|
| 254 | } else {
|
---|
| 255 | EqvClass newClass;
|
---|
| 256 | newClass.vars.insert( var1->get_name() );
|
---|
| 257 | newClass.vars.insert( var2->get_name() );
|
---|
| 258 | newClass.allowWidening = widen1 && widen2;
|
---|
| 259 | newClass.kind = kind;
|
---|
| 260 | env.add( newClass );
|
---|
| 261 | } // if
|
---|
| 262 | delete type1;
|
---|
| 263 | delete type2;
|
---|
| 264 | return result;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
| 268 | OpenVarSet closedVars;
|
---|
| 269 | findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
|
---|
| 270 | findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
|
---|
| 271 | Type *commonType = 0;
|
---|
| 272 | if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType ) ) {
|
---|
| 273 | if ( commonType ) {
|
---|
| 274 | delete commonType;
|
---|
| 275 | } // if
|
---|
| 276 | return true;
|
---|
| 277 | } else {
|
---|
| 278 | return false;
|
---|
| 279 | } // if
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer, Type *&commonType ) {
|
---|
| 283 | OpenVarSet closedVars;
|
---|
| 284 | findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
|
---|
| 285 | findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
|
---|
| 286 | return unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType );
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
|
---|
[51b73452] | 290 | #ifdef DEBUG
|
---|
[a32b204] | 291 | TypeEnvironment debugEnv( env );
|
---|
[51b73452] | 292 | #endif
|
---|
[eb50842] | 293 | if ( type1->get_qualifiers() != type2->get_qualifiers() ) {
|
---|
| 294 | return false;
|
---|
| 295 | }
|
---|
| 296 |
|
---|
[a32b204] | 297 | bool result;
|
---|
| 298 | TypeInstType *var1 = dynamic_cast< TypeInstType* >( type1 );
|
---|
| 299 | TypeInstType *var2 = dynamic_cast< TypeInstType* >( type2 );
|
---|
| 300 | OpenVarSet::const_iterator entry1, entry2;
|
---|
| 301 | if ( var1 ) {
|
---|
| 302 | entry1 = openVars.find( var1->get_name() );
|
---|
| 303 | } // if
|
---|
| 304 | if ( var2 ) {
|
---|
| 305 | entry2 = openVars.find( var2->get_name() );
|
---|
| 306 | } // if
|
---|
| 307 | bool isopen1 = var1 && ( entry1 != openVars.end() );
|
---|
| 308 | bool isopen2 = var2 && ( entry2 != openVars.end() );
|
---|
[eb50842] | 309 |
|
---|
| 310 | if ( isopen1 && isopen2 && entry1->second == entry2->second ) {
|
---|
[a32b204] | 311 | result = bindVarToVar( var1, var2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
| 312 | } else if ( isopen1 ) {
|
---|
| 313 | result = bindVar( var1, type2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
| 314 | } else if ( isopen2 ) {
|
---|
| 315 | result = bindVar( var2, type1, entry2->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
| 316 | } else {
|
---|
| 317 | Unify comparator( type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
| 318 | type1->accept( comparator );
|
---|
| 319 | result = comparator.get_result();
|
---|
| 320 | } // if
|
---|
[51b73452] | 321 | #ifdef DEBUG
|
---|
[a32b204] | 322 | std::cout << "============ unifyExact" << std::endl;
|
---|
| 323 | std::cout << "type1 is ";
|
---|
| 324 | type1->print( std::cout );
|
---|
| 325 | std::cout << std::endl << "type2 is ";
|
---|
| 326 | type2->print( std::cout );
|
---|
| 327 | std::cout << std::endl << "openVars are ";
|
---|
| 328 | printOpenVarSet( openVars, std::cout, 8 );
|
---|
| 329 | std::cout << std::endl << "input env is " << std::endl;
|
---|
| 330 | debugEnv.print( std::cout, 8 );
|
---|
| 331 | std::cout << std::endl << "result env is " << std::endl;
|
---|
| 332 | env.print( std::cout, 8 );
|
---|
| 333 | std::cout << "result is " << result << std::endl;
|
---|
[51b73452] | 334 | #endif
|
---|
[a32b204] | 335 | return result;
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
| 339 | return unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common ) {
|
---|
| 343 | Type::Qualifiers tq1 = type1->get_qualifiers(), tq2 = type2->get_qualifiers();
|
---|
| 344 | type1->get_qualifiers() = Type::Qualifiers();
|
---|
| 345 | type2->get_qualifiers() = Type::Qualifiers();
|
---|
| 346 | bool result;
|
---|
[51b73452] | 347 | #ifdef DEBUG
|
---|
[a32b204] | 348 | std::cout << "unifyInexact type 1 is ";
|
---|
| 349 | type1->print( std::cout );
|
---|
| 350 | std::cout << "type 2 is ";
|
---|
| 351 | type2->print( std::cout );
|
---|
| 352 | std::cout << std::endl;
|
---|
[51b73452] | 353 | #endif
|
---|
[a32b204] | 354 | if ( ! unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer ) ) {
|
---|
[51b73452] | 355 | #ifdef DEBUG
|
---|
[a32b204] | 356 | std::cout << "unifyInexact: no exact unification found" << std::endl;
|
---|
[51b73452] | 357 | #endif
|
---|
[a32b204] | 358 | if ( ( common = commonType( type1, type2, widenMode.widenFirst, widenMode.widenSecond, indexer, env, openVars ) ) ) {
|
---|
| 359 | common->get_qualifiers() = tq1 + tq2;
|
---|
[51b73452] | 360 | #ifdef DEBUG
|
---|
[a32b204] | 361 | std::cout << "unifyInexact: common type is ";
|
---|
| 362 | common->print( std::cout );
|
---|
| 363 | std::cout << std::endl;
|
---|
[51b73452] | 364 | #endif
|
---|
[a32b204] | 365 | result = true;
|
---|
| 366 | } else {
|
---|
[51b73452] | 367 | #ifdef DEBUG
|
---|
[a32b204] | 368 | std::cout << "unifyInexact: no common type found" << std::endl;
|
---|
[51b73452] | 369 | #endif
|
---|
[a32b204] | 370 | result = false;
|
---|
| 371 | } // if
|
---|
| 372 | } else {
|
---|
| 373 | if ( tq1 != tq2 ) {
|
---|
| 374 | if ( ( tq1 > tq2 || widenMode.widenFirst ) && ( tq2 > tq1 || widenMode.widenSecond ) ) {
|
---|
| 375 | common = type1->clone();
|
---|
| 376 | common->get_qualifiers() = tq1 + tq2;
|
---|
| 377 | result = true;
|
---|
| 378 | } else {
|
---|
| 379 | result = false;
|
---|
| 380 | } // if
|
---|
| 381 | } else {
|
---|
| 382 | result = true;
|
---|
| 383 | } // if
|
---|
| 384 | } // if
|
---|
| 385 | type1->get_qualifiers() = tq1;
|
---|
| 386 | type2->get_qualifiers() = tq2;
|
---|
| 387 | return result;
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | Unify::Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer )
|
---|
| 391 | : result( false ), type2( type2 ), env( env ), needAssertions( needAssertions ), haveAssertions( haveAssertions ), openVars( openVars ), widenMode( widenMode ), indexer( indexer ) {
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 | void Unify::visit(VoidType *voidType) {
|
---|
| 395 | result = dynamic_cast< VoidType* >( type2 );
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | void Unify::visit(BasicType *basicType) {
|
---|
| 399 | if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) {
|
---|
| 400 | result = basicType->get_kind() == otherBasic->get_kind();
|
---|
| 401 | } // if
|
---|
| 402 | }
|
---|
| 403 |
|
---|
| 404 | void markAssertionSet( AssertionSet &assertions, DeclarationWithType *assert ) {
|
---|
[51b73452] | 405 | /// std::cout << "assertion set is" << std::endl;
|
---|
| 406 | /// printAssertionSet( assertions, std::cout, 8 );
|
---|
| 407 | /// std::cout << "looking for ";
|
---|
| 408 | /// assert->print( std::cout );
|
---|
| 409 | /// std::cout << std::endl;
|
---|
[a32b204] | 410 | AssertionSet::iterator i = assertions.find( assert );
|
---|
| 411 | if ( i != assertions.end() ) {
|
---|
[51b73452] | 412 | /// std::cout << "found it!" << std::endl;
|
---|
[a32b204] | 413 | i->second = true;
|
---|
| 414 | } // if
|
---|
| 415 | }
|
---|
| 416 |
|
---|
| 417 | void markAssertions( AssertionSet &assertion1, AssertionSet &assertion2, Type *type ) {
|
---|
| 418 | for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
|
---|
| 419 | for ( std::list< DeclarationWithType* >::const_iterator assert = (*tyvar)->get_assertions().begin(); assert != (*tyvar)->get_assertions().end(); ++assert ) {
|
---|
| 420 | markAssertionSet( assertion1, *assert );
|
---|
| 421 | markAssertionSet( assertion2, *assert );
|
---|
| 422 | } // for
|
---|
| 423 | } // for
|
---|
| 424 | }
|
---|
| 425 |
|
---|
| 426 | void Unify::visit(PointerType *pointerType) {
|
---|
| 427 | if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) {
|
---|
| 428 | result = unifyExact( pointerType->get_base(), otherPointer->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 429 | markAssertions( haveAssertions, needAssertions, pointerType );
|
---|
| 430 | markAssertions( haveAssertions, needAssertions, otherPointer );
|
---|
| 431 | } // if
|
---|
| 432 | }
|
---|
| 433 |
|
---|
| 434 | void Unify::visit(ArrayType *arrayType) {
|
---|
| 435 | ArrayType *otherArray = dynamic_cast< ArrayType* >( type2 );
|
---|
[1cbca6e] | 436 | // to unify, array types must both be VLA or both not VLA
|
---|
| 437 | // and must both have a dimension expression or not have a dimension
|
---|
[41a2620] | 438 | if ( otherArray && arrayType->get_isVarLen() == otherArray->get_isVarLen() ) {
|
---|
[1cbca6e] | 439 |
|
---|
| 440 | // not positive this is correct in all cases, but it's needed for typedefs
|
---|
| 441 | if ( arrayType->get_isVarLen() || otherArray->get_isVarLen() ) {
|
---|
| 442 | return;
|
---|
| 443 | }
|
---|
| 444 |
|
---|
| 445 | if ( ! arrayType->get_isVarLen() && ! otherArray->get_isVarLen() &&
|
---|
| 446 | arrayType->get_dimension() != 0 && otherArray->get_dimension() != 0 ) {
|
---|
| 447 | ConstantExpr * ce1 = dynamic_cast< ConstantExpr * >( arrayType->get_dimension() );
|
---|
| 448 | ConstantExpr * ce2 = dynamic_cast< ConstantExpr * >( otherArray->get_dimension() );
|
---|
[41a2620] | 449 | // see C11 Reference Manual 6.7.6.2.6
|
---|
| 450 | // two array types with size specifiers that are integer constant expressions are
|
---|
| 451 | // compatible if both size specifiers have the same constant value
|
---|
| 452 | if ( ce1 && ce2 ) {
|
---|
| 453 | Constant * c1 = ce1->get_constant();
|
---|
| 454 | Constant * c2 = ce2->get_constant();
|
---|
| 455 |
|
---|
| 456 | if ( c1->get_value() != c2->get_value() ) {
|
---|
| 457 | // does not unify if the dimension is different
|
---|
| 458 | return;
|
---|
| 459 | }
|
---|
[1cbca6e] | 460 | }
|
---|
| 461 | }
|
---|
| 462 |
|
---|
[a32b204] | 463 | result = unifyExact( arrayType->get_base(), otherArray->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 464 | } // if
|
---|
| 465 | }
|
---|
| 466 |
|
---|
| 467 | template< typename Iterator1, typename Iterator2 >
|
---|
| 468 | bool unifyDeclList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
| 469 | for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
|
---|
[1cbca6e] | 470 | // Type * commonType;
|
---|
| 471 | // if ( ! unifyInexact( (*list1Begin)->get_type(), (*list2Begin)->get_type(), env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType ) ) {
|
---|
[a32b204] | 472 | if ( ! unifyExact( (*list1Begin)->get_type(), (*list2Begin)->get_type(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
|
---|
| 473 | return false;
|
---|
| 474 | } // if
|
---|
| 475 | } // for
|
---|
| 476 | if ( list1Begin != list1End || list2Begin != list2End ) {
|
---|
| 477 | return false;
|
---|
| 478 | } else {
|
---|
| 479 | return true;
|
---|
| 480 | } // if
|
---|
| 481 | }
|
---|
| 482 |
|
---|
| 483 | void Unify::visit(FunctionType *functionType) {
|
---|
| 484 | FunctionType *otherFunction = dynamic_cast< FunctionType* >( type2 );
|
---|
| 485 | if ( otherFunction && functionType->get_isVarArgs() == otherFunction->get_isVarArgs() ) {
|
---|
[03da511] | 486 | if ( functionType->get_parameters().size() == otherFunction->get_parameters().size() && functionType->get_returnVals().size() == otherFunction->get_returnVals().size() ) {
|
---|
| 487 | if ( unifyDeclList( functionType->get_parameters().begin(), functionType->get_parameters().end(), otherFunction->get_parameters().begin(), otherFunction->get_parameters().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
|
---|
| 488 | if ( unifyDeclList( functionType->get_returnVals().begin(), functionType->get_returnVals().end(), otherFunction->get_returnVals().begin(), otherFunction->get_returnVals().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
|
---|
[1cbca6e] | 489 |
|
---|
[03da511] | 490 | markAssertions( haveAssertions, needAssertions, functionType );
|
---|
| 491 | markAssertions( haveAssertions, needAssertions, otherFunction );
|
---|
[41a2620] | 492 |
|
---|
[03da511] | 493 | result = true;
|
---|
| 494 | } // if
|
---|
[a32b204] | 495 | } // if
|
---|
| 496 | } // if
|
---|
| 497 | } // if
|
---|
| 498 | }
|
---|
| 499 |
|
---|
| 500 | template< typename RefType >
|
---|
[02ec390] | 501 | void Unify::handleRefType( RefType *inst, Type *other ) {
|
---|
| 502 | // check that other type is compatible and named the same
|
---|
[a32b204] | 503 | RefType *otherStruct = dynamic_cast< RefType* >( other );
|
---|
| 504 | result = otherStruct && inst->get_name() == otherStruct->get_name();
|
---|
[02ec390] | 505 | }
|
---|
| 506 |
|
---|
| 507 | template< typename RefType >
|
---|
| 508 | void Unify::handleGenericRefType( RefType *inst, Type *other ) {
|
---|
| 509 | // Check that other type is compatible and named the same
|
---|
| 510 | handleRefType( inst, other );
|
---|
| 511 | if ( ! result ) return;
|
---|
[f5234f3] | 512 | // Check that parameters of types unify, if any
|
---|
[02ec390] | 513 | std::list< Expression* > params = inst->get_parameters();
|
---|
[f5234f3] | 514 | std::list< Expression* > otherParams = ((RefType*)other)->get_parameters();
|
---|
| 515 |
|
---|
| 516 | std::list< Expression* >::const_iterator it = params.begin(), jt = otherParams.begin();
|
---|
| 517 | for ( ; it != params.end() && jt != otherParams.end(); ++it, ++jt ) {
|
---|
| 518 | TypeExpr *param = dynamic_cast< TypeExpr* >(*it);
|
---|
| 519 | assert(param && "Aggregate parameters should be type expressions");
|
---|
| 520 | TypeExpr *otherParam = dynamic_cast< TypeExpr* >(*jt);
|
---|
| 521 | assert(otherParam && "Aggregate parameters should be type expressions");
|
---|
| 522 |
|
---|
| 523 | if ( ! unifyExact( param->get_type(), otherParam->get_type(), env, needAssertions, haveAssertions, openVars, WidenMode(false, false), indexer ) ) {
|
---|
[02ec390] | 524 | result = false;
|
---|
| 525 | return;
|
---|
| 526 | }
|
---|
| 527 | }
|
---|
[f5234f3] | 528 | result = ( it == params.end() && jt == otherParams.end() );
|
---|
[02ec390] | 529 | }
|
---|
[a32b204] | 530 |
|
---|
| 531 | void Unify::visit(StructInstType *structInst) {
|
---|
[02ec390] | 532 | handleGenericRefType( structInst, type2 );
|
---|
[a32b204] | 533 | }
|
---|
| 534 |
|
---|
| 535 | void Unify::visit(UnionInstType *unionInst) {
|
---|
[02ec390] | 536 | handleGenericRefType( unionInst, type2 );
|
---|
[a32b204] | 537 | }
|
---|
| 538 |
|
---|
| 539 | void Unify::visit(EnumInstType *enumInst) {
|
---|
| 540 | handleRefType( enumInst, type2 );
|
---|
| 541 | }
|
---|
| 542 |
|
---|
[4040425] | 543 | void Unify::visit(TraitInstType *contextInst) {
|
---|
[a32b204] | 544 | handleRefType( contextInst, type2 );
|
---|
| 545 | }
|
---|
| 546 |
|
---|
| 547 | void Unify::visit(TypeInstType *typeInst) {
|
---|
| 548 | assert( openVars.find( typeInst->get_name() ) == openVars.end() );
|
---|
| 549 | TypeInstType *otherInst = dynamic_cast< TypeInstType* >( type2 );
|
---|
| 550 | if ( otherInst && typeInst->get_name() == otherInst->get_name() ) {
|
---|
| 551 | result = true;
|
---|
[51b73452] | 552 | /// } else {
|
---|
| 553 | /// NamedTypeDecl *nt = indexer.lookupType( typeInst->get_name() );
|
---|
[a32b204] | 554 | /// if ( nt ) {
|
---|
[51b73452] | 555 | /// TypeDecl *type = dynamic_cast< TypeDecl* >( nt );
|
---|
| 556 | /// assert( type );
|
---|
[a32b204] | 557 | /// if ( type->get_base() ) {
|
---|
[51b73452] | 558 | /// result = unifyExact( type->get_base(), typeInst, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 559 | /// }
|
---|
| 560 | /// }
|
---|
[a32b204] | 561 | } // if
|
---|
| 562 | }
|
---|
| 563 |
|
---|
| 564 | template< typename Iterator1, typename Iterator2 >
|
---|
| 565 | bool unifyList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
|
---|
| 566 | for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
|
---|
| 567 | Type *commonType = 0;
|
---|
| 568 | if ( ! unifyInexact( *list1Begin, *list2Begin, env, needAssertions, haveAssertions, openVars, widenMode, indexer, commonType ) ) {
|
---|
| 569 | return false;
|
---|
| 570 | }
|
---|
| 571 | delete commonType;
|
---|
| 572 | } // for
|
---|
| 573 | if ( list1Begin != list1End || list2Begin != list2End ) {
|
---|
| 574 | return false;
|
---|
| 575 | } else {
|
---|
| 576 | return true;
|
---|
| 577 | } //if
|
---|
| 578 | }
|
---|
| 579 |
|
---|
| 580 | void Unify::visit(TupleType *tupleType) {
|
---|
| 581 | if ( TupleType *otherTuple = dynamic_cast< TupleType* >( type2 ) ) {
|
---|
| 582 | result = unifyList( tupleType->get_types().begin(), tupleType->get_types().end(), otherTuple->get_types().begin(), otherTuple->get_types().end(), env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
| 583 | } // if
|
---|
| 584 | }
|
---|
[51b73452] | 585 |
|
---|
[44b7088] | 586 | void Unify::visit(VarArgsType *varArgsType) {
|
---|
| 587 | result = dynamic_cast< VarArgsType* >( type2 );
|
---|
| 588 | }
|
---|
| 589 |
|
---|
[51b73452] | 590 | } // namespace ResolvExpr
|
---|
[a32b204] | 591 |
|
---|
| 592 | // Local Variables: //
|
---|
| 593 | // tab-width: 4 //
|
---|
| 594 | // mode: c++ //
|
---|
| 595 | // compile-command: "make install" //
|
---|
| 596 | // End: //
|
---|