CheckDeclaration.cpp 31.7 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    DeclarationAST *previousDeclaration = switchDeclaration(declaration);
    accept(declaration);
    (void) switchDeclaration(previousDeclaration);
    (void) switchTemplateParameters(previousTemplateParameters);
    (void) switchScope(previousScope);
}

DeclarationAST *CheckDeclaration::switchDeclaration(DeclarationAST *declaration)
{
    DeclarationAST *previousDeclaration = _declaration;
    _declaration = declaration;
    return previousDeclaration;
}

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

102
TemplateParameters *CheckDeclaration::switchTemplateParameters(TemplateParameters *templateParameters)
con's avatar
con committed
103
{
104
    TemplateParameters *previousTemplateParameters = _templateParameters;
con's avatar
con committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    _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");
            }
        }
    }
}

127
128
129
130
131
132
133
134
135
136
137
138
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
139
140
bool CheckDeclaration::visit(SimpleDeclarationAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
141
    FullySpecifiedType ty = semantic()->check(ast->decl_specifier_list, _scope);
con's avatar
con committed
142
143
    FullySpecifiedType qualTy = ty.qualifiedType();

144
145
    if (_templateParameters && ty) {
        if (Class *klass = ty->asClassType()) {
con's avatar
con committed
146
            klass->setTemplateParameters(_templateParameters);
147
            _templateParameters = 0; // consume the template parameters
con's avatar
con committed
148
149
150
        }
    }

151
152
    if (ast->decl_specifier_list && ! ast->declarator_list) {
        ElaboratedTypeSpecifierAST *elab_type_spec = ast->decl_specifier_list->value->asElaboratedTypeSpecifier();
153

154
155
156
157
        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();
        }
158

159
160
        if (elab_type_spec) {
            unsigned sourceLocation = ast->decl_specifier_list->firstToken();
161

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

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

            _scope->enterSymbol(symbol);
            return false;
        }
    }

176
177
    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;
178
    const bool isQ_INVOKABLE = ast->qt_invokable_token && tokenKind(ast->qt_invokable_token) == T_Q_INVOKABLE;
179

Roberto Raggi's avatar
Roberto Raggi committed
180
    List<Declaration *> **decl_it = &ast->symbols;
Roberto Raggi's avatar
Roberto Raggi committed
181
    for (DeclaratorListAST *it = ast->declarator_list; it; it = it->next) {
Roberto Raggi's avatar
Roberto Raggi committed
182
        const Name *name = 0;
Roberto Raggi's avatar
Roberto Raggi committed
183
        FullySpecifiedType declTy = semantic()->check(it->value, qualTy,
con's avatar
con committed
184
185
                                                      _scope, &name);

Roberto Raggi's avatar
Roberto Raggi committed
186
        unsigned location = locationOfDeclaratorId(it->value);
187
        if (! location) {
Roberto Raggi's avatar
Roberto Raggi committed
188
189
            if (it->value)
                location = it->value->firstToken();
190
191
192
            else
                location = ast->firstToken();
        }
193

194
195
        Function *fun = 0;
        if (declTy && 0 != (fun = declTy->asFunctionType())) {
196
            fun->setSourceLocation(location);
con's avatar
con committed
197
198
199
            fun->setScope(_scope);
            fun->setName(name);
            fun->setMethodKey(semantic()->currentMethodKey());
200
            fun->setVirtual(ty.isVirtual());
201
202
203
204
            if (isQ_SIGNAL)
                fun->setMethodKey(Function::SignalMethod);
            else if (isQ_SLOT)
                fun->setMethodKey(Function::SlotMethod);
205
            else if (isQ_INVOKABLE)
206
                fun->setMethodKey(Function::InvokableMethod);
con's avatar
con committed
207
208
209
210
211
212
213
            fun->setVisibility(semantic()->currentVisibility());
        } else if (semantic()->currentMethodKey() != Function::NormalMethod) {
            translationUnit()->warning(ast->firstToken(),
                                       "expected a function declaration");
        }

        Declaration *symbol = control()->newDeclaration(location, name);
214
215
216
        symbol->setStartOffset(tokenAt(ast->firstToken()).offset);
        symbol->setEndOffset(tokenAt(ast->lastToken()).offset);

con's avatar
con committed
217
218
        symbol->setType(declTy);

219
        if (_templateParameters && it == ast->declarator_list) {
con's avatar
con committed
220
            symbol->setTemplateParameters(_templateParameters);
221
222
            _templateParameters = 0; // consume the template parameters
        }
con's avatar
con committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238

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

        if (ty.isFriend())
            symbol->setStorage(Symbol::Friend);
        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);

Roberto Raggi's avatar
Roberto Raggi committed
239
240
        if (it->value && it->value->initializer) {
            FullySpecifiedType initTy = semantic()->check(it->value->initializer, _scope);
241
242
        }

Roberto Raggi's avatar
Roberto Raggi committed
243
244
245
246
        *decl_it = new (translationUnit()->memoryPool()) List<Declaration *>();
        (*decl_it)->value = symbol;
        decl_it = &(*decl_it)->next;

con's avatar
con committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
        _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);
264
    else if (accessSpecifier == T_Q_SIGNALS)
con's avatar
con committed
265
266
267
268
269
270
271
272
273
274
275
        semantic()->switchMethodKey(Function::SignalMethod);
    else
        semantic()->switchMethodKey(Function::NormalMethod);
    return false;
}

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

276
bool CheckDeclaration::visit(ExceptionDeclarationAST *ast)
con's avatar
con committed
277
{
Roberto Raggi's avatar
Roberto Raggi committed
278
    FullySpecifiedType ty = semantic()->check(ast->type_specifier_list, _scope);
279
280
    FullySpecifiedType qualTy = ty.qualifiedType();

Roberto Raggi's avatar
Roberto Raggi committed
281
    const Name *name = 0;
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
    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->setStartOffset(tokenAt(ast->firstToken()).offset);
    symbol->setEndOffset(tokenAt(ast->lastToken()).offset);
    symbol->setType(declTy);
    _scope->enterSymbol(symbol);

con's avatar
con committed
299
300
301
302
303
    return false;
}

bool CheckDeclaration::visit(FunctionDefinitionAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
304
    FullySpecifiedType ty = semantic()->check(ast->decl_specifier_list, _scope);
con's avatar
con committed
305
    FullySpecifiedType qualTy = ty.qualifiedType();
Roberto Raggi's avatar
Roberto Raggi committed
306
    const Name *name = 0;
con's avatar
con committed
307
308
    FullySpecifiedType funTy = semantic()->check(ast->declarator, qualTy,
                                                 _scope, &name);
309
    if (! (funTy && funTy->isFunctionType())) {
con's avatar
con committed
310
311
312
313
314
        translationUnit()->error(ast->firstToken(),
                                 "expected a function prototype");
        return false;
    }

315
    Function *fun = funTy->asFunctionType();
316
    fun->setVirtual(ty.isVirtual());
317
318
    fun->setStartOffset(tokenAt(ast->firstToken()).offset);
    fun->setEndOffset(tokenAt(ast->lastToken()).offset);
319
320
    if (ast->declarator)
        fun->setSourceLocation(ast->declarator->firstToken());
con's avatar
con committed
321
322
323
324
325
    fun->setName(name);
    fun->setTemplateParameters(_templateParameters);
    fun->setVisibility(semantic()->currentVisibility());
    fun->setMethodKey(semantic()->currentMethodKey());

326
327
    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;
328
    const bool isQ_INVOKABLE = ast->qt_invokable_token && tokenKind(ast->qt_invokable_token) == T_Q_INVOKABLE;
329
330
331
332
333

    if (isQ_SIGNAL)
        fun->setMethodKey(Function::SignalMethod);
    else if (isQ_SLOT)
        fun->setMethodKey(Function::SlotMethod);
334
    else if (isQ_INVOKABLE)
335
        fun->setMethodKey(Function::InvokableMethod);
336

con's avatar
con committed
337
338
    checkFunctionArguments(fun);

Roberto Raggi's avatar
Roberto Raggi committed
339
    ast->symbol = fun;
con's avatar
con committed
340
341
    _scope->enterSymbol(fun);

342
343
344
345
346
347
348
349
350
351
352
353
    if (! semantic()->skipFunctionBodies()) {
        if (ast->ctor_initializer) {
            bool looksLikeCtor = false;
            if (ty.isValid() || ! fun->identity())
                looksLikeCtor = false;
            else if (fun->identity()->isNameId() || fun->identity()->isTemplateNameId())
                looksLikeCtor = true;

            if (! looksLikeCtor) {
                translationUnit()->error(ast->ctor_initializer->firstToken(),
                                         "only constructors take base initializers");
            }
354
            accept(ast->ctor_initializer);
355
        }
con's avatar
con committed
356

357
358
        const int previousVisibility = semantic()->switchVisibility(Symbol::Public);
        const int previousMethodKey = semantic()->switchMethodKey(Function::NormalMethod);
con's avatar
con committed
359

360
        semantic()->check(ast->function_body, fun->members());
con's avatar
con committed
361

362
363
364
        semantic()->switchMethodKey(previousMethodKey);
        semantic()->switchVisibility(previousVisibility);
    }
con's avatar
con committed
365
366
367
368

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
369
370
371
bool CheckDeclaration::visit(MemInitializerAST *ast)
{
    (void) semantic()->check(ast->name, _scope);
372
373
374
    for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
        FullySpecifiedType ty = semantic()->check(it->value, _scope);
    }
Roberto Raggi's avatar
Roberto Raggi committed
375
376
377
    return false;
}

con's avatar
con committed
378
379
bool CheckDeclaration::visit(LinkageBodyAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
380
    for (DeclarationListAST *decl = ast->declaration_list; decl; decl = decl->next) {
381
       semantic()->check(decl->value, _scope);
con's avatar
con committed
382
383
384
385
386
387
    }
    return false;
}

bool CheckDeclaration::visit(LinkageSpecificationAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
388
    semantic()->check(ast->declaration, _scope);
con's avatar
con committed
389
390
391
392
393
    return false;
}

bool CheckDeclaration::visit(NamespaceAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
394
395
396
    const Name *namespaceName = 0;
    if (const Identifier *id = identifier(ast->identifier_token))
        namespaceName = control()->nameId(id);
397
398
399
400
401
402
403

    unsigned sourceLocation = ast->firstToken();

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

    Namespace *ns = control()->newNamespace(sourceLocation, namespaceName);
404
405
    ns->setStartOffset(tokenAt(ast->firstToken()).offset);
    ns->setEndOffset(tokenAt(ast->lastToken()).offset);
Roberto Raggi's avatar
Roberto Raggi committed
406
    ast->symbol = ns;
con's avatar
con committed
407
408
409
410
411
412
413
414
415
416
417
418
419
    _scope->enterSymbol(ns);
    semantic()->check(ast->linkage_body, ns->members()); // ### we'll do the merge later.

    return false;
}

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

bool CheckDeclaration::visit(ParameterDeclarationAST *ast)
{
420
421
422
423
424
425
426
    unsigned sourceLocation = locationOfDeclaratorId(ast->declarator);
    if (! sourceLocation) {
        if (ast->declarator)
            sourceLocation = ast->declarator->firstToken();
        else
            sourceLocation = ast->firstToken();
    }
427

Roberto Raggi's avatar
Roberto Raggi committed
428
    const Name *argName = 0;
Roberto Raggi's avatar
Roberto Raggi committed
429
    FullySpecifiedType ty = semantic()->check(ast->type_specifier_list, _scope);
con's avatar
con committed
430
431
432
    FullySpecifiedType argTy = semantic()->check(ast->declarator, ty.qualifiedType(),
                                                 _scope, &argName);
    FullySpecifiedType exprTy = semantic()->check(ast->expression, _scope);
433
    Argument *arg = control()->newArgument(sourceLocation, argName);
Roberto Raggi's avatar
Roberto Raggi committed
434
    ast->symbol = arg;
435
436
437
438
439
440
441
442
443
444
445
446
447
    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
448
449
450
451
452
453
454
    arg->setType(argTy);
    _scope->enterSymbol(arg);
    return false;
}

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

Roberto Raggi's avatar
Roberto Raggi committed
457
    for (DeclarationListAST *param = ast->template_parameter_list; param; param = param->next) {
458
       semantic()->check(param->value, scope);
con's avatar
con committed
459
460
    }

461
462
463
    semantic()->check(ast->declaration, _scope,
                      new TemplateParameters(_templateParameters, scope));

con's avatar
con committed
464
465
466
467
468
    return false;
}

bool CheckDeclaration::visit(TypenameTypeParameterAST *ast)
{
469
470
471
472
    unsigned sourceLocation = ast->firstToken();
    if (ast->name)
        sourceLocation = ast->name->firstToken();

Roberto Raggi's avatar
Roberto Raggi committed
473
    const Name *name = semantic()->check(ast->name, _scope);
474
475
476
    TypenameArgument *arg = control()->newTypenameArgument(sourceLocation, name);
    FullySpecifiedType ty = semantic()->check(ast->type_id, _scope);
    arg->setType(ty);
Roberto Raggi's avatar
Roberto Raggi committed
477
    ast->symbol = arg;
con's avatar
con committed
478
479
480
481
482
483
    _scope->enterSymbol(arg);
    return false;
}

bool CheckDeclaration::visit(TemplateTypeParameterAST *ast)
{
484
485
486
487
    unsigned sourceLocation = ast->firstToken();
    if (ast->name)
        sourceLocation = ast->name->firstToken();

Roberto Raggi's avatar
Roberto Raggi committed
488
    const Name *name = semantic()->check(ast->name, _scope);
489
490
491
    TypenameArgument *arg = control()->newTypenameArgument(sourceLocation, name);
    FullySpecifiedType ty = semantic()->check(ast->type_id, _scope);
    arg->setType(ty);
Roberto Raggi's avatar
Roberto Raggi committed
492
    ast->symbol = arg;
con's avatar
con committed
493
494
495
496
497
498
    _scope->enterSymbol(arg);
    return false;
}

bool CheckDeclaration::visit(UsingAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
499
    const Name *name = semantic()->check(ast->name, _scope);
500
501
502
503
504
505

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

    UsingDeclaration *u = control()->newUsingDeclaration(sourceLocation, name);
Roberto Raggi's avatar
Roberto Raggi committed
506
    ast->symbol = u;
con's avatar
con committed
507
508
509
510
511
512
    _scope->enterSymbol(u);
    return false;
}

bool CheckDeclaration::visit(UsingDirectiveAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
513
    const Name *name = semantic()->check(ast->name, _scope);
514
515
516
517
518
519

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

    UsingNamespaceDirective *u = control()->newUsingNamespaceDirective(sourceLocation, name);
Roberto Raggi's avatar
Roberto Raggi committed
520
    ast->symbol = u;
con's avatar
con committed
521
    _scope->enterSymbol(u);
522
523
524
525
526

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

con's avatar
con committed
527
528
529
    return false;
}

Erik Verbruggen's avatar
Erik Verbruggen committed
530
bool CheckDeclaration::visit(ObjCProtocolForwardDeclarationAST *ast)
531
532
533
534
{
    const unsigned sourceLocation = ast->firstToken();

    List<ObjCForwardProtocolDeclaration *> **symbolIter = &ast->symbols;
535
    for (NameListAST *it = ast->identifier_list; it; it = it->next) {
536
        unsigned declarationLocation;
Roberto Raggi's avatar
Roberto Raggi committed
537
538
        if (it->value)
            declarationLocation = it->value->firstToken();
539
540
541
        else
            declarationLocation = sourceLocation;

Roberto Raggi's avatar
Roberto Raggi committed
542
        const Name *protocolName = semantic()->check(it->value, _scope);
543
544
545
546
547
548
549
550
551
552
553
554
555
556
        ObjCForwardProtocolDeclaration *fwdProtocol = control()->newObjCForwardProtocolDeclaration(sourceLocation, protocolName);
        fwdProtocol->setStartOffset(tokenAt(ast->firstToken()).offset);
        fwdProtocol->setEndOffset(tokenAt(ast->lastToken()).offset);

        _scope->enterSymbol(fwdProtocol);

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

    return false;
}

Erik Verbruggen's avatar
Erik Verbruggen committed
557
bool CheckDeclaration::visit(ObjCProtocolDeclarationAST *ast)
558
559
560
561
562
563
564
{
    unsigned sourceLocation;
    if (ast->name)
        sourceLocation = ast->name->firstToken();
    else
        sourceLocation = ast->firstToken();

Roberto Raggi's avatar
Roberto Raggi committed
565
    const Name *protocolName = semantic()->check(ast->name, _scope);
566
567
568
569
    ObjCProtocol *protocol = control()->newObjCProtocol(sourceLocation, protocolName);
    protocol->setStartOffset(tokenAt(ast->firstToken()).offset);
    protocol->setEndOffset(tokenAt(ast->lastToken()).offset);

570
    if (ast->protocol_refs && ast->protocol_refs->identifier_list) {
571
        for (NameListAST *iter = ast->protocol_refs->identifier_list; iter; iter = iter->next) {
Roberto Raggi's avatar
Roberto Raggi committed
572
            NameAST* name = iter->value;
Roberto Raggi's avatar
Roberto Raggi committed
573
            const Name *protocolName = semantic()->check(name, _scope);
574
575
576
577
578
579
            ObjCBaseProtocol *baseProtocol = control()->newObjCBaseProtocol(name->firstToken(), protocolName);
            protocol->addProtocol(baseProtocol);
        }
    }

    int previousObjCVisibility = semantic()->switchObjCVisibility(Function::Public);
Roberto Raggi's avatar
Roberto Raggi committed
580
    for (DeclarationListAST *it = ast->member_declaration_list; it; it = it->next) {
581
        semantic()->check(it->value, protocol->members());
582
583
584
585
    }
    (void) semantic()->switchObjCVisibility(previousObjCVisibility);

    ast->symbol = protocol;
586
587
588
589
590
    _scope->enterSymbol(protocol);

    return false;
}

Erik Verbruggen's avatar
Erik Verbruggen committed
591
bool CheckDeclaration::visit(ObjCClassForwardDeclarationAST *ast)
592
593
594
595
{
    const unsigned sourceLocation = ast->firstToken();

    List<ObjCForwardClassDeclaration *> **symbolIter = &ast->symbols;
596
    for (NameListAST *it = ast->identifier_list; it; it = it->next) {
597
        unsigned declarationLocation;
Roberto Raggi's avatar
Roberto Raggi committed
598
599
        if (it->value)
            declarationLocation = it->value->firstToken();
600
601
602
        else
            declarationLocation = sourceLocation;

Roberto Raggi's avatar
Roberto Raggi committed
603
        const Name *className = semantic()->check(it->value, _scope);
604
605
606
607
608
609
610
611
612
613
614
615
616
617
        ObjCForwardClassDeclaration *fwdClass = control()->newObjCForwardClassDeclaration(sourceLocation, className);
        fwdClass->setStartOffset(tokenAt(ast->firstToken()).offset);
        fwdClass->setEndOffset(tokenAt(ast->lastToken()).offset);

        _scope->enterSymbol(fwdClass);

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

    return false;
}

Erik Verbruggen's avatar
Erik Verbruggen committed
618
bool CheckDeclaration::visit(ObjCClassDeclarationAST *ast)
619
620
621
622
623
624
625
{
    unsigned sourceLocation;
    if (ast->class_name)
        sourceLocation = ast->class_name->firstToken();
    else
        sourceLocation = ast->firstToken();

Roberto Raggi's avatar
Roberto Raggi committed
626
    const Name *className = semantic()->check(ast->class_name, _scope);
627
628
629
630
631
    ObjCClass *klass = control()->newObjCClass(sourceLocation, className);
    klass->setStartOffset(tokenAt(ast->firstToken()).offset);
    klass->setEndOffset(tokenAt(ast->lastToken()).offset);
    ast->symbol = klass;

632
633
634
    klass->setInterface(ast->interface_token != 0);

    if (ast->category_name) {
Roberto Raggi's avatar
Roberto Raggi committed
635
        const Name *categoryName = semantic()->check(ast->category_name, _scope);
636
637
638
        klass->setCategoryName(categoryName);
    }

639
    if (ast->superclass) {
Roberto Raggi's avatar
Roberto Raggi committed
640
        const Name *superClassName = semantic()->check(ast->superclass, _scope);
641
642
643
644
645
        ObjCBaseClass *superKlass = control()->newObjCBaseClass(ast->superclass->firstToken(), superClassName);
        klass->setBaseClass(superKlass);
    }

    if (ast->protocol_refs && ast->protocol_refs->identifier_list) {
646
        for (NameListAST *iter = ast->protocol_refs->identifier_list; iter; iter = iter->next) {
Roberto Raggi's avatar
Roberto Raggi committed
647
            NameAST* name = iter->value;
Roberto Raggi's avatar
Roberto Raggi committed
648
            const Name *protocolName = semantic()->check(name, _scope);
649
650
651
652
653
            ObjCBaseProtocol *baseProtocol = control()->newObjCBaseProtocol(name->firstToken(), protocolName);
            klass->addProtocol(baseProtocol);
        }
    }

654
655
656
657
658
    _scope->enterSymbol(klass);

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

    if (ast->inst_vars_decl) {
Roberto Raggi's avatar
Roberto Raggi committed
659
        for (DeclarationListAST *it = ast->inst_vars_decl->instance_variable_list; it; it = it->next) {
660
            semantic()->check(it->value, klass->members());
661
662
663
        }
    }

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

Roberto Raggi's avatar
Roberto Raggi committed
666
    for (DeclarationListAST *it = ast->member_declaration_list; it; it = it->next) {
667
        semantic()->check(it->value, klass->members());
668
669
670
671
672
673
674
675
676
    }

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

    return false;
}

bool CheckDeclaration::visit(ObjCMethodDeclarationAST *ast)
{
677
678
679
680
681
    ObjCMethodPrototypeAST *methodProto = ast->method_prototype;
    if (!methodProto)
        return false;
    ObjCSelectorAST *selector = methodProto->selector;
    if (!selector)
682
683
        return false;

684
    FullySpecifiedType ty = semantic()->check(methodProto, _scope);
685
686
    ObjCMethod *methodTy = ty.type()->asObjCMethodType();
    if (!methodTy)
687
688
        return false;

689
    Symbol *symbol;
Erik Verbruggen's avatar
Erik Verbruggen committed
690
    if (ast->function_body) {
691
        if (!semantic()->skipFunctionBodies()) {
692
            semantic()->check(ast->function_body, methodTy->members());
693
        }
694

695
        symbol = methodTy;
Erik Verbruggen's avatar
Erik Verbruggen committed
696
    } else {
697
        Declaration *decl = control()->newDeclaration(selector->firstToken(), methodTy->name());
698
        decl->setType(methodTy);
Erik Verbruggen's avatar
Erik Verbruggen committed
699
        symbol = decl;
700
        symbol->setStorage(methodTy->storage());
701
    }
702

703
704
    symbol->setStartOffset(tokenAt(ast->firstToken()).offset);
    symbol->setEndOffset(tokenAt(ast->lastToken()).offset);
705
    symbol->setVisibility(semantic()->currentObjCVisibility());
706
707
708

    _scope->enterSymbol(symbol);

709
710
711
    return false;
}

712
713
714
715
716
717
718
719
bool CheckDeclaration::visit(ObjCVisibilityDeclarationAST *ast)
{
    int accessSpecifier = tokenKind(ast->visibility_token);
    int visibility = semantic()->visibilityForObjCAccessSpecifier(accessSpecifier);
    semantic()->switchObjCVisibility(visibility);
    return false;
}

720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
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)
{
737
738
739
740
741
742
743
744
745
746
    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
747
    const Name *getterName = 0, *setterName = 0;
748

Roberto Raggi's avatar
Roberto Raggi committed
749
    for (ObjCPropertyAttributeListAST *iter= ast->property_attribute_list; iter; iter = iter->next) {
750
        ObjCPropertyAttributeAST *attrAst = iter->value;
751
752
753
        if (!attrAst)
            continue;

Roberto Raggi's avatar
Roberto Raggi committed
754
        const Identifier *attrId = identifier(attrAst->attribute_identifier_token);
755
        if (attrId == control()->objcGetterId()) {
756
757
            if (checkPropertyAttribute(attrAst, propAttrs, ObjCPropertyDeclaration::Getter)) {
                getterName = semantic()->check(attrAst->method_selector, _scope);
758
            }
759
        } else if (attrId == control()->objcSetterId()) {
760
761
            if (checkPropertyAttribute(attrAst, propAttrs, ObjCPropertyDeclaration::Setter)) {
                setterName = semantic()->check(attrAst->method_selector, _scope);
762
            }
763
        } else if (attrId == control()->objcReadwriteId()) {
764
            checkPropertyAttribute(attrAst, propAttrs, ObjCPropertyDeclaration::ReadWrite);
765
        } else if (attrId == control()->objcReadonlyId()) {
766
            checkPropertyAttribute(attrAst, propAttrs, ObjCPropertyDeclaration::ReadOnly);
767
        } else if (attrId == control()->objcAssignId()) {
768
            checkPropertyAttribute(attrAst, propAttrs, ObjCPropertyDeclaration::Assign);
769
        } else if (attrId == control()->objcRetainId()) {
770
            checkPropertyAttribute(attrAst, propAttrs, ObjCPropertyDeclaration::Retain);
771
        } else if (attrId == control()->objcCopyId()) {
772
            checkPropertyAttribute(attrAst, propAttrs, ObjCPropertyDeclaration::Copy);
773
        } else if (attrId == control()->objcNonatomicId()) {
774
            checkPropertyAttribute(attrAst, propAttrs, ObjCPropertyDeclaration::NonAtomic);
775
776
777
        }
    }

778
779
    if (propAttrs & ObjCPropertyDeclaration::ReadOnly &&
        propAttrs & ObjCPropertyDeclaration::ReadWrite)
780
781
782
        // 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");
783
    int setterSemAttrs = propAttrs & ObjCPropertyDeclaration::SetterSemanticsMask;
784
    if (setterSemAttrs
785
786
787
            && setterSemAttrs != ObjCPropertyDeclaration::Assign
            && setterSemAttrs != ObjCPropertyDeclaration::Retain
            && setterSemAttrs != ObjCPropertyDeclaration::Copy) {
788
789
790
791
792
        // 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");
    }

793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
    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;
    }

808
809
    return false;
}
810
811
812

bool CheckDeclaration::visit(QtEnumDeclarationAST *ast)
{
813
    checkQEnumsQFlagsNames(ast->enumerator_list, "Q_ENUMS");
814
815
816
817
818
    return false;
}

bool CheckDeclaration::visit(QtFlagsDeclarationAST *ast)
{
819
    checkQEnumsQFlagsNames(ast->flag_enums_list, "Q_FLAGS");
820
821
    return false;
}
822
823
824
825
826
827
828
829
830
831
832
833
834
835

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;
}
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870

void CheckDeclaration::checkQEnumsQFlagsNames(NameListAST *nameListAst,
                                              const char *declName)
{
    for (NameListAST *iter = nameListAst; iter; iter = iter->next) {
        if (!iter)
            continue;

        const Name *name = semantic()->check(iter->value, _scope);
        if (!name)
            continue;

        if (name->isNameId())
            continue;

        const QualifiedNameId *qName = name->asQualifiedNameId();
        if (!qName)
            translationUnit()->error(iter->firstToken(), "invalid name in %s",
                                     declName);
        else if (qName->isGlobal())
                translationUnit()->error(iter->firstToken(),
                                         "invalid name '%s' in %s",
                                         qName->identifier()->chars(), declName);
        else {
            for (unsigned i = 0; i < qName->nameCount(); ++i) {
                const Name *namePart = qName->nameAt(i);
                if (!namePart || !namePart->isNameId()) {
                    translationUnit()->error(iter->firstToken(),
                                             "invalid name '%s' in %s",
                                             qName->identifier()->chars(), declName);
                }
            }
        }
    }
}