Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/ParseNode.h

    r413ad05 r8cc5cb0  
    1010// Created On       : Sat May 16 13:28:16 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Aug 28 21:14:51 2016
    13 // Update Count     : 575
     12// Last Modified On : Thu Aug 11 12:24:11 2016
     13// Update Count     : 443
    1414//
    1515
     
    2222#include <memory>
    2323
     24#include "Common/utility.h"
    2425#include "Parser/LinkageSpec.h"
    2526#include "SynTree/Type.h"
    2627#include "SynTree/Expression.h"
    2728#include "SynTree/Statement.h"
     29//#include "SynTree/Declaration.h"
     30#include "Common/UniqueName.h"
    2831#include "SynTree/Label.h"
    29 #include "Common/utility.h"
    30 #include "Common/UniqueName.h"
    3132
    3233class StatementNode;
     
    3637class InitializerNode;
    3738
    38 //##############################################################################
    39 
     39// Builder
    4040class ParseNode {
    4141  public:
    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         }
     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; };
    5953
    6054        const std::string &get_name() const { return name; }
    6155        void set_name( const std::string &newValue ) { name = newValue; }
    6256
    63         virtual void print( std::ostream &os, int indent = 0 ) const {}
    64         virtual void printList( std::ostream &os, int indent = 0 ) const {}
    65   private:
     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:
     62        std::string name;
    6663        static int indent_by;
    67 
    68         ParseNode *next = nullptr;
    69         std::string name;
    70 }; // ParseNode
     64        ParseNode *next;
     65};
     66
     67ParseNode *mkList( ParseNode & );
    7168
    7269//##############################################################################
     
    7774        InitializerNode( InitializerNode *, bool aggrp = false, ExpressionNode *des = 0 );
    7875        ~InitializerNode();
    79         virtual InitializerNode *clone() const { assert( false ); return nullptr; }
    8076
    8177        ExpressionNode *get_expression() const { return expr; }
     
    9692        ExpressionNode *expr;
    9793        bool aggregate;
    98         ExpressionNode *designator;                                                     // may be list
     94        ExpressionNode *designator; // may be list
    9995        InitializerNode *kids;
    10096        bool maybeConstructed;
    101 }; // InitializerNode
    102 
    103 //##############################################################################
    104 
    105 class ExpressionNode final : public ParseNode {
     97};
     98
     99//##############################################################################
     100
     101class ExpressionNode : public ParseNode {
    106102  public:
    107103        ExpressionNode( Expression * expr = nullptr ) : expr( expr ) {}
     
    109105        ExpressionNode( const ExpressionNode &other );
    110106        virtual ~ExpressionNode() {}
    111         virtual ExpressionNode *clone() const { return expr ? new ExpressionNode( expr->clone() ) : nullptr; }
     107
     108        virtual ExpressionNode *clone() const { return 0; }
    112109
    113110        bool get_extension() const { return extension; }
    114111        ExpressionNode *set_extension( bool exten ) { extension = exten; return this; }
    115112
    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(); }
     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; }
    125117  private:
    126118        bool extension = false;
    127         std::unique_ptr<Expression> expr;
    128 }; // ExpressionNode
     119        Expression *expr;
     120};
    129121
    130122template< typename T >
    131 struct maybeBuild_t< Expression, T > {
     123struct maybeBuild_t<Expression, T> {
    132124        static inline Expression * doit( const T *orig ) {
    133125                if ( orig ) {
     
    136128                        return p;
    137129                } else {
    138                         return nullptr;
     130                        return 0;
    139131                } // if
    140132        }
    141133};
     134
     135//##############################################################################
     136
     137Expression *build_constantInteger( std::string &str );
     138Expression *build_constantFloat( std::string &str );
     139Expression *build_constantChar( std::string &str );
     140ConstantExpr *build_constantStr( std::string &str );
     141
     142//##############################################################################
     143
     144NameExpr *build_varref( const std::string *name, bool labelp = false );
     145
     146//##############################################################################
     147
     148Expression *build_typevalue( DeclarationNode *decl );
     149
     150//##############################################################################
    142151
    143152enum class OperKinds {
     
    145154        SizeOf, AlignOf, OffsetOf, Plus, Minus, Mul, Div, Mod, Or, And,
    146155        BitOr, BitAnd, Xor, Cast, LShift, RShift, LThan, GThan, LEThan, GEThan, Eq, Neq,
    147         Assign, AtAssn, MulAssn, DivAssn, ModAssn, PlusAssn, MinusAssn, LSAssn, RSAssn, AndAssn, ERAssn, OrAssn,
     156        Assign, MulAssn, DivAssn, ModAssn, PlusAssn, MinusAssn, LSAssn, RSAssn, AndAssn, ERAssn, OrAssn,
    148157        Index, Range,
    149158        // monadic
    150159        UnPlus, UnMinus, AddressOf, PointTo, Neg, BitNeg, Incr, IncrPost, Decr, DecrPost, LabelAddress,
    151160        Ctor, Dtor,
    152 }; // OperKinds
    153 
    154 struct LabelNode {
    155         std::list< Label > labels;
    156 };
    157 
    158 Expression *build_constantInteger( const std::string &str );
    159 Expression *build_constantFloat( const std::string &str );
    160 Expression *build_constantChar( const std::string &str );
    161 ConstantExpr *build_constantStr( const std::string &str );
    162 
    163 NameExpr *build_varref( const std::string *name, bool labelp = false );
    164 Expression *build_typevalue( DeclarationNode *decl );
     161};
    165162
    166163Expression *build_cast( DeclarationNode * decl_node, ExpressionNode *expr_node );
     
    186183Expression *build_func( ExpressionNode * function, ExpressionNode * expr_node );
    187184Expression *build_range( ExpressionNode * low, ExpressionNode *high );
    188 Expression *build_asmexpr( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand );
     185
     186//##############################################################################
     187
     188Expression *build_asm( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand );
     189
     190//##############################################################################
     191
     192class 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
    189208Expression *build_valexpr( StatementNode *s );
     209
     210//##############################################################################
     211
    190212Expression *build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids );
    191213
     
    196218class DeclarationNode : public ParseNode {
    197219  public:
    198         enum Qualifier { Const, Restrict, Volatile, Lvalue, Atomic, NoOfQualifier };
     220        enum Qualifier { Const, Restrict, Volatile, Lvalue, Atomic };
    199221        enum StorageClass { Extern, Static, Auto, Register, Inline, Fortran, Noreturn, Threadlocal, NoStorageClass, };
    200222        enum BasicType { Char, Int, Float, Double, Void, Bool, Complex, Imaginary };
     
    204226        enum BuiltinType { Valist };
    205227
     228        static const char *storageName[];
    206229        static const char *qualifierName[];
    207         static const char *storageName[];
    208230        static const char *basicTypeName[];
    209231        static const char *modifierName[];
     
    214236        static DeclarationNode *newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle = false );
    215237        static DeclarationNode *newQualifier( Qualifier );
    216         static DeclarationNode *newForall( DeclarationNode *);
    217238        static DeclarationNode *newStorageClass( StorageClass );
    218239        static DeclarationNode *newBasicType( BasicType );
    219240        static DeclarationNode *newModifier( Modifier );
    220         static DeclarationNode *newBuiltinType( BuiltinType );
     241        static DeclarationNode *newForall( DeclarationNode *);
    221242        static DeclarationNode *newFromTypedef( std::string *);
    222243        static DeclarationNode *newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields, bool body );
     
    237258        static DeclarationNode *newAttr( std::string *, ExpressionNode *expr );
    238259        static DeclarationNode *newAttr( std::string *, DeclarationNode *type );
    239 
    240         DeclarationNode();
    241         ~DeclarationNode();
    242         DeclarationNode *clone() const;
     260        static DeclarationNode *newBuiltinType( BuiltinType );
    243261
    244262        DeclarationNode *addQualifiers( DeclarationNode *);
    245         void checkQualifiers( const TypeData *, const TypeData * );
    246263        DeclarationNode *copyStorageClasses( DeclarationNode *);
    247264        DeclarationNode *addType( DeclarationNode *);
     
    258275        DeclarationNode *addNewArray( DeclarationNode *array );
    259276        DeclarationNode *addParamList( DeclarationNode *list );
    260         DeclarationNode *addIdList( DeclarationNode *list ); // old-style functions
     277        DeclarationNode *addIdList( DeclarationNode *list );       // old-style functions
    261278        DeclarationNode *addInitializer( InitializerNode *init );
    262279
     
    267284        DeclarationNode *cloneBaseType( DeclarationNode *newdecl );
    268285
    269         DeclarationNode *appendList( DeclarationNode *node ) {
    270                 return (DeclarationNode *)set_last( node );
    271         }
    272 
     286        DeclarationNode *appendList( DeclarationNode * );
     287
     288        DeclarationNode *clone() const;
    273289        void print( std::ostream &os, int indent = 0 ) const;
    274290        void printList( std::ostream &os, int indent = 0 ) const;
     
    279295        bool get_hasEllipsis() const;
    280296        const std::string &get_name() const { return name; }
    281         LinkageSpec::Spec get_linkage() const { return linkage; }
     297        LinkageSpec::Type get_linkage() const { return linkage; }
    282298        DeclarationNode *extractAggregate() const;
    283         bool has_enumeratorValue() const { return (bool)enumeratorValue; }
    284         ExpressionNode *consume_enumeratorValue() const { return const_cast<DeclarationNode*>(this)->enumeratorValue.release(); }
     299        ExpressionNode *get_enumeratorValue() const { return enumeratorValue; }
    285300
    286301        bool get_extension() const { return extension; }
    287302        DeclarationNode *set_extension( bool exten ) { extension = exten; return this; }
    288   public:
    289         // StorageClass buildStorageClass() const;
    290         // bool buildFuncSpecifier( StorageClass key ) const;
     303
     304        DeclarationNode();
     305        ~DeclarationNode();
     306  private:
     307        StorageClass buildStorageClass() const;
     308        bool buildFuncSpecifier( StorageClass key ) const;
    291309
    292310        TypeData *type;
    293311        std::string name;
    294         // std::list< StorageClass > storageClasses;
    295         StorageClass storageClass;
    296         bool isInline, isNoreturn;
     312        std::list< StorageClass > storageClasses;
    297313        std::list< std::string > attributes;
    298314        ExpressionNode *bitfieldWidth;
    299         std::unique_ptr<ExpressionNode> enumeratorValue;
     315        ExpressionNode *enumeratorValue;
    300316        InitializerNode *initializer;
    301317        bool hasEllipsis;
    302         LinkageSpec::Spec linkage;
     318        LinkageSpec::Type linkage;
    303319        bool extension = false;
    304         std::string error;
    305320
    306321        static UniqueName anonymous;
     
    308323
    309324Type *buildType( TypeData *type );
    310 //Type::Qualifiers buildQualifiers( const TypeData::Qualifiers & qualifiers );
    311 
    312 static inline Type * maybeMoveBuildType( const DeclarationNode *orig ) {
    313         Type* ret = orig ? orig->buildType() : nullptr;
    314         delete orig;
    315         return ret;
    316 }
    317 
    318 //##############################################################################
    319 
    320 class StatementNode final : public ParseNode {
    321   public:
    322         StatementNode() { stmt = nullptr; }
    323         StatementNode( Statement *stmt ) : stmt( stmt ) {}
     325
     326//##############################################################################
     327
     328class 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 );
    324341        StatementNode( DeclarationNode *decl );
    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 ) {
     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 * );
     365        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
     381class 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 ) {
    331391                stmt->get_labels().emplace_back( *name );
    332                 delete name;
    333392                return this;
    334393        }
    335 
    336394        virtual StatementNode *append_last_case( StatementNode * );
     395        virtual std::list<std::string> get_labels() const { assert( false ); return StatementNode::get_labels(); }
    337396
    338397        virtual void print( std::ostream &os, int indent = 0 ) {}
    339398        virtual void printList( std::ostream &os, int indent = 0 ) {}
    340399  private:
    341         std::unique_ptr<Statement> stmt;
     400        Statement *stmt;
    342401}; // StatementNode
    343 
    344 Statement *build_expr( ExpressionNode *ctl );
    345402
    346403struct ForCtl {
    347404        ForCtl( ExpressionNode *expr, ExpressionNode *condition, ExpressionNode *change ) :
    348                 init( new StatementNode( build_expr( expr ) ) ), condition( condition ), change( change ) {}
     405                init( new StatementNode( StatementNode::Exp, expr ) ), condition( condition ), change( change ) {}
    349406        ForCtl( DeclarationNode *decl, ExpressionNode *condition, ExpressionNode *change ) :
    350407                init( new StatementNode( decl ) ), condition( condition ), change( change ) {}
     
    355412};
    356413
     414Statement *build_expr( ExpressionNode *ctl );
    357415Statement *build_if( ExpressionNode *ctl, StatementNode *then_stmt, StatementNode *else_stmt );
    358416Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt );
     
    361419Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind = false );
    362420Statement *build_for( ForCtl *forctl, StatementNode *stmt );
    363 Statement *build_branch( BranchStmt::Type kind );
    364 Statement *build_branch( std::string *identifier, BranchStmt::Type kind );
     421Statement *build_branch( std::string identifier, BranchStmt::Type kind );
    365422Statement *build_computedgoto( ExpressionNode *ctl );
    366423Statement *build_return( ExpressionNode *ctl );
    367424Statement *build_throw( ExpressionNode *ctl );
    368 Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt );
    369 Statement *build_catch( DeclarationNode *decl, StatementNode *stmt, bool catchAny = false );
    370 Statement *build_finally( StatementNode *stmt );
    371 Statement *build_compound( StatementNode *first );
    372 Statement *build_asmstmt( bool voltile, ConstantExpr *instruction, ExpressionNode *output = 0, ExpressionNode *input = 0, ExpressionNode *clobber = 0, LabelNode *gotolabels = 0 );
     425
     426//##############################################################################
     427
     428class 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
     445class 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};
    373459
    374460//##############################################################################
     
    377463void buildList( const NodeType *firstNode, std::list< SynTreeType * > &outputList ) {
    378464        SemanticError errors;
    379         std::back_insert_iterator< std::list< SynTreeType * > > out( outputList );
     465        std::back_insert_iterator< std::list< SynTreeType *> > out( outputList );
    380466        const NodeType *cur = firstNode;
    381467
    382468        while ( cur ) {
    383469                try {
    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 ) );
     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 ) );
    386472                        if ( result ) {
    387473                                *out++ = result;
     
    391477                        errors.append( e );
    392478                } // try
    393                 cur = dynamic_cast< NodeType * >( cur->get_next() );
     479                cur = dynamic_cast< NodeType *>( cur->get_link() );
    394480        } // while
    395481        if ( ! errors.isEmpty() ) {
     
    400486// in DeclarationNode.cc
    401487void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList );
    402 void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType * > &outputList );
     488void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList );
    403489void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList );
    404 
    405 template< typename SynTreeType, typename NodeType >
    406 void buildMoveList( const NodeType *firstNode, std::list< SynTreeType * > &outputList ) {
    407         buildList(firstNode, outputList);
    408         delete firstNode;
    409 }
    410 
    411490
    412491#endif // PARSENODE_H
Note: See TracChangeset for help on using the changeset viewer.