broader support for AST traversal, including Attr and Attributes objects

This commit is contained in:
Vern Paxson 2022-05-04 17:07:18 -07:00 committed by Tim Wojtulewicz
parent 9a2200e60a
commit a0fc8ca5e4
10 changed files with 280 additions and 23 deletions

View file

@ -274,6 +274,15 @@ unsigned int Type::MemoryAllocation() const
return padded_sizeof(*this);
}
detail::TraversalCode Type::Traverse(detail::TraversalCallback* cb) const
{
auto tc = cb->PreType(this);
HANDLE_TC_TYPE_PRE(tc);
tc = cb->PostType(this);
HANDLE_TC_TYPE_POST(tc);
}
bool TypeList::AllMatch(const Type* t, bool is_init) const
{
for ( const auto& type : types )
@ -340,6 +349,21 @@ unsigned int TypeList::MemoryAllocation() const
#pragma GCC diagnostic pop
}
detail::TraversalCode TypeList::Traverse(detail::TraversalCallback* cb) const
{
auto tc = cb->PreType(this);
HANDLE_TC_TYPE_PRE(tc);
for ( const auto& type : types )
{
tc = type->Traverse(cb);
HANDLE_TC_TYPE_PRE(tc);
}
tc = cb->PostType(this);
HANDLE_TC_TYPE_POST(tc);
}
int IndexType::MatchesIndex(detail::ListExpr* const index) const
{
// If we have a type indexed by subnets, addresses are ok.
@ -435,6 +459,27 @@ bool IndexType::IsSubNetIndex() const
return false;
}
detail::TraversalCode IndexType::Traverse(detail::TraversalCallback* cb) const
{
auto tc = cb->PreType(this);
HANDLE_TC_TYPE_PRE(tc);
for ( const auto& ind : GetIndexTypes() )
{
tc = ind->Traverse(cb);
HANDLE_TC_TYPE_PRE(tc);
}
if ( yield_type )
{
tc = yield_type->Traverse(cb);
HANDLE_TC_TYPE_PRE(tc);
}
tc = cb->PostType(this);
HANDLE_TC_TYPE_POST(tc);
}
static bool is_supported_index_type(const TypePtr& t, const char** tname)
{
if ( t->InternalType() != TYPE_INTERNAL_OTHER )
@ -865,6 +910,36 @@ std::optional<FuncType::Prototype> FuncType::FindPrototype(const RecordType& arg
return {};
}
detail::TraversalCode FuncType::Traverse(detail::TraversalCallback* cb) const
{
auto tc = cb->PreType(this);
HANDLE_TC_TYPE_PRE(tc);
tc = args->Traverse(cb);
HANDLE_TC_TYPE_PRE(tc);
if ( yield )
{
tc = yield->Traverse(cb);
HANDLE_TC_TYPE_PRE(tc);
}
tc = cb->PostType(this);
HANDLE_TC_TYPE_POST(tc);
}
detail::TraversalCode TypeType::Traverse(detail::TraversalCallback* cb) const
{
auto tc = cb->PreType(this);
HANDLE_TC_TYPE_PRE(tc);
tc = type->Traverse(cb);
HANDLE_TC_TYPE_PRE(tc);
tc = cb->PostType(this);
HANDLE_TC_TYPE_POST(tc);
}
TypeDecl::TypeDecl(const char* i, TypePtr t, detail::AttributesPtr arg_attrs)
: type(std::move(t)), attrs(std::move(arg_attrs)), id(i)
{
@ -1458,6 +1533,28 @@ string RecordType::GetFieldDeprecationWarning(int field, bool has_check) const
return "";
}
detail::TraversalCode RecordType::Traverse(detail::TraversalCallback* cb) const
{
auto tc = cb->PreType(this);
HANDLE_TC_TYPE_PRE(tc);
if ( types )
for ( const auto& td : *types )
{
tc = td->type->Traverse(cb);
HANDLE_TC_TYPE_PRE(tc);
if ( td->attrs )
{
tc = td->attrs->Traverse(cb);
HANDLE_TC_TYPE_PRE(tc);
}
}
tc = cb->PostType(this);
HANDLE_TC_TYPE_POST(tc);
}
FileType::FileType(TypePtr yield_type) : Type(TYPE_FILE), yield(std::move(yield_type)) { }
FileType::~FileType() = default;
@ -1476,6 +1573,18 @@ void FileType::DoDescribe(ODesc* d) const
}
}
detail::TraversalCode FileType::Traverse(detail::TraversalCallback* cb) const
{
auto tc = cb->PreType(this);
HANDLE_TC_TYPE_PRE(tc);
tc = yield->Traverse(cb);
HANDLE_TC_TYPE_PRE(tc);
tc = cb->PostType(this);
HANDLE_TC_TYPE_POST(tc);
}
OpaqueType::OpaqueType(const string& arg_name) : Type(TYPE_OPAQUE)
{
name = arg_name;
@ -1832,6 +1941,18 @@ void VectorType::DescribeReST(ODesc* d, bool roles_only) const
d->Add(util::fmt(":zeek:type:`%s`", yield_type->GetName().c_str()));
}
detail::TraversalCode VectorType::Traverse(detail::TraversalCallback* cb) const
{
auto tc = cb->PreType(this);
HANDLE_TC_TYPE_PRE(tc);
tc = yield_type->Traverse(cb);
HANDLE_TC_TYPE_PRE(tc);
tc = cb->PostType(this);
HANDLE_TC_TYPE_POST(tc);
}
// Returns true if t1 is initialization-compatible with t2 (i.e., if an
// initializer with type t1 can be used to initialize a value with type t2),
// false otherwise. Assumes that t1's tag is different from t2's. Note