| 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 | 20783 | explicit CodeLoc(const antlr4::Token *token, SourceFile *sourceFile = nullptr) | |
| 18 | 20783 | : sourceFile(sourceFile), sourceInterval(token->getStartIndex(), token->getStopIndex()), line(token->getLine()), | |
| 19 | 20783 | col(token->getCharPositionInLine() + 1){}; | |
| 20 | 1592656 | CodeLoc(const antlr4::Token *token, size_t startIdx, size_t stopIdx, SourceFile *sourceFile = nullptr) | |
| 21 | 1592656 | : sourceFile(sourceFile), sourceInterval(startIdx, stopIdx), line(token->getLine()), | |
| 22 | 1592656 | col(token->getCharPositionInLine() + 1){}; | |
| 23 | 121793 | 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 2 → 3 taken 1626 times.
✗ Branch 2 → 4 not taken.
✓ Branch 4 → 5 taken 2 times.
✓ Branch 4 → 6 taken 2384 times.
|
4012 | 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 |
3/4✓ Branch 2 → 3 taken 142 times.
✓ Branch 2 → 4 taken 6014 times.
✗ Branch 91 → 92 not taken.
✓ Branch 91 → 93 taken 7184 times.
|
13340 | 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 |