[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 |
---|
[0a2168f] | 12 | // Last Modified On : Sat Mar 3 18:22:33 2018 |
---|
| 13 | // Update Count : 796 |
---|
[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 | |
---|
[76c62b2] | 96 | static void sepNumeric( string & str, string & units ) { |
---|
| 97 | string::size_type posn = str.find_first_of( "`" ); |
---|
| 98 | if ( posn != string::npos ) { |
---|
[e8ccca3] | 99 | units = "?" + str.substr( posn ); // extract units |
---|
[76c62b2] | 100 | str.erase( posn ); // remove units |
---|
| 101 | } // if |
---|
| 102 | } // sepNumeric |
---|
| 103 | |
---|
[ba2a68b] | 104 | Expression * build_constantInteger( string & str ) { |
---|
[201aeb9] | 105 | static const BasicType::Kind kind[2][6] = { |
---|
[930f69e] | 106 | // short (h) must be before char (hh) |
---|
[201aeb9] | 107 | { BasicType::ShortSignedInt, BasicType::SignedChar, BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, BasicType::SignedInt128, }, |
---|
| 108 | { BasicType::ShortUnsignedInt, BasicType::UnsignedChar, BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::UnsignedInt128, }, |
---|
[7bf7fb9] | 109 | }; |
---|
[76c62b2] | 110 | |
---|
[201aeb9] | 111 | string units; |
---|
[76c62b2] | 112 | sepNumeric( str, units ); // separate constant from units |
---|
| 113 | |
---|
[7bf7fb9] | 114 | bool dec = true, Unsigned = false; // decimal, unsigned constant |
---|
[201aeb9] | 115 | int size; // 0 => short, 1 => char, 2 => int, 3 => long int, 4 => long long int, 5 => int128 |
---|
| 116 | int lnth = -1; // literal length |
---|
| 117 | |
---|
[6165ce7] | 118 | unsigned long long int v; // converted integral value |
---|
[0a2168f] | 119 | size_t last = str.length() - 1; // last subscript of constant |
---|
[6165ce7] | 120 | Expression * ret; |
---|
[7bf7fb9] | 121 | |
---|
[6165ce7] | 122 | // special constants |
---|
| 123 | if ( str == "0" ) { |
---|
| 124 | ret = new ConstantExpr( Constant( (Type *)new ZeroType( noQualifiers ), str, (unsigned long long int)0 ) ); |
---|
| 125 | goto CLEANUP; |
---|
| 126 | } // if |
---|
| 127 | if ( str == "1" ) { |
---|
| 128 | ret = new ConstantExpr( Constant( (Type *)new OneType( noQualifiers ), str, (unsigned long long int)1 ) ); |
---|
| 129 | goto CLEANUP; |
---|
| 130 | } // if |
---|
[ea6332d] | 131 | |
---|
[0a2168f] | 132 | // Cannot be "0" |
---|
| 133 | |
---|
| 134 | if ( str[0] == '0' ) { // radix character ? |
---|
[7bf7fb9] | 135 | dec = false; |
---|
[0a2168f] | 136 | if ( checkX( str[1] ) ) { // hex constant ? |
---|
[7bf7fb9] | 137 | sscanf( (char *)str.c_str(), "%llx", &v ); |
---|
| 138 | //printf( "%llx %llu\n", v, v ); |
---|
[0a2168f] | 139 | } else if ( checkB( str[1] ) ) { // binary constant ? |
---|
| 140 | v = 0; |
---|
| 141 | for ( unsigned int i = 2;; i += 1 ) { // compute value |
---|
| 142 | if ( str[i] == '1' ) v |= 1; |
---|
| 143 | if ( i == last ) break; |
---|
| 144 | v <<= 1; |
---|
| 145 | } // for |
---|
| 146 | //printf( "%llx %llu\n", v, v ); |
---|
[7bf7fb9] | 147 | } else { // octal constant |
---|
| 148 | sscanf( (char *)str.c_str(), "%llo", &v ); |
---|
| 149 | //printf( "%llo %llu\n", v, v ); |
---|
| 150 | } // if |
---|
| 151 | } else { // decimal constant ? |
---|
| 152 | sscanf( (char *)str.c_str(), "%llu", &v ); |
---|
| 153 | //printf( "%llu %llu\n", v, v ); |
---|
| 154 | } // if |
---|
| 155 | |
---|
| 156 | if ( v <= INT_MAX ) { // signed int |
---|
[930f69e] | 157 | size = 2; |
---|
[7bf7fb9] | 158 | } else if ( v <= UINT_MAX && ! dec ) { // unsigned int |
---|
[930f69e] | 159 | size = 2; |
---|
[7bf7fb9] | 160 | Unsigned = true; // unsigned |
---|
| 161 | } else if ( v <= LONG_MAX ) { // signed long int |
---|
[930f69e] | 162 | size = 3; |
---|
[7bf7fb9] | 163 | } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int |
---|
[930f69e] | 164 | size = 3; |
---|
[7bf7fb9] | 165 | Unsigned = true; // unsigned long int |
---|
| 166 | } else if ( v <= LLONG_MAX ) { // signed long long int |
---|
[930f69e] | 167 | size = 4; |
---|
[7bf7fb9] | 168 | } else { // unsigned long long int |
---|
[930f69e] | 169 | size = 4; |
---|
[7bf7fb9] | 170 | Unsigned = true; // unsigned long long int |
---|
| 171 | } // if |
---|
| 172 | |
---|
[930f69e] | 173 | // At least one digit in integer constant, so safe to backup while looking for suffix. |
---|
| 174 | |
---|
[7bf7fb9] | 175 | if ( checkU( str[last] ) ) { // suffix 'u' ? |
---|
| 176 | Unsigned = true; |
---|
[930f69e] | 177 | if ( checkL( str[last - 1] ) ) { // suffix 'l' ? |
---|
| 178 | size = 3; |
---|
| 179 | if ( checkL( str[last - 2] ) ) { // suffix "ll" ? |
---|
| 180 | size = 4; |
---|
[7bf7fb9] | 181 | } // if |
---|
[930f69e] | 182 | } else if ( checkH( str[last - 1] ) ) { // suffix 'h' ? |
---|
| 183 | size = 0; |
---|
| 184 | if ( checkH( str[last - 2] ) ) { // suffix "hh" ? |
---|
| 185 | size = 1; |
---|
| 186 | } // if |
---|
| 187 | str.erase( last - size - 1, size + 1 ); // remove 'h'/"hh" |
---|
[201aeb9] | 188 | } else { // suffix "ln" ? |
---|
| 189 | checkLNInt( str, lnth, size ); |
---|
[7bf7fb9] | 190 | } // if |
---|
| 191 | } else if ( checkL( str[ last ] ) ) { // suffix 'l' ? |
---|
[930f69e] | 192 | size = 3; |
---|
| 193 | if ( checkL( str[last - 1] ) ) { // suffix 'll' ? |
---|
| 194 | size = 4; |
---|
| 195 | if ( checkU( str[last - 2] ) ) { // suffix 'u' ? |
---|
[7bf7fb9] | 196 | Unsigned = true; |
---|
| 197 | } // if |
---|
[930f69e] | 198 | } else if ( checkU( str[last - 1] ) ) { // suffix 'u' ? |
---|
| 199 | Unsigned = true; |
---|
| 200 | } // if |
---|
| 201 | } else if ( checkH( str[ last ] ) ) { // suffix 'h' ? |
---|
| 202 | size = 0; |
---|
| 203 | if ( checkH( str[last - 1] ) ) { // suffix "hh" ? |
---|
| 204 | size = 1; |
---|
| 205 | if ( checkU( str[last - 2] ) ) { // suffix 'u' ? |
---|
[7bf7fb9] | 206 | Unsigned = true; |
---|
| 207 | } // if |
---|
[930f69e] | 208 | } else if ( checkU( str[last - 1] ) ) { // suffix 'u' ? |
---|
| 209 | Unsigned = true; |
---|
[7bf7fb9] | 210 | } // if |
---|
[930f69e] | 211 | str.erase( last - size, size + 1 ); // remove 'h'/"hh" |
---|
| 212 | } else if ( checkZ( str[last] ) ) { // suffix 'z' ? |
---|
[201aeb9] | 213 | lnth = 4; |
---|
[930f69e] | 214 | str.erase( last, 1 ); // remove 'z' |
---|
[201aeb9] | 215 | } else { // suffix "ln" ? |
---|
| 216 | checkLNInt( str, lnth, size ); |
---|
[7bf7fb9] | 217 | } // if |
---|
| 218 | |
---|
[201aeb9] | 219 | assert( 0 <= size && size < 6 ); |
---|
[a6c5d7c] | 220 | // Constant type is correct for overload resolving. |
---|
[e8ccca3] | 221 | ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][size] ), str, v ) ); |
---|
[a6c5d7c] | 222 | if ( Unsigned && size < 2 ) { // hh or h, less than int ? |
---|
[201aeb9] | 223 | // int i = -1uh => 65535 not -1, so cast is necessary for unsigned, which unfortunately eliminates warnings for large values. |
---|
[7b1d5ec] | 224 | ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][size] ) ); |
---|
[201aeb9] | 225 | } else if ( lnth != -1 ) { // explicit length ? |
---|
| 226 | if ( lnth == 5 ) { // int128 ? |
---|
| 227 | size = 5; |
---|
| 228 | ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][size] ) ); |
---|
| 229 | } else { |
---|
| 230 | ret = new CastExpr( ret, new TypeInstType( Type::Qualifiers(), lnthsInt[Unsigned][lnth], false ) ); |
---|
| 231 | } // if |
---|
[7b1d5ec] | 232 | } // if |
---|
[6165ce7] | 233 | CLEANUP: |
---|
[e8ccca3] | 234 | if ( units.length() != 0 ) { |
---|
[ba2a68b] | 235 | ret = new UntypedExpr( new NameExpr( units ), { ret } ); |
---|
[e8ccca3] | 236 | } // if |
---|
| 237 | |
---|
[ab57786] | 238 | delete &str; // created by lex |
---|
| 239 | return ret; |
---|
[7bf7fb9] | 240 | } // build_constantInteger |
---|
[51b7345] | 241 | |
---|
[201aeb9] | 242 | |
---|
| 243 | static inline void checkLNFloat( string & str, int & lnth, int & size ) { |
---|
| 244 | string::size_type posn = str.find_first_of( "lL" ), start = posn; |
---|
| 245 | if ( posn == string::npos ) return; |
---|
| 246 | size = 2; // assume largest size |
---|
| 247 | lnth = 0; |
---|
| 248 | posn += 1; // advance to size |
---|
| 249 | if ( str[posn] == '3' ) { // 32 |
---|
| 250 | size = 0; |
---|
| 251 | } else if ( str[posn] == '6' ) { // 64 |
---|
| 252 | size = 1; |
---|
| 253 | } else if ( str[posn] == '8' || str[posn] == '1' ) { // 80, 128 |
---|
| 254 | size = 2; |
---|
| 255 | if ( str[posn] == '1' ) posn += 1; |
---|
| 256 | } else { |
---|
| 257 | assertf( false, "internal error, bad floating point length %s", str.c_str() ); |
---|
| 258 | } // if |
---|
| 259 | posn += 1; |
---|
| 260 | str.erase( start, posn - start + 1 ); // remove length suffix |
---|
| 261 | } // checkLNFloat |
---|
| 262 | |
---|
| 263 | |
---|
[f43df73] | 264 | Expression * build_constantFloat( string & str ) { |
---|
[7bf7fb9] | 265 | static const BasicType::Kind kind[2][3] = { |
---|
| 266 | { BasicType::Float, BasicType::Double, BasicType::LongDouble }, |
---|
| 267 | { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
| 268 | }; |
---|
[59c24b6] | 269 | |
---|
[201aeb9] | 270 | string units; |
---|
[76c62b2] | 271 | sepNumeric( str, units ); // separate constant from units |
---|
| 272 | |
---|
[7bf7fb9] | 273 | bool complx = false; // real, complex |
---|
[201aeb9] | 274 | int size = 1; // 0 => float, 1 => double, 2 => long double |
---|
| 275 | int lnth = -1; // literal length |
---|
[7bf7fb9] | 276 | // floating-point constant has minimum of 2 characters: 1. or .1 |
---|
| 277 | size_t last = str.length() - 1; |
---|
[d56e5bc] | 278 | double v; |
---|
| 279 | |
---|
| 280 | sscanf( str.c_str(), "%lg", &v ); |
---|
[51b7345] | 281 | |
---|
[7bf7fb9] | 282 | if ( checkI( str[last] ) ) { // imaginary ? |
---|
| 283 | complx = true; |
---|
| 284 | last -= 1; // backup one character |
---|
| 285 | } // if |
---|
[0caaa6a] | 286 | |
---|
[7bf7fb9] | 287 | if ( checkF( str[last] ) ) { // float ? |
---|
| 288 | size = 0; |
---|
| 289 | } else if ( checkD( str[last] ) ) { // double ? |
---|
| 290 | size = 1; |
---|
| 291 | } else if ( checkL( str[last] ) ) { // long double ? |
---|
| 292 | size = 2; |
---|
[201aeb9] | 293 | } else { |
---|
| 294 | size = 1; // double (default) |
---|
| 295 | checkLNFloat( str, lnth, size ); |
---|
[7bf7fb9] | 296 | } // if |
---|
| 297 | if ( ! complx && checkI( str[last - 1] ) ) { // imaginary ? |
---|
| 298 | complx = true; |
---|
| 299 | } // if |
---|
[51b7345] | 300 | |
---|
[201aeb9] | 301 | assert( 0 <= size && size < 3 ); |
---|
[e8ccca3] | 302 | Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][size] ), str, v ) ); |
---|
[201aeb9] | 303 | if ( lnth != -1 ) { // explicit length ? |
---|
| 304 | ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[complx][size] ) ); |
---|
| 305 | } // if |
---|
[e8ccca3] | 306 | if ( units.length() != 0 ) { |
---|
[ba2a68b] | 307 | ret = new UntypedExpr( new NameExpr( units ), { ret } ); |
---|
[e8ccca3] | 308 | } // if |
---|
[76c62b2] | 309 | |
---|
[ab57786] | 310 | delete &str; // created by lex |
---|
| 311 | return ret; |
---|
[7bf7fb9] | 312 | } // build_constantFloat |
---|
[51b7345] | 313 | |
---|
[76c62b2] | 314 | static void sepString( string & str, string & units, char delimit ) { |
---|
| 315 | string::size_type posn = str.find_last_of( delimit ) + 1; |
---|
| 316 | if ( posn != str.length() ) { |
---|
[e8ccca3] | 317 | units = "?" + str.substr( posn ); // extract units |
---|
[76c62b2] | 318 | str.erase( posn ); // remove units |
---|
| 319 | } // if |
---|
| 320 | } // sepString |
---|
| 321 | |
---|
[f43df73] | 322 | Expression * build_constantChar( string & str ) { |
---|
[76c62b2] | 323 | string units; // units |
---|
| 324 | sepString( str, units, '\'' ); // separate constant from units |
---|
| 325 | |
---|
[e8ccca3] | 326 | Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) ); |
---|
| 327 | if ( units.length() != 0 ) { |
---|
[ba2a68b] | 328 | ret = new UntypedExpr( new NameExpr( units ), { ret } ); |
---|
[e8ccca3] | 329 | } // if |
---|
[76c62b2] | 330 | |
---|
[ab57786] | 331 | delete &str; // created by lex |
---|
| 332 | return ret; |
---|
[7bf7fb9] | 333 | } // build_constantChar |
---|
[51b7345] | 334 | |
---|
[e612146c] | 335 | Expression * build_constantStr( string & str ) { |
---|
[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 |
---|
[513e165] | 356 | ArrayType * at = new ArrayType( noQualifiers, strtype, |
---|
[e8ccca3] | 357 | new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"' |
---|
| 358 | false, false ); |
---|
[e612146c] | 359 | Expression * ret = new ConstantExpr( Constant( at, str, (unsigned long long int)0 ) ); // constant 0 is ignored for pure string value |
---|
| 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 ) { |
---|
[a16764a6] | 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; |
---|
[7ecbb7e] | 430 | return new CastExpr( maybeMoveBuild< Expression >(expr_node) ); |
---|
[064e3ff] | 431 | } else { |
---|
[7ecbb7e] | 432 | return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType ); |
---|
[064e3ff] | 433 | } // if |
---|
[a2e0687] | 434 | } // build_cast |
---|
[a5f0529] | 435 | |
---|
[a2e0687] | 436 | Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) { |
---|
[513e165] | 437 | return new VirtualCastExpr( maybeMoveBuild< Expression >( expr_node ), maybeMoveBuildType( decl_node ) ); |
---|
[a2e0687] | 438 | } // build_virtual_cast |
---|
[064e3ff] | 439 | |
---|
[a2e0687] | 440 | Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) { |
---|
[513e165] | 441 | return new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) ); |
---|
[a2e0687] | 442 | } // build_fieldSel |
---|
[064e3ff] | 443 | |
---|
[a2e0687] | 444 | Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) { |
---|
| 445 | UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
[64ac636] | 446 | deref->location = expr_node->location; |
---|
[7ecbb7e] | 447 | deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) ); |
---|
[a2e0687] | 448 | UntypedMemberExpr * ret = new UntypedMemberExpr( member, deref ); |
---|
[064e3ff] | 449 | return ret; |
---|
[a2e0687] | 450 | } // build_pfieldSel |
---|
[064e3ff] | 451 | |
---|
[a2e0687] | 452 | Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) { |
---|
[a7c90d4] | 453 | Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() ); |
---|
[ac71a86] | 454 | delete member; |
---|
| 455 | return ret; |
---|
[a2e0687] | 456 | } // build_offsetOf |
---|
[064e3ff] | 457 | |
---|
[a2e0687] | 458 | Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ) { |
---|
[7ecbb7e] | 459 | return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind ); |
---|
[a2e0687] | 460 | } // build_and_or |
---|
[51e076e] | 461 | |
---|
[a2e0687] | 462 | Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) { |
---|
[f43df73] | 463 | list< Expression * > args; |
---|
[7ecbb7e] | 464 | args.push_back( maybeMoveBuild< Expression >(expr_node) ); |
---|
[d9e2280] | 465 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
[a2e0687] | 466 | } // build_unary_val |
---|
| 467 | |
---|
| 468 | Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node ) { |
---|
[f43df73] | 469 | list< Expression * > args; |
---|
[cccc534] | 470 | args.push_back( maybeMoveBuild< Expression >(expr_node) ); // xxx -- this is exactly the same as the val case now, refactor this code. |
---|
[d9e2280] | 471 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
[a2e0687] | 472 | } // build_unary_ptr |
---|
| 473 | |
---|
| 474 | Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) { |
---|
[f43df73] | 475 | list< Expression * > args; |
---|
[7ecbb7e] | 476 | args.push_back( maybeMoveBuild< Expression >(expr_node1) ); |
---|
| 477 | args.push_back( maybeMoveBuild< Expression >(expr_node2) ); |
---|
[d9e2280] | 478 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
[a2e0687] | 479 | } // build_binary_val |
---|
| 480 | |
---|
| 481 | Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) { |
---|
[f43df73] | 482 | list< Expression * > args; |
---|
[cda7889] | 483 | args.push_back( maybeMoveBuild< Expression >(expr_node1) ); |
---|
[7ecbb7e] | 484 | args.push_back( maybeMoveBuild< Expression >(expr_node2) ); |
---|
[d9e2280] | 485 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
[a2e0687] | 486 | } // build_binary_ptr |
---|
[51e076e] | 487 | |
---|
[a2e0687] | 488 | Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) { |
---|
[7ecbb7e] | 489 | return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) ); |
---|
[a2e0687] | 490 | } // build_cond |
---|
[51e076e] | 491 | |
---|
[a2e0687] | 492 | Expression * build_tuple( ExpressionNode * expr_node ) { |
---|
[f43df73] | 493 | list< Expression * > exprs; |
---|
[907eccb] | 494 | buildMoveList( expr_node, exprs ); |
---|
| 495 | return new UntypedTupleExpr( exprs );; |
---|
[a2e0687] | 496 | } // build_tuple |
---|
[9706554] | 497 | |
---|
[a2e0687] | 498 | Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) { |
---|
[f43df73] | 499 | list< Expression * > args; |
---|
[7ecbb7e] | 500 | buildMoveList( expr_node, args ); |
---|
[bf4b4cf] | 501 | return new UntypedExpr( maybeMoveBuild< Expression >(function), args ); |
---|
[a2e0687] | 502 | } // build_func |
---|
[9706554] | 503 | |
---|
[a2e0687] | 504 | Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids ) { |
---|
[7880579] | 505 | Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type |
---|
[630a82a] | 506 | if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type |
---|
[7ecbb7e] | 507 | return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild< Initializer >(kids) ); |
---|
[630a82a] | 508 | // these types do not have associated type information |
---|
| 509 | } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl ) ) { |
---|
[fbcde64] | 510 | if ( newDeclStructDecl->has_body() ) { |
---|
| 511 | return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl ), maybeMoveBuild< Initializer >(kids) ); |
---|
| 512 | } else { |
---|
| 513 | return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild< Initializer >(kids) ); |
---|
| 514 | } // if |
---|
[630a82a] | 515 | } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl ) ) { |
---|
[fbcde64] | 516 | if ( newDeclUnionDecl->has_body() ) { |
---|
| 517 | return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl ), maybeMoveBuild< Initializer >(kids) ); |
---|
| 518 | } else { |
---|
| 519 | return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild< Initializer >(kids) ); |
---|
| 520 | } // if |
---|
[630a82a] | 521 | } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl ) ) { |
---|
[fbcde64] | 522 | if ( newDeclEnumDecl->has_body() ) { |
---|
| 523 | return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl ), maybeMoveBuild< Initializer >(kids) ); |
---|
| 524 | } else { |
---|
| 525 | return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild< Initializer >(kids) ); |
---|
| 526 | } // if |
---|
[630a82a] | 527 | } else { |
---|
| 528 | assert( false ); |
---|
| 529 | } // if |
---|
[a2e0687] | 530 | } // build_compoundLiteral |
---|
[630a82a] | 531 | |
---|
[b87a5ed] | 532 | // Local Variables: // |
---|
| 533 | // tab-width: 4 // |
---|
| 534 | // mode: c++ // |
---|
| 535 | // compile-command: "make install" // |
---|
| 536 | // End: // |
---|