Bash++
Bash++ compiler internal documentation
Public Member Functions | Protected Attributes | Private Member Functions | List of all members
AST::BaseListener< Derived > Class Template Reference

CRTP base class for AST listeners. CRTP is a kind of language hack that makes static polymorphism possible in C++. More...

#include <BaseListener.h>

Public Member Functions

virtual ~BaseListener ()=default
 
void walk (std::shared_ptr< AST::ASTNode > node)
 
void set_has_errors (bool has_errors)
 

Protected Attributes

bool program_has_errors = false
 

Private Member Functions

Derived & self ()
 

Detailed Description

template<class Derived>
class AST::BaseListener< Derived >

CRTP base class for AST listeners. CRTP is a kind of language hack that makes static polymorphism possible in C++.

Instead of using runtime virtual dispatch and RTTI (which incur performance penalties), the base class is declared as a template, and the derived class is instantiated as class Derived : public Base<Derived> (i.e., passes itself as the template parameter)

Normally, a base class could never call a derived class's functions directly, because those derived functions could not possibly have been declared at the time the base class was defined (this results in a compiler error, the compiler says "what function's that then? I haven't heard of it yet").

But doing things this way means that the base class is not as yet "defined" until the derived class is defined (since the derived class is needed to instantiate the base class template).

The result is that the base class can now call functions of the derived class directly, STATICALLY, at compile-time.

This allows us to avoid the overhead of virtual function calls and RTTI lookups, while still allowing the derived class to implement only the functions it cares about.

If this is a bit confusing, that's fine. Some people call CRTP an "advanced C++ technique" – this is code for TOTAL HACK. Hacky tricks like this are bound to be confusing. The important thing is that it works.

Template Parameters
DerivedThe derived listener class.

Constructor & Destructor Documentation

◆ ~BaseListener()

template<class Derived >
virtual AST::BaseListener< Derived >::~BaseListener ( )
virtualdefault

Member Function Documentation

◆ self()

template<class Derived >
Derived & AST::BaseListener< Derived >::self ( )
inlineprivate

◆ set_has_errors()

template<class Derived >
void AST::BaseListener< Derived >::set_has_errors ( bool  has_errors)
inline

◆ walk()

template<class Derived >
void AST::BaseListener< Derived >::walk ( std::shared_ptr< AST::ASTNode node)
inline

Member Data Documentation

◆ program_has_errors

template<class Derived >
bool AST::BaseListener< Derived >::program_has_errors = false
protected

The documentation for this class was generated from the following file: