@@ -466,7 +466,7 @@ struct Formal
466466 Expr * def;
467467};
468468
469- struct Formals
469+ struct FormalsBuilder
470470{
471471 typedef std::vector<Formal> Formals_;
472472 /* *
@@ -483,26 +483,67 @@ struct Formals
483483 }
484484};
485485
486+ struct Formals
487+ {
488+ std::span<Formal> formals;
489+ bool ellipsis;
490+
491+ Formals (std::span<Formal> formals, bool ellipsis)
492+ : formals(formals)
493+ , ellipsis(ellipsis) {};
494+
495+ bool has (Symbol arg) const
496+ {
497+ auto it = std::lower_bound (
498+ formals.begin (), formals.end (), arg, [](const Formal & f, const Symbol & sym) { return f.name < sym; });
499+ return it != formals.end () && it->name == arg;
500+ }
501+
502+ std::vector<Formal> lexicographicOrder (const SymbolTable & symbols) const
503+ {
504+ std::vector<Formal> result (formals.begin (), formals.end ());
505+ std::sort (result.begin (), result.end (), [&](const Formal & a, const Formal & b) {
506+ std::string_view sa = symbols[a.name ], sb = symbols[b.name ];
507+ return sa < sb;
508+ });
509+ return result;
510+ }
511+ };
512+
486513struct ExprLambda : Expr
487514{
488515 PosIdx pos;
489516 Symbol name;
490517 Symbol arg;
491518
492- bool ellipsis;
519+ private:
493520 bool hasFormals;
521+ bool ellipsis;
494522 uint16_t nFormals;
495523 Formal * formalsStart;
524+ public:
525+
526+ std::optional<Formals> getFormals () const
527+ {
528+ if (hasFormals)
529+ return Formals{{formalsStart, nFormals}, ellipsis};
530+ else
531+ return std::nullopt ;
532+ }
496533
497534 Expr * body;
498535 DocComment docComment;
499536
500537 ExprLambda (
501- std::pmr::polymorphic_allocator<char > & alloc, PosIdx pos, Symbol arg, const Formals & formals, Expr * body)
538+ std::pmr::polymorphic_allocator<char > & alloc,
539+ PosIdx pos,
540+ Symbol arg,
541+ const FormalsBuilder & formals,
542+ Expr * body)
502543 : pos(pos)
503544 , arg(arg)
504- , ellipsis(formals.ellipsis)
505545 , hasFormals(true )
546+ , ellipsis(formals.ellipsis)
506547 , nFormals(formals.formals.size())
507548 , formalsStart(alloc.allocate_object<Formal>(nFormals))
508549 , body(body)
@@ -514,44 +555,22 @@ struct ExprLambda : Expr
514555 : pos(pos)
515556 , arg(arg)
516557 , hasFormals(false )
558+ , ellipsis(false )
559+ , nFormals(0 )
517560 , formalsStart(nullptr )
518561 , body(body) {};
519562
520- ExprLambda (std::pmr::polymorphic_allocator<char > & alloc, PosIdx pos, Formals formals, Expr * body)
563+ ExprLambda (std::pmr::polymorphic_allocator<char > & alloc, PosIdx pos, FormalsBuilder formals, Expr * body)
521564 : ExprLambda(alloc, pos, Symbol(), formals, body) {};
522565
523- bool hasFormal (Symbol arg) const
524- {
525- auto formals = getFormals ();
526- auto it = std::lower_bound (
527- formals.begin (), formals.end (), arg, [](const Formal & f, const Symbol & sym) { return f.name < sym; });
528- return it != formals.end () && it->name == arg;
529- }
530-
531566 void setName (Symbol name) override ;
532567 std::string showNamePos (const EvalState & state) const ;
533568
534- std::vector<Formal> getFormalsLexicographic (const SymbolTable & symbols) const
535- {
536- std::vector<Formal> result (getFormals ().begin (), getFormals ().end ());
537- std::sort (result.begin (), result.end (), [&](const Formal & a, const Formal & b) {
538- std::string_view sa = symbols[a.name ], sb = symbols[b.name ];
539- return sa < sb;
540- });
541- return result;
542- }
543-
544569 PosIdx getPos () const override
545570 {
546571 return pos;
547572 }
548573
549- std::span<Formal> getFormals () const
550- {
551- assert (hasFormals);
552- return {formalsStart, nFormals};
553- }
554-
555574 virtual void setDocComment (DocComment docComment) override ;
556575 COMMON_METHODS
557576};
0 commit comments