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

421 lines
10 KiB
C++
Raw Normal View History

/**************************************************************************
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator
**
2010-03-05 11:25:49 +01:00
** Copyright (c) 2010 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 "Symbol.h"
#include "Symbols.h"
#include "Control.h"
#include "Names.h"
#include "TranslationUnit.h"
#include "Literals.h"
#include "MemoryPool.h"
#include "SymbolVisitor.h"
#include "NameVisitor.h"
#include "Scope.h"
2008-12-02 12:01:29 +01:00
#include <cassert>
using namespace CPlusPlus;
2008-12-02 12:01:29 +01:00
class Symbol::HashCode: protected NameVisitor
{
public:
HashCode()
: _value(0)
{ }
virtual ~HashCode()
{ }
2009-12-01 12:46:15 +01:00
unsigned operator()(const Name *name)
2008-12-02 12:01:29 +01:00
{
unsigned previousValue = switchValue(0);
accept(name);
return switchValue(previousValue);
}
protected:
unsigned switchValue(unsigned value)
{
unsigned previousValue = _value;
_value = value;
return previousValue;
}
2009-12-01 12:46:15 +01:00
virtual void visit(const NameId *name)
2008-12-02 12:01:29 +01:00
{ _value = name->identifier()->hashCode(); }
2009-12-01 12:46:15 +01:00
virtual void visit(const TemplateNameId *name)
2008-12-02 12:01:29 +01:00
{ _value = name->identifier()->hashCode(); }
2009-12-01 12:46:15 +01:00
virtual void visit(const DestructorNameId *name)
2008-12-02 12:01:29 +01:00
{ _value = name->identifier()->hashCode(); }
2009-12-01 12:46:15 +01:00
virtual void visit(const OperatorNameId *name)
2008-12-02 12:01:29 +01:00
{ _value = unsigned(name->kind()); }
2009-12-01 12:46:15 +01:00
virtual void visit(const ConversionNameId *)
2008-12-02 12:01:29 +01:00
{ _value = 0; } // ### TODO: implement me
2009-12-01 12:46:15 +01:00
virtual void visit(const QualifiedNameId *name)
{ _value = operator()(name->name()); }
2008-12-02 12:01:29 +01:00
2009-12-01 12:46:15 +01:00
virtual void visit(const SelectorNameId *name)
{ _value = name->identifier()->hashCode(); }
2008-12-02 12:01:29 +01:00
private:
unsigned _value;
};
2009-12-01 12:46:15 +01:00
Symbol::Symbol(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name)
: _name(0),
2008-12-02 12:01:29 +01:00
_hashCode(0),
_storage(Symbol::NoStorage),
_visibility(Symbol::Public),
_scope(0),
_index(0),
_next(0),
_isGenerated(false),
_isDeprecated(false),
_isUnavailable(false)
2008-12-02 12:01:29 +01:00
{
setSourceLocation(sourceLocation, translationUnit);
2008-12-02 12:01:29 +01:00
setName(name);
}
Symbol::~Symbol()
{ }
void Symbol::visitSymbol(SymbolVisitor *visitor)
{
if (visitor->preVisit(this))
visitSymbol0(visitor);
visitor->postVisit(this);
}
void Symbol::visitSymbol(Symbol *symbol, SymbolVisitor *visitor)
{
if (! symbol)
return;
symbol->visitSymbol(visitor);
}
unsigned Symbol::sourceLocation() const
{ return _sourceLocation; }
bool Symbol::isGenerated() const
{ return _isGenerated; }
bool Symbol::isDeprecated() const
{ return _isDeprecated; }
void Symbol::setDeprecated(bool isDeprecated)
{ _isDeprecated = isDeprecated; }
bool Symbol::isUnavailable() const
{ return _isUnavailable; }
void Symbol::setUnavailable(bool isUnavailable)
{ _isUnavailable = isUnavailable; }
void Symbol::setSourceLocation(unsigned sourceLocation, TranslationUnit *translationUnit)
2009-03-03 13:46:37 +01:00
{
_sourceLocation = sourceLocation;
2010-05-10 16:21:12 +02:00
if (translationUnit) {
const Token &tk = translationUnit->tokenAt(sourceLocation);
_isGenerated = tk.f.generated;
2010-05-10 16:21:12 +02:00
translationUnit->getPosition(tk.offset, &_line, &_column, &_fileId);
} else {
_isGenerated = false;
_line = 0;
_column = 0;
_fileId = 0;
}
2009-03-03 13:46:37 +01:00
}
2008-12-02 12:01:29 +01:00
unsigned Symbol::line() const
{
return _line;
2008-12-02 12:01:29 +01:00
}
unsigned Symbol::column() const
{
return _column;
2008-12-02 12:01:29 +01:00
}
2009-12-01 11:33:13 +01:00
const StringLiteral *Symbol::fileId() const
2008-12-02 12:01:29 +01:00
{
return _fileId;
2008-12-02 12:01:29 +01:00
}
const char *Symbol::fileName() const
{ return fileId()->chars(); }
unsigned Symbol::fileNameLength() const
{ return fileId()->size(); }
2009-12-01 12:46:15 +01:00
const Name *Symbol::identity() const
2008-12-02 12:01:29 +01:00
{
if (! _name)
return 0;
else if (const QualifiedNameId *q = _name->asQualifiedNameId())
return q->name();
return _name;
2008-12-02 12:01:29 +01:00
}
2009-12-01 12:46:15 +01:00
const Name *Symbol::name() const
2008-12-02 12:01:29 +01:00
{ return _name; }
2009-12-01 12:46:15 +01:00
void Symbol::setName(const Name *name)
2008-12-02 12:01:29 +01:00
{
_name = name;
if (! _name)
_hashCode = 0;
else {
HashCode hh;
_hashCode = hh(identity());
2008-12-02 12:01:29 +01:00
}
}
2009-12-01 11:33:13 +01:00
const Identifier *Symbol::identifier() const
{
if (_name)
return _name->identifier();
return 0;
}
2008-12-02 12:01:29 +01:00
Scope *Symbol::scope() const
{ return _scope; }
void Symbol::setScope(Scope *scope)
{
assert(! _scope);
_scope = scope;
}
2010-08-11 12:26:02 +02:00
Namespace *Symbol::enclosingNamespace() const
{
2010-08-11 12:26:02 +02:00
for (Scope *s = _scope; s; s = s->scope()) {
if (Namespace *ns = s->asNamespace())
return ns;
}
return 0;
}
2010-08-11 12:26:02 +02:00
Class *Symbol::enclosingClass() const
{
2010-08-11 12:26:02 +02:00
for (Scope *s = _scope; s; s = s->scope()) {
if (Class *klass = s->asClass())
return klass;
}
return 0;
}
2010-08-11 12:26:02 +02:00
Enum *Symbol::enclosingEnum() const
{
2010-08-11 12:26:02 +02:00
for (Scope *s = _scope; s; s = s->scope()) {
if (Enum *e = s->asEnum())
return e;
}
return 0;
}
2010-08-11 12:26:02 +02:00
Function *Symbol::enclosingFunction() const
{
2010-08-11 12:26:02 +02:00
for (Scope *s = _scope; s; s = s->scope()) {
if (Function *fun = s->asFunction())
return fun;
}
return 0;
}
2010-08-11 12:26:02 +02:00
Block *Symbol::enclosingBlock() const
{
2010-08-11 12:26:02 +02:00
for (Scope *s = _scope; s; s = s->scope()) {
if (Block *block = s->asBlock())
return block;
}
return 0;
}
2008-12-02 12:01:29 +01:00
unsigned Symbol::index() const
{ return _index; }
Symbol *Symbol::next() const
{ return _next; }
unsigned Symbol::hashCode() const
{ return _hashCode; }
int Symbol::storage() const
{ return _storage; }
void Symbol::setStorage(int storage)
{ _storage = storage; }
int Symbol::visibility() const
{ return _visibility; }
void Symbol::setVisibility(int visibility)
{ _visibility = visibility; }
bool Symbol::isFriend() const
{ return _storage == Friend; }
bool Symbol::isRegister() const
{ return _storage == Register; }
bool Symbol::isStatic() const
{ return _storage == Static; }
bool Symbol::isExtern() const
{ return _storage == Extern; }
bool Symbol::isMutable() const
{ return _storage == Mutable; }
bool Symbol::isTypedef() const
{ return _storage == Typedef; }
bool Symbol::isPublic() const
{ return _visibility == Public; }
bool Symbol::isProtected() const
{ return _visibility == Protected; }
bool Symbol::isPrivate() const
{ return _visibility == Private; }
bool Symbol::isScopedSymbol() const
2010-08-11 12:26:02 +02:00
{ return asScope() != 0; }
2008-12-02 12:01:29 +01:00
bool Symbol::isEnum() const
{ return asEnum() != 0; }
2008-12-02 12:01:29 +01:00
bool Symbol::isFunction() const
{ return asFunction() != 0; }
2008-12-02 12:01:29 +01:00
bool Symbol::isNamespace() const
{ return asNamespace() != 0; }
2008-12-02 12:01:29 +01:00
bool Symbol::isTemplate() const
2010-08-11 15:24:55 +02:00
{ return asTemplate() != 0; }
2008-12-02 12:01:29 +01:00
bool Symbol::isClass() const
{ return asClass() != 0; }
2008-12-02 12:01:29 +01:00
bool Symbol::isForwardClassDeclaration() const
{ return asForwardClassDeclaration() != 0; }
2008-12-02 12:01:29 +01:00
bool Symbol::isBlock() const
{ return asBlock() != 0; }
2008-12-02 12:01:29 +01:00
bool Symbol::isUsingNamespaceDirective() const
{ return asUsingNamespaceDirective() != 0; }
2008-12-02 12:01:29 +01:00
bool Symbol::isUsingDeclaration() const
{ return asUsingDeclaration() != 0; }
2008-12-02 12:01:29 +01:00
bool Symbol::isDeclaration() const
{ return asDeclaration() != 0; }
2008-12-02 12:01:29 +01:00
bool Symbol::isArgument() const
{ return asArgument() != 0; }
2008-12-02 12:01:29 +01:00
2009-12-08 11:58:27 +01:00
bool Symbol::isTypenameArgument() const
{ return asTypenameArgument() != 0; }
2008-12-02 12:01:29 +01:00
bool Symbol::isBaseClass() const
{ return asBaseClass() != 0; }
2008-12-02 12:01:29 +01:00
bool Symbol::isObjCBaseClass() const
{ return asObjCBaseClass() != 0; }
bool Symbol::isObjCBaseProtocol() const
{ return asObjCBaseProtocol() != 0; }
bool Symbol::isObjCClass() const
{ return asObjCClass() != 0; }
bool Symbol::isObjCForwardClassDeclaration() const
{ return asObjCForwardClassDeclaration() != 0; }
bool Symbol::isObjCProtocol() const
{ return asObjCProtocol() != 0; }
bool Symbol::isObjCForwardProtocolDeclaration() const
{ return asObjCForwardProtocolDeclaration() != 0; }
bool Symbol::isObjCMethod() const
{ return asObjCMethod() != 0; }
bool Symbol::isObjCPropertyDeclaration() const
{ return asObjCPropertyDeclaration() != 0; }
void Symbol::copy(Symbol *other)
{
_sourceLocation = other->_sourceLocation;
_name = other->_name;
_hashCode = other->_hashCode;
_storage = other->_storage;
_visibility = other->_visibility;
_scope = other->_scope;
_index = other->_index;
_next = other->_next;
_fileId = other->_fileId;
_line = other->_line;
_column = other->_column;
_isGenerated = other->_isGenerated;
_isDeprecated = other->_isDeprecated;
}