Changes in src/Parser/ExpressionNode.cc [843054c2:1869adf]
- File:
-
- 1 edited
-
src/Parser/ExpressionNode.cc (modified) (24 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/ExpressionNode.cc
r843054c2 r1869adf 9 9 // Author : Rodolfo G. Esteves 10 10 // Created On : Sat May 16 13:17:07 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Sat May 16 13:19:35201513 // Update Count : 211 // Last Modified By : Rob Schluntz 12 // Last Modified On : Wed Jun 24 16:20:00 2015 13 // Update Count : 158 14 14 // 15 15 … … 17 17 #include <cctype> 18 18 #include <algorithm> 19 #include <sstream> 20 #include <cstdio> 21 #include <climits> 19 22 20 23 #include "ParseNode.h" 21 #include "SynTree/Type.h"22 24 #include "SynTree/Constant.h" 23 25 #include "SynTree/Expression.h" 24 #include "SynTree/Declaration.h"25 26 #include "UnimplementedError.h" 26 27 #include "parseutility.h" … … 31 32 ExpressionNode::ExpressionNode() : ParseNode(), argName( 0 ) {} 32 33 33 ExpressionNode::ExpressionNode( string *name_) : ParseNode( *name_ ), argName( 0 ) { 34 delete name_; 35 } 34 ExpressionNode::ExpressionNode( const string *name_ ) : ParseNode( name_ ), argName( 0 ) {} 36 35 37 36 ExpressionNode::ExpressionNode( const ExpressionNode &other ) : ParseNode( other.name ) { … … 43 42 } 44 43 45 ExpressionNode * ExpressionNode::set_asArgName( std::string *aName ) {44 ExpressionNode * ExpressionNode::set_asArgName( const std::string *aName ) { 46 45 argName = new VarRefNode( aName ); 47 46 return this; … … 55 54 void ExpressionNode::printDesignation( std::ostream &os, int indent ) const { 56 55 if ( argName ) { 57 os << string( ' ', indent) << "(designated by: ";56 os << string( indent, ' ' ) << "(designated by: "; 58 57 argName->printOneLine( os, indent ); 59 58 os << ")" << std::endl; … … 61 60 } 62 61 62 //############################################################################## 63 63 64 NullExprNode::NullExprNode() {} 64 65 … … 85 86 } 86 87 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 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 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 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 lexer 216 return this; 217 } 218 219 void ConstantNode::printOneLine( std::ostream &os, int indent ) const { 220 os << string( indent, ' ' ); 221 printDesignation( os ); 222 114 223 switch ( type ) { 115 224 case Integer: 116 225 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 underscores128 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 } // if137 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 escapes148 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 here155 ;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 } else170 value += *newValue;171 172 delete newValue;173 } // if174 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 226 os << value ; 185 227 break; 186 case Float:187 os << value ;188 break;189 190 228 case Character: 191 229 os << "'" << value << "'"; 192 230 break; 193 194 231 case String: 195 232 os << '"' << value << '"'; 196 233 break; 197 } 234 } // switch 198 235 199 236 os << ' '; … … 206 243 207 244 Expression *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() ) { 223 248 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() ) ); 232 260 } 233 return new ConstantExpr( Constant( bt, get_value()), maybeBuild< Expression >( get_argName() ) ); 234 } 261 } 262 263 //############################################################################## 235 264 236 265 VarRefNode::VarRefNode() : isLabel( false ) {} 237 266 238 VarRefNode::VarRefNode( string *name_, bool labelp ) : ExpressionNode( name_), isLabel( labelp ) {}267 VarRefNode::VarRefNode( const string *name_, bool labelp ) : ExpressionNode( name_ ), isLabel( labelp ) {} 239 268 240 269 VarRefNode::VarRefNode( const VarRefNode &other ) : ExpressionNode( other ), isLabel( other.isLabel ) { … … 252 281 void VarRefNode::print( std::ostream &os, int indent ) const { 253 282 printDesignation( os ); 254 os << '\r' << string( indent, ' ') << "Referencing: ";283 os << string( indent, ' ' ) << "Referencing: "; 255 284 os << "Variable: " << get_name(); 256 285 os << endl; 257 286 } 258 287 288 //############################################################################## 289 259 290 OperatorNode::OperatorNode( Type t ) : type( t ) {} 260 291 … … 275 306 void OperatorNode::print( std::ostream &os, int indent ) const{ 276 307 printDesignation( os ); 277 os << '\r' << string( indent, ' ') << "Operator: " << OpName[type] << endl;308 os << string( indent, ' ' ) << "Operator: " << OpName[type] << endl; 278 309 return; 279 310 } 280 311 281 std::stringOperatorNode::get_typename( void ) const{282 return string( OpName[ type ]);312 const char *OperatorNode::get_typename( void ) const{ 313 return OpName[ type ]; 283 314 } 284 315 … … 288 319 "Cond", "NCond", 289 320 // diadic 290 "SizeOf", "AlignOf", "Attr", "CompLit", "Plus", "Minus", "Mul", "Div", "Mod", "Or",321 "SizeOf", "AlignOf", "Attr", "CompLit", "Plus", "Minus", "Mul", "Div", "Mod", "Or", 291 322 "And", "BitOr", "BitAnd", "Xor", "Cast", "LShift", "RShift", "LThan", "GThan", 292 323 "LEThan", "GEThan", "Eq", "Neq", "Assign", "MulAssn", "DivAssn", "ModAssn", "PlusAssn", … … 297 328 }; 298 329 330 //############################################################################## 331 299 332 CompositeExprNode::CompositeExprNode( void ) : ExpressionNode(), function( 0 ), arguments( 0 ) { 300 333 } 301 334 302 CompositeExprNode::CompositeExprNode( string *name_) : ExpressionNode( name_), function( 0 ), arguments( 0 ) {335 CompositeExprNode::CompositeExprNode( const string *name_ ) : ExpressionNode( name_ ), function( 0 ), arguments( 0 ) { 303 336 } 304 337 … … 331 364 // the names that users use to define operator functions 332 365 static const char *opFuncName[] = { 333 "", "","",334 "", "",335 // diadic336 "", "", "", "", "?+?", "?-?", "?*?", "?/?", "?%?", "","",337 "?|?", "?&?", "?^?", "", "?<<?", "?>>?", "?<?", "?>?","?<=?",338 "?>=?", "?==?", "?!=?", "?=?", "?*=?", "?/=?", "?%=?", "?+=?","?-=?",339 "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?", "?[?]", "","","Range",340 // monadic341 "+?", "-?", "", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "LabAddress"366 "", "", "", 367 "", "", 368 //diadic 369 "", "", "", "", "?+?", "?-?", "?*?", "?/?", "?%?", "", "", 370 "?|?", "?&?", "?^?", "", "?<<?", "?>>?", "?<?", "?>?", "?<=?", 371 "?>=?", "?==?", "?!=?", "?=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", 372 "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?", "?[?]", "", "", "Range", 373 //monadic 374 "+?", "-?", "", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&" 342 375 }; 343 376 … … 350 383 buildList( get_args(), args ); 351 384 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 354 386 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()); 474 485 delete member; 475 486 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++ ); 476 558 } 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 540 572 } 541 573 … … 552 584 void CompositeExprNode::print( std::ostream &os, int indent ) const { 553 585 printDesignation( os ); 554 os << '\r' << string( indent, ' ') << "Application of: " << endl;586 os << string( indent, ' ' ) << "Application of: " << endl; 555 587 function->print( os, indent + ParseNode::indent_by ); 556 588 557 os << '\r' << string( indent, ' ') ;589 os << string( indent, ' ' ) ; 558 590 if ( arguments ) { 559 591 os << "... on arguments: " << endl; … … 586 618 } 587 619 620 //############################################################################## 621 588 622 CommaExprNode::CommaExprNode(): CompositeExprNode( new OperatorNode( OperatorNode::Comma )) {} 589 623 … … 603 637 } 604 638 639 //############################################################################## 640 605 641 ValofExprNode::ValofExprNode( StatementNode *s ): body( s ) {} 606 642 … … 614 650 void ValofExprNode::print( std::ostream &os, int indent ) const { 615 651 printDesignation( os ); 616 os << string( indent, ' ' ) << "Valof Expression:" << std::endl;652 os << string( indent, ' ' ) << "Valof Expression:" << std::endl; 617 653 get_body()->print( os, indent + 4); 618 654 } … … 625 661 return new UntypedValofExpr ( get_body()->build(), maybeBuild< Expression >( get_argName() ) ); 626 662 } 663 664 //############################################################################## 627 665 628 666 ForCtlExprNode::ForCtlExprNode( ParseNode *init_, ExpressionNode *cond, ExpressionNode *incr ) throw ( SemanticError ) : condition( cond ), change( incr ) { … … 633 671 ExpressionNode *exp; 634 672 635 if (( decl = dynamic_cast<DeclarationNode *>( init_)) != 0)673 if (( decl = dynamic_cast<DeclarationNode *>(init_) ) != 0) 636 674 init = new StatementNode( decl ); 637 675 else if (( exp = dynamic_cast<ExpressionNode *>( init_)) != 0) … … 659 697 660 698 void 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 ); 673 711 } 674 712 … … 677 715 } 678 716 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 719 TypeValueNode::TypeValueNode( DeclarationNode *decl ) : decl( decl ) { 720 } 721 722 TypeValueNode::TypeValueNode( const TypeValueNode &other ) : ExpressionNode( other ), decl( maybeClone( other.decl ) ) { 685 723 } 686 724 … … 700 738 701 739 ExpressionNode *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 712 748 713 749 if ( ExpressionNode *next = dynamic_cast< ExpressionNode * >( list->get_link() ) ) … … 722 758 if ( ( op = dynamic_cast< OperatorNode * >( composite->get_function() )) && ( op->get_type() == OperatorNode::TupleC ) ) 723 759 return composite->get_args(); 724 } 760 } // if 725 761 return tuple; 726 762 }
Note:
See TracChangeset
for help on using the changeset viewer.