Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

Val.H

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 // 
00003 // This file is part of Vesta.
00004 // 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 // 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // File: Val.H                                                
00020 
00021 #ifndef Val_H
00022 #define Val_H
00023 
00024 #include "ValExpr.H"
00025 #include "Location.H"
00026 #include "Dep.H"
00027 #include "Prim.H"
00028 #include "Err.H"
00029 #include <Generics.H>
00030 #include <CacheC.H>
00031 #include <PrefixTbl.H>
00032 #include <VestaSource.H>
00033 #include <iostream>
00034 #include <fstream>
00035 
00036 // Convenient constants:
00037 extern Val valTrue, valFalse, valZero, valErr, valUnbnd;
00038 extern Val valTBinding, valTBool, valTClosure, valTErr;
00039 extern Val valTInt, valTList, valTText, valTModel;
00040 extern Context conEmpty, conInitial;
00041 extern Assoc nullAssoc;
00042 
00043 // Case selectors for subclasses of ValC:
00044 enum ValueKind {
00045   BooleanVK, IntegerVK, ListVK, BindingVK, PrimitiveVK,
00046   TextVK, ClosureVK, ModelVK, ErrorVK, FpVK, UnbndVK };
00047 
00049 class ValC {
00050   // We record evaluation dependencies during an evaluation.
00051   // The superclass of all vesta SDL values:
00052 public:
00053   ValueKind vKind;
00054   // Which subclass of ValC `this' belongs to.
00055 
00056   bool tagged;
00057   // Has fingerprint?
00058 
00059   bool cacheit;
00060   // The boolean is true iff no error occurs in creating this value.
00061   // We decided not to cache any value only when it is true.
00062 
00063   int dpsVersion;
00064   // Used to avoid duplicate merge of the dps of this value.
00065 
00066   FP::Tag tag;
00067   // Fingerprint of the value.  We save the fp here for later uses when
00068   // the fp is computed in the first time.
00069 
00070   DPaths* dps;
00071   // Path dependency set.
00072 
00073   DepPath* path;
00074   // Path to structural dependency.
00075 
00076   virtual void PrintD(std::ostream *os, bool verbose = false, int indent = 0) = 0;
00077   // Pretty-print the value on `os'.
00078 
00079   virtual Val Copy(bool more = false) = 0;
00080   // Construct a copy of the value with no dependency.
00081 
00082   virtual FP::Tag FingerPrint() = 0;
00083   // Fingerprinting this value.
00084 
00085   void VError(const Text& message) { Error(message); };
00086   // Print `message' on cerr.
00087 
00088   Val MergeDPS(DPaths* ps);
00089   // Merge in those paths of ps.
00090 
00091   Val Merge(Val val);
00092   // Merge val->dps into this->dps, and then add <val->path, val>.
00093 
00094   Val MergeAndTypeDPS(Val val);
00095   // Merge val->dps into this->dps, and then add <val->path, ValType(val)>.
00096 
00097   Val MergeAndLenDPS(Val val);
00098   // Merge val->dps into this->dps, and then add length dependency.
00099 
00100   Val MergeLenDPS(Val val);
00101   // Merge in those paths of val->lenDps.
00102 
00103   Val AddToDPS(DepPath* dp, Val v, PathKind pk = DummyPK);
00104   // Add a new path to dps.
00105 
00106   Val AddExtendToDPS(DepPath* dp, Val v, PathKind pk, const Text& id);
00107   // Add a new path to dps.
00108 
00109   Val Extend(Val v, const Text& id, PathKind pk = NormPK, bool add = true);
00110   // Create a copy of the value v, using dps and path of this.
00111 
00112   int SizeOfDPS() { return (dps == NULL) ? 0 : dps->Size(); };
00113   // Size of the dependency set.
00114 
00115 protected:
00116   ValC(ValueKind vk)
00117     : vKind(vk), tagged(false), cacheit(true), path(NULL),
00118       dps(NULL), dpsVersion(0) { /*SKIP*/ };
00119   // Construct a new value with empty dependency.
00120 
00121   ValC(const ValC& val)
00122     : vKind(val.vKind), tagged(val.tagged), tag(val.tag), cacheit(val.cacheit),
00123       path(NULL), dps(NULL), dpsVersion(0) { /*SKIP*/ };
00124   // Copy, but do not copy dependency.
00125 };
00126 
00127 // Subclasses of ValC:
00128 class BooleanVC: public ValC {
00129 public:
00130   BooleanVC(bool bb)
00131     : ValC(BooleanVK), b(bb) { /*SKIP*/ };
00132   BooleanVC(const BooleanVC& val)
00133     : ValC(val), b(val.b) { /*SKIP*/ };
00134   void PrintD(std::ostream *os, bool verbose = false, int indent = 0)
00135     { *os << ((b) ? "TRUE" : "FALSE"); };
00136   FP::Tag FingerPrint();
00137   bool b;
00138   Val Copy(bool more) { return NEW_CONSTR(BooleanVC, (*this)); };
00139 };
00140 
00141 class IntegerVC: public ValC {
00142 public:
00143   IntegerVC(Basics::int32 i): ValC(IntegerVK), num(i) { /*SKIP*/ };
00144   IntegerVC(const IntegerVC& val): ValC(val), num(val.num) { /*SKIP*/ };
00145   void PrintD(std::ostream *os, bool verbose = false, int indent = 0)
00146     { *os << num; };
00147   FP::Tag FingerPrint();
00148   Basics::int32 num;
00149   Val Copy(bool more) { return NEW_CONSTR(IntegerVC, (*this)); };
00150 };
00151 
00152 class ListVC: public ValC {
00153 public:
00154   ListVC(const Vals& telems)
00155     : ValC(ListVK), elems(telems), lenDps(NULL) { /*SKIP*/ };
00156   ListVC(const ListVC& val)
00157     : ValC(val), elems(val.elems), lenDps(NULL) { /*SKIP*/ };
00158   void PrintD(std::ostream *os, bool verbose = false, int indent = 0);
00159   FP::Tag FingerPrint();
00160   Vals elems;
00161   DPaths* lenDps;
00162   IntegerVC* Length();
00163   Val GetElem(int index);
00164   Val GetElemNoDpnd(int index);
00165   Val AddToLenDPS(const DepPath& dp, Val val);
00166   Val MergeToLenDPS(DPaths *ps);
00167   Val Copy(bool more);
00168 };
00169 
00170 class BindingVC: public ValC {
00171 public:
00172   BindingVC(): ValC(BindingVK), lenDps(NULL) { /*SKIP*/ };
00173   BindingVC(const Context& telems)
00174     : ValC(BindingVK), lenDps(NULL) { elems = telems; };
00175   BindingVC(Binding b1, Binding b2, bool rec);
00176   BindingVC(const BindingVC& val)
00177     : ValC(val), elems(val.elems), lenDps(NULL) { /*SKIP*/ };
00178   Context elems;
00179   DPaths* lenDps;
00180   void PrintD(std::ostream *os, bool verbose = false, int indent = 0);
00181   FP::Tag FingerPrint();
00182   IntegerVC* Length();
00183   Val Names();
00184   bool Null() { return this->elems.Null(); };
00185   Val Defined(const Text& id);
00186   Val Lookup(const Text& id);  // may return UnbndVC
00187   Val GetElem(const Text& name, int& index);
00188   Val GetElem(int index, Text& name);
00189   bool DefinedNoDpnd(const Text& id);
00190   Val LookupNoDpnd(const Text& id);
00191   Val GetElemNoDpnd(const Text& name, int& index);
00192   Val GetElemNoDpnd(int index, Text& name);
00193   Binding AddBindingAssoc(const Text& name, Val v);
00194   Binding RemoveBindingAssoc(const Text& id);
00195   Val AddToLenDPS(const DepPath& dp, Val val);
00196   Val MergeToLenDPS(DPaths *ps);
00197   Val Copy(bool more);
00198 private:
00199   Context SimpleOverlay(Binding b);
00200   Context RecursiveOverlay(Binding b);
00201 };
00202 
00203 class PrimitiveVC: public ValC {
00204 public:
00205   PrimitiveVC(const Text& tname, PrimExec texec)
00206     : ValC(PrimitiveVK), name(tname), exec(texec) { /*SKIP*/ };
00207   PrimitiveVC(const PrimitiveVC& val)
00208     : ValC(val), name(val.name), exec(val.exec) { /*SKIP*/ };
00209   void PrintD(std::ostream *os, bool verbose = false, int indent = 0)
00210     { *os << name; };
00211   FP::Tag FingerPrint();
00212   Text name;
00213   PrimExec exec;
00214   Val Copy(bool more) { return NEW_CONSTR(PrimitiveVC, (*this)); };
00215 };
00216 
00217 class TextVC: public ValC {
00218 public:
00219   class TextC {
00220   public:
00221     TextC(const Text& ttext)
00222       : txt(ttext), shortId(NullShortId), hasTxt(true), hasName(false)
00223         { /*SKIP*/ };
00224     TextC(const Text& tname, const Text& ttext);
00225     TextC(const Text& tname, std::fstream *iFile, VestaSource *vSource);
00226     TextC(const Text& tname, const ShortId sid)
00227       : name(tname), shortId(sid), hasTxt(false), hasName(true)
00228         { /*SKIP*/ };
00229     Text txt;            // the text
00230     Text name;
00231     ShortId shortId;
00232     bool hasTxt, hasName;
00233   };
00234   TextVC(const Text& ttext): ValC(TextVK) { content = NEW_CONSTR(TextC, (ttext)); };
00235     /* The constructor for a normal text string. */
00236   TextVC(const Text& tname, const Text& ttext, char c, const FP::Tag& fp);
00237     /* The constructor for the derived file generated for the text. */
00238   TextVC(const Text& tname, std::fstream *iFile, VestaSource *vSource);
00239     /* The constructor for the file specified by vSource. */
00240   TextVC(const Text& tname, const ShortId sid, int fp_content);
00241     /* The constructor for the derived file with known sid. */
00242   TextVC(const Text& tname, const ShortId sid, const FP::Tag& fp)
00243     : ValC(TextVK)
00244       { content = NEW_CONSTR(TextC, (tname, sid)); tag = fp; tagged = true; };
00245     /* The constructor for the file with known sid and fp. */
00246   TextVC(const TextVC& val)
00247     : ValC(val), content(val.content) { /*SKIP*/ };
00248   TextC* content;
00249   void PrintD(std::ostream *os, bool verbose = false, int indent = 0);
00250   FP::Tag FingerPrint();
00251   Text NDS();
00252   Text TName();
00253   ShortId Sid();
00254   bool HasTxt() { return content->hasTxt; };
00255   bool HasSid() { return (content->shortId != NullShortId); };
00256   int Length();
00257   Val Copy(bool more) { return NEW_CONSTR(TextVC, (*this)); };
00258 private:
00259   std::istream* Content(bool& closeIt);
00260 };
00261 
00262 class ModelVC: public ValC {
00263 public:
00264   class ModelC {
00265   public:
00266     ModelC(const Text& tlPath)
00267       : name(tlPath), sid(NullShortId), mRoot(NULL), parsed(false)
00268         { /*SKIP*/ };
00269     ModelC(const Text& tname, ShortId tsid, VestaSource *root, 
00270            Expr mod, const Context& cc)
00271       : name(tname), sid(tsid), mRoot(root), model(mod), c(cc),
00272         parsed(true) { /*SKIP*/ };
00273     ModelC(const Text& tname, ShortId tsid, VestaSource *root)
00274       : name(tname), sid(tsid), mRoot(root), parsed(false)
00275         { /*SKIP*/ };
00276     Text name;
00277     VestaSource *mRoot;
00278     Expr model;
00279     Context c;
00280     ShortId sid;
00281     bool parsed;
00282   };
00283   ModelVC(const Text& tname, ShortId tsid, VestaSource *root, 
00284           Expr mod, const Context& cc, VestaSource *vSource);
00285   ModelVC(const Text& tlPath, VestaSource *root, SrcLoc *loc);
00286   ModelVC(const Text& tname, ShortId tsid, VestaSource *root,
00287           const FP::Tag& ttag, const FP::Tag& tlidTag)
00288     : ValC(ModelVK)
00289       { content = NEW_CONSTR(ModelC, (tname, tsid, root));
00290         tag = ttag; tagged = true; lidTag = tlidTag; };
00291   ModelVC(const ModelVC& val)
00292     : ValC(val), content(val.content), lidTag(val.lidTag)
00293       { /*SKIP*/ };
00294   ModelC* content;
00295   void PrintD(std::ostream *os, bool verbose = false, int indent = 0);
00296   FP::Tag lidTag;
00297   FP::Tag FingerPrint() { return this->lidTag; };
00298   FP::Tag FingerPrintFile() { return this->tag; };
00299   ShortId Sid() { return this->content->sid; };
00300   Val Force();
00301   Val Copy(bool more) { return NEW_CONSTR(ModelVC, (*this)); };
00302 };
00303 
00304 class ClosureVC: public ValC {
00305 public:
00306   ClosureVC(FuncEC *tfunc, const Context& c, bool fresh);
00307   ClosureVC(const ClosureVC& val)
00308     : ValC(val), func(val.func), con(val.con),
00309       exprTag(val.exprTag), exprTagged(val.exprTagged) { /*SKIP*/ };
00310   FuncEC *func;
00311   Context con;
00312   bool exprTagged;
00313   FP::Tag exprTag;
00314   void PrintD(std::ostream *os, bool verbose = false, int indent = 0);
00315   FP::Tag FingerPrint();
00316   FP::Tag FingerPrintExpr();
00317   Val Copy(bool more) { return NEW_CONSTR(ClosureVC, (*this)); };
00318 };
00319 
00320 class ErrorVC: public ValC {
00321 public:
00322   ErrorVC();
00323   ErrorVC(bool cacheable)
00324     : ValC(ErrorVK) { this->cacheit = cacheable; };
00325   ErrorVC(const ErrorVC& val): ValC(val) { /*SKIP*/ };
00326   void PrintD(std::ostream *os, bool verbose = false, int indent = 0)
00327     { *os << "ERR"; };
00328   FP::Tag FingerPrint();
00329   Val Copy(bool more) { return NEW_CONSTR(ErrorVC, (*this)); };
00330 };
00331 
00332 class FpVC: public ValC {
00333   // For value we only know its fingerprint -- obtained from cache.
00334   // Never returned by Eval.
00335 public:
00336   FpVC(const FP::Tag& ttag)
00337     : ValC(FpVK) { tag = ttag; tagged = true; };
00338   void PrintD(std::ostream *os, bool verbose = false, int indent = 0)
00339     { *os << "<Value tag>"; };
00340   FP::Tag FingerPrint() { return tag; }
00341   Val Copy(bool more)
00342     { outputMu.lock();
00343       VError("Copy a FpVC?");
00344       outputMu.unlock();
00345       return NEW_CONSTR(FpVC, (tag)); };
00346 };
00347 
00348 class UnbndVC: public ValC {
00349   // Never returned by Eval.
00350 public:
00351   UnbndVC() : ValC(UnbndVK) { /*SKIP*/ };
00352   void PrintD(std::ostream *os, bool verbose = false, int indent = 0)
00353     { *os << "<unbound name>"; };
00354   FP::Tag FingerPrint();
00355   Val Copy(bool more) { return NEW(UnbndVC); };
00356 };
00357 
00358 class AssocVC {
00359   // Note that it is not a subclass of ValC.
00360 public:
00361   AssocVC(const Text& tname, Val tval)
00362     : name(tname), val(tval) { /*SKIP*/ };
00363   void PrintD(std::ostream *os, bool verbose = false, int indent = 0);
00364   FP::Tag FingerPrint();
00365   Atom name;
00366   Val val;
00367 };
00368 
00369 // Type predicates:
00370 bool IsValTrue(Val v);
00371 bool IsValFalse(Val v);
00372 bool IsEmptyBinding(Val v);
00373 bool IsEmptyList(Val v);
00374 
00375 // Return the type of a vesta value.
00376 Val ValType(Val v);
00377 
00378 // Collect dependency for value v in context c.
00379 Val LetDpnd(Val v, const Context& c);
00380 Val FuncDpnd(Val bodyv, Val fv, const Context& c);
00381 Val ModelDpnd(Val bodyv, Val fv, const Context& c);
00382 DPaths* ClosureDpnd(ClosureVC *cl);
00383 void ModelCutOff(Val v);
00384 void ValueDpnd(Val v, DPaths *ps);
00385 
00386 // Print a value:
00387 Text PrintForm(Val v);
00388 
00389 // Print a context:
00390 void PrintContext(std::ostream *os, const Context& c, bool verbose = false,
00391                   int indent = 0);
00392 
00393 // Return the (top) association binding id in context c:
00394 Assoc FindInContext(const Text& id, const Context& c);
00395 
00396 // Return the value of the (top) association binding id in context c:
00397 Val LookupInContext(const Text& id, const Context& c);
00398 
00399 // Lookup a path to get the corresponding value:
00400 Val LookupPath(const FV2::T& path, const Context& c);
00401 Val LookupPath(DepPath *dp, const Context& c);
00402 Val LookupPath(Basics::uint16 idx, PathKind pkind, const PrefixTbl& tbl,
00403                const Context& c, Val *vals);
00404 
00405 // Bind name to the value of elem in cElem and append to cc:
00406 void AppendDToContext(const Text& name, Expr elem, const Context& cElem,
00407                       Context& cc);
00408 
00409 // Return a context like con, limited to the free variables of fv:
00410 Context RestrictContext(const Context& con, Vars fv);
00411 
00412 // Return a context like orig, with a single identifier binding removed
00413 // from the original context (if present):
00414 Context Snip(const Context& orig, const Text& remove, bool& found);
00415 
00416 // Remove multiple bindings from a context:
00417 Context Prune(const Context& orig, const Context& remove);
00418 
00419 // Bind name to the value of elem in cElem and add to in:
00420 void PushToContext(const Text& name, Expr elem, const Context& cElem,
00421                    Context& in);
00422 
00423 // Return c augmented with the bindings created by the statements in assocs:
00424 Context AddStmtAssocs(StmtListEC *assocs, const Context& c);
00425 
00426 // Initialize module:
00427 void ValInit();
00428 
00429 #endif // Val_H

Generated on Mon May 8 00:48:41 2006 for Vesta by  doxygen 1.4.2