1

In the last few weeks I have been experimenting with ASTs and Clang, in particular clang-tidy.

Clang offers some classes and way to interact with the ASTs, but what I don't understand is if the clang::VarDecl I am using so often is something named and created by the creators of Clang, or by the creators of the language.

Who decided that was to be called VarDecl?

I mean, is the AST (and all its elements) something that came from the mind of the inventor of the language and the various frontends just creates classes named after a document written by him/her or every frontend potentially creates its AST of a given source code and so Clang's and GCC's are different?

Fede2424
  • 67
  • 6
  • https://github.com/llvm/llvm-project/commit/a11999d83a8ed1a2661feb858f0af786f2b829ad `by the creators of Clang, or by the creators of the language` What/which "language" are you referring to? You mean C++ programming language? – KamilCuk Nov 09 '21 at 09:38
  • 3
    You're looking at the internals of a specific compiler. The names and meanings don't have to be coordinated with anyone else. The internals of gcc will be very different. –  Nov 09 '21 at 09:38
  • The syntax (or grammar) is part of the language. Whether a compiler uses a tree during its processing or not is nothing a language cares about. You can define an AST over natural languages, like English, as well (at least most of them - some languages don't have a very hierarchical grammatical structure). – molbdnilo Nov 09 '21 at 09:41
  • @KamilCuk Sorry you are right, I was referring to C++. – Fede2424 Nov 09 '21 at 09:49
  • And "the frontend" is referring to a compiler? – KamilCuk Nov 09 '21 at 09:50
  • The frontend I am referring to in this case is the first phase on a 3-phase compiler. – Fede2424 Nov 09 '21 at 09:52
  • The Specs for the languages for Clang (C, C++, etc.) do not specify the AST. For C++, you can see drafts of the Spec [here](https://github.com/cplusplus/draft/tree/main/papers). There you will find not one mention of "AST" nor "abstract syntax tree". You can choose whatever implementation for the AST as you want--if you want one! For my implementations of a parser for an ISO C++, which I scrape directly from any and all Specs programmatically, I work directly with the parse tree. – kaby76 Nov 09 '21 at 12:27

1 Answers1

3

Is the AST, abstract syntax tree, defined by the language

Not fully. Each definition in the C++ language standard comes with a short syntax notation and there is a informative annex with grammar summary. But the annex notes https://eel.is/c++draft/gram :

This summary of C++ grammar is intended to be an aid to comprehension. It is not an exact statement of the language. In particular, the grammar described here accepts a superset of valid C++ constructs. [...]

There is no VarDecl in that grammar from standard, variable declaration just one interpretation of simple-declaration.

or by the frontend?

Internals of the compiler, if it has a frontend, or it hasn't, it has 3 or 1000 stages, are part of the compiler implementation. From the point of the language, the compiler can be implemented in any way it wants, as long as it translates valid programs correctly. Let's say generally, language specifies what should happen when, not how.

So to answer the question, AST (if used at all in any form) is defined by the compiler.

Who decided that was to be called VarDecl?

I most probably suspect Chris Lattner at https://github.com/llvm/llvm-project/commit/a11999d83a8ed1a2661feb858f0af786f2b829ad .

that came from the mind of the inventor of the language and the various frontends just creates classes named after a document written by him/her or every frontend potentially creates its AST of a given source code and so Clang's and GCC's are different?

Surely they are influenced by what is in the standard, but every compiler has its own internals. Well, in short, Clang VarDecl and GCC VAR_DECL are different, and they are also different conceptually - let's say GCC uses switch(...) case VAR_DECL: and Clang uses classes clang::VarDecl.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111