mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-22 19:22:05 +00:00
Replace Either with std::variant (#1731)
This commit is contained in:
@@ -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