Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/ExpressionNode.cc

    r65cdc1e ra67b60e  
    99// Author           : Rodolfo G. Esteves
    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
    14 //
    15 
    16 #include <climits>                                                                              // access INT_MAX, UINT_MAX, LONG_MAX, ULONG_MAX, LLONG_MAX
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Wed Jun 28 21:08:15 2017
     13// Update Count     : 542
     14//
     15
     16#include <cassert>
     17#include <cctype>
     18#include <climits>
     19#include <cstdio>
     20#include <algorithm>
    1721#include <sstream>
    1822
     
    2226#include "SynTree/Expression.h"
    2327#include "SynTree/Declaration.h"
     28#include "Common/UnimplementedError.h"
    2429#include "parserutility.h"
     30#include "Common/utility.h"
    2531
    2632using namespace std;
     
    4046// type.
    4147
    42 extern const Type::Qualifiers noQualifiers;             // no qualifiers on constants
     48Type::Qualifiers emptyQualifiers;                               // no qualifiers on constants
    4349
    4450static inline bool checkU( char c ) { return c == 'u' || c == 'U'; }
     
    4955static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }
    5056
    51 Expression * build_constantInteger( const std::string & str ) {
     57Expression *build_constantInteger( const std::string & str ) {
    5258        static const BasicType::Kind kind[2][3] = {
    5359                { BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt },
     
    5662        bool dec = true, Unsigned = false;                                      // decimal, unsigned constant
    5763        int size;                                                                                       // 0 => int, 1 => long, 2 => long long
    58         unsigned long long int v;                                                       // converted integral value
     64        unsigned long long int v;                                                               // converted integral value
    5965        size_t last = str.length() - 1;                                         // last character of constant
    60         Expression * ret;
    61 
    62         // special constants
    63         if ( str == "0" ) {
    64                 ret = new ConstantExpr( Constant( (Type *)new ZeroType( noQualifiers ), str, (unsigned long long int)0 ) );
    65                 goto CLEANUP;
    66         } // if
    67         if ( str == "1" ) {
    68                 ret = new ConstantExpr( Constant( (Type *)new OneType( noQualifiers ), str, (unsigned long long int)1 ) );
    69                 goto CLEANUP;
    70         } // if
    71        
     66
    7267        if ( str[0] == '0' ) {                                                          // octal/hex constant ?
    7368                dec = false;
     
    123118        } // if
    124119
    125         ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][size] ), str, v ) );
    126   CLEANUP:
     120        Expression * ret = new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[Unsigned][size] ), str, v ) );
    127121        delete &str;                                                                            // created by lex
    128122        return ret;
    129123} // build_constantInteger
    130124
    131 Expression * build_constantFloat( const std::string & str ) {
     125Expression *build_constantFloat( const std::string & str ) {
    132126        static const BasicType::Kind kind[2][3] = {
    133127                { BasicType::Float, BasicType::Double, BasicType::LongDouble },
     
    159153        } // if
    160154
    161         Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][size] ), str, v ) );
     155        Expression * ret = new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[complx][size] ), str, v ) );
    162156        delete &str;                                                                            // created by lex
    163157        return ret;
    164158} // build_constantFloat
    165159
    166 Expression * build_constantChar( const std::string & str ) {
    167         Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) );
     160Expression *build_constantChar( const std::string & str ) {
     161        Expression * ret = new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) );
    168162        delete &str;                                                                            // created by lex
    169163        return ret;
    170164} // build_constantChar
    171165
    172 ConstantExpr * build_constantStr( const std::string & str ) {
     166ConstantExpr *build_constantStr( const std::string & str ) {
    173167        // string should probably be a primitive type
    174         ArrayType * at = new ArrayType( noQualifiers, new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char ),
    175                                                                    new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"'
     168        ArrayType *at = new ArrayType( emptyQualifiers, new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char ),
     169                                                                   new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ),  // +1 for '\0' and -2 for '"'
    176170                                                                   false, false );
    177         ConstantExpr * ret = new ConstantExpr( Constant( at, str, (unsigned long long int)0 ) ); // constant 0 is ignored for pure string value
     171        // constant 0 is ignored for pure string value
     172        ConstantExpr * ret = new ConstantExpr( Constant( at, str, (unsigned long long int)0 ) );
    178173        delete &str;                                                                            // created by lex
    179174        return ret;
    180175} // build_constantStr
     176
     177Expression *build_constantZeroOne( const std::string & str ) {
     178        Expression * ret = new ConstantExpr( Constant( str == "0" ? (Type *)new ZeroType( emptyQualifiers ) : (Type*)new OneType( emptyQualifiers ), str,
     179                                                                                                   str == "0" ? (unsigned long long int)0 : (unsigned long long int)1 ) );
     180        delete &str;                                                                            // created by lex
     181        return ret;
     182} // build_constantChar
    181183
    182184Expression * build_field_name_FLOATINGconstant( const std::string & str ) {
     
    207209} // build_field_name_fraction_constants
    208210
     211
     212
    209213Expression * build_field_name_REALFRACTIONconstant( const std::string & str ) {
    210214        if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) throw SemanticError( "invalid tuple index " + str );
     
    221225} // build_field_name_REALDECIMALconstant
    222226
    223 NameExpr * build_varref( const string * name ) {
    224         NameExpr * expr = new NameExpr( *name, nullptr );
     227NameExpr * build_varref( const string *name ) {
     228        NameExpr *expr = new NameExpr( *name, nullptr );
    225229        delete name;
    226230        return expr;
    227 } // build_varref
    228 
    229 
    230 static const char * OperName[] = {                                              // must harmonize with OperKinds
     231}
     232
     233static const char *OperName[] = {
    231234        // diadic
    232         "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&",
     235        "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?*?", "?/?", "?%?", "||", "&&",
    233236        "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
    234         "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
     237        "?=?", "?@=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
    235238        "?[?]", "...",
    236239        // monadic
    237240        "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&"
    238 }; // OperName
    239 
    240 Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
    241         Type * targetType = maybeMoveBuildType( decl_node );
     241};
     242
     243Expression *build_cast( DeclarationNode *decl_node, ExpressionNode *expr_node ) {
     244        Type *targetType = maybeMoveBuildType( decl_node );
    242245        if ( dynamic_cast< VoidType * >( targetType ) ) {
    243246                delete targetType;
     
    246249                return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType );
    247250        } // if
    248 } // build_cast
    249 
    250 Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
    251         Type * targetType = maybeMoveBuildType( decl_node );
    252         Expression * castArg = maybeMoveBuild< Expression >( expr_node );
    253         return new VirtualCastExpr( castArg, targetType );
    254 } // build_virtual_cast
    255 
    256 Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) {
    257         UntypedMemberExpr * ret = new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) );
    258         return ret;
    259 } // build_fieldSel
    260 
    261 Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) {
    262         UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) );
     251}
     252
     253Expression *build_fieldSel( ExpressionNode *expr_node, Expression *member ) {
     254        UntypedMemberExpr *ret = new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) );
     255        return ret;
     256}
     257
     258Expression *build_pfieldSel( ExpressionNode *expr_node, Expression *member ) {
     259        UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
    263260        deref->location = expr_node->location;
    264261        deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) );
    265         UntypedMemberExpr * ret = new UntypedMemberExpr( member, deref );
    266         return ret;
    267 } // build_pfieldSel
    268 
    269 Expression * build_addressOf( ExpressionNode * expr_node ) {
     262        UntypedMemberExpr *ret = new UntypedMemberExpr( member, deref );
     263        return ret;
     264}
     265
     266Expression *build_addressOf( ExpressionNode *expr_node ) {
    270267                return new AddressExpr( maybeMoveBuild< Expression >(expr_node) );
    271 } // build_addressOf
    272 
    273 Expression * build_sizeOfexpr( ExpressionNode * expr_node ) {
     268}
     269Expression *build_sizeOfexpr( ExpressionNode *expr_node ) {
    274270        return new SizeofExpr( maybeMoveBuild< Expression >(expr_node) );
    275 } // build_sizeOfexpr
    276 
    277 Expression * build_sizeOftype( DeclarationNode * decl_node ) {
     271}
     272Expression *build_sizeOftype( DeclarationNode *decl_node ) {
    278273        return new SizeofExpr( maybeMoveBuildType( decl_node ) );
    279 } // build_sizeOftype
    280 
    281 Expression * build_alignOfexpr( ExpressionNode * expr_node ) {
     274}
     275Expression *build_alignOfexpr( ExpressionNode *expr_node ) {
    282276        return new AlignofExpr( maybeMoveBuild< Expression >(expr_node) );
    283 } // build_alignOfexpr
    284 
    285 Expression * build_alignOftype( DeclarationNode * decl_node ) {
     277}
     278Expression *build_alignOftype( DeclarationNode *decl_node ) {
    286279        return new AlignofExpr( maybeMoveBuildType( decl_node) );
    287 } // build_alignOftype
    288 
    289 Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) {
     280}
     281Expression *build_offsetOf( DeclarationNode *decl_node, NameExpr *member ) {
    290282        Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() );
    291283        delete member;
    292284        return ret;
    293 } // build_offsetOf
    294 
    295 Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ) {
     285}
     286
     287Expression *build_and_or( ExpressionNode *expr_node1, ExpressionNode *expr_node2, bool kind ) {
    296288        return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind );
    297 } // build_and_or
    298 
    299 Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) {
     289}
     290
     291Expression *build_unary_val( OperKinds op, ExpressionNode *expr_node ) {
    300292        std::list< Expression * > args;
    301293        args.push_back( maybeMoveBuild< Expression >(expr_node) );
    302294        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
    303 } // build_unary_val
    304 
    305 Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node ) {
     295}
     296Expression *build_unary_ptr( OperKinds op, ExpressionNode *expr_node ) {
    306297        std::list< Expression * > args;
    307298        args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node) ) );
    308299        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
    309 } // build_unary_ptr
    310 
    311 Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
     300}
     301Expression *build_binary_val( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
    312302        std::list< Expression * > args;
    313303        args.push_back( maybeMoveBuild< Expression >(expr_node1) );
    314304        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
    315305        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
    316 } // build_binary_val
    317 
    318 Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
     306}
     307Expression *build_binary_ptr( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
    319308        std::list< Expression * > args;
    320309        args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node1) ) );
    321310        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
    322311        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
    323 } // build_binary_ptr
    324 
    325 Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) {
     312}
     313
     314Expression *build_cond( ExpressionNode *expr_node1, ExpressionNode *expr_node2, ExpressionNode *expr_node3 ) {
    326315        return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) );
    327 } // build_cond
    328 
    329 Expression * build_comma( ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
     316}
     317
     318Expression *build_comma( ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
    330319        return new CommaExpr( maybeMoveBuild< Expression >(expr_node1), maybeMoveBuild< Expression >(expr_node2) );
    331 } // build_comma
    332 
    333 Expression * build_attrexpr( NameExpr * var, ExpressionNode * expr_node ) {
     320}
     321
     322Expression *build_attrexpr( NameExpr *var, ExpressionNode * expr_node ) {
    334323        return new AttrExpr( var, maybeMoveBuild< Expression >(expr_node) );
    335 } // build_attrexpr
    336 
    337 Expression * build_attrtype( NameExpr * var, DeclarationNode * decl_node ) {
     324}
     325Expression *build_attrtype( NameExpr *var, DeclarationNode * decl_node ) {
    338326        return new AttrExpr( var, maybeMoveBuildType( decl_node ) );
    339 } // build_attrtype
    340 
    341 Expression * build_tuple( ExpressionNode * expr_node ) {
     327}
     328
     329Expression *build_tuple( ExpressionNode * expr_node ) {
    342330        std::list< Expression * > exprs;
    343331        buildMoveList( expr_node, exprs );
    344332        return new UntypedTupleExpr( exprs );;
    345 } // build_tuple
    346 
    347 Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
     333}
     334
     335Expression *build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
    348336        std::list< Expression * > args;
    349337        buildMoveList( expr_node, args );
    350338        return new UntypedExpr( maybeMoveBuild< Expression >(function), args, nullptr );
    351 } // build_func
    352 
    353 Expression * build_range( ExpressionNode * low, ExpressionNode * high ) {
     339}
     340
     341Expression *build_range( ExpressionNode * low, ExpressionNode *high ) {
    354342        return new RangeExpr( maybeMoveBuild< Expression >( low ), maybeMoveBuild< Expression >( high ) );
    355 } // build_range
    356 
    357 Expression * build_asmexpr( ExpressionNode * inout, ConstantExpr * constraint, ExpressionNode * operand ) {
     343}
     344
     345Expression *build_asmexpr( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand ) {
    358346        return new AsmExpr( maybeMoveBuild< Expression >( inout ), constraint, maybeMoveBuild< Expression >(operand) );
    359 } // build_asmexpr
    360 
    361 Expression * build_valexpr( StatementNode * s ) {
     347}
     348
     349Expression *build_valexpr( StatementNode *s ) {
    362350        return new StmtExpr( dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(s) ) );
    363 } // build_valexpr
    364 
    365 Expression * build_typevalue( DeclarationNode * decl ) {
     351}
     352Expression *build_typevalue( DeclarationNode *decl ) {
    366353        return new TypeExpr( maybeMoveBuildType( decl ) );
    367 } // build_typevalue
    368 
    369 Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids ) {
     354}
     355
     356Expression *build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids ) {
    370357        Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type
    371358        if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type
     
    393380                assert( false );
    394381        } // if
    395 } // build_compoundLiteral
     382}
    396383
    397384// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.