UP | HOME

Classes and objects III
Lecture 15

Table of Contents

Review

Questions about the last class?

Quiz

When translating SimpleC to C, how do we distinguish functions in different classes that have the same name? E.g.,

impl A {
  def function() -> int { return 1; }
}

impl B {
  def function() -> string { return "hello"; }
}

Quiz Discussion

Typing rules

cd      ::=  class c { d* }            // class declaration

ci      ::=  impl c { f*  }            // class implementation

t       ::= int                        // integer type
          | bool                       // boolean type
          | string                     // string type
          | (t+) -> t                  // function type
          | class c                    // class type

n       ::= x                          // simple name
          | n . x                      // qualified name



O is the object environment, which maps class names to their scope

S mapping from name to type

O is mapping from name to a symbol table


S, O |- <d*> ~> S'      // add declarations to the class scope
O' = O + {c: S' - S}    // get the classes declarations
------------------------------
S, O |- <class c { d* }> ~> O'


C = lookup(c, O)
S' = S + C + [ this: class c ]
S' |- <f*> : S'
----------------------------
S, O |- <impl c { f* }> ~> O


class c = lookup(x, S)
----------------------  [init]
S |- <init x> ~> S


class c = lookup(x, S)
----------------------  [delete]
S |- <delete x> ~> S


S |- <n> : class c
C = lookup(c, O)
t = lookup(x, C)
----------------------  [qualified substitution]
S, O |- <n.x> : t

class alpha {
  b : class beta;
  constructor : () -> int;
}

if (x < y * 100) {
  // is this branch taken?
} else {
  return x;
}



class beta {
  x : int;
}

def main() -> int {
  a : class alpha;
  init a;
  a.b.x;
}



      QualifiedName
    /               \
  QualifiedName     x
/         \
a          b



visit(a.b.x)
  name=a.b
  ID=x
  visit(a.b)
    name=a
    ID=b
    type of a is class alpha
    lookup(b, class alpha) -> class beta
    return class beta
  type of a.b is class beta
  lookup(x, class beta) -> int





Implementing the typing rules

visitClassDef

  • Get the name of the class
  • Check if it's already declared (scope.hasScope(name))
  • Create a scope for the class
  • Add the class to the current scope
    • new Type.ClassType(name) to create a class type
    • scope.addScope(name, localScope) to add to the current scope
  • Get the classes declarations into its scope
    • What can we use to do this?

visitClassImpl

  • Make sure the class has been declared (see visitClassDef)
  • Get the classes local scope scope.getScope(name)
  • Type-check the function definitions
    • How do we type-check the functions under the class's scope?

visitInit

  • Check that the symbol is declared to be a class
    • hashSymbolAnyScope
    • getTypeAnyScope
    • type.kind() = Type.Kind.CLASS=

visitDelete

  • The same as visitInit

visitClassType

  • Create the class type (see visitClassDef)

visitQualified

  • Check that ctx.name() yields a class type (see visitInit)
  • Get the local scope of the class hasScopeAny
    • This allows you to check whether ctx.ID.getText() has been declared as part of the class
  • Return the type of the ID according to the class declaration
    • Use hasSymbol=/=getSymbol instead of the Any lookups, because we only want the symbols declared in the class, not the parents

Author: Paul Gazzillo

Created: 2023-04-13 Thu 14:59