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