CheckDeclaration.cpp 26.9 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 "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"
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
72
73
74

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

CheckDeclaration::~CheckDeclaration()
{ }

void CheckDeclaration::check(DeclarationAST *declaration,
75
                             Scope *scope, TemplateParameters *templateParameters)
con's avatar
con committed
76
77
{
    Scope *previousScope = switchScope(scope);
78
    TemplateParameters *previousTemplateParameters = switchTemplateParameters(templateParameters);
con's avatar
con committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    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;
}

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

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

142
143
    if (_templateParameters && ty) {
        if (Class *klass = ty->asClassType()) {
con's avatar
con committed
144
145
146
147
            klass->setTemplateParameters(_templateParameters);
        }
    }

Roberto Raggi's avatar
Roberto Raggi committed
148
149
    if (! ast->declarator_list && ast->decl_specifier_list && ! ast->decl_specifier_list->next) {
        if (ElaboratedTypeSpecifierAST *elab_type_spec = ast->decl_specifier_list->value->asElaboratedTypeSpecifier()) {
150
151
152
153
154
155

            unsigned sourceLocation = elab_type_spec->firstToken();

            if (elab_type_spec->name)
                sourceLocation = elab_type_spec->name->firstToken();

156
157
            Name *name = semantic()->check(elab_type_spec->name, _scope);
            ForwardClassDeclaration *symbol =
158
                    control()->newForwardClassDeclaration(sourceLocation, name);
159
160
161
162
163
164
165
166
167
168
169

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

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

170
171
172
    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;

Roberto Raggi's avatar
Roberto Raggi committed
173
    List<Declaration *> **decl_it = &ast->symbols;
Roberto Raggi's avatar
Roberto Raggi committed
174
    for (DeclaratorListAST *it = ast->declarator_list; it; it = it->next) {
con's avatar
con committed
175
        Name *name = 0;
Roberto Raggi's avatar
Roberto Raggi committed
176
        FullySpecifiedType declTy = semantic()->check(it->value, qualTy,
con's avatar
con committed
177
178
                                                      _scope, &name);

Roberto Raggi's avatar
Roberto Raggi committed
179
        unsigned location = locationOfDeclaratorId(it->value);
180
        if (! location) {
Roberto Raggi's avatar
Roberto Raggi committed
181
182
            if (it->value)
                location = it->value->firstToken();
183
184
185
            else
                location = ast->firstToken();
        }
186

187
188
        Function *fun = 0;
        if (declTy && 0 != (fun = declTy->asFunctionType())) {
189
            fun->setSourceLocation(location);
con's avatar
con committed
190
191
192
            fun->setScope(_scope);
            fun->setName(name);
            fun->setMethodKey(semantic()->currentMethodKey());
193
            fun->setVirtual(ty.isVirtual());
194
195
196
197
            if (isQ_SIGNAL)
                fun->setMethodKey(Function::SignalMethod);
            else if (isQ_SLOT)
                fun->setMethodKey(Function::SlotMethod);
con's avatar
con committed
198
199
200
201
202
203
204
            fun->setVisibility(semantic()->currentVisibility());
        } else if (semantic()->currentMethodKey() != Function::NormalMethod) {
            translationUnit()->warning(ast->firstToken(),
                                       "expected a function declaration");
        }

        Declaration *symbol = control()->newDeclaration(location, name);
205
206
207
        symbol->setStartOffset(tokenAt(ast->firstToken()).offset);
        symbol->setEndOffset(tokenAt(ast->lastToken()).offset);

con's avatar
con committed
208
209
210
        symbol->setType(control()->integerType(IntegerType::Int));
        symbol->setType(declTy);

Roberto Raggi's avatar
Roberto Raggi committed
211
        if (_templateParameters && it == ast->declarator_list && ty && ! ty->isClassType())
con's avatar
con committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
            symbol->setTemplateParameters(_templateParameters);

        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
229
230
        if (it->value && it->value->initializer) {
            FullySpecifiedType initTy = semantic()->check(it->value->initializer, _scope);
231
232
        }

Roberto Raggi's avatar
Roberto Raggi committed
233
234
235
236
        *decl_it = new (translationUnit()->memoryPool()) List<Declaration *>();
        (*decl_it)->value = symbol;
        decl_it = &(*decl_it)->next;

con's avatar
con committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
        _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);
254
    else if (accessSpecifier == T_Q_SIGNALS)
con's avatar
con committed
255
256
257
258
259
260
261
262
263
264
265
        semantic()->switchMethodKey(Function::SignalMethod);
    else
        semantic()->switchMethodKey(Function::NormalMethod);
    return false;
}

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

266
bool CheckDeclaration::visit(ExceptionDeclarationAST *ast)
con's avatar
con committed
267
{
Roberto Raggi's avatar
Roberto Raggi committed
268
    FullySpecifiedType ty = semantic()->check(ast->type_specifier_list, _scope);
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
    FullySpecifiedType qualTy = ty.qualifiedType();

    Name *name = 0;
    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
289
290
291
292
293
    return false;
}

bool CheckDeclaration::visit(FunctionDefinitionAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
294
    FullySpecifiedType ty = semantic()->check(ast->decl_specifier_list, _scope);
con's avatar
con committed
295
296
297
298
    FullySpecifiedType qualTy = ty.qualifiedType();
    Name *name = 0;
    FullySpecifiedType funTy = semantic()->check(ast->declarator, qualTy,
                                                 _scope, &name);
299
    if (! (funTy && funTy->isFunctionType())) {
con's avatar
con committed
300
301
302
303
304
        translationUnit()->error(ast->firstToken(),
                                 "expected a function prototype");
        return false;
    }

305
    Function *fun = funTy->asFunctionType();
306
    fun->setVirtual(ty.isVirtual());
307
308
    fun->setStartOffset(tokenAt(ast->firstToken()).offset);
    fun->setEndOffset(tokenAt(ast->lastToken()).offset);
309
310
    if (ast->declarator)
        fun->setSourceLocation(ast->declarator->firstToken());
con's avatar
con committed
311
312
313
314
315
    fun->setName(name);
    fun->setTemplateParameters(_templateParameters);
    fun->setVisibility(semantic()->currentVisibility());
    fun->setMethodKey(semantic()->currentMethodKey());

316
317
318
319
320
321
322
323
    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;

    if (isQ_SIGNAL)
        fun->setMethodKey(Function::SignalMethod);
    else if (isQ_SLOT)
        fun->setMethodKey(Function::SlotMethod);

con's avatar
con committed
324
325
    checkFunctionArguments(fun);

Roberto Raggi's avatar
Roberto Raggi committed
326
    ast->symbol = fun;
con's avatar
con committed
327
328
    _scope->enterSymbol(fun);

329
330
331
332
333
334
335
336
337
338
339
340
    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");
            }
341
            accept(ast->ctor_initializer);
342
        }
con's avatar
con committed
343

344
345
        const int previousVisibility = semantic()->switchVisibility(Symbol::Public);
        const int previousMethodKey = semantic()->switchMethodKey(Function::NormalMethod);
con's avatar
con committed
346

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

349
350
351
        semantic()->switchMethodKey(previousMethodKey);
        semantic()->switchVisibility(previousVisibility);
    }
con's avatar
con committed
352
353
354
355

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
356
357
358
359
360
361
362
bool CheckDeclaration::visit(MemInitializerAST *ast)
{
    (void) semantic()->check(ast->name, _scope);
    FullySpecifiedType ty = semantic()->check(ast->expression, _scope);
    return false;
}

con's avatar
con committed
363
364
bool CheckDeclaration::visit(LinkageBodyAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
365
    for (DeclarationListAST *decl = ast->declaration_list; decl; decl = decl->next) {
366
       semantic()->check(decl->value, _scope);
con's avatar
con committed
367
368
369
370
371
372
    }
    return false;
}

bool CheckDeclaration::visit(LinkageSpecificationAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
373
    semantic()->check(ast->declaration, _scope);
con's avatar
con committed
374
375
376
377
378
379
380
    return false;
}

bool CheckDeclaration::visit(NamespaceAST *ast)
{
    Identifier *id = identifier(ast->identifier_token);
    Name *namespaceName = control()->nameId(id);
381
382
383
384
385
386
387

    unsigned sourceLocation = ast->firstToken();

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

    Namespace *ns = control()->newNamespace(sourceLocation, namespaceName);
388
389
    ns->setStartOffset(tokenAt(ast->firstToken()).offset);
    ns->setEndOffset(tokenAt(ast->lastToken()).offset);
Roberto Raggi's avatar
Roberto Raggi committed
390
    ast->symbol = ns;
con's avatar
con committed
391
392
393
394
395
396
397
398
399
400
401
402
403
    _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)
{
404
405
406
407
408
409
410
    unsigned sourceLocation = locationOfDeclaratorId(ast->declarator);
    if (! sourceLocation) {
        if (ast->declarator)
            sourceLocation = ast->declarator->firstToken();
        else
            sourceLocation = ast->firstToken();
    }
411

con's avatar
con committed
412
    Name *argName = 0;
Roberto Raggi's avatar
Roberto Raggi committed
413
    FullySpecifiedType ty = semantic()->check(ast->type_specifier_list, _scope);
con's avatar
con committed
414
415
416
    FullySpecifiedType argTy = semantic()->check(ast->declarator, ty.qualifiedType(),
                                                 _scope, &argName);
    FullySpecifiedType exprTy = semantic()->check(ast->expression, _scope);
417
    Argument *arg = control()->newArgument(sourceLocation, argName);
Roberto Raggi's avatar
Roberto Raggi committed
418
    ast->symbol = arg;
con's avatar
con committed
419
420
421
422
423
424
425
426
427
    if (ast->expression)
        arg->setInitializer(true);
    arg->setType(argTy);
    _scope->enterSymbol(arg);
    return false;
}

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

Roberto Raggi's avatar
Roberto Raggi committed
430
    for (DeclarationListAST *param = ast->template_parameter_list; param; param = param->next) {
431
       semantic()->check(param->value, scope);
con's avatar
con committed
432
433
    }

434
435
436
    semantic()->check(ast->declaration, _scope,
                      new TemplateParameters(_templateParameters, scope));

con's avatar
con committed
437
438
439
440
441
    return false;
}

bool CheckDeclaration::visit(TypenameTypeParameterAST *ast)
{
442
443
444
445
    unsigned sourceLocation = ast->firstToken();
    if (ast->name)
        sourceLocation = ast->name->firstToken();

con's avatar
con committed
446
    Name *name = semantic()->check(ast->name, _scope);
447
    Argument *arg = control()->newArgument(sourceLocation, name); // ### new template type
Roberto Raggi's avatar
Roberto Raggi committed
448
    ast->symbol = arg;
con's avatar
con committed
449
450
451
452
453
454
    _scope->enterSymbol(arg);
    return false;
}

bool CheckDeclaration::visit(TemplateTypeParameterAST *ast)
{
455
456
457
458
    unsigned sourceLocation = ast->firstToken();
    if (ast->name)
        sourceLocation = ast->name->firstToken();

con's avatar
con committed
459
    Name *name = semantic()->check(ast->name, _scope);
460
    Argument *arg = control()->newArgument(sourceLocation, name); // ### new template type
Roberto Raggi's avatar
Roberto Raggi committed
461
    ast->symbol = arg;
con's avatar
con committed
462
463
464
465
466
467
468
    _scope->enterSymbol(arg);
    return false;
}

bool CheckDeclaration::visit(UsingAST *ast)
{
    Name *name = semantic()->check(ast->name, _scope);
469
470
471
472
473
474

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

    UsingDeclaration *u = control()->newUsingDeclaration(sourceLocation, name);
Roberto Raggi's avatar
Roberto Raggi committed
475
    ast->symbol = u;
con's avatar
con committed
476
477
478
479
480
481
482
    _scope->enterSymbol(u);
    return false;
}

bool CheckDeclaration::visit(UsingDirectiveAST *ast)
{
    Name *name = semantic()->check(ast->name, _scope);
483
484
485
486
487
488

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

    UsingNamespaceDirective *u = control()->newUsingNamespaceDirective(sourceLocation, name);
Roberto Raggi's avatar
Roberto Raggi committed
489
    ast->symbol = u;
con's avatar
con committed
490
    _scope->enterSymbol(u);
491
492
493
494
495

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

con's avatar
con committed
496
497
498
    return false;
}

Erik Verbruggen's avatar
Erik Verbruggen committed
499
bool CheckDeclaration::visit(ObjCProtocolForwardDeclarationAST *ast)
500
501
502
503
{
    const unsigned sourceLocation = ast->firstToken();

    List<ObjCForwardProtocolDeclaration *> **symbolIter = &ast->symbols;
Roberto Raggi's avatar
Roberto Raggi committed
504
    for (ObjCIdentifierListAST *it = ast->identifier_list; it; it = it->next) {
505
        unsigned declarationLocation;
Roberto Raggi's avatar
Roberto Raggi committed
506
507
        if (it->value)
            declarationLocation = it->value->firstToken();
508
509
510
        else
            declarationLocation = sourceLocation;

Roberto Raggi's avatar
Roberto Raggi committed
511
        Name *protocolName = semantic()->check(it->value, _scope);
512
513
514
515
516
517
518
519
520
521
522
523
524
525
        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
526
bool CheckDeclaration::visit(ObjCProtocolDeclarationAST *ast)
527
528
529
530
531
532
533
534
535
536
537
538
{
    unsigned sourceLocation;
    if (ast->name)
        sourceLocation = ast->name->firstToken();
    else
        sourceLocation = ast->firstToken();

    Name *protocolName = semantic()->check(ast->name, _scope);
    ObjCProtocol *protocol = control()->newObjCProtocol(sourceLocation, protocolName);
    protocol->setStartOffset(tokenAt(ast->firstToken()).offset);
    protocol->setEndOffset(tokenAt(ast->lastToken()).offset);

539
    if (ast->protocol_refs && ast->protocol_refs->identifier_list) {
Roberto Raggi's avatar
Roberto Raggi committed
540
541
        for (ObjCIdentifierListAST *iter = ast->protocol_refs->identifier_list; iter; iter = iter->next) {
            NameAST* name = iter->value;
542
543
544
545
546
547
548
            Name *protocolName = semantic()->check(name, _scope);
            ObjCBaseProtocol *baseProtocol = control()->newObjCBaseProtocol(name->firstToken(), protocolName);
            protocol->addProtocol(baseProtocol);
        }
    }

    int previousObjCVisibility = semantic()->switchObjCVisibility(Function::Public);
Roberto Raggi's avatar
Roberto Raggi committed
549
    for (DeclarationListAST *it = ast->member_declaration_list; it; it = it->next) {
550
        semantic()->check(it->value, protocol->members());
551
552
553
554
    }
    (void) semantic()->switchObjCVisibility(previousObjCVisibility);

    ast->symbol = protocol;
555
556
557
558
559
    _scope->enterSymbol(protocol);

    return false;
}

Erik Verbruggen's avatar
Erik Verbruggen committed
560
bool CheckDeclaration::visit(ObjCClassForwardDeclarationAST *ast)
561
562
563
564
{
    const unsigned sourceLocation = ast->firstToken();

    List<ObjCForwardClassDeclaration *> **symbolIter = &ast->symbols;
Roberto Raggi's avatar
Roberto Raggi committed
565
    for (ObjCIdentifierListAST *it = ast->identifier_list; it; it = it->next) {
566
        unsigned declarationLocation;
Roberto Raggi's avatar
Roberto Raggi committed
567
568
        if (it->value)
            declarationLocation = it->value->firstToken();
569
570
571
        else
            declarationLocation = sourceLocation;

Roberto Raggi's avatar
Roberto Raggi committed
572
        Name *className = semantic()->check(it->value, _scope);
573
574
575
576
577
578
579
580
581
582
583
584
585
586
        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
587
bool CheckDeclaration::visit(ObjCClassDeclarationAST *ast)
588
589
590
591
592
593
594
595
596
597
598
599
600
{
    unsigned sourceLocation;
    if (ast->class_name)
        sourceLocation = ast->class_name->firstToken();
    else
        sourceLocation = ast->firstToken();

    Name *className = semantic()->check(ast->class_name, _scope);
    ObjCClass *klass = control()->newObjCClass(sourceLocation, className);
    klass->setStartOffset(tokenAt(ast->firstToken()).offset);
    klass->setEndOffset(tokenAt(ast->lastToken()).offset);
    ast->symbol = klass;

601
602
603
604
605
606
607
    klass->setInterface(ast->interface_token != 0);

    if (ast->category_name) {
        Name *categoryName = semantic()->check(ast->category_name, _scope);
        klass->setCategoryName(categoryName);
    }

608
609
610
611
612
613
614
    if (ast->superclass) {
        Name *superClassName = semantic()->check(ast->superclass, _scope);
        ObjCBaseClass *superKlass = control()->newObjCBaseClass(ast->superclass->firstToken(), superClassName);
        klass->setBaseClass(superKlass);
    }

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

623
624
625
626
627
    _scope->enterSymbol(klass);

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

    if (ast->inst_vars_decl) {
Roberto Raggi's avatar
Roberto Raggi committed
628
        for (DeclarationListAST *it = ast->inst_vars_decl->instance_variable_list; it; it = it->next) {
629
            semantic()->check(it->value, klass->members());
630
631
632
        }
    }

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

Roberto Raggi's avatar
Roberto Raggi committed
635
    for (DeclarationListAST *it = ast->member_declaration_list; it; it = it->next) {
636
        semantic()->check(it->value, klass->members());
637
638
639
640
641
642
643
644
645
646
647
648
649
    }

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

    return false;
}

bool CheckDeclaration::visit(ObjCMethodDeclarationAST *ast)
{
    if (!ast->method_prototype)
        return false;

    FullySpecifiedType ty = semantic()->check(ast->method_prototype, _scope);
650
651
    ObjCMethod *methodType = ty.type()->asObjCMethodType();
    if (!methodType)
652
653
        return false;

654
    Symbol *symbol;
Erik Verbruggen's avatar
Erik Verbruggen committed
655
    if (ast->function_body) {
656
657
658
        if (!semantic()->skipFunctionBodies()) {
            semantic()->check(ast->function_body, methodType->members());
        }
659

660
        symbol = methodType;
Erik Verbruggen's avatar
Erik Verbruggen committed
661
662
663
664
    } else {
        Declaration *decl = control()->newDeclaration(ast->firstToken(), methodType->name());
        decl->setType(methodType);
        symbol = decl;
665
    }
666

667
668
    symbol->setStartOffset(tokenAt(ast->firstToken()).offset);
    symbol->setEndOffset(tokenAt(ast->lastToken()).offset);
669
670
671
672
673
674
675
    symbol->setVisibility(semantic()->currentVisibility());

    if (semantic()->isObjCClassMethod(ast->method_prototype->method_type_token))
        symbol->setStorage(Symbol::Static);

    _scope->enterSymbol(symbol);

676
677
678
    return false;
}

679
680
681
682
683
684
685
686
bool CheckDeclaration::visit(ObjCVisibilityDeclarationAST *ast)
{
    int accessSpecifier = tokenKind(ast->visibility_token);
    int visibility = semantic()->visibilityForObjCAccessSpecifier(accessSpecifier);
    semantic()->switchObjCVisibility(visibility);
    return false;
}

687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
enum PropertyAttributes {
    None = 0,
    Assign = 1 << 0,
    Retain = 1 << 1,
    Copy = 1 << 2,
    ReadOnly = 1 << 3,
    ReadWrite = 1 << 4,
    Getter = 1 << 5,
    Setter = 1 << 6,
    NonAtomic = 1 << 7,

    WritabilityMask = ReadOnly | ReadWrite,
    SetterSemanticsMask = Assign | Retain | Copy,
};

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)
{
    int propAttrs = None;

Roberto Raggi's avatar
Roberto Raggi committed
721
    for (ObjCPropertyAttributeListAST *iter= ast->property_attribute_list; iter; iter = iter->next) {
722
        ObjCPropertyAttributeAST *attrAst = iter->value;
723
724
725
        if (!attrAst)
            continue;

726
727
        Identifier *attrId = identifier(attrAst->attribute_identifier_token);
        if (attrId == control()->objcGetterId()) {
728
729
730
            if (checkPropertyAttribute(attrAst, propAttrs, Getter)) {
                // TODO: find method declaration for getter
            }
731
        } else if (attrId == control()->objcSetterId()) {
732
733
734
            if (checkPropertyAttribute(attrAst, propAttrs, Setter)) {
                // TODO: find method declaration for setter
            }
735
        } else if (attrId == control()->objcReadwriteId()) {
736
            checkPropertyAttribute(attrAst, propAttrs, ReadWrite);
737
        } else if (attrId == control()->objcReadonlyId()) {
738
            checkPropertyAttribute(attrAst, propAttrs, ReadOnly);
739
        } else if (attrId == control()->objcAssignId()) {
740
            checkPropertyAttribute(attrAst, propAttrs, Assign);
741
        } else if (attrId == control()->objcRetainId()) {
742
            checkPropertyAttribute(attrAst, propAttrs, Retain);
743
        } else if (attrId == control()->objcCopyId()) {
744
            checkPropertyAttribute(attrAst, propAttrs, Copy);
745
        } else if (attrId == control()->objcNonatomicId()) {
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
            checkPropertyAttribute(attrAst, propAttrs, NonAtomic);
        }
    }

    if (propAttrs & ReadOnly && propAttrs & ReadWrite)
        // 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");
    int setterSemAttrs = propAttrs & SetterSemanticsMask;
    if (setterSemAttrs
            && setterSemAttrs != Assign
            && setterSemAttrs != Retain
            && setterSemAttrs != Copy) {
        // 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");
    }

    // TODO: Check if the next line is correct (EV)
    semantic()->check(ast->simple_declaration, _scope);
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
769