Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/ExpressionNode.cc

    r843054c2 r1869adf  
    99// Author           : Rodolfo G. Esteves
    1010// Created On       : Sat May 16 13:17:07 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat May 16 13:19:35 2015
    13 // Update Count     : 2
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Wed Jun 24 16:20:00 2015
     13// Update Count     : 158
    1414//
    1515
     
    1717#include <cctype>
    1818#include <algorithm>
     19#include <sstream>
     20#include <cstdio>
     21#include <climits>
    1922
    2023#include "ParseNode.h"
    21 #include "SynTree/Type.h"
    2224#include "SynTree/Constant.h"
    2325#include "SynTree/Expression.h"
    24 #include "SynTree/Declaration.h"
    2526#include "UnimplementedError.h"
    2627#include "parseutility.h"
     
    3132ExpressionNode::ExpressionNode() : ParseNode(), argName( 0 ) {}
    3233
    33 ExpressionNode::ExpressionNode( string *name_) : ParseNode( *name_ ), argName( 0 ) {
    34         delete name_;
    35 }
     34ExpressionNode::ExpressionNode( const string *name_ ) : ParseNode( name_ ), argName( 0 ) {}
    3635
    3736ExpressionNode::ExpressionNode( const ExpressionNode &other ) : ParseNode( other.name ) {
     
    4342}
    4443
    45 ExpressionNode * ExpressionNode::set_asArgName( std::string *aName ) {
     44ExpressionNode * ExpressionNode::set_asArgName( const std::string *aName ) {
    4645        argName = new VarRefNode( aName );
    4746        return this;
     
    5554void ExpressionNode::printDesignation( std::ostream &os, int indent ) const {
    5655        if ( argName ) {
    57                 os << string(' ', indent ) << "(designated by:  ";
     56                os << string( indent, ' ' ) << "(designated by:  ";
    5857                argName->printOneLine( os, indent );
    5958                os << ")" << std::endl;
     
    6160}
    6261
     62//##############################################################################
     63
    6364NullExprNode::NullExprNode() {}
    6465
     
    8586}
    8687
    87 //  enum ConstantNode::Type =  { Integer, Float, Character, String, Range }
    88 
    89 ConstantNode::ConstantNode( void ) : ExpressionNode(), sign( true ), longs(0), size(0) {}
    90 
    91 ConstantNode::ConstantNode( string *name_) : ExpressionNode( name_), sign( true ), longs(0), size(0) {}
    92 
    93 ConstantNode::ConstantNode( Type t, string *inVal ) : type( t ), sign( true ), longs(0), size(0) {
    94         if ( inVal ) {
    95                 value = *inVal;
    96                 delete inVal;
    97         } else {
    98                 value = "";
    99         } // if
    100 
    101         classify( value );
    102 }
    103 
    104 ConstantNode::ConstantNode( const ConstantNode &other ) : ExpressionNode( other ), type( other.type ), value( other.value ), sign( other.sign ),
    105                                                                                                                   base( other.base ), longs( other.longs ), size( other.size ) {
    106 }
    107 
    108 // for some reason, std::tolower doesn't work as an argument to std::transform in g++ 3.1
    109 inline char tolower_hack( char c ) {
    110         return std::tolower( c );
    111 }
    112 
    113 void ConstantNode::classify( std::string &str ) {
     88//##############################################################################
     89
     90static inline bool checkU( char c ) { return c == 'u' || c == 'U'; }
     91static inline bool checkL( char c ) { return c == 'l' || c == 'L'; }
     92static inline bool checkF( char c ) { return c == 'f' || c == 'F'; }
     93static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }
     94
     95// Difficult to separate extra parts of constants during lexing because actions are not allow in the middle of patterns:
     96//
     97//              prefix action constant action suffix
     98//
     99// Alternatively, breaking a pattern using BEGIN does not work if the following pattern can be empty:
     100//
     101//              constant BEGIN CONT ...
     102//              <CONT>(...)? BEGIN 0 ... // possible empty suffix
     103//
     104// because the CONT rule is NOT triggered if the pattern is empty. Hence, constants are reparsed here to determine their
     105// type.
     106
     107ConstantNode::ConstantNode( Type t, string *inVal ) : type( t ), value( *inVal ) {
     108        // lexing divides constants into 4 kinds
     109        switch ( type ) {
     110          case Integer:
     111                {
     112                        static const BasicType::Kind kind[2][3] = {
     113                                { BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt },
     114                                { BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt },
     115                        };
     116                        size_t last = value.length() - 1;                       // last character of constant
     117                        unsigned long long v;                                           // converted integral value
     118                        bool dec = true, Unsigned = false;                      // decimal, unsigned constant
     119                        int size;                                                                       // 0 => int, 1 => long, 2 => long long
     120
     121                        if ( value[0] == '0' ) {                                        // octal constant ?
     122                                dec = false;
     123                                if ( last != 0 && checkX( value[1] ) ) { // hex constant ?
     124                                        sscanf( (char *)value.c_str(), "%llx", &v );
     125                                        //printf( "%llx %llu\n", v, v );
     126                                } else {
     127                                        sscanf( (char *)value.c_str(), "%llo", &v );
     128                                        //printf( "%llo %llu\n", v, v );
     129                                } // if
     130                        } else {                                                                        // decimal constant ?
     131                                sscanf( (char *)value.c_str(), "%llu", &v );
     132                                //printf( "%llu %llu\n", v, v );
     133                        } // if
     134
     135                        if ( v <= INT_MAX ) {                                           // signed int
     136                                size = 0;
     137                        } else if ( v <= UINT_MAX && ! dec ) {          // unsigned int
     138                                size = 0;
     139                                Unsigned = true;                                                // unsigned
     140                        } else if ( v <= LONG_MAX ) {                           // signed long int
     141                                size = 1;
     142                        } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int
     143                                size = 1;
     144                                Unsigned = true;                                                // unsigned long int
     145                        } else if ( v <= LLONG_MAX ) {                          // signed long long int
     146                                size = 2;
     147                        } else {                                                                        // unsigned long long int
     148                                size = 2;
     149                                Unsigned = true;                                                // unsigned long long int
     150                        } // if
     151
     152                        if ( checkU( value[last] ) ) {                          // suffix 'u' ?
     153                                Unsigned = true;
     154                                if ( last > 0 && checkL( value[ last - 1 ] ) ) { // suffix 'l' ?
     155                                        size = 1;
     156                                        if ( last > 1 && checkL( value[ last - 2 ] ) ) { // suffix 'll' ?
     157                                                size = 2;
     158                                        } // if
     159                                } // if
     160                        } else if ( checkL( value[ last ] ) ) {         // suffix 'l' ?
     161                                size = 1;
     162                                if ( last > 0 && checkL( value[ last - 1 ] ) ) { // suffix 'll' ?
     163                                        size = 2;
     164                                        if ( last > 1 && checkU( value[ last - 2 ] ) ) { // suffix 'u' ?
     165                                                Unsigned = true;
     166                                        } // if
     167                                } else {
     168                                        if ( last > 0 && checkU( value[ last - 1 ] ) ) { // suffix 'u' ?
     169                                                Unsigned = true;
     170                                        } // if
     171                                } // if
     172                        } // if
     173                        btype = kind[Unsigned][size];                           // lookup constant type
     174                        break;
     175                }
     176          case Float:
     177                {
     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
     187                        break;
     188                }
     189          case Character:
     190                btype = BasicType::Char;                                                // default
     191                if ( string( "LUu" ).find( value[0] ) != string::npos ) {
     192                        // ???
     193                } // if
     194                break;
     195          case String:
     196                // array of char
     197                if ( string( "LUu" ).find( value[0] ) != string::npos ) {
     198                        if ( value[0] == 'u' && value[1] == '8' ) {
     199                                // ???
     200                        } else {
     201                                // ???
     202                        } // if
     203                } // if
     204                break;
     205        } // switch
     206} // ConstantNode::ConstantNode
     207
     208ConstantNode *ConstantNode::appendstr( const std::string *newValue ) {
     209        assert( newValue != 0 );
     210        assert( type == String );
     211
     212        // "abc" "def" "ghi" => "abcdefghi", so remove new text from quotes and insert before last quote in old string.
     213        value.insert( value.length() - 1, newValue->substr( 1, newValue->length() - 2 ) );
     214       
     215        delete newValue;                                                                        // allocated by lexer
     216        return this;
     217}
     218
     219void ConstantNode::printOneLine( std::ostream &os, int indent ) const {
     220        os << string( indent, ' ' );
     221        printDesignation( os );
     222
    114223        switch ( type ) {
    115224          case Integer:
    116225          case Float:
    117                 {
    118                         std::string sfx("");
    119                         char c;
    120                         int i = str.length() - 1;
    121 
    122                         while ( i >= 0 && ! isxdigit( c = str.at( i--)) )
    123                                 sfx += c;
    124 
    125                         value = str.substr( 0, i + 2 );
    126 
    127                         // get rid of underscores
    128                         value.erase( remove( value.begin(), value.end(), '_'), value.end());
    129 
    130                         std::transform( sfx.begin(), sfx.end(), sfx.begin(), tolower_hack );
    131 
    132                         if ( sfx.find("ll") != string::npos ) {
    133                                 longs = 2;
    134                         } else if ( sfx.find("l") != string::npos ) {
    135                                 longs = 1;
    136                         } // if
    137 
    138                         assert(( longs >= 0) && ( longs <= 2));
    139 
    140                         if ( sfx.find("u") != string::npos )
    141                                 sign = false;
    142 
    143                         break;
    144                 }
    145           case Character:
    146                 {
    147                         // remove underscores from hex and oct escapes
    148                         if ( str.substr(1,2) == "\\x")
    149                                 value.erase( remove( value.begin(), value.end(), '_'), value.end());
    150 
    151                         break;
    152                 }
    153           default:
    154                 // shouldn't be here
    155                 ;
    156         }
    157 }
    158 
    159 ConstantNode::Type ConstantNode::get_type( void ) const {
    160         return type;
    161 }
    162 
    163 ConstantNode *ConstantNode::append( std::string *newValue ) {
    164         if ( newValue ) {
    165                 if ( type == String ) {
    166                         std::string temp = *newValue;
    167                         value.resize( value.size() - 1 );
    168                         value += newValue->substr(1, newValue->size());
    169                 } else
    170                         value += *newValue;
    171 
    172                 delete newValue;
    173         } // if
    174         return this;
    175 }
    176 
    177 void ConstantNode::printOneLine( std::ostream &os, int indent ) const {
    178         os << string( indent, ' ');
    179         printDesignation( os );
    180 
    181         switch ( type ) {
    182                 /* integers */
    183           case Integer:
    184226                os << value ;
    185227                break;
    186           case Float:
    187                 os << value ;
    188                 break;
    189 
    190228          case Character:
    191229                os << "'" << value << "'";
    192230                break;
    193 
    194231          case String:
    195232                os << '"' << value << '"';
    196233                break;
    197         }
     234        } // switch
    198235
    199236        os << ' ';
     
    206243
    207244Expression *ConstantNode::build() const {
    208         ::Type::Qualifiers q;
    209         BasicType *bt;
    210 
    211         switch ( get_type()) {
    212           case Integer:
    213                 /* Cfr. standard 6.4.4.1 */
    214                 //bt.set_kind( BasicType::SignedInt );
    215                 bt = new BasicType( q, BasicType::SignedInt );
    216                 break;
    217           case Float:
    218                 bt = new BasicType( q, BasicType::Float );
    219                 break;
    220           case Character:
    221                 bt = new BasicType( q, BasicType::Char );
    222                 break;
     245        ::Type::Qualifiers q;                                                           // no qualifiers on constants
     246
     247        switch ( get_type() ) {
    223248          case String:
    224                 // string should probably be a primitive type
    225                 ArrayType *at;
    226                 std::string value = get_value();
    227                 at = new ArrayType( q, new BasicType( q, BasicType::Char ),
    228                                                         new ConstantExpr( Constant( new BasicType( q, BasicType::SignedInt ),
    229                                                                                                                 toString( value.size() - 1 ) ) ),  // account for '\0'
    230                                                         false, false );
    231                 return new ConstantExpr( Constant( at, value ), maybeBuild< Expression >( get_argName() ) );
     249                {
     250                        // string should probably be a primitive type
     251                        ArrayType *at = new ArrayType( q, new BasicType( q, BasicType::Char ),
     252                                                                                   new ConstantExpr(
     253                                                                                           Constant( new BasicType( q, BasicType::UnsignedInt ),
     254                                                                                                                 toString( value.size()+1-2 ) ) ),  // +1 for '\0' and -2 for '"'
     255                                                                                   false, false );
     256                        return new ConstantExpr( Constant( at, value ), maybeBuild< Expression >( get_argName() ) );
     257                }
     258          default:
     259                return new ConstantExpr( Constant( new BasicType( q, btype ), get_value() ), maybeBuild< Expression >( get_argName() ) );
    232260        }
    233         return new ConstantExpr(  Constant( bt, get_value()),  maybeBuild< Expression >( get_argName() ) );
    234 }
     261}
     262
     263//##############################################################################
    235264
    236265VarRefNode::VarRefNode() : isLabel( false ) {}
    237266
    238 VarRefNode::VarRefNode( string *name_, bool labelp ) : ExpressionNode( name_), isLabel( labelp ) {}
     267VarRefNode::VarRefNode( const string *name_, bool labelp ) : ExpressionNode( name_ ), isLabel( labelp ) {}
    239268
    240269VarRefNode::VarRefNode( const VarRefNode &other ) : ExpressionNode( other ), isLabel( other.isLabel ) {
     
    252281void VarRefNode::print( std::ostream &os, int indent ) const {
    253282        printDesignation( os );
    254         os << '\r' << string( indent, ' ') << "Referencing: ";
     283        os << string( indent, ' ' ) << "Referencing: ";
    255284        os << "Variable: " << get_name();
    256285        os << endl;
    257286}
    258287
     288//##############################################################################
     289
    259290OperatorNode::OperatorNode( Type t ) : type( t ) {}
    260291
     
    275306void OperatorNode::print( std::ostream &os, int indent ) const{
    276307        printDesignation( os );
    277         os << '\r' << string( indent, ' ') << "Operator: " << OpName[type] << endl;
     308        os << string( indent, ' ' ) << "Operator: " << OpName[type] << endl;
    278309        return;
    279310}
    280311
    281 std::string OperatorNode::get_typename( void ) const{
    282         return string( OpName[ type ]);
     312const char *OperatorNode::get_typename( void ) const{
     313        return OpName[ type ];
    283314}
    284315
     
    288319        "Cond",   "NCond",
    289320        // diadic
    290         "SizeOf",      "AlignOf", "Attr", "CompLit", "Plus",    "Minus",   "Mul",     "Div",     "Mod",      "Or",
     321        "SizeOf",     "AlignOf", "Attr", "CompLit", "Plus",    "Minus",   "Mul",     "Div",     "Mod",      "Or",
    291322        "And",       "BitOr",   "BitAnd",  "Xor",     "Cast",    "LShift",  "RShift",  "LThan",   "GThan",
    292323        "LEThan",    "GEThan", "Eq",      "Neq",     "Assign",  "MulAssn", "DivAssn", "ModAssn", "PlusAssn",
     
    297328};
    298329
     330//##############################################################################
     331
    299332CompositeExprNode::CompositeExprNode( void ) : ExpressionNode(), function( 0 ), arguments( 0 ) {
    300333}
    301334
    302 CompositeExprNode::CompositeExprNode( string *name_) : ExpressionNode( name_), function( 0 ), arguments( 0 ) {
     335CompositeExprNode::CompositeExprNode( const string *name_ ) : ExpressionNode( name_ ), function( 0 ), arguments( 0 ) {
    303336}
    304337
     
    331364// the names that users use to define operator functions
    332365static const char *opFuncName[] = {
    333         "",  "", "",
    334         "",   "",
    335         // diadic
    336         "",   "", "", "", "?+?",    "?-?",   "?*?",     "?/?",     "?%?",     "",      "",
    337         "?|?",  "?&?",  "?^?",     "",    "?<<?",  "?>>?",  "?<?",   "?>?",    "?<=?",
    338         "?>=?", "?==?",      "?!=?",     "?=?",  "?*=?", "?/=?", "?%=?", "?+=?", "?-=?",
    339         "?<<=?", "?>>=?",  "?&=?", "?^=?",  "?|=?",  "?[?]",   "","","Range",
    340         // monadic
    341         "+?", "-?", "", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "LabAddress"
     366        "",             "",             "",
     367        "",             "",
     368        //diadic
     369        "",             "",             "",             "",             "?+?",          "?-?",  "?*?",  "?/?",  "?%?",  "",              "",
     370        "?|?",          "?&?",          "?^?",  "",             "?<<?", "?>>?", "?<?",  "?>?",  "?<=?",
     371        "?>=?",         "?==?",         "?!=?", "?=?",  "?*=?", "?/=?", "?%=?", "?+=?", "?-=?",
     372        "?<<=?",        "?>>=?",        "?&=?", "?^=?", "?|=?", "?[?]", "",             "",             "Range",
     373        //monadic
     374        "+?",           "-?",           "",             "*?",   "!?",   "~?",   "++?",  "?++",  "--?",  "?--",  "&&"
    342375};
    343376
     
    350383        buildList( get_args(), args );
    351384
    352         if ( ! ( op = dynamic_cast<OperatorNode *>( function )) ) {
    353                 // a function as opposed to an operator
     385        if ( ! ( op = dynamic_cast<OperatorNode *>( function ) ) ) { // function as opposed to operator
    354386                return new UntypedExpr( function->build(), args, maybeBuild< Expression >( get_argName() ));
    355         } else {
    356                 switch ( op->get_type()) {
    357                   case OperatorNode::Incr:
    358                   case OperatorNode::Decr:
    359                   case OperatorNode::IncrPost:
    360                   case OperatorNode::DecrPost:
    361                   case OperatorNode::Assign:
    362                   case OperatorNode::MulAssn:
    363                   case OperatorNode::DivAssn:
    364                   case OperatorNode::ModAssn:
    365                   case OperatorNode::PlusAssn:
    366                   case OperatorNode::MinusAssn:
    367                   case OperatorNode::LSAssn:
    368                   case OperatorNode::RSAssn:
    369                   case OperatorNode::AndAssn:
    370                   case OperatorNode::ERAssn:
    371                   case OperatorNode::OrAssn:
    372                         // the rewrite rules for these expressions specify that the first argument has its address taken
    373                         assert( ! args.empty() );
    374                         args.front() = new AddressExpr( args.front() );
    375                         break;
    376                   default:
    377                         /* do nothing */
    378                         ;
    379                 }
    380 
    381                 switch ( op->get_type() ) {
    382                   case OperatorNode::Incr:
    383                   case OperatorNode::Decr:
    384                   case OperatorNode::IncrPost:
    385                   case OperatorNode::DecrPost:
    386                   case OperatorNode::Assign:
    387                   case OperatorNode::MulAssn:
    388                   case OperatorNode::DivAssn:
    389                   case OperatorNode::ModAssn:
    390                   case OperatorNode::PlusAssn:
    391                   case OperatorNode::MinusAssn:
    392                   case OperatorNode::LSAssn:
    393                   case OperatorNode::RSAssn:
    394                   case OperatorNode::AndAssn:
    395                   case OperatorNode::ERAssn:
    396                   case OperatorNode::OrAssn:
    397                   case OperatorNode::Plus:
    398                   case OperatorNode::Minus:
    399                   case OperatorNode::Mul:
    400                   case OperatorNode::Div:
    401                   case OperatorNode::Mod:
    402                   case OperatorNode::BitOr:
    403                   case OperatorNode::BitAnd:
    404                   case OperatorNode::Xor:
    405                   case OperatorNode::LShift:
    406                   case OperatorNode::RShift:
    407                   case OperatorNode::LThan:
    408                   case OperatorNode::GThan:
    409                   case OperatorNode::LEThan:
    410                   case OperatorNode::GEThan:
    411                   case OperatorNode::Eq:
    412                   case OperatorNode::Neq:
    413                   case OperatorNode::Index:
    414                   case OperatorNode::Range:
    415                   case OperatorNode::UnPlus:
    416                   case OperatorNode::UnMinus:
    417                   case OperatorNode::PointTo:
    418                   case OperatorNode::Neg:
    419                   case OperatorNode::BitNeg:
    420                   case OperatorNode::LabelAddress:
    421                         return new UntypedExpr( new NameExpr( opFuncName[ op->get_type() ] ), args );
    422                   case OperatorNode::AddressOf:
    423                         assert( args.size() == 1 );
    424                         assert( args.front() );
    425 
    426                         return new AddressExpr( args.front() );
    427                   case OperatorNode::Cast:
    428                         {
    429                                 TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args());
    430                                 assert( arg );
    431 
    432                                 DeclarationNode *decl_node = arg->get_decl();
    433                                 ExpressionNode *expr_node = dynamic_cast<ExpressionNode *>( arg->get_link());
    434 
    435                                 Type *targetType = decl_node->buildType();
    436                                 if ( dynamic_cast< VoidType* >( targetType ) ) {
    437                                         delete targetType;
    438                                         return new CastExpr( expr_node->build(), maybeBuild< Expression >( get_argName() ) );
    439                                 } else {
    440                                         return new CastExpr( expr_node->build(),targetType, maybeBuild< Expression >( get_argName() ) );
    441                                 } // if
    442                         }
    443                   case OperatorNode::FieldSel:
    444                         {
    445                                 assert( args.size() == 2 );
    446 
    447                                 NameExpr *member = dynamic_cast<NameExpr *>( args.back());
    448                                 // TupleExpr *memberTup = dynamic_cast<TupleExpr *>( args.back());
    449 
    450                                 if ( member != 0 ) {
    451                                         UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), args.front());
    452                                         delete member;
    453                                         return ret;
    454                                         /* else if ( memberTup != 0 )
    455                                            {
    456                                            UntypedMemberExpr *ret = new UntypedMemberExpr( memberTup->get_name(), args.front());
    457                                            delete member;
    458                                            return ret;
    459                                            } */
    460                                 } else
    461                                         assert( false );
    462                         }
    463                   case OperatorNode::PFieldSel:
    464                         {
    465                                 assert( args.size() == 2 );
    466 
    467                                 NameExpr *member = dynamic_cast<NameExpr *>( args.back());  // modify for Tuples   xxx
    468                                 assert( member != 0 );
    469 
    470                                 UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
    471                                 deref->get_args().push_back( args.front() );
    472 
    473                                 UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), deref );
     387        } // if
     388
     389        switch ( op->get_type()) {
     390          case OperatorNode::Incr:
     391          case OperatorNode::Decr:
     392          case OperatorNode::IncrPost:
     393          case OperatorNode::DecrPost:
     394          case OperatorNode::Assign:
     395          case OperatorNode::MulAssn:
     396          case OperatorNode::DivAssn:
     397          case OperatorNode::ModAssn:
     398          case OperatorNode::PlusAssn:
     399          case OperatorNode::MinusAssn:
     400          case OperatorNode::LSAssn:
     401          case OperatorNode::RSAssn:
     402          case OperatorNode::AndAssn:
     403          case OperatorNode::ERAssn:
     404          case OperatorNode::OrAssn:
     405                // the rewrite rules for these expressions specify that the first argument has its address taken
     406                assert( ! args.empty() );
     407                args.front() = new AddressExpr( args.front() );
     408                break;
     409          default:
     410                /* do nothing */
     411                ;
     412        }
     413
     414        switch ( op->get_type() ) {
     415          case OperatorNode::Incr:
     416          case OperatorNode::Decr:
     417          case OperatorNode::IncrPost:
     418          case OperatorNode::DecrPost:
     419          case OperatorNode::Assign:
     420          case OperatorNode::MulAssn:
     421          case OperatorNode::DivAssn:
     422          case OperatorNode::ModAssn:
     423          case OperatorNode::PlusAssn:
     424          case OperatorNode::MinusAssn:
     425          case OperatorNode::LSAssn:
     426          case OperatorNode::RSAssn:
     427          case OperatorNode::AndAssn:
     428          case OperatorNode::ERAssn:
     429          case OperatorNode::OrAssn:
     430          case OperatorNode::Plus:
     431          case OperatorNode::Minus:
     432          case OperatorNode::Mul:
     433          case OperatorNode::Div:
     434          case OperatorNode::Mod:
     435          case OperatorNode::BitOr:
     436          case OperatorNode::BitAnd:
     437          case OperatorNode::Xor:
     438          case OperatorNode::LShift:
     439          case OperatorNode::RShift:
     440          case OperatorNode::LThan:
     441          case OperatorNode::GThan:
     442          case OperatorNode::LEThan:
     443          case OperatorNode::GEThan:
     444          case OperatorNode::Eq:
     445          case OperatorNode::Neq:
     446          case OperatorNode::Index:
     447          case OperatorNode::Range:
     448          case OperatorNode::UnPlus:
     449          case OperatorNode::UnMinus:
     450          case OperatorNode::PointTo:
     451          case OperatorNode::Neg:
     452          case OperatorNode::BitNeg:
     453          case OperatorNode::LabelAddress:
     454                return new UntypedExpr( new NameExpr( opFuncName[ op->get_type() ] ), args );
     455          case OperatorNode::AddressOf:
     456                assert( args.size() == 1 );
     457                assert( args.front() );
     458
     459                return new AddressExpr( args.front() );
     460          case OperatorNode::Cast:
     461                {
     462                        TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args());
     463                        assert( arg );
     464
     465                        DeclarationNode *decl_node = arg->get_decl();
     466                        ExpressionNode *expr_node = dynamic_cast<ExpressionNode *>( arg->get_link());
     467
     468                        Type *targetType = decl_node->buildType();
     469                        if ( dynamic_cast< VoidType* >( targetType ) ) {
     470                                delete targetType;
     471                                return new CastExpr( expr_node->build(), maybeBuild< Expression >( get_argName() ) );
     472                        } else {
     473                                return new CastExpr( expr_node->build(),targetType, maybeBuild< Expression >( get_argName() ) );
     474                        } // if
     475                }
     476          case OperatorNode::FieldSel:
     477                {
     478                        assert( args.size() == 2 );
     479
     480                        NameExpr *member = dynamic_cast<NameExpr *>( args.back());
     481                        // TupleExpr *memberTup = dynamic_cast<TupleExpr *>( args.back());
     482
     483                        if ( member != 0 ) {
     484                                UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), args.front());
    474485                                delete member;
    475486                                return ret;
     487                                /* else if ( memberTup != 0 )
     488                                   {
     489                                   UntypedMemberExpr *ret = new UntypedMemberExpr( memberTup->get_name(), args.front());
     490                                   delete member;
     491                                   return ret;
     492                                   } */
     493                        } else
     494                                assert( false );
     495                }
     496          case OperatorNode::PFieldSel:
     497                {
     498                        assert( args.size() == 2 );
     499
     500                        NameExpr *member = dynamic_cast<NameExpr *>( args.back());  // modify for Tuples   xxx
     501                        assert( member != 0 );
     502
     503                        UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
     504                        deref->get_args().push_back( args.front() );
     505
     506                        UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), deref );
     507                        delete member;
     508                        return ret;
     509                }
     510          case OperatorNode::AlignOf:
     511          case OperatorNode::SizeOf:
     512                {
     513///     bool isSizeOf = ( op->get_type() == OperatorNode::SizeOf );
     514
     515                        if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()) ) {
     516                                return new SizeofExpr( arg->get_decl()->buildType());
     517                        } else {
     518                                return new SizeofExpr( args.front());
     519                        } // if
     520                }
     521          case OperatorNode::Attr:
     522                {
     523                        VarRefNode *var = dynamic_cast<VarRefNode *>( get_args());
     524                        assert( var );
     525                        if ( ! get_args()->get_link() ) {
     526                                return new AttrExpr( var->build(), ( Expression*)0);
     527                        } else if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()->get_link()) ) {
     528                                return new AttrExpr( var->build(), arg->get_decl()->buildType());
     529                        } else {
     530                                return new AttrExpr( var->build(), args.back());
     531                        } // if
     532                }
     533          case OperatorNode::CompLit:
     534                throw UnimplementedError( "C99 compound literals" );
     535                // the short-circuited operators
     536          case OperatorNode::Or:
     537          case OperatorNode::And:
     538                assert( args.size() == 2);
     539                return new LogicalExpr( notZeroExpr( args.front() ), notZeroExpr( args.back() ), ( op->get_type() == OperatorNode::And ) );
     540          case OperatorNode::Cond:
     541                {
     542                        assert( args.size() == 3);
     543                        std::list< Expression* >::const_iterator i = args.begin();
     544                        Expression *arg1 = notZeroExpr( *i++ );
     545                        Expression *arg2 = *i++;
     546                        Expression *arg3 = *i++;
     547                        return new ConditionalExpr( arg1, arg2, arg3 );
     548                }
     549          case OperatorNode::NCond:
     550                throw UnimplementedError( "GNU 2-argument conditional expression" );
     551          case OperatorNode::Comma:
     552                {
     553                        assert( args.size() == 2);
     554                        std::list< Expression* >::const_iterator i = args.begin();
     555                        Expression *ret = *i++;
     556                        while ( i != args.end() ) {
     557                                ret = new CommaExpr( ret, *i++ );
    476558                        }
    477                   case OperatorNode::AlignOf:
    478                   case OperatorNode::SizeOf:
    479                         {
    480 ///     bool isSizeOf = ( op->get_type() == OperatorNode::SizeOf );
    481 
    482                                 if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()) ) {
    483                                         return new SizeofExpr( arg->get_decl()->buildType());
    484                                 } else {
    485                                         return new SizeofExpr( args.front());
    486                                 } // if
    487                         }
    488                   case OperatorNode::Attr:
    489                         {
    490                                 VarRefNode *var = dynamic_cast<VarRefNode *>( get_args());
    491                                 assert( var );
    492                                 if ( ! get_args()->get_link() ) {
    493                                         return new AttrExpr( var->build(), ( Expression*)0);
    494                                 } else if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()->get_link()) ) {
    495                                         return new AttrExpr( var->build(), arg->get_decl()->buildType());
    496                                 } else {
    497                                         return new AttrExpr( var->build(), args.back());
    498                                 } // if
    499                         }
    500                   case OperatorNode::CompLit:
    501                         throw UnimplementedError( "C99 compound literals" );
    502                         // the short-circuited operators
    503                   case OperatorNode::Or:
    504                   case OperatorNode::And:
    505                         assert( args.size() == 2);
    506                         return new LogicalExpr( notZeroExpr( args.front() ), notZeroExpr( args.back() ), ( op->get_type() == OperatorNode::And ) );
    507                   case OperatorNode::Cond:
    508                         {
    509                                 assert( args.size() == 3);
    510                                 std::list< Expression* >::const_iterator i = args.begin();
    511                                 Expression *arg1 = notZeroExpr( *i++ );
    512                                 Expression *arg2 = *i++;
    513                                 Expression *arg3 = *i++;
    514                                 return new ConditionalExpr( arg1, arg2, arg3 );
    515                         }
    516                   case OperatorNode::NCond:
    517                         throw UnimplementedError( "GNU 2-argument conditional expression" );
    518                   case OperatorNode::Comma:
    519                         {
    520                                 assert( args.size() == 2);
    521                                 std::list< Expression* >::const_iterator i = args.begin();
    522                                 Expression *ret = *i++;
    523                                 while ( i != args.end() ) {
    524                                         ret = new CommaExpr( ret, *i++ );
    525                                 }
    526                                 return ret;
    527                         }
    528                         // Tuples
    529                   case OperatorNode::TupleC:
    530                         {
    531                                 TupleExpr *ret = new TupleExpr();
    532                                 std::copy( args.begin(), args.end(), back_inserter( ret->get_exprs() ) );
    533                                 return ret;
    534                         }
    535                   default:
    536                         // shouldn't happen
    537                         return 0;
    538                 }
    539         }
     559                        return ret;
     560                }
     561                // Tuples
     562          case OperatorNode::TupleC:
     563                {
     564                        TupleExpr *ret = new TupleExpr();
     565                        std::copy( args.begin(), args.end(), back_inserter( ret->get_exprs() ) );
     566                        return ret;
     567                }
     568          default:
     569                // shouldn't happen
     570                return 0;
     571        } // switch
    540572}
    541573
     
    552584void CompositeExprNode::print( std::ostream &os, int indent ) const {
    553585        printDesignation( os );
    554         os << '\r' << string( indent, ' ') << "Application of: " << endl;
     586        os << string( indent, ' ' ) << "Application of: " << endl;
    555587        function->print( os, indent + ParseNode::indent_by );
    556588
    557         os << '\r' << string( indent, ' ') ;
     589        os << string( indent, ' ' ) ;
    558590        if ( arguments ) {
    559591                os << "... on arguments: " << endl;
     
    586618}
    587619
     620//##############################################################################
     621
    588622CommaExprNode::CommaExprNode(): CompositeExprNode( new OperatorNode( OperatorNode::Comma )) {}
    589623
     
    603637}
    604638
     639//##############################################################################
     640
    605641ValofExprNode::ValofExprNode( StatementNode *s ): body( s ) {}
    606642
     
    614650void ValofExprNode::print( std::ostream &os, int indent ) const {
    615651        printDesignation( os );
    616         os << string( indent, ' ') << "Valof Expression:" << std::endl;
     652        os << string( indent, ' ' ) << "Valof Expression:" << std::endl;
    617653        get_body()->print( os, indent + 4);
    618654}
     
    625661        return new UntypedValofExpr ( get_body()->build(), maybeBuild< Expression >( get_argName() ) );
    626662}
     663
     664//##############################################################################
    627665
    628666ForCtlExprNode::ForCtlExprNode( ParseNode *init_, ExpressionNode *cond, ExpressionNode *incr ) throw ( SemanticError ) : condition( cond ), change( incr ) {
     
    633671                ExpressionNode *exp;
    634672
    635                 if (( decl = dynamic_cast<DeclarationNode *>( init_)) != 0)
     673                if (( decl = dynamic_cast<DeclarationNode *>(init_) ) != 0)
    636674                        init = new StatementNode( decl );
    637675                else if (( exp = dynamic_cast<ExpressionNode *>( init_)) != 0)
     
    659697
    660698void ForCtlExprNode::print( std::ostream &os, int indent ) const{
    661         os << string( indent,' ') << "For Control Expression -- : " << endl;
    662 
    663         os << "\r" << string( indent + 2,' ') << "initialization: ";
    664         if ( init != 0)
    665                 init->print( os, indent + 4);
    666 
    667         os << "\n\r" << string( indent + 2,' ') << "condition: ";
    668         if ( condition != 0)
    669                 condition->print( os, indent + 4);
    670         os << "\n\r" << string( indent + 2,' ') << "increment: ";
    671         if ( change != 0)
    672                 change->print( os, indent + 4);
     699        os << string( indent,' ' ) << "For Control Expression -- :" << endl;
     700
     701        os << string( indent + 2, ' ' ) << "initialization:" << endl;
     702        if ( init != 0 )
     703                init->printList( os, indent + 4 );
     704
     705        os << string( indent + 2, ' ' ) << "condition: " << endl;
     706        if ( condition != 0 )
     707                condition->print( os, indent + 4 );
     708        os << string( indent + 2, ' ' ) << "increment: " << endl;
     709        if ( change != 0 )
     710                change->print( os, indent + 4 );
    673711}
    674712
     
    677715}
    678716
    679 TypeValueNode::TypeValueNode( DeclarationNode *decl )
    680         : decl( decl ) {
    681 }
    682 
    683 TypeValueNode::TypeValueNode( const TypeValueNode &other )
    684         : ExpressionNode( other ), decl( maybeClone( other.decl ) ) {
     717//##############################################################################
     718
     719TypeValueNode::TypeValueNode( DeclarationNode *decl ) : decl( decl ) {
     720}
     721
     722TypeValueNode::TypeValueNode( const TypeValueNode &other ) : ExpressionNode( other ), decl( maybeClone( other.decl ) ) {
    685723}
    686724
     
    700738
    701739ExpressionNode *flattenCommas( ExpressionNode *list ) {
    702         if ( CompositeExprNode *composite = dynamic_cast< CompositeExprNode * >( list ) )
    703                 {
    704                         OperatorNode *op;
    705                         if ( ( op = dynamic_cast< OperatorNode * >( composite->get_function() )) && ( op->get_type() == OperatorNode::Comma ) )
    706                                 {
    707                                         if ( ExpressionNode *next = dynamic_cast< ExpressionNode * >( list->get_link() ) )
    708                                                 composite->add_arg( next );
    709                                         return flattenCommas( composite->get_args() );
    710                                 }
    711                 }
     740        if ( CompositeExprNode *composite = dynamic_cast< CompositeExprNode * >( list ) ) {
     741                OperatorNode *op;
     742                if ( ( op = dynamic_cast< OperatorNode * >( composite->get_function() )) && ( op->get_type() == OperatorNode::Comma ) ) {
     743                        if ( ExpressionNode *next = dynamic_cast< ExpressionNode * >( list->get_link() ) )
     744                                composite->add_arg( next );
     745                        return flattenCommas( composite->get_args() );
     746                } // if
     747        } // if
    712748
    713749        if ( ExpressionNode *next = dynamic_cast< ExpressionNode * >( list->get_link() ) )
     
    722758                if ( ( op = dynamic_cast< OperatorNode * >( composite->get_function() )) && ( op->get_type() == OperatorNode::TupleC ) )
    723759                        return composite->get_args();
    724         }
     760        } // if
    725761        return tuple;
    726762}
Note: See TracChangeset for help on using the changeset viewer.