Ignore:
Timestamp:
Aug 4, 2016, 4:10:06 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, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
4819cac
Parents:
73bf8cf2
Message:

add gcc attributes to ObjectDecl?, hoist destructed static variables, add breaks to end of generated switch

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/InitTweak.cc

    r73bf8cf2 rf9cebb5  
    55#include "SynTree/Initializer.h"
    66#include "SynTree/Expression.h"
     7#include "SynTree/Attribute.h"
    78#include "GenPoly/GenPoly.h"
    89
     
    125126
    126127        namespace {
     128                /// given index i, dimension d, initializer init, and callExpr f, generates
     129                ///   if (i < d) f(..., init)
     130                ///   ++i;
     131                /// so that only elements within the range of the array are constructed
    127132                template< typename OutIterator >
    128                 void dothething( UntypedExpr * callExpr, Expression * index, Expression * dimension, Initializer * init, OutIterator out ) {
     133                void buildCallExpr( UntypedExpr * callExpr, Expression * index, Expression * dimension, Initializer * init, OutIterator out ) {
    129134                        UntypedExpr * cond = new UntypedExpr( new NameExpr( "?<?") );
    130135                        cond->get_args().push_back( index->clone() );
     
    148153                        Expression * dimension = *idx++;
    149154
     155                        // xxx - may want to eventually issue a warning here if we can detect
     156                        // that the number of elements exceeds to dimension of the array
    150157                        if ( idx == idxEnd ) {
    151158                                if ( ListInit * listInit = dynamic_cast< ListInit * >( init ) ) {
    152159                                        for ( Initializer * init : *listInit ) {
    153                                                 dothething( callExpr->clone(), index, dimension, init, out );
     160                                                buildCallExpr( callExpr->clone(), index, dimension, init, out );
    154161                                        }
    155162                                } else {
    156                                         dothething( callExpr->clone(), index, dimension, init, out );
     163                                        buildCallExpr( callExpr->clone(), index, dimension, init, out );
    157164                                }
    158165                        } else {
     
    166173                                        throw SemanticError( "unbalanced list initializers" );
    167174                                }
     175
     176                                static UniqueName targetLabel( "L__autogen__" );
     177                                Label switchLabel( targetLabel.newName(), 0, std::list< Attribute * >{ new Attribute("unused") } );
    168178                                for ( Initializer * init : *listInit ) {
    169179                                        Expression * condition;
     
    178188                                        std::list< Statement * > stmts;
    179189                                        build( callExpr, idx, idxEnd, init, back_inserter( stmts ) );
     190                                        stmts.push_back( new BranchStmt( noLabels, switchLabel, BranchStmt::Break ) );
    180191                                        CaseStmt * caseStmt = new CaseStmt( noLabels, condition, stmts );
    181192                                        branches.push_back( caseStmt );
    182193                                }
    183194                                *out++ = new SwitchStmt( noLabels, index->clone(), branches );
     195                                *out++ = new NullStmt( std::list<Label>{ switchLabel } );
    184196                        }
    185197                }
     
    194206        Statement * InitImpl::buildListInit( UntypedExpr * dst, std::list< Expression * > & indices ) {
    195207                if ( ! init ) return NULL;
    196                 std::list< Statement * > results;
    197                 build( dst, indices.begin(), indices.end(), init, back_inserter( results ) );
    198                 assert( results.size() <= 1 );
    199                 if ( results.empty() ) {
     208                CompoundStmt * block = new CompoundStmt( noLabels );
     209                build( dst, indices.begin(), indices.end(), init, back_inserter( block->get_kids() ) );
     210                if ( block->get_kids().empty() ) {
     211                        delete block;
    200212                        return NULL;
    201213                } else {
    202214                        init = NULL; // init was consumed in creating the list init
    203                         return results.front();
    204                 }
    205                 return ! results.empty() ? results.front() : NULL;
     215                        return block;
     216                }
    206217        }
    207218
     
    280291        }
    281292
    282         bool isInstrinsicSingleArgCallStmt( Statement * stmt ) {
     293        bool isIntrinsicSingleArgCallStmt( Statement * stmt ) {
    283294                std::list< Expression * > callExprs;
    284295                collectCtorDtorCalls( stmt, callExprs );
Note: See TracChangeset for help on using the changeset viewer.