source: src/Parser/ExpressionNode.cc @ a04ce4d

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since a04ce4d was 6165ce7, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

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

  • Property mode set to 100644
File size: 15.8 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 : Thu Jul 27 12:10:10 2017
13// Update Count     : 556
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 "parserutility.h"
30#include "Common/utility.h"
31
32using namespace std;
33
34//##############################################################################
35
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
48Type::Qualifiers noQualifiers;                          // no qualifiers on constants
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
57Expression *build_constantInteger( const std::string & str ) {
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
64        unsigned long long int v;                                                       // converted integral value
65        size_t last = str.length() - 1;                                         // last character of constant
66        Expression * ret;
67
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       
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
131        ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][size] ), str, v ) );
132  CLEANUP:
133        delete &str;                                                                            // created by lex
134        return ret;
135} // build_constantInteger
136
137Expression *build_constantFloat( const std::string & str ) {
138        static const BasicType::Kind kind[2][3] = {
139                { BasicType::Float, BasicType::Double, BasicType::LongDouble },
140                { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
141        };
142
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;
147        double v;
148
149        sscanf( str.c_str(), "%lg", &v );
150
151        if ( checkI( str[last] ) ) {                                            // imaginary ?
152                complx = true;
153                last -= 1;                                                                              // backup one character
154        } // if
155
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
166
167        Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][size] ), str, v ) );
168        delete &str;                                                                            // created by lex
169        return ret;
170} // build_constantFloat
171
172Expression *build_constantChar( const std::string & str ) {
173        Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) );
174        delete &str;                                                                            // created by lex
175        return ret;
176} // build_constantChar
177
178ConstantExpr *build_constantStr( const std::string & str ) {
179        // string should probably be a primitive type
180        ArrayType *at = new ArrayType( noQualifiers, new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char ),
181                                                                   new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ),  // +1 for '\0' and -2 for '"'
182                                                                   false, false );
183        // constant 0 is ignored for pure string value
184        ConstantExpr * ret = new ConstantExpr( Constant( at, str, (unsigned long long int)0 ) );
185        delete &str;                                                                            // created by lex
186        return ret;
187} // build_constantStr
188
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;
195        UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) );
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
216
217
218Expression * build_field_name_REALFRACTIONconstant( const std::string & str ) {
219        if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) throw SemanticError( "invalid tuple index " + str );
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 ) {
226        if ( str[str.size()-1] != '.' ) throw SemanticError( "invalid tuple index " + str );
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
232NameExpr * build_varref( const string *name ) {
233        NameExpr *expr = new NameExpr( *name, nullptr );
234        delete name;
235        return expr;
236}
237
238// Must harmonize with OperKinds.
239static const char *OperName[] = {
240        // diadic
241        "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&",
242        "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
243        "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
244        "?[?]", "...",
245        // monadic
246        "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&"
247};
248
249Expression *build_cast( DeclarationNode *decl_node, ExpressionNode *expr_node ) {
250        Type *targetType = maybeMoveBuildType( decl_node );
251        if ( dynamic_cast< VoidType * >( targetType ) ) {
252                delete targetType;
253                return new CastExpr( maybeMoveBuild< Expression >(expr_node) );
254        } else {
255                return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType );
256        } // if
257}
258
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
266Expression *build_fieldSel( ExpressionNode *expr_node, Expression *member ) {
267        UntypedMemberExpr *ret = new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) );
268        return ret;
269}
270
271Expression *build_pfieldSel( ExpressionNode *expr_node, Expression *member ) {
272        UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
273        deref->location = expr_node->location;
274        deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) );
275        UntypedMemberExpr *ret = new UntypedMemberExpr( member, deref );
276        return ret;
277}
278
279Expression *build_addressOf( ExpressionNode *expr_node ) {
280                return new AddressExpr( maybeMoveBuild< Expression >(expr_node) );
281}
282Expression *build_sizeOfexpr( ExpressionNode *expr_node ) {
283        return new SizeofExpr( maybeMoveBuild< Expression >(expr_node) );
284}
285Expression *build_sizeOftype( DeclarationNode *decl_node ) {
286        return new SizeofExpr( maybeMoveBuildType( decl_node ) );
287}
288Expression *build_alignOfexpr( ExpressionNode *expr_node ) {
289        return new AlignofExpr( maybeMoveBuild< Expression >(expr_node) );
290}
291Expression *build_alignOftype( DeclarationNode *decl_node ) {
292        return new AlignofExpr( maybeMoveBuildType( decl_node) );
293}
294Expression *build_offsetOf( DeclarationNode *decl_node, NameExpr *member ) {
295        Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() );
296        delete member;
297        return ret;
298}
299
300Expression *build_and_or( ExpressionNode *expr_node1, ExpressionNode *expr_node2, bool kind ) {
301        return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind );
302}
303
304Expression *build_unary_val( OperKinds op, ExpressionNode *expr_node ) {
305        std::list< Expression * > args;
306        args.push_back( maybeMoveBuild< Expression >(expr_node) );
307        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
308}
309Expression *build_unary_ptr( OperKinds op, ExpressionNode *expr_node ) {
310        std::list< Expression * > args;
311        args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node) ) );
312        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
313}
314Expression *build_binary_val( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
315        std::list< Expression * > args;
316        args.push_back( maybeMoveBuild< Expression >(expr_node1) );
317        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
318        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
319}
320Expression *build_binary_ptr( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
321        std::list< Expression * > args;
322        args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node1) ) );
323        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
324        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
325}
326
327Expression *build_cond( ExpressionNode *expr_node1, ExpressionNode *expr_node2, ExpressionNode *expr_node3 ) {
328        return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) );
329}
330
331Expression *build_comma( ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
332        return new CommaExpr( maybeMoveBuild< Expression >(expr_node1), maybeMoveBuild< Expression >(expr_node2) );
333}
334
335Expression *build_attrexpr( NameExpr *var, ExpressionNode * expr_node ) {
336        return new AttrExpr( var, maybeMoveBuild< Expression >(expr_node) );
337}
338Expression *build_attrtype( NameExpr *var, DeclarationNode * decl_node ) {
339        return new AttrExpr( var, maybeMoveBuildType( decl_node ) );
340}
341
342Expression *build_tuple( ExpressionNode * expr_node ) {
343        std::list< Expression * > exprs;
344        buildMoveList( expr_node, exprs );
345        return new UntypedTupleExpr( exprs );;
346}
347
348Expression *build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
349        std::list< Expression * > args;
350        buildMoveList( expr_node, args );
351        return new UntypedExpr( maybeMoveBuild< Expression >(function), args, nullptr );
352}
353
354Expression *build_range( ExpressionNode * low, ExpressionNode *high ) {
355        return new RangeExpr( maybeMoveBuild< Expression >( low ), maybeMoveBuild< Expression >( high ) );
356}
357
358Expression *build_asmexpr( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand ) {
359        return new AsmExpr( maybeMoveBuild< Expression >( inout ), constraint, maybeMoveBuild< Expression >(operand) );
360}
361
362Expression *build_valexpr( StatementNode *s ) {
363        return new StmtExpr( dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(s) ) );
364}
365Expression *build_typevalue( DeclarationNode *decl ) {
366        return new TypeExpr( maybeMoveBuildType( decl ) );
367}
368
369Expression *build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids ) {
370        Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type
371        if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type
372                return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild< Initializer >(kids) );
373        // these types do not have associated type information
374        } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl )  ) {
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
380        } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl )  ) {
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
386        } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl )  ) {
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
392        } else {
393                assert( false );
394        } // if
395}
396
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.