Obj: Implement with_location_of() as template

This commit is contained in:
Arne Welzel 2024-02-06 10:45:49 +01:00
parent e5bb63c662
commit caa1c7493f
3 changed files with 8 additions and 44 deletions

View file

@ -22,8 +22,6 @@ namespace zeek {
template<class T>
class IntrusivePtr;
using ObjPtr = IntrusivePtr<Obj>;
namespace detail {
class Frame;
@ -1794,28 +1792,8 @@ 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

View file

@ -49,6 +49,13 @@ inline void set_location(const Location start, const Location end) {
end_location = end;
}
// Helper for updating e's location to the one used by o, returning e.
template<typename T, typename U>
T with_location_of(T e, const U& o) {
e->SetLocationInfo(o->GetLocationInfo());
return e;
}
} // namespace detail
class Obj {

View file

@ -780,25 +780,4 @@ protected:
int expected_len;
};
// Helper functions for setting the location of a statement (usually newly
// created) to match that of the associated object, returning the statement
// for convenience.
inline StmtPtr with_location_of(StmtPtr s, const Obj* o) {
s->SetLocationInfo(o->GetLocationInfo());
return s;
}
inline StmtPtr with_location_of(StmtPtr s, const ObjPtr& o) { return with_location_of(s, o.get()); }
// Versions that preserve the statement as a StmtList.
inline IntrusivePtr<StmtList> with_location_of(IntrusivePtr<StmtList> s, const ObjPtr& o) {
(void)with_location_of(s, o.get());
return s;
}
inline IntrusivePtr<StmtList> with_location_of(IntrusivePtr<StmtList> s, const Obj* o) {
s->SetLocationInfo(o->GetLocationInfo());
return s;
}
} // namespace zeek::detail