next up previous
Next: About this document ... Up: Compiler Theory: Type Checking Previous: Type Conversions

Overloading of Functions and Operators

AN OVERLOADED OPERATOR may have different meanings depending upon its context.

Example 2   In the previous section we were resolving overloading of binary arithmetic operators by looking at the the types of the arguments. Indeed we had two possibles types, say $ \mathbb {Z}$ and $ \mathbb {R}$, with a natural coercion due to the inclusion

$\displaystyle \mbox{${\mathbb Z}$}$  $\displaystyle \subseteq$  $\displaystyle \mbox{${\mathbb R}$}$ (2)

But what could we do if we had the three types $ \mathbb {Z}$, $ \mbox{${\mathbb Z}$}$/p$ \mbox{${\mathbb Z}$}$ and $ \mbox{${\mathbb Z}$}$/m$ \mbox{${\mathbb Z}$}$ for two different integers m and p?


SET OF POSSIBLE TYPES FOR A SUBEXPRESSION. The first step in resolving the overloading of operators and functions occuring in an expression E' is to determine the possible types for E'.

E' $ \longmapsto$ E { E'.types := E.types }
E $ \longmapsto$ $ \bf id$ { E.types := lookup(id.entry) }
E $ \longmapsto$ E1[E2] { E.types := {t  |  ($ \exists$ s $ \in$ E2.types)  |  (s $ \rightarrow$ t) $ \in$ E1.types} }


NARROWING THE SET OF POSSIBLE TYPES. The second step in resolving the overloading of operators and functions in the expression E' consists of

This is done by

E' $ \longmapsto$   { E.unique := if E'.types = {t}
      then t
      else $ \bf type\_error$ }
  E    
    { E'.code := E.code }
E $ \longmapsto$ $ \bf id$ { E.code := generate( $ \bf id$.lexeme ':' E.unique) }
E $ \longmapsto$     { t := E.unique }
      { S := {s $ \in$ E2.types  |  (s $ \rightarrow$ t) $ \in$ E1.types} }
    { E2.unique := if S = {s}
      then s
      else $ \bf type\_error$ }
    { E1.unique := if S = {s}
      then s $ \rightarrow$ t
      else $ \bf type\_error$ }
  E1[E2]    
      { code := generate(apply':' E.unique) }
    { E.code := E1.code | | E2.code | | code


next up previous
Next: About this document ... Up: Compiler Theory: Type Checking Previous: Type Conversions
Marc Moreno Maza
2004-12-02