From 12a8a7f9a90e45e8313f26af527a52e6dda943d3 Mon Sep 17 00:00:00 2001 From: "Christopher L. Conway" Date: Tue, 13 Apr 2010 22:50:07 +0000 Subject: Merging from branches/decl-scopes (r401:411) --- src/expr/declaration_scope.h | 132 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 src/expr/declaration_scope.h (limited to 'src/expr/declaration_scope.h') diff --git a/src/expr/declaration_scope.h b/src/expr/declaration_scope.h new file mode 100644 index 000000000..e08c25173 --- /dev/null +++ b/src/expr/declaration_scope.h @@ -0,0 +1,132 @@ +/********************* */ +/** context.h + ** Original author: cconway + ** This file is part of the CVC4 prototype. + ** Copyright (c) 2009, 2010 The Analysis of Computer Systems Group (ACSys) + ** Courant Institute of Mathematical Sciences + ** New York University + ** See the file COPYING in the top-level source directory for licensing + ** information. + ** + ** Convenience class for scoping variable and type declarations. + **/ + +#ifndef DECLARATION_SCOPE_H_ +#define DECLARATION_SCOPE_H_ + +#include "expr.h" + +#include + +namespace CVC4 { + +class Type; + +namespace context { + +class Context; + +template +class CDMap; + +} //namespace context + +/** A basic hash function for std::string + * TODO: Does this belong somewhere else (like util/)? + */ +struct StringHashFunction { + size_t operator()(const std::string& str) const { + return __gnu_cxx::hash()(str.c_str()); + } +}; + +class CVC4_PUBLIC ScopeException : public Exception { +}; + +/** + * A convenience class for handling scoped declarations. Implements the usual + * nested scoping rules for declarations, with separate bindings for expressions + * and types. + */ +class CVC4_PUBLIC DeclarationScope { + /** The context manager for the scope maps. */ + context::Context *d_context; + + /** A map for expressions. */ + context::CDMap *d_exprMap; + + /** A map for types. */ + context::CDMap *d_typeMap; + +public: + /** Create a declaration scope. */ + DeclarationScope(); + + /** Destroy a declaration scope. */ + ~DeclarationScope(); + + /** Bind an expression to a name in the current scope level. + * If name is already bound in the current level, then the + * binding is replaced. If name is bound in a previous + * level, then the binding is "covered" by this one until the current + * scope is popped. + * + * @param name an identifier + * @param obj the expression to bind to name + */ + void bind(const std::string& name, const Expr& obj) throw (); + + /** Bind a type to a name in the current scope. + * If name is already bound to a type in the current level, + * then the binding is replaced. If name is bound in a + * previous level, then the binding is "covered" by this one until the + * current scope is popped. + * + * @param name an identifier + * @param t the type to bind to name + */ + void bindType(const std::string& name, Type* t) throw (); + + /** Check whether a name is bound to an expression. + * + * @param name the identifier to check. + * @returns true iff name is bound in the current scope. + */ + bool isBound(const std::string& name) const throw (); + + /** Check whether a name is bound to a type. + * + * @param name the identifier to check. + * @returns true iff name is bound to a type in the current scope. + */ + bool isBoundType(const std::string& name) const throw (); + + /** Lookup a bound expression. + * + * @param name the identifier to lookup + * @returns the expression bound to name in the current scope. + */ + Expr lookup(const std::string& name) const throw (); + + /** Lookup a bound type. + * + * @param name the identifier to lookup + * @returns the type bound to name in the current scope. + */ + Type* lookupType(const std::string& name) const throw (); + + /** Pop a scope level. Deletes all bindings since the last call to + * pushScope. Calls to pushScope and + * popScope must be "properly nested." I.e., a call to + * popScope is only legal if the number of prior calls to + * pushScope on this DeclarationScope is strictly + * greater than then number of prior calls to popScope. */ + void popScope() throw (ScopeException); + + /** Push a scope level. */ + void pushScope() throw (); +}; + +} // namespace CVC4 + +#endif /* DECLARATION_SCOPE_H_ */ -- cgit v1.2.3