source: src/ResolvExpr/Unify.cc @ 2c57025

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 2c57025 was 2c57025, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

add support for built-in sized trait which decouples size/alignment information from otype parameters, add test for sized trait

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