source: translator/Parser/ParseNode.h@ c8ffe20b

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 c8ffe20b was 51b73452, checked in by Peter A. Buhr <pabuhr@…>, 11 years ago

initial commit

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