[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 |
---|
[6e3eaa57] | 12 | // Last Modified On : Mon Jun 4 21:24:45 2018 |
---|
| 13 | // Update Count : 802 |
---|
[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. |
---|
[c0bf94e] | 213 | ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][size] ), false ); |
---|
[201aeb9] | 214 | } else if ( lnth != -1 ) { // explicit length ? |
---|
| 215 | if ( lnth == 5 ) { // int128 ? |
---|
| 216 | size = 5; |
---|
[c0bf94e] | 217 | ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][size] ), false ); |
---|
[201aeb9] | 218 | } else { |
---|
[c0bf94e] | 219 | ret = new CastExpr( ret, new TypeInstType( Type::Qualifiers(), lnthsInt[Unsigned][lnth], false ), false ); |
---|
[201aeb9] | 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 ? |
---|
[c0bf94e] | 287 | ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[complx][size] ), false ); |
---|
[201aeb9] | 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 ) { |
---|
[6e3eaa57] | 316 | assert( str.length() > 0 ); |
---|
[76c62b2] | 317 | string units; // units |
---|
| 318 | sepString( str, units, '"' ); // separate constant from units |
---|
| 319 | |
---|
[513e165] | 320 | Type * strtype; |
---|
| 321 | switch ( str[0] ) { // str has >= 2 characters, i.e, null string "" => safe to look at subscripts 0/1 |
---|
[e612146c] | 322 | case 'u': |
---|
[513e165] | 323 | if ( str[1] == '8' ) goto Default; // utf-8 characters => array of char |
---|
| 324 | // lookup type of associated typedef |
---|
| 325 | strtype = new TypeInstType( Type::Qualifiers( Type::Const ), "char16_t", false ); |
---|
[e612146c] | 326 | break; |
---|
| 327 | case 'U': |
---|
[513e165] | 328 | strtype = new TypeInstType( Type::Qualifiers( Type::Const ), "char32_t", false ); |
---|
[e612146c] | 329 | break; |
---|
| 330 | case 'L': |
---|
[513e165] | 331 | strtype = new TypeInstType( Type::Qualifiers( Type::Const ), "wchar_t", false ); |
---|
[e612146c] | 332 | break; |
---|
[513e165] | 333 | Default: // char default string type |
---|
| 334 | default: |
---|
| 335 | strtype = new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char ); |
---|
[e612146c] | 336 | } // switch |
---|
[513e165] | 337 | ArrayType * at = new ArrayType( noQualifiers, strtype, |
---|
[e8ccca3] | 338 | new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"' |
---|
| 339 | false, false ); |
---|
[e612146c] | 340 | Expression * ret = new ConstantExpr( Constant( at, str, (unsigned long long int)0 ) ); // constant 0 is ignored for pure string value |
---|
| 341 | if ( units.length() != 0 ) { |
---|
| 342 | ret = new UntypedExpr( new NameExpr( units ), { ret } ); |
---|
| 343 | } // if |
---|
[5809461] | 344 | |
---|
[ab57786] | 345 | delete &str; // created by lex |
---|
| 346 | return ret; |
---|
[7bf7fb9] | 347 | } // build_constantStr |
---|
[51b7345] | 348 | |
---|
[930f69e] | 349 | Expression * build_field_name_FLOATING_FRACTIONconstant( const string & str ) { |
---|
[a16764a6] | 350 | if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) SemanticError( yylloc, "invalid tuple index " + str ); |
---|
[930f69e] | 351 | Expression * ret = build_constantInteger( *new string( str.substr(1) ) ); |
---|
| 352 | delete &str; |
---|
| 353 | return ret; |
---|
| 354 | } // build_field_name_FLOATING_FRACTIONconstant |
---|
| 355 | |
---|
| 356 | Expression * build_field_name_FLOATING_DECIMALconstant( const string & str ) { |
---|
[a16764a6] | 357 | if ( str[str.size()-1] != '.' ) SemanticError( yylloc, "invalid tuple index " + str ); |
---|
[930f69e] | 358 | Expression * ret = build_constantInteger( *new string( str.substr( 0, str.size()-1 ) ) ); |
---|
| 359 | delete &str; |
---|
| 360 | return ret; |
---|
| 361 | } // build_field_name_FLOATING_DECIMALconstant |
---|
| 362 | |
---|
[f43df73] | 363 | Expression * build_field_name_FLOATINGconstant( const string & str ) { |
---|
[8780e30] | 364 | // str is of the form A.B -> separate at the . and return member expression |
---|
| 365 | int a, b; |
---|
| 366 | char dot; |
---|
[f43df73] | 367 | stringstream ss( str ); |
---|
[8780e30] | 368 | ss >> a >> dot >> b; |
---|
[d56e5bc] | 369 | UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) ); |
---|
[8780e30] | 370 | delete &str; |
---|
| 371 | return ret; |
---|
| 372 | } // build_field_name_FLOATINGconstant |
---|
| 373 | |
---|
| 374 | Expression * make_field_name_fraction_constants( Expression * fieldName, Expression * fracts ) { |
---|
| 375 | if ( fracts ) { |
---|
| 376 | if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( fracts ) ) { |
---|
| 377 | memberExpr->set_member( make_field_name_fraction_constants( fieldName, memberExpr->get_aggregate() ) ); |
---|
| 378 | return memberExpr; |
---|
| 379 | } else { |
---|
| 380 | return new UntypedMemberExpr( fracts, fieldName ); |
---|
[f43df73] | 381 | } // if |
---|
| 382 | } // if |
---|
[8780e30] | 383 | return fieldName; |
---|
| 384 | } // make_field_name_fraction_constants |
---|
| 385 | |
---|
| 386 | Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts ) { |
---|
| 387 | return make_field_name_fraction_constants( fieldName, maybeMoveBuild< Expression >( fracts ) ); |
---|
| 388 | } // build_field_name_fraction_constants |
---|
| 389 | |
---|
[a2e0687] | 390 | NameExpr * build_varref( const string * name ) { |
---|
[bf4b4cf] | 391 | NameExpr * expr = new NameExpr( *name ); |
---|
[7ecbb7e] | 392 | delete name; |
---|
| 393 | return expr; |
---|
[a2e0687] | 394 | } // build_varref |
---|
[51b1202] | 395 | |
---|
[5809461] | 396 | // TODO: get rid of this and OperKinds and reuse code from OperatorTable |
---|
[a2e0687] | 397 | static const char * OperName[] = { // must harmonize with OperKinds |
---|
[5721a6d] | 398 | // diadic |
---|
[e5f2a67] | 399 | "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&", |
---|
[5721a6d] | 400 | "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?", |
---|
[e5f2a67] | 401 | "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?", |
---|
[d9e2280] | 402 | "?[?]", "...", |
---|
[5721a6d] | 403 | // monadic |
---|
[5809461] | 404 | "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", |
---|
[a2e0687] | 405 | }; // OperName |
---|
[5721a6d] | 406 | |
---|
[a2e0687] | 407 | Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) { |
---|
| 408 | Type * targetType = maybeMoveBuildType( decl_node ); |
---|
[d1625f8] | 409 | if ( dynamic_cast< VoidType * >( targetType ) ) { |
---|
[064e3ff] | 410 | delete targetType; |
---|
[c0bf94e] | 411 | return new CastExpr( maybeMoveBuild< Expression >(expr_node), false ); |
---|
[064e3ff] | 412 | } else { |
---|
[c0bf94e] | 413 | return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType, false ); |
---|
[064e3ff] | 414 | } // if |
---|
[a2e0687] | 415 | } // build_cast |
---|
[a5f0529] | 416 | |
---|
[9a705dc8] | 417 | Expression * build_keyword_cast( KeywordCastExpr::Target target, ExpressionNode * expr_node ) { |
---|
| 418 | return new KeywordCastExpr( maybeMoveBuild< Expression >(expr_node), target ); |
---|
| 419 | } |
---|
| 420 | |
---|
[a2e0687] | 421 | Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) { |
---|
[513e165] | 422 | return new VirtualCastExpr( maybeMoveBuild< Expression >( expr_node ), maybeMoveBuildType( decl_node ) ); |
---|
[a2e0687] | 423 | } // build_virtual_cast |
---|
[064e3ff] | 424 | |
---|
[a2e0687] | 425 | Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) { |
---|
[513e165] | 426 | return new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) ); |
---|
[a2e0687] | 427 | } // build_fieldSel |
---|
[064e3ff] | 428 | |
---|
[a2e0687] | 429 | Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) { |
---|
| 430 | UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
[64ac636] | 431 | deref->location = expr_node->location; |
---|
[7ecbb7e] | 432 | deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) ); |
---|
[a2e0687] | 433 | UntypedMemberExpr * ret = new UntypedMemberExpr( member, deref ); |
---|
[064e3ff] | 434 | return ret; |
---|
[a2e0687] | 435 | } // build_pfieldSel |
---|
[064e3ff] | 436 | |
---|
[a2e0687] | 437 | Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) { |
---|
[a7c90d4] | 438 | Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() ); |
---|
[ac71a86] | 439 | delete member; |
---|
| 440 | return ret; |
---|
[a2e0687] | 441 | } // build_offsetOf |
---|
[064e3ff] | 442 | |
---|
[a2e0687] | 443 | Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ) { |
---|
[7ecbb7e] | 444 | return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind ); |
---|
[a2e0687] | 445 | } // build_and_or |
---|
[51e076e] | 446 | |
---|
[a2e0687] | 447 | Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) { |
---|
[f43df73] | 448 | list< Expression * > args; |
---|
[7ecbb7e] | 449 | args.push_back( maybeMoveBuild< Expression >(expr_node) ); |
---|
[d9e2280] | 450 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
[a2e0687] | 451 | } // build_unary_val |
---|
| 452 | |
---|
| 453 | Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node ) { |
---|
[f43df73] | 454 | list< Expression * > args; |
---|
[cccc534] | 455 | args.push_back( maybeMoveBuild< Expression >(expr_node) ); // xxx -- this is exactly the same as the val case now, refactor this code. |
---|
[d9e2280] | 456 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
[a2e0687] | 457 | } // build_unary_ptr |
---|
| 458 | |
---|
| 459 | Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) { |
---|
[f43df73] | 460 | list< Expression * > args; |
---|
[7ecbb7e] | 461 | args.push_back( maybeMoveBuild< Expression >(expr_node1) ); |
---|
| 462 | args.push_back( maybeMoveBuild< Expression >(expr_node2) ); |
---|
[d9e2280] | 463 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
[a2e0687] | 464 | } // build_binary_val |
---|
| 465 | |
---|
| 466 | Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) { |
---|
[f43df73] | 467 | list< Expression * > args; |
---|
[cda7889] | 468 | args.push_back( maybeMoveBuild< Expression >(expr_node1) ); |
---|
[7ecbb7e] | 469 | args.push_back( maybeMoveBuild< Expression >(expr_node2) ); |
---|
[d9e2280] | 470 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
[a2e0687] | 471 | } // build_binary_ptr |
---|
[51e076e] | 472 | |
---|
[a2e0687] | 473 | Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) { |
---|
[7ecbb7e] | 474 | return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) ); |
---|
[a2e0687] | 475 | } // build_cond |
---|
[51e076e] | 476 | |
---|
[a2e0687] | 477 | Expression * build_tuple( ExpressionNode * expr_node ) { |
---|
[f43df73] | 478 | list< Expression * > exprs; |
---|
[907eccb] | 479 | buildMoveList( expr_node, exprs ); |
---|
| 480 | return new UntypedTupleExpr( exprs );; |
---|
[a2e0687] | 481 | } // build_tuple |
---|
[9706554] | 482 | |
---|
[a2e0687] | 483 | Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) { |
---|
[f43df73] | 484 | list< Expression * > args; |
---|
[7ecbb7e] | 485 | buildMoveList( expr_node, args ); |
---|
[bf4b4cf] | 486 | return new UntypedExpr( maybeMoveBuild< Expression >(function), args ); |
---|
[a2e0687] | 487 | } // build_func |
---|
[9706554] | 488 | |
---|
[a2e0687] | 489 | Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids ) { |
---|
[7880579] | 490 | Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type |
---|
[630a82a] | 491 | if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type |
---|
[7ecbb7e] | 492 | return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild< Initializer >(kids) ); |
---|
[630a82a] | 493 | // these types do not have associated type information |
---|
| 494 | } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl ) ) { |
---|
[fbcde64] | 495 | if ( newDeclStructDecl->has_body() ) { |
---|
| 496 | return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl ), maybeMoveBuild< Initializer >(kids) ); |
---|
| 497 | } else { |
---|
| 498 | return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild< Initializer >(kids) ); |
---|
| 499 | } // if |
---|
[630a82a] | 500 | } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl ) ) { |
---|
[fbcde64] | 501 | if ( newDeclUnionDecl->has_body() ) { |
---|
| 502 | return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl ), maybeMoveBuild< Initializer >(kids) ); |
---|
| 503 | } else { |
---|
| 504 | return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild< Initializer >(kids) ); |
---|
| 505 | } // if |
---|
[630a82a] | 506 | } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl ) ) { |
---|
[fbcde64] | 507 | if ( newDeclEnumDecl->has_body() ) { |
---|
| 508 | return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl ), maybeMoveBuild< Initializer >(kids) ); |
---|
| 509 | } else { |
---|
| 510 | return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild< Initializer >(kids) ); |
---|
| 511 | } // if |
---|
[630a82a] | 512 | } else { |
---|
| 513 | assert( false ); |
---|
| 514 | } // if |
---|
[a2e0687] | 515 | } // build_compoundLiteral |
---|
[630a82a] | 516 | |
---|
[b87a5ed] | 517 | // Local Variables: // |
---|
| 518 | // tab-width: 4 // |
---|
| 519 | // mode: c++ // |
---|
| 520 | // compile-command: "make install" // |
---|
| 521 | // End: // |
---|