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