Avoid using std::get except in holds_alternative-asserting accessors

This commit is contained in:
Rangi42
2024-03-03 23:33:23 -05:00
parent 13904dc536
commit f2c875e71e
6 changed files with 23 additions and 8 deletions

View File

@@ -33,6 +33,9 @@ struct Symbol {
int32_t, // Constants just have a numeric value int32_t, // Constants just have a numeric value
Label // Label values refer to an offset within a specific section Label // Label values refer to an offset within a specific section
> data; > data;
Label &label();
Label const &label() const;
}; };
void sym_AddSymbol(Symbol &symbol); void sym_AddSymbol(Symbol &symbol);

View File

@@ -340,11 +340,11 @@ static void readSection(FILE *file, Section &section, char const *fileName,
static void linkSymToSect(Symbol &symbol, Section &section) static void linkSymToSect(Symbol &symbol, Section &section)
{ {
uint32_t a = 0, b = section.symbols.size(); uint32_t a = 0, b = section.symbols.size();
int32_t symbolOffset = std::get<Label>(symbol.data).offset; int32_t symbolOffset = symbol.label().offset;
while (a != b) { while (a != b) {
uint32_t c = (a + b) / 2; uint32_t c = (a + b) / 2;
int32_t otherOffset = std::get<Label>(section.symbols[c]->data).offset; int32_t otherOffset = section.symbols[c]->label().offset;
if (otherOffset > symbolOffset) if (otherOffset > symbolOffset)
b = c; b = c;

View File

@@ -347,7 +347,7 @@ static void writeSymBank(SortedSections const &bankSections, enum SectionType ty
if (!sym->name.empty() && canStartSymName(sym->name[0])) if (!sym->name.empty() && canStartSymName(sym->name[0]))
symList.push_back({ symList.push_back({
.sym = sym, .sym = sym,
.addr = (uint16_t)(std::get<Label>(sym->data).offset + sect->org) .addr = (uint16_t)(sym->label().offset + sect->org)
}); });
} }
}); });
@@ -417,7 +417,7 @@ static void writeMapBank(SortedSections const &sectList, enum SectionType type,
for (Symbol *sym : sect->symbols) for (Symbol *sym : sect->symbols)
// Space matches "\tSECTION: $xxxx ..." // Space matches "\tSECTION: $xxxx ..."
fprintf(mapFile, "\t $%04" PRIx32 " = %s\n", fprintf(mapFile, "\t $%04" PRIx32 " = %s\n",
std::get<Label>(sym->data).offset + org, sym->label().offset + org,
sym->name.c_str()); sym->name.c_str());
if (sect->nextu) { if (sect->nextu) {

View File

@@ -228,14 +228,14 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector<Symbol> const &fil
fileSymbols[value].name.c_str()); fileSymbols[value].name.c_str());
isError = true; isError = true;
value = 1; value = 1;
} else if (!std::holds_alternative<Label>(symbol->data)) { } else if (Label const *label = std::get_if<Label>(&symbol->data); label) {
value = label->section->bank;
} else {
error(patch.src, patch.lineNo, error(patch.src, patch.lineNo,
"Requested BANK() of non-label symbol \"%s\"", "Requested BANK() of non-label symbol \"%s\"",
fileSymbols[value].name.c_str()); fileSymbols[value].name.c_str());
isError = true; isError = true;
value = 1; value = 1;
} else {
value = std::get<Label>(symbol->data).section->bank;
} }
break; break;

View File

@@ -715,7 +715,7 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector<Symbol>
if (section->modifier == SECTION_FRAGMENT) { if (section->modifier == SECTION_FRAGMENT) {
// Add the fragment's offset to all of its symbols // Add the fragment's offset to all of its symbols
for (Symbol *symbol : section->symbols) for (Symbol *symbol : section->symbols)
std::get<Label>(symbol->data).offset += section->offset; symbol->label().offset += section->offset;
} }
} }

View File

@@ -16,6 +16,18 @@
std::map<std::string, Symbol *> symbols; std::map<std::string, Symbol *> symbols;
Label &Symbol::label()
{
assert(std::holds_alternative<Label>(data));
return std::get<Label>(data);
}
Label const &Symbol::label() const
{
assert(std::holds_alternative<Label>(data));
return std::get<Label>(data);
}
void sym_AddSymbol(Symbol &symbol) void sym_AddSymbol(Symbol &symbol)
{ {
// Check if the symbol already exists // Check if the symbol already exists