wish: generation of type annotation for C++11 code.
David Brown
david@westcontrol.com
Thu Nov 10 13:49:00 GMT 2011
On 10/11/2011 11:58, Basile Starynkevitch wrote:
> Hello All
>
> (I am playing with C++11, but I am not a C++ expert, and I don't know the C++ front-end
> part of GCC, so this is a feature wish only).
>
> Perhaps it could be useful for some later (4.8?) release of GCC to produce an inferred
> type annotation file.
>
> For those knowing Ocaml, I was thinking of something similar to the -annot (or -dtypes)
> option of the ocaml or ocamlopt compiler.
> http://caml.inria.fr/pub/docs/manual-ocaml/manual022.html
>
> For instance with the simplistic (single line) Ocaml file incr.ml:
> let f x = x + 1;;
> the incr.annot file obtained with ocamlc -annot incr.ml contains
>
> #######
> "incr.ml" 1 0 4 "incr.ml" 1 0 5
> type(
> int -> int
> )
> ident(
> def f "incr.ml" 1 0 15 "_none_" 1 0 -1
> )
> "incr.ml" 1 0 6 "incr.ml" 1 0 7
> type(
> int
> )
> ident(
> def x "incr.ml" 1 0 10 "incr.ml" 1 0 15
> )
> "incr.ml" 1 0 10 "incr.ml" 1 0 11
> type(
> int
> )
> ident(
> int_ref x "incr.ml" 1 0 6 "incr.ml" 1 0 7
> )
> "incr.ml" 1 0 12 "incr.ml" 1 0 13
> type(
> int -> int -> int
> )
> ident(
> ext_ref Pervasives.( + )
> )
> "incr.ml" 1 0 14 "incr.ml" 1 0 15
> type(
> int
> )
> "incr.ml" 1 0 10 "incr.ml" 1 0 15
> type(
> int
> )
> ############
>
> and external editors (in particular the ocaml or tuareg modes of Emacs) are able to parse
> such annotation file, and interactively to show the type (with Ctrl-C Ctrl-T under
> emacs) at the cursor's position.
>
> The main motivation would be to help C++ newbies, not C++ experts (those know well
> enough the C++ language, and don't need help). With the type inference abilities given by
> the auto keyword, it is sometimes hard for a beginner to understand what type is some
> particular expression in his code (or what exactly function is called in an overloaded
> context). The type annotation file can be generated by C++ front-end, and would contain
> type and position information for every expression inside a source file.
>
> That type annotation produced by g++ would be usable by external editors, etc.
>
> Of course, such a thing could be done by some GCC plugin, but I believe providing the
> feature inside GCC itself, and documenting the format of the textual type annotation
> file, would give this feature more visibility, and it would help people a lot. And I am
> pretty certain that the C++ frontend does compute the type of each expression, so it is
> mostly the point of defining a textual type annotation format and output it.
>
> The point is that inside languages with type inferences, like C++ is becoming in its 2011
> standard, newbies are helped a lot with type annotations (and the editor's support of
> them) - but experts rarely need this.
>
> So I would dream of a -fannotate-type option to g++ 4.8 with a section in the
> documentation describing the format of the type annotation file.
>
> We already have -M (etc..) options to help about include dependencies. We could have
> some other option to help about type inferences.
>
> Regards.
I don't know why you say such a feature would only help C++ newbies - my
guess is that it would be at least as helpful to experts. But maybe
/real/ C++ experts have a different opinion there!
You might get something useful from the "-fdump-" options:
<http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html>
I don't know which pass would be best for you, and obviously the use in
an editor depends entirely on the editor.
More information about the Gcc
mailing list