Using an LLVM-style RTTI for the AST – Turning the Source File into an Abstract Syntax Tree
By Reginald Bellamy / August 2, 2021 / No Comments / Emitting the function body, Exams of IT, Generating IR from the AST, Handling the scope of names, IT Certifications
Naturally, the AST nodes are a part of a class hierarchy. A declaration always has a name. Other attributes depend on what is being declared. If a variable is declared, then a type is required. A constant declaration needs a type, a value, and so on. Of course, at runtime, you need to find out which kind of declaration you are working with. The dynamic_cast<> C++ operator could be used for this. The problem is that the required RTTI is only available if the C++ class has a virtual table attached – that is, it uses virtual functions. Another disadvantage is that C++ RTTI is bloated. To avoid these disadvantages, the LLVM developers introduced a self-made RTTI style, which is used throughout the LLVM libraries.
The (abstract) base class of our hierarchy is Decl. To implement the LLVM-style RTTI, a public enumeration containing a label for each subclass must be added. Also, a private member of this type and a public getter are required. The private member is usually called Kind. In our case, this looks as follows:
class Decl {
public:
enum DeclKind { DK_Module, DK_Const, DK_Type,
DK_Var, DK_Param, DK_Proc };
private:
const DeclKind Kind;
public:
DeclKind getKind() const { return Kind; }
};
Each subclass now needs a special function member called classof. The purpose of this function is to determine if a given instance is of the requested type. For VariableDeclaration, it is implemented as follows:
static bool classof(const Decl *D) {
return D->getKind() == DK_Var;
}
Now, you can use the special templates, llvm::isa<>, to check if an object is of the requested type and llvm::dyn_cast<> to dynamically cast the object. More templates exist, but these two are the most commonly used ones. For the other templates, see https://llvm.org/docs/ProgrammersManual.htmlthe-isa-cast-and-dyn-cast-templatesand for more information about the LLVM style, including more advanced uses, see https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html.