Store the names in a set.

This commit is contained in:
Roberto Raggi
2009-12-01 14:38:42 +01:00
parent c4d1274273
commit c4737c1fdf
4 changed files with 125 additions and 180 deletions

View File

@@ -378,8 +378,9 @@ bool CheckDeclaration::visit(LinkageSpecificationAST *ast)
bool CheckDeclaration::visit(NamespaceAST *ast) bool CheckDeclaration::visit(NamespaceAST *ast)
{ {
const Identifier *id = identifier(ast->identifier_token); const Name *namespaceName = 0;
const Name *namespaceName = control()->nameId(id); if (const Identifier *id = identifier(ast->identifier_token))
namespaceName = control()->nameId(id);
unsigned sourceLocation = ast->firstToken(); unsigned sourceLocation = ast->firstToken();

View File

@@ -129,6 +129,76 @@ template <> struct Compare<ArrayType>
} }
}; };
template <> struct Compare<NameId>
{
bool operator()(const NameId &name, const NameId &otherName) const
{
return name.identifier() < otherName.identifier();
}
};
template <> struct Compare<DestructorNameId>
{
bool operator()(const DestructorNameId &name, const DestructorNameId &otherName) const
{
return name.identifier() < otherName.identifier();
}
};
template <> struct Compare<OperatorNameId>
{
bool operator()(const OperatorNameId &name, const OperatorNameId &otherName) const
{
return name.kind() < otherName.kind();
}
};
template <> struct Compare<ConversionNameId>
{
bool operator()(const ConversionNameId &name, const ConversionNameId &otherName) const
{
return name.type() < otherName.type();
}
};
template <> struct Compare<TemplateNameId>
{
bool operator()(const TemplateNameId &name, const TemplateNameId &otherName) const
{
const Identifier *id = name.identifier();
const Identifier *otherId = otherName.identifier();
if (id == otherId)
return std::lexicographical_compare(name.firstTemplateArgument(), name.lastTemplateArgument(),
otherName.firstTemplateArgument(), otherName.lastTemplateArgument());
return id < otherId;
}
};
template <> struct Compare<QualifiedNameId>
{
bool operator()(const QualifiedNameId &name, const QualifiedNameId &otherName) const
{
if (name.isGlobal() == otherName.isGlobal())
return std::lexicographical_compare(name.firstName(), name.lastName(),
otherName.firstName(), otherName.lastName());
return name.isGlobal() < otherName.isGlobal();
}
};
template <> struct Compare<SelectorNameId>
{
bool operator()(const SelectorNameId &name, const SelectorNameId &otherName) const
{
if (name.hasArguments() == otherName.hasArguments())
return std::lexicographical_compare(name.firstName(), name.lastName(),
otherName.firstName(), otherName.lastName());
return name.hasArguments() < otherName.hasArguments();
}
};
template <typename _Tp> template <typename _Tp>
class Table: public std::set<_Tp, Compare<_Tp> > class Table: public std::set<_Tp, Compare<_Tp> >
{ {
@@ -139,13 +209,6 @@ public:
} // end of anonymous namespace } // end of anonymous namespace
template <typename _Iterator>
static void delete_map_entries(_Iterator first, _Iterator last)
{
for (; first != last; ++first)
delete first->second;
}
template <typename _Iterator> template <typename _Iterator>
static void delete_array_entries(_Iterator first, _Iterator last) static void delete_array_entries(_Iterator first, _Iterator last)
{ {
@@ -153,10 +216,6 @@ static void delete_array_entries(_Iterator first, _Iterator last)
delete *first; delete *first;
} }
template <typename _Map>
static void delete_map_entries(const _Map &m)
{ delete_map_entries(m.begin(), m.end()); }
template <typename _Array> template <typename _Array>
static void delete_array_entries(const _Array &a) static void delete_array_entries(const _Array &a)
{ delete_array_entries(a.begin(), a.end()); } { delete_array_entries(a.begin(), a.end()); }
@@ -172,14 +231,6 @@ public:
~Data() ~Data()
{ {
// names
delete_map_entries(nameIds);
delete_map_entries(destructorNameIds);
delete_map_entries(operatorNameIds);
delete_map_entries(conversionNameIds);
delete_map_entries(qualifiedNameIds);
delete_map_entries(templateNameIds);
// symbols // symbols
delete_array_entries(symbols); delete_array_entries(symbols);
} }
@@ -188,77 +239,41 @@ public:
{ {
if (! id) if (! id)
return 0; return 0;
std::map<const Identifier *, const NameId *>::iterator it = nameIds.lower_bound(id);
if (it == nameIds.end() || it->first != id) return nameIds.intern(NameId(id));
it = nameIds.insert(it, std::make_pair(id, new NameId(id)));
return it->second;
} }
const TemplateNameId *findOrInsertTemplateNameId(const Identifier *id, template <typename _Iterator>
const std::vector<FullySpecifiedType> &templateArguments) const TemplateNameId *findOrInsertTemplateNameId(const Identifier *id, _Iterator first, _Iterator last)
{ {
if (! id) return templateNameIds.intern(TemplateNameId(id, first, last));
return 0;
const TemplateNameIdKey key(id, templateArguments);
std::map<TemplateNameIdKey, const TemplateNameId *>::iterator it =
templateNameIds.lower_bound(key);
if (it == templateNameIds.end() || it->first != key) {
const FullySpecifiedType *args = 0;
if (templateArguments.size())
args = &templateArguments[0];
const TemplateNameId *templ = new TemplateNameId(id, args, templateArguments.size());
it = templateNameIds.insert(it, std::make_pair(key, templ));
}
return it->second;
} }
const DestructorNameId *findOrInsertDestructorNameId(const Identifier *id) const DestructorNameId *findOrInsertDestructorNameId(const Identifier *id)
{ {
if (! id) return destructorNameIds.intern(DestructorNameId(id));
return 0;
std::map<const Identifier *, const DestructorNameId *>::iterator it = destructorNameIds.lower_bound(id);
if (it == destructorNameIds.end() || it->first != id)
it = destructorNameIds.insert(it, std::make_pair(id, new DestructorNameId(id)));
return it->second;
} }
const OperatorNameId *findOrInsertOperatorNameId(int kind) const OperatorNameId *findOrInsertOperatorNameId(int kind)
{ {
const int key(kind); return operatorNameIds.intern(OperatorNameId(kind));
std::map<int, const OperatorNameId *>::iterator it = operatorNameIds.lower_bound(key);
if (it == operatorNameIds.end() || it->first != key)
it = operatorNameIds.insert(it, std::make_pair(key, new OperatorNameId(kind)));
return it->second;
} }
const ConversionNameId *findOrInsertConversionNameId(const FullySpecifiedType &type) const ConversionNameId *findOrInsertConversionNameId(const FullySpecifiedType &type)
{ {
std::map<FullySpecifiedType, const ConversionNameId *>::iterator it = return conversionNameIds.intern(ConversionNameId(type));
conversionNameIds.lower_bound(type);
if (it == conversionNameIds.end() || it->first != type)
it = conversionNameIds.insert(it, std::make_pair(type, new ConversionNameId(type)));
return it->second;
} }
const QualifiedNameId *findOrInsertQualifiedNameId(const std::vector<const Name *> &names, bool isGlobal) template <typename _Iterator>
const QualifiedNameId *findOrInsertQualifiedNameId(_Iterator first, _Iterator last, bool isGlobal)
{ {
const QualifiedNameIdKey key(names, isGlobal); return qualifiedNameIds.intern(QualifiedNameId(first, last, isGlobal));
std::map<QualifiedNameIdKey, const QualifiedNameId *>::iterator it =
qualifiedNameIds.lower_bound(key);
if (it == qualifiedNameIds.end() || it->first != key) {
const QualifiedNameId *name = new QualifiedNameId(&names[0], names.size(), isGlobal);
it = qualifiedNameIds.insert(it, std::make_pair(key, name));
}
return it->second;
} }
const SelectorNameId *findOrInsertSelectorNameId(const std::vector<const Name *> &names, bool hasArguments) template <typename _Iterator>
const SelectorNameId *findOrInsertSelectorNameId(_Iterator first, _Iterator last, bool hasArguments)
{ {
const SelectorNameIdKey key(names, hasArguments); return selectorNameIds.intern(SelectorNameId(first, last, hasArguments));
std::map<SelectorNameIdKey, const SelectorNameId *>::iterator it = selectorNameIds.lower_bound(key);
if (it == selectorNameIds.end() || it->first != key)
it = selectorNameIds.insert(it, std::make_pair(key, new SelectorNameId(&names[0], names.size(), hasArguments)));
return it->second;
} }
IntegerType *findOrInsertIntegerType(int kind) IntegerType *findOrInsertIntegerType(int kind)
@@ -439,75 +454,6 @@ public:
return u; return u;
} }
struct TemplateNameIdKey {
const Identifier *id;
std::vector<FullySpecifiedType> templateArguments;
TemplateNameIdKey(const Identifier *id, const std::vector<FullySpecifiedType> &templateArguments)
: id(id), templateArguments(templateArguments)
{ }
bool operator == (const TemplateNameIdKey &other) const
{ return id == other.id && templateArguments == other.templateArguments; }
bool operator != (const TemplateNameIdKey &other) const
{ return ! operator==(other); }
bool operator < (const TemplateNameIdKey &other) const
{
if (id == other.id)
return std::lexicographical_compare(templateArguments.begin(),
templateArguments.end(),
other.templateArguments.begin(),
other.templateArguments.end());
return id < other.id;
}
};
struct QualifiedNameIdKey {
std::vector<const Name *> names;
bool isGlobal;
QualifiedNameIdKey(const std::vector<const Name *> &names, bool isGlobal) :
names(names), isGlobal(isGlobal)
{ }
bool operator == (const QualifiedNameIdKey &other) const
{ return isGlobal == other.isGlobal && names == other.names; }
bool operator != (const QualifiedNameIdKey &other) const
{ return ! operator==(other); }
bool operator < (const QualifiedNameIdKey &other) const
{
if (isGlobal == other.isGlobal)
return std::lexicographical_compare(names.begin(), names.end(),
other.names.begin(), other.names.end());
return isGlobal < other.isGlobal;
}
};
struct SelectorNameIdKey {
std::vector<const Name *> _names;
bool _hasArguments;
SelectorNameIdKey(const std::vector<const Name *> &names, bool hasArguments): _names(names), _hasArguments(hasArguments) {}
bool operator==(const SelectorNameIdKey &other) const
{ return _names == other._names && _hasArguments == other._hasArguments; }
bool operator!=(const SelectorNameIdKey &other) const
{ return !operator==(other); }
bool operator<(const SelectorNameIdKey &other) const
{
if (_hasArguments == other._hasArguments)
return std::lexicographical_compare(_names.begin(), _names.end(), other._names.begin(), other._names.end());
else
return _hasArguments < other._hasArguments;
}
};
Control *control; Control *control;
TranslationUnit *translationUnit; TranslationUnit *translationUnit;
DiagnosticClient *diagnosticClient; DiagnosticClient *diagnosticClient;
@@ -521,13 +467,13 @@ public:
// ### replace std::map with lookup tables. ASAP! // ### replace std::map with lookup tables. ASAP!
// names // names
std::map<const Identifier *, const NameId *> nameIds; Table<NameId> nameIds;
std::map<const Identifier *, const DestructorNameId *> destructorNameIds; Table<DestructorNameId> destructorNameIds;
std::map<int, const OperatorNameId *> operatorNameIds; Table<OperatorNameId> operatorNameIds;
std::map<FullySpecifiedType, const ConversionNameId *> conversionNameIds; Table<ConversionNameId> conversionNameIds;
std::map<TemplateNameIdKey, const TemplateNameId *> templateNameIds; Table<TemplateNameId> templateNameIds;
std::map<QualifiedNameIdKey, const QualifiedNameId *> qualifiedNameIds; Table<QualifiedNameId> qualifiedNameIds;
std::map<SelectorNameIdKey, const SelectorNameId *> selectorNameIds; Table<SelectorNameId> selectorNameIds;
// types // types
VoidType voidType; VoidType voidType;
@@ -641,8 +587,7 @@ const TemplateNameId *Control::templateNameId(const Identifier *id,
const FullySpecifiedType *const args, const FullySpecifiedType *const args,
unsigned argv) unsigned argv)
{ {
std::vector<FullySpecifiedType> templateArguments(args, args + argv); return d->findOrInsertTemplateNameId(id, args, args + argv);
return d->findOrInsertTemplateNameId(id, templateArguments);
} }
const DestructorNameId *Control::destructorNameId(const Identifier *id) const DestructorNameId *Control::destructorNameId(const Identifier *id)
@@ -658,16 +603,14 @@ const QualifiedNameId *Control::qualifiedNameId(const Name *const *names,
unsigned nameCount, unsigned nameCount,
bool isGlobal) bool isGlobal)
{ {
std::vector<const Name *> classOrNamespaceNames(names, names + nameCount); return d->findOrInsertQualifiedNameId(names, names + nameCount, isGlobal);
return d->findOrInsertQualifiedNameId(classOrNamespaceNames, isGlobal);
} }
const SelectorNameId *Control::selectorNameId(const Name *const *names, const SelectorNameId *Control::selectorNameId(const Name *const *names,
unsigned nameCount, unsigned nameCount,
bool hasArguments) bool hasArguments)
{ {
std::vector<const Name *> selectorNames(names, names + nameCount); return d->findOrInsertSelectorNameId(names, names + nameCount, hasArguments);
return d->findOrInsertSelectorNameId(selectorNames, hasArguments);
} }

View File

@@ -50,17 +50,11 @@
#include "NameVisitor.h" #include "NameVisitor.h"
#include "Literals.h" #include "Literals.h"
#include <cstring> #include <cstring>
#include <cassert>
#include <algorithm> #include <algorithm>
using namespace CPlusPlus; using namespace CPlusPlus;
QualifiedNameId::QualifiedNameId(const Name *const *names,
unsigned nameCount,
bool isGlobal)
: _names(names, names + nameCount),
_isGlobal(isGlobal)
{ }
QualifiedNameId::~QualifiedNameId() QualifiedNameId::~QualifiedNameId()
{ } { }
@@ -159,13 +153,6 @@ bool DestructorNameId::isEqualTo(const Name *other) const
return l->isEqualTo(r); return l->isEqualTo(r);
} }
TemplateNameId::TemplateNameId(const Identifier *identifier,
const FullySpecifiedType templateArguments[],
unsigned templateArgumentCount)
: _identifier(identifier),
_templateArguments(templateArguments, templateArguments + templateArgumentCount)
{ }
TemplateNameId::~TemplateNameId() TemplateNameId::~TemplateNameId()
{ } { }
@@ -249,13 +236,6 @@ bool ConversionNameId::isEqualTo(const Name *other) const
return _type.isEqualTo(c->type()); return _type.isEqualTo(c->type());
} }
SelectorNameId::SelectorNameId(const Name *const *names,
unsigned nameCount,
bool hasArguments)
: _names(names, names + nameCount),
_hasArguments(hasArguments)
{ }
SelectorNameId::~SelectorNameId() SelectorNameId::~SelectorNameId()
{ } { }

View File

@@ -59,7 +59,10 @@ namespace CPlusPlus {
class CPLUSPLUS_EXPORT QualifiedNameId: public Name class CPLUSPLUS_EXPORT QualifiedNameId: public Name
{ {
public: public:
QualifiedNameId(const Name *const *names, unsigned nameCount, bool isGlobal = false); template <typename _Iterator>
QualifiedNameId(_Iterator first, _Iterator last, bool isGlobal = false)
: _names(first, last), _isGlobal(isGlobal) {}
virtual ~QualifiedNameId(); virtual ~QualifiedNameId();
virtual const Identifier *identifier() const; virtual const Identifier *identifier() const;
@@ -75,6 +78,11 @@ public:
virtual const QualifiedNameId *asQualifiedNameId() const virtual const QualifiedNameId *asQualifiedNameId() const
{ return this; } { return this; }
typedef std::vector<const Name *>::const_iterator NameIterator;
NameIterator firstName() const { return _names.begin(); }
NameIterator lastName() const { return _names.end(); }
protected: protected:
virtual void accept0(NameVisitor *visitor) const; virtual void accept0(NameVisitor *visitor) const;
@@ -126,9 +134,10 @@ private:
class CPLUSPLUS_EXPORT TemplateNameId: public Name class CPLUSPLUS_EXPORT TemplateNameId: public Name
{ {
public: public:
TemplateNameId(const Identifier *identifier, template <typename _Iterator>
const FullySpecifiedType templateArguments[], TemplateNameId(const Identifier *identifier, _Iterator first, _Iterator last)
unsigned templateArgumentCount); : _identifier(identifier), _templateArguments(first, last) {}
virtual ~TemplateNameId(); virtual ~TemplateNameId();
virtual const Identifier *identifier() const; virtual const Identifier *identifier() const;
@@ -142,6 +151,11 @@ public:
virtual const TemplateNameId *asTemplateNameId() const virtual const TemplateNameId *asTemplateNameId() const
{ return this; } { return this; }
typedef std::vector<FullySpecifiedType>::const_iterator TemplateArgumentIterator;
TemplateArgumentIterator firstTemplateArgument() const { return _templateArguments.begin(); }
TemplateArgumentIterator lastTemplateArgument() const { return _templateArguments.end(); }
protected: protected:
virtual void accept0(NameVisitor *visitor) const; virtual void accept0(NameVisitor *visitor) const;
@@ -250,7 +264,10 @@ private:
class CPLUSPLUS_EXPORT SelectorNameId: public Name class CPLUSPLUS_EXPORT SelectorNameId: public Name
{ {
public: public:
SelectorNameId(const Name *const *names, unsigned nameCount, bool hasArguments); template <typename _Iterator>
SelectorNameId(_Iterator first, _Iterator last, bool hasArguments)
: _names(first, last), _hasArguments(hasArguments) {}
virtual ~SelectorNameId(); virtual ~SelectorNameId();
virtual const Identifier *identifier() const; virtual const Identifier *identifier() const;
@@ -264,6 +281,11 @@ public:
virtual const SelectorNameId *asSelectorNameId() const virtual const SelectorNameId *asSelectorNameId() const
{ return this; } { return this; }
typedef std::vector<const Name *>::const_iterator NameIterator;
NameIterator firstName() const { return _names.begin(); }
NameIterator lastName() const { return _names.end(); }
protected: protected:
virtual void accept0(NameVisitor *visitor) const; virtual void accept0(NameVisitor *visitor) const;
@@ -274,5 +296,4 @@ private:
} // end of namespace CPlusPlus } // end of namespace CPlusPlus
#endif // CPLUSPLUS_NAMES_H #endif // CPLUSPLUS_NAMES_H