#include #include #include #include #include #include "Type.h" #include "Expression.h" #include "Declaration.h" #include "Statement.h" #include "TypeSubstitution.h" #include "utility.h" //*** Expression Expression::Expression( Expression *_aname ) : env( 0 ), argName( _aname ) {} Expression::Expression( const Expression &other ) : env( maybeClone( other.env ) ) { cloneAll( other.results, results ); argName = other.get_argName(); } Expression::~Expression() { delete env; // delete argName; // xxx -- there's a problem in cloning ConstantExpr I still don't know how to fix deleteAll( results ); } void Expression::add_result( Type *t ) { if( TupleType *tuple = dynamic_cast< TupleType* >( t ) ) { std::copy( tuple->get_types().begin(), tuple->get_types().end(), back_inserter( results ) ); } else { results.push_back(t); } } void Expression::print(std::ostream &os, int indent) const { if( env ) { os << std::string(indent, ' ') << "with environment:" << std::endl; env->print( os, indent+2 ); } if( argName ) { os << std::string(indent, ' ') << "with designator:"; argName->print( os, indent+2 ); } } //*** ConstantExpr ConstantExpr::ConstantExpr(Constant _c, Expression *_aname ): Expression( _aname ), constant(_c) { add_result( constant.get_type()->clone() ); } ConstantExpr::ConstantExpr( const ConstantExpr &other) : Expression( other ), constant( other.constant ) {} ConstantExpr::~ConstantExpr() {} void ConstantExpr::print(std::ostream &os, int indent) const { os << std::string(indent, ' ') << "Constant Expression: " ; constant.print(os); os << std::endl; Expression::print( os, indent ); } //*** VariableExpr VariableExpr::VariableExpr( DeclarationWithType *_var, Expression *_aname ) : Expression( _aname ), var( _var ) { add_result( var->get_type()->clone() ); for( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) { (*i)->set_isLvalue( true ); } } VariableExpr::VariableExpr( const VariableExpr &other ) : Expression( other ), var( other.var ) { } VariableExpr::~VariableExpr() { // don't delete the declaration, since it points somewhere else in the tree } void VariableExpr::print(std::ostream &os, int indent) const { os << std::string(indent, ' ') << "Variable Expression: "; Declaration *decl = get_var(); // if( decl != 0) decl->print(os, indent + 2); if( decl != 0) decl->printShort(os, indent + 2); os << std::endl; Expression::print( os, indent ); } //*** SizeofExpr SizeofExpr::SizeofExpr( Expression *expr_, Expression *_aname ) : Expression( _aname ), expr(expr_), type(0), isType(false) { add_result( new BasicType( Type::Qualifiers(), BasicType::UnsignedInt ) ); } SizeofExpr::SizeofExpr( Type *type_, Expression *_aname ) : Expression( _aname ), expr(0), type(type_), isType(true) { add_result( new BasicType( Type::Qualifiers(), BasicType::UnsignedInt ) ); } SizeofExpr::SizeofExpr( const SizeofExpr &other ) : Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) { } SizeofExpr::~SizeofExpr(){ delete expr; delete type; } void SizeofExpr::print( std::ostream &os, int indent) const { os << std::string(indent, ' ') << "Sizeof Expression on: "; if(isType) type->print(os, indent + 2); else expr->print(os, indent + 2); os << std::endl; Expression::print( os, indent ); } //*** AttrExpr AttrExpr::AttrExpr( Expression *attr, Expression *expr_, Expression *_aname ) : Expression( _aname ), attr( attr ), expr(expr_), type(0), isType(false) { } AttrExpr::AttrExpr( Expression *attr, Type *type_, Expression *_aname ) : Expression( _aname ), attr( attr ), expr(0), type(type_), isType(true) { } AttrExpr::AttrExpr( const AttrExpr &other ) : Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) { } AttrExpr::~AttrExpr(){ delete attr; delete expr; delete type; } void AttrExpr::print( std::ostream &os, int indent) const { os << std::string(indent, ' ') << "Attr "; attr->print( os, indent + 2 ); if( isType || expr ) { os << "applied to: "; if(isType) type->print(os, indent + 2); else expr->print(os, indent + 2); } os << std::endl; Expression::print( os, indent ); } //*** CastExpr CastExpr::CastExpr( Expression *arg_, Type *toType, Expression *_aname ) : Expression( _aname ), arg(arg_) { add_result(toType); } CastExpr::CastExpr( Expression *arg_, Expression *_aname ) : Expression( _aname ), arg(arg_) { } CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) { } CastExpr::~CastExpr() { delete arg; } // CastExpr *CastExpr::clone() const { return 0; } void CastExpr::print( std::ostream &os, int indent ) const { os << std::string(indent, ' ') << "Cast of:" << std::endl; arg->print(os, indent+2); os << std::endl << std::string(indent, ' ') << "to:" << std::endl; if( results.empty() ) { os << std::string(indent+2, ' ') << "nothing" << std::endl; } else { printAll(results, os, indent+2); } Expression::print( os, indent ); } //*** UntypedMemberExpr UntypedMemberExpr::UntypedMemberExpr( std::string _member, Expression *_aggregate, Expression *_aname ) : Expression( _aname ), member(_member), aggregate(_aggregate) {} UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) : Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) { } UntypedMemberExpr::~UntypedMemberExpr() { delete aggregate; } void UntypedMemberExpr::print( std::ostream &os, int indent ) const { os << std::string(indent, ' ') << "Member Expression, with field: " << get_member(); Expression *agg = get_aggregate(); os << std::string(indent, ' ') << "from aggregate: "; if (agg != 0) agg->print(os, indent + 2); Expression::print( os, indent ); } //*** MemberExpr MemberExpr::MemberExpr( DeclarationWithType *_member, Expression *_aggregate, Expression *_aname ) : Expression( _aname ), member(_member), aggregate(_aggregate) { add_result( member->get_type()->clone() ); for( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) { (*i)->set_isLvalue( true ); } } MemberExpr::MemberExpr( const MemberExpr &other ) : Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) { } MemberExpr::~MemberExpr() { delete member; delete aggregate; } void MemberExpr::print( std::ostream &os, int indent ) const { os << std::string(indent, ' ') << "Member Expression, with field: " << std::endl; assert( member ); os << std::string(indent + 2, ' '); member->print( os, indent + 2 ); os << std::endl; Expression *agg = get_aggregate(); os << std::string(indent, ' ') << "from aggregate: " << std::endl; if (agg != 0) agg->print(os, indent + 2); Expression::print( os, indent ); } //*** UntypedExpr UntypedExpr::UntypedExpr( Expression *_function, Expression *_aname ) : Expression( _aname ), function( _function ) {} UntypedExpr::UntypedExpr( const UntypedExpr &other ) : Expression( other ), function( maybeClone( other.function ) ) { cloneAll( other.args, args ); } UntypedExpr::UntypedExpr( Expression *_function, std::list &_args, Expression *_aname ) : Expression( _aname ), function(_function), args(_args) {} UntypedExpr::~UntypedExpr() {} void UntypedExpr::print( std::ostream &os, int indent ) const { os << std::string(indent, ' ') << "Applying untyped: " << std::endl; function->print(os, indent + 4); os << "\r" << std::string(indent, ' ') << "...to: " << std::endl; printArgs(os, indent + 4); Expression::print( os, indent ); } void UntypedExpr::printArgs(std::ostream &os, int indent ) const { std::list::const_iterator i; for(i = args.begin(); i != args.end(); i++) (*i)->print(os, indent); } //*** NameExpr NameExpr::NameExpr( std::string _name, Expression *_aname ) : Expression( _aname ), name(_name) {} NameExpr::NameExpr( const NameExpr &other ) : Expression( other ), name( other.name ) { } NameExpr::~NameExpr() {} void NameExpr::print( std::ostream &os, int indent ) const { os << std::string(indent, ' ') << "Name: " << get_name() << std::endl; Expression::print( os, indent ); } //*** LogicalExpr LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp, Expression *_aname ) : Expression( _aname ), arg1(arg1_), arg2(arg2_), isAnd(andp) { add_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); } LogicalExpr::LogicalExpr( const LogicalExpr &other ) : Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) { } LogicalExpr::~LogicalExpr(){ delete arg1; delete arg2; } void LogicalExpr::print( std::ostream &os, int indent )const { os << std::string(indent, ' ') << "Short-circuited operation (" << (isAnd?"and":"or") << ") on: "; arg1->print(os); os << " and "; arg2->print(os); os << std::endl; Expression::print( os, indent ); } //*** ConditionalExpr ConditionalExpr::ConditionalExpr( Expression *arg1_, Expression *arg2_, Expression *arg3_, Expression *_aname ) : Expression( _aname ), arg1(arg1_), arg2(arg2_), arg3(arg3_) {} ConditionalExpr::ConditionalExpr( const ConditionalExpr &other ) : Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) { } ConditionalExpr::~ConditionalExpr() { delete arg1; delete arg2; delete arg3; } void ConditionalExpr::print( std::ostream &os, int indent ) const { os << std::string(indent, ' ') << "Conditional expression on: " << std::endl; arg1->print( os, indent+2 ); os << std::string(indent, ' ') << "First alternative:" << std::endl; arg2->print( os, indent+2 ); os << std::string(indent, ' ') << "Second alternative:" << std::endl; arg3->print( os, indent+2 ); os << std::endl; Expression::print( os, indent ); } //*** UntypedValofExpr void UntypedValofExpr::print( std::ostream &os, int indent ) const { os << std::string(indent, ' ') << "Valof Expression: " << std::endl; if( get_body() != 0 ) get_body()->print( os, indent + 2 ); }