- All of reflection now lives in upb/reflection/
- Each def type has its own .c file and its own .h file
- Functions that require a builder context are declared in def_builder.h
- The mini descriptor encoders have also been pulled into upb/reflection/
- upb/def.h, upb/def.hpp, upb/reflection.h, and upb/reflection.hpp are now deprecated stubs that point to the new headers

PiperOrigin-RevId: 474459500
diff --git a/BUILD b/BUILD
index b8253a3..cd515ba 100644
--- a/BUILD
+++ b/BUILD
@@ -168,6 +168,7 @@
     hdrs = [
         "upb/msg_internal.h",
     ],
+    visibility = ["//:__subpackages__"],
     deps = [
         ":extension_registry",
         ":port",
@@ -333,9 +334,7 @@
     copts = UPB_DEFAULT_COPTS,
     visibility = ["//visibility:public"],
     deps = [
-        ":descriptor_upb_proto",
         ":reflection",
-        ":table_internal",
     ],
 )
 
@@ -372,22 +371,49 @@
     ],
 )
 
+# TODO(b/232091617): Once we can delete the deprecated forwarding headers
+# (= everything in upb/) we can move this build target down into reflection/
 cc_library(
     name = "reflection",
     srcs = [
-        "upb/def.c",
-        "upb/internal/mini_descriptor.c",
-        "upb/internal/mini_descriptor.h",
-        "upb/mini_descriptor.c",
-        "upb/msg.h",
-        "upb/reflection.c",
+        "upb/reflection/common.h",
+        "upb/reflection/def_builder.c",
+        "upb/reflection/def_builder.h",
+        "upb/reflection/def_pool.c",
+        "upb/reflection/def_pool.h",
+        "upb/reflection/def_type.c",
+        "upb/reflection/def_type.h",
+        "upb/reflection/enum_def.c",
+        "upb/reflection/enum_def.h",
+        "upb/reflection/enum_value_def.c",
+        "upb/reflection/enum_value_def.h",
+        "upb/reflection/extension_range.c",
+        "upb/reflection/extension_range.h",
+        "upb/reflection/field_def.c",
+        "upb/reflection/field_def.h",
+        "upb/reflection/file_def.c",
+        "upb/reflection/file_def.h",
+        "upb/reflection/message.c",
+        "upb/reflection/message_def.c",
+        "upb/reflection/message_def.h",
+        "upb/reflection/method_def.c",
+        "upb/reflection/method_def.h",
+        "upb/reflection/mini_descriptor_encode.c",
+        "upb/reflection/oneof_def.c",
+        "upb/reflection/oneof_def.h",
+        "upb/reflection/service_def.c",
+        "upb/reflection/service_def.h",
     ],
     hdrs = [
         "upb/def.h",
         "upb/def.hpp",
-        "upb/mini_descriptor.h",
         "upb/reflection.h",
         "upb/reflection.hpp",
+        "upb/reflection/def.h",
+        "upb/reflection/def.hpp",
+        "upb/reflection/message.h",
+        "upb/reflection/message.hpp",
+        "upb/reflection/mini_descriptor_encode.h",
     ],
     copts = UPB_DEFAULT_COPTS,
     visibility = ["//visibility:public"],
@@ -395,6 +421,7 @@
         ":collections",
         ":descriptor_upb_proto",
         ":mini_table",
+        ":mini_table_internal",
         ":port",
         ":table_internal",
         ":upb",
@@ -445,6 +472,25 @@
 # Tests ########################################################################
 
 cc_test(
+    name = "def_builder_test",
+    srcs = [
+        "upb/reflection/common.h",
+        "upb/reflection/def_builder.h",
+        "upb/reflection/def_builder_test.cc",
+        "upb/reflection/def_pool.h",
+        "upb/reflection/def_type.h",
+    ],
+    deps = [
+        ":descriptor_upb_proto",
+        ":port",
+        ":reflection",
+        ":table_internal",
+        ":upb",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
+
+cc_test(
     name = "test_generated_code",
     srcs = ["upb/test_generated_code.cc"],
     deps = [
@@ -621,14 +667,14 @@
     srcs = ["upb/test_cpp.cc"],
     copts = UPB_DEFAULT_CPPOPTS,
     deps = [
+        ":json",
+        ":port",
+        ":reflection",
         ":test_cpp_upb_proto",
         ":test_cpp_upb_proto_reflection",
         ":timestamp_upb_proto",
         ":timestamp_upb_proto_reflection",
-        "//:json",
-        "//:port",
-        "//:reflection",
-        "//:upb",
+        ":upb",
         "@com_google_googletest//:gtest_main",
     ],
 )
@@ -638,9 +684,9 @@
     srcs = ["upb/test_table.cc"],
     copts = UPB_DEFAULT_CPPOPTS,
     deps = [
-        "//:port",
-        "//:table_internal",
-        "//:upb",
+        ":port",
+        ":table_internal",
+        ":upb",
         "@com_google_googletest//:gtest_main",
     ],
 )
@@ -678,13 +724,13 @@
     deps = [
         ":conformance_proto_upb",
         ":conformance_proto_upbdefs",
+        ":json",
+        ":port",
+        ":reflection",
         ":test_messages_proto2_upbdefs",
         ":test_messages_proto3_upbdefs",
-        "//:json",
-        "//:port",
-        "//:reflection",
-        "//:textformat",
-        "//:upb",
+        ":textformat",
+        ":upb",
     ],
 )
 
@@ -719,13 +765,13 @@
     deps = [
         ":conformance_proto_upb",
         ":conformance_proto_upbdefs",
+        ":json",
+        ":port",
+        ":reflection",
         ":test_messages_proto2_upbdefs",
         ":test_messages_proto3_upbdefs",
-        "//:json",
-        "//:port",
-        "//:reflection",
-        "//:textformat",
-        "//:upb",
+        ":textformat",
+        ":upb",
     ],
 )