Commit 5f6d8b62 authored by Alexandre Duret-Lutz's avatar Alexandre Duret-Lutz
Browse files

Massage the AST so that identical sub-formula share the same

reference-counted formula*.  One can't call constructors for AST
items anymore, everything need to be acquired through instance()
class methods.

* src/ltlast/formula.cc, src/ltlast/refformula.cc,
src/ltlast/refformula.hh: New files.
* src/ltlast/Makefile.am (libltlast_la_SOURCES): Add them.
* src/ltlast/atomic_prop.cc, src/ltlast/atomic_prop.hh,
src/ltlast/unop.cc, src/ltlast/unop.hh,
src/ltlast/binop.cc, src/ltlast/binop.hh: Make the constructor
and destructor protected.  Define a static function `instance()'
to get an instance with specific argument.  Use a map called
`instances' to store all known instances.  Inherit from
ref_formula.
* src/ltlast/constant.hh, src/ltlast/constant.cc: Protect
the constructor and destructor.  Provide the false_instance()
and true_instance() functions instead.
* src/formula.hh (ref, unref, ref_, unref_): New methods.
* src/ltlast/multop.cc, src/ltlast/multop.hh: Protect
the constructor, destructor, as well as the add() method.
Provides the instance(), and add() class methods instead.
Store children_ as a pointer.
* src/ltlenv/defaultenv.cc (require): Adjust to
call atomic_prop::instance.
* src/ltlparse/ltlparse.yy: Adjust to call instance() functions
instead of constructors.
* src/ltltest/Makefile.am (LDADD): Tweak library ordering.
* src/ltlvisit/clone.hh (clone_visitor): Inherit from visitor,
not const_visitor, and adjust all prototypes appropriately.
* src/ltlvisit/clone.cc (clone_visitor): Likewise.
Call ref() or instance() methods instead of copy constructors.
* src/ltlvisit/equals.cc: Simplify atomic_prop and constant
cases.
* src/ltlvisit/lunabbrev.hh, src/ltlvisit/lunabbrev.cc,
src/ltlvisit/tunabbrev.hh, src/ltlvisit/tunabbrev.cc,
src/ltlvisit/nenoform.hh, src/ltlvisit/nenoform.cc: Use instance()
methods instead of constructor.  Make these children of visitor, not
const_visitor.
* src/ltltest/readltl.c (main): Do not delete the formula.
parent f1838ab8
2003-05-15 Alexandre Duret-Lutz <aduret@src.lip6.fr>
Massage the AST so that identical sub-formula share the same
reference-counted formula*. One can't call constructors for AST
items anymore, everything need to be acquired through instance()
class methods.
* src/ltlast/formula.cc, src/ltlast/refformula.cc,
src/ltlast/refformula.hh: New files.
* src/ltlast/Makefile.am (libltlast_la_SOURCES): Add them.
* src/ltlast/atomic_prop.cc, src/ltlast/atomic_prop.hh,
src/ltlast/unop.cc, src/ltlast/unop.hh,
src/ltlast/binop.cc, src/ltlast/binop.hh: Make the constructor
and destructor protected. Define a static function `instance()'
to get an instance with specific argument. Use a map called
`instances' to store all known instances. Inherit from
ref_formula.
* src/ltlast/constant.hh, src/ltlast/constant.cc: Protect
the constructor and destructor. Provide the false_instance()
and true_instance() functions instead.
* src/formula.hh (ref, unref, ref_, unref_): New methods.
* src/ltlast/multop.cc, src/ltlast/multop.hh: Protect
the constructor, destructor, as well as the add() method.
Provides the instance(), and add() class methods instead.
Store children_ as a pointer.
* src/ltlenv/defaultenv.cc (require): Adjust to
call atomic_prop::instance.
* src/ltlparse/ltlparse.yy: Adjust to call instance() functions
instead of constructors.
* src/ltltest/Makefile.am (LDADD): Tweak library ordering.
* src/ltlvisit/clone.hh (clone_visitor): Inherit from visitor,
not const_visitor, and adjust all prototypes appropriately.
* src/ltlvisit/clone.cc (clone_visitor): Likewise.
Call ref() or instance() methods instead of copy constructors.
* src/ltlvisit/equals.cc: Simplify atomic_prop and constant
cases.
* src/ltlvisit/lunabbrev.hh, src/ltlvisit/lunabbrev.cc,
src/ltlvisit/tunabbrev.hh, src/ltlvisit/tunabbrev.cc,
src/ltlvisit/nenoform.hh, src/ltlvisit/nenoform.cc: Use instance()
methods instead of constructor. Make these children of visitor, not
const_visitor.
* src/ltltest/readltl.c (main): Do not delete the formula.
* src/ltlparse/ltlscan.ll (to_parse_size): Declare as size_t to
remove a warning with newer versions of Flex.
......
......@@ -11,10 +11,12 @@ libltlast_la_SOURCES = \
constant.cc \
constant.hh \
formula.hh \
formula.cc \
multop.cc \
multop.hh \
predecl.hh \
refformula.cc \
refformula.hh \
unop.cc \
unop.hh \
visitor.hh
......@@ -5,7 +5,7 @@ namespace spot
{
namespace ltl
{
atomic_prop::atomic_prop(const std::string& name, environment& env)
: name_(name), env_(&env)
{
......@@ -15,29 +15,45 @@ namespace spot
{
}
void
void
atomic_prop::accept(visitor& v)
{
v.visit(this);
}
void
atomic_prop::accept(const_visitor& v) const
{
v.visit(this);
}
}
const std::string&
atomic_prop::name() const
{
return name_;
}
environment&
environment&
atomic_prop::env() const
{
return *env_;
}
atomic_prop::map atomic_prop::instances;
atomic_prop*
atomic_prop::instance(const std::string& name, environment& env)
{
pair p(name, &env);
map::iterator i = instances.find(p);
if (i != instances.end())
{
return static_cast<atomic_prop*>(i->second->ref());
}
atomic_prop* ap = new atomic_prop(name, env);
instances[p] = ap;
return static_cast<atomic_prop*>(ap->ref());
}
}
}
......@@ -2,7 +2,8 @@
# define SPOT_LTLAST_ATOMIC_PROP_HH
#include <string>
#include "formula.hh"
#include <map>
#include "refformula.hh"
#include "ltlenv/environment.hh"
namespace spot
......@@ -11,13 +12,12 @@ namespace spot
{
/// Atomic propositions.
class atomic_prop : public formula
class atomic_prop : public ref_formula
{
public:
/// Build an atomic proposition with name \a name in
/// Build an atomic proposition with name \a name in
/// environment \a env.
atomic_prop(const std::string& name, environment& env);
virtual ~atomic_prop();
static atomic_prop* instance(const std::string& name, environment& env);
virtual void accept(visitor& visitor);
virtual void accept(const_visitor& visitor) const;
......@@ -26,6 +26,14 @@ namespace spot
const std::string& name() const;
/// Get the environment of the atomic proposition.
environment& env() const;
protected:
atomic_prop(const std::string& name, environment& env);
virtual ~atomic_prop();
typedef std::pair<std::string, environment*> pair;
typedef std::map<pair, atomic_prop*> map;
static map instances;
private:
std::string name_;
environment* env_;
......
......@@ -5,7 +5,7 @@
namespace spot
{
namespace ltl
{
{
binop::binop(type op, formula* first, formula* second)
: op_(op), first_(first), second_(second)
{
......@@ -45,19 +45,19 @@ namespace spot
return second_;
}
formula*
formula*
binop::second()
{
return second_;
}
binop::type
binop::type
binop::op() const
{
return op_;
}
const char*
const char*
binop::op_name() const
{
switch (op_)
......@@ -78,5 +78,22 @@ namespace spot
return 0;
}
binop::map binop::instances;
binop*
binop::instance(type op, formula* first, formula* second)
{
pairf pf(first, second);
pair p(op, pf);
map::iterator i = instances.find(p);
if (i != instances.end())
{
return static_cast<binop*>(i->second->ref());
}
binop* ap = new binop(op, first, second);
instances[p] = ap;
return static_cast<binop*>(ap->ref());
}
}
}
#ifndef SPOT_LTLAST_BINOP_HH
# define SPOT_LTLAST_BINOP_HH
#include <map>
#include "formula.hh"
namespace spot
......@@ -18,8 +19,9 @@ namespace spot
/// are often nested we represent them as multops.
enum type { Xor, Implies, Equiv, U, R };
binop(type op, formula* first, formula* second);
virtual ~binop();
/// Build an unary operator with operation \a op and
/// children \a first and \a second.
static binop* instance(type op, formula* first, formula* second);
virtual void accept(visitor& v);
virtual void accept(const_visitor& v) const;
......@@ -38,6 +40,15 @@ namespace spot
/// Get the type of this operator, as a string.
const char* op_name() const;
protected:
typedef std::pair<formula*, formula*> pairf;
typedef std::pair<type, pairf> pair;
typedef std::map<pair, formula*> map;
static map instances;
binop(type op, formula* first, formula* second);
virtual ~binop();
private:
type op_;
formula* first_;
......
......@@ -5,7 +5,7 @@
namespace spot
{
namespace ltl
{
{
constant::constant(type val)
: val_(val)
{
......@@ -27,13 +27,13 @@ namespace spot
v.visit(this);
}
constant::type
constant::type
constant::val() const
{
return val_;
}
const char*
const char*
constant::val_name() const
{
switch (val_)
......@@ -48,5 +48,18 @@ namespace spot
return 0;
}
constant*
constant::false_instance()
{
static constant f(constant::False);
return &f;
}
constant*
constant::true_instance()
{
static constant t(constant::True);
return &t;
}
}
}
......@@ -13,10 +13,6 @@ namespace spot
{
public:
enum type { False, True };
constant(type val);
virtual ~constant();
virtual void accept(visitor& v);
virtual void accept(const_visitor& v) const;
......@@ -25,6 +21,15 @@ namespace spot
/// Return the value of the constant as a string.
const char* val_name() const;
/// Get the sole instance of spot::ltl::constant::constant(True).
static constant* true_instance();
/// Get the sole instance of spot::ltl::constant::constant(False).
static constant* false_instance();
protected:
constant(type val);
virtual ~constant();
private:
type val_;
};
......
#include "formula.hh"
namespace spot
{
namespace ltl
{
formula*
formula::ref()
{
ref_();
return this;
}
void
formula::unref(formula* f)
{
if (f->unref_())
delete f;
}
void
formula::ref_()
{
// Not reference counted by default.
}
bool
formula::unref_()
{
// Not reference counted by default.
return false;
}
}
}
......@@ -3,20 +3,32 @@
#include "predecl.hh"
namespace spot
namespace spot
{
namespace ltl
namespace ltl
{
/// \brief An LTL formula.
///
/// The only way you can work with a formula is to
///
/// The only way you can work with a formula is to
/// build a spot::ltl::visitor or spot::ltl::const_visitor.
class formula
class formula
{
public:
virtual void accept(visitor& v) = 0;
virtual void accept(const_visitor& v) const = 0;
/// \brief clone this formula
formula* ref();
/// \brief release formula
static void unref(formula* f);
protected:
/// \brief increment reference counter if any
virtual void ref_();
/// \brief decrement reference counter if any, return true when
/// the instance must be delete (usually when the counter hits 0).
virtual bool unref_();
};
}
......
......@@ -6,42 +6,15 @@
namespace spot
{
namespace ltl
{
multop::multop(type op)
: op_(op)
{
multop::multop(type op, vec* v)
: op_(op), children_(v)
{
}
multop::multop(type op, formula* first, formula* second)
: op_(op)
{
children_.reserve(2);
add(first);
add(second);
}
void
multop::add(formula* f)
{
// If the formula we add is itself a multop for the same operator,
// merge its children with ours.
multop* p = dynamic_cast<multop*>(f);
if (p && p->op() == op())
{
unsigned ps = p->size();
for (unsigned i = 0; i < ps; ++i)
children_.push_back(p->nth(i));
// that sub-formula is now useless
delete f;
}
else
{
children_.push_back(f);
}
}
multop::~multop()
{
delete children_;
}
void
......@@ -59,28 +32,28 @@ namespace spot
unsigned
multop::size() const
{
return children_.size();
return children_->size();
}
const formula*
multop::nth(unsigned n) const
{
return children_[n];
return (*children_)[n];
}
formula*
formula*
multop::nth(unsigned n)
{
return children_[n];
return (*children_)[n];
}
multop::type
multop::type
multop::op() const
{
return op_;
}
const char*
const char*
multop::op_name() const
{
switch (op_)
......@@ -95,5 +68,69 @@ namespace spot
return 0;
}
multop::map multop::instances;
multop*
multop::instance(type op, vec* v)
{
pair p(op, v);
map::iterator i = instances.find(p);
if (i != instances.end())
{
delete v;
return static_cast<multop*>(i->second->ref());
}
multop* ap = new multop(op, v);
instances[p] = ap;
return static_cast<multop*>(ap->ref());
}
multop*
multop::instance(type op)
{
return instance(op, new vec);
}
multop*
multop::instance(type op, formula* first, formula* second)
{
vec* v = new vec;
multop::add(op, v, first);
multop::add(op, v, second);
return instance(op, v);
}
multop::vec*
multop::add(type op, vec* v, formula* f)
{
// If the formula we add is itself a multop for the same operator,
// merge its children.
multop* p = dynamic_cast<multop*>(f);
if (p && p->op() == op)
{
unsigned ps = p->size();
for (unsigned i = 0; i < ps; ++i)
v->push_back(p->nth(i));
// that sub-formula is now useless
formula::unref(f);
}
else
{
v->push_back(f);
}
return v;
}
void
multop::add(multop** m, formula* f)
{
vec* v = new vec(*(*m)->children_);
type op = (*m)->op();
multop::add(op, v, f);
formula::unref(*m);
*m = instance(op, v);
}
}
}
......@@ -2,13 +2,14 @@
# define SPOT_LTLAST_MULTOP_HH
#include <vector>
#include <map>
#include "formula.hh"
namespace spot
{
namespace ltl
{
/// \brief Multi-operand operators.
///
/// These operators are considered commutative and associative.
......@@ -17,25 +18,29 @@ namespace spot
public:
enum type { Or, And };
/// \brief Build a spot::ltl::multop with no child.
///
/// This has little value unless you call multop::add later.
multop(type op);
static multop* instance(type op);
/// \brief Build a spot::ltl::multop with two children.
///
///
/// If one of the children itself is a spot::ltl::multop
/// with the same type, it will be merged. I.e., children
/// if that child will be added, and that child itself will
/// be destroyed.
multop(type op, formula* first, formula* second);
static multop* instance(type op, formula* first, formula* second);
/// \brief Add another child to this operator.
///
/// If \a f itself is a spot::ltl::multop with the same type, it
/// will be merged. I.e., children of \a f will be added, and
/// that \a f will will be destroyed.
void add(formula* f);
virtual ~multop();
///
/// Note that this function overwrites the supplied ltl::multop pointer.
/// The old value is released and should not be used after this.
static void add(multop** m, formula* f);
virtual void accept(visitor& v);
virtual void accept(const_visitor& v) const;
......@@ -56,9 +61,21 @@ namespace spot
/// Get the type of this operator, as a string.
const char* op_name() const;
protected:
typedef std::vector<formula*> vec;
typedef std::pair<type, vec*> pair;
typedef std::map<pair, formula*> map;
static map instances;
multop(type op, vec* v);
static multop* instance(type op, vec* v);
static vec* multop::add(type op, vec* v, formula* f);
virtual ~multop();
private:
type op_;
std::vector<formula*> children_;
vec* children_;
};
}
......
#include "refformula.hh"
#include <cassert>
namespace spot
{
namespace ltl
{
ref_formula::ref_formula()