- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 1.8k
 
libexpr: store ExprLambda data in Expr::alloc #14384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3a3c062
              4a80c92
              e438888
              34f780d
              67be2df
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -466,7 +466,7 @@ struct Formal | |
| Expr * def; | ||
| }; | ||
| 
     | 
||
| struct Formals | ||
| struct FormalsBuilder | ||
| { | ||
| typedef std::vector<Formal> Formals_; | ||
| /** | ||
| 
        
          
        
         | 
    @@ -481,6 +481,23 @@ struct Formals | |
| formals.begin(), formals.end(), arg, [](const Formal & f, const Symbol & sym) { return f.name < sym; }); | ||
| return it != formals.end() && it->name == arg; | ||
| } | ||
| }; | ||
| 
     | 
||
| struct Formals | ||
| { | ||
| std::span<Formal> formals; | ||
| bool ellipsis; | ||
| 
     | 
||
| Formals(std::span<Formal> formals, bool ellipsis) | ||
| : formals(formals) | ||
| , ellipsis(ellipsis) {}; | ||
| 
     | 
||
| bool has(Symbol arg) const | ||
| { | ||
| auto it = std::lower_bound( | ||
| formals.begin(), formals.end(), arg, [](const Formal & f, const Symbol & sym) { return f.name < sym; }); | ||
| return it != formals.end() && it->name == arg; | ||
| } | ||
| 
     | 
||
| std::vector<Formal> lexicographicOrder(const SymbolTable & symbols) const | ||
| { | ||
| 
        
          
        
         | 
    @@ -498,31 +515,57 @@ struct ExprLambda : Expr | |
| PosIdx pos; | ||
| Symbol name; | ||
| Symbol arg; | ||
| Formals * formals; | ||
| 
     | 
||
| private: | ||
| bool hasFormals; | ||
| bool ellipsis; | ||
| uint16_t nFormals; | ||
| Formal * formalsStart; | ||
| public: | ||
| 
     | 
||
| std::optional<Formals> getFormals() const | ||
| { | ||
| if (hasFormals) | ||
| return Formals{{formalsStart, nFormals}, ellipsis}; | ||
| else | ||
| return std::nullopt; | ||
| } | ||
| 
     | 
||
| Expr * body; | ||
| DocComment docComment; | ||
| 
     | 
||
| ExprLambda(PosIdx pos, Symbol arg, Formals * formals, Expr * body) | ||
| ExprLambda( | ||
| std::pmr::polymorphic_allocator<char> & alloc, | ||
| PosIdx pos, | ||
| Symbol arg, | ||
| const FormalsBuilder & formals, | ||
| Expr * body) | ||
| : pos(pos) | ||
| , arg(arg) | ||
| , formals(formals) | ||
| , body(body) {}; | ||
| 
     | 
||
| ExprLambda(PosIdx pos, Formals * formals, Expr * body) | ||
| : pos(pos) | ||
| , formals(formals) | ||
| , hasFormals(true) | ||
| , ellipsis(formals.ellipsis) | ||
| , nFormals(formals.formals.size()) | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can do a silent truncation from size_t -> uint16_t.  | 
||
| , formalsStart(alloc.allocate_object<Formal>(nFormals)) | ||
| , body(body) | ||
| { | ||
| } | ||
| std::ranges::copy(formals.formals, formalsStart); | ||
| 
         
      Comment on lines
    
      -516
     to 
      +551
    
   
  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should have a check in case there a more than 65k formals. We must fail gracefully. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would overflow the heap buffer otherwise. Let's please not do this.  | 
||
| }; | ||
| 
     | 
||
| ExprLambda(PosIdx pos, Symbol arg, Expr * body) | ||
| : pos(pos) | ||
| , arg(arg) | ||
| , hasFormals(false) | ||
| , ellipsis(false) | ||
| , nFormals(0) | ||
| , formalsStart(nullptr) | ||
| , body(body) {}; | ||
| 
     | 
||
| ExprLambda(std::pmr::polymorphic_allocator<char> & alloc, PosIdx pos, FormalsBuilder formals, Expr * body) | ||
| : ExprLambda(alloc, pos, Symbol(), formals, body) {}; | ||
| 
     | 
||
| void setName(Symbol name) override; | ||
| std::string showNamePos(const EvalState & state) const; | ||
| 
     | 
||
| inline bool hasFormals() const | ||
| { | ||
| return formals != nullptr; | ||
| } | ||
| 
     | 
||
| PosIdx getPos() const override | ||
| { | ||
| return pos; | ||
| 
          
            
          
           | 
    ||
Uh oh!
There was an error while loading. Please reload this page.