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

• but sometimes this is not possible and an expression can have a set of possible types.

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 and , with a natural coercion due to the inclusion

 (2)

But what could we do if we had the three types , /p and /m for two different integers m and p?
• There is no natural coercion between /p and /m.
• So the type of an expression like 1 + 2 and consequently the signature of + may also depend on what is done with 1 + 2.

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'.

• For simplicity, we restrict here to unary functions.
• We assign to each subexpression E of E' a synthesized attribute E.types which is the set of possible types for E.
• These attributes can be computed by the following translation scheme.

 E' E { E'.types := E.types } E { E.types := lookup(id.entry) } E E1[E2] { E.types := {t  |  ( s E2.types)  |  (s t) 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

• determining if a unique type can be assigned to each subexpression E of E' and
• generating the code for evaluating each subexpression E of E'.
This is done by
• assigning an inherited attribute E.unique to each subexpression E of E' such that
• either E can be assigned a unique type and E.unique is this type,
• or E cannot be assigned a unique type and E.unique is .
• assigning a synthesized attribute E.code which is the target code for evaluating E and
• executing the following translation scheme (where the attributes E.types have already been computed).

 E' { E.unique := if E'.types = {t} then t else } E { E'.code := E.code } E { E.code := generate( .lexeme ':' E.unique) } E { t := E.unique } { S := {s E2.types  |  (s t) E1.types} } { E2.unique := if S = {s} then s else } { E1.unique := if S = {s} then s t else } E1[E2] { code := generate(apply':' E.unique) } { E.code := E1.code | | E2.code | | code