[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
|
---|
[07de76b] | 11 | // Last Modified By : Peter A. Buhr
|
---|
| 12 | // Last Modified On : Fri Dec 13 23:43:05 2019
|
---|
| 13 | // Update Count : 46
|
---|
[a32b204] | 14 | //
|
---|
[51b73452] | 15 |
|
---|
[f474e91] | 16 | #include "Unify.h"
|
---|
| 17 |
|
---|
[d76c588] | 18 | #include <cassert> // for assertf, assert
|
---|
| 19 | #include <iterator> // for back_insert_iterator, back_inserter
|
---|
| 20 | #include <map> // for _Rb_tree_const_iterator, _Rb_tree_i...
|
---|
| 21 | #include <memory> // for unique_ptr
|
---|
| 22 | #include <set> // for set
|
---|
| 23 | #include <string> // for string, operator==, operator!=, bas...
|
---|
| 24 | #include <utility> // for pair, move
|
---|
[54e41b3] | 25 | #include <vector>
|
---|
[51b73452] | 26 |
|
---|
[2890212] | 27 | #include "AST/Copy.hpp"
|
---|
[f474e91] | 28 | #include "AST/Decl.hpp"
|
---|
[54e41b3] | 29 | #include "AST/Node.hpp"
|
---|
[f474e91] | 30 | #include "AST/Pass.hpp"
|
---|
[2890212] | 31 | #include "AST/Print.hpp"
|
---|
[54e41b3] | 32 | #include "AST/Type.hpp"
|
---|
[d76c588] | 33 | #include "AST/TypeEnvironment.hpp"
|
---|
[f02f546] | 34 | #include "Common/Eval.h" // for eval
|
---|
[d76c588] | 35 | #include "Common/PassVisitor.h" // for PassVisitor
|
---|
[5bf3976] | 36 | #include "CommonType.hpp" // for commonType
|
---|
[d76c588] | 37 | #include "FindOpenVars.h" // for findOpenVars
|
---|
[5bf3976] | 38 | #include "SpecCost.hpp" // for SpecCost
|
---|
[07de76b] | 39 | #include "SynTree/LinkageSpec.h" // for C
|
---|
[d76c588] | 40 | #include "SynTree/Constant.h" // for Constant
|
---|
| 41 | #include "SynTree/Declaration.h" // for TypeDecl, TypeDecl::Data, Declarati...
|
---|
| 42 | #include "SynTree/Expression.h" // for TypeExpr, Expression, ConstantExpr
|
---|
| 43 | #include "SynTree/Mutator.h" // for Mutator
|
---|
| 44 | #include "SynTree/Type.h" // for Type, TypeInstType, FunctionType
|
---|
| 45 | #include "SynTree/Visitor.h" // for Visitor
|
---|
| 46 | #include "Tuples/Tuples.h" // for isTtype
|
---|
| 47 | #include "TypeEnvironment.h" // for EqvClass, AssertionSet, OpenVarSet
|
---|
[5bf3976] | 48 | #include "typeops.h" // for flatten, occurs
|
---|
[ea6332d] | 49 |
|
---|
[f474e91] | 50 | namespace ast {
|
---|
| 51 | class SymbolTable;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[ea6332d] | 54 | namespace SymTab {
|
---|
[e563edf] | 55 | class Indexer;
|
---|
[ea6332d] | 56 | } // namespace SymTab
|
---|
[51b73452] | 57 |
|
---|
[1cbca6e] | 58 | // #define DEBUG
|
---|
[51b73452] | 59 |
|
---|
| 60 | namespace ResolvExpr {
|
---|
| 61 |
|
---|
[e563edf] | 62 | // Template Helpers:
|
---|
| 63 | template< typename Iterator1, typename Iterator2 >
|
---|
| 64 | bool unifyList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer, std::list< Type* > &commonTypes ) {
|
---|
| 65 | for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
|
---|
| 66 | Type *commonType = 0;
|
---|
| 67 | if ( ! unify( *list1Begin, *list2Begin, env, needAssertions, haveAssertions, openVars, indexer, commonType ) ) {
|
---|
| 68 | return false;
|
---|
| 69 | } // if
|
---|
| 70 | commonTypes.push_back( commonType );
|
---|
| 71 | } // for
|
---|
| 72 | return ( list1Begin == list1End && list2Begin == list2End );
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | template< typename Iterator1, typename Iterator2 >
|
---|
| 76 | bool unifyList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
| 77 | std::list< Type* > commonTypes;
|
---|
| 78 | if ( unifyList( list1Begin, list1End, list2Begin, list2End, env, needAssertions, haveAssertions, openVars, indexer, commonTypes ) ) {
|
---|
| 79 | deleteAll( commonTypes );
|
---|
| 80 | return true;
|
---|
| 81 | } else {
|
---|
| 82 | return false;
|
---|
| 83 | } // if
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[f474e91] | 86 | struct Unify_old : public WithShortCircuiting {
|
---|
| 87 | Unify_old( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widen, const SymTab::Indexer &indexer );
|
---|
[41a2620] | 88 |
|
---|
[a32b204] | 89 | bool get_result() const { return result; }
|
---|
| 90 |
|
---|
[36a2367] | 91 | void previsit( BaseSyntaxNode * ) { visit_children = false; }
|
---|
| 92 |
|
---|
| 93 | void postvisit( VoidType * voidType );
|
---|
| 94 | void postvisit( BasicType * basicType );
|
---|
| 95 | void postvisit( PointerType * pointerType );
|
---|
| 96 | void postvisit( ArrayType * arrayType );
|
---|
| 97 | void postvisit( ReferenceType * refType );
|
---|
| 98 | void postvisit( FunctionType * functionType );
|
---|
| 99 | void postvisit( StructInstType * aggregateUseType );
|
---|
| 100 | void postvisit( UnionInstType * aggregateUseType );
|
---|
| 101 | void postvisit( EnumInstType * aggregateUseType );
|
---|
| 102 | void postvisit( TraitInstType * aggregateUseType );
|
---|
| 103 | void postvisit( TypeInstType * aggregateUseType );
|
---|
| 104 | void postvisit( TupleType * tupleType );
|
---|
| 105 | void postvisit( VarArgsType * varArgsType );
|
---|
| 106 | void postvisit( ZeroType * zeroType );
|
---|
| 107 | void postvisit( OneType * oneType );
|
---|
| 108 |
|
---|
| 109 | private:
|
---|
[a32b204] | 110 | template< typename RefType > void handleRefType( RefType *inst, Type *other );
|
---|
[02ec390] | 111 | template< typename RefType > void handleGenericRefType( RefType *inst, Type *other );
|
---|
[a32b204] | 112 |
|
---|
| 113 | bool result;
|
---|
| 114 | Type *type2; // inherited
|
---|
| 115 | TypeEnvironment &env;
|
---|
| 116 | AssertionSet &needAssertions;
|
---|
| 117 | AssertionSet &haveAssertions;
|
---|
| 118 | const OpenVarSet &openVars;
|
---|
[f474e91] | 119 | WidenMode widen;
|
---|
[a32b204] | 120 | const SymTab::Indexer &indexer;
|
---|
| 121 | };
|
---|
| 122 |
|
---|
[eb50842] | 123 | /// Attempts an inexact unification of type1 and type2.
|
---|
| 124 | /// Returns false if no such unification; if the types can be unified, sets common (unless they unify exactly and have identical type qualifiers)
|
---|
[f474e91] | 125 | bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widen, const SymTab::Indexer &indexer, Type *&common );
|
---|
| 126 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widen, const SymTab::Indexer &indexer );
|
---|
| 127 |
|
---|
[7870799] | 128 | bool unifyExact(
|
---|
| 129 | const ast::Type * type1, const ast::Type * type2, ast::TypeEnvironment & env,
|
---|
| 130 | ast::AssertionSet & need, ast::AssertionSet & have, const ast::OpenVarSet & open,
|
---|
[251ce80] | 131 | WidenMode widen );
|
---|
[41a2620] | 132 |
|
---|
[6f096d2] | 133 | bool typesCompatible( const Type * first, const Type * second, const SymTab::Indexer & indexer, const TypeEnvironment & env ) {
|
---|
[a32b204] | 134 | TypeEnvironment newEnv;
|
---|
[1cbca6e] | 135 | OpenVarSet openVars, closedVars; // added closedVars
|
---|
[a32b204] | 136 | AssertionSet needAssertions, haveAssertions;
|
---|
[6f096d2] | 137 | Type * newFirst = first->clone(), * newSecond = second->clone();
|
---|
[a32b204] | 138 | env.apply( newFirst );
|
---|
| 139 | env.apply( newSecond );
|
---|
[1cbca6e] | 140 |
|
---|
| 141 | // do we need to do this? Seems like we do, types should be able to be compatible if they
|
---|
| 142 | // have free variables that can unify
|
---|
| 143 | findOpenVars( newFirst, openVars, closedVars, needAssertions, haveAssertions, false );
|
---|
| 144 | findOpenVars( newSecond, openVars, closedVars, needAssertions, haveAssertions, true );
|
---|
| 145 |
|
---|
[a32b204] | 146 | bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 147 | delete newFirst;
|
---|
| 148 | delete newSecond;
|
---|
| 149 | return result;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[7870799] | 152 | bool typesCompatible(
|
---|
[251ce80] | 153 | const ast::Type * first, const ast::Type * second,
|
---|
[d76c588] | 154 | const ast::TypeEnvironment & env ) {
|
---|
[f474e91] | 155 | ast::TypeEnvironment newEnv;
|
---|
| 156 | ast::OpenVarSet open, closed;
|
---|
| 157 | ast::AssertionSet need, have;
|
---|
| 158 |
|
---|
| 159 | ast::ptr<ast::Type> newFirst{ first }, newSecond{ second };
|
---|
| 160 | env.apply( newFirst );
|
---|
| 161 | env.apply( newSecond );
|
---|
| 162 |
|
---|
[46da46b] | 163 | // findOpenVars( newFirst, open, closed, need, have, FirstClosed );
|
---|
| 164 | findOpenVars( newSecond, open, closed, need, have, newEnv, FirstOpen );
|
---|
[f474e91] | 165 |
|
---|
[251ce80] | 166 | return unifyExact(newFirst, newSecond, newEnv, need, have, open, noWiden() );
|
---|
[d76c588] | 167 | }
|
---|
| 168 |
|
---|
[7870799] | 169 | bool typesCompatibleIgnoreQualifiers( const Type * first, const Type * second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
|
---|
[a32b204] | 170 | TypeEnvironment newEnv;
|
---|
| 171 | OpenVarSet openVars;
|
---|
| 172 | AssertionSet needAssertions, haveAssertions;
|
---|
| 173 | Type *newFirst = first->clone(), *newSecond = second->clone();
|
---|
| 174 | env.apply( newFirst );
|
---|
| 175 | env.apply( newSecond );
|
---|
| 176 | newFirst->get_qualifiers() = Type::Qualifiers();
|
---|
| 177 | newSecond->get_qualifiers() = Type::Qualifiers();
|
---|
[2890212] | 178 |
|
---|
[a32b204] | 179 | bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 180 | delete newFirst;
|
---|
| 181 | delete newSecond;
|
---|
| 182 | return result;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[7870799] | 185 | bool typesCompatibleIgnoreQualifiers(
|
---|
[251ce80] | 186 | const ast::Type * first, const ast::Type * second,
|
---|
[d76c588] | 187 | const ast::TypeEnvironment & env ) {
|
---|
[f474e91] | 188 | ast::TypeEnvironment newEnv;
|
---|
| 189 | ast::OpenVarSet open;
|
---|
| 190 | ast::AssertionSet need, have;
|
---|
[7870799] | 191 |
|
---|
[2890212] | 192 | ast::Type * newFirst = shallowCopy( first );
|
---|
| 193 | ast::Type * newSecond = shallowCopy( second );
|
---|
[b729c01] | 194 | if ( auto temp = dynamic_cast<const ast::EnumInstType *>(first) ) {
|
---|
| 195 | if ( !dynamic_cast< const ast::EnumInstType * >( second ) ) {
|
---|
| 196 | const ast::EnumDecl * baseEnum = dynamic_cast<const ast::EnumDecl *>(temp->base.get());
|
---|
| 197 | if ( auto t = baseEnum->base.get() ) {
|
---|
| 198 | newFirst = ast::shallowCopy( t );
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 | } else if ( auto temp = dynamic_cast<const ast::EnumInstType *>(second) ) {
|
---|
| 202 | const ast::EnumDecl * baseEnum = dynamic_cast<const ast::EnumDecl *>(temp->base.get());
|
---|
| 203 | if ( auto t = baseEnum->base.get() ) {
|
---|
| 204 | newSecond = ast::shallowCopy( t );
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[2890212] | 208 | newFirst ->qualifiers = {};
|
---|
| 209 | newSecond->qualifiers = {};
|
---|
[f49b3fc] | 210 | ast::ptr< ast::Type > t1_(newFirst );
|
---|
| 211 | ast::ptr< ast::Type > t2_(newSecond);
|
---|
[f474e91] | 212 |
|
---|
[c7f834e] | 213 | ast::ptr< ast::Type > subFirst = env.apply(newFirst).node;
|
---|
| 214 | ast::ptr< ast::Type > subSecond = env.apply(newSecond).node;
|
---|
| 215 |
|
---|
[7870799] | 216 | return unifyExact(
|
---|
[c7f834e] | 217 | subFirst,
|
---|
| 218 | subSecond,
|
---|
[251ce80] | 219 | newEnv, need, have, open, noWiden() );
|
---|
[d76c588] | 220 | }
|
---|
| 221 |
|
---|
[a32b204] | 222 | bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
| 223 | OpenVarSet closedVars;
|
---|
| 224 | findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
|
---|
| 225 | findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
|
---|
| 226 | Type *commonType = 0;
|
---|
| 227 | if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType ) ) {
|
---|
| 228 | if ( commonType ) {
|
---|
| 229 | delete commonType;
|
---|
| 230 | } // if
|
---|
| 231 | return true;
|
---|
| 232 | } else {
|
---|
| 233 | return false;
|
---|
| 234 | } // if
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer, Type *&commonType ) {
|
---|
| 238 | OpenVarSet closedVars;
|
---|
| 239 | findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
|
---|
| 240 | findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
|
---|
| 241 | return unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType );
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[f474e91] | 244 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widen, const SymTab::Indexer &indexer ) {
|
---|
[51b73452] | 245 | #ifdef DEBUG
|
---|
[a32b204] | 246 | TypeEnvironment debugEnv( env );
|
---|
[51b73452] | 247 | #endif
|
---|
[eb50842] | 248 | if ( type1->get_qualifiers() != type2->get_qualifiers() ) {
|
---|
| 249 | return false;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
[a32b204] | 252 | bool result;
|
---|
| 253 | TypeInstType *var1 = dynamic_cast< TypeInstType* >( type1 );
|
---|
| 254 | TypeInstType *var2 = dynamic_cast< TypeInstType* >( type2 );
|
---|
| 255 | OpenVarSet::const_iterator entry1, entry2;
|
---|
| 256 | if ( var1 ) {
|
---|
| 257 | entry1 = openVars.find( var1->get_name() );
|
---|
| 258 | } // if
|
---|
| 259 | if ( var2 ) {
|
---|
| 260 | entry2 = openVars.find( var2->get_name() );
|
---|
| 261 | } // if
|
---|
| 262 | bool isopen1 = var1 && ( entry1 != openVars.end() );
|
---|
| 263 | bool isopen2 = var2 && ( entry2 != openVars.end() );
|
---|
[eb50842] | 264 |
|
---|
[7a63486] | 265 | if ( isopen1 && isopen2 ) {
|
---|
| 266 | if ( entry1->second.kind != entry2->second.kind ) {
|
---|
| 267 | result = false;
|
---|
| 268 | } else {
|
---|
[4139e3d] | 269 | result = env.bindVarToVar(
|
---|
| 270 | var1, var2, TypeDecl::Data{ entry1->second, entry2->second }, needAssertions,
|
---|
[f474e91] | 271 | haveAssertions, openVars, widen, indexer );
|
---|
[7a63486] | 272 | }
|
---|
[a32b204] | 273 | } else if ( isopen1 ) {
|
---|
[f474e91] | 274 | result = env.bindVar( var1, type2, entry1->second, needAssertions, haveAssertions, openVars, widen, indexer );
|
---|
| 275 | } else if ( isopen2 ) { // TODO: swap widen values in call, since type positions are flipped?
|
---|
| 276 | result = env.bindVar( var2, type1, entry2->second, needAssertions, haveAssertions, openVars, widen, indexer );
|
---|
[a32b204] | 277 | } else {
|
---|
[f474e91] | 278 | PassVisitor<Unify_old> comparator( type2, env, needAssertions, haveAssertions, openVars, widen, indexer );
|
---|
[a32b204] | 279 | type1->accept( comparator );
|
---|
[36a2367] | 280 | result = comparator.pass.get_result();
|
---|
[a32b204] | 281 | } // if
|
---|
[51b73452] | 282 | #ifdef DEBUG
|
---|
[2c57025] | 283 | std::cerr << "============ unifyExact" << std::endl;
|
---|
| 284 | std::cerr << "type1 is ";
|
---|
| 285 | type1->print( std::cerr );
|
---|
| 286 | std::cerr << std::endl << "type2 is ";
|
---|
| 287 | type2->print( std::cerr );
|
---|
| 288 | std::cerr << std::endl << "openVars are ";
|
---|
| 289 | printOpenVarSet( openVars, std::cerr, 8 );
|
---|
| 290 | std::cerr << std::endl << "input env is " << std::endl;
|
---|
| 291 | debugEnv.print( std::cerr, 8 );
|
---|
| 292 | std::cerr << std::endl << "result env is " << std::endl;
|
---|
| 293 | env.print( std::cerr, 8 );
|
---|
| 294 | std::cerr << "result is " << result << std::endl;
|
---|
[51b73452] | 295 | #endif
|
---|
[a32b204] | 296 | return result;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
| 300 | return unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 301 | }
|
---|
| 302 |
|
---|
[f474e91] | 303 | bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widen, const SymTab::Indexer &indexer, Type *&common ) {
|
---|
[a32b204] | 304 | Type::Qualifiers tq1 = type1->get_qualifiers(), tq2 = type2->get_qualifiers();
|
---|
| 305 | type1->get_qualifiers() = Type::Qualifiers();
|
---|
| 306 | type2->get_qualifiers() = Type::Qualifiers();
|
---|
| 307 | bool result;
|
---|
[51b73452] | 308 | #ifdef DEBUG
|
---|
[2c57025] | 309 | std::cerr << "unifyInexact type 1 is ";
|
---|
| 310 | type1->print( std::cerr );
|
---|
[0b150ec] | 311 | std::cerr << " type 2 is ";
|
---|
[2c57025] | 312 | type2->print( std::cerr );
|
---|
| 313 | std::cerr << std::endl;
|
---|
[51b73452] | 314 | #endif
|
---|
[f474e91] | 315 | if ( ! unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, widen, indexer ) ) {
|
---|
[51b73452] | 316 | #ifdef DEBUG
|
---|
[2c57025] | 317 | std::cerr << "unifyInexact: no exact unification found" << std::endl;
|
---|
[51b73452] | 318 | #endif
|
---|
[f474e91] | 319 | if ( ( common = commonType( type1, type2, widen.first, widen.second, indexer, env, openVars ) ) ) {
|
---|
[3315e3d] | 320 | common->tq = tq1.unify( tq2 );
|
---|
[51b73452] | 321 | #ifdef DEBUG
|
---|
[2c57025] | 322 | std::cerr << "unifyInexact: common type is ";
|
---|
| 323 | common->print( std::cerr );
|
---|
| 324 | std::cerr << std::endl;
|
---|
[51b73452] | 325 | #endif
|
---|
[a32b204] | 326 | result = true;
|
---|
| 327 | } else {
|
---|
[51b73452] | 328 | #ifdef DEBUG
|
---|
[2c57025] | 329 | std::cerr << "unifyInexact: no common type found" << std::endl;
|
---|
[51b73452] | 330 | #endif
|
---|
[a32b204] | 331 | result = false;
|
---|
| 332 | } // if
|
---|
| 333 | } else {
|
---|
| 334 | if ( tq1 != tq2 ) {
|
---|
[f474e91] | 335 | if ( ( tq1 > tq2 || widen.first ) && ( tq2 > tq1 || widen.second ) ) {
|
---|
[a32b204] | 336 | common = type1->clone();
|
---|
[3315e3d] | 337 | common->tq = tq1.unify( tq2 );
|
---|
[a32b204] | 338 | result = true;
|
---|
| 339 | } else {
|
---|
| 340 | result = false;
|
---|
| 341 | } // if
|
---|
| 342 | } else {
|
---|
[e6cee92] | 343 | common = type1->clone();
|
---|
[3315e3d] | 344 | common->tq = tq1.unify( tq2 );
|
---|
[a32b204] | 345 | result = true;
|
---|
| 346 | } // if
|
---|
| 347 | } // if
|
---|
| 348 | type1->get_qualifiers() = tq1;
|
---|
| 349 | type2->get_qualifiers() = tq2;
|
---|
| 350 | return result;
|
---|
| 351 | }
|
---|
| 352 |
|
---|
[f474e91] | 353 | Unify_old::Unify_old( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widen, const SymTab::Indexer &indexer )
|
---|
| 354 | : result( false ), type2( type2 ), env( env ), needAssertions( needAssertions ), haveAssertions( haveAssertions ), openVars( openVars ), widen( widen ), indexer( indexer ) {
|
---|
[d76c588] | 355 | }
|
---|
| 356 |
|
---|
[f474e91] | 357 | void Unify_old::postvisit( __attribute__((unused)) VoidType *voidType) {
|
---|
[a32b204] | 358 | result = dynamic_cast< VoidType* >( type2 );
|
---|
| 359 | }
|
---|
| 360 |
|
---|
[f474e91] | 361 | void Unify_old::postvisit(BasicType *basicType) {
|
---|
[a32b204] | 362 | if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) {
|
---|
| 363 | result = basicType->get_kind() == otherBasic->get_kind();
|
---|
| 364 | } // if
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | void markAssertionSet( AssertionSet &assertions, DeclarationWithType *assert ) {
|
---|
| 368 | AssertionSet::iterator i = assertions.find( assert );
|
---|
| 369 | if ( i != assertions.end() ) {
|
---|
[6c3a988f] | 370 | i->second.isUsed = true;
|
---|
[a32b204] | 371 | } // if
|
---|
| 372 | }
|
---|
| 373 |
|
---|
| 374 | void markAssertions( AssertionSet &assertion1, AssertionSet &assertion2, Type *type ) {
|
---|
[aefcc3b] | 375 | for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
|
---|
[a32b204] | 376 | for ( std::list< DeclarationWithType* >::const_iterator assert = (*tyvar)->get_assertions().begin(); assert != (*tyvar)->get_assertions().end(); ++assert ) {
|
---|
| 377 | markAssertionSet( assertion1, *assert );
|
---|
| 378 | markAssertionSet( assertion2, *assert );
|
---|
| 379 | } // for
|
---|
| 380 | } // for
|
---|
| 381 | }
|
---|
| 382 |
|
---|
[f474e91] | 383 | void Unify_old::postvisit(PointerType *pointerType) {
|
---|
[a32b204] | 384 | if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) {
|
---|
| 385 | result = unifyExact( pointerType->get_base(), otherPointer->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 386 | markAssertions( haveAssertions, needAssertions, pointerType );
|
---|
| 387 | markAssertions( haveAssertions, needAssertions, otherPointer );
|
---|
| 388 | } // if
|
---|
| 389 | }
|
---|
| 390 |
|
---|
[f474e91] | 391 | void Unify_old::postvisit(ReferenceType *refType) {
|
---|
[ce8c12f] | 392 | if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) {
|
---|
| 393 | result = unifyExact( refType->get_base(), otherRef->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 394 | markAssertions( haveAssertions, needAssertions, refType );
|
---|
| 395 | markAssertions( haveAssertions, needAssertions, otherRef );
|
---|
| 396 | } // if
|
---|
| 397 | }
|
---|
| 398 |
|
---|
[f474e91] | 399 | void Unify_old::postvisit(ArrayType *arrayType) {
|
---|
[a32b204] | 400 | ArrayType *otherArray = dynamic_cast< ArrayType* >( type2 );
|
---|
[1cbca6e] | 401 | // to unify, array types must both be VLA or both not VLA
|
---|
| 402 | // and must both have a dimension expression or not have a dimension
|
---|
[41a2620] | 403 | if ( otherArray && arrayType->get_isVarLen() == otherArray->get_isVarLen() ) {
|
---|
[1cbca6e] | 404 |
|
---|
| 405 | if ( ! arrayType->get_isVarLen() && ! otherArray->get_isVarLen() &&
|
---|
| 406 | arrayType->get_dimension() != 0 && otherArray->get_dimension() != 0 ) {
|
---|
| 407 | ConstantExpr * ce1 = dynamic_cast< ConstantExpr * >( arrayType->get_dimension() );
|
---|
| 408 | ConstantExpr * ce2 = dynamic_cast< ConstantExpr * >( otherArray->get_dimension() );
|
---|
[41a2620] | 409 | // see C11 Reference Manual 6.7.6.2.6
|
---|
| 410 | // two array types with size specifiers that are integer constant expressions are
|
---|
| 411 | // compatible if both size specifiers have the same constant value
|
---|
| 412 | if ( ce1 && ce2 ) {
|
---|
| 413 | Constant * c1 = ce1->get_constant();
|
---|
| 414 | Constant * c2 = ce2->get_constant();
|
---|
| 415 |
|
---|
| 416 | if ( c1->get_value() != c2->get_value() ) {
|
---|
| 417 | // does not unify if the dimension is different
|
---|
| 418 | return;
|
---|
| 419 | }
|
---|
[1cbca6e] | 420 | }
|
---|
| 421 | }
|
---|
| 422 |
|
---|
[a32b204] | 423 | result = unifyExact( arrayType->get_base(), otherArray->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 424 | } // if
|
---|
| 425 | }
|
---|
| 426 |
|
---|
[f3b0a07] | 427 | template< typename Iterator, typename Func >
|
---|
| 428 | std::unique_ptr<Type> combineTypes( Iterator begin, Iterator end, Func & toType ) {
|
---|
[53e3b4a] | 429 | std::list< Type * > types;
|
---|
| 430 | for ( ; begin != end; ++begin ) {
|
---|
[64eae56] | 431 | // it's guaranteed that a ttype variable will be bound to a flat tuple, so ensure that this results in a flat tuple
|
---|
[f3b0a07] | 432 | flatten( toType( *begin ), back_inserter( types ) );
|
---|
[53e3b4a] | 433 | }
|
---|
[6c3a988f] | 434 | return std::unique_ptr<Type>( new TupleType( Type::Qualifiers(), types ) );
|
---|
[53e3b4a] | 435 | }
|
---|
| 436 |
|
---|
[a32b204] | 437 | template< typename Iterator1, typename Iterator2 >
|
---|
[954c954] | 438 | bool unifyTypeList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
[f3b0a07] | 439 | auto get_type = [](DeclarationWithType * dwt){ return dwt->get_type(); };
|
---|
[a32b204] | 440 | for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
|
---|
[53e3b4a] | 441 | Type * t1 = (*list1Begin)->get_type();
|
---|
| 442 | Type * t2 = (*list2Begin)->get_type();
|
---|
| 443 | bool isTtype1 = Tuples::isTtype( t1 );
|
---|
| 444 | bool isTtype2 = Tuples::isTtype( t2 );
|
---|
[6c3a988f] | 445 | // xxx - assumes ttype must be last parameter
|
---|
| 446 | // xxx - there may be a nice way to refactor this, but be careful because the argument positioning might matter in some cases.
|
---|
[53e3b4a] | 447 | if ( isTtype1 && ! isTtype2 ) {
|
---|
| 448 | // combine all of the things in list2, then unify
|
---|
[f3b0a07] | 449 | return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
[53e3b4a] | 450 | } else if ( isTtype2 && ! isTtype1 ) {
|
---|
| 451 | // combine all of the things in list1, then unify
|
---|
[f3b0a07] | 452 | return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
[53e3b4a] | 453 | } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
|
---|
[a32b204] | 454 | return false;
|
---|
| 455 | } // if
|
---|
| 456 | } // for
|
---|
[6c3a988f] | 457 | // 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] | 458 | if ( list1Begin != list1End ) {
|
---|
| 459 | // try unifying empty tuple type with ttype
|
---|
| 460 | Type * t1 = (*list1Begin)->get_type();
|
---|
| 461 | if ( Tuples::isTtype( t1 ) ) {
|
---|
[f3b0a07] | 462 | return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
[4c8621ac] | 463 | } else return false;
|
---|
| 464 | } else if ( list2Begin != list2End ) {
|
---|
| 465 | // try unifying empty tuple type with ttype
|
---|
| 466 | Type * t2 = (*list2Begin)->get_type();
|
---|
| 467 | if ( Tuples::isTtype( t2 ) ) {
|
---|
[f3b0a07] | 468 | return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
[4c8621ac] | 469 | } else return false;
|
---|
[a32b204] | 470 | } else {
|
---|
| 471 | return true;
|
---|
| 472 | } // if
|
---|
| 473 | }
|
---|
| 474 |
|
---|
[6c3a988f] | 475 | /// Finds ttypes and replaces them with their expansion, if known.
|
---|
| 476 | /// This needs to be done so that satisfying ttype assertions is easier.
|
---|
| 477 | /// If this isn't done then argument lists can have wildly different
|
---|
| 478 | /// size and structure, when they should be compatible.
|
---|
[f474e91] | 479 | struct TtypeExpander_old : public WithShortCircuiting {
|
---|
[3096ec1] | 480 | TypeEnvironment & tenv;
|
---|
[f474e91] | 481 | TtypeExpander_old( TypeEnvironment & tenv ) : tenv( tenv ) {}
|
---|
[3096ec1] | 482 | void premutate( TypeInstType * ) { visit_children = false; }
|
---|
| 483 | Type * postmutate( TypeInstType * typeInst ) {
|
---|
[00ac42e] | 484 | if ( const EqvClass *eqvClass = tenv.lookup( typeInst->get_name() ) ) {
|
---|
| 485 | // expand ttype parameter into its actual type
|
---|
| 486 | if ( eqvClass->data.kind == TypeDecl::Ttype && eqvClass->type ) {
|
---|
| 487 | delete typeInst;
|
---|
| 488 | return eqvClass->type->clone();
|
---|
[6c3a988f] | 489 | }
|
---|
| 490 | }
|
---|
| 491 | return typeInst;
|
---|
| 492 | }
|
---|
| 493 | };
|
---|
| 494 |
|
---|
| 495 | /// flattens a list of declarations, so that each tuple type has a single declaration.
|
---|
| 496 | /// makes use of TtypeExpander to ensure ttypes are flat as well.
|
---|
| 497 | void flattenList( std::list< DeclarationWithType * > src, std::list< DeclarationWithType * > & dst, TypeEnvironment & env ) {
|
---|
| 498 | dst.clear();
|
---|
| 499 | for ( DeclarationWithType * dcl : src ) {
|
---|
[f474e91] | 500 | PassVisitor<TtypeExpander_old> expander( env );
|
---|
[6c3a988f] | 501 | dcl->acceptMutator( expander );
|
---|
| 502 | std::list< Type * > types;
|
---|
| 503 | flatten( dcl->get_type(), back_inserter( types ) );
|
---|
| 504 | for ( Type * t : types ) {
|
---|
[1dcd52a3] | 505 | // 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.
|
---|
| 506 | // 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.
|
---|
| 507 | t->get_qualifiers() -= Type::Qualifiers(Type::Const | Type::Volatile | Type::Atomic);
|
---|
| 508 |
|
---|
[68fe077a] | 509 | dst.push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::C, nullptr, t, nullptr ) );
|
---|
[6c3a988f] | 510 | }
|
---|
| 511 | delete dcl;
|
---|
| 512 | }
|
---|
| 513 | }
|
---|
| 514 |
|
---|
[f474e91] | 515 | void Unify_old::postvisit(FunctionType *functionType) {
|
---|
[a32b204] | 516 | FunctionType *otherFunction = dynamic_cast< FunctionType* >( type2 );
|
---|
| 517 | if ( otherFunction && functionType->get_isVarArgs() == otherFunction->get_isVarArgs() ) {
|
---|
[6c3a988f] | 518 | // flatten the parameter lists for both functions so that tuple structure
|
---|
| 519 | // doesn't affect unification. Must be a clone so that the types don't change.
|
---|
| 520 | std::unique_ptr<FunctionType> flatFunc( functionType->clone() );
|
---|
| 521 | std::unique_ptr<FunctionType> flatOther( otherFunction->clone() );
|
---|
| 522 | flattenList( flatFunc->get_parameters(), flatFunc->get_parameters(), env );
|
---|
| 523 | flattenList( flatOther->get_parameters(), flatOther->get_parameters(), env );
|
---|
| 524 |
|
---|
[53e3b4a] | 525 | // sizes don't have to match if ttypes are involved; need to be more precise wrt where the ttype is to prevent errors
|
---|
[7870799] | 526 | if (
|
---|
| 527 | (flatFunc->parameters.size() == flatOther->parameters.size() &&
|
---|
| 528 | flatFunc->returnVals.size() == flatOther->returnVals.size())
|
---|
| 529 | || flatFunc->isTtype()
|
---|
| 530 | || flatOther->isTtype()
|
---|
[f474e91] | 531 | ) {
|
---|
[954c954] | 532 | if ( unifyTypeList( flatFunc->parameters.begin(), flatFunc->parameters.end(), flatOther->parameters.begin(), flatOther->parameters.end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
|
---|
| 533 | if ( unifyTypeList( flatFunc->returnVals.begin(), flatFunc->returnVals.end(), flatOther->returnVals.begin(), flatOther->returnVals.end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
|
---|
[1cbca6e] | 534 |
|
---|
[6c3a988f] | 535 | // the original types must be used in mark assertions, since pointer comparisons are used
|
---|
[03da511] | 536 | markAssertions( haveAssertions, needAssertions, functionType );
|
---|
| 537 | markAssertions( haveAssertions, needAssertions, otherFunction );
|
---|
[41a2620] | 538 |
|
---|
[03da511] | 539 | result = true;
|
---|
| 540 | } // if
|
---|
[a32b204] | 541 | } // if
|
---|
| 542 | } // if
|
---|
| 543 | } // if
|
---|
| 544 | }
|
---|
| 545 |
|
---|
| 546 | template< typename RefType >
|
---|
[f474e91] | 547 | void Unify_old::handleRefType( RefType *inst, Type *other ) {
|
---|
[02ec390] | 548 | // check that other type is compatible and named the same
|
---|
[a32b204] | 549 | RefType *otherStruct = dynamic_cast< RefType* >( other );
|
---|
[538334a] | 550 | result = otherStruct && inst->name == otherStruct->name;
|
---|
[02ec390] | 551 | }
|
---|
| 552 |
|
---|
| 553 | template< typename RefType >
|
---|
[f474e91] | 554 | void Unify_old::handleGenericRefType( RefType *inst, Type *other ) {
|
---|
[02ec390] | 555 | // Check that other type is compatible and named the same
|
---|
| 556 | handleRefType( inst, other );
|
---|
| 557 | if ( ! result ) return;
|
---|
[f5234f3] | 558 | // Check that parameters of types unify, if any
|
---|
[538334a] | 559 | std::list< Expression* > params = inst->parameters;
|
---|
| 560 | std::list< Expression* > otherParams = ((RefType*)other)->parameters;
|
---|
[f5234f3] | 561 |
|
---|
| 562 | std::list< Expression* >::const_iterator it = params.begin(), jt = otherParams.begin();
|
---|
| 563 | for ( ; it != params.end() && jt != otherParams.end(); ++it, ++jt ) {
|
---|
| 564 | TypeExpr *param = dynamic_cast< TypeExpr* >(*it);
|
---|
[b2daebd4] | 565 | assertf(param, "Aggregate parameters should be type expressions");
|
---|
[f5234f3] | 566 | TypeExpr *otherParam = dynamic_cast< TypeExpr* >(*jt);
|
---|
[b2daebd4] | 567 | assertf(otherParam, "Aggregate parameters should be type expressions");
|
---|
[ce8c12f] | 568 |
|
---|
[b2daebd4] | 569 | Type* paramTy = param->get_type();
|
---|
| 570 | Type* otherParamTy = otherParam->get_type();
|
---|
[f5234f3] | 571 |
|
---|
[b2daebd4] | 572 | bool tupleParam = Tuples::isTtype( paramTy );
|
---|
| 573 | bool otherTupleParam = Tuples::isTtype( otherParamTy );
|
---|
| 574 |
|
---|
| 575 | if ( tupleParam && otherTupleParam ) {
|
---|
| 576 | ++it; ++jt; // skip ttype parameters for break
|
---|
| 577 | } else if ( tupleParam ) {
|
---|
| 578 | // bundle other parameters into tuple to match
|
---|
[62423350] | 579 | std::list< Type * > binderTypes;
|
---|
[b2daebd4] | 580 |
|
---|
| 581 | do {
|
---|
[62423350] | 582 | binderTypes.push_back( otherParam->get_type()->clone() );
|
---|
[b2daebd4] | 583 | ++jt;
|
---|
| 584 |
|
---|
| 585 | if ( jt == otherParams.end() ) break;
|
---|
| 586 |
|
---|
| 587 | otherParam = dynamic_cast< TypeExpr* >(*jt);
|
---|
| 588 | assertf(otherParam, "Aggregate parameters should be type expressions");
|
---|
| 589 | } while (true);
|
---|
| 590 |
|
---|
[62423350] | 591 | otherParamTy = new TupleType{ paramTy->get_qualifiers(), binderTypes };
|
---|
[b2daebd4] | 592 | ++it; // skip ttype parameter for break
|
---|
| 593 | } else if ( otherTupleParam ) {
|
---|
| 594 | // bundle parameters into tuple to match other
|
---|
[62423350] | 595 | std::list< Type * > binderTypes;
|
---|
[b2daebd4] | 596 |
|
---|
| 597 | do {
|
---|
[62423350] | 598 | binderTypes.push_back( param->get_type()->clone() );
|
---|
[b2daebd4] | 599 | ++it;
|
---|
| 600 |
|
---|
| 601 | if ( it == params.end() ) break;
|
---|
| 602 |
|
---|
| 603 | param = dynamic_cast< TypeExpr* >(*it);
|
---|
| 604 | assertf(param, "Aggregate parameters should be type expressions");
|
---|
| 605 | } while (true);
|
---|
| 606 |
|
---|
[62423350] | 607 | paramTy = new TupleType{ otherParamTy->get_qualifiers(), binderTypes };
|
---|
[b2daebd4] | 608 | ++jt; // skip ttype parameter for break
|
---|
| 609 | }
|
---|
| 610 |
|
---|
| 611 | if ( ! unifyExact( paramTy, otherParamTy, env, needAssertions, haveAssertions, openVars, WidenMode(false, false), indexer ) ) {
|
---|
[02ec390] | 612 | result = false;
|
---|
| 613 | return;
|
---|
| 614 | }
|
---|
[b2daebd4] | 615 |
|
---|
| 616 | // ttype parameter should be last
|
---|
| 617 | if ( tupleParam || otherTupleParam ) break;
|
---|
[02ec390] | 618 | }
|
---|
[f5234f3] | 619 | result = ( it == params.end() && jt == otherParams.end() );
|
---|
[02ec390] | 620 | }
|
---|
[a32b204] | 621 |
|
---|
[f474e91] | 622 | void Unify_old::postvisit(StructInstType *structInst) {
|
---|
[02ec390] | 623 | handleGenericRefType( structInst, type2 );
|
---|
[a32b204] | 624 | }
|
---|
| 625 |
|
---|
[f474e91] | 626 | void Unify_old::postvisit(UnionInstType *unionInst) {
|
---|
[02ec390] | 627 | handleGenericRefType( unionInst, type2 );
|
---|
[a32b204] | 628 | }
|
---|
| 629 |
|
---|
[f474e91] | 630 | void Unify_old::postvisit(EnumInstType *enumInst) {
|
---|
[a32b204] | 631 | handleRefType( enumInst, type2 );
|
---|
| 632 | }
|
---|
| 633 |
|
---|
[f474e91] | 634 | void Unify_old::postvisit(TraitInstType *contextInst) {
|
---|
[a32b204] | 635 | handleRefType( contextInst, type2 );
|
---|
| 636 | }
|
---|
| 637 |
|
---|
[f474e91] | 638 | void Unify_old::postvisit(TypeInstType *typeInst) {
|
---|
[a32b204] | 639 | assert( openVars.find( typeInst->get_name() ) == openVars.end() );
|
---|
| 640 | TypeInstType *otherInst = dynamic_cast< TypeInstType* >( type2 );
|
---|
| 641 | if ( otherInst && typeInst->get_name() == otherInst->get_name() ) {
|
---|
| 642 | result = true;
|
---|
[51b73452] | 643 | /// } else {
|
---|
| 644 | /// NamedTypeDecl *nt = indexer.lookupType( typeInst->get_name() );
|
---|
[a32b204] | 645 | /// if ( nt ) {
|
---|
[51b73452] | 646 | /// TypeDecl *type = dynamic_cast< TypeDecl* >( nt );
|
---|
| 647 | /// assert( type );
|
---|
[a32b204] | 648 | /// if ( type->get_base() ) {
|
---|
[51b73452] | 649 | /// result = unifyExact( type->get_base(), typeInst, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 650 | /// }
|
---|
| 651 | /// }
|
---|
[a32b204] | 652 | } // if
|
---|
| 653 | }
|
---|
| 654 |
|
---|
| 655 | template< typename Iterator1, typename Iterator2 >
|
---|
[c77fd8b] | 656 | bool unifyList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
[f3b0a07] | 657 | auto get_type = [](Type * t) { return t; };
|
---|
[a32b204] | 658 | for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
|
---|
[f3b0a07] | 659 | Type * t1 = *list1Begin;
|
---|
| 660 | Type * t2 = *list2Begin;
|
---|
| 661 | bool isTtype1 = Tuples::isTtype( t1 );
|
---|
| 662 | bool isTtype2 = Tuples::isTtype( t2 );
|
---|
| 663 | // xxx - assumes ttype must be last parameter
|
---|
| 664 | // xxx - there may be a nice way to refactor this, but be careful because the argument positioning might matter in some cases.
|
---|
| 665 | if ( isTtype1 && ! isTtype2 ) {
|
---|
| 666 | // combine all of the things in list2, then unify
|
---|
| 667 | return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 668 | } else if ( isTtype2 && ! isTtype1 ) {
|
---|
| 669 | // combine all of the things in list1, then unify
|
---|
| 670 | return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 671 | } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
|
---|
[a32b204] | 672 | return false;
|
---|
[f3b0a07] | 673 | } // if
|
---|
| 674 |
|
---|
[a32b204] | 675 | } // for
|
---|
[f3b0a07] | 676 | if ( list1Begin != list1End ) {
|
---|
| 677 | // try unifying empty tuple type with ttype
|
---|
| 678 | Type * t1 = *list1Begin;
|
---|
| 679 | if ( Tuples::isTtype( t1 ) ) {
|
---|
| 680 | return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 681 | } else return false;
|
---|
| 682 | } else if ( list2Begin != list2End ) {
|
---|
| 683 | // try unifying empty tuple type with ttype
|
---|
| 684 | Type * t2 = *list2Begin;
|
---|
| 685 | if ( Tuples::isTtype( t2 ) ) {
|
---|
| 686 | return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 687 | } else return false;
|
---|
[a32b204] | 688 | } else {
|
---|
| 689 | return true;
|
---|
[f3b0a07] | 690 | } // if
|
---|
[a32b204] | 691 | }
|
---|
| 692 |
|
---|
[f474e91] | 693 | void Unify_old::postvisit(TupleType *tupleType) {
|
---|
[a32b204] | 694 | if ( TupleType *otherTuple = dynamic_cast< TupleType* >( type2 ) ) {
|
---|
[f3b0a07] | 695 | std::unique_ptr<TupleType> flat1( tupleType->clone() );
|
---|
| 696 | std::unique_ptr<TupleType> flat2( otherTuple->clone() );
|
---|
| 697 | std::list<Type *> types1, types2;
|
---|
| 698 |
|
---|
[f474e91] | 699 | PassVisitor<TtypeExpander_old> expander( env );
|
---|
[f3b0a07] | 700 | flat1->acceptMutator( expander );
|
---|
| 701 | flat2->acceptMutator( expander );
|
---|
| 702 |
|
---|
| 703 | flatten( flat1.get(), back_inserter( types1 ) );
|
---|
| 704 | flatten( flat2.get(), back_inserter( types2 ) );
|
---|
| 705 |
|
---|
[c77fd8b] | 706 | result = unifyList( types1.begin(), types1.end(), types2.begin(), types2.end(), env, needAssertions, haveAssertions, openVars, indexer );
|
---|
[a32b204] | 707 | } // if
|
---|
| 708 | }
|
---|
[51b73452] | 709 |
|
---|
[f474e91] | 710 | void Unify_old::postvisit( __attribute__((unused)) VarArgsType *varArgsType ) {
|
---|
[44b7088] | 711 | result = dynamic_cast< VarArgsType* >( type2 );
|
---|
| 712 | }
|
---|
| 713 |
|
---|
[f474e91] | 714 | void Unify_old::postvisit( __attribute__((unused)) ZeroType *zeroType ) {
|
---|
[89e6ffc] | 715 | result = dynamic_cast< ZeroType* >( type2 );
|
---|
| 716 | }
|
---|
| 717 |
|
---|
[f474e91] | 718 | void Unify_old::postvisit( __attribute__((unused)) OneType *oneType ) {
|
---|
[89e6ffc] | 719 | result = dynamic_cast< OneType* >( type2 );
|
---|
| 720 | }
|
---|
| 721 |
|
---|
[906e24d] | 722 | Type * extractResultType( FunctionType * function ) {
|
---|
| 723 | if ( function->get_returnVals().size() == 0 ) {
|
---|
| 724 | return new VoidType( Type::Qualifiers() );
|
---|
| 725 | } else if ( function->get_returnVals().size() == 1 ) {
|
---|
| 726 | return function->get_returnVals().front()->get_type()->clone();
|
---|
| 727 | } else {
|
---|
[62423350] | 728 | std::list< Type * > types;
|
---|
[906e24d] | 729 | for ( DeclarationWithType * decl : function->get_returnVals() ) {
|
---|
[62423350] | 730 | types.push_back( decl->get_type()->clone() );
|
---|
[906e24d] | 731 | } // for
|
---|
[62423350] | 732 | return new TupleType( Type::Qualifiers(), types );
|
---|
[906e24d] | 733 | }
|
---|
| 734 | }
|
---|
[54e41b3] | 735 |
|
---|
[ef1da0e2] | 736 | namespace {
|
---|
| 737 | /// Replaces ttype variables with their bound types.
|
---|
| 738 | /// If this isn't done when satifying ttype assertions, then argument lists can have
|
---|
| 739 | /// different size and structure when they should be compatible.
|
---|
| 740 | struct TtypeExpander_new : public ast::WithShortCircuiting, public ast::PureVisitor {
|
---|
| 741 | ast::TypeEnvironment & tenv;
|
---|
| 742 |
|
---|
| 743 | TtypeExpander_new( ast::TypeEnvironment & env ) : tenv( env ) {}
|
---|
| 744 |
|
---|
| 745 | const ast::Type * postvisit( const ast::TypeInstType * typeInst ) {
|
---|
| 746 | if ( const ast::EqvClass * clz = tenv.lookup( *typeInst ) ) {
|
---|
| 747 | // expand ttype parameter into its actual type
|
---|
| 748 | if ( clz->data.kind == ast::TypeDecl::Ttype && clz->bound ) {
|
---|
| 749 | return clz->bound;
|
---|
| 750 | }
|
---|
| 751 | }
|
---|
| 752 | return typeInst;
|
---|
| 753 | }
|
---|
| 754 | };
|
---|
| 755 | }
|
---|
[0bd46fd] | 756 |
|
---|
[ef1da0e2] | 757 | std::vector< ast::ptr< ast::Type > > flattenList(
|
---|
| 758 | const std::vector< ast::ptr< ast::Type > > & src, ast::TypeEnvironment & env
|
---|
| 759 | ) {
|
---|
| 760 | std::vector< ast::ptr< ast::Type > > dst;
|
---|
| 761 | dst.reserve( src.size() );
|
---|
| 762 | for ( const auto & d : src ) {
|
---|
| 763 | ast::Pass<TtypeExpander_new> expander{ env };
|
---|
| 764 | // TtypeExpander pass is impure (may mutate nodes in place)
|
---|
| 765 | // need to make nodes shared to prevent accidental mutation
|
---|
| 766 | ast::ptr<ast::Type> dc = d->accept(expander);
|
---|
| 767 | auto types = flatten( dc );
|
---|
| 768 | for ( ast::ptr< ast::Type > & t : types ) {
|
---|
| 769 | // outermost const, volatile, _Atomic qualifiers in parameters should not play
|
---|
| 770 | // a role in the unification of function types, since they do not determine
|
---|
| 771 | // whether a function is callable.
|
---|
| 772 | // NOTE: **must** consider at least mutex qualifier, since functions can be
|
---|
| 773 | // overloaded on outermost mutex and a mutex function has different
|
---|
| 774 | // requirements than a non-mutex function
|
---|
| 775 | remove_qualifiers( t, ast::CV::Const | ast::CV::Volatile | ast::CV::Atomic );
|
---|
| 776 | dst.emplace_back( t );
|
---|
| 777 | }
|
---|
| 778 | }
|
---|
| 779 | return dst;
|
---|
| 780 | }
|
---|
| 781 |
|
---|
[f02f546] | 782 | // Unification of Expressions
|
---|
| 783 | //
|
---|
| 784 | // Boolean outcome (obvious): Are they basically spelled the same?
|
---|
| 785 | // Side effect of binding variables (subtle): if `sizeof(int)` ===_expr `sizeof(T)` then `int` ===_ty `T`
|
---|
| 786 | //
|
---|
| 787 | // Context: if `float[VAREXPR1]` ===_ty `float[VAREXPR2]` then `VAREXPR1` ===_expr `VAREXPR2`
|
---|
| 788 | // where the VAREXPR are meant as notational metavariables representing the fact that unification always
|
---|
| 789 | // sees distinct ast::VariableExpr objects at these positions
|
---|
| 790 |
|
---|
| 791 | static bool unify( const ast::Expr * e1, const ast::Expr * e2, ast::TypeEnvironment & env,
|
---|
| 792 | ast::AssertionSet & need, ast::AssertionSet & have, const ast::OpenVarSet & open,
|
---|
| 793 | WidenMode widen );
|
---|
| 794 |
|
---|
| 795 | class UnifyExpr final : public ast::WithShortCircuiting {
|
---|
| 796 | const ast::Expr * e2;
|
---|
| 797 | ast::TypeEnvironment & tenv;
|
---|
| 798 | ast::AssertionSet & need;
|
---|
| 799 | ast::AssertionSet & have;
|
---|
| 800 | const ast::OpenVarSet & open;
|
---|
| 801 | WidenMode widen;
|
---|
| 802 | public:
|
---|
| 803 | bool result;
|
---|
| 804 |
|
---|
| 805 | private:
|
---|
| 806 |
|
---|
| 807 | void tryMatchOnStaticValue( const ast::Expr * e1 ) {
|
---|
| 808 | Evaluation r1 = eval(e1);
|
---|
| 809 | Evaluation r2 = eval(e2);
|
---|
| 810 |
|
---|
| 811 | if ( ! r1.hasKnownValue ) return;
|
---|
| 812 | if ( ! r2.hasKnownValue ) return;
|
---|
| 813 |
|
---|
| 814 | if (r1.knownValue != r2.knownValue) return;
|
---|
| 815 |
|
---|
| 816 | visit_children = false;
|
---|
| 817 | result = true;
|
---|
| 818 | }
|
---|
| 819 |
|
---|
| 820 | public:
|
---|
| 821 |
|
---|
| 822 | void previsit( const ast::Node * ) { assert(false); }
|
---|
| 823 |
|
---|
| 824 | void previsit( const ast::Expr * e1 ) {
|
---|
| 825 | tryMatchOnStaticValue( e1 );
|
---|
| 826 | visit_children = false;
|
---|
| 827 | }
|
---|
| 828 |
|
---|
| 829 | void previsit( const ast::CastExpr * e1 ) {
|
---|
| 830 | tryMatchOnStaticValue( e1 );
|
---|
| 831 |
|
---|
| 832 | if (result) {
|
---|
| 833 | assert (visit_children == false);
|
---|
| 834 | } else {
|
---|
| 835 | assert (visit_children == true);
|
---|
| 836 | visit_children = false;
|
---|
| 837 |
|
---|
| 838 | auto e2c = dynamic_cast< const ast::CastExpr * >( e2 );
|
---|
| 839 | if ( ! e2c ) return;
|
---|
| 840 |
|
---|
| 841 | // inspect casts' target types
|
---|
| 842 | if ( ! unifyExact(
|
---|
| 843 | e1->result, e2c->result, tenv, need, have, open, widen ) ) return;
|
---|
| 844 |
|
---|
| 845 | // inspect casts' inner expressions
|
---|
| 846 | result = unify( e1->arg, e2c->arg, tenv, need, have, open, widen );
|
---|
| 847 | }
|
---|
| 848 | }
|
---|
| 849 |
|
---|
| 850 | void previsit( const ast::VariableExpr * e1 ) {
|
---|
| 851 | tryMatchOnStaticValue( e1 );
|
---|
| 852 |
|
---|
| 853 | if (result) {
|
---|
| 854 | assert (visit_children == false);
|
---|
| 855 | } else {
|
---|
| 856 | assert (visit_children == true);
|
---|
| 857 | visit_children = false;
|
---|
| 858 |
|
---|
| 859 | auto e2v = dynamic_cast< const ast::VariableExpr * >( e2 );
|
---|
| 860 | if ( ! e2v ) return;
|
---|
| 861 |
|
---|
| 862 | assert(e1->var);
|
---|
| 863 | assert(e2v->var);
|
---|
| 864 |
|
---|
| 865 | // conservative: variable exprs match if their declarations are represented by the same C++ AST object
|
---|
| 866 | result = (e1->var == e2v->var);
|
---|
| 867 | }
|
---|
| 868 | }
|
---|
| 869 |
|
---|
| 870 | void previsit( const ast::SizeofExpr * e1 ) {
|
---|
| 871 | tryMatchOnStaticValue( e1 );
|
---|
| 872 |
|
---|
| 873 | if (result) {
|
---|
| 874 | assert (visit_children == false);
|
---|
| 875 | } else {
|
---|
| 876 | assert (visit_children == true);
|
---|
| 877 | visit_children = false;
|
---|
| 878 |
|
---|
| 879 | auto e2so = dynamic_cast< const ast::SizeofExpr * >( e2 );
|
---|
| 880 | if ( ! e2so ) return;
|
---|
| 881 |
|
---|
| 882 | assert((e1->type != nullptr) ^ (e1->expr != nullptr));
|
---|
| 883 | assert((e2so->type != nullptr) ^ (e2so->expr != nullptr));
|
---|
| 884 | if ( ! (e1->type && e2so->type) ) return;
|
---|
| 885 |
|
---|
| 886 | // expression unification calls type unification (mutual recursion)
|
---|
| 887 | result = unifyExact( e1->type, e2so->type, tenv, need, have, open, widen );
|
---|
| 888 | }
|
---|
| 889 | }
|
---|
| 890 |
|
---|
| 891 | UnifyExpr( const ast::Expr * e2, ast::TypeEnvironment & env, ast::AssertionSet & need,
|
---|
| 892 | ast::AssertionSet & have, const ast::OpenVarSet & open, WidenMode widen )
|
---|
| 893 | : e2( e2 ), tenv(env), need(need), have(have), open(open), widen(widen), result(false) {}
|
---|
| 894 | };
|
---|
| 895 |
|
---|
| 896 | static bool unify( const ast::Expr * e1, const ast::Expr * e2, ast::TypeEnvironment & env,
|
---|
| 897 | ast::AssertionSet & need, ast::AssertionSet & have, const ast::OpenVarSet & open,
|
---|
| 898 | WidenMode widen ) {
|
---|
| 899 | assert( e1 && e2 );
|
---|
| 900 | return ast::Pass<UnifyExpr>::read( e1, e2, env, need, have, open, widen );
|
---|
| 901 | }
|
---|
| 902 |
|
---|
[ee574a2] | 903 | class Unify_new final : public ast::WithShortCircuiting {
|
---|
[f474e91] | 904 | const ast::Type * type2;
|
---|
| 905 | ast::TypeEnvironment & tenv;
|
---|
| 906 | ast::AssertionSet & need;
|
---|
| 907 | ast::AssertionSet & have;
|
---|
| 908 | const ast::OpenVarSet & open;
|
---|
| 909 | WidenMode widen;
|
---|
| 910 | public:
|
---|
[c15085d] | 911 | static size_t traceId;
|
---|
[f474e91] | 912 | bool result;
|
---|
| 913 |
|
---|
[7870799] | 914 | Unify_new(
|
---|
| 915 | const ast::Type * type2, ast::TypeEnvironment & env, ast::AssertionSet & need,
|
---|
[251ce80] | 916 | ast::AssertionSet & have, const ast::OpenVarSet & open, WidenMode widen )
|
---|
[7870799] | 917 | : type2(type2), tenv(env), need(need), have(have), open(open), widen(widen),
|
---|
[251ce80] | 918 | result(false) {}
|
---|
[f474e91] | 919 |
|
---|
| 920 | void previsit( const ast::Node * ) { visit_children = false; }
|
---|
[7870799] | 921 |
|
---|
[ee574a2] | 922 | void postvisit( const ast::VoidType * ) {
|
---|
[f474e91] | 923 | result = dynamic_cast< const ast::VoidType * >( type2 );
|
---|
| 924 | }
|
---|
| 925 |
|
---|
[ee574a2] | 926 | void postvisit( const ast::BasicType * basic ) {
|
---|
[f474e91] | 927 | if ( auto basic2 = dynamic_cast< const ast::BasicType * >( type2 ) ) {
|
---|
| 928 | result = basic->kind == basic2->kind;
|
---|
| 929 | }
|
---|
| 930 | }
|
---|
| 931 |
|
---|
[ee574a2] | 932 | void postvisit( const ast::PointerType * pointer ) {
|
---|
[f474e91] | 933 | if ( auto pointer2 = dynamic_cast< const ast::PointerType * >( type2 ) ) {
|
---|
[7870799] | 934 | result = unifyExact(
|
---|
| 935 | pointer->base, pointer2->base, tenv, need, have, open,
|
---|
[251ce80] | 936 | noWiden());
|
---|
[f474e91] | 937 | }
|
---|
| 938 | }
|
---|
| 939 |
|
---|
[ee574a2] | 940 | void postvisit( const ast::ArrayType * array ) {
|
---|
[f474e91] | 941 | auto array2 = dynamic_cast< const ast::ArrayType * >( type2 );
|
---|
| 942 | if ( ! array2 ) return;
|
---|
| 943 |
|
---|
| 944 | if ( array->isVarLen != array2->isVarLen ) return;
|
---|
[f02f546] | 945 | if ( (array->dimension != nullptr) != (array2->dimension != nullptr) ) return;
|
---|
[f474e91] | 946 |
|
---|
[f02f546] | 947 | if ( array->dimension ) {
|
---|
| 948 | assert( array2->dimension );
|
---|
| 949 | // type unification calls expression unification (mutual recursion)
|
---|
| 950 | if ( ! unify(array->dimension, array2->dimension,
|
---|
| 951 | tenv, need, have, open, widen) ) return;
|
---|
[f474e91] | 952 | }
|
---|
| 953 |
|
---|
[7870799] | 954 | result = unifyExact(
|
---|
[251ce80] | 955 | array->base, array2->base, tenv, need, have, open, noWiden());
|
---|
[f474e91] | 956 | }
|
---|
| 957 |
|
---|
[ee574a2] | 958 | void postvisit( const ast::ReferenceType * ref ) {
|
---|
[f474e91] | 959 | if ( auto ref2 = dynamic_cast< const ast::ReferenceType * >( type2 ) ) {
|
---|
[7870799] | 960 | result = unifyExact(
|
---|
[251ce80] | 961 | ref->base, ref2->base, tenv, need, have, open, noWiden());
|
---|
[f474e91] | 962 | }
|
---|
| 963 | }
|
---|
| 964 |
|
---|
| 965 | private:
|
---|
| 966 |
|
---|
| 967 | template< typename Iter >
|
---|
[954c954] | 968 | static bool unifyTypeList(
|
---|
[7870799] | 969 | Iter crnt1, Iter end1, Iter crnt2, Iter end2, ast::TypeEnvironment & env,
|
---|
[251ce80] | 970 | ast::AssertionSet & need, ast::AssertionSet & have, const ast::OpenVarSet & open
|
---|
[f474e91] | 971 | ) {
|
---|
| 972 | while ( crnt1 != end1 && crnt2 != end2 ) {
|
---|
[954c954] | 973 | const ast::Type * t1 = *crnt1;
|
---|
| 974 | const ast::Type * t2 = *crnt2;
|
---|
[f474e91] | 975 | bool isTuple1 = Tuples::isTtype( t1 );
|
---|
| 976 | bool isTuple2 = Tuples::isTtype( t2 );
|
---|
| 977 |
|
---|
| 978 | // assumes here that ttype *must* be last parameter
|
---|
| 979 | if ( isTuple1 && ! isTuple2 ) {
|
---|
| 980 | // combine remainder of list2, then unify
|
---|
[7870799] | 981 | return unifyExact(
|
---|
[954c954] | 982 | t1, tupleFromTypes( crnt2, end2 ), env, need, have, open,
|
---|
[251ce80] | 983 | noWiden() );
|
---|
[f474e91] | 984 | } else if ( ! isTuple1 && isTuple2 ) {
|
---|
| 985 | // combine remainder of list1, then unify
|
---|
[7870799] | 986 | return unifyExact(
|
---|
[954c954] | 987 | tupleFromTypes( crnt1, end1 ), t2, env, need, have, open,
|
---|
[251ce80] | 988 | noWiden() );
|
---|
[f474e91] | 989 | }
|
---|
| 990 |
|
---|
[7870799] | 991 | if ( ! unifyExact(
|
---|
[251ce80] | 992 | t1, t2, env, need, have, open, noWiden() )
|
---|
[f474e91] | 993 | ) return false;
|
---|
| 994 |
|
---|
| 995 | ++crnt1; ++crnt2;
|
---|
| 996 | }
|
---|
| 997 |
|
---|
[7870799] | 998 | // May get to the end of one argument list before the other. This is only okay if the
|
---|
[f474e91] | 999 | // other is a ttype
|
---|
| 1000 | if ( crnt1 != end1 ) {
|
---|
| 1001 | // try unifying empty tuple with ttype
|
---|
[954c954] | 1002 | const ast::Type * t1 = *crnt1;
|
---|
[f474e91] | 1003 | if ( ! Tuples::isTtype( t1 ) ) return false;
|
---|
[7870799] | 1004 | return unifyExact(
|
---|
[954c954] | 1005 | t1, tupleFromTypes( crnt2, end2 ), env, need, have, open,
|
---|
[251ce80] | 1006 | noWiden() );
|
---|
[f474e91] | 1007 | } else if ( crnt2 != end2 ) {
|
---|
| 1008 | // try unifying empty tuple with ttype
|
---|
[954c954] | 1009 | const ast::Type * t2 = *crnt2;
|
---|
[f474e91] | 1010 | if ( ! Tuples::isTtype( t2 ) ) return false;
|
---|
[7870799] | 1011 | return unifyExact(
|
---|
[954c954] | 1012 | tupleFromTypes( crnt1, end1 ), t2, env, need, have, open,
|
---|
[251ce80] | 1013 | noWiden() );
|
---|
[f474e91] | 1014 | }
|
---|
| 1015 |
|
---|
| 1016 | return true;
|
---|
| 1017 | }
|
---|
| 1018 |
|
---|
[954c954] | 1019 | static bool unifyTypeList(
|
---|
| 1020 | const std::vector< ast::ptr< ast::Type > > & list1,
|
---|
| 1021 | const std::vector< ast::ptr< ast::Type > > & list2,
|
---|
[7870799] | 1022 | ast::TypeEnvironment & env, ast::AssertionSet & need, ast::AssertionSet & have,
|
---|
[251ce80] | 1023 | const ast::OpenVarSet & open
|
---|
[f474e91] | 1024 | ) {
|
---|
[954c954] | 1025 | return unifyTypeList(
|
---|
[251ce80] | 1026 | list1.begin(), list1.end(), list2.begin(), list2.end(), env, need, have, open);
|
---|
[f474e91] | 1027 | }
|
---|
| 1028 |
|
---|
[3e5dd913] | 1029 | static void markAssertionSet( ast::AssertionSet & assns, const ast::VariableExpr * assn ) {
|
---|
[f474e91] | 1030 | auto i = assns.find( assn );
|
---|
| 1031 | if ( i != assns.end() ) {
|
---|
| 1032 | i->second.isUsed = true;
|
---|
| 1033 | }
|
---|
| 1034 | }
|
---|
| 1035 |
|
---|
| 1036 | /// mark all assertions in `type` used in both `assn1` and `assn2`
|
---|
[7870799] | 1037 | static void markAssertions(
|
---|
| 1038 | ast::AssertionSet & assn1, ast::AssertionSet & assn2,
|
---|
[361bf01] | 1039 | const ast::FunctionType * type
|
---|
[f474e91] | 1040 | ) {
|
---|
[3e5dd913] | 1041 | for ( auto & assert : type->assertions ) {
|
---|
| 1042 | markAssertionSet( assn1, assert );
|
---|
| 1043 | markAssertionSet( assn2, assert );
|
---|
[f474e91] | 1044 | }
|
---|
| 1045 | }
|
---|
| 1046 |
|
---|
| 1047 | public:
|
---|
[ee574a2] | 1048 | void postvisit( const ast::FunctionType * func ) {
|
---|
[f474e91] | 1049 | auto func2 = dynamic_cast< const ast::FunctionType * >( type2 );
|
---|
| 1050 | if ( ! func2 ) return;
|
---|
| 1051 |
|
---|
| 1052 | if ( func->isVarArgs != func2->isVarArgs ) return;
|
---|
[7870799] | 1053 |
|
---|
| 1054 | // Flatten the parameter lists for both functions so that tuple structure does not
|
---|
[f474e91] | 1055 | // affect unification. Does not actually mutate function parameters.
|
---|
| 1056 | auto params = flattenList( func->params, tenv );
|
---|
| 1057 | auto params2 = flattenList( func2->params, tenv );
|
---|
| 1058 |
|
---|
[7870799] | 1059 | // sizes don't have to match if ttypes are involved; need to be more precise w.r.t.
|
---|
[f474e91] | 1060 | // where the ttype is to prevent errors
|
---|
[7870799] | 1061 | if (
|
---|
[f474e91] | 1062 | ( params.size() != params2.size() || func->returns.size() != func2->returns.size() )
|
---|
| 1063 | && ! func->isTtype()
|
---|
| 1064 | && ! func2->isTtype()
|
---|
| 1065 | ) return;
|
---|
| 1066 |
|
---|
[251ce80] | 1067 | if ( ! unifyTypeList( params, params2, tenv, need, have, open ) ) return;
|
---|
[954c954] | 1068 | if ( ! unifyTypeList(
|
---|
[251ce80] | 1069 | func->returns, func2->returns, tenv, need, have, open ) ) return;
|
---|
[7870799] | 1070 |
|
---|
[f474e91] | 1071 | markAssertions( have, need, func );
|
---|
| 1072 | markAssertions( have, need, func2 );
|
---|
| 1073 |
|
---|
| 1074 | result = true;
|
---|
| 1075 | }
|
---|
[7870799] | 1076 |
|
---|
[f474e91] | 1077 | private:
|
---|
[90ce35aa] | 1078 | // Returns: other, cast as XInstType
|
---|
| 1079 | // Assigns this->result: whether types are compatible (up to generic parameters)
|
---|
| 1080 | template< typename XInstType >
|
---|
| 1081 | const XInstType * handleRefType( const XInstType * inst, const ast::Type * other ) {
|
---|
[f474e91] | 1082 | // check that the other type is compatible and named the same
|
---|
[90ce35aa] | 1083 | auto otherInst = dynamic_cast< const XInstType * >( other );
|
---|
[24d6572] | 1084 | if (otherInst && inst->name == otherInst->name)
|
---|
| 1085 | this->result = otherInst;
|
---|
[f474e91] | 1086 | return otherInst;
|
---|
| 1087 | }
|
---|
| 1088 |
|
---|
| 1089 | /// Creates a tuple type based on a list of TypeExpr
|
---|
| 1090 | template< typename Iter >
|
---|
[7870799] | 1091 | static const ast::Type * tupleFromExprs(
|
---|
[f474e91] | 1092 | const ast::TypeExpr * param, Iter & crnt, Iter end, ast::CV::Qualifiers qs
|
---|
| 1093 | ) {
|
---|
| 1094 | std::vector< ast::ptr< ast::Type > > types;
|
---|
| 1095 | do {
|
---|
| 1096 | types.emplace_back( param->type );
|
---|
| 1097 |
|
---|
| 1098 | ++crnt;
|
---|
| 1099 | if ( crnt == end ) break;
|
---|
| 1100 | param = strict_dynamic_cast< const ast::TypeExpr * >( crnt->get() );
|
---|
| 1101 | } while(true);
|
---|
| 1102 |
|
---|
| 1103 | return new ast::TupleType{ std::move(types), qs };
|
---|
| 1104 | }
|
---|
| 1105 |
|
---|
[90ce35aa] | 1106 | template< typename XInstType >
|
---|
| 1107 | void handleGenericRefType( const XInstType * inst, const ast::Type * other ) {
|
---|
[f474e91] | 1108 | // check that other type is compatible and named the same
|
---|
[90ce35aa] | 1109 | const XInstType * otherInst = handleRefType( inst, other );
|
---|
| 1110 | if ( ! this->result ) return;
|
---|
[7870799] | 1111 |
|
---|
[f474e91] | 1112 | // check that parameters of types unify, if any
|
---|
| 1113 | const std::vector< ast::ptr< ast::Expr > > & params = inst->params;
|
---|
[90ce35aa] | 1114 | const std::vector< ast::ptr< ast::Expr > > & params2 = otherInst->params;
|
---|
[f474e91] | 1115 |
|
---|
| 1116 | auto it = params.begin();
|
---|
| 1117 | auto jt = params2.begin();
|
---|
| 1118 | for ( ; it != params.end() && jt != params2.end(); ++it, ++jt ) {
|
---|
| 1119 | auto param = strict_dynamic_cast< const ast::TypeExpr * >( it->get() );
|
---|
| 1120 | auto param2 = strict_dynamic_cast< const ast::TypeExpr * >( jt->get() );
|
---|
| 1121 |
|
---|
| 1122 | ast::ptr< ast::Type > pty = param->type;
|
---|
| 1123 | ast::ptr< ast::Type > pty2 = param2->type;
|
---|
| 1124 |
|
---|
| 1125 | bool isTuple = Tuples::isTtype( pty );
|
---|
| 1126 | bool isTuple2 = Tuples::isTtype( pty2 );
|
---|
| 1127 |
|
---|
| 1128 | if ( isTuple && isTuple2 ) {
|
---|
| 1129 | ++it; ++jt; // skip ttype parameters before break
|
---|
[0bd46fd] | 1130 | } else if ( isTuple ) {
|
---|
[f474e91] | 1131 | // bundle remaining params into tuple
|
---|
| 1132 | pty2 = tupleFromExprs( param2, jt, params2.end(), pty->qualifiers );
|
---|
| 1133 | ++it; // skip ttype parameter for break
|
---|
| 1134 | } else if ( isTuple2 ) {
|
---|
| 1135 | // bundle remaining params into tuple
|
---|
| 1136 | pty = tupleFromExprs( param, it, params.end(), pty2->qualifiers );
|
---|
| 1137 | ++jt; // skip ttype parameter for break
|
---|
| 1138 | }
|
---|
| 1139 |
|
---|
[7870799] | 1140 | if ( ! unifyExact(
|
---|
[251ce80] | 1141 | pty, pty2, tenv, need, have, open, noWiden() ) ) {
|
---|
[f474e91] | 1142 | result = false;
|
---|
| 1143 | return;
|
---|
| 1144 | }
|
---|
| 1145 |
|
---|
| 1146 | // ttype parameter should be last
|
---|
| 1147 | if ( isTuple || isTuple2 ) break;
|
---|
| 1148 | }
|
---|
| 1149 | result = it == params.end() && jt == params2.end();
|
---|
| 1150 | }
|
---|
| 1151 |
|
---|
| 1152 | public:
|
---|
[ee574a2] | 1153 | void postvisit( const ast::StructInstType * aggrType ) {
|
---|
[f474e91] | 1154 | handleGenericRefType( aggrType, type2 );
|
---|
| 1155 | }
|
---|
| 1156 |
|
---|
[ee574a2] | 1157 | void postvisit( const ast::UnionInstType * aggrType ) {
|
---|
[f474e91] | 1158 | handleGenericRefType( aggrType, type2 );
|
---|
| 1159 | }
|
---|
| 1160 |
|
---|
[ee574a2] | 1161 | void postvisit( const ast::EnumInstType * aggrType ) {
|
---|
[f474e91] | 1162 | handleRefType( aggrType, type2 );
|
---|
| 1163 | }
|
---|
| 1164 |
|
---|
[ee574a2] | 1165 | void postvisit( const ast::TraitInstType * aggrType ) {
|
---|
[f474e91] | 1166 | handleRefType( aggrType, type2 );
|
---|
| 1167 | }
|
---|
| 1168 |
|
---|
[ee574a2] | 1169 | void postvisit( const ast::TypeInstType * typeInst ) {
|
---|
[46da46b] | 1170 | // assert( open.find( *typeInst ) == open.end() );
|
---|
[24d6572] | 1171 | auto otherInst = dynamic_cast< const ast::TypeInstType * >( type2 );
|
---|
| 1172 | if (otherInst && typeInst->name == otherInst->name)
|
---|
| 1173 | this->result = otherInst;
|
---|
| 1174 | // return otherInst;
|
---|
[f474e91] | 1175 | }
|
---|
| 1176 |
|
---|
| 1177 | private:
|
---|
| 1178 | /// Creates a tuple type based on a list of Type
|
---|
[0bd46fd] | 1179 |
|
---|
[7870799] | 1180 | static bool unifyList(
|
---|
| 1181 | const std::vector< ast::ptr< ast::Type > > & list1,
|
---|
| 1182 | const std::vector< ast::ptr< ast::Type > > & list2, ast::TypeEnvironment & env,
|
---|
[251ce80] | 1183 | ast::AssertionSet & need, ast::AssertionSet & have, const ast::OpenVarSet & open
|
---|
[f474e91] | 1184 | ) {
|
---|
| 1185 | auto crnt1 = list1.begin();
|
---|
| 1186 | auto crnt2 = list2.begin();
|
---|
| 1187 | while ( crnt1 != list1.end() && crnt2 != list2.end() ) {
|
---|
| 1188 | const ast::Type * t1 = *crnt1;
|
---|
| 1189 | const ast::Type * t2 = *crnt2;
|
---|
| 1190 | bool isTuple1 = Tuples::isTtype( t1 );
|
---|
| 1191 | bool isTuple2 = Tuples::isTtype( t2 );
|
---|
| 1192 |
|
---|
| 1193 | // assumes ttype must be last parameter
|
---|
| 1194 | if ( isTuple1 && ! isTuple2 ) {
|
---|
| 1195 | // combine entirety of list2, then unify
|
---|
[7870799] | 1196 | return unifyExact(
|
---|
| 1197 | t1, tupleFromTypes( list2 ), env, need, have, open,
|
---|
[251ce80] | 1198 | noWiden() );
|
---|
[f474e91] | 1199 | } else if ( ! isTuple1 && isTuple2 ) {
|
---|
| 1200 | // combine entirety of list1, then unify
|
---|
| 1201 | return unifyExact(
|
---|
[7870799] | 1202 | tupleFromTypes( list1 ), t2, env, need, have, open,
|
---|
[251ce80] | 1203 | noWiden() );
|
---|
[f474e91] | 1204 | }
|
---|
| 1205 |
|
---|
[7870799] | 1206 | if ( ! unifyExact(
|
---|
[251ce80] | 1207 | t1, t2, env, need, have, open, noWiden() )
|
---|
[f474e91] | 1208 | ) return false;
|
---|
| 1209 |
|
---|
| 1210 | ++crnt1; ++crnt2;
|
---|
| 1211 | }
|
---|
| 1212 |
|
---|
| 1213 | if ( crnt1 != list1.end() ) {
|
---|
| 1214 | // try unifying empty tuple type with ttype
|
---|
| 1215 | const ast::Type * t1 = *crnt1;
|
---|
| 1216 | if ( ! Tuples::isTtype( t1 ) ) return false;
|
---|
[7870799] | 1217 | // xxx - this doesn't generate an empty tuple, contrary to comment; both ported
|
---|
[f474e91] | 1218 | // from Rob's code
|
---|
[7870799] | 1219 | return unifyExact(
|
---|
| 1220 | t1, tupleFromTypes( list2 ), env, need, have, open,
|
---|
[251ce80] | 1221 | noWiden() );
|
---|
[f474e91] | 1222 | } else if ( crnt2 != list2.end() ) {
|
---|
| 1223 | // try unifying empty tuple with ttype
|
---|
| 1224 | const ast::Type * t2 = *crnt2;
|
---|
| 1225 | if ( ! Tuples::isTtype( t2 ) ) return false;
|
---|
[7870799] | 1226 | // xxx - this doesn't generate an empty tuple, contrary to comment; both ported
|
---|
[f474e91] | 1227 | // from Rob's code
|
---|
| 1228 | return unifyExact(
|
---|
[7870799] | 1229 | tupleFromTypes( list1 ), t2, env, need, have, open,
|
---|
[251ce80] | 1230 | noWiden() );
|
---|
[f474e91] | 1231 | }
|
---|
| 1232 |
|
---|
| 1233 | return true;
|
---|
| 1234 | }
|
---|
| 1235 |
|
---|
| 1236 | public:
|
---|
[ee574a2] | 1237 | void postvisit( const ast::TupleType * tuple ) {
|
---|
[f474e91] | 1238 | auto tuple2 = dynamic_cast< const ast::TupleType * >( type2 );
|
---|
| 1239 | if ( ! tuple2 ) return;
|
---|
| 1240 |
|
---|
| 1241 | ast::Pass<TtypeExpander_new> expander{ tenv };
|
---|
[ef9988b] | 1242 |
|
---|
[d3aa64f1] | 1243 | const ast::Type * flat = tuple->accept( expander );
|
---|
| 1244 | const ast::Type * flat2 = tuple2->accept( expander );
|
---|
[f474e91] | 1245 |
|
---|
| 1246 | auto types = flatten( flat );
|
---|
| 1247 | auto types2 = flatten( flat2 );
|
---|
| 1248 |
|
---|
[251ce80] | 1249 | result = unifyList( types, types2, tenv, need, have, open );
|
---|
[f474e91] | 1250 | }
|
---|
| 1251 |
|
---|
[ee574a2] | 1252 | void postvisit( const ast::VarArgsType * ) {
|
---|
[f474e91] | 1253 | result = dynamic_cast< const ast::VarArgsType * >( type2 );
|
---|
| 1254 | }
|
---|
| 1255 |
|
---|
[ee574a2] | 1256 | void postvisit( const ast::ZeroType * ) {
|
---|
[f474e91] | 1257 | result = dynamic_cast< const ast::ZeroType * >( type2 );
|
---|
| 1258 | }
|
---|
| 1259 |
|
---|
[ee574a2] | 1260 | void postvisit( const ast::OneType * ) {
|
---|
[f474e91] | 1261 | result = dynamic_cast< const ast::OneType * >( type2 );
|
---|
[7870799] | 1262 | }
|
---|
[f474e91] | 1263 |
|
---|
| 1264 | private:
|
---|
| 1265 | template< typename RefType > void handleRefType( RefType *inst, Type *other );
|
---|
| 1266 | template< typename RefType > void handleGenericRefType( RefType *inst, Type *other );
|
---|
| 1267 | };
|
---|
| 1268 |
|
---|
[0d070ca] | 1269 | // size_t Unify_new::traceId = Stats::Heap::new_stacktrace_id("Unify_new");
|
---|
[7870799] | 1270 | bool unify(
|
---|
| 1271 | const ast::ptr<ast::Type> & type1, const ast::ptr<ast::Type> & type2,
|
---|
| 1272 | ast::TypeEnvironment & env, ast::AssertionSet & need, ast::AssertionSet & have,
|
---|
[251ce80] | 1273 | ast::OpenVarSet & open
|
---|
[2773ab8] | 1274 | ) {
|
---|
| 1275 | ast::ptr<ast::Type> common;
|
---|
[251ce80] | 1276 | return unify( type1, type2, env, need, have, open, common );
|
---|
[2773ab8] | 1277 | }
|
---|
| 1278 |
|
---|
[7870799] | 1279 | bool unify(
|
---|
| 1280 | const ast::ptr<ast::Type> & type1, const ast::ptr<ast::Type> & type2,
|
---|
| 1281 | ast::TypeEnvironment & env, ast::AssertionSet & need, ast::AssertionSet & have,
|
---|
[251ce80] | 1282 | ast::OpenVarSet & open, ast::ptr<ast::Type> & common
|
---|
[ee574a2] | 1283 | ) {
|
---|
| 1284 | ast::OpenVarSet closed;
|
---|
[46da46b] | 1285 | // findOpenVars( type1, open, closed, need, have, FirstClosed );
|
---|
| 1286 | findOpenVars( type2, open, closed, need, have, env, FirstOpen );
|
---|
[7870799] | 1287 | return unifyInexact(
|
---|
[251ce80] | 1288 | type1, type2, env, need, have, open, WidenMode{ true, true }, common );
|
---|
[ee574a2] | 1289 | }
|
---|
| 1290 |
|
---|
[7870799] | 1291 | bool unifyExact(
|
---|
| 1292 | const ast::Type * type1, const ast::Type * type2, ast::TypeEnvironment & env,
|
---|
| 1293 | ast::AssertionSet & need, ast::AssertionSet & have, const ast::OpenVarSet & open,
|
---|
[251ce80] | 1294 | WidenMode widen
|
---|
[f474e91] | 1295 | ) {
|
---|
| 1296 | if ( type1->qualifiers != type2->qualifiers ) return false;
|
---|
| 1297 |
|
---|
| 1298 | auto var1 = dynamic_cast< const ast::TypeInstType * >( type1 );
|
---|
| 1299 | auto var2 = dynamic_cast< const ast::TypeInstType * >( type2 );
|
---|
[46da46b] | 1300 | bool isopen1 = var1 && env.lookup(*var1);
|
---|
| 1301 | bool isopen2 = var2 && env.lookup(*var2);
|
---|
[f474e91] | 1302 |
|
---|
[46da46b] | 1303 | if ( isopen1 && isopen2 ) {
|
---|
| 1304 | if ( var1->base->kind != var2->base->kind ) return false;
|
---|
| 1305 | return env.bindVarToVar(
|
---|
| 1306 | var1, var2, ast::TypeData{ var1->base->kind, var1->base->sized||var2->base->sized }, need, have,
|
---|
[24d6572] | 1307 | open, widen );
|
---|
[46da46b] | 1308 | } else if ( isopen1 ) {
|
---|
[24d6572] | 1309 | return env.bindVar( var1, type2, ast::TypeData{var1->base}, need, have, open, widen );
|
---|
[46da46b] | 1310 | } else if ( isopen2 ) {
|
---|
[24d6572] | 1311 | return env.bindVar( var2, type1, ast::TypeData{var2->base}, need, have, open, widen );
|
---|
[8f31be6] | 1312 | } else {
|
---|
[5d8dae7] | 1313 | return ast::Pass<Unify_new>::read(
|
---|
[251ce80] | 1314 | type1, type2, env, need, have, open, widen );
|
---|
[f474e91] | 1315 | }
|
---|
| 1316 | }
|
---|
| 1317 |
|
---|
[7870799] | 1318 | bool unifyInexact(
|
---|
| 1319 | const ast::ptr<ast::Type> & type1, const ast::ptr<ast::Type> & type2,
|
---|
| 1320 | ast::TypeEnvironment & env, ast::AssertionSet & need, ast::AssertionSet & have,
|
---|
[251ce80] | 1321 | const ast::OpenVarSet & open, WidenMode widen,
|
---|
[7870799] | 1322 | ast::ptr<ast::Type> & common
|
---|
[f474e91] | 1323 | ) {
|
---|
| 1324 | ast::CV::Qualifiers q1 = type1->qualifiers, q2 = type2->qualifiers;
|
---|
[7870799] | 1325 |
|
---|
| 1326 | // force t1 and t2 to be cloned if their qualifiers must be stripped, so that type1 and
|
---|
[f474e91] | 1327 | // type2 are left unchanged; calling convention forces type{1,2}->strong_ref >= 1
|
---|
[2890212] | 1328 | ast::Type * t1 = shallowCopy(type1.get());
|
---|
| 1329 | ast::Type * t2 = shallowCopy(type2.get());
|
---|
| 1330 | t1->qualifiers = {};
|
---|
| 1331 | t2->qualifiers = {};
|
---|
[f49b3fc] | 1332 | ast::ptr< ast::Type > t1_(t1);
|
---|
| 1333 | ast::ptr< ast::Type > t2_(t2);
|
---|
[7870799] | 1334 |
|
---|
[251ce80] | 1335 | if ( unifyExact( t1, t2, env, need, have, open, widen ) ) {
|
---|
[f474e91] | 1336 | // if exact unification on unqualified types, try to merge qualifiers
|
---|
| 1337 | if ( q1 == q2 || ( ( q1 > q2 || widen.first ) && ( q2 > q1 || widen.second ) ) ) {
|
---|
[2890212] | 1338 | t1->qualifiers = q1 | q2;
|
---|
| 1339 | common = t1;
|
---|
[f474e91] | 1340 | return true;
|
---|
| 1341 | } else {
|
---|
| 1342 | return false;
|
---|
| 1343 | }
|
---|
| 1344 |
|
---|
[251ce80] | 1345 | } else if (( common = commonType( t1, t2, env, need, have, open, widen ))) {
|
---|
[f474e91] | 1346 | // no exact unification, but common type
|
---|
[2890212] | 1347 | auto c = shallowCopy(common.get());
|
---|
| 1348 | c->qualifiers = q1 | q2;
|
---|
| 1349 | common = c;
|
---|
[f474e91] | 1350 | return true;
|
---|
| 1351 | } else {
|
---|
| 1352 | return false;
|
---|
| 1353 | }
|
---|
| 1354 | }
|
---|
| 1355 |
|
---|
[54e41b3] | 1356 | ast::ptr<ast::Type> extractResultType( const ast::FunctionType * func ) {
|
---|
[4139e3d] | 1357 | if ( func->returns.empty() ) return new ast::VoidType{};
|
---|
[954c954] | 1358 | if ( func->returns.size() == 1 ) return func->returns[0];
|
---|
[4139e3d] | 1359 |
|
---|
| 1360 | std::vector<ast::ptr<ast::Type>> tys;
|
---|
[954c954] | 1361 | for ( const auto & decl : func->returns ) {
|
---|
| 1362 | tys.emplace_back( decl );
|
---|
[4139e3d] | 1363 | }
|
---|
| 1364 | return new ast::TupleType{ std::move(tys) };
|
---|
[54e41b3] | 1365 | }
|
---|
[51b73452] | 1366 | } // namespace ResolvExpr
|
---|
[a32b204] | 1367 |
|
---|
| 1368 | // Local Variables: //
|
---|
| 1369 | // tab-width: 4 //
|
---|
| 1370 | // mode: c++ //
|
---|
| 1371 | // compile-command: "make install" //
|
---|
| 1372 | // End: //
|
---|