| [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 | 
|---|
|  | 12 | // Last Modified On : Mon May 18 08:27:07 2015 | 
|---|
|  | 13 | // Update Count     : 2 | 
|---|
|  | 14 | // | 
|---|
|  | 15 |  | 
|---|
| [51b73452] | 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; | 
|---|
|  | 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 | 
|---|
| [51b73452] | 50 | } | 
|---|
|  | 51 |  | 
|---|
|  | 52 | void Expression::print(std::ostream &os, int indent) const { | 
|---|
| [0dd3a2f] | 53 | if ( env ) { | 
|---|
|  | 54 | os << std::string(indent, ' ') << "with environment:" << std::endl; | 
|---|
|  | 55 | env->print( os, indent+2 ); | 
|---|
|  | 56 | } // if | 
|---|
| [51b73452] | 57 |  | 
|---|
| [0dd3a2f] | 58 | if ( argName ) { | 
|---|
|  | 59 | os << std::string(indent, ' ') << "with designator:"; | 
|---|
|  | 60 | argName->print( os, indent+2 ); | 
|---|
|  | 61 | } // if | 
|---|
| [51b73452] | 62 | } | 
|---|
|  | 63 |  | 
|---|
| [0dd3a2f] | 64 | ConstantExpr::ConstantExpr( Constant _c, Expression *_aname ) : Expression( _aname ), constant( _c ) { | 
|---|
|  | 65 | add_result( constant.get_type()->clone() ); | 
|---|
| [51b73452] | 66 | } | 
|---|
|  | 67 |  | 
|---|
| [0dd3a2f] | 68 | ConstantExpr::ConstantExpr( const ConstantExpr &other) : Expression( other ), constant( other.constant ) { | 
|---|
|  | 69 | } | 
|---|
| [51b73452] | 70 |  | 
|---|
|  | 71 | ConstantExpr::~ConstantExpr() {} | 
|---|
|  | 72 |  | 
|---|
| [0dd3a2f] | 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 ); | 
|---|
| [51b73452] | 78 | } | 
|---|
|  | 79 |  | 
|---|
| [0dd3a2f] | 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 | 
|---|
| [51b73452] | 85 | } | 
|---|
|  | 86 |  | 
|---|
| [0dd3a2f] | 87 | VariableExpr::VariableExpr( const VariableExpr &other ) : Expression( other ), var( other.var ) { | 
|---|
| [51b73452] | 88 | } | 
|---|
|  | 89 |  | 
|---|
| [0dd3a2f] | 90 | VariableExpr::~VariableExpr() { | 
|---|
|  | 91 | // don't delete the declaration, since it points somewhere else in the tree | 
|---|
| [51b73452] | 92 | } | 
|---|
|  | 93 |  | 
|---|
| [0dd3a2f] | 94 | void VariableExpr::print( std::ostream &os, int indent ) const { | 
|---|
|  | 95 | os << std::string(indent, ' ') << "Variable Expression: "; | 
|---|
| [51b73452] | 96 |  | 
|---|
| [0dd3a2f] | 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 ); | 
|---|
| [51b73452] | 102 | } | 
|---|
|  | 103 |  | 
|---|
|  | 104 | SizeofExpr::SizeofExpr( Expression *expr_, Expression *_aname ) : | 
|---|
| [0dd3a2f] | 105 | Expression( _aname ), expr(expr_), type(0), isType(false) { | 
|---|
|  | 106 | add_result( new BasicType( Type::Qualifiers(), BasicType::UnsignedInt ) ); | 
|---|
| [51b73452] | 107 | } | 
|---|
|  | 108 |  | 
|---|
|  | 109 | SizeofExpr::SizeofExpr( Type *type_, Expression *_aname ) : | 
|---|
| [0dd3a2f] | 110 | Expression( _aname ), expr(0), type(type_), isType(true) { | 
|---|
|  | 111 | add_result( new BasicType( Type::Qualifiers(), BasicType::UnsignedInt ) ); | 
|---|
| [51b73452] | 112 | } | 
|---|
|  | 113 |  | 
|---|
| [0dd3a2f] | 114 | SizeofExpr::SizeofExpr( const SizeofExpr &other ) : | 
|---|
|  | 115 | Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) { | 
|---|
| [51b73452] | 116 | } | 
|---|
|  | 117 |  | 
|---|
| [0dd3a2f] | 118 | SizeofExpr::~SizeofExpr() { | 
|---|
|  | 119 | delete expr; | 
|---|
|  | 120 | delete type; | 
|---|
| [51b73452] | 121 | } | 
|---|
|  | 122 |  | 
|---|
|  | 123 | void SizeofExpr::print( std::ostream &os, int indent) const { | 
|---|
| [0dd3a2f] | 124 | os << std::string(indent, ' ') << "Sizeof Expression on: "; | 
|---|
| [51b73452] | 125 |  | 
|---|
| [0dd3a2f] | 126 | if (isType) | 
|---|
|  | 127 | type->print(os, indent + 2); | 
|---|
|  | 128 | else | 
|---|
|  | 129 | expr->print(os, indent + 2); | 
|---|
| [51b73452] | 130 |  | 
|---|
| [0dd3a2f] | 131 | os << std::endl; | 
|---|
|  | 132 | Expression::print( os, indent ); | 
|---|
| [51b73452] | 133 | } | 
|---|
|  | 134 |  | 
|---|
|  | 135 | AttrExpr::AttrExpr( Expression *attr, Expression *expr_, Expression *_aname ) : | 
|---|
| [0dd3a2f] | 136 | Expression( _aname ), attr( attr ), expr(expr_), type(0), isType(false) { | 
|---|
| [51b73452] | 137 | } | 
|---|
|  | 138 |  | 
|---|
|  | 139 | AttrExpr::AttrExpr( Expression *attr, Type *type_, Expression *_aname ) : | 
|---|
| [0dd3a2f] | 140 | Expression( _aname ), attr( attr ), expr(0), type(type_), isType(true) { | 
|---|
| [51b73452] | 141 | } | 
|---|
|  | 142 |  | 
|---|
| [0dd3a2f] | 143 | AttrExpr::AttrExpr( const AttrExpr &other ) : | 
|---|
|  | 144 | Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) { | 
|---|
| [51b73452] | 145 | } | 
|---|
|  | 146 |  | 
|---|
| [0dd3a2f] | 147 | AttrExpr::~AttrExpr() { | 
|---|
|  | 148 | delete attr; | 
|---|
|  | 149 | delete expr; | 
|---|
|  | 150 | delete type; | 
|---|
| [51b73452] | 151 | } | 
|---|
|  | 152 |  | 
|---|
|  | 153 | void AttrExpr::print( std::ostream &os, int indent) const { | 
|---|
| [0dd3a2f] | 154 | os << std::string(indent, ' ') << "Attr "; | 
|---|
|  | 155 | attr->print( os, indent + 2 ); | 
|---|
|  | 156 | if ( isType || expr ) { | 
|---|
|  | 157 | os << "applied to: "; | 
|---|
| [51b73452] | 158 |  | 
|---|
| [0dd3a2f] | 159 | if (isType) | 
|---|
|  | 160 | type->print(os, indent + 2); | 
|---|
|  | 161 | else | 
|---|
|  | 162 | expr->print(os, indent + 2); | 
|---|
|  | 163 | } // if | 
|---|
| [51b73452] | 164 |  | 
|---|
| [0dd3a2f] | 165 | os << std::endl; | 
|---|
|  | 166 | Expression::print( os, indent ); | 
|---|
| [51b73452] | 167 | } | 
|---|
|  | 168 |  | 
|---|
| [0dd3a2f] | 169 | CastExpr::CastExpr( Expression *arg_, Type *toType, Expression *_aname ) : Expression( _aname ), arg(arg_) { | 
|---|
|  | 170 | add_result(toType); | 
|---|
| [51b73452] | 171 | } | 
|---|
|  | 172 |  | 
|---|
|  | 173 | CastExpr::CastExpr( Expression *arg_, Expression *_aname ) : Expression( _aname ), arg(arg_) { | 
|---|
|  | 174 | } | 
|---|
|  | 175 |  | 
|---|
| [0dd3a2f] | 176 | CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) { | 
|---|
| [51b73452] | 177 | } | 
|---|
|  | 178 |  | 
|---|
|  | 179 | CastExpr::~CastExpr() { | 
|---|
| [0dd3a2f] | 180 | delete arg; | 
|---|
| [51b73452] | 181 | } | 
|---|
|  | 182 |  | 
|---|
|  | 183 | // CastExpr *CastExpr::clone() const { return 0; } | 
|---|
|  | 184 |  | 
|---|
|  | 185 | void CastExpr::print( std::ostream &os, int indent ) const { | 
|---|
| [0dd3a2f] | 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 |  | 
|---|
| [51b73452] | 197 | UntypedMemberExpr::UntypedMemberExpr( std::string _member, Expression *_aggregate, Expression *_aname ) : | 
|---|
| [0dd3a2f] | 198 | Expression( _aname ), member(_member), aggregate(_aggregate) {} | 
|---|
| [51b73452] | 199 |  | 
|---|
| [0dd3a2f] | 200 | UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) : | 
|---|
|  | 201 | Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) { | 
|---|
| [51b73452] | 202 | } | 
|---|
|  | 203 |  | 
|---|
|  | 204 | UntypedMemberExpr::~UntypedMemberExpr() { | 
|---|
| [0dd3a2f] | 205 | delete aggregate; | 
|---|
| [51b73452] | 206 | } | 
|---|
|  | 207 |  | 
|---|
|  | 208 | void UntypedMemberExpr::print( std::ostream &os, int indent ) const { | 
|---|
| [0dd3a2f] | 209 | os << std::string(indent, ' ') << "Member Expression, with field: " << get_member(); | 
|---|
| [51b73452] | 210 |  | 
|---|
| [0dd3a2f] | 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 ); | 
|---|
| [51b73452] | 215 | } | 
|---|
|  | 216 |  | 
|---|
|  | 217 |  | 
|---|
|  | 218 | MemberExpr::MemberExpr( DeclarationWithType *_member, Expression *_aggregate, Expression *_aname ) : | 
|---|
| [0dd3a2f] | 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 | 
|---|
| [51b73452] | 224 | } | 
|---|
|  | 225 |  | 
|---|
| [0dd3a2f] | 226 | MemberExpr::MemberExpr( const MemberExpr &other ) : | 
|---|
|  | 227 | Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) { | 
|---|
| [51b73452] | 228 | } | 
|---|
|  | 229 |  | 
|---|
|  | 230 | MemberExpr::~MemberExpr() { | 
|---|
| [0dd3a2f] | 231 | delete member; | 
|---|
|  | 232 | delete aggregate; | 
|---|
| [51b73452] | 233 | } | 
|---|
|  | 234 |  | 
|---|
|  | 235 | void MemberExpr::print( std::ostream &os, int indent ) const { | 
|---|
| [0dd3a2f] | 236 | os << std::string(indent, ' ') << "Member Expression, with field: " << std::endl; | 
|---|
| [51b73452] | 237 |  | 
|---|
| [0dd3a2f] | 238 | assert( member ); | 
|---|
|  | 239 | os << std::string(indent + 2, ' '); | 
|---|
|  | 240 | member->print( os, indent + 2 ); | 
|---|
|  | 241 | os << std::endl; | 
|---|
| [51b73452] | 242 |  | 
|---|
| [0dd3a2f] | 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 ); | 
|---|
| [51b73452] | 247 | } | 
|---|
|  | 248 |  | 
|---|
|  | 249 |  | 
|---|
|  | 250 | UntypedExpr::UntypedExpr( Expression *_function, Expression *_aname ) : Expression( _aname ), function( _function ) {} | 
|---|
|  | 251 |  | 
|---|
| [0dd3a2f] | 252 | UntypedExpr::UntypedExpr( const UntypedExpr &other ) : | 
|---|
|  | 253 | Expression( other ), function( maybeClone( other.function ) ) { | 
|---|
|  | 254 | cloneAll( other.args, args ); | 
|---|
| [51b73452] | 255 | } | 
|---|
|  | 256 |  | 
|---|
|  | 257 | UntypedExpr::UntypedExpr( Expression *_function, std::list<Expression *> &_args, Expression *_aname ) : | 
|---|
| [0dd3a2f] | 258 | Expression( _aname ), function(_function), args(_args) {} | 
|---|
| [51b73452] | 259 |  | 
|---|
|  | 260 | UntypedExpr::~UntypedExpr() {} | 
|---|
|  | 261 |  | 
|---|
|  | 262 | void UntypedExpr::print( std::ostream &os, int indent ) const { | 
|---|
| [0dd3a2f] | 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 ); | 
|---|
| [51b73452] | 268 | } | 
|---|
|  | 269 |  | 
|---|
| [0dd3a2f] | 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); | 
|---|
| [51b73452] | 274 | } | 
|---|
|  | 275 |  | 
|---|
|  | 276 | NameExpr::NameExpr( std::string _name, Expression *_aname ) : Expression( _aname ), name(_name) {} | 
|---|
|  | 277 |  | 
|---|
| [0dd3a2f] | 278 | NameExpr::NameExpr( const NameExpr &other ) : Expression( other ), name( other.name ) { | 
|---|
| [51b73452] | 279 | } | 
|---|
|  | 280 |  | 
|---|
|  | 281 | NameExpr::~NameExpr() {} | 
|---|
|  | 282 |  | 
|---|
|  | 283 | void NameExpr::print( std::ostream &os, int indent ) const { | 
|---|
| [0dd3a2f] | 284 | os << std::string(indent, ' ') << "Name: " << get_name() << std::endl; | 
|---|
|  | 285 | Expression::print( os, indent ); | 
|---|
| [51b73452] | 286 | } | 
|---|
|  | 287 |  | 
|---|
|  | 288 | LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp, Expression *_aname ) : | 
|---|
| [0dd3a2f] | 289 | Expression( _aname ), arg1(arg1_), arg2(arg2_), isAnd(andp) { | 
|---|
|  | 290 | add_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); | 
|---|
| [51b73452] | 291 | } | 
|---|
|  | 292 |  | 
|---|
| [0dd3a2f] | 293 | LogicalExpr::LogicalExpr( const LogicalExpr &other ) : | 
|---|
|  | 294 | Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) { | 
|---|
| [51b73452] | 295 | } | 
|---|
|  | 296 |  | 
|---|
| [a08ba92] | 297 | LogicalExpr::~LogicalExpr() { | 
|---|
| [0dd3a2f] | 298 | delete arg1; | 
|---|
|  | 299 | delete arg2; | 
|---|
| [51b73452] | 300 | } | 
|---|
|  | 301 |  | 
|---|
|  | 302 | void LogicalExpr::print( std::ostream &os, int indent )const { | 
|---|
| [0dd3a2f] | 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 ); | 
|---|
| [51b73452] | 309 | } | 
|---|
|  | 310 |  | 
|---|
|  | 311 | ConditionalExpr::ConditionalExpr( Expression *arg1_, Expression *arg2_, Expression *arg3_, Expression *_aname ) : | 
|---|
| [0dd3a2f] | 312 | Expression( _aname ), arg1(arg1_), arg2(arg2_), arg3(arg3_) {} | 
|---|
| [51b73452] | 313 |  | 
|---|
| [0dd3a2f] | 314 | ConditionalExpr::ConditionalExpr( const ConditionalExpr &other ) : | 
|---|
|  | 315 | Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) { | 
|---|
|  | 316 | } | 
|---|
| [51b73452] | 317 |  | 
|---|
|  | 318 | ConditionalExpr::~ConditionalExpr() { | 
|---|
| [0dd3a2f] | 319 | delete arg1; | 
|---|
|  | 320 | delete arg2; | 
|---|
|  | 321 | delete arg3; | 
|---|
| [51b73452] | 322 | } | 
|---|
|  | 323 |  | 
|---|
|  | 324 | void ConditionalExpr::print( std::ostream &os, int indent ) const { | 
|---|
| [0dd3a2f] | 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: // | 
|---|