Next: About this document ...
Up: Compiler Theory: Type Checking
Previous: Type Conversions
AN OVERLOADED OPERATOR may have different meanings depending upon its context.
- Normally overloading is resolved by the types of the arguments,
- but sometimes this is not possible and an expression can have a set of
possible types.
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 |
Next: About this document ...
Up: Compiler Theory: Type Checking
Previous: Type Conversions
Marc Moreno Maza
2004-12-02