From: Marek Polacek Date: Mon, 2 Feb 2026 23:09:08 +0000 (-0500) Subject: c++/reflection: refactor compare_reflections X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d941833a7f8d44ad2c445b756156d4f61dc79ab5;p=thirdparty%2Fgcc.git c++/reflection: refactor compare_reflections In Jason suggested using cp_tree_equal for all exprs in compare_reflections. This patch does so. We just have to handle comparing annotations and types specially, then we can use cp_tree_equal for the rest. It just had to be taught not to crash on unequal NAMESPACE_DECLs. gcc/cp/ChangeLog: * reflect.cc (compare_reflections): Handle comparing annotations and types specially, use cp_tree_equal for the rest. * tree.cc (cp_tree_equal) : New. Reviewed-by: Jason Merrill --- diff --git a/gcc/cp/reflect.cc b/gcc/cp/reflect.cc index 5b6ad8be14b..bc69bdad73b 100644 --- a/gcc/cp/reflect.cc +++ b/gcc/cp/reflect.cc @@ -8230,6 +8230,9 @@ compare_reflections (tree lhs, tree rhs) // ??? Can we do something better? lhs = maybe_get_first_fn (lhs); rhs = maybe_get_first_fn (rhs); + + /* First handle reflection-specific comparisons, then fall back to + cp_tree_equal. */ if (lkind == REFLECT_PARM) { lhs = maybe_update_function_parm (lhs); @@ -8243,27 +8246,19 @@ compare_reflections (tree lhs, tree rhs) && tree_int_cst_equal (TREE_VEC_ELT (lhs, 3), TREE_VEC_ELT (rhs, 3)) && TREE_VEC_ELT (lhs, 4) == TREE_VEC_ELT (rhs, 4)); - - if (lhs == rhs) - return true; - - /* Some trees are not shared. */ - if (TREE_CODE (lhs) == TREE_CODE (rhs)) - switch (TREE_CODE (lhs)) - { - case ARRAY_REF: - case COMPONENT_REF: - case REAL_CST: - return cp_tree_equal (lhs, rhs); - default: - break; - } - - if (TYPE_P (lhs) && TYPE_P (rhs)) - if (!typedef_variant_p (lhs) && !typedef_variant_p (rhs)) + else if (lkind == REFLECT_ANNOTATION) + return lhs == rhs; + else if (TYPE_P (lhs) && TYPE_P (rhs)) + { + /* Given "using A = int;", "^^int != ^^A" should hold. */ + if (typedef_variant_p (lhs) != typedef_variant_p (rhs)) + return false; + /* This is for comparing function types. E.g., + auto fn() -> int; type_of(^^fn) == ^^auto()->int; */ return same_type_p (lhs, rhs); + } - return false; + return cp_tree_equal (lhs, rhs); } /* Return true if T is a valid splice-type-specifier. diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index 2f386e16b9c..761f7d8ff3d 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -4354,6 +4354,7 @@ cp_tree_equal (tree t1, tree t2) case SSA_NAME: case USING_DECL: case DEFERRED_PARSE: + case NAMESPACE_DECL: return false; case BASELINK: