00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef _CVC_lite__expr_h_
00033 #include "expr.h"
00034 #endif
00035
00036 #ifndef _CVC_lite__include__expr_manager_h_
00037 #define _CVC_lite__include__expr_manager_h_
00038
00039 #include "expr_map.h"
00040
00041 namespace CVCL {
00042
00043 class CLFlags;
00044 class PrettyPrinter;
00045 class MemoryManager;
00046 class ExprManagerNotifyObj;
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 class ExprManager {
00064 friend class Expr;
00065 friend class ExprValue;
00066 friend class Op;
00067 friend class HashEV;
00068 friend class Type;
00069
00070 ContextManager* d_cm;
00071 ExprManagerNotifyObj* d_notifyObj;
00072 ExprIndex d_index;
00073 unsigned d_flagCounter;
00074
00075
00076 std::hash_map<int, std::string> d_kindMap;
00077
00078 std::hash_set<int> d_typeKinds;
00079
00080 class HashString {
00081 std::hash<char*> h;
00082 public:
00083 size_t operator()(const std::string& s) const {
00084 return h(const_cast<char*>(s.c_str()));
00085 }
00086 };
00087
00088 std::hash_map<std::string, int, HashString> d_kindMapByName;
00089
00090
00091 PrettyPrinter *d_prettyPrinter;
00092
00093 size_t hash(const ExprValue* ev) const;
00094
00095
00096
00097
00098
00099 const int* d_printDepth;
00100
00101 const bool* d_withIndentation;
00102
00103 int d_indent;
00104
00105
00106
00107 int d_indentTransient;
00108
00109 const int* d_lineWidth;
00110
00111 const std::string* d_inputLang;
00112
00113 const std::string* d_outputLang;
00114
00115 const bool* d_dagPrinting;
00116
00117 const std::string d_mmFlag;
00118
00119
00120 class HashEV {
00121 ExprManager* d_em;
00122 public:
00123 HashEV(ExprManager* em): d_em(em) { }
00124 size_t operator()(ExprValue* ev) const { return d_em->hash(ev); }
00125 };
00126
00127 class EqEV {
00128 public:
00129 bool operator()(const ExprValue* ev1, const ExprValue* ev2) const;
00130 };
00131
00132
00133 typedef std::hash_set<ExprValue*, HashEV, EqEV> ExprValueSet;
00134
00135 ExprValueSet d_exprSet;
00136
00137 std::vector<MemoryManager*> d_mm;
00138
00139
00140 std::hash<void*> d_pointerHash;
00141
00142
00143 Expr d_bool;
00144 Expr d_false;
00145 Expr d_true;
00146
00147 std::vector<Expr> d_emptyVec;
00148
00149 Expr d_nullExpr;
00150
00151 void installExprValue(ExprValue* ev);
00152
00153
00154
00155
00156
00157 unsigned d_simpCacheTagCurrent;
00158
00159
00160
00161
00162
00163 bool d_disableGC;
00164
00165
00166
00167
00168
00169
00170
00171
00172 bool d_postponeGC;
00173
00174 std::vector<ExprValue*> d_postponed;
00175
00176 ExprHashMap<Expr> d_rebuildCache;
00177 IF_DEBUG(bool d_inRebuild);
00178
00179 public:
00180
00181 class TypeComputer {
00182 public:
00183 TypeComputer() {}
00184 virtual ~TypeComputer() {}
00185
00186 virtual void computeType(const Expr& e) = 0;
00187
00188 virtual void checkType(const Expr& e) = 0;
00189 };
00190 private:
00191
00192 TypeComputer* d_typeComputer;
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 Expr rebuildRec(const Expr& e);
00203
00204
00205 ExprValue* newExprValue(ExprValue* ev);
00206
00207
00208 unsigned getFlag() { return d_flagCounter; }
00209
00210 unsigned nextFlag() { return ++d_flagCounter; }
00211
00212
00213 void computeType(const Expr& e);
00214
00215 void checkType(const Expr& e);
00216
00217 public:
00218
00219 ExprManager(ContextManager* cm, const CLFlags& flags);
00220
00221 ~ExprManager();
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 void clear();
00233
00234 bool isActive();
00235
00236
00237
00238 void gc(ExprValue* ev);
00239
00240
00241 void postponeGC() { d_postponeGC = true; }
00242
00243
00244 void resumeGC();
00245
00246
00247
00248 Expr rebuild(const Expr& e);
00249
00250
00251
00252 ExprIndex nextIndex() { return d_index++; }
00253 ExprIndex lastIndex() { return d_index - 1; }
00254
00255
00256 void clearFlags() { nextFlag(); }
00257
00258
00259
00260 const Expr& boolExpr() { return d_bool; }
00261
00262 const Expr& falseExpr() { return d_false; }
00263
00264 const Expr& trueExpr() { return d_true; }
00265
00266 const std::vector<Expr>& getEmptyVector() { return d_emptyVec; }
00267
00268 const Expr& getNullExpr() { return d_nullExpr; }
00269
00270
00271
00272
00273 Expr newExpr(ExprValue* ev) { return Expr(newExprValue(ev)); }
00274
00275 Expr newLeafExpr(const Op& op);
00276 Expr newStringExpr(const std::string &s);
00277 Expr newRatExpr(const Rational& r);
00278 Expr newSkolemExpr(const Expr& e, int i);
00279 Expr newVarExpr(const std::string &s);
00280 Expr newSymbolExpr(const std::string &s, int kind);
00281 Expr newBoundVarExpr(const std::string &name, const std::string& uid);
00282 Expr newBoundVarExpr(const std::string &name, const std::string& uid,
00283 const Type& type);
00284 Expr newBoundVarExpr(const Type& type);
00285 Expr newClosureExpr(int kind, const std::vector<Expr>& vars,
00286 const Expr& body);
00287
00288
00289 Expr andExpr(const std::vector <Expr>& children)
00290 { return Expr(AND, children, this); }
00291 Expr orExpr(const std::vector <Expr>& children)
00292 { return Expr(OR, children, this); }
00293
00294
00295
00296
00297 size_t hash(const Expr& e) const;
00298
00299 ContextManager* getCM() const { return d_cm; }
00300
00301 Context* getCurrentContext() const { return d_cm->getCurrentContext(); }
00302
00303 int scopelevel() { return d_cm->scopeLevel(); }
00304
00305
00306 MemoryManager* getMM(size_t MMIndex) {
00307 DebugAssert(MMIndex < d_mm.size(), "ExprManager::getMM()");
00308 return d_mm[MMIndex];
00309 }
00310
00311 unsigned getSimpCacheTag() const { return d_simpCacheTagCurrent; }
00312
00313 void invalidateSimpCache() { d_simpCacheTagCurrent++; }
00314
00315
00316 void registerTypeComputer(TypeComputer* typeComputer)
00317 { d_typeComputer = typeComputer; }
00318
00319
00320 int printDepth() const { return *d_printDepth; }
00321
00322 bool withIndentation() const { return *d_withIndentation; }
00323
00324 int lineWidth() const { return *d_lineWidth; }
00325
00326 int indent() const { return d_indentTransient; }
00327
00328 int indent(int n, bool permanent = false);
00329
00330
00331
00332 int incIndent(int n, bool permanent = false);
00333
00334 void restoreIndent() { d_indentTransient = d_indent; }
00335
00336 InputLanguage getInputLang() const;
00337
00338 InputLanguage getOutputLang() const;
00339
00340 bool dagPrinting() const { return *d_dagPrinting; }
00341
00342
00343 PrettyPrinter* getPrinter() const { return d_prettyPrinter; }
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355 void newKind(int kind, const std::string &name, bool isType = false);
00356
00357
00358
00359 void registerPrettyPrinter(PrettyPrinter& printer);
00360
00361 void unregisterPrettyPrinter();
00362
00363
00364 bool isKindRegistered(int kind) { return d_kindMap.count(kind) > 0; }
00365
00366 bool isTypeKind(int kind) { return d_typeKinds.count(kind) > 0; }
00367
00368
00369
00370 const std::string& getKindName(int kind);
00371
00372 int getKind(const std::string& name);
00373
00374
00375
00376
00377
00378
00379 size_t registerSubclass(size_t sizeOfSubclass);
00380
00381 };
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398 class ExprManagerNotifyObj: public ContextNotifyObj {
00399 ExprManager* d_em;
00400 public:
00401
00402 ExprManagerNotifyObj(ExprManager* em, Context* cxt)
00403 : ContextNotifyObj(cxt), d_em(em) { }
00404
00405 void notifyPre(void);
00406 void notify(void);
00407 };
00408
00409
00410 }
00411
00412
00413 #include "expr_value.h"
00414
00415 namespace CVCL {
00416
00417 inline Expr ExprManager::newLeafExpr(const Op& op)
00418 {
00419 if (op.getExpr().isNull()) {
00420 ExprValue ev(this, op.getKind());
00421 return newExpr(&ev);
00422 }
00423 else {
00424 DebugAssert(op.getExpr().getEM() == this, "ExprManager mismatch");
00425 std::vector<Expr> kids;
00426 ExprApply ev(this, op, kids);
00427 return newExpr(&ev);
00428 }
00429 }
00430
00431 inline Expr ExprManager::newStringExpr(const std::string &s)
00432 { ExprString ev(this, s); return newExpr(&ev); }
00433
00434 inline Expr ExprManager::newRatExpr(const Rational& r)
00435 { ExprRational ev(this, r); return newExpr(&ev); }
00436
00437 inline Expr ExprManager::newSkolemExpr(const Expr& e, int i)
00438 { DebugAssert(e.getEM() == this, "ExprManager mismatch");
00439 ExprSkolem ev(this, i, e); return newExpr(&ev); }
00440
00441 inline Expr ExprManager::newVarExpr(const std::string &s)
00442 { ExprVar ev(this, s); return newExpr(&ev); }
00443
00444 inline Expr ExprManager::newSymbolExpr(const std::string &s, int kind)
00445 { ExprSymbol ev(this, kind, s); return newExpr(&ev); }
00446
00447 inline Expr ExprManager::newBoundVarExpr(const std::string &name,
00448 const std::string& uid)
00449 { ExprBoundVar ev(this, name, uid); return newExpr(&ev); }
00450
00451 inline Expr ExprManager::newBoundVarExpr(const std::string& name,
00452 const std::string& uid,
00453 const Type& type) {
00454 Expr res = newBoundVarExpr(name, uid);
00455 DebugAssert(type.getExpr().getKind() != ARROW,"");
00456 DebugAssert(res.lookupType().isNull(),
00457 "newBoundVarExpr: redefining a variable " + name);
00458 res.setType(type);
00459 return res;
00460 }
00461
00462 inline Expr ExprManager::newBoundVarExpr(const Type& type) {
00463 static int nextNum = 0;
00464 std::string name("_cvcl_");
00465 std::string uid = int2string(nextNum++);
00466 return newBoundVarExpr(name, uid, type);
00467 }
00468
00469 inline Expr ExprManager::newClosureExpr(int kind,
00470 const std::vector<Expr>& vars,
00471 const Expr& body)
00472 { ExprClosure ev(this, kind, vars, body); return newExpr(&ev); }
00473
00474 inline size_t ExprManager::hash(const ExprValue* ev) const {
00475 DebugAssert(ev!=NULL, "ExprManager::hash() called on a NULL ExprValue");
00476 return ev->hash();
00477 }
00478
00479 inline bool ExprManager::EqEV::operator()(const ExprValue* ev1,
00480 const ExprValue* ev2) const {
00481 return (*ev1) == (*ev2);
00482 }
00483
00484 inline size_t ExprManager::hash(const Expr& e) const {
00485 DebugAssert(!e.isNull(), "ExprManager::hash() called on a Null Expr");
00486 return e.d_expr->hash();
00487 }
00488
00489 }
00490
00491 #endif
00492