* Help requested on C++ template syntax (for Emacs development).
@ 2008-06-10 18:15 Alan Mackenzie
2008-06-10 18:56 ` Ian Lance Taylor
2008-06-11 18:15 ` Tom Tromey
0 siblings, 2 replies; 5+ messages in thread
From: Alan Mackenzie @ 2008-06-10 18:15 UTC (permalink / raw)
To: gcc
Hi, GCC list!
Apologies if this isn't the right place to ask, but it seems like a good
place to find experts on C++ and Java syntax. If there's a better place
to ask, would somebody give me a pointer, please.
I'm the maintainer of Emacs's CC Mode, which includes modes for C, C++,
Java, etc.
Currently, C++ templates are not handled 100% accurately by C++ mode -
neither are generics in Java mode, for that matter. I want to fix this.
My knowledge of C++ is moderate rather than comprehensive.
So, the question: is it possible to identify with 100% certainty, PURELY
SYNTACTICALLY (i.e. without access to the compiler's symbol table),
when "< ... >" is a pair of template (C++) or generic (Java) brackets?
I'm thinking of things like
foo (a < b, c > d);
I think this is unambiguously a function call with 2 parameters, the
expressions "a < b" and "c > d". It cannot be be one with 1 parameter
beginning with the template invocation "a < b , c >". Or can it?
I believe that a pair of template brackets cannot enclose a semicolon,
for example.
Another related question: although there is no maximum bound on how far
apart template/generic brackets can be, I believe that in practice, they
are never that far apart (a few hundred bytes max, perhaps). Is this, in
fact, the case?
A cc: to acm@muc.de would be most appreciated.
Thanks in advance for any and all help!
--
Alan Mackenzie (Nuremberg, Germany).
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Help requested on C++ template syntax (for Emacs development).
2008-06-10 18:15 Help requested on C++ template syntax (for Emacs development) Alan Mackenzie
@ 2008-06-10 18:56 ` Ian Lance Taylor
2008-06-10 19:05 ` Ian Lance Taylor
2008-06-11 18:15 ` Tom Tromey
1 sibling, 1 reply; 5+ messages in thread
From: Ian Lance Taylor @ 2008-06-10 18:56 UTC (permalink / raw)
To: Alan Mackenzie; +Cc: gcc
Alan Mackenzie <acm@muc.de> writes:
> I'm thinking of things like
>
> foo (a < b, c > d);
>
> I think this is unambiguously a function call with 2 parameters, the
> expressions "a < b" and "c > d". It cannot be be one with 1 parameter
> beginning with the template invocation "a < b , c >". Or can it?
No, it can't be, because a<b, c> is a type. The result would be
foo(TYPE d), which can not be a function call. On the other hand, if
there were a type before foo then this would be a function
declaration. For example, this is valid C++ code:
template <int a1, int a2> class a;
int fn(int d, int e)
{
const int b = 1;
const int c = 2;
typedef int f;
f foo (int, int);
f foo (a < b, c > d);
foo (e < b, c > d);
}
The line "f foo (a < b, c > d);" uses a template, the line "foo (e <
b, c > d);" does not.
I rather doubt that you can purely syntactically, fully reliably,
determine whether <> refers to a template, but I don't know for sure.
> Another related question: although there is no maximum bound on how far
> apart template/generic brackets can be, I believe that in practice, they
> are never that far apart (a few hundred bytes max, perhaps). Is this, in
> fact, the case?
A few hundred characters is probably a little too small, but in
practice I think one thousand characters is probably usually
sufficient.
Ian
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Help requested on C++ template syntax (for Emacs development).
2008-06-10 18:56 ` Ian Lance Taylor
@ 2008-06-10 19:05 ` Ian Lance Taylor
2008-06-15 14:12 ` Alan Mackenzie
0 siblings, 1 reply; 5+ messages in thread
From: Ian Lance Taylor @ 2008-06-10 19:05 UTC (permalink / raw)
To: Alan Mackenzie; +Cc: gcc
Ian Lance Taylor <iant@google.com> writes:
> Alan Mackenzie <acm@muc.de> writes:
>
>> I'm thinking of things like
>>
>> foo (a < b, c > d);
>>
>> I think this is unambiguously a function call with 2 parameters, the
>> expressions "a < b" and "c > d". It cannot be be one with 1 parameter
>> beginning with the template invocation "a < b , c >". Or can it?
>
> No, it can't be, because a<b, c> is a type. The result would be
> foo(TYPE d), which can not be a function call. On the other hand, if
> there were a type before foo then this would be a function
> declaration. For example, this is valid C++ code:
>
> template <int a1, int a2> class a;
>
> int fn(int d, int e)
> {
> const int b = 1;
> const int c = 2;
> typedef int f;
> f foo (int, int);
> f foo (a < b, c > d);
> foo (e < b, c > d);
> }
>
> The line "f foo (a < b, c > d);" uses a template, the line "foo (e <
> b, c > d);" does not.
Oh, wait, I forgot about constructors. This is valid C++ code:
template <int a1, int a2> class a;
const int b = 1;
const int c = 2;
int d;
int e;
class foo
{
foo (a < b, c > d);
};
extern void quux (int, int);
void bar()
{
quux (e < b, c > d);
}
Inside the class foo, you've got a template, inside the function bar,
you've got a function call.
I think you're going to have a hard time with this.
Ian
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Help requested on C++ template syntax (for Emacs development).
2008-06-10 19:05 ` Ian Lance Taylor
@ 2008-06-15 14:12 ` Alan Mackenzie
0 siblings, 0 replies; 5+ messages in thread
From: Alan Mackenzie @ 2008-06-15 14:12 UTC (permalink / raw)
To: Ian Lance Taylor; +Cc: gcc
Hi, Ian!
On Tue, Jun 10, 2008 at 12:05:20PM -0700, Ian Lance Taylor wrote:
> Ian Lance Taylor <iant@google.com> writes:
> > Alan Mackenzie <acm@muc.de> writes:
> >> I'm thinking of things like
> >> foo (a < b, c > d);
[ .... ]
> Oh, wait, I forgot about constructors. This is valid C++ code:
> template <int a1, int a2> class a;
> class foo
> {
> foo (a < b, c > d);
> };
> extern void quux (int, int);
> void bar()
> {
> quux (e < b, c > d);
> }
> Inside the class foo, you've got a template, inside the function bar,
> you've got a function call.
> I think you're going to have a hard time with this.
Er, yes. ;-) Thank you very much for all the help. It's going to take
some while to sink in. C++ is a difficult language. Hopefully, in a few
months time, Emacs will be doing better than it currently is.
> Ian
--
Alan Mackenzie (Nuremberg, Germany).
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Help requested on C++ template syntax (for Emacs development).
2008-06-10 18:15 Help requested on C++ template syntax (for Emacs development) Alan Mackenzie
2008-06-10 18:56 ` Ian Lance Taylor
@ 2008-06-11 18:15 ` Tom Tromey
1 sibling, 0 replies; 5+ messages in thread
From: Tom Tromey @ 2008-06-11 18:15 UTC (permalink / raw)
To: Alan Mackenzie; +Cc: gcc
>>>>> "Alan" == Alan Mackenzie <acm@muc.de> writes:
Alan> So, the question: is it possible to identify with 100% certainty, PURELY
Alan> SYNTACTICALLY (i.e. without access to the compiler's symbol table),
Alan> when "< ... >" is a pair of template (C++) or generic (Java) brackets?
In Java, yes, if the source is error-free.
Alan> I'm thinking of things like
Alan> foo (a < b, c > d);
Alan> I think this is unambiguously a function call with 2 parameters, the
Alan> expressions "a < b" and "c > d".
In Java this is a function call. If 'a' is a type, this is an error,
since you can't declare 'd' here.
So... if cc-mode assumes that the code it is looking at is
syntactically correct, you can always guess. But, it is a lot nicer
for the user if errors are flagged. That way lies the Eclipse
approach :-)
Alan> Another related question: although there is no maximum bound on how far
Alan> apart template/generic brackets can be, I believe that in practice, they
Alan> are never that far apart (a few hundred bytes max, perhaps). Is this, in
Alan> fact, the case?
For Java, yeah, generally speaking. There may be exceptions. And
sometimes I think you may find surprising amounts of whitespace in
there.
For C++, I think you are just doomed. Even if you could get the
compiler to emit perfect information, it would only emit information
about the code it actually saw -- not stuff in ignored #if groups.
Tom
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2008-06-15 14:12 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-10 18:15 Help requested on C++ template syntax (for Emacs development) Alan Mackenzie
2008-06-10 18:56 ` Ian Lance Taylor
2008-06-10 19:05 ` Ian Lance Taylor
2008-06-15 14:12 ` Alan Mackenzie
2008-06-11 18:15 ` Tom Tromey
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).