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