Skip to content

Commit 90ba96a

Browse files
committed
libexpr: use std::span rather than const std::vector &
1 parent 2d728f0 commit 90ba96a

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

src/libexpr/include/nix/expr/nixexpr.hh

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ struct ExprSelect : Expr
289289
std::pmr::polymorphic_allocator<char> & alloc,
290290
const PosIdx & pos,
291291
Expr * e,
292-
const std::span<const AttrName> & attrPath,
292+
std::span<const AttrName> attrPath,
293293
Expr * def)
294294
: pos(pos)
295295
, nAttrPath(attrPath.size())
@@ -339,7 +339,7 @@ struct ExprOpHasAttr : Expr
339339
Expr * e;
340340
std::span<AttrName> attrPath;
341341

342-
ExprOpHasAttr(std::pmr::polymorphic_allocator<char> & alloc, Expr * e, const std::vector<AttrName> & attrPath)
342+
ExprOpHasAttr(std::pmr::polymorphic_allocator<char> & alloc, Expr * e, std::span<AttrName> attrPath)
343343
: e(e)
344344
, attrPath({alloc.allocate_object<AttrName>(attrPath.size()), attrPath.size()})
345345
{
@@ -433,7 +433,7 @@ struct ExprList : Expr
433433
{
434434
std::span<Expr *> elems;
435435

436-
ExprList(std::pmr::polymorphic_allocator<char> & alloc, const std::vector<Expr *> & exprs)
436+
ExprList(std::pmr::polymorphic_allocator<char> & alloc, std::span<Expr *> exprs)
437437
: elems({alloc.allocate_object<Expr *>(exprs.size()), exprs.size()})
438438
{
439439
std::ranges::copy(exprs, elems.begin());
@@ -753,7 +753,19 @@ struct ExprConcatStrings : Expr
753753
std::pmr::polymorphic_allocator<char> & alloc,
754754
const PosIdx & pos,
755755
bool forceString,
756-
const std::vector<std::pair<PosIdx, Expr *>> & es)
756+
std::span<std::pair<PosIdx, Expr *>> es)
757+
: pos(pos)
758+
, forceString(forceString)
759+
, es({alloc.allocate_object<std::pair<PosIdx, Expr *>>(es.size()), es.size()})
760+
{
761+
std::ranges::copy(es, this->es.begin());
762+
};
763+
764+
ExprConcatStrings(
765+
std::pmr::polymorphic_allocator<char> & alloc,
766+
const PosIdx & pos,
767+
bool forceString,
768+
std::initializer_list<std::pair<PosIdx, Expr *>> es)
757769
: pos(pos)
758770
, forceString(forceString)
759771
, es({alloc.allocate_object<std::pair<PosIdx, Expr *>>(es.size()), es.size()})
@@ -833,7 +845,19 @@ public:
833845
add(std::pmr::polymorphic_allocator<char> & alloc,
834846
const PosIdx & pos,
835847
bool forceString,
836-
const std::vector<std::pair<PosIdx, Expr *>> & es)
848+
std::span<std::pair<PosIdx, Expr *>> es)
849+
requires(std::same_as<C, ExprConcatStrings>)
850+
{
851+
return alloc.new_object<C>(alloc, pos, forceString, es);
852+
}
853+
854+
template<class C>
855+
[[gnu::always_inline]]
856+
C *
857+
add(std::pmr::polymorphic_allocator<char> & alloc,
858+
const PosIdx & pos,
859+
bool forceString,
860+
std::initializer_list<std::pair<PosIdx, Expr *>> es)
837861
requires(std::same_as<C, ExprConcatStrings>)
838862
{
839863
return alloc.new_object<C>(alloc, pos, forceString, es);

src/libexpr/include/nix/expr/parser-state.hh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ struct ParserState
9696
ExprAttrs * attrs, AttrPath && attrPath, const ParserLocation & loc, Expr * e, const ParserLocation & exprLoc);
9797
void addAttr(ExprAttrs * attrs, AttrPath & attrPath, const Symbol & symbol, ExprAttrs::AttrDef && def);
9898
void validateFormals(FormalsBuilder & formals, PosIdx pos = noPos, Symbol arg = {});
99-
Expr *
100-
stripIndentation(const PosIdx pos, const std::vector<std::pair<PosIdx, std::variant<Expr *, StringToken>>> & es);
99+
Expr * stripIndentation(const PosIdx pos, std::span<std::pair<PosIdx, std::variant<Expr *, StringToken>>> es);
101100
PosIdx at(const ParserLocation & loc);
102101
};
103102

@@ -239,8 +238,8 @@ inline void ParserState::validateFormals(FormalsBuilder & formals, PosIdx pos, S
239238
{.msg = HintFmt("duplicate formal function argument '%1%'", symbols[arg]), .pos = positions[pos]});
240239
}
241240

242-
inline Expr * ParserState::stripIndentation(
243-
const PosIdx pos, const std::vector<std::pair<PosIdx, std::variant<Expr *, StringToken>>> & es)
241+
inline Expr *
242+
ParserState::stripIndentation(const PosIdx pos, std::span<std::pair<PosIdx, std::variant<Expr *, StringToken>>> es)
244243
{
245244
if (es.empty())
246245
return exprs.add<ExprString>(""_sds);

0 commit comments

Comments
 (0)