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