DeprecatedGenTemplateInstance.cpp 13 KB
Newer Older
Roberto Raggi's avatar
Roberto Raggi committed
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
Roberto Raggi's avatar
Roberto Raggi committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
29

30
#include "DeprecatedGenTemplateInstance.h"
31
32
#include "Overview.h"

33
34
35
36
37
#include <Control.h>
#include <Scope.h>
#include <Names.h>
#include <Symbols.h>
#include <CoreTypes.h>
38
39
#include <Literals.h>

40
#include <QtCore/QVarLengthArray>
41
#include <QtCore/QDebug>
42
43
44

using namespace CPlusPlus;

45
namespace {
46

47
48
49
class ApplySubstitution
{
public:
50
    ApplySubstitution(Control *control, Symbol *symbol, const DeprecatedGenTemplateInstance::Substitution &substitution);
51
52
    ~ApplySubstitution();

53
    inline Control *control() const { return _control; }
54

Roberto Raggi's avatar
Roberto Raggi committed
55
    FullySpecifiedType apply(const Name *name);
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
56
    FullySpecifiedType apply(const FullySpecifiedType &type);
57

Roberto Raggi's avatar
Roberto Raggi committed
58
    int findSubstitution(const Identifier *id) const;
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
    FullySpecifiedType applySubstitution(int index) const;

private:
    class ApplyToType: protected TypeVisitor
    {
    public:
        ApplyToType(ApplySubstitution *q)
            : q(q) {}

        FullySpecifiedType operator()(const FullySpecifiedType &ty)
        {
            FullySpecifiedType previousType = switchType(ty);
            accept(ty.type());
            return switchType(previousType);
        }

    protected:
        using TypeVisitor::visit;

        Control *control() const
        { return q->control(); }

        FullySpecifiedType switchType(const FullySpecifiedType &type)
        {
            FullySpecifiedType previousType = _type;
            _type = type;
            return previousType;
        }

        virtual void visit(VoidType *)
        {
            // nothing to do
        }

        virtual void visit(IntegerType *)
        {
            // nothing to do
        }

        virtual void visit(FloatType *)
        {
            // nothing to do
        }

        virtual void visit(PointerToMemberType *)
        {
            qDebug() << Q_FUNC_INFO; // ### TODO
        }

        virtual void visit(PointerType *ptrTy)
        {
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
110
            _type.setType(control()->pointerType(q->apply(ptrTy->elementType())));
111
112
113
114
        }

        virtual void visit(ReferenceType *refTy)
        {
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
115
            _type.setType(control()->referenceType(q->apply(refTy->elementType())));
116
117
118
119
        }

        virtual void visit(ArrayType *arrayTy)
        {
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
120
            _type.setType(control()->arrayType(q->apply(arrayTy->elementType()), arrayTy->size()));
121
122
123
124
        }

        virtual void visit(NamedType *ty)
        {
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
125
            FullySpecifiedType n = q->apply(ty->name());
126
127
128
129
130
131
132
133
134
135
136
137
138
            _type.setType(n.type());
        }

        virtual void visit(Function *funTy)
        {
            Function *fun = control()->newFunction(/*sourceLocation=*/ 0, funTy->name());
            fun->setScope(funTy->scope());
            fun->setConst(funTy->isConst());
            fun->setVolatile(funTy->isVolatile());
            fun->setVirtual(funTy->isVirtual());
            fun->setAmbiguous(funTy->isAmbiguous());
            fun->setVariadic(funTy->isVariadic());

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
139
            fun->setReturnType(q->apply(funTy->returnType()));
140
141
142
143
144
145

            for (unsigned i = 0; i < funTy->argumentCount(); ++i) {
                Argument *originalArgument = funTy->argumentAt(i)->asArgument();
                Argument *arg = control()->newArgument(/*sourceLocation*/ 0,
                                                       originalArgument->name());

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
146
                arg->setType(q->apply(originalArgument->type()));
147
                arg->setInitializer(originalArgument->initializer());
148
                fun->addMember(arg);
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
            }

            _type.setType(fun);
        }

        virtual void visit(Namespace *)
        {
            qDebug() << Q_FUNC_INFO;
        }

        virtual void visit(Class *)
        {
            qDebug() << Q_FUNC_INFO;
        }

        virtual void visit(Enum *)
        {
            qDebug() << Q_FUNC_INFO;
        }

        virtual void visit(ForwardClassDeclaration *)
        {
            qDebug() << Q_FUNC_INFO;
        }

        virtual void visit(ObjCClass *)
        {
            qDebug() << Q_FUNC_INFO;
        }

        virtual void visit(ObjCProtocol *)
        {
            qDebug() << Q_FUNC_INFO;
        }

        virtual void visit(ObjCMethod *)
        {
            qDebug() << Q_FUNC_INFO;
        }

        virtual void visit(ObjCForwardClassDeclaration *)
        {
            qDebug() << Q_FUNC_INFO;
        }

        virtual void visit(ObjCForwardProtocolDeclaration *)
        {
            qDebug() << Q_FUNC_INFO;
        }

    private:
        ApplySubstitution *q;
        FullySpecifiedType _type;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
202
        QHash<Symbol *, FullySpecifiedType> _processed;
203
204
205
206
207
208
209
    };

    class ApplyToName: protected NameVisitor
    {
    public:
        ApplyToName(ApplySubstitution *q): q(q) {}

Roberto Raggi's avatar
Roberto Raggi committed
210
        FullySpecifiedType operator()(const Name *name)
211
212
213
214
215
216
217
218
219
220
        {
            FullySpecifiedType previousType = switchType(FullySpecifiedType());
            accept(name);
            return switchType(previousType);
        }

    protected:
        Control *control() const
        { return q->control(); }

Roberto Raggi's avatar
Roberto Raggi committed
221
        int findSubstitution(const Identifier *id) const
222
223
224
225
226
227
228
229
230
231
232
233
        { return q->findSubstitution(id); }

        FullySpecifiedType applySubstitution(int index) const
        { return q->applySubstitution(index); }

        FullySpecifiedType switchType(const FullySpecifiedType &type)
        {
            FullySpecifiedType previousType = _type;
            _type = type;
            return previousType;
        }

Roberto Raggi's avatar
Roberto Raggi committed
234
        virtual void visit(const NameId *name)
235
236
237
238
239
240
241
242
243
244
        {
            int index = findSubstitution(name->identifier());

            if (index != -1)
                _type = applySubstitution(index);

            else
                _type = control()->namedType(name);
        }

Roberto Raggi's avatar
Roberto Raggi committed
245
        virtual void visit(const TemplateNameId *name)
246
247
248
249
        {
            QVarLengthArray<FullySpecifiedType, 8> arguments(name->templateArgumentCount());
            for (unsigned i = 0; i < name->templateArgumentCount(); ++i) {
                FullySpecifiedType argTy = name->templateArgumentAt(i);
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
250
                arguments[i] = q->apply(argTy);
251
252
            }

Roberto Raggi's avatar
Roberto Raggi committed
253
254
255
            const TemplateNameId *templId = control()->templateNameId(name->identifier(),
                                                                      arguments.data(),
                                                                      arguments.size());
256
257
258
            _type = control()->namedType(templId);
        }

259
        const Name *instantiate(const Name *name)
260
        {
261
262
263
264
            if (! name)
                return name;

            else if (const Name *nameId = name->asNameId()) {
265
                const Identifier *id = control()->identifier(nameId->identifier()->chars(),
266
267
268
269
270
271
272
273
                                                                         nameId->identifier()->size());
                return control()->nameId(id);

            } else if (const TemplateNameId *templId = name->asTemplateNameId()) {
                QVarLengthArray<FullySpecifiedType, 8> arguments(templId->templateArgumentCount());
                for (unsigned templateArgIndex = 0; templateArgIndex < templId->templateArgumentCount(); ++templateArgIndex) {
                    FullySpecifiedType argTy = templId->templateArgumentAt(templateArgIndex);
                    arguments[templateArgIndex] = q->apply(argTy);
274
                }
275
                const Identifier *id = control()->identifier(templId->identifier()->chars(),
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
                                                                         templId->identifier()->size());
                return control()->templateNameId(id, arguments.data(), arguments.size());

            } else if (const QualifiedNameId *qq = name->asQualifiedNameId()) {
                const Name *base = instantiate(qq->base());
                const Name *name = instantiate(qq->name());

                return control()->qualifiedNameId(base, name);

            } else if (const OperatorNameId *op = name->asOperatorNameId()) {
                return control()->operatorNameId(op->kind());

            } else if (const ConversionNameId *c = name->asConversionNameId()) {
                FullySpecifiedType ty = q->apply(c->type());
                return control()->conversionNameId(ty);
291
292
293

            }

294
295
296
297
298
299
300
            return 0;
        }

        virtual void visit(const QualifiedNameId *name)
        {
            if (const Name *n = instantiate(name))
                _type = control()->namedType(n);
301
302
        }

Roberto Raggi's avatar
Roberto Raggi committed
303
        virtual void visit(const DestructorNameId *name)
304
305
306
307
308
        {
            Overview oo;
            qWarning() << "ignored name:" << oo(name);
        }

Roberto Raggi's avatar
Roberto Raggi committed
309
        virtual void visit(const OperatorNameId *name)
310
311
312
313
314
        {
            Overview oo;
            qWarning() << "ignored name:" << oo(name);
        }

Roberto Raggi's avatar
Roberto Raggi committed
315
        virtual void visit(const ConversionNameId *name)
316
317
318
319
320
        {
            Overview oo;
            qWarning() << "ignored name:" << oo(name);
        }

Roberto Raggi's avatar
Roberto Raggi committed
321
        virtual void visit(const SelectorNameId *name)
322
323
324
325
326
327
328
329
330
331
332
        {
            Overview oo;
            qWarning() << "ignored name:" << oo(name);
        }

    private:
        ApplySubstitution *q;
        FullySpecifiedType _type;
    };

public: // attributes
333
    Control *_control;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
334
    Symbol *symbol;
335
    DeprecatedGenTemplateInstance::Substitution substitution;
336
337
338
339
    ApplyToType applyToType;
    ApplyToName applyToName;
};

340
ApplySubstitution::ApplySubstitution(Control *control, Symbol *symbol,
341
                                     const DeprecatedGenTemplateInstance::Substitution &substitution)
342
    : _control(control), symbol(symbol),
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
343
344
      substitution(substitution),
      applyToType(this), applyToName(this)
345
{ }
346

347
ApplySubstitution::~ApplySubstitution()
348
{
349
}
350

Roberto Raggi's avatar
Roberto Raggi committed
351
FullySpecifiedType ApplySubstitution::apply(const Name *name)
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
352
353
354
355
{
    FullySpecifiedType ty = applyToName(name);
    return ty;
}
356

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
357
358
359
360
361
FullySpecifiedType ApplySubstitution::apply(const FullySpecifiedType &type)
{
    FullySpecifiedType ty = applyToType(type);
    return ty;
}
362

Roberto Raggi's avatar
Roberto Raggi committed
363
int ApplySubstitution::findSubstitution(const Identifier *id) const
364
365
{
    Q_ASSERT(id != 0);
366

367
    for (int index = 0; index < substitution.size(); ++index) {
Roberto Raggi's avatar
Roberto Raggi committed
368
        QPair<const Identifier *, FullySpecifiedType> s = substitution.at(index);
369

370
371
372
        if (id->isEqualTo(s.first))
            return index;
    }
373

374
    return -1;
375
376
}

377
FullySpecifiedType ApplySubstitution::applySubstitution(int index) const
378
{
379
380
    Q_ASSERT(index != -1);
    Q_ASSERT(index < substitution.size());
381

382
    return substitution.at(index).second;
383
384
}

385
} // end of anonymous namespace
386

387
DeprecatedGenTemplateInstance::DeprecatedGenTemplateInstance(QSharedPointer<Control> control, const Substitution &substitution)
388
    : _symbol(0),
389
      _control(control),
390
391
      _substitution(substitution)
{ }
392

393
FullySpecifiedType DeprecatedGenTemplateInstance::gen(Symbol *symbol)
394
{
395
    ApplySubstitution o(_control.data(), symbol, _substitution);
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
396
    return o.apply(symbol->type());
397
398
}

399
400
FullySpecifiedType DeprecatedGenTemplateInstance::instantiate(const Name *className, Symbol *candidate,
                                                              QSharedPointer<Control> control)
401
402
403
{
    if (className) {
        if (const TemplateNameId *templId = className->asTemplateNameId()) {
404
            if (Template *templ = candidate->enclosingTemplate()) {
405
                DeprecatedGenTemplateInstance::Substitution subst;
406
407
408
409

                for (unsigned i = 0; i < templId->templateArgumentCount(); ++i) {
                    FullySpecifiedType templArgTy = templId->templateArgumentAt(i);

410
411
                    if (i < templ->templateParameterCount()) {
                        const Name *templArgName = templ->templateParameterAt(i)->name();
412
413
414
415
416
417
418
419

                        if (templArgName && templArgName->identifier()) {
                            const Identifier *templArgId = templArgName->identifier();
                            subst.append(qMakePair(templArgId, templArgTy));
                        }
                    }
                }

420
                DeprecatedGenTemplateInstance inst(control, subst);
421
422
423
424
425
426
                return inst.gen(candidate);
            }
        }
    }
    return candidate->type();
}