This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[C++ PATCH] Fix lookup if class has a field with the same name as class
- To: jason at redhat dot com, mark at codesourcery dot com
- Subject: [C++ PATCH] Fix lookup if class has a field with the same name as class
- From: Jakub Jelinek <jakub at redhat dot com>
- Date: Mon, 19 Feb 2001 22:52:01 +0100
- Cc: gcc-patches at gcc dot gnu dot org
- Reply-To: Jakub Jelinek <jakub at redhat dot com>
Hi!
The testcase below (actually already the two struct lines) cause a segfault.
The reason is that g++ now allows structures having fields with the same
name if they don't have user defined constructors, but when doing lookup it
does not expect this to happen (if looking up a type name and non-type is
found, it would just scan CLASSTYPE_TAGS).
This patch seems to fix it (passes bootstrap and does not create any
regressions) and should not slow lookup down unless the case where current
g++ would segfault happens, but if it would be fine to set
if (lfi->name == TYPE_IDENTIFIER (type))
nval = TYPE_NAME (type);
instead of looking up the TYPE_DECL stored in TYPE_FIELDS(type) chain, the
code would be even simpler.
Ok to commit?
2001-02-19 Jakub Jelinek <jakub@redhat.com>
* search.c (lookup_field_r): If looking for type and non-TYPE_DECL
is found, look first if name does not match the structure name.
* g++.old-deja/g++.other/lookup22.C: New test.
--- gcc/cp/search.c.jj Mon Feb 19 12:34:01 2001
+++ gcc/cp/search.c Mon Feb 19 19:50:10 2001
@@ -1382,11 +1382,27 @@ lookup_field_r (binfo, data)
we ignore all non-types we find. */
if (lfi->want_type && TREE_CODE (nval) != TYPE_DECL)
{
- nval = purpose_member (lfi->name, CLASSTYPE_TAGS (type));
- if (nval)
- nval = TYPE_MAIN_DECL (TREE_VALUE (nval));
- else
- return NULL_TREE;
+ if (lfi->name == TYPE_IDENTIFIER (type))
+ {
+ /* If the aggregate has no user defined constructors, we allow
+ it to have fields with the same name as the enclosing type.
+ If we are looking for that name, find the corresponding
+ TYPE_DECL. */
+ for (nval = TREE_CHAIN (nval); nval; nval = TREE_CHAIN (nval))
+ if (DECL_NAME (nval) == lfi->name
+ && TREE_CODE (nval) == TYPE_DECL)
+ break;
+ }
+ else
+ nval = NULL_TREE;
+ if (!nval)
+ {
+ nval = purpose_member (lfi->name, CLASSTYPE_TAGS (type));
+ if (nval)
+ nval = TYPE_MAIN_DECL (TREE_VALUE (nval));
+ else
+ return NULL_TREE;
+ }
}
/* You must name a template base class with a template-id. */
--- gcc/testsuite/g++.old-deja/g++.other/lookup22.C.jj Mon Feb 19 19:42:51 2001
+++ gcc/testsuite/g++.old-deja/g++.other/lookup22.C Mon Feb 19 19:54:55 2001
@@ -0,0 +1,13 @@
+// Test for proper handling of type lookup if base class has field with the
+// same name as the containing class.
+// Build don't link:
+
+struct a { int a; };
+struct b : a {};
+
+b x;
+
+void foo ()
+{
+ x.a = 22;
+}
Jakub