Changes in / [376c632a:f681823]


Ignore:
Files:
6 deleted
30 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/io.cfa

    r376c632a rf681823  
    306306                ctx->proc->io.pending = true;
    307307                ctx->proc->io.dirty   = true;
    308                 if(sq.to_submit > 30) {
    309                         __tls_stats()->io.flush.full++;
    310                         __cfa_io_flush( ctx->proc, 0 );
    311                 }
    312                 if(!lazy) {
    313                         __tls_stats()->io.flush.eager++;
     308                if(sq.to_submit > 30 || !lazy) {
    314309                        __cfa_io_flush( ctx->proc, 0 );
    315310                }
  • libcfa/src/concurrency/kernel.cfa

    r376c632a rf681823  
    4242
    4343#if !defined(__CFA_NO_STATISTICS__)
    44         #define __STATS_DEF( ...) __VA_ARGS__
     44        #define __STATS( ...) __VA_ARGS__
    4545#else
    46         #define __STATS_DEF( ...)
     46        #define __STATS( ...)
    4747#endif
    4848
     
    122122static thread$ * __next_thread(cluster * this);
    123123static thread$ * __next_thread_slow(cluster * this);
    124 static thread$ * __next_thread_search(cluster * this);
    125124static inline bool __must_unpark( thread$ * thrd ) __attribute((nonnull(1)));
    126125static void __run_thread(processor * this, thread$ * dst);
     
    188187                MAIN_LOOP:
    189188                for() {
     189                        #define OLD_MAIN 1
     190                        #if OLD_MAIN
    190191                        // Check if there is pending io
    191192                        __maybe_io_drain( this );
     
    195196
    196197                        if( !readyThread ) {
    197                                 __IO_STATS__(true, io.flush.idle++; )
    198198                                __cfa_io_flush( this, 0 );
    199199
    200                                 readyThread = __next_thread( this->cltr );
    201                         }
    202 
    203                         if( !readyThread ) for(5) {
    204                                 __IO_STATS__(true, io.flush.idle++; )
    205 
    206200                                readyThread = __next_thread_slow( this->cltr );
    207 
    208                                 if( readyThread ) break;
    209 
    210                                 __cfa_io_flush( this, 0 );
    211201                        }
    212202
     
    220210
    221211                                // Confirm the ready-queue is empty
    222                                 readyThread = __next_thread_search( this->cltr );
     212                                readyThread = __next_thread_slow( this->cltr );
    223213                                if( readyThread ) {
    224214                                        // A thread was found, cancel the halt
    225215                                        mark_awake(this->cltr->procs, * this);
    226216
    227                                         __STATS__(true, ready.sleep.cancels++; )
     217                                        #if !defined(__CFA_NO_STATISTICS__)
     218                                                __tls_stats()->ready.sleep.cancels++;
     219                                        #endif
    228220
    229221                                        // continue the mai loop
     
    252244
    253245                        if(this->io.pending && !this->io.dirty) {
    254                                 __IO_STATS__(true, io.flush.dirty++; )
    255246                                __cfa_io_flush( this, 0 );
    256247                        }
     248
     249                        #else
     250                                #warning new kernel loop
     251                        SEARCH: {
     252                                /* paranoid */ verify( ! __preemption_enabled() );
     253
     254                                // First, lock the scheduler since we are searching for a thread
     255                                ready_schedule_lock();
     256
     257                                // Try to get the next thread
     258                                readyThread = pop_fast( this->cltr );
     259                                if(readyThread) { ready_schedule_unlock(); break SEARCH; }
     260
     261                                // If we can't find a thread, might as well flush any outstanding I/O
     262                                if(this->io.pending) { __cfa_io_flush( this, 0 ); }
     263
     264                                // Spin a little on I/O, just in case
     265                                for(5) {
     266                                        __maybe_io_drain( this );
     267                                        readyThread = pop_fast( this->cltr );
     268                                        if(readyThread) { ready_schedule_unlock(); break SEARCH; }
     269                                }
     270
     271                                // no luck, try stealing a few times
     272                                for(5) {
     273                                        if( __maybe_io_drain( this ) ) {
     274                                                readyThread = pop_fast( this->cltr );
     275                                        } else {
     276                                                readyThread = pop_slow( this->cltr );
     277                                        }
     278                                        if(readyThread) { ready_schedule_unlock(); break SEARCH; }
     279                                }
     280
     281                                // still no luck, search for a thread
     282                                readyThread = pop_search( this->cltr );
     283                                if(readyThread) { ready_schedule_unlock(); break SEARCH; }
     284
     285                                // Don't block if we are done
     286                                if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) {
     287                                        ready_schedule_unlock();
     288                                        break MAIN_LOOP;
     289                                }
     290
     291                                __STATS( __tls_stats()->ready.sleep.halts++; )
     292
     293                                // Push self to idle stack
     294                                ready_schedule_unlock();
     295                                if(!mark_idle(this->cltr->procs, * this)) goto SEARCH;
     296                                ready_schedule_lock();
     297
     298                                // Confirm the ready-queue is empty
     299                                __maybe_io_drain( this );
     300                                readyThread = pop_search( this->cltr );
     301                                ready_schedule_unlock();
     302
     303                                if( readyThread ) {
     304                                        // A thread was found, cancel the halt
     305                                        mark_awake(this->cltr->procs, * this);
     306
     307                                        __STATS( __tls_stats()->ready.sleep.cancels++; )
     308
     309                                        // continue the main loop
     310                                        break SEARCH;
     311                                }
     312
     313                                __STATS( if(this->print_halts) __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 0\n", this->unique_id, rdtscl()); )
     314                                __cfadbg_print_safe(runtime_core, "Kernel : core %p waiting on eventfd %d\n", this, this->idle_fd);
     315
     316                                {
     317                                        eventfd_t val;
     318                                        ssize_t ret = read( this->idle_fd, &val, sizeof(val) );
     319                                        if(ret < 0) {
     320                                                switch((int)errno) {
     321                                                case EAGAIN:
     322                                                #if EAGAIN != EWOULDBLOCK
     323                                                        case EWOULDBLOCK:
     324                                                #endif
     325                                                case EINTR:
     326                                                        // No need to do anything special here, just assume it's a legitimate wake-up
     327                                                        break;
     328                                                default:
     329                                                        abort( "KERNEL : internal error, read failure on idle eventfd, error(%d) %s.", (int)errno, strerror( (int)errno ) );
     330                                                }
     331                                        }
     332                                }
     333
     334                                        __STATS( if(this->print_halts) __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 1\n", this->unique_id, rdtscl()); )
     335
     336                                // We were woken up, remove self from idle
     337                                mark_awake(this->cltr->procs, * this);
     338
     339                                // DON'T just proceed, start looking again
     340                                continue MAIN_LOOP;
     341                        }
     342
     343                RUN_THREAD:
     344                        /* paranoid */ verify( ! __preemption_enabled() );
     345                        /* paranoid */ verify( readyThread );
     346
     347                        // Reset io dirty bit
     348                        this->io.dirty = false;
     349
     350                        // We found a thread run it
     351                        __run_thread(this, readyThread);
     352
     353                        // Are we done?
     354                        if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP;
     355
     356                        if(this->io.pending && !this->io.dirty) {
     357                                __cfa_io_flush( this, 0 );
     358                        }
     359
     360                        ready_schedule_lock();
     361                        __maybe_io_drain( this );
     362                        ready_schedule_unlock();
     363                        #endif
    257364                }
    258365
     
    365472                                break RUNNING;
    366473                        case TICKET_UNBLOCK:
    367                                 __STATS__(true, ready.threads.threads++; )
     474                                #if !defined(__CFA_NO_STATISTICS__)
     475                                        __tls_stats()->ready.threads.threads++;
     476                                #endif
    368477                                // This is case 2, the racy case, someone tried to run this thread before it finished blocking
    369478                                // In this case, just run it again.
     
    380489        __cfadbg_print_safe(runtime_core, "Kernel : core %p finished running thread %p\n", this, thrd_dst);
    381490
    382         __STATS__(true, ready.threads.threads--; )
     491        #if !defined(__CFA_NO_STATISTICS__)
     492                __tls_stats()->ready.threads.threads--;
     493        #endif
    383494
    384495        /* paranoid */ verify( ! __preemption_enabled() );
     
    391502        thread$ * thrd_src = kernelTLS().this_thread;
    392503
    393         __STATS_DEF( thrd_src->last_proc = kernelTLS().this_processor; )
     504        __STATS( thrd_src->last_proc = kernelTLS().this_processor; )
    394505
    395506        // Run the thread on this processor
     
    443554        // Dereference the thread now because once we push it, there is not guaranteed it's still valid.
    444555        struct cluster * cl = thrd->curr_cluster;
    445         __STATS_DEF(bool outside = hint == UNPARK_LOCAL && thrd->last_proc && thrd->last_proc != kernelTLS().this_processor; )
     556        __STATS(bool outside = hint == UNPARK_LOCAL && thrd->last_proc && thrd->last_proc != kernelTLS().this_processor; )
    446557
    447558        // push the thread to the cluster ready-queue
     
    494605
    495606        ready_schedule_lock();
    496                 thread$ * thrd = pop_slow( this );
    497         ready_schedule_unlock();
    498 
    499         /* paranoid */ verify( ! __preemption_enabled() );
    500         return thrd;
    501 }
    502 
    503 // KERNEL ONLY
    504 static inline thread$ * __next_thread_search(cluster * this) with( *this ) {
    505         /* paranoid */ verify( ! __preemption_enabled() );
    506 
    507         ready_schedule_lock();
    508                 thread$ * thrd = pop_search( this );
     607                thread$ * thrd;
     608                for(25) {
     609                        thrd = pop_slow( this );
     610                        if(thrd) goto RET;
     611                }
     612                thrd = pop_search( this );
     613
     614                RET:
    509615        ready_schedule_unlock();
    510616
     
    748854
    749855static bool mark_idle(__cluster_proc_list & this, processor & proc) {
    750         __STATS__(true, ready.sleep.halts++; )
     856        #if !defined(__CFA_NO_STATISTICS__)
     857                __tls_stats()->ready.sleep.halts++;
     858        #endif
    751859
    752860        proc.idle_wctx.fd = 0;
     
    841949                unsigned tail = *ctx->cq.tail;
    842950                if(head == tail) return false;
    843                 ready_schedule_lock();
    844                 ret = __cfa_io_drain( proc );
    845                 ready_schedule_unlock();
     951                #if OLD_MAIN
     952                        ready_schedule_lock();
     953                        ret = __cfa_io_drain( proc );
     954                        ready_schedule_unlock();
     955                #else
     956                        ret = __cfa_io_drain( proc );
     957                #endif
    846958        #endif
    847959        return ret;
  • libcfa/src/concurrency/kernel/fwd.hfa

    r376c632a rf681823  
    396396                                if( !(in_kernel) ) enable_interrupts(); \
    397397                        }
    398                         #if defined(CFA_HAVE_LINUX_IO_URING_H)
    399                                 #define __IO_STATS__(in_kernel, ...) { \
    400                                         if( !(in_kernel) ) disable_interrupts(); \
    401                                         with( *__tls_stats() ) { \
    402                                                 __VA_ARGS__ \
    403                                         } \
    404                                         if( !(in_kernel) ) enable_interrupts(); \
    405                                 }
    406                         #else
    407                                 #define __IO_STATS__(in_kernel, ...)
    408                         #endif
    409398                #else
    410399                        #define __STATS__(in_kernel, ...)
    411                         #define __IO_STATS__(in_kernel, ...)
    412400                #endif
    413401        }
  • libcfa/src/concurrency/stats.cfa

    r376c632a rf681823  
    4545                        stats->io.submit.slow       = 0;
    4646                        stats->io.flush.external    = 0;
    47                         stats->io.flush.dirty       = 0;
    48                         stats->io.flush.full        = 0;
    49                         stats->io.flush.idle        = 0;
    50                         stats->io.flush.eager       = 0;
    5147                        stats->io.calls.flush       = 0;
    5248                        stats->io.calls.submitted   = 0;
     
    111107                        tally_one( &cltr->io.submit.slow      , &proc->io.submit.slow       );
    112108                        tally_one( &cltr->io.flush.external   , &proc->io.flush.external    );
    113                         tally_one( &cltr->io.flush.dirty      , &proc->io.flush.dirty       );
    114                         tally_one( &cltr->io.flush.full       , &proc->io.flush.full        );
    115                         tally_one( &cltr->io.flush.idle       , &proc->io.flush.idle        );
    116                         tally_one( &cltr->io.flush.eager      , &proc->io.flush.eager       );
    117109                        tally_one( &cltr->io.calls.flush      , &proc->io.calls.flush       );
    118110                        tally_one( &cltr->io.calls.submitted  , &proc->io.calls.submitted   );
     
    192184                                if(io.alloc.fail || io.alloc.revoke || io.alloc.block)
    193185                                        sstr | "-     failures      : " | eng3(io.alloc.fail) | "oom, " | eng3(io.alloc.revoke) | "rvk, " | eng3(io.alloc.block) | "blk";
    194                                 // if(io.flush.external)
    195                                 //      sstr | "- flush external    : " | eng3(io.flush.external);
     186                                if(io.flush.external)
     187                                        sstr | "- flush external    : " | eng3(io.flush.external);
    196188
    197189                                double avgsubs = ((double)io.calls.submitted) / io.calls.flush;
    198190                                double avgcomp = ((double)io.calls.completed) / io.calls.drain;
    199191                                sstr | "- syscll : "
    200                                      |   " sub " | eng3(io.calls.submitted) | "/" | eng3(io.calls.flush) | "(" | ws(3, 3, avgsubs) | "/flush)"
    201                                      | " - cmp " | eng3(io.calls.completed) | "/" | eng3(io.calls.drain) | "(" | ws(3, 3, avgcomp) | "/drain)"
     192                                     |   " sub " | eng3(io.calls.flush) | "/" | eng3(io.calls.submitted) | "(" | ws(3, 3, avgsubs) | "/flush)"
     193                                     | " - cmp " | eng3(io.calls.drain) | "/" | eng3(io.calls.completed) | "(" | ws(3, 3, avgcomp) | "/drain)"
    202194                                     | " - " | eng3(io.calls.errors.busy) | " EBUSY";
    203                                 sstr | " - sub: " | eng3(io.flush.full) | "full, " | eng3(io.flush.dirty) | "drty, " | eng3(io.flush.idle) | "idle, " | eng3(io.flush.eager) | "eagr, " | eng3(io.flush.external) | "ext";
    204195                                sstr | "- ops blk: "
    205196                                     |   " sk rd: " | eng3(io.ops.sockread)  | "epll: " | eng3(io.ops.epllread)
  • libcfa/src/concurrency/stats.hfa

    r376c632a rf681823  
    9191                        struct {
    9292                                volatile uint64_t external;
    93                                 volatile uint64_t dirty;
    94                                 volatile uint64_t full;
    95                                 volatile uint64_t idle;
    96                                 volatile uint64_t eager;
    9793                        } flush;
    9894                        struct {
  • src/AST/Copy.hpp

    r376c632a rf681823  
    1010// Created On       : Wed Jul 10 16:13:00 2019
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Wed Dec 15 11:07:00 2021
    13 // Update Count     : 3
     12// Last Modified On : Thr Nov 11  9:22:00 2021
     13// Update Count     : 2
    1414//
    1515
     
    5252Node * deepCopy<Node>( const Node * localRoot );
    5353
    54 template<typename node_t, enum Node::ref_type ref_t>
    55 node_t * shallowCopy( const ptr_base<node_t, ref_t> & localRoot ) {
    56         return shallowCopy( localRoot.get() );
    57 }
    58 
    59 template<typename node_t, enum Node::ref_type ref_t>
    60 node_t * deepCopy( const ptr_base<node_t, ref_t> & localRoot ) {
    61         return deepCopy( localRoot.get() );
    62 }
    63 
    6454}
    6555
  • src/AST/Node.hpp

    r376c632a rf681823  
    188188        }
    189189
    190         ptr_base & operator=( const node_t * node ) {
    191                 assign( node );
    192                 return *this;
    193         }
    194 
    195190        template<typename o_node_t>
    196191        ptr_base & operator=( const o_node_t * node ) {
  • src/AST/Pass.impl.hpp

    r376c632a rf681823  
    3333        /* call the implementation of the previsit of this pass */ \
    3434        __pass::previsit( core, node, 0 );
     35
     36#define VISIT( code... ) \
     37        /* if this node should visit its children */ \
     38        if ( __visit_children() ) { \
     39                /* visit the children */ \
     40                code \
     41        }
    3542
    3643#define VISIT_END( type, node ) \
     
    445452        VISIT_START( node );
    446453
    447         if ( __visit_children() ) {
     454        VISIT(
    448455                {
    449456                        guard_symtab guard { *this };
     
    453460                maybe_accept( node, &ObjectDecl::bitfieldWidth );
    454461                maybe_accept( node, &ObjectDecl::attributes    );
    455         }
     462        )
    456463
    457464        __pass::symtab::addId( core, 0, node );
     
    468475        __pass::symtab::addId( core, 0, node );
    469476
    470         if ( __visit_children() ) {
    471                 maybe_accept( node, &FunctionDecl::withExprs );
    472         }
     477        VISIT(maybe_accept( node, &FunctionDecl::withExprs );)
    473478        {
    474479                // with clause introduces a level of scope (for the with expression members).
     
    488493                        } };
    489494                        __pass::symtab::addId( core, 0, func );
    490                         if ( __visit_children() ) {
     495                        VISIT(
    491496                                // parameter declarations
    492497                                maybe_accept( node, &FunctionDecl::params );
     
    504509                                maybe_accept( node, &FunctionDecl::stmts );
    505510                                maybe_accept( node, &FunctionDecl::attributes );
    506                         }
     511                        )
    507512                }
    508513        }
     
    521526        __pass::symtab::addStructFwd( core, 0, node );
    522527
    523         if ( __visit_children() ) {
     528        VISIT({
    524529                guard_symtab guard { * this };
    525530                maybe_accept( node, &StructDecl::params     );
    526531                maybe_accept( node, &StructDecl::members    );
    527532                maybe_accept( node, &StructDecl::attributes );
    528         }
     533        })
    529534
    530535        // this addition replaces the forward declaration
     
    543548        __pass::symtab::addUnionFwd( core, 0, node );
    544549
    545         if ( __visit_children() ) {
     550        VISIT({
    546551                guard_symtab guard { * this };
    547552                maybe_accept( node, &UnionDecl::params     );
    548553                maybe_accept( node, &UnionDecl::members    );
    549554                maybe_accept( node, &UnionDecl::attributes );
    550         }
     555        })
    551556
    552557        __pass::symtab::addUnion( core, 0, node );
     
    563568        __pass::symtab::addEnum( core, 0, node );
    564569
    565         if ( __visit_children() ) {
     570        VISIT(
    566571                // unlike structs, traits, and unions, enums inject their members into the global scope
    567572                maybe_accept( node, &EnumDecl::params     );
    568573                maybe_accept( node, &EnumDecl::members    );
    569574                maybe_accept( node, &EnumDecl::attributes );
    570         }
     575        )
    571576
    572577        VISIT_END( Decl, node );
     
    579584        VISIT_START( node );
    580585
    581         if ( __visit_children() ) {
     586        VISIT({
    582587                guard_symtab guard { *this };
    583588                maybe_accept( node, &TraitDecl::params     );
    584589                maybe_accept( node, &TraitDecl::members    );
    585590                maybe_accept( node, &TraitDecl::attributes );
    586         }
     591        })
    587592
    588593        __pass::symtab::addTrait( core, 0, node );
     
    597602        VISIT_START( node );
    598603
    599         if ( __visit_children() ) {
     604        VISIT({
    600605                guard_symtab guard { *this };
    601606                maybe_accept( node, &TypeDecl::base   );
    602         }
     607        })
    603608
    604609        // see A NOTE ON THE ORDER OF TRAVERSAL, above
     
    607612        __pass::symtab::addType( core, 0, node );
    608613
    609         if ( __visit_children() ) {
     614        VISIT(
    610615                maybe_accept( node, &TypeDecl::assertions );
    611616
     
    614619                        maybe_accept( node, &TypeDecl::init );
    615620                }
    616         }
     621        )
    617622
    618623        VISIT_END( Decl, node );
     
    625630        VISIT_START( node );
    626631
    627         if ( __visit_children() ) {
     632        VISIT({
    628633                guard_symtab guard { *this };
    629634                maybe_accept( node, &TypedefDecl::base   );
    630         }
     635        })
    631636
    632637        __pass::symtab::addType( core, 0, node );
    633638
    634         if ( __visit_children() ) {
    635                 maybe_accept( node, &TypedefDecl::assertions );
    636         }
     639        VISIT( maybe_accept( node, &TypedefDecl::assertions ); )
    637640
    638641        VISIT_END( Decl, node );
     
    645648        VISIT_START( node );
    646649
    647         if ( __visit_children() ) {
     650        VISIT(
    648651                maybe_accept( node, &AsmDecl::stmt );
    649         }
     652        )
    650653
    651654        VISIT_END( AsmDecl, node );
     
    658661        VISIT_START( node );
    659662
    660         if ( __visit_children() ) {
     663        VISIT(
    661664                maybe_accept( node, &DirectiveDecl::stmt );
    662         }
     665        )
    663666
    664667        VISIT_END( DirectiveDecl, node );
     
    671674        VISIT_START( node );
    672675
    673         if ( __visit_children() ) {
     676        VISIT(
    674677                maybe_accept( node, &StaticAssertDecl::cond );
    675678                maybe_accept( node, &StaticAssertDecl::msg  );
    676         }
     679        )
    677680
    678681        VISIT_END( StaticAssertDecl, node );
     
    684687const ast::CompoundStmt * ast::Pass< core_t >::visit( const ast::CompoundStmt * node ) {
    685688        VISIT_START( node );
    686 
    687         if ( __visit_children() ) {
     689        VISIT(
    688690                // Do not enter (or leave) a new scope if atFunctionTop. Remember to save the result.
    689691                auto guard1 = makeFuncGuard( [this, enterScope = !this->atFunctionTop]() {
     
    702704                guard_scope guard3 { *this };
    703705                maybe_accept( node, &CompoundStmt::kids );
    704         }
    705 
     706        )
    706707        VISIT_END( CompoundStmt, node );
    707708}
     
    713714        VISIT_START( node );
    714715
    715         if ( __visit_children() ) {
     716        VISIT(
    716717                maybe_accept( node, &ExprStmt::expr );
    717         }
     718        )
    718719
    719720        VISIT_END( Stmt, node );
     
    726727        VISIT_START( node )
    727728
    728         if ( __visit_children() ) {
     729        VISIT(
    729730                maybe_accept( node, &AsmStmt::instruction );
    730731                maybe_accept( node, &AsmStmt::output      );
    731732                maybe_accept( node, &AsmStmt::input       );
    732733                maybe_accept( node, &AsmStmt::clobber     );
    733         }
     734        )
    734735
    735736        VISIT_END( Stmt, node );
     
    751752        VISIT_START( node );
    752753
    753         if ( __visit_children() ) {
     754        VISIT({
    754755                // if statements introduce a level of scope (for the initialization)
    755756                guard_symtab guard { *this };
     
    758759                maybe_accept_as_compound( node, &IfStmt::thenPart );
    759760                maybe_accept_as_compound( node, &IfStmt::elsePart );
    760         }
     761        })
    761762
    762763        VISIT_END( Stmt, node );
     
    769770        VISIT_START( node );
    770771
    771         if ( __visit_children() ) {
     772        VISIT({
    772773                // while statements introduce a level of scope (for the initialization)
    773774                guard_symtab guard { *this };
     
    775776                maybe_accept( node, &WhileStmt::cond  );
    776777                maybe_accept_as_compound( node, &WhileStmt::body  );
    777         }
     778        })
    778779
    779780        VISIT_END( Stmt, node );
     
    786787        VISIT_START( node );
    787788
    788         if ( __visit_children() ) {
     789        VISIT({
    789790                // for statements introduce a level of scope (for the initialization)
    790791                guard_symtab guard { *this };
     
    794795                maybe_accept( node, &ForStmt::inc   );
    795796                maybe_accept_as_compound( node, &ForStmt::body  );
    796         }
     797        })
    797798
    798799        VISIT_END( Stmt, node );
     
    805806        VISIT_START( node );
    806807
    807         if ( __visit_children() ) {
     808        VISIT(
    808809                maybe_accept( node, &SwitchStmt::cond  );
    809810                maybe_accept( node, &SwitchStmt::stmts );
    810         }
     811        )
    811812
    812813        VISIT_END( Stmt, node );
     
    819820        VISIT_START( node );
    820821
    821         if ( __visit_children() ) {
     822        VISIT(
    822823                maybe_accept( node, &CaseStmt::cond  );
    823824                maybe_accept( node, &CaseStmt::stmts );
    824         }
     825        )
    825826
    826827        VISIT_END( Stmt, node );
     
    841842        VISIT_START( node );
    842843
    843         if ( __visit_children() ) {
     844        VISIT(
    844845                maybe_accept( node, &ReturnStmt::expr );
    845         }
     846        )
    846847
    847848        VISIT_END( Stmt, node );
     
    854855        VISIT_START( node );
    855856
    856         if ( __visit_children() ) {
     857        VISIT(
    857858                maybe_accept( node, &ThrowStmt::expr   );
    858859                maybe_accept( node, &ThrowStmt::target );
    859         }
     860        )
    860861
    861862        VISIT_END( Stmt, node );
     
    868869        VISIT_START( node );
    869870
    870         if ( __visit_children() ) {
     871        VISIT(
    871872                maybe_accept( node, &TryStmt::body     );
    872873                maybe_accept( node, &TryStmt::handlers );
    873874                maybe_accept( node, &TryStmt::finally  );
    874         }
     875        )
    875876
    876877        VISIT_END( Stmt, node );
     
    883884        VISIT_START( node );
    884885
    885         if ( __visit_children() ) {
     886        VISIT({
    886887                // catch statements introduce a level of scope (for the caught exception)
    887888                guard_symtab guard { *this };
     
    889890                maybe_accept( node, &CatchStmt::cond );
    890891                maybe_accept_as_compound( node, &CatchStmt::body );
    891         }
     892        })
    892893
    893894        VISIT_END( Stmt, node );
     
    900901        VISIT_START( node );
    901902
    902         if ( __visit_children() ) {
     903        VISIT(
    903904                maybe_accept( node, &FinallyStmt::body );
    904         }
     905        )
    905906
    906907        VISIT_END( Stmt, node );
     
    913914        VISIT_START( node );
    914915
    915         if ( __visit_children() ) {
     916        VISIT(
    916917                maybe_accept( node, &SuspendStmt::then   );
    917         }
     918        )
    918919
    919920        VISIT_END( Stmt, node );
     
    933934                // }
    934935
    935         if ( __visit_children() ) {
     936        VISIT({
    936937                std::vector<WaitForStmt::Clause> new_clauses;
    937938                new_clauses.reserve( node->clauses.size() );
     
    964965                        node = n;
    965966                }
    966         }
     967        })
    967968
    968969        #define maybe_accept(field) \
     
    976977                }
    977978
    978         if ( __visit_children() ) {
     979        VISIT(
    979980                maybe_accept( timeout.time );
    980981                maybe_accept( timeout.stmt );
     
    982983                maybe_accept( orElse.stmt  );
    983984                maybe_accept( orElse.cond  );
    984         }
     985        )
    985986
    986987        #undef maybe_accept
     
    995996        VISIT_START( node );
    996997
    997         if ( __visit_children() ) {
     998        VISIT(
    998999                maybe_accept( node, &WithStmt::exprs );
    9991000                {
     
    10031004                        maybe_accept( node, &WithStmt::stmt );
    10041005                }
    1005         }
    1006 
     1006        )
    10071007        VISIT_END( Stmt, node );
    10081008}
     
    10221022        VISIT_START( node );
    10231023
    1024         if ( __visit_children() ) {
     1024        VISIT(
    10251025                maybe_accept( node, &DeclStmt::decl );
    1026         }
     1026        )
    10271027
    10281028        VISIT_END( Stmt, node );
     
    10371037        // For now this isn't visited, it is unclear if this causes problem
    10381038        // if all tests are known to pass, remove this code
    1039         if ( __visit_children() ) {
     1039        VISIT(
    10401040                maybe_accept( node, &ImplicitCtorDtorStmt::callStmt );
    1041         }
     1041        )
    10421042
    10431043        VISIT_END( Stmt, node );
     
    10501050        VISIT_START( node );
    10511051
    1052         if ( __visit_children() ) {
     1052        VISIT({
    10531053                // mutex statements introduce a level of scope (for the initialization)
    10541054                guard_symtab guard { *this };
    10551055                maybe_accept( node, &MutexStmt::stmt );
    10561056                maybe_accept( node, &MutexStmt::mutexObjs );
    1057         }
     1057        })
    10581058
    10591059        VISIT_END( Stmt, node );
     
    10661066        VISIT_START( node );
    10671067
    1068         if ( __visit_children() ) {
     1068        VISIT(
    10691069                {
    10701070                        guard_symtab guard { *this };
     
    10731073                maybe_accept( node, &ApplicationExpr::func );
    10741074                maybe_accept( node, &ApplicationExpr::args );
    1075         }
     1075        )
    10761076
    10771077        VISIT_END( Expr, node );
     
    10841084        VISIT_START( node );
    10851085
    1086         if ( __visit_children() ) {
     1086        VISIT(
    10871087                {
    10881088                        guard_symtab guard { *this };
     
    10911091
    10921092                maybe_accept( node, &UntypedExpr::args );
    1093         }
     1093        )
    10941094
    10951095        VISIT_END( Expr, node );
     
    11021102        VISIT_START( node );
    11031103
    1104         if ( __visit_children() ) {
     1104        VISIT({
    11051105                guard_symtab guard { *this };
    11061106                maybe_accept( node, &NameExpr::result );
    1107         }
     1107        })
    11081108
    11091109        VISIT_END( Expr, node );
     
    11161116        VISIT_START( node );
    11171117
    1118         if ( __visit_children() ) {
    1119                 {
     1118        VISIT({
    11201119                        guard_symtab guard { *this };
    11211120                        maybe_accept( node, &CastExpr::result );
    11221121                }
    11231122                maybe_accept( node, &CastExpr::arg );
    1124         }
     1123        )
    11251124
    11261125        VISIT_END( Expr, node );
     
    11331132        VISIT_START( node );
    11341133
    1135         if ( __visit_children() ) {
    1136                 {
     1134        VISIT({
    11371135                        guard_symtab guard { *this };
    11381136                        maybe_accept( node, &KeywordCastExpr::result );
    11391137                }
    11401138                maybe_accept( node, &KeywordCastExpr::arg );
    1141         }
     1139        )
    11421140
    11431141        VISIT_END( Expr, node );
     
    11501148        VISIT_START( node );
    11511149
    1152         if ( __visit_children() ) {
    1153                 {
     1150        VISIT({
    11541151                        guard_symtab guard { *this };
    11551152                        maybe_accept( node, &VirtualCastExpr::result );
    11561153                }
    11571154                maybe_accept( node, &VirtualCastExpr::arg );
    1158         }
     1155        )
    11591156
    11601157        VISIT_END( Expr, node );
     
    11671164        VISIT_START( node );
    11681165
    1169         if ( __visit_children() ) {
    1170                 {
     1166        VISIT({
    11711167                        guard_symtab guard { *this };
    11721168                        maybe_accept( node, &AddressExpr::result );
    11731169                }
    11741170                maybe_accept( node, &AddressExpr::arg );
    1175         }
     1171        )
    11761172
    11771173        VISIT_END( Expr, node );
     
    11841180        VISIT_START( node );
    11851181
    1186         if ( __visit_children() ) {
     1182        VISIT({
    11871183                guard_symtab guard { *this };
    11881184                maybe_accept( node, &LabelAddressExpr::result );
    1189         }
     1185        })
    11901186
    11911187        VISIT_END( Expr, node );
     
    11981194        VISIT_START( node );
    11991195
    1200         if ( __visit_children() ) {
    1201                 {
     1196        VISIT({
    12021197                        guard_symtab guard { *this };
    12031198                        maybe_accept( node, &UntypedMemberExpr::result );
     
    12051200                maybe_accept( node, &UntypedMemberExpr::aggregate );
    12061201                maybe_accept( node, &UntypedMemberExpr::member    );
    1207         }
     1202        )
    12081203
    12091204        VISIT_END( Expr, node );
     
    12161211        VISIT_START( node );
    12171212
    1218         if ( __visit_children() ) {
    1219                 {
     1213        VISIT({
    12201214                        guard_symtab guard { *this };
    12211215                        maybe_accept( node, &MemberExpr::result );
    12221216                }
    12231217                maybe_accept( node, &MemberExpr::aggregate );
    1224         }
     1218        )
    12251219
    12261220        VISIT_END( Expr, node );
     
    12331227        VISIT_START( node );
    12341228
    1235         if ( __visit_children() ) {
     1229        VISIT({
    12361230                guard_symtab guard { *this };
    12371231                maybe_accept( node, &VariableExpr::result );
    1238         }
     1232        })
    12391233
    12401234        VISIT_END( Expr, node );
     
    12471241        VISIT_START( node );
    12481242
    1249         if ( __visit_children() ) {
     1243        VISIT({
    12501244                guard_symtab guard { *this };
    12511245                maybe_accept( node, &ConstantExpr::result );
    1252         }
     1246        })
    12531247
    12541248        VISIT_END( Expr, node );
     
    12611255        VISIT_START( node );
    12621256
    1263         if ( __visit_children() ) {
    1264                 {
     1257        VISIT({
    12651258                        guard_symtab guard { *this };
    12661259                        maybe_accept( node, &SizeofExpr::result );
     
    12711264                        maybe_accept( node, &SizeofExpr::expr );
    12721265                }
    1273         }
     1266        )
    12741267
    12751268        VISIT_END( Expr, node );
     
    12821275        VISIT_START( node );
    12831276
    1284         if ( __visit_children() ) {
    1285                 {
     1277        VISIT({
    12861278                        guard_symtab guard { *this };
    12871279                        maybe_accept( node, &AlignofExpr::result );
     
    12921284                        maybe_accept( node, &AlignofExpr::expr );
    12931285                }
    1294         }
     1286        )
    12951287
    12961288        VISIT_END( Expr, node );
     
    13031295        VISIT_START( node );
    13041296
    1305         if ( __visit_children() ) {
    1306                 {
     1297        VISIT({
    13071298                        guard_symtab guard { *this };
    13081299                        maybe_accept( node, &UntypedOffsetofExpr::result );
    13091300                }
    13101301                maybe_accept( node, &UntypedOffsetofExpr::type   );
    1311         }
     1302        )
    13121303
    13131304        VISIT_END( Expr, node );
     
    13201311        VISIT_START( node );
    13211312
    1322         if ( __visit_children() ) {
    1323                 {
     1313        VISIT({
    13241314                        guard_symtab guard { *this };
    13251315                        maybe_accept( node, &OffsetofExpr::result );
    13261316                }
    13271317                maybe_accept( node, &OffsetofExpr::type   );
    1328         }
     1318        )
    13291319
    13301320        VISIT_END( Expr, node );
     
    13371327        VISIT_START( node );
    13381328
    1339         if ( __visit_children() ) {
    1340                 {
     1329        VISIT({
    13411330                        guard_symtab guard { *this };
    13421331                        maybe_accept( node, &OffsetPackExpr::result );
    13431332                }
    13441333                maybe_accept( node, &OffsetPackExpr::type   );
    1345         }
     1334        )
    13461335
    13471336        VISIT_END( Expr, node );
     
    13541343        VISIT_START( node );
    13551344
    1356         if ( __visit_children() ) {
    1357                 {
     1345        VISIT({
    13581346                        guard_symtab guard { *this };
    13591347                        maybe_accept( node, &LogicalExpr::result );
     
    13611349                maybe_accept( node, &LogicalExpr::arg1 );
    13621350                maybe_accept( node, &LogicalExpr::arg2 );
    1363         }
     1351        )
    13641352
    13651353        VISIT_END( Expr, node );
     
    13721360        VISIT_START( node );
    13731361
    1374         if ( __visit_children() ) {
    1375                 {
     1362        VISIT({
    13761363                        guard_symtab guard { *this };
    13771364                        maybe_accept( node, &ConditionalExpr::result );
     
    13801367                maybe_accept( node, &ConditionalExpr::arg2 );
    13811368                maybe_accept( node, &ConditionalExpr::arg3 );
    1382         }
     1369        )
    13831370
    13841371        VISIT_END( Expr, node );
     
    13911378        VISIT_START( node );
    13921379
    1393         if ( __visit_children() ) {
    1394                 {
     1380        VISIT({
    13951381                        guard_symtab guard { *this };
    13961382                        maybe_accept( node, &CommaExpr::result );
     
    13981384                maybe_accept( node, &CommaExpr::arg1 );
    13991385                maybe_accept( node, &CommaExpr::arg2 );
    1400         }
     1386        )
    14011387
    14021388        VISIT_END( Expr, node );
     
    14091395        VISIT_START( node );
    14101396
    1411         if ( __visit_children() ) {
    1412                 {
     1397        VISIT({
    14131398                        guard_symtab guard { *this };
    14141399                        maybe_accept( node, &TypeExpr::result );
    14151400                }
    14161401                maybe_accept( node, &TypeExpr::type );
    1417         }
     1402        )
    14181403
    14191404        VISIT_END( Expr, node );
     
    14261411        VISIT_START( node );
    14271412
    1428         if ( __visit_children() ) {
    1429                 {
     1413        VISIT({
    14301414                        guard_symtab guard { *this };
    14311415                        maybe_accept( node, &AsmExpr::result );
     
    14331417                maybe_accept( node, &AsmExpr::constraint );
    14341418                maybe_accept( node, &AsmExpr::operand    );
    1435         }
     1419        )
    14361420
    14371421        VISIT_END( Expr, node );
     
    14441428        VISIT_START( node );
    14451429
    1446         if ( __visit_children() ) {
    1447                 {
     1430        VISIT({
    14481431                        guard_symtab guard { *this };
    14491432                        maybe_accept( node, &ImplicitCopyCtorExpr::result );
    14501433                }
    14511434                maybe_accept( node, &ImplicitCopyCtorExpr::callExpr    );
    1452         }
     1435        )
    14531436
    14541437        VISIT_END( Expr, node );
     
    14611444        VISIT_START( node );
    14621445
    1463         if ( __visit_children() ) {
    1464                 {
     1446        VISIT({
    14651447                        guard_symtab guard { *this };
    14661448                        maybe_accept( node, &ConstructorExpr::result );
    14671449                }
    14681450                maybe_accept( node, &ConstructorExpr::callExpr );
    1469         }
     1451        )
    14701452
    14711453        VISIT_END( Expr, node );
     
    14781460        VISIT_START( node );
    14791461
    1480         if ( __visit_children() ) {
    1481                 {
     1462        VISIT({
    14821463                        guard_symtab guard { *this };
    14831464                        maybe_accept( node, &CompoundLiteralExpr::result );
    14841465                }
    14851466                maybe_accept( node, &CompoundLiteralExpr::init );
    1486         }
     1467        )
    14871468
    14881469        VISIT_END( Expr, node );
     
    14951476        VISIT_START( node );
    14961477
    1497         if ( __visit_children() ) {
    1498                 {
     1478        VISIT({
    14991479                        guard_symtab guard { *this };
    15001480                        maybe_accept( node, &RangeExpr::result );
     
    15021482                maybe_accept( node, &RangeExpr::low    );
    15031483                maybe_accept( node, &RangeExpr::high   );
    1504         }
     1484        )
    15051485
    15061486        VISIT_END( Expr, node );
     
    15131493        VISIT_START( node );
    15141494
    1515         if ( __visit_children() ) {
    1516                 {
     1495        VISIT({
    15171496                        guard_symtab guard { *this };
    15181497                        maybe_accept( node, &UntypedTupleExpr::result );
    15191498                }
    15201499                maybe_accept( node, &UntypedTupleExpr::exprs  );
    1521         }
     1500        )
    15221501
    15231502        VISIT_END( Expr, node );
     
    15301509        VISIT_START( node );
    15311510
    1532         if ( __visit_children() ) {
    1533                 {
     1511        VISIT({
    15341512                        guard_symtab guard { *this };
    15351513                        maybe_accept( node, &TupleExpr::result );
    15361514                }
    15371515                maybe_accept( node, &TupleExpr::exprs  );
    1538         }
     1516        )
    15391517
    15401518        VISIT_END( Expr, node );
     
    15471525        VISIT_START( node );
    15481526
    1549         if ( __visit_children() ) {
    1550                 {
     1527        VISIT({
    15511528                        guard_symtab guard { *this };
    15521529                        maybe_accept( node, &TupleIndexExpr::result );
    15531530                }
    15541531                maybe_accept( node, &TupleIndexExpr::tuple  );
    1555         }
     1532        )
    15561533
    15571534        VISIT_END( Expr, node );
     
    15641541        VISIT_START( node );
    15651542
    1566         if ( __visit_children() ) {
    1567                 {
     1543        VISIT({
    15681544                        guard_symtab guard { *this };
    15691545                        maybe_accept( node, &TupleAssignExpr::result );
    15701546                }
    15711547                maybe_accept( node, &TupleAssignExpr::stmtExpr );
    1572         }
     1548        )
    15731549
    15741550        VISIT_END( Expr, node );
     
    15811557        VISIT_START( node );
    15821558
    1583         if ( __visit_children() ) {
    1584                 // don't want statements from outer CompoundStmts to be added to this StmtExpr
     1559        VISIT(// don't want statements from outer CompoundStmts to be added to this StmtExpr
    15851560                // get the stmts that will need to be spliced in
    15861561                auto stmts_before = __pass::stmtsToAddBefore( core, 0);
     
    15991574                maybe_accept( node, &StmtExpr::returnDecls );
    16001575                maybe_accept( node, &StmtExpr::dtors       );
    1601         }
     1576        )
    16021577
    16031578        VISIT_END( Expr, node );
     
    16101585        VISIT_START( node );
    16111586
    1612         if ( __visit_children() ) {
    1613                 {
     1587        VISIT({
    16141588                        guard_symtab guard { *this };
    16151589                        maybe_accept( node, &UniqueExpr::result );
    16161590                }
    16171591                maybe_accept( node, &UniqueExpr::expr   );
    1618         }
     1592        )
    16191593
    16201594        VISIT_END( Expr, node );
     
    16271601        VISIT_START( node );
    16281602
    1629         if ( __visit_children() ) {
    1630                 {
     1603        VISIT({
    16311604                        guard_symtab guard { *this };
    16321605                        maybe_accept( node, &UntypedInitExpr::result );
     
    16341607                maybe_accept( node, &UntypedInitExpr::expr   );
    16351608                // not currently visiting initAlts, but this doesn't matter since this node is only used in the resolver.
    1636         }
     1609        )
    16371610
    16381611        VISIT_END( Expr, node );
     
    16451618        VISIT_START( node );
    16461619
    1647         if ( __visit_children() ) {
    1648                 {
     1620        VISIT({
    16491621                        guard_symtab guard { *this };
    16501622                        maybe_accept( node, &InitExpr::result );
     
    16521624                maybe_accept( node, &InitExpr::expr   );
    16531625                maybe_accept( node, &InitExpr::designation );
    1654         }
     1626        )
    16551627
    16561628        VISIT_END( Expr, node );
     
    16631635        VISIT_START( node );
    16641636
    1665         if ( __visit_children() ) {
    1666                 {
     1637        VISIT({
    16671638                        guard_symtab guard { *this };
    16681639                        maybe_accept( node, &DeletedExpr::result );
     
    16701641                maybe_accept( node, &DeletedExpr::expr );
    16711642                // don't visit deleteStmt, because it is a pointer to somewhere else in the tree.
    1672         }
     1643        )
    16731644
    16741645        VISIT_END( Expr, node );
     
    16811652        VISIT_START( node );
    16821653
    1683         if ( __visit_children() ) {
    1684                 {
     1654        VISIT({
    16851655                        guard_symtab guard { *this };
    16861656                        maybe_accept( node, &DefaultArgExpr::result );
    16871657                }
    16881658                maybe_accept( node, &DefaultArgExpr::expr );
    1689         }
     1659        )
    16901660
    16911661        VISIT_END( Expr, node );
     
    16981668        VISIT_START( node );
    16991669
    1700         if ( __visit_children() ) {
    1701                 {
     1670        VISIT({
    17021671                        guard_symtab guard { *this };
    17031672                        maybe_accept( node, &GenericExpr::result );
     
    17281697                        node = n;
    17291698                }
    1730         }
     1699        )
    17311700
    17321701        VISIT_END( Expr, node );
     
    17571726        VISIT_START( node );
    17581727
    1759         if ( __visit_children() ) {
     1728        VISIT(
    17601729                // xxx - should PointerType visit/mutate dimension?
    17611730                maybe_accept( node, &PointerType::base );
    1762         }
     1731        )
    17631732
    17641733        VISIT_END( Type, node );
     
    17711740        VISIT_START( node );
    17721741
    1773         if ( __visit_children() ) {
     1742        VISIT(
    17741743                maybe_accept( node, &ArrayType::dimension );
    17751744                maybe_accept( node, &ArrayType::base );
    1776         }
     1745        )
    17771746
    17781747        VISIT_END( Type, node );
     
    17851754        VISIT_START( node );
    17861755
    1787         if ( __visit_children() ) {
     1756        VISIT(
    17881757                maybe_accept( node, &ReferenceType::base );
    1789         }
     1758        )
    17901759
    17911760        VISIT_END( Type, node );
     
    17981767        VISIT_START( node );
    17991768
    1800         if ( __visit_children() ) {
     1769        VISIT(
    18011770                maybe_accept( node, &QualifiedType::parent );
    18021771                maybe_accept( node, &QualifiedType::child );
    1803         }
     1772        )
    18041773
    18051774        VISIT_END( Type, node );
     
    18121781        VISIT_START( node );
    18131782
    1814         if ( __visit_children() ) {
     1783        VISIT({
    18151784                // guard_forall_subs forall_guard { *this, node };
    18161785                // mutate_forall( node );
     
    18181787                maybe_accept( node, &FunctionType::returns );
    18191788                maybe_accept( node, &FunctionType::params  );
    1820         }
     1789        })
    18211790
    18221791        VISIT_END( Type, node );
     
    18311800        __pass::symtab::addStruct( core, 0, node->name );
    18321801
    1833         if ( __visit_children() ) {
     1802        VISIT({
    18341803                guard_symtab guard { *this };
    18351804                maybe_accept( node, &StructInstType::params );
    1836         }
     1805        })
    18371806
    18381807        VISIT_END( Type, node );
     
    18471816        __pass::symtab::addUnion( core, 0, node->name );
    18481817
    1849         if ( __visit_children() ) {
     1818        VISIT({
    18501819                guard_symtab guard { *this };
    18511820                maybe_accept( node, &UnionInstType::params );
    1852         }
     1821        })
    18531822
    18541823        VISIT_END( Type, node );
     
    18611830        VISIT_START( node );
    18621831
    1863         if ( __visit_children() ) {
     1832        VISIT({
    18641833                maybe_accept( node, &EnumInstType::params );
    1865         }
     1834        })
    18661835
    18671836        VISIT_END( Type, node );
     
    18741843        VISIT_START( node );
    18751844
    1876         if ( __visit_children() ) {
     1845        VISIT({
    18771846                maybe_accept( node, &TraitInstType::params );
    1878         }
     1847        })
    18791848
    18801849        VISIT_END( Type, node );
     
    18871856        VISIT_START( node );
    18881857
    1889         if ( __visit_children() ) {
     1858        VISIT(
    18901859                {
    18911860                        maybe_accept( node, &TypeInstType::params );
     
    18931862                // ensure that base re-bound if doing substitution
    18941863                __pass::forall::replace( core, 0, node );
    1895         }
     1864        )
    18961865
    18971866        VISIT_END( Type, node );
     
    19041873        VISIT_START( node );
    19051874
    1906         if ( __visit_children() ) {
     1875        VISIT(
    19071876                maybe_accept( node, &TupleType::types );
    19081877                maybe_accept( node, &TupleType::members );
    1909         }
     1878        )
    19101879
    19111880        VISIT_END( Type, node );
     
    19181887        VISIT_START( node );
    19191888
    1920         if ( __visit_children() ) {
     1889        VISIT(
    19211890                maybe_accept( node, &TypeofType::expr );
    1922         }
     1891        )
    19231892
    19241893        VISIT_END( Type, node );
     
    19311900        VISIT_START( node );
    19321901
    1933         if ( __visit_children() ) {
     1902        VISIT(
    19341903                maybe_accept( node, &VTableType::base );
    1935         }
     1904        )
    19361905
    19371906        VISIT_END( Type, node );
     
    19811950        VISIT_START( node );
    19821951
    1983         if ( __visit_children() ) {
    1984                 maybe_accept( node, &Designation::designators );
    1985         }
     1952        VISIT( maybe_accept( node, &Designation::designators ); )
    19861953
    19871954        VISIT_END( Designation, node );
     
    19941961        VISIT_START( node );
    19951962
    1996         if ( __visit_children() ) {
     1963        VISIT(
    19971964                maybe_accept( node, &SingleInit::value );
    1998         }
     1965        )
    19991966
    20001967        VISIT_END( Init, node );
     
    20071974        VISIT_START( node );
    20081975
    2009         if ( __visit_children() ) {
     1976        VISIT(
    20101977                maybe_accept( node, &ListInit::designations );
    20111978                maybe_accept( node, &ListInit::initializers );
    2012         }
     1979        )
    20131980
    20141981        VISIT_END( Init, node );
     
    20211988        VISIT_START( node );
    20221989
    2023         if ( __visit_children() ) {
     1990        VISIT(
    20241991                maybe_accept( node, &ConstructorInit::ctor );
    20251992                maybe_accept( node, &ConstructorInit::dtor );
    20261993                maybe_accept( node, &ConstructorInit::init );
    2027         }
     1994        )
    20281995
    20291996        VISIT_END( Init, node );
     
    20362003        VISIT_START( node );
    20372004
    2038         if ( __visit_children() ) {
     2005        VISIT(
    20392006                maybe_accept( node, &Attribute::params );
    2040         }
     2007        )
    20412008
    20422009        VISIT_END( Attribute, node );
     
    20492016        VISIT_START( node );
    20502017
    2051         if ( __visit_children() ) {
     2018        VISIT(
    20522019                {
    20532020                        bool mutated = false;
     
    20652032                        }
    20662033                }
    2067         }
     2034        )
    20682035
    20692036        VISIT_END( TypeSubstitution, node );
     
    20712038
    20722039#undef VISIT_START
     2040#undef VISIT
    20732041#undef VISIT_END
  • src/AST/Stmt.hpp

    r376c632a rf681823  
    99// Author           : Aaron B. Moss
    1010// Created On       : Wed May  8 13:00:00 2019
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jan 31 22:38:53 2022
    13 // Update Count     : 12
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Fri May 17 12:45:00 2019
     13// Update Count     : 5
    1414//
    1515
     
    1717
    1818#include <list>
    19 #include <utility>                                                                              // for move
     19#include <utility>                // for move
    2020#include <vector>
    2121
    2222#include "Label.hpp"
    23 #include "Node.hpp"                                                                             // for node, ptr
     23#include "Node.hpp"               // for node, ptr
    2424#include "ParseNode.hpp"
    2525#include "Visitor.hpp"
     
    2727
    2828// Must be included in *all* AST classes; should be #undef'd at the end of the file
    29 #define MUTATE_FRIEND                                                                                                   \
     29#define MUTATE_FRIEND \
    3030    template<typename node_t> friend node_t * mutate(const node_t * node); \
    3131        template<typename node_t> friend node_t * shallowCopy(const node_t * node);
    3232
    3333namespace ast {
     34
    3435class Expr;
    3536
    36 // Base statement node
     37/// Base statement node
    3738class Stmt : public ParseNode {
    38   public:
     39public:
    3940        std::vector<Label> labels;
    4041
    4142        Stmt( const CodeLocation & loc, std::vector<Label> && labels = {} )
    42                 : ParseNode(loc), labels(std::move(labels)) {}
     43        : ParseNode(loc), labels(std::move(labels)) {}
    4344
    4445        Stmt(const Stmt& o) : ParseNode(o), labels(o.labels) {}
    4546
    4647        const Stmt * accept( Visitor & v ) const override = 0;
    47   private:
     48private:
    4849        Stmt * clone() const override = 0;
    4950        MUTATE_FRIEND
    5051};
    5152
    52 // Compound statement: { ... }
     53/// Compound statement `{ ... }`
    5354class CompoundStmt final : public Stmt {
    54   public:
     55public:
    5556        std::list<ptr<Stmt>> kids;
    5657
    5758        CompoundStmt(const CodeLocation & loc, std::list<ptr<Stmt>> && ks = {},
    58                                  std::vector<Label>&& labels = {} )
    59                 : Stmt(loc, std::move(labels)), kids(std::move(ks)) {}
     59                std::vector<Label>&& labels = {} )
     60        : Stmt(loc, std::move(labels)), kids(std::move(ks)) {}
    6061
    6162        CompoundStmt( const CompoundStmt& o );
     
    6667
    6768        const CompoundStmt * accept( Visitor & v ) const override { return v.visit( this ); }
    68   private:
     69private:
    6970        CompoundStmt * clone() const override { return new CompoundStmt{ *this }; }
    7071        MUTATE_FRIEND
    7172};
    7273
    73 // Empty statment: ;
     74/// Empty statment `;`
    7475class NullStmt final : public Stmt {
    75   public:
     76public:
    7677        NullStmt( const CodeLocation & loc, std::vector<Label> && labels = {} )
    77                 : Stmt(loc, std::move(labels)) {}
     78        : Stmt(loc, std::move(labels)) {}
    7879
    7980        const NullStmt * accept( Visitor & v ) const override { return v.visit( this ); }
    80   private:
     81private:
    8182        NullStmt * clone() const override { return new NullStmt{ *this }; }
    8283        MUTATE_FRIEND
    8384};
    8485
    85 // Expression wrapped by statement
     86/// Expression wrapped by statement
    8687class ExprStmt final : public Stmt {
    87   public:
     88public:
    8889        ptr<Expr> expr;
    8990
    9091        ExprStmt( const CodeLocation& loc, const Expr* e, std::vector<Label>&& labels = {} )
    91                 : Stmt(loc, std::move(labels)), expr(e) {}
    92 
    93         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    94   private:
     92        : Stmt(loc, std::move(labels)), expr(e) {}
     93
     94        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     95private:
    9596        ExprStmt * clone() const override { return new ExprStmt{ *this }; }
    9697        MUTATE_FRIEND
    9798};
    9899
    99 // Assembly statement: asm ... ( "..." : ... )
     100/// Assembly statement `asm ... ( "..." : ... )`
    100101class AsmStmt final : public Stmt {
    101   public:
     102public:
    102103        bool isVolatile;
    103104        ptr<Expr> instruction;
     
    107108
    108109        AsmStmt( const CodeLocation & loc, bool isVolatile, const Expr * instruction,
    109                          std::vector<ptr<Expr>> && output, std::vector<ptr<Expr>> && input,
    110                          std::vector<ptr<ConstantExpr>> && clobber, std::vector<Label> && gotoLabels,
    111                          std::vector<Label> && labels = {})
    112                 : Stmt(loc, std::move(labels)), isVolatile(isVolatile), instruction(instruction),
    113                   output(std::move(output)), input(std::move(input)), clobber(std::move(clobber)),
    114                   gotoLabels(std::move(gotoLabels)) {}
    115 
    116         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    117   private:
     110                std::vector<ptr<Expr>> && output, std::vector<ptr<Expr>> && input,
     111                std::vector<ptr<ConstantExpr>> && clobber, std::vector<Label> && gotoLabels,
     112                std::vector<Label> && labels = {})
     113        : Stmt(loc, std::move(labels)), isVolatile(isVolatile), instruction(instruction),
     114          output(std::move(output)), input(std::move(input)), clobber(std::move(clobber)),
     115          gotoLabels(std::move(gotoLabels)) {}
     116
     117        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     118private:
    118119        AsmStmt * clone() const override { return new AsmStmt{ *this }; }
    119120        MUTATE_FRIEND
    120121};
    121122
    122 // C-preprocessor directive: #...
     123/// C-preprocessor directive `#...`
    123124class DirectiveStmt final : public Stmt {
    124   public:
     125public:
    125126        std::string directive;
    126127
    127128        DirectiveStmt( const CodeLocation & loc, const std::string & directive,
    128                                    std::vector<Label> && labels = {} )
    129                 : Stmt(loc, std::move(labels)), directive(directive) {}
    130 
    131         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    132   private:
     129                std::vector<Label> && labels = {} )
     130        : Stmt(loc, std::move(labels)), directive(directive) {}
     131
     132        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     133private:
    133134        DirectiveStmt * clone() const override { return new DirectiveStmt{ *this }; }
    134135        MUTATE_FRIEND
    135136};
    136137
    137 // If statement: if (...) ... else ...
     138/// If conditional statement `if (...) ... else ...`
    138139class IfStmt final : public Stmt {
    139   public:
     140public:
    140141        ptr<Expr> cond;
    141142        ptr<Stmt> thenPart;
     
    144145
    145146        IfStmt( const CodeLocation & loc, const Expr * cond, const Stmt * thenPart,
    146                         const Stmt * elsePart = nullptr, std::vector<ptr<Stmt>> && inits = {},
    147                         std::vector<Label> && labels = {} )
    148                 : Stmt(loc, std::move(labels)), cond(cond), thenPart(thenPart), elsePart(elsePart),
    149                   inits(std::move(inits)) {}
    150 
    151         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    152   private:
     147                const Stmt * elsePart = nullptr, std::vector<ptr<Stmt>> && inits = {},
     148                std::vector<Label> && labels = {} )
     149        : Stmt(loc, std::move(labels)), cond(cond), thenPart(thenPart), elsePart(elsePart),
     150          inits(std::move(inits)) {}
     151
     152        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     153private:
    153154        IfStmt * clone() const override { return new IfStmt{ *this }; }
    154155        MUTATE_FRIEND
    155156};
    156157
    157 // Switch or choose statement: switch (...) { ... }
     158/// Switch or choose conditional statement `switch (...) { ... }`
    158159class SwitchStmt final : public Stmt {
    159   public:
     160public:
    160161        ptr<Expr> cond;
    161162        std::vector<ptr<Stmt>> stmts;
    162163
    163164        SwitchStmt( const CodeLocation & loc, const Expr * cond, std::vector<ptr<Stmt>> && stmts,
    164                                 std::vector<Label> && labels = {} )
    165                 : Stmt(loc, std::move(labels)), cond(cond), stmts(std::move(stmts)) {}
    166 
    167         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    168   private:
     165                std::vector<Label> && labels = {} )
     166        : Stmt(loc, std::move(labels)), cond(cond), stmts(std::move(stmts)) {}
     167
     168        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     169private:
    169170        SwitchStmt * clone() const override { return new SwitchStmt{ *this }; }
    170171        MUTATE_FRIEND
    171172};
    172173
    173 // Case label: case ...: or default:
     174/// Case label `case ...:` `default:`
    174175class CaseStmt final : public Stmt {
    175   public:
    176         // Null for the default label.
     176public:
     177        /// Null for the default label.
    177178        ptr<Expr> cond;
    178179        std::vector<ptr<Stmt>> stmts;
    179180
    180181        CaseStmt( const CodeLocation & loc, const Expr * cond, std::vector<ptr<Stmt>> && stmts,
    181                           std::vector<Label> && labels = {} )
    182                 : Stmt(loc, std::move(labels)), cond(cond), stmts(std::move(stmts)) {}
     182                std::vector<Label> && labels = {} )
     183        : Stmt(loc, std::move(labels)), cond(cond), stmts(std::move(stmts)) {}
    183184
    184185        bool isDefault() const { return !cond; }
    185186
    186187        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    187   private:
     188private:
    188189        CaseStmt * clone() const override { return new CaseStmt{ *this }; }
    189190        MUTATE_FRIEND
    190191};
    191192
    192 // While loop: while (...) ... else ... or do ... while (...) else ...;
     193/// While loop `while (...) ...` `do ... while (...);
    193194class WhileStmt final : public Stmt {
    194   public:
     195public:
    195196        ptr<Expr> cond;
    196197        ptr<Stmt> body;
    197         ptr<Stmt> elsePart;
    198198        std::vector<ptr<Stmt>> inits;
    199199        bool isDoWhile;
    200200
    201201        WhileStmt( const CodeLocation & loc, const Expr * cond, const Stmt * body,
    202                            std::vector<ptr<Stmt>> && inits, bool isDoWhile = false, std::vector<Label> && labels = {} )
    203                 : Stmt(loc, std::move(labels)), cond(cond), body(body), inits(std::move(inits)), isDoWhile(isDoWhile) {}
    204 
    205         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    206   private:
     202                std::vector<ptr<Stmt>> && inits, bool isDoWhile = false, std::vector<Label> && labels = {} )
     203        : Stmt(loc, std::move(labels)), cond(cond), body(body), inits(std::move(inits)),
     204          isDoWhile(isDoWhile) {}
     205
     206        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     207private:
    207208        WhileStmt * clone() const override { return new WhileStmt{ *this }; }
    208209        MUTATE_FRIEND
    209210};
    210211
    211 // For loop: for (... ; ... ; ...) ... else ...
     212/// For loop `for (... ; ... ; ...) ...`
    212213class ForStmt final : public Stmt {
    213   public:
     214public:
    214215        std::vector<ptr<Stmt>> inits;
    215216        ptr<Expr> cond;
    216217        ptr<Expr> inc;
    217218        ptr<Stmt> body;
    218         ptr<Stmt> elsePart;
    219219
    220220        ForStmt( const CodeLocation & loc, std::vector<ptr<Stmt>> && inits, const Expr * cond,
    221                          const Expr * inc, const Stmt * body, std::vector<Label> && labels = {} )
    222                 : Stmt(loc, std::move(labels)), inits(std::move(inits)), cond(cond), inc(inc), body(body) {}
    223 
    224         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    225   private:
     221                const Expr * inc, const Stmt * body, std::vector<Label> && labels = {} )
     222        : Stmt(loc, std::move(labels)), inits(std::move(inits)), cond(cond), inc(inc),
     223          body(body) {}
     224
     225        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     226private:
    226227        ForStmt * clone() const override { return new ForStmt{ *this }; }
    227228        MUTATE_FRIEND
    228229};
    229230
    230 // Branch control flow statement: goto ... or break or continue or fallthru
     231/// Branch control flow statement `goto ...` `break` `continue` `fallthru`
    231232class BranchStmt final : public Stmt {
    232   public:
     233public:
    233234        enum Kind { Goto, Break, Continue, FallThrough, FallThroughDefault };
    234235        static constexpr size_t kindEnd = 1 + (size_t)FallThroughDefault;
     
    240241
    241242        BranchStmt( const CodeLocation & loc, Kind kind, Label target,
    242                                 std::vector<Label> && labels = {} );
     243                std::vector<Label> && labels = {} );
    243244        BranchStmt( const CodeLocation & loc, const Expr * computedTarget,
    244                                 std::vector<Label> && labels = {} )
    245                 : Stmt(loc, std::move(labels)), originalTarget(loc), target(loc),
    246                   computedTarget(computedTarget), kind(Goto) {}
     245                std::vector<Label> && labels = {} )
     246        : Stmt(loc, std::move(labels)), originalTarget(loc), target(loc),
     247          computedTarget(computedTarget), kind(Goto) {}
    247248
    248249        const char * kindName() const { return kindNames[kind]; }
    249250
    250251        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    251   private:
     252private:
    252253        BranchStmt * clone() const override { return new BranchStmt{ *this }; }
    253254        MUTATE_FRIEND
     
    256257};
    257258
    258 // Return statement: return ...
     259/// Return statement `return ...`
    259260class ReturnStmt final : public Stmt {
    260   public:
     261public:
    261262        ptr<Expr> expr;
    262263
    263264        ReturnStmt( const CodeLocation & loc, const Expr * expr, std::vector<Label> && labels = {} )
    264                 : Stmt(loc, std::move(labels)), expr(expr) {}
    265 
    266         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    267   private:
     265        : Stmt(loc, std::move(labels)), expr(expr) {}
     266
     267        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     268private:
    268269        ReturnStmt * clone() const override { return new ReturnStmt{ *this }; }
    269270        MUTATE_FRIEND
    270271};
    271272
    272 // Kind of exception
     273/// Kind of exception
    273274enum ExceptionKind { Terminate, Resume };
    274275
    275 // Throw statement: throw ...
     276/// Throw statement `throw ...`
    276277class ThrowStmt final : public Stmt {
    277   public:
     278public:
    278279        ptr<Expr> expr;
    279280        ptr<Expr> target;
     
    283284                const CodeLocation & loc, ExceptionKind kind, const Expr * expr, const Expr * target,
    284285                std::vector<Label> && labels = {} )
    285                 : Stmt(loc, std::move(labels)), expr(expr), target(target), kind(kind) {}
    286 
    287         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    288   private:
     286        : Stmt(loc, std::move(labels)), expr(expr), target(target), kind(kind) {}
     287
     288        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     289private:
    289290        ThrowStmt * clone() const override { return new ThrowStmt{ *this }; }
    290291        MUTATE_FRIEND
    291292};
    292293
    293 // Try statement: try { ... } ...
     294/// Try statement `try { ... } ...`
    294295class TryStmt final : public Stmt {
    295   public:
     296public:
    296297        ptr<CompoundStmt> body;
    297298        std::vector<ptr<CatchStmt>> handlers;
     
    302303                std::vector<ptr<CatchStmt>> && handlers, const FinallyStmt * finally,
    303304                std::vector<Label> && labels = {} )
    304                 : Stmt(loc, std::move(labels)), body(body), handlers(std::move(handlers)), finally(finally) {}
    305 
    306         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    307   private:
     305        : Stmt(loc, std::move(labels)), body(body), handlers(std::move(handlers)), finally(finally) {}
     306
     307        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     308private:
    308309        TryStmt * clone() const override { return new TryStmt{ *this }; }
    309310        MUTATE_FRIEND
    310311};
    311312
    312 // Catch clause of try statement
     313/// Catch clause of try statement
    313314class CatchStmt final : public Stmt {
    314   public:
     315public:
    315316        ptr<Decl> decl;
    316317        ptr<Expr> cond;
     
    321322                const CodeLocation & loc, ExceptionKind kind, const Decl * decl, const Expr * cond,
    322323                const Stmt * body, std::vector<Label> && labels = {} )
    323                 : Stmt(loc, std::move(labels)), decl(decl), cond(cond), body(body), kind(kind) {}
    324 
    325         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    326   private:
     324        : Stmt(loc, std::move(labels)), decl(decl), cond(cond), body(body), kind(kind) {}
     325
     326        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     327private:
    327328        CatchStmt * clone() const override { return new CatchStmt{ *this }; }
    328329        MUTATE_FRIEND
    329330};
    330331
    331 // Finally clause of try statement
     332/// Finally clause of try statement
    332333class FinallyStmt final : public Stmt {
    333   public:
     334public:
    334335        ptr<CompoundStmt> body;
    335336
    336337        FinallyStmt( const CodeLocation & loc, const CompoundStmt * body,
    337                                  std::vector<Label> && labels = {} )
    338                 : Stmt(loc, std::move(labels)), body(body) {}
    339 
    340         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    341   private:
     338                std::vector<Label> && labels = {} )
     339        : Stmt(loc, std::move(labels)), body(body) {}
     340
     341        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     342private:
    342343        FinallyStmt * clone() const override { return new FinallyStmt{ *this }; }
    343344        MUTATE_FRIEND
    344345};
    345346
    346 // Suspend statement
     347/// Suspend statement
    347348class SuspendStmt final : public Stmt {
    348   public:
     349public:
    349350        ptr<CompoundStmt> then;
    350351        enum Type { None, Coroutine, Generator } type = None;
    351352
    352353        SuspendStmt( const CodeLocation & loc, const CompoundStmt * then, Type type, std::vector<Label> && labels = {} )
    353                 : Stmt(loc, std::move(labels)), then(then), type(type) {}
    354 
    355         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    356   private:
     354        : Stmt(loc, std::move(labels)), then(then), type(type) {}
     355
     356        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     357private:
    357358        SuspendStmt * clone() const override { return new SuspendStmt{ *this }; }
    358359        MUTATE_FRIEND
    359360};
    360361
    361 // Waitfor statement: when (...) waitfor (... , ...) ... timeout(...) ... else ...
     362/// Wait for concurrency statement `when (...) waitfor (... , ...) ... timeout(...) ... else ...`
    362363class WaitForStmt final : public Stmt {
    363   public:
     364public:
    364365        struct Target {
    365366                ptr<Expr> func;
     
    389390
    390391        WaitForStmt( const CodeLocation & loc, std::vector<Label> && labels = {} )
    391                 : Stmt(loc, std::move(labels)) {}
    392 
    393         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    394   private:
     392        : Stmt(loc, std::move(labels)) {}
     393
     394        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     395private:
    395396        WaitForStmt * clone() const override { return new WaitForStmt{ *this }; }
    396397        MUTATE_FRIEND
    397398};
    398399
    399 // Any declaration in a (compound) statement.
     400/// Any declaration in a (compound) statement.
    400401class DeclStmt final : public Stmt {
    401   public:
     402public:
    402403        ptr<Decl> decl;
    403404
    404405        DeclStmt( const CodeLocation & loc, const Decl * decl, std::vector<Label> && labels = {} )
    405                 : Stmt(loc, std::move(labels)), decl(decl) {}
    406 
    407         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    408   private:
     406        : Stmt(loc, std::move(labels)), decl(decl) {}
     407
     408        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     409private:
    409410        DeclStmt * clone() const override { return new DeclStmt{ *this }; }
    410411        MUTATE_FRIEND
    411412};
    412413
    413 // Represents an implicit application of a constructor or destructor.
     414/// Represents an implicit application of a constructor or destructor.
    414415class ImplicitCtorDtorStmt final : public Stmt {
    415   public:
     416public:
    416417        ptr<Stmt> callStmt;
    417418
    418419        ImplicitCtorDtorStmt( const CodeLocation & loc, const Stmt * callStmt,
    419                                                   std::vector<Label> && labels = {} )
    420                 : Stmt(loc, std::move(labels)), callStmt(callStmt) {}
    421 
    422         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    423   private:
     420                std::vector<Label> && labels = {} )
     421        : Stmt(loc, std::move(labels)), callStmt(callStmt) {}
     422
     423        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     424private:
    424425        ImplicitCtorDtorStmt * clone() const override { return new ImplicitCtorDtorStmt{ *this }; }
    425426        MUTATE_FRIEND
    426427};
    427428
    428 // Mutex Statement
     429/// Mutex Statement
    429430class MutexStmt final : public Stmt {
    430   public:
     431public:
    431432        ptr<Stmt> stmt;
    432433        std::vector<ptr<Expr>> mutexObjs;
    433434
    434435        MutexStmt( const CodeLocation & loc, const Stmt * stmt,
    435                            std::vector<ptr<Expr>> && mutexes, std::vector<Label> && labels = {} )
    436                 : Stmt(loc, std::move(labels)), stmt(stmt), mutexObjs(std::move(mutexes)) {}
    437 
    438         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    439   private:
     436                std::vector<ptr<Expr>> && mutexes, std::vector<Label> && labels = {} )
     437        : Stmt(loc, std::move(labels)), stmt(stmt), mutexObjs(std::move(mutexes)) {}
     438
     439        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     440private:
    440441        MutexStmt * clone() const override { return new MutexStmt{ *this }; }
    441442        MUTATE_FRIEND
    442443};
    443 } // namespace ast
     444
     445}
    444446
    445447#undef MUTATE_FRIEND
    446448
    447449// Local Variables: //
     450// tab-width: 4 //
    448451// mode: c++ //
     452// compile-command: "make install" //
    449453// End: //
  • src/ControlStruct/ExceptTranslateNew.cpp

    r376c632a rf681823  
    99// Author           : Andrew Beach
    1010// Created On       : Mon Nov  8 11:53:00 2021
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jan 31 18:49:58 2022
    13 // Update Count     : 1
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Mon Nov  8 16:50:00 2021
     13// Update Count     : 0
    1414//
    1515
     
    2222
    2323namespace ControlStruct {
     24
     25namespace {
    2426
    2527class TranslateThrowsCore : public ast::WithGuards {
     
    126128}
    127129
     130} // namespace
     131
    128132void translateThrows( ast::TranslationUnit & transUnit ) {
    129133        ast::Pass<TranslateThrowsCore>::run( transUnit );
  • src/ControlStruct/FixLabels.cpp

    r376c632a rf681823  
    99// Author           : Andrew Beach
    1010// Created On       : Mon Nov  1 09:39:00 2021
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jan 31 22:19:17 2022
    13 // Update Count     : 9
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Mon Nov  8 10:53:00 2021
     13// Update Count     : 3
    1414//
    1515
     
    2020#include "AST/Stmt.hpp"
    2121#include "ControlStruct/MultiLevelExit.hpp"
    22 using namespace ast;
    2322
    2423namespace ControlStruct {
    25 class FixLabelsCore final : public WithGuards {
     24
     25namespace {
     26
     27class FixLabelsCore final : public ast::WithGuards {
    2628        LabelToStmt labelTable;
    27   public:
     29public:
    2830        FixLabelsCore() : labelTable() {}
    2931
    30         void previsit( const FunctionDecl * );
    31         const FunctionDecl * postvisit( const FunctionDecl * );
    32         void previsit( const Stmt * );
    33         void previsit( const BranchStmt * );
    34         void previsit( const LabelAddressExpr * );
     32        void previsit( const ast::FunctionDecl * );
     33        const ast::FunctionDecl * postvisit( const ast::FunctionDecl * );
     34        void previsit( const ast::Stmt * );
     35        void previsit( const ast::BranchStmt * );
     36        void previsit( const ast::LabelAddressExpr * );
    3537
    36         void setLabelsDef( const std::vector<Label> &, const Stmt * );
    37         void setLabelsUsage( const Label & );
     38        void setLabelsDef( const std::vector<ast::Label> &, const ast::Stmt * );
     39        void setLabelsUsage( const ast::Label & );
    3840};
    3941
    40 void FixLabelsCore::previsit( const FunctionDecl * ) {
     42void FixLabelsCore::previsit( const ast::FunctionDecl * ) {
    4143        GuardValue( labelTable ).clear();
    4244}
    4345
    44 const FunctionDecl * FixLabelsCore::postvisit(
    45         const FunctionDecl * decl ) {
     46const ast::FunctionDecl * FixLabelsCore::postvisit(
     47                const ast::FunctionDecl * decl ) {
    4648        if ( nullptr == decl->stmts ) return decl;
    4749        for ( auto kvp : labelTable ) {
    4850                if ( nullptr == kvp.second ) {
    4951                        SemanticError( kvp.first.location,
    50                                                    "Use of undefined label: " + kvp.first.name );
     52                                "Use of undefined label: " + kvp.first.name );
    5153                }
    5254        }
    53         return mutate_field( decl, &FunctionDecl::stmts,
    54                                                  multiLevelExitUpdate( decl->stmts.get(), labelTable ) );
     55        return ast::mutate_field( decl, &ast::FunctionDecl::stmts,
     56                multiLevelExitUpdate( decl->stmts.get(), labelTable ) );
    5557}
    5658
    57 void FixLabelsCore::previsit( const Stmt * stmt ) {
     59void FixLabelsCore::previsit( const ast::Stmt * stmt ) {
    5860        if ( !stmt->labels.empty() ) {
    5961                setLabelsDef( stmt->labels, stmt );
     
    6163}
    6264
    63 void FixLabelsCore::previsit( const BranchStmt * stmt ) {
     65void FixLabelsCore::previsit( const ast::BranchStmt * stmt ) {
    6466        if ( !stmt->labels.empty() ) {
    6567                setLabelsDef( stmt->labels, stmt );
     
    7072}
    7173
    72 void FixLabelsCore::previsit( const LabelAddressExpr * expr ) {
     74void FixLabelsCore::previsit( const ast::LabelAddressExpr * expr ) {
    7375        assert( !expr->arg.empty() );
    7476        setLabelsUsage( expr->arg );
     
    7678
    7779void FixLabelsCore::setLabelsDef(
    78         const std::vector<Label> & labels, const Stmt * stmt ) {
     80                const std::vector<ast::Label> & labels, const ast::Stmt * stmt ) {
    7981        assert( !labels.empty() );
    8082        assert( stmt );
     
    8789                        // Duplicate definition, this is an error.
    8890                        SemanticError( label.location,
    89                                                    "Duplicate definition of label: " + label.name );
     91                                "Duplicate definition of label: " + label.name );
    9092                } else {
    9193                        // Perviously used, but not defined until now.
     
    9698
    9799// Label was used, if it is new add it to the table.
    98 void FixLabelsCore::setLabelsUsage( const Label & label ) {
     100void FixLabelsCore::setLabelsUsage( const ast::Label & label ) {
    99101        if ( labelTable.find( label ) == labelTable.end() ) {
    100102                labelTable[ label ] = nullptr;
     
    102104}
    103105
    104 void fixLabels( TranslationUnit & translationUnit ) {
    105         Pass<FixLabelsCore>::run( translationUnit );
     106} // namespace
     107
     108void fixLabels( ast::TranslationUnit & translationUnit ) {
     109        ast::Pass<FixLabelsCore>::run( translationUnit );
    106110}
     111
    107112} // namespace ControlStruct
    108113
  • src/ControlStruct/FixLabels.hpp

    r376c632a rf681823  
    99// Author           : Andrew Beach
    1010// Created On       : Mon Nov  1 09:36:00 2021
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jan 31 22:18:43 2022
    13 // Update Count     : 2
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Mon Nov  1 09:40:00 2021
     13// Update Count     : 0
    1414//
    1515
     
    1717
    1818namespace ast {
    19 class TranslationUnit;
     19        class TranslationUnit;
    2020}
    2121
    2222namespace ControlStruct {
    23 // normalizes label definitions and generates multi-level exit labels
     23
     24/// normalizes label definitions and generates multi-level exit labels
    2425void fixLabels( ast::TranslationUnit & translationUnit );
     26
    2527}
    2628
  • src/ControlStruct/ForExprMutator.h

    r376c632a rf681823  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Jan 30 09:14:46 2022
    13 // Update Count     : 6
     12// Last Modified On : Thu Aug 17 15:32:48 2017
     13// Update Count     : 5
    1414//
    1515
     
    2424        class ForExprMutator {
    2525          public:
    26                 Statement * postmutate( IfStmt * );
    27                 Statement * postmutate( ForStmt * );
    28                 Statement * postmutate( WhileStmt * );
     26                Statement *postmutate( IfStmt * );
     27                Statement *postmutate( ForStmt * );
     28                Statement *postmutate( WhileStmt * );
    2929        };
    3030} // namespace ControlStruct
  • src/ControlStruct/LabelFixer.cc

    r376c632a rf681823  
    99// Author           : Rodolfo G. Esteves
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jan 31 22:28:31 2022
    13 // Update Count     : 161
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Tue Jan 21 10:32:00 2020
     13// Update Count     : 160
    1414//
    1515
     
    2727
    2828namespace ControlStruct {
    29 bool LabelFixer::Entry::insideLoop() {
    30         return ( dynamic_cast< ForStmt * > ( definition ) ||
    31                 dynamic_cast< WhileStmt * > ( definition )  );
    32 }
     29        bool LabelFixer::Entry::insideLoop() {
     30                return ( dynamic_cast< ForStmt * > ( definition ) ||
     31                        dynamic_cast< WhileStmt * > ( definition )  );
     32        }
    3333
    34 LabelFixer::LabelFixer( LabelGenerator * gen ) : generator ( gen ) {
    35         if ( generator == 0 )
    36                 generator = LabelGenerator::getGenerator();
    37 }
     34        LabelFixer::LabelFixer( LabelGenerator * gen ) : generator ( gen ) {
     35                if ( generator == 0 )
     36                        generator = LabelGenerator::getGenerator();
     37        }
    3838
    39 void LabelFixer::previsit( FunctionDecl * ) {
    40         // need to go into a nested function in a fresh state
    41         GuardValue( labelTable );
    42         labelTable.clear();
    43 }
     39        void LabelFixer::previsit( FunctionDecl * ) {
     40                // need to go into a nested function in a fresh state
     41                GuardValue( labelTable );
     42                labelTable.clear();
     43        }
    4444
    45 void LabelFixer::postvisit( FunctionDecl * functionDecl ) {
    46         PassVisitor<MultiLevelExitMutator> mlem( resolveJumps(), generator );
    47         // We start in the body so we can stop when we hit another FunctionDecl.
    48         maybeMutate( functionDecl->statements, mlem );
    49 }
     45        void LabelFixer::postvisit( FunctionDecl * functionDecl ) {
     46                PassVisitor<MultiLevelExitMutator> mlem( resolveJumps(), generator );
     47                // We start in the body so we can stop when we hit another FunctionDecl.
     48                maybeMutate( functionDecl->statements, mlem );
     49        }
    5050
    51 // prune to at most one label definition for each statement
    52 void LabelFixer::previsit( Statement * stmt ) {
    53         std::list< Label > &labels = stmt->get_labels();
     51        // prune to at most one label definition for each statement
     52        void LabelFixer::previsit( Statement * stmt ) {
     53                std::list< Label > &labels = stmt->get_labels();
    5454
    55         if ( ! labels.empty() ) {
    56                 // only remember one label for each statement
    57                 Label current = setLabelsDef( labels, stmt );
    58         } // if
    59 }
     55                if ( ! labels.empty() ) {
     56                        // only remember one label for each statement
     57                        Label current = setLabelsDef( labels, stmt );
     58                } // if
     59        }
    6060
    61 void LabelFixer::previsit( BranchStmt * branchStmt ) {
    62         previsit( ( Statement *)branchStmt );
     61        void LabelFixer::previsit( BranchStmt * branchStmt ) {
     62                previsit( ( Statement *)branchStmt );
    6363
    64         // for labeled branches, add an entry to the label table
    65         Label target = branchStmt->get_target();
    66         if ( target != "" ) {
    67                 setLabelsUsg( target, branchStmt );
     64                // for labeled branches, add an entry to the label table
     65                Label target = branchStmt->get_target();
     66                if ( target != "" ) {
     67                        setLabelsUsg( target, branchStmt );
     68                }
    6869        }
    69 }
    7070
    71 void LabelFixer::previsit( LabelAddressExpr * addrExpr ) {
    72         Label & target = addrExpr->arg;
    73         assert( target != "" );
    74         setLabelsUsg( target, addrExpr );
    75 }
     71        void LabelFixer::previsit( LabelAddressExpr * addrExpr ) {
     72                Label & target = addrExpr->arg;
     73                assert( target != "" );
     74                setLabelsUsg( target, addrExpr );
     75        }
    7676
    7777
    78 // Sets the definition of the labelTable entry to be the provided statement for every label in
    79 // the list parameter. Happens for every kind of statement.
    80 Label LabelFixer::setLabelsDef( std::list< Label > & llabel, Statement * definition ) {
    81         assert( definition != 0 );
    82         assert( llabel.size() > 0 );
     78        // Sets the definition of the labelTable entry to be the provided statement for every label in
     79        // the list parameter. Happens for every kind of statement.
     80        Label LabelFixer::setLabelsDef( std::list< Label > & llabel, Statement * definition ) {
     81                assert( definition != 0 );
     82                assert( llabel.size() > 0 );
    8383
    84         for ( std::list< Label >::iterator i = llabel.begin(); i != llabel.end(); i++ ) {
    85                 Label & l = *i;
    86                 l.set_statement( definition ); // attach statement to the label to be used later
    87                 if ( labelTable.find( l ) == labelTable.end() ) {
    88                         // All labels on this statement need to use the same entry,
    89                         // so this should only be created once.
    90                         // undefined and unused until now, add an entry
    91                         labelTable[ l ] = new Entry( definition );
    92                 } else if ( labelTable[ l ]->defined() ) {
    93                         // defined twice, error
    94                         SemanticError( l.get_statement()->location,
    95                                 "Duplicate definition of label: " + l.get_name() );
    96                 } else {
    97                         // used previously, but undefined until now -> link with this entry
    98                         // Question: Is changing objects important?
    99                         delete labelTable[ l ];
    100                         labelTable[ l ] = new Entry( definition );
    101                 } // if
    102         } // for
     84                for ( std::list< Label >::iterator i = llabel.begin(); i != llabel.end(); i++ ) {
     85                        Label & l = *i;
     86                        l.set_statement( definition ); // attach statement to the label to be used later
     87                        if ( labelTable.find( l ) == labelTable.end() ) {
     88                                // All labels on this statement need to use the same entry,
     89                                // so this should only be created once.
     90                                // undefined and unused until now, add an entry
     91                                labelTable[ l ] = new Entry( definition );
     92                        } else if ( labelTable[ l ]->defined() ) {
     93                                // defined twice, error
     94                                SemanticError( l.get_statement()->location,
     95                                        "Duplicate definition of label: " + l.get_name() );
     96                        } else {
     97                                // used previously, but undefined until now -> link with this entry
     98                                // Question: Is changing objects important?
     99                                delete labelTable[ l ];
     100                                labelTable[ l ] = new Entry( definition );
     101                        } // if
     102                } // for
    103103
    104         // Produce one of the labels attached to this statement to be temporarily used as the
    105         // canonical label.
    106         return labelTable[ llabel.front() ]->get_label();
    107 }
    108 
    109 // A label was used, add it to the table if it isn't already there
    110 template< typename UsageNode >
    111 void LabelFixer::setLabelsUsg( Label orgValue, UsageNode *use ) {
    112         assert( use != 0 );
    113 
    114         // add label with an unknown origin
    115         if ( labelTable.find( orgValue ) == labelTable.end() ) {
    116                 labelTable[ orgValue ] = new Entry( 0 );
    117         }
    118 }
    119 
    120 // Builds a table that maps a label to its defining statement.
    121 std::map<Label, Statement * > * LabelFixer::resolveJumps() throw ( SemanticErrorException ) {
    122         std::map< Label, Statement * > *ret = new std::map< Label, Statement * >();
    123         for ( std::map< Label, Entry * >::iterator i = labelTable.begin(); i != labelTable.end(); ++i ) {
    124                 if ( ! i->second->defined() ) {
    125                         SemanticError( i->first.get_statement()->location, "Use of undefined label: " + i->first.get_name() );
    126                 }
    127                 (*ret)[ i->first ] = i->second->get_definition();
     104                // Produce one of the labels attached to this statement to be temporarily used as the
     105                // canonical label.
     106                return labelTable[ llabel.front() ]->get_label();
    128107        }
    129108
    130         return ret;
    131 }
     109        // A label was used, add it to the table if it isn't already there
     110        template< typename UsageNode >
     111        void LabelFixer::setLabelsUsg( Label orgValue, UsageNode *use ) {
     112                assert( use != 0 );
     113
     114                // add label with an unknown origin
     115                if ( labelTable.find( orgValue ) == labelTable.end() ) {
     116                        labelTable[ orgValue ] = new Entry( 0 );
     117                }
     118        }
     119
     120        // Builds a table that maps a label to its defining statement.
     121        std::map<Label, Statement * > * LabelFixer::resolveJumps() throw ( SemanticErrorException ) {
     122                std::map< Label, Statement * > *ret = new std::map< Label, Statement * >();
     123                for ( std::map< Label, Entry * >::iterator i = labelTable.begin(); i != labelTable.end(); ++i ) {
     124                        if ( ! i->second->defined() ) {
     125                                SemanticError( i->first.get_statement()->location, "Use of undefined label: " + i->first.get_name() );
     126                        }
     127                        (*ret)[ i->first ] = i->second->get_definition();
     128                }
     129
     130                return ret;
     131        }
    132132}  // namespace ControlStruct
    133133
  • src/ControlStruct/LabelFixer.h

    r376c632a rf681823  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jan 31 22:28:04 2022
    13 // Update Count     : 35
     12// Last Modified On : Sat Jul 22 09:17:24 2017
     13// Update Count     : 34
    1414//
    1515
     
    2626
    2727namespace ControlStruct {
    28 // normalizes label definitions and generates multi-level exit labels
    29 class LabelGenerator;
     28        /// normalizes label definitions and generates multi-level exit labels
     29        class LabelGenerator;
    3030
    31 class LabelFixer final : public WithGuards {
    32   public:
    33         LabelFixer( LabelGenerator *gen = 0 );
     31        class LabelFixer final : public WithGuards {
     32          public:
     33                LabelFixer( LabelGenerator *gen = 0 );
    3434
    35         std::map < Label, Statement * > *resolveJumps() throw ( SemanticErrorException );
     35                std::map < Label, Statement * > *resolveJumps() throw ( SemanticErrorException );
    3636
    37         // Declarations
    38         void previsit( FunctionDecl *functionDecl );
    39         void postvisit( FunctionDecl *functionDecl );
     37                // Declarations
     38                void previsit( FunctionDecl *functionDecl );
     39                void postvisit( FunctionDecl *functionDecl );
    4040
    41         // Statements
    42         void previsit( Statement *stmt );
    43         void previsit( BranchStmt *branchStmt );
     41                // Statements
     42                void previsit( Statement *stmt );
     43                void previsit( BranchStmt *branchStmt );
    4444
    45         // Expressions
    46         void previsit( LabelAddressExpr *addrExpr );
     45                // Expressions
     46                void previsit( LabelAddressExpr *addrExpr );
    4747
    48         Label setLabelsDef( std::list< Label > &, Statement *definition );
    49         template< typename UsageNode >
    50         void setLabelsUsg( Label, UsageNode *usage = 0 );
    51 
    52   private:
    53         class Entry {
    54                 public:
    55                 Entry( Statement *to ) : definition( to ) {}
    56                 bool defined() { return ( definition != 0 ); }
    57                 bool insideLoop();
    58 
    59                 Label get_label() const { return label; }
    60                 void set_label( Label lab ) { label = lab; }
    61 
    62                 Statement *get_definition() const { return definition; }
    63                 void set_definition( Statement *def ) { definition = def; }
     48                Label setLabelsDef( std::list< Label > &, Statement *definition );
     49                template< typename UsageNode >
     50                void setLabelsUsg( Label, UsageNode *usage = 0 );
    6451
    6552          private:
    66                 Label label;
    67                 Statement *definition;
     53                class Entry {
     54                        public:
     55                        Entry( Statement *to ) : definition( to ) {}
     56                        bool defined() { return ( definition != 0 ); }
     57                        bool insideLoop();
     58
     59                        Label get_label() const { return label; }
     60                        void set_label( Label lab ) { label = lab; }
     61
     62                        Statement *get_definition() const { return definition; }
     63                        void set_definition( Statement *def ) { definition = def; }
     64
     65                  private:
     66                        Label label;
     67                        Statement *definition;
     68                };
     69
     70                std::map < Label, Entry *> labelTable;
     71                LabelGenerator *generator;
    6872        };
    69 
    70         std::map < Label, Entry *> labelTable;
    71         LabelGenerator *generator;
    72 };
    7373} // namespace ControlStruct
    7474
  • src/ControlStruct/LabelGenerator.cc

    r376c632a rf681823  
    99// Author           : Rodolfo G. Esteves
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jan 31 22:30:26 2022
    13 // Update Count     : 28
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Mon Nov  8 10:18:00 2021
     13// Update Count     : 17
    1414//
    1515
     
    1717#include <sstream>              // for ostringstream
    1818#include <list>                 // for list
    19 using namespace std;
    2019
    2120#include "LabelGenerator.h"
    2221
     22#include "AST/Attribute.hpp"
     23#include "AST/Label.hpp"
     24#include "AST/Stmt.hpp"
    2325#include "SynTree/Attribute.h"  // for Attribute
    2426#include "SynTree/Label.h"      // for Label, operator<<
     
    2628
    2729namespace ControlStruct {
     30
    2831int LabelGenerator::current = 0;
    2932LabelGenerator * LabelGenerator::labelGenerator = nullptr;
    3033
    31 LabelGenerator * LabelGenerator::getGenerator() {
    32         if ( LabelGenerator::labelGenerator == 0 )
    33                 LabelGenerator::labelGenerator = new LabelGenerator();
    34         return labelGenerator;
     34        LabelGenerator * LabelGenerator::getGenerator() {
     35                if ( LabelGenerator::labelGenerator == 0 )
     36                        LabelGenerator::labelGenerator = new LabelGenerator();
     37                return labelGenerator;
     38        }
     39
     40        Label LabelGenerator::newLabel( std::string suffix, Statement * stmt ) {
     41                std::ostringstream os;
     42                os << "__L" << current++ << "__" << suffix;
     43                if ( stmt && ! stmt->get_labels().empty() ) {
     44                        os << "_" << stmt->get_labels().front() << "__";
     45                } // if
     46                std::string ret = os.str();
     47                Label l( ret );
     48                l.get_attributes().push_back( new Attribute("unused") );
     49                return l;
     50        }
     51
     52ast::Label LabelGenerator::newLabel(
     53                const std::string & suffix, const ast::Stmt * stmt ) {
     54        assert( stmt );
     55
     56        std::ostringstream os;
     57        os << "__L" << current++ << "__" << suffix;
     58        if ( stmt && !stmt->labels.empty() ) {
     59                os << "_" << stmt->labels.front() << "__";
     60        }
     61        ast::Label ret_label( stmt->location, os.str() );
     62        ret_label.attributes.push_back( new ast::Attribute( "unused" ) );
     63        return ret_label;
    3564}
    3665
    37 Label LabelGenerator::newLabel( string suffix, Statement * stmt ) {
    38         ostringstream os;
    39         os << "__L_OLD" << current++ << "__" << suffix;
    40         if ( stmt && ! stmt->get_labels().empty() ) {
    41                 os << "_" << stmt->get_labels().front() << "__";
    42         } // if
    43         string ret = os.str();
    44         Label l( ret );
    45         l.get_attributes().push_back( new Attribute( "unused" ) );
    46         return l;
    47 }
    4866} // namespace ControlStruct
    4967
    5068// Local Variables: //
     69// tab-width: 4 //
    5170// mode: c++ //
     71// compile-command: "make install" //
    5272// End: //
  • src/ControlStruct/LabelGenerator.h

    r376c632a rf681823  
    99// Author           : Rodolfo G. Esteves
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jan 31 22:30:10 2022
    13 // Update Count     : 16
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Mon Nov  8 10:16:00 2021
     13// Update Count     : 8
    1414//
    1515
     
    2121
    2222class Statement;
    23 
    2423namespace ast {
    25 class Stmt;
    26 class Label;
     24        class Stmt;
     25        class Label;
    2726}
    2827
    2928namespace ControlStruct {
     29
    3030class LabelGenerator {
    3131        static int current;
    3232        static LabelGenerator *labelGenerator;
    33   protected:
     33protected:
    3434        LabelGenerator() {}
    35   public:
     35public:
    3636        static LabelGenerator *getGenerator();
    3737        static Label newLabel(std::string suffix, Statement * stmt = nullptr);
     38        static ast::Label newLabel( const std::string&, const ast::Stmt * );
     39        static void reset() { current = 0; }
     40        static void rewind() { current--; }
    3841};
     42
    3943} // namespace ControlStruct
    4044
  • src/ControlStruct/MultiLevelExit.cpp

    r376c632a rf681823  
    99// Author           : Andrew Beach
    1010// Created On       : Mon Nov  1 13:48:00 2021
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jan 31 22:35:08 2022
    13 // Update Count     : 28
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Mon Nov  8 10:56:00 2021
     13// Update Count     : 2
    1414//
    1515
     
    1818#include "AST/Pass.hpp"
    1919#include "AST/Stmt.hpp"
    20 #include "LabelGeneratorNew.hpp"
     20#include "ControlStruct/LabelGenerator.h"
    2121
    2222#include <set>
    23 using namespace std;
    24 using namespace ast;
    2523
    2624namespace ControlStruct {
     25
     26namespace {
     27
    2728class Entry {
    28   public:
    29         const Stmt * stmt;
    30   private:
     29public:
     30        const ast::Stmt * stmt;
     31private:
    3132        // Organized like a manual ADT. Avoids creating a bunch of dead data.
    3233        struct Target {
    33                 Label label;
     34                ast::Label label;
    3435                bool used = false;
    35                 Target( const Label & label ) : label( label ) {}
     36                Target( const ast::Label & label ) : label( label ) {}
    3637                Target() : label( CodeLocation() ) {}
    3738        };
     
    4041
    4142        enum Kind {
    42                 ForStmtK, WhileStmtK, CompoundStmtK, IfStmtK, CaseStmtK, SwitchStmtK, TryStmtK
     43                ForStmt, WhileStmt, CompoundStmt, IfStmt, CaseStmt, SwitchStmt, TryStmt
    4344        } kind;
    4445
    4546        bool fallDefaultValid = true;
    4647
    47         static Label & useTarget( Target & target ) {
     48        static ast::Label & useTarget( Target & target ) {
    4849                target.used = true;
    4950                return target.label;
    5051        }
    5152
    52   public:
    53         Entry( const ForStmt * stmt, Label breakExit, Label contExit ) :
    54                 stmt( stmt ), firstTarget( breakExit ), secondTarget( contExit ), kind( ForStmtK ) {}
    55         Entry( const WhileStmt * stmt, Label breakExit, Label contExit ) :
    56                 stmt( stmt ), firstTarget( breakExit ), secondTarget( contExit ), kind( WhileStmtK ) {}
    57         Entry( const CompoundStmt *stmt, Label breakExit ) :
    58                 stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( CompoundStmtK ) {}
    59         Entry( const IfStmt *stmt, Label breakExit ) :
    60                 stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( IfStmtK ) {}
    61         Entry( const CaseStmt *stmt, Label fallExit ) :
    62                 stmt( stmt ), firstTarget( fallExit ), secondTarget(), kind( CaseStmtK ) {}
    63         Entry( const SwitchStmt *stmt, Label breakExit, Label fallDefaultExit ) :
    64                 stmt( stmt ), firstTarget( breakExit ), secondTarget( fallDefaultExit ), kind( SwitchStmtK ) {}
    65         Entry( const TryStmt *stmt, Label breakExit ) :
    66                 stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( TryStmtK ) {}
    67 
    68         bool isContTarget() const { return kind <= WhileStmtK; }
    69         bool isBreakTarget() const { return kind != CaseStmtK; }
    70         bool isFallTarget() const { return kind == CaseStmtK; }
    71         bool isFallDefaultTarget() const { return kind == SwitchStmtK; }
    72 
    73         Label useContExit() { assert( kind <= WhileStmtK ); return useTarget(secondTarget); }
    74         Label useBreakExit() { assert( kind != CaseStmtK ); return useTarget(firstTarget); }
    75         Label useFallExit() { assert( kind == CaseStmtK );  return useTarget(firstTarget); }
    76         Label useFallDefaultExit() { assert( kind == SwitchStmtK ); return useTarget(secondTarget); }
    77 
    78         bool isContUsed() const { assert( kind <= WhileStmtK ); return secondTarget.used; }
    79         bool isBreakUsed() const { assert( kind != CaseStmtK ); return firstTarget.used; }
    80         bool isFallUsed() const { assert( kind == CaseStmtK ); return firstTarget.used; }
    81         bool isFallDefaultUsed() const { assert( kind == SwitchStmtK ); return secondTarget.used; }
     53public:
     54        Entry( const ast::ForStmt * stmt, ast::Label breakExit, ast::Label contExit ) :
     55                stmt( stmt ), firstTarget( breakExit ), secondTarget( contExit ), kind( ForStmt ) {}
     56        Entry( const ast::WhileStmt * stmt, ast::Label breakExit, ast::Label contExit ) :
     57                stmt( stmt ), firstTarget( breakExit ), secondTarget( contExit ), kind( WhileStmt ) {}
     58        Entry( const ast::CompoundStmt *stmt, ast::Label breakExit ) :
     59                stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( CompoundStmt ) {}
     60        Entry( const ast::IfStmt *stmt, ast::Label breakExit ) :
     61                stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( IfStmt ) {}
     62        Entry( const ast::CaseStmt *stmt, ast::Label fallExit ) :
     63                stmt( stmt ), firstTarget( fallExit ), secondTarget(), kind( CaseStmt ) {}
     64        Entry( const ast::SwitchStmt *stmt, ast::Label breakExit, ast::Label fallDefaultExit ) :
     65                stmt( stmt ), firstTarget( breakExit ), secondTarget( fallDefaultExit ), kind( SwitchStmt ) {}
     66        Entry( const ast::TryStmt *stmt, ast::Label breakExit ) :
     67                stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( TryStmt ) {}
     68
     69        bool isContTarget() const { return kind <= WhileStmt; }
     70        bool isBreakTarget() const { return CaseStmt != kind; }
     71        bool isFallTarget() const { return CaseStmt == kind; }
     72        bool isFallDefaultTarget() const { return SwitchStmt == kind; }
     73
     74        ast::Label useContExit() { assert( kind <= WhileStmt ); return useTarget(secondTarget); }
     75        ast::Label useBreakExit() { assert( CaseStmt != kind ); return useTarget(firstTarget); }
     76        ast::Label useFallExit() { assert( CaseStmt == kind );  return useTarget(firstTarget); }
     77        ast::Label useFallDefaultExit() { assert( SwitchStmt == kind ); return useTarget(secondTarget); }
     78
     79        bool isContUsed() const { assert( kind <= WhileStmt ); return secondTarget.used; }
     80        bool isBreakUsed() const { assert( CaseStmt != kind ); return firstTarget.used; }
     81        bool isFallUsed() const { assert( CaseStmt == kind ); return firstTarget.used; }
     82        bool isFallDefaultUsed() const { assert( SwitchStmt == kind ); return secondTarget.used; }
    8283        void seenDefault() { fallDefaultValid = false; }
    8384        bool isFallDefaultValid() const { return fallDefaultValid; }
    8485};
    8586
    86 // Helper predicates used in find_if calls (it doesn't take methods):
     87// Helper predicates used in std::find_if calls (it doesn't take methods):
    8788bool isBreakTarget( const Entry & entry ) {
    8889        return entry.isBreakTarget();
     
    102103
    103104struct MultiLevelExitCore final :
    104         public WithVisitorRef<MultiLevelExitCore>,
    105         public WithShortCircuiting, public WithGuards {
     105                public ast::WithVisitorRef<MultiLevelExitCore>,
     106                public ast::WithShortCircuiting, public ast::WithGuards {
    106107        MultiLevelExitCore( const LabelToStmt & lt );
    107108
    108         void previsit( const FunctionDecl * );
    109 
    110         const CompoundStmt * previsit( const CompoundStmt * );
    111         const BranchStmt * postvisit( const BranchStmt * );
    112         void previsit( const WhileStmt * );
    113         const WhileStmt * postvisit( const WhileStmt * );
    114         void previsit( const ForStmt * );
    115         const ForStmt * postvisit( const ForStmt * );
    116         const CaseStmt * previsit( const CaseStmt * );
    117         void previsit( const IfStmt * );
    118         const IfStmt * postvisit( const IfStmt * );
    119         void previsit( const SwitchStmt * );
    120         const SwitchStmt * postvisit( const SwitchStmt * );
    121         void previsit( const ReturnStmt * );
    122         void previsit( const TryStmt * );
    123         void postvisit( const TryStmt * );
    124         void previsit( const FinallyStmt * );
    125 
    126         const Stmt * mutateLoop( const Stmt * body, Entry& );
     109        void previsit( const ast::FunctionDecl * );
     110
     111        const ast::CompoundStmt * previsit( const ast::CompoundStmt * );
     112        const ast::BranchStmt * postvisit( const ast::BranchStmt * );
     113        void previsit( const ast::WhileStmt * );
     114        const ast::WhileStmt * postvisit( const ast::WhileStmt * );
     115        void previsit( const ast::ForStmt * );
     116        const ast::ForStmt * postvisit( const ast::ForStmt * );
     117        const ast::CaseStmt * previsit( const ast::CaseStmt * );
     118        void previsit( const ast::IfStmt * );
     119        const ast::IfStmt * postvisit( const ast::IfStmt * );
     120        void previsit( const ast::SwitchStmt * );
     121        const ast::SwitchStmt * postvisit( const ast::SwitchStmt * );
     122        void previsit( const ast::ReturnStmt * );
     123        void previsit( const ast::TryStmt * );
     124        void postvisit( const ast::TryStmt * );
     125        void previsit( const ast::FinallyStmt * );
     126
     127        const ast::Stmt * mutateLoop( const ast::Stmt * body, Entry& );
    127128
    128129        const LabelToStmt & target_table;
    129         set<Label> fallthrough_labels;
    130         vector<Entry> enclosing_control_structures;
    131         Label break_label;
     130        std::set<ast::Label> fallthrough_labels;
     131        std::vector<Entry> enclosing_control_structures;
     132        ast::Label break_label;
    132133        bool inFinally;
    133134
     
    137138        const LoopNode * posthandleLoopStmt( const LoopNode * loopStmt );
    138139
    139         list<ptr<Stmt>> fixBlock(
    140                 const list<ptr<Stmt>> & kids, bool caseClause );
     140        std::list<ast::ptr<ast::Stmt>> fixBlock(
     141                const std::list<ast::ptr<ast::Stmt>> & kids, bool caseClause );
    141142
    142143        template<typename UnaryPredicate>
    143144        auto findEnclosingControlStructure( UnaryPredicate pred ) {
    144                 return find_if( enclosing_control_structures.rbegin(),
    145                                                 enclosing_control_structures.rend(), pred );
     145                return std::find_if( enclosing_control_structures.rbegin(),
     146                        enclosing_control_structures.rend(), pred );
    146147        }
    147148};
    148149
    149 NullStmt * labelledNullStmt(
    150         const CodeLocation & cl, const Label & label ) {
    151         return new NullStmt( cl, vector<Label>{ label } );
     150ast::NullStmt * labelledNullStmt(
     151                const CodeLocation & cl, const ast::Label & label ) {
     152        return new ast::NullStmt( cl, std::vector<ast::Label>{ label } );
    152153}
    153154
     
    157158{}
    158159
    159 void MultiLevelExitCore::previsit( const FunctionDecl * ) {
     160void MultiLevelExitCore::previsit( const ast::FunctionDecl * ) {
    160161        visit_children = false;
    161162}
    162163
    163 const CompoundStmt * MultiLevelExitCore::previsit(
    164         const CompoundStmt * stmt ) {
     164const ast::CompoundStmt * MultiLevelExitCore::previsit(
     165                const ast::CompoundStmt * stmt ) {
    165166        visit_children = false;
    166167        bool isLabeled = !stmt->labels.empty();
    167168        if ( isLabeled ) {
    168                 Label breakLabel = newLabel( "blockBreak", stmt );
     169                ast::Label breakLabel = LabelGenerator::newLabel( "blockBreak", stmt );
    169170                enclosing_control_structures.emplace_back( stmt, breakLabel );
    170171                GuardAction( [this]() { enclosing_control_structures.pop_back(); } );
    171172        }
    172173
    173         auto mutStmt = mutate( stmt );
     174        auto mutStmt = ast::mutate( stmt );
    174175        // A child statement may set the break label.
    175         mutStmt->kids = move( fixBlock( stmt->kids, false ) );
     176        mutStmt->kids = std::move( fixBlock( stmt->kids, false ) );
    176177
    177178        if ( isLabeled ) {
     
    186187
    187188size_t getUnusedIndex(
    188         const Stmt * stmt, const Label & originalTarget ) {
     189                const ast::Stmt * stmt, const ast::Label & originalTarget ) {
    189190        const size_t size = stmt->labels.size();
    190191
    191         // If the label is empty, do not add unused attribute.
    192   if ( originalTarget.empty() ) return size;
     192        // If the label is empty, we can skip adding the unused attribute:
     193        if ( originalTarget.empty() ) return size;
    193194
    194195        // Search for a label that matches the originalTarget.
    195196        for ( size_t i = 0 ; i < size ; ++i ) {
    196                 const Label & label = stmt->labels[i];
     197                const ast::Label & label = stmt->labels[i];
    197198                if ( label == originalTarget ) {
    198                         for ( const Attribute * attr : label.attributes ) {
     199                        for ( const ast::Attribute * attr : label.attributes ) {
    199200                                if ( attr->name == "unused" ) return size;
    200201                        }
     
    203204        }
    204205        assertf( false, "Could not find label '%s' on statement %s",
    205                          originalTarget.name.c_str(), toString( stmt ).c_str() );
    206 }
    207 
    208 const Stmt * addUnused(
    209         const Stmt * stmt, const Label & originalTarget ) {
     206                originalTarget.name.c_str(), toString( stmt ).c_str() );
     207}
     208
     209const ast::Stmt * addUnused(
     210                const ast::Stmt * stmt, const ast::Label & originalTarget ) {
    210211        size_t i = getUnusedIndex( stmt, originalTarget );
    211212        if ( i == stmt->labels.size() ) {
    212213                return stmt;
    213214        }
    214         Stmt * mutStmt = mutate( stmt );
    215         mutStmt->labels[i].attributes.push_back( new Attribute( "unused" ) );
     215        ast::Stmt * mutStmt = ast::mutate( stmt );
     216        mutStmt->labels[i].attributes.push_back( new ast::Attribute( "unused" ) );
    216217        return mutStmt;
    217218}
    218219
    219 const BranchStmt * MultiLevelExitCore::postvisit( const BranchStmt * stmt ) {
    220         vector<Entry>::reverse_iterator targetEntry =
     220const ast::BranchStmt * MultiLevelExitCore::postvisit( const ast::BranchStmt * stmt ) {
     221        std::vector<Entry>::reverse_iterator targetEntry =
    221222                enclosing_control_structures.rend();
    222223        switch ( stmt->kind ) {
    223           case BranchStmt::Goto:
     224        case ast::BranchStmt::Goto:
    224225                return stmt;
    225           case BranchStmt::Continue:
    226           case BranchStmt::Break: {
    227                   bool isContinue = stmt->kind == BranchStmt::Continue;
    228                   // Handle unlabeled break and continue.
    229                   if ( stmt->target.empty() ) {
    230                           if ( isContinue ) {
    231                                   targetEntry = findEnclosingControlStructure( isContinueTarget );
    232                           } else {
    233                                   if ( enclosing_control_structures.empty() ) {
    234                                           SemanticError( stmt->location,
    235                                                                          "'break' outside a loop, 'switch', or labelled block" );
    236                                   }
    237                                   targetEntry = findEnclosingControlStructure( isBreakTarget );
    238                           }
    239                           // Handle labeled break and continue.
    240                   } else {
    241                           // Lookup label in table to find attached control structure.
    242                           targetEntry = findEnclosingControlStructure(
    243                                   [ targetStmt = target_table.at(stmt->target) ](auto entry){
    244                                           return entry.stmt == targetStmt;
    245                                   } );
    246                   }
    247                   // Ensure that selected target is valid.
    248                   if ( targetEntry == enclosing_control_structures.rend() || ( isContinue && !isContinueTarget( *targetEntry ) ) ) {
    249                           SemanticError( stmt->location, toString( (isContinue ? "'continue'" : "'break'"),
    250                                                         " target must be an enclosing ", (isContinue ? "loop: " : "control structure: "),
    251                                                         stmt->originalTarget ) );
    252                   }
    253                   break;
    254           }
    255           case BranchStmt::FallThrough: {
    256                   targetEntry = findEnclosingControlStructure( isFallthroughTarget );
    257                   // Check that target is valid.
    258                   if ( targetEntry == enclosing_control_structures.rend() ) {
    259                           SemanticError( stmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" );
    260                   }
    261                   if ( !stmt->target.empty() ) {
    262                           // Labelled fallthrough: target must be a valid fallthough label.
    263                           if ( !fallthrough_labels.count( stmt->target ) ) {
    264                                   SemanticError( stmt->location, toString( "'fallthrough' target must be a later case statement: ",
    265                                                                                                                    stmt->originalTarget ) );
    266                           }
    267                           return new BranchStmt(
    268                                   stmt->location, BranchStmt::Goto, stmt->originalTarget );
    269                   }
    270                   break;
    271           }
    272           case BranchStmt::FallThroughDefault: {
    273                   targetEntry = findEnclosingControlStructure( isFallthroughDefaultTarget );
    274 
    275                   // Check if in switch or choose statement.
    276                   if ( targetEntry == enclosing_control_structures.rend() ) {
    277                           SemanticError( stmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" );
    278                   }
    279 
    280                   // Check if switch or choose has default clause.
    281                   auto switchStmt = strict_dynamic_cast< const SwitchStmt * >( targetEntry->stmt );
    282                   bool foundDefault = false;
    283                   for ( auto subStmt : switchStmt->stmts ) {
    284                           const CaseStmt * caseStmt = subStmt.strict_as<CaseStmt>();
    285                           if ( caseStmt->isDefault() ) {
    286                                   foundDefault = true;
    287                                   break;
    288                           }
    289                   }
    290                   if ( ! foundDefault ) {
    291                           SemanticError( stmt->location, "'fallthrough default' must be enclosed in a 'switch' or 'choose'"
    292                                                          "control structure with a 'default' clause" );
    293                   }
    294                   break;
    295           }
    296           default:
     226        case ast::BranchStmt::Continue:
     227        case ast::BranchStmt::Break: {
     228                bool isContinue = stmt->kind == ast::BranchStmt::Continue;
     229                // Handle unlabeled break and continue.
     230                if ( stmt->target.empty() ) {
     231                        if ( isContinue ) {
     232                                targetEntry = findEnclosingControlStructure( isContinueTarget );
     233                        } else {
     234                                if ( enclosing_control_structures.empty() ) {
     235                                        SemanticError( stmt->location,
     236                                                "'break' outside a loop, 'switch', or labelled block" );
     237                                }
     238                                targetEntry = findEnclosingControlStructure( isBreakTarget );
     239                        }
     240                // Handle labeled break and continue.
     241                } else {
     242                        // Lookup label in table to find attached control structure.
     243                        targetEntry = findEnclosingControlStructure(
     244                                [ targetStmt = target_table.at(stmt->target) ](auto entry){
     245                                        return entry.stmt == targetStmt;
     246                                } );
     247                }
     248                // Ensure that selected target is valid.
     249                if ( targetEntry == enclosing_control_structures.rend() || ( isContinue && !isContinueTarget( *targetEntry ) ) ) {
     250                        SemanticError(
     251                                stmt->location,
     252                                toString( (isContinue ? "'continue'" : "'break'"),
     253                                        " target must be an enclosing ",
     254                                        (isContinue ? "loop: " : "control structure: "),
     255                                        stmt->originalTarget ) );
     256                }
     257                break;
     258        }
     259        case ast::BranchStmt::FallThrough: {
     260                targetEntry = findEnclosingControlStructure( isFallthroughTarget );
     261                // Check that target is valid.
     262                if ( targetEntry == enclosing_control_structures.rend() ) {
     263                        SemanticError( stmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" );
     264                }
     265                if ( !stmt->target.empty() ) {
     266                        // Labelled fallthrough: target must be a valid fallthough label.
     267                        if ( !fallthrough_labels.count( stmt->target ) ) {
     268                                SemanticError( stmt->location, toString( "'fallthrough' target must be a later case statement: ", stmt->originalTarget ) );
     269                        }
     270                        return new ast::BranchStmt(
     271                                stmt->location, ast::BranchStmt::Goto, stmt->originalTarget );
     272                }
     273                break;
     274        }
     275        case ast::BranchStmt::FallThroughDefault: {
     276                targetEntry = findEnclosingControlStructure( isFallthroughDefaultTarget );
     277
     278                // Check that this is in a switch or choose statement.
     279                if ( targetEntry == enclosing_control_structures.rend() ) {
     280                        SemanticError( stmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" );
     281                }
     282
     283                // Check that the switch or choose has a default clause.
     284                auto switchStmt = strict_dynamic_cast< const ast::SwitchStmt * >(
     285                        targetEntry->stmt );
     286                bool foundDefault = false;
     287                for ( auto subStmt : switchStmt->stmts ) {
     288                        const ast::CaseStmt * caseStmt = subStmt.strict_as<ast::CaseStmt>();
     289                        if ( caseStmt->isDefault() ) {
     290                                foundDefault = true;
     291                                break;
     292                        }
     293                }
     294                if ( !foundDefault ) {
     295                        SemanticError( stmt->location, "'fallthrough default' must be enclosed in a 'switch' or 'choose' control structure with a 'default' clause" );
     296                }
     297                break;
     298        }
     299        default:
    297300                assert( false );
    298301        }
    299302
    300303        // Branch error checks: get the appropriate label name:
    301         // (This label is always replaced.)
    302         Label exitLabel( CodeLocation(), "" );
     304        // (This label will always be replaced.)
     305        ast::Label exitLabel( CodeLocation(), "" );
    303306        switch ( stmt->kind ) {
    304           case BranchStmt::Break:
     307        case ast::BranchStmt::Break:
    305308                assert( !targetEntry->useBreakExit().empty() );
    306309                exitLabel = targetEntry->useBreakExit();
    307310                break;
    308           case BranchStmt::Continue:
     311        case ast::BranchStmt::Continue:
    309312                assert( !targetEntry->useContExit().empty() );
    310313                exitLabel = targetEntry->useContExit();
    311314                break;
    312           case BranchStmt::FallThrough:
     315        case ast::BranchStmt::FallThrough:
    313316                assert( !targetEntry->useFallExit().empty() );
    314317                exitLabel = targetEntry->useFallExit();
    315318                break;
    316           case BranchStmt::FallThroughDefault:
     319        case ast::BranchStmt::FallThroughDefault:
    317320                assert( !targetEntry->useFallDefaultExit().empty() );
    318321                exitLabel = targetEntry->useFallDefaultExit();
    319322                // Check that fallthrough default comes before the default clause.
    320323                if ( !targetEntry->isFallDefaultValid() ) {
    321                         SemanticError( stmt->location, "'fallthrough default' must precede the 'default' clause" );
     324                        SemanticError( stmt->location,
     325                                "'fallthrough default' must precede the 'default' clause" );
    322326                }
    323327                break;
    324           default:
     328        default:
    325329                assert(0);
    326330        }
     
    329333        targetEntry->stmt = addUnused( targetEntry->stmt, stmt->originalTarget );
    330334
    331         // Replace with goto to make later passes more uniform.
    332         return new BranchStmt( stmt->location, BranchStmt::Goto, exitLabel );
    333 }
    334 
    335 void MultiLevelExitCore::previsit( const WhileStmt * stmt ) {
     335        // Replace this with a goto to make later passes more uniform.
     336        return new ast::BranchStmt( stmt->location, ast::BranchStmt::Goto, exitLabel );
     337}
     338
     339void MultiLevelExitCore::previsit( const ast::WhileStmt * stmt ) {
    336340        return prehandleLoopStmt( stmt );
    337341}
    338342
    339 const WhileStmt * MultiLevelExitCore::postvisit( const WhileStmt * stmt ) {
     343const ast::WhileStmt * MultiLevelExitCore::postvisit( const ast::WhileStmt * stmt ) {
    340344        return posthandleLoopStmt( stmt );
    341345}
    342346
    343 void MultiLevelExitCore::previsit( const ForStmt * stmt ) {
     347void MultiLevelExitCore::previsit( const ast::ForStmt * stmt ) {
    344348        return prehandleLoopStmt( stmt );
    345349}
    346350
    347 const ForStmt * MultiLevelExitCore::postvisit( const ForStmt * stmt ) {
     351const ast::ForStmt * MultiLevelExitCore::postvisit( const ast::ForStmt * stmt ) {
    348352        return posthandleLoopStmt( stmt );
    349353}
     
    351355// Mimic what the built-in push_front would do anyways. It is O(n).
    352356void push_front(
    353         vector<ptr<Stmt>> & vec, const Stmt * element ) {
     357                std::vector<ast::ptr<ast::Stmt>> & vec, const ast::Stmt * element ) {
    354358        vec.emplace_back( nullptr );
    355359        for ( size_t i = vec.size() - 1 ; 0 < i ; --i ) {
    356                 vec[ i ] = move( vec[ i - 1 ] );
     360                vec[ i ] = std::move( vec[ i - 1 ] );
    357361        }
    358362        vec[ 0 ] = element;
    359363}
    360364
    361 const CaseStmt * MultiLevelExitCore::previsit( const CaseStmt * stmt ) {
     365const ast::CaseStmt * MultiLevelExitCore::previsit( const ast::CaseStmt * stmt ) {
    362366        visit_children = false;
    363367
    364         // If default, mark seen.
     368        // If it is the default, mark the default as seen.
    365369        if ( stmt->isDefault() ) {
    366370                assert( !enclosing_control_structures.empty() );
     
    369373
    370374        // The cond may not exist, but if it does update it now.
    371         visitor->maybe_accept( stmt, &CaseStmt::cond );
     375        visitor->maybe_accept( stmt, &ast::CaseStmt::cond );
    372376
    373377        // Just save the mutated node for simplicity.
    374         CaseStmt * mutStmt = mutate( stmt );
    375 
    376         Label fallLabel = newLabel( "fallThrough", stmt );
    377         if ( ! mutStmt->stmts.empty() ) {
     378        ast::CaseStmt * mutStmt = ast::mutate( stmt );
     379
     380        ast::Label fallLabel = LabelGenerator::newLabel( "fallThrough", stmt );
     381        if ( !mutStmt->stmts.empty() ) {
    378382                // Ensure that the stack isn't corrupted by exceptions in fixBlock.
    379383                auto guard = makeFuncGuard(
    380384                        [&](){ enclosing_control_structures.emplace_back( mutStmt, fallLabel ); },
    381385                        [this](){ enclosing_control_structures.pop_back(); }
    382                         );
     386                );
    383387
    384388                // These should already be in a block.
    385                 auto block = mutate( mutStmt->stmts.front().strict_as<CompoundStmt>() );
     389                auto block = ast::mutate( mutStmt->stmts.front().strict_as<ast::CompoundStmt>() );
    386390                block->kids = fixBlock( block->kids, true );
    387391
    388392                // Add fallthrough label if necessary.
    389                 assert( ! enclosing_control_structures.empty() );
     393                assert( !enclosing_control_structures.empty() );
    390394                Entry & entry = enclosing_control_structures.back();
    391395                if ( entry.isFallUsed() ) {
     
    394398                }
    395399        }
    396         assert( ! enclosing_control_structures.empty() );
     400        assert( !enclosing_control_structures.empty() );
    397401        Entry & entry = enclosing_control_structures.back();
    398         assertf( dynamic_cast< const SwitchStmt * >( entry.stmt ),
    399                          "Control structure enclosing a case clause must be a switch, but is: %s",
    400                          toString( entry.stmt ).c_str() );
     402        assertf( dynamic_cast< const ast::SwitchStmt * >( entry.stmt ),
     403                "Control structure enclosing a case clause must be a switch, but is: %s",
     404                toString( entry.stmt ).c_str() );
    401405        if ( mutStmt->isDefault() ) {
    402406                if ( entry.isFallDefaultUsed() ) {
    403407                        // Add fallthrough default label if necessary.
    404408                        push_front( mutStmt->stmts, labelledNullStmt(
    405                                                         stmt->location, entry.useFallDefaultExit()
    406                                                         ) );
     409                                stmt->location, entry.useFallDefaultExit()
     410                        ) );
    407411                }
    408412        }
     
    410414}
    411415
    412 void MultiLevelExitCore::previsit( const IfStmt * stmt ) {
     416void MultiLevelExitCore::previsit( const ast::IfStmt * stmt ) {
    413417        bool labeledBlock = !stmt->labels.empty();
    414418        if ( labeledBlock ) {
    415                 Label breakLabel = newLabel( "blockBreak", stmt );
     419                ast::Label breakLabel = LabelGenerator::newLabel( "blockBreak", stmt );
    416420                enclosing_control_structures.emplace_back( stmt, breakLabel );
    417421                GuardAction( [this](){ enclosing_control_structures.pop_back(); } );
     
    419423}
    420424
    421 const IfStmt * MultiLevelExitCore::postvisit( const IfStmt * stmt ) {
     425const ast::IfStmt * MultiLevelExitCore::postvisit( const ast::IfStmt * stmt ) {
    422426        bool labeledBlock = !stmt->labels.empty();
    423427        if ( labeledBlock ) {
     
    430434}
    431435
    432 bool isDefaultCase( const ptr<Stmt> & stmt ) {
    433         const CaseStmt * caseStmt = stmt.strict_as<CaseStmt>();
     436bool isDefaultCase( const ast::ptr<ast::Stmt> & stmt ) {
     437        const ast::CaseStmt * caseStmt = stmt.strict_as<ast::CaseStmt>();
    434438        return caseStmt->isDefault();
    435439}
    436440
    437 void MultiLevelExitCore::previsit( const SwitchStmt * stmt ) {
    438         Label label = newLabel( "switchBreak", stmt );
    439         auto it = find_if( stmt->stmts.rbegin(), stmt->stmts.rend(), isDefaultCase );
    440 
    441         const CaseStmt * defaultCase = it != stmt->stmts.rend()
    442                 ? (it)->strict_as<CaseStmt>() : nullptr;
    443         Label defaultLabel = defaultCase
    444                 ? newLabel( "fallThroughDefault", defaultCase )
    445                 : Label( stmt->location, "" );
     441void MultiLevelExitCore::previsit( const ast::SwitchStmt * stmt ) {
     442        ast::Label label = LabelGenerator::newLabel( "switchBreak", stmt );
     443        auto it = std::find_if( stmt->stmts.rbegin(), stmt->stmts.rend(), isDefaultCase );
     444
     445        const ast::CaseStmt * defaultCase = it != stmt->stmts.rend()
     446                ? (it)->strict_as<ast::CaseStmt>() : nullptr;
     447        ast::Label defaultLabel = defaultCase
     448                ? LabelGenerator::newLabel( "fallThroughDefault", defaultCase )
     449                : ast::Label( stmt->location, "" );
    446450        enclosing_control_structures.emplace_back( stmt, label, defaultLabel );
    447451        GuardAction( [this]() { enclosing_control_structures.pop_back(); } );
    448452
    449453        // Collect valid labels for fallthrough. It starts with all labels at
    450         // this level, then remove as each is seen during traversal.
    451         for ( const Stmt * stmt : stmt->stmts ) {
    452                 auto * caseStmt = strict_dynamic_cast< const CaseStmt * >( stmt );
     454        // this level, then removed as we see them in traversal.
     455        for ( const ast::Stmt * stmt : stmt->stmts ) {
     456                auto * caseStmt = strict_dynamic_cast< const ast::CaseStmt * >( stmt );
    453457                if ( caseStmt->stmts.empty() ) continue;
    454                 auto block = caseStmt->stmts.front().strict_as<CompoundStmt>();
    455                 for ( const Stmt * stmt : block->kids ) {
    456                         for ( const Label & l : stmt->labels ) {
     458                auto block = caseStmt->stmts.front().strict_as<ast::CompoundStmt>();
     459                for ( const ast::Stmt * stmt : block->kids ) {
     460                        for ( const ast::Label & l : stmt->labels ) {
    457461                                fallthrough_labels.insert( l );
    458462                        }
     
    461465}
    462466
    463 const SwitchStmt * MultiLevelExitCore::postvisit( const SwitchStmt * stmt ) {
     467const ast::SwitchStmt * MultiLevelExitCore::postvisit( const ast::SwitchStmt * stmt ) {
    464468        assert( !enclosing_control_structures.empty() );
    465469        Entry & entry = enclosing_control_structures.back();
    466470        assert( entry.stmt == stmt );
    467471
    468         // Only run to generate the break label.
     472        // Only run if we need to generate the break label.
    469473        if ( entry.isBreakUsed() ) {
    470474                // To keep the switch statements uniform (all direct children of a
    471475                // SwitchStmt should be CastStmts), append the exit label and break
    472476                // to the last case, create a default case is there are no cases.
    473                 SwitchStmt * mutStmt = mutate( stmt );
     477                ast::SwitchStmt * mutStmt = ast::mutate( stmt );
    474478                if ( mutStmt->stmts.empty() ) {
    475                         mutStmt->stmts.push_back( new CaseStmt(
    476                                                                                   mutStmt->location, nullptr, {} ));
    477                 }
    478 
    479                 auto caseStmt = mutStmt->stmts.back().strict_as<CaseStmt>();
    480                 auto mutCase = mutate( caseStmt );
     479                        mutStmt->stmts.push_back( new ast::CaseStmt(
     480                                mutStmt->location, nullptr, {} ));
     481                }
     482
     483                auto caseStmt = mutStmt->stmts.back().strict_as<ast::CaseStmt>();
     484                auto mutCase = ast::mutate( caseStmt );
    481485                mutStmt->stmts.back() = mutCase;
    482486
    483                 Label label( mutCase->location, "breakLabel" );
    484                 auto branch = new BranchStmt( mutCase->location, BranchStmt::Break, label );
     487                ast::Label label( mutCase->location, "breakLabel" );
     488                auto branch = new ast::BranchStmt( mutCase->location, ast::BranchStmt::Break, label );
    485489                branch->labels.push_back( entry.useBreakExit() );
    486490                mutCase->stmts.push_back( branch );
     
    491495}
    492496
    493 void MultiLevelExitCore::previsit( const ReturnStmt * stmt ) {
     497void MultiLevelExitCore::previsit( const ast::ReturnStmt * stmt ) {
    494498        if ( inFinally ) {
    495499                SemanticError( stmt->location, "'return' may not appear in a finally clause" );
     
    497501}
    498502
    499 void MultiLevelExitCore::previsit( const TryStmt * stmt ) {
     503void MultiLevelExitCore::previsit( const ast::TryStmt * stmt ) {
    500504        bool isLabeled = !stmt->labels.empty();
    501505        if ( isLabeled ) {
    502                 Label breakLabel = newLabel( "blockBreak", stmt );
     506                ast::Label breakLabel = LabelGenerator::newLabel( "blockBreak", stmt );
    503507                enclosing_control_structures.emplace_back( stmt, breakLabel );
    504508                GuardAction([this](){ enclosing_control_structures.pop_back(); } );
     
    506510}
    507511
    508 void MultiLevelExitCore::postvisit( const TryStmt * stmt ) {
     512void MultiLevelExitCore::postvisit( const ast::TryStmt * stmt ) {
    509513        bool isLabeled = !stmt->labels.empty();
    510514        if ( isLabeled ) {
     
    516520}
    517521
    518 void MultiLevelExitCore::previsit( const FinallyStmt * ) {
    519         GuardAction([this, old = move(enclosing_control_structures)](){
    520                                         enclosing_control_structures = move(old);
    521                                 });
    522         enclosing_control_structures = vector<Entry>();
     522void MultiLevelExitCore::previsit( const ast::FinallyStmt * ) {
     523        GuardAction([this, old = std::move(enclosing_control_structures)](){
     524                enclosing_control_structures = std::move(old);
     525        });
     526        enclosing_control_structures = std::vector<Entry>();
    523527        GuardValue( inFinally ) = true;
    524528}
    525529
    526 const Stmt * MultiLevelExitCore::mutateLoop(
    527         const Stmt * body, Entry & entry ) {
     530const ast::Stmt * MultiLevelExitCore::mutateLoop(
     531                const ast::Stmt * body, Entry & entry ) {
    528532        if ( entry.isBreakUsed() ) {
    529533                break_label = entry.useBreakExit();
     
    531535
    532536        if ( entry.isContUsed() ) {
    533                 CompoundStmt * new_body = new CompoundStmt( body->location );
     537                ast::CompoundStmt * new_body = new ast::CompoundStmt( body->location );
    534538                new_body->kids.push_back( body );
    535539                new_body->kids.push_back(
     
    545549        // Remember is loop before going onto mutate the body.
    546550        // The labels will be folded in if they are used.
    547         Label breakLabel = newLabel( "loopBreak", loopStmt );
    548         Label contLabel = newLabel( "loopContinue", loopStmt );
     551        ast::Label breakLabel = LabelGenerator::newLabel( "loopBreak", loopStmt );
     552        ast::Label contLabel = LabelGenerator::newLabel( "loopContinue", loopStmt );
    549553        enclosing_control_structures.emplace_back( loopStmt, breakLabel, contLabel );
    550554        GuardAction( [this](){ enclosing_control_structures.pop_back(); } );
     
    557561        assert( entry.stmt == loopStmt );
    558562
    559         // Now check if the labels are used and add them if so.
    560         return mutate_field(
     563        // Now we check if the labels are used and add them if so.
     564        return ast::mutate_field(
    561565                loopStmt, &LoopNode::body, mutateLoop( loopStmt->body, entry ) );
    562566}
    563567
    564 list<ptr<Stmt>> MultiLevelExitCore::fixBlock(
    565         const list<ptr<Stmt>> & kids, bool is_case_clause ) {
    566         // Unfortunately cannot use automatic error collection.
     568std::list<ast::ptr<ast::Stmt>> MultiLevelExitCore::fixBlock(
     569                const std::list<ast::ptr<ast::Stmt>> & kids, bool is_case_clause ) {
     570        // Unfortunately we can't use the automatic error collection.
    567571        SemanticErrorException errors;
    568572
    569         list<ptr<Stmt>> ret;
     573        std::list<ast::ptr<ast::Stmt>> ret;
    570574
    571575        // Manually visit each child.
    572         for ( const ptr<Stmt> & kid : kids ) {
     576        for ( const ast::ptr<ast::Stmt> & kid : kids ) {
    573577                if ( is_case_clause ) {
    574578                        // Once a label is seen, it's no longer a valid for fallthrough.
    575                         for ( const Label & l : kid->labels ) {
     579                        for ( const ast::Label & l : kid->labels ) {
    576580                                fallthrough_labels.erase( l );
    577581                        }
     
    587591                        ret.push_back(
    588592                                labelledNullStmt( ret.back()->location, break_label ) );
    589                         break_label = Label( CodeLocation(), "" );
     593                        break_label = ast::Label( CodeLocation(), "" );
    590594                }
    591595        }
     
    597601}
    598602
    599 const CompoundStmt * multiLevelExitUpdate(
    600         const CompoundStmt * stmt,
    601         const LabelToStmt & labelTable ) {
     603} // namespace
     604
     605const ast::CompoundStmt * multiLevelExitUpdate(
     606        const ast::CompoundStmt * stmt,
     607                const LabelToStmt & labelTable ) {
    602608        // Must start in the body, so FunctionDecls can be a stopping point.
    603         Pass<MultiLevelExitCore> visitor( labelTable );
    604         const CompoundStmt * ret = stmt->accept( visitor );
     609        ast::Pass<MultiLevelExitCore> visitor( labelTable );
     610        const ast::CompoundStmt * ret = stmt->accept( visitor );
    605611        return ret;
    606612}
     613
    607614} // namespace ControlStruct
    608615
  • src/ControlStruct/MultiLevelExit.hpp

    r376c632a rf681823  
    99// Author           : Andrew Beach
    1010// Created On       : Mon Nov  1 13:49:00 2021
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jan 31 22:34:06 2022
    13 // Update Count     : 6
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Mon Nov  8 10:53:00 2021
     13// Update Count     : 3
    1414//
    1515
     
    1919
    2020namespace ast {
    21 class CompoundStmt;
    22 class Label;
    23 class Stmt;
     21        class CompoundStmt;
     22        class Label;
     23        class Stmt;
    2424}
    2525
    2626namespace ControlStruct {
     27
    2728using LabelToStmt = std::map<ast::Label, const ast::Stmt *>;
    2829
    29 // Mutate a function body to handle multi-level exits.
    30 const ast::CompoundStmt * multiLevelExitUpdate( const ast::CompoundStmt *, const LabelToStmt & );
     30/// Mutate a function body to handle multi-level exits.
     31const ast::CompoundStmt * multiLevelExitUpdate(
     32        const ast::CompoundStmt *, const LabelToStmt & );
     33
    3134}
    3235
  • src/ControlStruct/module.mk

    r376c632a rf681823  
    1010## Author           : Richard C. Bilson
    1111## Created On       : Mon Jun  1 17:49:17 2015
    12 ## Last Modified By : Peter A. Buhr
    13 ## Last Modified On : Sat Jan 29 12:04:19 2022
    14 ## Update Count     : 7
     12## Last Modified By : Henry Xue
     13## Last Modified On : Tue Jul 20 04:10:50 2021
     14## Update Count     : 5
    1515###############################################################################
    1616
     
    2222        ControlStruct/ForExprMutator.cc \
    2323        ControlStruct/ForExprMutator.h \
    24         ControlStruct/HoistControlDecls.cpp \
    25         ControlStruct/HoistControlDecls.hpp \
    2624        ControlStruct/LabelFixer.cc \
    2725        ControlStruct/LabelFixer.h \
    2826        ControlStruct/LabelGenerator.cc \
    2927        ControlStruct/LabelGenerator.h \
    30         ControlStruct/LabelGeneratorNew.cpp \
    31         ControlStruct/LabelGeneratorNew.hpp \
    3228        ControlStruct/MLEMutator.cc \
    3329        ControlStruct/MLEMutator.h \
  • src/InitTweak/InitTweak.cc

    r376c632a rf681823  
    1010// Created On       : Fri May 13 11:26:36 2016
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Mon Dec  6 13:21:00 2021
    13 // Update Count     : 20
     12// Last Modified On : Fri Nov 19 19:22:00 2021
     13// Update Count     : 19
    1414//
    1515
     
    11911191        }
    11921192
    1193 bool isAssignment( const ast::FunctionDecl * decl ) {
    1194         return isAssignment( decl->name ) && isCopyFunction( decl );
    1195 }
    1196 
    1197 bool isDestructor( const ast::FunctionDecl * decl ) {
    1198         return isDestructor( decl->name );
    1199 }
    1200 
    1201 bool isDefaultConstructor( const ast::FunctionDecl * decl ) {
    1202         return isConstructor( decl->name ) && 1 == decl->params.size();
    1203 }
    1204 
    1205 bool isCopyConstructor( const ast::FunctionDecl * decl ) {
    1206         return isConstructor( decl->name ) && 2 == decl->params.size();
    1207 }
    1208 
    1209 bool isCopyFunction( const ast::FunctionDecl * decl ) {
    1210         const ast::FunctionType * ftype = decl->type;
    1211         if ( ftype->params.size() != 2 ) return false;
    1212 
    1213         const ast::Type * t1 = getPointerBase( ftype->params.front() );
    1214         if ( ! t1 ) return false;
    1215         const ast::Type * t2 = ftype->params.back();
    1216 
    1217         return ResolvExpr::typesCompatibleIgnoreQualifiers( t1, t2, ast::SymbolTable{} );
    1218 }
     1193        bool isCopyFunction( const ast::FunctionDecl * decl ) {
     1194                const ast::FunctionType * ftype = decl->type;
     1195                if ( ftype->params.size() != 2 ) return false;
     1196
     1197                const ast::Type * t1 = getPointerBase( ftype->params.front() );
     1198                if ( ! t1 ) return false;
     1199                const ast::Type * t2 = ftype->params.back();
     1200
     1201                return ResolvExpr::typesCompatibleIgnoreQualifiers( t1, t2, ast::SymbolTable{} );
     1202        }
    12191203
    12201204        const FunctionDecl * isAssignment( const Declaration * decl ) {
  • src/InitTweak/InitTweak.h

    r376c632a rf681823  
    1010// Created On       : Fri May 13 11:26:36 2016
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Mon Dec  6 13:20:00 2021
    13 // Update Count     : 8
     12// Last Modified On : Fri Nov 19 14:18:00 2021
     13// Update Count     : 7
    1414//
    1515
     
    3131        const FunctionDecl * isCopyConstructor( const Declaration * decl );
    3232        const FunctionDecl * isCopyFunction( const Declaration * decl, const std::string & fname );
    33         bool isAssignment( const ast::FunctionDecl * decl );
    34         bool isDestructor( const ast::FunctionDecl * decl );
    35         bool isDefaultConstructor( const ast::FunctionDecl * decl );
    36         bool isCopyConstructor( const ast::FunctionDecl * decl );
    3733        bool isCopyFunction( const ast::FunctionDecl * decl );
    3834
  • src/Parser/ParseNode.h

    r376c632a rf681823  
    1010// Created On       : Sat May 16 13:28:16 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Jan 29 09:45:56 2022
    13 // Update Count     : 901
     12// Last Modified On : Wed Jul 14 17:28:53 2021
     13// Update Count     : 900
    1414//
    1515
     
    390390Statement * build_expr( ExpressionNode * ctl );
    391391
    392 struct CondCtl {
    393         CondCtl( DeclarationNode * decl, ExpressionNode * condition ) :
     392struct IfCtrl {
     393        IfCtrl( DeclarationNode * decl, ExpressionNode * condition ) :
    394394                init( decl ? new StatementNode( decl ) : nullptr ), condition( condition ) {}
    395395
     
    409409};
    410410
    411 Expression * build_if_control( CondCtl * ctl, std::list< Statement * > & init );
    412 Statement * build_if( CondCtl * ctl, StatementNode * then_stmt, StatementNode * else_stmt );
     411Expression * build_if_control( IfCtrl * ctl, std::list< Statement * > & init );
     412Statement * build_if( IfCtrl * ctl, StatementNode * then_stmt, StatementNode * else_stmt );
    413413Statement * build_switch( bool isSwitch, ExpressionNode * ctl, StatementNode * stmt );
    414414Statement * build_case( ExpressionNode * ctl );
    415415Statement * build_default();
    416 Statement * build_while( CondCtl * ctl, StatementNode * stmt );
     416Statement * build_while( IfCtrl * ctl, StatementNode * stmt );
    417417Statement * build_do_while( ExpressionNode * ctl, StatementNode * stmt );
    418418Statement * build_for( ForCtrl * forctl, StatementNode * stmt );
  • src/Parser/StatementNode.cc

    r376c632a rf681823  
    1010// Created On       : Sat May 16 14:59:41 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Jan 29 09:45:51 2022
    13 // Update Count     : 384
     12// Last Modified On : Sat Oct 24 04:20:55 2020
     13// Update Count     : 383
    1414//
    1515
     
    7878} // build_expr
    7979
    80 Expression * build_if_control( CondCtl * ctl, std::list< Statement * > & init ) {
     80Expression * build_if_control( IfCtrl * ctl, std::list< Statement * > & init ) {
    8181        if ( ctl->init != 0 ) {
    8282                buildMoveList( ctl->init, init );
     
    100100} // build_if_control
    101101
    102 Statement * build_if( CondCtl * ctl, StatementNode * then_stmt, StatementNode * else_stmt ) {
     102Statement * build_if( IfCtrl * ctl, StatementNode * then_stmt, StatementNode * else_stmt ) {
    103103        Statement * thenb, * elseb = nullptr;
    104104        std::list< Statement * > branches;
     
    145145} // build_default
    146146
    147 Statement * build_while( CondCtl * ctl, StatementNode * stmt ) {
     147Statement * build_while( IfCtrl * ctl, StatementNode * stmt ) {
    148148        std::list< Statement * > branches;
    149149        buildMoveList< Statement, StatementNode >( stmt, branches );
  • src/Parser/parser.yy

    r376c632a rf681823  
    1010// Created On       : Sat Sep  1 20:22:55 2001
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Jan 30 09:41:13 2022
    13 // Update Count     : 5165
     12// Last Modified On : Fri Oct 15 09:20:17 2021
     13// Update Count     : 5163
    1414//
    1515
     
    238238        WaitForStmt * wfs;
    239239        Expression * constant;
    240         CondCtl * ifctl;
     240        IfCtrl * ifctl;
    241241        ForCtrl * fctl;
    242242        enum OperKinds compop;
     
    327327%type<en> comma_expression                              comma_expression_opt
    328328%type<en> argument_expression_list_opt  argument_expression_list        argument_expression                     default_initializer_opt
    329 %type<ifctl> conditional_declaration
     329%type<ifctl> if_control_expression
    330330%type<fctl> for_control_expression              for_control_expression_list
    331331%type<compop> inclexcl
     
    11231123
    11241124if_statement:
    1125         IF '(' conditional_declaration ')' statement            %prec THEN
     1125        IF '(' if_control_expression ')' statement                      %prec THEN
    11261126                // explicitly deal with the shift/reduce conflict on if/else
    11271127                { $$ = new StatementNode( build_if( $3, maybe_build_compound( $5 ), nullptr ) ); }
    1128         | IF '(' conditional_declaration ')' statement ELSE statement
     1128        | IF '(' if_control_expression ')' statement ELSE statement
    11291129                { $$ = new StatementNode( build_if( $3, maybe_build_compound( $5 ), maybe_build_compound( $7 ) ) ); }
    11301130        ;
    11311131
    1132 conditional_declaration:
     1132if_control_expression:
    11331133        comma_expression
    1134                 { $$ = new CondCtl( nullptr, $1 ); }
     1134                { $$ = new IfCtrl( nullptr, $1 ); }
    11351135        | c_declaration                                                                         // no semi-colon
    1136                 { $$ = new CondCtl( $1, nullptr ); }
     1136                { $$ = new IfCtrl( $1, nullptr ); }
    11371137        | cfa_declaration                                                                       // no semi-colon
    1138                 { $$ = new CondCtl( $1, nullptr ); }
     1138                { $$ = new IfCtrl( $1, nullptr ); }
    11391139        | declaration comma_expression                                          // semi-colon separated
    1140                 { $$ = new CondCtl( $1, $2 ); }
     1140                { $$ = new IfCtrl( $1, $2 ); }
    11411141        ;
    11421142
     
    11931193iteration_statement:
    11941194        WHILE '(' ')' statement                                                         // CFA => while ( 1 )
    1195                 { $$ = new StatementNode( build_while( new CondCtl( nullptr, new ExpressionNode( build_constantInteger( *new string( "1" ) ) ) ), maybe_build_compound( $4 ) ) ); }
    1196         | WHILE '(' conditional_declaration ')' statement       %prec THEN
     1195                { $$ = new StatementNode( build_while( new IfCtrl( nullptr, new ExpressionNode( build_constantInteger( *new string( "1" ) ) ) ), maybe_build_compound( $4 ) ) ); }
     1196        | WHILE '(' if_control_expression ')' statement         %prec THEN
    11971197                { $$ = new StatementNode( build_while( $3, maybe_build_compound( $5 ) ) ); }
    1198         | WHILE '(' conditional_declaration ')' statement ELSE statement // CFA
     1198        | WHILE '(' if_control_expression ')' statement ELSE statement // CFA
    11991199                { SemanticError( yylloc, "Loop default block is currently unimplemented." ); $$ = nullptr; }
    12001200        | DO statement WHILE '(' ')' ';'                                        // CFA => do while( 1 )
  • src/SymTab/Validate.cc

    r376c632a rf681823  
    453453        }
    454454
    455         void decayForallPointers( std::list< Declaration * > & translationUnit ) {
    456                 PassVisitor<ForallPointerDecay_old> fpd;
    457                 acceptAll( translationUnit, fpd );
    458         }
    459 
    460455        void validate( std::list< Declaration * > &translationUnit, __attribute__((unused)) bool doDebug ) {
    461456                validate_A( translationUnit );
     
    475470                type->accept( fpd );
    476471        }
     472
    477473
    478474        void HoistTypeDecls::handleType( Type * type ) {
  • src/SymTab/Validate.h

    r376c632a rf681823  
    4242        void validate_E( std::list< Declaration * > &translationUnit );
    4343        void validate_F( std::list< Declaration * > &translationUnit );
    44         void decayForallPointers( std::list< Declaration * > & translationUnit );
    4544
    4645        const ast::Type * validateType(
  • src/Validate/module.mk

    r376c632a rf681823  
    1616
    1717SRC_VALIDATE = \
    18         Validate/Autogen.cpp \
    19         Validate/Autogen.hpp \
    2018        Validate/CompoundLiteral.cpp \
    2119        Validate/CompoundLiteral.hpp \
  • src/main.cc

    r376c632a rf681823  
    1010// Created On       : Fri May 15 23:12:02 2015
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Wed Jan 26 14:09:00 2022
    13 // Update Count     : 670
     12// Last Modified On : Tue Nov 30 10:25:00 2021
     13// Update Count     : 659
    1414//
    1515
     
    5555#include "ControlStruct/ExceptTranslate.h"  // for translateEHM
    5656#include "ControlStruct/FixLabels.hpp"      // for fixLabels
    57 #include "ControlStruct/HoistControlDecls.hpp" //  hoistControlDecls
    5857#include "ControlStruct/Mutate.h"           // for mutate
    5958#include "GenPoly/Box.h"                    // for box
     
    7473#include "SynTree/Visitor.h"                // for acceptAll
    7574#include "Tuples/Tuples.h"                  // for expandMemberTuples, expan...
    76 #include "Validate/Autogen.hpp"             // for autogenerateRoutines
    7775#include "Validate/FindSpecialDecls.h"      // for findGlobalDecls
    7876#include "Validate/CompoundLiteral.hpp"     // for handleCompoundLiterals
     
    8078#include "Validate/LabelAddressFixer.hpp"   // for fixLabelAddresses
    8179#include "Virtual/ExpandCasts.h"            // for expandCasts
     80
    8281
    8382static void NewPass( const char * const name ) {
     
    327326                PASS( "Validate-B", SymTab::validate_B( translationUnit ) );
    328327                PASS( "Validate-C", SymTab::validate_C( translationUnit ) );
     328                PASS( "Validate-D", SymTab::validate_D( translationUnit ) );
    329329
    330330                CodeTools::fillLocations( translationUnit );
    331331
    332332                if( useNewAST ) {
    333                         PASS( "Apply Concurrent Keywords", Concurrency::applyKeywords( translationUnit ) );
    334                         PASS( "Forall Pointer Decay", SymTab::decayForallPointers( translationUnit ) );
    335                         CodeTools::fillLocations( translationUnit );
    336 
    337333                        if (Stats::Counters::enabled) {
    338334                                ast::pass_visitor_stats.avg = Stats::Counters::build<Stats::Counters::AverageCounter<double>>("Average Depth - New");
     
    342338
    343339                        forceFillCodeLocations( transUnit );
    344 
    345                         // Must happen before autogen routines are added.
    346                         PASS( "Hoist Control Declarations", ControlStruct::hoistControlDecls( transUnit ) );
    347 
    348                         // Must be after enum and pointer decay.
    349                         // Must be before compound literals.
    350                         PASS( "Generate Autogen Routines", Validate::autogenerateRoutines( transUnit ) );
    351340
    352341                        PASS( "Implement Mutex", Concurrency::implementMutex( transUnit ) );
     
    417406                        translationUnit = convert( move( transUnit ) );
    418407                } else {
    419                         PASS( "Validate-D", SymTab::validate_D( translationUnit ) );
    420408                        PASS( "Validate-E", SymTab::validate_E( translationUnit ) );
    421409                        PASS( "Validate-F", SymTab::validate_F( translationUnit ) );
  • tests/concurrent/preempt.cfa

    r376c632a rf681823  
    1 #include <clock.hfa>
    21#include <fstream.hfa>
    32#include <kernel.hfa>
     
    1918#else
    2019static const unsigned long N = 500ul;
    21 static volatile Time prev;
    22 static Duration preempt_durations[6] = { 0 };
    2320#endif
    2421
     
    2926thread worker_t {
    3027        int value;
    31         unsigned long long spin;
     28        unsigned spin;
    3229};
    3330
     
    3936void main(worker_t & this) {
    4037        while(TEST(counter < N)) {
    41                 if(this.spin > 50_000_000_000) abort | "Worker" | this.value | "has been spinning too long! (" | this.spin | ")";
     38                if(this.spin > 100_000_000) abort | "Worker" | this.value | "has been spinning too long! (" | this.spin | ")";
    4239                __cfaabi_check_preemption();
    4340                if( (counter % 7) == this.value ) {
    4441                        __cfaabi_check_preemption();
    45                         #if !defined(TEST_LONG)
    46                                 Time now = timeHiRes();
    47                                 Duration diff = now - prev;
    48                                 prev = now;
    49                         #endif
    5042                        int next = __atomic_add_fetch( &counter, 1, __ATOMIC_SEQ_CST );
    5143                        __cfaabi_check_preemption();
    52                         if( (next % 100) == 0 ) {
    53                                 #if !defined(TEST_LONG)
    54                                         unsigned idx = next / 100;
    55                                         if (idx >= 6) abort | "Idx from next is invalid: " | idx | "vs" | next;
    56                                         preempt_durations[idx] = diff;
    57                                         if(diff > 12`s) serr | "Duration suspiciously large:" | diff;
    58                                 #endif
    59                                 printf("%d\n", (int)next);
    60 
    61                         }
     44                        if( (next % 100) == 0 ) printf("%d\n", (int)next);
    6245                        __cfaabi_check_preemption();
    6346                        this.spin = 0;
     
    7255        processor p;
    7356        {
    74                 prev = timeHiRes();
    7557                worker_t w0 = 0;
    7658                worker_t w1 = 1;
Note: See TracChangeset for help on using the changeset viewer.