FE 0.10.0
Header-only C++ frontend library
Loading...
Searching...
No Matches
loc.h
Go to the documentation of this file.
1#pragma once
2
3#include <filesystem>
4
5#include "fe/sym.h"
6
7namespace fe {
8
9/// Pos%ition in a source file; pass around as value.
10/// We are only using `uint16_t` for row/col because this allows us to have `sizeof(Loc) == 16`:
11/// two machine words, which common 64 bit calling conventions (x86-64 System V, AArch64)
12/// still pass and return in registers - with `uint32_t`, every Loc would go through memory instead.
13/// @warning Rows/cols beyond 65535 wrap around - and row 0 doubles as the "invalid" sentinel,
14/// so Pos%itions in such files silently degrade to invalid/wrong ones.
15struct Pos {
16 Pos() = default; ///< Creates an invalid Pos%ition.
17 Pos(uint16_t row)
18 : row(row) {}
19 Pos(uint16_t row, uint16_t col)
20 : row(row)
21 , col(col) {}
22
23 explicit operator bool() const { return row != 0; } ///< Is a valid Pos%ition?
24 auto operator<=>(const Pos&) const = default;
25 void dump() const;
26
27 uint16_t row = 0;
28 uint16_t col = 0;
29
30 /// `fe/loc.h` only declares the stream output and dump helpers.
31 /// Include `fe/loc.cpp.h` in exactly one translation unit for the default implementation,
32 /// or provide your own definitions instead.
33 friend std::ostream& operator<<(std::ostream& os, Pos pos);
34};
35
36/// Loc%ation in a File.
37/// It's only two machine words on a 64 bit arch, so pass around as value.
38/// @warning Loc::path is only a pointer and it is your job to guarantee
39/// that the underlying `std::filesystem::path` outlives this Loc%ation.
40struct Loc {
41 Loc() = default; ///< Creates an invalid Loc%ation.
42 Loc(const std::filesystem::path* path, Pos begin, Pos finis)
43 : path(path)
44 , begin(begin)
45 , finis(finis) {}
46 Loc(const std::filesystem::path* file, Pos pos)
47 : Loc(file, pos, pos) {}
49 : Loc(nullptr, begin, finis) {}
50 Loc(Pos pos)
51 : Loc(nullptr, pos, pos) {}
52
53 Loc anew_begin() const { return {path, begin, begin}; }
54 Loc anew_finis() const { return {path, finis, finis}; }
55 Loc operator+(Pos pos) const { return {path, begin, pos}; }
56 Loc operator+(Loc loc) const { return {path, begin, loc.finis}; }
57 explicit operator bool() const { return (bool)begin; } ///< Is a valid Loc%ation?
58 /// @note Loc::path is only checked via pointer equality.
59 bool operator==(Loc other) const { return begin == other.begin && finis == other.finis && path == other.path; }
60 void dump() const;
61
62 const std::filesystem::path* path = {};
63 Pos begin = {};
64 Pos finis = {};
65 ///< It's called `finis` because it refers to the **last** character within this Loc%ation.
66 /// In the STL the word `end` refers to the position of something that is one element **past** the end.
67
68 /// `fe/loc.h` only declares the stream output and dump helpers.
69 /// Include `fe/loc.cpp.h` in exactly one translation unit for the default implementation,
70 /// or provide your own definitions instead.
71 friend std::ostream& operator<<(std::ostream& os, Loc loc);
72};
73
74// Keep Loc at two machine words so it is passed/returned in registers (see Pos).
75static_assert(sizeof(void*) != 8 || sizeof(Loc) == 16, "Loc should stay two machine words");
76
77} // namespace fe
Definition arena.h:13
Location in a File.
Definition loc.h:40
Loc(Pos begin, Pos finis)
Definition loc.h:48
void dump() const
Definition loc.cpp.h:28
Pos finis
It's called finis because it refers to the last character within this Location.
Definition loc.h:64
Loc()=default
Creates an invalid Location.
Loc(const std::filesystem::path *path, Pos begin, Pos finis)
Definition loc.h:42
const std::filesystem::path * path
Definition loc.h:62
Loc operator+(Loc loc) const
Definition loc.h:56
friend std::ostream & operator<<(std::ostream &os, Loc loc)
fe/loc.h only declares the stream output and dump helpers.
Definition loc.cpp.h:18
bool operator==(Loc other) const
Definition loc.h:59
Loc(const std::filesystem::path *file, Pos pos)
Definition loc.h:46
Pos begin
Definition loc.h:63
Loc(Pos pos)
Definition loc.h:50
Loc anew_begin() const
Definition loc.h:53
Loc operator+(Pos pos) const
Definition loc.h:55
Loc anew_finis() const
Definition loc.h:54
Position in a source file; pass around as value.
Definition loc.h:15
uint16_t col
Definition loc.h:28
Pos(uint16_t row)
Definition loc.h:17
auto operator<=>(const Pos &) const =default
Pos(uint16_t row, uint16_t col)
Definition loc.h:19
Pos()=default
Creates an invalid Position.
friend std::ostream & operator<<(std::ostream &os, Pos pos)
fe/loc.h only declares the stream output and dump helpers.
Definition loc.cpp.h:10
uint16_t row
Definition loc.h:27
void dump() const
Definition loc.cpp.h:27