Files
qt-creator/src/shared/cplusplus/CheckSpecifier.cpp

419 lines
16 KiB
C++
Raw Normal View History

/**************************************************************************
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
2008-12-02 12:01:29 +01:00
**
** Contact: Nokia Corporation (qt-info@nokia.com)
2008-12-02 12:01:29 +01:00
**
** Commercial Usage
**
** 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.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
2009-08-14 09:30:56 +02:00
** contact the sales department at http://qt.nokia.com/contact.
2008-12-02 12:01:29 +01:00
**
**************************************************************************/
2008-12-02 12:01:29 +01:00
// 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 "CheckSpecifier.h"
#include "Semantic.h"
#include "AST.h"
#include "Token.h"
#include "TranslationUnit.h"
#include "Literals.h"
#include "Names.h"
#include "CoreTypes.h"
#include "Symbols.h"
#include "Control.h"
#include "Scope.h"
using namespace CPlusPlus;
2008-12-02 12:01:29 +01:00
CheckSpecifier::CheckSpecifier(Semantic *semantic)
: SemanticCheck(semantic),
_specifier(0),
_scope(0)
{ }
CheckSpecifier::~CheckSpecifier()
{ }
2009-11-10 16:00:22 +01:00
FullySpecifiedType CheckSpecifier::check(SpecifierListAST *specifier, Scope *scope)
2008-12-02 12:01:29 +01:00
{
FullySpecifiedType previousType = switchFullySpecifiedType(FullySpecifiedType());
Scope *previousScope = switchScope(scope);
2009-11-10 16:00:22 +01:00
SpecifierListAST *previousSpecifier = switchSpecifier(specifier);
2008-12-02 12:01:29 +01:00
accept(specifier);
(void) switchSpecifier(previousSpecifier);
(void) switchScope(previousScope);
return switchFullySpecifiedType(previousType);
}
FullySpecifiedType CheckSpecifier::check(ObjCTypeNameAST *typeName, Scope *scope)
{
FullySpecifiedType previousType = switchFullySpecifiedType(FullySpecifiedType());
Scope *previousScope = switchScope(scope);
accept(typeName);
(void) switchScope(previousScope);
return switchFullySpecifiedType(previousType);
}
2009-11-10 16:00:22 +01:00
SpecifierListAST *CheckSpecifier::switchSpecifier(SpecifierListAST *specifier)
2008-12-02 12:01:29 +01:00
{
2009-11-10 16:00:22 +01:00
SpecifierListAST *previousSpecifier = _specifier;
2008-12-02 12:01:29 +01:00
_specifier = specifier;
return previousSpecifier;
}
2009-11-10 16:00:22 +01:00
FullySpecifiedType CheckSpecifier::switchFullySpecifiedType(const FullySpecifiedType &type)
2008-12-02 12:01:29 +01:00
{
FullySpecifiedType previousType = _fullySpecifiedType;
_fullySpecifiedType = type;
return previousType;
}
Scope *CheckSpecifier::switchScope(Scope *scope)
{
Scope *previousScope = _scope;
_scope = scope;
return previousScope;
}
bool CheckSpecifier::visit(SimpleSpecifierAST *ast)
{
switch (tokenKind(ast->specifier_token)) {
case T_CONST:
if (_fullySpecifiedType.isConst())
translationUnit()->error(ast->specifier_token,
"duplicate `%s'", spell(ast->specifier_token));
_fullySpecifiedType.setConst(true);
break;
case T_VOLATILE:
if (_fullySpecifiedType.isVolatile())
translationUnit()->error(ast->specifier_token,
"duplicate `%s'", spell(ast->specifier_token));
_fullySpecifiedType.setVolatile(true);
break;
case T_FRIEND:
if (_fullySpecifiedType.isFriend())
translationUnit()->error(ast->specifier_token,
"duplicate `%s'", spell(ast->specifier_token));
_fullySpecifiedType.setFriend(true);
break;
case T_REGISTER:
if (_fullySpecifiedType.isRegister())
translationUnit()->error(ast->specifier_token,
"duplicate `%s'", spell(ast->specifier_token));
_fullySpecifiedType.setRegister(true);
break;
case T_STATIC:
if (_fullySpecifiedType.isStatic())
translationUnit()->error(ast->specifier_token,
"duplicate `%s'", spell(ast->specifier_token));
_fullySpecifiedType.setStatic(true);
break;
case T_EXTERN:
if (_fullySpecifiedType.isExtern())
translationUnit()->error(ast->specifier_token,
"duplicate `%s'", spell(ast->specifier_token));
_fullySpecifiedType.setExtern(true);
break;
case T_MUTABLE:
if (_fullySpecifiedType.isMutable())
translationUnit()->error(ast->specifier_token,
"duplicate `%s'", spell(ast->specifier_token));
_fullySpecifiedType.setMutable(true);
break;
case T_TYPEDEF:
if (_fullySpecifiedType.isTypedef())
translationUnit()->error(ast->specifier_token,
"duplicate `%s'", spell(ast->specifier_token));
_fullySpecifiedType.setTypedef(true);
break;
case T_INLINE:
if (_fullySpecifiedType.isInline())
translationUnit()->error(ast->specifier_token,
"duplicate `%s'", spell(ast->specifier_token));
_fullySpecifiedType.setInline(true);
break;
case T_VIRTUAL:
if (_fullySpecifiedType.isVirtual())
translationUnit()->error(ast->specifier_token,
"duplicate `%s'", spell(ast->specifier_token));
_fullySpecifiedType.setVirtual(true);
break;
case T_EXPLICIT:
if (_fullySpecifiedType.isExplicit())
translationUnit()->error(ast->specifier_token,
"duplicate `%s'", spell(ast->specifier_token));
_fullySpecifiedType.setExplicit(true);
break;
case T_SIGNED:
if (_fullySpecifiedType.isSigned())
translationUnit()->error(ast->specifier_token,
"duplicate `%s'", spell(ast->specifier_token));
_fullySpecifiedType.setSigned(true);
break;
case T_UNSIGNED:
if (_fullySpecifiedType.isUnsigned())
translationUnit()->error(ast->specifier_token,
"duplicate `%s'", spell(ast->specifier_token));
_fullySpecifiedType.setUnsigned(true);
break;
case T_CHAR:
if (_fullySpecifiedType)
2008-12-02 12:01:29 +01:00
translationUnit()->error(ast->specifier_token,
"duplicate data type in declaration");
_fullySpecifiedType.setType(control()->integerType(IntegerType::Char));
break;
case T_WCHAR_T:
if (_fullySpecifiedType)
2008-12-02 12:01:29 +01:00
translationUnit()->error(ast->specifier_token,
"duplicate data type in declaration");
_fullySpecifiedType.setType(control()->integerType(IntegerType::WideChar));
break;
case T_BOOL:
if (_fullySpecifiedType)
2008-12-02 12:01:29 +01:00
translationUnit()->error(ast->specifier_token,
"duplicate data type in declaration");
_fullySpecifiedType.setType(control()->integerType(IntegerType::Bool));
break;
case T_SHORT:
if (_fullySpecifiedType) {
2008-12-02 12:01:29 +01:00
IntegerType *intType = control()->integerType(IntegerType::Int);
if (_fullySpecifiedType.type() != intType)
2008-12-02 12:01:29 +01:00
translationUnit()->error(ast->specifier_token,
"duplicate data type in declaration");
}
_fullySpecifiedType.setType(control()->integerType(IntegerType::Short));
break;
case T_INT:
if (_fullySpecifiedType) {
Type *tp = _fullySpecifiedType.type();
2008-12-02 12:01:29 +01:00
IntegerType *shortType = control()->integerType(IntegerType::Short);
IntegerType *longType = control()->integerType(IntegerType::Long);
IntegerType *longLongType = control()->integerType(IntegerType::LongLong);
if (tp == shortType || tp == longType || tp == longLongType)
break;
translationUnit()->error(ast->specifier_token,
"duplicate data type in declaration");
}
_fullySpecifiedType.setType(control()->integerType(IntegerType::Int));
break;
case T_LONG:
if (_fullySpecifiedType) {
Type *tp = _fullySpecifiedType.type();
2008-12-02 12:01:29 +01:00
IntegerType *intType = control()->integerType(IntegerType::Int);
IntegerType *longType = control()->integerType(IntegerType::Long);
FloatType *doubleType = control()->floatType(FloatType::Double);
if (tp == longType) {
_fullySpecifiedType.setType(control()->integerType(IntegerType::LongLong));
break;
} else if (tp == doubleType) {
_fullySpecifiedType.setType(control()->floatType(FloatType::LongDouble));
break;
} else if (tp != intType) {
translationUnit()->error(ast->specifier_token,
"duplicate data type in declaration");
}
}
_fullySpecifiedType.setType(control()->integerType(IntegerType::Long));
break;
case T_FLOAT:
if (_fullySpecifiedType)
2008-12-02 12:01:29 +01:00
translationUnit()->error(ast->specifier_token,
"duplicate data type in declaration");
_fullySpecifiedType.setType(control()->floatType(FloatType::Float));
break;
case T_DOUBLE:
if (_fullySpecifiedType) {
2008-12-02 12:01:29 +01:00
IntegerType *longType = control()->integerType(IntegerType::Long);
if (_fullySpecifiedType.type() == longType) {
2008-12-02 12:01:29 +01:00
_fullySpecifiedType.setType(control()->floatType(FloatType::LongDouble));
break;
}
translationUnit()->error(ast->specifier_token,
"duplicate data type in declaration");
}
_fullySpecifiedType.setType(control()->floatType(FloatType::Double));
break;
case T_VOID:
if (_fullySpecifiedType)
2008-12-02 12:01:29 +01:00
translationUnit()->error(ast->specifier_token,
"duplicate data type in declaration");
_fullySpecifiedType.setType(control()->voidType());
break;
default:
break;
} // switch
2009-11-10 16:00:22 +01:00
2008-12-02 12:01:29 +01:00
return false;
}
bool CheckSpecifier::visit(ClassSpecifierAST *ast)
{
2009-03-03 13:46:37 +01:00
unsigned sourceLocation = ast->firstToken();
if (ast->name)
sourceLocation = ast->name->firstToken();
2009-12-01 12:46:15 +01:00
const Name *className = semantic()->check(ast->name, _scope);
2009-03-03 13:46:37 +01:00
Class *klass = control()->newClass(sourceLocation, className);
klass->setStartOffset(tokenAt(ast->firstToken()).offset);
klass->setEndOffset(tokenAt(ast->lastToken()).offset);
2009-02-09 12:19:17 +01:00
ast->symbol = klass;
2008-12-02 12:01:29 +01:00
unsigned classKey = tokenKind(ast->classkey_token);
if (classKey == T_CLASS)
klass->setClassKey(Class::ClassKey);
else if (classKey == T_STRUCT)
klass->setClassKey(Class::StructKey);
else if (classKey == T_UNION)
klass->setClassKey(Class::UnionKey);
klass->setVisibility(semantic()->currentVisibility());
_scope->enterSymbol(klass);
_fullySpecifiedType.setType(klass);
2009-11-10 14:03:40 +01:00
for (BaseSpecifierListAST *it = ast->base_clause_list; it; it = it->next) {
BaseSpecifierAST *base = it->value;
2009-12-01 12:46:15 +01:00
const Name *baseClassName = semantic()->check(base->name, _scope);
2008-12-02 12:01:29 +01:00
BaseClass *baseClass = control()->newBaseClass(ast->firstToken(), baseClassName);
2009-02-09 12:19:17 +01:00
base->symbol = baseClass;
2009-06-05 09:44:25 +02:00
if (base->virtual_token)
2008-12-02 12:01:29 +01:00
baseClass->setVirtual(true);
2009-06-05 09:44:25 +02:00
if (base->access_specifier_token) {
int accessSpecifier = tokenKind(base->access_specifier_token);
2008-12-02 12:01:29 +01:00
int visibility = semantic()->visibilityForAccessSpecifier(accessSpecifier);
baseClass->setVisibility(visibility);
}
klass->addBaseClass(baseClass);
}
int visibility = semantic()->visibilityForClassKey(classKey);
int previousVisibility = semantic()->switchVisibility(visibility);
int previousMethodKey = semantic()->switchMethodKey(Function::NormalMethod);
2009-11-10 16:47:16 +01:00
for (DeclarationListAST *member = ast->member_specifier_list; member; member = member->next) {
semantic()->check(member->value, klass->members());
2008-12-02 12:01:29 +01:00
}
(void) semantic()->switchMethodKey(previousMethodKey);
(void) semantic()->switchVisibility(previousVisibility);
return false;
}
bool CheckSpecifier::visit(NamedTypeSpecifierAST *ast)
{
2009-12-01 12:46:15 +01:00
const Name *name = semantic()->check(ast->name, _scope);
2008-12-02 12:01:29 +01:00
_fullySpecifiedType.setType(control()->namedType(name));
return false;
}
bool CheckSpecifier::visit(ElaboratedTypeSpecifierAST *ast)
{
2009-12-01 12:46:15 +01:00
const Name *name = semantic()->check(ast->name, _scope);
2008-12-02 12:01:29 +01:00
_fullySpecifiedType.setType(control()->namedType(name));
return false;
}
bool CheckSpecifier::visit(EnumSpecifierAST *ast)
{
2009-03-03 13:46:37 +01:00
unsigned sourceLocation = ast->firstToken();
if (ast->name)
sourceLocation = ast->name->firstToken();
2009-12-01 12:46:15 +01:00
const Name *name = semantic()->check(ast->name, _scope);
2009-03-03 13:46:37 +01:00
Enum *e = control()->newEnum(sourceLocation, name);
e->setStartOffset(tokenAt(ast->firstToken()).offset);
e->setEndOffset(tokenAt(ast->lastToken()).offset);
2008-12-02 12:01:29 +01:00
e->setVisibility(semantic()->currentVisibility());
_scope->enterSymbol(e);
_fullySpecifiedType.setType(e);
2009-11-10 16:47:16 +01:00
for (EnumeratorListAST *it = ast->enumerator_list; it; it = it->next) {
2009-11-10 14:11:21 +01:00
EnumeratorAST *enumerator = it->value;
2009-12-01 11:33:13 +01:00
const Identifier *id = identifier(enumerator->identifier_token);
2008-12-02 12:01:29 +01:00
if (! id)
continue;
2009-12-01 12:46:15 +01:00
const NameId *enumeratorName = control()->nameId(id);
2008-12-02 12:01:29 +01:00
Declaration *decl = control()->newDeclaration(enumerator->firstToken(),
enumeratorName);
e->addMember(decl);
}
return false;
}
bool CheckSpecifier::visit(TypeofSpecifierAST *ast)
{
semantic()->check(ast->expression, _scope);
return false;
}
2009-12-01 11:33:13 +01:00
bool CheckSpecifier::visit(AttributeSpecifierAST * /*ast*/)
2008-12-02 12:01:29 +01:00
{
return false;
}
bool CheckSpecifier::visit(ObjCTypeNameAST * /*ast*/)
{
// TODO: implement this (EV)
// _fullySpecifiedType = FullySpecifiedType();
return true;
}