Changeset 89a5a1f


Ignore:
Timestamp:
Jan 31, 2022, 10:40:24 PM (23 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, enum, forall-pointer-decay, master, pthread-emulation, qualifiedEnum
Children:
376c632a
Parents:
7b38b28
Message:

formatting, starting adding elsePart to while, do, and for statements

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Stmt.hpp

    r7b38b28 r89a5a1f  
    99// Author           : Aaron B. Moss
    1010// Created On       : Wed May  8 13:00:00 2019
    11 // Last Modified By : Andrew Beach
    12 // Last Modified On : Fri May 17 12:45:00 2019
    13 // Update Count     : 5
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Mon Jan 31 22:38:53 2022
     13// Update Count     : 12
    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 
    3534class Expr;
    3635
    37 /// Base statement node
     36// Base statement node
    3837class Stmt : public ParseNode {
    39 public:
     38  public:
    4039        std::vector<Label> labels;
    4140
    4241        Stmt( const CodeLocation & loc, std::vector<Label> && labels = {} )
    43         : ParseNode(loc), labels(std::move(labels)) {}
     42                : ParseNode(loc), labels(std::move(labels)) {}
    4443
    4544        Stmt(const Stmt& o) : ParseNode(o), labels(o.labels) {}
    4645
    4746        const Stmt * accept( Visitor & v ) const override = 0;
    48 private:
     47  private:
    4948        Stmt * clone() const override = 0;
    5049        MUTATE_FRIEND
    5150};
    5251
    53 /// Compound statement `{ ... }`
     52// Compound statement: { ... }
    5453class CompoundStmt final : public Stmt {
    55 public:
     54  public:
    5655        std::list<ptr<Stmt>> kids;
    5756
    5857        CompoundStmt(const CodeLocation & loc, std::list<ptr<Stmt>> && ks = {},
    59                 std::vector<Label>&& labels = {} )
    60         : Stmt(loc, std::move(labels)), kids(std::move(ks)) {}
     58                                 std::vector<Label>&& labels = {} )
     59                : Stmt(loc, std::move(labels)), kids(std::move(ks)) {}
    6160
    6261        CompoundStmt( const CompoundStmt& o );
     
    6766
    6867        const CompoundStmt * accept( Visitor & v ) const override { return v.visit( this ); }
    69 private:
     68  private:
    7069        CompoundStmt * clone() const override { return new CompoundStmt{ *this }; }
    7170        MUTATE_FRIEND
    7271};
    7372
    74 /// Empty statment `;`
     73// Empty statment: ;
    7574class NullStmt final : public Stmt {
    76 public:
     75  public:
    7776        NullStmt( const CodeLocation & loc, std::vector<Label> && labels = {} )
    78         : Stmt(loc, std::move(labels)) {}
     77                : Stmt(loc, std::move(labels)) {}
    7978
    8079        const NullStmt * accept( Visitor & v ) const override { return v.visit( this ); }
    81 private:
     80  private:
    8281        NullStmt * clone() const override { return new NullStmt{ *this }; }
    8382        MUTATE_FRIEND
    8483};
    8584
    86 /// Expression wrapped by statement
     85// Expression wrapped by statement
    8786class ExprStmt final : public Stmt {
    88 public:
     87  public:
    8988        ptr<Expr> expr;
    9089
    9190        ExprStmt( const CodeLocation& loc, const Expr* e, std::vector<Label>&& labels = {} )
    92         : Stmt(loc, std::move(labels)), expr(e) {}
    93 
    94         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    95 private:
     91                : Stmt(loc, std::move(labels)), expr(e) {}
     92
     93        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     94  private:
    9695        ExprStmt * clone() const override { return new ExprStmt{ *this }; }
    9796        MUTATE_FRIEND
    9897};
    9998
    100 /// Assembly statement `asm ... ( "..." : ... )`
     99// Assembly statement: asm ... ( "..." : ... )
    101100class AsmStmt final : public Stmt {
    102 public:
     101  public:
    103102        bool isVolatile;
    104103        ptr<Expr> instruction;
     
    108107
    109108        AsmStmt( const CodeLocation & loc, bool isVolatile, const Expr * instruction,
    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 ); }
    118 private:
     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:
    119118        AsmStmt * clone() const override { return new AsmStmt{ *this }; }
    120119        MUTATE_FRIEND
    121120};
    122121
    123 /// C-preprocessor directive `#...`
     122// C-preprocessor directive: #...
    124123class DirectiveStmt final : public Stmt {
    125 public:
     124  public:
    126125        std::string directive;
    127126
    128127        DirectiveStmt( const CodeLocation & loc, const std::string & directive,
    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 ); }
    133 private:
     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:
    134133        DirectiveStmt * clone() const override { return new DirectiveStmt{ *this }; }
    135134        MUTATE_FRIEND
    136135};
    137136
    138 /// If conditional statement `if (...) ... else ...`
     137// If statement: if (...) ... else ...
    139138class IfStmt final : public Stmt {
    140 public:
     139  public:
    141140        ptr<Expr> cond;
    142141        ptr<Stmt> thenPart;
     
    145144
    146145        IfStmt( const CodeLocation & loc, const Expr * cond, const Stmt * thenPart,
    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 ); }
    153 private:
     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:
    154153        IfStmt * clone() const override { return new IfStmt{ *this }; }
    155154        MUTATE_FRIEND
    156155};
    157156
    158 /// Switch or choose conditional statement `switch (...) { ... }`
     157// Switch or choose statement: switch (...) { ... }
    159158class SwitchStmt final : public Stmt {
    160 public:
     159  public:
    161160        ptr<Expr> cond;
    162161        std::vector<ptr<Stmt>> stmts;
    163162
    164163        SwitchStmt( const CodeLocation & loc, const Expr * cond, std::vector<ptr<Stmt>> && stmts,
    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 ); }
    169 private:
     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:
    170169        SwitchStmt * clone() const override { return new SwitchStmt{ *this }; }
    171170        MUTATE_FRIEND
    172171};
    173172
    174 /// Case label `case ...:` `default:`
     173// Case label: case ...: or default:
    175174class CaseStmt final : public Stmt {
    176 public:
    177         /// Null for the default label.
     175  public:
     176        // Null for the default label.
    178177        ptr<Expr> cond;
    179178        std::vector<ptr<Stmt>> stmts;
    180179
    181180        CaseStmt( const CodeLocation & loc, const Expr * cond, std::vector<ptr<Stmt>> && stmts,
    182                 std::vector<Label> && labels = {} )
    183         : Stmt(loc, std::move(labels)), cond(cond), stmts(std::move(stmts)) {}
     181                          std::vector<Label> && labels = {} )
     182                : Stmt(loc, std::move(labels)), cond(cond), stmts(std::move(stmts)) {}
    184183
    185184        bool isDefault() const { return !cond; }
    186185
    187186        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    188 private:
     187  private:
    189188        CaseStmt * clone() const override { return new CaseStmt{ *this }; }
    190189        MUTATE_FRIEND
    191190};
    192191
    193 /// While loop `while (...) ...` `do ... while (...);
     192// While loop: while (...) ... else ... or do ... while (...) else ...;
    194193class WhileStmt final : public Stmt {
    195 public:
     194  public:
    196195        ptr<Expr> cond;
    197196        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)),
    204           isDoWhile(isDoWhile) {}
    205 
    206         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    207 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)), isDoWhile(isDoWhile) {}
     204
     205        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     206  private:
    208207        WhileStmt * clone() const override { return new WhileStmt{ *this }; }
    209208        MUTATE_FRIEND
    210209};
    211210
    212 /// For loop `for (... ; ... ; ...) ...`
     211// For loop: for (... ; ... ; ...) ... else ...
    213212class ForStmt final : public Stmt {
    214 public:
     213  public:
    215214        std::vector<ptr<Stmt>> inits;
    216215        ptr<Expr> cond;
    217216        ptr<Expr> inc;
    218217        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),
    223           body(body) {}
    224 
    225         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    226 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), body(body) {}
     223
     224        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     225  private:
    227226        ForStmt * clone() const override { return new ForStmt{ *this }; }
    228227        MUTATE_FRIEND
    229228};
    230229
    231 /// Branch control flow statement `goto ...` `break` `continue` `fallthru`
     230// Branch control flow statement: goto ... or break or continue or fallthru
    232231class BranchStmt final : public Stmt {
    233 public:
     232  public:
    234233        enum Kind { Goto, Break, Continue, FallThrough, FallThroughDefault };
    235234        static constexpr size_t kindEnd = 1 + (size_t)FallThroughDefault;
     
    241240
    242241        BranchStmt( const CodeLocation & loc, Kind kind, Label target,
    243                 std::vector<Label> && labels = {} );
     242                                std::vector<Label> && labels = {} );
    244243        BranchStmt( const CodeLocation & loc, const Expr * computedTarget,
    245                 std::vector<Label> && labels = {} )
    246         : Stmt(loc, std::move(labels)), originalTarget(loc), target(loc),
    247           computedTarget(computedTarget), kind(Goto) {}
     244                                std::vector<Label> && labels = {} )
     245                : Stmt(loc, std::move(labels)), originalTarget(loc), target(loc),
     246                  computedTarget(computedTarget), kind(Goto) {}
    248247
    249248        const char * kindName() const { return kindNames[kind]; }
    250249
    251250        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    252 private:
     251  private:
    253252        BranchStmt * clone() const override { return new BranchStmt{ *this }; }
    254253        MUTATE_FRIEND
     
    257256};
    258257
    259 /// Return statement `return ...`
     258// Return statement: return ...
    260259class ReturnStmt final : public Stmt {
    261 public:
     260  public:
    262261        ptr<Expr> expr;
    263262
    264263        ReturnStmt( const CodeLocation & loc, const Expr * expr, std::vector<Label> && labels = {} )
    265         : Stmt(loc, std::move(labels)), expr(expr) {}
    266 
    267         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    268 private:
     264                : Stmt(loc, std::move(labels)), expr(expr) {}
     265
     266        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     267  private:
    269268        ReturnStmt * clone() const override { return new ReturnStmt{ *this }; }
    270269        MUTATE_FRIEND
    271270};
    272271
    273 /// Kind of exception
     272// Kind of exception
    274273enum ExceptionKind { Terminate, Resume };
    275274
    276 /// Throw statement `throw ...`
     275// Throw statement: throw ...
    277276class ThrowStmt final : public Stmt {
    278 public:
     277  public:
    279278        ptr<Expr> expr;
    280279        ptr<Expr> target;
     
    284283                const CodeLocation & loc, ExceptionKind kind, const Expr * expr, const Expr * target,
    285284                std::vector<Label> && labels = {} )
    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 ); }
    289 private:
     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:
    290289        ThrowStmt * clone() const override { return new ThrowStmt{ *this }; }
    291290        MUTATE_FRIEND
    292291};
    293292
    294 /// Try statement `try { ... } ...`
     293// Try statement: try { ... } ...
    295294class TryStmt final : public Stmt {
    296 public:
     295  public:
    297296        ptr<CompoundStmt> body;
    298297        std::vector<ptr<CatchStmt>> handlers;
     
    303302                std::vector<ptr<CatchStmt>> && handlers, const FinallyStmt * finally,
    304303                std::vector<Label> && labels = {} )
    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 ); }
    308 private:
     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:
    309308        TryStmt * clone() const override { return new TryStmt{ *this }; }
    310309        MUTATE_FRIEND
    311310};
    312311
    313 /// Catch clause of try statement
     312// Catch clause of try statement
    314313class CatchStmt final : public Stmt {
    315 public:
     314  public:
    316315        ptr<Decl> decl;
    317316        ptr<Expr> cond;
     
    322321                const CodeLocation & loc, ExceptionKind kind, const Decl * decl, const Expr * cond,
    323322                const Stmt * body, std::vector<Label> && labels = {} )
    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 ); }
    327 private:
     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:
    328327        CatchStmt * clone() const override { return new CatchStmt{ *this }; }
    329328        MUTATE_FRIEND
    330329};
    331330
    332 /// Finally clause of try statement
     331// Finally clause of try statement
    333332class FinallyStmt final : public Stmt {
    334 public:
     333  public:
    335334        ptr<CompoundStmt> body;
    336335
    337336        FinallyStmt( const CodeLocation & loc, const CompoundStmt * body,
    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 ); }
    342 private:
     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:
    343342        FinallyStmt * clone() const override { return new FinallyStmt{ *this }; }
    344343        MUTATE_FRIEND
    345344};
    346345
    347 /// Suspend statement
     346// Suspend statement
    348347class SuspendStmt final : public Stmt {
    349 public:
     348  public:
    350349        ptr<CompoundStmt> then;
    351350        enum Type { None, Coroutine, Generator } type = None;
    352351
    353352        SuspendStmt( const CodeLocation & loc, const CompoundStmt * then, Type type, std::vector<Label> && labels = {} )
    354         : Stmt(loc, std::move(labels)), then(then), type(type) {}
    355 
    356         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    357 private:
     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:
    358357        SuspendStmt * clone() const override { return new SuspendStmt{ *this }; }
    359358        MUTATE_FRIEND
    360359};
    361360
    362 /// Wait for concurrency statement `when (...) waitfor (... , ...) ... timeout(...) ... else ...`
     361// Waitfor statement: when (...) waitfor (... , ...) ... timeout(...) ... else ...
    363362class WaitForStmt final : public Stmt {
    364 public:
     363  public:
    365364        struct Target {
    366365                ptr<Expr> func;
     
    390389
    391390        WaitForStmt( const CodeLocation & loc, std::vector<Label> && labels = {} )
    392         : Stmt(loc, std::move(labels)) {}
    393 
    394         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    395 private:
     391                : Stmt(loc, std::move(labels)) {}
     392
     393        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     394  private:
    396395        WaitForStmt * clone() const override { return new WaitForStmt{ *this }; }
    397396        MUTATE_FRIEND
    398397};
    399398
    400 /// Any declaration in a (compound) statement.
     399// Any declaration in a (compound) statement.
    401400class DeclStmt final : public Stmt {
    402 public:
     401  public:
    403402        ptr<Decl> decl;
    404403
    405404        DeclStmt( const CodeLocation & loc, const Decl * decl, std::vector<Label> && labels = {} )
    406         : Stmt(loc, std::move(labels)), decl(decl) {}
    407 
    408         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    409 private:
     405                : Stmt(loc, std::move(labels)), decl(decl) {}
     406
     407        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     408  private:
    410409        DeclStmt * clone() const override { return new DeclStmt{ *this }; }
    411410        MUTATE_FRIEND
    412411};
    413412
    414 /// Represents an implicit application of a constructor or destructor.
     413// Represents an implicit application of a constructor or destructor.
    415414class ImplicitCtorDtorStmt final : public Stmt {
    416 public:
     415  public:
    417416        ptr<Stmt> callStmt;
    418417
    419418        ImplicitCtorDtorStmt( const CodeLocation & loc, const Stmt * callStmt,
    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 ); }
    424 private:
     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:
    425424        ImplicitCtorDtorStmt * clone() const override { return new ImplicitCtorDtorStmt{ *this }; }
    426425        MUTATE_FRIEND
    427426};
    428427
    429 /// Mutex Statement
     428// Mutex Statement
    430429class MutexStmt final : public Stmt {
    431 public:
     430  public:
    432431        ptr<Stmt> stmt;
    433432        std::vector<ptr<Expr>> mutexObjs;
    434433
    435434        MutexStmt( const CodeLocation & loc, const Stmt * stmt,
    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 ); }
    440 private:
     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:
    441440        MutexStmt * clone() const override { return new MutexStmt{ *this }; }
    442441        MUTATE_FRIEND
    443442};
    444 
    445 }
     443} // namespace ast
    446444
    447445#undef MUTATE_FRIEND
    448446
    449447// Local Variables: //
    450 // tab-width: 4 //
    451448// mode: c++ //
    452 // compile-command: "make install" //
    453449// End: //
Note: See TracChangeset for help on using the changeset viewer.