source: src/Parser/ExpressionNode.cc@ fcd17b2f

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 stuck-waitfor-destruct with_gc
Last change on this file since fcd17b2f was 6165ce7, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

remove old zero/one constant, replaced by zero_t/one_t types

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