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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.