Type Checking
- Type checking is basically the enforcement of a set of rules
designed to ensure that only valid operands are provided to
the various operations that can be invoked within a program.
- To make the enforcement of such rules possible within a compiler,
the compiler must have an encoding that can be used to
represent the types associated with expressions by the rules
of the type system.
- An obvious approach to handling such type specifications, is to
provide distinct descriptors (i.e. structures or objects)
for type specifications.
- Pointers to type descriptors would be found in:
- the "variable type" fields in declaration descriptors
for variable names,
- return type fields,
- the "type" fields in formal parameter name
descriptors, and
- Structured types are not really "described" by any single
structure. Instead, the descriptors for structured types act
as the roots of trees of descriptors that represent the full
structure of the type.
- The tree structure must be traversed to determine the
type of a complex "variable" like
x.b(3).c
.
- One aspect of the type rules of a language that determines
the degree to which the trees/graphs representing types
must be traversed is the language's definition of type
compatibility or "matching"
- No matter how type equivalence is defined, there are usually lots of things to check.
- The types used on the left and right hand sides of assignment
statements must "match".
- Method calls must be checked both to make
sure that the numbers of actuals and parameters match and that
their types match.
- Warning. The ordering of parameter descriptors can cause
annoying problems.
- You must check the operand types for each built-in operator.
- In general, processing operators can be complicated by
implicit conversion rules (e.g. integers become reals when
added to reals).
- Checking most binary and unary operators is easy in Woolite. They
all take and return integers. The equality operator is an exception
since you can use it to compare values of other types
- Your routine to process variables should check:
- That names used as variables are variables!
- That the type of the sub-variable is some
array type when processing an Nsubs.
- The types required by expressions used in certain contexts are
fixed by the language.
- Most languages require booleans as conditions in ifs and
whiles (Woolite requires integers).
- Semantic processing routines often return values that summarize
the properties of the sub-phrases to which they were applied.
- For example, to make type checking possible
your function to process expressions should
return the type descriptor for the type of the expression.
Computer Science 434
Department of Computer Science
Williams College