| 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.cpp -- | 
|---|
| 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 : Tue Apr  1 08:13:17 2025 | 
|---|
| 13 | // Update Count     : 1122 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include "ExpressionNode.hpp" | 
|---|
| 17 |  | 
|---|
| 18 | #include <cassert>                 // for assert | 
|---|
| 19 | #include <stdio.h>                 // for sscanf, size_t | 
|---|
| 20 | #include <climits>                 // for LLONG_MAX, LONG_MAX, INT_MAX, UINT... | 
|---|
| 21 | #include <list>                    // for list | 
|---|
| 22 | #include <sstream>                 // for basic_istream::operator>>, basic_i... | 
|---|
| 23 | #include <string>                  // for string, operator+, operator== | 
|---|
| 24 |  | 
|---|
| 25 | #include "AST/BasicKind.hpp"       // for BasicKind | 
|---|
| 26 | #include "AST/Expr.hpp"            // for NameExpr | 
|---|
| 27 | #include "AST/Type.hpp"            // for Type, LengthFlag, DimentionFlag | 
|---|
| 28 | #include "Common/SemanticError.hpp"// for SemanticError | 
|---|
| 29 | #include "Common/Utility.hpp"      // for maybeMoveBuild, maybeBuild, CodeLo... | 
|---|
| 30 | #include "DeclarationNode.hpp"     // for DeclarationNode | 
|---|
| 31 | #include "InitializerNode.hpp"     // for InitializerNode | 
|---|
| 32 | #include "TypeData.hpp"            // for addType, build_basic_type, build_c... | 
|---|
| 33 | #include "ParserUtility.hpp"       // for notZeroExpr | 
|---|
| 34 |  | 
|---|
| 35 | using 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 |  | 
|---|
| 51 | // static inline bool checkH( char c ) { return c == 'h' || c == 'H'; } | 
|---|
| 52 | // static inline bool checkZ( char c ) { return c == 'z' || c == 'Z'; } | 
|---|
| 53 | // static inline bool checkU( char c ) { return c == 'u' || c == 'U'; } | 
|---|
| 54 | static inline bool checkF( char c ) { return c == 'f' || c == 'F'; } | 
|---|
| 55 | static inline bool checkD( char c ) { return c == 'd' || c == 'D'; } | 
|---|
| 56 | static inline bool checkF80( char c ) { return c == 'w' || c == 'W'; } | 
|---|
| 57 | static inline bool checkF128( char c ) { return c == 'q' || c == 'Q'; } | 
|---|
| 58 | static inline bool checkL( char c ) { return c == 'l' || c == 'L'; } | 
|---|
| 59 | static inline bool checkI( char c ) { return c == 'i' || c == 'I'; } | 
|---|
| 60 | static inline bool checkB( char c ) { return c == 'b' || c == 'B'; } | 
|---|
| 61 | static inline bool checkX( char c ) { return c == 'x' || c == 'X'; } | 
|---|
| 62 | // static inline bool checkN( char c ) { return c == 'n' || c == 'N'; } | 
|---|
| 63 |  | 
|---|
| 64 | void lnthSuffix( string & str, int & type, int & ltype ) { | 
|---|
| 65 | // 'u' can appear before or after length suffix | 
|---|
| 66 | string::size_type posn = str.find_last_of( "lL" ); | 
|---|
| 67 |  | 
|---|
| 68 | if ( posn == string::npos ) return;                                     // no suffix | 
|---|
| 69 | size_t end = str.length() - 1; | 
|---|
| 70 | if ( posn == end ) { type = 3; return; }                        // no length after 'l' => 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 |  | 
|---|
| 87 | char fix = '\0'; | 
|---|
| 88 | if ( str[end] == 'u' || str[end] == 'U' ) fix = str[end]; // ends with 'uU' ? | 
|---|
| 89 | str.erase( posn );                                                                      // remove length suffix and possibly uU | 
|---|
| 90 | if ( type == 5 ) {                                                                      // L128 does not need uU | 
|---|
| 91 | end = str.length() - 1; | 
|---|
| 92 | if ( str[end] == 'u' || str[end] == 'U' ) str.erase( end ); // ends with 'uU' ? remove | 
|---|
| 93 | } else if ( fix != '\0' ) str += fix;                           // put 'uU' back if removed | 
|---|
| 94 | } // lnthSuffix | 
|---|
| 95 |  | 
|---|
| 96 | void valueToType( unsigned long long int & v, bool dec, int & type, bool & Unsigned ) { | 
|---|
| 97 | // use value to determine type | 
|---|
| 98 | if ( v <= INT_MAX ) {                                                           // signed int | 
|---|
| 99 | type = 2; | 
|---|
| 100 | } else if ( v <= UINT_MAX && ! dec ) {                          // unsigned int | 
|---|
| 101 | type = 2; | 
|---|
| 102 | Unsigned = true;                                                                // unsigned | 
|---|
| 103 | } else if ( v <= LONG_MAX ) {                                           // signed long int | 
|---|
| 104 | type = 3; | 
|---|
| 105 | } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int | 
|---|
| 106 | type = 3; | 
|---|
| 107 | Unsigned = true;                                                                // unsigned long int | 
|---|
| 108 | } else if ( v <= LLONG_MAX ) {                                          // signed long long int | 
|---|
| 109 | type = 4; | 
|---|
| 110 | } else {                                                                                        // unsigned long long int | 
|---|
| 111 | type = 4; | 
|---|
| 112 | Unsigned = true;                                                                // unsigned long long int | 
|---|
| 113 | } // if | 
|---|
| 114 | } // valueToType | 
|---|
| 115 |  | 
|---|
| 116 | static void scanbin( string & str, unsigned long long int & v ) { | 
|---|
| 117 | v = 0; | 
|---|
| 118 | size_t last = str.length() - 1;                                         // last subscript of constant | 
|---|
| 119 | for ( unsigned int i = 2;; ) {                                          // ignore prefix | 
|---|
| 120 | if ( str[i] == '1' ) v |= 1; | 
|---|
| 121 | i += 1; | 
|---|
| 122 | if ( i == last - 1 || (str[i] != '0' && str[i] != '1') ) break; | 
|---|
| 123 | v <<= 1; | 
|---|
| 124 | } // for | 
|---|
| 125 | } // scanbin | 
|---|
| 126 |  | 
|---|
| 127 | ast::Expr * build_constantInteger( | 
|---|
| 128 | const CodeLocation & location, string & str ) { | 
|---|
| 129 | static const ast::BasicKind kind[2][6] = { | 
|---|
| 130 | // short (h) must be before char (hh) because shorter type has the longer suffix | 
|---|
| 131 | { ast::BasicKind::ShortSignedInt, ast::BasicKind::SignedChar, ast::BasicKind::SignedInt, ast::BasicKind::LongSignedInt, ast::BasicKind::LongLongSignedInt, /* BasicKind::SignedInt128 */ ast::BasicKind::LongLongSignedInt, }, | 
|---|
| 132 | { ast::BasicKind::ShortUnsignedInt, ast::BasicKind::UnsignedChar, ast::BasicKind::UnsignedInt, ast::BasicKind::LongUnsignedInt, ast::BasicKind::LongLongUnsignedInt, /* BasicKind::UnsignedInt128 */ ast::BasicKind::LongLongUnsignedInt, }, | 
|---|
| 133 | }; | 
|---|
| 134 |  | 
|---|
| 135 | static const char * lnthsInt[2][6] = { | 
|---|
| 136 | { "int16_t",  "int8_t",  "int32_t",  "int64_t",  "size_t",  "uintptr_t", }, | 
|---|
| 137 | { "uint16_t", "uint8_t", "uint32_t", "uint64_t", "size_t",  "uintptr_t", }, | 
|---|
| 138 | }; // lnthsInt | 
|---|
| 139 |  | 
|---|
| 140 | string str2( "0x0" ); | 
|---|
| 141 | unsigned long long int v, v2 = 0;                                       // converted integral value | 
|---|
| 142 | ast::Expr * ret, * ret2; | 
|---|
| 143 |  | 
|---|
| 144 | int type = -1;                                                                          // 0 => short, 1 => char, 2 => int, 3 => long int, 4 => long long int, 5 => int128 | 
|---|
| 145 | int ltype = -1;                                                                         // 0 => 16 bits, 1 => 8 bits, 2 => 32 bits, 3 => 64 bits, 4 => size_t, 5 => intptr, 6 => pointer | 
|---|
| 146 | bool dec = true, Unsigned = false;                                      // decimal, unsigned constant | 
|---|
| 147 |  | 
|---|
| 148 | // special constants | 
|---|
| 149 | if ( str == "0" ) { | 
|---|
| 150 | ret = new ast::ConstantExpr( location, new ast::ZeroType(), str, 0 ); | 
|---|
| 151 | goto CLEANUP; | 
|---|
| 152 | } // if | 
|---|
| 153 | if ( str == "1" ) { | 
|---|
| 154 | ret = new ast::ConstantExpr( location, new ast::OneType(), str, 1 ); | 
|---|
| 155 | goto CLEANUP; | 
|---|
| 156 | } // if | 
|---|
| 157 |  | 
|---|
| 158 | // 'u' can appear before or after length suffix | 
|---|
| 159 | if ( str.find_last_of( "uU" ) != string::npos ) Unsigned = true; | 
|---|
| 160 |  | 
|---|
| 161 | if ( isdigit( str[str.length() - 1] ) ) {                       // no suffix ? | 
|---|
| 162 | lnthSuffix( str, type, ltype );                                 // could have length suffix | 
|---|
| 163 | } else { | 
|---|
| 164 | // At least one digit in integer constant, so safe to backup while looking for suffix.  This | 
|---|
| 165 | // declaration and the comma expressions in the conditions mimic the declare and check | 
|---|
| 166 | // pattern allowed in later compiler versions.  (Only some early compilers/C++ standards do | 
|---|
| 167 | // not support it.) | 
|---|
| 168 | string::size_type posn; | 
|---|
| 169 | // pointer value | 
|---|
| 170 | if ( posn = str.find_last_of( "pP" ), posn != string::npos ) { | 
|---|
| 171 | ltype = 5; str.erase( posn, 1 ); | 
|---|
| 172 | // size_t | 
|---|
| 173 | } else if ( posn = str.find_last_of( "zZ" ), posn != string::npos ) { | 
|---|
| 174 | Unsigned = true; type = 2; ltype = 4; str.erase( posn, 1 ); | 
|---|
| 175 | // signed char | 
|---|
| 176 | } else if ( posn = str.rfind( "hh" ), posn != string::npos ) { | 
|---|
| 177 | type = 1; str.erase( posn, 2 ); | 
|---|
| 178 | // signed char | 
|---|
| 179 | } else if ( posn = str.rfind( "HH" ), posn != string::npos ) { | 
|---|
| 180 | type = 1; str.erase( posn, 2 ); | 
|---|
| 181 | // short | 
|---|
| 182 | } else if ( posn = str.find_last_of( "hH" ), posn != string::npos ) { | 
|---|
| 183 | type = 0; str.erase( posn, 1 ); | 
|---|
| 184 | // int (natural number) | 
|---|
| 185 | } else if ( posn = str.find_last_of( "nN" ), posn != string::npos ) { | 
|---|
| 186 | type = 2; str.erase( posn, 1 ); | 
|---|
| 187 | } else if ( str.rfind( "ll" ) != string::npos || str.rfind( "LL" ) != string::npos ) { | 
|---|
| 188 | type = 4; | 
|---|
| 189 | } else { | 
|---|
| 190 | lnthSuffix( str, type, ltype ); | 
|---|
| 191 | } // if | 
|---|
| 192 | } // if | 
|---|
| 193 |  | 
|---|
| 194 | // Cannot be just "0"/"1"; sscanf stops at the suffix, if any; value goes over the wall => always generate | 
|---|
| 195 |  | 
|---|
| 196 | #if ! defined(__SIZEOF_INT128__) | 
|---|
| 197 | if ( type == 5 ) SemanticError( yylloc, "int128 constant is not supported on this target \"%s\"", str.c_str() ); | 
|---|
| 198 | #endif // ! __SIZEOF_INT128__ | 
|---|
| 199 |  | 
|---|
| 200 | if ( str[0] == '0' ) {                                                          // radix character ? | 
|---|
| 201 | dec = false; | 
|---|
| 202 | if ( checkX( str[1] ) ) {                                               // hex constant ? | 
|---|
| 203 | if ( type < 5 ) {                                                       // not L128 ? | 
|---|
| 204 | sscanf( (char *)str.c_str(), "%llx", &v ); | 
|---|
| 205 | #if defined(__SIZEOF_INT128__) | 
|---|
| 206 | } else {                                                                        // hex int128 constant | 
|---|
| 207 | unsigned int len = str.length(); | 
|---|
| 208 | if ( len > (2 + 16 + 16) ) SemanticError( yylloc, "128-bit hexadecimal constant to large \"%s\"", str.c_str() ); | 
|---|
| 209 | // hex digits < 2^64 | 
|---|
| 210 | if ( len > (2 + 16) ) { | 
|---|
| 211 | str2 = "0x" + str.substr( len - 16 ); | 
|---|
| 212 | sscanf( (char *)str2.c_str(), "%llx", &v2 ); | 
|---|
| 213 | str = str.substr( 0, len - 16 ); | 
|---|
| 214 | } // if | 
|---|
| 215 | sscanf( (char *)str.c_str(), "%llx", &v ); | 
|---|
| 216 | #endif // __SIZEOF_INT128__ | 
|---|
| 217 | } // if | 
|---|
| 218 | //printf( "%llx %llu\n", v, v ); | 
|---|
| 219 | } else if ( checkB( str[1] ) ) {                                // binary constant ? | 
|---|
| 220 | #if defined(__SIZEOF_INT128__) | 
|---|
| 221 | unsigned int len = str.length(); | 
|---|
| 222 | if ( type == 5 && len > 2 + 64 ) { | 
|---|
| 223 | if ( len > 2 + 64 + 64 ) SemanticError( yylloc, "128-bit binary constant to large \"%s\".", str.c_str() ); | 
|---|
| 224 | str2 = "0b" + str.substr( len - 64 ); | 
|---|
| 225 | str = str.substr( 0, len - 64 ); | 
|---|
| 226 | scanbin( str2, v2 ); | 
|---|
| 227 | } // if | 
|---|
| 228 | #endif // __SIZEOF_INT128__ | 
|---|
| 229 | scanbin( str, v ); | 
|---|
| 230 | //printf( "%#llx %llu\n", v, v ); | 
|---|
| 231 | } else {                                                                                // octal constant | 
|---|
| 232 | if ( type < 5 ) {                                                       // not L128 ? | 
|---|
| 233 | sscanf( (char *)str.c_str(), "%llo", &v ); | 
|---|
| 234 | #if defined(__SIZEOF_INT128__) | 
|---|
| 235 | } else {                                                                        // octal int128 constant | 
|---|
| 236 | unsigned int len = str.length(); | 
|---|
| 237 | if ( len > 1 + 43 || (len == 1 + 43 && str[0] > '3') ) SemanticError( yylloc, "128-bit octal constant to large \"%s\"", str.c_str() ); | 
|---|
| 238 | char buf[32]; | 
|---|
| 239 | if ( len <= 1 + 21 ) {                                  // value < 21 octal digitis | 
|---|
| 240 | sscanf( (char *)str.c_str(), "%llo", &v ); | 
|---|
| 241 | } else { | 
|---|
| 242 | sscanf( &str[len - 21], "%llo", &v ); | 
|---|
| 243 | __int128 val = v;                                       // accumulate bits | 
|---|
| 244 | str[len - 21] ='\0';                            // shorten string | 
|---|
| 245 | sscanf( &str[len == 43 ? 1 : 0], "%llo", &v ); | 
|---|
| 246 | val |= (__int128)v << 63;                       // store bits | 
|---|
| 247 | if ( len == 1 + 43 ) {                          // most significant 2 bits ? | 
|---|
| 248 | str[2] = '\0';                                  // shorten string | 
|---|
| 249 | sscanf( &str[1], "%llo", &v );  // process most significant 2 bits | 
|---|
| 250 | val |= (__int128)v << 126;              // store bits | 
|---|
| 251 | } // if | 
|---|
| 252 | v = val >> 64; v2 = (uint64_t)val;      // replace octal constant with 2 hex constants | 
|---|
| 253 | sprintf( buf, "%#llx", v2 ); | 
|---|
| 254 | str2 = buf; | 
|---|
| 255 | } // if | 
|---|
| 256 | sprintf( buf, "%#llx", v ); | 
|---|
| 257 | str = buf; | 
|---|
| 258 | #endif // __SIZEOF_INT128__ | 
|---|
| 259 | } // if | 
|---|
| 260 | //printf( "%#llo %llu\n", v, v ); | 
|---|
| 261 | } // if | 
|---|
| 262 | } else {                                                                                        // decimal constant ? | 
|---|
| 263 | if ( type < 5 ) {                                                               // not L128 ? | 
|---|
| 264 | sscanf( (char *)str.c_str(), "%llu", &v ); | 
|---|
| 265 | #if defined(__SIZEOF_INT128__) | 
|---|
| 266 | } else {                                                                                // decimal int128 constant | 
|---|
| 267 | #define P10_UINT64 10'000'000'000'000'000'000ULL // 19 zeroes | 
|---|
| 268 | unsigned int len = str.length(); | 
|---|
| 269 | if ( str.length() == 39 && str > (Unsigned ? "340282366920938463463374607431768211455" : "170141183460469231731687303715884105727") ) | 
|---|
| 270 | SemanticError( yylloc, "128-bit decimal constant to large \"%s\".", str.c_str() ); | 
|---|
| 271 | char buf[32]; | 
|---|
| 272 | if ( len <= 19 ) {                                                      // value < 19 decimal digitis | 
|---|
| 273 | sscanf( (char *)str.c_str(), "%llu", &v ); | 
|---|
| 274 | } else { | 
|---|
| 275 | sscanf( &str[len - 19], "%llu", &v ); | 
|---|
| 276 | __int128 val = v;                                               // accumulate bits | 
|---|
| 277 | str[len - 19] ='\0';                                    // shorten string | 
|---|
| 278 | sscanf( &str[len == 39 ? 1 : 0], "%llu", &v ); | 
|---|
| 279 | val += (__int128)v * (__int128)P10_UINT64; // store bits | 
|---|
| 280 | if ( len == 39 ) {                                              // most significant 2 bits ? | 
|---|
| 281 | str[1] = '\0';                                          // shorten string | 
|---|
| 282 | sscanf( &str[0], "%llu", &v );          // process most significant 2 bits | 
|---|
| 283 | val += (__int128)v * (__int128)P10_UINT64 * (__int128)P10_UINT64; // store bits | 
|---|
| 284 | } // if | 
|---|
| 285 | v = val >> 64; v2 = (uint64_t)val;              // replace decimal constant with 2 hex constants | 
|---|
| 286 | sprintf( buf, "%#llx", v2 ); | 
|---|
| 287 | str2 = buf; | 
|---|
| 288 | } // if | 
|---|
| 289 | sprintf( buf, "%#llx", v ); | 
|---|
| 290 | str = buf; | 
|---|
| 291 | #endif // __SIZEOF_INT128__ | 
|---|
| 292 | } // if | 
|---|
| 293 | //printf( "%llu\n", v ); | 
|---|
| 294 | } // if | 
|---|
| 295 |  | 
|---|
| 296 | if ( type == -1 ) {                                                                     // no suffix => determine type from value size | 
|---|
| 297 | valueToType( v, dec, type, Unsigned ); | 
|---|
| 298 | } // if | 
|---|
| 299 | /* printf( "%s %llo %s %llo\n", str.c_str(), v, str2.c_str(), v2 ); */ | 
|---|
| 300 |  | 
|---|
| 301 | //if ( !( 0 <= type && type <= 6 ) ) { printf( "%s %lu %d %s\n", fred.c_str(), fred.length(), type, str.c_str() ); } | 
|---|
| 302 | assert( 0 <= type && type <= 6 ); | 
|---|
| 303 |  | 
|---|
| 304 | // Constant type is correct for overload resolving. | 
|---|
| 305 | ret = new ast::ConstantExpr( location, | 
|---|
| 306 | new ast::BasicType( kind[Unsigned][type] ), str, v ); | 
|---|
| 307 | if ( Unsigned && type < 2 ) {                                           // hh or h, less than int ? | 
|---|
| 308 | // int i = -1uh => 65535 not -1, so cast is necessary for unsigned, which unfortunately eliminates warnings for large values. | 
|---|
| 309 | ret = new ast::CastExpr( location, | 
|---|
| 310 | ret, | 
|---|
| 311 | new ast::BasicType( kind[Unsigned][type] ), | 
|---|
| 312 | ast::ExplicitCast ); | 
|---|
| 313 | } else if ( ltype != -1 ) {                                                     // explicit length ? | 
|---|
| 314 | if ( ltype == 6 ) {                                                             // int128, (int128)constant | 
|---|
| 315 | ret2 = new ast::ConstantExpr( location, | 
|---|
| 316 | new ast::BasicType( ast::BasicKind::LongLongSignedInt ), | 
|---|
| 317 | str2, | 
|---|
| 318 | v2 ); | 
|---|
| 319 | ret = build_compoundLiteral( location, | 
|---|
| 320 | DeclarationNode::newFromTypeData( | 
|---|
| 321 | addType( | 
|---|
| 322 | build_basic_type( TypeData::Int128 ), | 
|---|
| 323 | build_signedness( TypeData::Unsigned ) ) ), | 
|---|
| 324 | new InitializerNode( | 
|---|
| 325 | (new InitializerNode( new ExpressionNode( v2 == 0 ? ret2 : ret ) ))->set_last( new InitializerNode( new ExpressionNode( v2 == 0 ? ret : ret2 ) ) ), true ) | 
|---|
| 326 | ); | 
|---|
| 327 | } else {                                                                                // explicit length, (length_type)constant | 
|---|
| 328 | ret = new ast::CastExpr( location, | 
|---|
| 329 | ret, | 
|---|
| 330 | new ast::TypeInstType( lnthsInt[Unsigned][ltype], ast::TypeDecl::Dtype ), | 
|---|
| 331 | ast::ExplicitCast ); | 
|---|
| 332 | if ( ltype == 5 ) {                                                     // pointer, intptr( (uintptr_t)constant ) | 
|---|
| 333 | ret = build_func( location, | 
|---|
| 334 | new ExpressionNode( | 
|---|
| 335 | build_varref( location, new string( "intptr" ) ) ), | 
|---|
| 336 | new ExpressionNode( ret ) ); | 
|---|
| 337 | } // if | 
|---|
| 338 | } // if | 
|---|
| 339 | } // if | 
|---|
| 340 |  | 
|---|
| 341 | CLEANUP: ; | 
|---|
| 342 | delete &str;                                                                            // created by lex | 
|---|
| 343 | return ret; | 
|---|
| 344 | } // build_constantInteger | 
|---|
| 345 |  | 
|---|
| 346 |  | 
|---|
| 347 | static inline void checkFnxFloat( string & str, size_t last, bool & explnth, int & type ) { | 
|---|
| 348 | string::size_type posn; | 
|---|
| 349 | // floating-point constant has minimum of 2 characters, 1. or .1, so safe to look ahead | 
|---|
| 350 | if ( str[1] == 'x' ) {                                                          // hex ? | 
|---|
| 351 | posn = str.find_last_of( "pP" );                                // back for exponent (must have) | 
|---|
| 352 | posn = str.find_first_of( "fF", posn + 1 );             // forward for size (fF allowed in hex constant) | 
|---|
| 353 | } else { | 
|---|
| 354 | posn = str.find_last_of( "fF" );                                // back for size (fF not allowed) | 
|---|
| 355 | } // if | 
|---|
| 356 | if ( posn == string::npos ) return; | 
|---|
| 357 | explnth = true; | 
|---|
| 358 | posn += 1;                                                                                      // advance to size | 
|---|
| 359 | if ( str[posn] == '3' ) {                                                       // 32 | 
|---|
| 360 | if ( str[last] != 'x' ) type = 6; | 
|---|
| 361 | else type = 7; | 
|---|
| 362 | } else if ( str[posn] == '6' ) {                                        // 64 | 
|---|
| 363 | if ( str[last] != 'x' ) type = 8; | 
|---|
| 364 | else type = 9; | 
|---|
| 365 | } else if ( str[posn] == '8' ) {                                        // 80 | 
|---|
| 366 | type = 3; | 
|---|
| 367 | } else if ( str[posn] == '1' ) {                                        // 16/128 | 
|---|
| 368 | if ( str[posn + 1] == '6' ) {                                   // 16 | 
|---|
| 369 | type = 5; | 
|---|
| 370 | } else {                                                                                // 128 | 
|---|
| 371 | if ( str[last] != 'x' ) type = 10; | 
|---|
| 372 | else type = 11; | 
|---|
| 373 | } // if | 
|---|
| 374 | } else { | 
|---|
| 375 | assertf( false, "internal error, bad floating point length %s", str.c_str() ); | 
|---|
| 376 | } // if | 
|---|
| 377 | } // checkFnxFloat | 
|---|
| 378 |  | 
|---|
| 379 |  | 
|---|
| 380 | ast::Expr * build_constantFloat( | 
|---|
| 381 | const CodeLocation & location, string & str ) { | 
|---|
| 382 | static const ast::BasicKind kind[2][12] = { | 
|---|
| 383 | { ast::BasicKind::Float, ast::BasicKind::Double, ast::BasicKind::LongDouble, ast::BasicKind::Float80, ast::BasicKind::uuFloat128, ast::BasicKind::Float16, ast::BasicKind::Float32, ast::BasicKind::Float32x, ast::BasicKind::Float64, ast::BasicKind::Float64x, ast::BasicKind::Float128, ast::BasicKind::Float128x }, | 
|---|
| 384 | { ast::BasicKind::FloatComplex, ast::BasicKind::DoubleComplex, ast::BasicKind::LongDoubleComplex, ast::BasicKind::NUMBER_OF_BASIC_TYPES, ast::BasicKind::NUMBER_OF_BASIC_TYPES, ast::BasicKind::Float16Complex, ast::BasicKind::Float32Complex, ast::BasicKind::Float32xComplex, ast::BasicKind::Float64Complex, ast::BasicKind::Float64xComplex, ast::BasicKind::Float128Complex, ast::BasicKind::Float128xComplex }, | 
|---|
| 385 | }; | 
|---|
| 386 |  | 
|---|
| 387 | // floating-point constant has minimum of 2 characters 1. or .1 | 
|---|
| 388 | size_t last = str.length() - 1; | 
|---|
| 389 | double v; | 
|---|
| 390 | int type;                                                                                       // 0 => float, 1 => double, 3 => long double, ... | 
|---|
| 391 | bool complx = false;                                                            // real, complex | 
|---|
| 392 | bool explnth = false;                                                           // explicit literal length | 
|---|
| 393 |  | 
|---|
| 394 | sscanf( str.c_str(), "%lg", &v ); | 
|---|
| 395 |  | 
|---|
| 396 | if ( checkI( str[last] ) ) {                                            // imaginary ? | 
|---|
| 397 | complx = true; | 
|---|
| 398 | last -= 1;                                                                              // backup one character | 
|---|
| 399 | } // if | 
|---|
| 400 |  | 
|---|
| 401 | if ( checkF( str[last] ) ) {                                            // float ? | 
|---|
| 402 | type = 0; | 
|---|
| 403 | } else if ( checkD( str[last] ) ) {                                     // double ? | 
|---|
| 404 | type = 1; | 
|---|
| 405 | } else if ( checkL( str[last] ) ) {                                     // long double ? | 
|---|
| 406 | type = 2; | 
|---|
| 407 | } else if ( checkF80( str[last] ) ) {                           // __float80 ? | 
|---|
| 408 | type = 3; | 
|---|
| 409 | } else if ( checkF128( str[last] ) ) {                          // __float128 ? | 
|---|
| 410 | type = 4; | 
|---|
| 411 | } else { | 
|---|
| 412 | type = 1;                                                                               // double (default if no suffix) | 
|---|
| 413 | checkFnxFloat( str, last, explnth, type ); | 
|---|
| 414 | } // if | 
|---|
| 415 |  | 
|---|
| 416 | if ( ! complx && checkI( str[last - 1] ) ) {            // imaginary ? | 
|---|
| 417 | complx = true; | 
|---|
| 418 | } // if | 
|---|
| 419 |  | 
|---|
| 420 | assert( 0 <= type && type < 12 ); | 
|---|
| 421 | ast::Expr * ret = new ast::ConstantExpr( location, | 
|---|
| 422 | new ast::BasicType( kind[complx][type] ), | 
|---|
| 423 | str, | 
|---|
| 424 | v ); | 
|---|
| 425 | // explicit length ? | 
|---|
| 426 | if ( explnth ) { | 
|---|
| 427 | ret = new ast::CastExpr( location, | 
|---|
| 428 | ret, | 
|---|
| 429 | new ast::BasicType( kind[complx][type] ), | 
|---|
| 430 | ast::ExplicitCast ); | 
|---|
| 431 | } // if | 
|---|
| 432 |  | 
|---|
| 433 | delete &str;                                                                            // created by lex | 
|---|
| 434 | return ret; | 
|---|
| 435 | } // build_constantFloat | 
|---|
| 436 |  | 
|---|
| 437 | static void sepString( string & str, string & units, char delimit ) { | 
|---|
| 438 | string::size_type posn = str.find_last_of( delimit ) + 1; | 
|---|
| 439 | if ( posn != str.length() ) { | 
|---|
| 440 | units = "?" + str.substr( posn );                               // extract units | 
|---|
| 441 | str.erase( posn );                                                              // remove units | 
|---|
| 442 | } // if | 
|---|
| 443 | } // sepString | 
|---|
| 444 |  | 
|---|
| 445 | static ast::Type * charstrKind( string & str ) { | 
|---|
| 446 | ast::Type * kind; | 
|---|
| 447 | switch ( str[0] ) {                                                                     // str has >= 2 characters, i.e, null string "" => safe to look at subscripts 0/1 | 
|---|
| 448 | case 'u': | 
|---|
| 449 | if ( str[1] == '8' ) goto Default;                              // utf-8 characters => array of char (save check for char as no 8 allowed) | 
|---|
| 450 | // lookup type of associated typedef | 
|---|
| 451 | kind = new ast::TypeInstType( "char16_t", ast::TypeDecl::Dtype ); | 
|---|
| 452 | break; | 
|---|
| 453 | case 'U': | 
|---|
| 454 | kind = new ast::TypeInstType( "char32_t", ast::TypeDecl::Dtype ); | 
|---|
| 455 | break; | 
|---|
| 456 | case 'L': | 
|---|
| 457 | kind = new ast::TypeInstType( "wchar_t", ast::TypeDecl::Dtype ); | 
|---|
| 458 | break; | 
|---|
| 459 | Default:                                                                                        // char default string type | 
|---|
| 460 | default: | 
|---|
| 461 | kind = new ast::BasicType( ast::BasicKind::Char ); | 
|---|
| 462 | } // switch | 
|---|
| 463 | return kind; | 
|---|
| 464 | } // charstrKind | 
|---|
| 465 |  | 
|---|
| 466 | static bool isoctal( char ch ) { | 
|---|
| 467 | return ('0' <= ch && ch <= '7'); | 
|---|
| 468 | } // isoctal | 
|---|
| 469 |  | 
|---|
| 470 | // A "sequence" is the series of characters in a character/string literal that becomes a single | 
|---|
| 471 | // character value in the runtime value. | 
|---|
| 472 | static size_t sequenceLength( const std::string & str, size_t pos ) { | 
|---|
| 473 | // Most "sequences" are just a single character, filter those out: | 
|---|
| 474 | if ( '\\' != str[pos] ) return 1; | 
|---|
| 475 | switch ( str[pos + 1] ) { | 
|---|
| 476 | // Simple Escape Sequence (\_ where _ is one of the following): | 
|---|
| 477 | case '\'': case '\"': case '?': case '\\': | 
|---|
| 478 | case 'a': case 'b': case 'f': case 'n': case 'r': case 't': case 'v': | 
|---|
| 479 | // GCC Escape Sequence (as simple, just some different letters): | 
|---|
| 480 | case 'e': | 
|---|
| 481 | return 2; | 
|---|
| 482 | // Numeric Escape Sequence (\___ where _ is 1-3 octal digits): | 
|---|
| 483 | case '0': case '1': case '2': case '3': | 
|---|
| 484 | case '4': case '5': case '6': case '7': | 
|---|
| 485 | return ( !isoctal( str[pos + 2] ) ) ? 2 : | 
|---|
| 486 | ( !isoctal( str[pos + 3] ) ) ? 3 : 4; | 
|---|
| 487 | // Numeric Escape Sequence (\x_ where _ is 1 or more hexadecimal digits): | 
|---|
| 488 | case 'x': { | 
|---|
| 489 | size_t length = 2; | 
|---|
| 490 | while ( isxdigit( str[pos + length] ) ) ++length; | 
|---|
| 491 | return length; | 
|---|
| 492 | } | 
|---|
| 493 | // Universal Character Name (\u____ where _ is 4 decimal digits): | 
|---|
| 494 | case 'u': | 
|---|
| 495 | return 6; | 
|---|
| 496 | // Universal Character Name (\U________ where _ is 8 decimal digits): | 
|---|
| 497 | case 'U': | 
|---|
| 498 | return 10; | 
|---|
| 499 | default: | 
|---|
| 500 | SemanticError( yylloc, "Unknown escape sequence \"\\%c\"", str[pos + 1] ); | 
|---|
| 501 | return 1; | 
|---|
| 502 | } // switch | 
|---|
| 503 | } // sequenceLength | 
|---|
| 504 |  | 
|---|
| 505 | ast::Expr * build_constantChar( const CodeLocation & location, string & str ) { | 
|---|
| 506 | string units;                                                                           // units | 
|---|
| 507 | sepString( str, units, '\'' );                                          // separate constant from units | 
|---|
| 508 |  | 
|---|
| 509 | ast::Expr * ret = new ast::ConstantExpr( location, charstrKind( str ), str, (unsigned long long int)(unsigned char)str[1] ); | 
|---|
| 510 | if ( units.length() != 0 ) { | 
|---|
| 511 | ret = new ast::UntypedExpr( location, new ast::NameExpr( location, units ),     { ret } ); | 
|---|
| 512 | } // if | 
|---|
| 513 |  | 
|---|
| 514 | delete &str;                                                                            // created by lex | 
|---|
| 515 | return ret; | 
|---|
| 516 | } // build_constantChar | 
|---|
| 517 |  | 
|---|
| 518 | ast::Expr * build_constantStr( const CodeLocation & location, string & str ) { | 
|---|
| 519 | assert( str.length() > 0 ); | 
|---|
| 520 | string units;                                                                           // units | 
|---|
| 521 | sepString( str, units, '"' );                                           // separate constant from units | 
|---|
| 522 |  | 
|---|
| 523 | // The length value of the type is equal to the number of "sequences" not including the openning | 
|---|
| 524 | // and closing quotes in the literal plus 1 for the implicit null terminator. | 
|---|
| 525 | size_t length = 1; | 
|---|
| 526 | for ( size_t pos = 1 ; pos < str.size() - 1 ; pos += sequenceLength( str, pos ) ) { | 
|---|
| 527 | if ( '"' == str[pos] ) {                                                // concatenated strings ? "ABC" "DEF" | 
|---|
| 528 | int cnt = 1;                                                            // skip outside quotes and space between | 
|---|
| 529 | for ( unsigned int i = pos + 1; str[i] != '"'; i += 1, cnt += 1 ); | 
|---|
| 530 | pos += cnt; | 
|---|
| 531 | continue;                                                                       // not part of length | 
|---|
| 532 | } // if | 
|---|
| 533 | length += 1; | 
|---|
| 534 | } // for | 
|---|
| 535 |  | 
|---|
| 536 | ast::ArrayType * at = new ast::ArrayType( charstrKind( str ), ast::ConstantExpr::from_ulong( location, length ), ast::FixedLen, ast::DynamicDim ); | 
|---|
| 537 | ast::Expr * ret = new ast::ConstantExpr( location, at, str, std::nullopt ); | 
|---|
| 538 | if ( units.length() != 0 ) { | 
|---|
| 539 | ret = new ast::UntypedExpr( location, new ast::NameExpr( location, units ),     { ret } ); | 
|---|
| 540 | } // if | 
|---|
| 541 |  | 
|---|
| 542 | delete &str;                                                                            // created by lex | 
|---|
| 543 | return ret; | 
|---|
| 544 | } // build_constantStr | 
|---|
| 545 |  | 
|---|
| 546 | ast::Expr * build_field_name_FLOATING_FRACTIONconstant( | 
|---|
| 547 | const CodeLocation & location, const string & str ) { | 
|---|
| 548 | if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) SemanticError( yylloc, "invalid tuple index \"%s\".", str.c_str() ); | 
|---|
| 549 | ast::Expr * ret = build_constantInteger( location, | 
|---|
| 550 | *new string( str.substr(1) ) ); | 
|---|
| 551 | delete &str; | 
|---|
| 552 | return ret; | 
|---|
| 553 | } // build_field_name_FLOATING_FRACTIONconstant | 
|---|
| 554 |  | 
|---|
| 555 | ast::Expr * build_field_name_FLOATING_DECIMALconstant( | 
|---|
| 556 | const CodeLocation & location, const string & str ) { | 
|---|
| 557 | if ( str[str.size() - 1] != '.' ) SemanticError( yylloc, "invalid tuple index \"%s\".", str.c_str() ); | 
|---|
| 558 | ast::Expr * ret = build_constantInteger( | 
|---|
| 559 | location, *new string( str.substr( 0, str.size()-1 ) ) ); | 
|---|
| 560 | delete &str; | 
|---|
| 561 | return ret; | 
|---|
| 562 | } // build_field_name_FLOATING_DECIMALconstant | 
|---|
| 563 |  | 
|---|
| 564 | ast::Expr * build_field_name_FLOATINGconstant( const CodeLocation & location, | 
|---|
| 565 | const string & str ) { | 
|---|
| 566 | // str is of the form A.B -> separate at the . and return member expression | 
|---|
| 567 | int a, b; | 
|---|
| 568 | char dot; | 
|---|
| 569 | stringstream ss( str ); | 
|---|
| 570 | ss >> a >> dot >> b; | 
|---|
| 571 | auto ret = new ast::UntypedMemberExpr( location, | 
|---|
| 572 | ast::ConstantExpr::from_int( location, b ), | 
|---|
| 573 | ast::ConstantExpr::from_int( location, a ) | 
|---|
| 574 | ); | 
|---|
| 575 | delete &str; | 
|---|
| 576 | return ret; | 
|---|
| 577 | } // build_field_name_FLOATINGconstant | 
|---|
| 578 |  | 
|---|
| 579 | ast::Expr * make_field_name_fraction_constants( const CodeLocation & location, | 
|---|
| 580 | ast::Expr * fieldName, | 
|---|
| 581 | ast::Expr * fracts ) { | 
|---|
| 582 | if ( nullptr == fracts ) { | 
|---|
| 583 | return fieldName; | 
|---|
| 584 | } else if ( auto memberExpr = dynamic_cast<ast::UntypedMemberExpr *>( fracts ) ) { | 
|---|
| 585 | memberExpr->member = make_field_name_fraction_constants( location, | 
|---|
| 586 | fieldName, | 
|---|
| 587 | ast::mutate( memberExpr->aggregate.get() ) ); | 
|---|
| 588 | return memberExpr; | 
|---|
| 589 | } else { | 
|---|
| 590 | return new ast::UntypedMemberExpr( location, fracts, fieldName ); | 
|---|
| 591 | } // if | 
|---|
| 592 | } // make_field_name_fraction_constants | 
|---|
| 593 |  | 
|---|
| 594 | ast::Expr * build_field_name_fraction_constants( const CodeLocation & location, | 
|---|
| 595 | ast::Expr * fieldName, | 
|---|
| 596 | ExpressionNode * fracts ) { | 
|---|
| 597 | return make_field_name_fraction_constants( location, fieldName, maybeMoveBuild( fracts ) ); | 
|---|
| 598 | } // build_field_name_fraction_constants | 
|---|
| 599 |  | 
|---|
| 600 | ast::NameExpr * build_varref( const CodeLocation & location, | 
|---|
| 601 | const string * name ) { | 
|---|
| 602 | ast::NameExpr * expr = new ast::NameExpr( location, *name ); | 
|---|
| 603 | delete name; | 
|---|
| 604 | return expr; | 
|---|
| 605 | } // build_varref | 
|---|
| 606 |  | 
|---|
| 607 | ast::QualifiedNameExpr * build_qualified_expr( const CodeLocation & location, | 
|---|
| 608 | const DeclarationNode * decl_node, | 
|---|
| 609 | const ast::NameExpr * name ) { | 
|---|
| 610 | ast::Decl * newDecl = maybeBuild( decl_node ); | 
|---|
| 611 | if ( ast::DeclWithType * newDeclWithType = dynamic_cast<ast::DeclWithType *>( newDecl ) ) { | 
|---|
| 612 | if ( const ast::Type * t = newDeclWithType->get_type() ) { | 
|---|
| 613 | if ( auto typeInst = dynamic_cast<const ast::TypeInstType *>( t ) ) { | 
|---|
| 614 | newDecl = new ast::EnumDecl( location, typeInst->name ); | 
|---|
| 615 | } | 
|---|
| 616 | } | 
|---|
| 617 | } | 
|---|
| 618 | return new ast::QualifiedNameExpr( location, newDecl, name->name ); | 
|---|
| 619 | } | 
|---|
| 620 |  | 
|---|
| 621 | ast::QualifiedNameExpr * build_qualified_expr( const CodeLocation & location, | 
|---|
| 622 | const ast::EnumDecl * decl, | 
|---|
| 623 | const ast::NameExpr * name ) { | 
|---|
| 624 | return new ast::QualifiedNameExpr( location, decl, name->name ); | 
|---|
| 625 | } | 
|---|
| 626 |  | 
|---|
| 627 | ast::DimensionExpr * build_dimensionref( const CodeLocation & location, | 
|---|
| 628 | const string * name ) { | 
|---|
| 629 | ast::DimensionExpr * expr = new ast::DimensionExpr( location, *name ); | 
|---|
| 630 | delete name; | 
|---|
| 631 | return expr; | 
|---|
| 632 | } // build_varref | 
|---|
| 633 |  | 
|---|
| 634 | // TODO: get rid of this and OperKinds and reuse code from OperatorTable | 
|---|
| 635 | static const char * OperName[] = {                                              // must harmonize with OperKinds | 
|---|
| 636 | // diadic | 
|---|
| 637 | "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&", | 
|---|
| 638 | "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?", | 
|---|
| 639 | "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?", | 
|---|
| 640 | "?[?]", "...", | 
|---|
| 641 | // monadic | 
|---|
| 642 | "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", | 
|---|
| 643 | }; // OperName | 
|---|
| 644 |  | 
|---|
| 645 | ast::Expr * build_cast( const CodeLocation & location, | 
|---|
| 646 | DeclarationNode * decl_node, | 
|---|
| 647 | ExpressionNode * expr_node, | 
|---|
| 648 | ast::CastKind kind ) { | 
|---|
| 649 | ast::Type * targetType = maybeMoveBuildType( decl_node ); | 
|---|
| 650 | if ( dynamic_cast<ast::VoidType *>( targetType ) ) { | 
|---|
| 651 | delete targetType; | 
|---|
| 652 | return new ast::CastExpr( location, | 
|---|
| 653 | maybeMoveBuild( expr_node ), | 
|---|
| 654 | ast::ExplicitCast, kind ); | 
|---|
| 655 | } else { | 
|---|
| 656 | return new ast::CastExpr( location, | 
|---|
| 657 | maybeMoveBuild( expr_node ), | 
|---|
| 658 | targetType, | 
|---|
| 659 | ast::ExplicitCast, kind ); | 
|---|
| 660 | } // if | 
|---|
| 661 | } // build_cast | 
|---|
| 662 |  | 
|---|
| 663 | ast::Expr * build_keyword_cast( const CodeLocation & location, | 
|---|
| 664 | ast::AggregateDecl::Aggregate target, | 
|---|
| 665 | ExpressionNode * expr_node ) { | 
|---|
| 666 | return new ast::KeywordCastExpr( location, | 
|---|
| 667 | maybeMoveBuild( expr_node ), | 
|---|
| 668 | target | 
|---|
| 669 | ); | 
|---|
| 670 | } | 
|---|
| 671 |  | 
|---|
| 672 | ast::Expr * build_virtual_cast( const CodeLocation & location, | 
|---|
| 673 | DeclarationNode * decl_node, | 
|---|
| 674 | ExpressionNode * expr_node ) { | 
|---|
| 675 | return new ast::VirtualCastExpr( location, | 
|---|
| 676 | maybeMoveBuild( expr_node ), | 
|---|
| 677 | maybeMoveBuildType( decl_node ) | 
|---|
| 678 | ); | 
|---|
| 679 | } // build_virtual_cast | 
|---|
| 680 |  | 
|---|
| 681 | ast::Expr * build_fieldSel( const CodeLocation & location, | 
|---|
| 682 | ExpressionNode * expr_node, | 
|---|
| 683 | ast::Expr * member ) { | 
|---|
| 684 | return new ast::UntypedMemberExpr( location, | 
|---|
| 685 | member, | 
|---|
| 686 | maybeMoveBuild( expr_node ) | 
|---|
| 687 | ); | 
|---|
| 688 | } // build_fieldSel | 
|---|
| 689 |  | 
|---|
| 690 | ast::Expr * build_pfieldSel( const CodeLocation & location, | 
|---|
| 691 | ExpressionNode * expr_node, | 
|---|
| 692 | ast::Expr * member ) { | 
|---|
| 693 | auto deref = new ast::UntypedExpr( location, | 
|---|
| 694 | new ast::NameExpr( location, "*?" ) | 
|---|
| 695 | ); | 
|---|
| 696 | deref->location = expr_node->location; | 
|---|
| 697 | deref->args.push_back( maybeMoveBuild( expr_node ) ); | 
|---|
| 698 | auto ret = new ast::UntypedMemberExpr( location, member, deref ); | 
|---|
| 699 | return ret; | 
|---|
| 700 | } // build_pfieldSel | 
|---|
| 701 |  | 
|---|
| 702 | ast::Expr * build_offsetOf( const CodeLocation & location, | 
|---|
| 703 | DeclarationNode * decl_node, | 
|---|
| 704 | ast::NameExpr * member ) { | 
|---|
| 705 | ast::Expr * ret = new ast::UntypedOffsetofExpr( location, | 
|---|
| 706 | maybeMoveBuildType( decl_node ), | 
|---|
| 707 | member->name | 
|---|
| 708 | ); | 
|---|
| 709 | ret->result = new ast::BasicType( ast::BasicKind::LongUnsignedInt ); | 
|---|
| 710 | delete member; | 
|---|
| 711 | return ret; | 
|---|
| 712 | } // build_offsetOf | 
|---|
| 713 |  | 
|---|
| 714 | ast::Expr * build_and_or( const CodeLocation & location, | 
|---|
| 715 | ExpressionNode * expr_node1, | 
|---|
| 716 | ExpressionNode * expr_node2, | 
|---|
| 717 | ast::LogicalFlag flag ) { | 
|---|
| 718 | return new ast::LogicalExpr( location, | 
|---|
| 719 | maybeMoveBuild( expr_node1 ), | 
|---|
| 720 | maybeMoveBuild( expr_node2 ), | 
|---|
| 721 | flag | 
|---|
| 722 | ); | 
|---|
| 723 | } // build_and_or | 
|---|
| 724 |  | 
|---|
| 725 | ast::Expr * build_unary_val( const CodeLocation & location, | 
|---|
| 726 | OperKinds op, | 
|---|
| 727 | ExpressionNode * expr_node ) { | 
|---|
| 728 | std::vector<ast::ptr<ast::Expr>> args; | 
|---|
| 729 | args.push_back( maybeMoveBuild( expr_node ) ); | 
|---|
| 730 | return new ast::UntypedExpr( location, | 
|---|
| 731 | new ast::NameExpr( location, OperName[ (int)op ] ), | 
|---|
| 732 | std::move( args ) | 
|---|
| 733 | ); | 
|---|
| 734 | } // build_unary_val | 
|---|
| 735 |  | 
|---|
| 736 | ast::Expr * build_binary_val( const CodeLocation & location, | 
|---|
| 737 | OperKinds op, | 
|---|
| 738 | ExpressionNode * expr_node1, | 
|---|
| 739 | ExpressionNode * expr_node2 ) { | 
|---|
| 740 | std::vector<ast::ptr<ast::Expr>> args; | 
|---|
| 741 | args.push_back( maybeMoveBuild( expr_node1 ) ); | 
|---|
| 742 | args.push_back( maybeMoveBuild( expr_node2 ) ); | 
|---|
| 743 | return new ast::UntypedExpr( location, | 
|---|
| 744 | new ast::NameExpr( location, OperName[ (int)op ] ), | 
|---|
| 745 | std::move( args ) | 
|---|
| 746 | ); | 
|---|
| 747 | } // build_binary_val | 
|---|
| 748 |  | 
|---|
| 749 | ast::Expr * build_cond( const CodeLocation & location, | 
|---|
| 750 | ExpressionNode * expr_node1, | 
|---|
| 751 | ExpressionNode * expr_node2, | 
|---|
| 752 | ExpressionNode * expr_node3 ) { | 
|---|
| 753 | return new ast::ConditionalExpr( location, | 
|---|
| 754 | maybeMoveBuild( expr_node1 ), | 
|---|
| 755 | maybeMoveBuild( expr_node2 ), | 
|---|
| 756 | maybeMoveBuild( expr_node3 ) | 
|---|
| 757 | ); | 
|---|
| 758 | } // build_cond | 
|---|
| 759 |  | 
|---|
| 760 | ast::Expr * build_tuple( const CodeLocation & location, | 
|---|
| 761 | ExpressionNode * expr_node ) { | 
|---|
| 762 | std::vector<ast::ptr<ast::Expr>> exprs; | 
|---|
| 763 | buildMoveList( expr_node, exprs ); | 
|---|
| 764 | return new ast::UntypedTupleExpr( location, std::move( exprs ) ); | 
|---|
| 765 | } // build_tuple | 
|---|
| 766 |  | 
|---|
| 767 | ast::Expr * build_func( const CodeLocation & location, | 
|---|
| 768 | ExpressionNode * function, | 
|---|
| 769 | ExpressionNode * expr_node ) { | 
|---|
| 770 | std::vector<ast::ptr<ast::Expr>> args; | 
|---|
| 771 | buildMoveList( expr_node, args ); | 
|---|
| 772 | return new ast::UntypedExpr( location, | 
|---|
| 773 | maybeMoveBuild( function ), | 
|---|
| 774 | std::move( args ) | 
|---|
| 775 | ); | 
|---|
| 776 | } // build_func | 
|---|
| 777 |  | 
|---|
| 778 | ast::Expr * build_compoundLiteral( const CodeLocation & location, | 
|---|
| 779 | DeclarationNode * decl_node, | 
|---|
| 780 | InitializerNode * kids ) { | 
|---|
| 781 | // compound literal type | 
|---|
| 782 | ast::Decl * newDecl = maybeBuild( decl_node ); | 
|---|
| 783 | // non-sue compound-literal type | 
|---|
| 784 | if ( ast::DeclWithType * newDeclWithType = dynamic_cast<ast::DeclWithType *>( newDecl ) ) { | 
|---|
| 785 | return new ast::CompoundLiteralExpr( location, | 
|---|
| 786 | newDeclWithType->get_type(), | 
|---|
| 787 | maybeMoveBuild( kids ) ); | 
|---|
| 788 | // these types do not have associated type information | 
|---|
| 789 | } else if ( auto newDeclStructDecl = dynamic_cast<ast::StructDecl *>( newDecl ) ) { | 
|---|
| 790 | if ( newDeclStructDecl->body ) { | 
|---|
| 791 | return new ast::CompoundLiteralExpr( location, | 
|---|
| 792 | new ast::StructInstType( newDeclStructDecl ), | 
|---|
| 793 | maybeMoveBuild( kids ) ); | 
|---|
| 794 | } else { | 
|---|
| 795 | return new ast::CompoundLiteralExpr( location, | 
|---|
| 796 | new ast::StructInstType( newDeclStructDecl->name ), | 
|---|
| 797 | maybeMoveBuild( kids ) ); | 
|---|
| 798 | } // if | 
|---|
| 799 | } else if ( auto newDeclUnionDecl = dynamic_cast<ast::UnionDecl *>( newDecl )  ) { | 
|---|
| 800 | if ( newDeclUnionDecl->body ) { | 
|---|
| 801 | return new ast::CompoundLiteralExpr( location, | 
|---|
| 802 | new ast::UnionInstType( newDeclUnionDecl ), | 
|---|
| 803 | maybeMoveBuild( kids ) ); | 
|---|
| 804 | } else { | 
|---|
| 805 | return new ast::CompoundLiteralExpr( location, | 
|---|
| 806 | new ast::UnionInstType( newDeclUnionDecl->name ), | 
|---|
| 807 | maybeMoveBuild( kids ) ); | 
|---|
| 808 | } // if | 
|---|
| 809 | } else if ( auto newDeclEnumDecl = dynamic_cast<ast::EnumDecl *>( newDecl )  ) { | 
|---|
| 810 | if ( newDeclEnumDecl->body ) { | 
|---|
| 811 | return new ast::CompoundLiteralExpr( location, | 
|---|
| 812 | new ast::EnumInstType( newDeclEnumDecl ), | 
|---|
| 813 | maybeMoveBuild( kids ) ); | 
|---|
| 814 | } else { | 
|---|
| 815 | return new ast::CompoundLiteralExpr( location, | 
|---|
| 816 | new ast::EnumInstType( newDeclEnumDecl->name ), | 
|---|
| 817 | maybeMoveBuild( kids ) ); | 
|---|
| 818 | } // if | 
|---|
| 819 | } else { | 
|---|
| 820 | assert( false ); | 
|---|
| 821 | } // if | 
|---|
| 822 | } // build_compoundLiteral | 
|---|
| 823 |  | 
|---|
| 824 | ast::Expr * build_va_arg( const CodeLocation & location, | 
|---|
| 825 | ExpressionNode * function, DeclarationNode * declaration ) { | 
|---|
| 826 | return build_func( location, | 
|---|
| 827 | new ExpressionNode( | 
|---|
| 828 | build_varref( location, new std::string( "__builtin_va_arg" ) ) ), | 
|---|
| 829 | function->set_last( new ExpressionNode( new ast::TypeExpr( location, | 
|---|
| 830 | maybeMoveBuildType( declaration ) ) ) ) | 
|---|
| 831 | ); | 
|---|
| 832 | } | 
|---|
| 833 |  | 
|---|
| 834 | // Local Variables: // | 
|---|
| 835 | // tab-width: 4 // | 
|---|
| 836 | // End: // | 
|---|