source_buffer.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 SOURCE_SOURCEBUFFER_H_
  5. #define SOURCE_SOURCEBUFFER_H_
  6. #include <string>
  7. #include <utility>
  8. #include "llvm/ADT/StringRef.h"
  9. #include "llvm/ADT/Twine.h"
  10. #include "llvm/Support/Error.h"
  11. namespace Carbon {
  12. // A buffer of Carbon source code.
  13. //
  14. // This class holds a buffer of Carbon source code as text and makes it
  15. // available for use in the rest of the Carbon compiler. It owns the memory for
  16. // the underlying source code text and ensures it lives as long as the buffer
  17. // objects.
  18. //
  19. // Every buffer of source code text is notionally loaded from a Carbon source
  20. // file, even if provided directly when constructing the buffer. The name that
  21. // should be used for that Carbon source file is also retained and made
  22. // available.
  23. //
  24. // Because the underlying memory for the source code text may have been read
  25. // from a file, and we may want to use facilities like `mmap` to simply map that
  26. // file into memory, the buffer itself is not copyable to avoid needing to
  27. // define copy semantics for a mapped file. We can relax this restriction with
  28. // some implementation complexity in the future if needed.
  29. class SourceBuffer {
  30. public:
  31. static auto CreateFromText(llvm::Twine text,
  32. llvm::StringRef filename = "/text")
  33. -> SourceBuffer;
  34. static auto CreateFromFile(llvm::StringRef filename)
  35. -> llvm::Expected<SourceBuffer>;
  36. // Use one of the factory functions above to create a source buffer.
  37. SourceBuffer() = delete;
  38. // Cannot copy as there may be non-trivial owned file data, see the class
  39. // comment for details.
  40. SourceBuffer(const SourceBuffer& arg) = delete;
  41. SourceBuffer(SourceBuffer&& arg) noexcept
  42. : filename_(std::move(arg.filename_)),
  43. text_(arg.text_),
  44. is_string_rep_(arg.is_string_rep_) {
  45. // The easy case in when we don't need to transfer an allocated string
  46. // representation.
  47. if (!arg.is_string_rep_) {
  48. // Take ownership of a non-string representation by clearing its text.
  49. arg.text_ = llvm::StringRef();
  50. return;
  51. }
  52. // If the argument is using a string rep we need to move that storage over
  53. // and recreate our text `StringRef` to point at our storage.
  54. new (&string_storage_) std::string(std::move(arg.string_storage_));
  55. text_ = string_storage_;
  56. }
  57. ~SourceBuffer();
  58. [[nodiscard]] auto Filename() const -> llvm::StringRef { return filename_; }
  59. [[nodiscard]] auto Text() const -> llvm::StringRef { return text_; }
  60. private:
  61. std::string filename_;
  62. llvm::StringRef text_;
  63. bool is_string_rep_;
  64. // We use a transparent union to avoid constructing the storage.
  65. // FIXME: We should replace this and the boolean with an optional which would
  66. // be much simpler.
  67. union {
  68. std::string string_storage_;
  69. };
  70. explicit SourceBuffer(llvm::StringRef fake_filename, std::string buffer_text)
  71. : filename_(fake_filename.str()),
  72. is_string_rep_(true),
  73. string_storage_(std::move(buffer_text)) {
  74. text_ = string_storage_;
  75. }
  76. explicit SourceBuffer(llvm::StringRef filename)
  77. : filename_(filename.str()), text_(), is_string_rep_(false) {}
  78. };
  79. } // namespace Carbon
  80. #endif // SOURCE_SOURCEBUFFER_H_