File: adlhelper.cpp

package info (click to toggle)
kdevelop 4%3A4.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 18,844 kB
  • sloc: cpp: 91,758; python: 1,095; lex: 422; ruby: 120; sh: 114; xml: 42; makefile: 38
file content (326 lines) | stat: -rw-r--r-- 10,993 bytes parent folder | download | duplicates (2)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
    Copyright (C) 2010 Ciprian Ciubotariu <cheepeero@gmx.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "adlhelper.h"
#include <language/duchain/types/alltypes.h>
#include <language/duchain/classdeclaration.h>
#include "templatedeclaration.h"
#include "typeutils.h"

// uncomment to get debugging info on ADL - very expensive on parsing
//#define DEBUG_ADL

using namespace Cpp;
using namespace KDevelop;

ADLTypeVisitor::ADLTypeVisitor(ADLHelper & helper) : m_helper(helper)
{
}

bool ADLTypeVisitor::preVisit(const AbstractType * type)
{
    // the following types are of no interest to ADL
    switch (type->whichType())
    {
    case AbstractType::TypeAbstract:
    case AbstractType::TypeIntegral:
    case AbstractType::TypeDelayed:
    case AbstractType::TypeUnsure:
        return false;
    default:
        return true;
    };
}

void ADLTypeVisitor::postVisit(const AbstractType *)
{
}

bool ADLTypeVisitor::visit(const AbstractType* type)
{
    return !seen(type);
}

void ADLTypeVisitor::visit(const IntegralType *)
{
}

bool ADLTypeVisitor::visit(const PointerType * type)
{
    return !seen(type);
}

void ADLTypeVisitor::endVisit(const PointerType *)
{
    // traversed by PointerType::accept0
}

bool ADLTypeVisitor::visit(const ReferenceType * type)
{
    return !seen(type);
}

void ADLTypeVisitor::endVisit(const ReferenceType *)
{
    // traversed by ReferenceType::accept0
}

bool ADLTypeVisitor::visit(const FunctionType * type)
{
    if (m_helper.m_templateArgsDepth > 0)
        return false;

    return !seen(type);
}

void ADLTypeVisitor::endVisit(const FunctionType * /*type*/)
{
    // return type and argument types are handled by FunctionType::accept0

    // here we process the namespace of the function name (or containing class), if any

    /* at the bottom of 3.4.2.2 we find the following:

    In addition, if the argument is the name or address of a set of overloaded functions and/or function tem-
    plates, its associated classes and namespaces are the union of those associated with each of the members of
    the set: the namespace in which the function or function template is defined and the classes and namespaces
    associated with its (non-dependent) parameter types and return type.
    */

    if (m_helper.m_possibleFunctionName.data() && m_helper.m_possibleFunctionName.data()->isFunctionDeclaration())
    {
        Declaration * declaration = m_helper.m_possibleFunctionName.data();

#ifdef DEBUG_ADL
        kDebug() << "    function name = " << declaration->toString() << " ; identifier = " << declaration->qualifiedIdentifier().toString();
#endif

        // start going towards the global scope until we match an interesting name
        // note that calling addDeclarationScopeIdentifier does not work because for some reason
        // for function names DUContext::scopeIdentifier returns the function name instead of the
        // name of the function's scope
        DUContext* context = declaration->context();
        while (context) {
            if (Declaration* decl = context->owner())
            {
              if (context->type() == DUContext::Namespace)
              {
                  m_helper.addAssociatedNamespace(decl->qualifiedIdentifier());
                  break;
              } else if (context->type() == DUContext::Class) {
                  m_helper.addAssociatedClass(decl);
                  break;
              }
            }
            context = context->parentContext();
        }
    }
}

bool ADLTypeVisitor::visit(const StructureType * type)
{
    return !seen(type);
}

void ADLTypeVisitor::endVisit(const StructureType * type)
{
    // StructureType does not visit base classes etc
    // so the processing is done by ADLHelper
    m_helper.addAssociatedClass(type->declaration(m_helper.m_topContext.data()));
}

bool ADLTypeVisitor::visit(const ArrayType * type)
{
    return !seen(type);
}

void ADLTypeVisitor::endVisit(const ArrayType *)
{
    // traversed by ArrayType::accept0
}

bool ADLTypeVisitor::seen(const KDevelop::AbstractType* type)
{
    if (m_helper.m_alreadyProcessed.contains(type))
        return true;

    m_helper.m_alreadyProcessed.insert(type);
    return false;
}

ADLHelper::ADLHelper(DUContextPointer context, TopDUContextPointer topContext)
        : m_context(context), m_topContext(topContext),
        m_typeVisitor(*this),
        m_templateArgsDepth(0)
{
}

void ADLHelper::addArgument(const OverloadResolver::Parameter & argument)
{
    m_possibleFunctionName = argument.declaration;
    addArgumentType(argument.type);
}

void ADLHelper::addArgumentType(const AbstractType::Ptr typePtr)
{
    if(m_alreadyProcessed.contains(typePtr.unsafeData()))
      return;
    
    if (typePtr)
    {
#ifdef DEBUG_ADL
        kDebug() << "    added argument type " << typePtr->toString() << " to ADL lookup";
#endif
        // the enumeration and enumerator types are not part of the TypeVisitor interface
        switch (typePtr->whichType())
        {
        case AbstractType::TypeEnumeration:
        {
            EnumerationType* specificType = fastCast<EnumerationType*>(typePtr.unsafeData());
            if (specificType)
            {
                Declaration * enumDecl = specificType->declaration(m_topContext.data());
                addDeclarationScopeIdentifier(enumDecl);
            }
            break;
        }
        case AbstractType::TypeEnumerator:
        {
            if (m_templateArgsDepth == 0)
            {
                EnumeratorType* specificType = fastCast<EnumeratorType*>(typePtr.unsafeData());
                if (specificType)
                {
                    // use the enumeration context for the enumerator value declaration to find out the namespace
                    Declaration * enumeratorDecl = specificType->declaration(m_topContext.data());
                    if (enumeratorDecl) {
                        DUContext * enumContext = enumeratorDecl->context();
                        if (enumContext) {
                            addAssociatedNamespace(enumContext->scopeIdentifier(false));
                        }
                    }
                }
            }
            break;
        }
        default:
            typePtr->accept(&m_typeVisitor);
        }
    }
    
    m_alreadyProcessed.insert(typePtr.unsafeData());
}

QSet< QualifiedIdentifier > ADLHelper::associatedNamespaces() const
{
    return m_associatedNamespaces;
}

void ADLHelper::addAssociatedClass(Declaration * declaration)
{
    if (!declaration || !m_context || !m_topContext)
        return;

    // from the standard:
    // Typedef names and using-declarations used to specify types do not contribute to this set.
    if (declaration->isTypeAlias())
        return;

    if(m_alreadyProcessed.contains(declaration))
      return;
    
    m_alreadyProcessed.insert(declaration);
    
    addDeclarationScopeIdentifier(declaration);

    /*
    Here comes the standard on template ADL, along with my interpretation:

    This works just like for classes or class members:
      If T is a template-id, its associated namespaces and classes are the namespace in which the template is
      defined; for member templates, the member template's class;

    This means that if the template's argument T is a type, treat it just like any other function argument.
    GCC seems to include fully instantiated templates (which are a type indeed) here, so I followed the same
    interpretation.
      the namespaces and classes associated
      with the types of the template arguments provided for template type parameters (excluding template
      template parameters);

    If the template's argument T is a template, take just its namespace, and not associated classes or
    the namespaces associated with its own arguments.
      the namespaces in which any template template arguments are defined; and the
      classes in which any member templates used as template template arguments are defined.

    This means when we are processing template arguments we need to skip numeric template arguments,
    i.e. enumeration values (not types!), function pointers, integral values etc
      [Note: non-
      type template arguments do not contribute to the set of associated namespaces. ]
    */

    TemplateDeclaration * templateDecl = dynamic_cast<TemplateDeclaration*>(declaration);
    bool isFunctionArg = (m_templateArgsDepth == 0);
    bool isTemplateClassArg = (m_templateArgsDepth > 0 && !templateDecl);
    bool isTemplateTemplateArg = (templateDecl ? (templateDecl->instantiatedFrom() == NULL) : false);
    if (isFunctionArg || (isTemplateClassArg && !isTemplateTemplateArg))
        addBaseClasses(declaration);

    if (templateDecl && !isTemplateTemplateArg)
    {
        ///@todo Probably the parent has to be considered too (see previousInstantiationInformation), ouch
        m_templateArgsDepth++;
        const InstantiationInformation& instantiationInfo = templateDecl->instantiatedWith().information();
        for (unsigned int i = 0, n = instantiationInfo.templateParametersSize(); i < n; ++i)
        {
            const IndexedType & rType = instantiationInfo.templateParameters()[i];
            addArgumentType(rType.abstractType());
        }
        m_templateArgsDepth--;
    }
}

void ADLHelper::addDeclarationScopeIdentifier(Declaration * decl)
{
    if(decl)
        addAssociatedNamespace(decl->context()->scopeIdentifier(false));
}

void ADLHelper::addAssociatedNamespace(const KDevelop::QualifiedIdentifier& identifier)
{
#ifdef DEBUG_ADL
        kDebug() << "    adding namespace " << identifier.identifier().toString();
#endif
    if(identifier.count())
      m_associatedNamespaces.insert(identifier);
}

void ADLHelper::addBaseClasses(Declaration* declaration)
{
    ClassDeclaration * classDecl = dynamic_cast<ClassDeclaration*>(declaration);
    if (classDecl)
    {
        int nBaseClassesCount = classDecl->baseClassesSize();
        for (int i = 0; i < nBaseClassesCount; ++i)
        {
            const BaseClassInstance baseClass = classDecl->baseClasses()[i];
            StructureType::Ptr type = baseClass.baseClass.type<StructureType>();
            if (type)
                addAssociatedClass(type->declaration(m_topContext.data()));
        }
    }
}