source: src/Parser/ParseNode.h@ c09e4bc

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 c09e4bc was 13e3b50, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

DeclarationNodes and Declaration now store exactly the same data for storage classes

  • Property mode set to 100644
File size: 15.2 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 StorageClass storageClass;
280 bool isInline, isNoreturn;
281 std::list< std::string > attributes;
282 ExpressionNode *bitfieldWidth;
283 ExpressionNode *enumeratorValue;
284 InitializerNode *initializer;
285 bool hasEllipsis;
286 LinkageSpec::Type linkage;
287 bool extension = false;
288 std::string error;
289
290 static UniqueName anonymous;
291}; // DeclarationNode
292
293Type *buildType( TypeData *type );
294
295//##############################################################################
296
297class StatementNode : public ParseNode {
298 public:
299 StatementNode() { stmt = nullptr; }
300 StatementNode( Statement *stmt ) : stmt( stmt ) {}
301 StatementNode( DeclarationNode *decl );
302 virtual ~StatementNode() {}
303
304 virtual StatementNode *clone() const { assert( false ); return nullptr; }
305 virtual Statement *build() const { return stmt; }
306
307 virtual StatementNode *add_label( const std::string * name ) {
308 stmt->get_labels().emplace_back( *name );
309 return this;
310 }
311
312 virtual StatementNode *append_last_case( StatementNode * );
313
314 virtual void print( std::ostream &os, int indent = 0 ) {}
315 virtual void printList( std::ostream &os, int indent = 0 ) {}
316 private:
317 Statement *stmt;
318}; // StatementNode
319
320Statement *build_expr( ExpressionNode *ctl );
321
322struct ForCtl {
323 ForCtl( ExpressionNode *expr, ExpressionNode *condition, ExpressionNode *change ) :
324 init( new StatementNode( build_expr( expr ) ) ), condition( condition ), change( change ) {}
325 ForCtl( DeclarationNode *decl, ExpressionNode *condition, ExpressionNode *change ) :
326 init( new StatementNode( decl ) ), condition( condition ), change( change ) {}
327
328 StatementNode *init;
329 ExpressionNode *condition;
330 ExpressionNode *change;
331};
332
333Statement *build_if( ExpressionNode *ctl, StatementNode *then_stmt, StatementNode *else_stmt );
334Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt );
335Statement *build_case( ExpressionNode *ctl );
336Statement *build_default();
337Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind = false );
338Statement *build_for( ForCtl *forctl, StatementNode *stmt );
339Statement *build_branch( std::string identifier, BranchStmt::Type kind );
340Statement *build_computedgoto( ExpressionNode *ctl );
341Statement *build_return( ExpressionNode *ctl );
342Statement *build_throw( ExpressionNode *ctl );
343Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt );
344Statement *build_catch( DeclarationNode *decl, StatementNode *stmt, bool catchAny = false );
345Statement *build_finally( StatementNode *stmt );
346Statement *build_compound( StatementNode *first );
347Statement *build_asmstmt( bool voltile, ConstantExpr *instruction, ExpressionNode *output = 0, ExpressionNode *input = 0, ExpressionNode *clobber = 0, LabelNode *gotolabels = 0 );
348
349//##############################################################################
350
351template< typename SynTreeType, typename NodeType >
352void buildList( const NodeType *firstNode, std::list< SynTreeType * > &outputList ) {
353 SemanticError errors;
354 std::back_insert_iterator< std::list< SynTreeType * > > out( outputList );
355 const NodeType *cur = firstNode;
356
357 while ( cur ) {
358 try {
359// SynTreeType *result = dynamic_cast< SynTreeType * >( maybeBuild< typename std::result_of< decltype(&NodeType::build)(NodeType)>::type >( cur ) );
360 SynTreeType *result = dynamic_cast< SynTreeType * >( maybeBuild< typename std::pointer_traits< decltype(cur->build())>::element_type >( cur ) );
361 if ( result ) {
362 *out++ = result;
363 } else {
364 } // if
365 } catch( SemanticError &e ) {
366 errors.append( e );
367 } // try
368 cur = dynamic_cast< NodeType * >( cur->get_next() );
369 } // while
370 if ( ! errors.isEmpty() ) {
371 throw errors;
372 } // if
373}
374
375// in DeclarationNode.cc
376void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList );
377void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType * > &outputList );
378void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList );
379
380#endif // PARSENODE_H
381
382// Local Variables: //
383// tab-width: 4 //
384// mode: c++ //
385// compile-command: "make install" //
386// End: //
Note: See TracBrowser for help on using the repository browser.