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