Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 1 | //===-- BitcodeReader.h - ClangDoc Bitcode Reader --------------*- 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 | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements a reader for parsing the clang-doc internal |
| 10 | // representation from LLVM bitcode. The reader takes in a stream of bits and |
| 11 | // generates the set of infos that it represents. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEREADER_H |
| 16 | #define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEREADER_H |
| 17 | |
| 18 | #include "BitcodeWriter.h" |
| 19 | #include "Representation.h" |
| 20 | #include "clang/AST/AST.h" |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallVector.h" |
Francis Visoiu Mistrih | e030827 | 2019-07-03 22:40:07 +0000 | [diff] [blame] | 22 | #include "llvm/Bitstream/BitstreamReader.h" |
Julie Hockett | 46fc959 | 2018-08-15 16:02:28 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Error.h" |
Krzysztof Parzyszek | 49e75eb | 2022-12-07 10:04:25 -0800 | [diff] [blame] | 24 | #include <optional> |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 25 | |
| 26 | namespace clang { |
| 27 | namespace doc { |
| 28 | |
| 29 | // Class to read bitstream into an InfoSet collection |
| 30 | class ClangDocBitcodeReader { |
| 31 | public: |
| 32 | ClangDocBitcodeReader(llvm::BitstreamCursor &Stream) : Stream(Stream) {} |
| 33 | |
| 34 | // Main entry point, calls readBlock to read each block in the given stream. |
Julie Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 35 | llvm::Expected<std::vector<std::unique_ptr<Info>>> readBitcode(); |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 36 | |
| 37 | private: |
| 38 | enum class Cursor { BadBlock = 1, Record, BlockEnd, BlockBegin }; |
| 39 | |
| 40 | // Top level parsing |
Julie Hockett | 46fc959 | 2018-08-15 16:02:28 +0000 | [diff] [blame] | 41 | llvm::Error validateStream(); |
| 42 | llvm::Error readVersion(); |
| 43 | llvm::Error readBlockInfoBlock(); |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 44 | |
| 45 | // Read a block of records into a single Info struct, calls readRecord on each |
| 46 | // record found. |
Julie Hockett | 46fc959 | 2018-08-15 16:02:28 +0000 | [diff] [blame] | 47 | template <typename T> llvm::Error readBlock(unsigned ID, T I); |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 48 | |
| 49 | // Step through a block of records to find the next data field. |
Julie Hockett | 46fc959 | 2018-08-15 16:02:28 +0000 | [diff] [blame] | 50 | template <typename T> llvm::Error readSubBlock(unsigned ID, T I); |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 51 | |
| 52 | // Read record data into the given Info data field, calling the appropriate |
| 53 | // parseRecord functions to parse and store the data. |
Julie Hockett | 46fc959 | 2018-08-15 16:02:28 +0000 | [diff] [blame] | 54 | template <typename T> llvm::Error readRecord(unsigned ID, T I); |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 55 | |
| 56 | // Allocate the relevant type of info and add read data to the object. |
Julie Hockett | 46fc959 | 2018-08-15 16:02:28 +0000 | [diff] [blame] | 57 | template <typename T> |
| 58 | llvm::Expected<std::unique_ptr<Info>> createInfo(unsigned ID); |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 59 | |
| 60 | // Helper function to step through blocks to find and dispatch the next record |
| 61 | // or block to be read. |
| 62 | Cursor skipUntilRecordOrBlock(unsigned &BlockOrRecordID); |
| 63 | |
Kazuaki Ishizaki | b7ecf1c | 2020-01-04 10:28:41 -0500 | [diff] [blame] | 64 | // Helper function to set up the appropriate type of Info. |
Julie Hockett | 46fc959 | 2018-08-15 16:02:28 +0000 | [diff] [blame] | 65 | llvm::Expected<std::unique_ptr<Info>> readBlockToInfo(unsigned ID); |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 66 | |
| 67 | llvm::BitstreamCursor &Stream; |
Krzysztof Parzyszek | 49e75eb | 2022-12-07 10:04:25 -0800 | [diff] [blame] | 68 | std::optional<llvm::BitstreamBlockInfo> BlockInfo; |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 69 | FieldId CurrentReferenceField; |
| 70 | }; |
| 71 | |
| 72 | } // namespace doc |
| 73 | } // namespace clang |
| 74 | |
| 75 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEREADER_H |