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