File: templatedeclaration.h

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 (370 lines) | stat: -rw-r--r-- 18,474 bytes parent folder | download
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* This file is part of KDevelop
    Copyright 2007-2009 David Nolden <david.nolden.kdevelop@art-master.de>

   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.
*/

#ifndef TEMPLATEDECLARATION_H
#define TEMPLATEDECLARATION_H

#include <QList>
#include <QMutex>

#include <language/duchain/declaration.h>
#include <language/duchain/declarationid.h>
#include <language/duchain/forwarddeclaration.h>
#include <language/duchain/duchainpointer.h>
#include <language/duchain/ducontext.h>
#include <language/duchain/topducontext.h>
#include "cppduchainexport.h"
#include <language/duchain/appendedlist.h>
#include "expressionevaluationresult.h"
#include <interfaces/icore.h>
#include <interfaces/ilanguagecontroller.h>
#include <language/backgroundparser/backgroundparser.h>
#include <language/duchain/instantiationinformation.h>


namespace KTextEditor {
  class Range;
}
namespace  KDevelop {
  class DUContext;
  class Declaration;
  class DelayedType;
}

namespace Cpp {
  using KDevelop::InstantiationInformation;
  using KDevelop::IndexedInstantiationInformation;
  template<class Base>
  class CppDUContext;
  
  struct KDEVCPPDUCHAIN_EXPORT TemplateDeclarationData {
    TemplateDeclarationData() {
    }
    TemplateDeclarationData(const TemplateDeclarationData& rhs) : m_parameterContext(rhs.m_parameterContext) {
    }
    //The context in which the template-parameters are declared
    KDevelop::IndexedDUContext m_parameterContext;
  };
  
  //Represents the template-part of a template-class'es or template-function's template-part
  class KDEVCPPDUCHAIN_EXPORT TemplateDeclaration {
    public:
      typedef QHash<IndexedInstantiationInformation, TemplateDeclaration*> InstantiationsHash;
      
      ///Copy-constructor for cloning
      TemplateDeclaration(const TemplateDeclaration& rhs);
      TemplateDeclaration();
      virtual ~TemplateDeclaration();

      void setTemplateParameterContext(KDevelop::DUContext* context);
      KDevelop::DUContext* templateParameterContext() const;

      TemplateDeclaration* instantiatedFrom() const;

      ///Marks this template-declaration as a instantiation of the given one. This also means that this declaration will be deleted when the here given is deleted.
      ///from can also be zero, then the declaration will just be marked as a specialization(instantiatedWith is set)
      void setInstantiatedFrom(TemplateDeclaration* from, const InstantiationInformation& instantiatedWith);
      
      /**
       * Either finds the existing instance instantiated with the given template-arguments, or creates a new one.
       * The template-arguments must chained up with the template-arguments of the parent, if the parent is a template class.
       * @param source the top-context from which ones perspective the instantiation should happen. If @p topContext is zero, only existing specializations are returned
       * @param forceLocal if this is true, it is this exact declaration that is instantiated. Else, this declaration is instantiated again.
       * */
      KDevelop::Declaration* instantiate( const InstantiationInformation& templateArguments, const KDevelop::TopDUContext* source, bool forceLocal = false );

      ///Returns true if this class is a direct instantiation of the given class. Not if it is an instantiation of a specialization of the given class.
      bool isInstantiatedFrom(const TemplateDeclaration* other) const;

      void setSpecializedFrom(TemplateDeclaration* other);

      IndexedInstantiationInformation instantiatedWith() const;
      
      ///Marks that a declaration is currently instantiated with the given instantiation-information.
      ///A zero instantiation is inserted into the cache to make sure that we don't instantiate the same declaration again in the meantime.
      void reserveInstantiation(const IndexedInstantiationInformation& info);

      ///An instantiation is an additional temporary version of this declaration that was created given some template-parameters.
      ///Opposed to that a specialization is a real declaration that was created by the user, that specializes this declaration.
      ///Returns all current instantiations of this declaration
      ///@warning Some instantiations may have the value zero when an instantiation is currently happening.
       InstantiationsHash instantiations() const;
      
      IndexedInstantiationInformation specialization() const;
      
      DeclarationId id(bool forceDirect) const;
      
      Declaration* specialize(IndexedInstantiationInformation specialization, const TopDUContext* topContext, int upDistance);
    
      //Duchain must be write-locked
      void deleteAllInstantiations();
      
      ///Returns the template-context that belongs to this template declaration, or zero
      DUContext* templateContext(const TopDUContext* source) const;
      
      ///@return The declaration this one was explicitly specialized from.
      ///Zero, or a non-specialized (thus also non-instantiated) template-class, which this was explicitly specialized from.
      virtual IndexedDeclaration specializedFrom() = 0;
      virtual const IndexedDeclaration* specializations() const = 0;
      virtual uint specializationsSize() const = 0;
      
      ///These are internal, do not use them. They have to be public so they are visible from SpecialTemplateDeclaration.
      virtual void setSpecializedFromInternal(IndexedDeclaration decl) = 0;
      virtual void addSpecializationInternal(IndexedDeclaration decl) = 0;
      virtual void removeSpecializationInternal(IndexedDeclaration decl) = 0;
      
      virtual void setSpecializedWith(IndexedInstantiationInformation info) = 0;
      virtual IndexedInstantiationInformation specializedWith() const = 0;
      
    protected:
      //Matches the given instantiation-information to this declarations local specialization information
      QPair<unsigned int, TemplateDeclaration*> matchTemplateParameters(InstantiationInformation info, const TopDUContext* source);
      
      virtual TemplateDeclarationData* dynamicTemplateData() = 0;
      virtual const TemplateDeclarationData* templateData() const = 0;

      TemplateDeclaration* m_instantiatedFrom;

      IndexedInstantiationInformation m_instantiatedWith;
      
      static QMutex instantiationsMutex;
      ///Every access to m_instantiations must be serialized through instantiationsMutex!
      typedef QHash<IndexedInstantiationInformation, IndexedInstantiationInformation> DefaultParameterInstantiationHash;
      DefaultParameterInstantiationHash m_defaultParameterInstantiations;
      InstantiationsHash m_instantiations; ///Every declaration nested within a template declaration knows all its instantiations.
      // recursion counter
      int m_instantiationDepth;
  };
  
  
    KDEVCPPDUCHAIN_EXPORT DECLARE_LIST_MEMBER_HASH(SpecialTemplateDeclarationData, m_specializations, IndexedDeclaration)
  
    template<class Base>
    class SpecialTemplateDeclarationData : public Base, public TemplateDeclarationData
    {
    public:
    SpecialTemplateDeclarationData() {
      initializeAppendedLists();
    }
    
    ~SpecialTemplateDeclarationData() {
      freeAppendedLists();
    }
    
    SpecialTemplateDeclarationData(const SpecialTemplateDeclarationData& rhs) : Base(rhs), TemplateDeclarationData(rhs) {
      initializeAppendedLists();
      copyListsFrom(rhs);
      m_specializedFrom = rhs.m_specializedFrom;
      m_specializedWith = rhs.m_specializedWith;
    }
    
    uint classSize() const {
      return Base::classSize();
    }

    IndexedDeclaration m_specializedFrom;
    IndexedInstantiationInformation m_specializedWith;

    START_APPENDED_LISTS_BASE(SpecialTemplateDeclarationData, Base);
    APPENDED_LIST_FIRST(SpecialTemplateDeclarationData, IndexedDeclaration, m_specializations);
    END_APPENDED_LISTS(SpecialTemplateDeclarationData, m_specializations);

    //Just for visibility
    static bool appendedListDynamicDefault() {
      return Base::appendedListDynamicDefault();
    }
    };

  /**
   * Use this to merge any type of declaration with a TemplateDeclaration.
   * This allows wrapping template-declaration's over any kind of declaration, without changing the basic structure of types.
   * Necessary because we have at least 3 different declaration-classes that are not derived from each other, and that can
   * be TemplateDeclarations.
   */
  template<class BaseDeclaration>
  class KDEVCPPDUCHAIN_EXPORT SpecialTemplateDeclaration : public BaseDeclaration, public TemplateDeclaration {
    public:
    template<class Data>
    SpecialTemplateDeclaration(Data& data) : BaseDeclaration(data) {
    }

    typedef SpecialTemplateDeclarationData<typename BaseDeclaration::Data> Data;
    
    ///Copy-constructor for cloning
    SpecialTemplateDeclaration(const SpecialTemplateDeclaration<BaseDeclaration>& rhs) : BaseDeclaration(*new SpecialTemplateDeclarationData<typename BaseDeclaration::Data>(*rhs.d_func())), TemplateDeclaration(rhs) {
      DUChainBaseData* data = static_cast<DUChainBase*>(this)->d_func_dynamic();
      data->setClassId(this);
      //To keep link-consistency these specialization links are cleared in copies/instantiations
      static_cast<Data*>(data)->m_specializedFrom = IndexedDeclaration();
      static_cast<Data*>(data)->m_specializationsList().clear();
    }
    ///Arguments are passed to the base
    SpecialTemplateDeclaration( const KDevelop::RangeInRevision& range, KDevelop::DUContext* context ) : BaseDeclaration(*new SpecialTemplateDeclarationData<typename BaseDeclaration::Data>()) {
      static_cast<DUChainBase*>(this)->d_func_dynamic()->setClassId(this);
      
      this->setRange(range);
      
      if(context)
        this->setContext(context);
    }
    
    ~SpecialTemplateDeclaration() {
      TopDUContext* top = this->topContext();
      Q_ASSERT(top);

      //This actually belongs into ~TemplateDeclaration, but we do it here because here we do not need to go through virtual
      //functions or dynamic casts, because those are not guaranteed to work from within destructors.
      if(!top->deleting() || !top->isOnDisk()) {
        ///When the declaration is being deleted, disconnect the links on both sides
        Declaration* specializedFromDeclaration = static_cast<const Data*>(this->DUChainBase::d_func())->m_specializedFrom.data();
        if(TemplateDeclaration* specializedFromTemplate = dynamic_cast<TemplateDeclaration*>(specializedFromDeclaration))
          specializedFromTemplate->removeSpecializationInternal(IndexedDeclaration(this));
        
        FOREACH_FUNCTION(IndexedDeclaration decl, static_cast<const Data*>(this->DUChainBase::d_func())->m_specializations) {
          TemplateDeclaration* tDecl = dynamic_cast<TemplateDeclaration*>(decl.data());
          if(tDecl)
            tDecl->setSpecializedFrom(0);
        }    
      }
    }

    virtual IndexedDeclaration specializedFrom() {
      return static_cast<const Data*>(this->DUChainBase::d_func())->m_specializedFrom;
    }
    virtual const IndexedDeclaration* specializations() const {
      return static_cast<const Data*>(this->DUChainBase::d_func())->m_specializations();
    }
    virtual uint specializationsSize() const {
      return static_cast<const Data*>(this->DUChainBase::d_func())->m_specializationsSize();
    }
    virtual IndexedInstantiationInformation specializedWith() const {
      return static_cast<const Data*>(this->DUChainBase::d_func())->m_specializedWith;
    }
    virtual bool inDUChain() const {
      return instantiatedFrom() || BaseDeclaration::inDUChain();
    }
    
    virtual uint additionalIdentity() const {
      return BaseDeclaration::additionalIdentity() + 101;
    }
    virtual Declaration* specialize(IndexedInstantiationInformation specialization, const TopDUContext* topContext, int upDistance) {
      return TemplateDeclaration::specialize(specialization, topContext, upDistance);
    }
    
    virtual IndexedInstantiationInformation specialization() const {
      return TemplateDeclaration::specialization();
    }
    
    virtual DeclarationId id(bool forceDirect) const {
      return TemplateDeclaration::id(forceDirect);
    }
    
    //Is specialized for ForwardDeclaration in templatedeclaration.cpp to actively instantiate template forward declarations
    virtual Declaration* resolve(const TopDUContext* /*topContext*/) const {
      Q_ASSERT(0);
      return 0;
    }
    
    enum {
      Identity = BaseDeclaration::Identity + 50
    };
    
    private:
    virtual void setSpecializedFromInternal(IndexedDeclaration decl) {
      static_cast<Data*>(this->DUChainBase::d_func_dynamic())->m_specializedFrom = decl;
    }
    virtual void addSpecializationInternal(IndexedDeclaration decl) {
      static_cast<Data*>(this->DUChainBase::d_func_dynamic())->m_specializationsList().append(decl);
    }
    virtual void removeSpecializationInternal(IndexedDeclaration decl) {
      bool result = static_cast<Data*>(this->DUChainBase::d_func_dynamic())->m_specializationsList().removeOne(decl);
      Q_ASSERT(result);
      Q_UNUSED(result);
    }
    virtual void setSpecializedWith(IndexedInstantiationInformation info) {
      static_cast<Data*>(this->DUChainBase::d_func_dynamic())->m_specializedWith = info;
    }
      
    virtual TemplateDeclarationData* dynamicTemplateData() {
        return d_func_dynamic();
    }
    
    virtual const TemplateDeclarationData* templateData() const {
        return d_func();
    }

    virtual void activateSpecialization()
    {
      BaseDeclaration::activateSpecialization();
      
      if(specialization().index()) {
        //Also register parents
        DUContext* context = this->context();
        if(context->owner() && context->owner()->specialization().index()) {
          context->owner()->activateSpecialization(); //will also add to the background-parser
        }else{
//           context->topContext()->setHasUses(false); //Force re-building of the uses
//           ICore::self()->languageController()->backgroundParser()->addDocument( KUrl(this->url().str()) );
        }
      }
    }

    inline SpecialTemplateDeclarationData<typename BaseDeclaration::Data>* d_func_dynamic() { this->makeDynamic(); return reinterpret_cast<SpecialTemplateDeclarationData<typename BaseDeclaration::Data>*>(this->d_ptr); }
    inline const SpecialTemplateDeclarationData<typename BaseDeclaration::Data>* d_func() const { return reinterpret_cast<const SpecialTemplateDeclarationData<typename BaseDeclaration::Data>*>(this->d_ptr); }
    virtual KDevelop::Declaration* clonePrivate() const {
      return new SpecialTemplateDeclaration(*this);
    }
  };

  bool KDEVCPPDUCHAIN_EXPORT isTemplateDeclaration(const KDevelop::Declaration*);

  /**
   * If this is used to instantiate a declaration, it should be called BEFORE the Declaration's types DelayedType's are resolved,
   * because those are needed to resolve missing base-classes.
   * 
   * The given context should be one that, on some level, imports a template-parameter-declaration context.
   * The given declaration will be registered anonymously, the same for the created contexts.
   * @param parentContext he parent-context everything should be created in(instantiatedDeclaration will be moved into that context anonymously)
   * @param inclusionTrace a trace as used in findDeclarationsInternal(..)
   * @param context A du-context that will be copied and used as internal context for declaration. If this is zero, no context will be copied.
   * @param templateArguments The template-arguments that will be used to instantiate the input-context. If this is empty, the intersting context will be only copied without specialization. If it contains exactly one argument, and that argument is invalid, the context is instantiated without arguments(default-arguments are used). Default-arguments will be used if needed.
   * @param instantiatedDeclaration The copied declaration this should belong to. If this is set, the created context will be made the given declaration's internal-context, and its parent-context will be set to the given context's parent-context. Also delayed types in the declaration will be resolved(The declaration will be changed)
   * @param instantiatedFrom The declaration instantiatedDeclaration should be/is instantiated from. This is needed to eventually change the declaration of in IdentifiedType. The instantiation has to be reserved before calling instantiateDeclarationAndContext!
   * @param doNotRegister If this is true, the context will not be registered anywhere, and can be deleted at will.
   *
   * The DU-Context must be read-locked but not write-locked when this is called.
   * */
  KDEVCPPDUCHAIN_EXPORT CppDUContext<KDevelop::DUContext>* instantiateDeclarationAndContext( KDevelop::DUContext* parentContext, const KDevelop::TopDUContext* source, KDevelop::DUContext* context, const InstantiationInformation& templateArguments, KDevelop::Declaration* instantiatedDeclaration, KDevelop::Declaration* instantiatedFrom, bool doNotRegister = false );

  /**
  * Returns whether any count of reference/pointer-types are followed by a delayed type
  * */
  KDEVCPPDUCHAIN_EXPORT TypePtr<DelayedType> containsDelayedType(AbstractType::Ptr type);
  
  /**
   * Eventually creates a copy of the given type, where all DelayedTypes that can be resolved in the given context are resolved.
   * */
  KDEVCPPDUCHAIN_EXPORT AbstractType::Ptr resolveDelayedTypes( AbstractType::Ptr type, const KDevelop::DUContext* context, const KDevelop::TopDUContext* source, KDevelop::DUContext::SearchFlags searchFlags = KDevelop::DUContext::NoUndefinedTemplateParams );

template<>
Declaration* SpecialTemplateDeclaration<ForwardDeclaration>::resolve(const TopDUContext* topContext) const;

}

#endif