source: src/Parser/ExpressionNode.cc @ 6896548

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 6896548 was 6896548, checked in by Michael Brooks <mlbrooks@…>, 5 years ago

Fixed convert-convert issues with strings, when conversion happens after resolve. Three specific issues fixed.

  1. String literals were not roundtripping new-old-new with the original type at the constant level, whereas the resolver changes the expression-level type from array to pointer.
  2. String literals were roundtripping with noise on a "--print astexpr" diff, with the char-array's index type coming up signed int (expecting unsigned long long int).
  3. Function calls that pass a foo[] variable as argument to a foo* formal were crashing on copy-constructor generation because convert-convert was changing the arg-expression type from foo* (set by the resolver, as expecteed) back to foo[], taken from the variable (switched to foo* taken from convert-src expression).
  • Property mode set to 100644
File size: 22.5 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           : Peter A. Buhr
10// Created On       : Sat May 16 13:17:07 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sun Mar 10 16:10:32 2019
13// Update Count     : 976
14//
15
16#include <cassert>                 // 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
53// static inline bool checkH( char c ) { return c == 'h' || c == 'H'; }
54// static inline bool checkZ( char c ) { return c == 'z' || c == 'Z'; }
55// static inline bool checkU( char c ) { return c == 'u' || c == 'U'; }
56static inline bool checkF( char c ) { return c == 'f' || c == 'F'; }
57static inline bool checkD( char c ) { return c == 'd' || c == 'D'; }
58static inline bool checkF80( char c ) { return c == 'w' || c == 'W'; }
59static inline bool checkF128( char c ) { return c == 'q' || c == 'Q'; }
60static inline bool checkL( char c ) { return c == 'l' || c == 'L'; }
61static inline bool checkI( char c ) { return c == 'i' || c == 'I'; }
62static inline bool checkB( char c ) { return c == 'b' || c == 'B'; }
63static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }
64// static inline bool checkN( char c ) { return c == 'n' || c == 'N'; }
65
66void lnthSuffix( string & str, int & type, int & ltype ) {
67        string::size_type posn = str.find_last_of( "lL" );
68
69        if ( posn == string::npos ) return;                                     // no suffix
70        if ( posn == str.length() - 1 ) { type = 3; return; } // no length => long
71
72        string::size_type next = posn + 1;                                      // advance to length
73        if ( str[next] == '3' ) {                                                       // 32
74                type = ltype = 2;
75        } else if ( str[next] == '6' ) {                                        // 64
76                type = ltype = 3;
77        } else if ( str[next] == '8' ) {                                        // 8
78                type = ltype = 1;
79        } else if ( str[next] == '1' ) {
80                if ( str[next + 1] == '6' ) {                                   // 16
81                        type = ltype = 0;
82                } else {                                                                                // 128
83                        type = 5; ltype = 6;
84                } // if
85        } // if
86        // remove "lL" for these cases because it may not imply long
87        str.erase( posn );                                                                      // remove length
88} // lnthSuffix
89
90void valueToType( unsigned long long int & v, bool dec, int & type, bool & Unsigned ) {
91        // use value to determine type
92        if ( v <= INT_MAX ) {                                                           // signed int
93                type = 2;
94        } else if ( v <= UINT_MAX && ! dec ) {                          // unsigned int
95                type = 2;
96                Unsigned = true;                                                                // unsigned
97        } else if ( v <= LONG_MAX ) {                                           // signed long int
98                type = 3;
99        } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int
100                type = 3;
101                Unsigned = true;                                                                // unsigned long int
102        } else if ( v <= LLONG_MAX ) {                                          // signed long long int
103                type = 4;
104        } else {                                                                                        // unsigned long long int
105                type = 4;
106                Unsigned = true;                                                                // unsigned long long int
107        } // if
108} // valueToType
109
110Expression * build_constantInteger( string & str ) {
111        static const BasicType::Kind kind[2][7] = {
112                // short (h) must be before char (hh) because shorter type has the longer suffix
113                { BasicType::ShortSignedInt, BasicType::SignedChar, BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, BasicType::SignedInt128, },
114                { BasicType::ShortUnsignedInt, BasicType::UnsignedChar, BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::UnsignedInt128, },
115        };
116
117        static const char * lnthsInt[2][6] = {
118                { "int16_t",  "int8_t",  "int32_t",  "int64_t",  "size_t",  "uintptr_t", },
119                { "uint16_t", "uint8_t", "uint32_t", "uint64_t", "size_t",  "uintptr_t", },
120        }; // lnthsInt
121
122        unsigned long long int v;                                                       // converted integral value
123        size_t last = str.length() - 1;                                         // last subscript of constant
124        Expression * ret;
125        //string fred( str );
126
127        int type = -1;                                                                          // 0 => short, 1 => char, 2 => int, 3 => long int, 4 => long long int, 5 => int128
128        int ltype = -1;                                                                         // 0 => 16 bits, 1 => 8 bits, 2 => 32 bits, 3 => 64 bits, 4 => size_t, 5 => intptr, 6 => pointer
129        bool dec = true, Unsigned = false;                                      // decimal, unsigned constant
130
131        // special constants
132        if ( str == "0" ) {
133                ret = new ConstantExpr( Constant( (Type *)new ZeroType( noQualifiers ), str, (unsigned long long int)0 ) );
134                goto CLEANUP;
135        } // if
136        if ( str == "1" ) {
137                ret = new ConstantExpr( Constant( (Type *)new OneType( noQualifiers ), str, (unsigned long long int)1 ) );
138                goto CLEANUP;
139        } // if
140
141        // Cannot be just "0"/"1"; sscanf stops at the suffix, if any; value goes over the wall => always generate
142
143        if ( str[0] == '0' ) {                                                          // radix character ?
144                dec = false;
145                if ( checkX( str[1] ) ) {                                               // hex constant ?
146                        sscanf( (char *)str.c_str(), "%llx", &v );
147                        //printf( "%llx %llu\n", v, v );
148                } else if ( checkB( str[1] ) ) {                                // binary constant ?
149                        v = 0;                                                                          // compute value
150                        for ( unsigned int i = 2;; ) {                          // ignore prefix
151                                if ( str[i] == '1' ) v |= 1;
152                                i += 1;
153                          if ( i == last - 1 || (str[i] != '0' && str[i] != '1') ) break;
154                                v <<= 1;
155                        } // for
156                        //printf( "%#llx %llu\n", v, v );
157                } else {                                                                                // octal constant
158                        sscanf( (char *)str.c_str(), "%llo", &v );
159                        //printf( "%#llo %llu\n", v, v );
160                } // if
161        } else {                                                                                        // decimal constant ?
162                sscanf( (char *)str.c_str(), "%llu", &v );
163                //printf( "%llu\n", v );
164        } // if
165
166        string::size_type posn;
167
168        if ( isdigit( str[last] ) ) {                                           // no suffix ?
169                lnthSuffix( str, type, ltype );                                 // could have length suffix
170                if ( type == -1 ) {                                                             // no suffix
171                        valueToType( v, dec, type, Unsigned );
172                } // if
173        } else {
174                // At least one digit in integer constant, so safe to backup while looking for suffix.
175
176                posn = str.find_last_of( "pP" );
177                if ( posn != string::npos ) { valueToType( v, dec, type, Unsigned ); ltype = 5; str.erase( posn, 1 ); goto FINI; }
178
179                posn = str.find_last_of( "zZ" );
180                if ( posn != string::npos ) { Unsigned = true; type = 2; ltype = 4; str.erase( posn, 1 ); goto FINI; }
181
182                // 'u' can appear before or after length suffix
183                if ( str.find_last_of( "uU" ) != string::npos ) Unsigned = true;
184
185                posn = str.rfind( "hh" );
186                if ( posn != string::npos ) { type = 1; str.erase( posn, 2 ); goto FINI; }
187
188                posn = str.rfind( "HH" );
189                if ( posn != string::npos ) { type = 1; str.erase( posn, 2 ); goto FINI; }
190
191                posn = str.find_last_of( "hH" );
192                if ( posn != string::npos ) { type = 0; str.erase( posn, 1 ); goto FINI; }
193
194                posn = str.find_last_of( "nN" );
195                if ( posn != string::npos ) { type = 2; str.erase( posn, 1 ); goto FINI; }
196
197                if ( str.rfind( "ll" ) != string::npos || str.rfind( "LL" ) != string::npos ) { type = 4; goto FINI; }
198
199                lnthSuffix( str, type, ltype );                                 // must be after check for "ll"
200                if ( type == -1 ) {                                                             // only 'u' suffix ?
201                        valueToType( v, dec, type, Unsigned );
202                } // if
203          FINI: ;
204        } // if
205
206        //if ( !( 0 <= type && type <= 6 ) ) { printf( "%s %lu %d %s\n", fred.c_str(), fred.length(), type, str.c_str() ); }
207        assert( 0 <= type && type <= 6 );
208
209        // Constant type is correct for overload resolving.
210        ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][type] ), str, v ) );
211        if ( Unsigned && type < 2 ) {                                           // hh or h, less than int ?
212                // int i = -1uh => 65535 not -1, so cast is necessary for unsigned, which unfortunately eliminates warnings for large values.
213                ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][type] ), false );
214        } else if ( ltype != -1 ) {                                                     // explicit length ?
215                if ( ltype == 6 ) {                                                             // int128, (int128)constant
216                        ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][type] ), false );
217                } else {                                                                                // explicit length, (length_type)constant
218                        ret = new CastExpr( ret, new TypeInstType( Type::Qualifiers(), lnthsInt[Unsigned][ltype], false ), false );
219                        if ( ltype == 5 ) {                                                     // pointer, intptr( (uintptr_t)constant )
220                                ret = build_func( new ExpressionNode( build_varref( new string( "intptr" ) ) ), new ExpressionNode( ret ) );
221                        } // if
222                } // if
223        } // if
224
225  CLEANUP: ;
226        delete &str;                                                                            // created by lex
227        return ret;
228} // build_constantInteger
229
230
231static inline void checkFnxFloat( string & str, size_t last, bool & explnth, int & type ) {
232        string::size_type posn;
233        // floating-point constant has minimum of 2 characters, 1. or .1, so safe to look ahead
234        if ( str[1] == 'x' ) {                                                          // hex ?
235                posn = str.find_last_of( "pP" );                                // back for exponent (must have)
236                posn = str.find_first_of( "fF", posn + 1 );             // forward for size (fF allowed in hex constant)
237        } else {
238                posn = str.find_last_of( "fF" );                                // back for size (fF not allowed)
239        } // if
240  if ( posn == string::npos ) return;
241        explnth = true;
242        posn += 1;                                                                                      // advance to size
243        if ( str[posn] == '3' ) {                                                       // 32
244                if ( str[last] != 'x' ) type = 6;
245                else type = 7;
246        } else if ( str[posn] == '6' ) {                                        // 64
247                if ( str[last] != 'x' ) type = 8;
248                else type = 9;
249        } else if ( str[posn] == '8' ) {                                        // 80
250                type = 3;
251        } else if ( str[posn] == '1' ) {                                        // 16/128
252                if ( str[posn + 1] == '6' ) {                                   // 16
253                        type = 5;
254                } else {                                                                                // 128
255                        if ( str[last] != 'x' ) type = 10;
256                        else type = 11;
257                } // if
258        } else {
259                assertf( false, "internal error, bad floating point length %s", str.c_str() );
260        } // if
261} // checkFnxFloat
262
263
264Expression * build_constantFloat( string & str ) {
265        static const BasicType::Kind kind[2][12] = {
266                { BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::uuFloat80, BasicType::uuFloat128, BasicType::uFloat16, BasicType::uFloat32, BasicType::uFloat32x, BasicType::uFloat64, BasicType::uFloat64x, BasicType::uFloat128, BasicType::uFloat128x },
267                { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, (BasicType::Kind)-1, (BasicType::Kind)-1, BasicType::uFloat16Complex, BasicType::uFloat32Complex, BasicType::uFloat32xComplex, BasicType::uFloat64Complex, BasicType::uFloat64xComplex, BasicType::uFloat128Complex, BasicType::uFloat128xComplex },
268        };
269
270        // floating-point constant has minimum of 2 characters 1. or .1
271        size_t last = str.length() - 1;
272        double v;
273        int type;                                                                                       // 0 => float, 1 => double, 3 => long double, ...
274        bool complx = false;                                                            // real, complex
275        bool explnth = false;                                                           // explicit literal length
276
277        sscanf( str.c_str(), "%lg", &v );
278
279        if ( checkI( str[last] ) ) {                                            // imaginary ?
280                complx = true;
281                last -= 1;                                                                              // backup one character
282        } // if
283
284        if ( checkF( str[last] ) ) {                                            // float ?
285                type = 0;
286        } else if ( checkD( str[last] ) ) {                                     // double ?
287                type = 1;
288        } else if ( checkL( str[last] ) ) {                                     // long double ?
289                type = 2;
290        } else if ( checkF80( str[last] ) ) {                           // __float80 ?
291                type = 3;
292        } else if ( checkF128( str[last] ) ) {                          // __float128 ?
293                type = 4;
294        } else {
295                type = 1;                                                                               // double (default if no suffix)
296                checkFnxFloat( str, last, explnth, type );
297        } // if
298
299        if ( ! complx && checkI( str[last - 1] ) ) {            // imaginary ?
300                complx = true;
301        } // if
302
303        assert( 0 <= type && type < 12 );
304        Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][type] ), str, v ) );
305        if ( explnth ) {                                                                        // explicit length ?
306                ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[complx][type] ), false );
307        } // if
308
309        delete &str;                                                                            // created by lex
310        return ret;
311} // build_constantFloat
312
313static void sepString( string & str, string & units, char delimit ) {
314        string::size_type posn = str.find_last_of( delimit ) + 1;
315        if ( posn != str.length() ) {
316                units = "?" + str.substr( posn );                               // extract units
317                str.erase( posn );                                                              // remove units
318        } // if
319} // sepString
320
321Expression * build_constantChar( string & str ) {
322        string units;                                                                           // units
323        sepString( str, units, '\'' );                                          // separate constant from units
324
325        Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) );
326        if ( units.length() != 0 ) {
327                ret = new UntypedExpr( new NameExpr( units ), { ret } );
328        } // if
329
330        delete &str;                                                                            // created by lex
331        return ret;
332} // build_constantChar
333
334Expression * build_constantStr( string & str ) {
335        assert( str.length() > 0 );
336        string units;                                                                           // units
337        sepString( str, units, '"' );                                           // separate constant from units
338
339        Type * strtype;
340        switch ( str[0] ) {                                                                     // str has >= 2 characters, i.e, null string "" => safe to look at subscripts 0/1
341          case 'u':
342                if ( str[1] == '8' ) goto Default;                              // utf-8 characters => array of char
343                // lookup type of associated typedef
344                strtype = new TypeInstType( Type::Qualifiers( Type::Const ), "char16_t", false );
345                break;
346          case 'U':
347                strtype = new TypeInstType( Type::Qualifiers( Type::Const ), "char32_t", false );
348                break;
349          case 'L':
350                strtype = new TypeInstType( Type::Qualifiers( Type::Const ), "wchar_t", false );
351                break;
352          Default:                                                                                      // char default string type
353          default:
354                strtype = new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char );
355        } // switch
356        Expression * ret = new ConstantExpr( Constant::from_string( str, strtype ) );
357        if ( units.length() != 0 ) {
358                ret = new UntypedExpr( new NameExpr( units ), { ret } );
359        } // if
360
361        delete &str;                                                                            // created by lex
362        return ret;
363} // build_constantStr
364
365Expression * build_field_name_FLOATING_FRACTIONconstant( const string & str ) {
366        if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) SemanticError( yylloc, "invalid tuple index " + str );
367        Expression * ret = build_constantInteger( *new string( str.substr(1) ) );
368        delete &str;
369        return ret;
370} // build_field_name_FLOATING_FRACTIONconstant
371
372Expression * build_field_name_FLOATING_DECIMALconstant( const string & str ) {
373        if ( str[str.size()-1] != '.' ) SemanticError( yylloc, "invalid tuple index " + str );
374        Expression * ret = build_constantInteger( *new string( str.substr( 0, str.size()-1 ) ) );
375        delete &str;
376        return ret;
377} // build_field_name_FLOATING_DECIMALconstant
378
379Expression * build_field_name_FLOATINGconstant( const string & str ) {
380        // str is of the form A.B -> separate at the . and return member expression
381        int a, b;
382        char dot;
383        stringstream ss( str );
384        ss >> a >> dot >> b;
385        UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) );
386        delete &str;
387        return ret;
388} // build_field_name_FLOATINGconstant
389
390Expression * make_field_name_fraction_constants( Expression * fieldName, Expression * fracts ) {
391        if ( fracts ) {
392                if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( fracts ) ) {
393                        memberExpr->set_member( make_field_name_fraction_constants( fieldName, memberExpr->get_aggregate() ) );
394                        return memberExpr;
395                } else {
396                        return new UntypedMemberExpr( fracts, fieldName );
397                } // if
398        } // if
399        return fieldName;
400} // make_field_name_fraction_constants
401
402Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts ) {
403        return make_field_name_fraction_constants( fieldName, maybeMoveBuild< Expression >( fracts ) );
404} // build_field_name_fraction_constants
405
406NameExpr * build_varref( const string * name ) {
407        NameExpr * expr = new NameExpr( *name );
408        delete name;
409        return expr;
410} // build_varref
411
412// TODO: get rid of this and OperKinds and reuse code from OperatorTable
413static const char * OperName[] = {                                              // must harmonize with OperKinds
414        // diadic
415        "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&",
416        "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
417        "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
418        "?[?]", "...",
419        // monadic
420        "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--",
421}; // OperName
422
423Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
424        Type * targetType = maybeMoveBuildType( decl_node );
425        if ( dynamic_cast< VoidType * >( targetType ) ) {
426                delete targetType;
427                return new CastExpr( maybeMoveBuild< Expression >(expr_node), false );
428        } else {
429                return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType, false );
430        } // if
431} // build_cast
432
433Expression * build_keyword_cast( KeywordCastExpr::Target target, ExpressionNode * expr_node ) {
434        return new KeywordCastExpr( maybeMoveBuild< Expression >(expr_node), target );
435}
436
437Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
438        return new VirtualCastExpr( maybeMoveBuild< Expression >( expr_node ), maybeMoveBuildType( decl_node ) );
439} // build_virtual_cast
440
441Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) {
442        return new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) );
443} // build_fieldSel
444
445Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) {
446        UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) );
447        deref->location = expr_node->location;
448        deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) );
449        UntypedMemberExpr * ret = new UntypedMemberExpr( member, deref );
450        return ret;
451} // build_pfieldSel
452
453Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) {
454        Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() );
455        delete member;
456        return ret;
457} // build_offsetOf
458
459Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ) {
460        return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind );
461} // build_and_or
462
463Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) {
464        list< Expression * > args;
465        args.push_back( maybeMoveBuild< Expression >(expr_node) );
466        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
467} // build_unary_val
468
469Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node ) {
470        list< Expression * > args;
471        args.push_back(  maybeMoveBuild< Expression >(expr_node) ); // xxx -- this is exactly the same as the val case now, refactor this code.
472        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
473} // build_unary_ptr
474
475Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
476        list< Expression * > args;
477        args.push_back( maybeMoveBuild< Expression >(expr_node1) );
478        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
479        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
480} // build_binary_val
481
482Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
483        list< Expression * > args;
484        args.push_back( maybeMoveBuild< Expression >(expr_node1) );
485        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
486        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
487} // build_binary_ptr
488
489Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) {
490        return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) );
491} // build_cond
492
493Expression * build_tuple( ExpressionNode * expr_node ) {
494        list< Expression * > exprs;
495        buildMoveList( expr_node, exprs );
496        return new UntypedTupleExpr( exprs );;
497} // build_tuple
498
499Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
500        list< Expression * > args;
501        buildMoveList( expr_node, args );
502        return new UntypedExpr( maybeMoveBuild< Expression >(function), args );
503} // build_func
504
505Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids ) {
506        Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type
507        if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type
508                return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild< Initializer >(kids) );
509        // these types do not have associated type information
510        } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl )  ) {
511                if ( newDeclStructDecl->has_body() ) {
512                        return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl ), maybeMoveBuild< Initializer >(kids) );
513                } else {
514                        return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
515                } // if
516        } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl )  ) {
517                if ( newDeclUnionDecl->has_body() ) {
518                        return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl ), maybeMoveBuild< Initializer >(kids) );
519                } else {
520                        return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
521                } // if
522        } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl )  ) {
523                if ( newDeclEnumDecl->has_body() ) {
524                        return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl ), maybeMoveBuild< Initializer >(kids) );
525                } else {
526                        return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
527                } // if
528        } else {
529                assert( false );
530        } // if
531} // build_compoundLiteral
532
533// Local Variables: //
534// tab-width: 4 //
535// mode: c++ //
536// compile-command: "make install" //
537// End: //
Note: See TracBrowser for help on using the repository browser.