source: src/Parser/ExpressionNode.cc @ e5f2a67

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

second attempt add exponential operator

  • Property mode set to 100644
File size: 15.6 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 : Sat Jul 15 16:09:04 2017
13// Update Count     : 549
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 emptyQualifiers;                               // 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
67        if ( str[0] == '0' ) {                                                          // octal/hex constant ?
68                dec = false;
69                if ( last != 0 && checkX( str[1] ) ) {                  // hex constant ?
70                        sscanf( (char *)str.c_str(), "%llx", &v );
71                        //printf( "%llx %llu\n", v, v );
72                } else {                                                                                // octal constant
73                        sscanf( (char *)str.c_str(), "%llo", &v );
74                        //printf( "%llo %llu\n", v, v );
75                } // if
76        } else {                                                                                        // decimal constant ?
77                sscanf( (char *)str.c_str(), "%llu", &v );
78                //printf( "%llu %llu\n", v, v );
79        } // if
80
81        if ( v <= INT_MAX ) {                                                           // signed int
82                size = 0;
83        } else if ( v <= UINT_MAX && ! dec ) {                          // unsigned int
84                size = 0;
85                Unsigned = true;                                                                // unsigned
86        } else if ( v <= LONG_MAX ) {                                           // signed long int
87                size = 1;
88        } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int
89                size = 1;
90                Unsigned = true;                                                                // unsigned long int
91        } else if ( v <= LLONG_MAX ) {                                          // signed long long int
92                size = 2;
93        } else {                                                                                        // unsigned long long int
94                size = 2;
95                Unsigned = true;                                                                // unsigned long long int
96        } // if
97
98        if ( checkU( str[last] ) ) {                                            // suffix 'u' ?
99                Unsigned = true;
100                if ( last > 0 && checkL( str[last - 1] ) ) {    // suffix 'l' ?
101                        size = 1;
102                        if ( last > 1 && checkL( str[last - 2] ) ) { // suffix 'll' ?
103                                size = 2;
104                        } // if
105                } // if
106        } else if ( checkL( str[ last ] ) ) {                           // suffix 'l' ?
107                size = 1;
108                if ( last > 0 && checkL( str[last - 1] ) ) {    // suffix 'll' ?
109                        size = 2;
110                        if ( last > 1 && checkU( str[last - 2] ) ) { // suffix 'u' ?
111                                Unsigned = true;
112                        } // if
113                } else {
114                        if ( last > 0 && checkU( str[last - 1] ) ) { // suffix 'u' ?
115                                Unsigned = true;
116                        } // if
117                } // if
118        } // if
119
120        Expression * ret = new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[Unsigned][size] ), str, v ) );
121        delete &str;                                                                            // created by lex
122        return ret;
123} // build_constantInteger
124
125Expression *build_constantFloat( const std::string & str ) {
126        static const BasicType::Kind kind[2][3] = {
127                { BasicType::Float, BasicType::Double, BasicType::LongDouble },
128                { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
129        };
130
131        bool complx = false;                                                            // real, complex
132        int size = 1;                                                                           // 0 => float, 1 => double (default), 2 => long double
133        // floating-point constant has minimum of 2 characters: 1. or .1
134        size_t last = str.length() - 1;
135        double v;
136
137        sscanf( str.c_str(), "%lg", &v );
138
139        if ( checkI( str[last] ) ) {                                            // imaginary ?
140                complx = true;
141                last -= 1;                                                                              // backup one character
142        } // if
143
144        if ( checkF( str[last] ) ) {                                            // float ?
145                size = 0;
146        } else if ( checkD( str[last] ) ) {                                     // double ?
147                size = 1;
148        } else if ( checkL( str[last] ) ) {                                     // long double ?
149                size = 2;
150        } // if
151        if ( ! complx && checkI( str[last - 1] ) ) {            // imaginary ?
152                complx = true;
153        } // if
154
155        Expression * ret = new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[complx][size] ), str, v ) );
156        delete &str;                                                                            // created by lex
157        return ret;
158} // build_constantFloat
159
160Expression *build_constantChar( const std::string & str ) {
161        Expression * ret = new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) );
162        delete &str;                                                                            // created by lex
163        return ret;
164} // build_constantChar
165
166ConstantExpr *build_constantStr( const std::string & str ) {
167        // string should probably be a primitive type
168        ArrayType *at = new ArrayType( emptyQualifiers, new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char ),
169                                                                   new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ),  // +1 for '\0' and -2 for '"'
170                                                                   false, false );
171        // constant 0 is ignored for pure string value
172        ConstantExpr * ret = new ConstantExpr( Constant( at, str, (unsigned long long int)0 ) );
173        delete &str;                                                                            // created by lex
174        return ret;
175} // build_constantStr
176
177Expression *build_constantZeroOne( const std::string & str ) {
178        Expression * ret = new ConstantExpr( Constant( str == "0" ? (Type *)new ZeroType( emptyQualifiers ) : (Type*)new OneType( emptyQualifiers ), str,
179                                                                                                   str == "0" ? (unsigned long long int)0 : (unsigned long long int)1 ) );
180        delete &str;                                                                            // created by lex
181        return ret;
182} // build_constantChar
183
184Expression * build_field_name_FLOATINGconstant( const std::string & str ) {
185        // str is of the form A.B -> separate at the . and return member expression
186        int a, b;
187        char dot;
188        std::stringstream ss( str );
189        ss >> a >> dot >> b;
190        UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) );
191        delete &str;
192        return ret;
193} // build_field_name_FLOATINGconstant
194
195Expression * make_field_name_fraction_constants( Expression * fieldName, Expression * fracts ) {
196        if ( fracts ) {
197                if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( fracts ) ) {
198                        memberExpr->set_member( make_field_name_fraction_constants( fieldName, memberExpr->get_aggregate() ) );
199                        return memberExpr;
200                } else {
201                        return new UntypedMemberExpr( fracts, fieldName );
202                }
203        }
204        return fieldName;
205} // make_field_name_fraction_constants
206
207Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts ) {
208        return make_field_name_fraction_constants( fieldName, maybeMoveBuild< Expression >( fracts ) );
209} // build_field_name_fraction_constants
210
211
212
213Expression * build_field_name_REALFRACTIONconstant( const std::string & str ) {
214        if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) throw SemanticError( "invalid tuple index " + str );
215        Expression * ret = build_constantInteger( *new std::string( str.substr(1) ) );
216        delete &str;
217        return ret;
218} // build_field_name_REALFRACTIONconstant
219
220Expression * build_field_name_REALDECIMALconstant( const std::string & str ) {
221        if ( str[str.size()-1] != '.' ) throw SemanticError( "invalid tuple index " + str );
222        Expression * ret = build_constantInteger( *new std::string( str.substr( 0, str.size()-1 ) ) );
223        delete &str;
224        return ret;
225} // build_field_name_REALDECIMALconstant
226
227NameExpr * build_varref( const string *name ) {
228        NameExpr *expr = new NameExpr( *name, nullptr );
229        delete name;
230        return expr;
231}
232
233// Must harmonize with OperKinds.
234static const char *OperName[] = {
235        // diadic
236        "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&",
237        "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
238        "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
239        "?[?]", "...",
240        // monadic
241        "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&"
242};
243
244Expression *build_cast( DeclarationNode *decl_node, ExpressionNode *expr_node ) {
245        Type *targetType = maybeMoveBuildType( decl_node );
246        if ( dynamic_cast< VoidType * >( targetType ) ) {
247                delete targetType;
248                return new CastExpr( maybeMoveBuild< Expression >(expr_node) );
249        } else {
250                return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType );
251        } // if
252}
253
254Expression *build_fieldSel( ExpressionNode *expr_node, Expression *member ) {
255        UntypedMemberExpr *ret = new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) );
256        return ret;
257}
258
259Expression *build_pfieldSel( ExpressionNode *expr_node, Expression *member ) {
260        UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
261        deref->location = expr_node->location;
262        deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) );
263        UntypedMemberExpr *ret = new UntypedMemberExpr( member, deref );
264        return ret;
265}
266
267Expression *build_addressOf( ExpressionNode *expr_node ) {
268                return new AddressExpr( maybeMoveBuild< Expression >(expr_node) );
269}
270Expression *build_sizeOfexpr( ExpressionNode *expr_node ) {
271        return new SizeofExpr( maybeMoveBuild< Expression >(expr_node) );
272}
273Expression *build_sizeOftype( DeclarationNode *decl_node ) {
274        return new SizeofExpr( maybeMoveBuildType( decl_node ) );
275}
276Expression *build_alignOfexpr( ExpressionNode *expr_node ) {
277        return new AlignofExpr( maybeMoveBuild< Expression >(expr_node) );
278}
279Expression *build_alignOftype( DeclarationNode *decl_node ) {
280        return new AlignofExpr( maybeMoveBuildType( decl_node) );
281}
282Expression *build_offsetOf( DeclarationNode *decl_node, NameExpr *member ) {
283        Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() );
284        delete member;
285        return ret;
286}
287
288Expression *build_and_or( ExpressionNode *expr_node1, ExpressionNode *expr_node2, bool kind ) {
289        return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind );
290}
291
292Expression *build_unary_val( OperKinds op, ExpressionNode *expr_node ) {
293        std::list< Expression * > args;
294        args.push_back( maybeMoveBuild< Expression >(expr_node) );
295        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
296}
297Expression *build_unary_ptr( OperKinds op, ExpressionNode *expr_node ) {
298        std::list< Expression * > args;
299        args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node) ) );
300        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
301}
302Expression *build_binary_val( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
303        std::list< Expression * > args;
304        args.push_back( maybeMoveBuild< Expression >(expr_node1) );
305        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
306        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
307}
308Expression *build_binary_ptr( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
309        std::list< Expression * > args;
310        args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node1) ) );
311        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
312        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
313}
314
315Expression *build_cond( ExpressionNode *expr_node1, ExpressionNode *expr_node2, ExpressionNode *expr_node3 ) {
316        return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) );
317}
318
319Expression *build_comma( ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
320        return new CommaExpr( maybeMoveBuild< Expression >(expr_node1), maybeMoveBuild< Expression >(expr_node2) );
321}
322
323Expression *build_attrexpr( NameExpr *var, ExpressionNode * expr_node ) {
324        return new AttrExpr( var, maybeMoveBuild< Expression >(expr_node) );
325}
326Expression *build_attrtype( NameExpr *var, DeclarationNode * decl_node ) {
327        return new AttrExpr( var, maybeMoveBuildType( decl_node ) );
328}
329
330Expression *build_tuple( ExpressionNode * expr_node ) {
331        std::list< Expression * > exprs;
332        buildMoveList( expr_node, exprs );
333        return new UntypedTupleExpr( exprs );;
334}
335
336Expression *build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
337        std::list< Expression * > args;
338        buildMoveList( expr_node, args );
339        return new UntypedExpr( maybeMoveBuild< Expression >(function), args, nullptr );
340}
341
342Expression *build_range( ExpressionNode * low, ExpressionNode *high ) {
343        return new RangeExpr( maybeMoveBuild< Expression >( low ), maybeMoveBuild< Expression >( high ) );
344}
345
346Expression *build_asmexpr( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand ) {
347        return new AsmExpr( maybeMoveBuild< Expression >( inout ), constraint, maybeMoveBuild< Expression >(operand) );
348}
349
350Expression *build_valexpr( StatementNode *s ) {
351        return new StmtExpr( dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(s) ) );
352}
353Expression *build_typevalue( DeclarationNode *decl ) {
354        return new TypeExpr( maybeMoveBuildType( decl ) );
355}
356
357Expression *build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids ) {
358        Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type
359        if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type
360                return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild< Initializer >(kids) );
361        // these types do not have associated type information
362        } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl )  ) {
363                if ( newDeclStructDecl->has_body() ) {
364                        return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl ), maybeMoveBuild< Initializer >(kids) );
365                } else {
366                        return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
367                } // if
368        } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl )  ) {
369                if ( newDeclUnionDecl->has_body() ) {
370                        return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl ), maybeMoveBuild< Initializer >(kids) );
371                } else {
372                        return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
373                } // if
374        } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl )  ) {
375                if ( newDeclEnumDecl->has_body() ) {
376                        return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl ), maybeMoveBuild< Initializer >(kids) );
377                } else {
378                        return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
379                } // if
380        } else {
381                assert( false );
382        } // if
383}
384
385// Local Variables: //
386// tab-width: 4 //
387// mode: c++ //
388// compile-command: "make install" //
389// End: //
Note: See TracBrowser for help on using the repository browser.