BitReader.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //===-- BitReader.cpp -----------------------------------------------------===//
  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. #include "llvm-c/BitReader.h"
  10. #include "llvm/Bitcode/ReaderWriter.h"
  11. #include "llvm/LLVMContext.h"
  12. #include "llvm/Support/MemoryBuffer.h"
  13. #include <cstring>
  14. #include <string>
  15. using namespace llvm;
  16. /* Builds a module from the bitcode in the specified memory buffer, returning a
  17. reference to the module via the OutModule parameter. Returns 0 on success.
  18. Optionally returns a human-readable error message via OutMessage. */
  19. LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,
  20. LLVMModuleRef *OutModule, char **OutMessage) {
  21. return LLVMParseBitcodeInContext(wrap(&getGlobalContext()), MemBuf, OutModule,
  22. OutMessage);
  23. }
  24. LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
  25. LLVMMemoryBufferRef MemBuf,
  26. LLVMModuleRef *OutModule,
  27. char **OutMessage) {
  28. std::string Message;
  29. *OutModule = wrap(ParseBitcodeFile(unwrap(MemBuf), *unwrap(ContextRef),
  30. &Message));
  31. if (!*OutModule) {
  32. if (OutMessage)
  33. *OutMessage = strdup(Message.c_str());
  34. return 1;
  35. }
  36. return 0;
  37. }
  38. /* Reads a module from the specified path, returning via the OutModule parameter
  39. a module provider which performs lazy deserialization. Returns 0 on success.
  40. Optionally returns a human-readable error message via OutMessage. */
  41. LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
  42. LLVMMemoryBufferRef MemBuf,
  43. LLVMModuleRef *OutM,
  44. char **OutMessage) {
  45. std::string Message;
  46. *OutM = wrap(getLazyBitcodeModule(unwrap(MemBuf), *unwrap(ContextRef),
  47. &Message));
  48. if (!*OutM) {
  49. if (OutMessage)
  50. *OutMessage = strdup(Message.c_str());
  51. return 1;
  52. }
  53. return 0;
  54. }
  55. LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
  56. char **OutMessage) {
  57. return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM,
  58. OutMessage);
  59. }
  60. /* Deprecated: Use LLVMGetBitcodeModuleInContext instead. */
  61. LLVMBool LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef,
  62. LLVMMemoryBufferRef MemBuf,
  63. LLVMModuleProviderRef *OutMP,
  64. char **OutMessage) {
  65. return LLVMGetBitcodeModuleInContext(ContextRef, MemBuf,
  66. reinterpret_cast<LLVMModuleRef*>(OutMP),
  67. OutMessage);
  68. }
  69. /* Deprecated: Use LLVMGetBitcodeModule instead. */
  70. LLVMBool LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf,
  71. LLVMModuleProviderRef *OutMP,
  72. char **OutMessage) {
  73. return LLVMGetBitcodeModuleProviderInContext(LLVMGetGlobalContext(), MemBuf,
  74. OutMP, OutMessage);
  75. }