source: src/Parser/ParseNode.h@ 8c84ebd

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since 8c84ebd was 7f5566b, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

asm statement, memory leaks

  • Property mode set to 100644
File size: 18.0 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// ParseNode.h --
8//
9// Author : Rodolfo G. Esteves
10// Created On : Sat May 16 13:28:16 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Jul 30 15:11:39 2015
13// Update Count : 141
14//
15
16#ifndef PARSENODE_H
17#define PARSENODE_H
18
19#include <string>
20#include <list>
21#include <iterator>
22
23#include "utility.h"
24#include "Parser/LinkageSpec.h"
25#include "SynTree/Type.h"
26//#include "SynTree/Declaration.h"
27#include "UniqueName.h"
28
29class ExpressionNode;
30class CompositeExprNode;
31class CommaExprNode;
32class StatementNode;
33class CompoundStmtNode;
34class DeclarationNode;
35class InitializerNode;
36
37// Builder
38class ParseNode {
39 public:
40 ParseNode();
41 ParseNode( const std::string * );
42 virtual ~ParseNode();
43
44 ParseNode *get_link() const;
45 ParseNode *get_last();
46 ParseNode *set_link( ParseNode * );
47 void set_next( ParseNode *newlink ) { next = newlink; }
48
49 virtual ParseNode *clone() const { return 0; };
50
51 const std::string &get_name() const { return *name; }
52 virtual void print( std::ostream &, int indent = 0 ) const;
53 virtual void printList( std::ostream &, int indent = 0 ) const;
54
55 ParseNode &operator,( ParseNode &);
56 protected:
57 const std::string *name;
58 ParseNode *next;
59 static int indent_by;
60};
61
62ParseNode *mkList( ParseNode & );
63
64class ExpressionNode : public ParseNode {
65 public:
66 ExpressionNode();
67 ExpressionNode( const std::string * );
68 ExpressionNode( const ExpressionNode &other );
69 virtual ~ExpressionNode() { delete argName; } // cannot delete argName because it might be referenced elsewhere
70
71 virtual ExpressionNode *clone() const = 0;
72
73 virtual CommaExprNode *add_to_list( ExpressionNode * );
74
75 ExpressionNode *get_argName() const { return argName; }
76 ExpressionNode *set_argName( const std::string *aName );
77 ExpressionNode *set_argName( ExpressionNode *aDesignator );
78
79 virtual void print( std::ostream &, int indent = 0) const = 0;
80 virtual void printOneLine( std::ostream &, int indent = 0) const = 0;
81
82 virtual Expression *build() const = 0;
83 protected:
84 void printDesignation ( std::ostream &, int indent = 0) const;
85 private:
86 ExpressionNode *argName;
87};
88
89// NullExprNode is used in tuples as a place-holder where a tuple component is omitted e.g., [ 2, , 3 ]
90class NullExprNode : public ExpressionNode {
91 public:
92 NullExprNode();
93
94 virtual NullExprNode *clone() const;
95
96 virtual void print( std::ostream &, int indent = 0) const;
97 virtual void printOneLine( std::ostream &, int indent = 0) const;
98
99 virtual Expression *build() const;
100};
101
102class ConstantNode : public ExpressionNode {
103 public:
104 enum Type { Integer, Float, Character, String };
105
106 ConstantNode( Type, std::string * );
107 ~ConstantNode() { delete &value; }
108
109 virtual ConstantNode *clone() const { return new ConstantNode( *this ); }
110 Type get_type( void ) const { return type; }
111 virtual void print( std::ostream &, int indent = 0) const;
112 virtual void printOneLine( std::ostream &, int indent = 0) const;
113
114 const std::string &get_value() const { return value; }
115 ConstantNode *appendstr( const std::string *newValue );
116
117 Expression *build() const;
118 private:
119 Type type;
120 BasicType::Kind btype;
121 std::string &value;
122};
123
124class VarRefNode : public ExpressionNode {
125 public:
126 VarRefNode();
127 VarRefNode( const std::string *, bool isLabel = false );
128 VarRefNode( const VarRefNode &other );
129
130 virtual Expression *build() const ;
131
132 virtual VarRefNode *clone() const { return new VarRefNode( *this ); }
133
134 virtual void print( std::ostream &, int indent = 0 ) const;
135 virtual void printOneLine( std::ostream &, int indent = 0 ) const;
136 private:
137 bool isLabel;
138};
139
140class TypeValueNode : public ExpressionNode {
141 public:
142 TypeValueNode( DeclarationNode * );
143 TypeValueNode( const TypeValueNode &other );
144
145 DeclarationNode *get_decl() const { return decl; }
146
147 virtual Expression *build() const ;
148
149 virtual TypeValueNode *clone() const { return new TypeValueNode( *this ); }
150
151 virtual void print( std::ostream &, int indent = 0) const;
152 virtual void printOneLine( std::ostream &, int indent = 0) const;
153 private:
154 DeclarationNode *decl;
155};
156
157class OperatorNode : public ExpressionNode {
158 public:
159 enum Type { TupleC, Comma, TupleFieldSel,
160 Cond, NCond,
161 SizeOf, AlignOf, Attr, CompLit, Plus, Minus, Mul, Div, Mod, Or, And,
162 BitOr, BitAnd, Xor, Cast, LShift, RShift, LThan, GThan, LEThan, GEThan, Eq, Neq,
163 Assign, MulAssn, DivAssn, ModAssn, PlusAssn, MinusAssn, LSAssn, RSAssn, AndAssn,
164 ERAssn, OrAssn, Index, FieldSel, PFieldSel, Range,
165 UnPlus, UnMinus, AddressOf, PointTo, Neg, BitNeg, Incr, IncrPost, Decr, DecrPost, LabelAddress
166 };
167
168 OperatorNode( Type t );
169 OperatorNode( const OperatorNode &other );
170 virtual ~OperatorNode();
171
172 virtual OperatorNode *clone() const { return new OperatorNode( *this ); }
173
174 Type get_type() const;
175 const char *get_typename() const;
176
177 virtual void print( std::ostream &, int indent = 0) const;
178 virtual void printOneLine( std::ostream &, int indent = 0) const;
179
180 virtual Expression *build() const { return 0; }
181 private:
182 Type type;
183 static const char *OpName[];
184};
185
186class CompositeExprNode : public ExpressionNode {
187 public:
188 CompositeExprNode();
189 CompositeExprNode( const std::string * );
190 CompositeExprNode( ExpressionNode *f, ExpressionNode *args = 0 );
191 CompositeExprNode( ExpressionNode *f, ExpressionNode *arg1, ExpressionNode *arg2 );
192 CompositeExprNode( const CompositeExprNode &other );
193 virtual ~CompositeExprNode();
194
195 virtual CompositeExprNode *clone() const { return new CompositeExprNode( *this ); }
196 virtual Expression *build() const;
197
198 virtual void print( std::ostream &, int indent = 0) const;
199 virtual void printOneLine( std::ostream &, int indent = 0) const;
200
201 void set_function( ExpressionNode * );
202 void set_args( ExpressionNode * );
203
204 void add_arg( ExpressionNode * );
205
206 ExpressionNode *get_function() const;
207 ExpressionNode *get_args() const;
208 private:
209 ExpressionNode *function;
210 ExpressionNode *arguments;
211};
212
213class AsmExprNode : public ExpressionNode {
214 public:
215 AsmExprNode();
216 AsmExprNode( ExpressionNode *inout, ConstantNode *constraint, ExpressionNode *operand ) : inout( inout ), constraint( constraint ), operand( operand ) {}
217 virtual ~AsmExprNode() { delete inout; delete constraint; delete operand; }
218
219 virtual AsmExprNode *clone() const { return new AsmExprNode( *this ); }
220 virtual Expression *build() const;
221
222 virtual void print( std::ostream &, int indent = 0) const;
223 virtual void printOneLine( std::ostream &, int indent = 0) const;
224
225 ExpressionNode *get_inout() const { return inout; };
226 void set_inout( ExpressionNode *newValue ) { inout = newValue; }
227
228 ConstantNode *get_constraint() const { return constraint; };
229 void set_constraint( ConstantNode *newValue ) { constraint = newValue; }
230
231 ExpressionNode *get_operand() const { return operand; };
232 void set_operand( ExpressionNode *newValue ) { operand = newValue; }
233 private:
234 ExpressionNode *inout;
235 ConstantNode *constraint;
236 ExpressionNode *operand;
237};
238
239class LabelNode : public ExpressionNode {
240 public:
241 virtual Expression *build() const { return NULL; }
242 virtual LabelNode *clone() const { return new LabelNode( *this ); }
243
244 virtual void print( std::ostream &, int indent = 0) const;
245 virtual void printOneLine( std::ostream &, int indent = 0) const;
246
247 const std::list< std::string > &get_labels() const { return labels; };
248 void append_label( std::string *label ) { labels.push_back( *label ); delete label; }
249 private:
250 std::list< std::string > labels;
251};
252
253class CommaExprNode : public CompositeExprNode {
254 public:
255 CommaExprNode();
256 CommaExprNode( ExpressionNode * );
257 CommaExprNode( ExpressionNode *, ExpressionNode * );
258 CommaExprNode( const CommaExprNode &other );
259
260 virtual CommaExprNode *add_to_list( ExpressionNode * );
261 virtual CommaExprNode *clone() const { return new CommaExprNode( *this ); }
262};
263
264class ForCtlExprNode : public ExpressionNode {
265 public:
266 ForCtlExprNode( ParseNode *, ExpressionNode *, ExpressionNode * ) throw ( SemanticError );
267 ForCtlExprNode( const ForCtlExprNode &other );
268 ~ForCtlExprNode();
269
270 StatementNode *get_init() const { return init; }
271 ExpressionNode *get_condition() const { return condition; }
272 ExpressionNode *get_change() const { return change; }
273
274 virtual ForCtlExprNode *clone() const { return new ForCtlExprNode( *this ); }
275 virtual Expression *build() const;
276
277 virtual void print( std::ostream &, int indent = 0 ) const;
278 virtual void printOneLine( std::ostream &, int indent = 0 ) const;
279 private:
280 StatementNode *init;
281 ExpressionNode *condition;
282 ExpressionNode *change;
283};
284
285class ValofExprNode : public ExpressionNode {
286 public:
287 ValofExprNode();
288 ValofExprNode( StatementNode *s = 0 );
289 ValofExprNode( const ValofExprNode &other );
290 ~ValofExprNode();
291
292 virtual ValofExprNode *clone() const { return new ValofExprNode( *this ); }
293
294 StatementNode *get_body() const { return body; }
295 void print( std::ostream &, int indent = 0 ) const;
296 void printOneLine( std::ostream &, int indent = 0 ) const;
297 Expression *build() const;
298
299 private:
300 StatementNode *body;
301};
302
303class TypeData;
304
305class DeclarationNode : public ParseNode {
306 public:
307 enum Qualifier { Const, Restrict, Volatile, Lvalue, Atomic };
308 enum StorageClass { Extern, Static, Auto, Register, Inline, Fortran, Noreturn, Threadlocal, NoStorageClass, };
309 enum BasicType { Char, Int, Float, Double, Void, Bool, Complex, Imaginary };
310 enum Modifier { Signed, Unsigned, Short, Long };
311 enum Aggregate { Struct, Union, Context };
312 enum TypeClass { Type, Dtype, Ftype };
313
314 static const char *storageName[];
315 static const char *qualifierName[];
316 static const char *basicTypeName[];
317 static const char *modifierName[];
318 static const char *aggregateName[];
319 static const char *typeClassName[];
320
321 static DeclarationNode *newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle = false );
322 static DeclarationNode *newQualifier( Qualifier );
323 static DeclarationNode *newStorageClass( StorageClass );
324 static DeclarationNode *newBasicType( BasicType );
325 static DeclarationNode *newModifier( Modifier );
326 static DeclarationNode *newForall( DeclarationNode *);
327 static DeclarationNode *newFromTypedef( std::string *);
328 static DeclarationNode *newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields );
329 static DeclarationNode *newEnum( std::string *name, DeclarationNode *constants );
330 static DeclarationNode *newEnumConstant( std::string *name, ExpressionNode *constant );
331 static DeclarationNode *newName( std::string *);
332 static DeclarationNode *newFromTypeGen( std::string *, ExpressionNode *params );
333 static DeclarationNode *newTypeParam( TypeClass, std::string *);
334 static DeclarationNode *newContext( std::string *name, DeclarationNode *params, DeclarationNode *asserts );
335 static DeclarationNode *newContextUse( std::string *name, ExpressionNode *params );
336 static DeclarationNode *newTypeDecl( std::string *name, DeclarationNode *typeParams );
337 static DeclarationNode *newPointer( DeclarationNode *qualifiers );
338 static DeclarationNode *newArray( ExpressionNode *size, DeclarationNode *qualifiers, bool isStatic );
339 static DeclarationNode *newVarArray( DeclarationNode *qualifiers );
340 static DeclarationNode *newBitfield( ExpressionNode *size );
341 static DeclarationNode *newTuple( DeclarationNode *members );
342 static DeclarationNode *newTypeof( ExpressionNode *expr );
343 static DeclarationNode *newAttr( std::string *, ExpressionNode *expr );
344 static DeclarationNode *newAttr( std::string *, DeclarationNode *type );
345
346 DeclarationNode *addQualifiers( DeclarationNode *);
347 DeclarationNode *copyStorageClasses( DeclarationNode *);
348 DeclarationNode *addType( DeclarationNode *);
349 DeclarationNode *addTypedef();
350 DeclarationNode *addAssertions( DeclarationNode *);
351 DeclarationNode *addName( std::string *);
352 DeclarationNode *addBitfield( ExpressionNode *size );
353 DeclarationNode *addVarArgs();
354 DeclarationNode *addFunctionBody( StatementNode *body );
355 DeclarationNode *addOldDeclList( DeclarationNode *list );
356 DeclarationNode *addPointer( DeclarationNode *qualifiers );
357 DeclarationNode *addArray( DeclarationNode *array );
358 DeclarationNode *addNewPointer( DeclarationNode *pointer );
359 DeclarationNode *addNewArray( DeclarationNode *array );
360 DeclarationNode *addParamList( DeclarationNode *list );
361 DeclarationNode *addIdList( DeclarationNode *list ); // old-style functions
362 DeclarationNode *addInitializer( InitializerNode *init );
363
364 DeclarationNode *cloneType( std::string *newName );
365 DeclarationNode *cloneType( DeclarationNode *existing );
366 DeclarationNode *cloneType( int ) { return cloneType( ( std::string *)0 ); }
367 DeclarationNode *cloneBaseType( std::string *newName );
368 DeclarationNode *cloneBaseType( DeclarationNode *newdecl );
369
370 DeclarationNode *appendList( DeclarationNode * );
371
372 DeclarationNode *clone() const;
373 void print( std::ostream &, int indent = 0 ) const;
374 void printList( std::ostream &, int indent = 0 ) const;
375
376 Declaration *build() const;
377 ::Type *buildType() const;
378
379 bool get_hasEllipsis() const;
380 const std::string &get_name() const { return name; }
381 LinkageSpec::Type get_linkage() const { return linkage; }
382 DeclarationNode *extractAggregate() const;
383
384 DeclarationNode();
385 ~DeclarationNode();
386 private:
387 StorageClass buildStorageClass() const;
388 bool buildFuncSpecifier( StorageClass key ) const;
389
390 TypeData *type;
391 std::string name;
392 std::list< StorageClass > storageClasses;
393 std::list< std::string > attributes;
394 ExpressionNode *bitfieldWidth;
395 InitializerNode *initializer;
396 bool hasEllipsis;
397 LinkageSpec::Type linkage;
398
399 static UniqueName anonymous;
400}; // DeclarationNode
401
402class StatementNode : public ParseNode {
403 public:
404 enum Type { Exp, If, Switch, Case, Default, Choose, Fallthru,
405 While, Do, For,
406 Goto, Continue, Break, Return, Throw,
407 Try, Catch, Finally, Asm,
408 Decl
409 };
410
411 StatementNode();
412 StatementNode( const std::string *name );
413 StatementNode( Type t, ExpressionNode *control = 0, StatementNode *block = 0 );
414 StatementNode( Type t, std::string *target );
415 StatementNode( DeclarationNode *decl );
416
417 ~StatementNode();
418
419 static StatementNode *newCatchStmt( DeclarationNode *d = 0, StatementNode *s = 0, bool catchRestP = false );
420
421 StatementNode *set_block( StatementNode *b ) { block = b; return this; }
422 StatementNode *get_block() const { return block; }
423
424 void set_control( ExpressionNode *c ) { control = c; }
425 ExpressionNode *get_control() const { return control; }
426
427 StatementNode::Type get_type() const { return type; }
428
429 StatementNode *add_label( const std::string * );
430 const std::list<std::string> &get_labels() const { return labels; }
431
432 void addDeclaration( DeclarationNode *newDecl ) { decl = newDecl; }
433 void setCatchRest( bool newVal ) { isCatchRest = newVal; }
434
435 std::string get_target() const;
436
437 StatementNode *add_controlexp( ExpressionNode * );
438 StatementNode *append_block( StatementNode * );
439 StatementNode *append_last_case( StatementNode * );
440
441 void print( std::ostream &, int indent = 0) const;
442 virtual StatementNode *clone() const;
443 virtual Statement *build() const;
444 private:
445 static const char *StType[];
446 Type type;
447 ExpressionNode *control;
448 StatementNode *block;
449 std::list<std::string> labels;
450 std::string *target; // target label for jump statements
451 DeclarationNode *decl;
452 bool isCatchRest;
453}; // StatementNode
454
455class CompoundStmtNode : public StatementNode {
456 public:
457 CompoundStmtNode();
458 CompoundStmtNode( const std::string * );
459 CompoundStmtNode( StatementNode * );
460 ~CompoundStmtNode();
461
462 void add_statement( StatementNode * );
463
464 void print( std::ostream &, int indent = 0 ) const;
465 virtual Statement *build() const;
466 private:
467 StatementNode *first, *last;
468};
469
470class AsmStmtNode : public StatementNode {
471 public:
472 AsmStmtNode( Type, bool voltile, ConstantNode *instruction, ExpressionNode *output = 0, ExpressionNode *input = 0, ConstantNode *clobber = 0, LabelNode *gotolabels = 0 );
473 ~AsmStmtNode();
474
475 void print( std::ostream &, int indent = 0 ) const;
476 Statement *build() const;
477 private:
478 bool voltile;
479 ConstantNode *instruction;
480 ExpressionNode *output, *input;
481 ConstantNode *clobber;
482 std::list<std::string> gotolabels;
483};
484
485class NullStmtNode : public CompoundStmtNode {
486 public:
487 Statement *build() const;
488 void print( std::ostream &, int indent = 0 ) const;
489};
490
491class InitializerNode : public ParseNode {
492 public:
493 InitializerNode( ExpressionNode *, bool aggrp = false, ExpressionNode *des = 0 );
494 InitializerNode( InitializerNode *, bool aggrp = false, ExpressionNode *des = 0 );
495 ~InitializerNode();
496
497 ExpressionNode *get_expression() const { return expr; }
498
499 InitializerNode *set_designators( ExpressionNode *des ) { designator = des; return this; }
500 ExpressionNode *get_designators() const { return designator; }
501
502 InitializerNode *next_init() const { return kids; }
503
504 void print( std::ostream &, int indent = 0 ) const;
505 void printOneLine( std::ostream & ) const;
506
507 virtual Initializer *build() const;
508 private:
509 ExpressionNode *expr;
510 bool aggregate;
511 ExpressionNode *designator; // may be list
512 InitializerNode *kids;
513};
514
515template< typename SynTreeType, typename NodeType >
516void buildList( const NodeType *firstNode, std::list< SynTreeType *> &outputList ) {
517 SemanticError errors;
518 std::back_insert_iterator< std::list< SynTreeType *> > out( outputList );
519 const NodeType *cur = firstNode;
520
521 while ( cur ) {
522 try {
523 SynTreeType *result = dynamic_cast< SynTreeType *>( cur->build() );
524 if ( result ) {
525 *out++ = result;
526 } else {
527 } // if
528 } catch( SemanticError &e ) {
529 errors.append( e );
530 } // try
531 cur = dynamic_cast< NodeType *>( cur->get_link() );
532 } // while
533 if ( ! errors.isEmpty() ) {
534 throw errors;
535 } // if
536}
537
538// in DeclarationNode.cc
539void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList );
540void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList );
541void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList );
542
543// in ExpressionNode.cc
544ExpressionNode *flattenCommas( ExpressionNode *list );
545ExpressionNode *tupleContents( ExpressionNode *tuple );
546
547#endif // PARSENODE_H
548
549// Local Variables: //
550// tab-width: 4 //
551// mode: c++ //
552// compile-command: "make install" //
553// End: //
Note: See TracBrowser for help on using the repository browser.