[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
|
---|
[6f95000] | 12 | // Last Modified On : Thu Mar 16 16:22:54 2017
|
---|
| 13 | // Update Count : 42
|
---|
[a32b204] | 14 | //
|
---|
[51b73452] | 15 |
|
---|
[ea6332d] | 16 | #include <cassert> // for assertf, assert
|
---|
| 17 | #include <iterator> // for back_insert_iterator, back_inserter
|
---|
| 18 | #include <map> // for _Rb_tree_const_iterator, _Rb_tree_i...
|
---|
| 19 | #include <memory> // for unique_ptr, auto_ptr
|
---|
| 20 | #include <set> // for set
|
---|
| 21 | #include <string> // for string, operator==, operator!=, bas...
|
---|
| 22 | #include <utility> // for pair
|
---|
[51b73452] | 23 |
|
---|
[3096ec1] | 24 | #include "Common/PassVisitor.h" // for PassVisitor
|
---|
[ea6332d] | 25 | #include "FindOpenVars.h" // for findOpenVars
|
---|
| 26 | #include "Parser/LinkageSpec.h" // for C
|
---|
| 27 | #include "SynTree/Constant.h" // for Constant
|
---|
| 28 | #include "SynTree/Declaration.h" // for TypeDecl, TypeDecl::Data, Declarati...
|
---|
| 29 | #include "SynTree/Expression.h" // for TypeExpr, Expression, ConstantExpr
|
---|
| 30 | #include "SynTree/Mutator.h" // for Mutator
|
---|
| 31 | #include "SynTree/Type.h" // for Type, TypeInstType, FunctionType
|
---|
| 32 | #include "SynTree/Visitor.h" // for Visitor
|
---|
| 33 | #include "Tuples/Tuples.h" // for isTtype
|
---|
| 34 | #include "TypeEnvironment.h" // for EqvClass, AssertionSet, OpenVarSet
|
---|
[51b73452] | 35 | #include "Unify.h"
|
---|
[ea6332d] | 36 | #include "typeops.h" // for flatten, occurs, commonType
|
---|
| 37 |
|
---|
| 38 | namespace SymTab {
|
---|
| 39 | class Indexer;
|
---|
| 40 | } // namespace SymTab
|
---|
[51b73452] | 41 |
|
---|
[1cbca6e] | 42 | // #define DEBUG
|
---|
[51b73452] | 43 |
|
---|
| 44 | namespace ResolvExpr {
|
---|
| 45 |
|
---|
[a32b204] | 46 | class Unify : public Visitor {
|
---|
| 47 | public:
|
---|
| 48 | Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
|
---|
[41a2620] | 49 |
|
---|
[a32b204] | 50 | bool get_result() const { return result; }
|
---|
| 51 | private:
|
---|
| 52 | virtual void visit(VoidType *voidType);
|
---|
| 53 | virtual void visit(BasicType *basicType);
|
---|
| 54 | virtual void visit(PointerType *pointerType);
|
---|
| 55 | virtual void visit(ArrayType *arrayType);
|
---|
[ce8c12f] | 56 | virtual void visit(ReferenceType *refType);
|
---|
[a32b204] | 57 | virtual void visit(FunctionType *functionType);
|
---|
| 58 | virtual void visit(StructInstType *aggregateUseType);
|
---|
| 59 | virtual void visit(UnionInstType *aggregateUseType);
|
---|
| 60 | virtual void visit(EnumInstType *aggregateUseType);
|
---|
[4040425] | 61 | virtual void visit(TraitInstType *aggregateUseType);
|
---|
[a32b204] | 62 | virtual void visit(TypeInstType *aggregateUseType);
|
---|
| 63 | virtual void visit(TupleType *tupleType);
|
---|
[44b7088] | 64 | virtual void visit(VarArgsType *varArgsType);
|
---|
[89e6ffc] | 65 | virtual void visit(ZeroType *zeroType);
|
---|
| 66 | virtual void visit(OneType *oneType);
|
---|
[a32b204] | 67 |
|
---|
| 68 | template< typename RefType > void handleRefType( RefType *inst, Type *other );
|
---|
[02ec390] | 69 | template< typename RefType > void handleGenericRefType( RefType *inst, Type *other );
|
---|
[a32b204] | 70 |
|
---|
| 71 | bool result;
|
---|
| 72 | Type *type2; // inherited
|
---|
| 73 | TypeEnvironment &env;
|
---|
| 74 | AssertionSet &needAssertions;
|
---|
| 75 | AssertionSet &haveAssertions;
|
---|
| 76 | const OpenVarSet &openVars;
|
---|
| 77 | WidenMode widenMode;
|
---|
| 78 | const SymTab::Indexer &indexer;
|
---|
| 79 | };
|
---|
| 80 |
|
---|
[eb50842] | 81 | /// Attempts an inexact unification of type1 and type2.
|
---|
| 82 | /// Returns false if no such unification; if the types can be unified, sets common (unless they unify exactly and have identical type qualifiers)
|
---|
[a32b204] | 83 | bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common );
|
---|
| 84 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
|
---|
[41a2620] | 85 |
|
---|
[a32b204] | 86 | bool typesCompatible( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
|
---|
| 87 | TypeEnvironment newEnv;
|
---|
[1cbca6e] | 88 | OpenVarSet openVars, closedVars; // added closedVars
|
---|
[a32b204] | 89 | AssertionSet needAssertions, haveAssertions;
|
---|
| 90 | Type *newFirst = first->clone(), *newSecond = second->clone();
|
---|
| 91 | env.apply( newFirst );
|
---|
| 92 | env.apply( newSecond );
|
---|
[1cbca6e] | 93 |
|
---|
| 94 | // do we need to do this? Seems like we do, types should be able to be compatible if they
|
---|
| 95 | // have free variables that can unify
|
---|
| 96 | findOpenVars( newFirst, openVars, closedVars, needAssertions, haveAssertions, false );
|
---|
| 97 | findOpenVars( newSecond, openVars, closedVars, needAssertions, haveAssertions, true );
|
---|
| 98 |
|
---|
[a32b204] | 99 | bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 100 | delete newFirst;
|
---|
| 101 | delete newSecond;
|
---|
| 102 | return result;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | bool typesCompatibleIgnoreQualifiers( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
|
---|
| 106 | TypeEnvironment newEnv;
|
---|
| 107 | OpenVarSet openVars;
|
---|
| 108 | AssertionSet needAssertions, haveAssertions;
|
---|
| 109 | Type *newFirst = first->clone(), *newSecond = second->clone();
|
---|
| 110 | env.apply( newFirst );
|
---|
| 111 | env.apply( newSecond );
|
---|
| 112 | newFirst->get_qualifiers() = Type::Qualifiers();
|
---|
| 113 | newSecond->get_qualifiers() = Type::Qualifiers();
|
---|
[2c57025] | 114 | /// std::cerr << "first is ";
|
---|
| 115 | /// first->print( std::cerr );
|
---|
| 116 | /// std::cerr << std::endl << "second is ";
|
---|
| 117 | /// second->print( std::cerr );
|
---|
| 118 | /// std::cerr << std::endl << "newFirst is ";
|
---|
| 119 | /// newFirst->print( std::cerr );
|
---|
| 120 | /// std::cerr << std::endl << "newSecond is ";
|
---|
| 121 | /// newSecond->print( std::cerr );
|
---|
| 122 | /// std::cerr << std::endl;
|
---|
[a32b204] | 123 | bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 124 | delete newFirst;
|
---|
| 125 | delete newSecond;
|
---|
| 126 | return result;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[d7dc824] | 129 | bool isFtype( Type *type ) {
|
---|
[a32b204] | 130 | if ( dynamic_cast< FunctionType* >( type ) ) {
|
---|
| 131 | return true;
|
---|
| 132 | } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) {
|
---|
| 133 | return typeInst->get_isFtype();
|
---|
| 134 | } // if
|
---|
| 135 | return false;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[d7dc824] | 138 | bool tyVarCompatible( const TypeDecl::Data & data, Type *type ) {
|
---|
[2c57025] | 139 | switch ( data.kind ) {
|
---|
[a32b204] | 140 | case TypeDecl::Any:
|
---|
| 141 | case TypeDecl::Dtype:
|
---|
[2c57025] | 142 | // to bind to an object type variable, the type must not be a function type.
|
---|
| 143 | // if the type variable is specified to be a complete type then the incoming
|
---|
| 144 | // type must also be complete
|
---|
[6c3a988f] | 145 | // xxx - should this also check that type is not a tuple type and that it's not a ttype?
|
---|
[d7dc824] | 146 | return ! isFtype( type ) && (! data.isComplete || type->isComplete() );
|
---|
[a32b204] | 147 | case TypeDecl::Ftype:
|
---|
[d7dc824] | 148 | return isFtype( type );
|
---|
[b2daebd4] | 149 | case TypeDecl::Ttype:
|
---|
[6c3a988f] | 150 | // ttype unifies with any tuple type
|
---|
[f3b0a07] | 151 | return dynamic_cast< TupleType * >( type ) || Tuples::isTtype( type );
|
---|
[a32b204] | 152 | } // switch
|
---|
| 153 | return false;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[2c57025] | 156 | bool bindVar( TypeInstType *typeInst, Type *other, const TypeDecl::Data & data, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
|
---|
[36a5a77] | 157 | // remove references from other, so that type variables can only bind to value types
|
---|
| 158 | other = other->stripReferences();
|
---|
[a32b204] | 159 | OpenVarSet::const_iterator tyvar = openVars.find( typeInst->get_name() );
|
---|
| 160 | assert( tyvar != openVars.end() );
|
---|
[d7dc824] | 161 | if ( ! tyVarCompatible( tyvar->second, other ) ) {
|
---|
[a32b204] | 162 | return false;
|
---|
| 163 | } // if
|
---|
| 164 | if ( occurs( other, typeInst->get_name(), env ) ) {
|
---|
| 165 | return false;
|
---|
| 166 | } // if
|
---|
| 167 | EqvClass curClass;
|
---|
| 168 | if ( env.lookup( typeInst->get_name(), curClass ) ) {
|
---|
| 169 | if ( curClass.type ) {
|
---|
| 170 | Type *common = 0;
|
---|
[eb50842] | 171 | // attempt to unify equivalence class type (which has qualifiers stripped, so they must be restored) with the type to bind to
|
---|
[a32b204] | 172 | std::auto_ptr< Type > newType( curClass.type->clone() );
|
---|
[721f17a] | 173 | newType->get_qualifiers() = typeInst->get_qualifiers();
|
---|
[a32b204] | 174 | if ( unifyInexact( newType.get(), other, env, needAssertions, haveAssertions, openVars, widenMode & WidenMode( curClass.allowWidening, true ), indexer, common ) ) {
|
---|
| 175 | if ( common ) {
|
---|
| 176 | common->get_qualifiers() = Type::Qualifiers();
|
---|
| 177 | delete curClass.type;
|
---|
| 178 | curClass.type = common;
|
---|
| 179 | env.add( curClass );
|
---|
| 180 | } // if
|
---|
| 181 | return true;
|
---|
| 182 | } else {
|
---|
| 183 | return false;
|
---|
| 184 | } // if
|
---|
| 185 | } else {
|
---|
| 186 | curClass.type = other->clone();
|
---|
| 187 | curClass.type->get_qualifiers() = Type::Qualifiers();
|
---|
| 188 | curClass.allowWidening = widenMode.widenFirst && widenMode.widenSecond;
|
---|
| 189 | env.add( curClass );
|
---|
| 190 | } // if
|
---|
| 191 | } else {
|
---|
| 192 | EqvClass newClass;
|
---|
| 193 | newClass.vars.insert( typeInst->get_name() );
|
---|
| 194 | newClass.type = other->clone();
|
---|
| 195 | newClass.type->get_qualifiers() = Type::Qualifiers();
|
---|
| 196 | newClass.allowWidening = widenMode.widenFirst && widenMode.widenSecond;
|
---|
[2c57025] | 197 | newClass.data = data;
|
---|
[a32b204] | 198 | env.add( newClass );
|
---|
| 199 | } // if
|
---|
| 200 | return true;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[2c57025] | 203 | bool bindVarToVar( TypeInstType *var1, TypeInstType *var2, const TypeDecl::Data & data, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
|
---|
[a32b204] | 204 | bool result = true;
|
---|
| 205 | EqvClass class1, class2;
|
---|
| 206 | bool hasClass1 = false, hasClass2 = false;
|
---|
| 207 | bool widen1 = false, widen2 = false;
|
---|
| 208 | Type *type1 = 0, *type2 = 0;
|
---|
[41a2620] | 209 |
|
---|
[a32b204] | 210 | if ( env.lookup( var1->get_name(), class1 ) ) {
|
---|
| 211 | hasClass1 = true;
|
---|
| 212 | if ( class1.type ) {
|
---|
| 213 | if ( occurs( class1.type, var2->get_name(), env ) ) {
|
---|
| 214 | return false;
|
---|
| 215 | } // if
|
---|
| 216 | type1 = class1.type->clone();
|
---|
| 217 | } // if
|
---|
| 218 | widen1 = widenMode.widenFirst && class1.allowWidening;
|
---|
| 219 | } // if
|
---|
| 220 | if ( env.lookup( var2->get_name(), class2 ) ) {
|
---|
| 221 | hasClass2 = true;
|
---|
| 222 | if ( class2.type ) {
|
---|
| 223 | if ( occurs( class2.type, var1->get_name(), env ) ) {
|
---|
| 224 | return false;
|
---|
| 225 | } // if
|
---|
| 226 | type2 = class2.type->clone();
|
---|
| 227 | } // if
|
---|
| 228 | widen2 = widenMode.widenSecond && class2.allowWidening;
|
---|
| 229 | } // if
|
---|
[41a2620] | 230 |
|
---|
[a32b204] | 231 | if ( type1 && type2 ) {
|
---|
[2c57025] | 232 | // std::cerr << "has type1 && type2" << std::endl;
|
---|
[a32b204] | 233 | WidenMode newWidenMode ( widen1, widen2 );
|
---|
| 234 | Type *common = 0;
|
---|
| 235 | if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, newWidenMode, indexer, common ) ) {
|
---|
| 236 | class1.vars.insert( class2.vars.begin(), class2.vars.end() );
|
---|
| 237 | class1.allowWidening = widen1 && widen2;
|
---|
| 238 | if ( common ) {
|
---|
| 239 | common->get_qualifiers() = Type::Qualifiers();
|
---|
| 240 | delete class1.type;
|
---|
| 241 | class1.type = common;
|
---|
| 242 | } // if
|
---|
| 243 | env.add( class1 );
|
---|
| 244 | } else {
|
---|
| 245 | result = false;
|
---|
| 246 | } // if
|
---|
| 247 | } else if ( hasClass1 && hasClass2 ) {
|
---|
| 248 | if ( type1 ) {
|
---|
| 249 | class1.vars.insert( class2.vars.begin(), class2.vars.end() );
|
---|
| 250 | class1.allowWidening = widen1;
|
---|
| 251 | env.add( class1 );
|
---|
| 252 | } else {
|
---|
| 253 | class2.vars.insert( class1.vars.begin(), class1.vars.end() );
|
---|
| 254 | class2.allowWidening = widen2;
|
---|
| 255 | env.add( class2 );
|
---|
| 256 | } // if
|
---|
| 257 | } else if ( hasClass1 ) {
|
---|
| 258 | class1.vars.insert( var2->get_name() );
|
---|
| 259 | class1.allowWidening = widen1;
|
---|
| 260 | env.add( class1 );
|
---|
| 261 | } else if ( hasClass2 ) {
|
---|
| 262 | class2.vars.insert( var1->get_name() );
|
---|
| 263 | class2.allowWidening = widen2;
|
---|
| 264 | env.add( class2 );
|
---|
| 265 | } else {
|
---|
| 266 | EqvClass newClass;
|
---|
| 267 | newClass.vars.insert( var1->get_name() );
|
---|
| 268 | newClass.vars.insert( var2->get_name() );
|
---|
| 269 | newClass.allowWidening = widen1 && widen2;
|
---|
[2c57025] | 270 | newClass.data = data;
|
---|
[a32b204] | 271 | env.add( newClass );
|
---|
| 272 | } // if
|
---|
| 273 | delete type1;
|
---|
| 274 | delete type2;
|
---|
| 275 | return result;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
| 279 | OpenVarSet closedVars;
|
---|
| 280 | findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
|
---|
| 281 | findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
|
---|
| 282 | Type *commonType = 0;
|
---|
| 283 | if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType ) ) {
|
---|
| 284 | if ( commonType ) {
|
---|
| 285 | delete commonType;
|
---|
| 286 | } // if
|
---|
| 287 | return true;
|
---|
| 288 | } else {
|
---|
| 289 | return false;
|
---|
| 290 | } // if
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer, Type *&commonType ) {
|
---|
| 294 | OpenVarSet closedVars;
|
---|
| 295 | findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
|
---|
| 296 | findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
|
---|
| 297 | return unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType );
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
|
---|
[51b73452] | 301 | #ifdef DEBUG
|
---|
[a32b204] | 302 | TypeEnvironment debugEnv( env );
|
---|
[51b73452] | 303 | #endif
|
---|
[eb50842] | 304 | if ( type1->get_qualifiers() != type2->get_qualifiers() ) {
|
---|
| 305 | return false;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
[a32b204] | 308 | bool result;
|
---|
| 309 | TypeInstType *var1 = dynamic_cast< TypeInstType* >( type1 );
|
---|
| 310 | TypeInstType *var2 = dynamic_cast< TypeInstType* >( type2 );
|
---|
| 311 | OpenVarSet::const_iterator entry1, entry2;
|
---|
| 312 | if ( var1 ) {
|
---|
| 313 | entry1 = openVars.find( var1->get_name() );
|
---|
| 314 | } // if
|
---|
| 315 | if ( var2 ) {
|
---|
| 316 | entry2 = openVars.find( var2->get_name() );
|
---|
| 317 | } // if
|
---|
| 318 | bool isopen1 = var1 && ( entry1 != openVars.end() );
|
---|
| 319 | bool isopen2 = var2 && ( entry2 != openVars.end() );
|
---|
[eb50842] | 320 |
|
---|
| 321 | if ( isopen1 && isopen2 && entry1->second == entry2->second ) {
|
---|
[a32b204] | 322 | result = bindVarToVar( var1, var2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
| 323 | } else if ( isopen1 ) {
|
---|
| 324 | result = bindVar( var1, type2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
| 325 | } else if ( isopen2 ) {
|
---|
| 326 | result = bindVar( var2, type1, entry2->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
| 327 | } else {
|
---|
| 328 | Unify comparator( type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
| 329 | type1->accept( comparator );
|
---|
| 330 | result = comparator.get_result();
|
---|
| 331 | } // if
|
---|
[51b73452] | 332 | #ifdef DEBUG
|
---|
[2c57025] | 333 | std::cerr << "============ unifyExact" << std::endl;
|
---|
| 334 | std::cerr << "type1 is ";
|
---|
| 335 | type1->print( std::cerr );
|
---|
| 336 | std::cerr << std::endl << "type2 is ";
|
---|
| 337 | type2->print( std::cerr );
|
---|
| 338 | std::cerr << std::endl << "openVars are ";
|
---|
| 339 | printOpenVarSet( openVars, std::cerr, 8 );
|
---|
| 340 | std::cerr << std::endl << "input env is " << std::endl;
|
---|
| 341 | debugEnv.print( std::cerr, 8 );
|
---|
| 342 | std::cerr << std::endl << "result env is " << std::endl;
|
---|
| 343 | env.print( std::cerr, 8 );
|
---|
| 344 | std::cerr << "result is " << result << std::endl;
|
---|
[51b73452] | 345 | #endif
|
---|
[a32b204] | 346 | return result;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
| 350 | return unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common ) {
|
---|
| 354 | Type::Qualifiers tq1 = type1->get_qualifiers(), tq2 = type2->get_qualifiers();
|
---|
| 355 | type1->get_qualifiers() = Type::Qualifiers();
|
---|
| 356 | type2->get_qualifiers() = Type::Qualifiers();
|
---|
| 357 | bool result;
|
---|
[51b73452] | 358 | #ifdef DEBUG
|
---|
[2c57025] | 359 | std::cerr << "unifyInexact type 1 is ";
|
---|
| 360 | type1->print( std::cerr );
|
---|
[0b150ec] | 361 | std::cerr << " type 2 is ";
|
---|
[2c57025] | 362 | type2->print( std::cerr );
|
---|
| 363 | std::cerr << std::endl;
|
---|
[51b73452] | 364 | #endif
|
---|
[a32b204] | 365 | if ( ! unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer ) ) {
|
---|
[51b73452] | 366 | #ifdef DEBUG
|
---|
[2c57025] | 367 | std::cerr << "unifyInexact: no exact unification found" << std::endl;
|
---|
[51b73452] | 368 | #endif
|
---|
[a32b204] | 369 | if ( ( common = commonType( type1, type2, widenMode.widenFirst, widenMode.widenSecond, indexer, env, openVars ) ) ) {
|
---|
[6f95000] | 370 | common->get_qualifiers() = tq1 | tq2;
|
---|
[51b73452] | 371 | #ifdef DEBUG
|
---|
[2c57025] | 372 | std::cerr << "unifyInexact: common type is ";
|
---|
| 373 | common->print( std::cerr );
|
---|
| 374 | std::cerr << std::endl;
|
---|
[51b73452] | 375 | #endif
|
---|
[a32b204] | 376 | result = true;
|
---|
| 377 | } else {
|
---|
[51b73452] | 378 | #ifdef DEBUG
|
---|
[2c57025] | 379 | std::cerr << "unifyInexact: no common type found" << std::endl;
|
---|
[51b73452] | 380 | #endif
|
---|
[a32b204] | 381 | result = false;
|
---|
| 382 | } // if
|
---|
| 383 | } else {
|
---|
| 384 | if ( tq1 != tq2 ) {
|
---|
| 385 | if ( ( tq1 > tq2 || widenMode.widenFirst ) && ( tq2 > tq1 || widenMode.widenSecond ) ) {
|
---|
| 386 | common = type1->clone();
|
---|
[6f95000] | 387 | common->get_qualifiers() = tq1 | tq2;
|
---|
[a32b204] | 388 | result = true;
|
---|
| 389 | } else {
|
---|
| 390 | result = false;
|
---|
| 391 | } // if
|
---|
| 392 | } else {
|
---|
[e6cee92] | 393 | common = type1->clone();
|
---|
| 394 | common->get_qualifiers() = tq1 | tq2;
|
---|
[a32b204] | 395 | result = true;
|
---|
| 396 | } // if
|
---|
| 397 | } // if
|
---|
| 398 | type1->get_qualifiers() = tq1;
|
---|
| 399 | type2->get_qualifiers() = tq2;
|
---|
| 400 | return result;
|
---|
| 401 | }
|
---|
| 402 |
|
---|
| 403 | Unify::Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer )
|
---|
| 404 | : result( false ), type2( type2 ), env( env ), needAssertions( needAssertions ), haveAssertions( haveAssertions ), openVars( openVars ), widenMode( widenMode ), indexer( indexer ) {
|
---|
| 405 | }
|
---|
| 406 |
|
---|
[af397ef8] | 407 | void Unify::visit( __attribute__((unused)) VoidType *voidType) {
|
---|
[a32b204] | 408 | result = dynamic_cast< VoidType* >( type2 );
|
---|
| 409 | }
|
---|
| 410 |
|
---|
| 411 | void Unify::visit(BasicType *basicType) {
|
---|
| 412 | if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) {
|
---|
| 413 | result = basicType->get_kind() == otherBasic->get_kind();
|
---|
| 414 | } // if
|
---|
| 415 | }
|
---|
| 416 |
|
---|
| 417 | void markAssertionSet( AssertionSet &assertions, DeclarationWithType *assert ) {
|
---|
[2c57025] | 418 | /// std::cerr << "assertion set is" << std::endl;
|
---|
| 419 | /// printAssertionSet( assertions, std::cerr, 8 );
|
---|
| 420 | /// std::cerr << "looking for ";
|
---|
| 421 | /// assert->print( std::cerr );
|
---|
| 422 | /// std::cerr << std::endl;
|
---|
[a32b204] | 423 | AssertionSet::iterator i = assertions.find( assert );
|
---|
| 424 | if ( i != assertions.end() ) {
|
---|
[2c57025] | 425 | /// std::cerr << "found it!" << std::endl;
|
---|
[6c3a988f] | 426 | i->second.isUsed = true;
|
---|
[a32b204] | 427 | } // if
|
---|
| 428 | }
|
---|
| 429 |
|
---|
| 430 | void markAssertions( AssertionSet &assertion1, AssertionSet &assertion2, Type *type ) {
|
---|
[aefcc3b] | 431 | for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
|
---|
[a32b204] | 432 | for ( std::list< DeclarationWithType* >::const_iterator assert = (*tyvar)->get_assertions().begin(); assert != (*tyvar)->get_assertions().end(); ++assert ) {
|
---|
| 433 | markAssertionSet( assertion1, *assert );
|
---|
| 434 | markAssertionSet( assertion2, *assert );
|
---|
| 435 | } // for
|
---|
| 436 | } // for
|
---|
| 437 | }
|
---|
| 438 |
|
---|
| 439 | void Unify::visit(PointerType *pointerType) {
|
---|
| 440 | if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) {
|
---|
| 441 | result = unifyExact( pointerType->get_base(), otherPointer->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 442 | markAssertions( haveAssertions, needAssertions, pointerType );
|
---|
| 443 | markAssertions( haveAssertions, needAssertions, otherPointer );
|
---|
| 444 | } // if
|
---|
| 445 | }
|
---|
| 446 |
|
---|
[ce8c12f] | 447 | void Unify::visit(ReferenceType *refType) {
|
---|
| 448 | if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) {
|
---|
| 449 | result = unifyExact( refType->get_base(), otherRef->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 450 | markAssertions( haveAssertions, needAssertions, refType );
|
---|
| 451 | markAssertions( haveAssertions, needAssertions, otherRef );
|
---|
| 452 | } // if
|
---|
| 453 | }
|
---|
| 454 |
|
---|
[a32b204] | 455 | void Unify::visit(ArrayType *arrayType) {
|
---|
| 456 | ArrayType *otherArray = dynamic_cast< ArrayType* >( type2 );
|
---|
[1cbca6e] | 457 | // to unify, array types must both be VLA or both not VLA
|
---|
| 458 | // and must both have a dimension expression or not have a dimension
|
---|
[41a2620] | 459 | if ( otherArray && arrayType->get_isVarLen() == otherArray->get_isVarLen() ) {
|
---|
[1cbca6e] | 460 |
|
---|
| 461 | // not positive this is correct in all cases, but it's needed for typedefs
|
---|
| 462 | if ( arrayType->get_isVarLen() || otherArray->get_isVarLen() ) {
|
---|
| 463 | return;
|
---|
| 464 | }
|
---|
| 465 |
|
---|
| 466 | if ( ! arrayType->get_isVarLen() && ! otherArray->get_isVarLen() &&
|
---|
| 467 | arrayType->get_dimension() != 0 && otherArray->get_dimension() != 0 ) {
|
---|
| 468 | ConstantExpr * ce1 = dynamic_cast< ConstantExpr * >( arrayType->get_dimension() );
|
---|
| 469 | ConstantExpr * ce2 = dynamic_cast< ConstantExpr * >( otherArray->get_dimension() );
|
---|
[41a2620] | 470 | // see C11 Reference Manual 6.7.6.2.6
|
---|
| 471 | // two array types with size specifiers that are integer constant expressions are
|
---|
| 472 | // compatible if both size specifiers have the same constant value
|
---|
| 473 | if ( ce1 && ce2 ) {
|
---|
| 474 | Constant * c1 = ce1->get_constant();
|
---|
| 475 | Constant * c2 = ce2->get_constant();
|
---|
| 476 |
|
---|
| 477 | if ( c1->get_value() != c2->get_value() ) {
|
---|
| 478 | // does not unify if the dimension is different
|
---|
| 479 | return;
|
---|
| 480 | }
|
---|
[1cbca6e] | 481 | }
|
---|
| 482 | }
|
---|
| 483 |
|
---|
[a32b204] | 484 | result = unifyExact( arrayType->get_base(), otherArray->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 485 | } // if
|
---|
| 486 | }
|
---|
| 487 |
|
---|
[f3b0a07] | 488 | template< typename Iterator, typename Func >
|
---|
| 489 | std::unique_ptr<Type> combineTypes( Iterator begin, Iterator end, Func & toType ) {
|
---|
[53e3b4a] | 490 | std::list< Type * > types;
|
---|
| 491 | for ( ; begin != end; ++begin ) {
|
---|
[64eae56] | 492 | // it's guaranteed that a ttype variable will be bound to a flat tuple, so ensure that this results in a flat tuple
|
---|
[f3b0a07] | 493 | flatten( toType( *begin ), back_inserter( types ) );
|
---|
[53e3b4a] | 494 | }
|
---|
[6c3a988f] | 495 | return std::unique_ptr<Type>( new TupleType( Type::Qualifiers(), types ) );
|
---|
[53e3b4a] | 496 | }
|
---|
| 497 |
|
---|
[a32b204] | 498 | template< typename Iterator1, typename Iterator2 >
|
---|
| 499 | bool unifyDeclList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
[f3b0a07] | 500 | auto get_type = [](DeclarationWithType * dwt){ return dwt->get_type(); };
|
---|
[a32b204] | 501 | for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
|
---|
[53e3b4a] | 502 | Type * t1 = (*list1Begin)->get_type();
|
---|
| 503 | Type * t2 = (*list2Begin)->get_type();
|
---|
| 504 | bool isTtype1 = Tuples::isTtype( t1 );
|
---|
| 505 | bool isTtype2 = Tuples::isTtype( t2 );
|
---|
[6c3a988f] | 506 | // xxx - assumes ttype must be last parameter
|
---|
| 507 | // xxx - there may be a nice way to refactor this, but be careful because the argument positioning might matter in some cases.
|
---|
[53e3b4a] | 508 | if ( isTtype1 && ! isTtype2 ) {
|
---|
| 509 | // combine all of the things in list2, then unify
|
---|
[f3b0a07] | 510 | return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
[53e3b4a] | 511 | } else if ( isTtype2 && ! isTtype1 ) {
|
---|
| 512 | // combine all of the things in list1, then unify
|
---|
[f3b0a07] | 513 | return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
[53e3b4a] | 514 | } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
|
---|
[a32b204] | 515 | return false;
|
---|
| 516 | } // if
|
---|
| 517 | } // for
|
---|
[6c3a988f] | 518 | // may get to the end of one argument list before the end of the other. This is only okay when the other is a ttype
|
---|
[4c8621ac] | 519 | if ( list1Begin != list1End ) {
|
---|
| 520 | // try unifying empty tuple type with ttype
|
---|
| 521 | Type * t1 = (*list1Begin)->get_type();
|
---|
| 522 | if ( Tuples::isTtype( t1 ) ) {
|
---|
[f3b0a07] | 523 | return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
[4c8621ac] | 524 | } else return false;
|
---|
| 525 | } else if ( list2Begin != list2End ) {
|
---|
| 526 | // try unifying empty tuple type with ttype
|
---|
| 527 | Type * t2 = (*list2Begin)->get_type();
|
---|
| 528 | if ( Tuples::isTtype( t2 ) ) {
|
---|
[f3b0a07] | 529 | return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
[4c8621ac] | 530 | } else return false;
|
---|
[a32b204] | 531 | } else {
|
---|
| 532 | return true;
|
---|
| 533 | } // if
|
---|
| 534 | }
|
---|
| 535 |
|
---|
[6c3a988f] | 536 | /// Finds ttypes and replaces them with their expansion, if known.
|
---|
| 537 | /// This needs to be done so that satisfying ttype assertions is easier.
|
---|
| 538 | /// If this isn't done then argument lists can have wildly different
|
---|
| 539 | /// size and structure, when they should be compatible.
|
---|
[3096ec1] | 540 | struct TtypeExpander : public WithShortCircuiting {
|
---|
| 541 | TypeEnvironment & tenv;
|
---|
| 542 | TtypeExpander( TypeEnvironment & tenv ) : tenv( tenv ) {}
|
---|
| 543 | void premutate( TypeInstType * ) { visit_children = false; }
|
---|
| 544 | Type * postmutate( TypeInstType * typeInst ) {
|
---|
[6c3a988f] | 545 | EqvClass eqvClass;
|
---|
[3096ec1] | 546 | if ( tenv.lookup( typeInst->get_name(), eqvClass ) ) {
|
---|
[6c3a988f] | 547 | if ( eqvClass.data.kind == TypeDecl::Ttype ) {
|
---|
| 548 | // expand ttype parameter into its actual type
|
---|
| 549 | if ( eqvClass.type ) {
|
---|
| 550 | delete typeInst;
|
---|
| 551 | return eqvClass.type->clone();
|
---|
| 552 | }
|
---|
| 553 | }
|
---|
| 554 | }
|
---|
| 555 | return typeInst;
|
---|
| 556 | }
|
---|
| 557 | };
|
---|
| 558 |
|
---|
| 559 | /// flattens a list of declarations, so that each tuple type has a single declaration.
|
---|
| 560 | /// makes use of TtypeExpander to ensure ttypes are flat as well.
|
---|
| 561 | void flattenList( std::list< DeclarationWithType * > src, std::list< DeclarationWithType * > & dst, TypeEnvironment & env ) {
|
---|
| 562 | dst.clear();
|
---|
| 563 | for ( DeclarationWithType * dcl : src ) {
|
---|
[3096ec1] | 564 | PassVisitor<TtypeExpander> expander( env );
|
---|
[6c3a988f] | 565 | dcl->acceptMutator( expander );
|
---|
| 566 | std::list< Type * > types;
|
---|
| 567 | flatten( dcl->get_type(), back_inserter( types ) );
|
---|
| 568 | for ( Type * t : types ) {
|
---|
[68fe077a] | 569 | dst.push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::C, nullptr, t, nullptr ) );
|
---|
[6c3a988f] | 570 | }
|
---|
| 571 | delete dcl;
|
---|
| 572 | }
|
---|
| 573 | }
|
---|
| 574 |
|
---|
[a32b204] | 575 | void Unify::visit(FunctionType *functionType) {
|
---|
| 576 | FunctionType *otherFunction = dynamic_cast< FunctionType* >( type2 );
|
---|
| 577 | if ( otherFunction && functionType->get_isVarArgs() == otherFunction->get_isVarArgs() ) {
|
---|
[6c3a988f] | 578 | // flatten the parameter lists for both functions so that tuple structure
|
---|
| 579 | // doesn't affect unification. Must be a clone so that the types don't change.
|
---|
| 580 | std::unique_ptr<FunctionType> flatFunc( functionType->clone() );
|
---|
| 581 | std::unique_ptr<FunctionType> flatOther( otherFunction->clone() );
|
---|
| 582 | flattenList( flatFunc->get_parameters(), flatFunc->get_parameters(), env );
|
---|
| 583 | flattenList( flatOther->get_parameters(), flatOther->get_parameters(), env );
|
---|
| 584 |
|
---|
[53e3b4a] | 585 | // sizes don't have to match if ttypes are involved; need to be more precise wrt where the ttype is to prevent errors
|
---|
[6c3a988f] | 586 | if ( (flatFunc->get_parameters().size() == flatOther->get_parameters().size() && flatFunc->get_returnVals().size() == flatOther->get_returnVals().size()) || flatFunc->isTtype() || flatOther->isTtype() ) {
|
---|
| 587 | if ( unifyDeclList( flatFunc->get_parameters().begin(), flatFunc->get_parameters().end(), flatOther->get_parameters().begin(), flatOther->get_parameters().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
|
---|
| 588 | if ( unifyDeclList( flatFunc->get_returnVals().begin(), flatFunc->get_returnVals().end(), flatOther->get_returnVals().begin(), flatOther->get_returnVals().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
|
---|
[1cbca6e] | 589 |
|
---|
[6c3a988f] | 590 | // the original types must be used in mark assertions, since pointer comparisons are used
|
---|
[03da511] | 591 | markAssertions( haveAssertions, needAssertions, functionType );
|
---|
| 592 | markAssertions( haveAssertions, needAssertions, otherFunction );
|
---|
[41a2620] | 593 |
|
---|
[03da511] | 594 | result = true;
|
---|
| 595 | } // if
|
---|
[a32b204] | 596 | } // if
|
---|
| 597 | } // if
|
---|
| 598 | } // if
|
---|
| 599 | }
|
---|
| 600 |
|
---|
| 601 | template< typename RefType >
|
---|
[02ec390] | 602 | void Unify::handleRefType( RefType *inst, Type *other ) {
|
---|
| 603 | // check that other type is compatible and named the same
|
---|
[a32b204] | 604 | RefType *otherStruct = dynamic_cast< RefType* >( other );
|
---|
| 605 | result = otherStruct && inst->get_name() == otherStruct->get_name();
|
---|
[02ec390] | 606 | }
|
---|
| 607 |
|
---|
| 608 | template< typename RefType >
|
---|
| 609 | void Unify::handleGenericRefType( RefType *inst, Type *other ) {
|
---|
| 610 | // Check that other type is compatible and named the same
|
---|
| 611 | handleRefType( inst, other );
|
---|
| 612 | if ( ! result ) return;
|
---|
[f5234f3] | 613 | // Check that parameters of types unify, if any
|
---|
[02ec390] | 614 | std::list< Expression* > params = inst->get_parameters();
|
---|
[f5234f3] | 615 | std::list< Expression* > otherParams = ((RefType*)other)->get_parameters();
|
---|
| 616 |
|
---|
| 617 | std::list< Expression* >::const_iterator it = params.begin(), jt = otherParams.begin();
|
---|
| 618 | for ( ; it != params.end() && jt != otherParams.end(); ++it, ++jt ) {
|
---|
| 619 | TypeExpr *param = dynamic_cast< TypeExpr* >(*it);
|
---|
[b2daebd4] | 620 | assertf(param, "Aggregate parameters should be type expressions");
|
---|
[f5234f3] | 621 | TypeExpr *otherParam = dynamic_cast< TypeExpr* >(*jt);
|
---|
[b2daebd4] | 622 | assertf(otherParam, "Aggregate parameters should be type expressions");
|
---|
[ce8c12f] | 623 |
|
---|
[b2daebd4] | 624 | Type* paramTy = param->get_type();
|
---|
| 625 | Type* otherParamTy = otherParam->get_type();
|
---|
[f5234f3] | 626 |
|
---|
[b2daebd4] | 627 | bool tupleParam = Tuples::isTtype( paramTy );
|
---|
| 628 | bool otherTupleParam = Tuples::isTtype( otherParamTy );
|
---|
| 629 |
|
---|
| 630 | if ( tupleParam && otherTupleParam ) {
|
---|
| 631 | ++it; ++jt; // skip ttype parameters for break
|
---|
| 632 | } else if ( tupleParam ) {
|
---|
| 633 | // bundle other parameters into tuple to match
|
---|
[62423350] | 634 | std::list< Type * > binderTypes;
|
---|
[b2daebd4] | 635 |
|
---|
| 636 | do {
|
---|
[62423350] | 637 | binderTypes.push_back( otherParam->get_type()->clone() );
|
---|
[b2daebd4] | 638 | ++jt;
|
---|
| 639 |
|
---|
| 640 | if ( jt == otherParams.end() ) break;
|
---|
| 641 |
|
---|
| 642 | otherParam = dynamic_cast< TypeExpr* >(*jt);
|
---|
| 643 | assertf(otherParam, "Aggregate parameters should be type expressions");
|
---|
| 644 | } while (true);
|
---|
| 645 |
|
---|
[62423350] | 646 | otherParamTy = new TupleType{ paramTy->get_qualifiers(), binderTypes };
|
---|
[b2daebd4] | 647 | ++it; // skip ttype parameter for break
|
---|
| 648 | } else if ( otherTupleParam ) {
|
---|
| 649 | // bundle parameters into tuple to match other
|
---|
[62423350] | 650 | std::list< Type * > binderTypes;
|
---|
[b2daebd4] | 651 |
|
---|
| 652 | do {
|
---|
[62423350] | 653 | binderTypes.push_back( param->get_type()->clone() );
|
---|
[b2daebd4] | 654 | ++it;
|
---|
| 655 |
|
---|
| 656 | if ( it == params.end() ) break;
|
---|
| 657 |
|
---|
| 658 | param = dynamic_cast< TypeExpr* >(*it);
|
---|
| 659 | assertf(param, "Aggregate parameters should be type expressions");
|
---|
| 660 | } while (true);
|
---|
| 661 |
|
---|
[62423350] | 662 | paramTy = new TupleType{ otherParamTy->get_qualifiers(), binderTypes };
|
---|
[b2daebd4] | 663 | ++jt; // skip ttype parameter for break
|
---|
| 664 | }
|
---|
| 665 |
|
---|
| 666 | if ( ! unifyExact( paramTy, otherParamTy, env, needAssertions, haveAssertions, openVars, WidenMode(false, false), indexer ) ) {
|
---|
[02ec390] | 667 | result = false;
|
---|
| 668 | return;
|
---|
| 669 | }
|
---|
[b2daebd4] | 670 |
|
---|
| 671 | // ttype parameter should be last
|
---|
| 672 | if ( tupleParam || otherTupleParam ) break;
|
---|
[02ec390] | 673 | }
|
---|
[f5234f3] | 674 | result = ( it == params.end() && jt == otherParams.end() );
|
---|
[02ec390] | 675 | }
|
---|
[a32b204] | 676 |
|
---|
| 677 | void Unify::visit(StructInstType *structInst) {
|
---|
[02ec390] | 678 | handleGenericRefType( structInst, type2 );
|
---|
[a32b204] | 679 | }
|
---|
| 680 |
|
---|
| 681 | void Unify::visit(UnionInstType *unionInst) {
|
---|
[02ec390] | 682 | handleGenericRefType( unionInst, type2 );
|
---|
[a32b204] | 683 | }
|
---|
| 684 |
|
---|
| 685 | void Unify::visit(EnumInstType *enumInst) {
|
---|
| 686 | handleRefType( enumInst, type2 );
|
---|
| 687 | }
|
---|
| 688 |
|
---|
[4040425] | 689 | void Unify::visit(TraitInstType *contextInst) {
|
---|
[a32b204] | 690 | handleRefType( contextInst, type2 );
|
---|
| 691 | }
|
---|
| 692 |
|
---|
| 693 | void Unify::visit(TypeInstType *typeInst) {
|
---|
| 694 | assert( openVars.find( typeInst->get_name() ) == openVars.end() );
|
---|
| 695 | TypeInstType *otherInst = dynamic_cast< TypeInstType* >( type2 );
|
---|
| 696 | if ( otherInst && typeInst->get_name() == otherInst->get_name() ) {
|
---|
| 697 | result = true;
|
---|
[51b73452] | 698 | /// } else {
|
---|
| 699 | /// NamedTypeDecl *nt = indexer.lookupType( typeInst->get_name() );
|
---|
[a32b204] | 700 | /// if ( nt ) {
|
---|
[51b73452] | 701 | /// TypeDecl *type = dynamic_cast< TypeDecl* >( nt );
|
---|
| 702 | /// assert( type );
|
---|
[a32b204] | 703 | /// if ( type->get_base() ) {
|
---|
[51b73452] | 704 | /// result = unifyExact( type->get_base(), typeInst, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 705 | /// }
|
---|
| 706 | /// }
|
---|
[a32b204] | 707 | } // if
|
---|
| 708 | }
|
---|
| 709 |
|
---|
| 710 | template< typename Iterator1, typename Iterator2 >
|
---|
[c77fd8b] | 711 | bool unifyList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
[f3b0a07] | 712 | auto get_type = [](Type * t) { return t; };
|
---|
[a32b204] | 713 | for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
|
---|
[f3b0a07] | 714 | Type * t1 = *list1Begin;
|
---|
| 715 | Type * t2 = *list2Begin;
|
---|
| 716 | bool isTtype1 = Tuples::isTtype( t1 );
|
---|
| 717 | bool isTtype2 = Tuples::isTtype( t2 );
|
---|
| 718 | // xxx - assumes ttype must be last parameter
|
---|
| 719 | // xxx - there may be a nice way to refactor this, but be careful because the argument positioning might matter in some cases.
|
---|
| 720 | if ( isTtype1 && ! isTtype2 ) {
|
---|
| 721 | // combine all of the things in list2, then unify
|
---|
| 722 | return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 723 | } else if ( isTtype2 && ! isTtype1 ) {
|
---|
| 724 | // combine all of the things in list1, then unify
|
---|
| 725 | return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 726 | } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
|
---|
[a32b204] | 727 | return false;
|
---|
[f3b0a07] | 728 | } // if
|
---|
| 729 |
|
---|
[a32b204] | 730 | } // for
|
---|
[f3b0a07] | 731 | if ( list1Begin != list1End ) {
|
---|
| 732 | // try unifying empty tuple type with ttype
|
---|
| 733 | Type * t1 = *list1Begin;
|
---|
| 734 | if ( Tuples::isTtype( t1 ) ) {
|
---|
| 735 | return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 736 | } else return false;
|
---|
| 737 | } else if ( list2Begin != list2End ) {
|
---|
| 738 | // try unifying empty tuple type with ttype
|
---|
| 739 | Type * t2 = *list2Begin;
|
---|
| 740 | if ( Tuples::isTtype( t2 ) ) {
|
---|
| 741 | return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 742 | } else return false;
|
---|
[a32b204] | 743 | } else {
|
---|
| 744 | return true;
|
---|
[f3b0a07] | 745 | } // if
|
---|
[a32b204] | 746 | }
|
---|
| 747 |
|
---|
| 748 | void Unify::visit(TupleType *tupleType) {
|
---|
| 749 | if ( TupleType *otherTuple = dynamic_cast< TupleType* >( type2 ) ) {
|
---|
[f3b0a07] | 750 | std::unique_ptr<TupleType> flat1( tupleType->clone() );
|
---|
| 751 | std::unique_ptr<TupleType> flat2( otherTuple->clone() );
|
---|
| 752 | std::list<Type *> types1, types2;
|
---|
| 753 |
|
---|
[3096ec1] | 754 | PassVisitor<TtypeExpander> expander( env );
|
---|
[f3b0a07] | 755 | flat1->acceptMutator( expander );
|
---|
| 756 | flat2->acceptMutator( expander );
|
---|
| 757 |
|
---|
| 758 | flatten( flat1.get(), back_inserter( types1 ) );
|
---|
| 759 | flatten( flat2.get(), back_inserter( types2 ) );
|
---|
| 760 |
|
---|
[c77fd8b] | 761 | result = unifyList( types1.begin(), types1.end(), types2.begin(), types2.end(), env, needAssertions, haveAssertions, openVars, indexer );
|
---|
[a32b204] | 762 | } // if
|
---|
| 763 | }
|
---|
[51b73452] | 764 |
|
---|
[af397ef8] | 765 | void Unify::visit( __attribute__((unused)) VarArgsType *varArgsType ) {
|
---|
[44b7088] | 766 | result = dynamic_cast< VarArgsType* >( type2 );
|
---|
| 767 | }
|
---|
| 768 |
|
---|
[af397ef8] | 769 | void Unify::visit( __attribute__((unused)) ZeroType *zeroType ) {
|
---|
[89e6ffc] | 770 | result = dynamic_cast< ZeroType* >( type2 );
|
---|
| 771 | }
|
---|
| 772 |
|
---|
[af397ef8] | 773 | void Unify::visit( __attribute__((unused)) OneType *oneType ) {
|
---|
[89e6ffc] | 774 | result = dynamic_cast< OneType* >( type2 );
|
---|
| 775 | }
|
---|
| 776 |
|
---|
[906e24d] | 777 | // xxx - compute once and store in the FunctionType?
|
---|
| 778 | Type * extractResultType( FunctionType * function ) {
|
---|
| 779 | if ( function->get_returnVals().size() == 0 ) {
|
---|
| 780 | return new VoidType( Type::Qualifiers() );
|
---|
| 781 | } else if ( function->get_returnVals().size() == 1 ) {
|
---|
| 782 | return function->get_returnVals().front()->get_type()->clone();
|
---|
| 783 | } else {
|
---|
[62423350] | 784 | std::list< Type * > types;
|
---|
[906e24d] | 785 | for ( DeclarationWithType * decl : function->get_returnVals() ) {
|
---|
[62423350] | 786 | types.push_back( decl->get_type()->clone() );
|
---|
[906e24d] | 787 | } // for
|
---|
[62423350] | 788 | return new TupleType( Type::Qualifiers(), types );
|
---|
[906e24d] | 789 | }
|
---|
| 790 | }
|
---|
[51b73452] | 791 | } // namespace ResolvExpr
|
---|
[a32b204] | 792 |
|
---|
| 793 | // Local Variables: //
|
---|
| 794 | // tab-width: 4 //
|
---|
| 795 | // mode: c++ //
|
---|
| 796 | // compile-command: "make install" //
|
---|
| 797 | // End: //
|
---|