LARA

Labs 05

This week you will implement type checking in your Tool compiler. After this step, you will have completed the front-end of your compiler. This means that it will be able to reject all invalid programs, and accept all valid programs. You will then be able to turn these valid inputs into assembly code that runs on the JVM, just like javac and scalac do. Isn't that great? We think it's great.

Type checking

A valid Tool program has the following properties:

  • It follows the Tool concrete syntax.
  • It respects all the constraints mentioned in Labs 04.
  • Method overriding respects some typing constraints:
    • The overriding method must have exactly as many arguments as the overridden one.
    • The types of the parameters in the overriding and overridden methods must match exactly (no contravariance allowed).
    • The return type must match exactly (no covariance allowed, as opposed to Java, for instance).
  • All expressions typecheck and have the expected type (the returned expression matches the declared return type, for instance).
  • All statements typecheck.

Your goal in this assignment is to enforce all the constraints not enforced already by the previous phases.

Note: The language and the type rules presented in the course may differ from the rules of Tool. If there are any differences, please use the description on the current page for your implementation, and not the rules in the lecture. Of course, feel free to clarify with us if you have any changes.

Types

The following primary types exist in Tool (note that we prefix them with T to differentiate them from the tree nodes with the same name, for instance):

  • TInt
  • TBoolean
  • TInt[]
  • TString 1)

Additionally, we have object types:

  • TClass[name]

We define a subtyping relation on these types. All primary types are subtypes of themselves and of no other type. For instance:

  • TInt <: TInt

All object types are subtypes of themselves and the special “Object” object type. The subtyping relation is also transitive.

  • TClass[name] <: TClass[name] and TClass[name] <: TClass[Object]
  • TClass[B] <: TClass[A] and TClass[C] <: TClass[B] implies TClass[C] <: TClass[A]

With this in mind, we give some of the non-trivial typing constraints. This is naturally not an exhaustive list of what you should implement, but we expect you to be able to deduce the other rules unambiguously yourself (if in doubt about a rule, ask on the Moodle forum).

Overloaded "+"

The “+” operator can represent integer addition, or string concatenation. If the types of e1 and e2 are T1 and T2 respectively, we have for the type Ts of e1 + e2:

  • T1 = TInt and T2 = TIntTs = TInt
  • T1 = TString and T2 = TIntTs = TString
  • T1 = TInt and T2 = TStringTs = TString
  • T1 = TString and T2 = TStringTs = TString

All other values for T1 and T2 should result in type errors.

Comparison operator

The “==” operator is also overloaded. Expression e1==e2 is type correct if and only if one of the following two cases apply:

  • e1 and e2 are both primitive types, and these types are equal
  • e1 and e2 are both classes (in which case they can be different classes)

Note that it is not type correct to compare a primitive type to a class type.

Note that strings and arrays of integers are considered primitive!

Consider the following code.

class A {}
class B {}

Let e1:T1 and e2:T2. Then the following table summarizes some of the cases for e1 == e1.

T1 T2 type checks?
int int yes
string string yes
string A no
A int no
A B yes
A int[] no
int int[] no

Method calls

The dereferenced object must be of an object type, and its class must declare the method called. The number of arguments must of course match the number of parameters. The arguments passed must be subtypes of the declared parameters (matching one-by-one).

Assignment

Assignment of an expression of type T can only be done to a variable of type S such that T <: S.

Assignment to array elements can only be done through an array variable, and the index must be an integer expression. The assigned value must be an integer.

This

this is always considered to carry the object type corresponding to the class where it occurs.

Returned expression

The returned expression must be of a subtype of the declared return type.

Printing out

We will consider println calls to be valid if the argument is an integer, a Boolean or a string. If you want to accept statements that print objects or array you are free to do it, but please let us know in a README file or something similar.

Suggested implementation

Here are the steps we suggest you take:

  • Modify your analyzer such that it attaches types to the various symbols. Since symbols are shared, this has the advantage that you can recover the proper type from any occurrence of the symbol.
  • Modify your analyzer so that it enforces the overriding type constraints on methods.
  • Implement your typechecker. Make sure you attach the types to the expression subtrees (this will be required for code generation, to differentiate between the versions of the overloaded operators, for instance).
  • While you typecheck expressions, attach the proper symbols to the occurences of method calls (since you can now determine the class from which they are called 2).
  • Test, test, test!

User-friendliness

It is very important that your compiler does not stop at the first type error! TypeChecker.scala contains some hints on how to achieve this. Detect as many errors as possible!

Stubs

As usual, merge the stubs of the parser into your main branch by typing

git fetch –all git merge origin/Lab05

(assuming that origin is the name of your remote repository).

It should merge the following files in your project. If you get conflicts, don't panic, they should be relatively easy to resolve.

toolc
  ├── Main.scala               (updated this week)
  │
  ├── lexer
  │    ├── Lexer.scala      
  │    └── Tokens.scala     
  │
  ├── analyzer
  │    ├── NameAnalysis.scala
  │    ├── Symbols.scala       (updated this week)
  │    ├── TypeChecking.scala  (stubs provided this week)
  │    └── Types.scala         (stubs provided this week)
  │
  ├── ast
  │    ├── Parser.scala
  │    ├── Printer.scala
  │    └── Trees.scala         (updated this week)
  │
  └── utils
       ├── Context.scala
       ├── Positioned.scala
       ├── Reporter.scala
       └── Pipeline.scala

Deliverables

As usual, please choose a commit from your git repository as a deliverable on our server before Tuesday, Dec. 01st, 11.59pm (23h59).


1)
We consider String to be a primary type, unlike in Java where it is a proper class. No methods can be called on Strings, for instance.
2)
Although what you are able to determine may not match the run-time types.