clang-tools 23.0.0git
BitcodeReader.h
Go to the documentation of this file.
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-exception
6//
7//===----------------------------------------------------------------------===//
8//
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.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEREADER_H
16#define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEREADER_H
17
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 collection
29public:
30 ClangDocBitcodeReader(llvm::BitstreamCursor &Stream, DiagnosticsEngine &Diags)
31 : Stream(Stream), Diags(Diags) {}
32
33 // Main entry point, calls readBlock to read each block in the given stream.
34 llvm::Expected<std::vector<Info *>> readBitcode();
35
36private:
37 enum class Cursor { BadBlock = 1, Record, BlockEnd, BlockBegin };
38
39 // Top level parsing
40 llvm::Error validateStream();
41 llvm::Error readVersion();
42 llvm::Error readBlockInfoBlock();
43
44 // Read a block of records into a single Info struct, calls readRecord on each
45 // record found.
46 template <typename T> llvm::Error readBlock(unsigned ID, T I);
47 template <typename T> llvm::Error readBlockWithNamespace(unsigned ID, T I);
48
49 template <typename T, typename BlockBeginHandler, typename BlockEndHandler,
50 typename RecordHandler>
51 llvm::Error parseBlock(unsigned ID, T I, BlockBeginHandler &&BBH,
52 BlockEndHandler &&BEH, RecordHandler &&RH);
53
54 template <typename T, typename BlockBeginHandler, typename BlockEndHandler>
55 llvm::Error parseBlock(unsigned ID, T I, BlockBeginHandler &&BBH,
56 BlockEndHandler &&BEH);
57
58 template <typename ChildType>
59 llvm::Expected<bool> readSubBlockIfMatch(unsigned ID, unsigned TargetID,
61
62 struct ReferenceMap {
63 FieldId Field;
65 };
66
67 template <typename InfoT>
68 llvm::Expected<bool>
69 routeReferenceBlock(unsigned ID, llvm::SmallVectorImpl<Reference> &Namespaces,
70 InfoT *I,
71 std::initializer_list<ReferenceMap> Mappings = {});
72
73 // Step through a block of records to find the next data field.
74 template <typename T> llvm::Error readSubBlock(unsigned ID, T I);
75
76 // Read record data into the given Info data field, calling the appropriate
77 // parseRecord functions to parse and store the data.
78 template <typename T> llvm::Error readRecord(unsigned ID, T I);
79
80 // Allocate the relevant type of info and add read data to the object.
81 template <typename T> llvm::Expected<Info *> createInfo(unsigned ID);
82
83 // Helper function to step through blocks to find and dispatch the next record
84 // or block to be read.
85 llvm::Expected<Cursor> skipUntilRecordOrBlock(unsigned &BlockOrRecordID);
86
87 // Helper function to set up the appropriate type of Info.
88 llvm::Expected<Info *> readBlockToInfo(unsigned ID);
89
90 template <typename InfoType, typename T, typename CallbackFunction>
91 llvm::Error handleSubBlock(unsigned ID, T Parent, CallbackFunction Function);
92
93 template <typename InfoType, typename T>
94 llvm::Error handleSubBlock(unsigned ID, T Parent);
95
96 template <typename InfoType, typename T, typename CallbackFunction>
97 llvm::Error handleTypeSubBlock(unsigned ID, T Parent,
98 CallbackFunction Function);
99
100 llvm::BitstreamCursor &Stream;
101 std::optional<llvm::BitstreamBlockInfo> BlockInfo;
102 FieldId CurrentReferenceField = FieldId::F_default;
103 DiagnosticsEngine &Diags;
104};
105
106} // namespace doc
107} // namespace clang
108
109#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEREADER_H
ClangDocBitcodeReader(llvm::BitstreamCursor &Stream, DiagnosticsEngine &Diags)
llvm::Expected< std::vector< Info * > > readBitcode()
llvm::SmallVector< uint64_t, 1024 > Record
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//