NamePrettyPrinter.cpp 7.96 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 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
**
con's avatar
con committed
9
** No Commercial Usage
10
**
con's avatar
con committed
11
12
13
14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
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
**
con's avatar
con committed
25
26
27
28
29
30
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
con's avatar
con committed
31
**
32
**************************************************************************/
hjk's avatar
hjk committed
33

con's avatar
con committed
34
#include "NamePrettyPrinter.h"
hjk's avatar
hjk committed
35

con's avatar
con committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#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
51
52
53
{
    return _overview;
}
con's avatar
con committed
54

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

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

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

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

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

Roberto Raggi's avatar
Roberto Raggi committed
109
void NamePrettyPrinter::visit(const OperatorNameId *name)
con's avatar
con committed
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
{
    _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
245
void NamePrettyPrinter::visit(const ConversionNameId *name)
con's avatar
con committed
246
247
248
249
250
{
    _name += QLatin1String("operator ");
    _name += overview()->prettyType(name->type());
}

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