OpenADFortTk (basic)
src/xaif2whirl/XAIF_DOMFilters.cxx
Go to the documentation of this file.
00001 // -*-Mode: C++;-*-
00002 // $Header: /Volumes/cvsrep/developer/OpenADFortTk/src/xaif2whirl/XAIF_DOMFilters.cxx,v 1.19 2006/05/12 16:12:23 utke Exp $
00003 
00004 #include <iostream>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 
00008 #include "xercesc/dom/DOMNode.hpp"
00009 #include "xercesc/dom/DOMNodeList.hpp"
00010 #include "xercesc/dom/DOMElement.hpp"
00011 #include "xercesc/dom/DOMAttr.hpp"
00012 #include "xercesc/dom/DOMNamedNodeMap.hpp"
00013 
00014 #include "xercesc/util/XMLString.hpp"
00015 
00016 #include "XAIFStrings.h"
00017 
00018 #include "XAIF_DOMFilters.h"
00019 #include "XercesStrX.h"
00020 
00021 namespace xaif2whirl { 
00022 
00023   static void 
00024   XercesPrintNode(std::ostream& os, const DOMNode* n, int iter);
00025 
00026   static void 
00027   XercesPrintTree(std::ostream& os, const DOMNode* n, int ilevel);
00028 
00029 
00030   void 
00031   XercesPrintNode(std::ostream& os, const DOMNode* n)
00032   {
00033     // Iteration count starts at 0
00034     XercesPrintNode(os, n, 0);
00035   }
00036 
00037 
00038   void 
00039   XercesPrintTree(std::ostream& os, const DOMNode* n)
00040   {
00041     // Indentation level starts at 0
00042     XercesPrintTree(os, n, 0);
00043   }
00044 
00045 
00046   std::ostream& 
00047   operator<<(std::ostream& os, const DOMElement& elem)
00048   { 
00049     XercesPrintNode(os, &elem);
00050     return os;
00051   }
00052 
00053 
00054   void 
00055   XercesDumpNode(const DOMNode* n)
00056   {
00057     XercesPrintNode(std::cout, n);
00058   }
00059 
00060 
00061   void 
00062   XercesDumpTree(const DOMNode* n)
00063   {
00064     XercesPrintTree(std::cout, n);
00065   }
00066 
00067 
00068   void 
00069   XercesDumpNode(void* n) // For *(#% debuggers
00070   {
00071     XercesDumpNode((DOMNode*)n);
00072   }
00073 
00074 
00075   void 
00076   XercesDumpTree(void* n) // For *(#% debuggers
00077   {
00078     XercesDumpTree((DOMNode*)n);
00079   }
00080 
00081 
00082   static void 
00083   XercesPrintNode(std::ostream& os, const DOMNode* n, int iter)
00084   {
00085     if (!n) { return; }
00086 
00087     // Depending on iteration, start or continue the line
00088     const char* prefix = (iter == 0) ? "<" : " ";
00089     os << prefix;
00090 
00091     // Depending on node type, print different things
00092     if (n->getNodeType() == DOMNode::ATTRIBUTE_NODE) {
00093       const DOMAttr* attr = dynamic_cast<const DOMAttr*>(n);
00094       const XMLCh* nm = attr->getName();
00095       const XMLCh* val = attr->getValue();
00096       os << XercesStrX(nm) << "='" << XercesStrX(val) << "'";
00097     } else {
00098       const XMLCh* nm = n->getNodeName();
00099       os << XercesStrX(nm);
00100     }
00101 
00102     // Recur on certain nodes
00103     DOMNamedNodeMap* attrs = n->getAttributes();
00104     if (attrs) {
00105       for (XMLSize_t i = 0; i < attrs->getLength(); ++i) {
00106         DOMNode* attr = attrs->item(i);
00107         XercesPrintNode(os, attr, iter + 1);
00108       }
00109     }
00110 
00111     // End the line, if necessary
00112     if (iter == 0) { 
00113       os << ">" << std::endl;
00114     }
00115   }
00116 
00117 
00118   static void 
00119   XercesPrintTree(std::ostream& os, const DOMNode* n, int ilevel)
00120   {
00121     if (!n) { return; }
00122 
00123     // Dump the current node with indentation
00124     for (int i = ilevel; i > 0; --i) { os << "  "; }
00125     XercesPrintNode(os, n);
00126 
00127     // Dump all children at the next indentation level
00128     DOMNodeList* children = n->getChildNodes();
00129     if (children) {
00130       for (XMLSize_t i = 0; i < children->getLength(); ++i) {
00131         DOMNode* child = children->item(i);
00132         if (child->getNodeType() == DOMNode::ELEMENT_NODE) { 
00133           XercesPrintTree(os, child, ilevel + 1);
00134         }
00135       }
00136     }
00137   }
00138 
00139 
00140   DOMElement*
00141   GetFirstChildElement(const DOMNode* n)
00142   {
00143     if (!n) { return NULL; }
00144 
00145     DOMNodeList* children = n->getChildNodes();
00146     if (children) {
00147       for (XMLSize_t i = 0; i < children->getLength(); ++i) {
00148         DOMNode* child = children->item(i);
00149         if (child->getNodeType() == DOMNode::ELEMENT_NODE) {
00150           return dynamic_cast<DOMElement*>(child);
00151         }
00152       }
00153     }
00154     return NULL;
00155   }
00156 
00157 
00158   DOMElement*
00159   GetLastChildElement(const DOMNode* n)
00160   {
00161     if (!n) { return NULL; }
00162 
00163     DOMNodeList* children = n->getChildNodes();
00164     if (children) {
00165       for (int i = ((int)children->getLength() - 1); i >= 0; --i) {
00166         DOMNode* child = children->item(i);
00167         if (child->getNodeType() == DOMNode::ELEMENT_NODE) {
00168           return dynamic_cast<DOMElement*>(child);
00169         }
00170       }
00171     }
00172     return NULL;
00173   }
00174 
00175 
00176   DOMElement*
00177   GetChildElement(const DOMNode* n, const XMLCh* name)
00178   {
00179     XAIF_ElemFilter filter(name);
00180     DOMElement* e = GetChildElement(n, &filter);
00181     return e;
00182   }
00183 
00184 
00185   DOMElement*
00186   GetChildElement(const DOMNode* n, const DOMNodeFilter* filter)
00187   {
00188     if (!n) { return NULL; }
00189   
00190     DOMNodeList* children = n->getChildNodes();
00191     if (children) {
00192       for (XMLSize_t i = 0; i < children->getLength(); ++i) {
00193         DOMNode* child = children->item(i);
00194         if (child->getNodeType() == DOMNode::ELEMENT_NODE &&
00195             filter->acceptNode(child) == DOMNodeFilter::FILTER_ACCEPT) {
00196           return dynamic_cast<DOMElement*>(child);
00197         }
00198       }
00199     }
00200     return NULL;
00201   }
00202 
00203 
00204   unsigned int
00205   GetChildElementCount(const DOMNode* n)
00206   {
00207     unsigned int cnt = 0;
00208     if (!n) { return cnt; }
00209 
00210     DOMNodeList* children = n->getChildNodes();
00211     if (children) {
00212       for (XMLSize_t i = 0; i < children->getLength(); ++i) {
00213         DOMNode* child = children->item(i);
00214         if (child->getNodeType() == DOMNode::ELEMENT_NODE) { ++cnt; }
00215       }
00216     }
00217     return cnt;
00218   }
00219 
00220 
00221   DOMElement*
00222   GetPrevSiblingElement(const DOMNode* n)
00223   {
00224     if (!n) { return NULL; }
00225 
00226     const DOMNode* node = n;
00227     while ( (node = node->getPreviousSibling()) ) {
00228       if (node->getNodeType() == DOMNode::ELEMENT_NODE) {
00229         return dynamic_cast<DOMElement*>(const_cast<DOMNode*>(node));
00230       }
00231     }
00232     return NULL;
00233   }
00234 
00235 
00236   DOMElement*
00237   GetNextSiblingElement(const DOMNode* n)
00238   {
00239     if (!n) { return NULL; }
00240 
00241     const DOMNode* node = n;
00242     while ( (node = node->getNextSibling()) ) {
00243       if (node->getNodeType() == DOMNode::ELEMENT_NODE) {
00244         return dynamic_cast<DOMElement*>(const_cast<DOMNode*>(node));
00245       }
00246     }
00247     return NULL;
00248   }
00249 
00250 
00251   DOMElement*
00252   GetNextSiblingElement(const DOMNode* n, const XMLCh* name)
00253   {
00254     XAIF_ElemFilter filter(name);
00255     DOMElement* e = GetNextSiblingElement(n, &filter);
00256     return e;
00257   }
00258 
00259 
00260   DOMElement*
00261   GetNextSiblingElement(const DOMNode* n, const DOMNodeFilter* filter)
00262   {
00263     if (!n) { return NULL; }
00264   
00265     const DOMNode* node = n;
00266     while ( (node = GetNextSiblingElement(node)) ) { // node must be a DOMElement
00267       if (filter->acceptNode(node) == DOMNodeFilter::FILTER_ACCEPT) {
00268         return dynamic_cast<DOMElement*>(const_cast<DOMNode*>(node));
00269       }
00270     }
00271     return NULL;
00272   }
00273 
00274 
00275   // ****************************************************************************
00276 
00277   short
00278   XAIF_ElemFilter::acceptNode(const DOMNode *node) const
00279   {
00280     const XMLCh* name = node->getNodeName();
00281     if ( (node->getNodeType() == DOMNode::ELEMENT_NODE)
00282          && XMLString::equals(name, mName) ) {
00283       return FILTER_ACCEPT;
00284     }
00285     return FILTER_SKIP;
00286   }
00287 
00288   // ****************************************************************************
00289 
00290   short
00291   XAIF_ScopeElemFilter::acceptNode(const DOMNode *node) const
00292   {
00293     const XMLCh* name = node->getNodeName();
00294     if ( (node->getNodeType() == DOMNode::ELEMENT_NODE)
00295          && XMLString::equals(name, XAIFStrings.elem_Scope_x()) ) {
00296       return FILTER_ACCEPT;
00297     }
00298     return FILTER_SKIP;
00299   }
00300 
00301   // ****************************************************************************
00302 
00303   short
00304   XAIF_SymbolElemFilter::acceptNode(const DOMNode *node) const
00305   {
00306     const XMLCh* name = node->getNodeName();
00307     if ( (node->getNodeType() == DOMNode::ELEMENT_NODE)
00308          && XMLString::equals(name, XAIFStrings.elem_Symbol_x()) ) {
00309       return FILTER_ACCEPT;
00310     }
00311     return FILTER_SKIP;
00312   }
00313 
00314   // ****************************************************************************
00315 
00316   short
00317   XAIF_DimensionBoundsElemFilter::acceptNode(const DOMNode *node) const
00318   {
00319     const XMLCh* name = node->getNodeName();
00320     if ( (node->getNodeType() == DOMNode::ELEMENT_NODE)
00321          && XMLString::equals(name, XAIFStrings.elem_DimensionBounds_x()) ) {
00322       return FILTER_ACCEPT;
00323     }
00324     return FILTER_SKIP;
00325   }
00326 
00327 
00328   // ****************************************************************************
00329 
00330   short
00331   XAIF_CFGElemFilter::acceptNode(const DOMNode *node) const
00332   {
00333     bool ans = (node->getNodeType() == DOMNode::ELEMENT_NODE);
00334   
00335     if (cfgOrReplaceList) {
00336       ans &= (IsCFG(node) || IsReplaceList(node));
00337     } else {
00338       ans &= IsReplacement(node);
00339     }
00340     return (ans) ? FILTER_ACCEPT : FILTER_SKIP;
00341   }
00342 
00343 
00344   bool 
00345   XAIF_CFGElemFilter::IsCFG(const DOMNode *node)
00346   {
00347     const XMLCh* name = node->getNodeName();
00348     return (XMLString::equals(name, XAIFStrings.elem_CFG_x()));
00349   }
00350 
00351 
00352   bool 
00353   XAIF_CFGElemFilter::IsReplaceList(const DOMNode *node)
00354   {
00355     const XMLCh* name = node->getNodeName();
00356     return (XMLString::equals(name, XAIFStrings.elem_ReplaceList_x()));
00357   }
00358 
00359 
00360   bool 
00361   XAIF_CFGElemFilter::IsReplacement(const DOMNode *node)
00362   {
00363     const XMLCh* name = node->getNodeName();
00364     return (XMLString::equals(name, XAIFStrings.elem_Replacement_x()));
00365   }
00366 
00367   // ****************************************************************************
00368 
00369   short
00370   XAIF_BBElemFilter::acceptNode(const DOMNode *node) const
00371   {
00372     bool ans = (node->getNodeType() == DOMNode::ELEMENT_NODE);
00373   
00374     if (includeEdges) {
00375       ans &= (IsAnyBB(node) || IsEdge(node));
00376     } else {
00377       ans &= IsAnyBB(node);
00378     }
00379     return (ans) ? FILTER_ACCEPT : FILTER_SKIP;
00380   }
00381 
00382 
00383   bool 
00384   XAIF_BBElemFilter::IsAnyBB(const DOMNode *node)
00385   {
00386     const XMLCh* name = node->getNodeName();
00387     return (IsBBEntry(node) 
00388             || IsBBExit(node) 
00389             || IsBB(node)
00390             || IsBBBranch(node) 
00391             || IsBBEndBr(node)
00392             || IsBBForLoop(node) 
00393             || IsBBPreLoop(node) 
00394             || IsBBPostLoop(node)
00395             || IsBBEndLoop(node));
00396   }
00397 
00398   bool 
00399   XAIF_BBElemFilter::IsBBEntry(const DOMNode *node)
00400   {
00401     const XMLCh* name = node->getNodeName();
00402     return (XMLString::equals(name, XAIFStrings.elem_BBEntry_x()));
00403   }
00404 
00405 
00406   bool 
00407   XAIF_BBElemFilter::IsBBExit(const DOMNode *node)
00408   {
00409     const XMLCh* name = node->getNodeName();
00410     return (XMLString::equals(name, XAIFStrings.elem_BBExit_x()));
00411   }
00412 
00413 
00414   bool 
00415   XAIF_BBElemFilter::IsBB(const DOMNode *node)
00416   {
00417     const XMLCh* name = node->getNodeName();
00418     return (XMLString::equals(name, XAIFStrings.elem_BB_x()));
00419   }
00420 
00421 
00422   bool 
00423   XAIF_BBElemFilter::IsBBBranch(const DOMNode *node)
00424   {
00425     const XMLCh* name = node->getNodeName();
00426     return (XMLString::equals(name, XAIFStrings.elem_BBBranch_x()));
00427   }
00428 
00429 
00430   bool 
00431   XAIF_BBElemFilter::IsBBEndBr(const DOMNode *node)
00432   {
00433     const XMLCh* name = node->getNodeName();
00434     return (XMLString::equals(name, XAIFStrings.elem_BBEndBranch_x()));
00435   }
00436 
00437 
00438   bool 
00439   XAIF_BBElemFilter::IsBBForLoop(const DOMNode *node)
00440   {
00441     const XMLCh* name = node->getNodeName();
00442     return (XMLString::equals(name, XAIFStrings.elem_BBForLoop_x()));
00443   }
00444 
00445 
00446   bool 
00447   XAIF_BBElemFilter::IsBBPreLoop(const DOMNode *node)
00448   {
00449     const XMLCh* name = node->getNodeName();
00450     return (XMLString::equals(name, XAIFStrings.elem_BBPreLoop_x()));
00451   }
00452 
00453 
00454   bool 
00455   XAIF_BBElemFilter::IsBBPostLoop(const DOMNode *node)
00456   {
00457     const XMLCh* name = node->getNodeName();
00458     return (XMLString::equals(name, XAIFStrings.elem_BBPostLoop_x()));
00459   }
00460 
00461 
00462   bool 
00463   XAIF_BBElemFilter::IsBBEndLoop(const DOMNode *node)
00464   {
00465     const XMLCh* name = node->getNodeName();
00466     return (XMLString::equals(name, XAIFStrings.elem_BBEndLoop_x()));
00467   }
00468 
00469 
00470   bool 
00471   XAIF_BBElemFilter::IsEdge(const DOMNode *node)
00472   {
00473     const XMLCh* name = node->getNodeName();
00474     return (XMLString::equals(name, XAIFStrings.elem_CFGEdge_x()));
00475   }
00476 
00477 
00478   // ****************************************************************************
00479 
00480   short
00481   XAIF_BBStmtElemFilter::acceptNode(const DOMNode *node) const
00482   {
00483     if ( (node->getNodeType() == DOMNode::ELEMENT_NODE) && IsAnyStmt(node) ) {
00484       return FILTER_ACCEPT;
00485     }
00486     return FILTER_SKIP;
00487   }
00488 
00489 
00490   bool 
00491   XAIF_BBStmtElemFilter::IsAnyStmt(const DOMNode *node)
00492   {
00493     return (IsAssign(node) || IsSubCall(node) || IsInlinableSubCall(node)
00494             || IsMarker(node) || IsDerivProp(node));
00495   }
00496 
00497 
00498   bool 
00499   XAIF_BBStmtElemFilter::IsAssign(const DOMNode *node)
00500   {
00501     const XMLCh* name = node->getNodeName();
00502     return (XMLString::equals(name, XAIFStrings.elem_Assign_x()));
00503   }
00504 
00505 
00506   bool 
00507   XAIF_BBStmtElemFilter::IsSubCall(const DOMNode *node)
00508   {
00509     const XMLCh* name = node->getNodeName();
00510     return (XMLString::equals(name, XAIFStrings.elem_SubCall_x()));
00511   }
00512 
00513 
00514   bool 
00515   XAIF_BBStmtElemFilter::IsInlinableSubCall(const DOMNode *node)
00516   {
00517     const XMLCh* name = node->getNodeName();
00518     return (XMLString::equals(name, XAIFStrings.elem_InlinableSubCall_x()));
00519   }
00520 
00521 
00522   bool 
00523   XAIF_BBStmtElemFilter::IsMarker(const DOMNode *node)
00524   {
00525     const XMLCh* name = node->getNodeName();
00526     return (XMLString::equals(name, XAIFStrings.elem_Marker_x()));
00527   }
00528 
00529 
00530   bool 
00531   XAIF_BBStmtElemFilter::IsDerivProp(const DOMNode *node)
00532   {
00533     const XMLCh* name = node->getNodeName();
00534     return (XMLString::equals(name, XAIFStrings.elem_DerivProp_x()));
00535   }
00536 
00537 
00538   // ****************************************************************************
00539 
00540   short
00541   XAIF_DerivPropStmt::acceptNode(const DOMNode *node) const
00542   {
00543     if ((node->getNodeType() == DOMNode::ELEMENT_NODE)
00544      && (IsSetDeriv(node) || IsSetNegDeriv(node) || IsIncDeriv(node) || IsDecDeriv(node) || IsSax(node) || IsSaxpy(node) || IsZeroDeriv(node))) {
00545       return FILTER_ACCEPT;
00546     }
00547     return FILTER_SKIP;
00548   }
00549 
00550 
00551   bool 
00552   XAIF_DerivPropStmt::IsSetDeriv(const DOMNode *node)
00553   {
00554     const XMLCh* name = node->getNodeName();
00555     return (XMLString::equals(name, XAIFStrings.elem_SetDeriv_x()));
00556   }
00557 
00558 
00559   bool
00560   XAIF_DerivPropStmt::IsSetNegDeriv(const DOMNode *node)
00561   {
00562     const XMLCh* name = node->getNodeName();
00563     return (XMLString::equals(name, XAIFStrings.elem_SetNegDeriv_x()));
00564   }
00565 
00566 
00567   bool
00568   XAIF_DerivPropStmt::IsIncDeriv(const DOMNode *node)
00569   {
00570     const XMLCh* name = node->getNodeName();
00571     return (XMLString::equals(name, XAIFStrings.elem_IncDeriv_x()));
00572   }
00573 
00574 
00575   bool
00576   XAIF_DerivPropStmt::IsDecDeriv(const DOMNode *node)
00577   {
00578     const XMLCh* name = node->getNodeName();
00579     return (XMLString::equals(name, XAIFStrings.elem_DecDeriv_x()));
00580   }
00581 
00582 
00583   bool 
00584   XAIF_DerivPropStmt::IsSax(const DOMNode *node)
00585   {
00586     const XMLCh* name = node->getNodeName();
00587     return (XMLString::equals(name, XAIFStrings.elem_Sax_x()));
00588   }
00589 
00590 
00591   bool 
00592   XAIF_DerivPropStmt::IsSaxpy(const DOMNode *node)
00593   {
00594     const XMLCh* name = node->getNodeName();
00595     return (XMLString::equals(name, XAIFStrings.elem_Saxpy_x()));
00596   }
00597 
00598 
00599   bool 
00600   XAIF_DerivPropStmt::IsZeroDeriv(const DOMNode *node)
00601   {
00602     const XMLCh* name = node->getNodeName();
00603     return (XMLString::equals(name, XAIFStrings.elem_ZeroDeriv_x()));
00604   }
00605 
00606 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines