| 1 | #include <iostream>
|
|---|
| 2 | #include <cassert>
|
|---|
| 3 | #include <list>
|
|---|
| 4 | #include <algorithm>
|
|---|
| 5 |
|
|---|
| 6 | #include <iterator>
|
|---|
| 7 |
|
|---|
| 8 | #include "Type.h"
|
|---|
| 9 | #include "Expression.h"
|
|---|
| 10 | #include "Declaration.h"
|
|---|
| 11 | #include "Statement.h"
|
|---|
| 12 | #include "TypeSubstitution.h"
|
|---|
| 13 | #include "utility.h"
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 | //*** Expression
|
|---|
| 17 | 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 | }
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | 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 | {}
|
|---|
| 63 |
|
|---|
| 64 | ConstantExpr::~ConstantExpr() {}
|
|---|
| 65 |
|
|---|
| 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
|
|---|
| 103 | 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 ) );
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 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;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | 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
|
|---|
| 138 | AttrExpr::AttrExpr( Expression *attr, Expression *expr_, Expression *_aname ) :
|
|---|
| 139 | Expression( _aname ), attr( attr ), expr(expr_), type(0), isType(false)
|
|---|
| 140 | {
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | 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;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | 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);
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | CastExpr::CastExpr( Expression *arg_, Expression *_aname ) : Expression( _aname ), arg(arg_) {
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | CastExpr::CastExpr( const CastExpr &other )
|
|---|
| 185 | : Expression( other ), arg( maybeClone( other.arg ) )
|
|---|
| 186 | {
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | CastExpr::~CastExpr() {
|
|---|
| 190 | delete arg;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | // CastExpr *CastExpr::clone() const { return 0; }
|
|---|
| 194 |
|
|---|
| 195 | 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
|
|---|
| 208 | 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 | {
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | UntypedMemberExpr::~UntypedMemberExpr() {
|
|---|
| 217 | delete aggregate;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | 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
|
|---|
| 231 | 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 | {
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | MemberExpr::~MemberExpr() {
|
|---|
| 246 | delete member;
|
|---|
| 247 | delete aggregate;
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | 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
|
|---|
| 266 | UntypedExpr::UntypedExpr( Expression *_function, Expression *_aname ) : Expression( _aname ), function( _function ) {}
|
|---|
| 267 |
|
|---|
| 268 | UntypedExpr::UntypedExpr( const UntypedExpr &other )
|
|---|
| 269 | : Expression( other ), function( maybeClone( other.function ) )
|
|---|
| 270 | {
|
|---|
| 271 | cloneAll( other.args, args );
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | UntypedExpr::UntypedExpr( Expression *_function, std::list<Expression *> &_args, Expression *_aname ) :
|
|---|
| 275 | Expression( _aname ), function(_function), args(_args) {}
|
|---|
| 276 |
|
|---|
| 277 | UntypedExpr::~UntypedExpr() {}
|
|---|
| 278 |
|
|---|
| 279 | 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
|
|---|
| 294 | NameExpr::NameExpr( std::string _name, Expression *_aname ) : Expression( _aname ), name(_name) {}
|
|---|
| 295 |
|
|---|
| 296 | NameExpr::NameExpr( const NameExpr &other )
|
|---|
| 297 | : Expression( other ), name( other.name )
|
|---|
| 298 | {
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | NameExpr::~NameExpr() {}
|
|---|
| 302 |
|
|---|
| 303 | 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
|
|---|
| 309 | 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 | {
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | LogicalExpr::~LogicalExpr(){
|
|---|
| 321 | delete arg1;
|
|---|
| 322 | delete arg2;
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | 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
|
|---|
| 335 | 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 | }
|
|---|
| 342 |
|
|---|
| 343 | ConditionalExpr::~ConditionalExpr() {
|
|---|
| 344 | delete arg1;
|
|---|
| 345 | delete arg2;
|
|---|
| 346 | delete arg3;
|
|---|
| 347 | }
|
|---|
| 348 |
|
|---|
| 349 | 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 |
|
|---|