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