Changes in src/Parser/ExpressionNode.cc [1869adf:843054c2]
- File:
-
- 1 edited
-
src/Parser/ExpressionNode.cc (modified) (24 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/ExpressionNode.cc
r1869adf r843054c2 9 9 // Author : Rodolfo G. Esteves 10 10 // Created On : Sat May 16 13:17:07 2015 11 // Last Modified By : Rob Schluntz12 // Last Modified On : Wed Jun 24 16:20:00201513 // Update Count : 15811 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat May 16 13:19:35 2015 13 // Update Count : 2 14 14 // 15 15 … … 17 17 #include <cctype> 18 18 #include <algorithm> 19 #include <sstream>20 #include <cstdio>21 #include <climits>22 19 23 20 #include "ParseNode.h" 21 #include "SynTree/Type.h" 24 22 #include "SynTree/Constant.h" 25 23 #include "SynTree/Expression.h" 24 #include "SynTree/Declaration.h" 26 25 #include "UnimplementedError.h" 27 26 #include "parseutility.h" … … 32 31 ExpressionNode::ExpressionNode() : ParseNode(), argName( 0 ) {} 33 32 34 ExpressionNode::ExpressionNode( const string *name_ ) : ParseNode( name_ ), argName( 0 ) {} 33 ExpressionNode::ExpressionNode( string *name_) : ParseNode( *name_ ), argName( 0 ) { 34 delete name_; 35 } 35 36 36 37 ExpressionNode::ExpressionNode( const ExpressionNode &other ) : ParseNode( other.name ) { … … 42 43 } 43 44 44 ExpressionNode * ExpressionNode::set_asArgName( conststd::string *aName ) {45 ExpressionNode * ExpressionNode::set_asArgName( std::string *aName ) { 45 46 argName = new VarRefNode( aName ); 46 47 return this; … … 54 55 void ExpressionNode::printDesignation( std::ostream &os, int indent ) const { 55 56 if ( argName ) { 56 os << string( indent, ' ') << "(designated by: ";57 os << string(' ', indent ) << "(designated by: "; 57 58 argName->printOneLine( os, indent ); 58 59 os << ")" << std::endl; … … 60 61 } 61 62 62 //##############################################################################63 64 63 NullExprNode::NullExprNode() {} 65 64 … … 86 85 } 87 86 88 //############################################################################## 89 90 static inline bool checkU( char c ) { return c == 'u' || c == 'U'; } 91 static inline bool checkL( char c ) { return c == 'l' || c == 'L'; } 92 static inline bool checkF( char c ) { return c == 'f' || c == 'F'; } 93 static 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 107 ConstantNode::ConstantNode( Type t, string *inVal ) : type( t ), value( *inVal ) { 108 // lexing divides constants into 4 kinds 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 ) { 109 114 switch ( type ) { 110 115 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 constant117 unsigned long long v; // converted integral value118 bool dec = true, Unsigned = false; // decimal, unsigned constant119 int size; // 0 => int, 1 => long, 2 => long long120 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 } // if130 } else { // decimal constant ?131 sscanf( (char *)value.c_str(), "%llu", &v );132 //printf( "%llu %llu\n", v, v );133 } // if134 135 if ( v <= INT_MAX ) { // signed int136 size = 0;137 } else if ( v <= UINT_MAX && ! dec ) { // unsigned int138 size = 0;139 Unsigned = true; // unsigned140 } else if ( v <= LONG_MAX ) { // signed long int141 size = 1;142 } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int143 size = 1;144 Unsigned = true; // unsigned long int145 } else if ( v <= LLONG_MAX ) { // signed long long int146 size = 2;147 } else { // unsigned long long int148 size = 2;149 Unsigned = true; // unsigned long long int150 } // if151 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 } // if159 } // if160 } 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 } // if167 } else {168 if ( last > 0 && checkU( value[ last - 1 ] ) ) { // suffix 'u' ?169 Unsigned = true;170 } // if171 } // if172 } // if173 btype = kind[Unsigned][size]; // lookup constant type174 break;175 }176 116 case Float: 177 117 { 178 size_t len = value.length() - 1; 179 180 btype = BasicType::Double; // default 181 if ( checkF( value[len] ) ) { // float ? 182 btype = BasicType::Float; 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; 183 136 } // if 184 if ( checkL( value[len] ) ) { // long double ? 185 btype = BasicType::LongDouble; 186 } // if 137 138 assert(( longs >= 0) && ( longs <= 2)); 139 140 if ( sfx.find("u") != string::npos ) 141 sign = false; 142 187 143 break; 188 144 } 189 145 case Character: 190 btype = BasicType::Char; // default 191 if ( string( "LUu" ).find( value[0] ) != string::npos ) { 192 // ??? 193 } // if 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: 184 os << value ; 194 185 break; 195 case String:196 // array of char197 if ( string( "LUu" ).find( value[0] ) != string::npos ) {198 if ( value[0] == 'u' && value[1] == '8' ) {199 // ???200 } else {201 // ???202 } // if203 } // if204 break;205 } // switch206 } // ConstantNode::ConstantNode207 208 ConstantNode *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 lexer216 return this;217 }218 219 void ConstantNode::printOneLine( std::ostream &os, int indent ) const {220 os << string( indent, ' ' );221 printDesignation( os );222 223 switch ( type ) {224 case Integer:225 186 case Float: 226 187 os << value ; 227 188 break; 189 228 190 case Character: 229 191 os << "'" << value << "'"; 230 192 break; 193 231 194 case String: 232 195 os << '"' << value << '"'; 233 196 break; 234 } // switch197 } 235 198 236 199 os << ' '; … … 243 206 244 207 Expression *ConstantNode::build() const { 245 ::Type::Qualifiers q; // no qualifiers on constants 246 247 switch ( get_type() ) { 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; 248 223 case String: 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() ) ); 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() ) ); 260 232 } 261 } 262 263 //############################################################################## 233 return new ConstantExpr( Constant( bt, get_value()), maybeBuild< Expression >( get_argName() ) ); 234 } 264 235 265 236 VarRefNode::VarRefNode() : isLabel( false ) {} 266 237 267 VarRefNode::VarRefNode( const string *name_, bool labelp ) : ExpressionNode( name_), isLabel( labelp ) {}238 VarRefNode::VarRefNode( string *name_, bool labelp ) : ExpressionNode( name_), isLabel( labelp ) {} 268 239 269 240 VarRefNode::VarRefNode( const VarRefNode &other ) : ExpressionNode( other ), isLabel( other.isLabel ) { … … 281 252 void VarRefNode::print( std::ostream &os, int indent ) const { 282 253 printDesignation( os ); 283 os << string( indent, ' ') << "Referencing: ";254 os << '\r' << string( indent, ' ') << "Referencing: "; 284 255 os << "Variable: " << get_name(); 285 256 os << endl; 286 257 } 287 258 288 //##############################################################################289 290 259 OperatorNode::OperatorNode( Type t ) : type( t ) {} 291 260 … … 306 275 void OperatorNode::print( std::ostream &os, int indent ) const{ 307 276 printDesignation( os ); 308 os << string( indent, ' ') << "Operator: " << OpName[type] << endl;277 os << '\r' << string( indent, ' ') << "Operator: " << OpName[type] << endl; 309 278 return; 310 279 } 311 280 312 const char *OperatorNode::get_typename( void ) const{313 return OpName[ type ];281 std::string OperatorNode::get_typename( void ) const{ 282 return string( OpName[ type ]); 314 283 } 315 284 … … 319 288 "Cond", "NCond", 320 289 // diadic 321 "SizeOf", "AlignOf", "Attr", "CompLit", "Plus", "Minus", "Mul", "Div", "Mod", "Or",290 "SizeOf", "AlignOf", "Attr", "CompLit", "Plus", "Minus", "Mul", "Div", "Mod", "Or", 322 291 "And", "BitOr", "BitAnd", "Xor", "Cast", "LShift", "RShift", "LThan", "GThan", 323 292 "LEThan", "GEThan", "Eq", "Neq", "Assign", "MulAssn", "DivAssn", "ModAssn", "PlusAssn", … … 328 297 }; 329 298 330 //##############################################################################331 332 299 CompositeExprNode::CompositeExprNode( void ) : ExpressionNode(), function( 0 ), arguments( 0 ) { 333 300 } 334 301 335 CompositeExprNode::CompositeExprNode( const string *name_ ) : ExpressionNode( name_), function( 0 ), arguments( 0 ) {302 CompositeExprNode::CompositeExprNode( string *name_) : ExpressionNode( name_), function( 0 ), arguments( 0 ) { 336 303 } 337 304 … … 364 331 // the names that users use to define operator functions 365 332 static const char *opFuncName[] = { 366 "", "","",367 "", "",368 // diadic369 "", "", "", "", "?+?", "?-?", "?*?", "?/?", "?%?", "","",370 "?|?", "?&?", "?^?", "", "?<<?", "?>>?", "?<?", "?>?","?<=?",371 "?>=?", "?==?", "?!=?", "?=?", "?*=?", "?/=?", "?%=?", "?+=?","?-=?",372 "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?", "?[?]", "", "","Range",373 // monadic374 "+?", "-?", "", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&"333 "", "", "", 334 "", "", 335 // diadic 336 "", "", "", "", "?+?", "?-?", "?*?", "?/?", "?%?", "", "", 337 "?|?", "?&?", "?^?", "", "?<<?", "?>>?", "?<?", "?>?", "?<=?", 338 "?>=?", "?==?", "?!=?", "?=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", 339 "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?", "?[?]", "","","Range", 340 // monadic 341 "+?", "-?", "", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "LabAddress" 375 342 }; 376 343 … … 383 350 buildList( get_args(), args ); 384 351 385 if ( ! ( op = dynamic_cast<OperatorNode *>( function ) ) ) { // function as opposed to operator 352 if ( ! ( op = dynamic_cast<OperatorNode *>( function )) ) { 353 // a function as opposed to an operator 386 354 return new UntypedExpr( function->build(), args, maybeBuild< Expression >( get_argName() )); 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 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 ; 475 379 } 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()); 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 ); 485 474 delete member; 486 475 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 ); 476 } 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; 495 538 } 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++ ); 558 } 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 539 } 572 540 } 573 541 … … 584 552 void CompositeExprNode::print( std::ostream &os, int indent ) const { 585 553 printDesignation( os ); 586 os << string( indent, ' ') << "Application of: " << endl;554 os << '\r' << string( indent, ' ') << "Application of: " << endl; 587 555 function->print( os, indent + ParseNode::indent_by ); 588 556 589 os << string( indent, ' ') ;557 os << '\r' << string( indent, ' ') ; 590 558 if ( arguments ) { 591 559 os << "... on arguments: " << endl; … … 618 586 } 619 587 620 //##############################################################################621 622 588 CommaExprNode::CommaExprNode(): CompositeExprNode( new OperatorNode( OperatorNode::Comma )) {} 623 589 … … 637 603 } 638 604 639 //##############################################################################640 641 605 ValofExprNode::ValofExprNode( StatementNode *s ): body( s ) {} 642 606 … … 650 614 void ValofExprNode::print( std::ostream &os, int indent ) const { 651 615 printDesignation( os ); 652 os << string( indent, ' ' ) << "Valof Expression:" << std::endl;616 os << string( indent, ' ') << "Valof Expression:" << std::endl; 653 617 get_body()->print( os, indent + 4); 654 618 } … … 661 625 return new UntypedValofExpr ( get_body()->build(), maybeBuild< Expression >( get_argName() ) ); 662 626 } 663 664 //##############################################################################665 627 666 628 ForCtlExprNode::ForCtlExprNode( ParseNode *init_, ExpressionNode *cond, ExpressionNode *incr ) throw ( SemanticError ) : condition( cond ), change( incr ) { … … 671 633 ExpressionNode *exp; 672 634 673 if (( decl = dynamic_cast<DeclarationNode *>( init_)) != 0)635 if (( decl = dynamic_cast<DeclarationNode *>( init_)) != 0) 674 636 init = new StatementNode( decl ); 675 637 else if (( exp = dynamic_cast<ExpressionNode *>( init_)) != 0) … … 697 659 698 660 void ForCtlExprNode::print( std::ostream &os, int indent ) const{ 699 os << string( indent,' ' ) << "For Control Expression -- :" << endl;700 701 os << string( indent + 2, ' ' ) << "initialization:" << endl;702 if ( init != 0 )703 init->print List( 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 );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); 711 673 } 712 674 … … 715 677 } 716 678 717 //############################################################################## 718 719 TypeValueNode::TypeValueNode( DeclarationNode *decl ) : decl( decl ) { 720 } 721 722 TypeValueNode::TypeValueNode( const TypeValueNode &other ): ExpressionNode( other ), decl( maybeClone( other.decl ) ) {679 TypeValueNode::TypeValueNode( DeclarationNode *decl ) 680 : decl( decl ) { 681 } 682 683 TypeValueNode::TypeValueNode( const TypeValueNode &other ) 684 : ExpressionNode( other ), decl( maybeClone( other.decl ) ) { 723 685 } 724 686 … … 738 700 739 701 ExpressionNode *flattenCommas( ExpressionNode *list ) { 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 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 } 748 712 749 713 if ( ExpressionNode *next = dynamic_cast< ExpressionNode * >( list->get_link() ) ) … … 758 722 if ( ( op = dynamic_cast< OperatorNode * >( composite->get_function() )) && ( op->get_type() == OperatorNode::TupleC ) ) 759 723 return composite->get_args(); 760 } // if724 } 761 725 return tuple; 762 726 }
Note:
See TracChangeset
for help on using the changeset viewer.