Store the types in a set.

This commit is contained in:
Roberto Raggi
2009-11-30 18:13:08 +01:00
parent e6edf54a9c
commit 522835ae01
2 changed files with 95 additions and 85 deletions

View File

@@ -55,10 +55,90 @@
#include "Names.h" #include "Names.h"
#include "Array.h" #include "Array.h"
#include "TypeMatcher.h" #include "TypeMatcher.h"
#include <map> // ### replace me with LiteralTable #include <map>
#include <set>
using namespace CPlusPlus; using namespace CPlusPlus;
namespace {
template <typename _Tp>
struct Compare;
template <> struct Compare<IntegerType>
{
bool operator()(const IntegerType &ty, const IntegerType &otherTy) const
{ return ty.kind() < otherTy.kind(); }
};
template <> struct Compare<FloatType>
{
bool operator()(const FloatType &ty, const FloatType &otherTy) const
{ return ty.kind() < otherTy.kind(); }
};
template <> struct Compare<PointerToMemberType>
{
bool operator()(const PointerToMemberType &ty, const PointerToMemberType &otherTy) const
{
if (ty.memberName() < otherTy.memberName())
return true;
else if (ty.memberName() == otherTy.memberName())
return ty.elementType() < otherTy.elementType();
return false;
}
};
template <> struct Compare<PointerType>
{
bool operator()(const PointerType &ty, const PointerType &otherTy) const
{
return ty.elementType() < otherTy.elementType();
}
};
template <> struct Compare<ReferenceType>
{
bool operator()(const ReferenceType &ty, const ReferenceType &otherTy) const
{
return ty.elementType() < otherTy.elementType();
}
};
template <> struct Compare<NamedType>
{
bool operator()(const NamedType &ty, const NamedType &otherTy) const
{
return ty.name() < otherTy.name();
}
};
template <> struct Compare<ArrayType>
{
bool operator()(const ArrayType &ty, const ArrayType &otherTy) const
{
if (ty.size() < otherTy.size())
return true;
else if (ty.size() == otherTy.size())
return ty.elementType() < otherTy.elementType();
return false;
}
};
template <typename _Tp>
class Table: public std::set<_Tp, Compare<_Tp> >
{
public:
_Tp *intern(const _Tp &element)
{ return const_cast<_Tp *>(&*insert(element).first); }
};
} // end of anonymous namespace
template <typename _Iterator> template <typename _Iterator>
static void delete_map_entries(_Iterator first, _Iterator last) static void delete_map_entries(_Iterator first, _Iterator last)
{ {
@@ -100,15 +180,6 @@ public:
delete_map_entries(qualifiedNameIds); delete_map_entries(qualifiedNameIds);
delete_map_entries(templateNameIds); delete_map_entries(templateNameIds);
// types
delete_array_entries(integerTypes);
delete_array_entries(floatTypes);
delete_array_entries(pointerToMemberTypes);
delete_array_entries(pointerTypes);
delete_array_entries(referenceTypes);
delete_array_entries(arrayTypes);
delete_array_entries(namedTypes);
// symbols // symbols
delete_array_entries(symbols); delete_array_entries(symbols);
} }
@@ -193,95 +264,37 @@ public:
IntegerType *findOrInsertIntegerType(int kind) IntegerType *findOrInsertIntegerType(int kind)
{ {
for (unsigned i = 0; i < integerTypes.size(); ++i) { return integerTypes.intern(IntegerType(kind));
IntegerType *ty = integerTypes.at(i);
if (ty->kind() == kind)
return ty;
}
IntegerType *ty = new IntegerType(kind);
integerTypes.push_back(ty);
return ty;
} }
FloatType *findOrInsertFloatType(int kind) FloatType *findOrInsertFloatType(int kind)
{ {
for (unsigned i = 0; i < floatTypes.size(); ++i) { return floatTypes.intern(FloatType(kind));
FloatType *ty = floatTypes.at(i);
if (ty->kind() == kind)
return ty;
}
FloatType *ty = new FloatType(kind);
floatTypes.push_back(ty);
return ty;
} }
PointerToMemberType *findOrInsertPointerToMemberType(Name *memberName, const FullySpecifiedType &elementType) PointerToMemberType *findOrInsertPointerToMemberType(Name *memberName, const FullySpecifiedType &elementType)
{ {
for (unsigned i = 0; i < pointerToMemberTypes.size(); ++i) { return pointerToMemberTypes.intern(PointerToMemberType(memberName, elementType));
PointerToMemberType *ty = pointerToMemberTypes.at(i);
if (ty->elementType().match(elementType, &matcher) && matcher.isEqualTo(ty->memberName(), memberName))
return ty;
}
PointerToMemberType *ty = new PointerToMemberType(memberName, elementType);
pointerToMemberTypes.push_back(ty);
return ty;
} }
PointerType *findOrInsertPointerType(const FullySpecifiedType &elementType) PointerType *findOrInsertPointerType(const FullySpecifiedType &elementType)
{ {
for (unsigned i = 0; i < pointerTypes.size(); ++i) { return pointerTypes.intern(PointerType(elementType));
PointerType *ty = pointerTypes.at(i);
if (ty->elementType().match(elementType, &matcher))
return ty;
}
PointerType *ty = new PointerType(elementType);
pointerTypes.push_back(ty);
return ty;
} }
ReferenceType *findOrInsertReferenceType(const FullySpecifiedType &elementType) ReferenceType *findOrInsertReferenceType(const FullySpecifiedType &elementType)
{ {
for (unsigned i = 0; i < referenceTypes.size(); ++i) { return referenceTypes.intern(ReferenceType(elementType));
ReferenceType *ty = referenceTypes.at(i);
if (ty->elementType().match(elementType, &matcher))
return ty;
}
ReferenceType *ty = new ReferenceType(elementType);
referenceTypes.push_back(ty);
return ty;
} }
ArrayType *findOrInsertArrayType(const FullySpecifiedType &elementType, unsigned size) ArrayType *findOrInsertArrayType(const FullySpecifiedType &elementType, unsigned size)
{ {
for (unsigned i = 0; i < arrayTypes.size(); ++i) { return arrayTypes.intern(ArrayType(elementType, size));
ArrayType *ty = arrayTypes.at(i);
if (ty->size() == size && ty->elementType().match(elementType, &matcher))
return ty;
}
ArrayType *ty = new ArrayType(elementType, size);
arrayTypes.push_back(ty);
return ty;
} }
NamedType *findOrInsertNamedType(Name *name) NamedType *findOrInsertNamedType(Name *name)
{ {
for (unsigned i = 0; i < namedTypes.size(); ++i) { return namedTypes.intern(NamedType(name));
NamedType *ty = namedTypes.at(i);
if (matcher.isEqualTo(ty->name(), name))
return ty;
}
NamedType *ty = new NamedType(name);
namedTypes.push_back(ty);
return ty;
} }
Declaration *newDeclaration(unsigned sourceLocation, Name *name) Declaration *newDeclaration(unsigned sourceLocation, Name *name)
@@ -519,13 +532,13 @@ public:
// types // types
VoidType voidType; VoidType voidType;
std::vector<IntegerType *> integerTypes; Table<IntegerType> integerTypes;
std::vector<FloatType *> floatTypes; Table<FloatType> floatTypes;
std::vector<PointerToMemberType *> pointerToMemberTypes; Table<PointerToMemberType> pointerToMemberTypes;
std::vector<PointerType *> pointerTypes; Table<PointerType> pointerTypes;
std::vector<ReferenceType *> referenceTypes; Table<ReferenceType> referenceTypes;
std::vector<ArrayType *> arrayTypes; Table<ArrayType> arrayTypes;
std::vector<NamedType *> namedTypes; Table<NamedType> namedTypes;
// symbols // symbols
std::vector<Symbol *> symbols; std::vector<Symbol *> symbols;

View File

@@ -55,9 +55,6 @@ namespace CPlusPlus {
class CPLUSPLUS_EXPORT Type class CPLUSPLUS_EXPORT Type
{ {
Type(const Type &other);
void operator =(const Type &other);
public: public:
Type(); Type();
virtual ~Type(); virtual ~Type();