source: src/ResolvExpr/Unify.cc @ 41a2620

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 41a2620 was 41a2620, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

changed unification constraints on array types to more closely match C11 standard

  • Property mode set to 100644
File size: 23.6 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
[1cbca6e]11// Last Modified By : Rob Schluntz
12// Last Modified On : Wed Sep 02 14:43:22 2015
13// Update Count     : 36
[a32b204]14//
[51b7345]15
16#include <set>
17#include <memory>
18
19#include "Unify.h"
20#include "TypeEnvironment.h"
21#include "typeops.h"
22#include "FindOpenVars.h"
23#include "SynTree/Visitor.h"
24#include "SynTree/Type.h"
25#include "SynTree/Declaration.h"
26#include "SymTab/Indexer.h"
[d3b7937]27#include "Common/utility.h"
[51b7345]28
29
[1cbca6e]30// #define DEBUG
[51b7345]31
32namespace ResolvExpr {
[a32b204]33        struct WidenMode {
34                WidenMode( bool widenFirst, bool widenSecond ): widenFirst( widenFirst ), widenSecond( widenSecond ) {}
35                WidenMode &operator|=( const WidenMode &other ) { widenFirst |= other.widenFirst; widenSecond |= other.widenSecond; return *this; }
36                WidenMode &operator&=( const WidenMode &other ) { widenFirst &= other.widenFirst; widenSecond &= other.widenSecond; return *this; }
37                WidenMode operator|( const WidenMode &other ) { WidenMode newWM( *this ); newWM |= other; return newWM; }
38                WidenMode operator&( const WidenMode &other ) { WidenMode newWM( *this ); newWM &= other; return newWM; }
39                operator bool() { return widenFirst && widenSecond; }
[41a2620]40
[a32b204]41                bool widenFirst : 1, widenSecond : 1;
42        };
[51b7345]43
[a32b204]44        class Unify : public Visitor {
45          public:
46                Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
[41a2620]47
[a32b204]48                bool get_result() const { return result; }
49          private:
50                virtual void visit(VoidType *voidType);
51                virtual void visit(BasicType *basicType);
52                virtual void visit(PointerType *pointerType);
53                virtual void visit(ArrayType *arrayType);
54                virtual void visit(FunctionType *functionType);
55                virtual void visit(StructInstType *aggregateUseType);
56                virtual void visit(UnionInstType *aggregateUseType);
57                virtual void visit(EnumInstType *aggregateUseType);
58                virtual void visit(ContextInstType *aggregateUseType);
59                virtual void visit(TypeInstType *aggregateUseType);
60                virtual void visit(TupleType *tupleType);
61
62                template< typename RefType > void handleRefType( RefType *inst, Type *other );
[02ec390]63                template< typename RefType > void handleGenericRefType( RefType *inst, Type *other );
[a32b204]64
65                bool result;
66                Type *type2;                            // inherited
67                TypeEnvironment &env;
68                AssertionSet &needAssertions;
69                AssertionSet &haveAssertions;
70                const OpenVarSet &openVars;
71                WidenMode widenMode;
72                Type *commonType;
73                const SymTab::Indexer &indexer;
74        };
75
[eb50842]76        /// Attempts an inexact unification of type1 and type2.
77        /// Returns false if no such unification; if the types can be unified, sets common (unless they unify exactly and have identical type qualifiers)
[a32b204]78        bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common );
79        bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
[41a2620]80
[a32b204]81        bool typesCompatible( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
82                TypeEnvironment newEnv;
[1cbca6e]83                OpenVarSet openVars, closedVars; // added closedVars
[a32b204]84                AssertionSet needAssertions, haveAssertions;
85                Type *newFirst = first->clone(), *newSecond = second->clone();
86                env.apply( newFirst );
87                env.apply( newSecond );
[1cbca6e]88
89                // do we need to do this? Seems like we do, types should be able to be compatible if they
90                // have free variables that can unify
91                findOpenVars( newFirst, openVars, closedVars, needAssertions, haveAssertions, false );
92                findOpenVars( newSecond, openVars, closedVars, needAssertions, haveAssertions, true );
93
[a32b204]94                bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
95                delete newFirst;
96                delete newSecond;
97                return result;
98        }
99
100        bool typesCompatibleIgnoreQualifiers( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
101                TypeEnvironment newEnv;
102                OpenVarSet openVars;
103                AssertionSet needAssertions, haveAssertions;
104                Type *newFirst = first->clone(), *newSecond = second->clone();
105                env.apply( newFirst );
106                env.apply( newSecond );
107                newFirst->get_qualifiers() = Type::Qualifiers();
108                newSecond->get_qualifiers() = Type::Qualifiers();
[51b7345]109///   std::cout << "first is ";
110///   first->print( std::cout );
111///   std::cout << std::endl << "second is ";
112///   second->print( std::cout );
113///   std::cout << std::endl << "newFirst is ";
114///   newFirst->print( std::cout );
115///   std::cout << std::endl << "newSecond is ";
116///   newSecond->print( std::cout );
117///   std::cout << std::endl;
[a32b204]118                bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
119                delete newFirst;
120                delete newSecond;
121                return result;
122        }
123
124        bool isFtype( Type *type, const SymTab::Indexer &indexer ) {
125                if ( dynamic_cast< FunctionType* >( type ) ) {
126                        return true;
127                } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) {
128                        return typeInst->get_isFtype();
129                } // if
130                return false;
131        }
132
133        bool tyVarCompatible( TypeDecl::Kind kind, Type *type, const SymTab::Indexer &indexer ) {
134                switch ( kind ) {
135                  case TypeDecl::Any:
136                  case TypeDecl::Dtype:
137                        return ! isFtype( type, indexer );
[41a2620]138
[a32b204]139                  case TypeDecl::Ftype:
140                        return isFtype( type, indexer );
141                } // switch
142                assert( false );
143                return false;
144        }
145
146        bool bindVar( TypeInstType *typeInst, Type *other, TypeDecl::Kind kind, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
147                OpenVarSet::const_iterator tyvar = openVars.find( typeInst->get_name() );
148                assert( tyvar != openVars.end() );
149                if ( ! tyVarCompatible( tyvar->second, other, indexer ) ) {
150                        return false;
151                } // if
152                if ( occurs( other, typeInst->get_name(), env ) ) {
153                        return false;
154                } // if
155                EqvClass curClass;
156                if ( env.lookup( typeInst->get_name(), curClass ) ) {
157                        if ( curClass.type ) {
158                                Type *common = 0;
[eb50842]159                                // attempt to unify equivalence class type (which has qualifiers stripped, so they must be restored) with the type to bind to
[a32b204]160                                std::auto_ptr< Type > newType( curClass.type->clone() );
[721f17a]161                                newType->get_qualifiers() = typeInst->get_qualifiers();
[a32b204]162                                if ( unifyInexact( newType.get(), other, env, needAssertions, haveAssertions, openVars, widenMode & WidenMode( curClass.allowWidening, true ), indexer, common ) ) {
163                                        if ( common ) {
164                                                common->get_qualifiers() = Type::Qualifiers();
165                                                delete curClass.type;
166                                                curClass.type = common;
167                                                env.add( curClass );
168                                        } // if
169                                        return true;
170                                } else {
171                                        return false;
172                                } // if
173                        } else {
174                                curClass.type = other->clone();
175                                curClass.type->get_qualifiers() = Type::Qualifiers();
176                                curClass.allowWidening = widenMode.widenFirst && widenMode.widenSecond;
177                                env.add( curClass );
178                        } // if
179                } else {
180                        EqvClass newClass;
181                        newClass.vars.insert( typeInst->get_name() );
182                        newClass.type = other->clone();
183                        newClass.type->get_qualifiers() = Type::Qualifiers();
184                        newClass.allowWidening = widenMode.widenFirst && widenMode.widenSecond;
185                        newClass.kind = kind;
186                        env.add( newClass );
187                } // if
188                return true;
189        }
190
191        bool bindVarToVar( TypeInstType *var1, TypeInstType *var2, TypeDecl::Kind kind, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
192                bool result = true;
193                EqvClass class1, class2;
194                bool hasClass1 = false, hasClass2 = false;
195                bool widen1 = false, widen2 = false;
196                Type *type1 = 0, *type2 = 0;
[41a2620]197
[a32b204]198                if ( env.lookup( var1->get_name(), class1 ) ) {
199                        hasClass1 = true;
200                        if ( class1.type ) {
201                                if ( occurs( class1.type, var2->get_name(), env ) ) {
202                                        return false;
203                                } // if
204                                type1 = class1.type->clone();
205                        } // if
206                        widen1 = widenMode.widenFirst && class1.allowWidening;
207                } // if
208                if ( env.lookup( var2->get_name(), class2 ) ) {
209                        hasClass2 = true;
210                        if ( class2.type ) {
211                                if ( occurs( class2.type, var1->get_name(), env ) ) {
212                                        return false;
213                                } // if
214                                type2 = class2.type->clone();
215                        } // if
216                        widen2 = widenMode.widenSecond && class2.allowWidening;
217                } // if
[41a2620]218
[a32b204]219                if ( type1 && type2 ) {
[51b7345]220//    std::cout << "has type1 && type2" << std::endl;
[a32b204]221                        WidenMode newWidenMode ( widen1, widen2 );
222                        Type *common = 0;
223                        if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, newWidenMode, indexer, common ) ) {
224                                class1.vars.insert( class2.vars.begin(), class2.vars.end() );
225                                class1.allowWidening = widen1 && widen2;
226                                if ( common ) {
227                                        common->get_qualifiers() = Type::Qualifiers();
228                                        delete class1.type;
229                                        class1.type = common;
230                                } // if
231                                env.add( class1 );
232                        } else {
233                                result = false;
234                        } // if
235                } else if ( hasClass1 && hasClass2 ) {
236                        if ( type1 ) {
237                                class1.vars.insert( class2.vars.begin(), class2.vars.end() );
238                                class1.allowWidening = widen1;
239                                env.add( class1 );
240                        } else {
241                                class2.vars.insert( class1.vars.begin(), class1.vars.end() );
242                                class2.allowWidening = widen2;
243                                env.add( class2 );
244                        } // if
245                } else if ( hasClass1 ) {
246                        class1.vars.insert( var2->get_name() );
247                        class1.allowWidening = widen1;
248                        env.add( class1 );
249                } else if ( hasClass2 ) {
250                        class2.vars.insert( var1->get_name() );
251                        class2.allowWidening = widen2;
252                        env.add( class2 );
253                } else {
254                        EqvClass newClass;
255                        newClass.vars.insert( var1->get_name() );
256                        newClass.vars.insert( var2->get_name() );
257                        newClass.allowWidening = widen1 && widen2;
258                        newClass.kind = kind;
259                        env.add( newClass );
260                } // if
261                delete type1;
262                delete type2;
263                return result;
264        }
265
266        bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
267                OpenVarSet closedVars;
268                findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
269                findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
270                Type *commonType = 0;
271                if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType ) ) {
272                        if ( commonType ) {
273                                delete commonType;
274                        } // if
275                        return true;
276                } else {
277                        return false;
278                } // if
279        }
280
281        bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer, Type *&commonType ) {
282                OpenVarSet closedVars;
283                findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
284                findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
285                return unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType );
286        }
287
288        bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
[51b7345]289#ifdef DEBUG
[a32b204]290                TypeEnvironment debugEnv( env );
[51b7345]291#endif
[eb50842]292                if ( type1->get_qualifiers() != type2->get_qualifiers() ) {
293                        return false;
294                }
295
[a32b204]296                bool result;
297                TypeInstType *var1 = dynamic_cast< TypeInstType* >( type1 );
298                TypeInstType *var2 = dynamic_cast< TypeInstType* >( type2 );
299                OpenVarSet::const_iterator entry1, entry2;
300                if ( var1 ) {
301                        entry1 = openVars.find( var1->get_name() );
302                } // if
303                if ( var2 ) {
304                        entry2 = openVars.find( var2->get_name() );
305                } // if
306                bool isopen1 = var1 && ( entry1 != openVars.end() );
307                bool isopen2 = var2 && ( entry2 != openVars.end() );
[eb50842]308
309                if ( isopen1 && isopen2 && entry1->second == entry2->second ) {
[a32b204]310                        result = bindVarToVar( var1, var2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
311                } else if ( isopen1 ) {
312                        result = bindVar( var1, type2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
313                } else if ( isopen2 ) {
314                        result = bindVar( var2, type1, entry2->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
315                } else {
316                        Unify comparator( type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
317                        type1->accept( comparator );
318                        result = comparator.get_result();
319                } // if
[51b7345]320#ifdef DEBUG
[a32b204]321                std::cout << "============ unifyExact" << std::endl;
322                std::cout << "type1 is ";
323                type1->print( std::cout );
324                std::cout << std::endl << "type2 is ";
325                type2->print( std::cout );
326                std::cout << std::endl << "openVars are ";
327                printOpenVarSet( openVars, std::cout, 8 );
328                std::cout << std::endl << "input env is " << std::endl;
329                debugEnv.print( std::cout, 8 );
330                std::cout << std::endl << "result env is " << std::endl;
331                env.print( std::cout, 8 );
332                std::cout << "result is " << result << std::endl;
[51b7345]333#endif
[a32b204]334                return result;
335        }
336
337        bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
338                return unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
339        }
340
341        bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common ) {
342                Type::Qualifiers tq1 = type1->get_qualifiers(), tq2 = type2->get_qualifiers();
343                type1->get_qualifiers() = Type::Qualifiers();
344                type2->get_qualifiers() = Type::Qualifiers();
345                bool result;
[51b7345]346#ifdef DEBUG
[a32b204]347                std::cout << "unifyInexact type 1 is ";
348                type1->print( std::cout );
349                std::cout << "type 2 is ";
350                type2->print( std::cout );
351                std::cout << std::endl;
[51b7345]352#endif
[a32b204]353                if ( ! unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer ) ) {
[51b7345]354#ifdef DEBUG
[a32b204]355                        std::cout << "unifyInexact: no exact unification found" << std::endl;
[51b7345]356#endif
[a32b204]357                        if ( ( common = commonType( type1, type2, widenMode.widenFirst, widenMode.widenSecond, indexer, env, openVars ) ) ) {
358                                common->get_qualifiers() = tq1 + tq2;
[51b7345]359#ifdef DEBUG
[a32b204]360                                std::cout << "unifyInexact: common type is ";
361                                common->print( std::cout );
362                                std::cout << std::endl;
[51b7345]363#endif
[a32b204]364                                result = true;
365                        } else {
[51b7345]366#ifdef DEBUG
[a32b204]367                                std::cout << "unifyInexact: no common type found" << std::endl;
[51b7345]368#endif
[a32b204]369                                result = false;
370                        } // if
371                } else {
372                        if ( tq1 != tq2 ) {
373                                if ( ( tq1 > tq2 || widenMode.widenFirst ) && ( tq2 > tq1 || widenMode.widenSecond ) ) {
374                                        common = type1->clone();
375                                        common->get_qualifiers() = tq1 + tq2;
376                                        result = true;
377                                } else {
378                                        result = false;
379                                } // if
380                        } else {
381                                result = true;
382                        } // if
383                } // if
384                type1->get_qualifiers() = tq1;
385                type2->get_qualifiers() = tq2;
386                return result;
387        }
388
389        Unify::Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer )
390                : result( false ), type2( type2 ), env( env ), needAssertions( needAssertions ), haveAssertions( haveAssertions ), openVars( openVars ), widenMode( widenMode ), indexer( indexer ) {
391        }
392
393        void Unify::visit(VoidType *voidType) {
394                result = dynamic_cast< VoidType* >( type2 );
395        }
396
397        void Unify::visit(BasicType *basicType) {
398                if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) {
399                        result = basicType->get_kind() == otherBasic->get_kind();
400                } // if
401        }
402
403        void markAssertionSet( AssertionSet &assertions, DeclarationWithType *assert ) {
[51b7345]404///   std::cout << "assertion set is" << std::endl;
405///   printAssertionSet( assertions, std::cout, 8 );
406///   std::cout << "looking for ";
407///   assert->print( std::cout );
408///   std::cout << std::endl;
[a32b204]409                AssertionSet::iterator i = assertions.find( assert );
410                if ( i != assertions.end() ) {
[51b7345]411///     std::cout << "found it!" << std::endl;
[a32b204]412                        i->second = true;
413                } // if
414        }
415
416        void markAssertions( AssertionSet &assertion1, AssertionSet &assertion2, Type *type ) {
417                for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
418                        for ( std::list< DeclarationWithType* >::const_iterator assert = (*tyvar)->get_assertions().begin(); assert != (*tyvar)->get_assertions().end(); ++assert ) {
419                                markAssertionSet( assertion1, *assert );
420                                markAssertionSet( assertion2, *assert );
421                        } // for
422                } // for
423        }
424
425        void Unify::visit(PointerType *pointerType) {
426                if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) {
427                        result = unifyExact( pointerType->get_base(), otherPointer->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
428                        markAssertions( haveAssertions, needAssertions, pointerType );
429                        markAssertions( haveAssertions, needAssertions, otherPointer );
430                } // if
431        }
432
433        void Unify::visit(ArrayType *arrayType) {
434                ArrayType *otherArray = dynamic_cast< ArrayType* >( type2 );
[1cbca6e]435                // to unify, array types must both be VLA or both not VLA
436                // and must both have a dimension expression or not have a dimension
[41a2620]437                if ( otherArray && arrayType->get_isVarLen() == otherArray->get_isVarLen() ) {
[1cbca6e]438
439                        // not positive this is correct in all cases, but it's needed for typedefs
440                        if ( arrayType->get_isVarLen() || otherArray->get_isVarLen() ) {
441                                return;
442                        }
443
444                        if ( ! arrayType->get_isVarLen() && ! otherArray->get_isVarLen() &&
445                                arrayType->get_dimension() != 0 && otherArray->get_dimension() != 0 ) {
446                                ConstantExpr * ce1 = dynamic_cast< ConstantExpr * >( arrayType->get_dimension() );
447                                ConstantExpr * ce2 = dynamic_cast< ConstantExpr * >( otherArray->get_dimension() );
[41a2620]448                                // see C11 Reference Manual 6.7.6.2.6
449                                // two array types with size specifiers that are integer constant expressions are
450                                // compatible if both size specifiers have the same constant value
451                                if ( ce1 && ce2 ) {
452                                        Constant * c1 = ce1->get_constant();
453                                        Constant * c2 = ce2->get_constant();
454
455                                        if ( c1->get_value() != c2->get_value() ) {
456                                                // does not unify if the dimension is different
457                                                return;
458                                        }
[1cbca6e]459                                }
460                        }
461
[a32b204]462                        result = unifyExact( arrayType->get_base(), otherArray->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
463                } // if
464        }
465
466        template< typename Iterator1, typename Iterator2 >
467        bool unifyDeclList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
468                for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
[1cbca6e]469                        // Type * commonType;
470                        // if ( ! unifyInexact( (*list1Begin)->get_type(), (*list2Begin)->get_type(), env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType ) ) {
[a32b204]471                        if ( ! unifyExact( (*list1Begin)->get_type(), (*list2Begin)->get_type(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
472                                return false;
473                        } // if
474                } // for
475                if ( list1Begin != list1End || list2Begin != list2End ) {
476                        return false;
477                } else {
478                        return true;
479                } // if
480        }
481
482        void Unify::visit(FunctionType *functionType) {
483                FunctionType *otherFunction = dynamic_cast< FunctionType* >( type2 );
484                if ( otherFunction && functionType->get_isVarArgs() == otherFunction->get_isVarArgs() ) {
[1cbca6e]485
[a32b204]486                        if ( unifyDeclList( functionType->get_parameters().begin(), functionType->get_parameters().end(), otherFunction->get_parameters().begin(), otherFunction->get_parameters().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
[41a2620]487
[a32b204]488                                if ( unifyDeclList( functionType->get_returnVals().begin(), functionType->get_returnVals().end(), otherFunction->get_returnVals().begin(), otherFunction->get_returnVals().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
489
490                                        markAssertions( haveAssertions, needAssertions, functionType );
491                                        markAssertions( haveAssertions, needAssertions, otherFunction );
492
493                                        result = true;
494                                } // if
495                        } // if
496                } // if
497        }
498
499        template< typename RefType >
[02ec390]500        void Unify::handleRefType( RefType *inst, Type *other ) {
501                // check that other type is compatible and named the same
[a32b204]502                RefType *otherStruct = dynamic_cast< RefType* >( other );
503                result = otherStruct && inst->get_name() == otherStruct->get_name();
[02ec390]504        }
505
506        template< typename RefType >
507        void Unify::handleGenericRefType( RefType *inst, Type *other ) {
508                // Check that other type is compatible and named the same
509                handleRefType( inst, other );
510                if ( ! result ) return;
[f5234f3]511                // Check that parameters of types unify, if any
[02ec390]512                std::list< Expression* > params = inst->get_parameters();
[f5234f3]513                std::list< Expression* > otherParams = ((RefType*)other)->get_parameters();
514
515                std::list< Expression* >::const_iterator it = params.begin(), jt = otherParams.begin();
516                for ( ; it != params.end() && jt != otherParams.end(); ++it, ++jt ) {
517                        TypeExpr *param = dynamic_cast< TypeExpr* >(*it);
518                        assert(param && "Aggregate parameters should be type expressions");
519                        TypeExpr *otherParam = dynamic_cast< TypeExpr* >(*jt);
520                        assert(otherParam && "Aggregate parameters should be type expressions");
521
522                        if ( ! unifyExact( param->get_type(), otherParam->get_type(), env, needAssertions, haveAssertions, openVars, WidenMode(false, false), indexer ) ) {
[02ec390]523                                result = false;
524                                return;
525                        }
526                }
[f5234f3]527                result = ( it == params.end() && jt == otherParams.end() );
[02ec390]528        }
[a32b204]529
530        void Unify::visit(StructInstType *structInst) {
[02ec390]531                handleGenericRefType( structInst, type2 );
[a32b204]532        }
533
534        void Unify::visit(UnionInstType *unionInst) {
[02ec390]535                handleGenericRefType( unionInst, type2 );
[a32b204]536        }
537
538        void Unify::visit(EnumInstType *enumInst) {
539                handleRefType( enumInst, type2 );
540        }
541
542        void Unify::visit(ContextInstType *contextInst) {
543                handleRefType( contextInst, type2 );
544        }
545
546        void Unify::visit(TypeInstType *typeInst) {
547                assert( openVars.find( typeInst->get_name() ) == openVars.end() );
548                TypeInstType *otherInst = dynamic_cast< TypeInstType* >( type2 );
549                if ( otherInst && typeInst->get_name() == otherInst->get_name() ) {
550                        result = true;
[51b7345]551///   } else {
552///     NamedTypeDecl *nt = indexer.lookupType( typeInst->get_name() );
[a32b204]553///     if ( nt ) {
[51b7345]554///       TypeDecl *type = dynamic_cast< TypeDecl* >( nt );
555///       assert( type );
[a32b204]556///       if ( type->get_base() ) {
[51b7345]557///         result = unifyExact( type->get_base(), typeInst, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
558///       }
559///     }
[a32b204]560                } // if
561        }
562
563        template< typename Iterator1, typename Iterator2 >
564        bool unifyList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
565                for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
566                        Type *commonType = 0;
567                        if ( ! unifyInexact( *list1Begin, *list2Begin, env, needAssertions, haveAssertions, openVars, widenMode, indexer, commonType ) ) {
568                                return false;
569                        }
570                        delete commonType;
571                } // for
572                if ( list1Begin != list1End || list2Begin != list2End ) {
573                        return false;
574                } else {
575                        return true;
576                } //if
577        }
578
579        void Unify::visit(TupleType *tupleType) {
580                if ( TupleType *otherTuple = dynamic_cast< TupleType* >( type2 ) ) {
581                        result = unifyList( tupleType->get_types().begin(), tupleType->get_types().end(), otherTuple->get_types().begin(), otherTuple->get_types().end(), env, needAssertions, haveAssertions, openVars, widenMode, indexer );
582                } // if
583        }
[51b7345]584
585} // namespace ResolvExpr
[a32b204]586
587// Local Variables: //
588// tab-width: 4 //
589// mode: c++ //
590// compile-command: "make install" //
591// End: //
Note: See TracBrowser for help on using the repository browser.