Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Stmt.hpp

    r89a5a1f rb8ab91a  
    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: //
Note: See TracChangeset for help on using the changeset viewer.