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