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.
159};
160
161static constexpr unsigned BlockIdCount = BI_LAST - BI_FIRST;
162static constexpr unsigned RecordIdCount = RI_LAST - RI_FIRST;
163
164// Identifiers for differentiating between subblocks
176
178public:
179 ClangDocBitcodeWriter(llvm::BitstreamWriter &Stream, DiagnosticsEngine &Diags)
180 : Stream(Stream), Diags(Diags) {
181 emitHeader();
182 emitBlockInfoBlock();
183 emitVersionBlock();
184 }
185
186 // Write a specific info to a bitcode stream.
187 bool dispatchInfoForWrite(Info *I);
188
189 // Block emission of different info types.
190 void emitBlock(const NamespaceInfo &I);
191 void emitBlock(const RecordInfo &I);
192 void emitBlock(const BaseRecordInfo &I);
193 void emitBlock(const FunctionInfo &I);
194 void emitBlock(const EnumInfo &I);
195 void emitBlock(const EnumValueInfo &I);
196 void emitBlock(const TypeInfo &B);
197 void emitBlock(const TypedefInfo &B);
198 void emitBlock(const FieldTypeInfo &B);
199 void emitBlock(const MemberTypeInfo &T);
200 void emitBlock(const CommentInfo &B);
201 void emitBlock(const TemplateInfo &T);
203 void emitBlock(const TemplateParamInfo &T);
204 void emitBlock(const ConceptInfo &T);
205 void emitBlock(const ConstraintInfo &T);
206 void emitBlock(const Reference &B, FieldId F);
207 void emitBlock(const FriendInfo &R);
208 void emitBlock(const VarInfo &B);
209
210private:
211 class AbbreviationMap {
212 llvm::DenseMap<unsigned, unsigned> Abbrevs;
213
214 public:
215 AbbreviationMap() : Abbrevs(RecordIdCount) {}
216
217 void add(RecordId RID, unsigned AbbrevID);
218 unsigned get(RecordId RID) const;
219 };
220
221 class StreamSubBlockGuard {
222 llvm::BitstreamWriter &Stream;
223
224 public:
225 StreamSubBlockGuard(llvm::BitstreamWriter &Stream_, BlockId ID)
226 : Stream(Stream_) {
227 // NOTE: SubBlockIDSize could theoretically be calculated on the fly,
228 // based on the initialization list of records in each block.
229 Stream.EnterSubblock(ID, BitCodeConstants::SubblockIDSize);
230 }
231
232 StreamSubBlockGuard(const StreamSubBlockGuard &) = delete;
233 StreamSubBlockGuard &operator=(const StreamSubBlockGuard &) = delete;
234
235 ~StreamSubBlockGuard() { Stream.ExitBlock(); }
236 };
237
238 // Emission of validation and overview blocks.
239 void emitHeader();
240 void emitVersionBlock();
241 void emitRecordID(RecordId ID);
242 void emitBlockID(BlockId ID);
243 void emitBlockInfoBlock();
244 void emitBlockInfo(BlockId BID, const std::vector<RecordId> &RIDs);
245
246 // Emission of individual record types.
247 void emitRecord(StringRef Str, RecordId ID);
248 void emitRecord(const SymbolID &Str, RecordId ID);
249 void emitRecord(const Location &Loc, RecordId ID);
250 void emitRecord(const Reference &Ref, RecordId ID);
251 void emitRecord(bool Value, RecordId ID);
252 void emitRecord(int Value, RecordId ID);
253 void emitRecord(unsigned Value, RecordId ID);
254 void emitRecord(const TemplateInfo &Templ);
255 bool prepRecordData(RecordId ID, bool ShouldEmit = true);
256
257 // Emission of appropriate abbreviation type.
258 void emitAbbrev(RecordId ID, BlockId Block);
259
260 // Static size is the maximum length of the block/record names we're pushing
261 // to this + 1. Longest is currently `MemberTypeBlock` at 15 chars.
262 SmallVector<uint32_t, BitCodeConstants::RecordSize> Record;
263 llvm::BitstreamWriter &Stream;
264 AbbreviationMap Abbrevs;
265 DiagnosticsEngine &Diags;
266};
267
268} // namespace doc
269} // namespace clang
270
271#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.