mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
Replace Either with std::variant (#1731)
This commit is contained in:
@@ -10,16 +10,16 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "either.hpp"
|
||||
#include "linkdefs.hpp"
|
||||
|
||||
#include "asm/lexer.hpp"
|
||||
|
||||
struct FileStackNode {
|
||||
FileStackNodeType type;
|
||||
Either<
|
||||
std::variant<
|
||||
std::vector<uint32_t>, // NODE_REPT
|
||||
std::string // NODE_FILE, NODE_MACRO
|
||||
>
|
||||
@@ -34,13 +34,13 @@ struct FileStackNode {
|
||||
uint32_t ID = UINT32_MAX;
|
||||
|
||||
// REPT iteration counts since last named node, in reverse depth order
|
||||
std::vector<uint32_t> &iters() { return data.get<std::vector<uint32_t>>(); }
|
||||
std::vector<uint32_t> const &iters() const { return data.get<std::vector<uint32_t>>(); }
|
||||
std::vector<uint32_t> &iters() { return std::get<std::vector<uint32_t>>(data); }
|
||||
std::vector<uint32_t> const &iters() const { return std::get<std::vector<uint32_t>>(data); }
|
||||
// File name for files, file::macro name for macros
|
||||
std::string &name() { return data.get<std::string>(); }
|
||||
std::string const &name() const { return data.get<std::string>(); }
|
||||
std::string &name() { return std::get<std::string>(data); }
|
||||
std::string const &name() const { return std::get<std::string>(data); }
|
||||
|
||||
FileStackNode(FileStackNodeType type_, Either<std::vector<uint32_t>, std::string> data_)
|
||||
FileStackNode(FileStackNodeType type_, std::variant<std::vector<uint32_t>, std::string> data_)
|
||||
: type(type_), data(data_) {}
|
||||
|
||||
std::string const &dump(uint32_t curLineNo) const;
|
||||
|
||||
@@ -8,9 +8,9 @@
|
||||
#include <optional>
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "either.hpp"
|
||||
#include "platform.hpp" // SSIZE_MAX
|
||||
|
||||
// This value is a compromise between `LexerState` allocation performance when `mmap` works, and
|
||||
@@ -97,7 +97,7 @@ struct LexerState {
|
||||
bool expandStrings;
|
||||
std::deque<Expansion> expansions; // Front is the innermost current expansion
|
||||
|
||||
Either<ViewedContent, BufferedContent> content;
|
||||
std::variant<std::monostate, ViewedContent, BufferedContent> content;
|
||||
|
||||
~LexerState();
|
||||
|
||||
|
||||
@@ -5,15 +5,15 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "either.hpp"
|
||||
#include "linkdefs.hpp"
|
||||
|
||||
struct Symbol;
|
||||
|
||||
struct Expression {
|
||||
Either<
|
||||
std::variant<
|
||||
int32_t, // If the expression's value is known, it's here
|
||||
std::string // Why the expression is not known, if it isn't
|
||||
>
|
||||
@@ -22,8 +22,8 @@ struct Expression {
|
||||
std::vector<uint8_t> rpn{}; // Bytes serializing the RPN expression
|
||||
uint32_t rpnPatchSize = 0; // Size the expression will take in the object file
|
||||
|
||||
bool isKnown() const { return data.holds<int32_t>(); }
|
||||
int32_t value() const { return data.get<int32_t>(); }
|
||||
bool isKnown() const { return std::holds_alternative<int32_t>(data); }
|
||||
int32_t value() const { return std::get<int32_t>(data); }
|
||||
|
||||
int32_t getConstVal() const;
|
||||
Symbol const *symbolOf() const;
|
||||
|
||||
@@ -1,176 +0,0 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#ifndef RGBDS_EITHER_HPP
|
||||
#define RGBDS_EITHER_HPP
|
||||
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "helpers.hpp" // assume
|
||||
|
||||
template<typename T1, typename T2>
|
||||
union Either {
|
||||
typedef T1 type1;
|
||||
typedef T2 type2;
|
||||
|
||||
private:
|
||||
template<typename T, unsigned V>
|
||||
struct Field {
|
||||
constexpr static unsigned tag_value = V;
|
||||
|
||||
unsigned tag = tag_value;
|
||||
T value;
|
||||
|
||||
Field() : value() {}
|
||||
Field(T &value_) : value(value_) {}
|
||||
Field(T const &value_) : value(value_) {}
|
||||
Field(T &&value_) : value(std::move(value_)) {}
|
||||
};
|
||||
|
||||
// The `_tag` unifies with the first `tag` member of each `struct`.
|
||||
constexpr static unsigned nulltag = 0;
|
||||
unsigned _tag = nulltag;
|
||||
Field<T1, 1> _t1;
|
||||
Field<T2, 2> _t2;
|
||||
|
||||
// Value accessors; the function parameters are dummies for overload resolution.
|
||||
// Only used to implement `field()` below.
|
||||
auto &pick(T1 *) { return _t1; }
|
||||
auto const &pick(T1 *) const { return _t1; }
|
||||
auto &pick(T2 *) { return _t2; }
|
||||
auto const &pick(T2 *) const { return _t2; }
|
||||
|
||||
// Generic field accessors; for internal use only.
|
||||
template<typename T>
|
||||
auto &field() {
|
||||
return pick(static_cast<T *>(nullptr));
|
||||
}
|
||||
template<typename T>
|
||||
auto const &field() const {
|
||||
return pick(static_cast<T *>(nullptr));
|
||||
}
|
||||
|
||||
public:
|
||||
// Equivalent of `std::monostate` for `std::variant`s.
|
||||
Either() : _tag() {}
|
||||
// These constructors cannot be generic over the value type, because that would prevent
|
||||
// constructible values from being inferred, e.g. a `const char *` string literal for an
|
||||
// `std::string` field value.
|
||||
Either(T1 &value) : _t1(value) {}
|
||||
Either(T2 &value) : _t2(value) {}
|
||||
Either(T1 const &value) : _t1(value) {}
|
||||
Either(T2 const &value) : _t2(value) {}
|
||||
Either(T1 &&value) : _t1(std::move(value)) {}
|
||||
Either(T2 &&value) : _t2(std::move(value)) {}
|
||||
|
||||
// Destructor manually calls the appropriate value destructor.
|
||||
~Either() {
|
||||
if (_tag == _t1.tag_value) {
|
||||
_t1.value.~T1();
|
||||
} else if (_tag == _t2.tag_value) {
|
||||
_t2.value.~T2();
|
||||
}
|
||||
}
|
||||
|
||||
// Copy assignment operators for each possible value.
|
||||
Either &operator=(T1 const &value) {
|
||||
_t1.tag = _t1.tag_value;
|
||||
new (&_t1.value) T1(value);
|
||||
return *this;
|
||||
}
|
||||
Either &operator=(T2 const &value) {
|
||||
_t2.tag = _t2.tag_value;
|
||||
new (&_t2.value) T2(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Move assignment operators for each possible value.
|
||||
Either &operator=(T1 &&value) {
|
||||
_t1.tag = _t1.tag_value;
|
||||
new (&_t1.value) T1(std::move(value));
|
||||
return *this;
|
||||
}
|
||||
Either &operator=(T2 &&value) {
|
||||
_t2.tag = _t2.tag_value;
|
||||
new (&_t2.value) T2(std::move(value));
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Copy assignment operator from another `Either`.
|
||||
Either &operator=(Either other) {
|
||||
if (other._tag == other._t1.tag_value) {
|
||||
*this = other._t1.value;
|
||||
} else if (other._tag == other._t2.tag_value) {
|
||||
*this = other._t2.value;
|
||||
} else {
|
||||
_tag = nulltag; // LCOV_EXCL_LINE
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Copy constructor from another `Either`; implemented in terms of value assignment operators.
|
||||
Either(Either const &other) {
|
||||
if (other._tag == other._t1.tag_value) {
|
||||
*this = other._t1.value;
|
||||
} else if (other._tag == other._t2.tag_value) {
|
||||
*this = other._t2.value;
|
||||
} else {
|
||||
_tag = nulltag;
|
||||
}
|
||||
}
|
||||
|
||||
// Move constructor from another `Either`; implemented in terms of value assignment operators.
|
||||
Either(Either &&other) {
|
||||
if (other._tag == other._t1.tag_value) {
|
||||
*this = std::move(other._t1.value);
|
||||
} else if (other._tag == other._t2.tag_value) {
|
||||
*this = std::move(other._t2.value);
|
||||
} else {
|
||||
_tag = nulltag;
|
||||
}
|
||||
}
|
||||
|
||||
// Equivalent of `.emplace<T>()` for `std::variant`s.
|
||||
template<typename T, typename... Args>
|
||||
void emplace(Args &&...args) {
|
||||
this->~Either();
|
||||
if constexpr (std::is_same_v<T, T1>) {
|
||||
_t1.tag = _t1.tag_value;
|
||||
new (&_t1.value) T1(std::forward<Args>(args)...);
|
||||
} else if constexpr (std::is_same_v<T, T2>) {
|
||||
_t2.tag = _t2.tag_value;
|
||||
new (&_t2.value) T2(std::forward<Args>(args)...);
|
||||
} else {
|
||||
_tag = nulltag;
|
||||
}
|
||||
}
|
||||
|
||||
// Equivalent of `std::holds_alternative<std::monostate>()` for `std::variant`s.
|
||||
bool empty() const { return _tag == nulltag; }
|
||||
|
||||
// Equivalent of `std::holds_alternative<T>()` for `std::variant`s.
|
||||
template<typename T>
|
||||
bool holds() const {
|
||||
if constexpr (std::is_same_v<T, T1>) {
|
||||
return _tag == _t1.tag_value;
|
||||
} else if constexpr (std::is_same_v<T, T2>) {
|
||||
return _tag == _t2.tag_value;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Equivalent of `std::get<T>()` for `std::variant`s.
|
||||
template<typename T>
|
||||
auto &get() {
|
||||
assume(holds<T>());
|
||||
return field<T>().value;
|
||||
}
|
||||
template<typename T>
|
||||
auto const &get() const {
|
||||
assume(holds<T>());
|
||||
return field<T>().value;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // RGBDS_EITHER_HPP
|
||||
@@ -10,15 +10,15 @@
|
||||
#include <streambuf>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
#include "either.hpp"
|
||||
#include "helpers.hpp" // assume
|
||||
#include "platform.hpp"
|
||||
|
||||
#include "gfx/main.hpp"
|
||||
#include "gfx/warning.hpp"
|
||||
|
||||
class File {
|
||||
Either<std::streambuf *, std::filebuf> _file;
|
||||
std::variant<std::streambuf *, std::filebuf> _file;
|
||||
|
||||
public:
|
||||
File() : _file(nullptr) {}
|
||||
@@ -27,8 +27,7 @@ public:
|
||||
// Returns `nullptr` on error, and a non-null pointer otherwise.
|
||||
File *open(std::string const &path, std::ios_base::openmode mode) {
|
||||
if (path != "-") {
|
||||
_file.emplace<std::filebuf>();
|
||||
return _file.get<std::filebuf>().open(path, mode) ? this : nullptr;
|
||||
return _file.emplace<std::filebuf>().open(path, mode) ? this : nullptr;
|
||||
} else if (mode & std::ios_base::in) {
|
||||
assume(!(mode & std::ios_base::out));
|
||||
_file.emplace<std::streambuf *>(std::cin.rdbuf());
|
||||
@@ -46,8 +45,8 @@ public:
|
||||
return this;
|
||||
}
|
||||
std::streambuf &operator*() {
|
||||
return _file.holds<std::filebuf>() ? _file.get<std::filebuf>()
|
||||
: *_file.get<std::streambuf *>();
|
||||
return std::holds_alternative<std::filebuf>(_file) ? std::get<std::filebuf>(_file)
|
||||
: *std::get<std::streambuf *>(_file);
|
||||
}
|
||||
std::streambuf const &operator*() const {
|
||||
// The non-`const` version does not perform any modifications, so it's okay.
|
||||
@@ -60,9 +59,9 @@ public:
|
||||
}
|
||||
|
||||
char const *c_str(std::string const &path) const {
|
||||
return _file.holds<std::filebuf>() ? path.c_str()
|
||||
: _file.get<std::streambuf *>() == std::cin.rdbuf() ? "<stdin>"
|
||||
: "<stdout>";
|
||||
return std::holds_alternative<std::filebuf>(_file) ? path.c_str()
|
||||
: std::get<std::streambuf *>(_file) == std::cin.rdbuf() ? "<stdin>"
|
||||
: "<stdout>";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -6,9 +6,9 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "either.hpp"
|
||||
#include "linkdefs.hpp"
|
||||
|
||||
// Variables related to CLI options
|
||||
@@ -39,7 +39,8 @@ extern bool disablePadding;
|
||||
|
||||
struct FileStackNode {
|
||||
FileStackNodeType type;
|
||||
Either<
|
||||
std::variant<
|
||||
std::monostate, // Default constructed; `.type` and `.data` must be set manually
|
||||
std::vector<uint32_t>, // NODE_REPT
|
||||
std::string // NODE_FILE, NODE_MACRO
|
||||
>
|
||||
@@ -50,11 +51,11 @@ struct FileStackNode {
|
||||
uint32_t lineNo;
|
||||
|
||||
// REPT iteration counts since last named node, in reverse depth order
|
||||
std::vector<uint32_t> &iters() { return data.get<std::vector<uint32_t>>(); }
|
||||
std::vector<uint32_t> const &iters() const { return data.get<std::vector<uint32_t>>(); }
|
||||
std::vector<uint32_t> &iters() { return std::get<std::vector<uint32_t>>(data); }
|
||||
std::vector<uint32_t> const &iters() const { return std::get<std::vector<uint32_t>>(data); }
|
||||
// File name for files, file::macro name for macros
|
||||
std::string &name() { return data.get<std::string>(); }
|
||||
std::string const &name() const { return data.get<std::string>(); }
|
||||
std::string &name() { return std::get<std::string>(data); }
|
||||
std::string const &name() const { return std::get<std::string>(data); }
|
||||
|
||||
std::string const &dump(uint32_t curLineNo) const;
|
||||
};
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
#include "either.hpp"
|
||||
#include "linkdefs.hpp"
|
||||
|
||||
struct FileStackNode;
|
||||
@@ -27,14 +27,14 @@ struct Symbol {
|
||||
ExportLevel type;
|
||||
FileStackNode const *src;
|
||||
int32_t lineNo;
|
||||
Either<
|
||||
std::variant<
|
||||
int32_t, // Constants just have a numeric value
|
||||
Label // Label values refer to an offset within a specific section
|
||||
>
|
||||
data;
|
||||
|
||||
Label &label() { return data.get<Label>(); }
|
||||
Label const &label() const { return data.get<Label>(); }
|
||||
Label &label() { return std::get<Label>(data); }
|
||||
Label const &label() const { return std::get<Label>(data); }
|
||||
};
|
||||
|
||||
void sym_ForEach(void (*callback)(Symbol &));
|
||||
|
||||
@@ -49,7 +49,7 @@ static std::vector<std::string> includePaths = {""};
|
||||
static std::string preIncludeName;
|
||||
|
||||
std::string const &FileStackNode::dump(uint32_t curLineNo) const {
|
||||
if (data.holds<std::vector<uint32_t>>()) {
|
||||
if (std::holds_alternative<std::vector<uint32_t>>(data)) {
|
||||
assume(parent); // REPT nodes use their parent's name
|
||||
std::string const &lastName = parent->dump(lineNo);
|
||||
fputs(" -> ", stderr);
|
||||
|
||||
@@ -108,10 +108,10 @@ using namespace std::literals;
|
||||
|
||||
struct Token {
|
||||
int type;
|
||||
Either<uint32_t, std::string> value;
|
||||
std::variant<std::monostate, uint32_t, std::string> value;
|
||||
|
||||
Token() : type(T_(NUMBER)), value() {}
|
||||
Token(int type_) : type(type_), value() {}
|
||||
Token() : type(T_(NUMBER)), value(std::monostate{}) {}
|
||||
Token(int type_) : type(type_), value(std::monostate{}) {}
|
||||
Token(int type_, uint32_t value_) : type(type_), value(value_) {}
|
||||
Token(int type_, std::string const &value_) : type(type_), value(value_) {}
|
||||
Token(int type_, std::string &&value_) : type(type_), value(value_) {}
|
||||
@@ -489,8 +489,8 @@ void LexerState::setViewAsNextState(char const *name, ContentSpan const &span, u
|
||||
}
|
||||
|
||||
void lexer_RestartRept(uint32_t lineNo) {
|
||||
if (lexerState->content.holds<ViewedContent>()) {
|
||||
lexerState->content.get<ViewedContent>().offset = 0;
|
||||
if (std::holds_alternative<ViewedContent>(lexerState->content)) {
|
||||
std::get<ViewedContent>(lexerState->content).offset = 0;
|
||||
}
|
||||
lexerState->clear(lineNo);
|
||||
}
|
||||
@@ -755,13 +755,13 @@ int LexerState::peekChar() {
|
||||
}
|
||||
}
|
||||
|
||||
if (content.holds<ViewedContent>()) {
|
||||
auto &view = content.get<ViewedContent>();
|
||||
if (std::holds_alternative<ViewedContent>(content)) {
|
||||
auto &view = std::get<ViewedContent>(content);
|
||||
if (view.offset < view.span.size) {
|
||||
return static_cast<uint8_t>(view.span.ptr[view.offset]);
|
||||
}
|
||||
} else {
|
||||
auto &cbuf = content.get<BufferedContent>();
|
||||
auto &cbuf = std::get<BufferedContent>(content);
|
||||
if (cbuf.size == 0) {
|
||||
cbuf.refill();
|
||||
}
|
||||
@@ -791,13 +791,13 @@ int LexerState::peekCharAhead() {
|
||||
distance -= exp.size() - exp.offset;
|
||||
}
|
||||
|
||||
if (content.holds<ViewedContent>()) {
|
||||
auto &view = content.get<ViewedContent>();
|
||||
if (std::holds_alternative<ViewedContent>(content)) {
|
||||
auto &view = std::get<ViewedContent>(content);
|
||||
if (view.offset + distance < view.span.size) {
|
||||
return static_cast<uint8_t>(view.span.ptr[view.offset + distance]);
|
||||
}
|
||||
} else {
|
||||
auto &cbuf = content.get<BufferedContent>();
|
||||
auto &cbuf = std::get<BufferedContent>(content);
|
||||
assume(distance < std::size(cbuf.buf));
|
||||
if (cbuf.size <= distance) {
|
||||
cbuf.refill();
|
||||
@@ -882,10 +882,10 @@ static void shiftChar() {
|
||||
}
|
||||
} else {
|
||||
// Advance within the file contents
|
||||
if (lexerState->content.holds<ViewedContent>()) {
|
||||
lexerState->content.get<ViewedContent>().offset++;
|
||||
if (std::holds_alternative<ViewedContent>(lexerState->content)) {
|
||||
std::get<ViewedContent>(lexerState->content).offset++;
|
||||
} else {
|
||||
lexerState->content.get<BufferedContent>().advance();
|
||||
std::get<BufferedContent>(lexerState->content).advance();
|
||||
}
|
||||
}
|
||||
return;
|
||||
@@ -2030,12 +2030,12 @@ static Token yylex_NORMAL() {
|
||||
}
|
||||
|
||||
// `token` is either a `SYMBOL` or a `LOCAL`, and both have a `std::string` value.
|
||||
assume(token.value.holds<std::string>());
|
||||
assume(std::holds_alternative<std::string>(token.value));
|
||||
|
||||
// Raw symbols and local symbols cannot be string expansions
|
||||
if (!raw && token.type == T_(SYMBOL) && lexerState->expandStrings) {
|
||||
// Attempt string expansion
|
||||
Symbol const *sym = sym_FindExactSymbol(token.value.get<std::string>());
|
||||
Symbol const *sym = sym_FindExactSymbol(std::get<std::string>(token.value));
|
||||
|
||||
if (sym && sym->type == SYM_EQUS) {
|
||||
std::shared_ptr<std::string> str = sym->getEqus();
|
||||
@@ -2449,21 +2449,17 @@ yy::parser::symbol_type yylex() {
|
||||
|
||||
// Captures end at their buffer's boundary no matter what
|
||||
if (token.type == T_(YYEOF) && !lexerState->capturing) {
|
||||
// Doing `token = Token(T_(EOB));` here would be valid but redundant, because YYEOF and EOB
|
||||
// both have the same empty value. Furthermore, g++ 11.4.0 was giving a false-positive
|
||||
// '-Wmaybe-uninitialized' warning for `Token::value.Either<...>::_tag` that way.
|
||||
// (This was on a developer's local machine; GitHub Actions CI's g++ was not warning.)
|
||||
token.type = T_(EOB);
|
||||
}
|
||||
lexerState->lastToken = token.type;
|
||||
lexerState->atLineStart = token.type == T_(NEWLINE) || token.type == T_(EOB);
|
||||
|
||||
if (token.value.holds<uint32_t>()) {
|
||||
return yy::parser::symbol_type(token.type, token.value.get<uint32_t>());
|
||||
} else if (token.value.holds<std::string>()) {
|
||||
return yy::parser::symbol_type(token.type, token.value.get<std::string>());
|
||||
if (std::holds_alternative<uint32_t>(token.value)) {
|
||||
return yy::parser::symbol_type(token.type, std::get<uint32_t>(token.value));
|
||||
} else if (std::holds_alternative<std::string>(token.value)) {
|
||||
return yy::parser::symbol_type(token.type, std::get<std::string>(token.value));
|
||||
} else {
|
||||
assume(token.value.empty());
|
||||
assume(std::holds_alternative<std::monostate>(token.value));
|
||||
return yy::parser::symbol_type(token.type);
|
||||
}
|
||||
}
|
||||
@@ -2479,8 +2475,9 @@ static Capture startCapture() {
|
||||
lexerState->captureSize = 0;
|
||||
|
||||
uint32_t lineNo = lexer_GetLineNo();
|
||||
if (lexerState->content.holds<ViewedContent>() && lexerState->expansions.empty()) {
|
||||
auto &view = lexerState->content.get<ViewedContent>();
|
||||
if (std::holds_alternative<ViewedContent>(lexerState->content)
|
||||
&& lexerState->expansions.empty()) {
|
||||
auto &view = std::get<ViewedContent>(lexerState->content);
|
||||
return {
|
||||
.lineNo = lineNo, .span = {.ptr = view.makeSharedContentPtr(), .size = 0}
|
||||
};
|
||||
|
||||
@@ -7,9 +7,9 @@
|
||||
%code requires {
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "either.hpp"
|
||||
#include "linkdefs.hpp"
|
||||
|
||||
#include "asm/lexer.hpp"
|
||||
@@ -30,7 +30,7 @@
|
||||
|
||||
struct StrFmtArgList {
|
||||
std::string format;
|
||||
std::vector<Either<uint32_t, std::string>> args;
|
||||
std::vector<std::variant<uint32_t, std::string>> args;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@
|
||||
static uint32_t adjustNegativePos(int32_t pos, size_t len, char const *functionName);
|
||||
static std::string strrpl(std::string_view str, std::string const &old, std::string const &rep);
|
||||
static std::string strfmt(
|
||||
std::string const &spec, std::vector<Either<uint32_t, std::string>> const &args
|
||||
std::string const &spec, std::vector<std::variant<uint32_t, std::string>> const &args
|
||||
);
|
||||
static void compoundAssignment(std::string const &symName, RPNCommand op, int32_t constValue);
|
||||
static void failAssert(AssertionType type);
|
||||
@@ -2964,7 +2964,7 @@ static std::string strrpl(std::string_view str, std::string const &old, std::str
|
||||
}
|
||||
|
||||
static std::string
|
||||
strfmt(std::string const &spec, std::vector<Either<uint32_t, std::string>> const &args) {
|
||||
strfmt(std::string const &spec, std::vector<std::variant<uint32_t, std::string>> const &args) {
|
||||
std::string str;
|
||||
size_t argIndex = 0;
|
||||
|
||||
@@ -3005,10 +3005,10 @@ static std::string
|
||||
} else if (argIndex >= args.size()) {
|
||||
// Will warn after formatting is done.
|
||||
str += '%';
|
||||
} else if (args[argIndex].holds<uint32_t>()) {
|
||||
fmt.appendNumber(str, args[argIndex].get<uint32_t>());
|
||||
} else if (std::holds_alternative<uint32_t>(args[argIndex])) {
|
||||
fmt.appendNumber(str, std::get<uint32_t>(args[argIndex]));
|
||||
} else {
|
||||
fmt.appendString(str, args[argIndex].get<std::string>());
|
||||
fmt.appendString(str, std::get<std::string>(args[argIndex]));
|
||||
}
|
||||
|
||||
argIndex++;
|
||||
|
||||
@@ -39,7 +39,7 @@ uint8_t *Expression::reserveSpace(uint32_t size, uint32_t patchSize) {
|
||||
|
||||
int32_t Expression::getConstVal() const {
|
||||
if (!isKnown()) {
|
||||
error("Expected constant expression: %s", data.get<std::string>().c_str());
|
||||
error("Expected constant expression: %s", std::get<std::string>(data).c_str());
|
||||
return 0;
|
||||
}
|
||||
return value();
|
||||
|
||||
@@ -46,7 +46,7 @@ bool disablePadding; // -x
|
||||
FILE *linkerScript;
|
||||
|
||||
std::string const &FileStackNode::dump(uint32_t curLineNo) const {
|
||||
if (data.holds<std::vector<uint32_t>>()) {
|
||||
if (std::holds_alternative<std::vector<uint32_t>>(data)) {
|
||||
assume(parent); // REPT nodes use their parent's name
|
||||
std::string const &lastName = parent->dump(lineNo);
|
||||
fputs(" -> ", stderr);
|
||||
|
||||
@@ -468,7 +468,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) {
|
||||
// object file. It's better than nothing.
|
||||
nodes[fileID].push_back({
|
||||
.type = NODE_FILE,
|
||||
.data = Either<std::vector<uint32_t>, std::string>(fileName),
|
||||
.data = std::variant<std::monostate, std::vector<uint32_t>, std::string>(fileName),
|
||||
.parent = nullptr,
|
||||
.lineNo = 0,
|
||||
});
|
||||
@@ -533,8 +533,8 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) {
|
||||
readSymbol(file, symbol, fileName, nodes[fileID]);
|
||||
|
||||
sym_AddSymbol(symbol);
|
||||
if (symbol.data.holds<Label>()) {
|
||||
nbSymPerSect[symbol.data.get<Label>().sectionID]++;
|
||||
if (std::holds_alternative<Label>(symbol.data)) {
|
||||
nbSymPerSect[std::get<Label>(symbol.data).sectionID]++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -574,8 +574,8 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) {
|
||||
|
||||
// Give symbols' section pointers to their sections
|
||||
for (uint32_t i = 0; i < nbSymbols; i++) {
|
||||
if (fileSymbols[i].data.holds<Label>()) {
|
||||
Label &label = fileSymbols[i].data.get<Label>();
|
||||
if (std::holds_alternative<Label>(fileSymbols[i].data)) {
|
||||
Label &label = std::get<Label>(fileSymbols[i].data);
|
||||
label.section = fileSections[label.sectionID].get();
|
||||
// Give the section a pointer to the symbol as well
|
||||
linkSymToSect(fileSymbols[i], *label.section);
|
||||
@@ -591,8 +591,8 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) {
|
||||
// This has to run **after** all the `sect_AddSection()` calls,
|
||||
// so that `sect_GetSection()` will work
|
||||
for (uint32_t i = 0; i < nbSymbols; i++) {
|
||||
if (fileSymbols[i].data.holds<Label>()) {
|
||||
Label &label = fileSymbols[i].data.get<Label>();
|
||||
if (std::holds_alternative<Label>(fileSymbols[i].data)) {
|
||||
Label &label = std::get<Label>(fileSymbols[i].data);
|
||||
if (Section *section = label.section; section->modifier != SECTION_NORMAL) {
|
||||
if (section->modifier == SECTION_FRAGMENT) {
|
||||
// Add the fragment's offset to the symbol's
|
||||
|
||||
@@ -362,7 +362,7 @@ static void writeSymBank(SortedSections const &bankSections, SectionType type, u
|
||||
if (auto pos = sym->name.find('.'); pos != std::string::npos) {
|
||||
std::string parentName = sym->name.substr(0, pos);
|
||||
if (Symbol const *parentSym = sym_GetSymbol(parentName);
|
||||
parentSym && parentSym->data.holds<Label>()) {
|
||||
parentSym && std::holds_alternative<Label>(parentSym->data)) {
|
||||
auto const &parentLabel = parentSym->label();
|
||||
assume(parentLabel.section != nullptr);
|
||||
parentAddr =
|
||||
@@ -593,17 +593,17 @@ static void writeSym() {
|
||||
constants.clear();
|
||||
sym_ForEach([](Symbol &sym) {
|
||||
// Symbols are already limited to the exported ones
|
||||
if (sym.data.holds<int32_t>()) {
|
||||
if (std::holds_alternative<int32_t>(sym.data)) {
|
||||
constants.push_back(&sym);
|
||||
}
|
||||
});
|
||||
// Numeric constants are ordered by value, then by name
|
||||
std::sort(RANGE(constants), [](Symbol *sym1, Symbol *sym2) -> bool {
|
||||
int32_t val1 = sym1->data.get<int32_t>(), val2 = sym2->data.get<int32_t>();
|
||||
int32_t val1 = std::get<int32_t>(sym1->data), val2 = std::get<int32_t>(sym2->data);
|
||||
return val1 != val2 ? val1 < val2 : sym1->name < sym2->name;
|
||||
});
|
||||
for (Symbol *sym : constants) {
|
||||
int32_t val = sym->data.get<int32_t>();
|
||||
int32_t val = std::get<int32_t>(sym->data);
|
||||
int width = val < 0x100 ? 2 : val < 0x10000 ? 4 : 8;
|
||||
fprintf(symFile, "%0*" PRIx32 " ", width, val);
|
||||
printSymName(sym->name, symFile);
|
||||
|
||||
@@ -230,8 +230,8 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector<Symbol> const &fil
|
||||
);
|
||||
isError = true;
|
||||
value = 1;
|
||||
} else if (symbol->data.holds<Label>()) {
|
||||
value = symbol->data.get<Label>().section->bank;
|
||||
} else if (std::holds_alternative<Label>(symbol->data)) {
|
||||
value = std::get<Label>(symbol->data).section->bank;
|
||||
} else {
|
||||
error(
|
||||
patch.src,
|
||||
@@ -420,11 +420,11 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector<Symbol> const &fil
|
||||
);
|
||||
sym_DumpLocalAliasedSymbols(fileSymbols[value].name);
|
||||
isError = true;
|
||||
} else if (symbol->data.holds<Label>()) {
|
||||
Label const &label = symbol->data.get<Label>();
|
||||
} else if (std::holds_alternative<Label>(symbol->data)) {
|
||||
Label const &label = std::get<Label>(symbol->data);
|
||||
value = label.section->org + label.offset;
|
||||
} else {
|
||||
value = symbol->data.get<int32_t>();
|
||||
value = std::get<int32_t>(symbol->data);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -418,11 +418,11 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector<Symbol>
|
||||
// The same symbol can only be defined twice if neither
|
||||
// definition is in a floating section
|
||||
auto checkSymbol = [](Symbol const &sym) -> std::tuple<Section *, int32_t> {
|
||||
if (sym.data.holds<Label>()) {
|
||||
Label const &label = sym.data.get<Label>();
|
||||
if (std::holds_alternative<Label>(sym.data)) {
|
||||
Label const &label = std::get<Label>(sym.data);
|
||||
return {label.section, label.offset};
|
||||
}
|
||||
return {nullptr, sym.data.get<int32_t>()};
|
||||
return {nullptr, std::get<int32_t>(sym.data)};
|
||||
};
|
||||
auto [symbolSection, symbolValue] = checkSymbol(symbol);
|
||||
auto [otherSection, otherValue] = checkSymbol(*other);
|
||||
@@ -929,8 +929,8 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector<Symbol>
|
||||
// This has to run **after** all the `sect_AddSection()` calls,
|
||||
// so that `sect_GetSection()` will work
|
||||
for (Symbol &sym : fileSymbols) {
|
||||
if (sym.data.holds<Label>()) {
|
||||
Label &label = sym.data.get<Label>();
|
||||
if (std::holds_alternative<Label>(sym.data)) {
|
||||
Label &label = std::get<Label>(sym.data);
|
||||
if (Section *section = label.section; section->modifier != SECTION_NORMAL) {
|
||||
if (section->modifier == SECTION_FRAGMENT) {
|
||||
// Add the fragment's offset to the symbol's
|
||||
|
||||
@@ -31,9 +31,11 @@ void sym_AddSymbol(Symbol &symbol) {
|
||||
}
|
||||
|
||||
Symbol *other = sym_GetSymbol(symbol.name);
|
||||
int32_t *symValue = symbol.data.holds<int32_t>() ? &symbol.data.get<int32_t>() : nullptr;
|
||||
int32_t *otherValue =
|
||||
other && other->data.holds<int32_t>() ? &other->data.get<int32_t>() : nullptr;
|
||||
int32_t *symValue =
|
||||
std::holds_alternative<int32_t>(symbol.data) ? &std::get<int32_t>(symbol.data) : nullptr;
|
||||
int32_t *otherValue = other && std::holds_alternative<int32_t>(other->data)
|
||||
? &std::get<int32_t>(other->data)
|
||||
: nullptr;
|
||||
|
||||
// Check if the symbol already exists with a different value
|
||||
if (other && !(symValue && otherValue && *symValue == *otherValue)) {
|
||||
@@ -85,7 +87,7 @@ void sym_DumpLocalAliasedSymbols(std::string const &name) {
|
||||
fprintf(
|
||||
stderr,
|
||||
" A %s with that name is defined but not exported at ",
|
||||
local->data.holds<Label>() ? "label" : "constant"
|
||||
std::holds_alternative<Label>(local->data) ? "label" : "constant"
|
||||
);
|
||||
assume(local->src);
|
||||
local->src->dump(local->lineNo);
|
||||
|
||||
Reference in New Issue
Block a user