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