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