source: src/Parser/ExpressionNode.cc @ d180746

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 d180746 was d180746, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Big header cleaning pass - commit 2

  • Property mode set to 100644
File size: 17.0 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 : Andrew Beach
12// Last Modified On : Wed Aug  2 11:12:00 2017
13// Update Count     : 568
14//
15
16#include <assert.h>                // for assert
17#include <stdio.h>                 // for sscanf, size_t
18#include <climits>                 // for LLONG_MAX, LONG_MAX, INT_MAX, UINT...
19#include <list>                    // for list
20#include <sstream>                 // for basic_istream::operator>>, basic_i...
21#include <string>                  // for string, operator+, operator==
22
23#include "Common/SemanticError.h"  // for SemanticError
24#include "Common/utility.h"        // for maybeMoveBuild, maybeBuild, CodeLo...
25#include "ParseNode.h"             // for ExpressionNode, maybeMoveBuildType
26#include "SynTree/Constant.h"      // for Constant
27#include "SynTree/Declaration.h"   // for EnumDecl, StructDecl, UnionDecl
28#include "SynTree/Expression.h"    // for Expression, ConstantExpr, NameExpr
29#include "SynTree/Statement.h"     // for CompoundStmt, Statement
30#include "SynTree/Type.h"          // for BasicType, Type, Type::Qualifiers
31#include "parserutility.h"         // for notZeroExpr
32
33class Initializer;
34
35using namespace std;
36
37//##############################################################################
38
39// Difficult to separate extra parts of constants during lexing because actions are not allow in the middle of patterns:
40//
41//              prefix action constant action suffix
42//
43// Alternatively, breaking a pattern using BEGIN does not work if the following pattern can be empty:
44//
45//              constant BEGIN CONT ...
46//              <CONT>(...)? BEGIN 0 ... // possible empty suffix
47//
48// because the CONT rule is NOT triggered if the pattern is empty. Hence, constants are reparsed here to determine their
49// type.
50
51extern const Type::Qualifiers noQualifiers;             // no qualifiers on constants
52
53static inline bool checkU( char c ) { return c == 'u' || c == 'U'; }
54static inline bool checkL( char c ) { return c == 'l' || c == 'L'; }
55static inline bool checkF( char c ) { return c == 'f' || c == 'F'; }
56static inline bool checkD( char c ) { return c == 'd' || c == 'D'; }
57static inline bool checkI( char c ) { return c == 'i' || c == 'I'; }
58static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }
59
60Expression * build_constantInteger( const std::string & str ) {
61        static const BasicType::Kind kind[2][3] = {
62                { BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt },
63                { BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt },
64        };
65        bool dec = true, Unsigned = false;                                      // decimal, unsigned constant
66        int size;                                                                                       // 0 => int, 1 => long, 2 => long long
67        unsigned long long int v;                                                       // converted integral value
68        size_t last = str.length() - 1;                                         // last character of constant
69        Expression * ret;
70
71        // special constants
72        if ( str == "0" ) {
73                ret = new ConstantExpr( Constant( (Type *)new ZeroType( noQualifiers ), str, (unsigned long long int)0 ) );
74                goto CLEANUP;
75        } // if
76        if ( str == "1" ) {
77                ret = new ConstantExpr( Constant( (Type *)new OneType( noQualifiers ), str, (unsigned long long int)1 ) );
78                goto CLEANUP;
79        } // if
80       
81        if ( str[0] == '0' ) {                                                          // octal/hex constant ?
82                dec = false;
83                if ( last != 0 && checkX( str[1] ) ) {                  // hex constant ?
84                        sscanf( (char *)str.c_str(), "%llx", &v );
85                        //printf( "%llx %llu\n", v, v );
86                } else {                                                                                // octal constant
87                        sscanf( (char *)str.c_str(), "%llo", &v );
88                        //printf( "%llo %llu\n", v, v );
89                } // if
90        } else {                                                                                        // decimal constant ?
91                sscanf( (char *)str.c_str(), "%llu", &v );
92                //printf( "%llu %llu\n", v, v );
93        } // if
94
95        if ( v <= INT_MAX ) {                                                           // signed int
96                size = 0;
97        } else if ( v <= UINT_MAX && ! dec ) {                          // unsigned int
98                size = 0;
99                Unsigned = true;                                                                // unsigned
100        } else if ( v <= LONG_MAX ) {                                           // signed long int
101                size = 1;
102        } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int
103                size = 1;
104                Unsigned = true;                                                                // unsigned long int
105        } else if ( v <= LLONG_MAX ) {                                          // signed long long int
106                size = 2;
107        } else {                                                                                        // unsigned long long int
108                size = 2;
109                Unsigned = true;                                                                // unsigned long long int
110        } // if
111
112        if ( checkU( str[last] ) ) {                                            // suffix 'u' ?
113                Unsigned = true;
114                if ( last > 0 && checkL( str[last - 1] ) ) {    // suffix 'l' ?
115                        size = 1;
116                        if ( last > 1 && checkL( str[last - 2] ) ) { // suffix 'll' ?
117                                size = 2;
118                        } // if
119                } // if
120        } else if ( checkL( str[ last ] ) ) {                           // suffix 'l' ?
121                size = 1;
122                if ( last > 0 && checkL( str[last - 1] ) ) {    // suffix 'll' ?
123                        size = 2;
124                        if ( last > 1 && checkU( str[last - 2] ) ) { // suffix 'u' ?
125                                Unsigned = true;
126                        } // if
127                } else {
128                        if ( last > 0 && checkU( str[last - 1] ) ) { // suffix 'u' ?
129                                Unsigned = true;
130                        } // if
131                } // if
132        } // if
133
134        ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][size] ), str, v ) );
135  CLEANUP:
136        delete &str;                                                                            // created by lex
137        return ret;
138} // build_constantInteger
139
140Expression * build_constantFloat( const std::string & str ) {
141        static const BasicType::Kind kind[2][3] = {
142                { BasicType::Float, BasicType::Double, BasicType::LongDouble },
143                { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
144        };
145
146        bool complx = false;                                                            // real, complex
147        int size = 1;                                                                           // 0 => float, 1 => double (default), 2 => long double
148        // floating-point constant has minimum of 2 characters: 1. or .1
149        size_t last = str.length() - 1;
150        double v;
151
152        sscanf( str.c_str(), "%lg", &v );
153
154        if ( checkI( str[last] ) ) {                                            // imaginary ?
155                complx = true;
156                last -= 1;                                                                              // backup one character
157        } // if
158
159        if ( checkF( str[last] ) ) {                                            // float ?
160                size = 0;
161        } else if ( checkD( str[last] ) ) {                                     // double ?
162                size = 1;
163        } else if ( checkL( str[last] ) ) {                                     // long double ?
164                size = 2;
165        } // if
166        if ( ! complx && checkI( str[last - 1] ) ) {            // imaginary ?
167                complx = true;
168        } // if
169
170        Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][size] ), str, v ) );
171        delete &str;                                                                            // created by lex
172        return ret;
173} // build_constantFloat
174
175Expression * build_constantChar( const std::string & str ) {
176        Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) );
177        delete &str;                                                                            // created by lex
178        return ret;
179} // build_constantChar
180
181ConstantExpr * build_constantStr( const std::string & str ) {
182        // string should probably be a primitive type
183        ArrayType * at = new ArrayType( noQualifiers, new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char ),
184                                                                   new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"'
185                                                                   false, false );
186        ConstantExpr * ret = new ConstantExpr( Constant( at, str, (unsigned long long int)0 ) ); // constant 0 is ignored for pure string value
187        delete &str;                                                                            // created by lex
188        return ret;
189} // build_constantStr
190
191Expression * build_field_name_FLOATINGconstant( const std::string & str ) {
192        // str is of the form A.B -> separate at the . and return member expression
193        int a, b;
194        char dot;
195        std::stringstream ss( str );
196        ss >> a >> dot >> b;
197        UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) );
198        delete &str;
199        return ret;
200} // build_field_name_FLOATINGconstant
201
202Expression * make_field_name_fraction_constants( Expression * fieldName, Expression * fracts ) {
203        if ( fracts ) {
204                if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( fracts ) ) {
205                        memberExpr->set_member( make_field_name_fraction_constants( fieldName, memberExpr->get_aggregate() ) );
206                        return memberExpr;
207                } else {
208                        return new UntypedMemberExpr( fracts, fieldName );
209                }
210        }
211        return fieldName;
212} // make_field_name_fraction_constants
213
214Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts ) {
215        return make_field_name_fraction_constants( fieldName, maybeMoveBuild< Expression >( fracts ) );
216} // build_field_name_fraction_constants
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} // build_varref
237
238
239static const char * OperName[] = {                                              // must harmonize with OperKinds
240        // diadic
241        "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&",
242        "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
243        "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
244        "?[?]", "...",
245        // monadic
246        "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&"
247}; // OperName
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} // build_cast
258
259Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
260        Type * targetType = maybeMoveBuildType( decl_node );
261        Expression * castArg = maybeMoveBuild< Expression >( expr_node );
262        return new VirtualCastExpr( castArg, targetType );
263} // build_virtual_cast
264
265Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) {
266        UntypedMemberExpr * ret = new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) );
267        return ret;
268} // build_fieldSel
269
270Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) {
271        UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) );
272        deref->location = expr_node->location;
273        deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) );
274        UntypedMemberExpr * ret = new UntypedMemberExpr( member, deref );
275        return ret;
276} // build_pfieldSel
277
278Expression * build_addressOf( ExpressionNode * expr_node ) {
279                return new AddressExpr( maybeMoveBuild< Expression >(expr_node) );
280} // build_addressOf
281
282Expression * build_sizeOfexpr( ExpressionNode * expr_node ) {
283        return new SizeofExpr( maybeMoveBuild< Expression >(expr_node) );
284} // build_sizeOfexpr
285
286Expression * build_sizeOftype( DeclarationNode * decl_node ) {
287        return new SizeofExpr( maybeMoveBuildType( decl_node ) );
288} // build_sizeOftype
289
290Expression * build_alignOfexpr( ExpressionNode * expr_node ) {
291        return new AlignofExpr( maybeMoveBuild< Expression >(expr_node) );
292} // build_alignOfexpr
293
294Expression * build_alignOftype( DeclarationNode * decl_node ) {
295        return new AlignofExpr( maybeMoveBuildType( decl_node) );
296} // build_alignOftype
297
298Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) {
299        Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() );
300        delete member;
301        return ret;
302} // build_offsetOf
303
304Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ) {
305        return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind );
306} // build_and_or
307
308Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) {
309        std::list< Expression * > args;
310        args.push_back( maybeMoveBuild< Expression >(expr_node) );
311        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
312} // build_unary_val
313
314Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node ) {
315        std::list< Expression * > args;
316        args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node) ) );
317        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
318} // build_unary_ptr
319
320Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
321        std::list< Expression * > args;
322        args.push_back( maybeMoveBuild< Expression >(expr_node1) );
323        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
324        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
325} // build_binary_val
326
327Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
328        std::list< Expression * > args;
329        args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node1) ) );
330        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
331        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
332} // build_binary_ptr
333
334Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) {
335        return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) );
336} // build_cond
337
338Expression * build_comma( ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
339        return new CommaExpr( maybeMoveBuild< Expression >(expr_node1), maybeMoveBuild< Expression >(expr_node2) );
340} // build_comma
341
342Expression * build_attrexpr( NameExpr * var, ExpressionNode * expr_node ) {
343        return new AttrExpr( var, maybeMoveBuild< Expression >(expr_node) );
344} // build_attrexpr
345
346Expression * build_attrtype( NameExpr * var, DeclarationNode * decl_node ) {
347        return new AttrExpr( var, maybeMoveBuildType( decl_node ) );
348} // build_attrtype
349
350Expression * build_tuple( ExpressionNode * expr_node ) {
351        std::list< Expression * > exprs;
352        buildMoveList( expr_node, exprs );
353        return new UntypedTupleExpr( exprs );;
354} // build_tuple
355
356Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
357        std::list< Expression * > args;
358        buildMoveList( expr_node, args );
359        return new UntypedExpr( maybeMoveBuild< Expression >(function), args, nullptr );
360} // build_func
361
362Expression * build_range( ExpressionNode * low, ExpressionNode * high ) {
363        return new RangeExpr( maybeMoveBuild< Expression >( low ), maybeMoveBuild< Expression >( high ) );
364} // build_range
365
366Expression * build_asmexpr( ExpressionNode * inout, ConstantExpr * constraint, ExpressionNode * operand ) {
367        return new AsmExpr( maybeMoveBuild< Expression >( inout ), constraint, maybeMoveBuild< Expression >(operand) );
368} // build_asmexpr
369
370Expression * build_valexpr( StatementNode * s ) {
371        return new StmtExpr( dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(s) ) );
372} // build_valexpr
373
374Expression * build_typevalue( DeclarationNode * decl ) {
375        return new TypeExpr( maybeMoveBuildType( decl ) );
376} // build_typevalue
377
378Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids ) {
379        Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type
380        if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type
381                return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild< Initializer >(kids) );
382        // these types do not have associated type information
383        } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl )  ) {
384                if ( newDeclStructDecl->has_body() ) {
385                        return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl ), maybeMoveBuild< Initializer >(kids) );
386                } else {
387                        return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
388                } // if
389        } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl )  ) {
390                if ( newDeclUnionDecl->has_body() ) {
391                        return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl ), maybeMoveBuild< Initializer >(kids) );
392                } else {
393                        return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
394                } // if
395        } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl )  ) {
396                if ( newDeclEnumDecl->has_body() ) {
397                        return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl ), maybeMoveBuild< Initializer >(kids) );
398                } else {
399                        return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
400                } // if
401        } else {
402                assert( false );
403        } // if
404} // build_compoundLiteral
405
406// Local Variables: //
407// tab-width: 4 //
408// mode: c++ //
409// compile-command: "make install" //
410// End: //
Note: See TracBrowser for help on using the repository browser.