source_location.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 EXECUTABLE_SEMANTICS_COMMON_SOURCE_LOCATION_H_
  5. #define EXECUTABLE_SEMANTICS_COMMON_SOURCE_LOCATION_H_
  6. #include <string>
  7. #include <string_view>
  8. #include "common/ostream.h"
  9. #include "executable_semantics/common/nonnull.h"
  10. namespace Carbon {
  11. class SourceLocation {
  12. public:
  13. // The filename should be eternal or arena-allocated to eliminate copies.
  14. constexpr SourceLocation(const char* filename, int line_num)
  15. : filename_(filename), line_num_(line_num) {}
  16. SourceLocation(Nonnull<const std::string*> filename, int line_num)
  17. : filename_(filename->c_str()), line_num_(line_num) {}
  18. SourceLocation(const SourceLocation&) = default;
  19. SourceLocation(SourceLocation&&) = default;
  20. auto operator=(const SourceLocation&) -> SourceLocation& = default;
  21. auto operator=(SourceLocation&&) -> SourceLocation& = default;
  22. auto operator==(SourceLocation other) const -> bool {
  23. return filename_ == other.filename_ && line_num_ == other.line_num_;
  24. }
  25. void Print(llvm::raw_ostream& out) const {
  26. out << filename_ << ":" << line_num_;
  27. }
  28. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  29. private:
  30. std::string_view filename_;
  31. int line_num_;
  32. };
  33. } // namespace Carbon
  34. #endif // EXECUTABLE_SEMANTICS_COMMON_SOURCE_LOCATION_H_