Memory management for types in complex languages

Viewed 313

I've come across a slight problem for writing memory management with regard to the internal representation of types in a compiler for statically typed, complex languages. Consider a simple snippet in C++ which easily demonstrates a type that refers to itself.

class X {
    void f(const X&) {}
};

Types can have nearly infinitely complex relationships to each other. So, as a compiler process, how do you make sure that they are properly collected?

So far, I've decided that garbage collection might be the right way to go, which I wouldn't be too happy with because I want to write the compiler in C++, or alternatively, just leave them and never collect them for the life of the compile phase for which they are needed (which has a very fixed lifetime) and then collect them all afterwards. The problem with that is that if you had a lot of complex types, you could lose a lot of memory that way.

2 Answers
Related