source: src/Parser/ExpressionNode.cc @ 0213af6

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

fix tuple index and constant suffix

  • Property mode set to 100644
File size: 15.4 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 : Wed May 17 21:31:01 2017
13// Update Count     : 527
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 "parseutility.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 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 ) );
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
136        if ( checkI( str[last] ) ) {                                            // imaginary ?
137                complx = true;
138                last -= 1;                                                                              // backup one character
139        } // if
140
141        if ( checkF( str[last] ) ) {                                            // float ?
142                size = 0;
143        } else if ( checkD( str[last] ) ) {                                     // double ?
144                size = 1;
145        } else if ( checkL( str[last] ) ) {                                     // long double ?
146                size = 2;
147        } // if
148        if ( ! complx && checkI( str[last - 1] ) ) {            // imaginary ?
149                complx = true;
150        } // if
151
152        Expression * ret = new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[complx][size] ), str ) );
153        delete &str;                                                                            // created by lex
154        return ret;
155} // build_constantFloat
156
157Expression *build_constantChar( const std::string & str ) {
158        Expression * ret = new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::Char ), str ) );
159        delete &str;                                                                            // created by lex
160        return ret;
161} // build_constantChar
162
163ConstantExpr *build_constantStr( const std::string & str ) {
164        // string should probably be a primitive type
165        ArrayType *at = new ArrayType( emptyQualifiers, new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char ),
166                                new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::UnsignedInt ),
167                                                                                        toString( str.size()+1-2 ) ) ),  // +1 for '\0' and -2 for '"'
168                                                                   false, false );
169        ConstantExpr * ret = new ConstantExpr( Constant( at, str ) );
170        delete &str;                                                                            // created by lex
171        return ret;
172} // build_constantStr
173
174Expression *build_constantZeroOne( const std::string & str ) {
175        Expression * ret = new ConstantExpr( Constant( str == "0" ? (Type *)new ZeroType( emptyQualifiers ) : (Type*)new OneType( emptyQualifiers ), str ) );
176        delete &str;                                                                            // created by lex
177        return ret;
178} // build_constantChar
179
180Expression * build_field_name_FLOATINGconstant( const std::string & str ) {
181        // str is of the form A.B -> separate at the . and return member expression
182        int a, b;
183        char dot;
184        std::stringstream ss( str );
185        ss >> a >> dot >> b;
186        UntypedMemberExpr * ret = new UntypedMemberExpr(
187                new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::SignedInt ), toString( b ) ) ),
188                new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::SignedInt ), toString( 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
209
210
211Expression * build_field_name_REALFRACTIONconstant( const std::string & str ) {
212        if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) throw SemanticError( "invalid tuple index " + str );
213        Expression * ret = build_constantInteger( *new std::string( str.substr(1) ) );
214        delete &str;
215        return ret;
216} // build_field_name_REALFRACTIONconstant
217
218Expression * build_field_name_REALDECIMALconstant( const std::string & str ) {
219        if ( str[str.size()-1] != '.' ) throw SemanticError( "invalid tuple index " + str );
220        Expression * ret = build_constantInteger( *new std::string( str.substr( 0, str.size()-1 ) ) );
221        delete &str;
222        return ret;
223} // build_field_name_REALDECIMALconstant
224
225NameExpr * build_varref( const string *name, bool labelp ) {
226        NameExpr *expr = new NameExpr( *name, nullptr );
227        delete name;
228        return expr;
229}
230
231static const char *OperName[] = {
232        // diadic
233        "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?*?", "?/?", "?%?", "||", "&&",
234        "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
235        "?=?", "?@=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
236        "?[?]", "...",
237        // monadic
238        "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&"
239};
240
241Expression *build_cast( DeclarationNode *decl_node, ExpressionNode *expr_node ) {
242        Type *targetType = maybeMoveBuildType( decl_node );
243        if ( dynamic_cast< VoidType * >( targetType ) ) {
244                delete targetType;
245                return new CastExpr( maybeMoveBuild< Expression >(expr_node) );
246        } else {
247                return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType );
248        } // if
249}
250
251Expression *build_fieldSel( ExpressionNode *expr_node, Expression *member ) {
252        UntypedMemberExpr *ret = new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) );
253        return ret;
254}
255
256Expression *build_pfieldSel( ExpressionNode *expr_node, Expression *member ) {
257        UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
258        deref->location = expr_node->location;
259        deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) );
260        UntypedMemberExpr *ret = new UntypedMemberExpr( member, deref );
261        return ret;
262}
263
264Expression *build_addressOf( ExpressionNode *expr_node ) {
265                return new AddressExpr( maybeMoveBuild< Expression >(expr_node) );
266}
267Expression *build_sizeOfexpr( ExpressionNode *expr_node ) {
268        return new SizeofExpr( maybeMoveBuild< Expression >(expr_node) );
269}
270Expression *build_sizeOftype( DeclarationNode *decl_node ) {
271        return new SizeofExpr( maybeMoveBuildType( decl_node ) );
272}
273Expression *build_alignOfexpr( ExpressionNode *expr_node ) {
274        return new AlignofExpr( maybeMoveBuild< Expression >(expr_node) );
275}
276Expression *build_alignOftype( DeclarationNode *decl_node ) {
277        return new AlignofExpr( maybeMoveBuildType( decl_node) );
278}
279Expression *build_offsetOf( DeclarationNode *decl_node, NameExpr *member ) {
280        Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() );
281        delete member;
282        return ret;
283}
284
285Expression *build_and_or( ExpressionNode *expr_node1, ExpressionNode *expr_node2, bool kind ) {
286        return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind );
287}
288
289Expression *build_unary_val( OperKinds op, ExpressionNode *expr_node ) {
290        std::list< Expression * > args;
291        args.push_back( maybeMoveBuild< Expression >(expr_node) );
292        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
293}
294Expression *build_unary_ptr( OperKinds op, ExpressionNode *expr_node ) {
295        std::list< Expression * > args;
296        args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node) ) );
297        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
298}
299Expression *build_binary_val( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
300        std::list< Expression * > args;
301        args.push_back( maybeMoveBuild< Expression >(expr_node1) );
302        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
303        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
304}
305Expression *build_binary_ptr( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
306        std::list< Expression * > args;
307        args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node1) ) );
308        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
309        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
310}
311
312Expression *build_cond( ExpressionNode *expr_node1, ExpressionNode *expr_node2, ExpressionNode *expr_node3 ) {
313        return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) );
314}
315
316Expression *build_comma( ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
317        return new CommaExpr( maybeMoveBuild< Expression >(expr_node1), maybeMoveBuild< Expression >(expr_node2) );
318}
319
320Expression *build_attrexpr( NameExpr *var, ExpressionNode * expr_node ) {
321        return new AttrExpr( var, maybeMoveBuild< Expression >(expr_node) );
322}
323Expression *build_attrtype( NameExpr *var, DeclarationNode * decl_node ) {
324        return new AttrExpr( var, maybeMoveBuildType( decl_node ) );
325}
326
327Expression *build_tuple( ExpressionNode * expr_node ) {
328        std::list< Expression * > exprs;
329        buildMoveList( expr_node, exprs );
330        return new UntypedTupleExpr( exprs );;
331}
332
333Expression *build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
334        std::list< Expression * > args;
335        buildMoveList( expr_node, args );
336        return new UntypedExpr( maybeMoveBuild< Expression >(function), args, nullptr );
337}
338
339Expression *build_range( ExpressionNode * low, ExpressionNode *high ) {
340        return new RangeExpr( maybeMoveBuild< Expression >( low ), maybeMoveBuild< Expression >( high ) );
341}
342
343Expression *build_asmexpr( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand ) {
344        return new AsmExpr( maybeMoveBuild< Expression >( inout ), constraint, maybeMoveBuild< Expression >(operand) );
345}
346
347Expression *build_valexpr( StatementNode *s ) {
348        return new UntypedValofExpr( maybeMoveBuild< Statement >(s), nullptr );
349}
350Expression *build_typevalue( DeclarationNode *decl ) {
351        return new TypeExpr( maybeMoveBuildType( decl ) );
352}
353
354Expression *build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids ) {
355        Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type
356        if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type
357                return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild< Initializer >(kids) );
358        // these types do not have associated type information
359        } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl )  ) {
360                if ( newDeclStructDecl->has_body() ) {
361                        return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl ), maybeMoveBuild< Initializer >(kids) );
362                } else {
363                        return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
364                } // if
365        } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl )  ) {
366                if ( newDeclUnionDecl->has_body() ) {
367                        return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl ), maybeMoveBuild< Initializer >(kids) );
368                } else {
369                        return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
370                } // if
371        } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl )  ) {
372                if ( newDeclEnumDecl->has_body() ) {
373                        return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl ), maybeMoveBuild< Initializer >(kids) );
374                } else {
375                        return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
376                } // if
377        } else {
378                assert( false );
379        } // if
380}
381
382// Local Variables: //
383// tab-width: 4 //
384// mode: c++ //
385// compile-command: "make install" //
386// End: //
Note: See TracBrowser for help on using the repository browser.