Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright (c) 2021-2025 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 | // Constructors | ||
17 | 18583 | explicit CodeLoc(const antlr4::Token *token, SourceFile *sourceFile = nullptr) | |
18 | 18583 | : sourceFile(sourceFile), sourceInterval(token->getStartIndex(), token->getStopIndex()), line(token->getLine()), | |
19 | 18583 | col(token->getCharPositionInLine() + 1){}; | |
20 | 1424076 | CodeLoc(const antlr4::Token *token, size_t startIdx, size_t stopIdx, SourceFile *sourceFile = nullptr) | |
21 | 1424076 | : sourceFile(sourceFile), sourceInterval(startIdx, stopIdx), line(token->getLine()), | |
22 | 1424076 | col(token->getCharPositionInLine() + 1){}; | |
23 | 119593 | CodeLoc(uint32_t line, uint32_t col, SourceFile *sourceFile = nullptr) : sourceFile(sourceFile), line(line), col(col) {} | |
24 | |||
25 | // Public members | ||
26 | SourceFile *sourceFile = nullptr; | ||
27 | antlr4::misc::Interval sourceInterval; | ||
28 | uint32_t line; | ||
29 | uint32_t col; | ||
30 | |||
31 | // Public methods | ||
32 | [[nodiscard]] std::string toString() const; | ||
33 | [[nodiscard]] std::string toPrettyString() const; | ||
34 | [[nodiscard]] std::string toPrettyLine() const; | ||
35 | [[nodiscard]] std::string toPrettyLineAndColumn() const; | ||
36 | |||
37 | // Operators | ||
38 | friend bool operator==(const CodeLoc &a, const CodeLoc &b); | ||
39 | ALWAYS_INLINE friend bool operator<(const CodeLoc &a, const CodeLoc &b) { | ||
40 |
3/4✓ Branch 0 (4→5) taken 1296 times.
✓ Branch 1 (4→6) taken 294 times.
✗ Branch 2 (4→5) not taken.
✓ Branch 3 (4→6) taken 1588 times.
|
3178 | return a.line == b.line ? a.col < b.col : a.line < b.line; |
41 | } | ||
42 | ALWAYS_INLINE friend bool operator>(const CodeLoc &a, const CodeLoc &b) { | ||
43 |
2/2✓ Branch 0 (91→92) taken 157 times.
✓ Branch 1 (91→93) taken 12755 times.
|
12912 | return a.line == b.line ? a.col > b.col : a.line > b.line; |
44 | } | ||
45 | }; | ||
46 | |||
47 | // Make sure we have no unexpected increases in memory consumption | ||
48 | static_assert(sizeof(CodeLoc) == 32); | ||
49 | |||
50 | } // namespace spice::compiler | ||
51 |