Changeset a465caf


Ignore:
Timestamp:
Aug 8, 2016, 4:20:29 PM (8 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, 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:
0853178
Parents:
242d458
Message:

generate a field constructor for union types and some refactoring

Location:
src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/InitTweak.cc

    r242d458 ra465caf  
    291291        }
    292292
     293        namespace {
     294                template <typename Predicate>
     295                bool allofCtorDtor( Statement * stmt, const Predicate & pred ) {
     296                        std::list< Expression * > callExprs;
     297                        collectCtorDtorCalls( stmt, callExprs );
     298                        // if ( callExprs.empty() ) return false; // xxx - do I still need this check?
     299                        return std::all_of( callExprs.begin(), callExprs.end(), pred);
     300                }
     301        }
     302
    293303        bool isIntrinsicSingleArgCallStmt( Statement * stmt ) {
    294                 std::list< Expression * > callExprs;
    295                 collectCtorDtorCalls( stmt, callExprs );
    296                 // if ( callExprs.empty() ) return false; // xxx - do I still need this check?
    297                 return std::all_of( callExprs.begin(), callExprs.end(), []( Expression * callExpr ){
     304                return allofCtorDtor( stmt, []( Expression * callExpr ){
    298305                        if ( ApplicationExpr * appExpr = isIntrinsicCallExpr( callExpr ) ) {
    299306                                assert( ! appExpr->get_function()->get_results().empty() );
     
    303310                        }
    304311                        return false;
     312                });
     313        }
     314
     315        bool isIntrinsicCallStmt( Statement * stmt ) {
     316                return allofCtorDtor( stmt, []( Expression * callExpr ) {
     317                        return isIntrinsicCallExpr( callExpr );
    305318                });
    306319        }
  • src/InitTweak/InitTweak.h

    r242d458 ra465caf  
    4141        /// Intended to be used for default ctor/dtor calls, but might have use elsewhere.
    4242        /// Currently has assertions that make it less than fully general.
    43         bool isIntrinsicSingleArgCallStmt( Statement * expr );
     43        bool isIntrinsicSingleArgCallStmt( Statement * stmt );
     44
     45        /// True if stmt is a call statement where the function called is intrinsic.
     46        bool isIntrinsicCallStmt( Statement * stmt );
    4447
    4548        /// get all Ctor/Dtor call expressions from a Statement
  • src/ResolvExpr/Resolver.cc

    r242d458 ra465caf  
    550550                }
    551551
    552                 // xxx - todo
    553                 // if ( InitTweak::isIntrinsicCallStmt( ctorInit->get_ctor() ) ) {
    554                 //      // can reduce the constructor down to a SingleInit using the
    555                 //      // second argument from the ctor call
    556                 // }
    557 
    558552                if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_dtor() ) ) {
    559553                        delete ctorInit->get_dtor();
    560554                        ctorInit->set_dtor( NULL );
    561555                }
     556
     557                // xxx - todo -- what about arrays?
     558                // if ( dtor == NULL && InitTweak::isIntrinsicCallStmt( ctorInit->get_ctor() ) ) {
     559                //      // can reduce the constructor down to a SingleInit using the
     560                //      // second argument from the ctor call, since
     561                //      delete ctorInit->get_ctor();
     562                //      ctorInit->set_ctor( NULL );
     563
     564                //      Expression * arg =
     565                //      ctorInit->set_init( new SingleInit( arg ) );
     566                // }
    562567        }
    563568} // namespace ResolvExpr
  • src/SymTab/Autogen.cc

    r242d458 ra465caf  
    6868                copy->get_args().push_back( new VariableExpr( dstParam ) );
    6969                copy->get_args().push_back( new AddressExpr( new VariableExpr( srcParam ) ) );
    70                 copy->get_args().push_back( new SizeofExpr( unionType ) );
     70                copy->get_args().push_back( new SizeofExpr( srcParam->get_type()->clone() ) );
    7171
    7272                *out++ = new ExprStmt( noLabels, copy );
     
    421421                makeUnionFieldsAssignment( srcParam, dstParam, cloneWithParams( refType, unionParams ), back_inserter( assignDecl->get_statements()->get_kids() ) );
    422422                if ( isGeneric ) makeUnionFieldsAssignment( srcParam, returnVal, cloneWithParams( refType, unionParams ), back_inserter( assignDecl->get_statements()->get_kids() ) );
    423 
    424                 if ( ! isGeneric ) assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
     423                else assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
    425424
    426425                // body of assignment and copy ctor is the same
    427426                copyCtorDecl->set_statements( assignDecl->get_statements()->clone() );
     427
     428                // create a constructor which takes the first member type as a parameter.
     429                // for example, for Union A { int x; double y; }; generate
     430                // void ?{}(A *, int)
     431                // This is to mimic C's behaviour which initializes the first member of the union.
     432                std::list<Declaration *> memCtors;
     433                for ( Declaration * member : aggregateDecl->get_members() ) {
     434                        if ( DeclarationWithType * field = dynamic_cast< DeclarationWithType * >( member ) ) {
     435                                ObjectDecl * srcParam = new ObjectDecl( "src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, field->get_type()->clone(), 0 );
     436
     437                                FunctionType * memCtorType = ctorType->clone();
     438                                memCtorType->get_parameters().push_back( srcParam );
     439                                FunctionDecl * ctor = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, memCtorType, new CompoundStmt( noLabels ), true, false );
     440                                ctor->fixUniqueId();
     441
     442                                makeUnionFieldsAssignment( srcParam, dstParam, cloneWithParams( refType, unionParams ), back_inserter( ctor->get_statements()->get_kids() ) );
     443                                memCtors.push_back( ctor );
     444                                // only generate a ctor for the first field
     445                                break;
     446                        }
     447                }
    428448
    429449                declsToAdd.push_back( assignDecl );
     
    431451                declsToAdd.push_back( copyCtorDecl );
    432452                declsToAdd.push_back( dtorDecl );
     453                declsToAdd.splice( declsToAdd.end(), memCtors );
    433454        }
    434455
Note: See TracChangeset for help on using the changeset viewer.