[b87a5ed] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // ExpressionNode.cc -- |
---|
| 8 | // |
---|
| 9 | // Author : Rodolfo G. Esteves |
---|
| 10 | // 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 |
---|
| 14 | // |
---|
| 15 | |
---|
[51b7345] | 16 | #include <cassert> |
---|
| 17 | #include <cctype> |
---|
| 18 | #include <algorithm> |
---|
| 19 | |
---|
| 20 | #include "ParseNode.h" |
---|
| 21 | #include "SynTree/Type.h" |
---|
| 22 | #include "SynTree/Constant.h" |
---|
| 23 | #include "SynTree/Expression.h" |
---|
| 24 | #include "SynTree/Declaration.h" |
---|
| 25 | #include "UnimplementedError.h" |
---|
| 26 | #include "parseutility.h" |
---|
| 27 | #include "utility.h" |
---|
| 28 | |
---|
| 29 | using namespace std; |
---|
| 30 | |
---|
[3848e0e] | 31 | ExpressionNode::ExpressionNode() : ParseNode(), argName( 0 ) {} |
---|
[51b7345] | 32 | |
---|
[bdd516a] | 33 | ExpressionNode::ExpressionNode( string *name_) : ParseNode( *name_ ), argName( 0 ) { |
---|
[b87a5ed] | 34 | delete name_; |
---|
[51b7345] | 35 | } |
---|
| 36 | |
---|
[3848e0e] | 37 | ExpressionNode::ExpressionNode( const ExpressionNode &other ) : ParseNode( other.name ) { |
---|
[b87a5ed] | 38 | if ( other.argName ) { |
---|
| 39 | argName = other.argName->clone(); |
---|
| 40 | } else { |
---|
| 41 | argName = 0; |
---|
| 42 | } // if |
---|
[51b7345] | 43 | } |
---|
| 44 | |
---|
| 45 | ExpressionNode * ExpressionNode::set_asArgName( std::string *aName ) { |
---|
[b87a5ed] | 46 | argName = new VarRefNode( aName ); |
---|
| 47 | return this; |
---|
[51b7345] | 48 | } |
---|
| 49 | |
---|
| 50 | ExpressionNode * ExpressionNode::set_asArgName( ExpressionNode *aDesignator ) { |
---|
[b87a5ed] | 51 | argName = aDesignator; |
---|
| 52 | return this; |
---|
[51b7345] | 53 | } |
---|
| 54 | |
---|
| 55 | void ExpressionNode::printDesignation( std::ostream &os, int indent ) const { |
---|
[b87a5ed] | 56 | if ( argName ) { |
---|
| 57 | os << string(' ', indent ) << "(designated by: "; |
---|
| 58 | argName->printOneLine( os, indent ); |
---|
| 59 | os << ")" << std::endl; |
---|
| 60 | } // if |
---|
[51b7345] | 61 | } |
---|
| 62 | |
---|
[3848e0e] | 63 | NullExprNode::NullExprNode() {} |
---|
[51b7345] | 64 | |
---|
[3848e0e] | 65 | NullExprNode *NullExprNode::clone() const { |
---|
[b87a5ed] | 66 | return new NullExprNode(); |
---|
[51b7345] | 67 | } |
---|
| 68 | |
---|
[bdd516a] | 69 | void NullExprNode::print( std::ostream & os, int indent ) const { |
---|
[b87a5ed] | 70 | printDesignation( os ); |
---|
| 71 | os << "null expression"; |
---|
[51b7345] | 72 | } |
---|
| 73 | |
---|
[bdd516a] | 74 | void NullExprNode::printOneLine( std::ostream & os, int indent ) const { |
---|
[b87a5ed] | 75 | printDesignation( os ); |
---|
| 76 | os << "null"; |
---|
[51b7345] | 77 | } |
---|
| 78 | |
---|
[3848e0e] | 79 | Expression *NullExprNode::build() const { |
---|
[b87a5ed] | 80 | return 0; |
---|
[51b7345] | 81 | } |
---|
| 82 | |
---|
[a08ba92] | 83 | CommaExprNode *ExpressionNode::add_to_list( ExpressionNode *exp ) { |
---|
[b87a5ed] | 84 | return new CommaExprNode( this, exp ); |
---|
[51b7345] | 85 | } |
---|
| 86 | |
---|
| 87 | // enum ConstantNode::Type = { Integer, Float, Character, String, Range } |
---|
| 88 | |
---|
[bdd516a] | 89 | ConstantNode::ConstantNode( void ) : ExpressionNode(), sign( true ), longs(0), size(0) {} |
---|
[51b7345] | 90 | |
---|
[bdd516a] | 91 | ConstantNode::ConstantNode( string *name_) : ExpressionNode( name_), sign( true ), longs(0), size(0) {} |
---|
[51b7345] | 92 | |
---|
[bdd516a] | 93 | ConstantNode::ConstantNode( Type t, string *inVal ) : type( t ), sign( true ), longs(0), size(0) { |
---|
[b87a5ed] | 94 | if ( inVal ) { |
---|
| 95 | value = *inVal; |
---|
| 96 | delete inVal; |
---|
| 97 | } else { |
---|
| 98 | value = ""; |
---|
| 99 | } // if |
---|
[51b7345] | 100 | |
---|
[b87a5ed] | 101 | classify( value ); |
---|
[51b7345] | 102 | } |
---|
| 103 | |
---|
[b87a5ed] | 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 ) { |
---|
[51b7345] | 106 | } |
---|
| 107 | |
---|
| 108 | // for some reason, std::tolower doesn't work as an argument to std::transform in g++ 3.1 |
---|
[3848e0e] | 109 | inline char tolower_hack( char c ) { |
---|
[b87a5ed] | 110 | return std::tolower( c ); |
---|
[51b7345] | 111 | } |
---|
| 112 | |
---|
[a08ba92] | 113 | void ConstantNode::classify( std::string &str ) { |
---|
| 114 | switch ( type ) { |
---|
[b87a5ed] | 115 | case Integer: |
---|
| 116 | case Float: |
---|
| 117 | { |
---|
| 118 | std::string sfx(""); |
---|
| 119 | char c; |
---|
| 120 | int i = str.length() - 1; |
---|
[51b7345] | 121 | |
---|
[a32b204] | 122 | while ( i >= 0 && ! isxdigit( c = str.at( i--)) ) |
---|
[b87a5ed] | 123 | sfx += c; |
---|
[51b7345] | 124 | |
---|
[b87a5ed] | 125 | value = str.substr( 0, i + 2 ); |
---|
[51b7345] | 126 | |
---|
[b87a5ed] | 127 | // get rid of underscores |
---|
| 128 | value.erase( remove( value.begin(), value.end(), '_'), value.end()); |
---|
[51b7345] | 129 | |
---|
[b87a5ed] | 130 | std::transform( sfx.begin(), sfx.end(), sfx.begin(), tolower_hack ); |
---|
[51b7345] | 131 | |
---|
[a08ba92] | 132 | if ( sfx.find("ll") != string::npos ) { |
---|
[b87a5ed] | 133 | longs = 2; |
---|
[a08ba92] | 134 | } else if ( sfx.find("l") != string::npos ) { |
---|
[b87a5ed] | 135 | longs = 1; |
---|
| 136 | } // if |
---|
[51b7345] | 137 | |
---|
[b87a5ed] | 138 | assert(( longs >= 0) && ( longs <= 2)); |
---|
[51b7345] | 139 | |
---|
[b87a5ed] | 140 | if ( sfx.find("u") != string::npos ) |
---|
| 141 | sign = false; |
---|
[51b7345] | 142 | |
---|
[b87a5ed] | 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()); |
---|
[51b7345] | 150 | |
---|
[b87a5ed] | 151 | break; |
---|
| 152 | } |
---|
| 153 | default: |
---|
| 154 | // shouldn't be here |
---|
| 155 | ; |
---|
[3848e0e] | 156 | } |
---|
[51b7345] | 157 | } |
---|
| 158 | |
---|
[bdd516a] | 159 | ConstantNode::Type ConstantNode::get_type( void ) const { |
---|
[b87a5ed] | 160 | return type; |
---|
[51b7345] | 161 | } |
---|
| 162 | |
---|
[3848e0e] | 163 | ConstantNode *ConstantNode::append( std::string *newValue ) { |
---|
[b87a5ed] | 164 | if ( newValue ) { |
---|
[a08ba92] | 165 | if ( type == String ) { |
---|
[b87a5ed] | 166 | std::string temp = *newValue; |
---|
| 167 | value.resize( value.size() - 1 ); |
---|
| 168 | value += newValue->substr(1, newValue->size()); |
---|
| 169 | } else |
---|
| 170 | value += *newValue; |
---|
[51b7345] | 171 | |
---|
[b87a5ed] | 172 | delete newValue; |
---|
| 173 | } // if |
---|
| 174 | return this; |
---|
[51b7345] | 175 | } |
---|
| 176 | |
---|
[bdd516a] | 177 | void ConstantNode::printOneLine( std::ostream &os, int indent ) const { |
---|
[b87a5ed] | 178 | os << string( indent, ' '); |
---|
| 179 | printDesignation( os ); |
---|
| 180 | |
---|
| 181 | switch ( type ) { |
---|
| 182 | /* integers */ |
---|
| 183 | case Integer: |
---|
| 184 | os << value ; |
---|
| 185 | break; |
---|
| 186 | case Float: |
---|
| 187 | os << value ; |
---|
| 188 | break; |
---|
| 189 | |
---|
| 190 | case Character: |
---|
| 191 | os << "'" << value << "'"; |
---|
| 192 | break; |
---|
| 193 | |
---|
| 194 | case String: |
---|
| 195 | os << '"' << value << '"'; |
---|
| 196 | break; |
---|
| 197 | } |
---|
[51b7345] | 198 | |
---|
[b87a5ed] | 199 | os << ' '; |
---|
[51b7345] | 200 | } |
---|
| 201 | |
---|
[bdd516a] | 202 | void ConstantNode::print( std::ostream &os, int indent ) const { |
---|
[b87a5ed] | 203 | printOneLine( os, indent ); |
---|
| 204 | os << endl; |
---|
[51b7345] | 205 | } |
---|
| 206 | |
---|
| 207 | Expression *ConstantNode::build() const { |
---|
[b87a5ed] | 208 | ::Type::Qualifiers q; |
---|
| 209 | BasicType *bt; |
---|
| 210 | |
---|
[a08ba92] | 211 | switch ( get_type()) { |
---|
[b87a5ed] | 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; |
---|
| 223 | 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() ) ); |
---|
| 232 | } |
---|
| 233 | return new ConstantExpr( Constant( bt, get_value()), maybeBuild< Expression >( get_argName() ) ); |
---|
[51b7345] | 234 | } |
---|
| 235 | |
---|
[bdd516a] | 236 | VarRefNode::VarRefNode() : isLabel( false ) {} |
---|
[51b7345] | 237 | |
---|
[bdd516a] | 238 | VarRefNode::VarRefNode( string *name_, bool labelp ) : ExpressionNode( name_), isLabel( labelp ) {} |
---|
[51b7345] | 239 | |
---|
[3848e0e] | 240 | VarRefNode::VarRefNode( const VarRefNode &other ) : ExpressionNode( other ), isLabel( other.isLabel ) { |
---|
[51b7345] | 241 | } |
---|
| 242 | |
---|
| 243 | Expression *VarRefNode::build() const { |
---|
[b87a5ed] | 244 | return new NameExpr( get_name(), maybeBuild< Expression >( get_argName() ) ); |
---|
[51b7345] | 245 | } |
---|
| 246 | |
---|
[bdd516a] | 247 | void VarRefNode::printOneLine( std::ostream &os, int indent ) const { |
---|
[b87a5ed] | 248 | printDesignation( os ); |
---|
| 249 | os << get_name() << ' '; |
---|
[51b7345] | 250 | } |
---|
| 251 | |
---|
[bdd516a] | 252 | void VarRefNode::print( std::ostream &os, int indent ) const { |
---|
[b87a5ed] | 253 | printDesignation( os ); |
---|
| 254 | os << '\r' << string( indent, ' ') << "Referencing: "; |
---|
| 255 | os << "Variable: " << get_name(); |
---|
| 256 | os << endl; |
---|
[51b7345] | 257 | } |
---|
| 258 | |
---|
[bdd516a] | 259 | OperatorNode::OperatorNode( Type t ) : type( t ) {} |
---|
[51b7345] | 260 | |
---|
[3848e0e] | 261 | OperatorNode::OperatorNode( const OperatorNode &other ) : ExpressionNode( other ), type( other.type ) { |
---|
[51b7345] | 262 | } |
---|
| 263 | |
---|
| 264 | OperatorNode::~OperatorNode() {} |
---|
| 265 | |
---|
[bdd516a] | 266 | OperatorNode::Type OperatorNode::get_type( void ) const{ |
---|
[b87a5ed] | 267 | return type; |
---|
[51b7345] | 268 | } |
---|
| 269 | |
---|
[3848e0e] | 270 | void OperatorNode::printOneLine( std::ostream &os, int indent ) const { |
---|
[b87a5ed] | 271 | printDesignation( os ); |
---|
| 272 | os << OpName[ type ] << ' '; |
---|
[51b7345] | 273 | } |
---|
| 274 | |
---|
| 275 | void OperatorNode::print( std::ostream &os, int indent ) const{ |
---|
[b87a5ed] | 276 | printDesignation( os ); |
---|
| 277 | os << '\r' << string( indent, ' ') << "Operator: " << OpName[type] << endl; |
---|
| 278 | return; |
---|
[51b7345] | 279 | } |
---|
| 280 | |
---|
[bdd516a] | 281 | std::string OperatorNode::get_typename( void ) const{ |
---|
[b87a5ed] | 282 | return string( OpName[ type ]); |
---|
[51b7345] | 283 | } |
---|
| 284 | |
---|
[3848e0e] | 285 | const char *OperatorNode::OpName[] = { |
---|
[b87a5ed] | 286 | "TupleC", "Comma", "TupleFieldSel",// "TuplePFieldSel", //n-adic |
---|
| 287 | // triadic |
---|
| 288 | "Cond", "NCond", |
---|
| 289 | // diadic |
---|
| 290 | "SizeOf", "AlignOf", "Attr", "CompLit", "Plus", "Minus", "Mul", "Div", "Mod", "Or", |
---|
| 291 | "And", "BitOr", "BitAnd", "Xor", "Cast", "LShift", "RShift", "LThan", "GThan", |
---|
| 292 | "LEThan", "GEThan", "Eq", "Neq", "Assign", "MulAssn", "DivAssn", "ModAssn", "PlusAssn", |
---|
| 293 | "MinusAssn", "LSAssn", "RSAssn", "AndAssn", "ERAssn", "OrAssn", "Index", "FieldSel","PFieldSel", |
---|
| 294 | "Range", |
---|
| 295 | // monadic |
---|
| 296 | "UnPlus", "UnMinus", "AddressOf", "PointTo", "Neg", "BitNeg", "Incr", "IncrPost", "Decr", "DecrPost", "LabelAddress" |
---|
[3848e0e] | 297 | }; |
---|
[51b7345] | 298 | |
---|
[bdd516a] | 299 | CompositeExprNode::CompositeExprNode( void ) : ExpressionNode(), function( 0 ), arguments( 0 ) { |
---|
[51b7345] | 300 | } |
---|
| 301 | |
---|
[bdd516a] | 302 | CompositeExprNode::CompositeExprNode( string *name_) : ExpressionNode( name_), function( 0 ), arguments( 0 ) { |
---|
[51b7345] | 303 | } |
---|
| 304 | |
---|
[bdd516a] | 305 | CompositeExprNode::CompositeExprNode( ExpressionNode *f, ExpressionNode *args ): |
---|
[b87a5ed] | 306 | function( f ), arguments( args ) { |
---|
[51b7345] | 307 | } |
---|
| 308 | |
---|
[bdd516a] | 309 | CompositeExprNode::CompositeExprNode( ExpressionNode *f, ExpressionNode *arg1, ExpressionNode *arg2): |
---|
[b87a5ed] | 310 | function( f ), arguments( arg1) { |
---|
| 311 | arguments->set_link( arg2); |
---|
[51b7345] | 312 | } |
---|
| 313 | |
---|
[3848e0e] | 314 | CompositeExprNode::CompositeExprNode( const CompositeExprNode &other ) : ExpressionNode( other ), function( maybeClone( other.function ) ) { |
---|
[b87a5ed] | 315 | ParseNode *cur = other.arguments; |
---|
| 316 | while ( cur ) { |
---|
| 317 | if ( arguments ) { |
---|
| 318 | arguments->set_link( cur->clone() ); |
---|
| 319 | } else { |
---|
| 320 | arguments = ( ExpressionNode*)cur->clone(); |
---|
| 321 | } // if |
---|
| 322 | cur = cur->get_link(); |
---|
| 323 | } |
---|
[51b7345] | 324 | } |
---|
| 325 | |
---|
[3848e0e] | 326 | CompositeExprNode::~CompositeExprNode() { |
---|
[b87a5ed] | 327 | delete function; |
---|
| 328 | delete arguments; |
---|
[51b7345] | 329 | } |
---|
| 330 | |
---|
| 331 | // the names that users use to define operator functions |
---|
[3848e0e] | 332 | static const char *opFuncName[] = { |
---|
[b87a5ed] | 333 | "", "", "", |
---|
| 334 | "", "", |
---|
| 335 | // diadic |
---|
| 336 | "", "", "", "", "?+?", "?-?", "?*?", "?/?", "?%?", "", "", |
---|
| 337 | "?|?", "?&?", "?^?", "", "?<<?", "?>>?", "?<?", "?>?", "?<=?", |
---|
| 338 | "?>=?", "?==?", "?!=?", "?=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", |
---|
| 339 | "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?", "?[?]", "","","Range", |
---|
| 340 | // monadic |
---|
| 341 | "+?", "-?", "", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "LabAddress" |
---|
[3848e0e] | 342 | }; |
---|
[51b7345] | 343 | |
---|
| 344 | #include "utility.h" |
---|
| 345 | |
---|
[3848e0e] | 346 | Expression *CompositeExprNode::build() const { |
---|
[b87a5ed] | 347 | OperatorNode *op; |
---|
| 348 | std::list<Expression *> args; |
---|
[51b7345] | 349 | |
---|
[b87a5ed] | 350 | buildList( get_args(), args ); |
---|
| 351 | |
---|
| 352 | if ( ! ( op = dynamic_cast<OperatorNode *>( function )) ) { |
---|
| 353 | // a function as opposed to an operator |
---|
| 354 | return new UntypedExpr( function->build(), args, maybeBuild< Expression >( get_argName() )); |
---|
| 355 | } else { |
---|
[a08ba92] | 356 | switch ( op->get_type()) { |
---|
[b87a5ed] | 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 |
---|
[a32b204] | 373 | assert( ! args.empty() ); |
---|
[b87a5ed] | 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 ); |
---|
| 474 | delete member; |
---|
| 475 | return ret; |
---|
| 476 | } |
---|
| 477 | case OperatorNode::AlignOf: |
---|
| 478 | case OperatorNode::SizeOf: |
---|
| 479 | { |
---|
[bdd516a] | 480 | /// bool isSizeOf = ( op->get_type() == OperatorNode::SizeOf ); |
---|
[51b7345] | 481 | |
---|
[b87a5ed] | 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 ); |
---|
[a32b204] | 492 | if ( ! get_args()->get_link() ) { |
---|
[b87a5ed] | 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; |
---|
[3848e0e] | 538 | } |
---|
| 539 | } |
---|
[51b7345] | 540 | } |
---|
| 541 | |
---|
[bdd516a] | 542 | void CompositeExprNode::printOneLine( std::ostream &os, int indent ) const { |
---|
[b87a5ed] | 543 | printDesignation( os ); |
---|
| 544 | os << "( "; |
---|
| 545 | function->printOneLine( os, indent ); |
---|
| 546 | for ( ExpressionNode *cur = arguments; cur != 0; cur = dynamic_cast< ExpressionNode* >( cur->get_link() ) ) { |
---|
| 547 | cur->printOneLine( os, indent ); |
---|
| 548 | } |
---|
| 549 | os << ") "; |
---|
[51b7345] | 550 | } |
---|
| 551 | |
---|
[bdd516a] | 552 | void CompositeExprNode::print( std::ostream &os, int indent ) const { |
---|
[b87a5ed] | 553 | printDesignation( os ); |
---|
| 554 | os << '\r' << string( indent, ' ') << "Application of: " << endl; |
---|
| 555 | function->print( os, indent + ParseNode::indent_by ); |
---|
[51b7345] | 556 | |
---|
[b87a5ed] | 557 | os << '\r' << string( indent, ' ') ; |
---|
| 558 | if ( arguments ) { |
---|
| 559 | os << "... on arguments: " << endl; |
---|
| 560 | arguments->printList( os, indent + ParseNode::indent_by ); |
---|
| 561 | } else |
---|
| 562 | os << "... on no arguments: " << endl; |
---|
[51b7345] | 563 | } |
---|
| 564 | |
---|
[a08ba92] | 565 | void CompositeExprNode::set_function( ExpressionNode *f ) { |
---|
[b87a5ed] | 566 | function = f; |
---|
[51b7345] | 567 | } |
---|
| 568 | |
---|
[a08ba92] | 569 | void CompositeExprNode::set_args( ExpressionNode *args ) { |
---|
[b87a5ed] | 570 | arguments = args; |
---|
[51b7345] | 571 | } |
---|
| 572 | |
---|
[bdd516a] | 573 | ExpressionNode *CompositeExprNode::get_function( void ) const { |
---|
[b87a5ed] | 574 | return function; |
---|
[51b7345] | 575 | } |
---|
| 576 | |
---|
[bdd516a] | 577 | ExpressionNode *CompositeExprNode::get_args( void ) const { |
---|
[b87a5ed] | 578 | return arguments; |
---|
[51b7345] | 579 | } |
---|
| 580 | |
---|
[a08ba92] | 581 | void CompositeExprNode::add_arg( ExpressionNode *arg ) { |
---|
[b87a5ed] | 582 | if ( arguments ) |
---|
| 583 | arguments->set_link( arg ); |
---|
| 584 | else |
---|
| 585 | set_args( arg ); |
---|
[51b7345] | 586 | } |
---|
| 587 | |
---|
[bdd516a] | 588 | CommaExprNode::CommaExprNode(): CompositeExprNode( new OperatorNode( OperatorNode::Comma )) {} |
---|
[51b7345] | 589 | |
---|
[bdd516a] | 590 | CommaExprNode::CommaExprNode( ExpressionNode *exp ) : CompositeExprNode( new OperatorNode( OperatorNode::Comma ), exp ) { |
---|
[3848e0e] | 591 | } |
---|
[51b7345] | 592 | |
---|
[bdd516a] | 593 | CommaExprNode::CommaExprNode( ExpressionNode *exp1, ExpressionNode *exp2) : CompositeExprNode( new OperatorNode( OperatorNode::Comma ), exp1, exp2) { |
---|
[51b7345] | 594 | } |
---|
| 595 | |
---|
[a08ba92] | 596 | CommaExprNode *CommaExprNode::add_to_list( ExpressionNode *exp ) { |
---|
[b87a5ed] | 597 | add_arg( exp ); |
---|
[51b7345] | 598 | |
---|
[b87a5ed] | 599 | return this; |
---|
[51b7345] | 600 | } |
---|
| 601 | |
---|
[3848e0e] | 602 | CommaExprNode::CommaExprNode( const CommaExprNode &other ) : CompositeExprNode( other ) { |
---|
[51b7345] | 603 | } |
---|
| 604 | |
---|
[bdd516a] | 605 | ValofExprNode::ValofExprNode( StatementNode *s ): body( s ) {} |
---|
[51b7345] | 606 | |
---|
[3848e0e] | 607 | ValofExprNode::ValofExprNode( const ValofExprNode &other ) : ExpressionNode( other ), body( maybeClone( body ) ) { |
---|
[51b7345] | 608 | } |
---|
| 609 | |
---|
| 610 | ValofExprNode::~ValofExprNode() { |
---|
[b87a5ed] | 611 | delete body; |
---|
[51b7345] | 612 | } |
---|
| 613 | |
---|
| 614 | void ValofExprNode::print( std::ostream &os, int indent ) const { |
---|
[b87a5ed] | 615 | printDesignation( os ); |
---|
| 616 | os << string( indent, ' ') << "Valof Expression:" << std::endl; |
---|
| 617 | get_body()->print( os, indent + 4); |
---|
[51b7345] | 618 | } |
---|
| 619 | |
---|
[3848e0e] | 620 | void ValofExprNode::printOneLine( std::ostream &, int indent ) const { |
---|
[b87a5ed] | 621 | assert( false ); |
---|
[51b7345] | 622 | } |
---|
| 623 | |
---|
| 624 | Expression *ValofExprNode::build() const { |
---|
[b87a5ed] | 625 | return new UntypedValofExpr ( get_body()->build(), maybeBuild< Expression >( get_argName() ) ); |
---|
[3848e0e] | 626 | } |
---|
| 627 | |
---|
[bdd516a] | 628 | ForCtlExprNode::ForCtlExprNode( ParseNode *init_, ExpressionNode *cond, ExpressionNode *incr ) throw ( SemanticError ) : condition( cond ), change( incr ) { |
---|
[b87a5ed] | 629 | if ( init_ == 0 ) |
---|
| 630 | init = 0; |
---|
| 631 | else { |
---|
| 632 | DeclarationNode *decl; |
---|
| 633 | ExpressionNode *exp; |
---|
| 634 | |
---|
| 635 | if (( decl = dynamic_cast<DeclarationNode *>( init_)) != 0) |
---|
| 636 | init = new StatementNode( decl ); |
---|
| 637 | else if (( exp = dynamic_cast<ExpressionNode *>( init_)) != 0) |
---|
| 638 | init = new StatementNode( StatementNode::Exp, exp ); |
---|
| 639 | else |
---|
| 640 | throw SemanticError("Error in for control expression"); |
---|
| 641 | } |
---|
[51b7345] | 642 | } |
---|
| 643 | |
---|
| 644 | ForCtlExprNode::ForCtlExprNode( const ForCtlExprNode &other ) |
---|
[b87a5ed] | 645 | : ExpressionNode( other ), init( maybeClone( other.init ) ), condition( maybeClone( other.condition ) ), change( maybeClone( other.change ) ) { |
---|
[51b7345] | 646 | } |
---|
| 647 | |
---|
[a08ba92] | 648 | ForCtlExprNode::~ForCtlExprNode() { |
---|
[b87a5ed] | 649 | delete init; |
---|
| 650 | delete condition; |
---|
| 651 | delete change; |
---|
[51b7345] | 652 | } |
---|
| 653 | |
---|
| 654 | Expression *ForCtlExprNode::build() const { |
---|
[b87a5ed] | 655 | // this shouldn't be used! |
---|
| 656 | assert( false ); |
---|
| 657 | return 0; |
---|
[51b7345] | 658 | } |
---|
| 659 | |
---|
| 660 | void ForCtlExprNode::print( std::ostream &os, int indent ) const{ |
---|
[b87a5ed] | 661 | os << string( indent,' ') << "For Control Expression -- : " << endl; |
---|
[51b7345] | 662 | |
---|
[b87a5ed] | 663 | os << "\r" << string( indent + 2,' ') << "initialization: "; |
---|
| 664 | if ( init != 0) |
---|
| 665 | init->print( os, indent + 4); |
---|
[51b7345] | 666 | |
---|
[b87a5ed] | 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); |
---|
[51b7345] | 673 | } |
---|
| 674 | |
---|
[3848e0e] | 675 | void ForCtlExprNode::printOneLine( std::ostream &, int indent ) const { |
---|
[b87a5ed] | 676 | assert( false ); |
---|
[51b7345] | 677 | } |
---|
| 678 | |
---|
[bdd516a] | 679 | TypeValueNode::TypeValueNode( DeclarationNode *decl ) |
---|
[b87a5ed] | 680 | : decl( decl ) { |
---|
[51b7345] | 681 | } |
---|
| 682 | |
---|
| 683 | TypeValueNode::TypeValueNode( const TypeValueNode &other ) |
---|
[b87a5ed] | 684 | : ExpressionNode( other ), decl( maybeClone( other.decl ) ) { |
---|
[51b7345] | 685 | } |
---|
| 686 | |
---|
[3848e0e] | 687 | Expression *TypeValueNode::build() const { |
---|
[b87a5ed] | 688 | return new TypeExpr( decl->buildType() ); |
---|
[51b7345] | 689 | } |
---|
| 690 | |
---|
[bdd516a] | 691 | void TypeValueNode::print( std::ostream &os, int indent ) const { |
---|
[b87a5ed] | 692 | os << std::string( indent, ' ' ) << "Type:"; |
---|
| 693 | get_decl()->print( os, indent + 2); |
---|
[51b7345] | 694 | } |
---|
| 695 | |
---|
[bdd516a] | 696 | void TypeValueNode::printOneLine( std::ostream &os, int indent ) const { |
---|
[b87a5ed] | 697 | os << "Type:"; |
---|
| 698 | get_decl()->print( os, indent + 2); |
---|
[51b7345] | 699 | } |
---|
| 700 | |
---|
[3848e0e] | 701 | ExpressionNode *flattenCommas( ExpressionNode *list ) { |
---|
[b87a5ed] | 702 | if ( CompositeExprNode *composite = dynamic_cast< CompositeExprNode * >( list ) ) |
---|
[3848e0e] | 703 | { |
---|
[b87a5ed] | 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 | } |
---|
[3848e0e] | 711 | } |
---|
[51b7345] | 712 | |
---|
[b87a5ed] | 713 | if ( ExpressionNode *next = dynamic_cast< ExpressionNode * >( list->get_link() ) ) |
---|
| 714 | list->set_next( flattenCommas( next ) ); |
---|
[51b7345] | 715 | |
---|
[b87a5ed] | 716 | return list; |
---|
[51b7345] | 717 | } |
---|
| 718 | |
---|
[3848e0e] | 719 | ExpressionNode *tupleContents( ExpressionNode *tuple ) { |
---|
[b87a5ed] | 720 | if ( CompositeExprNode *composite = dynamic_cast< CompositeExprNode * >( tuple ) ) { |
---|
| 721 | OperatorNode *op = 0; |
---|
| 722 | if ( ( op = dynamic_cast< OperatorNode * >( composite->get_function() )) && ( op->get_type() == OperatorNode::TupleC ) ) |
---|
| 723 | return composite->get_args(); |
---|
| 724 | } |
---|
| 725 | return tuple; |
---|
[51b7345] | 726 | } |
---|
[b87a5ed] | 727 | |
---|
| 728 | // Local Variables: // |
---|
| 729 | // tab-width: 4 // |
---|
| 730 | // mode: c++ // |
---|
| 731 | // compile-command: "make install" // |
---|
| 732 | // End: // |
---|