Files
carbon-lang/executable_semantics/ast/source_location.h
T
Jon Meow 3ec550a85f Replace int lines with file-associated SourceLocations (#779)
Most interesting changes should be ast/source_location.h and syntax/parse_and_lex_context.h
2021-08-26 10:07:16 -07:00

46 lines
1.4 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef EXECUTABLE_SEMANTICS_AST_SOURCE_LOCATION_H_
#define EXECUTABLE_SEMANTICS_AST_SOURCE_LOCATION_H_
#include <string>
#include <string_view>
#include "common/ostream.h"
#include "executable_semantics/common/ptr.h"
namespace Carbon {
class SourceLocation {
public:
// The filename should be eternal or arena-allocated to eliminate copies.
SourceLocation(const char* filename, int line_num)
: filename(filename), line_num(line_num) {}
SourceLocation(Ptr<const std::string> filename, int line_num)
: filename(filename->c_str()), line_num(line_num) {}
SourceLocation(const SourceLocation&) = default;
SourceLocation(SourceLocation&&) = default;
auto operator=(const SourceLocation&) -> SourceLocation& = default;
auto operator=(SourceLocation&&) -> SourceLocation& = default;
bool operator==(SourceLocation other) const {
return filename == other.filename && line_num == other.line_num;
}
void Print(llvm::raw_ostream& out) const {
out << filename << ":" << line_num;
}
LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
private:
std::string_view filename;
int line_num;
};
} // namespace Carbon
#endif // EXECUTABLE_SEMANTICS_AST_SOURCE_LOCATION_H_