source_buffer.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TOOLCHAIN_SOURCE_SOURCE_BUFFER_H_
  5. #define CARBON_TOOLCHAIN_SOURCE_SOURCE_BUFFER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "llvm/ADT/StringRef.h"
  9. #include "llvm/Support/MemoryBuffer.h"
  10. #include "llvm/Support/VirtualFileSystem.h"
  11. #include "toolchain/diagnostics/emitter.h"
  12. namespace Carbon {
  13. // A buffer of Carbon source code.
  14. //
  15. // This class holds a buffer of Carbon source code as text and makes it
  16. // available for use in the rest of the Carbon compiler. It owns the memory for
  17. // the underlying source code text and ensures it lives as long as the buffer
  18. // objects.
  19. //
  20. // Every buffer of source code text is notionally loaded from a Carbon source
  21. // file, even if provided directly when constructing the buffer. The name that
  22. // should be used for that Carbon source file is also retained and made
  23. // available.
  24. //
  25. // Because the underlying memory for the source code text may have been read
  26. // from a file, and we may want to use facilities like `mmap` to simply map that
  27. // file into memory, the buffer itself is not copyable to avoid needing to
  28. // define copy semantics for a mapped file. We can relax this restriction with
  29. // some implementation complexity in the future if needed.
  30. class SourceBuffer {
  31. public:
  32. // Opens and reads the contents of stdin. Returns a SourceBuffer on success.
  33. // Prints an error and returns nullopt on failure.
  34. static auto MakeFromStdin(Diagnostics::Consumer& consumer)
  35. -> std::optional<SourceBuffer>;
  36. // Opens the requested file. Returns a SourceBuffer on success. Prints an
  37. // error and returns nullopt on failure.
  38. static auto MakeFromFile(llvm::vfs::FileSystem& fs, llvm::StringRef filename,
  39. Diagnostics::Consumer& consumer)
  40. -> std::optional<SourceBuffer>;
  41. // Handles conditional use of stdin based on the filename being "-".
  42. static auto MakeFromFileOrStdin(llvm::vfs::FileSystem& fs,
  43. llvm::StringRef filename,
  44. Diagnostics::Consumer& consumer)
  45. -> std::optional<SourceBuffer> {
  46. if (filename == "-") {
  47. return MakeFromStdin(consumer);
  48. } else {
  49. return MakeFromFile(fs, filename, consumer);
  50. }
  51. }
  52. // Returns a source buffer with the provided text content. Copies `filename`
  53. // and `text` to take ownership.
  54. static auto MakeFromStringCopy(llvm::StringRef filename, llvm::StringRef text,
  55. Diagnostics::Consumer& consumer)
  56. -> std::optional<SourceBuffer>;
  57. // Use one of the factory functions above to create a source buffer.
  58. SourceBuffer() = delete;
  59. auto filename() const -> llvm::StringRef { return filename_; }
  60. auto text() const -> llvm::StringRef { return text_->getBuffer(); }
  61. [[nodiscard]] auto is_regular_file() const -> bool {
  62. return is_regular_file_;
  63. }
  64. private:
  65. // Creates a `SourceBuffer` from the given `llvm::MemoryBuffer`. Prints an
  66. // error and returns nullopt on failure.
  67. static auto MakeFromMemoryBuffer(
  68. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buffer,
  69. llvm::StringRef filename, bool is_regular_file,
  70. Diagnostics::Consumer& consumer) -> std::optional<SourceBuffer>;
  71. explicit SourceBuffer(std::string filename,
  72. std::unique_ptr<llvm::MemoryBuffer> text,
  73. bool is_regular_file)
  74. : filename_(std::move(filename)),
  75. text_(std::move(text)),
  76. is_regular_file_(is_regular_file) {}
  77. std::string filename_;
  78. std::unique_ptr<llvm::MemoryBuffer> text_;
  79. // Whether this buffer is a regular file, rather than stdin or a named pipe or
  80. // similar.
  81. bool is_regular_file_;
  82. };
  83. } // namespace Carbon
  84. #endif // CARBON_TOOLCHAIN_SOURCE_SOURCE_BUFFER_H_