Changeset 5070fe4


Ignore:
Timestamp:
Aug 5, 2016, 10:55:01 AM (5 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, ctor, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
c331406
Parents:
ea5daeb
Message:

Tweak autogen to treat static-layout generic types like normal structs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Autogen.cc

    rea5daeb r5070fe4  
    203203        }
    204204
    205         void makeStructMemberOp( ObjectDecl * dstParam, Expression * src, DeclarationWithType * field, FunctionDecl * func, TypeSubstitution & genericSubs, bool isGeneric, bool forward = true ) {
     205        void makeStructMemberOp( ObjectDecl * dstParam, Expression * src, DeclarationWithType * field, FunctionDecl * func, TypeSubstitution & genericSubs, bool isGeneric, bool isDynamicLayout, bool forward = true ) {
    206206                if ( isGeneric ) {
    207207                        // rewrite member type in terms of the type variables on this operator
     
    226226
    227227                        makeArrayFunction( src, dstselect, array, func->get_name(), back_inserter( func->get_statements()->get_kids() ), forward );
    228                         if ( isGeneric && returnVal ) {
     228                        if ( isDynamicLayout && returnVal ) {
    229229                                UntypedExpr *derefRet = new UntypedExpr( new NameExpr( "*?" ) );
    230230                                derefRet->get_args().push_back( new VariableExpr( returnVal ) );
     
    235235                } else {
    236236                        makeScalarFunction( src, dstParam, field, func->get_name(), back_inserter( func->get_statements()->get_kids() ) );
    237                         if ( isGeneric && returnVal ) makeScalarFunction( src, returnVal, field, func->get_name(), back_inserter( func->get_statements()->get_kids() ) );
     237                        if ( isDynamicLayout && returnVal ) makeScalarFunction( src, returnVal, field, func->get_name(), back_inserter( func->get_statements()->get_kids() ) );
    238238                } // if
    239239        }
    240240
    241241        template<typename Iterator>
    242         void makeStructFunctionBody( Iterator member, Iterator end, FunctionDecl * func, TypeSubstitution & genericSubs, bool isGeneric, bool forward = true ) {
     242        void makeStructFunctionBody( Iterator member, Iterator end, FunctionDecl * func, TypeSubstitution & genericSubs, bool isGeneric, bool isDynamicLayout, bool forward = true ) {
    243243                for ( ; member != end; ++member ) {
    244244                        if ( DeclarationWithType *field = dynamic_cast< DeclarationWithType * >( *member ) ) { // otherwise some form of type declaration, e.g. Aggregate
     
    276276
    277277                                Expression *srcselect = srcParam ? new MemberExpr( field, new VariableExpr( srcParam ) ) : NULL;
    278                                 makeStructMemberOp( dstParam, srcselect, field, func, genericSubs, isGeneric, forward );
     278                                makeStructMemberOp( dstParam, srcselect, field, func, genericSubs, isGeneric, isDynamicLayout, forward );
    279279                        } // if
    280280                } // for
     
    284284        /// void ?{}(A *, int) and void?{}(A *, int, int) for a struct A which has two int fields.
    285285        template<typename Iterator>
    286         void makeStructFieldCtorBody( Iterator member, Iterator end, FunctionDecl * func, TypeSubstitution & genericSubs, bool isGeneric ) {
     286        void makeStructFieldCtorBody( Iterator member, Iterator end, FunctionDecl * func, TypeSubstitution & genericSubs, bool isGeneric, bool isDynamicLayout ) {
    287287                FunctionType * ftype = func->get_functionType();
    288288                std::list<DeclarationWithType*> & params = ftype->get_parameters();
     
    310310                                        // matching parameter, initialize field with copy ctor
    311311                                        Expression *srcselect = new VariableExpr(*parameter);
    312                                         makeStructMemberOp( dstParam, srcselect, field, func, genericSubs, isGeneric );
     312                                        makeStructMemberOp( dstParam, srcselect, field, func, genericSubs, isGeneric, isDynamicLayout );
    313313                                        ++parameter;
    314314                                } else {
    315315                                        // no matching parameter, initialize field with default ctor
    316                                         makeStructMemberOp( dstParam, NULL, field, func, genericSubs, isGeneric );
     316                                        makeStructMemberOp( dstParam, NULL, field, func, genericSubs, isGeneric, isDynamicLayout );
    317317                                }
    318318                        }
     
    325325                // Make function polymorphic in same parameters as generic struct, if applicable
    326326                bool isGeneric = false;  // NOTE this flag is an incredibly ugly kludge; we should fix the assignment signature instead (ditto for union)
     327                bool isDynamicLayout = false;  // NOTE see above re: kludge
    327328                std::list< TypeDecl* >& genericParams = aggregateDecl->get_parameters();
    328329                std::list< Expression* > structParams;  // List of matching parameters to put on types
     
    330331                for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) {
    331332                        isGeneric = true;
     333                        if ( (*param)->get_kind() == TypeDecl::Any ) isDynamicLayout = true;
    332334                        TypeDecl *typeParam = cloneAndRename( *param, "_autoassign_" + aggregateDecl->get_name() + "_" + (*param)->get_name() );
    333335                        assignType->get_forall().push_back( typeParam );
     
    389391                        FunctionDecl * ctor = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, memCtorType->clone(), new CompoundStmt( noLabels ), true, false );
    390392                        ctor->fixUniqueId();
    391                         makeStructFieldCtorBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), ctor, genericSubs, isGeneric );
     393                        makeStructFieldCtorBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), ctor, genericSubs, isGeneric, isDynamicLayout );
    392394                        memCtors.push_back( ctor );
    393395                }
     
    395397
    396398                // generate appropriate calls to member ctor, assignment
    397                 makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), assignDecl, genericSubs, isGeneric );
    398                 makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), ctorDecl, genericSubs, isGeneric );
    399                 makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), copyCtorDecl, genericSubs, isGeneric );
     399                makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), assignDecl, genericSubs, isGeneric, isDynamicLayout );
     400                makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), ctorDecl, genericSubs, isGeneric, isDynamicLayout );
     401                makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), copyCtorDecl, genericSubs, isGeneric, isDynamicLayout );
    400402                // needs to do everything in reverse, so pass "forward" as false
    401                 makeStructFunctionBody( aggregateDecl->get_members().rbegin(), aggregateDecl->get_members().rend(), dtorDecl, genericSubs, isGeneric, false );
     403                makeStructFunctionBody( aggregateDecl->get_members().rbegin(), aggregateDecl->get_members().rend(), dtorDecl, genericSubs, isGeneric, isDynamicLayout, false );
    402404
    403405                if ( ! isGeneric ) assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
     
    414416
    415417                // Make function polymorphic in same parameters as generic union, if applicable
    416                 bool isGeneric = false; // NOTE this flag is an incredibly ugly kludge; we should fix the assignment signature instead (ditto for struct)
     418                bool isDynamicLayout = false; // NOTE this flag is an incredibly ugly kludge; we should fix the assignment signature instead (ditto for struct)
    417419                std::list< TypeDecl* >& genericParams = aggregateDecl->get_parameters();
    418420                std::list< Expression* > unionParams;  // List of matching parameters to put on types
    419421                for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) {
    420                         isGeneric = true;
     422                        if ( (*param)->get_kind() == TypeDecl::Any ) isDynamicLayout = true;
    421423                        TypeDecl *typeParam = cloneAndRename( *param, "_autoassign_" + aggregateDecl->get_name() + "_" + (*param)->get_name() );
    422424                        assignType->get_forall().push_back( typeParam );
     
    454456
    455457                makeUnionFieldsAssignment( srcParam, dstParam, cloneWithParams( refType, unionParams ), back_inserter( assignDecl->get_statements()->get_kids() ) );
    456                 if ( isGeneric ) makeUnionFieldsAssignment( srcParam, returnVal, cloneWithParams( refType, unionParams ), back_inserter( assignDecl->get_statements()->get_kids() ) );
    457 
    458                 if ( ! isGeneric ) assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
     458                if ( isDynamicLayout ) makeUnionFieldsAssignment( srcParam, returnVal, cloneWithParams( refType, unionParams ), back_inserter( assignDecl->get_statements()->get_kids() ) );
     459                else assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
    459460
    460461                // body of assignment and copy ctor is the same
Note: See TracChangeset for help on using the changeset viewer.