source: src/Parser/ExpressionNode.cc@ cd218e8

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 cd218e8 was 7b1d5ec, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

add cast for designated (hh or h) unsigned char and short literals to get correct behaviour

  • Property mode set to 100644
File size: 19.9 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 : Mon Sep 11 16:06:42 2017
13// Update Count : 666
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 => int, 1 => long, 2 => long long
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 str.erase( last, 1 ); // remove 'z'
166 } // if
167
168 ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][size] ), str, v ) );
169 if ( Unsigned && size < 2 ) { // less than int ?
170 // int i = -1uh => 65535 not -1, so cast is necessary for unsigned, which eliminates some warnings for large
171 // values.
172 ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][size] ) );
173 } // if
174 CLEANUP:
175 if ( units.length() != 0 ) {
176 ret = new UntypedExpr( new NameExpr( units ), { ret } );
177 } // if
178
179 delete &str; // created by lex
180 return ret;
181} // build_constantInteger
182
183Expression * build_constantFloat( string & str ) {
184 static const BasicType::Kind kind[2][3] = {
185 { BasicType::Float, BasicType::Double, BasicType::LongDouble },
186 { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
187 };
188
189 string units; // units
190 sepNumeric( str, units ); // separate constant from units
191
192 bool complx = false; // real, complex
193 int size = 1; // 0 => float, 1 => double (default), 2 => long double
194 // floating-point constant has minimum of 2 characters: 1. or .1
195 size_t last = str.length() - 1;
196 double v;
197
198 sscanf( str.c_str(), "%lg", &v );
199
200 if ( checkI( str[last] ) ) { // imaginary ?
201 complx = true;
202 last -= 1; // backup one character
203 } // if
204
205 if ( checkF( str[last] ) ) { // float ?
206 size = 0;
207 } else if ( checkD( str[last] ) ) { // double ?
208 size = 1;
209 } else if ( checkL( str[last] ) ) { // long double ?
210 size = 2;
211 } // if
212 if ( ! complx && checkI( str[last - 1] ) ) { // imaginary ?
213 complx = true;
214 } // if
215
216 Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][size] ), str, v ) );
217 if ( units.length() != 0 ) {
218 ret = new UntypedExpr( new NameExpr( units ), { ret } );
219 } // if
220
221 delete &str; // created by lex
222 return ret;
223} // build_constantFloat
224
225static void sepString( string & str, string & units, char delimit ) {
226 string::size_type posn = str.find_last_of( delimit ) + 1;
227 if ( posn != str.length() ) {
228 units = "?" + str.substr( posn ); // extract units
229 str.erase( posn ); // remove units
230 } // if
231} // sepString
232
233Expression * build_constantChar( string & str ) {
234 string units; // units
235 sepString( str, units, '\'' ); // separate constant from units
236
237 Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) );
238 if ( units.length() != 0 ) {
239 ret = new UntypedExpr( new NameExpr( units ), { ret } );
240 } // if
241
242 delete &str; // created by lex
243 return ret;
244} // build_constantChar
245
246Expression * build_constantStr( string & str ) {
247 string units; // units
248 sepString( str, units, '"' ); // separate constant from units
249
250 BasicType::Kind strtype = BasicType::Char; // default string type
251 switch ( str[0] ) { // str has >= 2 characters, i.e, null string ""
252 case 'u':
253 if ( str[1] == '8' ) break; // utf-8 characters
254 strtype = BasicType::ShortUnsignedInt;
255 break;
256 case 'U':
257 strtype = BasicType::UnsignedInt;
258 break;
259 case 'L':
260 strtype = BasicType::SignedInt;
261 break;
262 } // switch
263 ArrayType * at = new ArrayType( noQualifiers, new BasicType( Type::Qualifiers( Type::Const ), strtype ),
264 new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"'
265 false, false );
266 Expression * ret = new ConstantExpr( Constant( at, str, (unsigned long long int)0 ) ); // constant 0 is ignored for pure string value
267 if ( units.length() != 0 ) {
268 ret = new UntypedExpr( new NameExpr( units ), { ret } );
269 } // if
270
271 delete &str; // created by lex
272 return ret;
273} // build_constantStr
274
275Expression * build_field_name_FLOATING_FRACTIONconstant( const string & str ) {
276 if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) throw SemanticError( "invalid tuple index " + str );
277 Expression * ret = build_constantInteger( *new string( str.substr(1) ) );
278 delete &str;
279 return ret;
280} // build_field_name_FLOATING_FRACTIONconstant
281
282Expression * build_field_name_FLOATING_DECIMALconstant( const string & str ) {
283 if ( str[str.size()-1] != '.' ) throw SemanticError( "invalid tuple index " + str );
284 Expression * ret = build_constantInteger( *new string( str.substr( 0, str.size()-1 ) ) );
285 delete &str;
286 return ret;
287} // build_field_name_FLOATING_DECIMALconstant
288
289Expression * build_field_name_FLOATINGconstant( const string & str ) {
290 // str is of the form A.B -> separate at the . and return member expression
291 int a, b;
292 char dot;
293 stringstream ss( str );
294 ss >> a >> dot >> b;
295 UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) );
296 delete &str;
297 return ret;
298} // build_field_name_FLOATINGconstant
299
300Expression * make_field_name_fraction_constants( Expression * fieldName, Expression * fracts ) {
301 if ( fracts ) {
302 if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( fracts ) ) {
303 memberExpr->set_member( make_field_name_fraction_constants( fieldName, memberExpr->get_aggregate() ) );
304 return memberExpr;
305 } else {
306 return new UntypedMemberExpr( fracts, fieldName );
307 } // if
308 } // if
309 return fieldName;
310} // make_field_name_fraction_constants
311
312Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts ) {
313 return make_field_name_fraction_constants( fieldName, maybeMoveBuild< Expression >( fracts ) );
314} // build_field_name_fraction_constants
315
316NameExpr * build_varref( const string * name ) {
317 NameExpr * expr = new NameExpr( *name, nullptr );
318 delete name;
319 return expr;
320} // build_varref
321
322// TODO: get rid of this and OperKinds and reuse code from OperatorTable
323static const char * OperName[] = { // must harmonize with OperKinds
324 // diadic
325 "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&",
326 "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
327 "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
328 "?[?]", "...",
329 // monadic
330 "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--",
331}; // OperName
332
333Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
334 Type * targetType = maybeMoveBuildType( decl_node );
335 if ( dynamic_cast< VoidType * >( targetType ) ) {
336 delete targetType;
337 return new CastExpr( maybeMoveBuild< Expression >(expr_node) );
338 } else {
339 return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType );
340 } // if
341} // build_cast
342
343Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
344 Type * targetType = maybeMoveBuildType( decl_node );
345 Expression * castArg = maybeMoveBuild< Expression >( expr_node );
346 return new VirtualCastExpr( castArg, targetType );
347} // build_virtual_cast
348
349Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) {
350 UntypedMemberExpr * ret = new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) );
351 return ret;
352} // build_fieldSel
353
354Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) {
355 UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) );
356 deref->location = expr_node->location;
357 deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) );
358 UntypedMemberExpr * ret = new UntypedMemberExpr( member, deref );
359 return ret;
360} // build_pfieldSel
361
362Expression * build_addressOf( ExpressionNode * expr_node ) {
363 return new AddressExpr( maybeMoveBuild< Expression >(expr_node) );
364} // build_addressOf
365
366Expression * build_sizeOfexpr( ExpressionNode * expr_node ) {
367 return new SizeofExpr( maybeMoveBuild< Expression >(expr_node) );
368} // build_sizeOfexpr
369
370Expression * build_sizeOftype( DeclarationNode * decl_node ) {
371 return new SizeofExpr( maybeMoveBuildType( decl_node ) );
372} // build_sizeOftype
373
374Expression * build_alignOfexpr( ExpressionNode * expr_node ) {
375 return new AlignofExpr( maybeMoveBuild< Expression >(expr_node) );
376} // build_alignOfexpr
377
378Expression * build_alignOftype( DeclarationNode * decl_node ) {
379 return new AlignofExpr( maybeMoveBuildType( decl_node) );
380} // build_alignOftype
381
382Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) {
383 Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() );
384 delete member;
385 return ret;
386} // build_offsetOf
387
388Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ) {
389 return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind );
390} // build_and_or
391
392Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) {
393 list< Expression * > args;
394 args.push_back( maybeMoveBuild< Expression >(expr_node) );
395 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
396} // build_unary_val
397
398Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node ) {
399 list< Expression * > args;
400 args.push_back( maybeMoveBuild< Expression >(expr_node) ); // xxx -- this is exactly the same as the val case now, refactor this code.
401 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
402} // build_unary_ptr
403
404Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
405 list< Expression * > args;
406 args.push_back( maybeMoveBuild< Expression >(expr_node1) );
407 args.push_back( maybeMoveBuild< Expression >(expr_node2) );
408 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
409} // build_binary_val
410
411Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
412 list< Expression * > args;
413 args.push_back( maybeMoveBuild< Expression >(expr_node1) );
414 args.push_back( maybeMoveBuild< Expression >(expr_node2) );
415 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
416} // build_binary_ptr
417
418Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) {
419 return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) );
420} // build_cond
421
422Expression * build_comma( ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
423 return new CommaExpr( maybeMoveBuild< Expression >(expr_node1), maybeMoveBuild< Expression >(expr_node2) );
424} // build_comma
425
426Expression * build_attrexpr( NameExpr * var, ExpressionNode * expr_node ) {
427 return new AttrExpr( var, maybeMoveBuild< Expression >(expr_node) );
428} // build_attrexpr
429
430Expression * build_attrtype( NameExpr * var, DeclarationNode * decl_node ) {
431 return new AttrExpr( var, maybeMoveBuildType( decl_node ) );
432} // build_attrtype
433
434Expression * build_tuple( ExpressionNode * expr_node ) {
435 list< Expression * > exprs;
436 buildMoveList( expr_node, exprs );
437 return new UntypedTupleExpr( exprs );;
438} // build_tuple
439
440Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
441 list< Expression * > args;
442 buildMoveList( expr_node, args );
443 return new UntypedExpr( maybeMoveBuild< Expression >(function), args, nullptr );
444} // build_func
445
446Expression * build_range( ExpressionNode * low, ExpressionNode * high ) {
447 return new RangeExpr( maybeMoveBuild< Expression >( low ), maybeMoveBuild< Expression >( high ) );
448} // build_range
449
450Expression * build_asmexpr( ExpressionNode * inout, Expression * constraint, ExpressionNode * operand ) {
451 return new AsmExpr( maybeMoveBuild< Expression >( inout ), constraint, maybeMoveBuild< Expression >(operand) );
452} // build_asmexpr
453
454Expression * build_valexpr( StatementNode * s ) {
455 return new StmtExpr( dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(s) ) );
456} // build_valexpr
457
458Expression * build_typevalue( DeclarationNode * decl ) {
459 return new TypeExpr( maybeMoveBuildType( decl ) );
460} // build_typevalue
461
462Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids ) {
463 Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type
464 if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type
465 return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild< Initializer >(kids) );
466 // these types do not have associated type information
467 } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl ) ) {
468 if ( newDeclStructDecl->has_body() ) {
469 return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl ), maybeMoveBuild< Initializer >(kids) );
470 } else {
471 return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
472 } // if
473 } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl ) ) {
474 if ( newDeclUnionDecl->has_body() ) {
475 return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl ), maybeMoveBuild< Initializer >(kids) );
476 } else {
477 return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
478 } // if
479 } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl ) ) {
480 if ( newDeclEnumDecl->has_body() ) {
481 return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl ), maybeMoveBuild< Initializer >(kids) );
482 } else {
483 return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
484 } // if
485 } else {
486 assert( false );
487 } // if
488} // build_compoundLiteral
489
490// Local Variables: //
491// tab-width: 4 //
492// mode: c++ //
493// compile-command: "make install" //
494// End: //
Note: See TracBrowser for help on using the repository browser.