blob: ec3f6b0fd0ea1a559168f749c5b085e5ba251408 [file] [log] [blame]
Julie Hockettd0f9a872018-06-04 17:22:20 +00001//===-- 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 Hockett8899c292018-08-02 20:10:17 +000022#include "llvm/ADT/Optional.h"
Julie Hockettd0f9a872018-06-04 17:22:20 +000023#include "llvm/ADT/SmallVector.h"
24#include "llvm/Bitcode/BitstreamReader.h"
Julie Hockett46fc9592018-08-15 16:02:28 +000025#include "llvm/Support/Error.h"
Julie Hockettd0f9a872018-06-04 17:22:20 +000026
27namespace clang {
28namespace doc {
29
30// Class to read bitstream into an InfoSet collection
31class ClangDocBitcodeReader {
32public:
33 ClangDocBitcodeReader(llvm::BitstreamCursor &Stream) : Stream(Stream) {}
34
35 // Main entry point, calls readBlock to read each block in the given stream.
Julie Hockett8899c292018-08-02 20:10:17 +000036 llvm::Expected<std::vector<std::unique_ptr<Info>>> readBitcode();
Julie Hockettd0f9a872018-06-04 17:22:20 +000037
38private:
39 enum class Cursor { BadBlock = 1, Record, BlockEnd, BlockBegin };
40
41 // Top level parsing
Julie Hockett46fc9592018-08-15 16:02:28 +000042 llvm::Error validateStream();
43 llvm::Error readVersion();
44 llvm::Error readBlockInfoBlock();
Julie Hockettd0f9a872018-06-04 17:22:20 +000045
46 // Read a block of records into a single Info struct, calls readRecord on each
47 // record found.
Julie Hockett46fc9592018-08-15 16:02:28 +000048 template <typename T> llvm::Error readBlock(unsigned ID, T I);
Julie Hockettd0f9a872018-06-04 17:22:20 +000049
50 // Step through a block of records to find the next data field.
Julie Hockett46fc9592018-08-15 16:02:28 +000051 template <typename T> llvm::Error readSubBlock(unsigned ID, T I);
Julie Hockettd0f9a872018-06-04 17:22:20 +000052
53 // Read record data into the given Info data field, calling the appropriate
54 // parseRecord functions to parse and store the data.
Julie Hockett46fc9592018-08-15 16:02:28 +000055 template <typename T> llvm::Error readRecord(unsigned ID, T I);
Julie Hockettd0f9a872018-06-04 17:22:20 +000056
57 // Allocate the relevant type of info and add read data to the object.
Julie Hockett46fc9592018-08-15 16:02:28 +000058 template <typename T>
59 llvm::Expected<std::unique_ptr<Info>> createInfo(unsigned ID);
Julie Hockettd0f9a872018-06-04 17:22:20 +000060
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 Hockett46fc9592018-08-15 16:02:28 +000066 llvm::Expected<std::unique_ptr<Info>> readBlockToInfo(unsigned ID);
Julie Hockettd0f9a872018-06-04 17:22:20 +000067
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