This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: Patch: performing decl operation on type
- From: Robert Bowdidge <bowdidge at apple dot com>
- To: gcc-patches at gcc dot gnu dot org
- Cc: Robert Bowdidge <bowdidge at apple dot com>
- Date: Tue, 14 Oct 2003 19:06:02 -0700
- Subject: Re: Patch: performing decl operation on type
- References: <97577826-FB7E-11D7-9911-000A957D89DA@apple.com>
In cp_finish_decl() (gcc/cp/decl.c:7837, top of tree code), the
following code appears:
if (TREE_CODE (decl) == FIELD_DECL && asmspec)
{
/* This must override the asm specifier which was placed by
grokclassfn. Lay this out fresh. */
SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX);
This code is suspicious -- it's one of very few places in the C and C++
front ends where the contents of the type field are manipulated in a
non-type way. The code, in one form or another, dates from at least
the beginning of Cygnus's RCS server in 1992. I suspect that the the
SET_DECL_RTL was supposed to act on decl, not on TREE_TYPE(decl), and
the TREE_TYPE() was added incorrectly. None of the other occurrences
I found manipulated declarations in the type field; the other offenders
were storing line numbers in the type field (config/rs6000/rs6000.c)
and lists (in the Objective C front end).
This code also must never be run, for the type check in SET_DECL_RTL
would have put out an assertion if so. With a test case that should
trigger this code, the offending code was never actually called:
struct foo {
static int bar __asm__ ("barbaz");
}
main() {
printf("Hello world!\n");
}
Specifically, static variable declarations in a structure get converted
from FIELD_DECL to VAR_DECL before this point. Non-static variables in
a structure cannot have asm directives in C++; the warning happens
before cp_finish_decl, and thus also never hit this code.
I'd like to remove this code because it's (1) dead code and (2) shows a
misleading use of the macros and data structures. With this code
removed, dejagnu shows no regressions. I've also added g++ test cases
to check that asmspecs on structure fields generate an error, and that
asmspecs on static structure fields correctly generates a binary
referencing these symbols.
This code was found as part of a quick-and-dirty test to see if making
typing stricter was possible.
Ok to submit?
Robert Bowdidge
diff -u -b -d -r1.1139 decl.c
--- cp/decl.c 3 Oct 2003 20:27:04 -0000 1.1139
+++ cp/decl.c 10 Oct 2003 23:41:35 -0000
@@ -7834,7 +7834,7 @@
{
/* This must override the asm specifier which was placed by
grokclassfn. Lay this out fresh. */
- SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX);
+ SET_DECL_RTL (decl, NULL_RTX);
SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
make_decl_rtl (decl, asmspec);
}
ChangeLog:
* cp/decl.c (cp_finish_decl): Remove clause intended for asm
directives in struct or class fields: this code is never executed.
Added test cases:
gcc/testsuite/g++.dg/ext/asmspecValid.C:
// tests that the asm directive is correctly handled for static fields
// in structures and classes. This only applies to C++; such
// directives generate errors in C. Assembler directives for local
// variables should be tested by the C test suite.
//
// Contributed by Robert Bowdidge (bowdidge@apple.com) 14 Oct 2003
// { dg-do compile }
struct Foo {
// This should reference a variable called bar
static int i __asm__("bar");
};
class Bar {
public:
static int i __asm__("theRealI");
static int j __asm__("theRealJ");
int boof;
};
class Baz : public Bar {
public:
static char *ptr __asm__ ("theRealString");
};
int main (int argc, char **argv) {
struct Foo myFoo;
Bar b;
myFoo.i = 1;
Foo::i = 2;
Baz::j = 10;
Baz::ptr = 0;
b.i = 1;
return (b.i);
}
/* { dg-final {scan-assembler "bar"} } */
/* { dg-final {scan-assembler "theRealString"} } */
/* { dg-final {scan-assembler "theRealI" } } */
/* { dg-final {scan-assembler "theRealJ" } } */
gcc/testsuite/g++.dg/ext/asmspecInvalid.C:
// tests that the asm directive is correctly handled for static fields
// in structures and classes. This only applies to C++; such
// directives generate errors in C. Assembler directives for local
// variables should be tested by the C test suite.
//
// Contributed by Robert Bowdidge (bowdidge@apple.com) 14 Oct 2003
// { dg-do compile }
struct Foo {
// This should reference a variable called bar
int i __asm__("bar"); /* { dg-error "specifiers are not permitted" }
*/
};
int main (void ) {
int j = 0;
return j;
}