source: src/Parser/ExpressionNode.cc @ c5b55c4

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since c5b55c4 was c5b55c4, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

fix missing 'u' suffix for length suffix, change L128 constants to always generate hexadecimal components

  • Property mode set to 100644
File size: 27.2 KB
RevLine 
[b87a5ed]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//
[0caaa6a]7// ExpressionNode.cc --
8//
[f43df73]9// Author           : Peter A. Buhr
[b87a5ed]10// Created On       : Sat May 16 13:17:07 2015
[76c62b2]11// Last Modified By : Peter A. Buhr
[c5b55c4]12// Last Modified On : Thu Aug 20 11:52:59 2020
13// Update Count     : 1075
[0caaa6a]14//
[b87a5ed]15
[ea6332d]16#include <cassert>                 // for assert
[d180746]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==
[51b7345]22
[d180746]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;
[51b7345]34
35using namespace std;
36
[cd623a4]37//##############################################################################
38
[7bf7fb9]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
[beec62c]51extern const Type::Qualifiers noQualifiers;                             // no qualifiers on constants
[7bf7fb9]52
[ba01b14]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'; }
[7bf7fb9]56static inline bool checkF( char c ) { return c == 'f' || c == 'F'; }
57static inline bool checkD( char c ) { return c == 'd' || c == 'D'; }
[ba01b14]58static inline bool checkF80( char c ) { return c == 'w' || c == 'W'; }
59static inline bool checkF128( char c ) { return c == 'q' || c == 'Q'; }
[f56c32e]60static inline bool checkL( char c ) { return c == 'l' || c == 'L'; }
[7bf7fb9]61static inline bool checkI( char c ) { return c == 'i' || c == 'I'; }
[0a2168f]62static inline bool checkB( char c ) { return c == 'b' || c == 'B'; }
[7bf7fb9]63static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }
[3d8d7a7]64// static inline bool checkN( char c ) { return c == 'n' || c == 'N'; }
[f56c32e]65
66void lnthSuffix( string & str, int & type, int & ltype ) {
[c5b55c4]67        // 'u' can appear before or after length suffix
[f56c32e]68        string::size_type posn = str.find_last_of( "lL" );
[0a616e0]69
70        if ( posn == string::npos ) return;                                     // no suffix
[c5b55c4]71        size_t end = str.length() - 1;
72        if ( posn == end ) { type = 3; return; }                        // no length after 'l' => long
73       
[0a616e0]74        string::size_type next = posn + 1;                                      // advance to length
75        if ( str[next] == '3' ) {                                                       // 32
76                type = ltype = 2;
77        } else if ( str[next] == '6' ) {                                        // 64
78                type = ltype = 3;
79        } else if ( str[next] == '8' ) {                                        // 8
80                type = ltype = 1;
81        } else if ( str[next] == '1' ) {
82                if ( str[next + 1] == '6' ) {                                   // 16
83                        type = ltype = 0;
84                } else {                                                                                // 128
85                        type = 5; ltype = 6;
[f56c32e]86                } // if
87        } // if
[c5b55c4]88
89        char fix = '\0';
90        if ( str[end] == 'u' || str[end] == 'U' ) fix = str[end]; // ends with 'uU' ?
91        str.erase( posn );                                                                      // remove length suffix and possibly uU
92        if ( type == 5 ) {                                                                      // L128 does not need uU
93                end = str.length() - 1;
94                if ( str[end] == 'u' || str[end] == 'U' ) str.erase( end ); // ends with 'uU' ? remove
95        } else if ( fix != '\0' ) str += fix;                           // put 'uU' back if removed
[f56c32e]96} // lnthSuffix
[7bf7fb9]97
[0a616e0]98void valueToType( unsigned long long int & v, bool dec, int & type, bool & Unsigned ) {
99        // use value to determine type
100        if ( v <= INT_MAX ) {                                                           // signed int
101                type = 2;
102        } else if ( v <= UINT_MAX && ! dec ) {                          // unsigned int
103                type = 2;
104                Unsigned = true;                                                                // unsigned
105        } else if ( v <= LONG_MAX ) {                                           // signed long int
106                type = 3;
107        } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int
108                type = 3;
109                Unsigned = true;                                                                // unsigned long int
110        } else if ( v <= LLONG_MAX ) {                                          // signed long long int
111                type = 4;
112        } else {                                                                                        // unsigned long long int
113                type = 4;
114                Unsigned = true;                                                                // unsigned long long int
115        } // if
116} // valueToType
117
[f6582252]118static void scanbin( string & str, unsigned long long int & v ) {
119        v = 0;
120        size_t last = str.length() - 1;                                         // last subscript of constant
121        for ( unsigned int i = 2;; ) {                                          // ignore prefix
122                if ( str[i] == '1' ) v |= 1;
123                i += 1;
124          if ( i == last - 1 || (str[i] != '0' && str[i] != '1') ) break;
125                v <<= 1;
126        } // for
127} // scanbin
128
[ba2a68b]129Expression * build_constantInteger( string & str ) {
[05c27fc]130        static const BasicType::Kind kind[2][6] = {
[ba01b14]131                // short (h) must be before char (hh) because shorter type has the longer suffix
[f6582252]132                { BasicType::ShortSignedInt, BasicType::SignedChar, BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, /* BasicType::SignedInt128 */ BasicType::LongLongSignedInt, },
133                { BasicType::ShortUnsignedInt, BasicType::UnsignedChar, BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, /* BasicType::UnsignedInt128 */ BasicType::LongLongUnsignedInt, },
[7bf7fb9]134        };
[76c62b2]135
[0a616e0]136        static const char * lnthsInt[2][6] = {
137                { "int16_t",  "int8_t",  "int32_t",  "int64_t",  "size_t",  "uintptr_t", },
138                { "uint16_t", "uint8_t", "uint32_t", "uint64_t", "size_t",  "uintptr_t", },
[ba01b14]139        }; // lnthsInt
140
[f6582252]141        string str2( "0x0" );
142        unsigned long long int v, v2 = 0;                                       // converted integral value
143        Expression * ret, * ret2;
[0a616e0]144
145        int type = -1;                                                                          // 0 => short, 1 => char, 2 => int, 3 => long int, 4 => long long int, 5 => int128
146        int ltype = -1;                                                                         // 0 => 16 bits, 1 => 8 bits, 2 => 32 bits, 3 => 64 bits, 4 => size_t, 5 => intptr, 6 => pointer
147        bool dec = true, Unsigned = false;                                      // decimal, unsigned constant
[7bf7fb9]148
[6165ce7]149        // special constants
150        if ( str == "0" ) {
151                ret = new ConstantExpr( Constant( (Type *)new ZeroType( noQualifiers ), str, (unsigned long long int)0 ) );
152                goto CLEANUP;
153        } // if
154        if ( str == "1" ) {
155                ret = new ConstantExpr( Constant( (Type *)new OneType( noQualifiers ), str, (unsigned long long int)1 ) );
156                goto CLEANUP;
157        } // if
[ea6332d]158
[ba01b14]159        string::size_type posn;
160
[f6582252]161        // 'u' can appear before or after length suffix
162        if ( str.find_last_of( "uU" ) != string::npos ) Unsigned = true;
163
164        if ( isdigit( str[str.length() - 1] ) ) {                       // no suffix ?
[f56c32e]165                lnthSuffix( str, type, ltype );                                 // could have length suffix
166        } else {
167                // At least one digit in integer constant, so safe to backup while looking for suffix.
168
[f6582252]169                posn = str.find_last_of( "pP" );                                // pointer value
170                if ( posn != string::npos ) { ltype = 5; str.erase( posn, 1 ); goto FINI; }
[0a616e0]171
[f6582252]172                posn = str.find_last_of( "zZ" );                                // size_t
[0a616e0]173                if ( posn != string::npos ) { Unsigned = true; type = 2; ltype = 4; str.erase( posn, 1 ); goto FINI; }
174
[f6582252]175                posn = str.rfind( "hh" );                                               // char
[f56c32e]176                if ( posn != string::npos ) { type = 1; str.erase( posn, 2 ); goto FINI; }
[ba01b14]177
[f6582252]178                posn = str.rfind( "HH" );                                               // char
[f56c32e]179                if ( posn != string::npos ) { type = 1; str.erase( posn, 2 ); goto FINI; }
[ba01b14]180
[f6582252]181                posn = str.find_last_of( "hH" );                                // short
[f56c32e]182                if ( posn != string::npos ) { type = 0; str.erase( posn, 1 ); goto FINI; }
[ba01b14]183
[f6582252]184                posn = str.find_last_of( "nN" );                                // int (natural number)
[f56c32e]185                if ( posn != string::npos ) { type = 2; str.erase( posn, 1 ); goto FINI; }
[ba01b14]186
[f56c32e]187                if ( str.rfind( "ll" ) != string::npos || str.rfind( "LL" ) != string::npos ) { type = 4; goto FINI; }
188
189                lnthSuffix( str, type, ltype );                                 // must be after check for "ll"
190          FINI: ;
[7bf7fb9]191        } // if
192
[f6582252]193        // Cannot be just "0"/"1"; sscanf stops at the suffix, if any; value goes over the wall => always generate
[dbe8e31c]194
[cf5af9c]195#if ! defined(__SIZEOF_INT128__)
[dbe8e31c]196        if ( type == 5 ) SemanticError( yylloc, "int128 constant is not supported on this target " + str );
[cf5af9c]197#endif // ! __SIZEOF_INT128__
[f6582252]198       
199        if ( str[0] == '0' ) {                                                          // radix character ?
200                dec = false;
201                if ( checkX( str[1] ) ) {                                               // hex constant ?
202                        if ( type < 5 ) {                                                       // not L128 ?
203                                sscanf( (char *)str.c_str(), "%llx", &v );
[c5b55c4]204#if defined(__SIZEOF_INT128__)
[f6582252]205                        } else {                                                                        // hex int128 constant
206                                unsigned int len = str.length();
207                                if ( len > (2 + 16 + 16) ) SemanticError( yylloc, "128-bit hexadecimal constant to large " + str );
208                          if ( len <= (2 + 16) ) goto FHEX1;            // hex digits < 2^64
209                                str2 = "0x" + str.substr( len - 16 );
210                                sscanf( (char *)str2.c_str(), "%llx", &v2 );
211                                str = str.substr( 0, len - 16 );
212                          FHEX1: ;
213                                sscanf( (char *)str.c_str(), "%llx", &v );
[c5b55c4]214#endif // __SIZEOF_INT128__
[f6582252]215                        } // if
216                        //printf( "%llx %llu\n", v, v );
217                } else if ( checkB( str[1] ) ) {                                // binary constant ?
218                        unsigned int len = str.length();
[c5b55c4]219#if defined(__SIZEOF_INT128__)
[f6582252]220                        if ( type == 5 && len > 2 + 64 ) {
221                                if ( len > 2 + 64 + 64 ) SemanticError( yylloc, "128-bit binary constant to large " + str );
222                                str2 = "0b" + str.substr( len - 64 );
223                                str = str.substr( 0, len - 64 );
224                                scanbin( str2, v2 );
225                        } // if
[c5b55c4]226#endif // __SIZEOF_INT128__
[f6582252]227                        scanbin( str, v );
228                        //printf( "%#llx %llu\n", v, v );
229                } else {                                                                                // octal constant
230                        if ( type < 5 ) {                                                       // not L128 ?
231                                sscanf( (char *)str.c_str(), "%llo", &v );
[cf5af9c]232#if defined(__SIZEOF_INT128__)
[f6582252]233                        } else {                                                                        // octal int128 constant
234                                unsigned int len = str.length();
235                                if ( len > 1 + 43 || (len == 1 + 43 && str[0] > '3') ) SemanticError( yylloc, "128-bit octal constant to large " + str );
[c5b55c4]236                                char buf[32];
[f6582252]237                                if ( len <= 1 + 21 ) {                                  // value < 21 octal digitis
[c5b55c4]238                                        sscanf( (char *)str.c_str(), "%llo", &v );
[f6582252]239                                } else {
240                                        sscanf( &str[len - 21], "%llo", &v );
[791028a]241                                        __int128 val = v;                                       // accumulate bits
[f6582252]242                                        str[len - 21] ='\0';                            // shorten string
243                                        sscanf( &str[len == 43 ? 1 : 0], "%llo", &v );
244                                        val |= (__int128)v << 63;                       // store bits
245                                        if ( len == 1 + 43 ) {                          // most significant 2 bits ?
246                                                str[2] = '\0';                                  // shorten string
247                                                sscanf( &str[1], "%llo", &v );  // process most significant 2 bits
248                                                val |= (__int128)v << 126;              // store bits
249                                        } // if
250                                        v = val >> 64; v2 = (uint64_t)val;      // replace octal constant with 2 hex constants
251                                        sprintf( buf, "%#llx", v2 );
252                                        str2 = buf;
253                                } // if
[c5b55c4]254                                sprintf( buf, "%#llx", v );
255                                str = buf;
[cf5af9c]256#endif // __SIZEOF_INT128__
[f6582252]257                        } // if
258                        //printf( "%#llo %llu\n", v, v );
259                } // if
260        } else {                                                                                        // decimal constant ?
261                if ( type < 5 ) {                                                               // not L128 ?
262                        sscanf( (char *)str.c_str(), "%llu", &v );
[cf5af9c]263#if defined(__SIZEOF_INT128__)
[f6582252]264                } else {                                                                                // decimal int128 constant
265                        #define P10_UINT64 10'000'000'000'000'000'000ULL // 19 zeroes
266                        unsigned int len = str.length();
267                        if ( str.length() == 39 && str > (Unsigned ? "340282366920938463463374607431768211455" : "170141183460469231731687303715884105727") )
268                                SemanticError( yylloc, "128-bit decimal constant to large " + str );
[c5b55c4]269                        char buf[32];
[f6582252]270                        if ( len <= 19 ) {                                                      // value < 19 decimal digitis
[c5b55c4]271                                sscanf( (char *)str.c_str(), "%llu", &v );
[f6582252]272                        } else {
273                                sscanf( &str[len - 19], "%llu", &v );
[791028a]274                                __int128 val = v;                                               // accumulate bits
[f6582252]275                                str[len - 19] ='\0';                                    // shorten string
276                                sscanf( &str[len == 39 ? 1 : 0], "%llu", &v );
277                                val += (__int128)v * (__int128)P10_UINT64; // store bits
278                                if ( len == 39 ) {                                              // most significant 2 bits ?
279                                        str[1] = '\0';                                          // shorten string
280                                        sscanf( &str[0], "%llu", &v );          // process most significant 2 bits
281                                        val += (__int128)v * (__int128)P10_UINT64 * (__int128)P10_UINT64; // store bits
282                                } // if
283                                v = val >> 64; v2 = (uint64_t)val;              // replace decimal constant with 2 hex constants
284                                sprintf( buf, "%#llx", v2 );
285                                str2 = buf;
286                        } // if
[c5b55c4]287                        sprintf( buf, "%#llx", v );
288                        str = buf;
[cf5af9c]289#endif // __SIZEOF_INT128__
[f6582252]290                } // if
291                //printf( "%llu\n", v );
292        } // if
293
294        if ( type == -1 ) {                                                                     // no suffix => determine type from value size
295                valueToType( v, dec, type, Unsigned );
296        } // if
297        /* printf( "%s %llo %s %llo\n", str.c_str(), v, str2.c_str(), v2 ); */
298
[0a616e0]299        //if ( !( 0 <= type && type <= 6 ) ) { printf( "%s %lu %d %s\n", fred.c_str(), fred.length(), type, str.c_str() ); }
300        assert( 0 <= type && type <= 6 );
[f56c32e]301
[a6c5d7c]302        // Constant type is correct for overload resolving.
[ba01b14]303        ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][type] ), str, v ) );
304        if ( Unsigned && type < 2 ) {                                           // hh or h, less than int ?
[201aeb9]305                // int i = -1uh => 65535 not -1, so cast is necessary for unsigned, which unfortunately eliminates warnings for large values.
[ba01b14]306                ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][type] ), false );
307        } else if ( ltype != -1 ) {                                                     // explicit length ?
[0a616e0]308                if ( ltype == 6 ) {                                                             // int128, (int128)constant
[f6582252]309//                      ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][type] ), false );
310                        ret2 = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::LongLongSignedInt ), str2, v2 ) );
311                        ret = build_compoundLiteral( DeclarationNode::newBasicType( DeclarationNode::Int128 )->addType( DeclarationNode::newSignedNess( DeclarationNode::Unsigned ) ),
312                                                                                 new InitializerNode( (InitializerNode *)(new InitializerNode( new ExpressionNode( v2 == 0 ? ret2 : ret ) ))->set_last( new InitializerNode( new ExpressionNode( v2 == 0 ? ret : ret2 ) ) ), true ) );
[0a616e0]313                } else {                                                                                // explicit length, (length_type)constant
[ba01b14]314                        ret = new CastExpr( ret, new TypeInstType( Type::Qualifiers(), lnthsInt[Unsigned][ltype], false ), false );
[3d8d7a7]315                        if ( ltype == 5 ) {                                                     // pointer, intptr( (uintptr_t)constant )
316                                ret = build_func( new ExpressionNode( build_varref( new string( "intptr" ) ) ), new ExpressionNode( ret ) );
[0a616e0]317                        } // if
[201aeb9]318                } // if
[7b1d5ec]319        } // if
[e8ccca3]320
[f56c32e]321  CLEANUP: ;
[ab57786]322        delete &str;                                                                            // created by lex
323        return ret;
[7bf7fb9]324} // build_constantInteger
[51b7345]325
[201aeb9]326
[ba01b14]327static inline void checkFnxFloat( string & str, size_t last, bool & explnth, int & type ) {
328        string::size_type posn;
329        // floating-point constant has minimum of 2 characters, 1. or .1, so safe to look ahead
330        if ( str[1] == 'x' ) {                                                          // hex ?
331                posn = str.find_last_of( "pP" );                                // back for exponent (must have)
332                posn = str.find_first_of( "fF", posn + 1 );             // forward for size (fF allowed in hex constant)
333        } else {
334                posn = str.find_last_of( "fF" );                                // back for size (fF not allowed)
335        } // if
[201aeb9]336  if ( posn == string::npos ) return;
[ba01b14]337        explnth = true;
[201aeb9]338        posn += 1;                                                                                      // advance to size
339        if ( str[posn] == '3' ) {                                                       // 32
[ba01b14]340                if ( str[last] != 'x' ) type = 6;
341                else type = 7;
[201aeb9]342        } else if ( str[posn] == '6' ) {                                        // 64
[ba01b14]343                if ( str[last] != 'x' ) type = 8;
344                else type = 9;
345        } else if ( str[posn] == '8' ) {                                        // 80
346                type = 3;
347        } else if ( str[posn] == '1' ) {                                        // 16/128
348                if ( str[posn + 1] == '6' ) {                                   // 16
349                        type = 5;
350                } else {                                                                                // 128
351                        if ( str[last] != 'x' ) type = 10;
352                        else type = 11;
353                } // if
[201aeb9]354        } else {
355                assertf( false, "internal error, bad floating point length %s", str.c_str() );
356        } // if
[ba01b14]357} // checkFnxFloat
[201aeb9]358
359
[f43df73]360Expression * build_constantFloat( string & str ) {
[ba01b14]361        static const BasicType::Kind kind[2][12] = {
[e15853c]362                { BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::uuFloat80, BasicType::uuFloat128, BasicType::uFloat16, BasicType::uFloat32, BasicType::uFloat32x, BasicType::uFloat64, BasicType::uFloat64x, BasicType::uFloat128, BasicType::uFloat128x },
[3c67255]363                { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::NUMBER_OF_BASIC_TYPES, BasicType::NUMBER_OF_BASIC_TYPES, BasicType::uFloat16Complex, BasicType::uFloat32Complex, BasicType::uFloat32xComplex, BasicType::uFloat64Complex, BasicType::uFloat64xComplex, BasicType::uFloat128Complex, BasicType::uFloat128xComplex },
[7bf7fb9]364        };
[59c24b6]365
[ba01b14]366        // floating-point constant has minimum of 2 characters 1. or .1
[7bf7fb9]367        size_t last = str.length() - 1;
[d56e5bc]368        double v;
[ba01b14]369        int type;                                                                                       // 0 => float, 1 => double, 3 => long double, ...
370        bool complx = false;                                                            // real, complex
371        bool explnth = false;                                                           // explicit literal length
[d56e5bc]372
373        sscanf( str.c_str(), "%lg", &v );
[51b7345]374
[7bf7fb9]375        if ( checkI( str[last] ) ) {                                            // imaginary ?
376                complx = true;
377                last -= 1;                                                                              // backup one character
378        } // if
[0caaa6a]379
[7bf7fb9]380        if ( checkF( str[last] ) ) {                                            // float ?
[ba01b14]381                type = 0;
[7bf7fb9]382        } else if ( checkD( str[last] ) ) {                                     // double ?
[ba01b14]383                type = 1;
[7bf7fb9]384        } else if ( checkL( str[last] ) ) {                                     // long double ?
[ba01b14]385                type = 2;
386        } else if ( checkF80( str[last] ) ) {                           // __float80 ?
387                type = 3;
388        } else if ( checkF128( str[last] ) ) {                          // __float128 ?
389                type = 4;
[201aeb9]390        } else {
[ba01b14]391                type = 1;                                                                               // double (default if no suffix)
392                checkFnxFloat( str, last, explnth, type );
[7bf7fb9]393        } // if
[ba01b14]394
[7bf7fb9]395        if ( ! complx && checkI( str[last - 1] ) ) {            // imaginary ?
396                complx = true;
397        } // if
[51b7345]398
[ba01b14]399        assert( 0 <= type && type < 12 );
400        Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][type] ), str, v ) );
401        if ( explnth ) {                                                                        // explicit length ?
402                ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[complx][type] ), false );
[201aeb9]403        } // if
[76c62b2]404
[ab57786]405        delete &str;                                                                            // created by lex
406        return ret;
[7bf7fb9]407} // build_constantFloat
[51b7345]408
[76c62b2]409static void sepString( string & str, string & units, char delimit ) {
410        string::size_type posn = str.find_last_of( delimit ) + 1;
411        if ( posn != str.length() ) {
[e8ccca3]412                units = "?" + str.substr( posn );                               // extract units
[76c62b2]413                str.erase( posn );                                                              // remove units
414        } // if
415} // sepString
416
[f43df73]417Expression * build_constantChar( string & str ) {
[76c62b2]418        string units;                                                                           // units
419        sepString( str, units, '\'' );                                          // separate constant from units
420
[e8ccca3]421        Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) );
422        if ( units.length() != 0 ) {
[ba2a68b]423                ret = new UntypedExpr( new NameExpr( units ), { ret } );
[e8ccca3]424        } // if
[76c62b2]425
[ab57786]426        delete &str;                                                                            // created by lex
427        return ret;
[7bf7fb9]428} // build_constantChar
[51b7345]429
[e612146c]430Expression * build_constantStr( string & str ) {
[6e3eaa57]431        assert( str.length() > 0 );
[76c62b2]432        string units;                                                                           // units
433        sepString( str, units, '"' );                                           // separate constant from units
434
[513e165]435        Type * strtype;
436        switch ( str[0] ) {                                                                     // str has >= 2 characters, i.e, null string "" => safe to look at subscripts 0/1
[e612146c]437          case 'u':
[513e165]438                if ( str[1] == '8' ) goto Default;                              // utf-8 characters => array of char
439                // lookup type of associated typedef
[b81fd95]440                strtype = new TypeInstType( Type::Qualifiers( ), "char16_t", false );
[e612146c]441                break;
442          case 'U':
[b81fd95]443                strtype = new TypeInstType( Type::Qualifiers( ), "char32_t", false );
[e612146c]444                break;
445          case 'L':
[b81fd95]446                strtype = new TypeInstType( Type::Qualifiers( ), "wchar_t", false );
[e612146c]447                break;
[513e165]448          Default:                                                                                      // char default string type
449          default:
[b81fd95]450                strtype = new BasicType( Type::Qualifiers( ), BasicType::Char );
[e612146c]451        } // switch
[c36298d]452        ArrayType * at = new ArrayType( noQualifiers, strtype,
453                                                                        new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"'
454                                                                        false, false );
455        Expression * ret = new ConstantExpr( Constant( at, str, std::nullopt ) );
[e612146c]456        if ( units.length() != 0 ) {
457                ret = new UntypedExpr( new NameExpr( units ), { ret } );
458        } // if
[5809461]459
[ab57786]460        delete &str;                                                                            // created by lex
461        return ret;
[7bf7fb9]462} // build_constantStr
[51b7345]463
[930f69e]464Expression * build_field_name_FLOATING_FRACTIONconstant( const string & str ) {
[a16764a6]465        if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) SemanticError( yylloc, "invalid tuple index " + str );
[930f69e]466        Expression * ret = build_constantInteger( *new string( str.substr(1) ) );
467        delete &str;
468        return ret;
469} // build_field_name_FLOATING_FRACTIONconstant
470
471Expression * build_field_name_FLOATING_DECIMALconstant( const string & str ) {
[3c67255]472        if ( str[str.size() - 1] != '.' ) SemanticError( yylloc, "invalid tuple index " + str );
[930f69e]473        Expression * ret = build_constantInteger( *new string( str.substr( 0, str.size()-1 ) ) );
474        delete &str;
475        return ret;
476} // build_field_name_FLOATING_DECIMALconstant
477
[f43df73]478Expression * build_field_name_FLOATINGconstant( const string & str ) {
[8780e30]479        // str is of the form A.B -> separate at the . and return member expression
480        int a, b;
481        char dot;
[f43df73]482        stringstream ss( str );
[8780e30]483        ss >> a >> dot >> b;
[d56e5bc]484        UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) );
[8780e30]485        delete &str;
486        return ret;
487} // build_field_name_FLOATINGconstant
488
489Expression * make_field_name_fraction_constants( Expression * fieldName, Expression * fracts ) {
490        if ( fracts ) {
491                if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( fracts ) ) {
492                        memberExpr->set_member( make_field_name_fraction_constants( fieldName, memberExpr->get_aggregate() ) );
493                        return memberExpr;
494                } else {
495                        return new UntypedMemberExpr( fracts, fieldName );
[f43df73]496                } // if
497        } // if
[8780e30]498        return fieldName;
499} // make_field_name_fraction_constants
500
501Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts ) {
502        return make_field_name_fraction_constants( fieldName, maybeMoveBuild< Expression >( fracts ) );
503} // build_field_name_fraction_constants
504
[a2e0687]505NameExpr * build_varref( const string * name ) {
[bf4b4cf]506        NameExpr * expr = new NameExpr( *name );
[7ecbb7e]507        delete name;
508        return expr;
[a2e0687]509} // build_varref
[51b1202]510
[5809461]511// TODO: get rid of this and OperKinds and reuse code from OperatorTable
[a2e0687]512static const char * OperName[] = {                                              // must harmonize with OperKinds
[5721a6d]513        // diadic
[e5f2a67]514        "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&",
[5721a6d]515        "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
[e5f2a67]516        "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
[d9e2280]517        "?[?]", "...",
[5721a6d]518        // monadic
[5809461]519        "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--",
[a2e0687]520}; // OperName
[5721a6d]521
[a2e0687]522Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
523        Type * targetType = maybeMoveBuildType( decl_node );
[d1625f8]524        if ( dynamic_cast< VoidType * >( targetType ) ) {
[064e3ff]525                delete targetType;
[c0bf94e]526                return new CastExpr( maybeMoveBuild< Expression >(expr_node), false );
[064e3ff]527        } else {
[c0bf94e]528                return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType, false );
[064e3ff]529        } // if
[a2e0687]530} // build_cast
[a5f0529]531
[312029a]532Expression * build_keyword_cast( AggregateDecl::Aggregate target, ExpressionNode * expr_node ) {
[9a705dc8]533        return new KeywordCastExpr( maybeMoveBuild< Expression >(expr_node), target );
534}
535
[a2e0687]536Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
[513e165]537        return new VirtualCastExpr( maybeMoveBuild< Expression >( expr_node ), maybeMoveBuildType( decl_node ) );
[a2e0687]538} // build_virtual_cast
[064e3ff]539
[a2e0687]540Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) {
[513e165]541        return new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) );
[a2e0687]542} // build_fieldSel
[064e3ff]543
[a2e0687]544Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) {
545        UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) );
[64ac636]546        deref->location = expr_node->location;
[7ecbb7e]547        deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) );
[a2e0687]548        UntypedMemberExpr * ret = new UntypedMemberExpr( member, deref );
[064e3ff]549        return ret;
[a2e0687]550} // build_pfieldSel
[064e3ff]551
[a2e0687]552Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) {
[a7c90d4]553        Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() );
[ac71a86]554        delete member;
555        return ret;
[a2e0687]556} // build_offsetOf
[064e3ff]557
[a2e0687]558Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ) {
[7ecbb7e]559        return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind );
[a2e0687]560} // build_and_or
[51e076e]561
[a2e0687]562Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) {
[f43df73]563        list< Expression * > args;
[7ecbb7e]564        args.push_back( maybeMoveBuild< Expression >(expr_node) );
[d9e2280]565        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
[a2e0687]566} // build_unary_val
567
568Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node ) {
[f43df73]569        list< Expression * > args;
[cccc534]570        args.push_back(  maybeMoveBuild< Expression >(expr_node) ); // xxx -- this is exactly the same as the val case now, refactor this code.
[d9e2280]571        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
[a2e0687]572} // build_unary_ptr
573
574Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
[f43df73]575        list< Expression * > args;
[7ecbb7e]576        args.push_back( maybeMoveBuild< Expression >(expr_node1) );
577        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
[d9e2280]578        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
[a2e0687]579} // build_binary_val
580
581Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
[f43df73]582        list< Expression * > args;
[cda7889]583        args.push_back( maybeMoveBuild< Expression >(expr_node1) );
[7ecbb7e]584        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
[d9e2280]585        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
[a2e0687]586} // build_binary_ptr
[51e076e]587
[a2e0687]588Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) {
[7ecbb7e]589        return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) );
[a2e0687]590} // build_cond
[51e076e]591
[a2e0687]592Expression * build_tuple( ExpressionNode * expr_node ) {
[f43df73]593        list< Expression * > exprs;
[907eccb]594        buildMoveList( expr_node, exprs );
595        return new UntypedTupleExpr( exprs );;
[a2e0687]596} // build_tuple
[9706554]597
[a2e0687]598Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
[f43df73]599        list< Expression * > args;
[7ecbb7e]600        buildMoveList( expr_node, args );
[bf4b4cf]601        return new UntypedExpr( maybeMoveBuild< Expression >(function), args );
[a2e0687]602} // build_func
[9706554]603
[a2e0687]604Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids ) {
[7880579]605        Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type
[630a82a]606        if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type
[7ecbb7e]607                return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild< Initializer >(kids) );
[630a82a]608        // these types do not have associated type information
609        } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl )  ) {
[fbcde64]610                if ( newDeclStructDecl->has_body() ) {
611                        return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl ), maybeMoveBuild< Initializer >(kids) );
612                } else {
613                        return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
614                } // if
[630a82a]615        } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl )  ) {
[fbcde64]616                if ( newDeclUnionDecl->has_body() ) {
617                        return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl ), maybeMoveBuild< Initializer >(kids) );
618                } else {
619                        return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
620                } // if
[630a82a]621        } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl )  ) {
[fbcde64]622                if ( newDeclEnumDecl->has_body() ) {
623                        return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl ), maybeMoveBuild< Initializer >(kids) );
624                } else {
625                        return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
626                } // if
[630a82a]627        } else {
628                assert( false );
629        } // if
[a2e0687]630} // build_compoundLiteral
[630a82a]631
[b87a5ed]632// Local Variables: //
633// tab-width: 4 //
634// mode: c++ //
635// compile-command: "make install" //
636// End: //
Note: See TracBrowser for help on using the repository browser.