Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/ParseNode.h

    r8cc5cb0 r413ad05  
    1010// Created On       : Sat May 16 13:28:16 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Aug 11 12:24:11 2016
    13 // Update Count     : 443
     12// Last Modified On : Sun Aug 28 21:14:51 2016
     13// Update Count     : 575
    1414//
    1515
     
    2222#include <memory>
    2323
    24 #include "Common/utility.h"
    2524#include "Parser/LinkageSpec.h"
    2625#include "SynTree/Type.h"
    2726#include "SynTree/Expression.h"
    2827#include "SynTree/Statement.h"
    29 //#include "SynTree/Declaration.h"
     28#include "SynTree/Label.h"
     29#include "Common/utility.h"
    3030#include "Common/UniqueName.h"
    31 #include "SynTree/Label.h"
    3231
    3332class StatementNode;
     
    3736class InitializerNode;
    3837
    39 // Builder
     38//##############################################################################
     39
    4040class ParseNode {
    4141  public:
    42         ParseNode();
    43         ParseNode( const std::string * );
    44         ParseNode( const std::string & );                                       // for copy constructing subclasses
    45         virtual ~ParseNode();
    46 
    47         ParseNode *get_link() const { return next; }
    48         ParseNode *get_last();
    49         ParseNode *set_link( ParseNode * );
    50         void set_next( ParseNode *newlink ) { next = newlink; }
    51 
    52         virtual ParseNode *clone() const { return 0; };
     42        ParseNode() {};
     43        ParseNode( const std::string *name ) : name( *name ) { assert( false ); delete name; }
     44        ParseNode( const std::string &name ) : name( name ) { assert( false ); }
     45        virtual ~ParseNode() { delete next; };
     46        virtual ParseNode *clone() const = 0;
     47
     48        ParseNode *get_next() const { return next; }
     49        ParseNode *set_next( ParseNode *newlink ) { next = newlink; return this; }
     50        ParseNode *get_last() {
     51                ParseNode *current;
     52                for ( current = this; current->get_next() != 0; current = current->get_next() );
     53                return current;
     54        }
     55        ParseNode *set_last( ParseNode *newlast ) {
     56                if ( newlast != 0 ) get_last()->set_next( newlast );
     57                return this;
     58        }
    5359
    5460        const std::string &get_name() const { return name; }
    5561        void set_name( const std::string &newValue ) { name = newValue; }
    5662
    57         virtual void print( std::ostream &os, int indent = 0 ) const;
    58         virtual void printList( std::ostream &os, int indent = 0 ) const;
    59 
    60         ParseNode &operator,( ParseNode & );
    61   protected:
     63        virtual void print( std::ostream &os, int indent = 0 ) const {}
     64        virtual void printList( std::ostream &os, int indent = 0 ) const {}
     65  private:
     66        static int indent_by;
     67
     68        ParseNode *next = nullptr;
    6269        std::string name;
    63         static int indent_by;
    64         ParseNode *next;
    65 };
    66 
    67 ParseNode *mkList( ParseNode & );
     70}; // ParseNode
    6871
    6972//##############################################################################
     
    7477        InitializerNode( InitializerNode *, bool aggrp = false, ExpressionNode *des = 0 );
    7578        ~InitializerNode();
     79        virtual InitializerNode *clone() const { assert( false ); return nullptr; }
    7680
    7781        ExpressionNode *get_expression() const { return expr; }
     
    9296        ExpressionNode *expr;
    9397        bool aggregate;
    94         ExpressionNode *designator; // may be list
     98        ExpressionNode *designator;                                                     // may be list
    9599        InitializerNode *kids;
    96100        bool maybeConstructed;
    97 };
    98 
    99 //##############################################################################
    100 
    101 class ExpressionNode : public ParseNode {
     101}; // InitializerNode
     102
     103//##############################################################################
     104
     105class ExpressionNode final : public ParseNode {
    102106  public:
    103107        ExpressionNode( Expression * expr = nullptr ) : expr( expr ) {}
     
    105109        ExpressionNode( const ExpressionNode &other );
    106110        virtual ~ExpressionNode() {}
    107 
    108         virtual ExpressionNode *clone() const { return 0; }
     111        virtual ExpressionNode *clone() const { return expr ? new ExpressionNode( expr->clone() ) : nullptr; }
    109112
    110113        bool get_extension() const { return extension; }
    111114        ExpressionNode *set_extension( bool exten ) { extension = exten; return this; }
    112115
    113         virtual void print( std::ostream &os, int indent = 0 ) const {}
    114         virtual void printOneLine( std::ostream &os, int indent = 0 ) const {}
    115 
    116         virtual Expression *build() const { return expr; }
     116        void print( std::ostream &os, int indent = 0 ) const {}
     117        void printOneLine( std::ostream &os, int indent = 0 ) const {}
     118
     119        template<typename T>
     120        bool isExpressionType() const {
     121                return nullptr != dynamic_cast<T>(expr.get());
     122        }
     123
     124        Expression *build() const { return const_cast<ExpressionNode*>(this)->expr.release(); }
    117125  private:
    118126        bool extension = false;
    119         Expression *expr;
    120 };
     127        std::unique_ptr<Expression> expr;
     128}; // ExpressionNode
    121129
    122130template< typename T >
    123 struct maybeBuild_t<Expression, T> {
     131struct maybeBuild_t< Expression, T > {
    124132        static inline Expression * doit( const T *orig ) {
    125133                if ( orig ) {
     
    128136                        return p;
    129137                } else {
    130                         return 0;
     138                        return nullptr;
    131139                } // if
    132140        }
    133141};
    134 
    135 //##############################################################################
    136 
    137 Expression *build_constantInteger( std::string &str );
    138 Expression *build_constantFloat( std::string &str );
    139 Expression *build_constantChar( std::string &str );
    140 ConstantExpr *build_constantStr( std::string &str );
    141 
    142 //##############################################################################
    143 
    144 NameExpr *build_varref( const std::string *name, bool labelp = false );
    145 
    146 //##############################################################################
    147 
    148 Expression *build_typevalue( DeclarationNode *decl );
    149 
    150 //##############################################################################
    151142
    152143enum class OperKinds {
     
    154145        SizeOf, AlignOf, OffsetOf, Plus, Minus, Mul, Div, Mod, Or, And,
    155146        BitOr, BitAnd, Xor, Cast, LShift, RShift, LThan, GThan, LEThan, GEThan, Eq, Neq,
    156         Assign, MulAssn, DivAssn, ModAssn, PlusAssn, MinusAssn, LSAssn, RSAssn, AndAssn, ERAssn, OrAssn,
     147        Assign, AtAssn, MulAssn, DivAssn, ModAssn, PlusAssn, MinusAssn, LSAssn, RSAssn, AndAssn, ERAssn, OrAssn,
    157148        Index, Range,
    158149        // monadic
    159150        UnPlus, UnMinus, AddressOf, PointTo, Neg, BitNeg, Incr, IncrPost, Decr, DecrPost, LabelAddress,
    160151        Ctor, Dtor,
     152}; // OperKinds
     153
     154struct LabelNode {
     155        std::list< Label > labels;
    161156};
     157
     158Expression *build_constantInteger( const std::string &str );
     159Expression *build_constantFloat( const std::string &str );
     160Expression *build_constantChar( const std::string &str );
     161ConstantExpr *build_constantStr( const std::string &str );
     162
     163NameExpr *build_varref( const std::string *name, bool labelp = false );
     164Expression *build_typevalue( DeclarationNode *decl );
    162165
    163166Expression *build_cast( DeclarationNode * decl_node, ExpressionNode *expr_node );
     
    183186Expression *build_func( ExpressionNode * function, ExpressionNode * expr_node );
    184187Expression *build_range( ExpressionNode * low, ExpressionNode *high );
    185 
    186 //##############################################################################
    187 
    188 Expression *build_asm( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand );
    189 
    190 //##############################################################################
    191 
    192 class LabelNode : public ExpressionNode {
    193   public:
    194         virtual Expression *build() const { return NULL; }
    195         virtual LabelNode *clone() const { assert( false ); return new LabelNode( *this ); }
    196 
    197         virtual void print( std::ostream &os, int indent = 0) const;
    198         virtual void printOneLine( std::ostream &os, int indent = 0) const;
    199 
    200         const std::list< Label > &get_labels() const { return labels; };
    201         void append_label( std::string * label ) { labels.push_back( *label ); delete label; }
    202   private:
    203         std::list< Label > labels;
    204 };
    205 
    206 //##############################################################################
    207 
     188Expression *build_asmexpr( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand );
    208189Expression *build_valexpr( StatementNode *s );
    209 
    210 //##############################################################################
    211 
    212190Expression *build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids );
    213191
     
    218196class DeclarationNode : public ParseNode {
    219197  public:
    220         enum Qualifier { Const, Restrict, Volatile, Lvalue, Atomic };
     198        enum Qualifier { Const, Restrict, Volatile, Lvalue, Atomic, NoOfQualifier };
    221199        enum StorageClass { Extern, Static, Auto, Register, Inline, Fortran, Noreturn, Threadlocal, NoStorageClass, };
    222200        enum BasicType { Char, Int, Float, Double, Void, Bool, Complex, Imaginary };
     
    226204        enum BuiltinType { Valist };
    227205
     206        static const char *qualifierName[];
    228207        static const char *storageName[];
    229         static const char *qualifierName[];
    230208        static const char *basicTypeName[];
    231209        static const char *modifierName[];
     
    236214        static DeclarationNode *newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle = false );
    237215        static DeclarationNode *newQualifier( Qualifier );
     216        static DeclarationNode *newForall( DeclarationNode *);
    238217        static DeclarationNode *newStorageClass( StorageClass );
    239218        static DeclarationNode *newBasicType( BasicType );
    240219        static DeclarationNode *newModifier( Modifier );
    241         static DeclarationNode *newForall( DeclarationNode *);
     220        static DeclarationNode *newBuiltinType( BuiltinType );
    242221        static DeclarationNode *newFromTypedef( std::string *);
    243222        static DeclarationNode *newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields, bool body );
     
    258237        static DeclarationNode *newAttr( std::string *, ExpressionNode *expr );
    259238        static DeclarationNode *newAttr( std::string *, DeclarationNode *type );
    260         static DeclarationNode *newBuiltinType( BuiltinType );
     239
     240        DeclarationNode();
     241        ~DeclarationNode();
     242        DeclarationNode *clone() const;
    261243
    262244        DeclarationNode *addQualifiers( DeclarationNode *);
     245        void checkQualifiers( const TypeData *, const TypeData * );
    263246        DeclarationNode *copyStorageClasses( DeclarationNode *);
    264247        DeclarationNode *addType( DeclarationNode *);
     
    275258        DeclarationNode *addNewArray( DeclarationNode *array );
    276259        DeclarationNode *addParamList( DeclarationNode *list );
    277         DeclarationNode *addIdList( DeclarationNode *list );       // old-style functions
     260        DeclarationNode *addIdList( DeclarationNode *list ); // old-style functions
    278261        DeclarationNode *addInitializer( InitializerNode *init );
    279262
     
    284267        DeclarationNode *cloneBaseType( DeclarationNode *newdecl );
    285268
    286         DeclarationNode *appendList( DeclarationNode * );
    287 
    288         DeclarationNode *clone() const;
     269        DeclarationNode *appendList( DeclarationNode *node ) {
     270                return (DeclarationNode *)set_last( node );
     271        }
     272
    289273        void print( std::ostream &os, int indent = 0 ) const;
    290274        void printList( std::ostream &os, int indent = 0 ) const;
     
    295279        bool get_hasEllipsis() const;
    296280        const std::string &get_name() const { return name; }
    297         LinkageSpec::Type get_linkage() const { return linkage; }
     281        LinkageSpec::Spec get_linkage() const { return linkage; }
    298282        DeclarationNode *extractAggregate() const;
    299         ExpressionNode *get_enumeratorValue() const { return enumeratorValue; }
     283        bool has_enumeratorValue() const { return (bool)enumeratorValue; }
     284        ExpressionNode *consume_enumeratorValue() const { return const_cast<DeclarationNode*>(this)->enumeratorValue.release(); }
    300285
    301286        bool get_extension() const { return extension; }
    302287        DeclarationNode *set_extension( bool exten ) { extension = exten; return this; }
    303 
    304         DeclarationNode();
    305         ~DeclarationNode();
    306   private:
    307         StorageClass buildStorageClass() const;
    308         bool buildFuncSpecifier( StorageClass key ) const;
     288  public:
     289        // StorageClass buildStorageClass() const;
     290        // bool buildFuncSpecifier( StorageClass key ) const;
    309291
    310292        TypeData *type;
    311293        std::string name;
    312         std::list< StorageClass > storageClasses;
     294        // std::list< StorageClass > storageClasses;
     295        StorageClass storageClass;
     296        bool isInline, isNoreturn;
    313297        std::list< std::string > attributes;
    314298        ExpressionNode *bitfieldWidth;
    315         ExpressionNode *enumeratorValue;
     299        std::unique_ptr<ExpressionNode> enumeratorValue;
    316300        InitializerNode *initializer;
    317301        bool hasEllipsis;
    318         LinkageSpec::Type linkage;
     302        LinkageSpec::Spec linkage;
    319303        bool extension = false;
     304        std::string error;
    320305
    321306        static UniqueName anonymous;
     
    323308
    324309Type *buildType( TypeData *type );
    325 
    326 //##############################################################################
    327 
    328 class StatementNode : public ParseNode {
    329   public:
    330         enum Type { Exp,   If,        Switch,  Case,    Default,  Choose,   Fallthru,
    331                                 While, Do,        For,
    332                                 Goto,  Continue,  Break,   Return,  Throw,
    333                                 Try,   Catch,     Finally, Asm,
    334                                 Decl
    335         };
    336 
    337         StatementNode();
    338         StatementNode( const std::string *name );
    339         StatementNode( Type t, ExpressionNode *control = 0, StatementNode *block = 0 );
    340         StatementNode( Type t, std::string *target );
     310//Type::Qualifiers buildQualifiers( const TypeData::Qualifiers & qualifiers );
     311
     312static inline Type * maybeMoveBuildType( const DeclarationNode *orig ) {
     313        Type* ret = orig ? orig->buildType() : nullptr;
     314        delete orig;
     315        return ret;
     316}
     317
     318//##############################################################################
     319
     320class StatementNode final : public ParseNode {
     321  public:
     322        StatementNode() { stmt = nullptr; }
     323        StatementNode( Statement *stmt ) : stmt( stmt ) {}
    341324        StatementNode( DeclarationNode *decl );
    342 
    343         ~StatementNode();
    344 
    345         static StatementNode *newCatchStmt( DeclarationNode *d = 0, StatementNode *s = 0, bool catchRestP = false );
    346 
    347         StatementNode *set_block( StatementNode *b ) { block = b; return this; }
    348         StatementNode *get_block() const { return block; }
    349 
    350         void set_control( ExpressionNode *c ) { control = c; }
    351         ExpressionNode *get_control() const { return control; }
    352 
    353         StatementNode::Type get_type() const { return type; }
    354 
    355         virtual StatementNode *add_label( const std::string * );
    356         virtual std::list<std::string> get_labels() const { return labels; }
    357 
    358         void addDeclaration( DeclarationNode *newDecl ) { decl = newDecl; }
    359         void setCatchRest( bool newVal ) { isCatchRest = newVal; }
    360 
    361         std::string get_target() const;
    362 
    363         // StatementNode *add_controlexp( ExpressionNode * );
    364         StatementNode *append_block( StatementNode * );
     325        virtual ~StatementNode() {}
     326
     327        virtual StatementNode *clone() const final { assert( false ); return nullptr; }
     328        Statement *build() const { return const_cast<StatementNode*>(this)->stmt.release(); }
     329
     330        virtual StatementNode *add_label( const std::string * name ) {
     331                stmt->get_labels().emplace_back( *name );
     332                delete name;
     333                return this;
     334        }
     335
    365336        virtual StatementNode *append_last_case( StatementNode * );
    366 
    367         void print( std::ostream &os, int indent = 0) const;
    368         virtual StatementNode *clone() const;
    369         virtual Statement *build() const;
    370   private:
    371         static const char *StType[];
    372         Type type;
    373         ExpressionNode *control;
    374         StatementNode *block;
    375         std::list<std::string> labels;
    376         std::string *target;                            // target label for jump statements
    377         DeclarationNode *decl;
    378         bool isCatchRest;
    379 }; // StatementNode
    380 
    381 class StatementNode2 : public StatementNode {
    382   public:
    383         StatementNode2() {}
    384         StatementNode2( Statement *stmt ) : stmt( stmt ) {}
    385         virtual ~StatementNode2() {}
    386 
    387         virtual StatementNode2 *clone() const { assert( false ); return nullptr; }
    388         virtual Statement *build() const { return stmt; }
    389 
    390         virtual StatementNode2 *add_label( const std::string * name ) {
    391                 stmt->get_labels().emplace_back( *name );
    392                 return this;
    393         }
    394         virtual StatementNode *append_last_case( StatementNode * );
    395         virtual std::list<std::string> get_labels() const { assert( false ); return StatementNode::get_labels(); }
    396337
    397338        virtual void print( std::ostream &os, int indent = 0 ) {}
    398339        virtual void printList( std::ostream &os, int indent = 0 ) {}
    399340  private:
    400         Statement *stmt;
     341        std::unique_ptr<Statement> stmt;
    401342}; // StatementNode
     343
     344Statement *build_expr( ExpressionNode *ctl );
    402345
    403346struct ForCtl {
    404347        ForCtl( ExpressionNode *expr, ExpressionNode *condition, ExpressionNode *change ) :
    405                 init( new StatementNode( StatementNode::Exp, expr ) ), condition( condition ), change( change ) {}
     348                init( new StatementNode( build_expr( expr ) ) ), condition( condition ), change( change ) {}
    406349        ForCtl( DeclarationNode *decl, ExpressionNode *condition, ExpressionNode *change ) :
    407350                init( new StatementNode( decl ) ), condition( condition ), change( change ) {}
     
    412355};
    413356
    414 Statement *build_expr( ExpressionNode *ctl );
    415357Statement *build_if( ExpressionNode *ctl, StatementNode *then_stmt, StatementNode *else_stmt );
    416358Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt );
     
    419361Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind = false );
    420362Statement *build_for( ForCtl *forctl, StatementNode *stmt );
    421 Statement *build_branch( std::string identifier, BranchStmt::Type kind );
     363Statement *build_branch( BranchStmt::Type kind );
     364Statement *build_branch( std::string *identifier, BranchStmt::Type kind );
    422365Statement *build_computedgoto( ExpressionNode *ctl );
    423366Statement *build_return( ExpressionNode *ctl );
    424367Statement *build_throw( ExpressionNode *ctl );
    425 
    426 //##############################################################################
    427 
    428 class CompoundStmtNode : public StatementNode {
    429   public:
    430         CompoundStmtNode();
    431         CompoundStmtNode( const std::string * );
    432         CompoundStmtNode( StatementNode * );
    433         ~CompoundStmtNode();
    434 
    435         void add_statement( StatementNode * );
    436 
    437         void print( std::ostream &os, int indent = 0 ) const;
    438         virtual Statement *build() const;
    439   private:
    440         StatementNode *first, *last;
    441 };
    442 
    443 //##############################################################################
    444 
    445 class AsmStmtNode : public StatementNode {
    446   public:
    447         AsmStmtNode( Type, bool voltile, ConstantExpr *instruction, ExpressionNode *output = 0, ExpressionNode *input = 0, ExpressionNode *clobber = 0, LabelNode *gotolabels = 0 );
    448         ~AsmStmtNode();
    449 
    450         void print( std::ostream &os, int indent = 0 ) const;
    451         Statement *build() const;
    452   private:
    453         bool voltile;
    454         ConstantExpr *instruction;
    455         ExpressionNode *output, *input;
    456         ExpressionNode *clobber;
    457         std::list< Label > gotolabels;
    458 };
     368Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt );
     369Statement *build_catch( DeclarationNode *decl, StatementNode *stmt, bool catchAny = false );
     370Statement *build_finally( StatementNode *stmt );
     371Statement *build_compound( StatementNode *first );
     372Statement *build_asmstmt( bool voltile, ConstantExpr *instruction, ExpressionNode *output = 0, ExpressionNode *input = 0, ExpressionNode *clobber = 0, LabelNode *gotolabels = 0 );
    459373
    460374//##############################################################################
     
    463377void buildList( const NodeType *firstNode, std::list< SynTreeType * > &outputList ) {
    464378        SemanticError errors;
    465         std::back_insert_iterator< std::list< SynTreeType *> > out( outputList );
     379        std::back_insert_iterator< std::list< SynTreeType * > > out( outputList );
    466380        const NodeType *cur = firstNode;
    467381
    468382        while ( cur ) {
    469383                try {
    470 //                      SynTreeType *result = dynamic_cast< SynTreeType *>( maybeBuild<typename std::result_of<decltype(&NodeType::build)(NodeType)>::type>( cur ) );
    471                         SynTreeType *result = dynamic_cast< SynTreeType *>( maybeBuild<typename std::pointer_traits<decltype(cur->build())>::element_type>( cur ) );
     384//                      SynTreeType *result = dynamic_cast< SynTreeType * >( maybeBuild< typename std::result_of< decltype(&NodeType::build)(NodeType)>::type >( cur ) );
     385                        SynTreeType *result = dynamic_cast< SynTreeType * >( maybeBuild< typename std::pointer_traits< decltype(cur->build())>::element_type >( cur ) );
    472386                        if ( result ) {
    473387                                *out++ = result;
     
    477391                        errors.append( e );
    478392                } // try
    479                 cur = dynamic_cast< NodeType *>( cur->get_link() );
     393                cur = dynamic_cast< NodeType * >( cur->get_next() );
    480394        } // while
    481395        if ( ! errors.isEmpty() ) {
     
    486400// in DeclarationNode.cc
    487401void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList );
    488 void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList );
     402void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType * > &outputList );
    489403void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList );
     404
     405template< typename SynTreeType, typename NodeType >
     406void buildMoveList( const NodeType *firstNode, std::list< SynTreeType * > &outputList ) {
     407        buildList(firstNode, outputList);
     408        delete firstNode;
     409}
     410
    490411
    491412#endif // PARSENODE_H
Note: See TracChangeset for help on using the changeset viewer.