17140c8506c463e6eb48d2414afd2057702c7405
[cvc5.git] / src / expr / declaration_scope.h
1 /********************* */
2 /*! \file declaration_scope.h
3 ** \verbatim
4 ** Original author: cconway
5 ** Major contributors: none
6 ** Minor contributors (to current version): dejan, mdeters
7 ** This file is part of the CVC4 prototype.
8 ** Copyright (c) 2009, 2010 The Analysis of Computer Systems Group (ACSys)
9 ** Courant Institute of Mathematical Sciences
10 ** New York University
11 ** See the file COPYING in the top-level source directory for licensing
12 ** information.\endverbatim
13 **
14 ** \brief Convenience class for scoping variable and type declarations.
15 **
16 ** Convenience class for scoping variable and type declarations.
17 **/
18
19 #include "cvc4_public.h"
20
21 #ifndef __CVC4__DECLARATION_SCOPE_H
22 #define __CVC4__DECLARATION_SCOPE_H
23
24 #include <vector>
25 #include <utility>
26 #include <ext/hash_map>
27
28 #include "expr/expr.h"
29 #include "util/hash.h"
30
31 #include "context/cdset_forward.h"
32 #include "context/cdmap_forward.h"
33
34 namespace CVC4 {
35
36 class Type;
37
38 namespace context {
39 class Context;
40 }/* CVC4::context namespace */
41
42 class CVC4_PUBLIC ScopeException : public Exception {
43 };/* class ScopeException */
44
45 /**
46 * A convenience class for handling scoped declarations. Implements the usual
47 * nested scoping rules for declarations, with separate bindings for expressions
48 * and types.
49 */
50 class CVC4_PUBLIC DeclarationScope {
51 /** The context manager for the scope maps. */
52 context::Context *d_context;
53
54 /** A map for expressions. */
55 context::CDMap<std::string, Expr, StringHashFunction> *d_exprMap;
56
57 /** A map for types. */
58 context::CDMap<std::string, std::pair<std::vector<Type>, Type>, StringHashFunction> *d_typeMap;
59
60 /** A set of defined functions. */
61 context::CDSet<Expr, ExprHashFunction> *d_functions;
62
63 public:
64 /** Create a declaration scope. */
65 DeclarationScope();
66
67 /** Destroy a declaration scope. */
68 ~DeclarationScope();
69
70 /**
71 * Bind an expression to a name in the current scope level. If
72 * <code>name</code> is already bound to an expression in the current
73 * level, then the binding is replaced. If <code>name</code> is bound
74 * in a previous level, then the binding is "covered" by this one
75 * until the current scope is popped.
76 *
77 * @param name an identifier
78 * @param obj the expression to bind to <code>name</code>
79 */
80 void bind(const std::string& name, Expr obj) throw(AssertionException);
81
82 /**
83 * Bind a function body to a name in the current scope. If
84 * <code>name</code> is already bound to an expression in the current
85 * level, then the binding is replaced. If <code>name</code> is bound
86 * in a previous level, then the binding is "covered" by this one
87 * until the current scope is popped. Same as bind() but registers
88 * this as a function (so that isBoundDefinedFunction() returns true).
89 *
90 * @param name an identifier
91 * @param obj the expression to bind to <code>name</code>
92 */
93 void bindDefinedFunction(const std::string& name, Expr obj) throw(AssertionException);
94
95 /**
96 * Bind a type to a name in the current scope. If <code>name</code>
97 * is already bound to a type in the current level, then the binding
98 * is replaced. If <code>name</code> is bound in a previous level,
99 * then the binding is "covered" by this one until the current scope
100 * is popped.
101 *
102 * @param name an identifier
103 * @param t the type to bind to <code>name</code>
104 */
105 void bindType(const std::string& name, Type t) throw();
106
107 /**
108 * Bind a type to a name in the current scope. If <code>name</code>
109 * is already bound to a type or type constructor in the current
110 * level, then the binding is replaced. If <code>name</code> is
111 * bound in a previous level, then the binding is "covered" by this
112 * one until the current scope is popped.
113 *
114 * @param name an identifier
115 * @param params the parameters to the type
116 * @param t the type to bind to <code>name</code>
117 */
118 void bindType(const std::string& name,
119 const std::vector<Type>& params,
120 Type t) throw();
121
122 /**
123 * Check whether a name is bound to an expression with either bind()
124 * or bindDefinedFunction().
125 *
126 * @param name the identifier to check.
127 * @returns true iff name is bound in the current scope.
128 */
129 bool isBound(const std::string& name) const throw();
130
131 /**
132 * Check whether a name was bound to a function with bindDefinedFunction().
133 */
134 bool isBoundDefinedFunction(const std::string& name) const throw();
135
136 /**
137 * Check whether a name is bound to a type (or type constructor).
138 *
139 * @param name the identifier to check.
140 * @returns true iff name is bound to a type in the current scope.
141 */
142 bool isBoundType(const std::string& name) const throw();
143
144 /**
145 * Lookup a bound expression.
146 *
147 * @param name the identifier to lookup
148 * @returns the expression bound to <code>name</code> in the current scope.
149 */
150 Expr lookup(const std::string& name) const throw(AssertionException);
151
152 /**
153 * Lookup a bound type.
154 *
155 * @param name the type identifier to lookup
156 * @returns the type bound to <code>name</code> in the current scope.
157 */
158 Type lookupType(const std::string& name) const throw(AssertionException);
159
160 /**
161 * Lookup a bound parameterized type.
162 *
163 * @param name the type-constructor identifier to lookup
164 * @param params the types to use to parameterize the type
165 * @returns the type bound to <code>name(<i>params</i>)</code> in
166 * the current scope.
167 */
168 Type lookupType(const std::string& name,
169 const std::vector<Type>& params) const throw(AssertionException);
170
171 /**
172 * Pop a scope level. Deletes all bindings since the last call to
173 * <code>pushScope</code>. Calls to <code>pushScope</code> and
174 * <code>popScope</code> must be "properly nested." I.e., a call to
175 * <code>popScope</code> is only legal if the number of prior calls to
176 * <code>pushScope</code> on this <code>DeclarationScope</code> is strictly
177 * greater than then number of prior calls to <code>popScope</code>. */
178 void popScope() throw(ScopeException);
179
180 /** Push a scope level. */
181 void pushScope() throw();
182
183 };/* class DeclarationScope */
184
185 }/* CVC4 namespace */
186
187 #endif /* __CVC4__DECLARATION_SCOPE_H */