source: translator/Parser.old/ParseNode.h @ 3c70d38

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 3c70d38 was 51b7345, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

initial commit

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