source: src/ResolvExpr/Unify.cc @ 54e41b3

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 54e41b3 was 54e41b3, checked in by Aaron Moss <a3moss@…>, 5 years ago

Add first half of ast::Expr subclasses

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