C++ Insights – See your source code with the eyes of a Compiler
github.com
github.com
int foo(int i) {
int add(int x) { return i + x; } // nested function
return add(3);
}
int bar(int i {
alias lambda = (x) => x + i; // lambda
return lambda(3);
}
}Here's an example: https://cppinsights.io/s/f04b6896
(That also shows how C++'s range-based for-loops work, which is similarly some syntactic suger for a fairly mundane for-loop, but details are interesting: unlike a typical for-loop, the loop variable is declared inside the loop body.)
That’s not true in C++, is it? There, this does not compile
for(int i = 0; i < 3; ++i)
{}
i = i + 1;
(https://cppinsights.io/s/26405687: error: use of undeclared identifier 'i')https://en.cppreference.com/cpp/language/for:
“A for statement equivalent to:
{
init-statement
while ( condition )
{
statement
expression ;
}
}
Except that […]”and
“While in C names declared in the scope of init-statement and condition can be shadowed in the scope of statement, it is forbidden in C++:
for (int i = 0;;)
{
long i = 1; // valid C, invalid C++
// ...
}
“Like: I wish what is the first interpretation of my code to be obvious. Is not about advanced optimization, or nitty gritty compiler tricks, is about the language. This tool is useful in any C++ compiler. How did we end with such a complex language?
I mean (sorry, very personal take) the programming language is for me a tool to help me think about a problem and its solution. In this era of LLM more than before, is not a tool to get the machine to do something, is a language for formalizing ideas, problems and solutions, like math. Should not be so obscure, that you need to be a lawyer to understand it, right? Or I am very much old and totally in the weeds with my ideas?
In the README example, the C++ practitioner just doesn’t think about those automatically synthesized copy constructors and such for POD types, nor do they think about the fact that assignment is desugared to operator= or that an assignment can involve a static_cast.
Of course there are people who do not need that tool. The thing is, other languages doesn’t need such an explanation tool at all, is very much enough with reading a small book. I personally would like C++ to not really need it, but clearly as I stated: maybe “the only one”
Are you sure about that?
Plain old C has -E to expand macros. Rust has cargo expand. Haskell has -ddump-simpl. Common Lisp has macroexpand. Any language that has a complicated syntax or allows the user to customize the syntax needs such a tool. Whether such a tool actually exists reflects more on popularity and community desire.
Compilation per se is not difficult. Doing it with good error recovery, good feedback in case of error, and the optimization is difficult.
This tool does not tell me those parts (for that is gotbolt) this is about how the first layer of interpretation works, and not for making compilers, but for understanding what the code actually means, because is not obvious at first sight.
Only if you mean K&R C or the early C89/90 compilers, before the extension explosion, newer language standards, and modern ISAs.
Compilers for all languages have the concept of "lowering" the level of source-language abstractions from high-level to low-level viz. Written Source -> Simpler Source -> AST -> IR -> Machine Code. One type is "desugaring" i.e. transforming complex syntax to simpler ones so that the following stages of compilation have to operate only on a smaller and hence more manageable language subset.
This tool shows both desugaring and other lowering (eg. implicit transformations, type deductions etc.) within the C++ source language by regenerating the source from the AST thus giving you full visibility until that stage in the compilation process.
PS: Explanatory video presentation by the author - https://www.youtube.com/watch?v=VJ6ZvDRYzNE&t=558s
Can these issues be resolved?
What I'd like to see, is how much time is spent to compile each block of C++ code (sliced as fine as possible).