fixes & enhancements to location information associated w/ AST nodes & ZAM optimization

This commit is contained in:
Vern Paxson 2024-01-25 13:11:56 -08:00 committed by Arne Welzel
parent 7a283afe00
commit e5bb63c662
14 changed files with 299 additions and 259 deletions

View file

@ -22,6 +22,8 @@ namespace zeek {
template<class T>
class IntrusivePtr;
using ObjPtr = IntrusivePtr<Obj>;
namespace detail {
class Frame;
@ -365,17 +367,6 @@ public:
// Helper function to reduce boring code runs.
StmtPtr MergeStmts(StmtPtr s1, StmtPtr s2, StmtPtr s3 = nullptr) const;
// 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.
//
@ -384,19 +375,12 @@ public:
// call, as a convenient side effect, transforms that bare pointer
// into an ExprPtr.
virtual ExprPtr SetSucc(Expr* succ) {
succ->SetOriginal(ThisPtr());
succ->SetLocationInfo(GetLocationInfo());
if ( IsParen() )
succ->MarkParen();
return {AdoptRef{}, succ};
}
const detail::Location* GetLocationInfo() const override {
if ( original )
return original->GetLocationInfo();
else
return Obj::GetLocationInfo();
}
// Access script optimization information associated with
// this statement.
ExprOptInfo* GetOptInfo() const { return opt_info; }
@ -434,11 +418,6 @@ protected:
bool paren;
TypePtr type;
// 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;
// Information associated with the Expr for purposes of
// script optimization.
ExprOptInfo* opt_info;
@ -1593,11 +1572,12 @@ private:
class InlineExpr : public Expr {
public:
InlineExpr(ListExprPtr arg_args, std::vector<IDPtr> params, std::vector<bool> param_is_modified, StmtPtr body,
int frame_offset, TypePtr ret_type);
InlineExpr(ScriptFuncPtr sf, ListExprPtr arg_args, std::vector<IDPtr> params, std::vector<bool> param_is_modified,
StmtPtr body, int frame_offset, TypePtr ret_type);
bool IsPure() const override;
const ScriptFuncPtr& Func() const { return sf; }
ListExprPtr Args() const { return args; }
StmtPtr Body() const { return body; }
@ -1618,6 +1598,7 @@ protected:
std::vector<IDPtr> params;
std::vector<bool> param_is_modified;
int frame_offset;
ScriptFuncPtr sf;
ListExprPtr args;
StmtPtr body;
};
@ -1813,8 +1794,28 @@ inline bool is_vector(const ExprPtr& e) { return is_vector(e.get()); }
// True if the given Expr* has a list type
inline bool is_list(Expr* e) { return e->GetType()->Tag() == TYPE_LIST; }
inline bool is_list(const ExprPtr& e) { return is_list(e.get()); }
// Helper functions for setting the location of an expression (usually newly
// created) to match that of the associated object, returning the expression
// for convenience.
inline ExprPtr with_location_of(ExprPtr e, const Obj* o) {
e->SetLocationInfo(o->GetLocationInfo());
return e;
}
inline ExprPtr with_location_of(ExprPtr e, const ObjPtr& o) { return with_location_of(e, o.get()); }
// Versions that preserve the expression as a ListExpr.
inline ListExprPtr with_location_of(ListExprPtr e, const ObjPtr& o) {
(void)with_location_of(e, o.get());
return e;
}
inline ListExprPtr with_location_of(ListExprPtr e, const Obj* o) {
e->SetLocationInfo(o->GetLocationInfo());
return e;
}
} // namespace detail
} // namespace zeek