Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 1 | //===-- BitcodeWriter.h - ClangDoc Bitcode Writer --------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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 |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 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/Bitcode/BitstreamWriter.h" |
| 24 | #include <initializer_list> |
| 25 | #include <vector> |
| 26 | |
| 27 | namespace clang { |
| 28 | namespace 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. |
Julie Hockett | b59cd77 | 2018-05-04 17:02:13 +0000 | [diff] [blame] | 33 | static const unsigned VersionNumber = 2; |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 34 | |
| 35 | struct BitCodeConstants { |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 36 | static constexpr unsigned RecordSize = 32U; |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 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 = 16U; |
| 44 | static constexpr unsigned ReferenceTypeSize = 8U; |
| 45 | static constexpr unsigned USRLengthSize = 6U; |
| 46 | static constexpr unsigned USRBitLengthSize = 8U; |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 47 | static constexpr char Signature[4] = {'D', 'O', 'C', 'S'}; |
| 48 | static constexpr int USRHashSize = 20; |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | // New Ids need to be added to both the enum here and the relevant IdNameMap in |
| 52 | // the implementation file. |
| 53 | enum 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, |
| 61 | BI_FUNCTION_BLOCK_ID, |
| 62 | BI_COMMENT_BLOCK_ID, |
Julie Hockett | b59cd77 | 2018-05-04 17:02:13 +0000 | [diff] [blame] | 63 | BI_REFERENCE_BLOCK_ID, |
| 64 | BI_LAST, |
| 65 | BI_FIRST = BI_VERSION_BLOCK_ID |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | // New Ids need to be added to the enum here, and to the relevant IdNameMap and |
| 69 | // initialization list in the implementation file. |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 70 | enum RecordId { |
| 71 | VERSION = 1, |
Julie Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 72 | FUNCTION_USR, |
| 73 | FUNCTION_NAME, |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 74 | FUNCTION_DEFLOCATION, |
| 75 | FUNCTION_LOCATION, |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 76 | FUNCTION_ACCESS, |
| 77 | FUNCTION_IS_METHOD, |
| 78 | COMMENT_KIND, |
| 79 | COMMENT_TEXT, |
| 80 | COMMENT_NAME, |
| 81 | COMMENT_DIRECTION, |
| 82 | COMMENT_PARAMNAME, |
| 83 | COMMENT_CLOSENAME, |
| 84 | COMMENT_SELFCLOSING, |
| 85 | COMMENT_EXPLICIT, |
| 86 | COMMENT_ATTRKEY, |
| 87 | COMMENT_ATTRVAL, |
| 88 | COMMENT_ARG, |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 89 | FIELD_TYPE_NAME, |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 90 | MEMBER_TYPE_NAME, |
| 91 | MEMBER_TYPE_ACCESS, |
Julie Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 92 | NAMESPACE_USR, |
| 93 | NAMESPACE_NAME, |
| 94 | ENUM_USR, |
| 95 | ENUM_NAME, |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 96 | ENUM_DEFLOCATION, |
| 97 | ENUM_LOCATION, |
| 98 | ENUM_MEMBER, |
| 99 | ENUM_SCOPED, |
Julie Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 100 | RECORD_USR, |
| 101 | RECORD_NAME, |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 102 | RECORD_DEFLOCATION, |
| 103 | RECORD_LOCATION, |
| 104 | RECORD_TAG_TYPE, |
Julie Hockett | b59cd77 | 2018-05-04 17:02:13 +0000 | [diff] [blame] | 105 | REFERENCE_USR, |
| 106 | REFERENCE_NAME, |
| 107 | REFERENCE_TYPE, |
| 108 | REFERENCE_FIELD, |
| 109 | RI_LAST, |
| 110 | RI_FIRST = VERSION |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
Julie Hockett | b59cd77 | 2018-05-04 17:02:13 +0000 | [diff] [blame] | 113 | static constexpr unsigned BlockIdCount = BI_LAST - BI_FIRST; |
| 114 | static constexpr unsigned RecordIdCount = RI_LAST - RI_FIRST; |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 115 | |
Julie Hockett | b59cd77 | 2018-05-04 17:02:13 +0000 | [diff] [blame] | 116 | // Identifiers for differentiating between subblocks |
Julie Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 117 | enum class FieldId { |
| 118 | F_default, |
| 119 | F_namespace, |
| 120 | F_parent, |
| 121 | F_vparent, |
| 122 | F_type, |
| 123 | F_child_namespace, |
| 124 | F_child_record |
| 125 | }; |
Julie Hockett | b59cd77 | 2018-05-04 17:02:13 +0000 | [diff] [blame] | 126 | |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 127 | class ClangDocBitcodeWriter { |
| 128 | public: |
| 129 | ClangDocBitcodeWriter(llvm::BitstreamWriter &Stream) : Stream(Stream) { |
| 130 | emitHeader(); |
| 131 | emitBlockInfoBlock(); |
| 132 | emitVersionBlock(); |
| 133 | } |
| 134 | |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 135 | // Write a specific info to a bitcode stream. |
| 136 | bool dispatchInfoForWrite(Info *I); |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 137 | |
| 138 | // Block emission of different info types. |
| 139 | void emitBlock(const NamespaceInfo &I); |
| 140 | void emitBlock(const RecordInfo &I); |
| 141 | void emitBlock(const FunctionInfo &I); |
| 142 | void emitBlock(const EnumInfo &I); |
| 143 | void emitBlock(const TypeInfo &B); |
| 144 | void emitBlock(const FieldTypeInfo &B); |
| 145 | void emitBlock(const MemberTypeInfo &B); |
| 146 | void emitBlock(const CommentInfo &B); |
Julie Hockett | b59cd77 | 2018-05-04 17:02:13 +0000 | [diff] [blame] | 147 | void emitBlock(const Reference &B, FieldId F); |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 148 | |
| 149 | private: |
| 150 | class AbbreviationMap { |
| 151 | llvm::DenseMap<unsigned, unsigned> Abbrevs; |
| 152 | |
| 153 | public: |
| 154 | AbbreviationMap() : Abbrevs(RecordIdCount) {} |
| 155 | |
| 156 | void add(RecordId RID, unsigned AbbrevID); |
| 157 | unsigned get(RecordId RID) const; |
| 158 | }; |
| 159 | |
| 160 | class StreamSubBlockGuard { |
| 161 | llvm::BitstreamWriter &Stream; |
| 162 | |
| 163 | public: |
| 164 | StreamSubBlockGuard(llvm::BitstreamWriter &Stream_, BlockId ID) |
| 165 | : Stream(Stream_) { |
| 166 | // NOTE: SubBlockIDSize could theoretically be calculated on the fly, |
| 167 | // based on the initialization list of records in each block. |
| 168 | Stream.EnterSubblock(ID, BitCodeConstants::SubblockIDSize); |
| 169 | } |
| 170 | |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 171 | StreamSubBlockGuard(const StreamSubBlockGuard &) = delete; |
| 172 | StreamSubBlockGuard &operator=(const StreamSubBlockGuard &) = delete; |
| 173 | |
| 174 | ~StreamSubBlockGuard() { Stream.ExitBlock(); } |
| 175 | }; |
| 176 | |
| 177 | // Emission of validation and overview blocks. |
| 178 | void emitHeader(); |
| 179 | void emitVersionBlock(); |
| 180 | void emitRecordID(RecordId ID); |
| 181 | void emitBlockID(BlockId ID); |
| 182 | void emitBlockInfoBlock(); |
| 183 | void emitBlockInfo(BlockId BID, const std::vector<RecordId> &RIDs); |
| 184 | |
| 185 | // Emission of individual record types. |
| 186 | void emitRecord(StringRef Str, RecordId ID); |
| 187 | void emitRecord(const SymbolID &Str, RecordId ID); |
| 188 | void emitRecord(const Location &Loc, RecordId ID); |
| 189 | void emitRecord(const Reference &Ref, RecordId ID); |
| 190 | void emitRecord(bool Value, RecordId ID); |
| 191 | void emitRecord(int Value, RecordId ID); |
| 192 | void emitRecord(unsigned Value, RecordId ID); |
| 193 | bool prepRecordData(RecordId ID, bool ShouldEmit = true); |
| 194 | |
| 195 | // Emission of appropriate abbreviation type. |
| 196 | void emitAbbrev(RecordId ID, BlockId Block); |
| 197 | |
| 198 | // Static size is the maximum length of the block/record names we're pushing |
| 199 | // to this + 1. Longest is currently `MemberTypeBlock` at 15 chars. |
| 200 | SmallVector<uint32_t, BitCodeConstants::RecordSize> Record; |
| 201 | llvm::BitstreamWriter &Stream; |
| 202 | AbbreviationMap Abbrevs; |
| 203 | }; |
| 204 | |
| 205 | } // namespace doc |
| 206 | } // namespace clang |
| 207 | |
| 208 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEWRITER_H |