Skip to content

Commit

Permalink
Draft
Browse files Browse the repository at this point in the history
  • Loading branch information
ChuanqiXu9 committed Jun 18, 2024
1 parent e4b130f commit 9c1755c
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 171 deletions.
3 changes: 3 additions & 0 deletions clang/include/clang/Serialization/ASTBitCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ using unaligned_decl_id_t =
serialization::DeclID, llvm::endianness::native,
llvm::support::unaligned>;

/// The number of slots needed to record a DeclID in bitstreams.
const unsigned int DeclIDRefSize = 2;

/// The number of predefined preprocessed entity IDs.
const unsigned int NUM_PREDEF_PP_ENTITY_IDS = 1;

Expand Down
10 changes: 5 additions & 5 deletions clang/include/clang/Serialization/ASTReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ class ASTReader

/// An array of lexical contents of a declaration context, as a sequence of
/// Decl::Kind, DeclID pairs.
using LexicalContents = ArrayRef<serialization::unaligned_decl_id_t>;
using LexicalContents = ArrayRef<uint32_t>;

/// Map from a DeclContext to its lexical contents.
llvm::DenseMap<const DeclContext*, std::pair<ModuleFile*, LexicalContents>>
Expand Down Expand Up @@ -960,7 +960,7 @@ class ASTReader
SmallVector<uint64_t, 8> DelayedDeleteExprs;

// A list of late parsed template function data with their module files.
SmallVector<std::pair<ModuleFile *, SmallVector<uint64_t, 1>>, 4>
SmallVector<std::pair<ModuleFile *, RecordData>, 4>
LateParsedTemplates;

/// The IDs of all decls to be checked for deferred diags.
Expand Down Expand Up @@ -1955,12 +1955,12 @@ class ASTReader
/// given module.
///
/// \returns The declaration ID read from the record, adjusted to a global ID.
GlobalDeclID ReadDeclID(ModuleFile &F, const RecordData &Record,
GlobalDeclID ReadDeclID(ModuleFile &F, const RecordDataImpl &Record,
unsigned &Idx);

/// Reads a declaration from the given position in a record in the
/// given module.
Decl *ReadDecl(ModuleFile &F, const RecordData &R, unsigned &I) {
Decl *ReadDecl(ModuleFile &F, const RecordDataImpl &R, unsigned &I) {
return GetDecl(ReadDeclID(F, R, I));
}

Expand All @@ -1970,7 +1970,7 @@ class ASTReader
/// \returns The declaration read from this location, casted to the given
/// result type.
template<typename T>
T *ReadDeclAs(ModuleFile &F, const RecordData &R, unsigned &I) {
T *ReadDeclAs(ModuleFile &F, const RecordDataImpl &R, unsigned &I) {
return cast_or_null<T>(GetDecl(ReadDeclID(F, R, I)));
}

Expand Down
15 changes: 15 additions & 0 deletions clang/include/clang/Serialization/ASTRecordReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,27 @@ class ASTRecordReader
/// Reads a declaration from the given position in a record in the
/// given module, advancing Idx.
Decl *readDecl() {
#ifndef NDEBUG
unsigned OldIdx = Idx;
Decl *D = Reader->ReadDecl(*F, Record, Idx);
assert(Idx - OldIdx == serialization::DeclIDRefSize);
return D;
#endif
return Reader->ReadDecl(*F, Record, Idx);
}
Decl *readDeclRef() {
return readDecl();
}

template <class DeclKind, class Func>
void readDeclArray(Func &&ConsumeFunc) {
unsigned LengthOfArray = readInt();
unsigned End = Idx + LengthOfArray;

while (Idx < End)
ConsumeFunc(readDeclAs<DeclKind>());
}

/// Reads a declaration from the given position in the record,
/// advancing Idx.
///
Expand Down
28 changes: 28 additions & 0 deletions clang/include/clang/Serialization/ASTRecordWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,40 @@ class ASTRecordWriter

/// Emit a reference to a declaration.
void AddDeclRef(const Decl *D) {
#ifndef NDEBUG
unsigned OldSize = size();
Writer->AddDeclRef(D, *Record);
assert(size() - OldSize == serialization::DeclIDRefSize);
return;
#endif
return Writer->AddDeclRef(D, *Record);
}
void writeDeclRef(const Decl *D) {
AddDeclRef(D);
}

void writeNullDeclRef() {
#ifndef NDEBUG
unsigned OldSize = size();
#endif

push_back(0);
push_back(0);

#ifndef NDEBUG
assert(size() - OldSize == serialization::DeclIDRefSize);
#endif
}

template <class DeclKind>
void writeDeclArray(ArrayRef<DeclKind *> Array) {
unsigned ElementNum = Array.size();
push_back(ElementNum * serialization::DeclIDRefSize);

for (DeclKind *D : Array)
AddDeclRef(D);
}

/// Emit a declaration name.
void AddDeclarationName(DeclarationName Name) {
writeDeclarationName(Name);
Expand Down
Loading

0 comments on commit 9c1755c

Please sign in to comment.