Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/ParseNode.h

    rac71a86 re82aa9df  
    1010// Created On       : Sat May 16 13:28:16 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Aug 18 23:48:37 2016
    13 // Update Count     : 542
     12// Last Modified On : Mon Aug 15 14:52:12 2016
     13// Update Count     : 512
    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;
     42        ParseNode();
     43        ParseNode( const std::string * );
     44        ParseNode( const std::string & );                                       // for copy constructing subclasses
     45        virtual ~ParseNode();
    4746
    4847        ParseNode *get_next() const { return next; }
    4948        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         }
     49        ParseNode *get_last();
     50        ParseNode *set_last( ParseNode * );
     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};
    7166
    7267//##############################################################################
     
    7772        InitializerNode( InitializerNode *, bool aggrp = false, ExpressionNode *des = 0 );
    7873        ~InitializerNode();
    79         virtual InitializerNode *clone() const { assert( false ); return nullptr; }
    8074
    8175        ExpressionNode *get_expression() const { return expr; }
     
    10397//##############################################################################
    10498
    105 class ExpressionNode final : public ParseNode {
     99class ExpressionNode : public ParseNode {
    106100  public:
    107101        ExpressionNode( Expression * expr = nullptr ) : expr( expr ) {}
     
    109103        ExpressionNode( const ExpressionNode &other );
    110104        virtual ~ExpressionNode() {}
    111         virtual ExpressionNode *clone() const { assert( false ); return nullptr; }
     105
     106        virtual ExpressionNode *clone() const { return 0; }
    112107
    113108        bool get_extension() const { return extension; }
    114109        ExpressionNode *set_extension( bool exten ) { extension = exten; return this; }
    115110
    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(); }
     111        virtual void print( std::ostream &os, int indent = 0 ) const {}
     112        virtual void printOneLine( std::ostream &os, int indent = 0 ) const {}
     113
     114        virtual Expression *build() const { return expr; }
    125115  private:
    126116        bool extension = false;
    127         std::unique_ptr<Expression> expr;
     117        Expression *expr;
    128118};
    129119
    130120template< typename T >
    131 struct maybeBuild_t< Expression, T > {
     121struct maybeBuild_t<Expression, T> {
    132122        static inline Expression * doit( const T *orig ) {
    133123                if ( orig ) {
     
    136126                        return p;
    137127                } else {
    138                         return nullptr;
     128                        return 0;
    139129                } // if
    140130        }
     
    156146};
    157147
    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 );
     148Expression *build_constantInteger( std::string &str );
     149Expression *build_constantFloat( std::string &str );
     150Expression *build_constantChar( std::string &str );
     151ConstantExpr *build_constantStr( std::string &str );
    162152
    163153NameExpr *build_varref( const std::string *name, bool labelp = false );
     
    238228        static DeclarationNode *newBuiltinType( BuiltinType );
    239229
    240         DeclarationNode();
    241         ~DeclarationNode();
    242         DeclarationNode *clone() const;
    243 
    244230        DeclarationNode *addQualifiers( DeclarationNode *);
    245231        DeclarationNode *copyStorageClasses( DeclarationNode *);
     
    266252        DeclarationNode *cloneBaseType( DeclarationNode *newdecl );
    267253
    268         DeclarationNode *appendList( DeclarationNode *node ) {
    269                 return (DeclarationNode *)set_last( node );
    270         }
    271 
     254        DeclarationNode *appendList( DeclarationNode * );
     255
     256        DeclarationNode *clone() const;
    272257        void print( std::ostream &os, int indent = 0 ) const;
    273258        void printList( std::ostream &os, int indent = 0 ) const;
     
    278263        bool get_hasEllipsis() const;
    279264        const std::string &get_name() const { return name; }
    280         LinkageSpec::Spec get_linkage() const { return linkage; }
     265        LinkageSpec::Type get_linkage() const { return linkage; }
    281266        DeclarationNode *extractAggregate() const;
    282267        ExpressionNode *get_enumeratorValue() const { return enumeratorValue; }
     
    284269        bool get_extension() const { return extension; }
    285270        DeclarationNode *set_extension( bool exten ) { extension = exten; return this; }
     271
     272        DeclarationNode();
     273        ~DeclarationNode();
    286274  private:
    287         // StorageClass buildStorageClass() const;
    288         // bool buildFuncSpecifier( StorageClass key ) const;
     275        StorageClass buildStorageClass() const;
     276        bool buildFuncSpecifier( StorageClass key ) const;
    289277
    290278        TypeData *type;
    291279        std::string name;
    292         // std::list< StorageClass > storageClasses;
    293         StorageClass storageClass;
    294         bool isInline, isNoreturn;
     280        std::list< StorageClass > storageClasses;
    295281        std::list< std::string > attributes;
    296282        ExpressionNode *bitfieldWidth;
     
    298284        InitializerNode *initializer;
    299285        bool hasEllipsis;
    300         LinkageSpec::Spec linkage;
     286        LinkageSpec::Type linkage;
    301287        bool extension = false;
    302         std::string error;
    303288
    304289        static UniqueName anonymous;
     
    309294//##############################################################################
    310295
    311 class StatementNode final : public ParseNode {
     296class StatementNode : public ParseNode {
    312297  public:
    313298        StatementNode() { stmt = nullptr; }
     
    316301        virtual ~StatementNode() {}
    317302
    318         virtual StatementNode *clone() const final { assert( false ); return nullptr; }
    319         Statement *build() const { return const_cast<StatementNode*>(this)->stmt.release(); }
     303        virtual StatementNode *clone() const { assert( false ); return nullptr; }
     304        virtual Statement *build() const { return stmt; }
    320305
    321306        virtual StatementNode *add_label( const std::string * name ) {
    322307                stmt->get_labels().emplace_back( *name );
    323                 delete name;
    324308                return this;
    325309        }
     
    330314        virtual void printList( std::ostream &os, int indent = 0 ) {}
    331315  private:
    332         std::unique_ptr<Statement> stmt;
     316        Statement *stmt;
    333317}; // StatementNode
    334318
     
    367351void buildList( const NodeType *firstNode, std::list< SynTreeType * > &outputList ) {
    368352        SemanticError errors;
    369         std::back_insert_iterator< std::list< SynTreeType * > > out( outputList );
     353        std::back_insert_iterator< std::list< SynTreeType *> > out( outputList );
    370354        const NodeType *cur = firstNode;
    371355
    372356        while ( cur ) {
    373357                try {
    374 //                      SynTreeType *result = dynamic_cast< SynTreeType * >( maybeBuild< typename std::result_of< decltype(&NodeType::build)(NodeType)>::type >( cur ) );
    375                         SynTreeType *result = dynamic_cast< SynTreeType * >( maybeBuild< typename std::pointer_traits< decltype(cur->build())>::element_type >( cur ) );
     358//                      SynTreeType *result = dynamic_cast< SynTreeType *>( maybeBuild<typename std::result_of<decltype(&NodeType::build)(NodeType)>::type>( cur ) );
     359                        SynTreeType *result = dynamic_cast< SynTreeType *>( maybeBuild<typename std::pointer_traits<decltype(cur->build())>::element_type>( cur ) );
    376360                        if ( result ) {
    377361                                *out++ = result;
     
    381365                        errors.append( e );
    382366                } // try
    383                 cur = dynamic_cast< NodeType * >( cur->get_next() );
     367                cur = dynamic_cast< NodeType *>( cur->get_next() );
    384368        } // while
    385369        if ( ! errors.isEmpty() ) {
     
    390374// in DeclarationNode.cc
    391375void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList );
    392 void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType * > &outputList );
     376void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList );
    393377void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList );
    394 
    395 template< typename SynTreeType, typename NodeType >
    396 void buildMoveList( const NodeType *firstNode, std::list< SynTreeType * > &outputList ) {
    397         buildList(firstNode, outputList);
    398         delete firstNode;
    399 }
    400 
    401378
    402379#endif // PARSENODE_H
Note: See TracChangeset for help on using the changeset viewer.