~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to languages/cpp/parser/parsesession.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2010-05-05 07:21:55 UTC
  • mfrom: (1.2.3 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505072155-h78lx19pu04sbhtn
Tags: 4:4.0.0-2
* Upload to unstable (Closes: #579947, #481832).
* Acknowledge obsolete NMU fixes (Closes: #562410, #546961).

Show diffs side-by-side

added added

removed removed

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