Changeset 8a5530c


Ignore:
Timestamp:
May 16, 2019, 10:58:52 AM (2 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
arm-eh, cleanup-dtors, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr
Children:
f2e482cb
Parents:
89c2f7c9
Message:

Fixed FunctionType? cast, fixed maybe_accept, implemented statement visitation and fixed several nodes

Location:
src/AST
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • src/AST/CVQualifiers.hpp

    r89c2f7c9 r8a5530c  
    6060
    6161        bool operator== ( Qualifiers a, Qualifiers b ) {
    62                 return (a & EquivQualifiers) == (b & EquivQualifiers);
     62                return (a.val & EquivQualifiers) == (b.val & EquivQualifiers);
    6363        }
    6464        bool operator!= ( Qualifiers a, Qualifiers b ) {
    65                 return (a & EquivQualifiers) != (b & EquivQualifiers);
     65                return !(a == b);
    6666        }
    6767        bool operator<= ( Qualifiers a, Qualifiers b ) {
  • src/AST/Decl.cpp

    r89c2f7c9 r8a5530c  
    4545}
    4646
     47// --- FunctionDecl
     48
     49const Type * FunctionDecl::get_type() const override { return type.get(); }
     50void FunctionDecl::set_type(Type * t) override { type = strict_dynamic_cast< FunctionType* >( t ); }
     51
    4752// --- TypeDecl
    4853
    4954std::string TypeDecl::typeString() const {
    5055        static const std::string kindNames[] = { "object type", "function type", "tuple type" };
    51         assertf( sizeof(kindNames)/sizeof(kindNames[0]) == DeclarationNode::NoTypeClass-1, 
     56        assertf( sizeof(kindNames)/sizeof(kindNames[0]) == DeclarationNode::NoTypeClass-1,
    5257                "typeString: kindNames is out of sync." );
    5358        assertf( kind < sizeof(kindNames)/sizeof(kindNames[0]), "TypeDecl's kind is out of bounds." );
  • src/AST/Decl.hpp

    r89c2f7c9 r8a5530c  
    119119};
    120120
     121/// Object declaration `int foo()`
    121122class FunctionDecl : public DeclWithType {
    122123public:
     
    131132          stmts( stmts ) {}
    132133
    133         const Type * get_type() const override { return type.get(); }
    134         void set_type(Type * t) override { type = strict_dynamic_cast< FunctionType* >( t ); }
     134        const Type * get_type() const override;
     135        void set_type(Type * t) override;
    135136
    136137        bool has_body() const { return stmts; }
     
    189190        TypeDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, Type* b,
    190191                TypeVar::Kind k, bool s, Type* i = nullptr )
    191         : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == TypeVar::Ttype || s ), 
     192        : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == TypeVar::Ttype || s ),
    192193          init( i ) {}
    193194
  • src/AST/Pass.hpp

    r89c2f7c9 r8a5530c  
    2929#include "AST/Init.hpp"
    3030#include "AST/Stmt.hpp"
     31#include "AST/Type.hpp"
    3132
    3233#include "AST/Visitor.hpp"
     
    189190
    190191        template< typename node_t >
    191         auto call_accept( const node_t * node ) -> decltype( node->accept(*this) );
     192        auto call_accept( const node_t * node ) -> typename std::enable_if<
     193                                !std::is_base_of<ast::Expr, node_t>::value &&
     194                                !std::is_base_of<ast::Stmt, node_t>::value
     195                        , decltype( node->accept(*this) )
     196                >::type;
    192197
    193198        template< template <class...> class container_t >
  • src/AST/Pass.impl.hpp

    r89c2f7c9 r8a5530c  
    6464
    6565                        std::transform(decls->begin(), decls->end(), it, [](const ast::Decl * decl) -> auto {
    66                                         return new DeclStmt( decl );
     66                                        return new DeclStmt( decl->location, decl );
    6767                                });
    6868                        decls->clear();
     
    119119        template< typename pass_t >
    120120        template< typename node_t >
    121         auto Pass< pass_t >::call_accept( const node_t * node ) -> decltype( node->accept(*this) ) {
     121        auto Pass< pass_t >::call_accept( const node_t * node )
     122                -> typename std::enable_if<
     123                                !std::is_base_of<ast::Expr, node_t>::value &&
     124                                !std::is_base_of<ast::Stmt, node_t>::value
     125                        , decltype( node->accept(*this) )
     126                >::type
     127
     128        {
    122129                __pedantic_pass_assert( __visit_children() );
    123130                __pedantic_pass_assert( expr );
     
    296303                child_t parent_t::*child
    297304        ) {
    298                 static_assert( std::is_base_of<parent_t, node_t>::value, "Error deductiing member object" );
     305                static_assert( std::is_base_of<parent_t, node_t>::value, "Error deducing member object" );
    299306
    300307                if(__pass::skip(parent->*child)) return;
     
    424431                                new ast::ArrayType(
    425432                                        new ast::BasicType( ast::BasicType::Char, ast::CV::Qualifiers( ast::CV::Const ) ),
    426                                         nullptr, true, false
     433                                        nullptr, VariableLen, DynamicDim
    427434                                )
    428435                        );
     
    434441                                ValueGuard< bool > oldInFunction( inFunction );
    435442                                inFunction = true;
    436                                 maybe_accept( node, &FunctionDecl::statements );
     443                                maybe_accept( node, &FunctionDecl::stmts );
    437444                                maybe_accept( node, &FunctionDecl::attributes );
    438445                        )
     
    537544
    538545        VISIT(
    539                 maybe_accept( node, &TypeDecl::assertions, *this );
     546                maybe_accept( node, &TypeDecl::assertions );
    540547
    541548                {
     
    614621}
    615622
     623//--------------------------------------------------------------------------
     624// ExprStmt
     625template< typename pass_t >
     626const ast::Stmt * ast::Pass< pass_t >::visit( const ExprStmt * node ) {
     627        VISIT_START( node );
     628
     629        VISIT(
     630                maybe_accept( node, &ExprStmt::expr );
     631        )
     632
     633        VISIT_END( Stmt, node );
     634}
     635
     636//--------------------------------------------------------------------------
     637// AsmStmt
     638template< typename pass_t >
     639const ast::Stmt * ast::Pass< pass_t >::visit( const ast::AsmStmt * node ) {
     640        VISIT_START( node )
     641
     642        VISIT(
     643                maybe_accept( node, &AsmStmt::instruction );
     644                maybe_accept( node, &AsmStmt::output      );
     645                maybe_accept( node, &AsmStmt::input       );
     646                maybe_accept( node, &AsmStmt::clobber     );
     647        )
     648
     649        VISIT_END( Stmt, node );
     650}
     651
     652//--------------------------------------------------------------------------
     653// DirectiveStmt
     654template< typename pass_t >
     655const ast::Stmt * ast::Pass< pass_t >::visit( const ast::DirectiveStmt * node ) {
     656        VISIT_START( node )
     657
     658        VISIT_END( Stmt, node );
     659}
     660
     661//--------------------------------------------------------------------------
     662// IfStmt
     663template< typename pass_t >
     664const ast::Stmt * ast::Pass< pass_t >::visit( const ast::IfStmt * node ) {
     665        VISIT_START( node );
     666        VISIT({
     667                // if statements introduce a level of scope (for the initialization)
     668                guard_indexer guard { *this };
     669                maybe_accept( node, &IfStmt::inits    );
     670                maybe_accept( node, &IfStmt::cond     );
     671                maybe_accept( node, &IfStmt::thenPart );
     672                maybe_accept( node, &IfStmt::elsePart );
     673        })
     674        VISIT_END( Stmt, node );
     675}
     676
     677//--------------------------------------------------------------------------
     678// WhileStmt
     679template< typename pass_t >
     680const ast::Stmt * ast::Pass< pass_t >::visit( const WhileStmt * node ) {
     681        VISIT_START( node );
     682
     683        VISIT({
     684                // while statements introduce a level of scope (for the initialization)
     685                guard_indexer guard { *this };
     686                maybe_accept( node, &WhileStmt::inits );
     687                maybe_accept( node, &WhileStmt::cond  );
     688                maybe_accept( node, &WhileStmt::body  );
     689        })
     690
     691        VISIT_END( Stmt, node );
     692}
    616693
    617694//--------------------------------------------------------------------------
     
    667744        )
    668745
    669         VISIT_END( Attribute *, node );
     746        VISIT_END( Attribute, node );
    670747}
    671748
  • src/AST/Pass.proto.hpp

    r89c2f7c9 r8a5530c  
    241241                INDEXER_FUNC1( addUnion  , const UnionDecl *     );
    242242                INDEXER_FUNC1( addTrait  , const TraitDecl *     );
    243                 INDEXER_FUNC2( addWith   , const std::list< Expression * > &, const Node * );
     243                INDEXER_FUNC2( addWith   , const std::list< ptr<Expr> > &, const Node * );
    244244
    245245                // A few extra functions have more complicated behaviour, they are hand written
  • src/AST/Stmt.hpp

    r89c2f7c9 r8a5530c  
    6262private:
    6363        CompoundStmt* clone() const override { return new CompoundStmt{ *this }; }
     64
     65        /// Must be copied in ALL derived classes
     66        template<typename node_t>
     67        friend auto mutate(const node_t * node);
    6468};
    6569
     
    7377private:
    7478        NullStmt* clone() const override { return new NullStmt{ *this }; }
     79
     80        /// Must be copied in ALL derived classes
     81        template<typename node_t>
     82        friend auto mutate(const node_t * node);
    7583};
    7684
     
    8088        ptr<Expr> expr;
    8189
    82         ExprStmt( const CodeLocation& loc, const Expr* e ) : Stmt(loc), expr(e) {}
     90        ExprStmt( const CodeLocation & loc, const Expr * e ) : Stmt(loc), expr(e) {}
    8391
    8492        const Stmt * accept( Visitor& v ) const override { return v.visit( this ); }
    8593private:
    8694        ExprStmt * clone() const override { return new ExprStmt{ *this }; }
     95
     96        /// Must be copied in ALL derived classes
     97        template<typename node_t>
     98        friend auto mutate(const node_t * node);
    8799};
    88100
     
    106118private:
    107119        AsmStmt* clone() const override { return new AsmStmt{ *this }; }
     120
     121        /// Must be copied in ALL derived classes
     122        template<typename node_t>
     123        friend auto mutate(const node_t * node);
    108124};
    109125
     
    119135private:
    120136        DirectiveStmt* clone() const override { return new DirectiveStmt{ *this }; }
     137
     138        /// Must be copied in ALL derived classes
     139        template<typename node_t>
     140        friend auto mutate(const node_t * node);
    121141};
    122142
     
    137157private:
    138158        IfStmt* clone() const override { return new IfStmt{ *this }; }
     159
     160        /// Must be copied in ALL derived classes
     161        template<typename node_t>
     162        friend auto mutate(const node_t * node);
    139163};
    140164
     
    151175private:
    152176        SwitchStmt* clone() const override { return new SwitchStmt{ *this }; }
     177
     178        /// Must be copied in ALL derived classes
     179        template<typename node_t>
     180        friend auto mutate(const node_t * node);
    153181};
    154182
     
    167195private:
    168196        CaseStmt* clone() const override { return new CaseStmt{ *this }; }
     197
     198        /// Must be copied in ALL derived classes
     199        template<typename node_t>
     200        friend auto mutate(const node_t * node);
    169201};
    170202
     
    184216private:
    185217        WhileStmt* clone() const override { return new WhileStmt{ *this }; }
     218
     219        /// Must be copied in ALL derived classes
     220        template<typename node_t>
     221        friend auto mutate(const node_t * node);
    186222};
    187223
     
    190226        std::vector<ptr<Stmt>> inits;
    191227        ptr<Expr> cond;
    192         ptr<Expr> increment;
     228        ptr<Expr> inc;
    193229        ptr<Stmt> body;
    194230
    195231        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),
     232                const Expr* inc, const Stmt* body, std::vector<Label>&& labels = {} )
     233        : Stmt(loc, std::move(labels)), inits(std::move(inits)), cond(cond), inc(inc),
    198234          body(body) {}
    199235
     
    201237private:
    202238        ForStmt* clone() const override { return new ForStmt{ *this }; }
     239
     240        /// Must be copied in ALL derived classes
     241        template<typename node_t>
     242        friend auto mutate(const node_t * node);
    203243};
    204244
     
    225265private:
    226266        BranchStmt* clone() const override { return new BranchStmt{ *this }; }
     267
     268        /// Must be copied in ALL derived classes
     269        template<typename node_t>
     270        friend auto mutate(const node_t * node);
     271
    227272        static const char * kindNames[kindEnd];
    228273};
     
    238283private:
    239284        ReturnStmt* clone() const override { return new ReturnStmt{ *this }; }
     285
     286        /// Must be copied in ALL derived classes
     287        template<typename node_t>
     288        friend auto mutate(const node_t * node);
    240289};
    241290
     
    255304private:
    256305        ThrowStmt* clone() const override { return new ThrowStmt{ *this }; }
     306
     307        /// Must be copied in ALL derived classes
     308        template<typename node_t>
     309        friend auto mutate(const node_t * node);
    257310};
    258311
     
    271324private:
    272325        TryStmt* clone() const override { return new TryStmt{ *this }; }
     326
     327        /// Must be copied in ALL derived classes
     328        template<typename node_t>
     329        friend auto mutate(const node_t * node);
    273330};
    274331
     
    289346private:
    290347        CatchStmt* clone() const override { return new CatchStmt{ *this }; }
     348
     349        /// Must be copied in ALL derived classes
     350        template<typename node_t>
     351        friend auto mutate(const node_t * node);
    291352};
    292353
     
    302363private:
    303364        FinallyStmt* clone() const override { return new FinallyStmt{ *this }; }
     365
     366        /// Must be copied in ALL derived classes
     367        template<typename node_t>
     368        friend auto mutate(const node_t * node);
    304369};
    305370
     
    338403private:
    339404        WaitForStmt* clone() const override { return new WaitForStmt{ *this }; }
     405
     406        /// Must be copied in ALL derived classes
     407        template<typename node_t>
     408        friend auto mutate(const node_t * node);
    340409};
    341410
     
    352421private:
    353422        WithStmt* clone() const override { return new WithStmt{ *this }; }
     423
     424        /// Must be copied in ALL derived classes
     425        template<typename node_t>
     426        friend auto mutate(const node_t * node);
    354427};
    355428
     
    364437private:
    365438        DeclStmt* clone() const override { return new DeclStmt{ *this }; }
     439
     440        /// Must be copied in ALL derived classes
     441        template<typename node_t>
     442        friend auto mutate(const node_t * node);
    366443};
    367444
     
    377454private:
    378455        ImplicitCtorDtorStmt* clone() const override { return new ImplicitCtorDtorStmt{ *this }; }
     456
     457        /// Must be copied in ALL derived classes
     458        template<typename node_t>
     459        friend auto mutate(const node_t * node);
    379460};
    380461
     
    390471inline void increment( const class ExprStmt * node, Node::ref_type ref ) { node->increment( ref ); }
    391472inline void decrement( const class ExprStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
    392 // inline void increment( const class AsmStmt * node, Node::ref_type ref ) { node->increment( ref ); }
    393 // inline void decrement( const class AsmStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
    394 // inline void increment( const class DirectiveStmt * node, Node::ref_type ref ) { node->increment( ref ); }
    395 // inline void decrement( const class DirectiveStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
    396 // inline void increment( const class IfStmt * node, Node::ref_type ref ) { node->increment( ref ); }
    397 // inline void decrement( const class IfStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
    398 // inline void increment( const class WhileStmt * node, Node::ref_type ref ) { node->increment( ref ); }
    399 // inline void decrement( const class WhileStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
    400 // inline void increment( const class ForStmt * node, Node::ref_type ref ) { node->increment( ref ); }
    401 // inline void decrement( const class ForStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
    402 // inline void increment( const class SwitchStmt * node, Node::ref_type ref ) { node->increment( ref ); }
    403 // inline void decrement( const class SwitchStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
    404 // inline void increment( const class CaseStmt * node, Node::ref_type ref ) { node->increment( ref ); }
    405 // inline void decrement( const class CaseStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
    406 // inline void increment( const class BranchStmt * node, Node::ref_type ref ) { node->increment( ref ); }
    407 // inline void decrement( const class BranchStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
    408 // inline void increment( const class ReturnStmt * node, Node::ref_type ref ) { node->increment( ref ); }
    409 // inline void decrement( const class ReturnStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
    410 // inline void increment( const class ThrowStmt * node, Node::ref_type ref ) { node->increment( ref ); }
    411 // inline void decrement( const class ThrowStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
    412 // inline void increment( const class TryStmt * node, Node::ref_type ref ) { node->increment( ref ); }
    413 // inline void decrement( const class TryStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
    414 // inline void increment( const class CatchStmt * node, Node::ref_type ref ) { node->increment( ref ); }
    415 // inline void decrement( const class CatchStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
    416 // inline void increment( const class FinallyStmt * node, Node::ref_type ref ) { node->increment( ref ); }
    417 // inline void decrement( const class FinallyStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
    418 // inline void increment( const class WaitForStmt * node, Node::ref_type ref ) { node->increment( ref ); }
    419 // inline void decrement( const class WaitForStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
    420 // inline void increment( const class WithStmt * node, Node::ref_type ref ) { node->increment( ref ); }
    421 // inline void decrement( const class WithStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
    422 // inline void increment( const class DeclStmt * node, Node::ref_type ref ) { node->increment( ref ); }
    423 // inline void decrement( const class DeclStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
     473inline void increment( const class AsmStmt * node, Node::ref_type ref ) { node->increment( ref ); }
     474inline void decrement( const class AsmStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
     475inline void increment( const class DirectiveStmt * node, Node::ref_type ref ) { node->increment( ref ); }
     476inline void decrement( const class DirectiveStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
     477inline void increment( const class IfStmt * node, Node::ref_type ref ) { node->increment( ref ); }
     478inline void decrement( const class IfStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
     479inline void increment( const class WhileStmt * node, Node::ref_type ref ) { node->increment( ref ); }
     480inline void decrement( const class WhileStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
     481inline void increment( const class ForStmt * node, Node::ref_type ref ) { node->increment( ref ); }
     482inline void decrement( const class ForStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
     483inline void increment( const class SwitchStmt * node, Node::ref_type ref ) { node->increment( ref ); }
     484inline void decrement( const class SwitchStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
     485inline void increment( const class CaseStmt * node, Node::ref_type ref ) { node->increment( ref ); }
     486inline void decrement( const class CaseStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
     487inline void increment( const class BranchStmt * node, Node::ref_type ref ) { node->increment( ref ); }
     488inline void decrement( const class BranchStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
     489inline void increment( const class ReturnStmt * node, Node::ref_type ref ) { node->increment( ref ); }
     490inline void decrement( const class ReturnStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
     491inline void increment( const class ThrowStmt * node, Node::ref_type ref ) { node->increment( ref ); }
     492inline void decrement( const class ThrowStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
     493inline void increment( const class TryStmt * node, Node::ref_type ref ) { node->increment( ref ); }
     494inline void decrement( const class TryStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
     495inline void increment( const class CatchStmt * node, Node::ref_type ref ) { node->increment( ref ); }
     496inline void decrement( const class CatchStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
     497inline void increment( const class FinallyStmt * node, Node::ref_type ref ) { node->increment( ref ); }
     498inline void decrement( const class FinallyStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
     499inline void increment( const class WaitForStmt * node, Node::ref_type ref ) { node->increment( ref ); }
     500inline void decrement( const class WaitForStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
     501inline void increment( const class WithStmt * node, Node::ref_type ref ) { node->increment( ref ); }
     502inline void decrement( const class WithStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
     503inline void increment( const class DeclStmt * node, Node::ref_type ref ) { node->increment( ref ); }
     504inline void decrement( const class DeclStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
    424505inline void increment( const class NullStmt * node, Node::ref_type ref ) { node->increment( ref ); }
    425506inline void decrement( const class NullStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
    426 // inline void increment( const class ImplicitCtorDtorStmt * node, Node::ref_type ref ) { node->increment( ref ); }
    427 // inline void decrement( const class ImplicitCtorDtorStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
     507inline void increment( const class ImplicitCtorDtorStmt * node, Node::ref_type ref ) { node->increment( ref ); }
     508inline void decrement( const class ImplicitCtorDtorStmt * node, Node::ref_type ref ) { node->decrement( ref ); }
    428509
    429510}
Note: See TracChangeset for help on using the changeset viewer.