CheckName.cpp 11.7 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 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
**************************************************************************/
con's avatar
con committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) 2008 Roberto Raggi <roberto.raggi@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include "CheckName.h"
#include "Semantic.h"
#include "AST.h"
#include "Control.h"
#include "TranslationUnit.h"
#include "Literals.h"
#include "Names.h"
#include "CoreTypes.h"
#include "Symbols.h"
58
#include "Scope.h"
con's avatar
con committed
59
60
#include <cassert>

Roberto Raggi's avatar
Roberto Raggi committed
61
using namespace CPlusPlus;
con's avatar
con committed
62
63
64
65
66
67
68
69
70
71

CheckName::CheckName(Semantic *semantic)
    : SemanticCheck(semantic),
      _name(0),
      _scope(0)
{ }

CheckName::~CheckName()
{ }

Roberto Raggi's avatar
Roberto Raggi committed
72
const Name *CheckName::check(NameAST *name, Scope *scope)
con's avatar
con committed
73
{
Roberto Raggi's avatar
Roberto Raggi committed
74
    const Name *previousName = switchName(0);
con's avatar
con committed
75
76
    Scope *previousScope = switchScope(scope);
    accept(name);
77
78
79
80

    if (_name && name)
        name->name = _name;

con's avatar
con committed
81
82
83
84
    (void) switchScope(previousScope);
    return switchName(previousName);
}

Roberto Raggi's avatar
Roberto Raggi committed
85
const Name *CheckName::check(NestedNameSpecifierListAST *nested_name_specifier_list, Scope *scope)
con's avatar
con committed
86
{
Roberto Raggi's avatar
Roberto Raggi committed
87
    const Name *previousName = switchName(0);
con's avatar
con committed
88
89
    Scope *previousScope = switchScope(scope);

Roberto Raggi's avatar
Roberto Raggi committed
90
    std::vector<const Name *> names;
Roberto Raggi's avatar
Roberto Raggi committed
91
92
93
    for (NestedNameSpecifierListAST *it = nested_name_specifier_list; it; it = it->next) {
        NestedNameSpecifierAST *nested_name_specifier = it->value;
        names.push_back(semantic()->check(nested_name_specifier->class_or_namespace_name, _scope));
con's avatar
con committed
94
    }
Roberto Raggi's avatar
Roberto Raggi committed
95
96
97

    if (! names.empty())
        _name = control()->qualifiedNameId(&names[0], names.size());
con's avatar
con committed
98
99
100
101
102

    (void) switchScope(previousScope);
    return switchName(previousName);
}

Roberto Raggi's avatar
Roberto Raggi committed
103
const Name *CheckName::check(ObjCSelectorAST *args, Scope *scope)
104
{
Roberto Raggi's avatar
Roberto Raggi committed
105
    const Name *previousName = switchName(0);
106
107
108
109
110
111
112
113
    Scope *previousScope = switchScope(scope);

    accept(args);

    (void) switchScope(previousScope);
    return switchName(previousName);
}

114
115
void CheckName::check(ObjCMessageArgumentDeclarationAST *arg, Scope *scope)
{
Roberto Raggi's avatar
Roberto Raggi committed
116
    const Name *previousName = switchName(0);
117
118
119
120
121
122
123
124
    Scope *previousScope = switchScope(scope);

    accept(arg);

    (void) switchScope(previousScope);
    (void) switchName(previousName);
}

Roberto Raggi's avatar
Roberto Raggi committed
125
const Name *CheckName::switchName(const Name *name)
con's avatar
con committed
126
{
Roberto Raggi's avatar
Roberto Raggi committed
127
    const Name *previousName = _name;
con's avatar
con committed
128
129
130
131
132
133
134
135
136
137
138
139
140
    _name = name;
    return previousName;
}

Scope *CheckName::switchScope(Scope *scope)
{
    Scope *previousScope = _scope;
    _scope = scope;
    return previousScope;
}

bool CheckName::visit(QualifiedNameAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
141
    std::vector<const Name *> names;
Roberto Raggi's avatar
Roberto Raggi committed
142
    for (NestedNameSpecifierListAST *it = ast->nested_name_specifier_list; it; it = it->next) {
Roberto Raggi's avatar
Roberto Raggi committed
143
144
        NestedNameSpecifierAST *nested_name_specifier = it->value;
        names.push_back(semantic()->check(nested_name_specifier->class_or_namespace_name, _scope));
con's avatar
con committed
145
146
    }
    names.push_back(semantic()->check(ast->unqualified_name, _scope));
Roberto Raggi's avatar
Roberto Raggi committed
147
    _name = control()->qualifiedNameId(&names[0], names.size(), ast->global_scope_token != 0);
Roberto Raggi's avatar
Roberto Raggi committed
148
149

    ast->name = _name;
con's avatar
con committed
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
    return false;
}

bool CheckName::visit(OperatorFunctionIdAST *ast)
{
    assert(ast->op != 0);

    OperatorNameId::Kind kind = OperatorNameId::InvalidOp;

    switch (tokenKind(ast->op->op_token)) {
    case T_NEW:
        if (ast->op->open_token)
            kind = OperatorNameId::NewArrayOp;
        else
            kind = OperatorNameId::NewOp;
        break;

    case T_DELETE:
        if (ast->op->open_token)
            kind = OperatorNameId::DeleteArrayOp;
        else
            kind = OperatorNameId::DeleteOp;
        break;

    case T_PLUS:
        kind = OperatorNameId::PlusOp;
        break;

    case T_MINUS:
        kind = OperatorNameId::MinusOp;
        break;

    case T_STAR:
        kind = OperatorNameId::StarOp;
        break;

    case T_SLASH:
        kind = OperatorNameId::SlashOp;
        break;

    case T_PERCENT:
        kind = OperatorNameId::PercentOp;
        break;

    case T_CARET:
        kind = OperatorNameId::CaretOp;
        break;

    case T_AMPER:
        kind = OperatorNameId::AmpOp;
        break;

    case T_PIPE:
        kind = OperatorNameId::PipeOp;
        break;

    case T_TILDE:
        kind = OperatorNameId::TildeOp;
        break;

    case T_EXCLAIM:
        kind = OperatorNameId::ExclaimOp;
        break;

    case T_EQUAL:
        kind = OperatorNameId::EqualOp;
        break;

    case T_LESS:
        kind = OperatorNameId::LessOp;
        break;

    case T_GREATER:
        kind = OperatorNameId::GreaterOp;
        break;

    case T_PLUS_EQUAL:
        kind = OperatorNameId::PlusEqualOp;
        break;

    case T_MINUS_EQUAL:
        kind = OperatorNameId::MinusEqualOp;
        break;

    case T_STAR_EQUAL:
        kind = OperatorNameId::StarEqualOp;
        break;

    case T_SLASH_EQUAL:
        kind = OperatorNameId::SlashEqualOp;
        break;

    case T_PERCENT_EQUAL:
        kind = OperatorNameId::PercentEqualOp;
        break;

    case T_CARET_EQUAL:
        kind = OperatorNameId::CaretEqualOp;
        break;

    case T_AMPER_EQUAL:
        kind = OperatorNameId::AmpEqualOp;
        break;

    case T_PIPE_EQUAL:
        kind = OperatorNameId::PipeEqualOp;
        break;

    case T_LESS_LESS:
        kind = OperatorNameId::LessLessOp;
        break;

    case T_GREATER_GREATER:
        kind = OperatorNameId::GreaterGreaterOp;
        break;

    case T_LESS_LESS_EQUAL:
        kind = OperatorNameId::LessLessEqualOp;
        break;

    case T_GREATER_GREATER_EQUAL:
        kind = OperatorNameId::GreaterGreaterEqualOp;
        break;

    case T_EQUAL_EQUAL:
        kind = OperatorNameId::EqualEqualOp;
        break;

    case T_EXCLAIM_EQUAL:
        kind = OperatorNameId::ExclaimEqualOp;
        break;

    case T_LESS_EQUAL:
        kind = OperatorNameId::LessEqualOp;
        break;

    case T_GREATER_EQUAL:
        kind = OperatorNameId::GreaterEqualOp;
        break;

    case T_AMPER_AMPER:
        kind = OperatorNameId::AmpAmpOp;
        break;

    case T_PIPE_PIPE:
        kind = OperatorNameId::PipePipeOp;
        break;

    case T_PLUS_PLUS:
        kind = OperatorNameId::PlusPlusOp;
        break;

    case T_MINUS_MINUS:
        kind = OperatorNameId::MinusMinusOp;
        break;

    case T_COMMA:
        kind = OperatorNameId::CommaOp;
        break;

    case T_ARROW_STAR:
        kind = OperatorNameId::ArrowStarOp;
        break;

    case T_ARROW:
        kind = OperatorNameId::ArrowOp;
        break;

    case T_LPAREN:
        kind = OperatorNameId::FunctionCallOp;
        break;

    case T_LBRACKET:
        kind = OperatorNameId::ArrayAccessOp;
        break;

    default:
        kind = OperatorNameId::InvalidOp;
    } // switch

    _name = control()->operatorNameId(kind);
Roberto Raggi's avatar
Roberto Raggi committed
331
    ast->name = _name;
con's avatar
con committed
332
333
334
335
336
    return false;
}

bool CheckName::visit(ConversionFunctionIdAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
337
338
    FullySpecifiedType ty = semantic()->check(ast->type_specifier_list, _scope);
    ty = semantic()->check(ast->ptr_operator_list, ty, _scope);
con's avatar
con committed
339
340
341
342
343
344
    _name = control()->conversionNameId(ty);
    return false;
}

bool CheckName::visit(SimpleNameAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
345
    const Identifier *id = identifier(ast->identifier_token);
con's avatar
con committed
346
    _name = control()->nameId(id);
Roberto Raggi's avatar
Roberto Raggi committed
347
    ast->name = _name;
con's avatar
con committed
348
349
350
351
352
    return false;
}

bool CheckName::visit(DestructorNameAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
353
    const Identifier *id = identifier(ast->identifier_token);
con's avatar
con committed
354
    _name = control()->destructorNameId(id);
Roberto Raggi's avatar
Roberto Raggi committed
355
    ast->name = _name;
con's avatar
con committed
356
357
358
359
360
    return false;
}

bool CheckName::visit(TemplateIdAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
361
    const Identifier *id = identifier(ast->identifier_token);
con's avatar
con committed
362
    std::vector<FullySpecifiedType> templateArguments;
Roberto Raggi's avatar
Roberto Raggi committed
363
    for (TemplateArgumentListAST *it = ast->template_argument_list; it;
con's avatar
con committed
364
            it = it->next) {
365
        ExpressionAST *arg = it->value;
con's avatar
con committed
366
367
368
369
370
371
372
373
        FullySpecifiedType exprTy = semantic()->check(arg, _scope);
        templateArguments.push_back(exprTy);
    }
    if (templateArguments.empty())
        _name = control()->templateNameId(id);
    else
        _name = control()->templateNameId(id, &templateArguments[0],
                                          templateArguments.size());
Roberto Raggi's avatar
Roberto Raggi committed
374
    ast->name = _name;
con's avatar
con committed
375
376
377
    return false;
}

378
379
bool CheckName::visit(ObjCSelectorWithoutArgumentsAST *ast)
{
380
    if (ast->name_token) {
Roberto Raggi's avatar
Roberto Raggi committed
381
        std::vector<const Name *> names;
Roberto Raggi's avatar
Roberto Raggi committed
382
        const Identifier *id = control()->findOrInsertIdentifier(spell(ast->name_token));
Roberto Raggi's avatar
Roberto Raggi committed
383
        const NameId *nameId = control()->nameId(id);
384
385
386
387
        names.push_back(nameId);
        _name = control()->selectorNameId(&names[0], names.size(), false);
        ast->selector_name = _name;
    }
388
389
390
391
392
393

    return false;
}

bool CheckName::visit(ObjCSelectorWithArgumentsAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
394
    std::vector<const Name *> names;
Roberto Raggi's avatar
Roberto Raggi committed
395
    for (ObjCSelectorArgumentListAST *it = ast->selector_argument_list; it; it = it->next) {
396
        if (it->value->name_token) {
Roberto Raggi's avatar
Roberto Raggi committed
397
            const Identifier *id = control()->findOrInsertIdentifier(spell(it->value->name_token));
Roberto Raggi's avatar
Roberto Raggi committed
398
            const NameId *nameId = control()->nameId(id);
399
400
401
402
403
            names.push_back(nameId);
        } else {
            // we have an incomplete name due, probably due to error recovery. So, back out completely
            return false;
        }
404
    }
405
406
407
408
409

    if (!names.empty()) {
        _name = control()->selectorNameId(&names[0], names.size(), true);
        ast->selector_name = _name;
    }
410
411
412
413

    return false;
}

414
415
416
417
418
419
420
421
bool CheckName::visit(ObjCMessageArgumentDeclarationAST *ast)
{
    FullySpecifiedType type;

    if (ast->type_name)
        type = semantic()->check(ast->type_name, _scope);

    if (ast->param_name_token) {
Roberto Raggi's avatar
Roberto Raggi committed
422
        const Identifier *id = identifier(ast->param_name_token);
423
424
425
        _name = control()->nameId(id);
        ast->name = _name;

426
        Argument *arg = control()->newArgument(ast->param_name_token, _name);
427
428
        ast->argument = arg;
        arg->setType(type);
429
        arg->setInitializer(0);
430
431
432
433
434
435
        _scope->enterSymbol(arg);
    }

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
436