Separate union members for EQUS and MACROs

This commit is contained in:
Rangi42
2024-02-19 08:39:57 -05:00
parent 9cdd0b8a02
commit bc8fd8a6dc
2 changed files with 12 additions and 8 deletions

View File

@@ -37,12 +37,16 @@ struct Symbol {
// If sym_IsNumeric // If sym_IsNumeric
int32_t value; int32_t value;
int32_t (*numCallback)(void); int32_t (*numCallback)(void);
// For SYM_MACRO and SYM_EQUS; TODO: have separate fields // For SYM_MACRO
struct { struct {
size_t size; size_t size;
char *value; char *value;
} macro; } macro;
// For SYM_EQUS // For SYM_EQUS
struct {
size_t size;
char *value;
} equs;
char const *(*strCallback)(void); char const *(*strCallback)(void);
}; };
@@ -97,7 +101,7 @@ static inline char const *sym_GetStringValue(struct Symbol const *sym)
{ {
if (sym->hasCallback) if (sym->hasCallback)
return sym->strCallback(); return sym->strCallback();
return sym->macro.value; return sym->equs.value;
} }
void sym_ForEach(void (*func)(struct Symbol *, void *), void *arg); void sym_ForEach(void (*func)(struct Symbol *, void *), void *arg);

View File

@@ -164,8 +164,8 @@ static void assignStringSymbol(struct Symbol *sym, char const *value)
fatalerror("No memory for string equate: %s\n", strerror(errno)); fatalerror("No memory for string equate: %s\n", strerror(errno));
sym->type = SYM_EQUS; sym->type = SYM_EQUS;
sym->macro.value = string; sym->equs.value = string;
sym->macro.size = strlen(string); sym->equs.size = strlen(string);
} }
struct Symbol *sym_FindExactSymbol(char const *symName) struct Symbol *sym_FindExactSymbol(char const *symName)
@@ -233,8 +233,8 @@ void sym_Purge(char const *symName)
if (sym->name == labelScope) if (sym->name == labelScope)
sym_SetCurrentSymbolScope(NULL); sym_SetCurrentSymbolScope(NULL);
// FIXME: this leaks sym->macro.value for SYM_EQUS and SYM_MACRO, but this can't // FIXME: this leaks sym->equs.value for SYM_EQUS and sym->macro.value for SYM_MACRO,
// free(sym->macro.value) because the expansion may be purging itself. // but this can't free either of them because the expansion may be purging itself.
hash_RemoveElement(symbols, sym->name); hash_RemoveElement(symbols, sym->name);
// TODO: ideally, also unref the file stack nodes // TODO: ideally, also unref the file stack nodes
free(sym); free(sym);
@@ -401,8 +401,8 @@ struct Symbol *sym_RedefString(char const *symName, char const *value)
} }
updateSymbolFilename(sym); updateSymbolFilename(sym);
// FIXME: this leaks the previous sym->macro.value value, but this can't // FIXME: this leaks the previous sym->equs.value, but this can't free(sym->equs.value)
// free(sym->macro.value) because the expansion may be redefining itself. // because the expansion may be redefining itself.
assignStringSymbol(sym, value); assignStringSymbol(sym, value);
return sym; return sym;