<commit_before>
#include "Obj.h"
#include "Type.h"
#include "Plugin.h"
#undef List

#include <hilti/builder/builder.h>
#include <binpac/type.h>

#include "Converter.h"
#include "RuntimeInterface.h"
#include "LocalReporter.h"
#include "compiler/ConversionBuilder.h"
#include "compiler/ModuleBuilder.h"

using namespace bro::hilti;

TypeConverter::TypeConverter()
	{
	}

BroType* TypeConverter::Convert(std::shared_ptr<::hilti::Type> type, std::shared_ptr<::binpac::Type> btype)
	{
	setArg1(btype);
	BroType* rtype;
	processOne(type, &rtype);
	return rtype;
	}

string TypeConverter::CacheIndex(std::shared_ptr<::hilti::Type> type, std::shared_ptr<::binpac::Type> btype)
	{
	return ::util::fmt("%p-%p", type, type);
        }

BroType* TypeConverter::LookupCachedType(std::shared_ptr<::hilti::Type> type, std::shared_ptr<::binpac::Type> btype)
	{
        auto i = type_cache.find(CacheIndex(type, btype));
	return i != type_cache.end() ? i->second.first : nullptr;
        }

uint64_t TypeConverter::TypeIndex(std::shared_ptr<::hilti::Type> type, std::shared_ptr<::binpac::Type> btype)
	{
	Convert(type, btype); // Make sure we have the type cached.
	auto i = type_cache.find(CacheIndex(type, btype));
	assert(i != type_cache.end());
	return i->second.second;
        }

void TypeConverter::CacheType(std::shared_ptr<::hilti::Type> type, std::shared_ptr<::binpac::Type> btype, BroType* bro_type)
	{
	auto cache_idx = CacheIndex(type, btype);
	auto type_idx = lib_bro_add_indexed_type(bro_type);
	type_cache.insert(std::make_pair(cache_idx, std::make_pair(bro_type, type_idx)));
	}

ValueConverter::ValueConverter(compiler::ModuleBuilder* arg_mbuilder,
			       TypeConverter* arg_type_converter)
	{
	mbuilder = arg_mbuilder;
	type_converter = arg_type_converter;
	}

bool ValueConverter::Convert(shared_ptr<::hilti::Expression> value, shared_ptr<::hilti::Expression> dst, std::shared_ptr<::binpac::Type> btype, BroType* hint)
	{
	_bro_type_hints.push_back(hint);
	setArg1(value);
	setArg2(dst);
	_arg3 = btype;
	bool set = false;
	bool success = processOne(value->type(), &set);
	assert(set);
	_arg3 = nullptr;
	_bro_type_hints.pop_back();
	return success;
	}

shared_ptr<::binpac::Type> ValueConverter::arg3() const
	{
	return _arg3;
	}

shared_ptr<::hilti::builder::BlockBuilder> ValueConverter::Builder() const
	{
	return mbuilder->builder();
	}

void TypeConverter::visit(::hilti::type::Reference* b)
	{
	BroType* rtype;
	processOne(b->argType(), &rtype);
	setResult(rtype);
	}

void TypeConverter::visit(::hilti::type::Address* a)
	{
	auto result = base_type(TYPE_ADDR);
	setResult(result);
	}

void TypeConverter::visit(::hilti::type::Bool* a)
	{
	auto result = base_type(TYPE_BOOL);
	setResult(result);
	}

void TypeConverter::visit(::hilti::type::Bytes* b)
	{
	auto result = base_type(TYPE_STRING);
	setResult(result);
	}

void TypeConverter::visit(::hilti::type::Double* d)
	{
	auto result = base_type(TYPE_DOUBLE);
	setResult(result);
	}

void TypeConverter::visit(::hilti::type::Enum* e)
	{
	auto etype = ast::checkedCast<binpac::type::Enum>(arg1());

	if ( auto result = LookupCachedType(e->sharedPtr<::hilti::type::Enum>(), etype) )
		{
		setResult(result);
		return;
		}

	if ( ! etype->id() )
		reporter::internal_error("TypeConverter::visit(Enum): type has not ID");

	string module;
	string name;
	auto c = etype->id()->path();

	if ( c.size() == 1 )
		{
		name = c.front();

		if ( auto m = etype->id()->firstParent<binpac::Module>() )
			module = m->id()->pathAsString();
		}

	else if ( c.size() == 2 )
		{
		module = c.front();
		name = c.back();
		}

	if ( module.empty() || name.empty() )
                reporter::internal_error(::util::fmt("TypeConverter::visit(Enum): cannot determine type's module/name (%s/%s|%s)",
						     module.size(), name.size(), etype->id()->pathAsString().c_str()));

	// Build the Bro type.
	auto eresult = new EnumType(name);
	eresult->AddName(module, "Undef", lib_bro_enum_undef_val, true);

	for ( auto l : etype->labels() )
		{
		auto name = l.first->name();
		if ( name != "Undef" )
			eresult->AddName(module, name.c_str(), l.second, true);
		}

	PLUGIN_DBG_LOG(HiltiPlugin, "Adding Bro enum type %s::%s", module.c_str(), name.c_str());
	string id_name = ::util::fmt("%s::%s", module, name);
	::ID* id = install_ID(name.c_str(), module.c_str(), true, true);
	id->SetType(eresult);
	id->MakeType();
<commit_msg>Fixing pac2/double-types.bro.<commit_after>
#include "Obj.h"
#include "Type.h"
#include "Plugin.h"
#undef List

#include <hilti/builder/builder.h>
#include <binpac/type.h>

#include "Converter.h"
#include "RuntimeInterface.h"
#include "LocalReporter.h"
#include "compiler/ConversionBuilder.h"
#include "compiler/ModuleBuilder.h"

using namespace bro::hilti;

TypeConverter::TypeConverter()
	{
	}

BroType* TypeConverter::Convert(std::shared_ptr<::hilti::Type> type, std::shared_ptr<::binpac::Type> btype)
	{
	setArg1(btype);
	BroType* rtype;
	processOne(type, &rtype);
	return rtype;
	}

string TypeConverter::CacheIndex(std::shared_ptr<::hilti::Type> type, std::shared_ptr<::binpac::Type> btype)
	{
	return ::util::fmt("%p-%p", type, type);
        }

BroType* TypeConverter::LookupCachedType(std::shared_ptr<::hilti::Type> type, std::shared_ptr<::binpac::Type> btype)
	{
        auto i = type_cache.find(CacheIndex(type, btype));
	return i != type_cache.end() ? i->second.first : nullptr;
        }

uint64_t TypeConverter::TypeIndex(std::shared_ptr<::hilti::Type> type, std::shared_ptr<::binpac::Type> btype)
	{
	Convert(type, btype); // Make sure we have the type cached.
	auto i = type_cache.find(CacheIndex(type, btype));
	assert(i != type_cache.end());
	return i->second.second;
        }

void TypeConverter::CacheType(std::shared_ptr<::hilti::Type> type, std::shared_ptr<::binpac::Type> btype, BroType* bro_type)
	{
	auto cache_idx = CacheIndex(type, btype);
	auto type_idx = lib_bro_add_indexed_type(bro_type);
	type_cache.insert(std::make_pair(cache_idx, std::make_pair(bro_type, type_idx)));
	}

ValueConverter::ValueConverter(compiler::ModuleBuilder* arg_mbuilder,
			       TypeConverter* arg_type_converter)
	{
	mbuilder = arg_mbuilder;
	type_converter = arg_type_converter;
	}

bool ValueConverter::Convert(shared_ptr<::hilti::Expression> value, shared_ptr<::hilti::Expression> dst, std::shared_ptr<::binpac::Type> btype, BroType* hint)
	{
	_bro_type_hints.push_back(hint);
	setArg1(value);
	setArg2(dst);
	_arg3 = btype;
	bool set = false;
	bool success = processOne(value->type(), &set);
	assert(set);
	_arg3 = nullptr;
	_bro_type_hints.pop_back();
	return success;
	}

shared_ptr<::binpac::Type> ValueConverter::arg3() const
	{
	return _arg3;
	}

shared_ptr<::hilti::builder::BlockBuilder> ValueConverter::Builder() const
	{
	return mbuilder->builder();
	}

void TypeConverter::visit(::hilti::type::Reference* b)
	{
	BroType* rtype;
	processOne(b->argType(), &rtype);
	setResult(rtype);
	}

void TypeConverter::visit(::hilti::type::Address* a)
	{
	auto result = base_type(TYPE_ADDR);
	setResult(result);
	}

void TypeConverter::visit(::hilti::type::Bool* a)
	{
	auto result = base_type(TYPE_BOOL);
	setResult(result);
	}

void TypeConverter::visit(::hilti::type::Bytes* b)
	{
	auto result = base_type(TYPE_STRING);
	setResult(result);
	}

void TypeConverter::visit(::hilti::type::Double* d)
	{
	auto result = base_type(TYPE_DOUBLE);
	setResult(result);
	}

void TypeConverter::visit(::hilti::type::Enum* e)
	{
	auto etype = ast::checkedCast<binpac::type::Enum>(arg1());

	if ( auto result = LookupCachedType(e->sharedPtr<::hilti::type::Enum>(), etype) )
		{
		setResult(result);
		return;
		}

	if ( ! etype->id() )
		reporter::internal_error("TypeConverter::visit(Enum): type has not ID");

	string module;
	string name;
	auto c = etype->id()->path();

	if ( c.size() == 1 )
		{
		name = c.front();

		if ( auto m = etype->id()->firstParent<binpac::Module>() )
			module = m->id()->pathAsString();
		}

	else if ( c.size() == 2 )
		{
		module = c.front();
		name = c.back();
		}

	if ( module.empty() || name.empty() )
                reporter::internal_error(::util::fmt("TypeConverter::visit(Enum): cannot determine type's module/name (%s/%s|%s)",
						     module.size(), name.size(), etype->id()->pathAsString().c_str()));

	// Build the Bro type.
	auto type_id = etype->id()->name();
	auto eresult = new EnumType(name);
	auto undef = ::util::fmt("%s_Undef", type_id);
	eresult->AddName(module, undef.c_str(), lib_bro_enum_undef_val, true);

	for ( auto l : etype->labels() )
		{
		if ( l.first->name() == "Undef" )
			continue;

		auto name = ::util::fmt("%s_%s", type_id, l.first->name());
		eresult->AddName(module, name.c_str(), l.second, true);
		}

	PLUGIN_DBG_LOG(HiltiPlugin, "Adding Bro enum type %s::%s", module.c_str(), name.c_str());
	string id_name = ::util::fmt("%s::%s", module, name);
	::ID* id = install_ID(name.c_str(), module.c_str(), true, true);
	id->SetType(eresult);
	id->MakeType();

{'fraction_non_alphanumeric': 0.18389126039092338, 'fraction_numerical': 0.0028083576724331613, 'mean_word_length': 5.47962154294032, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 88, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2807110', 'n_tokens_mistral': 3456, 'n_tokens_neox': 3347, 'n_words': 587}