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