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