libexpr: store inherit source exprs in a list, not vec

it's a surprise tool that will help us later!

Change-Id: Ieac785b00b3d14e5937c03fb2c97c918f4646e56
This commit is contained in:
eldritch horrors
2025-05-01 14:28:05 +00:00
parent 558d921dac
commit 8b619b134d
4 changed files with 16 additions and 9 deletions
+8 -1
View File
@@ -130,12 +130,19 @@ void ExprAttrs::addBindingsToJSON(JSON & out, const SymbolTable & symbols) const
}
}
std::vector<Expr *> inheritFromExprs;
if (this->inheritFromExprs) {
for (auto & e : *this->inheritFromExprs) {
inheritFromExprs.push_back(e.get());
}
}
for (const auto & [from, syms] : inheritsFrom) {
JSON attrs = JSON::array();
for (auto sym : syms)
attrs.push_back(symbols[sym]);
out["inheritFrom"].push_back({
{"from", (*inheritFromExprs)[from]->toJSON(symbols)},
{"from", inheritFromExprs[from]->toJSON(symbols)},
{"attrs", attrs}
});
}
+3 -3
View File
@@ -151,10 +151,10 @@ struct ExprVar : Expr
*/
struct ExprInheritFrom : Expr
{
ref<Expr> fromExpr;
Expr & fromExpr;
Displacement displ;
ExprInheritFrom(PosIdx pos, Displacement displ, ref<Expr> fromExpr)
ExprInheritFrom(PosIdx pos, Displacement displ, Expr & fromExpr)
: Expr(pos), fromExpr(fromExpr), displ(displ)
{
}
@@ -238,7 +238,7 @@ struct ExprAttrs
};
typedef std::map<Symbol, AttrDef> AttrDefs;
AttrDefs attrs;
std::unique_ptr<std::vector<ref<Expr>>> inheritFromExprs;
std::unique_ptr<std::list<std::unique_ptr<Expr>>> inheritFromExprs;
struct DynamicAttrDef {
std::unique_ptr<Expr> nameExpr, valueExpr;
PosIdx pos;
+4 -4
View File
@@ -398,16 +398,16 @@ template<> struct BuildAST<grammar::v1::inherit> : change_head<InheritState> {
}
if (s.from != nullptr) {
if (!b.attrs.inheritFromExprs)
b.attrs.inheritFromExprs = std::make_unique<std::vector<ref<Expr>>>();
auto fromExpr = ref<Expr>::unsafeFromPtr(std::move(s.from));
b.attrs.inheritFromExprs->push_back(fromExpr);
b.attrs.inheritFromExprs = std::make_unique<std::list<std::unique_ptr<Expr>>>();
b.attrs.inheritFromExprs->push_back(std::move(s.from));
auto & fromExpr = b.attrs.inheritFromExprs->back();
for (auto & i : s.attrs) {
if (attrs.find(i.symbol) != attrs.end())
ps.dupAttr(i.symbol, i.pos, attrs[i.symbol].pos);
auto inheritFrom = std::make_unique<ExprInheritFrom>(
s.fromPos,
b.attrs.inheritFromExprs->size() - 1,
fromExpr
*fromExpr
);
attrs.emplace(
i.symbol,
+1 -1
View File
@@ -173,7 +173,7 @@ inline void State::addAttr(ExprAttrs * attrs, AttrPath && attrPath, std::unique_
auto * jAttrs = dynamic_cast<ExprSet *>(j->second.e.get());
if (jAttrs && ae) {
if (ae->inheritFromExprs && !jAttrs->inheritFromExprs)
jAttrs->inheritFromExprs = std::make_unique<std::vector<ref<Expr>>>();
jAttrs->inheritFromExprs = std::make_unique<std::list<std::unique_ptr<Expr>>>();
for (auto & ad : ae->attrs) {
auto j2 = jAttrs->attrs.find(ad.first);
if (j2 != jAttrs->attrs.end()) // Attr already defined in iAttrs, error.