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

« back to all changes in this revision

Viewing changes to lib/interfaces/codemodel_utils.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2006-05-23 18:39:42 UTC
  • Revision ID: james.westby@ubuntu.com-20060523183942-hucifbvh68k2bwz7
Tags: upstream-3.3.2
Import upstream version 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of KDevelop
 
2
    Copyright (C) 2003 Roberto Raggi <roberto@kdevelop.org>
 
3
    Copyright (C) 2003 Alexander Dymo <adymo@kdevelop.org>
 
4
    Copyright (C) 2004 Jonas Jacobi <j.jacobi@gmx.de>
 
5
 
 
6
    This library is free software; you can redistribute it and/or
 
7
    modify it under the terms of the GNU Library General Public
 
8
    License as published by the Free Software Foundation; either
 
9
    version 2 of the License, or (at your option) any later version.
 
10
 
 
11
    This library is distributed in the hope that it will be useful,
 
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
    Library General Public License for more details.
 
15
 
 
16
    You should have received a copy of the GNU Library General Public License
 
17
    along with this library; see the file COPYING.LIB.  If not, write to
 
18
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
19
    Boston, MA 02111-1307, USA.
 
20
*/
 
21
 
 
22
#include "codemodel_utils.h"
 
23
 
 
24
namespace CodeModelUtils
 
25
{
 
26
 
 
27
namespace Functions
 
28
{
 
29
 
 
30
void processClasses(FunctionList &list, const ClassDom dom)
 
31
{
 
32
    const ClassList cllist = dom->classList();
 
33
    for (ClassList::ConstIterator it = cllist.begin(); it != cllist.end(); ++it)
 
34
    {
 
35
        processClasses(list, *it);
 
36
    }
 
37
 
 
38
    const FunctionList fnlist = dom->functionList();
 
39
    for (FunctionList::ConstIterator it = fnlist.begin(); it != fnlist.end(); ++it)
 
40
    {
 
41
        list.append(*it);
 
42
    }
 
43
}
 
44
 
 
45
void processNamespaces(FunctionList &list, const NamespaceDom dom)
 
46
{
 
47
    const NamespaceList nslist = dom->namespaceList();
 
48
    for (NamespaceList::ConstIterator it = nslist.begin(); it != nslist.end(); ++it)
 
49
    {
 
50
        processNamespaces(list, *it);
 
51
    }
 
52
    const ClassList cllist = dom->classList();
 
53
    for (ClassList::ConstIterator it = cllist.begin(); it != cllist.end(); ++it)
 
54
    {
 
55
        processClasses(list, *it);
 
56
    }
 
57
 
 
58
    const FunctionList fnlist = dom->functionList();
 
59
    for (FunctionList::ConstIterator it = fnlist.begin(); it != fnlist.end(); ++it)
 
60
    {
 
61
        list.append(*it);
 
62
    }
 
63
}
 
64
 
 
65
void processNamespaces( FunctionList & list, const NamespaceDom dom, QMap< FunctionDom, Scope > & relations )
 
66
{
 
67
    const NamespaceList nslist = dom->namespaceList();
 
68
    for (NamespaceList::ConstIterator it = nslist.begin(); it != nslist.end(); ++it)
 
69
    {
 
70
        processNamespaces(list, *it, relations);
 
71
    }
 
72
    const ClassList cllist = dom->classList();
 
73
    for (ClassList::ConstIterator it = cllist.begin(); it != cllist.end(); ++it)
 
74
    {
 
75
        processClasses(list, *it, relations, dom);
 
76
    }
 
77
 
 
78
    const FunctionList fnlist = dom->functionList();
 
79
    for (FunctionList::ConstIterator it = fnlist.begin(); it != fnlist.end(); ++it)
 
80
    {
 
81
        list.append(*it);
 
82
        relations[*it].ns = dom;
 
83
    }
 
84
}
 
85
 
 
86
void processClasses( FunctionList & list, const ClassDom dom, QMap< FunctionDom, Scope > & relations )
 
87
{
 
88
    const ClassList cllist = dom->classList();
 
89
    for (ClassList::ConstIterator it = cllist.begin(); it != cllist.end(); ++it)
 
90
    {
 
91
        processClasses(list, *it, relations);
 
92
    }
 
93
 
 
94
    const FunctionList fnlist = dom->functionList();
 
95
    for (FunctionList::ConstIterator it = fnlist.begin(); it != fnlist.end(); ++it)
 
96
    {
 
97
        list.append(*it);
 
98
        relations[*it].klass = dom;
 
99
    }
 
100
}
 
101
 
 
102
void processClasses( FunctionList & list, const ClassDom dom, QMap< FunctionDom, Scope > & relations, const NamespaceDom & nsdom )
 
103
{
 
104
    const ClassList cllist = dom->classList();
 
105
    for (ClassList::ConstIterator it = cllist.begin(); it != cllist.end(); ++it)
 
106
    {
 
107
        processClasses(list, *it, relations, nsdom);
 
108
    }
 
109
 
 
110
    const FunctionList fnlist = dom->functionList();
 
111
    for (FunctionList::ConstIterator it = fnlist.begin(); it != fnlist.end(); ++it)
 
112
    {
 
113
        list.append(*it);
 
114
        relations[*it].klass = dom;
 
115
        relations[*it].ns = nsdom;
 
116
    }
 
117
}
 
118
 
 
119
} // end of Functions namespace
 
120
 
 
121
 
 
122
 
 
123
namespace FunctionDefinitions
 
124
{
 
125
 
 
126
void processClasses(FunctionDefinitionList &list, const ClassDom dom)
 
127
{
 
128
    const ClassList cllist = dom->classList();
 
129
    for (ClassList::ConstIterator it = cllist.begin(); it != cllist.end(); ++it)
 
130
    {
 
131
        processClasses(list, *it);
 
132
    }
 
133
 
 
134
    const FunctionDefinitionList fnlist = dom->functionDefinitionList();
 
135
    for (FunctionDefinitionList::ConstIterator it = fnlist.begin(); it != fnlist.end(); ++it)
 
136
    {
 
137
        list.append(*it);
 
138
    }
 
139
}
 
140
 
 
141
void processNamespaces(FunctionDefinitionList &list, const NamespaceDom dom)
 
142
{
 
143
    const NamespaceList nslist = dom->namespaceList();
 
144
    for (NamespaceList::ConstIterator it = nslist.begin(); it != nslist.end(); ++it)
 
145
    {
 
146
        processNamespaces(list, *it);
 
147
    }
 
148
    const ClassList cllist = dom->classList();
 
149
    for (ClassList::ConstIterator it = cllist.begin(); it != cllist.end(); ++it)
 
150
    {
 
151
        processClasses(list, *it);
 
152
    }
 
153
 
 
154
    const FunctionDefinitionList fnlist = dom->functionDefinitionList();
 
155
    for (FunctionDefinitionList::ConstIterator it = fnlist.begin(); it != fnlist.end(); ++it)
 
156
    {
 
157
        list.append(*it);
 
158
    }
 
159
}
 
160
 
 
161
void processNamespaces( FunctionDefinitionList & list, const NamespaceDom dom, QMap< FunctionDefinitionDom, Scope > & relations )
 
162
{
 
163
    const NamespaceList nslist = dom->namespaceList();
 
164
    for (NamespaceList::ConstIterator it = nslist.begin(); it != nslist.end(); ++it)
 
165
    {
 
166
        processNamespaces(list, *it, relations);
 
167
    }
 
168
    const ClassList cllist = dom->classList();
 
169
    for (ClassList::ConstIterator it = cllist.begin(); it != cllist.end(); ++it)
 
170
    {
 
171
        processClasses(list, *it, relations, dom);
 
172
    }
 
173
 
 
174
    const FunctionDefinitionList fnlist = dom->functionDefinitionList();
 
175
    for (FunctionDefinitionList::ConstIterator it = fnlist.begin(); it != fnlist.end(); ++it)
 
176
    {
 
177
        list.append(*it);
 
178
        relations[*it].ns = dom;
 
179
    }
 
180
}
 
181
 
 
182
void processClasses( FunctionDefinitionList & list, const ClassDom dom, QMap< FunctionDefinitionDom, Scope > & relations )
 
183
{
 
184
    const ClassList cllist = dom->classList();
 
185
    for (ClassList::ConstIterator it = cllist.begin(); it != cllist.end(); ++it)
 
186
    {
 
187
        processClasses(list, *it, relations);
 
188
    }
 
189
 
 
190
    const FunctionDefinitionList fnlist = dom->functionDefinitionList();
 
191
    for (FunctionDefinitionList::ConstIterator it = fnlist.begin(); it != fnlist.end(); ++it)
 
192
    {
 
193
        list.append(*it);
 
194
        relations[*it].klass = dom;
 
195
    }
 
196
}
 
197
 
 
198
void processClasses( FunctionDefinitionList & list, const ClassDom dom, QMap< FunctionDefinitionDom, Scope > & relations, const NamespaceDom & nsdom )
 
199
{
 
200
    const ClassList cllist = dom->classList();
 
201
    for (ClassList::ConstIterator it = cllist.begin(); it != cllist.end(); ++it)
 
202
    {
 
203
        processClasses(list, *it, relations, nsdom);
 
204
    }
 
205
 
 
206
    const FunctionDefinitionList fnlist = dom->functionDefinitionList();
 
207
    for (FunctionDefinitionList::ConstIterator it = fnlist.begin(); it != fnlist.end(); ++it)
 
208
    {
 
209
        list.append(*it);
 
210
        relations[*it].klass = dom;
 
211
        relations[*it].ns = nsdom;
 
212
    }
 
213
}
 
214
 
 
215
} // end of FunctionDefinitions namespace
 
216
 
 
217
 
 
218
 
 
219
FunctionList allFunctions(const FileDom &dom)
 
220
{
 
221
    using namespace Functions;
 
222
    FunctionList list;
 
223
 
 
224
    const NamespaceList nslist = dom->namespaceList();
 
225
    for (NamespaceList::ConstIterator it = nslist.begin(); it != nslist.end(); ++it)
 
226
    {
 
227
        processNamespaces(list, *it);
 
228
    }
 
229
 
 
230
    const ClassList cllist = dom->classList();
 
231
    for (ClassList::ConstIterator it = cllist.begin(); it != cllist.end(); ++it)
 
232
    {
 
233
        processClasses(list, *it);
 
234
    }
 
235
 
 
236
    const FunctionList fnlist = dom->functionList();
 
237
    for (FunctionList::ConstIterator it = fnlist.begin(); it != fnlist.end(); ++it)
 
238
    {
 
239
        list.append(*it);
 
240
    }
 
241
 
 
242
    return list;
 
243
}
 
244
 
 
245
AllFunctions allFunctionsDetailed( const FileDom & dom )
 
246
{
 
247
    using namespace Functions;
 
248
    AllFunctions list;
 
249
 
 
250
    const NamespaceList nslist = dom->namespaceList();
 
251
    for (NamespaceList::ConstIterator it = nslist.begin(); it != nslist.end(); ++it)
 
252
    {
 
253
        processNamespaces(list.functionList, *it, list.relations);
 
254
    }
 
255
 
 
256
    const ClassList cllist = dom->classList();
 
257
    for (ClassList::ConstIterator it = cllist.begin(); it != cllist.end(); ++it)
 
258
    {
 
259
        processClasses(list.functionList, *it, list.relations);
 
260
    }
 
261
 
 
262
    const FunctionList fnlist = dom->functionList();
 
263
    for (FunctionList::ConstIterator it = fnlist.begin(); it != fnlist.end(); ++it)
 
264
    {
 
265
        list.functionList.append(*it);
 
266
    }
 
267
 
 
268
    return list;
 
269
}
 
270
 
 
271
AllFunctionDefinitions allFunctionDefinitionsDetailed( const FileDom & dom )
 
272
{
 
273
    using namespace FunctionDefinitions;
 
274
    AllFunctionDefinitions list;
 
275
 
 
276
    const NamespaceList nslist = dom->namespaceList();
 
277
    for (NamespaceList::ConstIterator it = nslist.begin(); it != nslist.end(); ++it)
 
278
    {
 
279
        processNamespaces(list.functionList, *it, list.relations);
 
280
    }
 
281
 
 
282
    const ClassList cllist = dom->classList();
 
283
    for (ClassList::ConstIterator it = cllist.begin(); it != cllist.end(); ++it)
 
284
    {
 
285
        processClasses(list.functionList, *it, list.relations);
 
286
    }
 
287
 
 
288
    const FunctionDefinitionList fnlist = dom->functionDefinitionList();
 
289
    for (FunctionDefinitionList::ConstIterator it = fnlist.begin(); it != fnlist.end(); ++it)
 
290
    {
 
291
        list.functionList.append(*it);
 
292
    }
 
293
 
 
294
    return list;
 
295
}
 
296
 
 
297
bool compareDeclarationToDefinition( const FunctionDom & dec, const FunctionDefinitionDom & def )
 
298
{
 
299
        if (dec->scope() == def->scope() && dec->name() == def->name() && dec->resultType() == def->resultType() && dec->isConstant() == def->isConstant())
 
300
        {
 
301
                const ArgumentList defList = def->argumentList(), decList = dec->argumentList();
 
302
                if (defList.size() != decList.size())
 
303
                        return false;
 
304
                
 
305
                size_t n = defList.size();
 
306
                for(size_t i = 0; i < n; ++i)
 
307
                        if (defList[i]->type() != decList[i]->type())
 
308
                                return false;
 
309
                
 
310
                return true;
 
311
        }
 
312
        return false;
 
313
}
 
314
 
 
315
ClassDom findClassByPosition( NamespaceModel* nameSpace, int line, int col )
 
316
{
 
317
        if (nameSpace == 0)
 
318
                return 0;
 
319
                
 
320
        NamespaceList nsList = nameSpace->namespaceList();
 
321
        for (NamespaceList::iterator i = nsList.begin(); i != nsList.end(); ++i)
 
322
        {
 
323
                ClassDom result = findClassByPosition(*i, line, col);
 
324
                if (result != 0)
 
325
                return result;
 
326
        }
 
327
                
 
328
        ClassList classes = nameSpace->classList();
 
329
        for(ClassList::iterator i = classes.begin(); i != classes.end(); ++i)
 
330
        {
 
331
                ClassDom result = findClassByPosition( *i, line, col );
 
332
                if (result != 0)
 
333
                return result;
 
334
        }
 
335
        
 
336
        return 0;
 
337
}
 
338
 
 
339
ClassDom findClassByPosition( ClassModel* aClass, int line, int col )
 
340
{
 
341
        if (aClass == 0)
 
342
                return 0;
 
343
        
 
344
        ClassList classes = aClass->classList();
 
345
        for(ClassList::iterator i = classes.begin(); i != classes.end(); ++i)
 
346
        {
 
347
                ClassDom result = findClassByPosition( *i, line, col );
 
348
                if (result != 0)
 
349
                return result;
 
350
        }
 
351
                
 
352
        int startLine, startCol;
 
353
        aClass->getStartPosition(&startLine, &startCol);
 
354
        
 
355
        if (startLine <= line)
 
356
        {
 
357
                int endLine, endCol;
 
358
                aClass->getEndPosition(&endLine, &endCol);
 
359
                if (endLine >= line)
 
360
                return (aClass);
 
361
        }
 
362
        
 
363
        return 0;
 
364
}
 
365
 
 
366
int findLastMethodLine( ClassDom aClass, CodeModelItem::Access access )
 
367
{       
 
368
        int point = -1;
 
369
        
 
370
        const FunctionList functionList = aClass->functionList();
 
371
        for( FunctionList::ConstIterator it=functionList.begin(); it!=functionList.end(); ++it )
 
372
        {
 
373
                int funEndLine, funEndColumn;
 
374
                (*it)->getEndPosition( &funEndLine, &funEndColumn );
 
375
                
 
376
                if ((*it)->access() == access && point < funEndLine)
 
377
                point = funEndLine;
 
378
        }
 
379
                
 
380
        return point;
 
381
}
 
382
        
 
383
int findLastVariableLine( ClassDom aClass, CodeModelItem::Access access )
 
384
{
 
385
        int point = -1;
 
386
        
 
387
        const VariableList varList = aClass->variableList();
 
388
        for( VariableList::ConstIterator it= varList.begin(); it!= varList.end(); ++it )
 
389
        {
 
390
                int varEndLine, varEndColumn;
 
391
                (*it)->getEndPosition( &varEndLine, &varEndColumn );
 
392
                
 
393
                if ((*it)->access() == access && point < varEndLine)
 
394
                point = varEndLine;
 
395
        }
 
396
                
 
397
        return point;
 
398
}
 
399
 
 
400
QString accessSpecifierToString( CodeModelItem::Access access )
 
401
{
 
402
  switch(access)
 
403
  {
 
404
        case CodeModelItem::Public: return "public";
 
405
        case CodeModelItem::Protected: return "protected";
 
406
        case CodeModelItem::Private: return "private";
 
407
        default: return "unknown";
 
408
  }
 
409
}
 
410
 
 
411
}//end of namespace CodeModeUtils