source: src/Parser/ExpressionNode.cc@ dac593fd

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 dac593fd was 2f22cc4, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

more refactoring of parser code

  • Property mode set to 100644
File size: 14.7 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// ExpressionNode.cc --
8//
9// Author : Rodolfo G. Esteves
10// Created On : Sat May 16 13:17:07 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Wed Aug 10 11:07:38 2016
13// Update Count : 486
14//
15
16#include <cassert>
17#include <cctype>
18#include <climits>
19#include <cstdio>
20#include <algorithm>
21#include <sstream>
22
23#include "ParseNode.h"
24#include "TypeData.h"
25#include "SynTree/Constant.h"
26#include "SynTree/Expression.h"
27#include "SynTree/Declaration.h"
28#include "Common/UnimplementedError.h"
29#include "parseutility.h"
30#include "Common/utility.h"
31
32using namespace std;
33
34ExpressionNode::ExpressionNode( const ExpressionNode &other ) : ParseNode( other.name ), extension( other.extension ) {}
35
36//##############################################################################
37
38// Difficult to separate extra parts of constants during lexing because actions are not allow in the middle of patterns:
39//
40// prefix action constant action suffix
41//
42// Alternatively, breaking a pattern using BEGIN does not work if the following pattern can be empty:
43//
44// constant BEGIN CONT ...
45// <CONT>(...)? BEGIN 0 ... // possible empty suffix
46//
47// because the CONT rule is NOT triggered if the pattern is empty. Hence, constants are reparsed here to determine their
48// type.
49
50static Type::Qualifiers emptyQualifiers; // no qualifiers on constants
51
52static inline bool checkU( char c ) { return c == 'u' || c == 'U'; }
53static inline bool checkL( char c ) { return c == 'l' || c == 'L'; }
54static inline bool checkF( char c ) { return c == 'f' || c == 'F'; }
55static inline bool checkD( char c ) { return c == 'd' || c == 'D'; }
56static inline bool checkI( char c ) { return c == 'i' || c == 'I'; }
57static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }
58
59Expression *build_constantInteger( std::string & str ) {
60 static const BasicType::Kind kind[2][3] = {
61 { BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt },
62 { BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt },
63 };
64 bool dec = true, Unsigned = false; // decimal, unsigned constant
65 int size; // 0 => int, 1 => long, 2 => long long
66 unsigned long long v; // converted integral value
67 size_t last = str.length() - 1; // last character of constant
68
69 if ( str[0] == '0' ) { // octal/hex constant ?
70 dec = false;
71 if ( last != 0 && checkX( str[1] ) ) { // hex constant ?
72 sscanf( (char *)str.c_str(), "%llx", &v );
73 //printf( "%llx %llu\n", v, v );
74 } else { // octal constant
75 sscanf( (char *)str.c_str(), "%llo", &v );
76 //printf( "%llo %llu\n", v, v );
77 } // if
78 } else { // decimal constant ?
79 sscanf( (char *)str.c_str(), "%llu", &v );
80 //printf( "%llu %llu\n", v, v );
81 } // if
82
83 if ( v <= INT_MAX ) { // signed int
84 size = 0;
85 } else if ( v <= UINT_MAX && ! dec ) { // unsigned int
86 size = 0;
87 Unsigned = true; // unsigned
88 } else if ( v <= LONG_MAX ) { // signed long int
89 size = 1;
90 } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int
91 size = 1;
92 Unsigned = true; // unsigned long int
93 } else if ( v <= LLONG_MAX ) { // signed long long int
94 size = 2;
95 } else { // unsigned long long int
96 size = 2;
97 Unsigned = true; // unsigned long long int
98 } // if
99
100 if ( checkU( str[last] ) ) { // suffix 'u' ?
101 Unsigned = true;
102 if ( last > 0 && checkL( str[last - 1] ) ) { // suffix 'l' ?
103 size = 1;
104 if ( last > 1 && checkL( str[last - 2] ) ) { // suffix 'll' ?
105 size = 2;
106 } // if
107 } // if
108 } else if ( checkL( str[ last ] ) ) { // suffix 'l' ?
109 size = 1;
110 if ( last > 0 && checkL( str[last - 1] ) ) { // suffix 'll' ?
111 size = 2;
112 if ( last > 1 && checkU( str[last - 2] ) ) { // suffix 'u' ?
113 Unsigned = true;
114 } // if
115 } else {
116 if ( last > 0 && checkU( str[last - 1] ) ) { // suffix 'u' ?
117 Unsigned = true;
118 } // if
119 } // if
120 } // if
121
122 return new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[Unsigned][size] ), str ) );
123} // build_constantInteger
124
125Expression *build_constantFloat( std::string & str ) {
126 static const BasicType::Kind kind[2][3] = {
127 { BasicType::Float, BasicType::Double, BasicType::LongDouble },
128 { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
129 };
130
131 bool complx = false; // real, complex
132 int size = 1; // 0 => float, 1 => double (default), 2 => long double
133 // floating-point constant has minimum of 2 characters: 1. or .1
134 size_t last = str.length() - 1;
135
136 if ( checkI( str[last] ) ) { // imaginary ?
137 complx = true;
138 last -= 1; // backup one character
139 } // if
140
141 if ( checkF( str[last] ) ) { // float ?
142 size = 0;
143 } else if ( checkD( str[last] ) ) { // double ?
144 size = 1;
145 } else if ( checkL( str[last] ) ) { // long double ?
146 size = 2;
147 } // if
148 if ( ! complx && checkI( str[last - 1] ) ) { // imaginary ?
149 complx = true;
150 } // if
151
152 return new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[complx][size] ), str ) );
153} // build_constantFloat
154
155Expression *build_constantChar( std::string & str ) {
156 return new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::Char ), str ) );
157} // build_constantChar
158
159ConstantExpr *build_constantStr( std::string & str ) {
160 // string should probably be a primitive type
161 ArrayType *at = new ArrayType( emptyQualifiers, new BasicType( emptyQualifiers, BasicType::Char ),
162 new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::UnsignedInt ),
163 toString( str.size()+1-2 ) ) ), // +1 for '\0' and -2 for '"'
164 false, false );
165 return new ConstantExpr( Constant( at, str ) );
166} // build_constantStr
167
168//##############################################################################
169
170NameExpr * build_varref( const string *name, bool labelp ) {
171 return new NameExpr( *name, nullptr );
172}
173
174//##############################################################################
175
176static const char *OperName[] = {
177 // diadic
178 "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?*?", "?/?", "?%?", "||", "&&",
179 "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
180 "?=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
181 "?[?]", "...",
182 // monadic
183 "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&"
184};
185
186//##############################################################################
187
188Expression *build_cast( DeclarationNode *decl_node, ExpressionNode *expr_node ) {
189 Type *targetType = decl_node->buildType();
190 if ( dynamic_cast< VoidType * >( targetType ) ) {
191 delete targetType;
192 return new CastExpr( maybeBuild<Expression>(expr_node) );
193 } else {
194 return new CastExpr( maybeBuild<Expression>(expr_node), targetType );
195 } // if
196}
197
198Expression *build_fieldSel( ExpressionNode *expr_node, NameExpr *member ) {
199 UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), maybeBuild<Expression>(expr_node) );
200 delete member;
201 return ret;
202}
203
204Expression *build_pfieldSel( ExpressionNode *expr_node, NameExpr *member ) {
205 UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
206 deref->get_args().push_back( maybeBuild<Expression>(expr_node) );
207 UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), deref );
208 delete member;
209 return ret;
210}
211
212Expression *build_addressOf( ExpressionNode *expr_node ) {
213 return new AddressExpr( maybeBuild<Expression>(expr_node) );
214}
215Expression *build_sizeOfexpr( ExpressionNode *expr_node ) {
216 return new SizeofExpr( maybeBuild<Expression>(expr_node) );
217}
218Expression *build_sizeOftype( DeclarationNode *decl_node ) {
219 return new SizeofExpr( decl_node->buildType() );
220}
221Expression *build_alignOfexpr( ExpressionNode *expr_node ) {
222 return new AlignofExpr( maybeBuild<Expression>(expr_node) );
223}
224Expression *build_alignOftype( DeclarationNode *decl_node ) {
225 return new AlignofExpr( decl_node->buildType() );
226}
227Expression *build_offsetOf( DeclarationNode *decl_node, NameExpr *member ) {
228 return new UntypedOffsetofExpr( decl_node->buildType(), member->get_name() );
229}
230
231Expression *build_and_or( ExpressionNode *expr_node1, ExpressionNode *expr_node2, bool kind ) {
232 return new LogicalExpr( notZeroExpr( maybeBuild<Expression>(expr_node1) ), notZeroExpr( maybeBuild<Expression>(expr_node2) ), kind );
233}
234
235Expression *build_unary_val( OperKinds op, ExpressionNode *expr_node ) {
236 std::list<Expression *> args;
237 args.push_back( maybeBuild<Expression>(expr_node) );
238 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
239}
240Expression *build_unary_ptr( OperKinds op, ExpressionNode *expr_node ) {
241 std::list<Expression *> args;
242 args.push_back( new AddressExpr( maybeBuild<Expression>(expr_node) ) );
243 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
244}
245Expression *build_binary_val( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
246 std::list<Expression *> args;
247 args.push_back( maybeBuild<Expression>(expr_node1) );
248 args.push_back( maybeBuild<Expression>(expr_node2) );
249 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
250}
251Expression *build_binary_ptr( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
252 std::list<Expression *> args;
253 args.push_back( new AddressExpr( maybeBuild<Expression>(expr_node1) ) );
254 args.push_back( maybeBuild<Expression>(expr_node2) );
255 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
256}
257
258Expression *build_cond( ExpressionNode *expr_node1, ExpressionNode *expr_node2, ExpressionNode *expr_node3 ) {
259 return new ConditionalExpr( notZeroExpr( maybeBuild<Expression>(expr_node1) ), maybeBuild<Expression>(expr_node2), maybeBuild<Expression>(expr_node3) );
260}
261
262Expression *build_comma( ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
263 return new CommaExpr( maybeBuild<Expression>(expr_node1), maybeBuild<Expression>(expr_node2) );
264}
265
266Expression *build_attrexpr( NameExpr *var, ExpressionNode * expr_node ) {
267 return new AttrExpr( var, maybeBuild<Expression>(expr_node) );
268}
269Expression *build_attrtype( NameExpr *var, DeclarationNode * decl_node ) {
270 return new AttrExpr( var, decl_node->buildType() );
271}
272
273Expression *build_tuple( ExpressionNode * expr_node ) {
274 TupleExpr *ret = new TupleExpr();
275 buildList( expr_node, ret->get_exprs() );
276 return ret;
277}
278
279Expression *build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
280 std::list<Expression *> args;
281
282 buildList( expr_node, args );
283 return new UntypedExpr( maybeBuild<Expression>(function), args, nullptr );
284}
285
286Expression *build_range( ExpressionNode * low, ExpressionNode *high ) {
287 Expression *low_cexpr = maybeBuild<Expression>( low );
288 Expression *high_cexpr = maybeBuild<Expression>( high );
289 return new RangeExpr( low_cexpr, high_cexpr );
290}
291
292//##############################################################################
293
294Expression *build_asm( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand ) {
295 return new AsmExpr( maybeBuild< Expression >( inout ), constraint, maybeBuild<Expression>(operand) );
296}
297
298//##############################################################################
299
300void LabelNode::print( std::ostream &os, int indent ) const {}
301
302void LabelNode::printOneLine( std::ostream &os, int indent ) const {}
303
304//##############################################################################
305
306Expression *build_valexpr( StatementNode *s ) {
307 return new UntypedValofExpr( maybeBuild<Statement>(s), nullptr );
308}
309
310//##############################################################################
311
312// ForCtlExprNode::ForCtlExprNode( ParseNode *init_, ExpressionNode *cond, ExpressionNode *incr ) throw ( SemanticError ) : condition( cond ), change( incr ) {
313// if ( init_ == 0 )
314// init = 0;
315// else {
316// DeclarationNode *decl;
317// ExpressionNode *exp;
318
319// if (( decl = dynamic_cast<DeclarationNode *>(init_) ) != 0)
320// init = new StatementNode( decl );
321// else if (( exp = dynamic_cast<ExpressionNode *>( init_)) != 0)
322// init = new StatementNode( StatementNode::Exp, exp );
323// else
324// throw SemanticError("Error in for control expression");
325// }
326// }
327
328// ForCtlExprNode::ForCtlExprNode( const ForCtlExprNode &other )
329// : ExpressionNode( other ), init( maybeClone( other.init ) ), condition( maybeClone( other.condition ) ), change( maybeClone( other.change ) ) {
330// }
331
332// ForCtlExprNode::~ForCtlExprNode() {
333// delete init;
334// delete condition;
335// delete change;
336// }
337
338// Expression *ForCtlExprNode::build() const {
339// // this shouldn't be used!
340// assert( false );
341// return 0;
342// }
343
344// void ForCtlExprNode::print( std::ostream &os, int indent ) const{
345// os << string( indent,' ' ) << "For Control Expression -- :" << endl;
346
347// os << string( indent + 2, ' ' ) << "initialization:" << endl;
348// if ( init != 0 )
349// init->printList( os, indent + 4 );
350
351// os << string( indent + 2, ' ' ) << "condition: " << endl;
352// if ( condition != 0 )
353// condition->print( os, indent + 4 );
354// os << string( indent + 2, ' ' ) << "increment: " << endl;
355// if ( change != 0 )
356// change->print( os, indent + 4 );
357// }
358
359// void ForCtlExprNode::printOneLine( std::ostream &, int indent ) const {
360// assert( false );
361// }
362
363//##############################################################################
364
365Expression *build_typevalue( DeclarationNode *decl ) {
366 return new TypeExpr( decl->buildType() );
367}
368
369//##############################################################################
370
371Expression *build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids ) {
372 Declaration * newDecl = maybeBuild<Declaration>(decl_node); // compound literal type
373 if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type
374 return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeBuild<Initializer>(kids) );
375 // these types do not have associated type information
376 } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl ) ) {
377 return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeBuild<Initializer>(kids) );
378 } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl ) ) {
379 return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeBuild<Initializer>(kids) );
380 } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl ) ) {
381 return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeBuild<Initializer>(kids) );
382 } else {
383 assert( false );
384 } // if
385}
386
387// Local Variables: //
388// tab-width: 4 //
389// mode: c++ //
390// compile-command: "make install" //
391// End: //
Note: See TracBrowser for help on using the repository browser.