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