Ignore:
Timestamp:
Sep 26, 2017, 11:22:08 PM (7 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
d67cdb7
Parents:
9bae71f
Message:

first attempt at new basic-type int128, and length suffix with explicit size

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/ExpressionNode.cc

    r9bae71f r201aeb9  
    1010// Created On       : Sat May 16 13:17:07 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Sep 14 23:09:34 2017
    13 // Update Count     : 690
     12// Last Modified On : Tue Sep 26 11:23:36 2017
     13// Update Count     : 780
    1414//
    1515
     
    6060static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }
    6161
     62static 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
     67static 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
    6295static void sepNumeric( string & str, string & units ) {
    6396        string::size_type posn = str.find_first_of( "`" );
     
    69102
    70103Expression * build_constantInteger( string & str ) {
    71         static const BasicType::Kind kind[2][5] = {
     104        static const BasicType::Kind kind[2][6] = {
    72105                // 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 },
     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, },
    75108        };
    76109
    77         string units;                                                                           // units
     110        string units;
    78111        sepNumeric( str, units );                                                       // separate constant from units
    79112
    80113        bool dec = true, Unsigned = false;                                      // decimal, unsigned constant
    81         int size;                                                                                       // 0 => short, 1 => char, 2 => int, 3 => long int, 4 => long long int, 5 => size_t
     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
    82117        unsigned long long int v;                                                       // converted integral value
    83118        size_t last = str.length() - 1;                                         // last character of constant
     
    140175                        } // if
    141176                        str.erase( last - size - 1, size + 1 );         // remove 'h'/"hh"
     177                } else {                                                                                // suffix "ln" ?
     178                        checkLNInt( str, lnth, size );
    142179                } // if
    143180        } else if ( checkL( str[ last ] ) ) {                           // suffix 'l' ?
     
    163200                str.erase( last - size, size + 1 );                             // remove 'h'/"hh"
    164201        } else if ( checkZ( str[last] ) ) {                                     // suffix 'z' ?
    165                 size = 5;
     202                lnth = 4;
    166203                str.erase( last, 1 );                                                   // remove 'z'
    167         } // if
    168 
     204        } else {                                                                                        // suffix "ln" ?
     205                checkLNInt( str, lnth, size );
     206        } // if
     207
     208        assert( 0 <= size && size < 6 );
    169209        ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][size] ), str, v ) );
    170         if ( Unsigned && size < 2 ) {                                           // less than int ?
    171                 // int i = -1uh => 65535 not -1, so cast is necessary for unsigned, which eliminates warnings for large values.
     210        if ( size < 2 ) {                                                                       // hh or h, less than int ?
     211                // int i = -1uh => 65535 not -1, so cast is necessary for unsigned, which unfortunately eliminates warnings for large values.
    172212                ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][size] ) );
    173         } else if ( size == 5 ) {                                                       // explicit cast to size_t
    174                 ret = new CastExpr( ret, new TypeInstType( Type::Qualifiers(), "size_t", false ) );
     213        } else if ( lnth != -1 ) {                                                      // explicit length ?
     214                if ( lnth == 5 ) {                                                              // int128 ?
     215                        size = 5;
     216                        ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][size] ) );
     217                } else {
     218                        ret = new CastExpr( ret, new TypeInstType( Type::Qualifiers(), lnthsInt[Unsigned][lnth], false ) );
     219                } // if
    175220        } // if
    176221  CLEANUP:
     
    182227        return ret;
    183228} // build_constantInteger
     229
     230
     231static inline void checkLNFloat( string & str, int & lnth, int & size ) {
     232        string::size_type posn = str.find_first_of( "lL" ), start = posn;
     233  if ( posn == string::npos ) return;
     234        size = 2;                                                                                       // assume largest size
     235        lnth = 0;
     236        posn += 1;                                                                                      // advance to size
     237        if ( str[posn] == '3' ) {                                                       // 32
     238                size = 0;
     239        } else if ( str[posn] == '6' ) {                                        // 64
     240                size = 1;
     241        } else if ( str[posn] == '8' || str[posn] == '1' ) { // 80, 128
     242                size = 2;
     243                if ( str[posn] == '1' ) posn += 1;
     244        } else {
     245                assertf( false, "internal error, bad floating point length %s", str.c_str() );
     246        } // if
     247        posn += 1;
     248        str.erase( start, posn - start + 1 );                           // remove length suffix
     249} // checkLNFloat
     250
    184251
    185252Expression * build_constantFloat( string & str ) {
     
    189256        };
    190257
    191         string units;                                                                           // units
     258        string units;
    192259        sepNumeric( str, units );                                                       // separate constant from units
    193260
    194261        bool complx = false;                                                            // real, complex
    195         int size = 1;                                                                           // 0 => float, 1 => double (default), 2 => long double
     262        int size = 1;                                                                           // 0 => float, 1 => double, 2 => long double
     263        int lnth = -1;                                                                          // literal length
    196264        // floating-point constant has minimum of 2 characters: 1. or .1
    197265        size_t last = str.length() - 1;
     
    211279        } else if ( checkL( str[last] ) ) {                                     // long double ?
    212280                size = 2;
     281        } else {
     282                size = 1;                                                                               // double (default)
     283                checkLNFloat( str, lnth, size );
    213284        } // if
    214285        if ( ! complx && checkI( str[last - 1] ) ) {            // imaginary ?
     
    216287        } // if
    217288
     289        assert( 0 <= size && size < 3 );
    218290        Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][size] ), str, v ) );
     291        if ( lnth != -1 ) {                                                                     // explicit length ?
     292                ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[complx][size] ) );
     293        } // if
    219294        if ( units.length() != 0 ) {
    220295                ret = new UntypedExpr( new NameExpr( units ), { ret } );
Note: See TracChangeset for help on using the changeset viewer.