Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame^] | 1 | //===-- BitcodeWriter.h - ClangDoc Bitcode Writer --------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements a writer for serializing the clang-doc internal |
| 11 | // representation to LLVM bitcode. The writer takes in a stream and emits the |
| 12 | // generated bitcode to that stream. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEWRITER_H |
| 17 | #define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEWRITER_H |
| 18 | |
| 19 | #include "Representation.h" |
| 20 | #include "clang/AST/AST.h" |
| 21 | #include "llvm/ADT/DenseMap.h" |
| 22 | #include "llvm/ADT/SmallVector.h" |
| 23 | #include "llvm/ADT/StringRef.h" |
| 24 | #include "llvm/Bitcode/BitstreamWriter.h" |
| 25 | #include <initializer_list> |
| 26 | #include <vector> |
| 27 | |
| 28 | namespace clang { |
| 29 | namespace doc { |
| 30 | |
| 31 | // Current version number of clang-doc bitcode. |
| 32 | // Should be bumped when removing or changing BlockIds, RecordIds, or |
| 33 | // BitCodeConstants, though they can be added without breaking it. |
| 34 | static const unsigned VersionNumber = 1; |
| 35 | |
| 36 | struct BitCodeConstants { |
| 37 | static constexpr unsigned RecordSize = 16U; |
| 38 | static constexpr unsigned SignatureBitSize = 8U; |
| 39 | static constexpr unsigned SubblockIDSize = 4U; |
| 40 | static constexpr unsigned BoolSize = 1U; |
| 41 | static constexpr unsigned IntSize = 16U; |
| 42 | static constexpr unsigned StringLengthSize = 16U; |
| 43 | static constexpr unsigned FilenameLengthSize = 16U; |
| 44 | static constexpr unsigned LineNumberSize = 16U; |
| 45 | static constexpr unsigned ReferenceTypeSize = 8U; |
| 46 | static constexpr unsigned USRLengthSize = 6U; |
| 47 | static constexpr unsigned USRBitLengthSize = 8U; |
| 48 | }; |
| 49 | |
| 50 | // New Ids need to be added to both the enum here and the relevant IdNameMap in |
| 51 | // the implementation file. |
| 52 | enum BlockId { |
| 53 | BI_VERSION_BLOCK_ID = llvm::bitc::FIRST_APPLICATION_BLOCKID, |
| 54 | BI_NAMESPACE_BLOCK_ID, |
| 55 | BI_ENUM_BLOCK_ID, |
| 56 | BI_TYPE_BLOCK_ID, |
| 57 | BI_FIELD_TYPE_BLOCK_ID, |
| 58 | BI_MEMBER_TYPE_BLOCK_ID, |
| 59 | BI_RECORD_BLOCK_ID, |
| 60 | BI_FUNCTION_BLOCK_ID, |
| 61 | BI_COMMENT_BLOCK_ID, |
| 62 | BI_FIRST = BI_VERSION_BLOCK_ID, |
| 63 | BI_LAST = BI_COMMENT_BLOCK_ID |
| 64 | }; |
| 65 | |
| 66 | // New Ids need to be added to the enum here, and to the relevant IdNameMap and |
| 67 | // initialization list in the implementation file. |
| 68 | #define INFORECORDS(X) X##_USR, X##_NAME, X##_NAMESPACE |
| 69 | |
| 70 | enum RecordId { |
| 71 | VERSION = 1, |
| 72 | INFORECORDS(FUNCTION), |
| 73 | FUNCTION_DEFLOCATION, |
| 74 | FUNCTION_LOCATION, |
| 75 | FUNCTION_PARENT, |
| 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, |
| 89 | TYPE_REF, |
| 90 | FIELD_TYPE_REF, |
| 91 | FIELD_TYPE_NAME, |
| 92 | MEMBER_TYPE_REF, |
| 93 | MEMBER_TYPE_NAME, |
| 94 | MEMBER_TYPE_ACCESS, |
| 95 | INFORECORDS(NAMESPACE), |
| 96 | INFORECORDS(ENUM), |
| 97 | ENUM_DEFLOCATION, |
| 98 | ENUM_LOCATION, |
| 99 | ENUM_MEMBER, |
| 100 | ENUM_SCOPED, |
| 101 | INFORECORDS(RECORD), |
| 102 | RECORD_DEFLOCATION, |
| 103 | RECORD_LOCATION, |
| 104 | RECORD_TAG_TYPE, |
| 105 | RECORD_PARENT, |
| 106 | RECORD_VPARENT, |
| 107 | RI_FIRST = VERSION, |
| 108 | RI_LAST = RECORD_VPARENT |
| 109 | }; |
| 110 | |
| 111 | static constexpr unsigned BlockIdCount = BI_LAST - BI_FIRST + 1; |
| 112 | static constexpr unsigned RecordIdCount = RI_LAST - RI_FIRST + 1; |
| 113 | |
| 114 | #undef INFORECORDS |
| 115 | |
| 116 | class ClangDocBitcodeWriter { |
| 117 | public: |
| 118 | ClangDocBitcodeWriter(llvm::BitstreamWriter &Stream) : Stream(Stream) { |
| 119 | emitHeader(); |
| 120 | emitBlockInfoBlock(); |
| 121 | emitVersionBlock(); |
| 122 | } |
| 123 | |
| 124 | #ifndef NDEBUG // Don't want explicit dtor unless needed. |
| 125 | ~ClangDocBitcodeWriter() { |
| 126 | // Check that the static size is large-enough. |
| 127 | assert(Record.capacity() > BitCodeConstants::RecordSize); |
| 128 | } |
| 129 | #endif |
| 130 | |
| 131 | // Block emission of different info types. |
| 132 | void emitBlock(const NamespaceInfo &I); |
| 133 | void emitBlock(const RecordInfo &I); |
| 134 | void emitBlock(const FunctionInfo &I); |
| 135 | void emitBlock(const EnumInfo &I); |
| 136 | void emitBlock(const TypeInfo &B); |
| 137 | void emitBlock(const FieldTypeInfo &B); |
| 138 | void emitBlock(const MemberTypeInfo &B); |
| 139 | void emitBlock(const CommentInfo &B); |
| 140 | |
| 141 | private: |
| 142 | class AbbreviationMap { |
| 143 | llvm::DenseMap<unsigned, unsigned> Abbrevs; |
| 144 | |
| 145 | public: |
| 146 | AbbreviationMap() : Abbrevs(RecordIdCount) {} |
| 147 | |
| 148 | void add(RecordId RID, unsigned AbbrevID); |
| 149 | unsigned get(RecordId RID) const; |
| 150 | }; |
| 151 | |
| 152 | class StreamSubBlockGuard { |
| 153 | llvm::BitstreamWriter &Stream; |
| 154 | |
| 155 | public: |
| 156 | StreamSubBlockGuard(llvm::BitstreamWriter &Stream_, BlockId ID) |
| 157 | : Stream(Stream_) { |
| 158 | // NOTE: SubBlockIDSize could theoretically be calculated on the fly, |
| 159 | // based on the initialization list of records in each block. |
| 160 | Stream.EnterSubblock(ID, BitCodeConstants::SubblockIDSize); |
| 161 | } |
| 162 | |
| 163 | StreamSubBlockGuard() = default; |
| 164 | StreamSubBlockGuard(const StreamSubBlockGuard &) = delete; |
| 165 | StreamSubBlockGuard &operator=(const StreamSubBlockGuard &) = delete; |
| 166 | |
| 167 | ~StreamSubBlockGuard() { Stream.ExitBlock(); } |
| 168 | }; |
| 169 | |
| 170 | // Emission of validation and overview blocks. |
| 171 | void emitHeader(); |
| 172 | void emitVersionBlock(); |
| 173 | void emitRecordID(RecordId ID); |
| 174 | void emitBlockID(BlockId ID); |
| 175 | void emitBlockInfoBlock(); |
| 176 | void emitBlockInfo(BlockId BID, const std::vector<RecordId> &RIDs); |
| 177 | |
| 178 | // Emission of individual record types. |
| 179 | void emitRecord(StringRef Str, RecordId ID); |
| 180 | void emitRecord(const SymbolID &Str, RecordId ID); |
| 181 | void emitRecord(const Location &Loc, RecordId ID); |
| 182 | void emitRecord(const Reference &Ref, RecordId ID); |
| 183 | void emitRecord(bool Value, RecordId ID); |
| 184 | void emitRecord(int Value, RecordId ID); |
| 185 | void emitRecord(unsigned Value, RecordId ID); |
| 186 | bool prepRecordData(RecordId ID, bool ShouldEmit = true); |
| 187 | |
| 188 | // Emission of appropriate abbreviation type. |
| 189 | void emitAbbrev(RecordId ID, BlockId Block); |
| 190 | |
| 191 | // Static size is the maximum length of the block/record names we're pushing |
| 192 | // to this + 1. Longest is currently `MemberTypeBlock` at 15 chars. |
| 193 | SmallVector<uint32_t, BitCodeConstants::RecordSize> Record; |
| 194 | llvm::BitstreamWriter &Stream; |
| 195 | AbbreviationMap Abbrevs; |
| 196 | }; |
| 197 | |
| 198 | } // namespace doc |
| 199 | } // namespace clang |
| 200 | |
| 201 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEWRITER_H |