serialization::TypeID is used with or without qualifiers, both as index and as index + qualifiers.

Disambiguate and provide some type safety by using a new class TypeIdx for the "TypeID as index" semantics.

llvm-svn: 111630
This commit is contained in:
Argyrios Kyrtzidis 2010-08-20 16:03:59 +00:00
parent 4bd97102e9
commit bb5c7eae4c
6 changed files with 36 additions and 20 deletions

View File

@ -17,6 +17,7 @@
#ifndef LLVM_CLANG_FRONTEND_PCHBITCODES_H
#define LLVM_CLANG_FRONTEND_PCHBITCODES_H
#include "clang/AST/Type.h"
#include "llvm/Bitcode/BitCodes.h"
#include "llvm/System/DataTypes.h"
@ -64,6 +65,22 @@ namespace clang {
/// other types that have serialized representations.
typedef uint32_t TypeID;
/// \brief A type index; the type ID with the qualifier bits removed.
class TypeIdx {
uint32_t Idx;
public:
TypeIdx() : Idx(0) { }
explicit TypeIdx(uint32_t index) : Idx(index) { }
uint32_t getIndex() const { return Idx; }
TypeID asTypeID(unsigned FastQuals) const {
return (Idx << Qualifiers::FastWidth) | FastQuals;
}
static TypeIdx fromTypeID(TypeID ID) {
return TypeIdx(ID >> Qualifiers::FastWidth);
}
};
/// \brief An ID number that refers to an identifier in an AST
/// file.
typedef uint32_t IdentID;

View File

@ -37,7 +37,7 @@ public:
/// \brief A type was deserialized from the AST file. The ID here has the
/// qualifier bits already removed, and T is guaranteed to be locally
/// unqualified.
virtual void TypeRead(serialization::TypeID ID, QualType T) = 0;
virtual void TypeRead(serialization::TypeIdx Idx, QualType T) = 0;
/// \brief A decl was deserialized from the AST file.
virtual void DeclRead(serialization::DeclID ID, const Decl *D) = 0;
/// \brief A selector was read from the AST file.

View File

@ -18,7 +18,6 @@
#include "clang/Sema/ExternalSemaSource.h"
#include "clang/AST/DeclarationName.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/Type.h"
#include "clang/AST/TemplateBase.h"
#include "clang/Lex/ExternalPreprocessorSource.h"
#include "clang/Lex/PreprocessingRecord.h"

View File

@ -146,7 +146,7 @@ private:
/// allow for the const/volatile qualifiers.
///
/// Keys in the map never have const/volatile qualifiers.
llvm::DenseMap<QualType, serialization::TypeID, UnsafeQualTypeDenseMapInfo>
llvm::DenseMap<QualType, serialization::TypeIdx, UnsafeQualTypeDenseMapInfo>
TypeIDs;
/// \brief Offset of each type in the bitstream, indexed by
@ -466,7 +466,7 @@ public:
// ASTDeserializationListener implementation
void SetReader(ASTReader *Reader);
void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II);
void TypeRead(serialization::TypeID ID, QualType T);
void TypeRead(serialization::TypeIdx Idx, QualType T);
void DeclRead(serialization::DeclID ID, const Decl *D);
void SelectorRead(serialization::SelectorID iD, Selector Sel);
};

View File

@ -2826,7 +2826,7 @@ QualType ASTReader::GetType(TypeID ID) {
TypesLoaded[Index] = ReadTypeRecord(Index);
TypesLoaded[Index]->setFromAST();
if (DeserializationListener)
DeserializationListener->TypeRead(ID >> Qualifiers::FastWidth,
DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
TypesLoaded[Index]);
}

View File

@ -1397,12 +1397,12 @@ void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
/// \brief Write the representation of a type to the AST stream.
void ASTWriter::WriteType(QualType T) {
TypeID &ID = TypeIDs[T];
if (ID == 0) // we haven't seen this type before.
ID = NextTypeID++;
TypeIdx &Idx = TypeIDs[T];
if (Idx.getIndex() == 0) // we haven't seen this type before.
Idx = TypeIdx(NextTypeID++);
// Record the offset for this type.
unsigned Index = ID - FirstTypeID;
unsigned Index = Idx.getIndex() - FirstTypeID;
if (TypeOffsets.size() == Index)
TypeOffsets.push_back(Stream.GetCurrentBitNo());
else if (TypeOffsets.size() < Index) {
@ -2588,17 +2588,17 @@ void ASTWriter::AddTypeRef(QualType T, RecordData &Record) {
T.removeFastQualifiers();
if (T.hasLocalNonFastQualifiers()) {
TypeID &ID = TypeIDs[T];
if (ID == 0) {
TypeIdx &Idx = TypeIDs[T];
if (Idx.getIndex() == 0) {
// We haven't seen these qualifiers applied to this type before.
// Assign it a new ID. This is the only time we enqueue a
// qualified type, and it has no CV qualifiers.
ID = NextTypeID++;
Idx = TypeIdx(NextTypeID++);
DeclTypesToEmit.push(T);
}
// Encode the type qualifiers in the type reference.
Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Record.push_back(Idx.asTypeID(FastQuals));
return;
}
@ -2640,20 +2640,20 @@ void ASTWriter::AddTypeRef(QualType T, RecordData &Record) {
break;
}
Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Record.push_back(TypeIdx(ID).asTypeID(FastQuals));
return;
}
TypeID &ID = TypeIDs[T];
if (ID == 0) {
TypeIdx &Idx = TypeIDs[T];
if (Idx.getIndex() == 0) {
// We haven't seen this type before. Assign it a new ID and put it
// into the queue of types to emit.
ID = NextTypeID++;
Idx = TypeIdx(NextTypeID++);
DeclTypesToEmit.push(T);
}
// Encode the type qualifiers in the type reference.
Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Record.push_back(Idx.asTypeID(FastQuals));
}
void ASTWriter::AddDeclRef(const Decl *D, RecordData &Record) {
@ -2920,8 +2920,8 @@ void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
IdentifierIDs[II] = ID;
}
void ASTWriter::TypeRead(TypeID ID, QualType T) {
TypeIDs[T] = ID;
void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
TypeIDs[T] = Idx;
}
void ASTWriter::DeclRead(DeclID ID, const Decl *D) {