Topic: Source code of a compiler in compiling static_cast, dynamic_cast


Author: kent.h.to@gmail.com
Date: Sun, 26 Apr 2009 11:17:09 CST
Raw View
Hi,

I would like to understand what is actually inside a static_cast and
dynamic_cast, or new styled cast in C++ ("new" when being compared to
C).

I ask so since I could not be satisfied with the "Description of how
those cast work, the DOs and DONTs", I think I could almost ONLY
understand them if I look at a compiler's source code. Many books
could not help me much on this (sorry, this is my problem, not one of
those books).

What do you suggest me to look for ? Digging into g++ ? It seems to
big to be digged, no ?

Thanks

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: James Kuyper <jameskuyper@verizon.net>
Date: Mon, 27 Apr 2009 01:38:54 CST
Raw View
kent.h.to@gmail.com wrote:
> Hi,
>
> I would like to understand what is actually inside a static_cast and
> dynamic_cast, or new styled cast in C++ ("new" when being compared to
> C).
>
> I ask so since I could not be satisfied with the "Description of how
> those cast work, the DOs and DONTs", I think I could almost ONLY
> understand them if I look at a compiler's source code. Many books
> could not help me much on this (sorry, this is my problem, not one of
> those books).

The fundamental problem is that there's no unique "inside" to these
casts. The code that is generated depends upon the source type, and the
destination type. The difference between the different casts is
primarily a matter of which circumstances they are permitted to occur
in. Furthermore, in those situations where there are multiple different
specialized casts that can perform the same conversion, the code
generated to implement that conversion will generally be the same,
regardless of which cast was used.


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: wasti.redl@gmx.net
Date: Mon, 27 Apr 2009 01:41:37 CST
Raw View
On Apr 26, 7:17 pm, kent.h...@gmail.com wrote:
> Hi,
>
> I would like to understand what is actually inside a static_cast and
> dynamic_cast, or new styled cast in C++ ("new" when being compared to
> C).
>
> I ask so since I could not be satisfied with the "Description of how
> those cast work, the DOs and DONTs", I think I could almost ONLY
> understand them if I look at a compiler's source code. Many books
> could not help me much on this (sorry, this is my problem, not one of
> those books).
>
> What do you suggest me to look for ? Digging into g++ ? It seems to
> big to be digged, no ?

Here's a link to the cast validation routines of the Clang compiler.
http://llvm.org/svn/llvm-project/cfe/trunk/lib/Sema/SemaNamedCast.cpp
Clang's source is a lot more accessible than GCC's.

The validation of new is in SemaExprCXX.cpp in the same directory, but
it's got some significant FIXMEs yet.

(By the way, I wrote that code. If you have any suggestions, I'm of
course happy to receive them off-list.)

Frankly, though, I don't think you'll learn much from the source code
without understanding the standard wording behind that stuff. The
latest draft of the C++0x standard is here:
http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2009/n2857.pdf

Sebastian


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Pavel Minaev <int19h@gmail.com>
Date: Mon, 27 Apr 2009 14:38:54 CST
Raw View
On Apr 26, 10:17 am, kent.h...@gmail.com wrote:
> I would like to understand what is actually inside a static_cast and
> dynamic_cast, or new styled cast in C++ ("new" when being compared to
> C).

The semantics is defined in the Standard. The specific implementation
is, well, implementation-defined. For static_cast it's fairly trivial
(sometimes no-op, sometimes just a pointer increment or decrement), so
it's usually the same, but implementations of dynamic_cast can differ
very much. In any case, this really isn't something that you should be
concerned with. It's semantics that's important.

> I ask so since I could not be satisfied with the "Description of how
> those cast work, the DOs and DONTs", I think I could almost ONLY
> understand them if I look at a compiler's source code. Many books
> could not help me much on this (sorry, this is my problem, not one of
> those books).

No offense meant, but if you could not fully grasp dynamic_cast from a
decent C++ book, then I very much doubt that you'd be able to get
anything useful from a source code of a C++ compiler. C++ is
notoriously hard to parse and compile well, and the complexity of
existing implementations reflects that. I'm not aware of any reference-
like implementation that would focus strictly on correctness, ignoring
optimization opportunities (many of which are effectively requirements
for a modern production compiler), for study purposes.


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]