Ignore:
Timestamp:
Jun 29, 2017, 5:06:24 PM (7 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?.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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();
Note: See TracChangeset for help on using the changeset viewer.