Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/ExpressionNode.cc

    r47534159 r2a4b088  
    1010// Created On       : Sat May 16 13:17:07 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Oct  5 16:37:24 2015
    13 // Update Count     : 255
     12// Last Modified On : Mon Feb  1 13:32:30 2016
     13// Update Count     : 271
    1414//
    1515
     
    2424#include "SynTree/Constant.h"
    2525#include "SynTree/Expression.h"
    26 #include "UnimplementedError.h"
     26#include "Common/UnimplementedError.h"
    2727#include "parseutility.h"
    28 #include "utility.h"
     28#include "Common/utility.h"
    2929
    3030using namespace std;
     
    9191static inline bool checkL( char c ) { return c == 'l' || c == 'L'; }
    9292static inline bool checkF( char c ) { return c == 'f' || c == 'F'; }
     93static inline bool checkD( char c ) { return c == 'd' || c == 'D'; }
     94static inline bool checkI( char c ) { return c == 'i' || c == 'I'; }
    9395static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }
    9496
     
    114116                                { BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt },
    115117                        };
    116                         size_t last = value.length() - 1;                       // last character of constant
    117                         unsigned long long v;                                           // converted integral value
    118118                        bool dec = true, Unsigned = false;                      // decimal, unsigned constant
    119119                        int size;                                                                       // 0 => int, 1 => long, 2 => long long
     120                        unsigned long long v;                                           // converted integral value
     121                        size_t last = value.length() - 1;                       // last character of constant
    120122
    121123                        if ( value[0] == '0' ) {                                        // octal constant ?
     
    176178          case Float:
    177179                {
    178                         size_t len = value.length() - 1;
    179 
    180                         btype = BasicType::Double;                                      // default
    181                         if ( checkF( value[len] ) ) {                           // float ?
    182                                 btype = BasicType::Float;
    183                         } // if
    184                         if ( checkL( value[len] ) ) {                           // long double ?
    185                                 btype = BasicType::LongDouble;
    186                         } // if
     180                        static const BasicType::Kind kind[2][3] = {
     181                                { BasicType::Float, BasicType::Double, BasicType::LongDouble },
     182                                { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
     183                        };
     184                        bool complx = false;                                            // real, complex
     185                        int size = 1;                                                           // 0 => float, 1 => double (default), 2 => long double
     186                        // floating-point constant has minimum of 2 characters: 1. or .1
     187                        size_t last = value.length() - 1;
     188
     189                        if ( checkI( value[last] ) ) {                          // imaginary ?
     190                                complx = true;
     191                                last -= 1;                                                              // backup one character
     192                        } // if
     193                        if ( checkF( value[last] ) ) {                          // float ?
     194                                size = 0;
     195                        } else if ( checkD( value[last] ) ) {           // double ?
     196                                size = 1;
     197                        } else if ( checkL( value[last] ) ) {           // long double ?
     198                                size = 2;
     199                        } // if
     200                        if ( ! complx && checkI( value[last - 1] ) ) { // imaginary ?
     201                                complx = true;
     202                        } // if
     203                        btype = kind[complx][size];                                     // lookup constant type
    187204                        break;
    188205                }
     
    365382//##############################################################################
    366383
     384static const char *opName[] = {
     385        "TupleC", "Comma", "TupleFieldSel", // "TuplePFieldSel", // n-adic
     386        // triadic
     387        "Cond", "NCond",
     388        // diadic
     389        "SizeOf", "AlignOf", "OffsetOf", "Attr", "CompLit", "?+?", "?-?", "?*?", "?/?", "?%?", "||", "&&",
     390        "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
     391        "?=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
     392        "?[?]", "FieldSel", "PFieldSel", "Range",
     393        // monadic
     394        "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&"
     395};
     396
    367397OperatorNode::OperatorNode( Type t ) : type( t ) {}
    368398
     
    378408void OperatorNode::printOneLine( std::ostream &os, int indent ) const {
    379409        printDesignation( os );
    380         os << OpName[ type ] << ' ';
     410        os << opName[ type ] << ' ';
    381411}
    382412
    383413void OperatorNode::print( std::ostream &os, int indent ) const{
    384414        printDesignation( os );
    385         os << string( indent, ' ' ) << "Operator: " << OpName[type] << endl;
     415        os << string( indent, ' ' ) << "Operator: " << opName[type] << endl;
    386416        return;
    387417}
    388418
    389419const char *OperatorNode::get_typename( void ) const{
    390         return OpName[ type ];
    391 }
    392 
    393 const char *OperatorNode::OpName[] = {
    394         "TupleC",  "Comma", "TupleFieldSel",// "TuplePFieldSel", //n-adic
    395         // triadic
    396         "Cond",   "NCond",
    397         // diadic
    398         "SizeOf",     "AlignOf", "Attr", "CompLit", "Plus",    "Minus",   "Mul",     "Div",     "Mod",      "Or",
    399         "And",       "BitOr",   "BitAnd",  "Xor",     "Cast",    "LShift",  "RShift",  "LThan",   "GThan",
    400         "LEThan",    "GEThan", "Eq",      "Neq",     "Assign",  "MulAssn", "DivAssn", "ModAssn", "PlusAssn",
    401         "MinusAssn", "LSAssn", "RSAssn",  "AndAssn", "ERAssn",  "OrAssn",  "Index",   "FieldSel","PFieldSel",
    402         "Range",
    403         // monadic
    404         "UnPlus", "UnMinus", "AddressOf", "PointTo", "Neg", "BitNeg", "Incr", "IncrPost", "Decr", "DecrPost", "LabelAddress"
    405 };
     420        return opName[ type ];
     421}
    406422
    407423//##############################################################################
     
    439455}
    440456
    441 // the names that users use to define operator functions
    442 static const char *opFuncName[] = {
    443         "",             "",             "",
    444         "",             "",
    445         //diadic
    446         "",             "",             "",             "",             "?+?",          "?-?",  "?*?",  "?/?",  "?%?",  "",              "",
    447         "?|?",          "?&?",          "?^?",  "",             "?<<?", "?>>?", "?<?",  "?>?",  "?<=?",
    448         "?>=?",         "?==?",         "?!=?", "?=?",  "?*=?", "?/=?", "?%=?", "?+=?", "?-=?",
    449         "?<<=?",        "?>>=?",        "?&=?", "?^=?", "?|=?", "?[?]", "",             "",             "Range",
    450         //monadic
    451         "+?",           "-?",           "",             "*?",   "!?",   "~?",   "++?",  "?++",  "--?",  "?--",  "&&"
    452 };
    453 
    454 #include "utility.h"
     457#include "Common/utility.h"
    455458
    456459Expression *CompositeExprNode::build() const {
     
    529532          case OperatorNode::BitNeg:
    530533          case OperatorNode::LabelAddress:
    531                 return new UntypedExpr( new NameExpr( opFuncName[ op->get_type() ] ), args );
     534                return new UntypedExpr( new NameExpr( opName[ op->get_type() ] ), args );
    532535          case OperatorNode::AddressOf:
    533536                assert( args.size() == 1 );
     
    585588                        return ret;
    586589                }
     590          case OperatorNode::SizeOf:
     591                {
     592                        if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()) ) {
     593                                return new SizeofExpr( arg->get_decl()->buildType());
     594                        } else {
     595                                return new SizeofExpr( args.front());
     596                        } // if
     597                }
    587598          case OperatorNode::AlignOf:
    588599                {
     
    593604                        } // if
    594605                }
    595           case OperatorNode::SizeOf:
    596                 {
    597                         if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()) ) {
    598                                 return new SizeofExpr( arg->get_decl()->buildType());
    599                         } else {
    600                                 return new SizeofExpr( args.front());
    601                         } // if
     606          case OperatorNode::OffsetOf:
     607                {
     608                        assert( args.size() == 2 );
     609                       
     610                        if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args() ) ) {
     611                                NameExpr *member = dynamic_cast<NameExpr *>( args.back() );
     612                                assert( member != 0 );
     613
     614                                return new UntypedOffsetofExpr( arg->get_decl()->buildType(), member->get_name() );
     615                        } else assert( false );
    602616                }
    603617          case OperatorNode::Attr:
     
    650664          default:
    651665                // shouldn't happen
     666                assert( false );
    652667                return 0;
    653668        } // switch
     
    660675        for ( ExpressionNode *cur = arguments; cur != 0; cur = dynamic_cast< ExpressionNode* >( cur->get_link() ) ) {
    661676                cur->printOneLine( os, indent );
    662         }
     677        } // for
    663678        os << ") ";
    664679}
Note: See TracChangeset for help on using the changeset viewer.