term/FunctionTerm.h
00001 /*
00002  * FunctionTerm.h
00003  *
00004  *  Created on: Sep 1, 2008
00005  *      Author: tdillig
00006  */
00007 
00008 #ifndef FUNCTIONTERM_H_
00009 #define FUNCTIONTERM_H_
00010 
00011 #include "Term.h"
00012 #include <boost/serialization/vector.hpp>
00013 #include <string>
00014 using namespace std;
00015 
00016 Term* _make_ap(const string & name, vector<Term*>& args, bool invertible);
00017 
00018 
00019 class FunctionTerm: public Term {
00020         friend class Address;
00021         friend class StringLiteral;
00022         friend class ProgramFunction;
00023         friend class TypeConstant;
00024         friend class boost::serialization::access;
00025 public:
00026         int fun_id;
00027         vector<Term*> args;
00028 
00029         // Is this function invertible?
00030         bool invertible;
00031 
00032         template<class Archive>
00033         void save(Archive & ar, const unsigned int version) const
00034         {
00035                 ar & boost::serialization::base_object<Term>(*this);
00036                 const string& name = CNode::get_varmap().get_name(fun_id);
00037                 ar & name;
00038                 int attrib = get_id_attribute();
00039                 ar & attrib;
00040                 ar & args;
00041                 ar & invertible;
00042         }
00043         template<class Archive>
00044         void load(Archive & ar, const unsigned int version)
00045         {
00046                 ar & boost::serialization::base_object<Term>(*this);
00047                 string name;
00048                 ar & name;
00049                 int attrib;
00050                 ar & attrib;
00051                 fun_id = CNode::get_varmap().get_id(name);
00052                 fun_id |= attrib;
00053                 ar & args;
00054                 ar & invertible;
00055                 for(unsigned int i=0; i < args.size(); i++)
00056                 {
00057                         args[i] = Term::get_term_nodelete(args[i]);
00058                 }
00059                 compute_hash_code();
00060 
00061         }
00062         BOOST_SERIALIZATION_SPLIT_MEMBER()
00063 public:
00064         FunctionTerm(int id, const vector<Term*>& args, bool invertible,
00065                         int attribute = 0);
00066 protected:
00067         FunctionTerm(){}
00068 
00069         FunctionTerm(int id, Term* arg, bool invertible, int attribute = 0);
00070         FunctionTerm(int id, Term* arg1, Term* arg2, bool invertible,
00071                         int attribute = 0);
00072         virtual ~FunctionTerm();
00073         void compute_hash_code();
00074 public:
00075         static Term* make(int id, vector<Term*>& args, bool invertible);
00076         static Term* make(string name, vector<Term*>& args, bool invertible);
00077         virtual bool operator==(const Term& other);
00078         virtual string to_string();
00079         inline int get_id() const
00080         {
00081                 return fun_id;
00082         }
00083         inline bool is_invertible()
00084         {
00085                 return invertible;
00086         }
00087         string get_name();
00088         virtual Term* substitute(map<Term*, Term*>& subs);
00089         inline const vector<Term*>& get_args()
00090         {
00091                 return args;
00092         }
00093         inline int get_id_attribute() const
00094         {
00095                 int mask = (1 << NUM_BITS_RESERVED)-1;
00096                 int res = fun_id & mask;
00097                 return res;
00098         }
00099 
00100 };
00101 
00102 
00103 #endif /* FUNCTIONTERM_H_ */