| 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 |
|
|---|
| 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 ) {}
|
|---|
| 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
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | void Expression::print(std::ostream &os, int indent) const {
|
|---|
| 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 | }
|
|---|
| 70 |
|
|---|
| 71 | ConstantExpr::~ConstantExpr() {}
|
|---|
| 72 |
|
|---|
| 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 |
|
|---|
| 104 | SizeofExpr::SizeofExpr( Expression *expr_, Expression *_aname ) :
|
|---|
| 105 | Expression( _aname ), expr(expr_), type(0), isType(false) {
|
|---|
| 106 | add_result( new BasicType( Type::Qualifiers(), BasicType::UnsignedInt ) );
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | SizeofExpr::SizeofExpr( Type *type_, Expression *_aname ) :
|
|---|
| 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;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | void SizeofExpr::print( std::ostream &os, int indent) const {
|
|---|
| 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 |
|
|---|
| 135 | AttrExpr::AttrExpr( Expression *attr, Expression *expr_, Expression *_aname ) :
|
|---|
| 136 | Expression( _aname ), attr( attr ), expr(expr_), type(0), isType(false) {
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | AttrExpr::AttrExpr( Expression *attr, Type *type_, Expression *_aname ) :
|
|---|
| 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;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | void AttrExpr::print( std::ostream &os, int indent) const {
|
|---|
| 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);
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | CastExpr::CastExpr( Expression *arg_, Expression *_aname ) : Expression( _aname ), arg(arg_) {
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | CastExpr::~CastExpr() {
|
|---|
| 180 | delete arg;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | // CastExpr *CastExpr::clone() const { return 0; }
|
|---|
| 184 |
|
|---|
| 185 | void CastExpr::print( std::ostream &os, int indent ) const {
|
|---|
| 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 |
|
|---|
| 197 | UntypedMemberExpr::UntypedMemberExpr( std::string _member, Expression *_aggregate, Expression *_aname ) :
|
|---|
| 198 | Expression( _aname ), member(_member), aggregate(_aggregate) {}
|
|---|
| 199 |
|
|---|
| 200 | UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) :
|
|---|
| 201 | Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) {
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | UntypedMemberExpr::~UntypedMemberExpr() {
|
|---|
| 205 | delete aggregate;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | void UntypedMemberExpr::print( std::ostream &os, int indent ) const {
|
|---|
| 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 |
|
|---|
| 218 | MemberExpr::MemberExpr( DeclarationWithType *_member, Expression *_aggregate, Expression *_aname ) :
|
|---|
| 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 ) ) {
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | MemberExpr::~MemberExpr() {
|
|---|
| 231 | delete member;
|
|---|
| 232 | delete aggregate;
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | void MemberExpr::print( std::ostream &os, int indent ) const {
|
|---|
| 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 |
|
|---|
| 250 | UntypedExpr::UntypedExpr( Expression *_function, Expression *_aname ) : Expression( _aname ), function( _function ) {}
|
|---|
| 251 |
|
|---|
| 252 | UntypedExpr::UntypedExpr( const UntypedExpr &other ) :
|
|---|
| 253 | Expression( other ), function( maybeClone( other.function ) ) {
|
|---|
| 254 | cloneAll( other.args, args );
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | UntypedExpr::UntypedExpr( Expression *_function, std::list<Expression *> &_args, Expression *_aname ) :
|
|---|
| 258 | Expression( _aname ), function(_function), args(_args) {}
|
|---|
| 259 |
|
|---|
| 260 | UntypedExpr::~UntypedExpr() {}
|
|---|
| 261 |
|
|---|
| 262 | void UntypedExpr::print( std::ostream &os, int indent ) const {
|
|---|
| 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 |
|
|---|
| 276 | NameExpr::NameExpr( std::string _name, Expression *_aname ) : Expression( _aname ), name(_name) {}
|
|---|
| 277 |
|
|---|
| 278 | NameExpr::NameExpr( const NameExpr &other ) : Expression( other ), name( other.name ) {
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | NameExpr::~NameExpr() {}
|
|---|
| 282 |
|
|---|
| 283 | void NameExpr::print( std::ostream &os, int indent ) const {
|
|---|
| 284 | os << std::string(indent, ' ') << "Name: " << get_name() << std::endl;
|
|---|
| 285 | Expression::print( os, indent );
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp, Expression *_aname ) :
|
|---|
| 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 ) {
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | LogicalExpr::~LogicalExpr() {
|
|---|
| 298 | delete arg1;
|
|---|
| 299 | delete arg2;
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | void LogicalExpr::print( std::ostream &os, int indent )const {
|
|---|
| 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 |
|
|---|
| 311 | ConditionalExpr::ConditionalExpr( Expression *arg1_, Expression *arg2_, Expression *arg3_, Expression *_aname ) :
|
|---|
| 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 | }
|
|---|
| 317 |
|
|---|
| 318 | ConditionalExpr::~ConditionalExpr() {
|
|---|
| 319 | delete arg1;
|
|---|
| 320 | delete arg2;
|
|---|
| 321 | delete arg3;
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | void ConditionalExpr::print( std::ostream &os, int indent ) const {
|
|---|
| 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: //
|
|---|