[0dd3a2f] | 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 |
---|
[7f5566b] | 12 | // Last Modified On : Tue Jul 28 16:27:17 2015 |
---|
| 13 | // Update Count : 22 |
---|
[0dd3a2f] | 14 | // |
---|
| 15 | |
---|
[51b7345] | 16 | #include <iostream> |
---|
| 17 | #include <cassert> |
---|
| 18 | #include <list> |
---|
| 19 | #include <algorithm> |
---|
| 20 | |
---|
| 21 | #include <iterator> |
---|
| 22 | |
---|
| 23 | #include "Type.h" |
---|
| 24 | #include "Expression.h" |
---|
| 25 | #include "Declaration.h" |
---|
| 26 | #include "Statement.h" |
---|
| 27 | #include "TypeSubstitution.h" |
---|
| 28 | #include "utility.h" |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | Expression::Expression( Expression *_aname ) : env( 0 ), argName( _aname ) {} |
---|
[0dd3a2f] | 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; |
---|
[7f5566b] | 40 | delete argName; // xxx -- there's a problem in cloning ConstantExpr I still don't know how to fix |
---|
[0dd3a2f] | 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 |
---|
[51b7345] | 50 | } |
---|
| 51 | |
---|
[59db689] | 52 | void Expression::print( std::ostream &os, int indent ) const { |
---|
[0dd3a2f] | 53 | if ( env ) { |
---|
[cf0941d] | 54 | os << std::string( indent, ' ' ) << "with environment:" << std::endl; |
---|
[0dd3a2f] | 55 | env->print( os, indent+2 ); |
---|
| 56 | } // if |
---|
[51b7345] | 57 | |
---|
[0dd3a2f] | 58 | if ( argName ) { |
---|
[cf0941d] | 59 | os << std::string( indent, ' ' ) << "with designator:"; |
---|
[0dd3a2f] | 60 | argName->print( os, indent+2 ); |
---|
| 61 | } // if |
---|
[51b7345] | 62 | } |
---|
| 63 | |
---|
[0dd3a2f] | 64 | ConstantExpr::ConstantExpr( Constant _c, Expression *_aname ) : Expression( _aname ), constant( _c ) { |
---|
| 65 | add_result( constant.get_type()->clone() ); |
---|
[51b7345] | 66 | } |
---|
| 67 | |
---|
[0dd3a2f] | 68 | ConstantExpr::ConstantExpr( const ConstantExpr &other) : Expression( other ), constant( other.constant ) { |
---|
| 69 | } |
---|
[51b7345] | 70 | |
---|
| 71 | ConstantExpr::~ConstantExpr() {} |
---|
| 72 | |
---|
[0dd3a2f] | 73 | void ConstantExpr::print( std::ostream &os, int indent ) const { |
---|
[5f2f2d7] | 74 | os << "constant expression " ; |
---|
[cf0941d] | 75 | constant.print( os ); |
---|
[0dd3a2f] | 76 | Expression::print( os, indent ); |
---|
[51b7345] | 77 | } |
---|
| 78 | |
---|
[0dd3a2f] | 79 | VariableExpr::VariableExpr( DeclarationWithType *_var, Expression *_aname ) : Expression( _aname ), var( _var ) { |
---|
| 80 | add_result( var->get_type()->clone() ); |
---|
| 81 | for ( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) { |
---|
| 82 | (*i)->set_isLvalue( true ); |
---|
| 83 | } // for |
---|
[51b7345] | 84 | } |
---|
| 85 | |
---|
[0dd3a2f] | 86 | VariableExpr::VariableExpr( const VariableExpr &other ) : Expression( other ), var( other.var ) { |
---|
[51b7345] | 87 | } |
---|
| 88 | |
---|
[0dd3a2f] | 89 | VariableExpr::~VariableExpr() { |
---|
| 90 | // don't delete the declaration, since it points somewhere else in the tree |
---|
[51b7345] | 91 | } |
---|
| 92 | |
---|
[0dd3a2f] | 93 | void VariableExpr::print( std::ostream &os, int indent ) const { |
---|
[44b5ca0] | 94 | os << std::string( indent, ' ' ) << "Variable Expression: "; |
---|
[51b7345] | 95 | |
---|
[0dd3a2f] | 96 | Declaration *decl = get_var(); |
---|
| 97 | // if ( decl != 0) decl->print(os, indent + 2); |
---|
| 98 | if ( decl != 0) decl->printShort(os, indent + 2); |
---|
| 99 | os << std::endl; |
---|
| 100 | Expression::print( os, indent ); |
---|
[51b7345] | 101 | } |
---|
| 102 | |
---|
| 103 | SizeofExpr::SizeofExpr( Expression *expr_, Expression *_aname ) : |
---|
[0dd3a2f] | 104 | Expression( _aname ), expr(expr_), type(0), isType(false) { |
---|
| 105 | add_result( new BasicType( Type::Qualifiers(), BasicType::UnsignedInt ) ); |
---|
[51b7345] | 106 | } |
---|
| 107 | |
---|
| 108 | SizeofExpr::SizeofExpr( Type *type_, Expression *_aname ) : |
---|
[0dd3a2f] | 109 | Expression( _aname ), expr(0), type(type_), isType(true) { |
---|
| 110 | add_result( new BasicType( Type::Qualifiers(), BasicType::UnsignedInt ) ); |
---|
[51b7345] | 111 | } |
---|
| 112 | |
---|
[0dd3a2f] | 113 | SizeofExpr::SizeofExpr( const SizeofExpr &other ) : |
---|
| 114 | Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) { |
---|
[51b7345] | 115 | } |
---|
| 116 | |
---|
[0dd3a2f] | 117 | SizeofExpr::~SizeofExpr() { |
---|
| 118 | delete expr; |
---|
| 119 | delete type; |
---|
[51b7345] | 120 | } |
---|
| 121 | |
---|
| 122 | void SizeofExpr::print( std::ostream &os, int indent) const { |
---|
[44b5ca0] | 123 | os << std::string( indent, ' ' ) << "Sizeof Expression on: "; |
---|
[51b7345] | 124 | |
---|
[0dd3a2f] | 125 | if (isType) |
---|
| 126 | type->print(os, indent + 2); |
---|
| 127 | else |
---|
| 128 | expr->print(os, indent + 2); |
---|
[51b7345] | 129 | |
---|
[0dd3a2f] | 130 | os << std::endl; |
---|
| 131 | Expression::print( os, indent ); |
---|
[51b7345] | 132 | } |
---|
| 133 | |
---|
| 134 | AttrExpr::AttrExpr( Expression *attr, Expression *expr_, Expression *_aname ) : |
---|
[0dd3a2f] | 135 | Expression( _aname ), attr( attr ), expr(expr_), type(0), isType(false) { |
---|
[51b7345] | 136 | } |
---|
| 137 | |
---|
| 138 | AttrExpr::AttrExpr( Expression *attr, Type *type_, Expression *_aname ) : |
---|
[0dd3a2f] | 139 | Expression( _aname ), attr( attr ), expr(0), type(type_), isType(true) { |
---|
[51b7345] | 140 | } |
---|
| 141 | |
---|
[0dd3a2f] | 142 | AttrExpr::AttrExpr( const AttrExpr &other ) : |
---|
| 143 | Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) { |
---|
[51b7345] | 144 | } |
---|
| 145 | |
---|
[0dd3a2f] | 146 | AttrExpr::~AttrExpr() { |
---|
| 147 | delete attr; |
---|
| 148 | delete expr; |
---|
| 149 | delete type; |
---|
[51b7345] | 150 | } |
---|
| 151 | |
---|
| 152 | void AttrExpr::print( std::ostream &os, int indent) const { |
---|
[44b5ca0] | 153 | os << std::string( indent, ' ' ) << "Attr "; |
---|
[0dd3a2f] | 154 | attr->print( os, indent + 2 ); |
---|
| 155 | if ( isType || expr ) { |
---|
| 156 | os << "applied to: "; |
---|
[51b7345] | 157 | |
---|
[0dd3a2f] | 158 | if (isType) |
---|
| 159 | type->print(os, indent + 2); |
---|
| 160 | else |
---|
| 161 | expr->print(os, indent + 2); |
---|
| 162 | } // if |
---|
[51b7345] | 163 | |
---|
[0dd3a2f] | 164 | os << std::endl; |
---|
| 165 | Expression::print( os, indent ); |
---|
[51b7345] | 166 | } |
---|
| 167 | |
---|
[0dd3a2f] | 168 | CastExpr::CastExpr( Expression *arg_, Type *toType, Expression *_aname ) : Expression( _aname ), arg(arg_) { |
---|
| 169 | add_result(toType); |
---|
[51b7345] | 170 | } |
---|
| 171 | |
---|
| 172 | CastExpr::CastExpr( Expression *arg_, Expression *_aname ) : Expression( _aname ), arg(arg_) { |
---|
| 173 | } |
---|
| 174 | |
---|
[0dd3a2f] | 175 | CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) { |
---|
[51b7345] | 176 | } |
---|
| 177 | |
---|
| 178 | CastExpr::~CastExpr() { |
---|
[0dd3a2f] | 179 | delete arg; |
---|
[51b7345] | 180 | } |
---|
| 181 | |
---|
| 182 | // CastExpr *CastExpr::clone() const { return 0; } |
---|
| 183 | |
---|
| 184 | void CastExpr::print( std::ostream &os, int indent ) const { |
---|
[44b5ca0] | 185 | os << std::string( indent, ' ' ) << "Cast of:" << std::endl; |
---|
[0dd3a2f] | 186 | arg->print(os, indent+2); |
---|
[44b5ca0] | 187 | os << std::endl << std::string( indent, ' ' ) << "to:" << std::endl; |
---|
[0dd3a2f] | 188 | if ( results.empty() ) { |
---|
[44b5ca0] | 189 | os << std::string( indent+2, ' ' ) << "nothing" << std::endl; |
---|
[0dd3a2f] | 190 | } else { |
---|
| 191 | printAll(results, os, indent+2); |
---|
| 192 | } // if |
---|
| 193 | Expression::print( os, indent ); |
---|
| 194 | } |
---|
| 195 | |
---|
[51b7345] | 196 | UntypedMemberExpr::UntypedMemberExpr( std::string _member, Expression *_aggregate, Expression *_aname ) : |
---|
[0dd3a2f] | 197 | Expression( _aname ), member(_member), aggregate(_aggregate) {} |
---|
[51b7345] | 198 | |
---|
[0dd3a2f] | 199 | UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) : |
---|
| 200 | Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) { |
---|
[51b7345] | 201 | } |
---|
| 202 | |
---|
| 203 | UntypedMemberExpr::~UntypedMemberExpr() { |
---|
[0dd3a2f] | 204 | delete aggregate; |
---|
[51b7345] | 205 | } |
---|
| 206 | |
---|
| 207 | void UntypedMemberExpr::print( std::ostream &os, int indent ) const { |
---|
[44b5ca0] | 208 | os << std::string( indent, ' ' ) << "Member Expression, with field: " << get_member(); |
---|
[51b7345] | 209 | |
---|
[0dd3a2f] | 210 | Expression *agg = get_aggregate(); |
---|
[44b5ca0] | 211 | os << std::string( indent, ' ' ) << "from aggregate: "; |
---|
[0dd3a2f] | 212 | if (agg != 0) agg->print(os, indent + 2); |
---|
| 213 | Expression::print( os, indent ); |
---|
[51b7345] | 214 | } |
---|
| 215 | |
---|
| 216 | |
---|
| 217 | MemberExpr::MemberExpr( DeclarationWithType *_member, Expression *_aggregate, Expression *_aname ) : |
---|
[0dd3a2f] | 218 | Expression( _aname ), member(_member), aggregate(_aggregate) { |
---|
| 219 | add_result( member->get_type()->clone() ); |
---|
| 220 | for ( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) { |
---|
| 221 | (*i)->set_isLvalue( true ); |
---|
| 222 | } // for |
---|
[51b7345] | 223 | } |
---|
| 224 | |
---|
[0dd3a2f] | 225 | MemberExpr::MemberExpr( const MemberExpr &other ) : |
---|
| 226 | Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) { |
---|
[51b7345] | 227 | } |
---|
| 228 | |
---|
| 229 | MemberExpr::~MemberExpr() { |
---|
[0dd3a2f] | 230 | delete member; |
---|
| 231 | delete aggregate; |
---|
[51b7345] | 232 | } |
---|
| 233 | |
---|
| 234 | void MemberExpr::print( std::ostream &os, int indent ) const { |
---|
[44b5ca0] | 235 | os << std::string( indent, ' ' ) << "Member Expression, with field: " << std::endl; |
---|
[51b7345] | 236 | |
---|
[0dd3a2f] | 237 | assert( member ); |
---|
[44b5ca0] | 238 | os << std::string( indent + 2, ' ' ); |
---|
[0dd3a2f] | 239 | member->print( os, indent + 2 ); |
---|
| 240 | os << std::endl; |
---|
[51b7345] | 241 | |
---|
[0dd3a2f] | 242 | Expression *agg = get_aggregate(); |
---|
[44b5ca0] | 243 | os << std::string( indent, ' ' ) << "from aggregate: " << std::endl; |
---|
[0dd3a2f] | 244 | if (agg != 0) agg->print(os, indent + 2); |
---|
| 245 | Expression::print( os, indent ); |
---|
[51b7345] | 246 | } |
---|
| 247 | |
---|
| 248 | |
---|
| 249 | UntypedExpr::UntypedExpr( Expression *_function, Expression *_aname ) : Expression( _aname ), function( _function ) {} |
---|
| 250 | |
---|
[0dd3a2f] | 251 | UntypedExpr::UntypedExpr( const UntypedExpr &other ) : |
---|
| 252 | Expression( other ), function( maybeClone( other.function ) ) { |
---|
| 253 | cloneAll( other.args, args ); |
---|
[51b7345] | 254 | } |
---|
| 255 | |
---|
| 256 | UntypedExpr::UntypedExpr( Expression *_function, std::list<Expression *> &_args, Expression *_aname ) : |
---|
[0dd3a2f] | 257 | Expression( _aname ), function(_function), args(_args) {} |
---|
[51b7345] | 258 | |
---|
| 259 | UntypedExpr::~UntypedExpr() {} |
---|
| 260 | |
---|
| 261 | void UntypedExpr::print( std::ostream &os, int indent ) const { |
---|
[44b5ca0] | 262 | os << std::string( indent, ' ' ) << "Applying untyped: " << std::endl; |
---|
[0dd3a2f] | 263 | function->print(os, indent + 4); |
---|
[44b5ca0] | 264 | os << std::string( indent, ' ' ) << "...to: " << std::endl; |
---|
[0dd3a2f] | 265 | printArgs(os, indent + 4); |
---|
| 266 | Expression::print( os, indent ); |
---|
[51b7345] | 267 | } |
---|
| 268 | |
---|
[0dd3a2f] | 269 | void UntypedExpr::printArgs( std::ostream &os, int indent ) const { |
---|
| 270 | std::list<Expression *>::const_iterator i; |
---|
| 271 | for (i = args.begin(); i != args.end(); i++) |
---|
| 272 | (*i)->print(os, indent); |
---|
[51b7345] | 273 | } |
---|
| 274 | |
---|
| 275 | NameExpr::NameExpr( std::string _name, Expression *_aname ) : Expression( _aname ), name(_name) {} |
---|
| 276 | |
---|
[0dd3a2f] | 277 | NameExpr::NameExpr( const NameExpr &other ) : Expression( other ), name( other.name ) { |
---|
[51b7345] | 278 | } |
---|
| 279 | |
---|
| 280 | NameExpr::~NameExpr() {} |
---|
| 281 | |
---|
| 282 | void NameExpr::print( std::ostream &os, int indent ) const { |
---|
[44b5ca0] | 283 | os << std::string( indent, ' ' ) << "Name: " << get_name() << std::endl; |
---|
[0dd3a2f] | 284 | Expression::print( os, indent ); |
---|
[51b7345] | 285 | } |
---|
| 286 | |
---|
| 287 | LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp, Expression *_aname ) : |
---|
[0dd3a2f] | 288 | Expression( _aname ), arg1(arg1_), arg2(arg2_), isAnd(andp) { |
---|
| 289 | add_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); |
---|
[51b7345] | 290 | } |
---|
| 291 | |
---|
[0dd3a2f] | 292 | LogicalExpr::LogicalExpr( const LogicalExpr &other ) : |
---|
| 293 | Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) { |
---|
[51b7345] | 294 | } |
---|
| 295 | |
---|
[a08ba92] | 296 | LogicalExpr::~LogicalExpr() { |
---|
[0dd3a2f] | 297 | delete arg1; |
---|
| 298 | delete arg2; |
---|
[51b7345] | 299 | } |
---|
| 300 | |
---|
| 301 | void LogicalExpr::print( std::ostream &os, int indent )const { |
---|
[44b5ca0] | 302 | os << std::string( indent, ' ' ) << "Short-circuited operation (" << (isAnd?"and":"or") << ") on: "; |
---|
[0dd3a2f] | 303 | arg1->print(os); |
---|
| 304 | os << " and "; |
---|
| 305 | arg2->print(os); |
---|
| 306 | os << std::endl; |
---|
| 307 | Expression::print( os, indent ); |
---|
[51b7345] | 308 | } |
---|
| 309 | |
---|
| 310 | ConditionalExpr::ConditionalExpr( Expression *arg1_, Expression *arg2_, Expression *arg3_, Expression *_aname ) : |
---|
[0dd3a2f] | 311 | Expression( _aname ), arg1(arg1_), arg2(arg2_), arg3(arg3_) {} |
---|
[51b7345] | 312 | |
---|
[0dd3a2f] | 313 | ConditionalExpr::ConditionalExpr( const ConditionalExpr &other ) : |
---|
| 314 | Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) { |
---|
| 315 | } |
---|
[51b7345] | 316 | |
---|
| 317 | ConditionalExpr::~ConditionalExpr() { |
---|
[0dd3a2f] | 318 | delete arg1; |
---|
| 319 | delete arg2; |
---|
| 320 | delete arg3; |
---|
[51b7345] | 321 | } |
---|
| 322 | |
---|
| 323 | void ConditionalExpr::print( std::ostream &os, int indent ) const { |
---|
[44b5ca0] | 324 | os << std::string( indent, ' ' ) << "Conditional expression on: " << std::endl; |
---|
[0dd3a2f] | 325 | arg1->print( os, indent+2 ); |
---|
[44b5ca0] | 326 | os << std::string( indent, ' ' ) << "First alternative:" << std::endl; |
---|
[0dd3a2f] | 327 | arg2->print( os, indent+2 ); |
---|
[44b5ca0] | 328 | os << std::string( indent, ' ' ) << "Second alternative:" << std::endl; |
---|
[0dd3a2f] | 329 | arg3->print( os, indent+2 ); |
---|
| 330 | os << std::endl; |
---|
| 331 | Expression::print( os, indent ); |
---|
| 332 | } |
---|
| 333 | |
---|
[7f5566b] | 334 | void AsmExpr::print( std::ostream &os, int indent ) const { |
---|
| 335 | os << "Asm Expression: " << std::endl; |
---|
| 336 | if ( inout ) inout->print( os, indent + 2 ); |
---|
| 337 | if ( constraint ) constraint->print( os, indent + 2 ); |
---|
| 338 | if ( operand ) operand->print( os, indent + 2 ); |
---|
| 339 | } |
---|
| 340 | |
---|
[0dd3a2f] | 341 | void UntypedValofExpr::print( std::ostream &os, int indent ) const { |
---|
[44b5ca0] | 342 | os << std::string( indent, ' ' ) << "Valof Expression: " << std::endl; |
---|
[0dd3a2f] | 343 | if ( get_body() != 0 ) |
---|
| 344 | get_body()->print( os, indent + 2 ); |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | // Local Variables: // |
---|
| 348 | // tab-width: 4 // |
---|
| 349 | // mode: c++ // |
---|
| 350 | // compile-command: "make install" // |
---|
| 351 | // End: // |
---|