expr_value.cpp

Go to the documentation of this file.
00001 /*****************************************************************************/
00002 /*!
00003  * \file expr_value.cpp
00004  * 
00005  * Author: Sergey Berezin
00006  * 
00007  * Created: Fri Feb  7 22:29:04 2003
00008  *
00009  * <hr>
00010  * Copyright (C) 2003 by the Board of Trustees of Leland Stanford
00011  * Junior University and by New York University. 
00012  *
00013  * License to use, copy, modify, sell and/or distribute this software
00014  * and its documentation for any purpose is hereby granted without
00015  * royalty, subject to the terms and conditions defined in the \ref
00016  * LICENSE file provided with this distribution.  In particular:
00017  *
00018  * - The above copyright notice and this permission notice must appear
00019  * in all copies of the software and related documentation.
00020  *
00021  * - THE SOFTWARE IS PROVIDED "AS-IS", WITHOUT ANY WARRANTIES,
00022  * EXPRESSED OR IMPLIED.  USE IT AT YOUR OWN RISK.
00023  * 
00024  * <hr>
00025  * 
00026  */
00027 /*****************************************************************************/
00028 
00029 #include "expr_value.h"
00030 #include "notifylist.h"
00031 
00032 using namespace std;
00033 
00034 namespace CVCL {
00035 
00036 ////////////////////////////////////////////////////////////////////////
00037 // Class ExprValue static members
00038 ////////////////////////////////////////////////////////////////////////
00039 
00040 std::hash<char*> ExprValue::s_charHash;
00041 std::hash<long int> ExprValue::s_intHash;
00042 
00043 ////////////////////////////////////////////////////////////////////////
00044 // Class ExprValue methods
00045 ////////////////////////////////////////////////////////////////////////
00046 
00047 
00048 // Descructor
00049 ExprValue::~ExprValue() {
00050   // Be careful deleting the attributes: first set them to NULL, then
00051   // delete, to avoid circular destructor calls
00052   if (d_find) {
00053     CDO<Theorem>* find = d_find;
00054     d_find = NULL;
00055     delete find;
00056   }
00057   if(d_notifyList != NULL) {
00058     NotifyList* nl = d_notifyList;
00059     d_notifyList = NULL;
00060     delete nl;
00061   }
00062   // Set all smart pointers to Null
00063   d_type = Type();
00064   d_tcc = Expr();
00065   d_subtypePred=Theorem();
00066   d_simpCache=Theorem();
00067   d_simpFrom=Expr();
00068 }
00069 
00070 // Equality between any two ExprValue objects (including subclasses)
00071 bool ExprValue::operator==(const ExprValue& ev2) const {
00072   DebugAssert(getMMIndex() == EXPR_VALUE,
00073               "ExprValue::operator==() called from a subclass");
00074   if(getMMIndex() != ev2.getMMIndex())
00075     return false;
00076 
00077   return (d_kind == ev2.d_kind);
00078 }
00079 
00080 
00081 ExprValue* ExprValue::copy(ExprManager* em, ExprIndex idx) const {
00082   DebugAssert(getMMIndex() == EXPR_VALUE,
00083               "ExprValue::copy() called from a subclass");
00084   return new(em->getMM(EXPR_VALUE)) ExprValue(em, d_kind, idx);
00085 }
00086 
00087 
00088 ExprNode::~ExprNode()
00089 {
00090   // Be careful deleting the attributes: first set them to NULL, then
00091   // delete, to avoid circular destructor calls
00092   if (d_sig) {
00093     CDO<Theorem>* sig = d_sig;
00094     d_sig = NULL;
00095     delete sig;
00096   }
00097   if (d_rep) {
00098     CDO<Theorem>* rep = d_rep;
00099     d_rep = NULL;
00100     delete rep;
00101   }
00102 }
00103 
00104 
00105 bool ExprNode::operator==(const ExprValue& ev2) const {
00106   if(getMMIndex() != ev2.getMMIndex())
00107     return false;
00108 
00109   return (d_kind == ev2.getKind())
00110     && (getKids() == ev2.getKids());
00111 }
00112 
00113 ExprValue* ExprNode::copy(ExprManager* em, ExprIndex idx) const {
00114   if (d_em != em) {
00115     vector<Expr> children;
00116     vector<Expr>::const_iterator
00117       i = d_children.begin(), iend = d_children.end();
00118     for (; i != iend; ++i)
00119       children.push_back(rebuild(*i, em));
00120     return new(em->getMM(getMMIndex())) ExprNode(em, d_kind, children, idx);
00121   }
00122   return new(em->getMM(getMMIndex())) ExprNode(em, d_kind, d_children, idx);
00123 }
00124 
00125 
00126 bool ExprString::operator==(const ExprValue& ev2) const {
00127   if(getMMIndex() != ev2.getMMIndex())
00128     return false;
00129 
00130   return (getString() == ev2.getString());
00131 }
00132 
00133 ExprValue* ExprString::copy(ExprManager* em, ExprIndex idx) const {
00134   return new(em->getMM(getMMIndex())) ExprString(em, d_str, idx);
00135 }
00136 
00137 
00138 bool ExprSkolem::operator==(const ExprValue& ev2) const {
00139   if(getMMIndex() != ev2.getMMIndex())
00140     return false;
00141 
00142   return (getBoundIndex() == ev2.getBoundIndex()
00143           && getExistential() == ev2.getExistential());
00144 }
00145 
00146 ExprValue* ExprSkolem::copy(ExprManager* em, ExprIndex idx) const {
00147   if (d_em != em) {
00148     return new(em->getMM(getMMIndex()))
00149       ExprSkolem(em, getBoundIndex(), rebuild(getExistential(), em), idx);
00150   }
00151   return new(em->getMM(getMMIndex()))
00152     ExprSkolem(em, getBoundIndex(), getExistential(), idx);
00153 }
00154 
00155 
00156 bool ExprRational::operator==(const ExprValue& ev2) const {
00157   if(getMMIndex() != ev2.getMMIndex())
00158     return false;
00159 
00160   return (getRational() == ev2.getRational());
00161 }
00162 
00163 ExprValue* ExprRational::copy(ExprManager* em, ExprIndex idx) const {
00164   return new(em->getMM(getMMIndex())) ExprRational(em, d_r, idx);
00165 }
00166 
00167 
00168 bool ExprVar::operator==(const ExprValue& ev2) const {
00169   if(getMMIndex() != ev2.getMMIndex())
00170     return false;
00171 
00172   return (getKind() == ev2.getKind() && getName() == ev2.getName());
00173 }
00174 
00175 ExprValue* ExprVar::copy(ExprManager* em, ExprIndex idx) const {
00176   return new(em->getMM(getMMIndex())) ExprVar(em, d_name, idx);
00177 }
00178 
00179 
00180 bool ExprSymbol::operator==(const ExprValue& ev2) const {
00181   if(getMMIndex() != ev2.getMMIndex())
00182     return false;
00183 
00184   return (getKind() == ev2.getKind() && getName() == ev2.getName());
00185 }
00186 
00187 ExprValue* ExprSymbol::copy(ExprManager* em, ExprIndex idx) const {
00188   return new(em->getMM(getMMIndex())) ExprSymbol(em, d_kind, d_name, idx);
00189 }
00190 
00191 
00192 bool ExprBoundVar::operator==(const ExprValue& ev2) const {
00193   if(getMMIndex() != ev2.getMMIndex())
00194     return false;
00195 
00196   return (getKind() == ev2.getKind() && getName() == ev2.getName()
00197           && getUid() == ev2.getUid());
00198 }
00199 
00200 ExprValue* ExprBoundVar::copy(ExprManager* em, ExprIndex idx) const {
00201   return new(em->getMM(getMMIndex())) ExprBoundVar(em, d_name, d_uid, idx);
00202 }
00203 
00204 
00205 bool ExprApply::operator==(const ExprValue& ev2) const {
00206   if(getMMIndex() != ev2.getMMIndex())
00207     return false;
00208 
00209   return (getOp() == ev2.getOp())
00210       && (getKids() == ev2.getKids());
00211 }
00212 
00213 ExprValue* ExprApply::copy(ExprManager* em, ExprIndex idx) const {
00214   if (d_em != em) {
00215     vector<Expr> children;
00216     vector<Expr>::const_iterator
00217       i = d_children.begin(), iend = d_children.end();
00218     for (; i != iend; ++i)
00219       children.push_back(rebuild(*i, em));
00220     return new(em->getMM(getMMIndex()))
00221       ExprApply(em, Op(rebuild(d_opExpr, em)), children, idx);
00222   }
00223   return new(em->getMM(getMMIndex()))
00224     ExprApply(em, Op(d_opExpr), d_children, idx);
00225 }
00226 
00227 bool ExprClosure::operator==(const ExprValue& ev2) const {
00228   if(getMMIndex() != ev2.getMMIndex())
00229     return false;
00230 
00231   return (getKind() == ev2.getKind())
00232       && (getBody() == ev2.getBody())
00233       && (getVars() == ev2.getVars());
00234 }
00235 
00236 ExprValue* ExprClosure::copy(ExprManager* em, ExprIndex idx) const {
00237   if (d_em != em) {
00238     vector<Expr> vars;
00239     vector<Expr>::const_iterator i = d_vars.begin(), iend = d_vars.end();
00240     for (; i != iend; ++i)
00241       vars.push_back(rebuild(*i, em));
00242     return new(em->getMM(getMMIndex()))
00243       ExprClosure(em, d_kind, vars, rebuild(d_body, em), idx);
00244   }
00245   return new(em->getMM(getMMIndex()))
00246     ExprClosure(em, d_kind, d_vars, d_body, idx);
00247 }
00248 
00249 ////////////////////////////////////////////////////////////////////////
00250 // Methods of subclasses of ExprValue
00251 ////////////////////////////////////////////////////////////////////////
00252 
00253 // Hash function for subclasses with kids.
00254 size_t ExprValue::hash(const int kind, const std::vector<Expr>& kids) {
00255   size_t res(s_intHash((long int)kind));
00256   for(std::vector<Expr>::const_iterator i=kids.begin(), iend=kids.end();
00257       i!=iend; ++i) {
00258     void* ptr = i->d_expr;
00259     res = res*PRIME + pointerHash(ptr);
00260   }
00261   return res;
00262 }
00263 
00264 size_t ExprClosure::computeHash() const {
00265   return d_body.hash()*PRIME + ExprValue::hash(d_kind, d_vars);
00266 }
00267 
00268 
00269 } // end of namespace CVCL

Generated on Thu Apr 13 16:57:31 2006 for CVC Lite by  doxygen 1.4.4