source: src/Parser/ExpressionNode.cc@ c935c3a

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox 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 c935c3a was beec62c, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

handle "z" suffix cast

  • Property mode set to 100644
File size: 20.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// ExpressionNode.cc --
8//
9// Author : Peter A. Buhr
10// Created On : Sat May 16 13:17:07 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Sep 12 10:00:29 2017
13// Update Count : 672
14//
15
16#include <cassert> // for assert
17#include <stdio.h> // for sscanf, size_t
18#include <climits> // for LLONG_MAX, LONG_MAX, INT_MAX, UINT...
19#include <list> // for list
20#include <sstream> // for basic_istream::operator>>, basic_i...
21#include <string> // for string, operator+, operator==
22
23#include "Common/SemanticError.h" // for SemanticError
24#include "Common/utility.h" // for maybeMoveBuild, maybeBuild, CodeLo...
25#include "ParseNode.h" // for ExpressionNode, maybeMoveBuildType
26#include "SynTree/Constant.h" // for Constant
27#include "SynTree/Declaration.h" // for EnumDecl, StructDecl, UnionDecl
28#include "SynTree/Expression.h" // for Expression, ConstantExpr, NameExpr
29#include "SynTree/Statement.h" // for CompoundStmt, Statement
30#include "SynTree/Type.h" // for BasicType, Type, Type::Qualifiers
31#include "parserutility.h" // for notZeroExpr
32
33class Initializer;
34
35using namespace std;
36
37//##############################################################################
38
39// Difficult to separate extra parts of constants during lexing because actions are not allow in the middle of patterns:
40//
41// prefix action constant action suffix
42//
43// Alternatively, breaking a pattern using BEGIN does not work if the following pattern can be empty:
44//
45// constant BEGIN CONT ...
46// <CONT>(...)? BEGIN 0 ... // possible empty suffix
47//
48// because the CONT rule is NOT triggered if the pattern is empty. Hence, constants are reparsed here to determine their
49// type.
50
51extern const Type::Qualifiers noQualifiers; // no qualifiers on constants
52
53static inline bool checkH( char c ) { return c == 'h' || c == 'H'; }
54static inline bool checkL( char c ) { return c == 'l' || c == 'L'; }
55static inline bool checkZ( char c ) { return c == 'z' || c == 'Z'; }
56static inline bool checkU( char c ) { return c == 'u' || c == 'U'; }
57static inline bool checkF( char c ) { return c == 'f' || c == 'F'; }
58static inline bool checkD( char c ) { return c == 'd' || c == 'D'; }
59static inline bool checkI( char c ) { return c == 'i' || c == 'I'; }
60static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }
61
62static void sepNumeric( string & str, string & units ) {
63 string::size_type posn = str.find_first_of( "`" );
64 if ( posn != string::npos ) {
65 units = "?" + str.substr( posn ); // extract units
66 str.erase( posn ); // remove units
67 } // if
68} // sepNumeric
69
70Expression * build_constantInteger( string & str ) {
71 static const BasicType::Kind kind[2][5] = {
72 // short (h) must be before char (hh)
73 { BasicType::ShortSignedInt, BasicType::SignedChar, BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt },
74 { BasicType::ShortUnsignedInt, BasicType::UnsignedChar, BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt },
75 };
76
77 string units; // units
78 sepNumeric( str, units ); // separate constant from units
79
80 bool dec = true, Unsigned = false; // decimal, unsigned constant
81 int size; // 0 => short, 1 => char, 2 => int, 3 => long int, 4 => long long int, 5 => size_t
82 unsigned long long int v; // converted integral value
83 size_t last = str.length() - 1; // last character of constant
84 Expression * ret;
85
86 // special constants
87 if ( str == "0" ) {
88 ret = new ConstantExpr( Constant( (Type *)new ZeroType( noQualifiers ), str, (unsigned long long int)0 ) );
89 goto CLEANUP;
90 } // if
91 if ( str == "1" ) {
92 ret = new ConstantExpr( Constant( (Type *)new OneType( noQualifiers ), str, (unsigned long long int)1 ) );
93 goto CLEANUP;
94 } // if
95
96 if ( str[0] == '0' ) { // octal/hex constant ?
97 dec = false;
98 if ( last != 0 && checkX( str[1] ) ) { // hex constant ?
99 sscanf( (char *)str.c_str(), "%llx", &v );
100 //printf( "%llx %llu\n", v, v );
101 } else { // octal constant
102 sscanf( (char *)str.c_str(), "%llo", &v );
103 //printf( "%llo %llu\n", v, v );
104 } // if
105 } else { // decimal constant ?
106 sscanf( (char *)str.c_str(), "%llu", &v );
107 //printf( "%llu %llu\n", v, v );
108 } // if
109
110 if ( v <= INT_MAX ) { // signed int
111 size = 2;
112 } else if ( v <= UINT_MAX && ! dec ) { // unsigned int
113 size = 2;
114 Unsigned = true; // unsigned
115 } else if ( v <= LONG_MAX ) { // signed long int
116 size = 3;
117 } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int
118 size = 3;
119 Unsigned = true; // unsigned long int
120 } else if ( v <= LLONG_MAX ) { // signed long long int
121 size = 4;
122 } else { // unsigned long long int
123 size = 4;
124 Unsigned = true; // unsigned long long int
125 } // if
126
127 // At least one digit in integer constant, so safe to backup while looking for suffix.
128
129 if ( checkU( str[last] ) ) { // suffix 'u' ?
130 Unsigned = true;
131 if ( checkL( str[last - 1] ) ) { // suffix 'l' ?
132 size = 3;
133 if ( checkL( str[last - 2] ) ) { // suffix "ll" ?
134 size = 4;
135 } // if
136 } else if ( checkH( str[last - 1] ) ) { // suffix 'h' ?
137 size = 0;
138 if ( checkH( str[last - 2] ) ) { // suffix "hh" ?
139 size = 1;
140 } // if
141 str.erase( last - size - 1, size + 1 ); // remove 'h'/"hh"
142 } // if
143 } else if ( checkL( str[ last ] ) ) { // suffix 'l' ?
144 size = 3;
145 if ( checkL( str[last - 1] ) ) { // suffix 'll' ?
146 size = 4;
147 if ( checkU( str[last - 2] ) ) { // suffix 'u' ?
148 Unsigned = true;
149 } // if
150 } else if ( checkU( str[last - 1] ) ) { // suffix 'u' ?
151 Unsigned = true;
152 } // if
153 } else if ( checkH( str[ last ] ) ) { // suffix 'h' ?
154 size = 0;
155 if ( checkH( str[last - 1] ) ) { // suffix "hh" ?
156 size = 1;
157 if ( checkU( str[last - 2] ) ) { // suffix 'u' ?
158 Unsigned = true;
159 } // if
160 } else if ( checkU( str[last - 1] ) ) { // suffix 'u' ?
161 Unsigned = true;
162 } // if
163 str.erase( last - size, size + 1 ); // remove 'h'/"hh"
164 } else if ( checkZ( str[last] ) ) { // suffix 'z' ?
165 size = 5;
166 str.erase( last, 1 ); // remove 'z'
167 } // if
168
169 ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][size] ), str, v ) );
170 if ( Unsigned && size < 2 ) { // less than int ?
171 // int i = -1uh => 65535 not -1, so cast is necessary for unsigned, which eliminates warnings for large values.
172 ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][size] ) );
173 } else if ( size == 5 ) { // explicit cast to size_t
174 ret = new CastExpr( ret, new TypeInstType( Type::Qualifiers(), "size_t", false ) );
175 } // if
176 CLEANUP:
177 if ( units.length() != 0 ) {
178 ret = new UntypedExpr( new NameExpr( units ), { ret } );
179 } // if
180
181 delete &str; // created by lex
182 return ret;
183} // build_constantInteger
184
185Expression * build_constantFloat( string & str ) {
186 static const BasicType::Kind kind[2][3] = {
187 { BasicType::Float, BasicType::Double, BasicType::LongDouble },
188 { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
189 };
190
191 string units; // units
192 sepNumeric( str, units ); // separate constant from units
193
194 bool complx = false; // real, complex
195 int size = 1; // 0 => float, 1 => double (default), 2 => long double
196 // floating-point constant has minimum of 2 characters: 1. or .1
197 size_t last = str.length() - 1;
198 double v;
199
200 sscanf( str.c_str(), "%lg", &v );
201
202 if ( checkI( str[last] ) ) { // imaginary ?
203 complx = true;
204 last -= 1; // backup one character
205 } // if
206
207 if ( checkF( str[last] ) ) { // float ?
208 size = 0;
209 } else if ( checkD( str[last] ) ) { // double ?
210 size = 1;
211 } else if ( checkL( str[last] ) ) { // long double ?
212 size = 2;
213 } // if
214 if ( ! complx && checkI( str[last - 1] ) ) { // imaginary ?
215 complx = true;
216 } // if
217
218 Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][size] ), str, v ) );
219 if ( units.length() != 0 ) {
220 ret = new UntypedExpr( new NameExpr( units ), { ret } );
221 } // if
222
223 delete &str; // created by lex
224 return ret;
225} // build_constantFloat
226
227static void sepString( string & str, string & units, char delimit ) {
228 string::size_type posn = str.find_last_of( delimit ) + 1;
229 if ( posn != str.length() ) {
230 units = "?" + str.substr( posn ); // extract units
231 str.erase( posn ); // remove units
232 } // if
233} // sepString
234
235Expression * build_constantChar( string & str ) {
236 string units; // units
237 sepString( str, units, '\'' ); // separate constant from units
238
239 Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) );
240 if ( units.length() != 0 ) {
241 ret = new UntypedExpr( new NameExpr( units ), { ret } );
242 } // if
243
244 delete &str; // created by lex
245 return ret;
246} // build_constantChar
247
248Expression * build_constantStr( string & str ) {
249 string units; // units
250 sepString( str, units, '"' ); // separate constant from units
251
252 BasicType::Kind strtype = BasicType::Char; // default string type
253 switch ( str[0] ) { // str has >= 2 characters, i.e, null string ""
254 case 'u':
255 if ( str[1] == '8' ) break; // utf-8 characters
256 strtype = BasicType::ShortUnsignedInt;
257 break;
258 case 'U':
259 strtype = BasicType::UnsignedInt;
260 break;
261 case 'L':
262 strtype = BasicType::SignedInt;
263 break;
264 } // switch
265 ArrayType * at = new ArrayType( noQualifiers, new BasicType( Type::Qualifiers( Type::Const ), strtype ),
266 new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"'
267 false, false );
268 Expression * ret = new ConstantExpr( Constant( at, str, (unsigned long long int)0 ) ); // constant 0 is ignored for pure string value
269 if ( units.length() != 0 ) {
270 ret = new UntypedExpr( new NameExpr( units ), { ret } );
271 } // if
272
273 delete &str; // created by lex
274 return ret;
275} // build_constantStr
276
277Expression * build_field_name_FLOATING_FRACTIONconstant( const string & str ) {
278 if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) throw SemanticError( "invalid tuple index " + str );
279 Expression * ret = build_constantInteger( *new string( str.substr(1) ) );
280 delete &str;
281 return ret;
282} // build_field_name_FLOATING_FRACTIONconstant
283
284Expression * build_field_name_FLOATING_DECIMALconstant( const string & str ) {
285 if ( str[str.size()-1] != '.' ) throw SemanticError( "invalid tuple index " + str );
286 Expression * ret = build_constantInteger( *new string( str.substr( 0, str.size()-1 ) ) );
287 delete &str;
288 return ret;
289} // build_field_name_FLOATING_DECIMALconstant
290
291Expression * build_field_name_FLOATINGconstant( const string & str ) {
292 // str is of the form A.B -> separate at the . and return member expression
293 int a, b;
294 char dot;
295 stringstream ss( str );
296 ss >> a >> dot >> b;
297 UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) );
298 delete &str;
299 return ret;
300} // build_field_name_FLOATINGconstant
301
302Expression * make_field_name_fraction_constants( Expression * fieldName, Expression * fracts ) {
303 if ( fracts ) {
304 if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( fracts ) ) {
305 memberExpr->set_member( make_field_name_fraction_constants( fieldName, memberExpr->get_aggregate() ) );
306 return memberExpr;
307 } else {
308 return new UntypedMemberExpr( fracts, fieldName );
309 } // if
310 } // if
311 return fieldName;
312} // make_field_name_fraction_constants
313
314Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts ) {
315 return make_field_name_fraction_constants( fieldName, maybeMoveBuild< Expression >( fracts ) );
316} // build_field_name_fraction_constants
317
318NameExpr * build_varref( const string * name ) {
319 NameExpr * expr = new NameExpr( *name, nullptr );
320 delete name;
321 return expr;
322} // build_varref
323
324// TODO: get rid of this and OperKinds and reuse code from OperatorTable
325static const char * OperName[] = { // must harmonize with OperKinds
326 // diadic
327 "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&",
328 "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
329 "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
330 "?[?]", "...",
331 // monadic
332 "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--",
333}; // OperName
334
335Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
336 Type * targetType = maybeMoveBuildType( decl_node );
337 if ( dynamic_cast< VoidType * >( targetType ) ) {
338 delete targetType;
339 return new CastExpr( maybeMoveBuild< Expression >(expr_node) );
340 } else {
341 return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType );
342 } // if
343} // build_cast
344
345Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
346 Type * targetType = maybeMoveBuildType( decl_node );
347 Expression * castArg = maybeMoveBuild< Expression >( expr_node );
348 return new VirtualCastExpr( castArg, targetType );
349} // build_virtual_cast
350
351Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) {
352 UntypedMemberExpr * ret = new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) );
353 return ret;
354} // build_fieldSel
355
356Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) {
357 UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) );
358 deref->location = expr_node->location;
359 deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) );
360 UntypedMemberExpr * ret = new UntypedMemberExpr( member, deref );
361 return ret;
362} // build_pfieldSel
363
364Expression * build_addressOf( ExpressionNode * expr_node ) {
365 return new AddressExpr( maybeMoveBuild< Expression >(expr_node) );
366} // build_addressOf
367
368Expression * build_sizeOfexpr( ExpressionNode * expr_node ) {
369 return new SizeofExpr( maybeMoveBuild< Expression >(expr_node) );
370} // build_sizeOfexpr
371
372Expression * build_sizeOftype( DeclarationNode * decl_node ) {
373 return new SizeofExpr( maybeMoveBuildType( decl_node ) );
374} // build_sizeOftype
375
376Expression * build_alignOfexpr( ExpressionNode * expr_node ) {
377 return new AlignofExpr( maybeMoveBuild< Expression >(expr_node) );
378} // build_alignOfexpr
379
380Expression * build_alignOftype( DeclarationNode * decl_node ) {
381 return new AlignofExpr( maybeMoveBuildType( decl_node) );
382} // build_alignOftype
383
384Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) {
385 Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() );
386 delete member;
387 return ret;
388} // build_offsetOf
389
390Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ) {
391 return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind );
392} // build_and_or
393
394Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) {
395 list< Expression * > args;
396 args.push_back( maybeMoveBuild< Expression >(expr_node) );
397 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
398} // build_unary_val
399
400Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node ) {
401 list< Expression * > args;
402 args.push_back( maybeMoveBuild< Expression >(expr_node) ); // xxx -- this is exactly the same as the val case now, refactor this code.
403 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
404} // build_unary_ptr
405
406Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
407 list< Expression * > args;
408 args.push_back( maybeMoveBuild< Expression >(expr_node1) );
409 args.push_back( maybeMoveBuild< Expression >(expr_node2) );
410 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
411} // build_binary_val
412
413Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
414 list< Expression * > args;
415 args.push_back( maybeMoveBuild< Expression >(expr_node1) );
416 args.push_back( maybeMoveBuild< Expression >(expr_node2) );
417 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
418} // build_binary_ptr
419
420Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) {
421 return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) );
422} // build_cond
423
424Expression * build_comma( ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
425 return new CommaExpr( maybeMoveBuild< Expression >(expr_node1), maybeMoveBuild< Expression >(expr_node2) );
426} // build_comma
427
428Expression * build_attrexpr( NameExpr * var, ExpressionNode * expr_node ) {
429 return new AttrExpr( var, maybeMoveBuild< Expression >(expr_node) );
430} // build_attrexpr
431
432Expression * build_attrtype( NameExpr * var, DeclarationNode * decl_node ) {
433 return new AttrExpr( var, maybeMoveBuildType( decl_node ) );
434} // build_attrtype
435
436Expression * build_tuple( ExpressionNode * expr_node ) {
437 list< Expression * > exprs;
438 buildMoveList( expr_node, exprs );
439 return new UntypedTupleExpr( exprs );;
440} // build_tuple
441
442Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
443 list< Expression * > args;
444 buildMoveList( expr_node, args );
445 return new UntypedExpr( maybeMoveBuild< Expression >(function), args, nullptr );
446} // build_func
447
448Expression * build_range( ExpressionNode * low, ExpressionNode * high ) {
449 return new RangeExpr( maybeMoveBuild< Expression >( low ), maybeMoveBuild< Expression >( high ) );
450} // build_range
451
452Expression * build_asmexpr( ExpressionNode * inout, Expression * constraint, ExpressionNode * operand ) {
453 return new AsmExpr( maybeMoveBuild< Expression >( inout ), constraint, maybeMoveBuild< Expression >(operand) );
454} // build_asmexpr
455
456Expression * build_valexpr( StatementNode * s ) {
457 return new StmtExpr( dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(s) ) );
458} // build_valexpr
459
460Expression * build_typevalue( DeclarationNode * decl ) {
461 return new TypeExpr( maybeMoveBuildType( decl ) );
462} // build_typevalue
463
464Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids ) {
465 Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type
466 if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type
467 return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild< Initializer >(kids) );
468 // these types do not have associated type information
469 } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl ) ) {
470 if ( newDeclStructDecl->has_body() ) {
471 return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl ), maybeMoveBuild< Initializer >(kids) );
472 } else {
473 return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
474 } // if
475 } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl ) ) {
476 if ( newDeclUnionDecl->has_body() ) {
477 return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl ), maybeMoveBuild< Initializer >(kids) );
478 } else {
479 return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
480 } // if
481 } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl ) ) {
482 if ( newDeclEnumDecl->has_body() ) {
483 return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl ), maybeMoveBuild< Initializer >(kids) );
484 } else {
485 return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
486 } // if
487 } else {
488 assert( false );
489 } // if
490} // build_compoundLiteral
491
492// Local Variables: //
493// tab-width: 4 //
494// mode: c++ //
495// compile-command: "make install" //
496// End: //
Note: See TracBrowser for help on using the repository browser.