| 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 : Thu Mar 16 16:22:54 2017
 | 
|---|
| 13 | // Update Count     : 42
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 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...
 | 
|---|
| 19 | #include <memory>                 // for unique_ptr, auto_ptr
 | 
|---|
| 20 | #include <set>                    // for set
 | 
|---|
| 21 | #include <string>                 // for string, operator==, operator!=, bas...
 | 
|---|
| 22 | #include <utility>                // for pair
 | 
|---|
| 23 | 
 | 
|---|
| 24 | #include "FindOpenVars.h"         // for findOpenVars
 | 
|---|
| 25 | #include "Parser/LinkageSpec.h"   // for C
 | 
|---|
| 26 | #include "SynTree/Constant.h"     // for Constant
 | 
|---|
| 27 | #include "SynTree/Declaration.h"  // for TypeDecl, TypeDecl::Data, Declarati...
 | 
|---|
| 28 | #include "SynTree/Expression.h"   // for TypeExpr, Expression, ConstantExpr
 | 
|---|
| 29 | #include "SynTree/Mutator.h"      // for Mutator
 | 
|---|
| 30 | #include "SynTree/Type.h"         // for Type, TypeInstType, FunctionType
 | 
|---|
| 31 | #include "SynTree/Visitor.h"      // for Visitor
 | 
|---|
| 32 | #include "Tuples/Tuples.h"        // for isTtype
 | 
|---|
| 33 | #include "TypeEnvironment.h"      // for EqvClass, AssertionSet, OpenVarSet
 | 
|---|
| 34 | #include "Unify.h"
 | 
|---|
| 35 | #include "typeops.h"              // for flatten, occurs, commonType
 | 
|---|
| 36 | 
 | 
|---|
| 37 | namespace SymTab {
 | 
|---|
| 38 | class Indexer;
 | 
|---|
| 39 | }  // namespace SymTab
 | 
|---|
| 40 | 
 | 
|---|
| 41 | // #define DEBUG
 | 
|---|
| 42 | 
 | 
|---|
| 43 | namespace ResolvExpr {
 | 
|---|
| 44 | 
 | 
|---|
| 45 |         class Unify : public Visitor {
 | 
|---|
| 46 |           public:
 | 
|---|
| 47 |                 Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
 | 
|---|
| 48 | 
 | 
|---|
| 49 |                 bool get_result() const { return result; }
 | 
|---|
| 50 |           private:
 | 
|---|
| 51 |                 virtual void visit(VoidType *voidType);
 | 
|---|
| 52 |                 virtual void visit(BasicType *basicType);
 | 
|---|
| 53 |                 virtual void visit(PointerType *pointerType);
 | 
|---|
| 54 |                 virtual void visit(ArrayType *arrayType);
 | 
|---|
| 55 |                 virtual void visit(ReferenceType *refType);
 | 
|---|
| 56 |                 virtual void visit(FunctionType *functionType);
 | 
|---|
| 57 |                 virtual void visit(StructInstType *aggregateUseType);
 | 
|---|
| 58 |                 virtual void visit(UnionInstType *aggregateUseType);
 | 
|---|
| 59 |                 virtual void visit(EnumInstType *aggregateUseType);
 | 
|---|
| 60 |                 virtual void visit(TraitInstType *aggregateUseType);
 | 
|---|
| 61 |                 virtual void visit(TypeInstType *aggregateUseType);
 | 
|---|
| 62 |                 virtual void visit(TupleType *tupleType);
 | 
|---|
| 63 |                 virtual void visit(VarArgsType *varArgsType);
 | 
|---|
| 64 |                 virtual void visit(ZeroType *zeroType);
 | 
|---|
| 65 |                 virtual void visit(OneType *oneType);
 | 
|---|
| 66 | 
 | 
|---|
| 67 |                 template< typename RefType > void handleRefType( RefType *inst, Type *other );
 | 
|---|
| 68 |                 template< typename RefType > void handleGenericRefType( RefType *inst, Type *other );
 | 
|---|
| 69 | 
 | 
|---|
| 70 |                 bool result;
 | 
|---|
| 71 |                 Type *type2;                            // inherited
 | 
|---|
| 72 |                 TypeEnvironment &env;
 | 
|---|
| 73 |                 AssertionSet &needAssertions;
 | 
|---|
| 74 |                 AssertionSet &haveAssertions;
 | 
|---|
| 75 |                 const OpenVarSet &openVars;
 | 
|---|
| 76 |                 WidenMode widenMode;
 | 
|---|
| 77 |                 const SymTab::Indexer &indexer;
 | 
|---|
| 78 |         };
 | 
|---|
| 79 | 
 | 
|---|
| 80 |         /// Attempts an inexact unification of type1 and type2.
 | 
|---|
| 81 |         /// Returns false if no such unification; if the types can be unified, sets common (unless they unify exactly and have identical type qualifiers)
 | 
|---|
| 82 |         bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common );
 | 
|---|
| 83 |         bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
 | 
|---|
| 84 | 
 | 
|---|
| 85 |         bool typesCompatible( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
 | 
|---|
| 86 |                 TypeEnvironment newEnv;
 | 
|---|
| 87 |                 OpenVarSet openVars, closedVars; // added closedVars
 | 
|---|
| 88 |                 AssertionSet needAssertions, haveAssertions;
 | 
|---|
| 89 |                 Type *newFirst = first->clone(), *newSecond = second->clone();
 | 
|---|
| 90 |                 env.apply( newFirst );
 | 
|---|
| 91 |                 env.apply( newSecond );
 | 
|---|
| 92 | 
 | 
|---|
| 93 |                 // do we need to do this? Seems like we do, types should be able to be compatible if they
 | 
|---|
| 94 |                 // have free variables that can unify
 | 
|---|
| 95 |                 findOpenVars( newFirst, openVars, closedVars, needAssertions, haveAssertions, false );
 | 
|---|
| 96 |                 findOpenVars( newSecond, openVars, closedVars, needAssertions, haveAssertions, true );
 | 
|---|
| 97 | 
 | 
|---|
| 98 |                 bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
 | 
|---|
| 99 |                 delete newFirst;
 | 
|---|
| 100 |                 delete newSecond;
 | 
|---|
| 101 |                 return result;
 | 
|---|
| 102 |         }
 | 
|---|
| 103 | 
 | 
|---|
| 104 |         bool typesCompatibleIgnoreQualifiers( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
 | 
|---|
| 105 |                 TypeEnvironment newEnv;
 | 
|---|
| 106 |                 OpenVarSet openVars;
 | 
|---|
| 107 |                 AssertionSet needAssertions, haveAssertions;
 | 
|---|
| 108 |                 Type *newFirst = first->clone(), *newSecond = second->clone();
 | 
|---|
| 109 |                 env.apply( newFirst );
 | 
|---|
| 110 |                 env.apply( newSecond );
 | 
|---|
| 111 |                 newFirst->get_qualifiers() = Type::Qualifiers();
 | 
|---|
| 112 |                 newSecond->get_qualifiers() = Type::Qualifiers();
 | 
|---|
| 113 | ///   std::cerr << "first is ";
 | 
|---|
| 114 | ///   first->print( std::cerr );
 | 
|---|
| 115 | ///   std::cerr << std::endl << "second is ";
 | 
|---|
| 116 | ///   second->print( std::cerr );
 | 
|---|
| 117 | ///   std::cerr << std::endl << "newFirst is ";
 | 
|---|
| 118 | ///   newFirst->print( std::cerr );
 | 
|---|
| 119 | ///   std::cerr << std::endl << "newSecond is ";
 | 
|---|
| 120 | ///   newSecond->print( std::cerr );
 | 
|---|
| 121 | ///   std::cerr << std::endl;
 | 
|---|
| 122 |                 bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
 | 
|---|
| 123 |                 delete newFirst;
 | 
|---|
| 124 |                 delete newSecond;
 | 
|---|
| 125 |                 return result;
 | 
|---|
| 126 |         }
 | 
|---|
| 127 | 
 | 
|---|
| 128 |         bool isFtype( Type *type ) {
 | 
|---|
| 129 |                 if ( dynamic_cast< FunctionType* >( type ) ) {
 | 
|---|
| 130 |                         return true;
 | 
|---|
| 131 |                 } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) {
 | 
|---|
| 132 |                         return typeInst->get_isFtype();
 | 
|---|
| 133 |                 } // if
 | 
|---|
| 134 |                 return false;
 | 
|---|
| 135 |         }
 | 
|---|
| 136 | 
 | 
|---|
| 137 |         bool tyVarCompatible( const TypeDecl::Data & data, Type *type ) {
 | 
|---|
| 138 |                 switch ( data.kind ) {
 | 
|---|
| 139 |                   case TypeDecl::Any:
 | 
|---|
| 140 |                   case TypeDecl::Dtype:
 | 
|---|
| 141 |                         // to bind to an object type variable, the type must not be a function type.
 | 
|---|
| 142 |                         // if the type variable is specified to be a complete type then the incoming
 | 
|---|
| 143 |                         // type must also be complete
 | 
|---|
| 144 |                         // xxx - should this also check that type is not a tuple type and that it's not a ttype?
 | 
|---|
| 145 |                         return ! isFtype( type ) && (! data.isComplete || type->isComplete() );
 | 
|---|
| 146 |                   case TypeDecl::Ftype:
 | 
|---|
| 147 |                         return isFtype( type );
 | 
|---|
| 148 |                   case TypeDecl::Ttype:
 | 
|---|
| 149 |                         // ttype unifies with any tuple type
 | 
|---|
| 150 |                         return dynamic_cast< TupleType * >( type ) || Tuples::isTtype( type );
 | 
|---|
| 151 |                 } // switch
 | 
|---|
| 152 |                 return false;
 | 
|---|
| 153 |         }
 | 
|---|
| 154 | 
 | 
|---|
| 155 |         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 ) {
 | 
|---|
| 156 |                 // remove references from other, so that type variables can only bind to value types
 | 
|---|
| 157 |                 other = other->stripReferences();
 | 
|---|
| 158 |                 OpenVarSet::const_iterator tyvar = openVars.find( typeInst->get_name() );
 | 
|---|
| 159 |                 assert( tyvar != openVars.end() );
 | 
|---|
| 160 |                 if ( ! tyVarCompatible( tyvar->second, other ) ) {
 | 
|---|
| 161 |                         return false;
 | 
|---|
| 162 |                 } // if
 | 
|---|
| 163 |                 if ( occurs( other, typeInst->get_name(), env ) ) {
 | 
|---|
| 164 |                         return false;
 | 
|---|
| 165 |                 } // if
 | 
|---|
| 166 |                 EqvClass curClass;
 | 
|---|
| 167 |                 if ( env.lookup( typeInst->get_name(), curClass ) ) {
 | 
|---|
| 168 |                         if ( curClass.type ) {
 | 
|---|
| 169 |                                 Type *common = 0;
 | 
|---|
| 170 |                                 // attempt to unify equivalence class type (which has qualifiers stripped, so they must be restored) with the type to bind to
 | 
|---|
| 171 |                                 std::auto_ptr< Type > newType( curClass.type->clone() );
 | 
|---|
| 172 |                                 newType->get_qualifiers() = typeInst->get_qualifiers();
 | 
|---|
| 173 |                                 if ( unifyInexact( newType.get(), other, env, needAssertions, haveAssertions, openVars, widenMode & WidenMode( curClass.allowWidening, true ), indexer, common ) ) {
 | 
|---|
| 174 |                                         if ( common ) {
 | 
|---|
| 175 |                                                 common->get_qualifiers() = Type::Qualifiers();
 | 
|---|
| 176 |                                                 delete curClass.type;
 | 
|---|
| 177 |                                                 curClass.type = common;
 | 
|---|
| 178 |                                                 env.add( curClass );
 | 
|---|
| 179 |                                         } // if
 | 
|---|
| 180 |                                         return true;
 | 
|---|
| 181 |                                 } else {
 | 
|---|
| 182 |                                         return false;
 | 
|---|
| 183 |                                 } // if
 | 
|---|
| 184 |                         } else {
 | 
|---|
| 185 |                                 curClass.type = other->clone();
 | 
|---|
| 186 |                                 curClass.type->get_qualifiers() = Type::Qualifiers();
 | 
|---|
| 187 |                                 curClass.allowWidening = widenMode.widenFirst && widenMode.widenSecond;
 | 
|---|
| 188 |                                 env.add( curClass );
 | 
|---|
| 189 |                         } // if
 | 
|---|
| 190 |                 } else {
 | 
|---|
| 191 |                         EqvClass newClass;
 | 
|---|
| 192 |                         newClass.vars.insert( typeInst->get_name() );
 | 
|---|
| 193 |                         newClass.type = other->clone();
 | 
|---|
| 194 |                         newClass.type->get_qualifiers() = Type::Qualifiers();
 | 
|---|
| 195 |                         newClass.allowWidening = widenMode.widenFirst && widenMode.widenSecond;
 | 
|---|
| 196 |                         newClass.data = data;
 | 
|---|
| 197 |                         env.add( newClass );
 | 
|---|
| 198 |                 } // if
 | 
|---|
| 199 |                 return true;
 | 
|---|
| 200 |         }
 | 
|---|
| 201 | 
 | 
|---|
| 202 |         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 ) {
 | 
|---|
| 203 |                 bool result = true;
 | 
|---|
| 204 |                 EqvClass class1, class2;
 | 
|---|
| 205 |                 bool hasClass1 = false, hasClass2 = false;
 | 
|---|
| 206 |                 bool widen1 = false, widen2 = false;
 | 
|---|
| 207 |                 Type *type1 = 0, *type2 = 0;
 | 
|---|
| 208 | 
 | 
|---|
| 209 |                 if ( env.lookup( var1->get_name(), class1 ) ) {
 | 
|---|
| 210 |                         hasClass1 = true;
 | 
|---|
| 211 |                         if ( class1.type ) {
 | 
|---|
| 212 |                                 if ( occurs( class1.type, var2->get_name(), env ) ) {
 | 
|---|
| 213 |                                         return false;
 | 
|---|
| 214 |                                 } // if
 | 
|---|
| 215 |                                 type1 = class1.type->clone();
 | 
|---|
| 216 |                         } // if
 | 
|---|
| 217 |                         widen1 = widenMode.widenFirst && class1.allowWidening;
 | 
|---|
| 218 |                 } // if
 | 
|---|
| 219 |                 if ( env.lookup( var2->get_name(), class2 ) ) {
 | 
|---|
| 220 |                         hasClass2 = true;
 | 
|---|
| 221 |                         if ( class2.type ) {
 | 
|---|
| 222 |                                 if ( occurs( class2.type, var1->get_name(), env ) ) {
 | 
|---|
| 223 |                                         return false;
 | 
|---|
| 224 |                                 } // if
 | 
|---|
| 225 |                                 type2 = class2.type->clone();
 | 
|---|
| 226 |                         } // if
 | 
|---|
| 227 |                         widen2 = widenMode.widenSecond && class2.allowWidening;
 | 
|---|
| 228 |                 } // if
 | 
|---|
| 229 | 
 | 
|---|
| 230 |                 if ( type1 && type2 ) {
 | 
|---|
| 231 | //    std::cerr << "has type1 && type2" << std::endl;
 | 
|---|
| 232 |                         WidenMode newWidenMode ( widen1, widen2 );
 | 
|---|
| 233 |                         Type *common = 0;
 | 
|---|
| 234 |                         if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, newWidenMode, indexer, common ) ) {
 | 
|---|
| 235 |                                 class1.vars.insert( class2.vars.begin(), class2.vars.end() );
 | 
|---|
| 236 |                                 class1.allowWidening = widen1 && widen2;
 | 
|---|
| 237 |                                 if ( common ) {
 | 
|---|
| 238 |                                         common->get_qualifiers() = Type::Qualifiers();
 | 
|---|
| 239 |                                         delete class1.type;
 | 
|---|
| 240 |                                         class1.type = common;
 | 
|---|
| 241 |                                 } // if
 | 
|---|
| 242 |                                 env.add( class1 );
 | 
|---|
| 243 |                         } else {
 | 
|---|
| 244 |                                 result = false;
 | 
|---|
| 245 |                         } // if
 | 
|---|
| 246 |                 } else if ( hasClass1 && hasClass2 ) {
 | 
|---|
| 247 |                         if ( type1 ) {
 | 
|---|
| 248 |                                 class1.vars.insert( class2.vars.begin(), class2.vars.end() );
 | 
|---|
| 249 |                                 class1.allowWidening = widen1;
 | 
|---|
| 250 |                                 env.add( class1 );
 | 
|---|
| 251 |                         } else {
 | 
|---|
| 252 |                                 class2.vars.insert( class1.vars.begin(), class1.vars.end() );
 | 
|---|
| 253 |                                 class2.allowWidening = widen2;
 | 
|---|
| 254 |                                 env.add( class2 );
 | 
|---|
| 255 |                         } // if
 | 
|---|
| 256 |                 } else if ( hasClass1 ) {
 | 
|---|
| 257 |                         class1.vars.insert( var2->get_name() );
 | 
|---|
| 258 |                         class1.allowWidening = widen1;
 | 
|---|
| 259 |                         env.add( class1 );
 | 
|---|
| 260 |                 } else if ( hasClass2 ) {
 | 
|---|
| 261 |                         class2.vars.insert( var1->get_name() );
 | 
|---|
| 262 |                         class2.allowWidening = widen2;
 | 
|---|
| 263 |                         env.add( class2 );
 | 
|---|
| 264 |                 } else {
 | 
|---|
| 265 |                         EqvClass newClass;
 | 
|---|
| 266 |                         newClass.vars.insert( var1->get_name() );
 | 
|---|
| 267 |                         newClass.vars.insert( var2->get_name() );
 | 
|---|
| 268 |                         newClass.allowWidening = widen1 && widen2;
 | 
|---|
| 269 |                         newClass.data = data;
 | 
|---|
| 270 |                         env.add( newClass );
 | 
|---|
| 271 |                 } // if
 | 
|---|
| 272 |                 delete type1;
 | 
|---|
| 273 |                 delete type2;
 | 
|---|
| 274 |                 return result;
 | 
|---|
| 275 |         }
 | 
|---|
| 276 | 
 | 
|---|
| 277 |         bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
 | 
|---|
| 278 |                 OpenVarSet closedVars;
 | 
|---|
| 279 |                 findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
 | 
|---|
| 280 |                 findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
 | 
|---|
| 281 |                 Type *commonType = 0;
 | 
|---|
| 282 |                 if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType ) ) {
 | 
|---|
| 283 |                         if ( commonType ) {
 | 
|---|
| 284 |                                 delete commonType;
 | 
|---|
| 285 |                         } // if
 | 
|---|
| 286 |                         return true;
 | 
|---|
| 287 |                 } else {
 | 
|---|
| 288 |                         return false;
 | 
|---|
| 289 |                 } // if
 | 
|---|
| 290 |         }
 | 
|---|
| 291 | 
 | 
|---|
| 292 |         bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer, Type *&commonType ) {
 | 
|---|
| 293 |                 OpenVarSet closedVars;
 | 
|---|
| 294 |                 findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
 | 
|---|
| 295 |                 findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
 | 
|---|
| 296 |                 return unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType );
 | 
|---|
| 297 |         }
 | 
|---|
| 298 | 
 | 
|---|
| 299 |         bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
 | 
|---|
| 300 | #ifdef DEBUG
 | 
|---|
| 301 |                 TypeEnvironment debugEnv( env );
 | 
|---|
| 302 | #endif
 | 
|---|
| 303 |                 if ( type1->get_qualifiers() != type2->get_qualifiers() ) {
 | 
|---|
| 304 |                         return false;
 | 
|---|
| 305 |                 }
 | 
|---|
| 306 | 
 | 
|---|
| 307 |                 bool result;
 | 
|---|
| 308 |                 TypeInstType *var1 = dynamic_cast< TypeInstType* >( type1 );
 | 
|---|
| 309 |                 TypeInstType *var2 = dynamic_cast< TypeInstType* >( type2 );
 | 
|---|
| 310 |                 OpenVarSet::const_iterator entry1, entry2;
 | 
|---|
| 311 |                 if ( var1 ) {
 | 
|---|
| 312 |                         entry1 = openVars.find( var1->get_name() );
 | 
|---|
| 313 |                 } // if
 | 
|---|
| 314 |                 if ( var2 ) {
 | 
|---|
| 315 |                         entry2 = openVars.find( var2->get_name() );
 | 
|---|
| 316 |                 } // if
 | 
|---|
| 317 |                 bool isopen1 = var1 && ( entry1 != openVars.end() );
 | 
|---|
| 318 |                 bool isopen2 = var2 && ( entry2 != openVars.end() );
 | 
|---|
| 319 | 
 | 
|---|
| 320 |                 if ( isopen1 && isopen2 && entry1->second == entry2->second ) {
 | 
|---|
| 321 |                         result = bindVarToVar( var1, var2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
 | 
|---|
| 322 |                 } else if ( isopen1 ) {
 | 
|---|
| 323 |                         result = bindVar( var1, type2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
 | 
|---|
| 324 |                 } else if ( isopen2 ) {
 | 
|---|
| 325 |                         result = bindVar( var2, type1, entry2->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
 | 
|---|
| 326 |                 } else {
 | 
|---|
| 327 |                         Unify comparator( type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
 | 
|---|
| 328 |                         type1->accept( comparator );
 | 
|---|
| 329 |                         result = comparator.get_result();
 | 
|---|
| 330 |                 } // if
 | 
|---|
| 331 | #ifdef DEBUG
 | 
|---|
| 332 |                 std::cerr << "============ unifyExact" << std::endl;
 | 
|---|
| 333 |                 std::cerr << "type1 is ";
 | 
|---|
| 334 |                 type1->print( std::cerr );
 | 
|---|
| 335 |                 std::cerr << std::endl << "type2 is ";
 | 
|---|
| 336 |                 type2->print( std::cerr );
 | 
|---|
| 337 |                 std::cerr << std::endl << "openVars are ";
 | 
|---|
| 338 |                 printOpenVarSet( openVars, std::cerr, 8 );
 | 
|---|
| 339 |                 std::cerr << std::endl << "input env is " << std::endl;
 | 
|---|
| 340 |                 debugEnv.print( std::cerr, 8 );
 | 
|---|
| 341 |                 std::cerr << std::endl << "result env is " << std::endl;
 | 
|---|
| 342 |                 env.print( std::cerr, 8 );
 | 
|---|
| 343 |                 std::cerr << "result is " << result << std::endl;
 | 
|---|
| 344 | #endif
 | 
|---|
| 345 |                 return result;
 | 
|---|
| 346 |         }
 | 
|---|
| 347 | 
 | 
|---|
| 348 |         bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
 | 
|---|
| 349 |                 return unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
 | 
|---|
| 350 |         }
 | 
|---|
| 351 | 
 | 
|---|
| 352 |         bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common ) {
 | 
|---|
| 353 |                 Type::Qualifiers tq1 = type1->get_qualifiers(), tq2 = type2->get_qualifiers();
 | 
|---|
| 354 |                 type1->get_qualifiers() = Type::Qualifiers();
 | 
|---|
| 355 |                 type2->get_qualifiers() = Type::Qualifiers();
 | 
|---|
| 356 |                 bool result;
 | 
|---|
| 357 | #ifdef DEBUG
 | 
|---|
| 358 |                 std::cerr << "unifyInexact type 1 is ";
 | 
|---|
| 359 |                 type1->print( std::cerr );
 | 
|---|
| 360 |                 std::cerr << " type 2 is ";
 | 
|---|
| 361 |                 type2->print( std::cerr );
 | 
|---|
| 362 |                 std::cerr << std::endl;
 | 
|---|
| 363 | #endif
 | 
|---|
| 364 |                 if ( ! unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer ) ) {
 | 
|---|
| 365 | #ifdef DEBUG
 | 
|---|
| 366 |                         std::cerr << "unifyInexact: no exact unification found" << std::endl;
 | 
|---|
| 367 | #endif
 | 
|---|
| 368 |                         if ( ( common = commonType( type1, type2, widenMode.widenFirst, widenMode.widenSecond, indexer, env, openVars ) ) ) {
 | 
|---|
| 369 |                                 common->get_qualifiers() = tq1 | tq2;
 | 
|---|
| 370 | #ifdef DEBUG
 | 
|---|
| 371 |                                 std::cerr << "unifyInexact: common type is ";
 | 
|---|
| 372 |                                 common->print( std::cerr );
 | 
|---|
| 373 |                                 std::cerr << std::endl;
 | 
|---|
| 374 | #endif
 | 
|---|
| 375 |                                 result = true;
 | 
|---|
| 376 |                         } else {
 | 
|---|
| 377 | #ifdef DEBUG
 | 
|---|
| 378 |                                 std::cerr << "unifyInexact: no common type found" << std::endl;
 | 
|---|
| 379 | #endif
 | 
|---|
| 380 |                                 result = false;
 | 
|---|
| 381 |                         } // if
 | 
|---|
| 382 |                 } else {
 | 
|---|
| 383 |                         if ( tq1 != tq2 ) {
 | 
|---|
| 384 |                                 if ( ( tq1 > tq2 || widenMode.widenFirst ) && ( tq2 > tq1 || widenMode.widenSecond ) ) {
 | 
|---|
| 385 |                                         common = type1->clone();
 | 
|---|
| 386 |                                         common->get_qualifiers() = tq1 | tq2;
 | 
|---|
| 387 |                                         result = true;
 | 
|---|
| 388 |                                 } else {
 | 
|---|
| 389 |                                         result = false;
 | 
|---|
| 390 |                                 } // if
 | 
|---|
| 391 |                         } else {
 | 
|---|
| 392 |                                 common = type1->clone();
 | 
|---|
| 393 |                                 common->get_qualifiers() = tq1 | tq2;
 | 
|---|
| 394 |                                 result = true;
 | 
|---|
| 395 |                         } // if
 | 
|---|
| 396 |                 } // if
 | 
|---|
| 397 |                 type1->get_qualifiers() = tq1;
 | 
|---|
| 398 |                 type2->get_qualifiers() = tq2;
 | 
|---|
| 399 |                 return result;
 | 
|---|
| 400 |         }
 | 
|---|
| 401 | 
 | 
|---|
| 402 |         Unify::Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer )
 | 
|---|
| 403 |                 : result( false ), type2( type2 ), env( env ), needAssertions( needAssertions ), haveAssertions( haveAssertions ), openVars( openVars ), widenMode( widenMode ), indexer( indexer ) {
 | 
|---|
| 404 |         }
 | 
|---|
| 405 | 
 | 
|---|
| 406 |         void Unify::visit( __attribute__((unused)) VoidType *voidType) {
 | 
|---|
| 407 |                 result = dynamic_cast< VoidType* >( type2 );
 | 
|---|
| 408 |         }
 | 
|---|
| 409 | 
 | 
|---|
| 410 |         void Unify::visit(BasicType *basicType) {
 | 
|---|
| 411 |                 if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) {
 | 
|---|
| 412 |                         result = basicType->get_kind() == otherBasic->get_kind();
 | 
|---|
| 413 |                 } // if
 | 
|---|
| 414 |         }
 | 
|---|
| 415 | 
 | 
|---|
| 416 |         void markAssertionSet( AssertionSet &assertions, DeclarationWithType *assert ) {
 | 
|---|
| 417 | ///   std::cerr << "assertion set is" << std::endl;
 | 
|---|
| 418 | ///   printAssertionSet( assertions, std::cerr, 8 );
 | 
|---|
| 419 | ///   std::cerr << "looking for ";
 | 
|---|
| 420 | ///   assert->print( std::cerr );
 | 
|---|
| 421 | ///   std::cerr << std::endl;
 | 
|---|
| 422 |                 AssertionSet::iterator i = assertions.find( assert );
 | 
|---|
| 423 |                 if ( i != assertions.end() ) {
 | 
|---|
| 424 | ///     std::cerr << "found it!" << std::endl;
 | 
|---|
| 425 |                         i->second.isUsed = true;
 | 
|---|
| 426 |                 } // if
 | 
|---|
| 427 |         }
 | 
|---|
| 428 | 
 | 
|---|
| 429 |         void markAssertions( AssertionSet &assertion1, AssertionSet &assertion2, Type *type ) {
 | 
|---|
| 430 |                 for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
 | 
|---|
| 431 |                         for ( std::list< DeclarationWithType* >::const_iterator assert = (*tyvar)->get_assertions().begin(); assert != (*tyvar)->get_assertions().end(); ++assert ) {
 | 
|---|
| 432 |                                 markAssertionSet( assertion1, *assert );
 | 
|---|
| 433 |                                 markAssertionSet( assertion2, *assert );
 | 
|---|
| 434 |                         } // for
 | 
|---|
| 435 |                 } // for
 | 
|---|
| 436 |         }
 | 
|---|
| 437 | 
 | 
|---|
| 438 |         void Unify::visit(PointerType *pointerType) {
 | 
|---|
| 439 |                 if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) {
 | 
|---|
| 440 |                         result = unifyExact( pointerType->get_base(), otherPointer->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
 | 
|---|
| 441 |                         markAssertions( haveAssertions, needAssertions, pointerType );
 | 
|---|
| 442 |                         markAssertions( haveAssertions, needAssertions, otherPointer );
 | 
|---|
| 443 |                 } // if
 | 
|---|
| 444 |         }
 | 
|---|
| 445 | 
 | 
|---|
| 446 |         void Unify::visit(ReferenceType *refType) {
 | 
|---|
| 447 |                 if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) {
 | 
|---|
| 448 |                         result = unifyExact( refType->get_base(), otherRef->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
 | 
|---|
| 449 |                         markAssertions( haveAssertions, needAssertions, refType );
 | 
|---|
| 450 |                         markAssertions( haveAssertions, needAssertions, otherRef );
 | 
|---|
| 451 |                 } // if
 | 
|---|
| 452 |         }
 | 
|---|
| 453 | 
 | 
|---|
| 454 |         void Unify::visit(ArrayType *arrayType) {
 | 
|---|
| 455 |                 ArrayType *otherArray = dynamic_cast< ArrayType* >( type2 );
 | 
|---|
| 456 |                 // to unify, array types must both be VLA or both not VLA
 | 
|---|
| 457 |                 // and must both have a dimension expression or not have a dimension
 | 
|---|
| 458 |                 if ( otherArray && arrayType->get_isVarLen() == otherArray->get_isVarLen() ) {
 | 
|---|
| 459 | 
 | 
|---|
| 460 |                         // not positive this is correct in all cases, but it's needed for typedefs
 | 
|---|
| 461 |                         if ( arrayType->get_isVarLen() || otherArray->get_isVarLen() ) {
 | 
|---|
| 462 |                                 return;
 | 
|---|
| 463 |                         }
 | 
|---|
| 464 | 
 | 
|---|
| 465 |                         if ( ! arrayType->get_isVarLen() && ! otherArray->get_isVarLen() &&
 | 
|---|
| 466 |                                 arrayType->get_dimension() != 0 && otherArray->get_dimension() != 0 ) {
 | 
|---|
| 467 |                                 ConstantExpr * ce1 = dynamic_cast< ConstantExpr * >( arrayType->get_dimension() );
 | 
|---|
| 468 |                                 ConstantExpr * ce2 = dynamic_cast< ConstantExpr * >( otherArray->get_dimension() );
 | 
|---|
| 469 |                                 // see C11 Reference Manual 6.7.6.2.6
 | 
|---|
| 470 |                                 // two array types with size specifiers that are integer constant expressions are
 | 
|---|
| 471 |                                 // compatible if both size specifiers have the same constant value
 | 
|---|
| 472 |                                 if ( ce1 && ce2 ) {
 | 
|---|
| 473 |                                         Constant * c1 = ce1->get_constant();
 | 
|---|
| 474 |                                         Constant * c2 = ce2->get_constant();
 | 
|---|
| 475 | 
 | 
|---|
| 476 |                                         if ( c1->get_value() != c2->get_value() ) {
 | 
|---|
| 477 |                                                 // does not unify if the dimension is different
 | 
|---|
| 478 |                                                 return;
 | 
|---|
| 479 |                                         }
 | 
|---|
| 480 |                                 }
 | 
|---|
| 481 |                         }
 | 
|---|
| 482 | 
 | 
|---|
| 483 |                         result = unifyExact( arrayType->get_base(), otherArray->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
 | 
|---|
| 484 |                 } // if
 | 
|---|
| 485 |         }
 | 
|---|
| 486 | 
 | 
|---|
| 487 |         template< typename Iterator, typename Func >
 | 
|---|
| 488 |         std::unique_ptr<Type> combineTypes( Iterator begin, Iterator end, Func & toType ) {
 | 
|---|
| 489 |                 std::list< Type * > types;
 | 
|---|
| 490 |                 for ( ; begin != end; ++begin ) {
 | 
|---|
| 491 |                         // it's guaranteed that a ttype variable will be bound to a flat tuple, so ensure that this results in a flat tuple
 | 
|---|
| 492 |                         flatten( toType( *begin ), back_inserter( types ) );
 | 
|---|
| 493 |                 }
 | 
|---|
| 494 |                 return std::unique_ptr<Type>( new TupleType( Type::Qualifiers(), types ) );
 | 
|---|
| 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 |                 auto get_type = [](DeclarationWithType * dwt){ return dwt->get_type(); };
 | 
|---|
| 500 |                 for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
 | 
|---|
| 501 |                         Type * t1 = (*list1Begin)->get_type();
 | 
|---|
| 502 |                         Type * t2 = (*list2Begin)->get_type();
 | 
|---|
| 503 |                         bool isTtype1 = Tuples::isTtype( t1 );
 | 
|---|
| 504 |                         bool isTtype2 = Tuples::isTtype( t2 );
 | 
|---|
| 505 |                         // xxx - assumes ttype must be last parameter
 | 
|---|
| 506 |                         // xxx - there may be a nice way to refactor this, but be careful because the argument positioning might matter in some cases.
 | 
|---|
| 507 |                         if ( isTtype1 && ! isTtype2 ) {
 | 
|---|
| 508 |                                 // combine all of the things in list2, then unify
 | 
|---|
| 509 |                                 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
 | 
|---|
| 510 |                         } else if ( isTtype2 && ! isTtype1 ) {
 | 
|---|
| 511 |                                 // combine all of the things in list1, then unify
 | 
|---|
| 512 |                                 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
 | 
|---|
| 513 |                         } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
 | 
|---|
| 514 |                                 return false;
 | 
|---|
| 515 |                         } // if
 | 
|---|
| 516 |                 } // for
 | 
|---|
| 517 |                 // 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
 | 
|---|
| 518 |                 if ( list1Begin != list1End ) {
 | 
|---|
| 519 |                         // try unifying empty tuple type with ttype
 | 
|---|
| 520 |                         Type * t1 = (*list1Begin)->get_type();
 | 
|---|
| 521 |                         if ( Tuples::isTtype( t1 ) ) {
 | 
|---|
| 522 |                                 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
 | 
|---|
| 523 |                         } else return false;
 | 
|---|
| 524 |                 } else if ( list2Begin != list2End ) {
 | 
|---|
| 525 |                         // try unifying empty tuple type with ttype
 | 
|---|
| 526 |                         Type * t2 = (*list2Begin)->get_type();
 | 
|---|
| 527 |                         if ( Tuples::isTtype( t2 ) ) {
 | 
|---|
| 528 |                                 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
 | 
|---|
| 529 |                         } else return false;
 | 
|---|
| 530 |                 } else {
 | 
|---|
| 531 |                         return true;
 | 
|---|
| 532 |                 } // if
 | 
|---|
| 533 |         }
 | 
|---|
| 534 | 
 | 
|---|
| 535 |         /// Finds ttypes and replaces them with their expansion, if known.
 | 
|---|
| 536 |         /// This needs to be done so that satisfying ttype assertions is easier.
 | 
|---|
| 537 |         /// If this isn't done then argument lists can have wildly different
 | 
|---|
| 538 |         /// size and structure, when they should be compatible.
 | 
|---|
| 539 |         struct TtypeExpander : public Mutator {
 | 
|---|
| 540 |                 TypeEnvironment & env;
 | 
|---|
| 541 |                 TtypeExpander( TypeEnvironment & env ) : env( env ) {}
 | 
|---|
| 542 |                 Type * mutate( TypeInstType * typeInst ) {
 | 
|---|
| 543 |                         EqvClass eqvClass;
 | 
|---|
| 544 |                         if ( env.lookup( typeInst->get_name(), eqvClass ) ) {
 | 
|---|
| 545 |                                 if ( eqvClass.data.kind == TypeDecl::Ttype ) {
 | 
|---|
| 546 |                                         // expand ttype parameter into its actual type
 | 
|---|
| 547 |                                         if ( eqvClass.type ) {
 | 
|---|
| 548 |                                                 delete typeInst;
 | 
|---|
| 549 |                                                 return eqvClass.type->clone();
 | 
|---|
| 550 |                                         }
 | 
|---|
| 551 |                                 }
 | 
|---|
| 552 |                         }
 | 
|---|
| 553 |                         return typeInst;
 | 
|---|
| 554 |                 }
 | 
|---|
| 555 |         };
 | 
|---|
| 556 | 
 | 
|---|
| 557 |         /// flattens a list of declarations, so that each tuple type has a single declaration.
 | 
|---|
| 558 |         /// makes use of TtypeExpander to ensure ttypes are flat as well.
 | 
|---|
| 559 |         void flattenList( std::list< DeclarationWithType * > src, std::list< DeclarationWithType * > & dst, TypeEnvironment & env ) {
 | 
|---|
| 560 |                 dst.clear();
 | 
|---|
| 561 |                 for ( DeclarationWithType * dcl : src ) {
 | 
|---|
| 562 |                         TtypeExpander expander( env );
 | 
|---|
| 563 |                         dcl->acceptMutator( expander );
 | 
|---|
| 564 |                         std::list< Type * > types;
 | 
|---|
| 565 |                         flatten( dcl->get_type(), back_inserter( types ) );
 | 
|---|
| 566 |                         for ( Type * t : types ) {
 | 
|---|
| 567 |                                 dst.push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::C, nullptr, t, nullptr ) );
 | 
|---|
| 568 |                         }
 | 
|---|
| 569 |                         delete dcl;
 | 
|---|
| 570 |                 }
 | 
|---|
| 571 |         }
 | 
|---|
| 572 | 
 | 
|---|
| 573 |         void Unify::visit(FunctionType *functionType) {
 | 
|---|
| 574 |                 FunctionType *otherFunction = dynamic_cast< FunctionType* >( type2 );
 | 
|---|
| 575 |                 if ( otherFunction && functionType->get_isVarArgs() == otherFunction->get_isVarArgs() ) {
 | 
|---|
| 576 |                         // flatten the parameter lists for both functions so that tuple structure
 | 
|---|
| 577 |                         // doesn't affect unification. Must be a clone so that the types don't change.
 | 
|---|
| 578 |                         std::unique_ptr<FunctionType> flatFunc( functionType->clone() );
 | 
|---|
| 579 |                         std::unique_ptr<FunctionType> flatOther( otherFunction->clone() );
 | 
|---|
| 580 |                         flattenList( flatFunc->get_parameters(), flatFunc->get_parameters(), env );
 | 
|---|
| 581 |                         flattenList( flatOther->get_parameters(), flatOther->get_parameters(), env );
 | 
|---|
| 582 | 
 | 
|---|
| 583 |                         // sizes don't have to match if ttypes are involved; need to be more precise wrt where the ttype is to prevent errors
 | 
|---|
| 584 |                         if ( (flatFunc->get_parameters().size() == flatOther->get_parameters().size() && flatFunc->get_returnVals().size() == flatOther->get_returnVals().size()) || flatFunc->isTtype() || flatOther->isTtype() ) {
 | 
|---|
| 585 |                                 if ( unifyDeclList( flatFunc->get_parameters().begin(), flatFunc->get_parameters().end(), flatOther->get_parameters().begin(), flatOther->get_parameters().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
 | 
|---|
| 586 |                                         if ( unifyDeclList( flatFunc->get_returnVals().begin(), flatFunc->get_returnVals().end(), flatOther->get_returnVals().begin(), flatOther->get_returnVals().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
 | 
|---|
| 587 | 
 | 
|---|
| 588 |                                                 // the original types must be used in mark assertions, since pointer comparisons are used
 | 
|---|
| 589 |                                                 markAssertions( haveAssertions, needAssertions, functionType );
 | 
|---|
| 590 |                                                 markAssertions( haveAssertions, needAssertions, otherFunction );
 | 
|---|
| 591 | 
 | 
|---|
| 592 |                                                 result = true;
 | 
|---|
| 593 |                                         } // if
 | 
|---|
| 594 |                                 } // if
 | 
|---|
| 595 |                         } // if
 | 
|---|
| 596 |                 } // if
 | 
|---|
| 597 |         }
 | 
|---|
| 598 | 
 | 
|---|
| 599 |         template< typename RefType >
 | 
|---|
| 600 |         void Unify::handleRefType( RefType *inst, Type *other ) {
 | 
|---|
| 601 |                 // check that other type is compatible and named the same
 | 
|---|
| 602 |                 RefType *otherStruct = dynamic_cast< RefType* >( other );
 | 
|---|
| 603 |                 result = otherStruct && inst->get_name() == otherStruct->get_name();
 | 
|---|
| 604 |         }
 | 
|---|
| 605 | 
 | 
|---|
| 606 |         template< typename RefType >
 | 
|---|
| 607 |         void Unify::handleGenericRefType( RefType *inst, Type *other ) {
 | 
|---|
| 608 |                 // Check that other type is compatible and named the same
 | 
|---|
| 609 |                 handleRefType( inst, other );
 | 
|---|
| 610 |                 if ( ! result ) return;
 | 
|---|
| 611 |                 // Check that parameters of types unify, if any
 | 
|---|
| 612 |                 std::list< Expression* > params = inst->get_parameters();
 | 
|---|
| 613 |                 std::list< Expression* > otherParams = ((RefType*)other)->get_parameters();
 | 
|---|
| 614 | 
 | 
|---|
| 615 |                 std::list< Expression* >::const_iterator it = params.begin(), jt = otherParams.begin();
 | 
|---|
| 616 |                 for ( ; it != params.end() && jt != otherParams.end(); ++it, ++jt ) {
 | 
|---|
| 617 |                         TypeExpr *param = dynamic_cast< TypeExpr* >(*it);
 | 
|---|
| 618 |                         assertf(param, "Aggregate parameters should be type expressions");
 | 
|---|
| 619 |                         TypeExpr *otherParam = dynamic_cast< TypeExpr* >(*jt);
 | 
|---|
| 620 |                         assertf(otherParam, "Aggregate parameters should be type expressions");
 | 
|---|
| 621 | 
 | 
|---|
| 622 |                         Type* paramTy = param->get_type();
 | 
|---|
| 623 |                         Type* otherParamTy = otherParam->get_type();
 | 
|---|
| 624 | 
 | 
|---|
| 625 |                         bool tupleParam = Tuples::isTtype( paramTy );
 | 
|---|
| 626 |                         bool otherTupleParam = Tuples::isTtype( otherParamTy );
 | 
|---|
| 627 | 
 | 
|---|
| 628 |                         if ( tupleParam && otherTupleParam ) {
 | 
|---|
| 629 |                                 ++it; ++jt;  // skip ttype parameters for break
 | 
|---|
| 630 |                         } else if ( tupleParam ) {
 | 
|---|
| 631 |                                 // bundle other parameters into tuple to match
 | 
|---|
| 632 |                                 std::list< Type * > binderTypes;
 | 
|---|
| 633 | 
 | 
|---|
| 634 |                                 do {
 | 
|---|
| 635 |                                         binderTypes.push_back( otherParam->get_type()->clone() );
 | 
|---|
| 636 |                                         ++jt;
 | 
|---|
| 637 | 
 | 
|---|
| 638 |                                         if ( jt == otherParams.end() ) break;
 | 
|---|
| 639 | 
 | 
|---|
| 640 |                                         otherParam = dynamic_cast< TypeExpr* >(*jt);
 | 
|---|
| 641 |                                         assertf(otherParam, "Aggregate parameters should be type expressions");
 | 
|---|
| 642 |                                 } while (true);
 | 
|---|
| 643 | 
 | 
|---|
| 644 |                                 otherParamTy = new TupleType{ paramTy->get_qualifiers(), binderTypes };
 | 
|---|
| 645 |                                 ++it;  // skip ttype parameter for break
 | 
|---|
| 646 |                         } else if ( otherTupleParam ) {
 | 
|---|
| 647 |                                 // bundle parameters into tuple to match other
 | 
|---|
| 648 |                                 std::list< Type * > binderTypes;
 | 
|---|
| 649 | 
 | 
|---|
| 650 |                                 do {
 | 
|---|
| 651 |                                         binderTypes.push_back( param->get_type()->clone() );
 | 
|---|
| 652 |                                         ++it;
 | 
|---|
| 653 | 
 | 
|---|
| 654 |                                         if ( it == params.end() ) break;
 | 
|---|
| 655 | 
 | 
|---|
| 656 |                                         param = dynamic_cast< TypeExpr* >(*it);
 | 
|---|
| 657 |                                         assertf(param, "Aggregate parameters should be type expressions");
 | 
|---|
| 658 |                                 } while (true);
 | 
|---|
| 659 | 
 | 
|---|
| 660 |                                 paramTy = new TupleType{ otherParamTy->get_qualifiers(), binderTypes };
 | 
|---|
| 661 |                                 ++jt;  // skip ttype parameter for break
 | 
|---|
| 662 |                         }
 | 
|---|
| 663 | 
 | 
|---|
| 664 |                         if ( ! unifyExact( paramTy, otherParamTy, env, needAssertions, haveAssertions, openVars, WidenMode(false, false), indexer ) ) {
 | 
|---|
| 665 |                                 result = false;
 | 
|---|
| 666 |                                 return;
 | 
|---|
| 667 |                         }
 | 
|---|
| 668 | 
 | 
|---|
| 669 |                         // ttype parameter should be last
 | 
|---|
| 670 |                         if ( tupleParam || otherTupleParam ) break;
 | 
|---|
| 671 |                 }
 | 
|---|
| 672 |                 result = ( it == params.end() && jt == otherParams.end() );
 | 
|---|
| 673 |         }
 | 
|---|
| 674 | 
 | 
|---|
| 675 |         void Unify::visit(StructInstType *structInst) {
 | 
|---|
| 676 |                 handleGenericRefType( structInst, type2 );
 | 
|---|
| 677 |         }
 | 
|---|
| 678 | 
 | 
|---|
| 679 |         void Unify::visit(UnionInstType *unionInst) {
 | 
|---|
| 680 |                 handleGenericRefType( unionInst, type2 );
 | 
|---|
| 681 |         }
 | 
|---|
| 682 | 
 | 
|---|
| 683 |         void Unify::visit(EnumInstType *enumInst) {
 | 
|---|
| 684 |                 handleRefType( enumInst, type2 );
 | 
|---|
| 685 |         }
 | 
|---|
| 686 | 
 | 
|---|
| 687 |         void Unify::visit(TraitInstType *contextInst) {
 | 
|---|
| 688 |                 handleRefType( contextInst, type2 );
 | 
|---|
| 689 |         }
 | 
|---|
| 690 | 
 | 
|---|
| 691 |         void Unify::visit(TypeInstType *typeInst) {
 | 
|---|
| 692 |                 assert( openVars.find( typeInst->get_name() ) == openVars.end() );
 | 
|---|
| 693 |                 TypeInstType *otherInst = dynamic_cast< TypeInstType* >( type2 );
 | 
|---|
| 694 |                 if ( otherInst && typeInst->get_name() == otherInst->get_name() ) {
 | 
|---|
| 695 |                         result = true;
 | 
|---|
| 696 | ///   } else {
 | 
|---|
| 697 | ///     NamedTypeDecl *nt = indexer.lookupType( typeInst->get_name() );
 | 
|---|
| 698 | ///     if ( nt ) {
 | 
|---|
| 699 | ///       TypeDecl *type = dynamic_cast< TypeDecl* >( nt );
 | 
|---|
| 700 | ///       assert( type );
 | 
|---|
| 701 | ///       if ( type->get_base() ) {
 | 
|---|
| 702 | ///         result = unifyExact( type->get_base(), typeInst, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
 | 
|---|
| 703 | ///       }
 | 
|---|
| 704 | ///     }
 | 
|---|
| 705 |                 } // if
 | 
|---|
| 706 |         }
 | 
|---|
| 707 | 
 | 
|---|
| 708 |         template< typename Iterator1, typename Iterator2 >
 | 
|---|
| 709 |         bool unifyList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
 | 
|---|
| 710 |                 auto get_type = [](Type * t) { return t; };
 | 
|---|
| 711 |                 for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
 | 
|---|
| 712 |                         Type * t1 = *list1Begin;
 | 
|---|
| 713 |                         Type * t2 = *list2Begin;
 | 
|---|
| 714 |                         bool isTtype1 = Tuples::isTtype( t1 );
 | 
|---|
| 715 |                         bool isTtype2 = Tuples::isTtype( t2 );
 | 
|---|
| 716 |                         // xxx - assumes ttype must be last parameter
 | 
|---|
| 717 |                         // xxx - there may be a nice way to refactor this, but be careful because the argument positioning might matter in some cases.
 | 
|---|
| 718 |                         if ( isTtype1 && ! isTtype2 ) {
 | 
|---|
| 719 |                                 // combine all of the things in list2, then unify
 | 
|---|
| 720 |                                 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
 | 
|---|
| 721 |                         } else if ( isTtype2 && ! isTtype1 ) {
 | 
|---|
| 722 |                                 // combine all of the things in list1, then unify
 | 
|---|
| 723 |                                 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
 | 
|---|
| 724 |                         } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
 | 
|---|
| 725 |                                 return false;
 | 
|---|
| 726 |                         } // if
 | 
|---|
| 727 | 
 | 
|---|
| 728 |                 } // for
 | 
|---|
| 729 |                 if ( list1Begin != list1End ) {
 | 
|---|
| 730 |                         // try unifying empty tuple type with ttype
 | 
|---|
| 731 |                         Type * t1 = *list1Begin;
 | 
|---|
| 732 |                         if ( Tuples::isTtype( t1 ) ) {
 | 
|---|
| 733 |                                 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
 | 
|---|
| 734 |                         } else return false;
 | 
|---|
| 735 |                 } else if ( list2Begin != list2End ) {
 | 
|---|
| 736 |                         // try unifying empty tuple type with ttype
 | 
|---|
| 737 |                         Type * t2 = *list2Begin;
 | 
|---|
| 738 |                         if ( Tuples::isTtype( t2 ) ) {
 | 
|---|
| 739 |                                 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
 | 
|---|
| 740 |                         } else return false;
 | 
|---|
| 741 |                 } else {
 | 
|---|
| 742 |                         return true;
 | 
|---|
| 743 |                 } // if
 | 
|---|
| 744 |         }
 | 
|---|
| 745 | 
 | 
|---|
| 746 |         void Unify::visit(TupleType *tupleType) {
 | 
|---|
| 747 |                 if ( TupleType *otherTuple = dynamic_cast< TupleType* >( type2 ) ) {
 | 
|---|
| 748 |                         std::unique_ptr<TupleType> flat1( tupleType->clone() );
 | 
|---|
| 749 |                         std::unique_ptr<TupleType> flat2( otherTuple->clone() );
 | 
|---|
| 750 |                         std::list<Type *> types1, types2;
 | 
|---|
| 751 | 
 | 
|---|
| 752 |                         TtypeExpander expander( env );
 | 
|---|
| 753 |                         flat1->acceptMutator( expander );
 | 
|---|
| 754 |                         flat2->acceptMutator( expander );
 | 
|---|
| 755 | 
 | 
|---|
| 756 |                         flatten( flat1.get(), back_inserter( types1 ) );
 | 
|---|
| 757 |                         flatten( flat2.get(), back_inserter( types2 ) );
 | 
|---|
| 758 | 
 | 
|---|
| 759 |                         result = unifyList( types1.begin(), types1.end(), types2.begin(), types2.end(), env, needAssertions, haveAssertions, openVars, indexer );
 | 
|---|
| 760 |                 } // if
 | 
|---|
| 761 |         }
 | 
|---|
| 762 | 
 | 
|---|
| 763 |         void Unify::visit( __attribute__((unused)) VarArgsType *varArgsType ) {
 | 
|---|
| 764 |                 result = dynamic_cast< VarArgsType* >( type2 );
 | 
|---|
| 765 |         }
 | 
|---|
| 766 | 
 | 
|---|
| 767 |         void Unify::visit( __attribute__((unused)) ZeroType *zeroType ) {
 | 
|---|
| 768 |                 result = dynamic_cast< ZeroType* >( type2 );
 | 
|---|
| 769 |         }
 | 
|---|
| 770 | 
 | 
|---|
| 771 |         void Unify::visit( __attribute__((unused)) OneType *oneType ) {
 | 
|---|
| 772 |                 result = dynamic_cast< OneType* >( type2 );
 | 
|---|
| 773 |         }
 | 
|---|
| 774 | 
 | 
|---|
| 775 |         // xxx - compute once and store in the FunctionType?
 | 
|---|
| 776 |         Type * extractResultType( FunctionType * function ) {
 | 
|---|
| 777 |                 if ( function->get_returnVals().size() == 0 ) {
 | 
|---|
| 778 |                         return new VoidType( Type::Qualifiers() );
 | 
|---|
| 779 |                 } else if ( function->get_returnVals().size() == 1 ) {
 | 
|---|
| 780 |                         return function->get_returnVals().front()->get_type()->clone();
 | 
|---|
| 781 |                 } else {
 | 
|---|
| 782 |                         std::list< Type * > types;
 | 
|---|
| 783 |                         for ( DeclarationWithType * decl : function->get_returnVals() ) {
 | 
|---|
| 784 |                                 types.push_back( decl->get_type()->clone() );
 | 
|---|
| 785 |                         } // for
 | 
|---|
| 786 |                         return new TupleType( Type::Qualifiers(), types );
 | 
|---|
| 787 |                 }
 | 
|---|
| 788 |         }
 | 
|---|
| 789 | } // namespace ResolvExpr
 | 
|---|
| 790 | 
 | 
|---|
| 791 | // Local Variables: //
 | 
|---|
| 792 | // tab-width: 4 //
 | 
|---|
| 793 | // mode: c++ //
 | 
|---|
| 794 | // compile-command: "make install" //
 | 
|---|
| 795 | // End: //
 | 
|---|