Next: , Up: TC-5   [Contents][Index]

4.14.1 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-> and operator*. std::unique_ptrs are also smart pointers.


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.


C++ features the union keyword, inherited from C. Not only is union not type safe, it also forbids class members. Some people have worked hard to implement union à 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 use misc::variant in temp.

I (Akim) strongly encourage 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. You must be able to explain these pitfalls.

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>’ or ‘template <class T>’. But you may also parameterize with a class template. The temp module heavily uses this feature: understand it, and be ready to write similar code.

Explicit template instantiations

You must be able to explain how templates are “compiled”. In addition, you know how to explicitly instantiate templates, and explain what it can be used for. The implementation of temp::Identifier (and temp::Temp and temp::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 in frame::Access::clone(). Understand return type covariance.

Lazy/delayed computation

The ‘Ix’, ‘Cx’, ‘Nx’, and ‘Ex’ classes delay computation to address context-depend 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.

Next: , Up: TC-5   [Contents][Index]