CheckDeclaration.cpp 33.4 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
**************************************************************************/
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 "CheckDeclaration.h"
#include "Semantic.h"
#include "AST.h"
#include "TranslationUnit.h"
#include "Scope.h"
#include "Names.h"
#include "CoreTypes.h"
#include "Symbols.h"
#include "Control.h"
58
#include "Literals.h"
59
#include "QtContextKeywords.h"
60
#include <string>
con's avatar
con committed
61
62
#include <cassert>

Roberto Raggi's avatar
Roberto Raggi committed
63
using namespace CPlusPlus;
con's avatar
con committed
64
65
66
67
68
69
70
71
72
73
74
75
76

CheckDeclaration::CheckDeclaration(Semantic *semantic)
    : SemanticCheck(semantic),
      _declaration(0),
      _scope(0),
      _templateParameters(0),
      _checkAnonymousArguments(false)
{ }

CheckDeclaration::~CheckDeclaration()
{ }

void CheckDeclaration::check(DeclarationAST *declaration,
77
                             Scope *scope, TemplateParameters *templateParameters)
con's avatar
con committed
78
79
{
    Scope *previousScope = switchScope(scope);
80
    TemplateParameters *previousTemplateParameters = switchTemplateParameters(templateParameters);
con's avatar
con committed
81
82
83
84
85
86
87
    DeclarationAST *previousDeclaration = switchDeclaration(declaration);
    accept(declaration);
    (void) switchDeclaration(previousDeclaration);
    (void) switchTemplateParameters(previousTemplateParameters);
    (void) switchScope(previousScope);
}

88
89
90
91
92
93
94
void CheckDeclaration::check(CtorInitializerAST *ast, Scope *scope)
{
    Scope *previousScope = switchScope(scope);
    accept(ast);
    (void) switchScope(previousScope);
}

con's avatar
con committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
DeclarationAST *CheckDeclaration::switchDeclaration(DeclarationAST *declaration)
{
    DeclarationAST *previousDeclaration = _declaration;
    _declaration = declaration;
    return previousDeclaration;
}

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

109
TemplateParameters *CheckDeclaration::switchTemplateParameters(TemplateParameters *templateParameters)
con's avatar
con committed
110
{
111
    TemplateParameters *previousTemplateParameters = _templateParameters;
con's avatar
con committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
    _templateParameters = templateParameters;
    return previousTemplateParameters;
}

void CheckDeclaration::checkFunctionArguments(Function *fun)
{
    if (! _checkAnonymousArguments)
        return;

    if (_scope->isClassScope() && fun->isPublic()) {
        for (unsigned argc = 0; argc < fun->argumentCount(); ++argc) {
            Argument *arg = fun->argumentAt(argc)->asArgument();
            assert(arg != 0);

            if (! arg->name()) {
                translationUnit()->warning(arg->sourceLocation(),
                                           "anonymous argument");
            }
        }
    }
}

134
135
136
137
138
139
140
141
142
143
144
145
unsigned CheckDeclaration::locationOfDeclaratorId(DeclaratorAST *declarator) const
{
    if (declarator && declarator->core_declarator) {
        if (DeclaratorIdAST *declaratorId = declarator->core_declarator->asDeclaratorId())
            return declaratorId->firstToken();
        else if (NestedDeclaratorAST *nested = declarator->core_declarator->asNestedDeclarator())
            return locationOfDeclaratorId(nested->declarator);
    }

    return 0;
}

con's avatar
con committed
146
147
bool CheckDeclaration::visit(SimpleDeclarationAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
148
    FullySpecifiedType ty = semantic()->check(ast->decl_specifier_list, _scope);
con's avatar
con committed
149
150
    FullySpecifiedType qualTy = ty.qualifiedType();

151
152
    if (_templateParameters && ty) {
        if (Class *klass = ty->asClassType()) {
con's avatar
con committed
153
            klass->setTemplateParameters(_templateParameters);
154
            _templateParameters = 0; // consume the template parameters
con's avatar
con committed
155
156
157
        }
    }

158
159
    if (ast->decl_specifier_list && ! ast->declarator_list) {
        ElaboratedTypeSpecifierAST *elab_type_spec = ast->decl_specifier_list->value->asElaboratedTypeSpecifier();
160

161
162
163
164
        if (! elab_type_spec && ty.isFriend() && ast->decl_specifier_list->next && ! ast->decl_specifier_list->next->next) {
            // friend template class
            elab_type_spec = ast->decl_specifier_list->next->value->asElaboratedTypeSpecifier();
        }
165

166
167
        if (elab_type_spec) {
            unsigned sourceLocation = ast->decl_specifier_list->firstToken();
168

Roberto Raggi's avatar
Roberto Raggi committed
169
            const Name *name = semantic()->check(elab_type_spec->name, _scope);
170
            ForwardClassDeclaration *symbol =
171
                    control()->newForwardClassDeclaration(sourceLocation, name);
172
173
174
175
176

            if (_templateParameters) {
                symbol->setTemplateParameters(_templateParameters);
                _templateParameters = 0;
            }
177

178
179
            if (ty.isDeprecated())
                symbol->setDeprecated(true);
180
181
            if (ty.isUnavailable())
                symbol->setUnavailable(true);
182

183
184
185
            if (ty.isFriend())
                symbol->setStorage(Symbol::Friend);

186
187
188
189
190
            _scope->enterSymbol(symbol);
            return false;
        }
    }

191
192
    const bool isQ_SLOT   = ast->qt_invokable_token && tokenKind(ast->qt_invokable_token) == T_Q_SLOT;
    const bool isQ_SIGNAL = ast->qt_invokable_token && tokenKind(ast->qt_invokable_token) == T_Q_SIGNAL;
193
    const bool isQ_INVOKABLE = ast->qt_invokable_token && tokenKind(ast->qt_invokable_token) == T_Q_INVOKABLE;
194

Roberto Raggi's avatar
Roberto Raggi committed
195
    List<Declaration *> **decl_it = &ast->symbols;
Roberto Raggi's avatar
Roberto Raggi committed
196
    for (DeclaratorListAST *it = ast->declarator_list; it; it = it->next) {
Roberto Raggi's avatar
Roberto Raggi committed
197
        const Name *name = 0;
Roberto Raggi's avatar
Roberto Raggi committed
198
        FullySpecifiedType declTy = semantic()->check(it->value, qualTy,
con's avatar
con committed
199
200
                                                      _scope, &name);

Roberto Raggi's avatar
Roberto Raggi committed
201
        unsigned location = locationOfDeclaratorId(it->value);
202
        if (! location) {
Roberto Raggi's avatar
Roberto Raggi committed
203
204
            if (it->value)
                location = it->value->firstToken();
205
206
207
            else
                location = ast->firstToken();
        }
208

209
210
        Function *fun = 0;
        if (declTy && 0 != (fun = declTy->asFunctionType())) {
211
            fun->setSourceLocation(location, translationUnit());
con's avatar
con committed
212
213
214
            fun->setScope(_scope);
            fun->setName(name);
            fun->setMethodKey(semantic()->currentMethodKey());
215
            fun->setVirtual(ty.isVirtual());
216
217
            if (ty.isDeprecated())
                fun->setDeprecated(true);
218
219
            if (ty.isUnavailable())
                fun->setUnavailable(true);
220
221
222
223
            if (isQ_SIGNAL)
                fun->setMethodKey(Function::SignalMethod);
            else if (isQ_SLOT)
                fun->setMethodKey(Function::SlotMethod);
224
            else if (isQ_INVOKABLE)
225
                fun->setMethodKey(Function::InvokableMethod);
con's avatar
con committed
226
227
228
229
230
231
232
            fun->setVisibility(semantic()->currentVisibility());
        } else if (semantic()->currentMethodKey() != Function::NormalMethod) {
            translationUnit()->warning(ast->firstToken(),
                                       "expected a function declaration");
        }

        Declaration *symbol = control()->newDeclaration(location, name);
233

con's avatar
con committed
234
        symbol->setType(declTy);
235
236
        if (declTy.isDeprecated())
            symbol->setDeprecated(true);
237
238
        if (declTy.isUnavailable())
            symbol->setUnavailable(true);
con's avatar
con committed
239

240
        if (_templateParameters && it == ast->declarator_list) {
con's avatar
con committed
241
            symbol->setTemplateParameters(_templateParameters);
242
243
            _templateParameters = 0; // consume the template parameters
        }
con's avatar
con committed
244
245
246
247
248

        symbol->setVisibility(semantic()->currentVisibility());

        if (ty.isFriend())
            symbol->setStorage(Symbol::Friend);
249
250
        else if (ty.isAuto())
            symbol->setStorage(Symbol::Auto);
con's avatar
con committed
251
252
253
254
255
256
257
258
259
260
        else if (ty.isRegister())
            symbol->setStorage(Symbol::Register);
        else if (ty.isStatic())
            symbol->setStorage(Symbol::Static);
        else if (ty.isExtern())
            symbol->setStorage(Symbol::Extern);
        else if (ty.isMutable())
            symbol->setStorage(Symbol::Mutable);
        else if (ty.isTypedef())
            symbol->setStorage(Symbol::Typedef);
261
262

        if (ty.isDeprecated())
263
            symbol->setDeprecated(true);
264
265
        if (ty.isUnavailable())
            symbol->setUnavailable(true);
con's avatar
con committed
266

Roberto Raggi's avatar
Roberto Raggi committed
267
268
        if (it->value && it->value->initializer) {
            FullySpecifiedType initTy = semantic()->check(it->value->initializer, _scope);
269
270
        }

Roberto Raggi's avatar
Roberto Raggi committed
271
272
273
274
        *decl_it = new (translationUnit()->memoryPool()) List<Declaration *>();
        (*decl_it)->value = symbol;
        decl_it = &(*decl_it)->next;

con's avatar
con committed
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
        _scope->enterSymbol(symbol);
    }
    return false;
}

bool CheckDeclaration::visit(EmptyDeclarationAST *)
{
    return false;
}

bool CheckDeclaration::visit(AccessDeclarationAST *ast)
{
    int accessSpecifier = tokenKind(ast->access_specifier_token);
    int visibility = semantic()->visibilityForAccessSpecifier(accessSpecifier);
    semantic()->switchVisibility(visibility);
    if (ast->slots_token)
        semantic()->switchMethodKey(Function::SlotMethod);
292
    else if (accessSpecifier == T_Q_SIGNALS)
con's avatar
con committed
293
294
295
296
297
298
299
300
301
302
303
        semantic()->switchMethodKey(Function::SignalMethod);
    else
        semantic()->switchMethodKey(Function::NormalMethod);
    return false;
}

bool CheckDeclaration::visit(AsmDefinitionAST *)
{
    return false;
}

304
bool CheckDeclaration::visit(ExceptionDeclarationAST *ast)
con's avatar
con committed
305
{
Roberto Raggi's avatar
Roberto Raggi committed
306
    FullySpecifiedType ty = semantic()->check(ast->type_specifier_list, _scope);
307
308
    FullySpecifiedType qualTy = ty.qualifiedType();

Roberto Raggi's avatar
Roberto Raggi committed
309
    const Name *name = 0;
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
    FullySpecifiedType declTy = semantic()->check(ast->declarator, qualTy,
                                                  _scope, &name);

    unsigned location = locationOfDeclaratorId(ast->declarator);
    if (! location) {
        if (ast->declarator)
            location = ast->declarator->firstToken();
        else
            location = ast->firstToken();
    }

    Declaration *symbol = control()->newDeclaration(location, name);
    symbol->setType(declTy);
    _scope->enterSymbol(symbol);

con's avatar
con committed
325
326
327
328
329
    return false;
}

bool CheckDeclaration::visit(FunctionDefinitionAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
330
    FullySpecifiedType ty = semantic()->check(ast->decl_specifier_list, _scope);
con's avatar
con committed
331
    FullySpecifiedType qualTy = ty.qualifiedType();
Roberto Raggi's avatar
Roberto Raggi committed
332
    const Name *name = 0;
con's avatar
con committed
333
334
    FullySpecifiedType funTy = semantic()->check(ast->declarator, qualTy,
                                                 _scope, &name);
335
    if (! (funTy && funTy->isFunctionType())) {
con's avatar
con committed
336
337
338
339
340
        translationUnit()->error(ast->firstToken(),
                                 "expected a function prototype");
        return false;
    }

341
342
343
344
345
    unsigned funStartOffset = tokenAt(ast->firstToken()).offset;
    if (ast->declarator && ast->declarator->core_declarator) {
        funStartOffset = tokenAt(ast->declarator->core_declarator->lastToken() - 1).end();
    }

346
    Function *fun = funTy->asFunctionType();
347
    fun->setVirtual(ty.isVirtual());
348
349
    if (ty.isDeprecated())
        fun->setDeprecated(true);
350
351
    if (ty.isUnavailable())
        fun->setUnavailable(true);
352
353
    fun->members()->setStartOffset(funStartOffset);
    fun->members()->setEndOffset(tokenAt(ast->lastToken() - 1).end());
354
    if (ast->declarator)
355
        fun->setSourceLocation(ast->declarator->firstToken(), translationUnit());
con's avatar
con committed
356
357
358
359
360
    fun->setName(name);
    fun->setTemplateParameters(_templateParameters);
    fun->setVisibility(semantic()->currentVisibility());
    fun->setMethodKey(semantic()->currentMethodKey());

361
362
    const bool isQ_SLOT   = ast->qt_invokable_token && tokenKind(ast->qt_invokable_token) == T_Q_SLOT;
    const bool isQ_SIGNAL = ast->qt_invokable_token && tokenKind(ast->qt_invokable_token) == T_Q_SIGNAL;
363
    const bool isQ_INVOKABLE = ast->qt_invokable_token && tokenKind(ast->qt_invokable_token) == T_Q_INVOKABLE;
364
365
366
367
368

    if (isQ_SIGNAL)
        fun->setMethodKey(Function::SignalMethod);
    else if (isQ_SLOT)
        fun->setMethodKey(Function::SlotMethod);
369
    else if (isQ_INVOKABLE)
370
        fun->setMethodKey(Function::InvokableMethod);
371

con's avatar
con committed
372
373
    checkFunctionArguments(fun);

Roberto Raggi's avatar
Roberto Raggi committed
374
    ast->symbol = fun;
con's avatar
con committed
375
376
    _scope->enterSymbol(fun);

377
378
    if (! semantic()->skipFunctionBodies())
        semantic()->checkFunctionDefinition(ast);
con's avatar
con committed
379
380
381
382

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
383
384
385
bool CheckDeclaration::visit(MemInitializerAST *ast)
{
    (void) semantic()->check(ast->name, _scope);
386
387
388
    for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
        FullySpecifiedType ty = semantic()->check(it->value, _scope);
    }
Roberto Raggi's avatar
Roberto Raggi committed
389
390
391
    return false;
}

con's avatar
con committed
392
393
bool CheckDeclaration::visit(LinkageBodyAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
394
    for (DeclarationListAST *decl = ast->declaration_list; decl; decl = decl->next) {
395
       semantic()->check(decl->value, _scope);
con's avatar
con committed
396
397
398
399
400
401
    }
    return false;
}

bool CheckDeclaration::visit(LinkageSpecificationAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
402
    semantic()->check(ast->declaration, _scope);
con's avatar
con committed
403
404
405
406
407
    return false;
}

bool CheckDeclaration::visit(NamespaceAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
408
409
410
    const Name *namespaceName = 0;
    if (const Identifier *id = identifier(ast->identifier_token))
        namespaceName = control()->nameId(id);
411
412
413
414

    unsigned sourceLocation = ast->firstToken();
    if (ast->identifier_token)
        sourceLocation = ast->identifier_token;
415
416
417
    unsigned scopeStart = tokenAt(ast->firstToken()).offset;
    if (ast->linkage_body && ast->linkage_body->firstToken())
        scopeStart = tokenAt(ast->linkage_body->firstToken()).offset;
418
419

    Namespace *ns = control()->newNamespace(sourceLocation, namespaceName);
420
421
    ns->members()->setStartOffset(scopeStart);
    ns->members()->setEndOffset(tokenAt(ast->lastToken() - 1).end());
Roberto Raggi's avatar
Roberto Raggi committed
422
    ast->symbol = ns;
con's avatar
con committed
423
424
425
426
427
428
    _scope->enterSymbol(ns);
    semantic()->check(ast->linkage_body, ns->members()); // ### we'll do the merge later.

    return false;
}

429
bool CheckDeclaration::visit(NamespaceAliasDefinitionAST *ast)
con's avatar
con committed
430
{
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
    const Name *name = 0;

    if (const Identifier *id = identifier(ast->namespace_name_token))
        name = control()->nameId(id);

    unsigned sourceLocation = ast->firstToken();

    if (ast->namespace_name_token)
        sourceLocation = ast->namespace_name_token;

    const Name *namespaceName = semantic()->check(ast->name, _scope);

    NamespaceAlias *namespaceAlias = control()->newNamespaceAlias(sourceLocation, name);
    namespaceAlias->setNamespaceName(namespaceName);
    //ast->symbol = namespaceAlias;
    _scope->enterSymbol(namespaceAlias);

con's avatar
con committed
448
449
450
451
452
    return false;
}

bool CheckDeclaration::visit(ParameterDeclarationAST *ast)
{
453
454
455
456
457
458
459
    unsigned sourceLocation = locationOfDeclaratorId(ast->declarator);
    if (! sourceLocation) {
        if (ast->declarator)
            sourceLocation = ast->declarator->firstToken();
        else
            sourceLocation = ast->firstToken();
    }
460

Roberto Raggi's avatar
Roberto Raggi committed
461
    const Name *argName = 0;
Roberto Raggi's avatar
Roberto Raggi committed
462
    FullySpecifiedType ty = semantic()->check(ast->type_specifier_list, _scope);
con's avatar
con committed
463
464
465
    FullySpecifiedType argTy = semantic()->check(ast->declarator, ty.qualifiedType(),
                                                 _scope, &argName);
    FullySpecifiedType exprTy = semantic()->check(ast->expression, _scope);
466
    Argument *arg = control()->newArgument(sourceLocation, argName);
Roberto Raggi's avatar
Roberto Raggi committed
467
    ast->symbol = arg;
468
469
470
471
472
473
474
475
476
477
478
479
480
    if (ast->expression) {
        unsigned startOfExpression = ast->expression->firstToken();
        unsigned endOfExpression = ast->expression->lastToken();
        std::string buffer;
        for (unsigned index = startOfExpression; index != endOfExpression; ++index) {
            const Token &tk = tokenAt(index);
            if (tk.whitespace() || tk.newline())
                buffer += ' ';
            buffer += tk.spell();
        }
        const StringLiteral *initializer = control()->findOrInsertStringLiteral(buffer.c_str(), buffer.size());
        arg->setInitializer(initializer);
    }
con's avatar
con committed
481
482
483
484
485
486
487
    arg->setType(argTy);
    _scope->enterSymbol(arg);
    return false;
}

bool CheckDeclaration::visit(TemplateDeclarationAST *ast)
{
488
489
    Scope *scope = new Scope(_scope->owner());

Roberto Raggi's avatar
Roberto Raggi committed
490
    for (DeclarationListAST *param = ast->template_parameter_list; param; param = param->next) {
491
       semantic()->check(param->value, scope);
con's avatar
con committed
492
493
    }

494
495
496
    semantic()->check(ast->declaration, _scope,
                      new TemplateParameters(_templateParameters, scope));

con's avatar
con committed
497
498
499
500
501
    return false;
}

bool CheckDeclaration::visit(TypenameTypeParameterAST *ast)
{
502
503
504
505
    unsigned sourceLocation = ast->firstToken();
    if (ast->name)
        sourceLocation = ast->name->firstToken();

Roberto Raggi's avatar
Roberto Raggi committed
506
    const Name *name = semantic()->check(ast->name, _scope);
507
508
509
    TypenameArgument *arg = control()->newTypenameArgument(sourceLocation, name);
    FullySpecifiedType ty = semantic()->check(ast->type_id, _scope);
    arg->setType(ty);
Roberto Raggi's avatar
Roberto Raggi committed
510
    ast->symbol = arg;
con's avatar
con committed
511
512
513
514
515
516
    _scope->enterSymbol(arg);
    return false;
}

bool CheckDeclaration::visit(TemplateTypeParameterAST *ast)
{
517
518
519
520
    unsigned sourceLocation = ast->firstToken();
    if (ast->name)
        sourceLocation = ast->name->firstToken();

Roberto Raggi's avatar
Roberto Raggi committed
521
    const Name *name = semantic()->check(ast->name, _scope);
522
523
524
    TypenameArgument *arg = control()->newTypenameArgument(sourceLocation, name);
    FullySpecifiedType ty = semantic()->check(ast->type_id, _scope);
    arg->setType(ty);
Roberto Raggi's avatar
Roberto Raggi committed
525
    ast->symbol = arg;
con's avatar
con committed
526
527
528
529
530
531
    _scope->enterSymbol(arg);
    return false;
}

bool CheckDeclaration::visit(UsingAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
532
    const Name *name = semantic()->check(ast->name, _scope);
533
534
535
536
537
538

    unsigned sourceLocation = ast->firstToken();
    if (ast->name)
        sourceLocation = ast->name->firstToken();

    UsingDeclaration *u = control()->newUsingDeclaration(sourceLocation, name);
Roberto Raggi's avatar
Roberto Raggi committed
539
    ast->symbol = u;
con's avatar
con committed
540
541
542
543
544
545
    _scope->enterSymbol(u);
    return false;
}

bool CheckDeclaration::visit(UsingDirectiveAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
546
    const Name *name = semantic()->check(ast->name, _scope);
547
548
549
550
551
552

    unsigned sourceLocation = ast->firstToken();
    if (ast->name)
        sourceLocation = ast->name->firstToken();

    UsingNamespaceDirective *u = control()->newUsingNamespaceDirective(sourceLocation, name);
Roberto Raggi's avatar
Roberto Raggi committed
553
    ast->symbol = u;
con's avatar
con committed
554
    _scope->enterSymbol(u);
555
556
557
558
559

    if (! (_scope->isBlockScope() || _scope->isNamespaceScope()))
        translationUnit()->error(ast->firstToken(),
                                 "using-directive not within namespace or block scope");

con's avatar
con committed
560
561
562
    return false;
}

Erik Verbruggen's avatar
Erik Verbruggen committed
563
bool CheckDeclaration::visit(ObjCProtocolForwardDeclarationAST *ast)
564
565
566
567
{
    const unsigned sourceLocation = ast->firstToken();

    List<ObjCForwardProtocolDeclaration *> **symbolIter = &ast->symbols;
568
    for (NameListAST *it = ast->identifier_list; it; it = it->next) {
569
        unsigned declarationLocation;
Roberto Raggi's avatar
Roberto Raggi committed
570
571
        if (it->value)
            declarationLocation = it->value->firstToken();
572
573
574
        else
            declarationLocation = sourceLocation;

Roberto Raggi's avatar
Roberto Raggi committed
575
        const Name *protocolName = semantic()->check(it->value, _scope);
576
577
578
579
580
581
582
583
584
585
586
587
        ObjCForwardProtocolDeclaration *fwdProtocol = control()->newObjCForwardProtocolDeclaration(sourceLocation, protocolName);

        _scope->enterSymbol(fwdProtocol);

        *symbolIter = new (translationUnit()->memoryPool()) List<ObjCForwardProtocolDeclaration *>();
        (*symbolIter)->value = fwdProtocol;
        symbolIter = &(*symbolIter)->next;
    }

    return false;
}

588
589
590
591
592
593
594
595
596
597
598
599
unsigned CheckDeclaration::calculateScopeStart(ObjCProtocolDeclarationAST *ast) const
{
    if (ast->protocol_refs)
        if (unsigned pos = ast->protocol_refs->lastToken())
            return tokenAt(pos - 1).end();
    if (ast->name)
        if (unsigned pos = ast->name->lastToken())
            return tokenAt(pos - 1).end();

    return tokenAt(ast->firstToken()).offset;
}

Erik Verbruggen's avatar
Erik Verbruggen committed
600
bool CheckDeclaration::visit(ObjCProtocolDeclarationAST *ast)
601
602
603
604
605
606
607
{
    unsigned sourceLocation;
    if (ast->name)
        sourceLocation = ast->name->firstToken();
    else
        sourceLocation = ast->firstToken();

Roberto Raggi's avatar
Roberto Raggi committed
608
    const Name *protocolName = semantic()->check(ast->name, _scope);
609
    ObjCProtocol *protocol = control()->newObjCProtocol(sourceLocation, protocolName);
610
611
    protocol->members()->setStartOffset(calculateScopeStart(ast));
    protocol->members()->setEndOffset(tokenAt(ast->lastToken() - 1).end());
612

613
    if (ast->protocol_refs && ast->protocol_refs->identifier_list) {
614
        for (NameListAST *iter = ast->protocol_refs->identifier_list; iter; iter = iter->next) {
Roberto Raggi's avatar
Roberto Raggi committed
615
            NameAST* name = iter->value;
Roberto Raggi's avatar
Roberto Raggi committed
616
            const Name *protocolName = semantic()->check(name, _scope);
617
618
619
620
621
622
            ObjCBaseProtocol *baseProtocol = control()->newObjCBaseProtocol(name->firstToken(), protocolName);
            protocol->addProtocol(baseProtocol);
        }
    }

    int previousObjCVisibility = semantic()->switchObjCVisibility(Function::Public);
Roberto Raggi's avatar
Roberto Raggi committed
623
    for (DeclarationListAST *it = ast->member_declaration_list; it; it = it->next) {
624
        semantic()->check(it->value, protocol->members());
625
626
627
628
    }
    (void) semantic()->switchObjCVisibility(previousObjCVisibility);

    ast->symbol = protocol;
629
630
631
632
633
    _scope->enterSymbol(protocol);

    return false;
}

Erik Verbruggen's avatar
Erik Verbruggen committed
634
bool CheckDeclaration::visit(ObjCClassForwardDeclarationAST *ast)
635
636
637
638
{
    const unsigned sourceLocation = ast->firstToken();

    List<ObjCForwardClassDeclaration *> **symbolIter = &ast->symbols;
639
    for (NameListAST *it = ast->identifier_list; it; it = it->next) {
640
        unsigned declarationLocation;
Roberto Raggi's avatar
Roberto Raggi committed
641
642
        if (it->value)
            declarationLocation = it->value->firstToken();
643
644
645
        else
            declarationLocation = sourceLocation;

Roberto Raggi's avatar
Roberto Raggi committed
646
        const Name *className = semantic()->check(it->value, _scope);
647
648
649
650
651
652
653
654
655
656
657
658
        ObjCForwardClassDeclaration *fwdClass = control()->newObjCForwardClassDeclaration(sourceLocation, className);

        _scope->enterSymbol(fwdClass);

        *symbolIter = new (translationUnit()->memoryPool()) List<ObjCForwardClassDeclaration *>();
        (*symbolIter)->value = fwdClass;
        symbolIter = &(*symbolIter)->next;
    }

    return false;
}

659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
unsigned CheckDeclaration::calculateScopeStart(ObjCClassDeclarationAST *ast) const
{
    if (ast->inst_vars_decl)
        if (unsigned pos = ast->inst_vars_decl->lbrace_token)
            return tokenAt(pos).end();

    if (ast->protocol_refs)
        if (unsigned pos = ast->protocol_refs->lastToken())
            return tokenAt(pos - 1).end();

    if (ast->superclass)
        if (unsigned pos = ast->superclass->lastToken())
            return tokenAt(pos - 1).end();

    if (ast->colon_token)
        return tokenAt(ast->colon_token).end();

    if (ast->rparen_token)
        return tokenAt(ast->rparen_token).end();

    if (ast->category_name)
        if (unsigned pos = ast->category_name->lastToken())
            return tokenAt(pos - 1).end();

    if (ast->lparen_token)
        return tokenAt(ast->lparen_token).end();

    if (ast->class_name)
        if (unsigned pos = ast->class_name->lastToken())
            return tokenAt(pos - 1).end();

    return tokenAt(ast->firstToken()).offset;
}

Erik Verbruggen's avatar
Erik Verbruggen committed
693
bool CheckDeclaration::visit(ObjCClassDeclarationAST *ast)
694
695
696
697
698
699
700
{
    unsigned sourceLocation;
    if (ast->class_name)
        sourceLocation = ast->class_name->firstToken();
    else
        sourceLocation = ast->firstToken();

Roberto Raggi's avatar
Roberto Raggi committed
701
    const Name *className = semantic()->check(ast->class_name, _scope);
702
    ObjCClass *klass = control()->newObjCClass(sourceLocation, className);
703
704
    klass->members()->setStartOffset(calculateScopeStart(ast));
    klass->members()->setEndOffset(tokenAt(ast->lastToken() - 1).offset);
705
706
    ast->symbol = klass;

707
708
709
    klass->setInterface(ast->interface_token != 0);

    if (ast->category_name) {
Roberto Raggi's avatar
Roberto Raggi committed
710
        const Name *categoryName = semantic()->check(ast->category_name, _scope);
711
712
713
        klass->setCategoryName(categoryName);
    }

714
    if (ast->superclass) {
Roberto Raggi's avatar
Roberto Raggi committed
715
        const Name *superClassName = semantic()->check(ast->superclass, _scope);
716
717
718
719
720
        ObjCBaseClass *superKlass = control()->newObjCBaseClass(ast->superclass->firstToken(), superClassName);
        klass->setBaseClass(superKlass);
    }

    if (ast->protocol_refs && ast->protocol_refs->identifier_list) {
721
        for (NameListAST *iter = ast->protocol_refs->identifier_list; iter; iter = iter->next) {
Roberto Raggi's avatar
Roberto Raggi committed
722
            NameAST* name = iter->value;
Roberto Raggi's avatar
Roberto Raggi committed
723
            const Name *protocolName = semantic()->check(name, _scope);
724
725
726
727
728
            ObjCBaseProtocol *baseProtocol = control()->newObjCBaseProtocol(name->firstToken(), protocolName);
            klass->addProtocol(baseProtocol);
        }
    }

729
730
731
732
733
    _scope->enterSymbol(klass);

    int previousObjCVisibility = semantic()->switchObjCVisibility(Function::Protected);

    if (ast->inst_vars_decl) {
Roberto Raggi's avatar
Roberto Raggi committed
734
        for (DeclarationListAST *it = ast->inst_vars_decl->instance_variable_list; it; it = it->next) {
735
            semantic()->check(it->value, klass->members());
736
737
738
        }
    }

Erik Verbruggen's avatar
Erik Verbruggen committed
739
740
    (void) semantic()->switchObjCVisibility(Function::Public);

Roberto Raggi's avatar
Roberto Raggi committed
741
    for (DeclarationListAST *it = ast->member_declaration_list; it; it = it->next) {
742
        semantic()->check(it->value, klass->members());
743
744
745
746
747
748
749
750
751
    }

    (void) semantic()->switchObjCVisibility(previousObjCVisibility);

    return false;
}

bool CheckDeclaration::visit(ObjCMethodDeclarationAST *ast)
{
752
753
754
755
756
    ObjCMethodPrototypeAST *methodProto = ast->method_prototype;
    if (!methodProto)
        return false;
    ObjCSelectorAST *selector = methodProto->selector;
    if (!selector)
757
758
        return false;

759
    FullySpecifiedType ty = semantic()->check(methodProto, _scope);
760
761
    ObjCMethod *methodTy = ty.type()->asObjCMethodType();
    if (!methodTy)
762
763
        return false;

764
    Symbol *symbol;
Erik Verbruggen's avatar
Erik Verbruggen committed
765
    if (ast->function_body) {
766
        symbol = methodTy;
767
768
        methodTy->members()->setStartOffset(tokenAt(ast->function_body->firstToken()).offset);
        methodTy->members()->setEndOffset(tokenAt(ast->lastToken() - 1).end());
Erik Verbruggen's avatar
Erik Verbruggen committed
769
    } else {
770
        Declaration *decl = control()->newDeclaration(selector->firstToken(), methodTy->name());
771
        decl->setType(methodTy);
Erik Verbruggen's avatar
Erik Verbruggen committed
772
        symbol = decl;
773
        symbol->setStorage(methodTy->storage());
774
    }
775

776
    symbol->setVisibility(semantic()->currentObjCVisibility());
777
778
    if (ty.isDeprecated())
        symbol->setDeprecated(true);
779
780
    if (ty.isUnavailable())
        symbol->setUnavailable(true);
781
782
783

    _scope->enterSymbol(symbol);

784
785
786
787
    if (ast->function_body && !semantic()->skipFunctionBodies()) {
        semantic()->check(ast->function_body, methodTy->members());
    }

788
789
790
    return false;
}

791
792
793
794
795
796
797
798
bool CheckDeclaration::visit(ObjCVisibilityDeclarationAST *ast)
{
    int accessSpecifier = tokenKind(ast->visibility_token);
    int visibility = semantic()->visibilityForObjCAccessSpecifier(accessSpecifier);
    semantic()->switchObjCVisibility(visibility);
    return false;
}

799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
bool CheckDeclaration::checkPropertyAttribute(ObjCPropertyAttributeAST *attrAst,
                                              int &flags,
                                              int attr)
{
    if (flags & attr) {
        translationUnit()->warning(attrAst->attribute_identifier_token,
                                   "duplicate property attribute \"%s\"",
                                   spell(attrAst->attribute_identifier_token));
        return false;
    } else {
        flags |= attr;
        return true;
    }
}

bool CheckDeclaration::visit(ObjCPropertyDeclarationAST *ast)
{
816
817
818
819
820
821
822
823
824
825
    semantic()->check(ast->simple_declaration, _scope);
    SimpleDeclarationAST *simpleDecl = ast->simple_declaration->asSimpleDeclaration();

    if (!simpleDecl) {
        translationUnit()->warning(ast->simple_declaration->firstToken(),
                                   "invalid type for property declaration");
        return false;
    }

    int propAttrs = ObjCPropertyDeclaration::None;
Roberto Raggi's avatar
Roberto Raggi committed
826
    const Name *getterName = 0, *setterName = 0;
827

Roberto Raggi's avatar
Roberto Raggi committed
828
    for (ObjCPropertyAttributeListAST *iter= ast->property_attribute_list; iter; iter = iter->next) {
829
        ObjCPropertyAttributeAST *attrAst = iter->value;
830
831
832
        if (!attrAst)
            continue;

Roberto Raggi's avatar
Roberto Raggi committed
833
        const Identifier *attrId = identifier(attrAst->attribute_identifier_token);
834
        if (attrId == control()->objcGetterId()) {
835
836
            if (checkPropertyAttribute(attrAst, propAttrs, ObjCPropertyDeclaration::Getter)) {
                getterName = semantic()->check(attrAst->method_selector, _scope);
837
            }
838
        } else if (attrId == control()->objcSetterId()) {
839
840
            if (checkPropertyAttribute(attrAst, propAttrs, ObjCPropertyDeclaration::Setter)) {
                setterName = semantic()->check(attrAst->method_selector, _scope);
841
            }
842
        } else if (attrId == control()->objcReadwriteId()) {
843
            checkPropertyAttribute(attrAst, propAttrs, ObjCPropertyDeclaration::ReadWrite);
844
        } else if (attrId == control()->objcReadonlyId()) {
845
            checkPropertyAttribute(attrAst, propAttrs, ObjCPropertyDeclaration::ReadOnly);
846
        } else if (attrId == control()->objcAssignId()) {
847
            checkPropertyAttribute(attrAst, propAttrs, ObjCPropertyDeclaration::Assign);
848
        } else if (attrId == control()->objcRetainId()) {
849
            checkPropertyAttribute(attrAst, propAttrs, ObjCPropertyDeclaration::Retain);
850
        } else if (attrId == control()->objcCopyId()) {
851
            checkPropertyAttribute(attrAst, propAttrs, ObjCPropertyDeclaration::Copy);
852
        } else if (attrId == control()->objcNonatomicId()) {
853
            checkPropertyAttribute(attrAst, propAttrs, ObjCPropertyDeclaration::NonAtomic);
854
855
856
        }
    }

857
858
    if (propAttrs & ObjCPropertyDeclaration::ReadOnly &&
        propAttrs & ObjCPropertyDeclaration::ReadWrite)
859
860
861
        // Should this be an error instead of only a warning?
        translationUnit()->warning(ast->property_token,
                                   "property can have at most one attribute \"readonly\" or \"readwrite\" specified");
862
    int setterSemAttrs = propAttrs & ObjCPropertyDeclaration::SetterSemanticsMask;
863
    if (setterSemAttrs
864
865
866
            && setterSemAttrs != ObjCPropertyDeclaration::Assign
            && setterSemAttrs != ObjCPropertyDeclaration::Retain
            && setterSemAttrs != ObjCPropertyDeclaration::Copy) {
867
868
869
870
871
        // Should this be an error instead of only a warning?
        translationUnit()->warning(ast->property_token,
                                   "property can have at most one attribute \"assign\", \"retain\", or \"copy\" specified");
    }

872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
    List<ObjCPropertyDeclaration *> **lastSymbols = &ast->symbols;
    for (List<Declaration*> *iter = simpleDecl->symbols; iter; iter = iter->next) {
        ObjCPropertyDeclaration *propDecl = control()->newObjCPropertyDeclaration(ast->firstToken(),
                                                                                  iter->value->name());
        propDecl->setType(iter->value->type());
        propDecl->setAttributes(propAttrs);
        propDecl->setGetterName(getterName);
        propDecl->setSetterName(setterName);
        _scope->enterSymbol(propDecl);

        *lastSymbols = new (translationUnit()->memoryPool()) List<ObjCPropertyDeclaration *>();
        (*lastSymbols)->value = propDecl;
        lastSymbols = &(*lastSymbols)->next;
    }

887
888
    return false;
}
889
890
891

bool CheckDeclaration::visit(QtEnumDeclarationAST *ast)
{
892
    checkQEnumsQFlagsNames(ast->enumerator_list, "Q_ENUMS");
893
894
895
896
897
    return false;
}

bool CheckDeclaration::visit(QtFlagsDeclarationAST *ast)
{
898
    checkQEnumsQFlagsNames(ast->flag_enums_list, "Q_FLAGS");
899
900
    return false;
}
901
902
903
904
905
906
907
908
909
910
911
912
913
914

bool CheckDeclaration::visit(QtPropertyDeclarationAST *ast)
{
    if (ast->type_id)
        semantic()->check(ast->type_id, _scope);
    if (ast->property_name)
        semantic()->check(ast->property_name, _scope);
    for (QtPropertyDeclarationItemListAST *iter = ast->property_declaration_items;
         iter; iter = iter->next) {
        if (iter->value)
            semantic()->check(iter->value->expression, _scope);
    }
    return false;
}
915

916
static bool checkEnumName(const Name *name)
917
{
918
919
    if (! name)
        return false;
920

921
922
    else if (name->asNameId() != 0)
        return true;
923

924
925
926
    else if (const QualifiedNameId *q = name->asQualifiedNameId()) {
        if (! q->base())
            return false; // global qualified name
927

928
929
930
931
932
933
934
935
936
937
938
939
940
        if (checkEnumName(q->base()) && checkEnumName(q->name()))
            return true;
    }

    return false;
}

void CheckDeclaration::checkQEnumsQFlagsNames(NameListAST *nameListAst, const char *declName)
{
    for (NameListAST *iter = nameListAst; iter; iter = iter->next) {
        if (const Name *name = semantic()->check(iter->value, _scope)) {
            if (! checkEnumName(name))
                translationUnit()->error(iter->firstToken(), "invalid name in %s", declName);
941
942
943
        }
    }
}