1 //===-- BitcodeReader.h - ClangDoc Bitcode Reader --------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEREADER_H
16 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEREADER_H
18 #include "BitcodeWriter.h"
19 #include "Representation.h"
20 #include "clang/AST/AST.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/Bitstream/BitstreamReader.h"
23 #include "llvm/Support/Error.h"
29 // Class to read bitstream into an InfoSet collection
30 class ClangDocBitcodeReader
{
32 ClangDocBitcodeReader(llvm::BitstreamCursor
&Stream
) : Stream(Stream
) {}
34 // Main entry point, calls readBlock to read each block in the given stream.
35 llvm::Expected
<std::vector
<std::unique_ptr
<Info
>>> readBitcode();
38 enum class Cursor
{ BadBlock
= 1, Record
, BlockEnd
, BlockBegin
};
41 llvm::Error
validateStream();
42 llvm::Error
readVersion();
43 llvm::Error
readBlockInfoBlock();
45 // Read a block of records into a single Info struct, calls readRecord on each
47 template <typename T
> llvm::Error
readBlock(unsigned ID
, T I
);
49 // Step through a block of records to find the next data field.
50 template <typename T
> llvm::Error
readSubBlock(unsigned ID
, T I
);
52 // Read record data into the given Info data field, calling the appropriate
53 // parseRecord functions to parse and store the data.
54 template <typename T
> llvm::Error
readRecord(unsigned ID
, T I
);
56 // Allocate the relevant type of info and add read data to the object.
58 llvm::Expected
<std::unique_ptr
<Info
>> createInfo(unsigned ID
);
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
);
64 // Helper function to set up the appropriate type of Info.
65 llvm::Expected
<std::unique_ptr
<Info
>> readBlockToInfo(unsigned ID
);
67 llvm::BitstreamCursor
&Stream
;
68 std::optional
<llvm::BitstreamBlockInfo
> BlockInfo
;
69 FieldId CurrentReferenceField
;
75 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEREADER_H