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 "Scope.h"
|
|
|
|
|
#include "Symbols.h"
|
|
|
|
|
#include "Names.h"
|
|
|
|
|
#include "Literals.h"
|
2011-03-28 13:21:37 +02:00
|
|
|
#include "Templates.h"
|
2013-05-13 10:20:00 +02:00
|
|
|
|
|
|
|
|
#include "cppassert.h"
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <cstring>
|
2009-07-27 21:47:03 +02:00
|
|
|
|
2009-10-20 11:21:25 +02:00
|
|
|
using namespace CPlusPlus;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
class CPlusPlus::SymbolTable
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-08-11 12:26:02 +02:00
|
|
|
SymbolTable(const SymbolTable &other);
|
|
|
|
|
void operator =(const SymbolTable &other);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
public:
|
|
|
|
|
typedef Symbol **iterator;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
public:
|
|
|
|
|
/// Constructs an empty Scope.
|
|
|
|
|
SymbolTable(Scope *owner = 0);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
/// Destroy this scope.
|
|
|
|
|
~SymbolTable();
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
/// Returns this scope's owner Symbol.
|
|
|
|
|
Scope *owner() const;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
/// Sets this scope's owner Symbol.
|
|
|
|
|
void setOwner(Scope *owner); // ### remove me
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
/// Adds a Symbol to this Scope.
|
|
|
|
|
void enterSymbol(Symbol *symbol);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
/// Returns true if this Scope is empty; otherwise returns false.
|
|
|
|
|
bool isEmpty() const;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
/// Returns the number of symbols is in the scope.
|
|
|
|
|
unsigned symbolCount() const;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
/// Returns the Symbol at the given position.
|
|
|
|
|
Symbol *symbolAt(unsigned index) const;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
/// Returns the first Symbol in the scope.
|
|
|
|
|
iterator firstSymbol() const;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
/// Returns the last Symbol in the scope.
|
|
|
|
|
iterator lastSymbol() const;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
Symbol *lookat(const Identifier *id) const;
|
2010-09-03 12:11:15 +02:00
|
|
|
Symbol *lookat(OperatorNameId::Kind operatorId) const;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
private:
|
|
|
|
|
/// Returns the hash value for the given Symbol.
|
|
|
|
|
unsigned hashValue(Symbol *symbol) const;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
/// Updates the hash table.
|
|
|
|
|
void rehash();
|
2009-12-18 16:58:14 +01:00
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
private:
|
2010-09-02 16:44:58 +02:00
|
|
|
enum { DefaultInitialSize = 4 };
|
2010-05-05 10:18:11 +02:00
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
Scope *_owner;
|
|
|
|
|
Symbol **_symbols;
|
|
|
|
|
Symbol **_hash;
|
|
|
|
|
int _allocatedSymbols;
|
|
|
|
|
int _symbolCount;
|
|
|
|
|
int _hashSize;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SymbolTable::SymbolTable(Scope *owner)
|
|
|
|
|
: _owner(owner),
|
|
|
|
|
_symbols(0),
|
|
|
|
|
_hash(0),
|
|
|
|
|
_allocatedSymbols(0),
|
|
|
|
|
_symbolCount(-1),
|
|
|
|
|
_hashSize(0)
|
|
|
|
|
{ }
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
SymbolTable::~SymbolTable()
|
2009-12-29 18:19:35 +01:00
|
|
|
{
|
2010-08-11 12:26:02 +02:00
|
|
|
if (_symbols)
|
|
|
|
|
free(_symbols);
|
|
|
|
|
if (_hash)
|
|
|
|
|
free(_hash);
|
2009-12-29 18:19:35 +01:00
|
|
|
}
|
|
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
void SymbolTable::enterSymbol(Symbol *symbol)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2013-07-08 15:28:07 +02:00
|
|
|
CPP_ASSERT(! symbol->_enclosingScope || symbol->enclosingScope() == _owner, return);
|
2013-05-13 10:20:00 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
if (++_symbolCount == _allocatedSymbols) {
|
|
|
|
|
_allocatedSymbols <<= 1;
|
|
|
|
|
if (! _allocatedSymbols)
|
|
|
|
|
_allocatedSymbols = DefaultInitialSize;
|
|
|
|
|
|
|
|
|
|
_symbols = reinterpret_cast<Symbol **>(realloc(_symbols, sizeof(Symbol *) * _allocatedSymbols));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
symbol->_index = _symbolCount;
|
2013-07-08 15:28:07 +02:00
|
|
|
symbol->_enclosingScope = _owner;
|
2008-12-02 12:01:29 +01:00
|
|
|
_symbols[_symbolCount] = symbol;
|
|
|
|
|
|
2010-09-02 16:44:58 +02:00
|
|
|
if (_symbolCount * 5 >= _hashSize * 3)
|
2008-12-02 12:01:29 +01:00
|
|
|
rehash();
|
|
|
|
|
else {
|
|
|
|
|
const unsigned h = hashValue(symbol);
|
|
|
|
|
symbol->_next = _hash[h];
|
|
|
|
|
_hash[h] = symbol;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
Symbol *SymbolTable::lookat(const Identifier *id) const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-02-10 13:45:19 +01:00
|
|
|
if (! _hash || ! id)
|
2008-12-02 12:01:29 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
const unsigned h = id->hashCode() % _hashSize;
|
|
|
|
|
Symbol *symbol = _hash[h];
|
|
|
|
|
for (; symbol; symbol = symbol->_next) {
|
2010-08-26 12:23:09 +02:00
|
|
|
const Name *identity = symbol->unqualifiedName();
|
2009-02-09 20:22:00 +01:00
|
|
|
if (! identity) {
|
|
|
|
|
continue;
|
2010-09-02 11:59:01 +02:00
|
|
|
} else if (const Identifier *nameId = identity->asNameId()) {
|
2008-12-02 12:01:29 +01:00
|
|
|
if (nameId->identifier()->isEqualTo(id))
|
|
|
|
|
break;
|
2009-12-01 12:46:15 +01:00
|
|
|
} else if (const TemplateNameId *t = identity->asTemplateNameId()) {
|
2008-12-02 12:01:29 +01:00
|
|
|
if (t->identifier()->isEqualTo(id))
|
|
|
|
|
break;
|
2009-12-01 12:46:15 +01:00
|
|
|
} else if (const DestructorNameId *d = identity->asDestructorNameId()) {
|
2008-12-02 12:01:29 +01:00
|
|
|
if (d->identifier()->isEqualTo(id))
|
|
|
|
|
break;
|
|
|
|
|
} else if (identity->isQualifiedNameId()) {
|
2009-12-01 12:46:15 +01:00
|
|
|
return 0;
|
|
|
|
|
} else if (const SelectorNameId *selectorNameId = identity->asSelectorNameId()) {
|
2009-11-11 09:21:06 +01:00
|
|
|
if (selectorNameId->identifier()->isEqualTo(id))
|
|
|
|
|
break;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return symbol;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-03 12:11:15 +02:00
|
|
|
Symbol *SymbolTable::lookat(OperatorNameId::Kind operatorId) const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
if (! _hash)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
const unsigned h = operatorId % _hashSize;
|
|
|
|
|
Symbol *symbol = _hash[h];
|
|
|
|
|
for (; symbol; symbol = symbol->_next) {
|
2010-09-27 17:52:40 +02:00
|
|
|
if (const Name *identity = symbol->unqualifiedName()) {
|
|
|
|
|
if (const OperatorNameId *op = identity->asOperatorNameId()) {
|
|
|
|
|
if (op->kind() == operatorId)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return symbol;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
void SymbolTable::rehash()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
_hashSize <<= 1;
|
|
|
|
|
|
|
|
|
|
if (! _hashSize)
|
|
|
|
|
_hashSize = DefaultInitialSize;
|
|
|
|
|
|
|
|
|
|
_hash = reinterpret_cast<Symbol **>(realloc(_hash, sizeof(Symbol *) * _hashSize));
|
2009-11-23 11:56:44 +01:00
|
|
|
std::memset(_hash, 0, sizeof(Symbol *) * _hashSize);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
for (int index = 0; index < _symbolCount + 1; ++index) {
|
|
|
|
|
Symbol *symbol = _symbols[index];
|
|
|
|
|
const unsigned h = hashValue(symbol);
|
|
|
|
|
symbol->_next = _hash[h];
|
|
|
|
|
_hash[h] = symbol;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
unsigned SymbolTable::hashValue(Symbol *symbol) const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
if (! symbol)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return symbol->hashCode() % _hashSize;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
bool SymbolTable::isEmpty() const
|
2008-12-02 12:01:29 +01:00
|
|
|
{ return _symbolCount == -1; }
|
|
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
unsigned SymbolTable::symbolCount() const
|
2008-12-02 12:01:29 +01:00
|
|
|
{ return _symbolCount + 1; }
|
|
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
Symbol *SymbolTable::symbolAt(unsigned index) const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
if (! _symbols)
|
|
|
|
|
return 0;
|
|
|
|
|
return _symbols[index];
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
SymbolTable::iterator SymbolTable::firstSymbol() const
|
2008-12-02 12:01:29 +01:00
|
|
|
{ return _symbols; }
|
|
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
SymbolTable::iterator SymbolTable::lastSymbol() const
|
2008-12-02 12:01:29 +01:00
|
|
|
{ return _symbols + _symbolCount + 1; }
|
|
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
Scope::Scope(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name)
|
|
|
|
|
: Symbol(translationUnit, sourceLocation, name),
|
|
|
|
|
_members(0),
|
|
|
|
|
_startOffset(0),
|
|
|
|
|
_endOffset(0)
|
|
|
|
|
{ }
|
|
|
|
|
|
2011-03-28 13:21:37 +02:00
|
|
|
Scope::Scope(Clone *clone, Subst *subst, Scope *original)
|
|
|
|
|
: Symbol(clone, subst, original)
|
|
|
|
|
, _members(0)
|
|
|
|
|
, _startOffset(original->_startOffset)
|
|
|
|
|
, _endOffset(original->_endOffset)
|
|
|
|
|
{
|
|
|
|
|
for (iterator it = original->firstMember(), end = original->lastMember(); it != end; ++it)
|
|
|
|
|
addMember(clone->symbol(*it, subst));
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
Scope::~Scope()
|
|
|
|
|
{ delete _members; }
|
|
|
|
|
|
|
|
|
|
/// Adds a Symbol to this Scope.
|
|
|
|
|
void Scope::addMember(Symbol *symbol)
|
|
|
|
|
{
|
|
|
|
|
if (! _members)
|
|
|
|
|
_members = new SymbolTable(this);
|
|
|
|
|
|
|
|
|
|
_members->enterSymbol(symbol);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns true if this Scope is empty; otherwise returns false.
|
|
|
|
|
bool Scope::isEmpty() const
|
|
|
|
|
{ return _members ? _members->isEmpty() : true; }
|
|
|
|
|
|
|
|
|
|
/// Returns the number of symbols is in the scope.
|
|
|
|
|
unsigned Scope::memberCount() const
|
|
|
|
|
{ return _members ? _members->symbolCount() : 0; }
|
|
|
|
|
|
|
|
|
|
/// Returns the Symbol at the given position.
|
|
|
|
|
Symbol *Scope::memberAt(unsigned index) const
|
|
|
|
|
{ return _members ? _members->symbolAt(index) : 0; }
|
|
|
|
|
|
|
|
|
|
/// Returns the first Symbol in the scope.
|
|
|
|
|
Scope::iterator Scope::firstMember() const
|
|
|
|
|
{ return _members ? _members->firstSymbol() : 0; }
|
|
|
|
|
|
|
|
|
|
/// Returns the last Symbol in the scope.
|
|
|
|
|
Scope::iterator Scope::lastMember() const
|
|
|
|
|
{ return _members ? _members->lastSymbol() : 0; }
|
|
|
|
|
|
|
|
|
|
Symbol *Scope::find(const Identifier *id) const
|
|
|
|
|
{ return _members ? _members->lookat(id) : 0; }
|
|
|
|
|
|
2010-09-03 12:11:15 +02:00
|
|
|
Symbol *Scope::find(OperatorNameId::Kind operatorId) const
|
2010-08-11 12:26:02 +02:00
|
|
|
{ return _members ? _members->lookat(operatorId) : 0; }
|
|
|
|
|
|
|
|
|
|
/// Set the start offset of the scope
|
2010-07-16 16:01:41 +02:00
|
|
|
unsigned Scope::startOffset() const
|
|
|
|
|
{ return _startOffset; }
|
|
|
|
|
|
|
|
|
|
void Scope::setStartOffset(unsigned offset)
|
|
|
|
|
{ _startOffset = offset; }
|
|
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
/// Set the end offset of the scope
|
2010-07-16 16:01:41 +02:00
|
|
|
unsigned Scope::endOffset() const
|
|
|
|
|
{ return _endOffset; }
|
|
|
|
|
|
|
|
|
|
void Scope::setEndOffset(unsigned offset)
|
|
|
|
|
{ _endOffset = offset; }
|