Ignore:
Timestamp:
Sep 5, 2017, 3:41:04 PM (8 years ago)
Author:
Thierry Delisle <tdelisle@…>
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:
416cc86
Parents:
800d275 (diff), 3f8dd01 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/ExpressionNode.cc

    r800d275 r235b41f  
    77// ExpressionNode.cc --
    88//
    9 // Author           : Rodolfo G. Esteves
     9// Author           : Peter A. Buhr
    1010// Created On       : Sat May 16 13:17:07 2015
    11 // Last Modified By : Andrew Beach
    12 // Last Modified On : Wed Aug  2 11:12:00 2017
    13 // Update Count     : 568
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Sun Sep  3 22:21:21 2017
     13// Update Count     : 639
    1414//
    1515
     
    5858static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }
    5959
    60 Expression * build_constantInteger( const std::string & str ) {
     60static void sepNumeric( string & str, string & units ) {
     61        string::size_type posn = str.find_first_of( "`" );
     62        if ( posn != string::npos ) {
     63                units = "?" + str.substr( posn );                               // extract units
     64                str.erase( posn );                                                              // remove units
     65        } // if
     66} // sepNumeric
     67
     68Expression * build_constantInteger( string & str ) {
    6169        static const BasicType::Kind kind[2][3] = {
    6270                { BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt },
    6371                { BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt },
    6472        };
     73
     74        string units;                                                                           // units
     75        sepNumeric( str, units );                                                       // separate constant from units
     76
    6577        bool dec = true, Unsigned = false;                                      // decimal, unsigned constant
    6678        int size;                                                                                       // 0 => int, 1 => long, 2 => long long
     
    6981        Expression * ret;
    7082
     83        // ROB: what do we do with units on 0 and 1?
    7184        // special constants
    7285        if ( str == "0" ) {
     
    134147        ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][size] ), str, v ) );
    135148  CLEANUP:
     149        if ( units.length() != 0 ) {
     150                ret = new UntypedExpr( new NameExpr( units ), { ret } );
     151        } // if
     152
    136153        delete &str;                                                                            // created by lex
    137154        return ret;
    138155} // build_constantInteger
    139156
    140 Expression * build_constantFloat( const std::string & str ) {
     157Expression * build_constantFloat( string & str ) {
    141158        static const BasicType::Kind kind[2][3] = {
    142159                { BasicType::Float, BasicType::Double, BasicType::LongDouble },
    143160                { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
    144161        };
     162
     163        string units;                                                                           // units
     164        sepNumeric( str, units );                                                       // separate constant from units
    145165
    146166        bool complx = false;                                                            // real, complex
     
    169189
    170190        Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][size] ), str, v ) );
     191        if ( units.length() != 0 ) {
     192                ret = new UntypedExpr( new NameExpr( units ), { ret } );
     193        } // if
     194
    171195        delete &str;                                                                            // created by lex
    172196        return ret;
    173197} // build_constantFloat
    174198
    175 Expression * build_constantChar( const std::string & str ) {
     199static void sepString( string & str, string & units, char delimit ) {
     200        string::size_type posn = str.find_last_of( delimit ) + 1;
     201        if ( posn != str.length() ) {
     202                units = "?" + str.substr( posn );                               // extract units
     203                str.erase( posn );                                                              // remove units
     204        } // if
     205} // sepString
     206
     207Expression * build_constantChar( string & str ) {
     208        string units;                                                                           // units
     209        sepString( str, units, '\'' );                                          // separate constant from units
     210
    176211        Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) );
     212        if ( units.length() != 0 ) {
     213                ret = new UntypedExpr( new NameExpr( units ), { ret } );
     214        } // if
     215
    177216        delete &str;                                                                            // created by lex
    178217        return ret;
    179218} // build_constantChar
    180219
    181 ConstantExpr * build_constantStr( const std::string & str ) {
    182         // string should probably be a primitive type
    183         ArrayType * at = new ArrayType( noQualifiers, new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char ),
    184                                                                    new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"'
    185                                                                    false, false );
    186         ConstantExpr * ret = new ConstantExpr( Constant( at, str, (unsigned long long int)0 ) ); // constant 0 is ignored for pure string value
     220Expression * build_constantStr( string & str ) {
     221        string units;                                                                           // units
     222        sepString( str, units, '"' );                                           // separate constant from units
     223
     224        BasicType::Kind strtype = BasicType::Char;                      // default string type
     225        switch ( str[0] ) {                                                                     // str has >= 2 characters, i.e, null string ""
     226          case 'u':
     227                if ( str[1] == '8' ) break;                                             // utf-8 characters
     228                strtype = BasicType::ShortUnsignedInt;
     229                break;
     230          case 'U':
     231                strtype = BasicType::UnsignedInt;
     232                break;
     233          case 'L':
     234                strtype = BasicType::SignedInt;
     235                break;
     236        } // switch
     237        ArrayType * at = new ArrayType( noQualifiers, new BasicType( Type::Qualifiers( Type::Const ), strtype ),
     238                                                                        new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"'
     239                                                                        false, false );
     240        Expression * ret = new ConstantExpr( Constant( at, str, (unsigned long long int)0 ) ); // constant 0 is ignored for pure string value
     241        if ( units.length() != 0 ) {
     242                ret = new UntypedExpr( new NameExpr( units ), { ret } );
     243        } // if
     244
    187245        delete &str;                                                                            // created by lex
    188246        return ret;
    189247} // build_constantStr
    190248
    191 Expression * build_field_name_FLOATINGconstant( const std::string & str ) {
     249Expression * build_field_name_FLOATINGconstant( const string & str ) {
    192250        // str is of the form A.B -> separate at the . and return member expression
    193251        int a, b;
    194252        char dot;
    195         std::stringstream ss( str );
     253        stringstream ss( str );
    196254        ss >> a >> dot >> b;
    197255        UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) );
     
    207265                } else {
    208266                        return new UntypedMemberExpr( fracts, fieldName );
    209                 }
    210         }
     267                } // if
     268        } // if
    211269        return fieldName;
    212270} // make_field_name_fraction_constants
     
    216274} // build_field_name_fraction_constants
    217275
    218 Expression * build_field_name_REALFRACTIONconstant( const std::string & str ) {
     276Expression * build_field_name_REALFRACTIONconstant( const string & str ) {
    219277        if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) throw SemanticError( "invalid tuple index " + str );
    220         Expression * ret = build_constantInteger( *new std::string( str.substr(1) ) );
     278        Expression * ret = build_constantInteger( *new string( str.substr(1) ) );
    221279        delete &str;
    222280        return ret;
    223281} // build_field_name_REALFRACTIONconstant
    224282
    225 Expression * build_field_name_REALDECIMALconstant( const std::string & str ) {
     283Expression * build_field_name_REALDECIMALconstant( const string & str ) {
    226284        if ( str[str.size()-1] != '.' ) throw SemanticError( "invalid tuple index " + str );
    227         Expression * ret = build_constantInteger( *new std::string( str.substr( 0, str.size()-1 ) ) );
     285        Expression * ret = build_constantInteger( *new string( str.substr( 0, str.size()-1 ) ) );
    228286        delete &str;
    229287        return ret;
     
    236294} // build_varref
    237295
    238 
     296// TODO: get rid of this and OperKinds and reuse code from OperatorTable
    239297static const char * OperName[] = {                                              // must harmonize with OperKinds
    240298        // diadic
     
    244302        "?[?]", "...",
    245303        // monadic
    246         "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&"
     304        "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--",
    247305}; // OperName
    248306
     
    307365
    308366Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) {
    309         std::list< Expression * > args;
     367        list< Expression * > args;
    310368        args.push_back( maybeMoveBuild< Expression >(expr_node) );
    311369        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
     
    313371
    314372Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node ) {
    315         std::list< Expression * > args;
    316         args.push_back(  maybeMoveBuild< Expression >(expr_node) ); // xxx
     373        list< Expression * > args;
     374        args.push_back(  maybeMoveBuild< Expression >(expr_node) ); // xxx -- this is exactly the same as the val case now, refactor this code.
    317375        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
    318376} // build_unary_ptr
    319377
    320378Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
    321         std::list< Expression * > args;
     379        list< Expression * > args;
    322380        args.push_back( maybeMoveBuild< Expression >(expr_node1) );
    323381        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
     
    326384
    327385Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
    328         std::list< Expression * > args;
     386        list< Expression * > args;
    329387        args.push_back( maybeMoveBuild< Expression >(expr_node1) );
    330388        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
     
    349407
    350408Expression * build_tuple( ExpressionNode * expr_node ) {
    351         std::list< Expression * > exprs;
     409        list< Expression * > exprs;
    352410        buildMoveList( expr_node, exprs );
    353411        return new UntypedTupleExpr( exprs );;
     
    355413
    356414Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
    357         std::list< Expression * > args;
     415        list< Expression * > args;
    358416        buildMoveList( expr_node, args );
    359417        return new UntypedExpr( maybeMoveBuild< Expression >(function), args, nullptr );
     
    364422} // build_range
    365423
    366 Expression * build_asmexpr( ExpressionNode * inout, ConstantExpr * constraint, ExpressionNode * operand ) {
     424Expression * build_asmexpr( ExpressionNode * inout, Expression * constraint, ExpressionNode * operand ) {
    367425        return new AsmExpr( maybeMoveBuild< Expression >( inout ), constraint, maybeMoveBuild< Expression >(operand) );
    368426} // build_asmexpr
Note: See TracChangeset for help on using the changeset viewer.