| [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 | //
|
|---|
| [c92bdcc] | 7 | // ExpressionNode.cpp --
|
|---|
| [0caaa6a] | 8 | //
|
|---|
| [f43df73] | 9 | // Author : Peter A. Buhr
|
|---|
| [b87a5ed] | 10 | // Created On : Sat May 16 13:17:07 2015
|
|---|
| [5b95e67] | 11 | // Last Modified By : Peter A. Buhr
|
|---|
| [2737b37] | 12 | // Last Modified On : Fri May 1 11:07:48 2026
|
|---|
| 13 | // Update Count : 1128
|
|---|
| [0caaa6a] | 14 | //
|
|---|
| [b87a5ed] | 15 |
|
|---|
| [c92bdcc] | 16 | #include "ExpressionNode.hpp"
|
|---|
| [c468150] | 17 |
|
|---|
| [ea6332d] | 18 | #include <cassert> // for assert
|
|---|
| [d180746] | 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==
|
|---|
| [51b73452] | 24 |
|
|---|
| [7a780ad] | 25 | #include "AST/BasicKind.hpp" // for BasicKind
|
|---|
| [bb7422a] | 26 | #include "AST/Expr.hpp" // for NameExpr
|
|---|
| [7a780ad] | 27 | #include "AST/Type.hpp" // for Type, LengthFlag, DimentionFlag
|
|---|
| [c92bdcc] | 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
|
|---|
| [d180746] | 34 |
|
|---|
| [51b73452] | 35 | using 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 |
|
|---|
| [ba01b14] | 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'; }
|
|---|
| [7bf7fb9] | 54 | static inline bool checkF( char c ) { return c == 'f' || c == 'F'; }
|
|---|
| 55 | static inline bool checkD( char c ) { return c == 'd' || c == 'D'; }
|
|---|
| [ba01b14] | 56 | static inline bool checkF80( char c ) { return c == 'w' || c == 'W'; }
|
|---|
| 57 | static inline bool checkF128( char c ) { return c == 'q' || c == 'Q'; }
|
|---|
| [f56c32e] | 58 | static inline bool checkL( char c ) { return c == 'l' || c == 'L'; }
|
|---|
| [7bf7fb9] | 59 | static inline bool checkI( char c ) { return c == 'i' || c == 'I'; }
|
|---|
| [0a2168f] | 60 | static inline bool checkB( char c ) { return c == 'b' || c == 'B'; }
|
|---|
| [7bf7fb9] | 61 | static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }
|
|---|
| [3d8d7a7] | 62 | // static inline bool checkN( char c ) { return c == 'n' || c == 'N'; }
|
|---|
| [f56c32e] | 63 |
|
|---|
| 64 | void lnthSuffix( string & str, int & type, int & ltype ) {
|
|---|
| [c5b55c4] | 65 | // 'u' can appear before or after length suffix
|
|---|
| [f56c32e] | 66 | string::size_type posn = str.find_last_of( "lL" );
|
|---|
| [0a616e0] | 67 |
|
|---|
| 68 | if ( posn == string::npos ) return; // no suffix
|
|---|
| [c5b55c4] | 69 | size_t end = str.length() - 1;
|
|---|
| 70 | if ( posn == end ) { type = 3; return; } // no length after 'l' => long
|
|---|
| [702e826] | 71 |
|
|---|
| [0a616e0] | 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;
|
|---|
| [f56c32e] | 84 | } // if
|
|---|
| 85 | } // if
|
|---|
| [c5b55c4] | 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
|
|---|
| [f56c32e] | 94 | } // lnthSuffix
|
|---|
| [7bf7fb9] | 95 |
|
|---|
| [0a616e0] | 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 |
|
|---|
| [f6582252] | 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;
|
|---|
| [0d0931d] | 122 | if ( i == last - 1 || (str[i] != '0' && str[i] != '1') ) break;
|
|---|
| [f6582252] | 123 | v <<= 1;
|
|---|
| 124 | } // for
|
|---|
| 125 | } // scanbin
|
|---|
| 126 |
|
|---|
| [2737b37] | 127 | ast::Expr * build_constantInteger( const CodeLocation & location, string & str ) {
|
|---|
| [7a780ad] | 128 | static const ast::BasicKind kind[2][6] = {
|
|---|
| [ba01b14] | 129 | // short (h) must be before char (hh) because shorter type has the longer suffix
|
|---|
| [7a780ad] | 130 | { ast::BasicKind::ShortSignedInt, ast::BasicKind::SignedChar, ast::BasicKind::SignedInt, ast::BasicKind::LongSignedInt, ast::BasicKind::LongLongSignedInt, /* BasicKind::SignedInt128 */ ast::BasicKind::LongLongSignedInt, },
|
|---|
| 131 | { ast::BasicKind::ShortUnsignedInt, ast::BasicKind::UnsignedChar, ast::BasicKind::UnsignedInt, ast::BasicKind::LongUnsignedInt, ast::BasicKind::LongLongUnsignedInt, /* BasicKind::UnsignedInt128 */ ast::BasicKind::LongLongUnsignedInt, },
|
|---|
| [7bf7fb9] | 132 | };
|
|---|
| [76c62b2] | 133 |
|
|---|
| [0a616e0] | 134 | static const char * lnthsInt[2][6] = {
|
|---|
| 135 | { "int16_t", "int8_t", "int32_t", "int64_t", "size_t", "uintptr_t", },
|
|---|
| 136 | { "uint16_t", "uint8_t", "uint32_t", "uint64_t", "size_t", "uintptr_t", },
|
|---|
| [ba01b14] | 137 | }; // lnthsInt
|
|---|
| 138 |
|
|---|
| [f6582252] | 139 | string str2( "0x0" );
|
|---|
| 140 | unsigned long long int v, v2 = 0; // converted integral value
|
|---|
| [bb7422a] | 141 | ast::Expr * ret, * ret2;
|
|---|
| [0a616e0] | 142 |
|
|---|
| 143 | int type = -1; // 0 => short, 1 => char, 2 => int, 3 => long int, 4 => long long int, 5 => int128
|
|---|
| 144 | int ltype = -1; // 0 => 16 bits, 1 => 8 bits, 2 => 32 bits, 3 => 64 bits, 4 => size_t, 5 => intptr, 6 => pointer
|
|---|
| 145 | bool dec = true, Unsigned = false; // decimal, unsigned constant
|
|---|
| [7bf7fb9] | 146 |
|
|---|
| [6165ce7] | 147 | // special constants
|
|---|
| 148 | if ( str == "0" ) {
|
|---|
| [bb7422a] | 149 | ret = new ast::ConstantExpr( location, new ast::ZeroType(), str, 0 );
|
|---|
| [6165ce7] | 150 | goto CLEANUP;
|
|---|
| 151 | } // if
|
|---|
| 152 | if ( str == "1" ) {
|
|---|
| [bb7422a] | 153 | ret = new ast::ConstantExpr( location, new ast::OneType(), str, 1 );
|
|---|
| [6165ce7] | 154 | goto CLEANUP;
|
|---|
| 155 | } // if
|
|---|
| [ea6332d] | 156 |
|
|---|
| [f6582252] | 157 | // 'u' can appear before or after length suffix
|
|---|
| 158 | if ( str.find_last_of( "uU" ) != string::npos ) Unsigned = true;
|
|---|
| 159 |
|
|---|
| 160 | if ( isdigit( str[str.length() - 1] ) ) { // no suffix ?
|
|---|
| [f56c32e] | 161 | lnthSuffix( str, type, ltype ); // could have length suffix
|
|---|
| 162 | } else {
|
|---|
| [f9a0dd0] | 163 | // At least one digit in integer constant, so safe to backup while looking for suffix. This
|
|---|
| 164 | // declaration and the comma expressions in the conditions mimic the declare and check
|
|---|
| 165 | // pattern allowed in later compiler versions. (Only some early compilers/C++ standards do
|
|---|
| 166 | // not support it.)
|
|---|
| [15596d7] | 167 | string::size_type posn;
|
|---|
| 168 | // pointer value
|
|---|
| [52a2248] | 169 | if ( posn = str.find_last_of( "pP" ), posn != string::npos ) {
|
|---|
| [15596d7] | 170 | ltype = 5; str.erase( posn, 1 );
|
|---|
| [0d0931d] | 171 | // size_t
|
|---|
| [52a2248] | 172 | } else if ( posn = str.find_last_of( "zZ" ), posn != string::npos ) {
|
|---|
| [15596d7] | 173 | Unsigned = true; type = 2; ltype = 4; str.erase( posn, 1 );
|
|---|
| [0d0931d] | 174 | // signed char
|
|---|
| [52a2248] | 175 | } else if ( posn = str.rfind( "hh" ), posn != string::npos ) {
|
|---|
| [15596d7] | 176 | type = 1; str.erase( posn, 2 );
|
|---|
| [0d0931d] | 177 | // signed char
|
|---|
| [52a2248] | 178 | } else if ( posn = str.rfind( "HH" ), posn != string::npos ) {
|
|---|
| [15596d7] | 179 | type = 1; str.erase( posn, 2 );
|
|---|
| [0d0931d] | 180 | // short
|
|---|
| [52a2248] | 181 | } else if ( posn = str.find_last_of( "hH" ), posn != string::npos ) {
|
|---|
| [15596d7] | 182 | type = 0; str.erase( posn, 1 );
|
|---|
| [0d0931d] | 183 | // int (natural number)
|
|---|
| [52a2248] | 184 | } else if ( posn = str.find_last_of( "nN" ), posn != string::npos ) {
|
|---|
| [15596d7] | 185 | type = 2; str.erase( posn, 1 );
|
|---|
| 186 | } else if ( str.rfind( "ll" ) != string::npos || str.rfind( "LL" ) != string::npos ) {
|
|---|
| 187 | type = 4;
|
|---|
| 188 | } else {
|
|---|
| 189 | lnthSuffix( str, type, ltype );
|
|---|
| 190 | } // if
|
|---|
| [7bf7fb9] | 191 | } // if
|
|---|
| 192 |
|
|---|
| [f6582252] | 193 | // Cannot be just "0"/"1"; sscanf stops at the suffix, if any; value goes over the wall => always generate
|
|---|
| [dbe8e31c] | 194 |
|
|---|
| [cf5af9c] | 195 | #if ! defined(__SIZEOF_INT128__)
|
|---|
| [ca9d65e] | 196 | if ( type == 5 ) SemanticError( yylloc, "int128 constant is not supported on this target \"%s\"", str.c_str() );
|
|---|
| [cf5af9c] | 197 | #endif // ! __SIZEOF_INT128__
|
|---|
| [702e826] | 198 |
|
|---|
| [f6582252] | 199 | if ( str[0] == '0' ) { // radix character ?
|
|---|
| 200 | dec = false;
|
|---|
| 201 | if ( checkX( str[1] ) ) { // hex constant ?
|
|---|
| 202 | if ( type < 5 ) { // not L128 ?
|
|---|
| 203 | sscanf( (char *)str.c_str(), "%llx", &v );
|
|---|
| [c5b55c4] | 204 | #if defined(__SIZEOF_INT128__)
|
|---|
| [f6582252] | 205 | } else { // hex int128 constant
|
|---|
| 206 | unsigned int len = str.length();
|
|---|
| [ca9d65e] | 207 | if ( len > (2 + 16 + 16) ) SemanticError( yylloc, "128-bit hexadecimal constant to large \"%s\"", str.c_str() );
|
|---|
| [702e826] | 208 | // hex digits < 2^64
|
|---|
| 209 | if ( len > (2 + 16) ) {
|
|---|
| 210 | str2 = "0x" + str.substr( len - 16 );
|
|---|
| 211 | sscanf( (char *)str2.c_str(), "%llx", &v2 );
|
|---|
| 212 | str = str.substr( 0, len - 16 );
|
|---|
| [15596d7] | 213 | } // if
|
|---|
| [f6582252] | 214 | sscanf( (char *)str.c_str(), "%llx", &v );
|
|---|
| [c5b55c4] | 215 | #endif // __SIZEOF_INT128__
|
|---|
| [f6582252] | 216 | } // if
|
|---|
| 217 | //printf( "%llx %llu\n", v, v );
|
|---|
| 218 | } else if ( checkB( str[1] ) ) { // binary constant ?
|
|---|
| [c5b55c4] | 219 | #if defined(__SIZEOF_INT128__)
|
|---|
| [013b028] | 220 | unsigned int len = str.length();
|
|---|
| [f6582252] | 221 | if ( type == 5 && len > 2 + 64 ) {
|
|---|
| [ca9d65e] | 222 | if ( len > 2 + 64 + 64 ) SemanticError( yylloc, "128-bit binary constant to large \"%s\".", str.c_str() );
|
|---|
| [f6582252] | 223 | str2 = "0b" + str.substr( len - 64 );
|
|---|
| 224 | str = str.substr( 0, len - 64 );
|
|---|
| 225 | scanbin( str2, v2 );
|
|---|
| 226 | } // if
|
|---|
| [c5b55c4] | 227 | #endif // __SIZEOF_INT128__
|
|---|
| [f6582252] | 228 | scanbin( str, v );
|
|---|
| 229 | //printf( "%#llx %llu\n", v, v );
|
|---|
| 230 | } else { // octal constant
|
|---|
| 231 | if ( type < 5 ) { // not L128 ?
|
|---|
| 232 | sscanf( (char *)str.c_str(), "%llo", &v );
|
|---|
| [cf5af9c] | 233 | #if defined(__SIZEOF_INT128__)
|
|---|
| [f6582252] | 234 | } else { // octal int128 constant
|
|---|
| 235 | unsigned int len = str.length();
|
|---|
| [ca9d65e] | 236 | if ( len > 1 + 43 || (len == 1 + 43 && str[0] > '3') ) SemanticError( yylloc, "128-bit octal constant to large \"%s\"", str.c_str() );
|
|---|
| [c5b55c4] | 237 | char buf[32];
|
|---|
| [f6582252] | 238 | if ( len <= 1 + 21 ) { // value < 21 octal digitis
|
|---|
| [c5b55c4] | 239 | sscanf( (char *)str.c_str(), "%llo", &v );
|
|---|
| [f6582252] | 240 | } else {
|
|---|
| 241 | sscanf( &str[len - 21], "%llo", &v );
|
|---|
| [791028a] | 242 | __int128 val = v; // accumulate bits
|
|---|
| [f6582252] | 243 | str[len - 21] ='\0'; // shorten string
|
|---|
| 244 | sscanf( &str[len == 43 ? 1 : 0], "%llo", &v );
|
|---|
| 245 | val |= (__int128)v << 63; // store bits
|
|---|
| 246 | if ( len == 1 + 43 ) { // most significant 2 bits ?
|
|---|
| 247 | str[2] = '\0'; // shorten string
|
|---|
| 248 | sscanf( &str[1], "%llo", &v ); // process most significant 2 bits
|
|---|
| 249 | val |= (__int128)v << 126; // store bits
|
|---|
| 250 | } // if
|
|---|
| 251 | v = val >> 64; v2 = (uint64_t)val; // replace octal constant with 2 hex constants
|
|---|
| 252 | sprintf( buf, "%#llx", v2 );
|
|---|
| 253 | str2 = buf;
|
|---|
| 254 | } // if
|
|---|
| [c5b55c4] | 255 | sprintf( buf, "%#llx", v );
|
|---|
| 256 | str = buf;
|
|---|
| [cf5af9c] | 257 | #endif // __SIZEOF_INT128__
|
|---|
| [f6582252] | 258 | } // if
|
|---|
| 259 | //printf( "%#llo %llu\n", v, v );
|
|---|
| 260 | } // if
|
|---|
| 261 | } else { // decimal constant ?
|
|---|
| 262 | if ( type < 5 ) { // not L128 ?
|
|---|
| 263 | sscanf( (char *)str.c_str(), "%llu", &v );
|
|---|
| [cf5af9c] | 264 | #if defined(__SIZEOF_INT128__)
|
|---|
| [f6582252] | 265 | } else { // decimal int128 constant
|
|---|
| 266 | #define P10_UINT64 10'000'000'000'000'000'000ULL // 19 zeroes
|
|---|
| 267 | unsigned int len = str.length();
|
|---|
| 268 | if ( str.length() == 39 && str > (Unsigned ? "340282366920938463463374607431768211455" : "170141183460469231731687303715884105727") )
|
|---|
| [ca9d65e] | 269 | SemanticError( yylloc, "128-bit decimal constant to large \"%s\".", str.c_str() );
|
|---|
| [c5b55c4] | 270 | char buf[32];
|
|---|
| [f6582252] | 271 | if ( len <= 19 ) { // value < 19 decimal digitis
|
|---|
| [c5b55c4] | 272 | sscanf( (char *)str.c_str(), "%llu", &v );
|
|---|
| [f6582252] | 273 | } else {
|
|---|
| 274 | sscanf( &str[len - 19], "%llu", &v );
|
|---|
| [791028a] | 275 | __int128 val = v; // accumulate bits
|
|---|
| [f6582252] | 276 | str[len - 19] ='\0'; // shorten string
|
|---|
| 277 | sscanf( &str[len == 39 ? 1 : 0], "%llu", &v );
|
|---|
| 278 | val += (__int128)v * (__int128)P10_UINT64; // store bits
|
|---|
| 279 | if ( len == 39 ) { // most significant 2 bits ?
|
|---|
| 280 | str[1] = '\0'; // shorten string
|
|---|
| 281 | sscanf( &str[0], "%llu", &v ); // process most significant 2 bits
|
|---|
| 282 | val += (__int128)v * (__int128)P10_UINT64 * (__int128)P10_UINT64; // store bits
|
|---|
| 283 | } // if
|
|---|
| 284 | v = val >> 64; v2 = (uint64_t)val; // replace decimal constant with 2 hex constants
|
|---|
| 285 | sprintf( buf, "%#llx", v2 );
|
|---|
| 286 | str2 = buf;
|
|---|
| 287 | } // if
|
|---|
| [c5b55c4] | 288 | sprintf( buf, "%#llx", v );
|
|---|
| 289 | str = buf;
|
|---|
| [cf5af9c] | 290 | #endif // __SIZEOF_INT128__
|
|---|
| [f6582252] | 291 | } // if
|
|---|
| 292 | //printf( "%llu\n", v );
|
|---|
| 293 | } // if
|
|---|
| 294 |
|
|---|
| 295 | if ( type == -1 ) { // no suffix => determine type from value size
|
|---|
| 296 | valueToType( v, dec, type, Unsigned );
|
|---|
| 297 | } // if
|
|---|
| 298 | /* printf( "%s %llo %s %llo\n", str.c_str(), v, str2.c_str(), v2 ); */
|
|---|
| 299 |
|
|---|
| [0a616e0] | 300 | //if ( !( 0 <= type && type <= 6 ) ) { printf( "%s %lu %d %s\n", fred.c_str(), fred.length(), type, str.c_str() ); }
|
|---|
| 301 | assert( 0 <= type && type <= 6 );
|
|---|
| [f56c32e] | 302 |
|
|---|
| [a6c5d7c] | 303 | // Constant type is correct for overload resolving.
|
|---|
| [2737b37] | 304 | ret = new ast::ConstantExpr( location, new ast::BasicType( kind[Unsigned][type] ), str, v );
|
|---|
| [ba01b14] | 305 | if ( Unsigned && type < 2 ) { // hh or h, less than int ?
|
|---|
| [201aeb9] | 306 | // int i = -1uh => 65535 not -1, so cast is necessary for unsigned, which unfortunately eliminates warnings for large values.
|
|---|
| [2737b37] | 307 | ret = new ast::CastExpr( location, ret, new ast::BasicType( kind[Unsigned][type] ), ast::ExplicitCast );
|
|---|
| [ba01b14] | 308 | } else if ( ltype != -1 ) { // explicit length ?
|
|---|
| [0a616e0] | 309 | if ( ltype == 6 ) { // int128, (int128)constant
|
|---|
| [2737b37] | 310 | ret2 = new ast::ConstantExpr( location, new ast::BasicType( ast::BasicKind::LongLongSignedInt ), str2, v2 );
|
|---|
| [bb7422a] | 311 | ret = build_compoundLiteral( location,
|
|---|
| [2737b37] | 312 | DeclarationNode::newFromTypeData( addType( build_basic_type( TypeData::Int128 ), build_signedness( TypeData::Unsigned ) ) ),
|
|---|
| 313 | new InitializerNode( (new InitializerNode( new ExpressionNode( v2 == 0 ? ret2 : ret ) ))->set_last( new InitializerNode( new ExpressionNode( v2 == 0 ? ret : ret2 ) ) ), true )
|
|---|
| [bb7422a] | 314 | );
|
|---|
| [0a616e0] | 315 | } else { // explicit length, (length_type)constant
|
|---|
| [2737b37] | 316 | ret = new ast::CastExpr( location, ret, new ast::TypeInstType( lnthsInt[Unsigned][ltype], ast::TypeDecl::Dtype ), ast::ExplicitCast );
|
|---|
| [3d8d7a7] | 317 | if ( ltype == 5 ) { // pointer, intptr( (uintptr_t)constant )
|
|---|
| [2737b37] | 318 | ret = build_func( location, new ExpressionNode( build_varref( location, new string( "intptr" ) ) ), new ExpressionNode( ret ) );
|
|---|
| [0a616e0] | 319 | } // if
|
|---|
| [201aeb9] | 320 | } // if
|
|---|
| [7b1d5ec] | 321 | } // if
|
|---|
| [e8ccca3] | 322 |
|
|---|
| [f56c32e] | 323 | CLEANUP: ;
|
|---|
| [ab57786] | 324 | delete &str; // created by lex
|
|---|
| 325 | return ret;
|
|---|
| [7bf7fb9] | 326 | } // build_constantInteger
|
|---|
| [51b73452] | 327 |
|
|---|
| [201aeb9] | 328 |
|
|---|
| [ba01b14] | 329 | static inline void checkFnxFloat( string & str, size_t last, bool & explnth, int & type ) {
|
|---|
| 330 | string::size_type posn;
|
|---|
| 331 | // floating-point constant has minimum of 2 characters, 1. or .1, so safe to look ahead
|
|---|
| 332 | if ( str[1] == 'x' ) { // hex ?
|
|---|
| 333 | posn = str.find_last_of( "pP" ); // back for exponent (must have)
|
|---|
| 334 | posn = str.find_first_of( "fF", posn + 1 ); // forward for size (fF allowed in hex constant)
|
|---|
| 335 | } else {
|
|---|
| 336 | posn = str.find_last_of( "fF" ); // back for size (fF not allowed)
|
|---|
| 337 | } // if
|
|---|
| [201aeb9] | 338 | if ( posn == string::npos ) return;
|
|---|
| [ba01b14] | 339 | explnth = true;
|
|---|
| [201aeb9] | 340 | posn += 1; // advance to size
|
|---|
| 341 | if ( str[posn] == '3' ) { // 32
|
|---|
| [ba01b14] | 342 | if ( str[last] != 'x' ) type = 6;
|
|---|
| 343 | else type = 7;
|
|---|
| [201aeb9] | 344 | } else if ( str[posn] == '6' ) { // 64
|
|---|
| [ba01b14] | 345 | if ( str[last] != 'x' ) type = 8;
|
|---|
| 346 | else type = 9;
|
|---|
| 347 | } else if ( str[posn] == '8' ) { // 80
|
|---|
| 348 | type = 3;
|
|---|
| 349 | } else if ( str[posn] == '1' ) { // 16/128
|
|---|
| 350 | if ( str[posn + 1] == '6' ) { // 16
|
|---|
| 351 | type = 5;
|
|---|
| 352 | } else { // 128
|
|---|
| 353 | if ( str[last] != 'x' ) type = 10;
|
|---|
| 354 | else type = 11;
|
|---|
| 355 | } // if
|
|---|
| [201aeb9] | 356 | } else {
|
|---|
| 357 | assertf( false, "internal error, bad floating point length %s", str.c_str() );
|
|---|
| 358 | } // if
|
|---|
| [ba01b14] | 359 | } // checkFnxFloat
|
|---|
| [201aeb9] | 360 |
|
|---|
| 361 |
|
|---|
| [2737b37] | 362 | ast::Expr * build_constantFloat( const CodeLocation & location, string & str ) {
|
|---|
| [7a780ad] | 363 | static const ast::BasicKind kind[2][12] = {
|
|---|
| [2ac78a1] | 364 | { 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 },
|
|---|
| 365 | { 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 },
|
|---|
| [7bf7fb9] | 366 | };
|
|---|
| [59c24b6] | 367 |
|
|---|
| [ba01b14] | 368 | // floating-point constant has minimum of 2 characters 1. or .1
|
|---|
| [7bf7fb9] | 369 | size_t last = str.length() - 1;
|
|---|
| [d56e5bc] | 370 | double v;
|
|---|
| [ba01b14] | 371 | int type; // 0 => float, 1 => double, 3 => long double, ...
|
|---|
| 372 | bool complx = false; // real, complex
|
|---|
| 373 | bool explnth = false; // explicit literal length
|
|---|
| [d56e5bc] | 374 |
|
|---|
| 375 | sscanf( str.c_str(), "%lg", &v );
|
|---|
| [51b73452] | 376 |
|
|---|
| [7bf7fb9] | 377 | if ( checkI( str[last] ) ) { // imaginary ?
|
|---|
| 378 | complx = true;
|
|---|
| 379 | last -= 1; // backup one character
|
|---|
| 380 | } // if
|
|---|
| [0caaa6a] | 381 |
|
|---|
| [7bf7fb9] | 382 | if ( checkF( str[last] ) ) { // float ?
|
|---|
| [ba01b14] | 383 | type = 0;
|
|---|
| [7bf7fb9] | 384 | } else if ( checkD( str[last] ) ) { // double ?
|
|---|
| [ba01b14] | 385 | type = 1;
|
|---|
| [7bf7fb9] | 386 | } else if ( checkL( str[last] ) ) { // long double ?
|
|---|
| [ba01b14] | 387 | type = 2;
|
|---|
| 388 | } else if ( checkF80( str[last] ) ) { // __float80 ?
|
|---|
| 389 | type = 3;
|
|---|
| 390 | } else if ( checkF128( str[last] ) ) { // __float128 ?
|
|---|
| 391 | type = 4;
|
|---|
| [201aeb9] | 392 | } else {
|
|---|
| [ba01b14] | 393 | type = 1; // double (default if no suffix)
|
|---|
| 394 | checkFnxFloat( str, last, explnth, type );
|
|---|
| [7bf7fb9] | 395 | } // if
|
|---|
| [ba01b14] | 396 |
|
|---|
| [7bf7fb9] | 397 | if ( ! complx && checkI( str[last - 1] ) ) { // imaginary ?
|
|---|
| 398 | complx = true;
|
|---|
| 399 | } // if
|
|---|
| [51b73452] | 400 |
|
|---|
| [ba01b14] | 401 | assert( 0 <= type && type < 12 );
|
|---|
| [2737b37] | 402 | ast::Expr * ret = new ast::ConstantExpr( location, new ast::BasicType( kind[complx][type] ), str, v );
|
|---|
| [bb7422a] | 403 | // explicit length ?
|
|---|
| 404 | if ( explnth ) {
|
|---|
| [2737b37] | 405 | ret = new ast::CastExpr( location, ret, new ast::BasicType( kind[complx][type] ), ast::ExplicitCast );
|
|---|
| [201aeb9] | 406 | } // if
|
|---|
| [76c62b2] | 407 |
|
|---|
| [ab57786] | 408 | delete &str; // created by lex
|
|---|
| 409 | return ret;
|
|---|
| [7bf7fb9] | 410 | } // build_constantFloat
|
|---|
| [51b73452] | 411 |
|
|---|
| [76c62b2] | 412 | static void sepString( string & str, string & units, char delimit ) {
|
|---|
| 413 | string::size_type posn = str.find_last_of( delimit ) + 1;
|
|---|
| 414 | if ( posn != str.length() ) {
|
|---|
| [e8ccca3] | 415 | units = "?" + str.substr( posn ); // extract units
|
|---|
| [76c62b2] | 416 | str.erase( posn ); // remove units
|
|---|
| 417 | } // if
|
|---|
| 418 | } // sepString
|
|---|
| 419 |
|
|---|
| [41c3e46] | 420 | static ast::Type * charstrKind( string & str ) {
|
|---|
| 421 | ast::Type * kind;
|
|---|
| 422 | switch ( str[0] ) { // str has >= 2 characters, i.e, null string "" => safe to look at subscripts 0/1
|
|---|
| [2737b37] | 423 | case 'u':
|
|---|
| [41c3e46] | 424 | if ( str[1] == '8' ) goto Default; // utf-8 characters => array of char (save check for char as no 8 allowed)
|
|---|
| 425 | // lookup type of associated typedef
|
|---|
| 426 | kind = new ast::TypeInstType( "char16_t", ast::TypeDecl::Dtype );
|
|---|
| 427 | break;
|
|---|
| [2737b37] | 428 | case 'U':
|
|---|
| [41c3e46] | 429 | kind = new ast::TypeInstType( "char32_t", ast::TypeDecl::Dtype );
|
|---|
| 430 | break;
|
|---|
| [2737b37] | 431 | case 'L':
|
|---|
| [41c3e46] | 432 | kind = new ast::TypeInstType( "wchar_t", ast::TypeDecl::Dtype );
|
|---|
| 433 | break;
|
|---|
| [2737b37] | 434 | Default: // char default string type
|
|---|
| 435 | default:
|
|---|
| [41c3e46] | 436 | kind = new ast::BasicType( ast::BasicKind::Char );
|
|---|
| 437 | } // switch
|
|---|
| 438 | return kind;
|
|---|
| 439 | } // charstrKind
|
|---|
| [51b73452] | 440 |
|
|---|
| [20c2ade] | 441 | static bool isoctal( char ch ) {
|
|---|
| 442 | return ('0' <= ch && ch <= '7');
|
|---|
| [41c3e46] | 443 | } // isoctal
|
|---|
| [20c2ade] | 444 |
|
|---|
| [f9a0dd0] | 445 | // A "sequence" is the series of characters in a character/string literal that becomes a single
|
|---|
| 446 | // character value in the runtime value.
|
|---|
| [20c2ade] | 447 | static size_t sequenceLength( const std::string & str, size_t pos ) {
|
|---|
| 448 | // Most "sequences" are just a single character, filter those out:
|
|---|
| 449 | if ( '\\' != str[pos] ) return 1;
|
|---|
| 450 | switch ( str[pos + 1] ) {
|
|---|
| [5b95e67] | 451 | // Simple Escape Sequence (\_ where _ is one of the following):
|
|---|
| 452 | case '\'': case '\"': case '?': case '\\':
|
|---|
| 453 | case 'a': case 'b': case 'f': case 'n': case 'r': case 't': case 'v':
|
|---|
| 454 | // GCC Escape Sequence (as simple, just some different letters):
|
|---|
| 455 | case 'e':
|
|---|
| [20c2ade] | 456 | return 2;
|
|---|
| [5b95e67] | 457 | // Numeric Escape Sequence (\___ where _ is 1-3 octal digits):
|
|---|
| 458 | case '0': case '1': case '2': case '3':
|
|---|
| 459 | case '4': case '5': case '6': case '7':
|
|---|
| [20c2ade] | 460 | return ( !isoctal( str[pos + 2] ) ) ? 2 :
|
|---|
| [5b95e67] | 461 | ( !isoctal( str[pos + 3] ) ) ? 3 : 4;
|
|---|
| 462 | // Numeric Escape Sequence (\x_ where _ is 1 or more hexadecimal digits):
|
|---|
| 463 | case 'x': {
|
|---|
| 464 | size_t length = 2;
|
|---|
| [2ac78a1] | 465 | while ( isxdigit( str[pos + length] ) ) ++length;
|
|---|
| [5b95e67] | 466 | return length;
|
|---|
| 467 | }
|
|---|
| 468 | // Universal Character Name (\u____ where _ is 4 decimal digits):
|
|---|
| 469 | case 'u':
|
|---|
| [20c2ade] | 470 | return 6;
|
|---|
| [5b95e67] | 471 | // Universal Character Name (\U________ where _ is 8 decimal digits):
|
|---|
| 472 | case 'U':
|
|---|
| [20c2ade] | 473 | return 10;
|
|---|
| [5b95e67] | 474 | default:
|
|---|
| [3181a33] | 475 | SemanticError( yylloc, "Unknown escape sequence \"\\%c\"", str[pos + 1] );
|
|---|
| [20c2ade] | 476 | return 1;
|
|---|
| [f9a0dd0] | 477 | } // switch
|
|---|
| [41c3e46] | 478 | } // sequenceLength
|
|---|
| 479 |
|
|---|
| 480 | ast::Expr * build_constantChar( const CodeLocation & location, string & str ) {
|
|---|
| 481 | string units; // units
|
|---|
| 482 | sepString( str, units, '\'' ); // separate constant from units
|
|---|
| 483 |
|
|---|
| 484 | ast::Expr * ret = new ast::ConstantExpr( location, charstrKind( str ), str, (unsigned long long int)(unsigned char)str[1] );
|
|---|
| 485 | if ( units.length() != 0 ) {
|
|---|
| 486 | ret = new ast::UntypedExpr( location, new ast::NameExpr( location, units ), { ret } );
|
|---|
| 487 | } // if
|
|---|
| 488 |
|
|---|
| 489 | delete &str; // created by lex
|
|---|
| 490 | return ret;
|
|---|
| 491 | } // build_constantChar
|
|---|
| [20c2ade] | 492 |
|
|---|
| [f9a0dd0] | 493 | ast::Expr * build_constantStr( const CodeLocation & location, string & str ) {
|
|---|
| [6e3eaa57] | 494 | assert( str.length() > 0 );
|
|---|
| [76c62b2] | 495 | string units; // units
|
|---|
| 496 | sepString( str, units, '"' ); // separate constant from units
|
|---|
| 497 |
|
|---|
| [f9a0dd0] | 498 | // The length value of the type is equal to the number of "sequences" not including the openning
|
|---|
| 499 | // and closing quotes in the literal plus 1 for the implicit null terminator.
|
|---|
| 500 | size_t length = 1;
|
|---|
| 501 | for ( size_t pos = 1 ; pos < str.size() - 1 ; pos += sequenceLength( str, pos ) ) {
|
|---|
| 502 | if ( '"' == str[pos] ) { // concatenated strings ? "ABC" "DEF"
|
|---|
| 503 | int cnt = 1; // skip outside quotes and space between
|
|---|
| 504 | for ( unsigned int i = pos + 1; str[i] != '"'; i += 1, cnt += 1 );
|
|---|
| 505 | pos += cnt;
|
|---|
| 506 | continue; // not part of length
|
|---|
| 507 | } // if
|
|---|
| 508 | length += 1;
|
|---|
| 509 | } // for
|
|---|
| [20c2ade] | 510 |
|
|---|
| [41c3e46] | 511 | ast::ArrayType * at = new ast::ArrayType( charstrKind( str ), ast::ConstantExpr::from_ulong( location, length ), ast::FixedLen, ast::DynamicDim );
|
|---|
| [bb7422a] | 512 | ast::Expr * ret = new ast::ConstantExpr( location, at, str, std::nullopt );
|
|---|
| [e612146c] | 513 | if ( units.length() != 0 ) {
|
|---|
| [f9a0dd0] | 514 | ret = new ast::UntypedExpr( location, new ast::NameExpr( location, units ), { ret } );
|
|---|
| [e612146c] | 515 | } // if
|
|---|
| [5809461] | 516 |
|
|---|
| [ab57786] | 517 | delete &str; // created by lex
|
|---|
| 518 | return ret;
|
|---|
| [7bf7fb9] | 519 | } // build_constantStr
|
|---|
| [51b73452] | 520 |
|
|---|
| [2737b37] | 521 | ast::Expr * build_field_name_FLOATING_FRACTIONconstant( const CodeLocation & location, const string & str ) {
|
|---|
| [ca9d65e] | 522 | if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) SemanticError( yylloc, "invalid tuple index \"%s\".", str.c_str() );
|
|---|
| [2737b37] | 523 | ast::Expr * ret = build_constantInteger( location, *new string( str.substr(1) ) );
|
|---|
| [930f69e] | 524 | delete &str;
|
|---|
| 525 | return ret;
|
|---|
| 526 | } // build_field_name_FLOATING_FRACTIONconstant
|
|---|
| 527 |
|
|---|
| [2737b37] | 528 | ast::Expr * build_field_name_FLOATING_DECIMALconstant( const CodeLocation & location, const string & str ) {
|
|---|
| [ca9d65e] | 529 | if ( str[str.size() - 1] != '.' ) SemanticError( yylloc, "invalid tuple index \"%s\".", str.c_str() );
|
|---|
| [2737b37] | 530 | ast::Expr * ret = build_constantInteger( location, *new string( str.substr( 0, str.size()-1 ) ) );
|
|---|
| [930f69e] | 531 | delete &str;
|
|---|
| 532 | return ret;
|
|---|
| 533 | } // build_field_name_FLOATING_DECIMALconstant
|
|---|
| 534 |
|
|---|
| [2737b37] | 535 | ast::Expr * build_field_name_FLOATINGconstant( const CodeLocation & location, const string & str ) {
|
|---|
| [8780e30] | 536 | // str is of the form A.B -> separate at the . and return member expression
|
|---|
| 537 | int a, b;
|
|---|
| 538 | char dot;
|
|---|
| [f43df73] | 539 | stringstream ss( str );
|
|---|
| [8780e30] | 540 | ss >> a >> dot >> b;
|
|---|
| [2737b37] | 541 | auto ret = new ast::UntypedMemberExpr( location, ast::ConstantExpr::from_int( location, b ), ast::ConstantExpr::from_int( location, a )
|
|---|
| [bb7422a] | 542 | );
|
|---|
| [8780e30] | 543 | delete &str;
|
|---|
| 544 | return ret;
|
|---|
| 545 | } // build_field_name_FLOATINGconstant
|
|---|
| 546 |
|
|---|
| [2737b37] | 547 | ast::Expr * make_field_name_fraction_constants( const CodeLocation & location, ast::Expr * fieldName, ast::Expr * fracts ) {
|
|---|
| [bb7422a] | 548 | if ( nullptr == fracts ) {
|
|---|
| 549 | return fieldName;
|
|---|
| 550 | } else if ( auto memberExpr = dynamic_cast<ast::UntypedMemberExpr *>( fracts ) ) {
|
|---|
| [2737b37] | 551 | memberExpr->member = make_field_name_fraction_constants( location, fieldName, ast::mutate( memberExpr->aggregate.get() ) );
|
|---|
| [bb7422a] | 552 | return memberExpr;
|
|---|
| 553 | } else {
|
|---|
| 554 | return new ast::UntypedMemberExpr( location, fracts, fieldName );
|
|---|
| [f43df73] | 555 | } // if
|
|---|
| [8780e30] | 556 | } // make_field_name_fraction_constants
|
|---|
| 557 |
|
|---|
| [2737b37] | 558 | ast::Expr * build_field_name_fraction_constants( const CodeLocation & location, ast::Expr * fieldName, ExpressionNode * fracts ) {
|
|---|
| [bb7422a] | 559 | return make_field_name_fraction_constants( location, fieldName, maybeMoveBuild( fracts ) );
|
|---|
| [8780e30] | 560 | } // build_field_name_fraction_constants
|
|---|
| 561 |
|
|---|
| [2737b37] | 562 | ast::NameExpr * build_varref( const CodeLocation & location, const string * name ) {
|
|---|
| [bb7422a] | 563 | ast::NameExpr * expr = new ast::NameExpr( location, *name );
|
|---|
| [7ecbb7e] | 564 | delete name;
|
|---|
| 565 | return expr;
|
|---|
| [a2e0687] | 566 | } // build_varref
|
|---|
| [51b1202] | 567 |
|
|---|
| [2737b37] | 568 | ast::QualifiedNameExpr * build_qualified_expr( const CodeLocation & location, const DeclarationNode * decl_node, const ast::NameExpr * name ) {
|
|---|
| [bb7422a] | 569 | ast::Decl * newDecl = maybeBuild( decl_node );
|
|---|
| 570 | if ( ast::DeclWithType * newDeclWithType = dynamic_cast<ast::DeclWithType *>( newDecl ) ) {
|
|---|
| 571 | if ( const ast::Type * t = newDeclWithType->get_type() ) {
|
|---|
| 572 | if ( auto typeInst = dynamic_cast<const ast::TypeInstType *>( t ) ) {
|
|---|
| 573 | newDecl = new ast::EnumDecl( location, typeInst->name );
|
|---|
| [b0d9ff7] | 574 | }
|
|---|
| 575 | }
|
|---|
| 576 | }
|
|---|
| [bb7422a] | 577 | return new ast::QualifiedNameExpr( location, newDecl, name->name );
|
|---|
| [b0d9ff7] | 578 | }
|
|---|
| 579 |
|
|---|
| [2737b37] | 580 | ast::QualifiedNameExpr * build_qualified_expr( const CodeLocation & location, const ast::EnumDecl * decl, const ast::NameExpr * name ) {
|
|---|
| [bb7422a] | 581 | return new ast::QualifiedNameExpr( location, decl, name->name );
|
|---|
| [4e2befe3] | 582 | }
|
|---|
| 583 |
|
|---|
| [2737b37] | 584 | ast::DimensionExpr * build_dimensionref( const CodeLocation & location, const string * name ) {
|
|---|
| [bb7422a] | 585 | ast::DimensionExpr * expr = new ast::DimensionExpr( location, *name );
|
|---|
| [6e50a6b] | 586 | delete name;
|
|---|
| 587 | return expr;
|
|---|
| 588 | } // build_varref
|
|---|
| [ea54f1e] | 589 |
|
|---|
| [5809461] | 590 | // TODO: get rid of this and OperKinds and reuse code from OperatorTable
|
|---|
| [a2e0687] | 591 | static const char * OperName[] = { // must harmonize with OperKinds
|
|---|
| [5721a6d] | 592 | // diadic
|
|---|
| [e5f2a67] | 593 | "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&",
|
|---|
| [5721a6d] | 594 | "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
|
|---|
| [e5f2a67] | 595 | "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
|
|---|
| [d9e2280] | 596 | "?[?]", "...",
|
|---|
| [5721a6d] | 597 | // monadic
|
|---|
| [5809461] | 598 | "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--",
|
|---|
| [a2e0687] | 599 | }; // OperName
|
|---|
| [5721a6d] | 600 |
|
|---|
| [2737b37] | 601 | ast::Expr * build_cast( const CodeLocation & location, DeclarationNode * decl_node, ExpressionNode * expr_node, ast::CastKind kind ) {
|
|---|
| [bb7422a] | 602 | ast::Type * targetType = maybeMoveBuildType( decl_node );
|
|---|
| 603 | if ( dynamic_cast<ast::VoidType *>( targetType ) ) {
|
|---|
| [064e3ff] | 604 | delete targetType;
|
|---|
| [2737b37] | 605 | return new ast::CastExpr( location, maybeMoveBuild( expr_node ), ast::ExplicitCast, kind );
|
|---|
| [064e3ff] | 606 | } else {
|
|---|
| [2737b37] | 607 | return new ast::CastExpr( location, maybeMoveBuild( expr_node ), targetType, ast::ExplicitCast, kind );
|
|---|
| [064e3ff] | 608 | } // if
|
|---|
| [a2e0687] | 609 | } // build_cast
|
|---|
| [a5f0529] | 610 |
|
|---|
| [2737b37] | 611 | ast::Expr * build_keyword_cast( const CodeLocation & location, ast::AggregateDecl::Aggregate target, ExpressionNode * expr_node ) {
|
|---|
| 612 | return new ast::KeywordCastExpr( location, maybeMoveBuild( expr_node ), target );
|
|---|
| [9a705dc8] | 613 | }
|
|---|
| 614 |
|
|---|
| [2737b37] | 615 | ast::Expr * build_virtual_cast( const CodeLocation & location, DeclarationNode * decl_node, ExpressionNode * expr_node ) {
|
|---|
| 616 | return new ast::VirtualCastExpr( location, maybeMoveBuild( expr_node ), maybeMoveBuildType( decl_node ) );
|
|---|
| [a2e0687] | 617 | } // build_virtual_cast
|
|---|
| [064e3ff] | 618 |
|
|---|
| [2737b37] | 619 | ast::Expr * build_fieldSel( const CodeLocation & location, ExpressionNode * expr_node, ast::Expr * member ) {
|
|---|
| 620 | return new ast::UntypedMemberExpr( location, member, maybeMoveBuild( expr_node ) );
|
|---|
| [a2e0687] | 621 | } // build_fieldSel
|
|---|
| [064e3ff] | 622 |
|
|---|
| [2737b37] | 623 | ast::Expr * build_pfieldSel( const CodeLocation & location, ExpressionNode * expr_node, ast::Expr * member ) {
|
|---|
| 624 | auto deref = new ast::UntypedExpr( location, new ast::NameExpr( location, "*?" ) );
|
|---|
| [64ac636] | 625 | deref->location = expr_node->location;
|
|---|
| [bb7422a] | 626 | deref->args.push_back( maybeMoveBuild( expr_node ) );
|
|---|
| 627 | auto ret = new ast::UntypedMemberExpr( location, member, deref );
|
|---|
| [064e3ff] | 628 | return ret;
|
|---|
| [a2e0687] | 629 | } // build_pfieldSel
|
|---|
| [064e3ff] | 630 |
|
|---|
| [2737b37] | 631 | ast::Expr * build_offsetOf( const CodeLocation & location, DeclarationNode * decl_node, ast::NameExpr * member ) {
|
|---|
| 632 | ast::Expr * ret = new ast::UntypedOffsetofExpr( location, maybeMoveBuildType( decl_node ), member->name );
|
|---|
| [7a780ad] | 633 | ret->result = new ast::BasicType( ast::BasicKind::LongUnsignedInt );
|
|---|
| [ac71a86] | 634 | delete member;
|
|---|
| 635 | return ret;
|
|---|
| [a2e0687] | 636 | } // build_offsetOf
|
|---|
| [064e3ff] | 637 |
|
|---|
| [2737b37] | 638 | ast::Expr * build_and_or( const CodeLocation & location, ExpressionNode * expr_node1, ExpressionNode * expr_node2, ast::LogicalFlag flag ) {
|
|---|
| 639 | return new ast::LogicalExpr( location, maybeMoveBuild( expr_node1 ), maybeMoveBuild( expr_node2 ), flag );
|
|---|
| [a2e0687] | 640 | } // build_and_or
|
|---|
| [51e076e] | 641 |
|
|---|
| [2737b37] | 642 | ast::Expr * build_unary_val( const CodeLocation & location, OperKinds op, ExpressionNode * expr_node ) {
|
|---|
| [bb7422a] | 643 | std::vector<ast::ptr<ast::Expr>> args;
|
|---|
| [702e826] | 644 | args.push_back( maybeMoveBuild( expr_node ) );
|
|---|
| [2737b37] | 645 | return new ast::UntypedExpr( location, new ast::NameExpr( location, OperName[ (int)op ] ), std::move( args ) );
|
|---|
| [a2e0687] | 646 | } // build_unary_val
|
|---|
| 647 |
|
|---|
| [2737b37] | 648 | ast::Expr * build_binary_val( const CodeLocation & location, OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
|
|---|
| [bb7422a] | 649 | std::vector<ast::ptr<ast::Expr>> args;
|
|---|
| [702e826] | 650 | args.push_back( maybeMoveBuild( expr_node1 ) );
|
|---|
| 651 | args.push_back( maybeMoveBuild( expr_node2 ) );
|
|---|
| [2737b37] | 652 | return new ast::UntypedExpr( location, new ast::NameExpr( location, OperName[ (int)op ] ), std::move( args ) );
|
|---|
| [a2e0687] | 653 | } // build_binary_val
|
|---|
| 654 |
|
|---|
| [2737b37] | 655 | ast::Expr * build_cond( const CodeLocation & location, ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) {
|
|---|
| 656 | return new ast::ConditionalExpr( location, maybeMoveBuild( expr_node1 ), maybeMoveBuild( expr_node2 ), maybeMoveBuild( expr_node3 ) );
|
|---|
| [a2e0687] | 657 | } // build_cond
|
|---|
| [51e076e] | 658 |
|
|---|
| [2737b37] | 659 | ast::Expr * build_tuple( const CodeLocation & location, ExpressionNode * expr_node ) {
|
|---|
| [bb7422a] | 660 | std::vector<ast::ptr<ast::Expr>> exprs;
|
|---|
| [907eccb] | 661 | buildMoveList( expr_node, exprs );
|
|---|
| [bb7422a] | 662 | return new ast::UntypedTupleExpr( location, std::move( exprs ) );
|
|---|
| [a2e0687] | 663 | } // build_tuple
|
|---|
| [9706554] | 664 |
|
|---|
| [2737b37] | 665 | ast::Expr * build_func( const CodeLocation & location, ExpressionNode * function, ExpressionNode * expr_node ) {
|
|---|
| [bb7422a] | 666 | std::vector<ast::ptr<ast::Expr>> args;
|
|---|
| [7ecbb7e] | 667 | buildMoveList( expr_node, args );
|
|---|
| [2737b37] | 668 | return new ast::UntypedExpr( location, maybeMoveBuild( function ), std::move( args ) );
|
|---|
| [a2e0687] | 669 | } // build_func
|
|---|
| [9706554] | 670 |
|
|---|
| [2737b37] | 671 | ast::Expr * build_compoundLiteral( const CodeLocation & location, DeclarationNode * decl_node, InitializerNode * kids ) {
|
|---|
| [bb7422a] | 672 | // compound literal type
|
|---|
| 673 | ast::Decl * newDecl = maybeBuild( decl_node );
|
|---|
| 674 | // non-sue compound-literal type
|
|---|
| 675 | if ( ast::DeclWithType * newDeclWithType = dynamic_cast<ast::DeclWithType *>( newDecl ) ) {
|
|---|
| [2737b37] | 676 | return new ast::CompoundLiteralExpr( location, newDeclWithType->get_type(), maybeMoveBuild( kids ) );
|
|---|
| [630a82a] | 677 | // these types do not have associated type information
|
|---|
| [bb7422a] | 678 | } else if ( auto newDeclStructDecl = dynamic_cast<ast::StructDecl *>( newDecl ) ) {
|
|---|
| 679 | if ( newDeclStructDecl->body ) {
|
|---|
| [2737b37] | 680 | return new ast::CompoundLiteralExpr( location, new ast::StructInstType( newDeclStructDecl ), maybeMoveBuild( kids ) );
|
|---|
| [fbcde64] | 681 | } else {
|
|---|
| [2737b37] | 682 | return new ast::CompoundLiteralExpr( location, new ast::StructInstType( newDeclStructDecl->name ), maybeMoveBuild( kids ) );
|
|---|
| [fbcde64] | 683 | } // if
|
|---|
| [bb7422a] | 684 | } else if ( auto newDeclUnionDecl = dynamic_cast<ast::UnionDecl *>( newDecl ) ) {
|
|---|
| 685 | if ( newDeclUnionDecl->body ) {
|
|---|
| [2737b37] | 686 | return new ast::CompoundLiteralExpr( location, new ast::UnionInstType( newDeclUnionDecl ), maybeMoveBuild( kids ) );
|
|---|
| [fbcde64] | 687 | } else {
|
|---|
| [2737b37] | 688 | return new ast::CompoundLiteralExpr( location, new ast::UnionInstType( newDeclUnionDecl->name ), maybeMoveBuild( kids ) );
|
|---|
| [fbcde64] | 689 | } // if
|
|---|
| [bb7422a] | 690 | } else if ( auto newDeclEnumDecl = dynamic_cast<ast::EnumDecl *>( newDecl ) ) {
|
|---|
| 691 | if ( newDeclEnumDecl->body ) {
|
|---|
| [2737b37] | 692 | return new ast::CompoundLiteralExpr( location, new ast::EnumInstType( newDeclEnumDecl ), maybeMoveBuild( kids ) );
|
|---|
| [fbcde64] | 693 | } else {
|
|---|
| [2737b37] | 694 | return new ast::CompoundLiteralExpr( location, new ast::EnumInstType( newDeclEnumDecl->name ), maybeMoveBuild( kids ) );
|
|---|
| [fbcde64] | 695 | } // if
|
|---|
| [630a82a] | 696 | } else {
|
|---|
| 697 | assert( false );
|
|---|
| 698 | } // if
|
|---|
| [a2e0687] | 699 | } // build_compoundLiteral
|
|---|
| [630a82a] | 700 |
|
|---|
| [2737b37] | 701 | ast::Expr * build_va_arg( const CodeLocation & location, ExpressionNode * function, DeclarationNode * declaration ) {
|
|---|
| 702 | return build_func( location, new ExpressionNode( build_varref( location, new std::string( "__builtin_va_arg" ) ) ),
|
|---|
| 703 | function->set_last( new ExpressionNode( new ast::TypeExpr( location, maybeMoveBuildType( declaration ) ) ) ) );
|
|---|
| [45ee172] | 704 | }
|
|---|
| 705 |
|
|---|
| [b87a5ed] | 706 | // Local Variables: //
|
|---|
| 707 | // tab-width: 4 //
|
|---|
| 708 | // End: //
|
|---|