BitcodeReader.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEREADER_H
  15. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEREADER_H
  16. #include "BitcodeWriter.h"
  17. #include "Representation.h"
  18. #include "clang/AST/AST.h"
  19. #include "llvm/ADT/Optional.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/Bitstream/BitstreamReader.h"
  22. #include "llvm/Support/Error.h"
  23. namespace clang {
  24. namespace doc {
  25. // Class to read bitstream into an InfoSet collection
  26. class ClangDocBitcodeReader {
  27. public:
  28. ClangDocBitcodeReader(llvm::BitstreamCursor &Stream) : Stream(Stream) {}
  29. // Main entry point, calls readBlock to read each block in the given stream.
  30. llvm::Expected<std::vector<std::unique_ptr<Info>>> readBitcode();
  31. private:
  32. enum class Cursor { BadBlock = 1, Record, BlockEnd, BlockBegin };
  33. // Top level parsing
  34. llvm::Error validateStream();
  35. llvm::Error readVersion();
  36. llvm::Error readBlockInfoBlock();
  37. // Read a block of records into a single Info struct, calls readRecord on each
  38. // record found.
  39. template <typename T> llvm::Error readBlock(unsigned ID, T I);
  40. // Step through a block of records to find the next data field.
  41. template <typename T> llvm::Error readSubBlock(unsigned ID, T I);
  42. // Read record data into the given Info data field, calling the appropriate
  43. // parseRecord functions to parse and store the data.
  44. template <typename T> llvm::Error readRecord(unsigned ID, T I);
  45. // Allocate the relevant type of info and add read data to the object.
  46. template <typename T>
  47. llvm::Expected<std::unique_ptr<Info>> createInfo(unsigned ID);
  48. // Helper function to step through blocks to find and dispatch the next record
  49. // or block to be read.
  50. Cursor skipUntilRecordOrBlock(unsigned &BlockOrRecordID);
  51. // Helper function to set up the appropriate type of Info.
  52. llvm::Expected<std::unique_ptr<Info>> readBlockToInfo(unsigned ID);
  53. llvm::BitstreamCursor &Stream;
  54. Optional<llvm::BitstreamBlockInfo> BlockInfo;
  55. FieldId CurrentReferenceField;
  56. };
  57. } // namespace doc
  58. } // namespace clang
  59. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEREADER_H