source: src/Parser/ExpressionNode.cc@ c3acf0aa

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 c3acf0aa was 65cdc1e, checked in by Andrew Beach <ajbeach@…>, 8 years ago

Syntax Nodes give public access to the fields with effective public access.X

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