[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 |
---|
[dc7db63] | 12 | // Last Modified On : Thu Mar 22 11:57:39 2018 |
---|
| 13 | // Update Count : 801 |
---|
[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 | |
---|
[930f69e] | 53 | static inline bool checkH( char c ) { return c == 'h' || c == 'H'; } |
---|
[7bf7fb9] | 54 | static inline bool checkL( char c ) { return c == 'l' || c == 'L'; } |
---|
[930f69e] | 55 | static inline bool checkZ( char c ) { return c == 'z' || c == 'Z'; } |
---|
| 56 | static inline bool checkU( char c ) { return c == 'u' || c == 'U'; } |
---|
[7bf7fb9] | 57 | static inline bool checkF( char c ) { return c == 'f' || c == 'F'; } |
---|
| 58 | static inline bool checkD( char c ) { return c == 'd' || c == 'D'; } |
---|
| 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'; } |
---|
| 62 | |
---|
[201aeb9] | 63 | static const char * lnthsInt[2][6] = { |
---|
| 64 | { "int8_t", "int16_t", "int32_t", "int64_t", "size_t", }, |
---|
| 65 | { "uint8_t", "uint16_t", "uint32_t", "uint64_t", "size_t", } |
---|
| 66 | }; // lnthsInt |
---|
| 67 | |
---|
| 68 | static inline void checkLNInt( string & str, int & lnth, int & size ) { |
---|
| 69 | string::size_type posn = str.find_first_of( "lL" ), start = posn; |
---|
| 70 | if ( posn == string::npos ) return; |
---|
| 71 | size = 4; // assume largest size |
---|
| 72 | posn += 1; // advance to size |
---|
| 73 | if ( str[posn] == '8' ) { // 8 |
---|
| 74 | lnth = 0; |
---|
| 75 | } else if ( str[posn] == '1' ) { |
---|
| 76 | posn += 1; |
---|
| 77 | if ( str[posn] == '6' ) { // 16 |
---|
| 78 | lnth = 1; |
---|
| 79 | } else { // 128 |
---|
| 80 | posn += 1; |
---|
| 81 | lnth = 5; |
---|
| 82 | } // if |
---|
| 83 | } else { |
---|
| 84 | if ( str[posn] == '3' ) { // 32 |
---|
| 85 | lnth = 2; |
---|
| 86 | } else if ( str[posn] == '6' ) { // 64 |
---|
| 87 | lnth = 3; |
---|
| 88 | } else { |
---|
| 89 | assertf( false, "internal error, bad integral length %s", str.c_str() ); |
---|
[bf4b4cf] | 90 | } // if |
---|
[201aeb9] | 91 | posn += 1; |
---|
| 92 | } // if |
---|
| 93 | str.erase( start, posn - start + 1 ); // remove length suffix |
---|
| 94 | } // checkLNInt |
---|
| 95 | |
---|
[ba2a68b] | 96 | Expression * build_constantInteger( string & str ) { |
---|
[201aeb9] | 97 | static const BasicType::Kind kind[2][6] = { |
---|
[930f69e] | 98 | // short (h) must be before char (hh) |
---|
[201aeb9] | 99 | { BasicType::ShortSignedInt, BasicType::SignedChar, BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, BasicType::SignedInt128, }, |
---|
| 100 | { BasicType::ShortUnsignedInt, BasicType::UnsignedChar, BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::UnsignedInt128, }, |
---|
[7bf7fb9] | 101 | }; |
---|
[76c62b2] | 102 | |
---|
[7bf7fb9] | 103 | bool dec = true, Unsigned = false; // decimal, unsigned constant |
---|
[201aeb9] | 104 | int size; // 0 => short, 1 => char, 2 => int, 3 => long int, 4 => long long int, 5 => int128 |
---|
| 105 | int lnth = -1; // literal length |
---|
| 106 | |
---|
[6165ce7] | 107 | unsigned long long int v; // converted integral value |
---|
[0a2168f] | 108 | size_t last = str.length() - 1; // last subscript of constant |
---|
[6165ce7] | 109 | Expression * ret; |
---|
[7bf7fb9] | 110 | |
---|
[6165ce7] | 111 | // special constants |
---|
| 112 | if ( str == "0" ) { |
---|
| 113 | ret = new ConstantExpr( Constant( (Type *)new ZeroType( noQualifiers ), str, (unsigned long long int)0 ) ); |
---|
| 114 | goto CLEANUP; |
---|
| 115 | } // if |
---|
| 116 | if ( str == "1" ) { |
---|
| 117 | ret = new ConstantExpr( Constant( (Type *)new OneType( noQualifiers ), str, (unsigned long long int)1 ) ); |
---|
| 118 | goto CLEANUP; |
---|
| 119 | } // if |
---|
[ea6332d] | 120 | |
---|
[0a2168f] | 121 | // Cannot be "0" |
---|
| 122 | |
---|
| 123 | if ( str[0] == '0' ) { // radix character ? |
---|
[7bf7fb9] | 124 | dec = false; |
---|
[0a2168f] | 125 | if ( checkX( str[1] ) ) { // hex constant ? |
---|
[7bf7fb9] | 126 | sscanf( (char *)str.c_str(), "%llx", &v ); |
---|
| 127 | //printf( "%llx %llu\n", v, v ); |
---|
[0a2168f] | 128 | } else if ( checkB( str[1] ) ) { // binary constant ? |
---|
| 129 | v = 0; |
---|
| 130 | for ( unsigned int i = 2;; i += 1 ) { // compute value |
---|
| 131 | if ( str[i] == '1' ) v |= 1; |
---|
| 132 | if ( i == last ) break; |
---|
| 133 | v <<= 1; |
---|
| 134 | } // for |
---|
| 135 | //printf( "%llx %llu\n", v, v ); |
---|
[7bf7fb9] | 136 | } else { // octal constant |
---|
| 137 | sscanf( (char *)str.c_str(), "%llo", &v ); |
---|
| 138 | //printf( "%llo %llu\n", v, v ); |
---|
| 139 | } // if |
---|
| 140 | } else { // decimal constant ? |
---|
| 141 | sscanf( (char *)str.c_str(), "%llu", &v ); |
---|
| 142 | //printf( "%llu %llu\n", v, v ); |
---|
| 143 | } // if |
---|
| 144 | |
---|
| 145 | if ( v <= INT_MAX ) { // signed int |
---|
[930f69e] | 146 | size = 2; |
---|
[7bf7fb9] | 147 | } else if ( v <= UINT_MAX && ! dec ) { // unsigned int |
---|
[930f69e] | 148 | size = 2; |
---|
[7bf7fb9] | 149 | Unsigned = true; // unsigned |
---|
| 150 | } else if ( v <= LONG_MAX ) { // signed long int |
---|
[930f69e] | 151 | size = 3; |
---|
[7bf7fb9] | 152 | } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int |
---|
[930f69e] | 153 | size = 3; |
---|
[7bf7fb9] | 154 | Unsigned = true; // unsigned long int |
---|
| 155 | } else if ( v <= LLONG_MAX ) { // signed long long int |
---|
[930f69e] | 156 | size = 4; |
---|
[7bf7fb9] | 157 | } else { // unsigned long long int |
---|
[930f69e] | 158 | size = 4; |
---|
[7bf7fb9] | 159 | Unsigned = true; // unsigned long long int |
---|
| 160 | } // if |
---|
| 161 | |
---|
[930f69e] | 162 | // At least one digit in integer constant, so safe to backup while looking for suffix. |
---|
| 163 | |
---|
[7bf7fb9] | 164 | if ( checkU( str[last] ) ) { // suffix 'u' ? |
---|
| 165 | Unsigned = true; |
---|
[930f69e] | 166 | if ( checkL( str[last - 1] ) ) { // suffix 'l' ? |
---|
| 167 | size = 3; |
---|
| 168 | if ( checkL( str[last - 2] ) ) { // suffix "ll" ? |
---|
| 169 | size = 4; |
---|
[7bf7fb9] | 170 | } // if |
---|
[930f69e] | 171 | } else if ( checkH( str[last - 1] ) ) { // suffix 'h' ? |
---|
| 172 | size = 0; |
---|
| 173 | if ( checkH( str[last - 2] ) ) { // suffix "hh" ? |
---|
| 174 | size = 1; |
---|
| 175 | } // if |
---|
| 176 | str.erase( last - size - 1, size + 1 ); // remove 'h'/"hh" |
---|
[201aeb9] | 177 | } else { // suffix "ln" ? |
---|
| 178 | checkLNInt( str, lnth, size ); |
---|
[7bf7fb9] | 179 | } // if |
---|
| 180 | } else if ( checkL( str[ last ] ) ) { // suffix 'l' ? |
---|
[930f69e] | 181 | size = 3; |
---|
| 182 | if ( checkL( str[last - 1] ) ) { // suffix 'll' ? |
---|
| 183 | size = 4; |
---|
| 184 | if ( checkU( str[last - 2] ) ) { // suffix 'u' ? |
---|
[7bf7fb9] | 185 | Unsigned = true; |
---|
| 186 | } // if |
---|
[930f69e] | 187 | } else if ( checkU( str[last - 1] ) ) { // suffix 'u' ? |
---|
| 188 | Unsigned = true; |
---|
| 189 | } // if |
---|
| 190 | } else if ( checkH( str[ last ] ) ) { // suffix 'h' ? |
---|
| 191 | size = 0; |
---|
| 192 | if ( checkH( str[last - 1] ) ) { // suffix "hh" ? |
---|
| 193 | size = 1; |
---|
| 194 | if ( checkU( str[last - 2] ) ) { // suffix 'u' ? |
---|
[7bf7fb9] | 195 | Unsigned = true; |
---|
| 196 | } // if |
---|
[930f69e] | 197 | } else if ( checkU( str[last - 1] ) ) { // suffix 'u' ? |
---|
| 198 | Unsigned = true; |
---|
[7bf7fb9] | 199 | } // if |
---|
[930f69e] | 200 | str.erase( last - size, size + 1 ); // remove 'h'/"hh" |
---|
| 201 | } else if ( checkZ( str[last] ) ) { // suffix 'z' ? |
---|
[201aeb9] | 202 | lnth = 4; |
---|
[930f69e] | 203 | str.erase( last, 1 ); // remove 'z' |
---|
[201aeb9] | 204 | } else { // suffix "ln" ? |
---|
| 205 | checkLNInt( str, lnth, size ); |
---|
[7bf7fb9] | 206 | } // if |
---|
| 207 | |
---|
[201aeb9] | 208 | assert( 0 <= size && size < 6 ); |
---|
[a6c5d7c] | 209 | // Constant type is correct for overload resolving. |
---|
[e8ccca3] | 210 | ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][size] ), str, v ) ); |
---|
[a6c5d7c] | 211 | if ( Unsigned && size < 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. |
---|
[7b1d5ec] | 213 | ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][size] ) ); |
---|
[201aeb9] | 214 | } else if ( lnth != -1 ) { // explicit length ? |
---|
| 215 | if ( lnth == 5 ) { // int128 ? |
---|
| 216 | size = 5; |
---|
| 217 | ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][size] ) ); |
---|
| 218 | } else { |
---|
| 219 | ret = new CastExpr( ret, new TypeInstType( Type::Qualifiers(), lnthsInt[Unsigned][lnth], false ) ); |
---|
| 220 | } // if |
---|
[7b1d5ec] | 221 | } // if |
---|
[6165ce7] | 222 | CLEANUP: |
---|
[e8ccca3] | 223 | |
---|
[ab57786] | 224 | delete &str; // created by lex |
---|
| 225 | return ret; |
---|
[7bf7fb9] | 226 | } // build_constantInteger |
---|
[51b7345] | 227 | |
---|
[201aeb9] | 228 | |
---|
| 229 | static inline void checkLNFloat( string & str, int & lnth, int & size ) { |
---|
| 230 | string::size_type posn = str.find_first_of( "lL" ), start = posn; |
---|
| 231 | if ( posn == string::npos ) return; |
---|
| 232 | size = 2; // assume largest size |
---|
| 233 | lnth = 0; |
---|
| 234 | posn += 1; // advance to size |
---|
| 235 | if ( str[posn] == '3' ) { // 32 |
---|
| 236 | size = 0; |
---|
| 237 | } else if ( str[posn] == '6' ) { // 64 |
---|
| 238 | size = 1; |
---|
| 239 | } else if ( str[posn] == '8' || str[posn] == '1' ) { // 80, 128 |
---|
| 240 | size = 2; |
---|
| 241 | if ( str[posn] == '1' ) posn += 1; |
---|
| 242 | } else { |
---|
| 243 | assertf( false, "internal error, bad floating point length %s", str.c_str() ); |
---|
| 244 | } // if |
---|
| 245 | posn += 1; |
---|
| 246 | str.erase( start, posn - start + 1 ); // remove length suffix |
---|
| 247 | } // checkLNFloat |
---|
| 248 | |
---|
| 249 | |
---|
[f43df73] | 250 | Expression * build_constantFloat( string & str ) { |
---|
[7bf7fb9] | 251 | static const BasicType::Kind kind[2][3] = { |
---|
| 252 | { BasicType::Float, BasicType::Double, BasicType::LongDouble }, |
---|
| 253 | { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
| 254 | }; |
---|
[59c24b6] | 255 | |
---|
[7bf7fb9] | 256 | bool complx = false; // real, complex |
---|
[201aeb9] | 257 | int size = 1; // 0 => float, 1 => double, 2 => long double |
---|
| 258 | int lnth = -1; // literal length |
---|
[7bf7fb9] | 259 | // floating-point constant has minimum of 2 characters: 1. or .1 |
---|
| 260 | size_t last = str.length() - 1; |
---|
[d56e5bc] | 261 | double v; |
---|
| 262 | |
---|
| 263 | sscanf( str.c_str(), "%lg", &v ); |
---|
[51b7345] | 264 | |
---|
[7bf7fb9] | 265 | if ( checkI( str[last] ) ) { // imaginary ? |
---|
| 266 | complx = true; |
---|
| 267 | last -= 1; // backup one character |
---|
| 268 | } // if |
---|
[0caaa6a] | 269 | |
---|
[7bf7fb9] | 270 | if ( checkF( str[last] ) ) { // float ? |
---|
| 271 | size = 0; |
---|
| 272 | } else if ( checkD( str[last] ) ) { // double ? |
---|
| 273 | size = 1; |
---|
| 274 | } else if ( checkL( str[last] ) ) { // long double ? |
---|
| 275 | size = 2; |
---|
[201aeb9] | 276 | } else { |
---|
| 277 | size = 1; // double (default) |
---|
| 278 | checkLNFloat( str, lnth, size ); |
---|
[7bf7fb9] | 279 | } // if |
---|
| 280 | if ( ! complx && checkI( str[last - 1] ) ) { // imaginary ? |
---|
| 281 | complx = true; |
---|
| 282 | } // if |
---|
[51b7345] | 283 | |
---|
[201aeb9] | 284 | assert( 0 <= size && size < 3 ); |
---|
[e8ccca3] | 285 | Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][size] ), str, v ) ); |
---|
[201aeb9] | 286 | if ( lnth != -1 ) { // explicit length ? |
---|
| 287 | ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[complx][size] ) ); |
---|
| 288 | } // if |
---|
[76c62b2] | 289 | |
---|
[ab57786] | 290 | delete &str; // created by lex |
---|
| 291 | return ret; |
---|
[7bf7fb9] | 292 | } // build_constantFloat |
---|
[51b7345] | 293 | |
---|
[76c62b2] | 294 | static void sepString( string & str, string & units, char delimit ) { |
---|
| 295 | string::size_type posn = str.find_last_of( delimit ) + 1; |
---|
| 296 | if ( posn != str.length() ) { |
---|
[e8ccca3] | 297 | units = "?" + str.substr( posn ); // extract units |
---|
[76c62b2] | 298 | str.erase( posn ); // remove units |
---|
| 299 | } // if |
---|
| 300 | } // sepString |
---|
| 301 | |
---|
[f43df73] | 302 | Expression * build_constantChar( string & str ) { |
---|
[76c62b2] | 303 | string units; // units |
---|
| 304 | sepString( str, units, '\'' ); // separate constant from units |
---|
| 305 | |
---|
[e8ccca3] | 306 | Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) ); |
---|
| 307 | if ( units.length() != 0 ) { |
---|
[ba2a68b] | 308 | ret = new UntypedExpr( new NameExpr( units ), { ret } ); |
---|
[e8ccca3] | 309 | } // if |
---|
[76c62b2] | 310 | |
---|
[ab57786] | 311 | delete &str; // created by lex |
---|
| 312 | return ret; |
---|
[7bf7fb9] | 313 | } // build_constantChar |
---|
[51b7345] | 314 | |
---|
[e612146c] | 315 | Expression * build_constantStr( string & str ) { |
---|
[76c62b2] | 316 | string units; // units |
---|
| 317 | sepString( str, units, '"' ); // separate constant from units |
---|
| 318 | |
---|
[513e165] | 319 | Type * strtype; |
---|
| 320 | switch ( str[0] ) { // str has >= 2 characters, i.e, null string "" => safe to look at subscripts 0/1 |
---|
[e612146c] | 321 | case 'u': |
---|
[513e165] | 322 | if ( str[1] == '8' ) goto Default; // utf-8 characters => array of char |
---|
| 323 | // lookup type of associated typedef |
---|
| 324 | strtype = new TypeInstType( Type::Qualifiers( Type::Const ), "char16_t", false ); |
---|
[e612146c] | 325 | break; |
---|
| 326 | case 'U': |
---|
[513e165] | 327 | strtype = new TypeInstType( Type::Qualifiers( Type::Const ), "char32_t", false ); |
---|
[e612146c] | 328 | break; |
---|
| 329 | case 'L': |
---|
[513e165] | 330 | strtype = new TypeInstType( Type::Qualifiers( Type::Const ), "wchar_t", false ); |
---|
[e612146c] | 331 | break; |
---|
[513e165] | 332 | Default: // char default string type |
---|
| 333 | default: |
---|
| 334 | strtype = new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char ); |
---|
[e612146c] | 335 | } // switch |
---|
[513e165] | 336 | ArrayType * at = new ArrayType( noQualifiers, strtype, |
---|
[e8ccca3] | 337 | new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"' |
---|
| 338 | false, false ); |
---|
[e612146c] | 339 | Expression * ret = new ConstantExpr( Constant( at, str, (unsigned long long int)0 ) ); // constant 0 is ignored for pure string value |
---|
| 340 | if ( units.length() != 0 ) { |
---|
| 341 | ret = new UntypedExpr( new NameExpr( units ), { ret } ); |
---|
| 342 | } // if |
---|
[5809461] | 343 | |
---|
[ab57786] | 344 | delete &str; // created by lex |
---|
| 345 | return ret; |
---|
[7bf7fb9] | 346 | } // build_constantStr |
---|
[51b7345] | 347 | |
---|
[930f69e] | 348 | Expression * build_field_name_FLOATING_FRACTIONconstant( const string & str ) { |
---|
[a16764a6] | 349 | if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) SemanticError( yylloc, "invalid tuple index " + str ); |
---|
[930f69e] | 350 | Expression * ret = build_constantInteger( *new string( str.substr(1) ) ); |
---|
| 351 | delete &str; |
---|
| 352 | return ret; |
---|
| 353 | } // build_field_name_FLOATING_FRACTIONconstant |
---|
| 354 | |
---|
| 355 | Expression * build_field_name_FLOATING_DECIMALconstant( const string & str ) { |
---|
[a16764a6] | 356 | if ( str[str.size()-1] != '.' ) SemanticError( yylloc, "invalid tuple index " + str ); |
---|
[930f69e] | 357 | Expression * ret = build_constantInteger( *new string( str.substr( 0, str.size()-1 ) ) ); |
---|
| 358 | delete &str; |
---|
| 359 | return ret; |
---|
| 360 | } // build_field_name_FLOATING_DECIMALconstant |
---|
| 361 | |
---|
[f43df73] | 362 | Expression * build_field_name_FLOATINGconstant( const string & str ) { |
---|
[8780e30] | 363 | // str is of the form A.B -> separate at the . and return member expression |
---|
| 364 | int a, b; |
---|
| 365 | char dot; |
---|
[f43df73] | 366 | stringstream ss( str ); |
---|
[8780e30] | 367 | ss >> a >> dot >> b; |
---|
[d56e5bc] | 368 | UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) ); |
---|
[8780e30] | 369 | delete &str; |
---|
| 370 | return ret; |
---|
| 371 | } // build_field_name_FLOATINGconstant |
---|
| 372 | |
---|
| 373 | Expression * make_field_name_fraction_constants( Expression * fieldName, Expression * fracts ) { |
---|
| 374 | if ( fracts ) { |
---|
| 375 | if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( fracts ) ) { |
---|
| 376 | memberExpr->set_member( make_field_name_fraction_constants( fieldName, memberExpr->get_aggregate() ) ); |
---|
| 377 | return memberExpr; |
---|
| 378 | } else { |
---|
| 379 | return new UntypedMemberExpr( fracts, fieldName ); |
---|
[f43df73] | 380 | } // if |
---|
| 381 | } // if |
---|
[8780e30] | 382 | return fieldName; |
---|
| 383 | } // make_field_name_fraction_constants |
---|
| 384 | |
---|
| 385 | Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts ) { |
---|
| 386 | return make_field_name_fraction_constants( fieldName, maybeMoveBuild< Expression >( fracts ) ); |
---|
| 387 | } // build_field_name_fraction_constants |
---|
| 388 | |
---|
[a2e0687] | 389 | NameExpr * build_varref( const string * name ) { |
---|
[bf4b4cf] | 390 | NameExpr * expr = new NameExpr( *name ); |
---|
[7ecbb7e] | 391 | delete name; |
---|
| 392 | return expr; |
---|
[a2e0687] | 393 | } // build_varref |
---|
[51b1202] | 394 | |
---|
[5809461] | 395 | // TODO: get rid of this and OperKinds and reuse code from OperatorTable |
---|
[a2e0687] | 396 | static const char * OperName[] = { // must harmonize with OperKinds |
---|
[5721a6d] | 397 | // diadic |
---|
[e5f2a67] | 398 | "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&", |
---|
[5721a6d] | 399 | "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?", |
---|
[e5f2a67] | 400 | "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?", |
---|
[d9e2280] | 401 | "?[?]", "...", |
---|
[5721a6d] | 402 | // monadic |
---|
[5809461] | 403 | "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", |
---|
[a2e0687] | 404 | }; // OperName |
---|
[5721a6d] | 405 | |
---|
[a2e0687] | 406 | Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) { |
---|
| 407 | Type * targetType = maybeMoveBuildType( decl_node ); |
---|
[d1625f8] | 408 | if ( dynamic_cast< VoidType * >( targetType ) ) { |
---|
[064e3ff] | 409 | delete targetType; |
---|
[7ecbb7e] | 410 | return new CastExpr( maybeMoveBuild< Expression >(expr_node) ); |
---|
[064e3ff] | 411 | } else { |
---|
[7ecbb7e] | 412 | return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType ); |
---|
[064e3ff] | 413 | } // if |
---|
[a2e0687] | 414 | } // build_cast |
---|
[a5f0529] | 415 | |
---|
[a2e0687] | 416 | Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) { |
---|
[513e165] | 417 | return new VirtualCastExpr( maybeMoveBuild< Expression >( expr_node ), maybeMoveBuildType( decl_node ) ); |
---|
[a2e0687] | 418 | } // build_virtual_cast |
---|
[064e3ff] | 419 | |
---|
[a2e0687] | 420 | Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) { |
---|
[513e165] | 421 | return new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) ); |
---|
[a2e0687] | 422 | } // build_fieldSel |
---|
[064e3ff] | 423 | |
---|
[a2e0687] | 424 | Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) { |
---|
| 425 | UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
[64ac636] | 426 | deref->location = expr_node->location; |
---|
[7ecbb7e] | 427 | deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) ); |
---|
[a2e0687] | 428 | UntypedMemberExpr * ret = new UntypedMemberExpr( member, deref ); |
---|
[064e3ff] | 429 | return ret; |
---|
[a2e0687] | 430 | } // build_pfieldSel |
---|
[064e3ff] | 431 | |
---|
[a2e0687] | 432 | Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) { |
---|
[a7c90d4] | 433 | Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() ); |
---|
[ac71a86] | 434 | delete member; |
---|
| 435 | return ret; |
---|
[a2e0687] | 436 | } // build_offsetOf |
---|
[064e3ff] | 437 | |
---|
[a2e0687] | 438 | Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ) { |
---|
[7ecbb7e] | 439 | return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind ); |
---|
[a2e0687] | 440 | } // build_and_or |
---|
[51e076e] | 441 | |
---|
[a2e0687] | 442 | Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) { |
---|
[f43df73] | 443 | list< Expression * > args; |
---|
[7ecbb7e] | 444 | args.push_back( maybeMoveBuild< Expression >(expr_node) ); |
---|
[d9e2280] | 445 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
[a2e0687] | 446 | } // build_unary_val |
---|
| 447 | |
---|
| 448 | Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node ) { |
---|
[f43df73] | 449 | list< Expression * > args; |
---|
[cccc534] | 450 | args.push_back( maybeMoveBuild< Expression >(expr_node) ); // xxx -- this is exactly the same as the val case now, refactor this code. |
---|
[d9e2280] | 451 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
[a2e0687] | 452 | } // build_unary_ptr |
---|
| 453 | |
---|
| 454 | Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) { |
---|
[f43df73] | 455 | list< Expression * > args; |
---|
[7ecbb7e] | 456 | args.push_back( maybeMoveBuild< Expression >(expr_node1) ); |
---|
| 457 | args.push_back( maybeMoveBuild< Expression >(expr_node2) ); |
---|
[d9e2280] | 458 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
[a2e0687] | 459 | } // build_binary_val |
---|
| 460 | |
---|
| 461 | Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) { |
---|
[f43df73] | 462 | list< Expression * > args; |
---|
[cda7889] | 463 | args.push_back( maybeMoveBuild< Expression >(expr_node1) ); |
---|
[7ecbb7e] | 464 | args.push_back( maybeMoveBuild< Expression >(expr_node2) ); |
---|
[d9e2280] | 465 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
[a2e0687] | 466 | } // build_binary_ptr |
---|
[51e076e] | 467 | |
---|
[a2e0687] | 468 | Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) { |
---|
[7ecbb7e] | 469 | return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) ); |
---|
[a2e0687] | 470 | } // build_cond |
---|
[51e076e] | 471 | |
---|
[a2e0687] | 472 | Expression * build_tuple( ExpressionNode * expr_node ) { |
---|
[f43df73] | 473 | list< Expression * > exprs; |
---|
[907eccb] | 474 | buildMoveList( expr_node, exprs ); |
---|
| 475 | return new UntypedTupleExpr( exprs );; |
---|
[a2e0687] | 476 | } // build_tuple |
---|
[9706554] | 477 | |
---|
[a2e0687] | 478 | Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) { |
---|
[f43df73] | 479 | list< Expression * > args; |
---|
[7ecbb7e] | 480 | buildMoveList( expr_node, args ); |
---|
[bf4b4cf] | 481 | return new UntypedExpr( maybeMoveBuild< Expression >(function), args ); |
---|
[a2e0687] | 482 | } // build_func |
---|
[9706554] | 483 | |
---|
[a2e0687] | 484 | Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids ) { |
---|
[7880579] | 485 | Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type |
---|
[630a82a] | 486 | if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type |
---|
[7ecbb7e] | 487 | return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild< Initializer >(kids) ); |
---|
[630a82a] | 488 | // these types do not have associated type information |
---|
| 489 | } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl ) ) { |
---|
[fbcde64] | 490 | if ( newDeclStructDecl->has_body() ) { |
---|
| 491 | return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl ), maybeMoveBuild< Initializer >(kids) ); |
---|
| 492 | } else { |
---|
| 493 | return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild< Initializer >(kids) ); |
---|
| 494 | } // if |
---|
[630a82a] | 495 | } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl ) ) { |
---|
[fbcde64] | 496 | if ( newDeclUnionDecl->has_body() ) { |
---|
| 497 | return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl ), maybeMoveBuild< Initializer >(kids) ); |
---|
| 498 | } else { |
---|
| 499 | return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild< Initializer >(kids) ); |
---|
| 500 | } // if |
---|
[630a82a] | 501 | } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl ) ) { |
---|
[fbcde64] | 502 | if ( newDeclEnumDecl->has_body() ) { |
---|
| 503 | return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl ), maybeMoveBuild< Initializer >(kids) ); |
---|
| 504 | } else { |
---|
| 505 | return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild< Initializer >(kids) ); |
---|
| 506 | } // if |
---|
[630a82a] | 507 | } else { |
---|
| 508 | assert( false ); |
---|
| 509 | } // if |
---|
[a2e0687] | 510 | } // build_compoundLiteral |
---|
[630a82a] | 511 | |
---|
[b87a5ed] | 512 | // Local Variables: // |
---|
| 513 | // tab-width: 4 // |
---|
| 514 | // mode: c++ // |
---|
| 515 | // compile-command: "make install" // |
---|
| 516 | // End: // |
---|