@@ -460,7 +460,7 @@ struct Formal
460460 Expr * def;
461461};
462462
463- struct Formals
463+ struct FormalsBuilder
464464{
465465 typedef std::vector<Formal> Formals_;
466466 /* *
@@ -477,26 +477,67 @@ struct Formals
477477 }
478478};
479479
480+ struct Formals
481+ {
482+ std::span<Formal> formals;
483+ bool ellipsis;
484+
485+ Formals (std::span<Formal> formals, bool ellipsis)
486+ : formals(formals)
487+ , ellipsis(ellipsis) {};
488+
489+ bool has (Symbol arg) const
490+ {
491+ auto it = std::lower_bound (
492+ formals.begin (), formals.end (), arg, [](const Formal & f, const Symbol & sym) { return f.name < sym; });
493+ return it != formals.end () && it->name == arg;
494+ }
495+
496+ std::vector<Formal> lexicographicOrder (const SymbolTable & symbols) const
497+ {
498+ std::vector<Formal> result (formals.begin (), formals.end ());
499+ std::sort (result.begin (), result.end (), [&](const Formal & a, const Formal & b) {
500+ std::string_view sa = symbols[a.name ], sb = symbols[b.name ];
501+ return sa < sb;
502+ });
503+ return result;
504+ }
505+ };
506+
480507struct ExprLambda : Expr
481508{
482509 PosIdx pos;
483510 Symbol name;
484511 Symbol arg;
485512
486- bool ellipsis;
513+ private:
487514 bool hasFormals;
515+ bool ellipsis;
488516 uint16_t nFormals;
489517 Formal * formalsStart;
518+ public:
519+
520+ std::optional<Formals> getFormals () const
521+ {
522+ if (hasFormals)
523+ return Formals{{formalsStart, nFormals}, ellipsis};
524+ else
525+ return std::nullopt ;
526+ }
490527
491528 Expr * body;
492529 DocComment docComment;
493530
494531 ExprLambda (
495- std::pmr::polymorphic_allocator<char > & alloc, PosIdx pos, Symbol arg, const Formals & formals, Expr * body)
532+ std::pmr::polymorphic_allocator<char > & alloc,
533+ PosIdx pos,
534+ Symbol arg,
535+ const FormalsBuilder & formals,
536+ Expr * body)
496537 : pos(pos)
497538 , arg(arg)
498- , ellipsis(formals.ellipsis)
499539 , hasFormals(true )
540+ , ellipsis(formals.ellipsis)
500541 , nFormals(formals.formals.size())
501542 , formalsStart(alloc.allocate_object<Formal>(nFormals))
502543 , body(body)
@@ -508,44 +549,22 @@ struct ExprLambda : Expr
508549 : pos(pos)
509550 , arg(arg)
510551 , hasFormals(false )
552+ , ellipsis(false )
553+ , nFormals(0 )
511554 , formalsStart(nullptr )
512555 , body(body) {};
513556
514- ExprLambda (std::pmr::polymorphic_allocator<char > & alloc, PosIdx pos, Formals formals, Expr * body)
557+ ExprLambda (std::pmr::polymorphic_allocator<char > & alloc, PosIdx pos, FormalsBuilder formals, Expr * body)
515558 : ExprLambda(alloc, pos, Symbol(), formals, body) {};
516559
517- bool hasFormal (Symbol arg) const
518- {
519- auto formals = getFormals ();
520- auto it = std::lower_bound (
521- formals.begin (), formals.end (), arg, [](const Formal & f, const Symbol & sym) { return f.name < sym; });
522- return it != formals.end () && it->name == arg;
523- }
524-
525560 void setName (Symbol name) override ;
526561 std::string showNamePos (const EvalState & state) const ;
527562
528- std::vector<Formal> getFormalsLexicographic (const SymbolTable & symbols) const
529- {
530- std::vector<Formal> result (getFormals ().begin (), getFormals ().end ());
531- std::sort (result.begin (), result.end (), [&](const Formal & a, const Formal & b) {
532- std::string_view sa = symbols[a.name ], sb = symbols[b.name ];
533- return sa < sb;
534- });
535- return result;
536- }
537-
538563 PosIdx getPos () const override
539564 {
540565 return pos;
541566 }
542567
543- std::span<Formal> getFormals () const
544- {
545- assert (hasFormals);
546- return {formalsStart, nFormals};
547- }
548-
549568 virtual void setDocComment (DocComment docComment) override ;
550569 COMMON_METHODS
551570};
0 commit comments