1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
/* This file is part of KDevelop
Copyright 2006 Hamish Rodda <rodda@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "parsesession.h"
#include "rpp/pp-location.h"
#include "rpp/pp-environment.h"
#include "lexer.h"
#include "memorypool.h"
#include <language/interfaces/iproblem.h>
#include <language/duchain/indexedstring.h>
#include <language/duchain/declaration.h>
#include <cctype>
#include "ast.h"
#include "dumptree.h"
#include "parentvisitor.h"
ParseSession::ParseSession()
: mempool(new pool)
, token_stream(0)
, m_locationTable(0)
, m_topAstNode(0)
{
}
ParseSession::~ParseSession()
{
delete mempool;
delete token_stream;
delete m_locationTable;
}
TranslationUnitAST * ParseSession::topAstNode(void)
{
return m_topAstNode;
}
void ParseSession::topAstNode(TranslationUnitAST * node)
{
Q_ASSERT(!m_topAstNode);
m_topAstNode = node;
}
void ParseSession::mapAstDuChain (AST * node , KDevelop::DeclarationPointer declaration)
{
//Duplicates shouldn't exist
Q_ASSERT(m_AstToDuchain.find(node) == m_AstToDuchain.end() ||
m_AstToDuchain[node] != declaration);
// NOTE: Don't call declaration->toString() here. It seems that you cannot
// assume at this point that the DUChain is at least locked for reading.
//kDebug() << "Mapping AST node: " << names[node->kind] <<
// "With Declaration: " << declaration->toString();
m_AstToDuchain[node] = declaration;
m_DuchainToAst[declaration] = node;
}
void ParseSession::mapAstUse(AST *node, const SimpleUse& use)
{
//Duplicates shouldn't exist(? Same for uses?)
if(m_AstToUse.contains(node) && m_AstToUse[node] != use)
kWarning() << "Found dupplicate use mapping for node" << node;
m_AstToUse[node] = use;
m_UseToAst[use] = node;
}
void ParseSession::setASTNodeParents()
{
ParentVisitor visitor(this);
visitor.visit(m_topAstNode);
}
void ParseSession::mapAstParent(AST* node, AST* parent)
{
m_AstToParent.insert(node, parent);
}
AST * ParseSession::astNodeFromDeclaration(KDevelop::DeclarationPointer declaration)
{
//declaration was not mapped
if(m_DuchainToAst.find(declaration) == m_DuchainToAst.end())
return 0;
else
return m_DuchainToAst[declaration];
}
AST * ParseSession::astNodeFromDeclaration(KDevelop::Declaration * declaration)
{
return astNodeFromDeclaration(KDevelop::DeclarationPointer(declaration));
}
AST * ParseSession::astNodeFromUse(const SimpleUse &use) const
{
return m_UseToAst.value(use);
}
AST* ParseSession::parentAstNode(AST* node)
{
return m_AstToParent.value(node, 0);
}
KDevelop::DeclarationPointer ParseSession::declarationFromAstNode(AST * node)
{
//declaration was not mapped
if(m_AstToDuchain.find(node) == m_AstToDuchain.end())
return KDevelop::DeclarationPointer();
else
return m_AstToDuchain[node];
}
rpp::Anchor ParseSession::positionAt(std::size_t offset, bool collapseIfMacroExpansion) const
{
Q_ASSERT(m_locationTable);
return m_locationTable->positionAt(offset, m_contents, collapseIfMacroExpansion).first;
}
QPair<rpp::Anchor, uint> ParseSession::positionAndSpaceAt(std::size_t offset, bool collapseIfMacroExpansion) const
{
Q_ASSERT(m_locationTable);
return m_locationTable->positionAt(offset, m_contents, collapseIfMacroExpansion);
}
std::size_t ParseSession::size() const
{
return m_contents.size() + 1;
}
uint* ParseSession::contents()
{
return m_contents.data();
}
const uint* ParseSession::contents() const
{
return m_contents.data();
}
const PreprocessedContents& ParseSession::contentsVector() const
{
return m_contents;
}
void ParseSession::setContents(const PreprocessedContents& contents, rpp::LocationTable* locationTable)
{
m_contents = contents;
m_locationTable = locationTable;
}
void ParseSession::setContentsAndGenerateLocationTable(const PreprocessedContents& contents)
{
m_contents = contents;
///@todo We need this in the lexer, the problem is that we copy the vector when doing this
m_contents.append(0);
m_contents.append(0);
m_contents.append(0);
m_contents.append(0);
m_locationTable = new rpp::LocationTable(m_contents);
}
void ParseSession::setUrl(const KDevelop::IndexedString& url)
{
m_url = url;
}
const KDevelop::IndexedString& ParseSession::url() const
{
return m_url;
}
void ParseSession::dumpNode(AST* node) const
{
DumpTree dumper;
dumper.dump(node, token_stream, true);
}
|