81 lines · c
1//===-- BitcodeReader.h - ClangDoc Bitcode Reader --------------*- C++ -*-===//2//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-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements a reader for parsing the clang-doc internal10// representation from LLVM bitcode. The reader takes in a stream of bits and11// generates the set of infos that it represents.12//13//===----------------------------------------------------------------------===//14 15#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEREADER_H16#define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEREADER_H17 18#include "BitcodeWriter.h"19#include "Representation.h"20#include "llvm/Bitstream/BitstreamReader.h"21#include "llvm/Support/Error.h"22#include <optional>23 24namespace clang {25namespace doc {26 27// Class to read bitstream into an InfoSet collection28class ClangDocBitcodeReader {29public:30 ClangDocBitcodeReader(llvm::BitstreamCursor &Stream) : Stream(Stream) {}31 32 // Main entry point, calls readBlock to read each block in the given stream.33 llvm::Expected<std::vector<std::unique_ptr<Info>>> readBitcode();34 35private:36 enum class Cursor { BadBlock = 1, Record, BlockEnd, BlockBegin };37 38 // Top level parsing39 llvm::Error validateStream();40 llvm::Error readVersion();41 llvm::Error readBlockInfoBlock();42 43 // Read a block of records into a single Info struct, calls readRecord on each44 // record found.45 template <typename T> llvm::Error readBlock(unsigned ID, T I);46 47 // Step through a block of records to find the next data field.48 template <typename T> llvm::Error readSubBlock(unsigned ID, T I);49 50 // Read record data into the given Info data field, calling the appropriate51 // parseRecord functions to parse and store the data.52 template <typename T> llvm::Error readRecord(unsigned ID, T I);53 54 // Allocate the relevant type of info and add read data to the object.55 template <typename T>56 llvm::Expected<std::unique_ptr<Info>> createInfo(unsigned ID);57 58 // Helper function to step through blocks to find and dispatch the next record59 // or block to be read.60 Cursor skipUntilRecordOrBlock(unsigned &BlockOrRecordID);61 62 // Helper function to set up the appropriate type of Info.63 llvm::Expected<std::unique_ptr<Info>> readBlockToInfo(unsigned ID);64 65 template <typename InfoType, typename T, typename CallbackFunction>66 llvm::Error handleSubBlock(unsigned ID, T Parent, CallbackFunction Function);67 68 template <typename InfoType, typename T, typename CallbackFunction>69 llvm::Error handleTypeSubBlock(unsigned ID, T Parent,70 CallbackFunction Function);71 72 llvm::BitstreamCursor &Stream;73 std::optional<llvm::BitstreamBlockInfo> BlockInfo;74 FieldId CurrentReferenceField;75};76 77} // namespace doc78} // namespace clang79 80#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEREADER_H81