aboutsummaryrefslogtreecommitdiff
path: root/src/libexpr/nixexpr.cc
diff options
context:
space:
mode:
authorpennae <github@quasiparticle.net>2022-04-22 21:45:39 +0200
committerpennae <github@quasiparticle.net>2022-04-25 15:37:01 +0200
commita385e51a086006c0f7d7c4bc6ed910dbe5375c4d (patch)
tree2b9776ddd7fa0977827e21379cd138cc54943071 /src/libexpr/nixexpr.cc
parent7f814d6d9af9d78f922d59115a94078f807676a8 (diff)
rename SymbolIdx -> Symbol, Symbol -> SymbolStr
after #6218 `Symbol` no longer confers a uniqueness invariant on the string it wraps, it is now possible to create multiple symbols that compare equal but whose string contents have different addresses. this guarantee is now only provided by `SymbolIdx`, leaving `Symbol` only as a string wrapper that knows about the intricacies of how symbols need to be formatted for output. this change renames `SymbolIdx` to `Symbol` to restore the previous semantics of `Symbol` to that name. we also keep the wrapper type and rename it to `SymbolStr` instead of returning plain strings from lookups into the symbol table because symbols are formatted for output in many places. theoretically we do not need `SymbolStr`, only a function that formats a string for output as a symbol, but having to wrap every symbol that appears in a message into eg `formatSymbol()` is error-prone and inconvient.
Diffstat (limited to 'src/libexpr/nixexpr.cc')
-rw-r--r--src/libexpr/nixexpr.cc19
1 files changed, 8 insertions, 11 deletions
diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc
index 9fee3cb58..c529fdc89 100644
--- a/src/libexpr/nixexpr.cc
+++ b/src/libexpr/nixexpr.cc
@@ -24,8 +24,10 @@ static void showString(std::ostream & str, std::string_view s)
str << '"';
}
-static void showId(std::ostream & str, std::string_view s)
+std::ostream & operator <<(std::ostream & str, const SymbolStr & symbol)
{
+ std::string_view s = symbol;
+
if (s.empty())
str << "\"\"";
else if (s == "if") // FIXME: handle other keywords
@@ -34,7 +36,7 @@ static void showId(std::ostream & str, std::string_view s)
char c = s[0];
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_')) {
showString(str, s);
- return;
+ return str;
}
for (auto c : s)
if (!((c >= 'a' && c <= 'z') ||
@@ -42,15 +44,10 @@ static void showId(std::ostream & str, std::string_view s)
(c >= '0' && c <= '9') ||
c == '_' || c == '\'' || c == '-')) {
showString(str, s);
- return;
+ return str;
}
str << s;
}
-}
-
-std::ostream & operator << (std::ostream & str, const Symbol & sym)
-{
- showId(str, sym.s);
return str;
}
@@ -499,12 +496,12 @@ void ExprPos::bindVars(const EvalState & es, const StaticEnv & env)
/* Storing function names. */
-void Expr::setName(SymbolIdx name)
+void Expr::setName(Symbol name)
{
}
-void ExprLambda::setName(SymbolIdx name)
+void ExprLambda::setName(Symbol name)
{
this->name = name;
body->setName(name);
@@ -526,7 +523,7 @@ std::string ExprLambda::showNamePos(const EvalState & state) const
size_t SymbolTable::totalSize() const
{
size_t n = 0;
- dump([&] (const Symbol & s) { n += std::string_view(s).size(); });
+ dump([&] (const std::string & s) { n += s.size(); });
return n;
}