[Bf-blender-cvs] [d93ecc84bb4] functions: initial type inferencer

Jacques Lucke noreply at git.blender.org
Sun Feb 17 21:57:27 CET 2019


Commit: d93ecc84bb447040378dd706b30607d3ed576313
Author: Jacques Lucke
Date:   Sun Feb 17 15:54:00 2019 +0100
Branches: functions
https://developer.blender.org/rBd93ecc84bb447040378dd706b30607d3ed576313

initial type inferencer

===================================================================

M	source/blender/functions/CMakeLists.txt
M	source/blender/functions/FN_functions.hpp
A	source/blender/functions/core/type_inferencing.cpp
A	source/blender/functions/core/type_inferencing.hpp

===================================================================

diff --git a/source/blender/functions/CMakeLists.txt b/source/blender/functions/CMakeLists.txt
index 7ef027faec7..eaca171318e 100644
--- a/source/blender/functions/CMakeLists.txt
+++ b/source/blender/functions/CMakeLists.txt
@@ -21,6 +21,8 @@ set(SRC
 	core/dot_export.cpp
 	core/graph_to_function.hpp
 	core/graph_to_function.cpp
+	core/type_inferencing.hpp
+	core/type_inferencing.cpp
 
 	FN_functions.hpp
 
diff --git a/source/blender/functions/FN_functions.hpp b/source/blender/functions/FN_functions.hpp
index 735c6bca0cb..b7f13998859 100644
--- a/source/blender/functions/FN_functions.hpp
+++ b/source/blender/functions/FN_functions.hpp
@@ -3,6 +3,7 @@
 #include "core/core.hpp"
 #include "core/data_flow_graph.hpp"
 #include "core/graph_to_function.hpp"
+#include "core/type_inferencing.hpp"
 
 #include "core/cpu.hpp"
 
diff --git a/source/blender/functions/core/type_inferencing.cpp b/source/blender/functions/core/type_inferencing.cpp
new file mode 100644
index 00000000000..daa97cbc2b3
--- /dev/null
+++ b/source/blender/functions/core/type_inferencing.cpp
@@ -0,0 +1,58 @@
+#include "type_inferencing.hpp"
+
+namespace FN {
+	void Inferencer::finalize_id(uint64_t id, SharedType &type)
+	{
+		if (m_final_types.contains(id)) {
+			BLI_assert(m_final_types.lookup_ref(id) == type);
+		}
+		else {
+			m_final_types.add(id, type);
+		}
+	}
+
+	void Inferencer::insert_final_type(uint64_t id, SharedType &type)
+	{
+		this->finalize_id(id, type);
+	}
+
+	void Inferencer::insert_type_equality(uint64_t a, uint64_t b)
+	{
+		m_equalities.append(Link(a, b));
+	}
+
+	SharedType &Inferencer::get_final_type(uint64_t id)
+	{
+		return m_final_types.lookup_ref(id);
+	}
+
+	bool Inferencer::has_final_type(uint64_t id)
+	{
+		return m_final_types.contains(id);
+	}
+
+	void Inferencer::inference()
+	{
+		while (!m_equalities.empty()) {
+			for (uint i = 0; i < m_equalities.size(); i++) {
+				Link link = m_equalities[i];
+				bool done = false;
+
+				if (this->has_final_type(link.a)) {
+					this->finalize_id(link.b, this->get_final_type(link.a));
+					done = true;
+				}
+				if (this->has_final_type(link.b)) {
+					this->finalize_id(link.a, this->get_final_type(link.b));
+					done = true;
+				}
+
+				if (done) {
+					m_equalities.remove_and_reorder(i);
+					i--;
+				}
+			}
+		}
+	}
+
+} /* namespace FN */
\ No newline at end of file
diff --git a/source/blender/functions/core/type_inferencing.hpp b/source/blender/functions/core/type_inferencing.hpp
new file mode 100644
index 00000000000..1fdf643a4f4
--- /dev/null
+++ b/source/blender/functions/core/type_inferencing.hpp
@@ -0,0 +1,30 @@
+#include "core.hpp"
+
+namespace FN {
+
+	class Inferencer {
+	private:
+		struct Link {
+			uint64_t a, b;
+			Link(uint64_t a, uint64_t b)
+				: a(a), b(b) {}
+		};
+
+		SmallMap<uint64_t, SharedType> m_final_types;
+		SmallVector<Link> m_equalities;
+
+		void finalize_id(uint64_t id, SharedType &type);
+
+	public:
+		Inferencer() = default;
+
+		void insert_final_type(uint64_t id, SharedType &type);
+		void insert_type_equality(uint64_t a, uint64_t b);
+
+		void inference();
+
+		SharedType &get_final_type(uint64_t id);
+		bool has_final_type(uint64_t id);
+	};
+
+} /* namespace FN */
\ No newline at end of file



More information about the Bf-blender-cvs mailing list