File indexing completed on 2024-04-21 04:36:14

0001 /* This file is part of kdev-pg-qt
0002    Copyright (C) 2005 Roberto Raggi <roberto@kdevelop.org>
0003    Copyright (C) 2006 Jakob Petsovits <jpetso@gmx.at>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "kdev-pg-default-visitor-bits-gen.h"
0022 #include "kdev-pg.h"
0023 #include <iostream>
0024 
0025 namespace KDevPG
0026 {
0027 
0028 void GenerateDefaultVisitorBitsRule::operator()(QPair<QString,Model::SymbolItem*> const &__it)
0029 {
0030   mNames.clear();
0031   mVariableDeclarations.clear();
0032 
0033   Model::SymbolItem *sym = __it.second;
0034 
0035   bool has_members = false;
0036   HasMemberNodes hms(has_members);
0037   hms(sym);
0038 
0039   #define O(ast) \
0040       out << "void " << name << "::visit" << ast \
0041       << "(" << ast << "Ast *" << (has_members ? "node" : "") \
0042       << ") {" << Qt::endl;
0043       
0044   if(isOperatorSymbol(sym))
0045   {
0046     O("Prefix" + sym->mCapitalizedName)
0047     out << "visitNode(node->first);" << Qt::endl << "}" << Qt::endl << Qt::endl;
0048     O("Postfix" + sym->mCapitalizedName)
0049     out << "visitNode(node->first);" << Qt::endl << "}" << Qt::endl << Qt::endl;
0050     O("Binary" + sym->mCapitalizedName)
0051     out << "visitNode(node->first);" << Qt::endl << "visitNode(node->second); }" << Qt::endl << Qt::endl;
0052     O("Ternary" + sym->mCapitalizedName)
0053     out << "visitNode(node->first);" << Qt::endl << "visitNode(node->second);" << Qt::endl << "visitNode(node->third);";
0054   }
0055   else
0056   {
0057     O(sym->mCapitalizedName)
0058 
0059     World::Environment::iterator it = globalSystem.env.find(sym);
0060     while (it != globalSystem.env.end())
0061       {
0062         Model::EvolveItem *e = (*it);
0063         if (it.key() != sym)
0064           break;
0065 
0066         ++it;
0067 
0068         visitNode(e);
0069       }
0070   }
0071 
0072   out << "}" << Qt::endl << Qt::endl;
0073   
0074   #undef O
0075 }
0076 
0077 void GenerateDefaultVisitorBitsRule::visitVariableDeclaration(Model::VariableDeclarationItem *node)
0078 {
0079   do
0080   {
0081     if (node->mStorageType != Model::VariableDeclarationItem::StorageAstMember)
0082       break;
0083 
0084     if (node->mVariableType != Model::VariableDeclarationItem::TypeNode)
0085       break; // nothing to do
0086 
0087     if (mNames.find(node->mName) != mNames.end())
0088       break;
0089 
0090     QString base_type = node->mCapitalizedType + "Ast*";
0091 
0092     if (node->mIsSequence)
0093       {
0094         out << "if (" << "node->" << node->mName << "Sequence" << ") {"
0095             << "const KDevPG::ListNode<" << base_type << "> *__it = "
0096             << "node->" << node->mName << "Sequence" << "->front()"
0097             << ", *__end = __it;" << Qt::endl
0098             << "do {" << Qt::endl
0099             << "visitNode(__it->element);" << Qt::endl
0100             << "__it = __it->next;" << Qt::endl
0101             << "} while (__it != __end);" << Qt::endl
0102             << "}" << Qt::endl;
0103       }
0104     else
0105       {
0106         out << "visitNode(" << "node->" << node->mName << ")" << ";" << Qt::endl;
0107       }
0108 
0109     mNames.insert(node->mName);
0110     mVariableDeclarations.push_back(node);
0111 
0112   } while(false);
0113 
0114   DefaultVisitor::visitVariableDeclaration(node);
0115 }
0116 
0117 
0118 void HasMemberNodes::operator()(Model::SymbolItem *sym)
0119 {
0120   if(isOperatorSymbol(sym))
0121   {
0122     has_members = true;
0123     return;
0124   }
0125   
0126   has_members = false;
0127 
0128   World::Environment::iterator it = globalSystem.env.find(sym);
0129   while (it != globalSystem.env.end())
0130     {
0131       Model::EvolveItem *e = (*it);
0132       if (it.key() != sym)
0133         break;
0134 
0135       ++it;
0136 
0137       visitNode(e);
0138     }
0139 }
0140 
0141 void HasMemberNodes::visitVariableDeclaration(Model::VariableDeclarationItem *node)
0142 {
0143   if (node->mStorageType == Model::VariableDeclarationItem::StorageAstMember
0144       && node->mVariableType == Model::VariableDeclarationItem::TypeNode)
0145     {
0146       has_members = true;
0147     }
0148 }
0149 
0150 }