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

Debug.C

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: Debug.C
00020 // Last Modified On Wed Apr 27 10:01:44 EDT 2005 by irina.furman@intel.com 
00021 //      Modified On Wed Feb  9 21:17:53 EST 2005 by ken@xorian.net 
00022 //      Modified On Tue Oct  5 14:45:46 PDT 1999 by yuanyu    
00023 //      modified on Tue Feb 27 15:47:00 PST 1996 by levin
00024 
00025 #include "Debug.H"
00026 #include "Expr.H"
00027 
00028 using std::ostream;
00029 using std::cerr;
00030 using std::endl;
00031 
00032 bool CacheIt(Val v) {
00033   if (!v->cacheit) {
00034     cerr << "Cannot be cached." << endl;
00035     return false;
00036   }
00037   switch (v->vKind) {
00038   case BindingVK:
00039     {
00040       Binding b = (BindingVC*)v;
00041       Context elems = b->elems;
00042       while (!elems.Null()) {
00043         if (!CacheIt(elems.Pop()->val))
00044           return false;
00045       }
00046       break;
00047     }
00048   case ListVK:
00049     {
00050       Vals elems = ((ListVC*)v)->elems;
00051       while (!elems.Null()) {
00052         if (!CacheIt(elems.Pop()))
00053           return false;
00054       }
00055       break;
00056     }
00057   default:
00058     break;
00059   }
00060   return true;
00061 }
00062 
00063 void PrintVars(ostream *os, const Vars& fv) {
00064   Vars vars = fv;
00065 
00066   *os << "< ";
00067   if (!vars.Null())
00068     *os << vars.Pop();
00069   while (!vars.Null())
00070     *os << ", " << vars.Pop();
00071   *os << " >";
00072   os->flush();
00073 }
00074 
00075 void PrintTags(ostream *os, const FP::List& tags) {
00076   *os << "{\n";
00077   for (int i = 0; i < tags.len; i++) {
00078     *os << "  " << i << ". " << tags.fp[i] << endl;
00079   }
00080   *os << "}\n";
00081   return;
00082 }
00083 
00084 void PrintNames(ostream *os, const FV2::List& names) {
00085   *os << "{\n";
00086   for (int i = 0; i < names.len; i++) {
00087     *os << "  " << i << ". " << *names.name[i] << endl;
00088   }
00089   *os << "}\n";
00090   return;
00091 } 
00092 
00093 Val ContextNames(const Context& c) {
00094   Context work = c;
00095 
00096   Vals names;
00097   while (!work.Null()) {
00098     names.Append1D(NEW_CONSTR(TextVC, (work.Pop()->name)));
00099   }
00100   return NEW_CONSTR(ListVC, (names));
00101 }
00102 
00103 void PrintDpnd(ostream *os, Val v) {
00104   int index = 0;
00105   *os << "{ ";
00106   if (v->path) {
00107     *os << index++ << ". <";
00108     v->path->Print(os);
00109     *os << " : ";
00110     v->PrintD(os);
00111     *os << ">";
00112     *os << ";";
00113   }
00114   if (v->SizeOfDPS() != 0) {
00115     DepPathTbl::TIter iter(v->dps);
00116     DepPathTbl::KVPairPtr ptr;    
00117     if (iter.Next(ptr)) {
00118       *os << "\n  " << index++ << ". <";
00119       ptr->key.Print(os);
00120       *os << " : ";
00121       ptr->val->PrintD(os);
00122       *os << ">";
00123       while (iter.Next(ptr)) {
00124         *os << ",\n  " << index++ << ". <";
00125         ptr->key.Print(os);
00126         *os << " : ";
00127         ptr->val->PrintD(os);
00128         *os << ">";
00129       }
00130     }
00131   }
00132   *os << " }\n";
00133 }
00134 
00135 void CollectAllDpnd(Val v, bool isModel, /*OUT*/ DPaths& ps) {
00136   /* Collect all the dependency of v into ps. */
00137 
00138   // collect v->dps:
00139   if (v->SizeOfDPS() == 0)
00140     v->dps = NULL;
00141   else {
00142     DepPathTbl::TIter iter(v->dps);
00143     DepPathTbl::KVPairPtr ptr;
00144     if (isModel) {
00145       while (iter.Next(ptr)) {
00146         if (ptr->key.content->path->getlo() == nameDot)
00147           (void)ps.Put(ptr);
00148       }
00149     }
00150     else {
00151       while (iter.Next(ptr))
00152         (void)ps.Put(ptr);
00153     }
00154   }
00155   DepPath *dp = v->path;
00156   if (dp != NULL) {
00157     // collect v->path:
00158     if (!isModel || (dp->content->path->getlo() == nameDot)) {
00159       DepPathTbl::KVPairPtr pr;
00160       (void)ps.Put(*dp, v, pr);
00161       // assert(v->FingerPrint() == pr->val->FingerPrint());
00162     }
00163     else
00164       v->path = NULL;
00165   }
00166   else {
00167     // collect components of the value (only when no path):
00168     switch (v->vKind) {
00169     case BindingVK:
00170       {
00171         BindingVC *bv = (BindingVC*)v;
00172         if (bv->lenDps != NULL && bv->lenDps->Size() != 0) {
00173           DepPathTbl::TIter iter(bv->lenDps);
00174           DepPathTbl::KVPairPtr ptr;
00175           if (isModel) {
00176             while (iter.Next(ptr)) {
00177               if (ptr->key.content->path->getlo() == nameDot) {
00178                 DepPath lenPath(ptr->key.content->path, BLenPK, ptr->key.content->pathFP);
00179                 Val vNames = ((BindingVC*)ptr->val)->Names();
00180                 (void)ps.Put(lenPath, vNames, ptr);
00181                 // assert(vNames->FingerPrint() == ptr->val->FingerPrint());
00182               }
00183             }
00184           }
00185           else {
00186             while (iter.Next(ptr)) {
00187               DepPath lenPath(ptr->key.content->path, BLenPK, ptr->key.content->pathFP);
00188               Val vNames = ((BindingVC*)ptr->val)->Names();
00189               (void)ps.Put(lenPath, vNames, ptr);
00190               // assert(vNames->FingerPrint() == ptr->val->FingerPrint());
00191             }
00192           }
00193         }
00194         Context work = bv->elems;
00195         while (!work.Null())
00196           CollectAllDpnd(work.Pop()->val, isModel, ps);
00197         break;
00198       }
00199     case ListVK:
00200       {
00201         ListVC *lstv = (ListVC*)v;
00202         if (lstv->lenDps != NULL && lstv->lenDps->Size() != 0) {
00203           DepPathTbl::TIter iter(lstv->lenDps);
00204           DepPathTbl::KVPairPtr ptr;
00205           if (isModel) {
00206             while (iter.Next(ptr)) {
00207               if (ptr->key.content->path->getlo() == nameDot) {
00208                 DepPath lenPath(ptr->key.content->path, LLenPK, 
00209                                 ptr->key.content->pathFP);
00210                 Val vLen = NEW_CONSTR(IntegerVC, 
00211                                       (((ListVC*)ptr->val)->elems.Length()));
00212                 (void)ps.Put(lenPath, vLen, ptr);
00213                 // assert(vLen->FingerPrint() == ptr->val->FingernPrint());
00214               }
00215             }
00216           }
00217           else {
00218             while (iter.Next(ptr)) {
00219               DepPath lenPath(ptr->key.content->path, LLenPK, 
00220                               ptr->key.content->pathFP);
00221               Val vLen = NEW_CONSTR(IntegerVC, 
00222                                     (((ListVC*)ptr->val)->elems.Length()));
00223               (void)ps.Put(lenPath, vLen, ptr);
00224               // assert(vLen->FingerPrint() == ptr->val->FingerPrint());
00225             }
00226           }
00227         }
00228         Vals vv = lstv->elems;
00229         while (!vv.Null())
00230           CollectAllDpnd(vv.Pop(), isModel, ps);
00231         break;
00232       }
00233     case ClosureVK:
00234       {
00235         ClosureVC *cl = (ClosureVC*)v;
00236         Context work = cl->con;
00237         while (!work.Null()) {
00238           Assoc a = work.Pop();
00239           if (cl->func->name != a->name)
00240             CollectAllDpnd(a->val, isModel, ps);
00241         }
00242         break;
00243       }
00244     default:
00245       break;
00246     }
00247   }
00248   return;
00249 }
00250 
00251 void PrintAllDpnd(ostream *os, Val v) {
00252   // Collect dependency:
00253   DPaths *ps = NEW_CONSTR(DPaths, (v->SizeOfDPS()));
00254   CollectAllDpnd(v, false, *ps);
00255   ps->Print(os);
00256 }
00257   
00258 void PrintDpndSize(ostream *os, Val v) {
00259   switch (v->vKind) {
00260   case BindingVK:
00261     {
00262       Binding b = (BindingVC*)v;
00263       Context elems = b->elems;
00264       *os << "[";
00265       *os << v->SizeOfDPS();
00266       *os << ", ";
00267       if (v->path != NULL) *os << "*";
00268       *os << "[";
00269       if (!elems.Null())
00270         PrintDpndSize(os, elems.Pop()->val);
00271       while (!elems.Null()) {
00272         *os << ", ";
00273         PrintDpndSize(os, elems.Pop()->val);
00274       }
00275       *os << "]]";
00276       break;
00277     }
00278   case ListVK:
00279     {
00280       Vals elems = ((ListVC*)v)->elems;
00281       *os << "[";
00282       *os << v->SizeOfDPS();
00283       *os << ", ";
00284       if (v->path != NULL) *os << "*";
00285       *os << "[";
00286       if (!elems.Null())
00287         PrintDpndSize(os, elems.Pop());
00288       while (!elems.Null()) {
00289         *os << ", ";
00290         PrintDpndSize(os, elems.Pop());
00291       }
00292       *os << "]]";
00293       break;
00294     }
00295   default:
00296     *os << v->SizeOfDPS();
00297     break;
00298   }
00299   return;
00300 }
00301 
00302 #ifdef DUMP_VAL
00303 // If you #define DUMP_VAL when compiling this file, an additional
00304 // function will be available:
00305 
00306 void DumpVal(Val v);
00307 
00308 // The DumpVal function isn't called by any part of the evaluator, but
00309 // can be very useful for developers working on the implementation of
00310 // the evaluator.  It dumps out the complete structure of a value,
00311 // including all dependency information to standard output.  Because
00312 // it is not overloaded and you don't have to pass cout to it, it's
00313 // easy to call it from a debugger.
00314 
00315 static Text DumpValIndent(unsigned int nesting)
00316 {
00317   Text result = "";
00318   for(unsigned int i = 0; i < nesting; i++)
00319     result += "    ";
00320   return result;
00321 }
00322 
00323 static const char *DumpValKind(ValueKind k)
00324 {
00325   switch(k)
00326     {
00327     case BooleanVK:
00328       return "bool";
00329     case IntegerVK:
00330       return "int";
00331     case ListVK:
00332       return "list";
00333     case BindingVK:
00334       return "binding";
00335     case PrimitiveVK:
00336       return "prim";
00337     case  TextVK:
00338       return "text";
00339     case ClosureVK:
00340       return "closure";
00341     case ModelVK:
00342       return "model";
00343     case ErrorVK:
00344       return "error";
00345     case FpVK:
00346       return "fp";
00347     case UnbndVK:
00348       return "unbound";
00349     }
00350 }
00351 
00352 static void DumpValInner(Val v, unsigned int nesting = 0)
00353 {
00354   Text indent = DumpValIndent(nesting);
00355   cout << indent << "Val @ " << ((void *) v) << endl
00356        << indent << " type : " << DumpValKind(v->vKind) << endl;
00357   if(v->path)
00358     {
00359       cout << indent << " path @ " << ((void *) v->path) << " : ";
00360       v->path->Print(&cout);
00361       cout << endl;
00362     }
00363 
00364   if (v->SizeOfDPS() != 0)
00365     {
00366       cout << indent << " dps @ " << ((void *) v->dps) << " :" << endl;
00367       DepPathTbl::TIter iter(v->dps);
00368       DepPathTbl::KVPairPtr ptr;
00369       while(iter.Next(ptr))
00370         {
00371           int index = 0;
00372           cout << indent << "  " << index++ << ". <" << flush;
00373           ptr->key.Print(&cout);
00374           cout << flush;
00375           cout << " : " << flush;
00376           ptr->val->PrintD(&cout, false, (nesting*4)+3);
00377           cout << flush;
00378           cout << ">" << endl;
00379         }
00380     }
00381 
00382   switch (v->vKind)
00383     {
00384     case BindingVK:
00385       {
00386         BindingVC *bv = (BindingVC*)v;
00387         if(bv->lenDps && bv->lenDps->Size())
00388           {
00389             cout << indent << " lenDps @ " << ((void *) bv->lenDps) << " :"
00390                  << endl;
00391             DepPathTbl::TIter iter(bv->lenDps);
00392             DepPathTbl::KVPairPtr ptr;
00393             while(iter.Next(ptr))
00394               {
00395                 int index = 0;
00396                 cout << indent << "  " << index++ << ". <";
00397                 ptr->key.Print(&cout);
00398                 cout << " : ";
00399                 ptr->val->PrintD(&cout, false, (nesting*4)+3);
00400                 cout << ">";
00401               }
00402           }
00403 
00404         Context work = bv->elems;
00405         cout << indent << " value : " << endl
00406              << indent << "  [" << endl;
00407         while (!work.Null())
00408           {
00409             Assoc a = work.Pop();
00410             cout << indent << "   " << a->name << " = " << endl;
00411             DumpValInner(a->val, nesting + 1);
00412           }
00413         cout << indent << "  ]" << endl;
00414       }
00415       break;
00416     case ListVK:
00417       {
00418         ListVC *lv = (ListVC*)v;
00419 
00420         if(lv->lenDps && lv->lenDps->Size())
00421           {
00422             cout << indent << " lenDps @ " << ((void *) lv->lenDps) << " :"
00423                  << endl;
00424             DepPathTbl::TIter iter(lv->lenDps);
00425             DepPathTbl::KVPairPtr ptr;
00426             while(iter.Next(ptr))
00427               {
00428                 int index = 0;
00429                 cout << indent << "  " << index++ << ". <";
00430                 ptr->key.Print(&cout);
00431                 cout << " : ";
00432                 ptr->val->PrintD(&cout, false, (nesting*4)+3);
00433                 cout << ">";
00434               }
00435           }
00436 
00437         Vals elems = lv->elems;
00438         cout << indent << " value : " << endl
00439              << indent << "  <" << endl;
00440         while (!elems.Null())
00441           {
00442             DumpValInner(elems.Pop(), nesting + 1);
00443           }
00444       }
00445       break;
00446     case ClosureVK:
00447       {
00448         ClosureVC *cl = (ClosureVC*)v;
00449 
00450         cout << indent << " definition : "
00451              << cl->func->loc->file
00452              << ", line " << cl->func->loc->line
00453              << ", char " << cl->func->loc->character << endl;
00454 
00455         Context work = cl->con;
00456         cout << indent << " context : " << endl
00457              << indent << "  [" << endl;
00458         while (!work.Null())
00459           {
00460             Assoc a = work.Pop();
00461             if (a->name != cl->func->name)
00462               {
00463                 cout << indent << "   " << a->name << " = " << endl;
00464                 DumpValInner(a->val, nesting + 1);
00465               }
00466           }
00467         cout << indent << "  ]" << endl;
00468       }
00469       break;
00470     default:
00471       {
00472         cout << indent << " value : " << endl
00473              << indent << "  ";
00474         v->PrintD(&cout, false, (nesting*4)+2);
00475         cout << endl;
00476       }
00477       break;
00478     }
00479 }
00480 
00481 void DumpVal(Val v)
00482 {
00483   DumpValInner(v);
00484   cout << endl;
00485 }
00486 #endif

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