CheckDeclaration.cpp 15.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
26
** contact the sales department at http://www.qtsoftware.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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
#include <cassert>

CPLUSPLUS_BEGIN_NAMESPACE

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

CheckDeclaration::~CheckDeclaration()
{ }

void CheckDeclaration::check(DeclarationAST *declaration,
                             Scope *scope, Scope *templateParameters)
{
    Scope *previousScope = switchScope(scope);
    Scope *previousTemplateParameters = switchTemplateParameters(templateParameters);
    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;
}

Scope *CheckDeclaration::switchTemplateParameters(Scope *templateParameters)
{
    Scope *previousTemplateParameters = _templateParameters;
    _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 139 140 141
bool CheckDeclaration::visit(SimpleDeclarationAST *ast)
{
    FullySpecifiedType ty = semantic()->check(ast->decl_specifier_seq, _scope);
    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);
        }
    }

148 149
    if (! ast->declarators && ast->decl_specifier_seq && ! ast->decl_specifier_seq->next) {
        if (ElaboratedTypeSpecifierAST *elab_type_spec = ast->decl_specifier_seq->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;
con's avatar
con committed
174 175 176 177 178
    for (DeclaratorListAST *it = ast->declarators; it; it = it->next) {
        Name *name = 0;
        FullySpecifiedType declTy = semantic()->check(it->declarator, qualTy,
                                                      _scope, &name);

179 180 181 182 183 184 185
        unsigned location = locationOfDeclaratorId(it->declarator);
        if (! location) {
            if (it->declarator)
                location = it->declarator->firstToken();
            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 194 195 196
            if (isQ_SIGNAL)
                fun->setMethodKey(Function::SignalMethod);
            else if (isQ_SLOT)
                fun->setMethodKey(Function::SlotMethod);
con's avatar
con committed
197 198 199 200 201 202 203
            fun->setVisibility(semantic()->currentVisibility());
        } else if (semantic()->currentMethodKey() != Function::NormalMethod) {
            translationUnit()->warning(ast->firstToken(),
                                       "expected a function declaration");
        }

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

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

210
        if (_templateParameters && it == ast->declarators && ty && ! ty->isClassType())
con's avatar
con committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
            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);

228 229 230 231 232
        if (it->declarator && it->declarator->initializer) {
            FullySpecifiedType initTy = semantic()->check(it->declarator->initializer, _scope);
            Q_UNUSED(initTy);
        }

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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
        _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);
    else if (accessSpecifier == T_SIGNALS)
        semantic()->switchMethodKey(Function::SignalMethod);
    else
        semantic()->switchMethodKey(Function::NormalMethod);
    return false;
}

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

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

bool CheckDeclaration::visit(FunctionDefinitionAST *ast)
{
    FullySpecifiedType ty = semantic()->check(ast->decl_specifier_seq, _scope);
    FullySpecifiedType qualTy = ty.qualifiedType();
    Name *name = 0;
    FullySpecifiedType funTy = semantic()->check(ast->declarator, qualTy,
                                                 _scope, &name);
278
    if (! (funTy && funTy->isFunctionType())) {
con's avatar
con committed
279 280 281 282 283
        translationUnit()->error(ast->firstToken(),
                                 "expected a function prototype");
        return false;
    }

284
    Function *fun = funTy->asFunctionType();
285 286
    fun->setStartOffset(tokenAt(ast->firstToken()).offset);
    fun->setEndOffset(tokenAt(ast->lastToken()).offset);
287 288
    if (ast->declarator)
        fun->setSourceLocation(ast->declarator->firstToken());
con's avatar
con committed
289 290 291 292 293
    fun->setName(name);
    fun->setTemplateParameters(_templateParameters);
    fun->setVisibility(semantic()->currentVisibility());
    fun->setMethodKey(semantic()->currentMethodKey());

294 295 296 297 298 299 300 301
    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
302 303
    checkFunctionArguments(fun);

Roberto Raggi's avatar
Roberto Raggi committed
304
    ast->symbol = fun;
con's avatar
con committed
305 306
    _scope->enterSymbol(fun);

307 308 309 310 311 312 313 314 315 316 317
    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");
        }
con's avatar
con committed
318 319
    }

320 321
    const int previousVisibility = semantic()->switchVisibility(Symbol::Public);
    const int previousMethodKey = semantic()->switchMethodKey(Function::NormalMethod);
con's avatar
con committed
322 323 324 325 326 327 328 329 330 331 332

    semantic()->check(ast->function_body, fun->members());

    semantic()->switchMethodKey(previousMethodKey);
    semantic()->switchVisibility(previousVisibility);

    return false;
}

bool CheckDeclaration::visit(LinkageBodyAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
333 334
    for (DeclarationListAST *decl = ast->declarations; decl; decl = decl->next) {
       semantic()->check(decl->declaration, _scope);
con's avatar
con committed
335 336 337 338 339 340
    }
    return false;
}

bool CheckDeclaration::visit(LinkageSpecificationAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
341
    semantic()->check(ast->declaration, _scope);
con's avatar
con committed
342 343 344 345 346 347 348
    return false;
}

bool CheckDeclaration::visit(NamespaceAST *ast)
{
    Identifier *id = identifier(ast->identifier_token);
    Name *namespaceName = control()->nameId(id);
349 350 351 352 353 354 355

    unsigned sourceLocation = ast->firstToken();

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

    Namespace *ns = control()->newNamespace(sourceLocation, namespaceName);
356 357
    ns->setStartOffset(tokenAt(ast->firstToken()).offset);
    ns->setEndOffset(tokenAt(ast->lastToken()).offset);
Roberto Raggi's avatar
Roberto Raggi committed
358
    ast->symbol = ns;
con's avatar
con committed
359 360 361 362 363 364 365 366 367 368 369 370 371
    _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)
{
372 373 374 375 376 377 378
    unsigned sourceLocation = locationOfDeclaratorId(ast->declarator);
    if (! sourceLocation) {
        if (ast->declarator)
            sourceLocation = ast->declarator->firstToken();
        else
            sourceLocation = ast->firstToken();
    }
379

con's avatar
con committed
380 381 382 383 384
    Name *argName = 0;
    FullySpecifiedType ty = semantic()->check(ast->type_specifier, _scope);
    FullySpecifiedType argTy = semantic()->check(ast->declarator, ty.qualifiedType(),
                                                 _scope, &argName);
    FullySpecifiedType exprTy = semantic()->check(ast->expression, _scope);
385
    Argument *arg = control()->newArgument(sourceLocation, argName);
Roberto Raggi's avatar
Roberto Raggi committed
386
    ast->symbol = arg;
con's avatar
con committed
387 388 389 390 391 392 393 394 395 396
    if (ast->expression)
        arg->setInitializer(true);
    arg->setType(argTy);
    _scope->enterSymbol(arg);
    return false;
}

bool CheckDeclaration::visit(TemplateDeclarationAST *ast)
{
    Scope *previousScope = switchScope(new Scope(_scope->owner()));
Roberto Raggi's avatar
Roberto Raggi committed
397 398
    for (DeclarationListAST *param = ast->template_parameters; param; param = param->next) {
       semantic()->check(param->declaration, _scope);
con's avatar
con committed
399 400 401 402 403 404 405 406 407
    }

    Scope *templateParameters = switchScope(previousScope);
    semantic()->check(ast->declaration, _scope, templateParameters);
    return false;
}

bool CheckDeclaration::visit(TypenameTypeParameterAST *ast)
{
408 409 410 411
    unsigned sourceLocation = ast->firstToken();
    if (ast->name)
        sourceLocation = ast->name->firstToken();

con's avatar
con committed
412
    Name *name = semantic()->check(ast->name, _scope);
413
    Argument *arg = control()->newArgument(sourceLocation, name); // ### new template type
Roberto Raggi's avatar
Roberto Raggi committed
414
    ast->symbol = arg;
con's avatar
con committed
415 416 417 418 419 420
    _scope->enterSymbol(arg);
    return false;
}

bool CheckDeclaration::visit(TemplateTypeParameterAST *ast)
{
421 422 423 424
    unsigned sourceLocation = ast->firstToken();
    if (ast->name)
        sourceLocation = ast->name->firstToken();

con's avatar
con committed
425
    Name *name = semantic()->check(ast->name, _scope);
426
    Argument *arg = control()->newArgument(sourceLocation, name); // ### new template type
Roberto Raggi's avatar
Roberto Raggi committed
427
    ast->symbol = arg;
con's avatar
con committed
428 429 430 431 432 433 434
    _scope->enterSymbol(arg);
    return false;
}

bool CheckDeclaration::visit(UsingAST *ast)
{
    Name *name = semantic()->check(ast->name, _scope);
435 436 437 438 439 440

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

    UsingDeclaration *u = control()->newUsingDeclaration(sourceLocation, name);
Roberto Raggi's avatar
Roberto Raggi committed
441
    ast->symbol = u;
con's avatar
con committed
442 443 444 445 446 447 448
    _scope->enterSymbol(u);
    return false;
}

bool CheckDeclaration::visit(UsingDirectiveAST *ast)
{
    Name *name = semantic()->check(ast->name, _scope);
449 450 451 452 453 454

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

    UsingNamespaceDirective *u = control()->newUsingNamespaceDirective(sourceLocation, name);
Roberto Raggi's avatar
Roberto Raggi committed
455
    ast->symbol = u;
con's avatar
con committed
456
    _scope->enterSymbol(u);
457 458 459 460 461

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

con's avatar
con committed
462 463 464 465
    return false;
}

CPLUSPLUS_END_NAMESPACE