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 "llvm/ADT/DenseMap.h"
20#include "llvm/Bitstream/BitstreamWriter.h"
21#include <vector>
22
23namespace clang {
24namespace doc {
25
26// Current version number of clang-doc bitcode.
27// Should be bumped when removing or changing BlockIds, RecordIds, or
28// BitCodeConstants, though they can be added without breaking it.
29static const unsigned VersionNumber = 3;
30
32 static constexpr unsigned RecordSize = 32U;
33 static constexpr unsigned SignatureBitSize = 8U;
34 static constexpr unsigned SubblockIDSize = 4U;
35 static constexpr unsigned BoolSize = 1U;
36 static constexpr unsigned IntSize = 16U;
37 static constexpr unsigned StringLengthSize = 16U;
38 static constexpr unsigned FilenameLengthSize = 16U;
39 static constexpr unsigned LineNumberSize = 32U;
40 static constexpr unsigned ReferenceTypeSize = 8U;
41 static constexpr unsigned USRLengthSize = 6U;
42 static constexpr unsigned USRBitLengthSize = 8U;
43 static constexpr unsigned char Signature[4] = {'D', 'O', 'C', 'S'};
44 static constexpr int USRHashSize = 20;
45};
46
47// New Ids need to be added to both the enum here and the relevant IdNameMap in
48// the implementation file.
73
74// New Ids need to be added to the enum here, and to the relevant IdNameMap and
75// initialization list in the implementation file.
158};
159
160static constexpr unsigned BlockIdCount = BI_LAST - BI_FIRST;
161static constexpr unsigned RecordIdCount = RI_LAST - RI_FIRST;
162
163// Identifiers for differentiating between subblocks
175
177public:
178 ClangDocBitcodeWriter(llvm::BitstreamWriter &Stream) : Stream(Stream) {
179 emitHeader();
180 emitBlockInfoBlock();
181 emitVersionBlock();
182 }
183
184 // Write a specific info to a bitcode stream.
185 bool dispatchInfoForWrite(Info *I);
186
187 // Block emission of different info types.
188 void emitBlock(const NamespaceInfo &I);
189 void emitBlock(const RecordInfo &I);
190 void emitBlock(const BaseRecordInfo &I);
191 void emitBlock(const FunctionInfo &I);
192 void emitBlock(const EnumInfo &I);
193 void emitBlock(const EnumValueInfo &I);
194 void emitBlock(const TypeInfo &B);
195 void emitBlock(const TypedefInfo &B);
196 void emitBlock(const FieldTypeInfo &B);
197 void emitBlock(const MemberTypeInfo &T);
198 void emitBlock(const CommentInfo &B);
199 void emitBlock(const TemplateInfo &T);
201 void emitBlock(const TemplateParamInfo &T);
202 void emitBlock(const ConceptInfo &T);
203 void emitBlock(const ConstraintInfo &T);
204 void emitBlock(const Reference &B, FieldId F);
205 void emitBlock(const FriendInfo &R);
206 void emitBlock(const VarInfo &B);
207
208private:
209 class AbbreviationMap {
210 llvm::DenseMap<unsigned, unsigned> Abbrevs;
211
212 public:
213 AbbreviationMap() : Abbrevs(RecordIdCount) {}
214
215 void add(RecordId RID, unsigned AbbrevID);
216 unsigned get(RecordId RID) const;
217 };
218
219 class StreamSubBlockGuard {
220 llvm::BitstreamWriter &Stream;
221
222 public:
223 StreamSubBlockGuard(llvm::BitstreamWriter &Stream_, BlockId ID)
224 : Stream(Stream_) {
225 // NOTE: SubBlockIDSize could theoretically be calculated on the fly,
226 // based on the initialization list of records in each block.
227 Stream.EnterSubblock(ID, BitCodeConstants::SubblockIDSize);
228 }
229
230 StreamSubBlockGuard(const StreamSubBlockGuard &) = delete;
231 StreamSubBlockGuard &operator=(const StreamSubBlockGuard &) = delete;
232
233 ~StreamSubBlockGuard() { Stream.ExitBlock(); }
234 };
235
236 // Emission of validation and overview blocks.
237 void emitHeader();
238 void emitVersionBlock();
239 void emitRecordID(RecordId ID);
240 void emitBlockID(BlockId ID);
241 void emitBlockInfoBlock();
242 void emitBlockInfo(BlockId BID, const std::vector<RecordId> &RIDs);
243
244 // Emission of individual record types.
245 void emitRecord(StringRef Str, RecordId ID);
246 void emitRecord(const SymbolID &Str, RecordId ID);
247 void emitRecord(const Location &Loc, RecordId ID);
248 void emitRecord(const Reference &Ref, RecordId ID);
249 void emitRecord(bool Value, RecordId ID);
250 void emitRecord(int Value, RecordId ID);
251 void emitRecord(unsigned Value, RecordId ID);
252 void emitRecord(const TemplateInfo &Templ);
253 bool prepRecordData(RecordId ID, bool ShouldEmit = true);
254
255 // Emission of appropriate abbreviation type.
256 void emitAbbrev(RecordId ID, BlockId Block);
257
258 // Static size is the maximum length of the block/record names we're pushing
259 // to this + 1. Longest is currently `MemberTypeBlock` at 15 chars.
260 SmallVector<uint32_t, BitCodeConstants::RecordSize> Record;
261 llvm::BitstreamWriter &Stream;
262 AbbreviationMap Abbrevs;
263};
264
265} // namespace doc
266} // namespace clang
267
268#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEWRITER_H
void emitBlock(const NamespaceInfo &I)
ClangDocBitcodeWriter(llvm::BitstreamWriter &Stream)
@ 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.