// This file is part of the AspectC++ compiler 'ac++'.
// Copyright (C) 1999-2003  The 'ac++' developers (see aspectc.org)
//                                                                
// This program is free software;  you can redistribute it and/or 
// modify it under the terms of the GNU General Public License as 
// published by the Free Software Foundation; either version 2 of 
// the License, or (at your option) any later version.            
//                                                                
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
// GNU General Public License for more details.                   
//                                                                
// You should have received a copy of the GNU General Public      
// License along with this program; if not, write to the Free     
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
// MA  02111-1307  USA                                            
//von Jaydon Novan Freed

#ifndef __GenIfConditionCheck_h__
#define __GenIfConditionCheck_h__

#include "Condition.h"
#include "Naming.h"
#include "AdviceInfo.h"

class HasIfConditionVisitor : public Condition::Visitor {
  bool has_if_;
public:
  HasIfConditionVisitor() : has_if_(false) {}
  bool result() const { return has_if_; }

  virtual void visit(const Condition &obj) {
    if (obj) obj.accept(*this);
  }
  virtual void visit(const Condition::That &obj) {}
  virtual void visit(const Condition::Target &obj) {}
  virtual void visit(const Condition::CFlow &obj) {}
  virtual void visit(const Condition::NeededShortCircuitArg &obj) {}

  virtual void visit(const Condition::IfExpr &obj) {
    has_if_ = true;
  }
  virtual void visit(const Condition::And &obj) {
    obj.left().accept(*this);
    obj.right().accept(*this);
  }
  virtual void visit(const Condition::Or &obj) {
    obj.left().accept(*this);
    obj.right().accept(*this);
  }
  virtual void visit(const Condition::Not &obj) {
    obj.arg().accept(*this);
  }
};


class GenIfConditionCheck : public Condition::Visitor {
  std::ostream &out_;
  
  bool has_if(const Condition::Base &cond) {
    HasIfConditionVisitor v;
    cond.accept(v);
    return v.result();
  }
  
  bool has_if(const Condition &cond) {
    HasIfConditionVisitor v;
    cond.accept(v);
    return v.result();
  }
  
  
public:
  GenIfConditionCheck(ostream &out) : out_(out){}

  virtual void visit(const Condition &obj) {
    if (obj && has_if(obj))
      obj.accept(*this);
    else
      out_ << "true";
  }

  virtual void visit(const Condition::That &obj) {}
  virtual void visit(const Condition::Target &obj) {}
  virtual void visit(const Condition::CFlow &obj) {}
  virtual void visit(const Condition::NeededShortCircuitArg &obj) {}
  
  virtual void visit(const Condition::IfExpr &obj) {//neu
    out_ << obj.expr();
  }

  virtual void visit(const Condition::And &obj) {
    bool left_has  = has_if(obj.left());
    bool right_has = has_if(obj.right());

    if (left_has && right_has) {
      out_ << "(";
      obj.left().accept(*this);
      out_ << " && ";
      obj.right().accept(*this);
      out_ << ")";
    } else if (left_has) {
      obj.left().accept(*this);
    } else if (right_has) {
      obj.right().accept(*this);
    } else {
      out_ << "true";
    }
  }

  virtual void visit(const Condition::Or &obj) {
    bool left_has  = has_if(obj.left());
    bool right_has = has_if(obj.right());

    if (left_has && right_has) {
      out_ << "(";
      obj.left().accept(*this);
      out_ << " || ";
      obj.right().accept(*this);
      out_ << ")";
    } else if (left_has) {
      obj.left().accept(*this);
    } else if (right_has) {
      obj.right().accept(*this);
    } else {
      out_ << "true";
    }
  }

  virtual void visit(const Condition::Not &obj) {
    if (has_if(obj.arg())) {
      out_ << "!";
      obj.arg().accept(*this);
    } else {
      out_ << "true";
    }
  }
};

#endif
