source: src/Parser/ParseNode.h@ 7527e63

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

more refactoring of parser code

  • Property mode set to 100644
File size: 15.1 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 : Tue Aug 16 08:37:47 2016
13// Update Count : 527
14//
15
16#ifndef PARSENODE_H
17#define PARSENODE_H
18
19#include <string>
20#include <list>
21#include <iterator>
22#include <memory>
23
24#include "Parser/LinkageSpec.h"
25#include "SynTree/Type.h"
26#include "SynTree/Expression.h"
27#include "SynTree/Statement.h"
28#include "SynTree/Label.h"
29#include "Common/utility.h"
30#include "Common/UniqueName.h"
31
32class StatementNode;
33class CompoundStmtNode;
34class DeclarationNode;
35class ExpressionNode;
36class InitializerNode;
37
38//##############################################################################
39
40class ParseNode {
41 public:
42 ParseNode();
43 ParseNode( const std::string * );
44 ParseNode( const std::string & ); // for copy constructing subclasses
45 virtual ~ParseNode();
46 virtual ParseNode *clone() const { assert( false ); return nullptr; };
47
48 ParseNode *get_next() const { return next; }
49 ParseNode *set_next( ParseNode *newlink ) { next = newlink; return this; }
50 ParseNode *get_last();
51 ParseNode *set_last( ParseNode * );
52
53 const std::string &get_name() const { return name; }
54 void set_name( const std::string &newValue ) { name = newValue; }
55
56 virtual void print( std::ostream &os, int indent = 0 ) const {}
57 virtual void printList( std::ostream &os, int indent = 0 ) const;
58 private:
59 static int indent_by;
60
61 ParseNode *next;
62 std::string name;
63}; // ParseNode
64
65//##############################################################################
66
67class InitializerNode : public ParseNode {
68 public:
69 InitializerNode( ExpressionNode *, bool aggrp = false, ExpressionNode *des = 0 );
70 InitializerNode( InitializerNode *, bool aggrp = false, ExpressionNode *des = 0 );
71 ~InitializerNode();
72
73 ExpressionNode *get_expression() const { return expr; }
74
75 InitializerNode *set_designators( ExpressionNode *des ) { designator = des; return this; }
76 ExpressionNode *get_designators() const { return designator; }
77
78 InitializerNode *set_maybeConstructed( bool value ) { maybeConstructed = value; return this; }
79 bool get_maybeConstructed() const { return maybeConstructed; }
80
81 InitializerNode *next_init() const { return kids; }
82
83 void print( std::ostream &os, int indent = 0 ) const;
84 void printOneLine( std::ostream & ) const;
85
86 virtual Initializer *build() const;
87 private:
88 ExpressionNode *expr;
89 bool aggregate;
90 ExpressionNode *designator; // may be list
91 InitializerNode *kids;
92 bool maybeConstructed;
93};
94
95//##############################################################################
96
97class ExpressionNode : public ParseNode {
98 public:
99 ExpressionNode( Expression * expr = nullptr ) : expr( expr ) {}
100 ExpressionNode( Expression * expr, const std::string *name ) : ParseNode( name ), expr( expr ) {}
101 ExpressionNode( const ExpressionNode &other );
102 virtual ~ExpressionNode() {}
103
104 virtual ExpressionNode *clone() const { assert( false ); return nullptr; }
105
106 bool get_extension() const { return extension; }
107 ExpressionNode *set_extension( bool exten ) { extension = exten; return this; }
108
109 virtual void print( std::ostream &os, int indent = 0 ) const {}
110 virtual void printOneLine( std::ostream &os, int indent = 0 ) const {}
111
112 virtual Expression *build() const { return expr; }
113 private:
114 bool extension = false;
115 Expression *expr;
116};
117
118template< typename T >
119struct maybeBuild_t< Expression, T > {
120 static inline Expression * doit( const T *orig ) {
121 if ( orig ) {
122 Expression *p = orig->build();
123 p->set_extension( orig->get_extension() );
124 return p;
125 } else {
126 return nullptr;
127 } // if
128 }
129};
130
131enum class OperKinds {
132 // diadic
133 SizeOf, AlignOf, OffsetOf, Plus, Minus, Mul, Div, Mod, Or, And,
134 BitOr, BitAnd, Xor, Cast, LShift, RShift, LThan, GThan, LEThan, GEThan, Eq, Neq,
135 Assign, MulAssn, DivAssn, ModAssn, PlusAssn, MinusAssn, LSAssn, RSAssn, AndAssn, ERAssn, OrAssn,
136 Index, Range,
137 // monadic
138 UnPlus, UnMinus, AddressOf, PointTo, Neg, BitNeg, Incr, IncrPost, Decr, DecrPost, LabelAddress,
139 Ctor, Dtor,
140};
141
142struct LabelNode {
143 std::list< Label > labels;
144};
145
146Expression *build_constantInteger( std::string &str );
147Expression *build_constantFloat( std::string &str );
148Expression *build_constantChar( std::string &str );
149ConstantExpr *build_constantStr( std::string &str );
150
151NameExpr *build_varref( const std::string *name, bool labelp = false );
152Expression *build_typevalue( DeclarationNode *decl );
153
154Expression *build_cast( DeclarationNode * decl_node, ExpressionNode *expr_node );
155Expression *build_fieldSel( ExpressionNode *expr_node, NameExpr *member );
156Expression *build_pfieldSel( ExpressionNode *expr_node, NameExpr *member );
157Expression *build_addressOf( ExpressionNode *expr_node );
158Expression *build_sizeOfexpr( ExpressionNode *expr_node );
159Expression *build_sizeOftype( DeclarationNode *decl_node );
160Expression *build_alignOfexpr( ExpressionNode *expr_node );
161Expression *build_alignOftype( DeclarationNode *decl_node );
162Expression *build_offsetOf( DeclarationNode *decl_node, NameExpr *member );
163Expression *build_and( ExpressionNode *expr_node1, ExpressionNode *expr_node2 );
164Expression *build_and_or( ExpressionNode *expr_node1, ExpressionNode *expr_node2, bool kind );
165Expression *build_unary_val( OperKinds op, ExpressionNode *expr_node );
166Expression *build_unary_ptr( OperKinds op, ExpressionNode *expr_node );
167Expression *build_binary_val( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 );
168Expression *build_binary_ptr( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 );
169Expression *build_cond( ExpressionNode *expr_node1, ExpressionNode *expr_node2, ExpressionNode *expr_node3 );
170Expression *build_comma( ExpressionNode *expr_node1, ExpressionNode *expr_node2 );
171Expression *build_attrexpr( NameExpr *var, ExpressionNode * expr_node );
172Expression *build_attrtype( NameExpr *var, DeclarationNode * decl_node );
173Expression *build_tuple( ExpressionNode * expr_node = 0 );
174Expression *build_func( ExpressionNode * function, ExpressionNode * expr_node );
175Expression *build_range( ExpressionNode * low, ExpressionNode *high );
176Expression *build_asmexpr( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand );
177Expression *build_valexpr( StatementNode *s );
178Expression *build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids );
179
180//##############################################################################
181
182class TypeData;
183
184class DeclarationNode : public ParseNode {
185 public:
186 enum Qualifier { Const, Restrict, Volatile, Lvalue, Atomic };
187 enum StorageClass { Extern, Static, Auto, Register, Inline, Fortran, Noreturn, Threadlocal, NoStorageClass, };
188 enum BasicType { Char, Int, Float, Double, Void, Bool, Complex, Imaginary };
189 enum Modifier { Signed, Unsigned, Short, Long };
190 enum Aggregate { Struct, Union, Trait };
191 enum TypeClass { Type, Dtype, Ftype };
192 enum BuiltinType { Valist };
193
194 static const char *storageName[];
195 static const char *qualifierName[];
196 static const char *basicTypeName[];
197 static const char *modifierName[];
198 static const char *aggregateName[];
199 static const char *typeClassName[];
200 static const char *builtinTypeName[];
201
202 static DeclarationNode *newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle = false );
203 static DeclarationNode *newQualifier( Qualifier );
204 static DeclarationNode *newStorageClass( StorageClass );
205 static DeclarationNode *newBasicType( BasicType );
206 static DeclarationNode *newModifier( Modifier );
207 static DeclarationNode *newForall( DeclarationNode *);
208 static DeclarationNode *newFromTypedef( std::string *);
209 static DeclarationNode *newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields, bool body );
210 static DeclarationNode *newEnum( std::string *name, DeclarationNode *constants );
211 static DeclarationNode *newEnumConstant( std::string *name, ExpressionNode *constant );
212 static DeclarationNode *newName( std::string *);
213 static DeclarationNode *newFromTypeGen( std::string *, ExpressionNode *params );
214 static DeclarationNode *newTypeParam( TypeClass, std::string *);
215 static DeclarationNode *newTrait( std::string *name, DeclarationNode *params, DeclarationNode *asserts );
216 static DeclarationNode *newTraitUse( std::string *name, ExpressionNode *params );
217 static DeclarationNode *newTypeDecl( std::string *name, DeclarationNode *typeParams );
218 static DeclarationNode *newPointer( DeclarationNode *qualifiers );
219 static DeclarationNode *newArray( ExpressionNode *size, DeclarationNode *qualifiers, bool isStatic );
220 static DeclarationNode *newVarArray( DeclarationNode *qualifiers );
221 static DeclarationNode *newBitfield( ExpressionNode *size );
222 static DeclarationNode *newTuple( DeclarationNode *members );
223 static DeclarationNode *newTypeof( ExpressionNode *expr );
224 static DeclarationNode *newAttr( std::string *, ExpressionNode *expr );
225 static DeclarationNode *newAttr( std::string *, DeclarationNode *type );
226 static DeclarationNode *newBuiltinType( BuiltinType );
227
228 DeclarationNode();
229 ~DeclarationNode();
230 DeclarationNode *clone() const;
231
232 DeclarationNode *addQualifiers( DeclarationNode *);
233 DeclarationNode *copyStorageClasses( DeclarationNode *);
234 DeclarationNode *addType( DeclarationNode *);
235 DeclarationNode *addTypedef();
236 DeclarationNode *addAssertions( DeclarationNode *);
237 DeclarationNode *addName( std::string *);
238 DeclarationNode *addBitfield( ExpressionNode *size );
239 DeclarationNode *addVarArgs();
240 DeclarationNode *addFunctionBody( StatementNode *body );
241 DeclarationNode *addOldDeclList( DeclarationNode *list );
242 DeclarationNode *addPointer( DeclarationNode *qualifiers );
243 DeclarationNode *addArray( DeclarationNode *array );
244 DeclarationNode *addNewPointer( DeclarationNode *pointer );
245 DeclarationNode *addNewArray( DeclarationNode *array );
246 DeclarationNode *addParamList( DeclarationNode *list );
247 DeclarationNode *addIdList( DeclarationNode *list ); // old-style functions
248 DeclarationNode *addInitializer( InitializerNode *init );
249
250 DeclarationNode *cloneType( std::string *newName );
251 DeclarationNode *cloneType( DeclarationNode *existing );
252 DeclarationNode *cloneType( int ) { return cloneType( ( std::string *)0 ); }
253 DeclarationNode *cloneBaseType( std::string *newName );
254 DeclarationNode *cloneBaseType( DeclarationNode *newdecl );
255
256 DeclarationNode *appendList( DeclarationNode * );
257
258 void print( std::ostream &os, int indent = 0 ) const;
259 void printList( std::ostream &os, int indent = 0 ) const;
260
261 Declaration *build() const;
262 ::Type *buildType() const;
263
264 bool get_hasEllipsis() const;
265 const std::string &get_name() const { return name; }
266 LinkageSpec::Type get_linkage() const { return linkage; }
267 DeclarationNode *extractAggregate() const;
268 ExpressionNode *get_enumeratorValue() const { return enumeratorValue; }
269
270 bool get_extension() const { return extension; }
271 DeclarationNode *set_extension( bool exten ) { extension = exten; return this; }
272 private:
273 StorageClass buildStorageClass() const;
274 bool buildFuncSpecifier( StorageClass key ) const;
275
276 TypeData *type;
277 std::string name;
278 std::list< StorageClass > storageClasses;
279 std::list< std::string > attributes;
280 ExpressionNode *bitfieldWidth;
281 ExpressionNode *enumeratorValue;
282 InitializerNode *initializer;
283 bool hasEllipsis;
284 LinkageSpec::Type linkage;
285 bool extension = false;
286
287 static UniqueName anonymous;
288}; // DeclarationNode
289
290Type *buildType( TypeData *type );
291
292//##############################################################################
293
294class StatementNode : public ParseNode {
295 public:
296 StatementNode() { stmt = nullptr; }
297 StatementNode( Statement *stmt ) : stmt( stmt ) {}
298 StatementNode( DeclarationNode *decl );
299 virtual ~StatementNode() {}
300
301 virtual StatementNode *clone() const { assert( false ); return nullptr; }
302 virtual Statement *build() const { return stmt; }
303
304 virtual StatementNode *add_label( const std::string * name ) {
305 stmt->get_labels().emplace_back( *name );
306 return this;
307 }
308
309 virtual StatementNode *append_last_case( StatementNode * );
310
311 virtual void print( std::ostream &os, int indent = 0 ) {}
312 virtual void printList( std::ostream &os, int indent = 0 ) {}
313 private:
314 Statement *stmt;
315}; // StatementNode
316
317Statement *build_expr( ExpressionNode *ctl );
318
319struct ForCtl {
320 ForCtl( ExpressionNode *expr, ExpressionNode *condition, ExpressionNode *change ) :
321 init( new StatementNode( build_expr( expr ) ) ), condition( condition ), change( change ) {}
322 ForCtl( DeclarationNode *decl, ExpressionNode *condition, ExpressionNode *change ) :
323 init( new StatementNode( decl ) ), condition( condition ), change( change ) {}
324
325 StatementNode *init;
326 ExpressionNode *condition;
327 ExpressionNode *change;
328};
329
330Statement *build_if( ExpressionNode *ctl, StatementNode *then_stmt, StatementNode *else_stmt );
331Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt );
332Statement *build_case( ExpressionNode *ctl );
333Statement *build_default();
334Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind = false );
335Statement *build_for( ForCtl *forctl, StatementNode *stmt );
336Statement *build_branch( std::string identifier, BranchStmt::Type kind );
337Statement *build_computedgoto( ExpressionNode *ctl );
338Statement *build_return( ExpressionNode *ctl );
339Statement *build_throw( ExpressionNode *ctl );
340Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt );
341Statement *build_catch( DeclarationNode *decl, StatementNode *stmt, bool catchAny = false );
342Statement *build_finally( StatementNode *stmt );
343Statement *build_compound( StatementNode *first );
344Statement *build_asmstmt( bool voltile, ConstantExpr *instruction, ExpressionNode *output = 0, ExpressionNode *input = 0, ExpressionNode *clobber = 0, LabelNode *gotolabels = 0 );
345
346//##############################################################################
347
348template< typename SynTreeType, typename NodeType >
349void buildList( const NodeType *firstNode, std::list< SynTreeType * > &outputList ) {
350 SemanticError errors;
351 std::back_insert_iterator< std::list< SynTreeType * > > out( outputList );
352 const NodeType *cur = firstNode;
353
354 while ( cur ) {
355 try {
356// SynTreeType *result = dynamic_cast< SynTreeType * >( maybeBuild< typename std::result_of< decltype(&NodeType::build)(NodeType)>::type >( cur ) );
357 SynTreeType *result = dynamic_cast< SynTreeType * >( maybeBuild< typename std::pointer_traits< decltype(cur->build())>::element_type >( cur ) );
358 if ( result ) {
359 *out++ = result;
360 } else {
361 } // if
362 } catch( SemanticError &e ) {
363 errors.append( e );
364 } // try
365 cur = dynamic_cast< NodeType * >( cur->get_next() );
366 } // while
367 if ( ! errors.isEmpty() ) {
368 throw errors;
369 } // if
370}
371
372// in DeclarationNode.cc
373void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList );
374void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType * > &outputList );
375void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList );
376
377#endif // PARSENODE_H
378
379// Local Variables: //
380// tab-width: 4 //
381// mode: c++ //
382// compile-command: "make install" //
383// End: //
Note: See TracBrowser for help on using the repository browser.