diff --git a/src/Expr.cc b/src/Expr.cc index 634918c557..29666c5b60 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -40,9 +40,7 @@ const char* expr_name(BroExprTag t) "()", "function()", "event", "schedule", "coerce", "record_coerce", "table_coerce", "vector_coerce", "sizeof", "cast", "is", "[:]=", - -#include "zeek/script_opt/ExprOpt-Names.h" - + "inline()", "nop", }; diff --git a/src/Expr.h b/src/Expr.h index 7662bac81d..d93d51597b 100644 --- a/src/Expr.h +++ b/src/Expr.h @@ -67,9 +67,7 @@ enum BroExprTag : int { EXPR_CAST, EXPR_IS, EXPR_INDEX_SLICE_ASSIGN, - -#include "zeek/script_opt/ExprOpt-Enums.h" - + EXPR_INLINE, EXPR_NOP, #define NUM_EXPRS (int(EXPR_NOP) + 1) @@ -196,7 +194,39 @@ public: virtual TraversalCode Traverse(TraversalCallback* cb) const = 0; -#include "zeek/script_opt/ExprOpt-Public.h" + // Returns a duplicate of the expression. + virtual ExprPtr Duplicate() = 0; + + // Recursively traverses the AST to inline eligible function calls. + virtual ExprPtr Inline(Inliner* inl) { return ThisPtr(); } + + // Access to the original expression from which this one is derived, + // or this one if we don't have an original. Returns a bare pointer + // rather than an ExprPtr to emphasize that the access is read-only. + const Expr* Original() const + { return original ? original->Original() : this; } + + // Designate the given Expr node as the original for this one. + void SetOriginal(ExprPtr _orig) + { + if ( ! original ) + original = std::move(_orig); + } + + // A convenience function for taking a newly-created Expr, + // making it point to us as the successor, and returning it. + // + // Takes an Expr* rather than a ExprPtr to de-clutter the calling + // code, which is always passing in "new XyzExpr(...)". This + // call, as a convenient side effect, transforms that bare pointer + // into an ExprPtr. + virtual ExprPtr SetSucc(Expr* succ) + { + succ->SetOriginal(ThisPtr()); + if ( IsParen() ) + succ->MarkParen(); + return {AdoptRef{}, succ}; + } protected: Expr() = default; @@ -223,7 +253,10 @@ protected: TypePtr type; bool paren; -#include "zeek/script_opt/ExprOpt-Private.h" + // The original expression from which this statement was + // derived, if any. Used as an aid for generating meaningful + // and correctly-localized error messages. + ExprPtr original = nullptr; }; class NameExpr final : public Expr { @@ -1100,7 +1133,30 @@ private: }; -#include "zeek/script_opt/ExprOpt-Subclasses.h" +class InlineExpr : public Expr { +public: + InlineExpr(ListExprPtr arg_args, IDPList* params, StmtPtr body, + int frame_offset, TypePtr ret_type); + + bool IsPure() const override; + + ListExprPtr Args() const { return args; } + StmtPtr Body() const { return body; } + + ValPtr Eval(Frame* f) const override; + + ExprPtr Duplicate() override; + + TraversalCode Traverse(TraversalCallback* cb) const override; + +protected: + void ExprDescribe(ODesc* d) const override; + + IDPList* params; + int frame_offset; + ListExprPtr args; + StmtPtr body; +}; inline Val* Expr::ExprVal() const diff --git a/src/StmtBase.h b/src/StmtBase.h index 1a7759e1f2..8f6b98f561 100644 --- a/src/StmtBase.h +++ b/src/StmtBase.h @@ -81,7 +81,37 @@ public: virtual TraversalCode Traverse(TraversalCallback* cb) const = 0; -#include "zeek/script_opt/StmtOpt-Public.h" + // Returns a duplicate of the statement. + virtual StmtPtr Duplicate() = 0; + + // Recursively traverses the AST to inline eligible function calls. + virtual void Inline(Inliner* inl) { } + + // Access to the original statement from which this one is derived, + // or this one if we don't have an original. Returns a bare pointer + // rather than a StmtPtr to emphasize that the access is read-only. + const Stmt* Original() const + { return original ? original->Original() : this; } + + // Designate the given Stmt node as the original for this one. + void SetOriginal(StmtPtr _orig) + { + if ( ! original ) + original = std::move(_orig); + } + + // A convenience function for taking a newly-created Stmt, + // making it point to us as the successor, and returning it. + // + // Takes a Stmt* rather than a StmtPtr to de-clutter the calling + // code, which is always passing in "new XyzStmt(...)". This + // call, as a convenient side effect, transforms that bare pointer + // into a StmtPtr. + virtual StmtPtr SetSucc(Stmt* succ) + { + succ->SetOriginal({NewRef{}, this}); + return {AdoptRef{}, succ}; + } protected: explicit Stmt(StmtTag arg_tag); @@ -97,7 +127,10 @@ protected: mutable double last_access; // time of last execution mutable uint32_t access_count; // number of executions -#include "zeek/script_opt/StmtOpt-Private.h" + // The original statement from which this statement was + // derived, if any. Used as an aid for generating meaningful + // and correctly-localized error messages. + StmtPtr original = nullptr; }; } // namespace zeek::detail diff --git a/src/script_opt/ExprOpt-Enums.h b/src/script_opt/ExprOpt-Enums.h deleted file mode 100644 index d9000d4875..0000000000 --- a/src/script_opt/ExprOpt-Enums.h +++ /dev/null @@ -1,5 +0,0 @@ -// See the file "COPYING" in the main distribution directory for copyright. - -// Enums associated with script optimization. - - EXPR_INLINE, diff --git a/src/script_opt/ExprOpt-Names.h b/src/script_opt/ExprOpt-Names.h deleted file mode 100644 index 30d361155c..0000000000 --- a/src/script_opt/ExprOpt-Names.h +++ /dev/null @@ -1,6 +0,0 @@ -// See the file "COPYING" in the main distribution directory for copyright. - -// Names of Expr subclasses associated with script optimization. -// Companion to script_opt/ExprOpt-Enums.h. - - "inline()", diff --git a/src/script_opt/ExprOpt-Private.h b/src/script_opt/ExprOpt-Private.h deleted file mode 100644 index 4065ac7f3f..0000000000 --- a/src/script_opt/ExprOpt-Private.h +++ /dev/null @@ -1,13 +0,0 @@ -// See the file "COPYING" in the main distribution directory for copyright. - -// Private (protected) Expr methods and member varibles associated -// with script optimization. See script_opt/ExprOpt-public.h for -// why these aren't factored into a separate class. -// -// Right now, this file is small, but it will grow as we expand into -// other forms of script optimization. - - // The original expression from which this statement was - // derived, if any. Used as an aid for generating meaningful - // and correctly-localized error messages. - ExprPtr original = nullptr; diff --git a/src/script_opt/ExprOpt-Public.h b/src/script_opt/ExprOpt-Public.h deleted file mode 100644 index 3384c780c6..0000000000 --- a/src/script_opt/ExprOpt-Public.h +++ /dev/null @@ -1,44 +0,0 @@ -// See the file "COPYING" in the main distribution directory for copyright. - -// Public Expr methods associated with script optimization. -// -// We can't effectively factor these out into a separate class to -// include via multiple inheritance, because in general they rely -// on other Expr methods or member variables, so to do so we'd -// have to (1) make all of the methods virtual, and (2) still -// include (re-)definitions for them in Expr.h, defeating most -// of the benefits of using a separate class. - - // Returns a duplicate of the expression. - virtual ExprPtr Duplicate() = 0; - - // Recursively traverses the AST to inline eligible function calls. - virtual ExprPtr Inline(Inliner* inl) { return ThisPtr(); } - - // Access to the original expression from which this one is derived, - // or this one if we don't have an original. Returns a bare pointer - // rather than an ExprPtr to emphasize that the access is read-only. - const Expr* Original() const - { return original ? original->Original() : this; } - - // Designate the given Expr node as the original for this one. - void SetOriginal(ExprPtr _orig) - { - if ( ! original ) - original = std::move(_orig); - } - - // A convenience function for taking a newly-created Expr, - // making it point to us as the successor, and returning it. - // - // Takes an Expr* rather than a ExprPtr to de-clutter the calling - // code, which is always passing in "new XyzExpr(...)". This - // call, as a convenient side effect, transforms that bare pointer - // into an ExprPtr. - virtual ExprPtr SetSucc(Expr* succ) - { - succ->SetOriginal(ThisPtr()); - if ( IsParen() ) - succ->MarkParen(); - return {AdoptRef{}, succ}; - } diff --git a/src/script_opt/ExprOpt-Subclasses.h b/src/script_opt/ExprOpt-Subclasses.h deleted file mode 100644 index e4193d6338..0000000000 --- a/src/script_opt/ExprOpt-Subclasses.h +++ /dev/null @@ -1,29 +0,0 @@ -// See the file "COPYING" in the main distribution directory for copyright. - -// Expr subclasses and associated functions associated with script -// optimization. - -class InlineExpr : public Expr { -public: - InlineExpr(ListExprPtr arg_args, IDPList* params, StmtPtr body, - int frame_offset, TypePtr ret_type); - - bool IsPure() const override; - - ListExprPtr Args() const { return args; } - StmtPtr Body() const { return body; } - - ValPtr Eval(Frame* f) const override; - - ExprPtr Duplicate() override; - - TraversalCode Traverse(TraversalCallback* cb) const override; - -protected: - void ExprDescribe(ODesc* d) const override; - - IDPList* params; - int frame_offset; - ListExprPtr args; - StmtPtr body; -}; diff --git a/src/script_opt/StmtOpt-Private.h b/src/script_opt/StmtOpt-Private.h deleted file mode 100644 index 80beffae98..0000000000 --- a/src/script_opt/StmtOpt-Private.h +++ /dev/null @@ -1,13 +0,0 @@ -// See the file "COPYING" in the main distribution directory for copyright. - -// Private (protected) Stmt methods and member varibles associated -// with script optimization. See script_opt/ExprOpt-public.h for -// why these aren't factored into a separate class. -// -// Right now, this file is small, but it will grow as we expand into -// other forms of script optimization. - - // The original statement from which this statement was - // derived, if any. Used as an aid for generating meaningful - // and correctly-localized error messages. - StmtPtr original = nullptr; diff --git a/src/script_opt/StmtOpt-Public.h b/src/script_opt/StmtOpt-Public.h deleted file mode 100644 index cdc6e2376c..0000000000 --- a/src/script_opt/StmtOpt-Public.h +++ /dev/null @@ -1,37 +0,0 @@ -// See the file "COPYING" in the main distribution directory for copyright. - -// Stmt methods and member varibles associated with script optimization. -// See script_opt/ExprOpt-public.h for why these aren't factored into a -// separate class. - - // Returns a duplicate of the statement. - virtual StmtPtr Duplicate() = 0; - - // Recursively traverses the AST to inline eligible function calls. - virtual void Inline(Inliner* inl) { } - - // Access to the original statement from which this one is derived, - // or this one if we don't have an original. Returns a bare pointer - // rather than a StmtPtr to emphasize that the access is read-only. - const Stmt* Original() const - { return original ? original->Original() : this; } - - // Designate the given Stmt node as the original for this one. - void SetOriginal(StmtPtr _orig) - { - if ( ! original ) - original = std::move(_orig); - } - - // A convenience function for taking a newly-created Stmt, - // making it point to us as the successor, and returning it. - // - // Takes a Stmt* rather than a StmtPtr to de-clutter the calling - // code, which is always passing in "new XyzStmt(...)". This - // call, as a convenient side effect, transforms that bare pointer - // into a StmtPtr. - virtual StmtPtr SetSucc(Stmt* succ) - { - succ->SetOriginal({NewRef{}, this}); - return {AdoptRef{}, succ}; - }