NamePrettyPrinter.cpp 7.92 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
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).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** 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.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

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

con's avatar
con committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <Names.h>
#include <Overview.h>
#include <NameVisitor.h>
#include <Literals.h>

using namespace CPlusPlus;

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

NamePrettyPrinter::~NamePrettyPrinter()
{ }

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

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

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

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

Roberto Raggi's avatar
Roberto Raggi committed
74
void NamePrettyPrinter::visit(const TemplateNameId *name)
con's avatar
con committed
75
{
Roberto Raggi's avatar
Roberto Raggi committed
76
    const Identifier *id = name->identifier();
con's avatar
con committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    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;
    }
93
94
    if (! _name.isEmpty() && _name.at(_name.length() - 1) == '>')
        _name += QLatin1Char(' ');
con's avatar
con committed
95
96
97
    _name += QLatin1Char('>');
}

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

Roberto Raggi's avatar
Roberto Raggi committed
105
void NamePrettyPrinter::visit(const OperatorNameId *name)
con's avatar
con committed
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
{
    _name += QLatin1String("operator ");
    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
241
void NamePrettyPrinter::visit(const ConversionNameId *name)
con's avatar
con committed
242
243
244
245
246
{
    _name += QLatin1String("operator ");
    _name += overview()->prettyType(name->type());
}

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

    for (unsigned index = 0; index < name->nameCount(); ++index) {
        if (index != 0)
            _name += QLatin1String("::");
        _name += operator()(name->nameAt(index));
    }
}
258

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

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

            if (name->hasArguments() || name->nameCount() > 1)
                _name += ':';
        }
    }
}