Changeset 62423350 for src/ResolvExpr


Ignore:
Timestamp:
Jun 29, 2017, 5:06:24 PM (8 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
a12d5aa
Parents:
bb1cd95
Message:

Big push on designations and initialization: works with generic types, tuples, arrays, tests pass.
Refactor guard_value_impl.
Add list of declarations to TupleType.

Location:
src/ResolvExpr
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/AlternativeFinder.cc

    rbb1cd95 r62423350  
    809809        }
    810810
     811        Expression * restructureCast( Expression * argExpr, Type * toType ) {
     812                if ( argExpr->get_result()->size() > 1 && ! toType->isVoid() ) {
     813                        // Argument expression is a tuple and the target type is not void. Cast each member of the tuple
     814                        // to its corresponding target type, producing the tuple of those cast expressions. If there are
     815                        // more components of the tuple than components in the target type, then excess components do not
     816                        // come out in the result expression (but UniqueExprs ensure that side effects will still be done).
     817                        if ( Tuples::maybeImpure( argExpr ) && ! dynamic_cast< UniqueExpr * >( argExpr ) ) {
     818                                // expressions which may contain side effects require a single unique instance of the expression.
     819                                argExpr = new UniqueExpr( argExpr );
     820                        }
     821                        std::list< Expression * > componentExprs;
     822                        for ( unsigned int i = 0; i < toType->size(); i++ ) {
     823                                // cast each component
     824                                TupleIndexExpr * idx = new TupleIndexExpr( argExpr->clone(), i );
     825                                componentExprs.push_back( restructureCast( idx, toType->getComponent( i ) ) );
     826                        }
     827                        delete argExpr;
     828                        assert( componentExprs.size() > 0 );
     829                        // produce the tuple of casts
     830                        return new TupleExpr( componentExprs );
     831                } else {
     832                        // handle normally
     833                        return new CastExpr( argExpr, toType->clone() );
     834                }
     835        }
     836
    811837        void AlternativeFinder::visit( CastExpr *castExpr ) {
    812838                Type *& toType = castExpr->get_result();
     
    840866                                thisCost += Cost( 0, 0, discardedValues );
    841867
    842                                 Expression * argExpr = i->expr->clone();
    843                                 if ( argExpr->get_result()->size() > 1 && ! castExpr->get_result()->isVoid() ) {
    844                                         // Argument expression is a tuple and the target type is not void. Cast each member of the tuple
    845                                         // to its corresponding target type, producing the tuple of those cast expressions. If there are
    846                                         // more components of the tuple than components in the target type, then excess components do not
    847                                         // come out in the result expression (but UniqueExprs ensure that side effects will still be done).
    848                                         if ( Tuples::maybeImpure( argExpr ) && ! dynamic_cast< UniqueExpr * >( argExpr ) ) {
    849                                                 // expressions which may contain side effects require a single unique instance of the expression.
    850                                                 argExpr = new UniqueExpr( argExpr );
    851                                         }
    852                                         std::list< Expression * > componentExprs;
    853                                         for ( unsigned int i = 0; i < castExpr->get_result()->size(); i++ ) {
    854                                                 // cast each component
    855                                                 TupleIndexExpr * idx = new TupleIndexExpr( argExpr->clone(), i );
    856                                                 componentExprs.push_back( new CastExpr( idx, castExpr->get_result()->getComponent( i )->clone() ) );
    857                                         }
    858                                         delete argExpr;
    859                                         assert( componentExprs.size() > 0 );
    860                                         // produce the tuple of casts
    861                                         candidates.push_back( Alternative( new TupleExpr( componentExprs ), i->env, i->cost, thisCost ) );
    862                                 } else {
    863                                         // handle normally
    864                                         candidates.push_back( Alternative( new CastExpr( argExpr->clone(), toType->clone() ), i->env, i->cost, thisCost ) );
    865                                 }
     868                                candidates.push_back( Alternative( restructureCast( i->expr->clone(), toType ), i->env, i->cost, thisCost ) );
    866869                        } // if
    867870                } // for
     
    11831186
    11841187        void AlternativeFinder::visit( UntypedInitExpr *initExpr ) {
    1185                 AlternativeFinder finder( indexer, env );
    1186                 finder.findWithAdjustment( initExpr->get_expr() );
    1187                 // handle each option like a cast -- should probably push this info into AltFinder as a kind of expression, both to keep common code close and to have less 'reach-in', but more 'push-in'
     1188                // handle each option like a cast
    11881189                AltList candidates;
    1189                 std::cerr << "untyped init expr: " << initExpr << std::endl;
     1190                PRINT( std::cerr << "untyped init expr: " << initExpr << std::endl; )
    11901191                // O(N^2) checks of d-types with e-types
    1191                 for ( Alternative & alt : finder.get_alternatives() ) {
    1192                         for ( InitAlternative & initAlt : initExpr->get_initAlts() ) {
     1192                for ( InitAlternative & initAlt : initExpr->get_initAlts() ) {
     1193                        Type * toType = resolveTypeof( initAlt.type, indexer );
     1194                        SymTab::validateType( toType, &indexer );
     1195                        adjustExprType( toType, env, indexer );
     1196                        // Ideally the call to findWithAdjustment could be moved out of the loop, but unfortunately it currently has to occur inside or else
     1197                        // polymorphic return types are not properly bound to the initialization type, since return type variables are only open for the duration of resolving
     1198                        // the UntypedExpr. This is only actually an issue in initialization contexts that allow more than one possible initialization type, but it is still suboptimal.
     1199                        AlternativeFinder finder( indexer, env );
     1200                        finder.targetType = toType;
     1201                        finder.findWithAdjustment( initExpr->get_expr() );
     1202                        for ( Alternative & alt : finder.get_alternatives() ) {
     1203                                TypeEnvironment newEnv( alt.env );
    11931204                                AssertionSet needAssertions, haveAssertions;
    1194                                 OpenVarSet openVars;
    1195                                 std::cerr << "  " << initAlt.type << " " << initAlt.designation << std::endl;
     1205                                OpenVarSet openVars;  // find things in env that don't have a "representative type" and claim those are open vars?
     1206                                PRINT( std::cerr << "  @ " << toType << " " << initAlt.designation << std::endl; )
    11961207                                // It's possible that a cast can throw away some values in a multiply-valued expression.  (An example is a
    11971208                                // cast-to-void, which casts from one value to zero.)  Figure out the prefix of the subexpression results
    11981209                                // that are cast directly.  The candidate is invalid if it has fewer results than there are types to cast
    11991210                                // to.
    1200                                 int discardedValues = alt.expr->get_result()->size() - initAlt.type->size();
     1211                                int discardedValues = alt.expr->get_result()->size() - toType->size();
    12011212                                if ( discardedValues < 0 ) continue;
    12021213                                // xxx - may need to go into tuple types and extract relevant types and use unifyList. Note that currently, this does not
    12031214                                // allow casting a tuple to an atomic type (e.g. (int)([1, 2, 3]))
    12041215                                // unification run for side-effects
    1205                                 unify( initAlt.type, alt.expr->get_result(), alt.env, needAssertions, haveAssertions, openVars, indexer );
    1206 
    1207                                 Cost thisCost = castCost( alt.expr->get_result(), initAlt.type, indexer, alt.env );
     1216                                unify( toType, alt.expr->get_result(), newEnv, needAssertions, haveAssertions, openVars, indexer ); // xxx - do some inspecting on this line... why isn't result bound to initAlt.type??
     1217
     1218                                Cost thisCost = castCost( alt.expr->get_result(), toType, indexer, newEnv );
    12081219                                if ( thisCost != Cost::infinity ) {
    12091220                                        // count one safe conversion for each value that is thrown away
    12101221                                        thisCost += Cost( 0, 0, discardedValues );
    1211                                         candidates.push_back( Alternative( new InitExpr( alt.expr->clone(), initAlt.type->clone(), initAlt.designation->clone() ), alt.env, alt.cost, thisCost ) );
     1222                                        candidates.push_back( Alternative( new InitExpr( restructureCast( alt.expr->clone(), toType ), initAlt.designation->clone() ), newEnv, alt.cost, thisCost ) );
    12121223                                }
    12131224                        }
  • src/ResolvExpr/CurrentObject.cc

    rbb1cd95 r62423350  
    2222#include "SynTree/Initializer.h"
    2323#include "SynTree/Type.h"
     24#include "SynTree/TypeSubstitution.h"
    2425
    2526#if 0
     
    3233        long long int getConstValue( ConstantExpr * constExpr ) {
    3334                if ( BasicType * basicType = dynamic_cast< BasicType * >( constExpr->get_result() ) ) {
    34                         if ( basicType->get_kind() == BasicType::Char || basicType->get_kind() == BasicType::SignedChar || basicType->get_kind() == BasicType::UnsignedChar ) {
    35                                 assertf( constExpr->get_constant()->get_value().size() == 3, "constant value with unusual length: %s", constExpr->get_constant()->get_value().c_str() );
    36                                 return constExpr->get_constant()->get_value()[1];
     35                        if ( basicType->isInteger() ) {
     36                                return constExpr->get_constant()->get_ival();
    3737                        } else {
    38                                 return stoll( constExpr->get_constant()->get_value() );
    39                         }
     38                                assertf( false, "Non-integer constant expression in getConstValue", toString( constExpr ).c_str() ); // xxx - might be semantic error
     39                        }
     40                } else if ( dynamic_cast< OneType * >( constExpr->get_result() ) ) {
     41                        return 1;
     42                } else if ( dynamic_cast< ZeroType * >( constExpr->get_result() ) ) {
     43                        return 0;
    4044                } else {
    41                         assertf( false, "unhandled type on getConstValue %s", constExpr->get_result() );
     45                        assertf( false, "unhandled type on getConstValue %s", toString( constExpr->get_result() ).c_str() ); // xxx - might be semantic error
    4246                }
    4347        }
     
    5660        std::ostream & operator<<( std::ostream & out, Indenter & indent ) {
    5761                return out << std::string(indent.indent, ' ');
     62        }
     63
     64        template< typename AggrInst >
     65        TypeSubstitution makeGenericSubstitution( AggrInst * inst ) {
     66                assert( inst );
     67                assert( inst->get_baseParameters() );
     68                std::list< TypeDecl * > baseParams = *inst->get_baseParameters();
     69                std::list< Expression * > typeSubs = inst->get_parameters();
     70                TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() );
     71                return subs;
     72        }
     73
     74        TypeSubstitution makeGenericSubstitution( Type * type ) {
     75                if ( StructInstType * inst = dynamic_cast< StructInstType * >( type ) ) {
     76                        return makeGenericSubstitution( inst );
     77                } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( type ) ) {
     78                        return makeGenericSubstitution( inst );
     79                } else {
     80                        return TypeSubstitution();
     81                }
    5882        }
    5983
     
    135159                void setSize( Expression * expr ) {
    136160                        if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) {
    137                                 size = getConstValue( constExpr ); // xxx - if not a constant expression, it's not simple to determine how long the array actually is, which is necessary for initialization to be done correctly -- fix this
     161                                size = getConstValue( constExpr );
    138162                                PRINT( std::cerr << "array type with size: " << size << std::endl; )
    139163                        }       else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
    140                                 setSize( castExpr->get_arg() );
     164                                setSize( castExpr->get_arg() ); // xxx - need to perform the conversion specified by the cast
    141165                        } else {
    142                                 assertf( false, "unhandled expression in setSize: %s", toString( expr ).c_str() );
     166                                assertf( false, "unhandled expression in setSize: %s", toString( expr ).c_str() ); // xxx - if not a constant expression, it's not simple to determine how long the array actually is, which is necessary for initialization to be done correctly -- fix this
    143167                        }
    144168                }
     
    231255        class AggregateIterator : public MemberIterator {
    232256        public:
    233                 typedef std::list<Declaration *>::iterator iterator;
    234                 const char * kind = ""; // for debug
    235                 ReferenceToType * inst = nullptr;
    236                 AggregateDecl * decl = nullptr;
     257                typedef std::list<Declaration *> MemberList;
     258                typedef MemberList::const_iterator iterator;
     259                std::string kind = ""; // for debug
     260                std::string name;
     261                Type * inst = nullptr;
     262                const MemberList & members;
    237263                iterator curMember;
    238                 bool atbegin = true; // false at first {small,big}Step -- this struct type is only added to the possibilities at the beginning
     264                bool atbegin = true; // false at first {small,big}Step -- this aggr type is only added to the possibilities at the beginning
    239265                Type * curType = nullptr;
    240266                MemberIterator * memberIter = nullptr;
    241 
    242                 AggregateIterator( const char * kind, ReferenceToType * inst, AggregateDecl * decl ) : kind( kind ), inst( inst ), decl( decl ), curMember( decl->get_members().begin() ) {
     267                mutable TypeSubstitution sub;
     268
     269                AggregateIterator( const std::string & kind, const std::string & name, Type * inst, const MemberList & members ) : kind( kind ), name( name ), inst( inst ), members( members ), curMember( members.begin() ), sub( makeGenericSubstitution( inst ) ) {
    243270                        init();
    244271                }
     
    249276
    250277                bool init() {
    251                         PRINT( std::cerr << "--init()--" << std::endl; )
    252                         if ( curMember != decl->get_members().end() ) {
     278                        PRINT( std::cerr << "--init()--" << members.size() << std::endl; )
     279                        if ( curMember != members.end() ) {
    253280                                if ( ObjectDecl * field = dynamic_cast< ObjectDecl * >( *curMember ) ) {
    254281                                        PRINT( std::cerr << "incremented to field: " << field << std::endl; )
     
    264291                        if (memberIter && *memberIter) {
    265292                                std::list<InitAlternative> ret = memberIter->first();
     293                                PRINT( std::cerr << "sub: " << sub << std::endl; )
    266294                                for ( InitAlternative & alt : ret ) {
    267295                                        PRINT( std::cerr << "iterating and adding designators" << std::endl; )
    268296                                        alt.designation->get_designators().push_front( new VariableExpr( safe_dynamic_cast< ObjectDecl * >( *curMember ) ) );
     297                                        // need to substitute for generic types, so that casts are to concrete types
     298                                        PRINT( std::cerr << "  type is: " << alt.type; )
     299                                        sub.apply( alt.type ); // also apply to designation??
     300                                        PRINT( std::cerr << " ==> " << alt.type << std::endl; )
    269301                                }
    270302                                return ret;
     
    276308                        if ( ! designators.empty() ) {
    277309                                if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( designators.front() ) ) {
    278                                         for ( curMember = decl->get_members().begin(); curMember != decl->get_members().end(); ++curMember ) {
     310                                        for ( curMember = members.begin(); curMember != members.end(); ++curMember ) {
    279311                                                if ( *curMember == varExpr->get_var() ) {
    280312                                                        designators.pop_front();
     
    282314                                                        memberIter = createMemberIterator( varExpr->get_result() );
    283315                                                        curType = varExpr->get_result();
    284                                                         atbegin = curMember == decl->get_members().begin() && designators.empty(); // xxx - is this right??
     316                                                        atbegin = curMember == members.begin() && designators.empty(); // xxx - is this the right condition for atbegin??
    285317                                                        memberIter->setPosition( designators );
    286318                                                        return;
    287319                                                } // if
    288320                                        } // for
    289                                         assertf( false, "could not find member in %s: %s", kind, toString( varExpr ).c_str() );
     321                                        assertf( false, "could not find member in %s: %s", kind.c_str(), toString( varExpr ).c_str() );
    290322                                } else {
    291                                         assertf( false, "bad designator given to %s: %s", kind, toString( designators.front() ).c_str() );
     323                                        assertf( false, "bad designator given to %s: %s", kind.c_str(), toString( designators.front() ).c_str() );
    292324                                } // if
    293325                        } // if
     
    336368
    337369                virtual void print( std::ostream & out, Indenter indent ) const {
    338                         out << kind << "(" << decl->get_name() << ")";
     370                        out << kind << "(" << name << ")";
    339371                        if ( memberIter ) {
    340372                                Indenter childIndent = indent+1;
     
    347379        class UnionIterator : public AggregateIterator {
    348380        public:
    349                 UnionIterator( UnionInstType * inst ) : AggregateIterator( "UnionIterator", inst, inst->get_baseUnion() ) {}
     381                UnionIterator( UnionInstType * inst ) : AggregateIterator( "UnionIterator", inst->get_name(), inst, inst->get_baseUnion()->get_members() ) {}
    350382
    351383                virtual operator bool() const { return (memberIter && *memberIter); }
     
    357389                        memberIter = nullptr;
    358390                        curType = nullptr;
    359                         curMember = decl->get_members().end();
     391                        curMember = members.end();
    360392                        return *this;
    361393                }
     
    365397        class StructIterator : public AggregateIterator {
    366398        public:
    367                 StructIterator( StructInstType * inst ) : AggregateIterator( "StructIterator", inst, inst->get_baseStruct() ) {}
    368 
    369                 virtual operator bool() const { return curMember != decl->get_members().end() || (memberIter && *memberIter); }
     399                StructIterator( StructInstType * inst ) : AggregateIterator( "StructIterator", inst->get_name(), inst, inst->get_baseStruct()->get_members() ) {}
     400
     401                virtual operator bool() const { return curMember != members.end() || (memberIter && *memberIter); }
    370402
    371403                virtual MemberIterator & bigStep() {
     
    375407                        memberIter = nullptr;
    376408                        curType = nullptr;
    377                         for ( ; curMember != decl->get_members().end(); ) {
     409                        for ( ; curMember != members.end(); ) {
     410                                ++curMember;
     411                                if ( init() ) {
     412                                        return *this;
     413                                }
     414                        }
     415                        return *this;
     416                }
     417        };
     418
     419        class TupleIterator : public AggregateIterator {
     420        public:
     421                TupleIterator( TupleType * inst ) : AggregateIterator( "TupleIterator", toString("Tuple", inst->size()), inst, inst->get_members() ) {}
     422
     423                virtual operator bool() const { return curMember != members.end() || (memberIter && *memberIter); }
     424
     425                virtual MemberIterator & bigStep() {
     426                        PRINT( std::cerr << "bigStep in " << kind << std::endl; )
     427                        atbegin = false;
     428                        delete memberIter;
     429                        memberIter = nullptr;
     430                        curType = nullptr;
     431                        for ( ; curMember != members.end(); ) {
    378432                                ++curMember;
    379433                                if ( init() ) {
     
    392446                                return new UnionIterator( uit );
    393447                        } else {
    394                                 assertf( false, "some other reftotype" );
     448                                assertf( dynamic_cast< TypeInstType * >( type ), "some other reftotype" );
     449                                return new SimpleIterator( type );
    395450                        }
    396451                } else if ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
    397452                        return new ArrayIterator( at );
     453                } else if ( TupleType * tt = dynamic_cast< TupleType * >( type ) ) {
     454                        return new TupleIterator( tt );
    398455                } else {
    399456                        return new SimpleIterator( type );
     
    494551                Type * type = objStack.top()->getNext();
    495552                if ( type ) {
    496                         // xxx - record types.front()?
    497553                        objStack.push( createMemberIterator( type ) );
    498554                } else {
     
    517573                return **objStack.top();
    518574        }
     575
     576        Type * CurrentObject::getCurrentType() {
     577                PRINT( std::cerr << "____getting current type" << std::endl; )
     578                assertf( ! objStack.empty(), "objstack empty in getCurrentType" );
     579                return objStack.top()->getNext();
     580        }
    519581} // namespace ResolvExpr
    520582
  • src/ResolvExpr/CurrentObject.h

    rbb1cd95 r62423350  
    4242                /// produces a list of alternatives (Type *, Designation *) for the current sub-object's initializer
    4343                std::list< InitAlternative > getOptions();
     44                /// produces the type of the current object but no subobjects
     45                Type * getCurrentType();
    4446
    4547        private:
  • src/ResolvExpr/Resolver.cc

    rbb1cd95 r62423350  
    375375        }
    376376
    377         template< typename Aggr >
    378         void lookupDesignation( Aggr * aggr, const std::list< Expression > & designators ) {
    379 
    380         }
    381 
    382377        void Resolver::visit( SingleInit *singleInit ) {
     378                // resolve initialization using the possibilities as determined by the currentObject cursor
    383379                UntypedInitExpr * untyped = new UntypedInitExpr( singleInit->get_value(), currentObject.getOptions() );
    384380                Expression * newExpr = findSingleExpression( untyped, *this );
    385381                InitExpr * initExpr = safe_dynamic_cast< InitExpr * >( newExpr );
    386                 singleInit->set_value( new CastExpr( initExpr->get_expr()->clone(), initExpr->get_result()->clone() ) );
     382
     383                // move cursor to the object that is actually initialized
    387384                currentObject.setNext( initExpr->get_designation() );
     385
     386                // discard InitExpr wrapper and retain relevant pieces
     387                newExpr = initExpr->get_expr();
     388                singleInit->get_value()->set_env( initExpr->get_env() );
     389                initExpr->set_expr( nullptr );
     390                initExpr->set_env( nullptr );
     391                delete initExpr;
     392
     393                // get the actual object's type (may not exactly match what comes back from the resolver due to conversions)
     394                Type * initContext = currentObject.getCurrentType();
     395
     396                // check if actual object's type is char[]
     397                if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
     398                        if ( isCharType( at->get_base() ) ) {
     399                                // check if the resolved type is char *
     400                                if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_result() ) ) {
     401                                        if ( isCharType( pt->get_base() ) ) {
     402                                                // strip cast if we're initializing a char[] with a char *, e.g.  char x[] = "hello";
     403                                                CastExpr *ce = safe_dynamic_cast< CastExpr * >( newExpr );
     404                                                newExpr = ce->get_arg();
     405                                                ce->set_arg( nullptr );
     406                                                delete ce;
     407                                        }
     408                                }
     409                        }
     410                }
     411
     412                // set initializer expr to resolved express
     413                singleInit->set_value( newExpr );
     414
     415                // move cursor to next object in preparation for next initializer
    388416                currentObject.increment();
    389                 delete initExpr;
    390 
    391                 // // check if initializing type is char[]
    392                 // if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
    393                 //      if ( isCharType( at->get_base() ) ) {
    394                 //              // check if the resolved type is char *
    395                 //              if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_result() ) ) {
    396                 //                      if ( isCharType( pt->get_base() ) ) {
    397                 //                              // strip cast if we're initializing a char[] with a char *, e.g.  char x[] = "hello";
    398                 //                              CastExpr *ce = dynamic_cast< CastExpr * >( newExpr );
    399                 //                              singleInit->set_value( ce->get_arg() );
    400                 //                              ce->set_arg( NULL );
    401                 //                              delete ce;
    402                 //                      }
    403                 //              }
    404                 //      }
    405                 // }
    406         }
    407 
    408         // template< typename AggrInst >
    409         // TypeSubstitution makeGenericSubstitution( AggrInst * inst ) {
    410         //      assert( inst );
    411         //      assert( inst->get_baseParameters() );
    412         //      std::list< TypeDecl * > baseParams = *inst->get_baseParameters();
    413         //      std::list< Expression * > typeSubs = inst->get_parameters();
    414         //      TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() );
    415         //      return subs;
    416         // }
    417 
    418         // ReferenceToType * isStructOrUnion( Type * type ) {
    419         //      if ( StructInstType * sit = dynamic_cast< StructInstType * >( type ) ) {
    420         //              return sit;
    421         //      } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( type ) ) {
    422         //              return uit;
    423         //      }
    424         //      return nullptr;
    425         // }
    426 
    427         // void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd, TypeSubstitution sub ) {
    428         //      ObjectDecl * obj = dynamic_cast< ObjectDecl * >( dcl );
    429         //      assert( obj );
    430         //      // need to substitute for generic types, so that casts are to concrete types
    431         //      currentObject = obj->clone();  // xxx - delete this
    432         //      sub.apply( currentObject );
    433 
    434         //      try {
    435         //              if ( init == initEnd ) return; // stop when there are no more initializers
    436         //              (*init)->accept( *this );
    437         //              ++init; // made it past an initializer
    438         //      } catch( SemanticError & ) {
    439         //              // need to delve deeper, if you can
    440         //              if ( ReferenceToType * type = isStructOrUnion( currentObject->get_type() ) ) {
    441         //                      resolveAggrInit( type, init, initEnd );
    442         //              } else {
    443         //                      // member is not an aggregate type, so can't go any deeper
    444 
    445         //                      // might need to rethink what is being thrown
    446         //                      throw;
    447         //              } // if
    448         //      }
    449         // }
    450 
    451         // void Resolver::resolveAggrInit( ReferenceToType * inst, InitIterator & init, InitIterator & initEnd ) {
    452         //      if ( StructInstType * sit = dynamic_cast< StructInstType * >( inst ) ) {
    453         //              TypeSubstitution sub = makeGenericSubstitution( sit );
    454         //              StructDecl * st = sit->get_baseStruct();
    455         //              if(st->get_members().empty()) return;
    456         //              // want to resolve each initializer to the members of the struct,
    457         //              // but if there are more initializers than members we should stop
    458         //              list< Declaration * >::iterator it = st->get_members().begin();
    459         //              for ( ; it != st->get_members().end(); ++it) {
    460         //                      resolveSingleAggrInit( *it, init, initEnd, sub );
    461         //              }
    462         //      } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( inst ) ) {
    463         //              TypeSubstitution sub = makeGenericSubstitution( uit );
    464         //              UnionDecl * un = uit->get_baseUnion();
    465         //              if(un->get_members().empty()) return;
    466         //              // only resolve to the first member of a union
    467         //              resolveSingleAggrInit( *un->get_members().begin(), init, initEnd, sub );
    468         //      } // if
    469         // }
     417        }
    470418
    471419        void Resolver::visit( ListInit * listInit ) {
     420                // move cursor into brace-enclosed initializer-list
    472421                currentObject.enterListInit();
    473422                // xxx - fix this so that the list isn't copied, iterator should be used to change current element
    474423                std::list<Designation *> newDesignations;
    475424                for ( auto p : group_iterate(listInit->get_designations(), listInit->get_initializers()) ) {
     425                        // iterate designations and initializers in pairs, moving the cursor to the current designated object and resolving
     426                        // the initializer against that object.
    476427                        Designation * des = std::get<0>(p);
    477428                        Initializer * init = std::get<1>(p);
     
    479430                        init->accept( *this );
    480431                }
     432                // set the set of 'resolved' designations and leave the brace-enclosed initializer-list
    481433                listInit->get_designations() = newDesignations; // xxx - memory management
    482434                currentObject.exitListInit();
    483435
     436                // xxx - this part has not be folded into CurrentObject yet
    484437                // } else if ( TypeInstType * tt = dynamic_cast< TypeInstType * >( initContext ) ) {
    485438                //      Type * base = tt->get_baseType()->get_base();
  • src/ResolvExpr/Unify.cc

    rbb1cd95 r62423350  
    606606                        } else if ( tupleParam ) {
    607607                                // bundle other parameters into tuple to match
    608                                 TupleType* binder = new TupleType{ paramTy->get_qualifiers() };
     608                                std::list< Type * > binderTypes;
    609609
    610610                                do {
    611                                         binder->get_types().push_back( otherParam->get_type()->clone() );
     611                                        binderTypes.push_back( otherParam->get_type()->clone() );
    612612                                        ++jt;
    613613
     
    618618                                } while (true);
    619619
    620                                 otherParamTy = binder;
     620                                otherParamTy = new TupleType{ paramTy->get_qualifiers(), binderTypes };
    621621                                ++it;  // skip ttype parameter for break
    622622                        } else if ( otherTupleParam ) {
    623623                                // bundle parameters into tuple to match other
    624                                 TupleType* binder = new TupleType{ otherParamTy->get_qualifiers() };
     624                                std::list< Type * > binderTypes;
    625625
    626626                                do {
    627                                         binder->get_types().push_back( param->get_type()->clone() );
     627                                        binderTypes.push_back( param->get_type()->clone() );
    628628                                        ++it;
    629629
     
    634634                                } while (true);
    635635
    636                                 paramTy = binder;
     636                                paramTy = new TupleType{ otherParamTy->get_qualifiers(), binderTypes };
    637637                                ++jt;  // skip ttype parameter for break
    638638                        }
     
    756756                        return function->get_returnVals().front()->get_type()->clone();
    757757                } else {
    758                         TupleType * tupleType = new TupleType( Type::Qualifiers() );
     758                        std::list< Type * > types;
    759759                        for ( DeclarationWithType * decl : function->get_returnVals() ) {
    760                                 tupleType->get_types().push_back( decl->get_type()->clone() );
     760                                types.push_back( decl->get_type()->clone() );
    761761                        } // for
    762                         return tupleType;
     762                        return new TupleType( Type::Qualifiers(), types );
    763763                }
    764764        }
Note: See TracChangeset for help on using the changeset viewer.