blob: aaf25257c85d789a6c6a910200d60f48c1408ccc [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"
25
26namespace clang {
27namespace doc {
28
29// Class to read bitstream into an InfoSet collection
30class ClangDocBitcodeReader {
31public:
32 ClangDocBitcodeReader(llvm::BitstreamCursor &Stream) : Stream(Stream) {}
33
34 // Main entry point, calls readBlock to read each block in the given stream.
Julie Hockett8899c292018-08-02 20:10:17 +000035 llvm::Expected<std::vector<std::unique_ptr<Info>>> readBitcode();
Julie Hockettd0f9a872018-06-04 17:22:20 +000036
37private:
38 enum class Cursor { BadBlock = 1, Record, BlockEnd, BlockBegin };
39
40 // Top level parsing
41 bool validateStream();
42 bool readVersion();
43 bool readBlockInfoBlock();
44
45 // Read a block of records into a single Info struct, calls readRecord on each
46 // record found.
47 template <typename T> bool readBlock(unsigned ID, T I);
48
49 // Step through a block of records to find the next data field.
50 template <typename T> bool readSubBlock(unsigned ID, T I);
51
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> bool readRecord(unsigned ID, T I);
55
56 // Allocate the relevant type of info and add read data to the object.
57 template <typename T> std::unique_ptr<Info> createInfo(unsigned ID);
58
59 // Helper function to step through blocks to find and dispatch the next record
60 // or block to be read.
61 Cursor skipUntilRecordOrBlock(unsigned &BlockOrRecordID);
62
63 // Helper function to set up the approriate type of Info.
64 std::unique_ptr<Info> readBlockToInfo(unsigned ID);
65
66 llvm::BitstreamCursor &Stream;
67 Optional<llvm::BitstreamBlockInfo> BlockInfo;
68 FieldId CurrentReferenceField;
69};
70
71} // namespace doc
72} // namespace clang
73
74#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEREADER_H