Changeset 1e97287


Ignore:
Timestamp:
May 15, 2019, 4:04:26 PM (6 years ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
89c2f7c9
Parents:
23f99e1
Message:

Re-created the statement section of the AST.

Location:
src/AST
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Node.hpp

    r23f99e1 r1e97287  
    99// Author           : Thierry Delisle
    1010// Created On       : Wed May 8 10:27:04 2019
    11 // Last Modified By : Aaron B. Moss
    12 // Last Modified On : Wed May 8 11:00:00 2019
    13 // Update Count     : 2
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Wed May 15 16:02:00 2019
     13// Update Count     : 3
    1414//
    1515
     
    137137        const node_t & operator* () const { return *node; }
    138138        explicit operator bool() const { return node; }
    139         operator const node_t * const() const { return node; }
     139        operator const node_t *() const { return node; }
    140140
    141141        template<typename o_node_t>
  • src/AST/Stmt.cpp

    r23f99e1 r1e97287  
    88//
    99// Author           : Aaron B. Moss
    10 // Created On       : Wed May 8 13:00:00 2019
    11 // Last Modified By : Aaron B. Moss
    12 // Last Modified On : Wed May 8 13:00:00 2019
    13 // Update Count     : 1
     10// Created On       : Wed May  8 13:00:00 2019
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Wed May 15 15:53:00 2019
     13// Update Count     : 2
    1414//
    1515
     
    1818#include "DeclReplacer.hpp"
    1919
     20namespace ast {
     21
    2022// --- CompoundStmt
     23CompoundStmt::CompoundStmt( const CompoundStmt& o ) : Stmt(o), kids(o.kids) {
     24        assert(!"implemented");
     25}
    2126
    22 namespace ast {
    23         CompoundStmt::CompoundStmt( const CompoundStmt& o ) : Stmt(o), kids(o.kids) {
    24                 assert(!"implemented");
    25         }
     27// --- BranchStmt
     28BranchStmt( const CodeLocation& loc, Kind kind, Label target, std::vector<Label>&& labels )
     29: Stmt(loc, std::move(labels)), originalTarget(target), target(target), kind(kind) {
     30        // Make sure a syntax error hasn't slipped through.
     31        assert( Goto != kind || !target.empty() );
     32}
     33
     34const char * BranchStmt::kindNames[] = {
     35    "Goto", "Break", "Continue", "FallThrough", "FallThroughDefault"
     36}
     37
    2638}
    2739
  • src/AST/Stmt.hpp

    r23f99e1 r1e97287  
    88//
    99// Author           : Aaron B. Moss
    10 // Created On       : Wed May 8 13:00:00 2019
    11 // Last Modified By : Aaron B. Moss
    12 // Last Modified On : Wed May 8 13:00:00 2019
    13 // Update Count     : 1
     10// Created On       : Wed May  8 13:00:00 2019
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Wed May 15 16:01:00 2019
     13// Update Count     : 2
    1414//
    1515
     
    7070        : Stmt(loc, std::move(labels)) {}
    7171
    72         const NullStmt * accept( Visitor& v ) const override { return v.visit( this ); }
    73 private:
    74         NullStmt * clone() const override { return new NullStmt{ *this }; }
     72        const NullStmt* accept( Visitor& v ) const override { return v.visit( this ); }
     73private:
     74        NullStmt* clone() const override { return new NullStmt{ *this }; }
    7575};
    7676
     
    8080        ptr<Expr> expr;
    8181
    82         ExprStmt( const CodeLocation& loc, Expr* e ) : Stmt(loc), expr(e) {}
     82        ExprStmt( const CodeLocation& loc, const Expr* e ) : Stmt(loc), expr(e) {}
    8383
    8484        const Stmt * accept( Visitor& v ) const override { return v.visit( this ); }
     
    8787};
    8888
    89 
     89class AsmStmt final : public Stmt {
     90public:
     91        bool isVolatile;
     92        ptr<Expr> instruction;
     93        std::vector<ptr<Expr>> output, input;
     94        std::vector<ptr<ConstantExpr>> clobber;
     95        std::vector<Label> gotoLabels;
     96
     97        AsmStmt( const CodeLocation& loc, bool isVolatile, const Expr * instruction,
     98                std::vector<ptr<Expr>>&& output, std::vector<ptr<Expr>>&& input,
     99                std::vector<ptr<ConstantExpr>>&& clobber, std::vector<Label>&& gotoLabels,
     100                std::vector<Label>&& labels = {})
     101        : Stmt(loc, std::move(labels)), isVolatile(isVolatile), instruction(instruction),
     102          output(std::move(output)), input(std::move(input)), clobber(std::move(clobber)),
     103          gotoLabels(std::move(gotoLabels)) {}
     104
     105        const Stmt* accept( Visitor& v ) const override { return v.visit( this ); }
     106private:
     107        AsmStmt* clone() const override { return new AsmStmt{ *this }; }
     108};
     109
     110class DirectiveStmt final : public Stmt {
     111public:
     112        std::string directive;
     113
     114        DirectiveStmt( const CodeLocation& loc, const std::string & directive,
     115                std::vector<Label>&& labels = {} )
     116        : Stmt(loc, std::move(labels)), directive(directive) {}
     117
     118        const Stmt* accept( Visitor& v ) const override { return v.visit( this ); }
     119private:
     120        DirectiveStmt* clone() const override { return new DirectiveStmt{ *this }; }
     121};
     122
     123class IfStmt final : public Stmt {
     124public:
     125        ptr<Expr> cond;
     126        ptr<Stmt> thenPart;
     127        ptr<Stmt> elsePart;
     128        std::vector<ptr<Stmt>> inits;
     129
     130        IfStmt( const CodeLocation& loc, const Expr* cond, const Stmt* thenPart,
     131                Stmt * const elsePart, std::vector<ptr<Stmt>>&& inits,
     132                std::vector<Label>&& labels = {} )
     133        : Stmt(loc, std::move(labels)), cond(cond), thenPart(thenPart), elsePart(elsePart),
     134          inits(std::move(inits)) {}
     135
     136        const Stmt* accept( Visitor& v ) const override { return v.visit( this ); }
     137private:
     138        IfStmt* clone() const override { return new IfStmt{ *this }; }
     139};
     140
     141class SwitchStmt final : public Stmt {
     142public:
     143        ptr<Expr> cond;
     144        std::vector<ptr<Stmt>> stmts;
     145
     146        SwitchStmt( const CodeLocation& loc, const Expr* cond, std::vector<ptr<Stmt>>&& stmts,
     147                std::vector<Label>&& labels = {} )
     148        : Stmt(loc, std::move(labels)), cond(cond), stmts(std::move(stmts)) {}
     149
     150        const Stmt* accept( Visitor& v ) const override { return v.visit( this ); }
     151private:
     152        SwitchStmt* clone() const override { return new SwitchStmt{ *this }; }
     153};
     154
     155class CaseStmt final : public Stmt {
     156public:
     157        ptr<Expr> cond;
     158        std::vector<ptr<Stmt>> stmts;
     159
     160    CaseStmt( const CodeLocation& loc, const Expr* cond, std::vector<ptr<Stmt>>&& stmts,
     161        std::vector<Label>&& labels = {} )
     162    : Stmt(loc, std::move(labels)), cond(cond), stmts(std::move(stmts)) {}
     163
     164        bool isDefault() { return !cond; }
     165
     166        const Stmt* accept( Visitor& v ) const override { return v.visit( this ); }
     167private:
     168        CaseStmt* clone() const override { return new CaseStmt{ *this }; }
     169};
     170
     171class WhileStmt final : public Stmt {
     172public:
     173        ptr<Expr> cond;
     174        ptr<Stmt> body;
     175        std::vector<ptr<Stmt>> inits;
     176        bool isDoWhile;
     177
     178        WhileStmt( const CodeLocation& loc, const Expr* cond, const Stmt* body,
     179                std::vector<ptr<Stmt>>&& inits, bool isDoWhile = false, std::vector<Label>&& labels = {} )
     180        : Stmt(loc, std::move(labels)), cond(cond), body(body), inits(std::move(inits)),
     181          isDoWhile(isDoWhile) {}
     182
     183        const Stmt* accept( Visitor& v ) const override { return v.visit( this ); }
     184private:
     185        WhileStmt* clone() const override { return new WhileStmt{ *this }; }
     186};
     187
     188class ForStmt final : public Stmt {
     189public:
     190        std::vector<ptr<Stmt>> inits;
     191        ptr<Expr> cond;
     192        ptr<Expr> increment;
     193        ptr<Stmt> body;
     194
     195        ForStmt( const CodeLocation& loc, std::vector<ptr<Stmt>>&& inits, const Expr* cond,
     196                const Expr* increment, const Stmt* body, std::vector<Label>&& labels = {} )
     197        : Stmt(loc, std::move(labels)), inits(std::move(inits)), cond(cond), increment(increment),
     198          body(body) {}
     199
     200        const Stmt* accept( Visitor& v ) const override { return v.visit( this ); }
     201private:
     202        ForStmt* clone() const override { return new ForStmt{ *this }; }
     203};
     204
     205class BranchStmt final : public Stmt {
     206public:
     207        enum Kind { Goto, Break, Continue, FallThrough, FallThroughDefault };
     208        static constexpr size_t kindEnd = 1 + (size_t)FallThroughDefault;
     209
     210        const Label originalTarget;
     211        Label target;
     212        ptr<Expr> computedTarget;
     213        Kind kind;
     214
     215        BranchStmt( const CodeLocation& loc, Kind kind, Label target,
     216                std::vector<Label>&& labels = {} );
     217        BranchStmt( const CodeLocation& loc, const Expr* computedTarget,
     218                std::vector<Label>&& labels = {} )
     219        : Stmt(loc, std::move(labels)), originalTarget(loc), target(loc),
     220          computedTarget(computedTarget), kind(Goto) {}
     221
     222        const char * kindName() { return kindNames[kind]; }
     223
     224        const Stmt* accept( Visitor& v ) const override { return v.visit( this ); }
     225private:
     226        BranchStmt* clone() const override { return new BranchStmt{ *this }; }
     227        static const char * kindNames[kindEnd];
     228};
     229
     230class ReturnStmt final : public Stmt {
     231public:
     232        ptr<Expr> expr;
     233
     234        ReturnStmt( const CodeLocation& loc, const Expr* expr, std::vector<Label>&& labels = {} )
     235        : Stmt(loc, std::move(labels)), expr(expr) {}
     236
     237        const Stmt* accept( Visitor& v ) const override { return v.visit( this ); }
     238private:
     239        ReturnStmt* clone() const override { return new ReturnStmt{ *this }; }
     240};
     241
     242class ThrowStmt final : public Stmt {
     243public:
     244        enum Kind { Terminate, Resume };
     245
     246        ptr<Expr> expr;
     247        ptr<Expr> target;
     248        Kind kind;
     249
     250        ThrowStmt( const CodeLocation& loc, Kind kind, const Expr* expr, const Expr* target,
     251                std::vector<Label>&& labels = {} )
     252        : Stmt(loc, std::move(labels)), expr(expr), target(target), kind(kind) {}
     253
     254        const Stmt* accept( Visitor& v ) const override { return v.visit( this ); }
     255private:
     256        ThrowStmt* clone() const override { return new ThrowStmt{ *this }; }
     257};
     258
     259class TryStmt final : public Stmt {
     260public:
     261        ptr<CompoundStmt> body;
     262        std::vector<ptr<CatchStmt>> handlers;
     263        ptr<FinallyStmt> finally;
     264
     265        TryStmt( const CodeLocation& loc, const CompoundStmt* body,
     266                std::vector<ptr<CatchStmt>>&& handlers, const FinallyStmt* finally,
     267                std::vector<Label>&& labels = {} )
     268        : Stmt(loc, std::move(labels)), body(body), handlers(std::move(handlers)), finally(finally) {}
     269
     270        const Stmt* accept( Visitor& v ) const override { return v.visit( this ); }
     271private:
     272        TryStmt* clone() const override { return new TryStmt{ *this }; }
     273};
     274
     275class CatchStmt final : public Stmt {
     276public:
     277        enum Kind { Terminate, Resume };
     278
     279        ptr<Decl> decl;
     280        ptr<Expr> cond;
     281        ptr<Stmt> body;
     282        Kind kind;
     283
     284        CatchStmt( const CodeLocation& loc, Kind kind, const Decl* decl, const Expr* cond,
     285                const Stmt* body, std::vector<Label>&& labels = {} )
     286        : Stmt(loc, std::move(labels)), decl(decl), cond(cond), body(body), kind(kind) {}
     287
     288        const Stmt* accept( Visitor& v ) const override { return v.visit( this ); }
     289private:
     290        CatchStmt* clone() const override { return new CatchStmt{ *this }; }
     291};
     292
     293class FinallyStmt final : public Stmt {
     294public:
     295        ptr<CompoundStmt> body;
     296
     297        FinallyStmt( const CodeLocation& loc, const CompoundStmt* body,
     298                std::vector<Label>&& labels = {} )
     299        : Stmt(loc, std::move(labels)), body(body) {}
     300
     301        const Stmt* accept( Visitor& v ) const override { return v.visit( this ); }
     302private:
     303        FinallyStmt* clone() const override { return new FinallyStmt{ *this }; }
     304};
     305
     306class WaitForStmt final : public Stmt {
     307public:
     308        struct Target {
     309                ptr<Expr> function;
     310                std::vector<ptr<Expr>> arguments;
     311        };
     312
     313        struct Clause {
     314                Target target;
     315                ptr<Stmt> stmt;
     316                ptr<Expr> cond;
     317        };
     318
     319        struct Timeout {
     320                ptr<Expr> time;
     321                ptr<Stmt> stmt;
     322                ptr<Expr> cond;
     323        };
     324
     325        struct OrElse {
     326                ptr<Stmt> stmt;
     327                ptr<Expr> cond;
     328        };
     329
     330        std::vector<Clause> clauses;
     331        Timeout timeout;
     332        OrElse orElse;
     333
     334        WaitForStmt( const CodeLocation& loc, std::vector<Label>&& labels = {} )
     335        : Stmt(loc, std::move(labels)) {}
     336
     337        const Stmt* accept( Visitor& v ) const override { return v.visit( this ); }
     338private:
     339        WaitForStmt* clone() const override { return new WaitForStmt{ *this }; }
     340};
     341
     342class WithStmt final : public Stmt {
     343public:
     344        std::vector<ptr<Expr>> exprs;
     345        ptr<Stmt> stmt;
     346
     347        WithStmt( const CodeLocation& loc, std::vector<ptr<Expr>>&& exprs, const Stmt* stmt,
     348                std::vector<Label>&& labels = {} )
     349        : Stmt(loc, std::move(labels)), exprs(std::move(exprs)), stmt(stmt) {}
     350
     351        const Stmt* accept( Visitor& v ) const override { return v.visit( this ); }
     352private:
     353        WithStmt* clone() const override { return new WithStmt{ *this }; }
     354};
     355
     356class DeclStmt final : public Stmt {
     357public:
     358        ptr<Decl> decl;
     359
     360        DeclStmt( const CodeLocation& loc, const Decl* decl, std::vector<Label>&& labels = {} )
     361        : Stmt(loc, std::move(labels)), decl(decl) {}
     362
     363        const Stmt* accept( Visitor& v ) const override { return v.visit( this ); }
     364private:
     365        DeclStmt* clone() const override { return new DeclStmt{ *this }; }
     366};
     367
     368class ImplicitCtorDtorStmt final : public Stmt {
     369public:
     370        readonly<Stmt> callStmt;
     371
     372        ImplicitCtorDtorStmt( const CodeLocation& loc, const Stmt* callStmt,
     373                std::vector<Label>&& labels = {} )
     374        : Stmt(loc, std::move(labels)), callStmt(callStmt) {}
     375
     376        const Stmt* accept( Visitor& v ) const override { return v.visit( this ); }
     377private:
     378        ImplicitCtorDtorStmt* clone() const override { return new ImplicitCtorDtorStmt{ *this }; }
     379};
    90380
    91381//=================================================================================================
  • src/AST/porting.md

    r23f99e1 r1e97287  
    109109* `get_statement()` exclusively used for code location, replaced with `CodeLocation` field
    110110
     111`CaseStmt`
     112* `_isDefault` has been removed
     113  * `isDefault` calculates value from `cond`
     114  * default may not have a condition. I believe case (!default) requires a condition.
     115
     116`BranchStmt`
     117* `Type` -> `Kind` and `type` -> `kind`
     118* Constructors no longer throw SemanticErrorException:
     119  * `label` constructor claims it is now considered a syntax error, replaced with assert.
     120  * `computedTarget` constructor assumes `Goto`, other check would have SegFaulted.
     121
     122`TryStmt`
     123* `block` -> `body` and `finallyBlock` -> `finally`
     124
     125`FinallyStmt`
     126* `block` -> `body`
     127
    111128`CompoundStmt`
    112129* **TODO** port copy operator
Note: See TracChangeset for help on using the changeset viewer.