clang-tools 22.0.0git
BitcodeWriter.h
Go to the documentation of this file.
1//===-- BitcodeWriter.h - ClangDoc Bitcode Writer --------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements a writer for serializing the clang-doc internal
10// representation to LLVM bitcode. The writer takes in a stream and emits the
11// generated bitcode to that stream.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEWRITER_H
16#define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEWRITER_H
17
18#include "Representation.h"
19#include "clang/Basic/Diagnostic.h"
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/Bitstream/BitstreamWriter.h"
22#include <vector>
23
24namespace clang {
25namespace doc {
26
27// Current version number of clang-doc bitcode.
28// Should be bumped when removing or changing BlockIds, RecordIds, or
29// BitCodeConstants, though they can be added without breaking it.
30static const unsigned VersionNumber = 3;
31
33 static constexpr unsigned RecordSize = 32U;
34 static constexpr unsigned SignatureBitSize = 8U;
35 static constexpr unsigned SubblockIDSize = 4U;
36 static constexpr unsigned BoolSize = 1U;
37 static constexpr unsigned IntSize = 16U;
38 static constexpr unsigned StringLengthSize = 16U;
39 static constexpr unsigned FilenameLengthSize = 16U;
40 static constexpr unsigned LineNumberSize = 32U;
41 static constexpr unsigned ReferenceTypeSize = 8U;
42 static constexpr unsigned USRLengthSize = 6U;
43 static constexpr unsigned USRBitLengthSize = 8U;
44 static constexpr unsigned char Signature[4] = {'D', 'O', 'C', 'S'};
45 static constexpr int USRHashSize = 20;
46};
47
48// New Ids need to be added to both the enum here and the relevant IdNameMap in
49// the implementation file.
74
75// New Ids need to be added to the enum here, and to the relevant IdNameMap and
76// initialization list in the implementation file.
162};
163
164static constexpr unsigned BlockIdCount = BI_LAST - BI_FIRST;
165static constexpr unsigned RecordIdCount = RI_LAST - RI_FIRST;
166
167// Identifiers for differentiating between subblocks
179
181public:
182 ClangDocBitcodeWriter(llvm::BitstreamWriter &Stream, DiagnosticsEngine &Diags)
183 : Stream(Stream), Diags(Diags) {
184 emitHeader();
185 emitBlockInfoBlock();
186 emitVersionBlock();
187 }
188
189 // Write a specific info to a bitcode stream.
190 bool dispatchInfoForWrite(Info *I);
191
192 // Block emission of different info types.
193 void emitBlock(const NamespaceInfo &I);
194 void emitBlock(const RecordInfo &I);
195 void emitBlock(const BaseRecordInfo &I);
196 void emitBlock(const FunctionInfo &I);
197 void emitBlock(const EnumInfo &I);
198 void emitBlock(const EnumValueInfo &I);
199 void emitBlock(const TypeInfo &B);
200 void emitBlock(const TypedefInfo &B);
201 void emitBlock(const FieldTypeInfo &B);
202 void emitBlock(const MemberTypeInfo &T);
203 void emitBlock(const CommentInfo &B);
204 void emitBlock(const TemplateInfo &T);
206 void emitBlock(const TemplateParamInfo &T);
207 void emitBlock(const ConceptInfo &T);
208 void emitBlock(const ConstraintInfo &T);
209 void emitBlock(const Reference &B, FieldId F);
210 void emitBlock(const FriendInfo &R);
211 void emitBlock(const VarInfo &B);
212
213private:
214 class AbbreviationMap {
215 llvm::DenseMap<unsigned, unsigned> Abbrevs;
216
217 public:
218 AbbreviationMap() : Abbrevs(RecordIdCount) {}
219
220 void add(RecordId RID, unsigned AbbrevID);
221 unsigned get(RecordId RID) const;
222 };
223
224 class StreamSubBlockGuard {
225 llvm::BitstreamWriter &Stream;
226
227 public:
228 StreamSubBlockGuard(llvm::BitstreamWriter &Stream_, BlockId ID)
229 : Stream(Stream_) {
230 // NOTE: SubBlockIDSize could theoretically be calculated on the fly,
231 // based on the initialization list of records in each block.
232 Stream.EnterSubblock(ID, BitCodeConstants::SubblockIDSize);
233 }
234
235 StreamSubBlockGuard(const StreamSubBlockGuard &) = delete;
236 StreamSubBlockGuard &operator=(const StreamSubBlockGuard &) = delete;
237
238 ~StreamSubBlockGuard() { Stream.ExitBlock(); }
239 };
240
241 // Emission of validation and overview blocks.
242 void emitHeader();
243 void emitVersionBlock();
244 void emitRecordID(RecordId ID);
245 void emitBlockID(BlockId ID);
246 void emitBlockInfoBlock();
247 void emitBlockInfo(BlockId BID, const std::vector<RecordId> &RIDs);
248
249 // Emission of individual record types.
250 void emitRecord(StringRef Str, RecordId ID);
251 void emitRecord(const SymbolID &Str, RecordId ID);
252 void emitRecord(const Location &Loc, RecordId ID);
253 void emitRecord(const Reference &Ref, RecordId ID);
254 void emitRecord(bool Value, RecordId ID);
255 void emitRecord(int Value, RecordId ID);
256 void emitRecord(unsigned Value, RecordId ID);
257 void emitRecord(const TemplateInfo &Templ);
258 bool prepRecordData(RecordId ID, bool ShouldEmit = true);
259
260 // Emission of appropriate abbreviation type.
261 void emitAbbrev(RecordId ID, BlockId Block);
262
263 // Static size is the maximum length of the block/record names we're pushing
264 // to this + 1. Longest is currently `MemberTypeBlock` at 15 chars.
265 SmallVector<uint32_t, BitCodeConstants::RecordSize> Record;
266 llvm::BitstreamWriter &Stream;
267 AbbreviationMap Abbrevs;
268 DiagnosticsEngine &Diags;
269};
270
271} // namespace doc
272} // namespace clang
273
274#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEWRITER_H
void emitBlock(const NamespaceInfo &I)
ClangDocBitcodeWriter(llvm::BitstreamWriter &Stream, DiagnosticsEngine &Diags)
@ TEMPLATE_SPECIALIZATION_OF
@ MEMBER_TYPE_IS_TEMPLATE
@ TEMPLATE_PARAM_CONTENTS
@ CONCEPT_CONSTRAINT_EXPRESSION
static constexpr unsigned BlockIdCount
static const unsigned VersionNumber
static constexpr unsigned RecordIdCount
std::array< uint8_t, 20 > SymbolID
@ BI_CONSTRAINT_BLOCK_ID
@ BI_ENUM_VALUE_BLOCK_ID
@ BI_TEMPLATE_SPECIALIZATION_BLOCK_ID
@ BI_NAMESPACE_BLOCK_ID
@ BI_TEMPLATE_PARAM_BLOCK_ID
@ BI_MEMBER_TYPE_BLOCK_ID
@ BI_BASE_RECORD_BLOCK_ID
@ BI_FIELD_TYPE_BLOCK_ID
@ BI_REFERENCE_BLOCK_ID
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static constexpr unsigned BoolSize
static constexpr unsigned LineNumberSize
static constexpr unsigned FilenameLengthSize
static constexpr unsigned RecordSize
static constexpr int USRHashSize
static constexpr unsigned StringLengthSize
static constexpr unsigned USRBitLengthSize
static constexpr unsigned char Signature[4]
static constexpr unsigned IntSize
static constexpr unsigned SignatureBitSize
static constexpr unsigned SubblockIDSize
static constexpr unsigned USRLengthSize
static constexpr unsigned ReferenceTypeSize
A base struct for Infos.