source: src/Parser/ExpressionNode.cc @ 86d5ba7c

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 86d5ba7c was a2e0687, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

formatting

  • Property mode set to 100644
File size: 16.3 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 21:42:38 2017
13// Update Count     : 567
14//
15
16#include <climits>                                                                              // access INT_MAX, UINT_MAX, LONG_MAX, ULONG_MAX, LLONG_MAX
17#include <sstream>
18
19#include "ParseNode.h"
20#include "TypeData.h"
21#include "SynTree/Constant.h"
22#include "SynTree/Expression.h"
23#include "SynTree/Declaration.h"
24#include "parserutility.h"
25
26using namespace std;
27
28//##############################################################################
29
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
42Type::Qualifiers noQualifiers;                          // no qualifiers on constants
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
51Expression * build_constantInteger( const std::string & str ) {
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
58        unsigned long long int v;                                                       // converted integral value
59        size_t last = str.length() - 1;                                         // last character of constant
60        Expression * ret;
61
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       
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
125        ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][size] ), str, v ) );
126  CLEANUP:
127        delete &str;                                                                            // created by lex
128        return ret;
129} // build_constantInteger
130
131Expression * build_constantFloat( const std::string & str ) {
132        static const BasicType::Kind kind[2][3] = {
133                { BasicType::Float, BasicType::Double, BasicType::LongDouble },
134                { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
135        };
136
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;
141        double v;
142
143        sscanf( str.c_str(), "%lg", &v );
144
145        if ( checkI( str[last] ) ) {                                            // imaginary ?
146                complx = true;
147                last -= 1;                                                                              // backup one character
148        } // if
149
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
160
161        Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][size] ), str, v ) );
162        delete &str;                                                                            // created by lex
163        return ret;
164} // build_constantFloat
165
166Expression * build_constantChar( const std::string & str ) {
167        Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) );
168        delete &str;                                                                            // created by lex
169        return ret;
170} // build_constantChar
171
172ConstantExpr * build_constantStr( const std::string & str ) {
173        // string should probably be a primitive type
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 '"'
176                                                                   false, false );
177        ConstantExpr * ret = new ConstantExpr( Constant( at, str, (unsigned long long int)0 ) ); // constant 0 is ignored for pure string value
178        delete &str;                                                                            // created by lex
179        return ret;
180} // build_constantStr
181
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;
188        UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) );
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 ) {
210        if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) throw SemanticError( "invalid tuple index " + str );
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 ) {
217        if ( str[str.size()-1] != '.' ) throw SemanticError( "invalid tuple index " + str );
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
223NameExpr * build_varref( const string * name ) {
224        NameExpr * expr = new NameExpr( *name, nullptr );
225        delete name;
226        return expr;
227} // build_varref
228
229
230static const char * OperName[] = {                                              // must harmonize with OperKinds
231        // diadic
232        "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&",
233        "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
234        "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
235        "?[?]", "...",
236        // monadic
237        "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&"
238}; // OperName
239
240Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
241        Type * targetType = maybeMoveBuildType( decl_node );
242        if ( dynamic_cast< VoidType * >( targetType ) ) {
243                delete targetType;
244                return new CastExpr( maybeMoveBuild< Expression >(expr_node) );
245        } else {
246                return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType );
247        } // if
248} // build_cast
249
250Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
251        Type * targetType = maybeMoveBuildType( decl_node );
252        Expression * castArg = maybeMoveBuild< Expression >( expr_node );
253        return new VirtualCastExpr( castArg, targetType );
254} // build_virtual_cast
255
256Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) {
257        UntypedMemberExpr * ret = new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) );
258        return ret;
259} // build_fieldSel
260
261Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) {
262        UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) );
263        deref->location = expr_node->location;
264        deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) );
265        UntypedMemberExpr * ret = new UntypedMemberExpr( member, deref );
266        return ret;
267} // build_pfieldSel
268
269Expression * build_addressOf( ExpressionNode * expr_node ) {
270                return new AddressExpr( maybeMoveBuild< Expression >(expr_node) );
271} // build_addressOf
272
273Expression * build_sizeOfexpr( ExpressionNode * expr_node ) {
274        return new SizeofExpr( maybeMoveBuild< Expression >(expr_node) );
275} // build_sizeOfexpr
276
277Expression * build_sizeOftype( DeclarationNode * decl_node ) {
278        return new SizeofExpr( maybeMoveBuildType( decl_node ) );
279} // build_sizeOftype
280
281Expression * build_alignOfexpr( ExpressionNode * expr_node ) {
282        return new AlignofExpr( maybeMoveBuild< Expression >(expr_node) );
283} // build_alignOfexpr
284
285Expression * build_alignOftype( DeclarationNode * decl_node ) {
286        return new AlignofExpr( maybeMoveBuildType( decl_node) );
287} // build_alignOftype
288
289Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) {
290        Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() );
291        delete member;
292        return ret;
293} // build_offsetOf
294
295Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ) {
296        return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind );
297} // build_and_or
298
299Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) {
300        std::list< Expression * > args;
301        args.push_back( maybeMoveBuild< Expression >(expr_node) );
302        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
303} // build_unary_val
304
305Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node ) {
306        std::list< Expression * > args;
307        args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node) ) );
308        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
309} // build_unary_ptr
310
311Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
312        std::list< Expression * > args;
313        args.push_back( maybeMoveBuild< Expression >(expr_node1) );
314        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
315        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
316} // build_binary_val
317
318Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
319        std::list< Expression * > args;
320        args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node1) ) );
321        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
322        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
323} // build_binary_ptr
324
325Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) {
326        return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) );
327} // build_cond
328
329Expression * build_comma( ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
330        return new CommaExpr( maybeMoveBuild< Expression >(expr_node1), maybeMoveBuild< Expression >(expr_node2) );
331} // build_comma
332
333Expression * build_attrexpr( NameExpr * var, ExpressionNode * expr_node ) {
334        return new AttrExpr( var, maybeMoveBuild< Expression >(expr_node) );
335} // build_attrexpr
336
337Expression * build_attrtype( NameExpr * var, DeclarationNode * decl_node ) {
338        return new AttrExpr( var, maybeMoveBuildType( decl_node ) );
339} // build_attrtype
340
341Expression * build_tuple( ExpressionNode * expr_node ) {
342        std::list< Expression * > exprs;
343        buildMoveList( expr_node, exprs );
344        return new UntypedTupleExpr( exprs );;
345} // build_tuple
346
347Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
348        std::list< Expression * > args;
349        buildMoveList( expr_node, args );
350        return new UntypedExpr( maybeMoveBuild< Expression >(function), args, nullptr );
351} // build_func
352
353Expression * build_range( ExpressionNode * low, ExpressionNode * high ) {
354        return new RangeExpr( maybeMoveBuild< Expression >( low ), maybeMoveBuild< Expression >( high ) );
355} // build_range
356
357Expression * build_asmexpr( ExpressionNode * inout, ConstantExpr * constraint, ExpressionNode * operand ) {
358        return new AsmExpr( maybeMoveBuild< Expression >( inout ), constraint, maybeMoveBuild< Expression >(operand) );
359} // build_asmexpr
360
361Expression * build_valexpr( StatementNode * s ) {
362        return new StmtExpr( dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(s) ) );
363} // build_valexpr
364
365Expression * build_typevalue( DeclarationNode * decl ) {
366        return new TypeExpr( maybeMoveBuildType( decl ) );
367} // build_typevalue
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} // build_compoundLiteral
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.