source: src/Parser/ExpressionNode.cc @ 119e6c8

ADTast-experimental
Last change on this file since 119e6c8 was 52a2248, checked in by Andrew Beach <ajbeach@…>, 18 months ago

This should get some of the Parser changes working on older compilers.

  • Property mode set to 100644
File size: 27.5 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
[0d0931d]11// Last Modified By : Andrew Beach
12// Last Modified On : Tue Mar 14 12:00:00 2023
13// Update Count     : 1082
[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
[702e826]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;
[0d0931d]124                if ( i == last - 1 || (str[i] != '0' && str[i] != '1') ) break;
[f6582252]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
[f6582252]159        // 'u' can appear before or after length suffix
160        if ( str.find_last_of( "uU" ) != string::npos ) Unsigned = true;
161
162        if ( isdigit( str[str.length() - 1] ) ) {                       // no suffix ?
[f56c32e]163                lnthSuffix( str, type, ltype );                                 // could have length suffix
164        } else {
[15596d7]165                // At least one digit in integer constant, so safe to backup while looking for suffix.
[52a2248]166                // This declaration and the comma expressions in the conditions mimic
167                // the declare and check pattern allowed in later compiler versions.
168                // (Only some early compilers/C++ standards do not support it.)
[15596d7]169                string::size_type posn;
170                // pointer value
[52a2248]171                if ( posn = str.find_last_of( "pP" ), posn != string::npos ) {
[15596d7]172                        ltype = 5; str.erase( posn, 1 );
[0d0931d]173                // size_t
[52a2248]174                } else if ( posn = str.find_last_of( "zZ" ), posn != string::npos ) {
[15596d7]175                        Unsigned = true; type = 2; ltype = 4; str.erase( posn, 1 );
[0d0931d]176                // signed char
[52a2248]177                } else if ( posn = str.rfind( "hh" ), posn != string::npos ) {
[15596d7]178                        type = 1; str.erase( posn, 2 );
[0d0931d]179                // signed char
[52a2248]180                } else if ( posn = str.rfind( "HH" ), posn != string::npos ) {
[15596d7]181                        type = 1; str.erase( posn, 2 );
[0d0931d]182                // short
[52a2248]183                } else if ( posn = str.find_last_of( "hH" ), posn != string::npos ) {
[15596d7]184                        type = 0; str.erase( posn, 1 );
[0d0931d]185                // int (natural number)
[52a2248]186                } else if ( posn = str.find_last_of( "nN" ), posn != string::npos ) {
[15596d7]187                        type = 2; str.erase( posn, 1 );
188                } else if ( str.rfind( "ll" ) != string::npos || str.rfind( "LL" ) != string::npos ) {
189                        type = 4;
190                } else {
191                        lnthSuffix( str, type, ltype );
192                } // if
193        } // if
[7bf7fb9]194
[f6582252]195        // Cannot be just "0"/"1"; sscanf stops at the suffix, if any; value goes over the wall => always generate
[dbe8e31c]196
[cf5af9c]197#if ! defined(__SIZEOF_INT128__)
[dbe8e31c]198        if ( type == 5 ) SemanticError( yylloc, "int128 constant is not supported on this target " + str );
[cf5af9c]199#endif // ! __SIZEOF_INT128__
[702e826]200
[f6582252]201        if ( str[0] == '0' ) {                                                          // radix character ?
202                dec = false;
203                if ( checkX( str[1] ) ) {                                               // hex constant ?
204                        if ( type < 5 ) {                                                       // not L128 ?
205                                sscanf( (char *)str.c_str(), "%llx", &v );
[c5b55c4]206#if defined(__SIZEOF_INT128__)
[f6582252]207                        } else {                                                                        // hex int128 constant
208                                unsigned int len = str.length();
209                                if ( len > (2 + 16 + 16) ) SemanticError( yylloc, "128-bit hexadecimal constant to large " + str );
[702e826]210                                // hex digits < 2^64
211                                if ( len > (2 + 16) ) {
212                                        str2 = "0x" + str.substr( len - 16 );
213                                        sscanf( (char *)str2.c_str(), "%llx", &v2 );
214                                        str = str.substr( 0, len - 16 );
[15596d7]215                                } // if
[f6582252]216                                sscanf( (char *)str.c_str(), "%llx", &v );
[c5b55c4]217#endif // __SIZEOF_INT128__
[f6582252]218                        } // if
219                        //printf( "%llx %llu\n", v, v );
220                } else if ( checkB( str[1] ) ) {                                // binary constant ?
[c5b55c4]221#if defined(__SIZEOF_INT128__)
[013b028]222                        unsigned int len = str.length();
[f6582252]223                        if ( type == 5 && len > 2 + 64 ) {
224                                if ( len > 2 + 64 + 64 ) SemanticError( yylloc, "128-bit binary constant to large " + str );
225                                str2 = "0b" + str.substr( len - 64 );
226                                str = str.substr( 0, len - 64 );
227                                scanbin( str2, v2 );
228                        } // if
[c5b55c4]229#endif // __SIZEOF_INT128__
[f6582252]230                        scanbin( str, v );
231                        //printf( "%#llx %llu\n", v, v );
232                } else {                                                                                // octal constant
233                        if ( type < 5 ) {                                                       // not L128 ?
234                                sscanf( (char *)str.c_str(), "%llo", &v );
[cf5af9c]235#if defined(__SIZEOF_INT128__)
[f6582252]236                        } else {                                                                        // octal int128 constant
237                                unsigned int len = str.length();
238                                if ( len > 1 + 43 || (len == 1 + 43 && str[0] > '3') ) SemanticError( yylloc, "128-bit octal constant to large " + str );
[c5b55c4]239                                char buf[32];
[f6582252]240                                if ( len <= 1 + 21 ) {                                  // value < 21 octal digitis
[c5b55c4]241                                        sscanf( (char *)str.c_str(), "%llo", &v );
[f6582252]242                                } else {
243                                        sscanf( &str[len - 21], "%llo", &v );
[791028a]244                                        __int128 val = v;                                       // accumulate bits
[f6582252]245                                        str[len - 21] ='\0';                            // shorten string
246                                        sscanf( &str[len == 43 ? 1 : 0], "%llo", &v );
247                                        val |= (__int128)v << 63;                       // store bits
248                                        if ( len == 1 + 43 ) {                          // most significant 2 bits ?
249                                                str[2] = '\0';                                  // shorten string
250                                                sscanf( &str[1], "%llo", &v );  // process most significant 2 bits
251                                                val |= (__int128)v << 126;              // store bits
252                                        } // if
253                                        v = val >> 64; v2 = (uint64_t)val;      // replace octal constant with 2 hex constants
254                                        sprintf( buf, "%#llx", v2 );
255                                        str2 = buf;
256                                } // if
[c5b55c4]257                                sprintf( buf, "%#llx", v );
258                                str = buf;
[cf5af9c]259#endif // __SIZEOF_INT128__
[f6582252]260                        } // if
261                        //printf( "%#llo %llu\n", v, v );
262                } // if
263        } else {                                                                                        // decimal constant ?
264                if ( type < 5 ) {                                                               // not L128 ?
265                        sscanf( (char *)str.c_str(), "%llu", &v );
[cf5af9c]266#if defined(__SIZEOF_INT128__)
[f6582252]267                } else {                                                                                // decimal int128 constant
268                        #define P10_UINT64 10'000'000'000'000'000'000ULL // 19 zeroes
269                        unsigned int len = str.length();
270                        if ( str.length() == 39 && str > (Unsigned ? "340282366920938463463374607431768211455" : "170141183460469231731687303715884105727") )
271                                SemanticError( yylloc, "128-bit decimal constant to large " + str );
[c5b55c4]272                        char buf[32];
[f6582252]273                        if ( len <= 19 ) {                                                      // value < 19 decimal digitis
[c5b55c4]274                                sscanf( (char *)str.c_str(), "%llu", &v );
[f6582252]275                        } else {
276                                sscanf( &str[len - 19], "%llu", &v );
[791028a]277                                __int128 val = v;                                               // accumulate bits
[f6582252]278                                str[len - 19] ='\0';                                    // shorten string
279                                sscanf( &str[len == 39 ? 1 : 0], "%llu", &v );
280                                val += (__int128)v * (__int128)P10_UINT64; // store bits
281                                if ( len == 39 ) {                                              // most significant 2 bits ?
282                                        str[1] = '\0';                                          // shorten string
283                                        sscanf( &str[0], "%llu", &v );          // process most significant 2 bits
284                                        val += (__int128)v * (__int128)P10_UINT64 * (__int128)P10_UINT64; // store bits
285                                } // if
286                                v = val >> 64; v2 = (uint64_t)val;              // replace decimal constant with 2 hex constants
287                                sprintf( buf, "%#llx", v2 );
288                                str2 = buf;
289                        } // if
[c5b55c4]290                        sprintf( buf, "%#llx", v );
291                        str = buf;
[cf5af9c]292#endif // __SIZEOF_INT128__
[f6582252]293                } // if
294                //printf( "%llu\n", v );
295        } // if
296
297        if ( type == -1 ) {                                                                     // no suffix => determine type from value size
298                valueToType( v, dec, type, Unsigned );
299        } // if
300        /* printf( "%s %llo %s %llo\n", str.c_str(), v, str2.c_str(), v2 ); */
301
[0a616e0]302        //if ( !( 0 <= type && type <= 6 ) ) { printf( "%s %lu %d %s\n", fred.c_str(), fred.length(), type, str.c_str() ); }
303        assert( 0 <= type && type <= 6 );
[f56c32e]304
[a6c5d7c]305        // Constant type is correct for overload resolving.
[ba01b14]306        ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][type] ), str, v ) );
307        if ( Unsigned && type < 2 ) {                                           // hh or h, less than int ?
[201aeb9]308                // int i = -1uh => 65535 not -1, so cast is necessary for unsigned, which unfortunately eliminates warnings for large values.
[ba01b14]309                ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][type] ), false );
310        } else if ( ltype != -1 ) {                                                     // explicit length ?
[0a616e0]311                if ( ltype == 6 ) {                                                             // int128, (int128)constant
[f6582252]312//                      ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][type] ), false );
313                        ret2 = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::LongLongSignedInt ), str2, v2 ) );
[702e826]314                        ret = build_compoundLiteral(
315                                DeclarationNode::newBasicType( DeclarationNode::Int128 )->addType( DeclarationNode::newSignedNess( DeclarationNode::Unsigned ) ),
316                                new InitializerNode( (InitializerNode *)(new InitializerNode( new ExpressionNode( v2 == 0 ? ret2 : ret ) ))->set_last( new InitializerNode( new ExpressionNode( v2 == 0 ? ret : ret2 ) ) ), true ) );
[0a616e0]317                } else {                                                                                // explicit length, (length_type)constant
[ba01b14]318                        ret = new CastExpr( ret, new TypeInstType( Type::Qualifiers(), lnthsInt[Unsigned][ltype], false ), false );
[3d8d7a7]319                        if ( ltype == 5 ) {                                                     // pointer, intptr( (uintptr_t)constant )
320                                ret = build_func( new ExpressionNode( build_varref( new string( "intptr" ) ) ), new ExpressionNode( ret ) );
[0a616e0]321                        } // if
[201aeb9]322                } // if
[7b1d5ec]323        } // if
[e8ccca3]324
[f56c32e]325  CLEANUP: ;
[ab57786]326        delete &str;                                                                            // created by lex
327        return ret;
[7bf7fb9]328} // build_constantInteger
[51b7345]329
[201aeb9]330
[ba01b14]331static inline void checkFnxFloat( string & str, size_t last, bool & explnth, int & type ) {
332        string::size_type posn;
333        // floating-point constant has minimum of 2 characters, 1. or .1, so safe to look ahead
334        if ( str[1] == 'x' ) {                                                          // hex ?
335                posn = str.find_last_of( "pP" );                                // back for exponent (must have)
336                posn = str.find_first_of( "fF", posn + 1 );             // forward for size (fF allowed in hex constant)
337        } else {
338                posn = str.find_last_of( "fF" );                                // back for size (fF not allowed)
339        } // if
[201aeb9]340  if ( posn == string::npos ) return;
[ba01b14]341        explnth = true;
[201aeb9]342        posn += 1;                                                                                      // advance to size
343        if ( str[posn] == '3' ) {                                                       // 32
[ba01b14]344                if ( str[last] != 'x' ) type = 6;
345                else type = 7;
[201aeb9]346        } else if ( str[posn] == '6' ) {                                        // 64
[ba01b14]347                if ( str[last] != 'x' ) type = 8;
348                else type = 9;
349        } else if ( str[posn] == '8' ) {                                        // 80
350                type = 3;
351        } else if ( str[posn] == '1' ) {                                        // 16/128
352                if ( str[posn + 1] == '6' ) {                                   // 16
353                        type = 5;
354                } else {                                                                                // 128
355                        if ( str[last] != 'x' ) type = 10;
356                        else type = 11;
357                } // if
[201aeb9]358        } else {
359                assertf( false, "internal error, bad floating point length %s", str.c_str() );
360        } // if
[ba01b14]361} // checkFnxFloat
[201aeb9]362
363
[f43df73]364Expression * build_constantFloat( string & str ) {
[ba01b14]365        static const BasicType::Kind kind[2][12] = {
[e15853c]366                { 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]367                { 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]368        };
[59c24b6]369
[ba01b14]370        // floating-point constant has minimum of 2 characters 1. or .1
[7bf7fb9]371        size_t last = str.length() - 1;
[d56e5bc]372        double v;
[ba01b14]373        int type;                                                                                       // 0 => float, 1 => double, 3 => long double, ...
374        bool complx = false;                                                            // real, complex
375        bool explnth = false;                                                           // explicit literal length
[d56e5bc]376
377        sscanf( str.c_str(), "%lg", &v );
[51b7345]378
[7bf7fb9]379        if ( checkI( str[last] ) ) {                                            // imaginary ?
380                complx = true;
381                last -= 1;                                                                              // backup one character
382        } // if
[0caaa6a]383
[7bf7fb9]384        if ( checkF( str[last] ) ) {                                            // float ?
[ba01b14]385                type = 0;
[7bf7fb9]386        } else if ( checkD( str[last] ) ) {                                     // double ?
[ba01b14]387                type = 1;
[7bf7fb9]388        } else if ( checkL( str[last] ) ) {                                     // long double ?
[ba01b14]389                type = 2;
390        } else if ( checkF80( str[last] ) ) {                           // __float80 ?
391                type = 3;
392        } else if ( checkF128( str[last] ) ) {                          // __float128 ?
393                type = 4;
[201aeb9]394        } else {
[ba01b14]395                type = 1;                                                                               // double (default if no suffix)
396                checkFnxFloat( str, last, explnth, type );
[7bf7fb9]397        } // if
[ba01b14]398
[7bf7fb9]399        if ( ! complx && checkI( str[last - 1] ) ) {            // imaginary ?
400                complx = true;
401        } // if
[51b7345]402
[ba01b14]403        assert( 0 <= type && type < 12 );
404        Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][type] ), str, v ) );
405        if ( explnth ) {                                                                        // explicit length ?
406                ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[complx][type] ), false );
[201aeb9]407        } // if
[76c62b2]408
[ab57786]409        delete &str;                                                                            // created by lex
410        return ret;
[7bf7fb9]411} // build_constantFloat
[51b7345]412
[76c62b2]413static void sepString( string & str, string & units, char delimit ) {
414        string::size_type posn = str.find_last_of( delimit ) + 1;
415        if ( posn != str.length() ) {
[e8ccca3]416                units = "?" + str.substr( posn );                               // extract units
[76c62b2]417                str.erase( posn );                                                              // remove units
418        } // if
419} // sepString
420
[f43df73]421Expression * build_constantChar( string & str ) {
[76c62b2]422        string units;                                                                           // units
423        sepString( str, units, '\'' );                                          // separate constant from units
424
[e8ccca3]425        Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) );
426        if ( units.length() != 0 ) {
[ba2a68b]427                ret = new UntypedExpr( new NameExpr( units ), { ret } );
[e8ccca3]428        } // if
[76c62b2]429
[ab57786]430        delete &str;                                                                            // created by lex
431        return ret;
[7bf7fb9]432} // build_constantChar
[51b7345]433
[e612146c]434Expression * build_constantStr( string & str ) {
[6e3eaa57]435        assert( str.length() > 0 );
[76c62b2]436        string units;                                                                           // units
437        sepString( str, units, '"' );                                           // separate constant from units
438
[513e165]439        Type * strtype;
440        switch ( str[0] ) {                                                                     // str has >= 2 characters, i.e, null string "" => safe to look at subscripts 0/1
[0d0931d]441        case 'u':
[513e165]442                if ( str[1] == '8' ) goto Default;                              // utf-8 characters => array of char
443                // lookup type of associated typedef
[b81fd95]444                strtype = new TypeInstType( Type::Qualifiers( ), "char16_t", false );
[e612146c]445                break;
[0d0931d]446        case 'U':
[b81fd95]447                strtype = new TypeInstType( Type::Qualifiers( ), "char32_t", false );
[e612146c]448                break;
[0d0931d]449        case 'L':
[b81fd95]450                strtype = new TypeInstType( Type::Qualifiers( ), "wchar_t", false );
[e612146c]451                break;
[0d0931d]452        Default:                                                                                        // char default string type
453        default:
[b81fd95]454                strtype = new BasicType( Type::Qualifiers( ), BasicType::Char );
[e612146c]455        } // switch
[c36298d]456        ArrayType * at = new ArrayType( noQualifiers, strtype,
457                                                                        new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"'
458                                                                        false, false );
459        Expression * ret = new ConstantExpr( Constant( at, str, std::nullopt ) );
[e612146c]460        if ( units.length() != 0 ) {
461                ret = new UntypedExpr( new NameExpr( units ), { ret } );
462        } // if
[5809461]463
[ab57786]464        delete &str;                                                                            // created by lex
465        return ret;
[7bf7fb9]466} // build_constantStr
[51b7345]467
[930f69e]468Expression * build_field_name_FLOATING_FRACTIONconstant( const string & str ) {
[a16764a6]469        if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) SemanticError( yylloc, "invalid tuple index " + str );
[930f69e]470        Expression * ret = build_constantInteger( *new string( str.substr(1) ) );
471        delete &str;
472        return ret;
473} // build_field_name_FLOATING_FRACTIONconstant
474
475Expression * build_field_name_FLOATING_DECIMALconstant( const string & str ) {
[3c67255]476        if ( str[str.size() - 1] != '.' ) SemanticError( yylloc, "invalid tuple index " + str );
[930f69e]477        Expression * ret = build_constantInteger( *new string( str.substr( 0, str.size()-1 ) ) );
478        delete &str;
479        return ret;
480} // build_field_name_FLOATING_DECIMALconstant
481
[f43df73]482Expression * build_field_name_FLOATINGconstant( const string & str ) {
[8780e30]483        // str is of the form A.B -> separate at the . and return member expression
484        int a, b;
485        char dot;
[f43df73]486        stringstream ss( str );
[8780e30]487        ss >> a >> dot >> b;
[d56e5bc]488        UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) );
[8780e30]489        delete &str;
490        return ret;
491} // build_field_name_FLOATINGconstant
492
493Expression * make_field_name_fraction_constants( Expression * fieldName, Expression * fracts ) {
494        if ( fracts ) {
495                if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( fracts ) ) {
496                        memberExpr->set_member( make_field_name_fraction_constants( fieldName, memberExpr->get_aggregate() ) );
497                        return memberExpr;
498                } else {
499                        return new UntypedMemberExpr( fracts, fieldName );
[f43df73]500                } // if
501        } // if
[8780e30]502        return fieldName;
503} // make_field_name_fraction_constants
504
505Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts ) {
[702e826]506        return make_field_name_fraction_constants( fieldName, maybeMoveBuild( fracts ) );
[8780e30]507} // build_field_name_fraction_constants
508
[a2e0687]509NameExpr * build_varref( const string * name ) {
[bf4b4cf]510        NameExpr * expr = new NameExpr( *name );
[7ecbb7e]511        delete name;
512        return expr;
[a2e0687]513} // build_varref
[51b1202]514
[4e2befe3]515QualifiedNameExpr * build_qualified_expr( const DeclarationNode * decl_node, const NameExpr * name ) {
[702e826]516        Declaration * newDecl = maybeBuild(decl_node);
[b0d9ff7]517        if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) {
518                const Type * t = newDeclWithType->get_type();
519                if ( t ) {
520                        if ( const TypeInstType * typeInst = dynamic_cast<const TypeInstType *>( t ) ) {
521                                newDecl= new EnumDecl( typeInst->name );
522                        }
523                }
524        }
[e874605]525        return new QualifiedNameExpr( newDecl, name->name );
[b0d9ff7]526}
527
528QualifiedNameExpr * build_qualified_expr( const EnumDecl * decl_node, const NameExpr * name ) {
529        EnumDecl * newDecl = const_cast< EnumDecl * >( decl_node );
530        return new QualifiedNameExpr( newDecl, name->name );
[4e2befe3]531}
532
[6e50a6b]533DimensionExpr * build_dimensionref( const string * name ) {
534        DimensionExpr * expr = new DimensionExpr( *name );
535        delete name;
536        return expr;
537} // build_varref
[ea54f1e]538
[5809461]539// TODO: get rid of this and OperKinds and reuse code from OperatorTable
[a2e0687]540static const char * OperName[] = {                                              // must harmonize with OperKinds
[5721a6d]541        // diadic
[e5f2a67]542        "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&",
[5721a6d]543        "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
[e5f2a67]544        "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
[d9e2280]545        "?[?]", "...",
[5721a6d]546        // monadic
[5809461]547        "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--",
[a2e0687]548}; // OperName
[5721a6d]549
[a2e0687]550Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
551        Type * targetType = maybeMoveBuildType( decl_node );
[d1625f8]552        if ( dynamic_cast< VoidType * >( targetType ) ) {
[064e3ff]553                delete targetType;
[702e826]554                return new CastExpr( maybeMoveBuild( expr_node ), false );
[064e3ff]555        } else {
[702e826]556                return new CastExpr( maybeMoveBuild( expr_node ), targetType, false );
[064e3ff]557        } // if
[a2e0687]558} // build_cast
[a5f0529]559
[312029a]560Expression * build_keyword_cast( AggregateDecl::Aggregate target, ExpressionNode * expr_node ) {
[702e826]561        return new KeywordCastExpr( maybeMoveBuild( expr_node ), target );
[9a705dc8]562}
563
[a2e0687]564Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
[702e826]565        return new VirtualCastExpr( maybeMoveBuild( expr_node ), maybeMoveBuildType( decl_node ) );
[a2e0687]566} // build_virtual_cast
[064e3ff]567
[a2e0687]568Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) {
[702e826]569        return new UntypedMemberExpr( member, maybeMoveBuild( expr_node ) );
[a2e0687]570} // build_fieldSel
[064e3ff]571
[a2e0687]572Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) {
573        UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) );
[64ac636]574        deref->location = expr_node->location;
[702e826]575        deref->get_args().push_back( maybeMoveBuild( expr_node ) );
[a2e0687]576        UntypedMemberExpr * ret = new UntypedMemberExpr( member, deref );
[064e3ff]577        return ret;
[a2e0687]578} // build_pfieldSel
[064e3ff]579
[a2e0687]580Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) {
[a7c90d4]581        Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() );
[ac71a86]582        delete member;
583        return ret;
[a2e0687]584} // build_offsetOf
[064e3ff]585
[a2e0687]586Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ) {
[702e826]587        return new LogicalExpr( notZeroExpr( maybeMoveBuild( expr_node1 ) ), notZeroExpr( maybeMoveBuild( expr_node2 ) ), kind );
[a2e0687]588} // build_and_or
[51e076e]589
[a2e0687]590Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) {
[f43df73]591        list< Expression * > args;
[702e826]592        args.push_back( maybeMoveBuild( expr_node ) );
[d9e2280]593        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
[a2e0687]594} // build_unary_val
595
596Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
[f43df73]597        list< Expression * > args;
[702e826]598        args.push_back( maybeMoveBuild( expr_node1 ) );
599        args.push_back( maybeMoveBuild( expr_node2 ) );
[d9e2280]600        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
[a2e0687]601} // build_binary_val
602
603Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
[f43df73]604        list< Expression * > args;
[702e826]605        args.push_back( maybeMoveBuild( expr_node1 ) );
606        args.push_back( maybeMoveBuild( expr_node2 ) );
[d9e2280]607        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
[a2e0687]608} // build_binary_ptr
[51e076e]609
[a2e0687]610Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) {
[702e826]611        return new ConditionalExpr( notZeroExpr( maybeMoveBuild( expr_node1 ) ), maybeMoveBuild( expr_node2 ), maybeMoveBuild( expr_node3 ) );
[a2e0687]612} // build_cond
[51e076e]613
[a2e0687]614Expression * build_tuple( ExpressionNode * expr_node ) {
[f43df73]615        list< Expression * > exprs;
[907eccb]616        buildMoveList( expr_node, exprs );
617        return new UntypedTupleExpr( exprs );;
[a2e0687]618} // build_tuple
[9706554]619
[a2e0687]620Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
[f43df73]621        list< Expression * > args;
[7ecbb7e]622        buildMoveList( expr_node, args );
[702e826]623        return new UntypedExpr( maybeMoveBuild( function ), args );
[a2e0687]624} // build_func
[9706554]625
[a2e0687]626Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids ) {
[702e826]627        Declaration * newDecl = maybeBuild( decl_node ); // compound literal type
[630a82a]628        if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type
[702e826]629                return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild( kids ) );
[630a82a]630        // these types do not have associated type information
631        } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl )  ) {
[fbcde64]632                if ( newDeclStructDecl->has_body() ) {
[702e826]633                        return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl ), maybeMoveBuild( kids ) );
[fbcde64]634                } else {
[702e826]635                        return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild( kids ) );
[fbcde64]636                } // if
[630a82a]637        } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl )  ) {
[fbcde64]638                if ( newDeclUnionDecl->has_body() ) {
[702e826]639                        return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl ), maybeMoveBuild( kids ) );
[fbcde64]640                } else {
[702e826]641                        return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild( kids ) );
[fbcde64]642                } // if
[630a82a]643        } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl )  ) {
[fbcde64]644                if ( newDeclEnumDecl->has_body() ) {
[702e826]645                        return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl ), maybeMoveBuild( kids ) );
[fbcde64]646                } else {
[702e826]647                        return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild( kids ) );
[fbcde64]648                } // if
[630a82a]649        } else {
650                assert( false );
651        } // if
[a2e0687]652} // build_compoundLiteral
[630a82a]653
[b87a5ed]654// Local Variables: //
655// tab-width: 4 //
656// End: //
Note: See TracBrowser for help on using the repository browser.