NamePrettyPrinter.cpp 8.32 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "NamePrettyPrinter.h"
hjk's avatar
hjk committed
31

32
33
34
35
36
#include "Overview.h"

#include <cplusplus/Names.h>
#include <cplusplus/NameVisitor.h>
#include <cplusplus/Literals.h>
con's avatar
con committed
37
38
39
40
41
42
43
44
45
46
47

using namespace CPlusPlus;

NamePrettyPrinter::NamePrettyPrinter(const Overview *overview)
    : _overview(overview)
{ }

NamePrettyPrinter::~NamePrettyPrinter()
{ }

const Overview *NamePrettyPrinter::overview() const
hjk's avatar
hjk committed
48
49
50
{
    return _overview;
}
con's avatar
con committed
51

Roberto Raggi's avatar
Roberto Raggi committed
52
QString NamePrettyPrinter::operator()(const Name *name)
con's avatar
con committed
53
54
55
56
57
58
59
60
61
62
63
64
65
{
    QString previousName = switchName();
    accept(name);
    return switchName(previousName);
}

QString NamePrettyPrinter::switchName(const QString &name)
{
    QString previousName = _name;
    _name = name;
    return previousName;
}

66
void NamePrettyPrinter::visit(const Identifier *name)
con's avatar
con committed
67
{
Roberto Raggi's avatar
Roberto Raggi committed
68
    const Identifier *id = name->identifier();
con's avatar
con committed
69
70
71
72
73
74
    if (id)
        _name = QString::fromLatin1(id->chars(), id->size());
    else
        _name = QLatin1String("anonymous");
}

Roberto Raggi's avatar
Roberto Raggi committed
75
void NamePrettyPrinter::visit(const TemplateNameId *name)
con's avatar
con committed
76
{
Roberto Raggi's avatar
Roberto Raggi committed
77
    const Identifier *id = name->identifier();
con's avatar
con committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    if (id)
        _name = QString::fromLatin1(id->chars(), id->size());
    else
        _name = QLatin1String("anonymous");
    _name += QLatin1Char('<');
    for (unsigned index = 0; index < name->templateArgumentCount(); ++index) {
        if (index != 0)
            _name += QLatin1String(", ");

        FullySpecifiedType argTy = name->templateArgumentAt(index);
        QString arg = overview()->prettyType(argTy);
        if (arg.isEmpty())
            _name += QString::fromLatin1("_Tp%1").arg(index + 1);
        else
            _name += arg;
    }
94
    if (! _name.isEmpty() && _name.at(_name.length() - 1) == QLatin1Char('>'))
95
        _name += QLatin1Char(' ');
con's avatar
con committed
96
97
98
    _name += QLatin1Char('>');
}

Roberto Raggi's avatar
Roberto Raggi committed
99
void NamePrettyPrinter::visit(const DestructorNameId *name)
con's avatar
con committed
100
{
Roberto Raggi's avatar
Roberto Raggi committed
101
    const Identifier *id = name->identifier();
con's avatar
con committed
102
103
104
105
    _name += QLatin1Char('~');
    _name += QString::fromLatin1(id->chars(), id->size());
}

Roberto Raggi's avatar
Roberto Raggi committed
106
void NamePrettyPrinter::visit(const OperatorNameId *name)
con's avatar
con committed
107
{
108
109
110
    _name += QLatin1String("operator");
    if (_overview->includeWhiteSpaceInOperatorName)
        _name += QLatin1Char(' ');
con's avatar
con committed
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
    switch (name->kind()) { // ### i should probably do this in OperatorNameId
    case OperatorNameId::InvalidOp:
        _name += QLatin1String("<invalid>");
        break;
    case OperatorNameId::NewOp:
        _name += QLatin1String("new");
        break;
    case OperatorNameId::DeleteOp:
        _name += QLatin1String("delete");
        break;
    case OperatorNameId::NewArrayOp:
        _name += QLatin1String("new[]");
        break;
    case OperatorNameId::DeleteArrayOp:
        _name += QLatin1String("delete[]");
        break;
    case OperatorNameId::PlusOp:
        _name += QLatin1String("+");
        break;
    case OperatorNameId::MinusOp:
        _name += QLatin1String("-");
        break;
    case OperatorNameId::StarOp:
        _name += QLatin1String("*");
        break;
    case OperatorNameId::SlashOp:
        _name += QLatin1String("/");
        break;
    case OperatorNameId::PercentOp:
        _name += QLatin1String("%");
        break;
    case OperatorNameId::CaretOp:
        _name += QLatin1String("^");
        break;
    case OperatorNameId::AmpOp:
        _name += QLatin1String("&");
        break;
    case OperatorNameId::PipeOp:
        _name += QLatin1String("|");
        break;
    case OperatorNameId::TildeOp:
        _name += QLatin1String("~");
        break;
    case OperatorNameId::ExclaimOp:
        _name += QLatin1String("!");
        break;
    case OperatorNameId::EqualOp:
        _name += QLatin1String("=");
        break;
    case OperatorNameId::LessOp:
        _name += QLatin1String("<");
        break;
    case OperatorNameId::GreaterOp:
        _name += QLatin1String(">");
        break;
    case OperatorNameId::PlusEqualOp:
        _name += QLatin1String("+=");
        break;
    case OperatorNameId::MinusEqualOp:
        _name += QLatin1String("-=");
        break;
    case OperatorNameId::StarEqualOp:
        _name += QLatin1String("*=");
        break;
    case OperatorNameId::SlashEqualOp:
        _name += QLatin1String("/=");
        break;
    case OperatorNameId::PercentEqualOp:
        _name += QLatin1String("%=");
        break;
    case OperatorNameId::CaretEqualOp:
        _name += QLatin1String("^=");
        break;
    case OperatorNameId::AmpEqualOp:
        _name += QLatin1String("&=");
        break;
    case OperatorNameId::PipeEqualOp:
        _name += QLatin1String("|=");
        break;
    case OperatorNameId::LessLessOp:
        _name += QLatin1String("<<");
        break;
    case OperatorNameId::GreaterGreaterOp:
        _name += QLatin1String(">>");
        break;
    case OperatorNameId::LessLessEqualOp:
        _name += QLatin1String("<<=");
        break;
    case OperatorNameId::GreaterGreaterEqualOp:
        _name += QLatin1String(">>=");
        break;
    case OperatorNameId::EqualEqualOp:
        _name += QLatin1String("==");
        break;
    case OperatorNameId::ExclaimEqualOp:
        _name += QLatin1String("!=");
        break;
    case OperatorNameId::LessEqualOp:
        _name += QLatin1String("<=");
        break;
    case OperatorNameId::GreaterEqualOp:
        _name += QLatin1String(">=");
        break;
    case OperatorNameId::AmpAmpOp:
        _name += QLatin1String("&&");
        break;
    case OperatorNameId::PipePipeOp:
        _name += QLatin1String("||");
        break;
    case OperatorNameId::PlusPlusOp:
        _name += QLatin1String("++");
        break;
    case OperatorNameId::MinusMinusOp:
        _name += QLatin1String("--");
        break;
    case OperatorNameId::CommaOp:
        _name += QLatin1String(",");
        break;
    case OperatorNameId::ArrowStarOp:
        _name += QLatin1String("->*");
        break;
    case OperatorNameId::ArrowOp:
        _name += QLatin1String("->");
        break;
    case OperatorNameId::FunctionCallOp:
        _name += QLatin1String("()");
        break;
    case OperatorNameId::ArrayAccessOp:
        _name += QLatin1String("[]");
        break;
    } // switch
}

Roberto Raggi's avatar
Roberto Raggi committed
244
void NamePrettyPrinter::visit(const ConversionNameId *name)
con's avatar
con committed
245
246
247
248
249
{
    _name += QLatin1String("operator ");
    _name += overview()->prettyType(name->type());
}

Roberto Raggi's avatar
Roberto Raggi committed
250
void NamePrettyPrinter::visit(const QualifiedNameId *name)
con's avatar
con committed
251
{
252
253
254
255
    if (name->base())
        _name += operator()(name->base());
    _name += QLatin1String("::");
    _name += operator()(name->name());
con's avatar
con committed
256
}
257

Roberto Raggi's avatar
Roberto Raggi committed
258
void NamePrettyPrinter::visit(const SelectorNameId *name)
259
260
{
    for (unsigned i = 0; i < name->nameCount(); ++i) {
Roberto Raggi's avatar
Roberto Raggi committed
261
        const Name *n = name->nameAt(i);
262
263
264
        if (!n)
            continue;

Roberto Raggi's avatar
Roberto Raggi committed
265
        if (const Identifier *id = n->identifier()) {
266
267
268
            _name += QString::fromLatin1(id->chars(), id->size());

            if (name->hasArguments() || name->nameCount() > 1)
269
                _name += QLatin1Char(':');
270
271
272
        }
    }
}
273
274
275
276
277

void NamePrettyPrinter::visit(const AnonymousNameId *name)
{
    _name = QString(QLatin1String("Anonymous:%1")).arg(name->classTokenIndex());
}