Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright (c) 2021-2024 ChilliBits. All rights reserved. | ||
2 | |||
3 | #pragma once | ||
4 | |||
5 | #include <Token.h> | ||
6 | #include <misc/Interval.h> | ||
7 | |||
8 | #include <util/GlobalDefinitions.h> | ||
9 | |||
10 | namespace spice::compiler { | ||
11 | |||
12 | // Forward declarations | ||
13 | class SourceFile; | ||
14 | |||
15 | struct CodeLoc { | ||
16 | public: | ||
17 | // Constructors | ||
18 | 13743 | explicit CodeLoc(const antlr4::Token *token, SourceFile *sourceFile = nullptr) | |
19 | 13743 | : sourceFile(sourceFile), sourceInterval(token->getStartIndex(), token->getStopIndex()), line(token->getLine()), | |
20 | 13743 | col(token->getCharPositionInLine() + 1){}; | |
21 | 1027798 | CodeLoc(const antlr4::Token *token, size_t startIdx, size_t stopIdx, SourceFile *sourceFile = nullptr) | |
22 | 1027798 | : sourceFile(sourceFile), sourceInterval(startIdx, stopIdx), line(token->getLine()), | |
23 | 1027798 | col(token->getCharPositionInLine() + 1){}; | |
24 | 114754 | CodeLoc(uint32_t line, uint32_t col, SourceFile *sourceFile = nullptr) : sourceFile(sourceFile), line(line), col(col) {} | |
25 | |||
26 | // Public members | ||
27 | SourceFile *sourceFile = nullptr; | ||
28 | antlr4::misc::Interval sourceInterval; | ||
29 | uint32_t line; | ||
30 | uint32_t col; | ||
31 | |||
32 | // Public methods | ||
33 | [[nodiscard]] std::string toString() const; | ||
34 | [[nodiscard]] std::string toPrettyString() const; | ||
35 | [[nodiscard]] std::string toPrettyLine() const; | ||
36 | [[nodiscard]] std::string toPrettyLineAndColumn() const; | ||
37 | |||
38 | // Operators | ||
39 | friend bool operator==(const CodeLoc &a, const CodeLoc &b); | ||
40 | ALWAYS_INLINE friend bool operator<(const CodeLoc &a, const CodeLoc &b) { | ||
41 |
3/4✓ Branch 0 taken 1100 times.
✓ Branch 1 taken 208 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1360 times.
|
2668 | return a.line == b.line ? a.col < b.col : a.line < b.line; |
42 | } | ||
43 | ALWAYS_INLINE friend bool operator>(const CodeLoc &a, const CodeLoc &b) { | ||
44 |
2/2✓ Branch 0 taken 254 times.
✓ Branch 1 taken 10296 times.
|
10550 | return a.line == b.line ? a.col > b.col : a.line > b.line; |
45 | } | ||
46 | }; | ||
47 | |||
48 | // Make sure we have no unexpected increases in memory consumption | ||
49 | static_assert(sizeof(CodeLoc) == 32); | ||
50 | |||
51 | } // namespace spice::compiler | ||
52 |