From cee92cbf6b2d4efd341d2d7209bf57b1a3e6384d Mon Sep 17 00:00:00 2001 From: Robin Sommer Date: Wed, 24 Apr 2024 10:59:21 +0200 Subject: [PATCH] Spicy: Cleanup some runtime code. --- src/spicy/runtime-support.h | 117 ++++++++++++++---------------------- 1 file changed, 45 insertions(+), 72 deletions(-) diff --git a/src/spicy/runtime-support.h b/src/spicy/runtime-support.h index 3d943cbb8a..95d08f219f 100644 --- a/src/spicy/runtime-support.h +++ b/src/spicy/runtime-support.h @@ -30,11 +30,7 @@ namespace zeek::spicy::rt { // Adapt to rename of exception. -#if SPICY_VERSION_NUMBER >= 10700 using UsageError = ::hilti::rt::UsageError; -#else -using UsageError = ::hilti::rt::UserException; -#endif /** * Exception thrown by event generation code if the value of an `$...` @@ -71,11 +67,11 @@ class TypeMismatch : public UsageError { public: TypeMismatch(const std::string_view& msg, std::string_view location = "") : UsageError(hilti::rt::fmt("Event parameter mismatch, %s", msg)) {} - TypeMismatch(const std::string_view& have, TypePtr want, std::string_view location = "") + TypeMismatch(const std::string_view& have, const TypePtr& want, std::string_view location = "") : TypeMismatch(_fmt(have, want)) {} private: - std::string _fmt(const std::string_view& have, TypePtr want) { + std::string _fmt(const std::string_view& have, const TypePtr& want) { ODesc d; want->Describe(&d); return hilti::rt::fmt("cannot convert Spicy value of type '%s' to Zeek value of type '%s'", have, @@ -456,38 +452,38 @@ hilti::rt::Time network_time(); // Forward-declare to_val() functions. template::value>* = nullptr> -ValPtr to_val(const T& t, TypePtr target); +ValPtr to_val(const T& t, const TypePtr& target); template -inline ValPtr to_val(const hilti::rt::Bitfield& v, TypePtr target); +inline ValPtr to_val(const hilti::rt::Bitfield& v, const TypePtr& target); template::value>* = nullptr> -ValPtr to_val(const T& t, TypePtr target); +ValPtr to_val(const T& t, const TypePtr& target); template::value>* = nullptr> -ValPtr to_val(const T& t, TypePtr target); +ValPtr to_val(const T& t, const TypePtr& target); template::value>* = nullptr> -ValPtr to_val(const T& t, TypePtr target); +ValPtr to_val(const T& t, const TypePtr& target); template -ValPtr to_val(const hilti::rt::Map& s, TypePtr target); +ValPtr to_val(const hilti::rt::Map& s, const TypePtr& target); template -ValPtr to_val(const hilti::rt::Set& s, TypePtr target); +ValPtr to_val(const hilti::rt::Set& s, const TypePtr& target); template -ValPtr to_val(const hilti::rt::Vector& v, TypePtr target); +ValPtr to_val(const hilti::rt::Vector& v, const TypePtr& target); template -ValPtr to_val(const std::optional& t, TypePtr target); +ValPtr to_val(const std::optional& t, const TypePtr& target); template -ValPtr to_val(const hilti::rt::DeferredExpression& t, TypePtr target); +ValPtr to_val(const hilti::rt::DeferredExpression& t, const TypePtr& target); template -ValPtr to_val(hilti::rt::integer::safe i, TypePtr target); +ValPtr to_val(hilti::rt::integer::safe i, const TypePtr& target); template -ValPtr to_val(const hilti::rt::ValueReference& t, TypePtr target); +ValPtr to_val(const hilti::rt::ValueReference& t, const TypePtr& target); -inline ValPtr to_val(const hilti::rt::Bool& b, TypePtr target); -inline ValPtr to_val(const hilti::rt::Address& d, TypePtr target); -inline ValPtr to_val(const hilti::rt::Bytes& b, TypePtr target); -inline ValPtr to_val(const hilti::rt::Interval& t, TypePtr target); -inline ValPtr to_val(const hilti::rt::Port& d, TypePtr target); -inline ValPtr to_val(const hilti::rt::Time& t, TypePtr target); -inline ValPtr to_val(const std::string& s, TypePtr target); -inline ValPtr to_val(double r, TypePtr target); +inline ValPtr to_val(const hilti::rt::Bool& b, const TypePtr& target); +inline ValPtr to_val(const hilti::rt::Address& d, const TypePtr& target); +inline ValPtr to_val(const hilti::rt::Bytes& b, const TypePtr& target); +inline ValPtr to_val(const hilti::rt::Interval& t, const TypePtr& target); +inline ValPtr to_val(const hilti::rt::Port& d, const TypePtr& target); +inline ValPtr to_val(const hilti::rt::Time& t, const TypePtr& target); +inline ValPtr to_val(const std::string& s, const TypePtr& target); +inline ValPtr to_val(double r, const TypePtr& target); /** * Converts a Spicy-side optional value to a Zeek value. This assumes the @@ -495,7 +491,7 @@ inline ValPtr to_val(double r, TypePtr target); * returned with ref count +1. */ template -inline ValPtr to_val(const std::optional& t, TypePtr target) { +inline ValPtr to_val(const std::optional& t, const TypePtr& target) { if ( t.has_value() ) return to_val(hilti::rt::optional::value(t), target); @@ -509,7 +505,7 @@ inline ValPtr to_val(const std::optional& t, TypePtr target) { * picks up on). */ template -inline ValPtr to_val(const hilti::rt::DeferredExpression& t, TypePtr target) { +inline ValPtr to_val(const hilti::rt::DeferredExpression& t, const TypePtr& target) { try { return to_val(t(), target); } catch ( const hilti::rt::AttributeNotSet& ) { @@ -521,7 +517,7 @@ inline ValPtr to_val(const hilti::rt::DeferredExpression& t, TypePtr target) * Converts a Spicy-side string to a Zeek value. The result is returned with * ref count +1. */ -inline ValPtr to_val(const std::string& s, TypePtr target) { +inline ValPtr to_val(const std::string& s, const TypePtr& target) { if ( target->Tag() != TYPE_STRING ) throw TypeMismatch("string", target); @@ -532,7 +528,7 @@ inline ValPtr to_val(const std::string& s, TypePtr target) { * Converts a Spicy-side bytes instance to a Zeek value. The result is returned with * ref count +1. */ -inline ValPtr to_val(const hilti::rt::Bytes& b, TypePtr target) { +inline ValPtr to_val(const hilti::rt::Bytes& b, const TypePtr& target) { if ( target->Tag() != TYPE_STRING ) throw TypeMismatch("string", target); @@ -544,7 +540,7 @@ inline ValPtr to_val(const hilti::rt::Bytes& b, TypePtr target) { * returned with ref count +1. */ template -inline ValPtr to_val(hilti::rt::integer::safe i, TypePtr target) { +inline ValPtr to_val(hilti::rt::integer::safe i, const TypePtr& target) { ValPtr v = nullptr; if constexpr ( std::is_unsigned::value ) { if ( target->Tag() == TYPE_COUNT ) @@ -571,7 +567,7 @@ inline ValPtr to_val(hilti::rt::integer::safe i, TypePtr target) { } template -ValPtr to_val(const hilti::rt::ValueReference& t, TypePtr target) { +ValPtr to_val(const hilti::rt::ValueReference& t, const TypePtr& target) { if ( auto* x = t.get() ) return to_val(*x, target); @@ -582,7 +578,7 @@ ValPtr to_val(const hilti::rt::ValueReference& t, TypePtr target) { * Converts a Spicy-side signed bool to a Zeek value. The result is * returned with ref count +1. */ -inline ValPtr to_val(const hilti::rt::Bool& b, TypePtr target) { +inline ValPtr to_val(const hilti::rt::Bool& b, const TypePtr& target) { if ( target->Tag() != TYPE_BOOL ) throw TypeMismatch("bool", target); @@ -593,7 +589,7 @@ inline ValPtr to_val(const hilti::rt::Bool& b, TypePtr target) { * Converts a Spicy-side real to a Zeek value. The result is returned with * ref count +1. */ -inline ValPtr to_val(double r, TypePtr target) { +inline ValPtr to_val(double r, const TypePtr& target) { if ( target->Tag() != TYPE_DOUBLE ) throw TypeMismatch("double", target); @@ -604,7 +600,7 @@ inline ValPtr to_val(double r, TypePtr target) { * Converts a Spicy-side address to a Zeek value. The result is returned with * ref count +1. */ -inline ValPtr to_val(const hilti::rt::Address& d, TypePtr target) { +inline ValPtr to_val(const hilti::rt::Address& d, const TypePtr& target) { if ( target->Tag() != TYPE_ADDR ) throw TypeMismatch("addr", target); @@ -621,17 +617,11 @@ inline ValPtr to_val(const hilti::rt::Address& d, TypePtr target) { * Converts a Spicy-side address to a Zeek value. The result is returned with * ref count +1. */ -inline ValPtr to_val(const hilti::rt::Port& p, TypePtr target) { +inline ValPtr to_val(const hilti::rt::Port& p, const TypePtr& target) { if ( target->Tag() != TYPE_PORT ) throw TypeMismatch("port", target); -#if SPICY_VERSION_NUMBER >= 10700 - auto proto = p.protocol().value(); -#else - auto proto = p.protocol(); -#endif - - switch ( proto ) { + switch ( p.protocol().value() ) { case hilti::rt::Protocol::TCP: return val_mgr->Port(p.port(), ::TransportProto::TRANSPORT_TCP); case hilti::rt::Protocol::UDP: return val_mgr->Port(p.port(), ::TransportProto::TRANSPORT_UDP); @@ -646,7 +636,7 @@ inline ValPtr to_val(const hilti::rt::Port& p, TypePtr target) { * Converts a Spicy-side time to a Zeek value. The result is returned with * ref count +1. */ -inline ValPtr to_val(const hilti::rt::Interval& i, TypePtr target) { +inline ValPtr to_val(const hilti::rt::Interval& i, const TypePtr& target) { if ( target->Tag() != TYPE_INTERVAL ) throw TypeMismatch("interval", target); @@ -657,7 +647,7 @@ inline ValPtr to_val(const hilti::rt::Interval& i, TypePtr target) { * Converts a Spicy-side time to a Zeek value. The result is returned with * ref count +1. */ -inline ValPtr to_val(const hilti::rt::Time& t, TypePtr target) { +inline ValPtr to_val(const hilti::rt::Time& t, const TypePtr& target) { if ( target->Tag() != TYPE_TIME ) throw TypeMismatch("time", target); @@ -669,7 +659,7 @@ inline ValPtr to_val(const hilti::rt::Time& t, TypePtr target) { * ref count +1. */ template -inline ValPtr to_val(const hilti::rt::Vector& v, TypePtr target) { +inline ValPtr to_val(const hilti::rt::Vector& v, const TypePtr& target) { if ( target->Tag() != TYPE_VECTOR && target->Tag() != TYPE_LIST ) throw TypeMismatch("expected vector or list", target); @@ -686,7 +676,7 @@ inline ValPtr to_val(const hilti::rt::Vector& v, TypePtr target) { * ref count +1. */ template -inline ValPtr to_val(const hilti::rt::Map& m, TypePtr target) { +inline ValPtr to_val(const hilti::rt::Map& m, const TypePtr& target) { if constexpr ( hilti::rt::is_tuple::value ) throw TypeMismatch("internal error: sets with tuples not yet supported in to_val()"); @@ -716,7 +706,7 @@ inline ValPtr to_val(const hilti::rt::Map& m, TypePtr target) { * ref count +1. */ template -inline ValPtr to_val(const hilti::rt::Set& s, TypePtr target) { +inline ValPtr to_val(const hilti::rt::Set& s, const TypePtr& target) { if ( target->Tag() != TYPE_TABLE ) throw TypeMismatch("set", target); @@ -822,7 +812,7 @@ inline void set_record_field(RecordVal* rval, const IntrusivePtr& rt * with ref count +1. */ template::value>*> -inline ValPtr to_val(const T& t, TypePtr target) { +inline ValPtr to_val(const T& t, const TypePtr& target) { if ( target->Tag() != TYPE_RECORD ) throw TypeMismatch("tuple", target); @@ -843,7 +833,7 @@ inline ValPtr to_val(const T& t, TypePtr target) { * with ref count +1. */ template -inline ValPtr to_val(const hilti::rt::Bitfield& v, TypePtr target) { +inline ValPtr to_val(const hilti::rt::Bitfield& v, const TypePtr& target) { using Bitfield = hilti::rt::Bitfield; if ( target->Tag() != TYPE_RECORD ) @@ -876,7 +866,7 @@ constexpr bool is_optional = is_optional_impl::value>*> -inline ValPtr to_val(const T& t, TypePtr target) { +inline ValPtr to_val(const T& t, const TypePtr& target) { if ( target->Tag() != TYPE_RECORD ) throw TypeMismatch("struct", target); @@ -944,35 +934,18 @@ inline ValPtr to_val_for_transport_proto(int64_t val, const TypePtr& target) { } /** - * Converts a Spicy-side enum to a Zeek record value. The result is returned + * Converts a Spicy-side enum to a Zeek enum value. The result is returned * with ref count +1. */ template::value>*> -inline ValPtr to_val(const T& t, TypePtr target) { -#if SPICY_VERSION_NUMBER >= 10700 - auto proto = typename T::Value(t.value()); -#else - auto proto = t; -#endif - - return to_val(proto, target); -} - -/** - * Converts a C++ Spicy-side enum to a Zeek record value. The result is returned - * with ref count +1. This specialization is provided for compatibility with ::value>*> -inline ValPtr to_val(const T& t, TypePtr target) { +inline ValPtr to_val(const T& t, const TypePtr& target) { if ( target->Tag() != TYPE_ENUM ) throw TypeMismatch("enum", target); // We'll usually be getting an int64_t for T, but allow other signed ints // as well. - static_assert(std::is_signed>{}); - auto it = static_cast(t); + static_assert(std::is_signed>{}); + auto it = static_cast(t.value()); // Special case: map enum values to Zeek's semantics. if ( target->GetName() == "transport_proto" ) {