Changeset 0dd3a2f for translator/SynTree/Expression.cc
- Timestamp:
- May 18, 2015, 11:20:23 AM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 51587aa
- Parents:
- a32b204
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
translator/SynTree/Expression.cc
ra32b204 r0dd3a2f 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 // Expression.cc -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon May 18 08:27:07 2015 13 // Update Count : 2 14 // 15 1 16 #include <iostream> 2 17 #include <cassert> … … 14 29 15 30 16 //*** Expression17 31 Expression::Expression( Expression *_aname ) : env( 0 ), argName( _aname ) {} 18 Expression::Expression( const Expression &other ) 19 : env( maybeClone( other.env ) ) 20 { 21 cloneAll( other.results, results ); 22 argName = other.get_argName(); 23 } 24 Expression::~Expression() 25 { 26 delete env; 27 // delete argName; // xxx -- there's a problem in cloning ConstantExpr I still don't know how to fix 28 deleteAll( results ); 29 } 30 31 void 32 Expression::add_result( Type *t ) 33 { 34 if ( TupleType *tuple = dynamic_cast< TupleType* >( t ) ) { 35 std::copy( tuple->get_types().begin(), tuple->get_types().end(), back_inserter( results ) ); 36 } else { 37 results.push_back(t); 38 } 32 33 Expression::Expression( const Expression &other ) : env( maybeClone( other.env ) ) { 34 cloneAll( other.results, results ); 35 argName = other.get_argName(); 36 } 37 38 Expression::~Expression() { 39 delete env; 40 // delete argName; // xxx -- there's a problem in cloning ConstantExpr I still don't know how to fix 41 deleteAll( results ); 42 } 43 44 void Expression::add_result( Type *t ) { 45 if ( TupleType *tuple = dynamic_cast< TupleType* >( t ) ) { 46 std::copy( tuple->get_types().begin(), tuple->get_types().end(), back_inserter( results ) ); 47 } else { 48 results.push_back(t); 49 } // if 39 50 } 40 51 41 52 void Expression::print(std::ostream &os, int indent) const { 42 if ( env ) { 43 os << std::string(indent, ' ') << "with environment:" << std::endl; 44 env->print( os, indent+2 ); 45 } 46 47 if ( argName ) { 48 os << std::string(indent, ' ') << "with designator:"; 49 argName->print( os, indent+2 ); 50 } 51 } 52 53 //*** ConstantExpr 54 ConstantExpr::ConstantExpr(Constant _c, Expression *_aname ): 55 Expression( _aname ), constant(_c) 56 { 57 add_result( constant.get_type()->clone() ); 58 } 59 60 ConstantExpr::ConstantExpr( const ConstantExpr &other) 61 : Expression( other ), constant( other.constant ) 62 {} 53 if ( env ) { 54 os << std::string(indent, ' ') << "with environment:" << std::endl; 55 env->print( os, indent+2 ); 56 } // if 57 58 if ( argName ) { 59 os << std::string(indent, ' ') << "with designator:"; 60 argName->print( os, indent+2 ); 61 } // if 62 } 63 64 ConstantExpr::ConstantExpr( Constant _c, Expression *_aname ) : Expression( _aname ), constant( _c ) { 65 add_result( constant.get_type()->clone() ); 66 } 67 68 ConstantExpr::ConstantExpr( const ConstantExpr &other) : Expression( other ), constant( other.constant ) { 69 } 63 70 64 71 ConstantExpr::~ConstantExpr() {} 65 72 66 void ConstantExpr::print(std::ostream &os, int indent) const { 67 os << std::string(indent, ' ') << "Constant Expression: " ; 68 constant.print(os); 69 os << std::endl; 70 Expression::print( os, indent ); 71 } 72 73 //*** VariableExpr 74 VariableExpr::VariableExpr( DeclarationWithType *_var, Expression *_aname ) : Expression( _aname ), var( _var ) 75 { 76 add_result( var->get_type()->clone() ); 77 for ( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) { 78 (*i)->set_isLvalue( true ); 79 } 80 } 81 82 VariableExpr::VariableExpr( const VariableExpr &other ) 83 : Expression( other ), var( other.var ) 84 { 85 } 86 87 VariableExpr::~VariableExpr() 88 { 89 // don't delete the declaration, since it points somewhere else in the tree 90 } 91 92 void VariableExpr::print(std::ostream &os, int indent) const { 93 os << std::string(indent, ' ') << "Variable Expression: "; 94 95 Declaration *decl = get_var(); 96 // if ( decl != 0) decl->print(os, indent + 2); 97 if ( decl != 0) decl->printShort(os, indent + 2); 98 os << std::endl; 99 Expression::print( os, indent ); 100 } 101 102 //*** SizeofExpr 73 void ConstantExpr::print( std::ostream &os, int indent ) const { 74 os << std::string(indent, ' ') << "Constant Expression: " ; 75 constant.print(os); 76 os << std::endl; 77 Expression::print( os, indent ); 78 } 79 80 VariableExpr::VariableExpr( DeclarationWithType *_var, Expression *_aname ) : Expression( _aname ), var( _var ) { 81 add_result( var->get_type()->clone() ); 82 for ( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) { 83 (*i)->set_isLvalue( true ); 84 } // for 85 } 86 87 VariableExpr::VariableExpr( const VariableExpr &other ) : Expression( other ), var( other.var ) { 88 } 89 90 VariableExpr::~VariableExpr() { 91 // don't delete the declaration, since it points somewhere else in the tree 92 } 93 94 void VariableExpr::print( std::ostream &os, int indent ) const { 95 os << std::string(indent, ' ') << "Variable Expression: "; 96 97 Declaration *decl = get_var(); 98 // if ( decl != 0) decl->print(os, indent + 2); 99 if ( decl != 0) decl->printShort(os, indent + 2); 100 os << std::endl; 101 Expression::print( os, indent ); 102 } 103 103 104 SizeofExpr::SizeofExpr( Expression *expr_, Expression *_aname ) : 104 Expression( _aname ), expr(expr_), type(0), isType(false) 105 { 106 add_result( new BasicType( Type::Qualifiers(), BasicType::UnsignedInt ) ); 105 Expression( _aname ), expr(expr_), type(0), isType(false) { 106 add_result( new BasicType( Type::Qualifiers(), BasicType::UnsignedInt ) ); 107 107 } 108 108 109 109 SizeofExpr::SizeofExpr( Type *type_, Expression *_aname ) : 110 Expression( _aname ), expr(0), type(type_), isType(true) 111 { 112 add_result( new BasicType( Type::Qualifiers(), BasicType::UnsignedInt ) ); 113 } 114 115 SizeofExpr::SizeofExpr( const SizeofExpr &other ) 116 : Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) 117 { 118 } 119 120 SizeofExpr::~SizeofExpr(){ 121 delete expr; 122 delete type; 110 Expression( _aname ), expr(0), type(type_), isType(true) { 111 add_result( new BasicType( Type::Qualifiers(), BasicType::UnsignedInt ) ); 112 } 113 114 SizeofExpr::SizeofExpr( const SizeofExpr &other ) : 115 Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) { 116 } 117 118 SizeofExpr::~SizeofExpr() { 119 delete expr; 120 delete type; 123 121 } 124 122 125 123 void SizeofExpr::print( std::ostream &os, int indent) const { 126 os << std::string(indent, ' ') << "Sizeof Expression on: "; 127 128 if (isType) 129 type->print(os, indent + 2); 130 else 131 expr->print(os, indent + 2); 132 133 os << std::endl; 134 Expression::print( os, indent ); 135 } 136 137 //*** AttrExpr 124 os << std::string(indent, ' ') << "Sizeof Expression on: "; 125 126 if (isType) 127 type->print(os, indent + 2); 128 else 129 expr->print(os, indent + 2); 130 131 os << std::endl; 132 Expression::print( os, indent ); 133 } 134 138 135 AttrExpr::AttrExpr( Expression *attr, Expression *expr_, Expression *_aname ) : 139 Expression( _aname ), attr( attr ), expr(expr_), type(0), isType(false) 140 { 136 Expression( _aname ), attr( attr ), expr(expr_), type(0), isType(false) { 141 137 } 142 138 143 139 AttrExpr::AttrExpr( Expression *attr, Type *type_, Expression *_aname ) : 144 Expression( _aname ), attr( attr ), expr(0), type(type_), isType(true) 145 { 146 } 147 148 AttrExpr::AttrExpr( const AttrExpr &other ) 149 : Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) 150 { 151 } 152 153 AttrExpr::~AttrExpr(){ 154 delete attr; 155 delete expr; 156 delete type; 140 Expression( _aname ), attr( attr ), expr(0), type(type_), isType(true) { 141 } 142 143 AttrExpr::AttrExpr( const AttrExpr &other ) : 144 Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) { 145 } 146 147 AttrExpr::~AttrExpr() { 148 delete attr; 149 delete expr; 150 delete type; 157 151 } 158 152 159 153 void AttrExpr::print( std::ostream &os, int indent) const { 160 os << std::string(indent, ' ') << "Attr "; 161 attr->print( os, indent + 2 ); 162 if ( isType || expr ) { 163 os << "applied to: "; 164 165 if (isType) 166 type->print(os, indent + 2); 167 else 168 expr->print(os, indent + 2); 169 } 170 171 os << std::endl; 172 Expression::print( os, indent ); 173 } 174 175 //*** CastExpr 176 CastExpr::CastExpr( Expression *arg_, Type *toType, Expression *_aname ) : Expression( _aname ), arg(arg_) 177 { 178 add_result(toType); 154 os << std::string(indent, ' ') << "Attr "; 155 attr->print( os, indent + 2 ); 156 if ( isType || expr ) { 157 os << "applied to: "; 158 159 if (isType) 160 type->print(os, indent + 2); 161 else 162 expr->print(os, indent + 2); 163 } // if 164 165 os << std::endl; 166 Expression::print( os, indent ); 167 } 168 169 CastExpr::CastExpr( Expression *arg_, Type *toType, Expression *_aname ) : Expression( _aname ), arg(arg_) { 170 add_result(toType); 179 171 } 180 172 … … 182 174 } 183 175 184 CastExpr::CastExpr( const CastExpr &other ) 185 : Expression( other ), arg( maybeClone( other.arg ) ) 186 { 176 CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) { 187 177 } 188 178 189 179 CastExpr::~CastExpr() { 190 180 delete arg; 191 181 } 192 182 … … 194 184 195 185 void CastExpr::print( std::ostream &os, int indent ) const { 196 os << std::string(indent, ' ') << "Cast of:" << std::endl; 197 arg->print(os, indent+2); 198 os << std::endl << std::string(indent, ' ') << "to:" << std::endl; 199 if ( results.empty() ) { 200 os << std::string(indent+2, ' ') << "nothing" << std::endl; 201 } else { 202 printAll(results, os, indent+2); 203 } 204 Expression::print( os, indent ); 205 } 206 207 //*** UntypedMemberExpr 186 os << std::string(indent, ' ') << "Cast of:" << std::endl; 187 arg->print(os, indent+2); 188 os << std::endl << std::string(indent, ' ') << "to:" << std::endl; 189 if ( results.empty() ) { 190 os << std::string(indent+2, ' ') << "nothing" << std::endl; 191 } else { 192 printAll(results, os, indent+2); 193 } // if 194 Expression::print( os, indent ); 195 } 196 208 197 UntypedMemberExpr::UntypedMemberExpr( std::string _member, Expression *_aggregate, Expression *_aname ) : 209 Expression( _aname ), member(_member), aggregate(_aggregate) {} 210 211 UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) 212 : Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) 213 { 198 Expression( _aname ), member(_member), aggregate(_aggregate) {} 199 200 UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) : 201 Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) { 214 202 } 215 203 216 204 UntypedMemberExpr::~UntypedMemberExpr() { 217 205 delete aggregate; 218 206 } 219 207 220 208 void UntypedMemberExpr::print( std::ostream &os, int indent ) const { 221 os << std::string(indent, ' ') << "Member Expression, with field: " << get_member(); 222 223 Expression *agg = get_aggregate(); 224 os << std::string(indent, ' ') << "from aggregate: "; 225 if (agg != 0) agg->print(os, indent + 2); 226 Expression::print( os, indent ); 227 } 228 229 230 //*** MemberExpr 209 os << std::string(indent, ' ') << "Member Expression, with field: " << get_member(); 210 211 Expression *agg = get_aggregate(); 212 os << std::string(indent, ' ') << "from aggregate: "; 213 if (agg != 0) agg->print(os, indent + 2); 214 Expression::print( os, indent ); 215 } 216 217 231 218 MemberExpr::MemberExpr( DeclarationWithType *_member, Expression *_aggregate, Expression *_aname ) : 232 Expression( _aname ), member(_member), aggregate(_aggregate) 233 { 234 add_result( member->get_type()->clone() ); 235 for ( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) { 236 (*i)->set_isLvalue( true ); 237 } 238 } 239 240 MemberExpr::MemberExpr( const MemberExpr &other ) 241 : Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) 242 { 219 Expression( _aname ), member(_member), aggregate(_aggregate) { 220 add_result( member->get_type()->clone() ); 221 for ( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) { 222 (*i)->set_isLvalue( true ); 223 } // for 224 } 225 226 MemberExpr::MemberExpr( const MemberExpr &other ) : 227 Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) { 243 228 } 244 229 245 230 MemberExpr::~MemberExpr() { 246 247 231 delete member; 232 delete aggregate; 248 233 } 249 234 250 235 void MemberExpr::print( std::ostream &os, int indent ) const { 251 os << std::string(indent, ' ') << "Member Expression, with field: " << std::endl; 252 253 assert( member ); 254 os << std::string(indent + 2, ' '); 255 member->print( os, indent + 2 ); 256 os << std::endl; 257 258 Expression *agg = get_aggregate(); 259 os << std::string(indent, ' ') << "from aggregate: " << std::endl; 260 if (agg != 0) agg->print(os, indent + 2); 261 Expression::print( os, indent ); 262 } 263 264 265 //*** UntypedExpr 236 os << std::string(indent, ' ') << "Member Expression, with field: " << std::endl; 237 238 assert( member ); 239 os << std::string(indent + 2, ' '); 240 member->print( os, indent + 2 ); 241 os << std::endl; 242 243 Expression *agg = get_aggregate(); 244 os << std::string(indent, ' ') << "from aggregate: " << std::endl; 245 if (agg != 0) agg->print(os, indent + 2); 246 Expression::print( os, indent ); 247 } 248 249 266 250 UntypedExpr::UntypedExpr( Expression *_function, Expression *_aname ) : Expression( _aname ), function( _function ) {} 267 251 268 UntypedExpr::UntypedExpr( const UntypedExpr &other ) 269 : Expression( other ), function( maybeClone( other.function ) ) 270 { 271 cloneAll( other.args, args ); 252 UntypedExpr::UntypedExpr( const UntypedExpr &other ) : 253 Expression( other ), function( maybeClone( other.function ) ) { 254 cloneAll( other.args, args ); 272 255 } 273 256 274 257 UntypedExpr::UntypedExpr( Expression *_function, std::list<Expression *> &_args, Expression *_aname ) : 275 258 Expression( _aname ), function(_function), args(_args) {} 276 259 277 260 UntypedExpr::~UntypedExpr() {} 278 261 279 262 void UntypedExpr::print( std::ostream &os, int indent ) const { 280 os << std::string(indent, ' ') << "Applying untyped: " << std::endl; 281 function->print(os, indent + 4); 282 os << "\r" << std::string(indent, ' ') << "...to: " << std::endl; 283 printArgs(os, indent + 4); 284 Expression::print( os, indent ); 285 } 286 287 void UntypedExpr::printArgs(std::ostream &os, int indent ) const { 288 std::list<Expression *>::const_iterator i; 289 for (i = args.begin(); i != args.end(); i++) 290 (*i)->print(os, indent); 291 } 292 293 //*** NameExpr 263 os << std::string(indent, ' ') << "Applying untyped: " << std::endl; 264 function->print(os, indent + 4); 265 os << "\r" << std::string(indent, ' ') << "...to: " << std::endl; 266 printArgs(os, indent + 4); 267 Expression::print( os, indent ); 268 } 269 270 void UntypedExpr::printArgs( std::ostream &os, int indent ) const { 271 std::list<Expression *>::const_iterator i; 272 for (i = args.begin(); i != args.end(); i++) 273 (*i)->print(os, indent); 274 } 275 294 276 NameExpr::NameExpr( std::string _name, Expression *_aname ) : Expression( _aname ), name(_name) {} 295 277 296 NameExpr::NameExpr( const NameExpr &other ) 297 : Expression( other ), name( other.name ) 298 { 278 NameExpr::NameExpr( const NameExpr &other ) : Expression( other ), name( other.name ) { 299 279 } 300 280 … … 302 282 303 283 void NameExpr::print( std::ostream &os, int indent ) const { 304 os << std::string(indent, ' ') << "Name: " << get_name() << std::endl; 305 Expression::print( os, indent ); 306 } 307 308 //*** LogicalExpr 284 os << std::string(indent, ' ') << "Name: " << get_name() << std::endl; 285 Expression::print( os, indent ); 286 } 287 309 288 LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp, Expression *_aname ) : 310 Expression( _aname ), arg1(arg1_), arg2(arg2_), isAnd(andp) 311 { 312 add_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); 313 } 314 315 LogicalExpr::LogicalExpr( const LogicalExpr &other ) 316 : Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) 317 { 289 Expression( _aname ), arg1(arg1_), arg2(arg2_), isAnd(andp) { 290 add_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); 291 } 292 293 LogicalExpr::LogicalExpr( const LogicalExpr &other ) : 294 Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) { 318 295 } 319 296 320 297 LogicalExpr::~LogicalExpr(){ 321 322 298 delete arg1; 299 delete arg2; 323 300 } 324 301 325 302 void LogicalExpr::print( std::ostream &os, int indent )const { 326 os << std::string(indent, ' ') << "Short-circuited operation (" << (isAnd?"and":"or") << ") on: "; 327 arg1->print(os); 328 os << " and "; 329 arg2->print(os); 330 os << std::endl; 331 Expression::print( os, indent ); 332 } 333 334 //*** ConditionalExpr 303 os << std::string(indent, ' ') << "Short-circuited operation (" << (isAnd?"and":"or") << ") on: "; 304 arg1->print(os); 305 os << " and "; 306 arg2->print(os); 307 os << std::endl; 308 Expression::print( os, indent ); 309 } 310 335 311 ConditionalExpr::ConditionalExpr( Expression *arg1_, Expression *arg2_, Expression *arg3_, Expression *_aname ) : 336 Expression( _aname ), arg1(arg1_), arg2(arg2_), arg3(arg3_) {} 337 338 ConditionalExpr::ConditionalExpr( const ConditionalExpr &other ) 339 : Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) 340 { 341 } 312 Expression( _aname ), arg1(arg1_), arg2(arg2_), arg3(arg3_) {} 313 314 ConditionalExpr::ConditionalExpr( const ConditionalExpr &other ) : 315 Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) { 316 } 342 317 343 318 ConditionalExpr::~ConditionalExpr() { 344 345 346 319 delete arg1; 320 delete arg2; 321 delete arg3; 347 322 } 348 323 349 324 void ConditionalExpr::print( std::ostream &os, int indent ) const { 350 os << std::string(indent, ' ') << "Conditional expression on: " << std::endl; 351 arg1->print( os, indent+2 ); 352 os << std::string(indent, ' ') << "First alternative:" << std::endl; 353 arg2->print( os, indent+2 ); 354 os << std::string(indent, ' ') << "Second alternative:" << std::endl; 355 arg3->print( os, indent+2 ); 356 os << std::endl; 357 Expression::print( os, indent ); 358 } 359 360 //*** UntypedValofExpr 361 void UntypedValofExpr::print( std::ostream &os, int indent ) const 362 { 363 os << std::string(indent, ' ') << "Valof Expression: " << std::endl; 364 if ( get_body() != 0 ) 365 get_body()->print( os, indent + 2 ); 366 } 367 368 325 os << std::string(indent, ' ') << "Conditional expression on: " << std::endl; 326 arg1->print( os, indent+2 ); 327 os << std::string(indent, ' ') << "First alternative:" << std::endl; 328 arg2->print( os, indent+2 ); 329 os << std::string(indent, ' ') << "Second alternative:" << std::endl; 330 arg3->print( os, indent+2 ); 331 os << std::endl; 332 Expression::print( os, indent ); 333 } 334 335 void UntypedValofExpr::print( std::ostream &os, int indent ) const { 336 os << std::string(indent, ' ') << "Valof Expression: " << std::endl; 337 if ( get_body() != 0 ) 338 get_body()->print( os, indent + 2 ); 339 } 340 341 // Local Variables: // 342 // tab-width: 4 // 343 // mode: c++ // 344 // compile-command: "make install" // 345 // End: //
Note: See TracChangeset
for help on using the changeset viewer.