Do we need to check the inheritance graph of a unidirectional language for loops?

I am trying to write a compiler for " COOL ". As written in the manual for this language, each class must have at most one parent, and parent classes are children of the Object class.

For the semantic analysis phase of the compiler, we need to check if there is a loop in the inheritance hierarchy. for example, if class a is the parent of class b and class b is the parent of class c, then class c cannot be the parent of a!

But what I figured out is that it is not possible for one legacy language to have loops in the inheritance graph if we give the parent classes "object" as their parent. We just need to see, not somewhere in the code, someone declared the father "Object"!

I'm right?

+3


source to share


1 answer


Consider the following two classes:

class A extends B {}

class B extends A {}

      



In some languages, most notably C ++, the above would be illegal because you cannot reference B before declaring it, and you cannot inherit from a class that has only been declared forward. Thus, in such languages, an explicit loop check is not required, since it is impossible to construct such a loop without exploiting other errors.

However, in many other languages, including Java, C # and presumably COOL, it is perfectly possible to inherit from a class that is only defined later in a file (or another file without worrying about include-order). Thus, in these languages, the above code would be legal unless the compiler explicitly detected the loop and rejected the code because of it.

+2


source







All Articles