abstracttype.h 11.6 KB
Newer Older
1
2
3
/* This file is part of KDevelop
    Copyright 2006 Roberto Raggi <roberto@kdevelop.org>
    Copyright 2006-2008 Hamish Rodda <rodda@kde.org>
David Nolden's avatar
David Nolden committed
4
    Copyright 2007-2008 David Nolden <david.nolden.kdevelop@art-master.de>
5
6
7
8
9
10
11
12
13
14
15
16
17
18

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

21
22
#ifndef KDEVPLATFORM_ABSTRACTTYPE_H
#define KDEVPLATFORM_ABSTRACTTYPE_H
23

24
#include "typepointer.h"
Kevin Funk's avatar
Kevin Funk committed
25
#include <language/languageexport.h>
26

27
28
class QString;

29
namespace KDevelop {
30
31
32
33
34
35
36
class AbstractTypeData;

class IndexedType;

class TypeVisitor;
class TypeExchanger;

37
/// This macro is used to declare type-specific data-access functions within subclasses of AbstractType
38
#define TYPE_DECLARE_DATA(Class) \
39
40
    inline Class ## Data * d_func_dynamic() { makeDynamic(); return reinterpret_cast<Class ## Data*>(d_ptr); } \
    inline const Class ## Data* d_func() const { return reinterpret_cast<const Class ## Data*>(d_ptr); }
41

42
/// This function creates a local variable named 'd' pointing to the data type (as shortcut)
43
44
#define TYPE_D(Class) const Class ## Data * const d = d_func()
#define TYPE_D_DYNAMIC(Class) Class ## Data * const d = d_func_dynamic()
45
46
47
48
49
50
51
52
53
54

/**
 * \brief Base class for all types.
 *
 * The AbstractType class is a base class from which all types derive.  It features:
 * - mechanisms for visiting types
 * - toString() feature
 * - equivalence feature
 * - cloning of types, and
 * - hashing and indexing
55
 * - efficient, persistent, and reference-counted storage of types using IndexedType
56
57
58
59
60
 *
 *  Type classes are created in a way that allows storing them in memory or on disk
 *  efficiently.  They are classes which can store arbitrary lists immediately after their
 *  private data structures in memory (thus enabling them to be mmapped or memcopied),
 *  or being "dynamic" where you use exactly the same class and same access functions,
61
 *  but the list data is stored in a temporary KDevVarLengthArray from a central repository,
62
63
 *  until we save it back to the static memory-region again.
 *
64
65
66
67
68
69
70
71
 * When creating an own (sub-) type, you must:
 * - Override equals(..), hash().
 *   - The functions should _fully_ distinguish all types,
 *     in regard to all stored information, and regarding their identity.
 *   - This can be skipped if you're overriding a base-type which already incorporates
 *     all of your own types status within its equals/hash functions (eg. you don't add own data).
 * - Implement a copy-constructor in which you copy the data from the source using copyData<YourType>()
 * - Override the clone() function in which you use the copy-constructor to clone the type
72
 * - Add an enumerator "Identity" that contains an arbitrary unique identity value of the type
73
 * - Register your type in a source-file using REGISTER_TYPE(..), @see typeregister.h
74
 * - Add a typedef "Data", that contains the actual data of the type using the mechanisms described in appendedlist.h.
75
76
 *   That data type must follow the same inheritance chain as the type itself, so it must be based on the Data object
 *   of the base class. See AbstractTypeData.
77
78
79
 * - Use createData<YourType>() to create the data-object in a constructor (which you then reach to the parent constructor)
 * - Use TYPE_DECLARE_DATA(YourType) to declare the data access functions d_func and d_func_dynamic,
 *   and  then use d_func()->.. and d_func_dynamic()->.. to access your type data
80
 * - Create a constructor that only takes a reference to the type data, and passes it to the parent type
81
 *
82
 *   Every type can have only one other type as base-type,
83
84
85
86
 *   but it can have additional base-classes that are not a direct part of the type-system(@see IdentifiedType).
 *
 *  \sa appendedlist.h
 */
87
88
class KDEVPLATFORMLANGUAGE_EXPORT AbstractType
    : public QSharedData
89
90
{
public:
91
    using Ptr = TypePtr<AbstractType>;
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

    /**
     * An enumeration of common modifiers for data types.
     * If you have any language-specific modifiers that don't belong here,
     * you can add them at/after LanguageSpecificModifier
     * @warning Think twice what information you store into the type-system.
     *          The type-system should store information that is shared among many declarations,
     *          and attributes of specific Declarations like public/private should be stored in
     *          the Declarations themselves, not in the type-system.
     */
    enum CommonModifiers : quint32 {
        NoModifiers                 = 0,

        ConstModifier               = 1 << 0,
        VolatileModifier            = 1 << 1,
        TransientModifier           = 1 << 2,
        NewModifier                 = 1 << 3,
        SealedModifier              = 1 << 4,
        UnsafeModifier              = 1 << 5,
        FixedModifier               = 1 << 6,
        ShortModifier               = 1 << 7,
        LongModifier                = 1 << 8,
        LongLongModifier            = 1 << 9,
        SignedModifier              = 1 << 10,
        UnsignedModifier            = 1 << 11,

        LanguageSpecificModifier    = 1 << 12
    };

    /// Constructor.
    AbstractType();
    /// Constructor from data.
    explicit AbstractType(AbstractTypeData& dd);
    /// Destructor.
    virtual ~AbstractType ();

128
    AbstractType(const AbstractType& rhs) = delete;
129
130
    AbstractType& operator=(const AbstractType& rhs) = delete;

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    /**
     * Access the type modifiers
     *
     * \returns the type's modifiers.
     */
    quint32 modifiers() const;

    /**
     * Set the type's modifiers.
     *
     * \param modifiers modifiers of this type.
     */
    void setModifiers(quint32 modifiers);

    /**
Yuri Chornoivan's avatar
Yuri Chornoivan committed
146
     * Visitor method.  Called by TypeVisitor to visit the type hierarchy.
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
     * Do not reimplement this, reimplement accept0 instead.
     *
     * \param v visitor which is calling this function.
     */
    void accept(TypeVisitor* v) const;

    /**
     * Convenience visitor method which can be called with a null type.
     *
     * \param type type to visit, may be null.
     * \param v visitor which is visiting the given \a type
     */
    static void acceptType(AbstractType::Ptr type, TypeVisitor* v);

    /**
     * Returns this type as a string, preferably the same as it is expressed in the code.
     *
     * \return this type as a string
     */
    virtual QString toString() const;

    ///Must always be called before anything in the data pointer is changed!
    ///If it's not called beforehand, the type-repository gets corrupted
    void makeDynamic();

    ///Should return whether this type's content equals the given one
    ///Since this is used by the type-repository, it must compare ALL members of the data type.
    virtual bool equals(const AbstractType* rhs) const;

    /**
     * Should create a clone of the source-type, with as much data copied as possible without breaking the du-chain.
     * */
    virtual AbstractType* clone() const = 0;

    /**
     * A hash-value that should have the following properties:
     * - When two types match on equals(), it should be same.
     * - When two types don't match on equals(), it should be different with a high probability.
     * */
    virtual uint hash() const;

    ///This can also be called on zero types, those can then be reconstructed from the zero index
    IndexedType indexed() const;

    /// Enumeration of major data types.
    enum WhichType : quint8 {
        TypeAbstract /**< an abstract type */,
        TypeIntegral /**< an integral */,
        TypePointer /**< a pointer*/,
        TypeReference /**< a reference */,
        TypeFunction /**< a function */,
        TypeStructure /**< a structure */,
        TypeArray /**< an array */,
        TypeDelayed /**< a delayed type */,
        TypeEnumeration /**< an enumeration type */,
        TypeEnumerator /**< an enumerator type */,
        TypeAlias /**< a type-alias type */,
        TypeUnsure /**< may represent multiple different types */
    };

    /**
     * Determine which data type this abstract type represents.
     *
     * \returns the data type represented by this type.
     */
    virtual WhichType whichType() const;

    enum {
        Identity = 1
    };

    /**
     * Should, like accept0, be implemented by all types that hold references to other types.
     *
     * If this is called on one type, that type should call exchangeTypes(..) with all its referenced sub-types.
     * The type itself does not recurse into the sub-types, that can be done by the exchanger itself if desired.
     * */
    virtual void exchangeTypes(TypeExchanger* exchanger);

    /**
     * Method to create copies of internal type data. You must use this to create the internal
     * data instances in copy constructors. It is needed, because it may need to allocate more memory
     * for appended lists.
     *
     * \param rhs data to copy
     * \returns copy of the data
     */

    template <class Type>
    static typename Type::Data& copyData(const typename Type::Data& rhs)
    {
        uint size;
        if (!rhs.m_dynamic)
            size = sizeof(typename Type::Data); //Create a dynamic data instance
        else
            size = rhs.dynamicSize(); //Create a constant data instance, that holds all the data embedded.

        typename Type::Data& ret(*new (new char[size]) typename Type::Data(rhs));
        ret.template setTypeClassId<Type>();
        return ret;
    }

    /**
     * As above, but does not support copying data into a lower class(Should not be used while cloning)
     */
    template <class DataType>
    static DataType& copyDataDirectly(const DataType& rhs)
    {
        uint size;
        if (!rhs.m_dynamic)
            size = sizeof(DataType); //Create a dynamic data instance
        else
            size = rhs.dynamicSize(); //Create a constant data instance, that holds all the data embedded.

        return *new (new char[size]) DataType(rhs);
    }

    /**
     * Method to create internal data structures. Use this in normal constructors.
     *
     * \returns the internal data structure
     */
    template <class Type>
    static typename Type::Data& createData()
    {
        typename Type::Data& ret(*new (new char[sizeof(typename Type::Data)]) typename Type::Data());
        ret.template setTypeClassId<Type>();
        return ret;
    }

277
    using Data = AbstractTypeData;
278
279

protected:
280
281
282
283
284
285
    /**
     * Visitor method, reimplement to allow visiting of types.
     *
     * \param v visitor which is visiting.
     */
    virtual void accept0 (TypeVisitor* v) const = 0;
Hamish Rodda's avatar
Hamish Rodda committed
286

287
288
    /// toString() function which can provide \a spaceOnLeft rather than on right if desired.
    QString toString(bool spaceOnLeft) const;
Hamish Rodda's avatar
Hamish Rodda committed
289

290
    AbstractTypeData* d_ptr;
291

292
    TYPE_DECLARE_DATA(AbstractType)
293

294
    friend class AbstractTypeDataRequest;
295
296
297
298
};

/**
 * You can use these instead of dynamic_cast, for basic types it has better performance because it checks the whichType() member
299
300
301
302
303
 */
template <class To>
inline To fastCast(AbstractType* from)
{
    return dynamic_cast<To>(from);
304
305
}

306
307
308
309
template <class To>
inline const To fastCast(const AbstractType* from)
{
    return const_cast<const To>(fastCast<To>(const_cast<AbstractType*>(from))); //Hack so we don't need to define the functions twice, once for const, and once for not const
310
311
312
313
}
}

#endif