Changes in src/Parser/ParseNode.h [8cc5cb0:413ad05]
- File:
-
- 1 edited
-
src/Parser/ParseNode.h (modified) (22 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/ParseNode.h
r8cc5cb0 r413ad05 10 10 // Created On : Sat May 16 13:28:16 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Aug 11 12:24:11 201613 // Update Count : 44312 // Last Modified On : Sun Aug 28 21:14:51 2016 13 // Update Count : 575 14 14 // 15 15 … … 22 22 #include <memory> 23 23 24 #include "Common/utility.h"25 24 #include "Parser/LinkageSpec.h" 26 25 #include "SynTree/Type.h" 27 26 #include "SynTree/Expression.h" 28 27 #include "SynTree/Statement.h" 29 //#include "SynTree/Declaration.h" 28 #include "SynTree/Label.h" 29 #include "Common/utility.h" 30 30 #include "Common/UniqueName.h" 31 #include "SynTree/Label.h"32 31 33 32 class StatementNode; … … 37 36 class InitializerNode; 38 37 39 // Builder 38 //############################################################################## 39 40 40 class ParseNode { 41 41 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 } 53 59 54 60 const std::string &get_name() const { return name; } 55 61 void set_name( const std::string &newValue ) { name = newValue; } 56 62 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; 62 69 std::string name; 63 static int indent_by; 64 ParseNode *next; 65 }; 66 67 ParseNode *mkList( ParseNode & ); 70 }; // ParseNode 68 71 69 72 //############################################################################## … … 74 77 InitializerNode( InitializerNode *, bool aggrp = false, ExpressionNode *des = 0 ); 75 78 ~InitializerNode(); 79 virtual InitializerNode *clone() const { assert( false ); return nullptr; } 76 80 77 81 ExpressionNode *get_expression() const { return expr; } … … 92 96 ExpressionNode *expr; 93 97 bool aggregate; 94 ExpressionNode *designator; // may be list98 ExpressionNode *designator; // may be list 95 99 InitializerNode *kids; 96 100 bool maybeConstructed; 97 }; 98 99 //############################################################################## 100 101 class ExpressionNode : public ParseNode {101 }; // InitializerNode 102 103 //############################################################################## 104 105 class ExpressionNode final : public ParseNode { 102 106 public: 103 107 ExpressionNode( Expression * expr = nullptr ) : expr( expr ) {} … … 105 109 ExpressionNode( const ExpressionNode &other ); 106 110 virtual ~ExpressionNode() {} 107 108 virtual ExpressionNode *clone() const { return 0; } 111 virtual ExpressionNode *clone() const { return expr ? new ExpressionNode( expr->clone() ) : nullptr; } 109 112 110 113 bool get_extension() const { return extension; } 111 114 ExpressionNode *set_extension( bool exten ) { extension = exten; return this; } 112 115 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(); } 117 125 private: 118 126 bool extension = false; 119 Expression *expr;120 }; 127 std::unique_ptr<Expression> expr; 128 }; // ExpressionNode 121 129 122 130 template< typename T > 123 struct maybeBuild_t< Expression, T> {131 struct maybeBuild_t< Expression, T > { 124 132 static inline Expression * doit( const T *orig ) { 125 133 if ( orig ) { … … 128 136 return p; 129 137 } else { 130 return 0;138 return nullptr; 131 139 } // if 132 140 } 133 141 }; 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 //##############################################################################151 142 152 143 enum class OperKinds { … … 154 145 SizeOf, AlignOf, OffsetOf, Plus, Minus, Mul, Div, Mod, Or, And, 155 146 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, 157 148 Index, Range, 158 149 // monadic 159 150 UnPlus, UnMinus, AddressOf, PointTo, Neg, BitNeg, Incr, IncrPost, Decr, DecrPost, LabelAddress, 160 151 Ctor, Dtor, 152 }; // OperKinds 153 154 struct LabelNode { 155 std::list< Label > labels; 161 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 ); 162 165 163 166 Expression *build_cast( DeclarationNode * decl_node, ExpressionNode *expr_node ); … … 183 186 Expression *build_func( ExpressionNode * function, ExpressionNode * expr_node ); 184 187 Expression *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 188 Expression *build_asmexpr( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand ); 208 189 Expression *build_valexpr( StatementNode *s ); 209 210 //##############################################################################211 212 190 Expression *build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids ); 213 191 … … 218 196 class DeclarationNode : public ParseNode { 219 197 public: 220 enum Qualifier { Const, Restrict, Volatile, Lvalue, Atomic };198 enum Qualifier { Const, Restrict, Volatile, Lvalue, Atomic, NoOfQualifier }; 221 199 enum StorageClass { Extern, Static, Auto, Register, Inline, Fortran, Noreturn, Threadlocal, NoStorageClass, }; 222 200 enum BasicType { Char, Int, Float, Double, Void, Bool, Complex, Imaginary }; … … 226 204 enum BuiltinType { Valist }; 227 205 206 static const char *qualifierName[]; 228 207 static const char *storageName[]; 229 static const char *qualifierName[];230 208 static const char *basicTypeName[]; 231 209 static const char *modifierName[]; … … 236 214 static DeclarationNode *newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle = false ); 237 215 static DeclarationNode *newQualifier( Qualifier ); 216 static DeclarationNode *newForall( DeclarationNode *); 238 217 static DeclarationNode *newStorageClass( StorageClass ); 239 218 static DeclarationNode *newBasicType( BasicType ); 240 219 static DeclarationNode *newModifier( Modifier ); 241 static DeclarationNode *new Forall( DeclarationNode *);220 static DeclarationNode *newBuiltinType( BuiltinType ); 242 221 static DeclarationNode *newFromTypedef( std::string *); 243 222 static DeclarationNode *newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields, bool body ); … … 258 237 static DeclarationNode *newAttr( std::string *, ExpressionNode *expr ); 259 238 static DeclarationNode *newAttr( std::string *, DeclarationNode *type ); 260 static DeclarationNode *newBuiltinType( BuiltinType ); 239 240 DeclarationNode(); 241 ~DeclarationNode(); 242 DeclarationNode *clone() const; 261 243 262 244 DeclarationNode *addQualifiers( DeclarationNode *); 245 void checkQualifiers( const TypeData *, const TypeData * ); 263 246 DeclarationNode *copyStorageClasses( DeclarationNode *); 264 247 DeclarationNode *addType( DeclarationNode *); … … 275 258 DeclarationNode *addNewArray( DeclarationNode *array ); 276 259 DeclarationNode *addParamList( DeclarationNode *list ); 277 DeclarationNode *addIdList( DeclarationNode *list ); // old-style functions260 DeclarationNode *addIdList( DeclarationNode *list ); // old-style functions 278 261 DeclarationNode *addInitializer( InitializerNode *init ); 279 262 … … 284 267 DeclarationNode *cloneBaseType( DeclarationNode *newdecl ); 285 268 286 DeclarationNode *appendList( DeclarationNode * ); 287 288 DeclarationNode *clone() const; 269 DeclarationNode *appendList( DeclarationNode *node ) { 270 return (DeclarationNode *)set_last( node ); 271 } 272 289 273 void print( std::ostream &os, int indent = 0 ) const; 290 274 void printList( std::ostream &os, int indent = 0 ) const; … … 295 279 bool get_hasEllipsis() const; 296 280 const std::string &get_name() const { return name; } 297 LinkageSpec:: Typeget_linkage() const { return linkage; }281 LinkageSpec::Spec get_linkage() const { return linkage; } 298 282 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(); } 300 285 301 286 bool get_extension() const { return extension; } 302 287 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; 309 291 310 292 TypeData *type; 311 293 std::string name; 312 std::list< StorageClass > storageClasses; 294 // std::list< StorageClass > storageClasses; 295 StorageClass storageClass; 296 bool isInline, isNoreturn; 313 297 std::list< std::string > attributes; 314 298 ExpressionNode *bitfieldWidth; 315 ExpressionNode *enumeratorValue;299 std::unique_ptr<ExpressionNode> enumeratorValue; 316 300 InitializerNode *initializer; 317 301 bool hasEllipsis; 318 LinkageSpec:: Typelinkage;302 LinkageSpec::Spec linkage; 319 303 bool extension = false; 304 std::string error; 320 305 321 306 static UniqueName anonymous; … … 323 308 324 309 Type *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 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 ) {} 341 324 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 365 336 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 statements377 DeclarationNode *decl;378 bool isCatchRest;379 }; // StatementNode380 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(); }396 337 397 338 virtual void print( std::ostream &os, int indent = 0 ) {} 398 339 virtual void printList( std::ostream &os, int indent = 0 ) {} 399 340 private: 400 Statement *stmt;341 std::unique_ptr<Statement> stmt; 401 342 }; // StatementNode 343 344 Statement *build_expr( ExpressionNode *ctl ); 402 345 403 346 struct ForCtl { 404 347 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 ) {} 406 349 ForCtl( DeclarationNode *decl, ExpressionNode *condition, ExpressionNode *change ) : 407 350 init( new StatementNode( decl ) ), condition( condition ), change( change ) {} … … 412 355 }; 413 356 414 Statement *build_expr( ExpressionNode *ctl );415 357 Statement *build_if( ExpressionNode *ctl, StatementNode *then_stmt, StatementNode *else_stmt ); 416 358 Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt ); … … 419 361 Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind = false ); 420 362 Statement *build_for( ForCtl *forctl, StatementNode *stmt ); 421 Statement *build_branch( std::string identifier, BranchStmt::Type kind ); 363 Statement *build_branch( BranchStmt::Type kind ); 364 Statement *build_branch( std::string *identifier, BranchStmt::Type kind ); 422 365 Statement *build_computedgoto( ExpressionNode *ctl ); 423 366 Statement *build_return( ExpressionNode *ctl ); 424 367 Statement *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 }; 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 ); 459 373 460 374 //############################################################################## … … 463 377 void buildList( const NodeType *firstNode, std::list< SynTreeType * > &outputList ) { 464 378 SemanticError errors; 465 std::back_insert_iterator< std::list< SynTreeType * > > out( outputList );379 std::back_insert_iterator< std::list< SynTreeType * > > out( outputList ); 466 380 const NodeType *cur = firstNode; 467 381 468 382 while ( cur ) { 469 383 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 ) ); 472 386 if ( result ) { 473 387 *out++ = result; … … 477 391 errors.append( e ); 478 392 } // try 479 cur = dynamic_cast< NodeType * >( cur->get_link() );393 cur = dynamic_cast< NodeType * >( cur->get_next() ); 480 394 } // while 481 395 if ( ! errors.isEmpty() ) { … … 486 400 // in DeclarationNode.cc 487 401 void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList ); 488 void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType * > &outputList );402 void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType * > &outputList ); 489 403 void 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 490 411 491 412 #endif // PARSENODE_H
Note:
See TracChangeset
for help on using the changeset viewer.