[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 | //
|
---|
| 7 | // Unify.cc --
|
---|
| 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Sun May 17 12:27:10 2015
|
---|
| 11 | // Last Modified By : Peter A. Buhr
|
---|
[721f17a] | 12 | // Last Modified On : Fri Jun 26 14:57:05 2015
|
---|
| 13 | // Update Count : 7
|
---|
[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"
|
---|
| 27 | #include "utility.h"
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | //#define DEBUG
|
---|
| 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; }
|
---|
[51b73452] | 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 );
|
---|
[51b73452] | 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);
|
---|
| 58 | virtual void visit(ContextInstType *aggregateUseType);
|
---|
| 59 | virtual void visit(TypeInstType *aggregateUseType);
|
---|
| 60 | virtual void visit(TupleType *tupleType);
|
---|
| 61 |
|
---|
| 62 | template< typename RefType > void handleRefType( RefType *inst, Type *other );
|
---|
| 63 |
|
---|
| 64 | bool result;
|
---|
| 65 | Type *type2; // inherited
|
---|
| 66 | TypeEnvironment &env;
|
---|
| 67 | AssertionSet &needAssertions;
|
---|
| 68 | AssertionSet &haveAssertions;
|
---|
| 69 | const OpenVarSet &openVars;
|
---|
| 70 | WidenMode widenMode;
|
---|
| 71 | Type *commonType;
|
---|
| 72 | const SymTab::Indexer &indexer;
|
---|
| 73 | };
|
---|
| 74 |
|
---|
[eb50842] | 75 | /// Attempts an inexact unification of type1 and type2.
|
---|
| 76 | /// Returns false if no such unification; if the types can be unified, sets common (unless they unify exactly and have identical type qualifiers)
|
---|
[a32b204] | 77 | bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common );
|
---|
| 78 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
|
---|
[51b73452] | 79 |
|
---|
[a32b204] | 80 | bool typesCompatible( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
|
---|
| 81 | TypeEnvironment newEnv;
|
---|
| 82 | OpenVarSet openVars;
|
---|
| 83 | AssertionSet needAssertions, haveAssertions;
|
---|
| 84 | Type *newFirst = first->clone(), *newSecond = second->clone();
|
---|
| 85 | env.apply( newFirst );
|
---|
| 86 | env.apply( newSecond );
|
---|
| 87 | bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 88 | delete newFirst;
|
---|
| 89 | delete newSecond;
|
---|
| 90 | return result;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | bool typesCompatibleIgnoreQualifiers( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
|
---|
| 94 | TypeEnvironment newEnv;
|
---|
| 95 | OpenVarSet openVars;
|
---|
| 96 | AssertionSet needAssertions, haveAssertions;
|
---|
| 97 | Type *newFirst = first->clone(), *newSecond = second->clone();
|
---|
| 98 | env.apply( newFirst );
|
---|
| 99 | env.apply( newSecond );
|
---|
| 100 | newFirst->get_qualifiers() = Type::Qualifiers();
|
---|
| 101 | newSecond->get_qualifiers() = Type::Qualifiers();
|
---|
[51b73452] | 102 | /// std::cout << "first is ";
|
---|
| 103 | /// first->print( std::cout );
|
---|
| 104 | /// std::cout << std::endl << "second is ";
|
---|
| 105 | /// second->print( std::cout );
|
---|
| 106 | /// std::cout << std::endl << "newFirst is ";
|
---|
| 107 | /// newFirst->print( std::cout );
|
---|
| 108 | /// std::cout << std::endl << "newSecond is ";
|
---|
| 109 | /// newSecond->print( std::cout );
|
---|
| 110 | /// std::cout << std::endl;
|
---|
[a32b204] | 111 | bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 112 | delete newFirst;
|
---|
| 113 | delete newSecond;
|
---|
| 114 | return result;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | bool isFtype( Type *type, const SymTab::Indexer &indexer ) {
|
---|
| 118 | if ( dynamic_cast< FunctionType* >( type ) ) {
|
---|
| 119 | return true;
|
---|
| 120 | } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) {
|
---|
| 121 | return typeInst->get_isFtype();
|
---|
| 122 | } // if
|
---|
| 123 | return false;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | bool tyVarCompatible( TypeDecl::Kind kind, Type *type, const SymTab::Indexer &indexer ) {
|
---|
| 127 | switch ( kind ) {
|
---|
| 128 | case TypeDecl::Any:
|
---|
| 129 | case TypeDecl::Dtype:
|
---|
| 130 | return ! isFtype( type, indexer );
|
---|
[51b73452] | 131 |
|
---|
[a32b204] | 132 | case TypeDecl::Ftype:
|
---|
| 133 | return isFtype( type, indexer );
|
---|
| 134 | } // switch
|
---|
| 135 | assert( false );
|
---|
| 136 | return false;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | bool bindVar( TypeInstType *typeInst, Type *other, TypeDecl::Kind kind, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
|
---|
| 140 | OpenVarSet::const_iterator tyvar = openVars.find( typeInst->get_name() );
|
---|
| 141 | assert( tyvar != openVars.end() );
|
---|
| 142 | if ( ! tyVarCompatible( tyvar->second, other, indexer ) ) {
|
---|
| 143 | return false;
|
---|
| 144 | } // if
|
---|
| 145 | if ( occurs( other, typeInst->get_name(), env ) ) {
|
---|
| 146 | return false;
|
---|
| 147 | } // if
|
---|
| 148 | EqvClass curClass;
|
---|
| 149 | if ( env.lookup( typeInst->get_name(), curClass ) ) {
|
---|
| 150 | if ( curClass.type ) {
|
---|
| 151 | Type *common = 0;
|
---|
[eb50842] | 152 | // attempt to unify equivalence class type (which has qualifiers stripped, so they must be restored) with the type to bind to
|
---|
[a32b204] | 153 | std::auto_ptr< Type > newType( curClass.type->clone() );
|
---|
[721f17a] | 154 | newType->get_qualifiers() = typeInst->get_qualifiers();
|
---|
[a32b204] | 155 | if ( unifyInexact( newType.get(), other, env, needAssertions, haveAssertions, openVars, widenMode & WidenMode( curClass.allowWidening, true ), indexer, common ) ) {
|
---|
| 156 | if ( common ) {
|
---|
| 157 | common->get_qualifiers() = Type::Qualifiers();
|
---|
| 158 | delete curClass.type;
|
---|
| 159 | curClass.type = common;
|
---|
| 160 | env.add( curClass );
|
---|
| 161 | } // if
|
---|
| 162 | return true;
|
---|
| 163 | } else {
|
---|
| 164 | return false;
|
---|
| 165 | } // if
|
---|
| 166 | } else {
|
---|
| 167 | curClass.type = other->clone();
|
---|
| 168 | curClass.type->get_qualifiers() = Type::Qualifiers();
|
---|
| 169 | curClass.allowWidening = widenMode.widenFirst && widenMode.widenSecond;
|
---|
| 170 | env.add( curClass );
|
---|
| 171 | } // if
|
---|
| 172 | } else {
|
---|
| 173 | EqvClass newClass;
|
---|
| 174 | newClass.vars.insert( typeInst->get_name() );
|
---|
| 175 | newClass.type = other->clone();
|
---|
| 176 | newClass.type->get_qualifiers() = Type::Qualifiers();
|
---|
| 177 | newClass.allowWidening = widenMode.widenFirst && widenMode.widenSecond;
|
---|
| 178 | newClass.kind = kind;
|
---|
| 179 | env.add( newClass );
|
---|
| 180 | } // if
|
---|
| 181 | return true;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | bool bindVarToVar( TypeInstType *var1, TypeInstType *var2, TypeDecl::Kind kind, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
|
---|
| 185 | bool result = true;
|
---|
| 186 | EqvClass class1, class2;
|
---|
| 187 | bool hasClass1 = false, hasClass2 = false;
|
---|
| 188 | bool widen1 = false, widen2 = false;
|
---|
| 189 | Type *type1 = 0, *type2 = 0;
|
---|
[51b73452] | 190 |
|
---|
[a32b204] | 191 | if ( env.lookup( var1->get_name(), class1 ) ) {
|
---|
| 192 | hasClass1 = true;
|
---|
| 193 | if ( class1.type ) {
|
---|
| 194 | if ( occurs( class1.type, var2->get_name(), env ) ) {
|
---|
| 195 | return false;
|
---|
| 196 | } // if
|
---|
| 197 | type1 = class1.type->clone();
|
---|
| 198 | } // if
|
---|
| 199 | widen1 = widenMode.widenFirst && class1.allowWidening;
|
---|
| 200 | } // if
|
---|
| 201 | if ( env.lookup( var2->get_name(), class2 ) ) {
|
---|
| 202 | hasClass2 = true;
|
---|
| 203 | if ( class2.type ) {
|
---|
| 204 | if ( occurs( class2.type, var1->get_name(), env ) ) {
|
---|
| 205 | return false;
|
---|
| 206 | } // if
|
---|
| 207 | type2 = class2.type->clone();
|
---|
| 208 | } // if
|
---|
| 209 | widen2 = widenMode.widenSecond && class2.allowWidening;
|
---|
| 210 | } // if
|
---|
[51b73452] | 211 |
|
---|
[a32b204] | 212 | if ( type1 && type2 ) {
|
---|
[51b73452] | 213 | // std::cout << "has type1 && type2" << std::endl;
|
---|
[a32b204] | 214 | WidenMode newWidenMode ( widen1, widen2 );
|
---|
| 215 | Type *common = 0;
|
---|
| 216 | if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, newWidenMode, indexer, common ) ) {
|
---|
| 217 | class1.vars.insert( class2.vars.begin(), class2.vars.end() );
|
---|
| 218 | class1.allowWidening = widen1 && widen2;
|
---|
| 219 | if ( common ) {
|
---|
| 220 | common->get_qualifiers() = Type::Qualifiers();
|
---|
| 221 | delete class1.type;
|
---|
| 222 | class1.type = common;
|
---|
| 223 | } // if
|
---|
| 224 | env.add( class1 );
|
---|
| 225 | } else {
|
---|
| 226 | result = false;
|
---|
| 227 | } // if
|
---|
| 228 | } else if ( hasClass1 && hasClass2 ) {
|
---|
| 229 | if ( type1 ) {
|
---|
| 230 | class1.vars.insert( class2.vars.begin(), class2.vars.end() );
|
---|
| 231 | class1.allowWidening = widen1;
|
---|
| 232 | env.add( class1 );
|
---|
| 233 | } else {
|
---|
| 234 | class2.vars.insert( class1.vars.begin(), class1.vars.end() );
|
---|
| 235 | class2.allowWidening = widen2;
|
---|
| 236 | env.add( class2 );
|
---|
| 237 | } // if
|
---|
| 238 | } else if ( hasClass1 ) {
|
---|
| 239 | class1.vars.insert( var2->get_name() );
|
---|
| 240 | class1.allowWidening = widen1;
|
---|
| 241 | env.add( class1 );
|
---|
| 242 | } else if ( hasClass2 ) {
|
---|
| 243 | class2.vars.insert( var1->get_name() );
|
---|
| 244 | class2.allowWidening = widen2;
|
---|
| 245 | env.add( class2 );
|
---|
| 246 | } else {
|
---|
| 247 | EqvClass newClass;
|
---|
| 248 | newClass.vars.insert( var1->get_name() );
|
---|
| 249 | newClass.vars.insert( var2->get_name() );
|
---|
| 250 | newClass.allowWidening = widen1 && widen2;
|
---|
| 251 | newClass.kind = kind;
|
---|
| 252 | env.add( newClass );
|
---|
| 253 | } // if
|
---|
| 254 | delete type1;
|
---|
| 255 | delete type2;
|
---|
| 256 | return result;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
| 260 | OpenVarSet closedVars;
|
---|
| 261 | findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
|
---|
| 262 | findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
|
---|
| 263 | Type *commonType = 0;
|
---|
| 264 | if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType ) ) {
|
---|
| 265 | if ( commonType ) {
|
---|
| 266 | delete commonType;
|
---|
| 267 | } // if
|
---|
| 268 | return true;
|
---|
| 269 | } else {
|
---|
| 270 | return false;
|
---|
| 271 | } // if
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer, Type *&commonType ) {
|
---|
| 275 | OpenVarSet closedVars;
|
---|
| 276 | findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
|
---|
| 277 | findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
|
---|
| 278 | return unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType );
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
|
---|
[51b73452] | 282 | #ifdef DEBUG
|
---|
[a32b204] | 283 | TypeEnvironment debugEnv( env );
|
---|
[51b73452] | 284 | #endif
|
---|
[eb50842] | 285 | if ( type1->get_qualifiers() != type2->get_qualifiers() ) {
|
---|
| 286 | return false;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[a32b204] | 289 | bool result;
|
---|
| 290 | TypeInstType *var1 = dynamic_cast< TypeInstType* >( type1 );
|
---|
| 291 | TypeInstType *var2 = dynamic_cast< TypeInstType* >( type2 );
|
---|
| 292 | OpenVarSet::const_iterator entry1, entry2;
|
---|
| 293 | if ( var1 ) {
|
---|
| 294 | entry1 = openVars.find( var1->get_name() );
|
---|
| 295 | } // if
|
---|
| 296 | if ( var2 ) {
|
---|
| 297 | entry2 = openVars.find( var2->get_name() );
|
---|
| 298 | } // if
|
---|
| 299 | bool isopen1 = var1 && ( entry1 != openVars.end() );
|
---|
| 300 | bool isopen2 = var2 && ( entry2 != openVars.end() );
|
---|
[eb50842] | 301 |
|
---|
| 302 | if ( isopen1 && isopen2 && entry1->second == entry2->second ) {
|
---|
[a32b204] | 303 | result = bindVarToVar( var1, var2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
| 304 | } else if ( isopen1 ) {
|
---|
| 305 | result = bindVar( var1, type2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
| 306 | } else if ( isopen2 ) {
|
---|
| 307 | result = bindVar( var2, type1, entry2->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
| 308 | } else {
|
---|
| 309 | Unify comparator( type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
| 310 | type1->accept( comparator );
|
---|
| 311 | result = comparator.get_result();
|
---|
| 312 | } // if
|
---|
[51b73452] | 313 | #ifdef DEBUG
|
---|
[a32b204] | 314 | std::cout << "============ unifyExact" << std::endl;
|
---|
| 315 | std::cout << "type1 is ";
|
---|
| 316 | type1->print( std::cout );
|
---|
| 317 | std::cout << std::endl << "type2 is ";
|
---|
| 318 | type2->print( std::cout );
|
---|
| 319 | std::cout << std::endl << "openVars are ";
|
---|
| 320 | printOpenVarSet( openVars, std::cout, 8 );
|
---|
| 321 | std::cout << std::endl << "input env is " << std::endl;
|
---|
| 322 | debugEnv.print( std::cout, 8 );
|
---|
| 323 | std::cout << std::endl << "result env is " << std::endl;
|
---|
| 324 | env.print( std::cout, 8 );
|
---|
| 325 | std::cout << "result is " << result << std::endl;
|
---|
[51b73452] | 326 | #endif
|
---|
[a32b204] | 327 | return result;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
| 331 | return unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common ) {
|
---|
| 335 | Type::Qualifiers tq1 = type1->get_qualifiers(), tq2 = type2->get_qualifiers();
|
---|
| 336 | type1->get_qualifiers() = Type::Qualifiers();
|
---|
| 337 | type2->get_qualifiers() = Type::Qualifiers();
|
---|
| 338 | bool result;
|
---|
[51b73452] | 339 | #ifdef DEBUG
|
---|
[a32b204] | 340 | std::cout << "unifyInexact type 1 is ";
|
---|
| 341 | type1->print( std::cout );
|
---|
| 342 | std::cout << "type 2 is ";
|
---|
| 343 | type2->print( std::cout );
|
---|
| 344 | std::cout << std::endl;
|
---|
[51b73452] | 345 | #endif
|
---|
[a32b204] | 346 | if ( ! unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer ) ) {
|
---|
[51b73452] | 347 | #ifdef DEBUG
|
---|
[a32b204] | 348 | std::cout << "unifyInexact: no exact unification found" << std::endl;
|
---|
[51b73452] | 349 | #endif
|
---|
[a32b204] | 350 | if ( ( common = commonType( type1, type2, widenMode.widenFirst, widenMode.widenSecond, indexer, env, openVars ) ) ) {
|
---|
| 351 | common->get_qualifiers() = tq1 + tq2;
|
---|
[51b73452] | 352 | #ifdef DEBUG
|
---|
[a32b204] | 353 | std::cout << "unifyInexact: common type is ";
|
---|
| 354 | common->print( std::cout );
|
---|
| 355 | std::cout << std::endl;
|
---|
[51b73452] | 356 | #endif
|
---|
[a32b204] | 357 | result = true;
|
---|
| 358 | } else {
|
---|
[51b73452] | 359 | #ifdef DEBUG
|
---|
[a32b204] | 360 | std::cout << "unifyInexact: no common type found" << std::endl;
|
---|
[51b73452] | 361 | #endif
|
---|
[a32b204] | 362 | result = false;
|
---|
| 363 | } // if
|
---|
| 364 | } else {
|
---|
| 365 | if ( tq1 != tq2 ) {
|
---|
| 366 | if ( ( tq1 > tq2 || widenMode.widenFirst ) && ( tq2 > tq1 || widenMode.widenSecond ) ) {
|
---|
| 367 | common = type1->clone();
|
---|
| 368 | common->get_qualifiers() = tq1 + tq2;
|
---|
| 369 | result = true;
|
---|
| 370 | } else {
|
---|
| 371 | result = false;
|
---|
| 372 | } // if
|
---|
| 373 | } else {
|
---|
| 374 | result = true;
|
---|
| 375 | } // if
|
---|
| 376 | } // if
|
---|
| 377 | type1->get_qualifiers() = tq1;
|
---|
| 378 | type2->get_qualifiers() = tq2;
|
---|
| 379 | return result;
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 | Unify::Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer )
|
---|
| 383 | : result( false ), type2( type2 ), env( env ), needAssertions( needAssertions ), haveAssertions( haveAssertions ), openVars( openVars ), widenMode( widenMode ), indexer( indexer ) {
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | void Unify::visit(VoidType *voidType) {
|
---|
| 387 | result = dynamic_cast< VoidType* >( type2 );
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | void Unify::visit(BasicType *basicType) {
|
---|
| 391 | if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) {
|
---|
| 392 | result = basicType->get_kind() == otherBasic->get_kind();
|
---|
| 393 | } // if
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 | void markAssertionSet( AssertionSet &assertions, DeclarationWithType *assert ) {
|
---|
[51b73452] | 397 | /// std::cout << "assertion set is" << std::endl;
|
---|
| 398 | /// printAssertionSet( assertions, std::cout, 8 );
|
---|
| 399 | /// std::cout << "looking for ";
|
---|
| 400 | /// assert->print( std::cout );
|
---|
| 401 | /// std::cout << std::endl;
|
---|
[a32b204] | 402 | AssertionSet::iterator i = assertions.find( assert );
|
---|
| 403 | if ( i != assertions.end() ) {
|
---|
[51b73452] | 404 | /// std::cout << "found it!" << std::endl;
|
---|
[a32b204] | 405 | i->second = true;
|
---|
| 406 | } // if
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | void markAssertions( AssertionSet &assertion1, AssertionSet &assertion2, Type *type ) {
|
---|
| 410 | for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
|
---|
| 411 | for ( std::list< DeclarationWithType* >::const_iterator assert = (*tyvar)->get_assertions().begin(); assert != (*tyvar)->get_assertions().end(); ++assert ) {
|
---|
| 412 | markAssertionSet( assertion1, *assert );
|
---|
| 413 | markAssertionSet( assertion2, *assert );
|
---|
| 414 | } // for
|
---|
| 415 | } // for
|
---|
| 416 | }
|
---|
| 417 |
|
---|
| 418 | void Unify::visit(PointerType *pointerType) {
|
---|
| 419 | if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) {
|
---|
| 420 | result = unifyExact( pointerType->get_base(), otherPointer->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 421 | markAssertions( haveAssertions, needAssertions, pointerType );
|
---|
| 422 | markAssertions( haveAssertions, needAssertions, otherPointer );
|
---|
| 423 | } // if
|
---|
| 424 | }
|
---|
| 425 |
|
---|
| 426 | void Unify::visit(ArrayType *arrayType) {
|
---|
| 427 | // XXX -- compare array dimension
|
---|
| 428 | ArrayType *otherArray = dynamic_cast< ArrayType* >( type2 );
|
---|
| 429 | if ( otherArray && arrayType->get_isVarLen() == otherArray->get_isVarLen() ) {
|
---|
| 430 | result = unifyExact( arrayType->get_base(), otherArray->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 431 | } // if
|
---|
| 432 | }
|
---|
| 433 |
|
---|
| 434 | template< typename Iterator1, typename Iterator2 >
|
---|
| 435 | bool unifyDeclList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
| 436 | for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
|
---|
| 437 | if ( ! unifyExact( (*list1Begin)->get_type(), (*list2Begin)->get_type(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
|
---|
| 438 | return false;
|
---|
| 439 | } // if
|
---|
| 440 | } // for
|
---|
| 441 | if ( list1Begin != list1End || list2Begin != list2End ) {
|
---|
| 442 | return false;
|
---|
| 443 | } else {
|
---|
| 444 | return true;
|
---|
| 445 | } // if
|
---|
| 446 | }
|
---|
| 447 |
|
---|
| 448 | void Unify::visit(FunctionType *functionType) {
|
---|
| 449 | FunctionType *otherFunction = dynamic_cast< FunctionType* >( type2 );
|
---|
| 450 | if ( otherFunction && functionType->get_isVarArgs() == otherFunction->get_isVarArgs() ) {
|
---|
[51b73452] | 451 |
|
---|
[a32b204] | 452 | if ( unifyDeclList( functionType->get_parameters().begin(), functionType->get_parameters().end(), otherFunction->get_parameters().begin(), otherFunction->get_parameters().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
|
---|
| 453 |
|
---|
| 454 | if ( unifyDeclList( functionType->get_returnVals().begin(), functionType->get_returnVals().end(), otherFunction->get_returnVals().begin(), otherFunction->get_returnVals().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
|
---|
| 455 |
|
---|
| 456 | markAssertions( haveAssertions, needAssertions, functionType );
|
---|
| 457 | markAssertions( haveAssertions, needAssertions, otherFunction );
|
---|
| 458 |
|
---|
| 459 | result = true;
|
---|
| 460 | } // if
|
---|
| 461 | } // if
|
---|
| 462 | } // if
|
---|
| 463 | }
|
---|
| 464 |
|
---|
| 465 | template< typename RefType >
|
---|
| 466 | void Unify::handleRefType( RefType *inst, Type *other ) {
|
---|
| 467 | RefType *otherStruct = dynamic_cast< RefType* >( other );
|
---|
| 468 | result = otherStruct && inst->get_name() == otherStruct->get_name();
|
---|
| 469 | }
|
---|
| 470 |
|
---|
| 471 | void Unify::visit(StructInstType *structInst) {
|
---|
| 472 | handleRefType( structInst, type2 );
|
---|
| 473 | }
|
---|
| 474 |
|
---|
| 475 | void Unify::visit(UnionInstType *unionInst) {
|
---|
| 476 | handleRefType( unionInst, type2 );
|
---|
| 477 | }
|
---|
| 478 |
|
---|
| 479 | void Unify::visit(EnumInstType *enumInst) {
|
---|
| 480 | handleRefType( enumInst, type2 );
|
---|
| 481 | }
|
---|
| 482 |
|
---|
| 483 | void Unify::visit(ContextInstType *contextInst) {
|
---|
| 484 | handleRefType( contextInst, type2 );
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | void Unify::visit(TypeInstType *typeInst) {
|
---|
| 488 | assert( openVars.find( typeInst->get_name() ) == openVars.end() );
|
---|
| 489 | TypeInstType *otherInst = dynamic_cast< TypeInstType* >( type2 );
|
---|
| 490 | if ( otherInst && typeInst->get_name() == otherInst->get_name() ) {
|
---|
| 491 | result = true;
|
---|
[51b73452] | 492 | /// } else {
|
---|
| 493 | /// NamedTypeDecl *nt = indexer.lookupType( typeInst->get_name() );
|
---|
[a32b204] | 494 | /// if ( nt ) {
|
---|
[51b73452] | 495 | /// TypeDecl *type = dynamic_cast< TypeDecl* >( nt );
|
---|
| 496 | /// assert( type );
|
---|
[a32b204] | 497 | /// if ( type->get_base() ) {
|
---|
[51b73452] | 498 | /// result = unifyExact( type->get_base(), typeInst, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 499 | /// }
|
---|
| 500 | /// }
|
---|
[a32b204] | 501 | } // if
|
---|
| 502 | }
|
---|
| 503 |
|
---|
| 504 | template< typename Iterator1, typename Iterator2 >
|
---|
| 505 | 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 ) {
|
---|
| 506 | for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
|
---|
| 507 | Type *commonType = 0;
|
---|
| 508 | if ( ! unifyInexact( *list1Begin, *list2Begin, env, needAssertions, haveAssertions, openVars, widenMode, indexer, commonType ) ) {
|
---|
| 509 | return false;
|
---|
| 510 | }
|
---|
| 511 | delete commonType;
|
---|
| 512 | } // for
|
---|
| 513 | if ( list1Begin != list1End || list2Begin != list2End ) {
|
---|
| 514 | return false;
|
---|
| 515 | } else {
|
---|
| 516 | return true;
|
---|
| 517 | } //if
|
---|
| 518 | }
|
---|
| 519 |
|
---|
| 520 | void Unify::visit(TupleType *tupleType) {
|
---|
| 521 | if ( TupleType *otherTuple = dynamic_cast< TupleType* >( type2 ) ) {
|
---|
| 522 | result = unifyList( tupleType->get_types().begin(), tupleType->get_types().end(), otherTuple->get_types().begin(), otherTuple->get_types().end(), env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
| 523 | } // if
|
---|
| 524 | }
|
---|
[51b73452] | 525 |
|
---|
| 526 | } // namespace ResolvExpr
|
---|
[a32b204] | 527 |
|
---|
| 528 | // Local Variables: //
|
---|
| 529 | // tab-width: 4 //
|
---|
| 530 | // mode: c++ //
|
---|
| 531 | // compile-command: "make install" //
|
---|
| 532 | // End: //
|
---|