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