This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Local binding DECLs


> X-Original-To: geoffk@foam.wonderslug.com
> From: Stephen Biggs <xyzzy@hotpop.com>
> Cc: Geoff Keating <geoffk@geoffk.org>
> Date: 13 May 2003 11:48:56 +0300
> X-HotPOP: -----------------------------------------------
>                    Sent By HotPOP.com FREE Email
>              Get your FREE POP email at www.HotPOP.com
>           -----------------------------------------------
> X-OriginalArrivalTime: 13 May 2003 08:45:13.0274 (UTC) FILETIME=[F81241A0:01C3192B]
> 
> On Tue, 2003-05-13 at 03:33, Geoff Keating wrote:
> > Stephen Biggs <xyzzy@hotpop.com> writes:
> > 
> > > On Mon, 2003-05-12 at 04:13, Richard Henderson wrote:
> > > > On Sun, May 11, 2003 at 10:04:20AM +0300, Stephen Biggs wrote:
> > > > > Ok... is there ANY way to find out if the FUNCTION_DECL I am handed at
> > > > > any time (preferably in ENCODE_SECTION) is declared in a function block
> > > > > as opposed to globally?
> > > > 
> > > > *Declared*?  I.e. to distinguish
> > > > 
> > > > 	extern void foo();
> > > > 	void bar() { foo(); }
> > > > 
> > > > from 
> > > > 
> > > > 	void bar() {
> > > > 	  extern void foo();
> > > > 	  foo();
> > > > 	}
> > > > 
> > > > Absolutely not.  That question doesn't even make sense.
> > > > 
> > > > 
> > > > r~
> > > > 
> > > Sure it does, if you have, for example:
> > > 
> > > void bar() {
> > >   static void foo();
> > >   foo();
> > > }
> > > 
> > > void bar1() {
> > >   static int foo();
> > >   foo();
> > > }
> > > 
> > > void bar2() {
> > >   static int foo(int);
> > >   int a = foo(3);
> > > }
> > > foo() {}
> > > 
> > > This compiles.
> > 
> > My immediate reaction is "no it doesn't!"
> > 
> > It certainly shouldn't, ISO C specifically prohibits it and it's not
> > any GCC extension I recognize.
> 
> I'll agree 100% that this is garbage code.  That being said, why is
> stuff like this all over the GCC/deja testsuite and classified as
> "PASS"??  If it shouldn't compile and ISO C specifically prohibits it,
> then why does GCC allow this without barfing?  -W compiles cleanly
> without complaint. Only when -Wall is specified do the warnings start.

It'd help if you could point to a particular example.

As I said, this shouldn't be allowed.  I actually see *two* problems:

1. You can't have a declaration like 'static void foo(int)' other than
   at file scope
2. Even if the declarations were 'extern void foo(int)', they have to
   be compatible with the function's actual definition, and they aren't.

> It generates perfectly good assembly code on the x86:
>         .ident  "GCC: (GNU) 3.2 20020903 (Red Hat Linux 8.0 3.2-7)"
> 
> > 
> > > I see nothing that gives me the ability to know that any particular DECL
> > > is declared inside a particular function.
> > 
> > That question really doesn't make sense.  Why do you want to know this?
> 
> Why shouldn't I be able to know this?  What do IDENTIFIER_IMPLICIT_DECL
> and DECL_CONTEXT mean?  When are they set?  I am NEVER seeing
> DECL_CONTEXT set.  Is this only for nested functions where the body is
> enclosed in another function??

DECL_CONTEXT is documented in tree.h, and I thought it was pretty
clear:

    For VAR_DECL, PARM_DECL, FUNCTION_DECL, LABEL_DECL, and CONST_DECL
    nodes, this points to either the FUNCTION_DECL for the containing
    function, the RECORD_TYPE or UNION_TYPE for the containing type,
    or NULL_TREE if the given decl has "file scope".

[I think this comment is incomplete, though, because DECL_CONTEXT can
also be a BLOCK.]

However DECL_CONTEXT doesn't completely specify the scope of the
declaration or how it can be accessed, for instance in C++ you can get
to declarations inside classes or namespaces.

> You're saying that knowing the scope of a particular function
> declaration/prototype has no use?

I'm wondering why you should care in a backend, or how you could even
make sense of it once you knew it.

> > > Perhaps someone might need to write different code or do something else
> > > different per function based on the local declarations?  Does THIS make
> > > sense?
> > 
> > No, it doesn't.  Can you give an example of what you want to do?
> 
> In the code compiled for the foo function, it would help optimization if
> it was know whether the return value was used anywhere, whether or not
> it was declared to return "int" in the actual function definition.

This is tricky.  Consider function pointers, for example.  Ideally
you'd want to do this as a general optimisation at the tree level,
removing unused parameters and return values from static functions.

> I also don't want to generate machine instructions in a function that
> calls "foo" to handle return values if "foo" is declared as "void"
> inside the scope.  The problem here is that the declaration changes and
> the scope of the declaration isn't known, based on DECL_CONTEXT not
> being set.

You can do this based just on the type at the time of the call, you
don't need to have to know about scoping for it.

-- 
- Geoffrey Keating <geoffk@geoffk.org>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]