blob: 7deda2ed39ee7181f1e6e8dc091081d5ae94d30b [file] [log] [blame]
Julie Hockette975a472018-03-22 23:34:46 +00001//===-- BitcodeWriter.h - ClangDoc Bitcode Writer --------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Julie Hockette975a472018-03-22 23:34:46 +00006//
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"
Francis Visoiu Mistrihe0308272019-07-03 22:40:07 +000023#include "llvm/Bitstream/BitstreamWriter.h"
Julie Hockette975a472018-03-22 23:34:46 +000024#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.
Diego Astiazaranba3d5952019-08-16 00:10:49 +000033static const unsigned VersionNumber = 3;
Julie Hockette975a472018-03-22 23:34:46 +000034
35struct BitCodeConstants {
Julie Hockettd0f9a872018-06-04 17:22:20 +000036 static constexpr unsigned RecordSize = 32U;
Julie Hockette975a472018-03-22 23:34:46 +000037 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;
Julie Hockett245154d2019-08-23 21:14:05 +000043 static constexpr unsigned LineNumberSize = 32U;
Julie Hockette975a472018-03-22 23:34:46 +000044 static constexpr unsigned ReferenceTypeSize = 8U;
45 static constexpr unsigned USRLengthSize = 6U;
46 static constexpr unsigned USRBitLengthSize = 8U;
JF Bastien0e828952019-06-26 19:50:12 +000047 static constexpr unsigned char Signature[4] = {'D', 'O', 'C', 'S'};
Julie Hockettd0f9a872018-06-04 17:22:20 +000048 static constexpr int USRHashSize = 20;
Julie Hockette975a472018-03-22 23:34:46 +000049};
50
51// New Ids need to be added to both the enum here and the relevant IdNameMap in
52// the implementation file.
53enum BlockId {
54 BI_VERSION_BLOCK_ID = llvm::bitc::FIRST_APPLICATION_BLOCKID,
55 BI_NAMESPACE_BLOCK_ID,
56 BI_ENUM_BLOCK_ID,
57 BI_TYPE_BLOCK_ID,
58 BI_FIELD_TYPE_BLOCK_ID,
59 BI_MEMBER_TYPE_BLOCK_ID,
60 BI_RECORD_BLOCK_ID,
Diego Astiazaranba3d5952019-08-16 00:10:49 +000061 BI_BASE_RECORD_BLOCK_ID,
Julie Hockette975a472018-03-22 23:34:46 +000062 BI_FUNCTION_BLOCK_ID,
63 BI_COMMENT_BLOCK_ID,
Julie Hockettb59cd772018-05-04 17:02:13 +000064 BI_REFERENCE_BLOCK_ID,
65 BI_LAST,
66 BI_FIRST = BI_VERSION_BLOCK_ID
Julie Hockette975a472018-03-22 23:34:46 +000067};
68
69// New Ids need to be added to the enum here, and to the relevant IdNameMap and
70// initialization list in the implementation file.
Julie Hockette975a472018-03-22 23:34:46 +000071enum RecordId {
72 VERSION = 1,
Julie Hockett8899c292018-08-02 20:10:17 +000073 FUNCTION_USR,
74 FUNCTION_NAME,
Julie Hockette975a472018-03-22 23:34:46 +000075 FUNCTION_DEFLOCATION,
76 FUNCTION_LOCATION,
Julie Hockette975a472018-03-22 23:34:46 +000077 FUNCTION_ACCESS,
78 FUNCTION_IS_METHOD,
79 COMMENT_KIND,
80 COMMENT_TEXT,
81 COMMENT_NAME,
82 COMMENT_DIRECTION,
83 COMMENT_PARAMNAME,
84 COMMENT_CLOSENAME,
85 COMMENT_SELFCLOSING,
86 COMMENT_EXPLICIT,
87 COMMENT_ATTRKEY,
88 COMMENT_ATTRVAL,
89 COMMENT_ARG,
Julie Hockette975a472018-03-22 23:34:46 +000090 FIELD_TYPE_NAME,
Julie Hockette975a472018-03-22 23:34:46 +000091 MEMBER_TYPE_NAME,
92 MEMBER_TYPE_ACCESS,
Julie Hockett8899c292018-08-02 20:10:17 +000093 NAMESPACE_USR,
94 NAMESPACE_NAME,
Julie Hockett2c1c9a22019-07-12 18:32:00 +000095 NAMESPACE_PATH,
Julie Hockett8899c292018-08-02 20:10:17 +000096 ENUM_USR,
97 ENUM_NAME,
Julie Hockette975a472018-03-22 23:34:46 +000098 ENUM_DEFLOCATION,
99 ENUM_LOCATION,
100 ENUM_MEMBER,
101 ENUM_SCOPED,
Julie Hockett8899c292018-08-02 20:10:17 +0000102 RECORD_USR,
103 RECORD_NAME,
Julie Hockett2c1c9a22019-07-12 18:32:00 +0000104 RECORD_PATH,
Julie Hockette975a472018-03-22 23:34:46 +0000105 RECORD_DEFLOCATION,
106 RECORD_LOCATION,
107 RECORD_TAG_TYPE,
Julie Hockettb1f01e22019-06-24 19:31:02 +0000108 RECORD_IS_TYPE_DEF,
Diego Astiazaranba3d5952019-08-16 00:10:49 +0000109 BASE_RECORD_USR,
110 BASE_RECORD_NAME,
111 BASE_RECORD_PATH,
112 BASE_RECORD_TAG_TYPE,
113 BASE_RECORD_IS_VIRTUAL,
114 BASE_RECORD_ACCESS,
115 BASE_RECORD_IS_PARENT,
Julie Hockettb59cd772018-05-04 17:02:13 +0000116 REFERENCE_USR,
117 REFERENCE_NAME,
118 REFERENCE_TYPE,
Julie Hockett2c1c9a22019-07-12 18:32:00 +0000119 REFERENCE_PATH,
Diego Astiazaran15e468e2019-08-06 00:11:34 +0000120 REFERENCE_IS_IN_GLOBAL_NAMESPACE,
Julie Hockettb59cd772018-05-04 17:02:13 +0000121 REFERENCE_FIELD,
122 RI_LAST,
123 RI_FIRST = VERSION
Julie Hockette975a472018-03-22 23:34:46 +0000124};
125
Julie Hockettb59cd772018-05-04 17:02:13 +0000126static constexpr unsigned BlockIdCount = BI_LAST - BI_FIRST;
127static constexpr unsigned RecordIdCount = RI_LAST - RI_FIRST;
Julie Hockette975a472018-03-22 23:34:46 +0000128
Julie Hockettb59cd772018-05-04 17:02:13 +0000129// Identifiers for differentiating between subblocks
Julie Hockett8899c292018-08-02 20:10:17 +0000130enum class FieldId {
131 F_default,
132 F_namespace,
133 F_parent,
134 F_vparent,
135 F_type,
136 F_child_namespace,
137 F_child_record
138};
Julie Hockettb59cd772018-05-04 17:02:13 +0000139
Julie Hockette975a472018-03-22 23:34:46 +0000140class ClangDocBitcodeWriter {
141public:
142 ClangDocBitcodeWriter(llvm::BitstreamWriter &Stream) : Stream(Stream) {
143 emitHeader();
144 emitBlockInfoBlock();
145 emitVersionBlock();
146 }
147
Julie Hockettd0f9a872018-06-04 17:22:20 +0000148 // Write a specific info to a bitcode stream.
149 bool dispatchInfoForWrite(Info *I);
Julie Hockette975a472018-03-22 23:34:46 +0000150
151 // Block emission of different info types.
152 void emitBlock(const NamespaceInfo &I);
153 void emitBlock(const RecordInfo &I);
Diego Astiazaranba3d5952019-08-16 00:10:49 +0000154 void emitBlock(const BaseRecordInfo &I);
Julie Hockette975a472018-03-22 23:34:46 +0000155 void emitBlock(const FunctionInfo &I);
156 void emitBlock(const EnumInfo &I);
157 void emitBlock(const TypeInfo &B);
158 void emitBlock(const FieldTypeInfo &B);
159 void emitBlock(const MemberTypeInfo &B);
160 void emitBlock(const CommentInfo &B);
Julie Hockettb59cd772018-05-04 17:02:13 +0000161 void emitBlock(const Reference &B, FieldId F);
Julie Hockette975a472018-03-22 23:34:46 +0000162
163private:
164 class AbbreviationMap {
165 llvm::DenseMap<unsigned, unsigned> Abbrevs;
166
167 public:
168 AbbreviationMap() : Abbrevs(RecordIdCount) {}
169
170 void add(RecordId RID, unsigned AbbrevID);
171 unsigned get(RecordId RID) const;
172 };
173
174 class StreamSubBlockGuard {
175 llvm::BitstreamWriter &Stream;
176
177 public:
178 StreamSubBlockGuard(llvm::BitstreamWriter &Stream_, BlockId ID)
179 : Stream(Stream_) {
180 // NOTE: SubBlockIDSize could theoretically be calculated on the fly,
181 // based on the initialization list of records in each block.
182 Stream.EnterSubblock(ID, BitCodeConstants::SubblockIDSize);
183 }
184
Julie Hockette975a472018-03-22 23:34:46 +0000185 StreamSubBlockGuard(const StreamSubBlockGuard &) = delete;
186 StreamSubBlockGuard &operator=(const StreamSubBlockGuard &) = delete;
187
188 ~StreamSubBlockGuard() { Stream.ExitBlock(); }
189 };
190
191 // Emission of validation and overview blocks.
192 void emitHeader();
193 void emitVersionBlock();
194 void emitRecordID(RecordId ID);
195 void emitBlockID(BlockId ID);
196 void emitBlockInfoBlock();
197 void emitBlockInfo(BlockId BID, const std::vector<RecordId> &RIDs);
198
199 // Emission of individual record types.
200 void emitRecord(StringRef Str, RecordId ID);
201 void emitRecord(const SymbolID &Str, RecordId ID);
202 void emitRecord(const Location &Loc, RecordId ID);
203 void emitRecord(const Reference &Ref, RecordId ID);
204 void emitRecord(bool Value, RecordId ID);
205 void emitRecord(int Value, RecordId ID);
206 void emitRecord(unsigned Value, RecordId ID);
207 bool prepRecordData(RecordId ID, bool ShouldEmit = true);
208
209 // Emission of appropriate abbreviation type.
210 void emitAbbrev(RecordId ID, BlockId Block);
211
212 // Static size is the maximum length of the block/record names we're pushing
213 // to this + 1. Longest is currently `MemberTypeBlock` at 15 chars.
214 SmallVector<uint32_t, BitCodeConstants::RecordSize> Record;
215 llvm::BitstreamWriter &Stream;
216 AbbreviationMap Abbrevs;
217};
218
219} // namespace doc
220} // namespace clang
221
222#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEWRITER_H