TC-5 Goals
Things to learn during this stage that you should remember:
- Smart pointers
The techniques used to implement reference counting via the redefinition of
operator->
andoperator*
.std::unique_ptr
are also smart pointers.- std::unique_ptr
The intermediate translation is stored in an
unique_ptr
to guarantee it is released (delete
) at the end of the run.- Reference counting
The class template
misc::ref
provides reference counting smart pointers to ease the memory management. It is used to handle nodes of the intermediate representation, especially because during TC-6 some rewriting might transform this tree into an DAG, in which case memory deallocation is complex.- Variants
C++ features the
union
keyword, inherited from C. Not only isunion
not type safe, it also forbids class members. Some people have worked hard to implementunion
à la C++, i.e., with type safety, polymorphism etc. These union are called “discriminated unions” or “variants” to follow the vocabulary introduced by Caml. See the papers from Andrei Alexandrescu: Discriminated Unions (i), Discriminated Unions (ii), Generic: Discriminated Unions (iii) for an introduction to the techniques. We usemisc::variant
intemp
.
Attention
Akim strongly encourages you to read these enlightening articles.
- Default copy constructor, default assignment operator
The C++ standard specifies that unless specified, default implementations of the copy constructor and assignment operator must be provided by the compiler. There are some pitfalls though, clearly exhibited in the implementation of
misc::ref
(misc ref).- Template template parameters
C++ allows several kinds of entities to be used as template parameters. The most well known kind is “type”: you frequently parameterize class templates with types via
template <typename T>
ortemplate <class T>
. You may also parameterize with a class template. Thetemp
module heavily uses this feature: understand it, and be ready to write similar code.- Explicit template instantiations
You must know how to explicitly instantiate templates, and what they can be used for. The implementation of
temp::Identifier
(andtemp::Temp
andtemp::Label
) is based on these ideas. See the corresponding rule in File Conventions for some explanations on this topic.- Covariant return
C++ supports covariance of the method return type. This feature is crucial to implement methods such as
clone
, as inframe::Access::clone()
. Understand return type covariance.- Lazy/delayed computation
The ‘Ix’, ‘Cx’, ‘Nx’, and ‘Ex’ classes delay computation to address context-dependent issues in a context independent way.
- Intermediate Representations, a different approach of hierarchies
In this project, the AST is composed of different classes related by inheritance (as if the kinds of the nodes were class members). Here, the nodes are members of a single class, but their nature is specified by the object itself (as if the kinds of the nodes were object members).
- Stack Frame, Activation Record
The implementation of recursion and automatic variables.
- Inner functions and their impact on memory management at runtime
Reaching non local variables.