Type Checking
Type Checking
Type Checking
Winter 2010
Static Checking
Abstract Syntax Tree Decorated Abstract Syntax Tree
Token Stream
Parser
Static Checker
Intermediate Code
Type checks: operator applied to incompatible operands? Flow of control checks: break (outside while?) Uniqueness checks: labels in case statements Name related checks: same name?
Winter 2010
Type Checking
Problem: Verify that a type of a construct matches that expected by its context. Examples: mod requires integer operands (PASCAL) * (dereferencing) applied to a pointer a[i] indexing applied to an array f(a1, a2, , an) function applied to correct arguments. Information gathered by a type checker: Needed during code generation.
based on CSE 504, Stony Brook University 3
Winter 2010
Type Systems
A collection of rules for assigning type expressions to the various parts of a program. Based on: Syntactic constructs, notion of a type. Example: If both operators of +, -, * are of type integer then so is the result. Type Checker: An implementation of a type system. Syntax Directed. Sound Type System: eliminates the need for checking type errors during run time.
Winter 2010
Type Expressions
Implicit Assumptions:
Expressions Statements
Type Constructors
Arrays (strings) Records Sets Pointers Functions
5
Boolean Real
Enumerations Sub-ranges
Void Variables
Winter 2010
Tree
DAG
Winter 2010
Basic Types
Structured Types
(Type Type)
7
{ E.type = sym_lookup(id.entry, type) } {E.type = if E1.type {int, float} | E2.type {int, float}) then error else if E1.type == E2.type == int then int else float }
Winter 2010
{E.type = pointer(E1.type)}
{E.type = if (E1.type = fcn(S, T) E2.type = S, then T else error} {E.type = tuple(E1.type, E2.type)}
10
E (E1, E2)
Winter 2010
{S.type := if id.type = E.type then void else error} {S.type := if E.type = boolean then S1.type else error} {S.type := if E.type = boolean then S1.type}
Example:
We need a precise definition for type equivalence Interaction between type equivalence and type representation
type vector = array [1..10] of real type weight = array [1..10] of real var x, y: vector; z: weight
Name Equivalence: When they have the same name. x, y have the same type; z has a different type. Structural Equivalence: When they have the same structure. x, y, z have the same type.
Winter 2010 based on CSE 504, Stony Brook University 12
Structural Equivalence
Definition: by Induction
Same basic type Same constructor applied to SE Type Same DAG Representation
Do not include array bounds when they are passed as parameters Other applied representations (More compact) Does not check for cycles Later improve it.
Winter 2010
13
Winter 2010
14
Recursive Types
Where: Linked Lists, Trees, etc. How: records containing pointers to similar records Example: type link = cell; cell = record info: int; next = link end Representation: cell = record x x x ptr cell x cell = record x x ptr
Recursive Types in C
C Policy: avoid cycles in type graphs by: Using structural equivalence for all types Except for records name equivalence Example:
Name use: name cell becomes part of the type of the record.
Use the acyclic representation Names declared before use except for pointers to records. Cycles potential due to pointers in records Testing for structural equivalence stops when a record constructor is reached ~ same named record type?
based on CSE 504, Stony Brook University
Winter 2010
16
Overloaded Symbol: one that has different meanings depending on its context Example: Addition operator + Resolving (operator identification): overloading is resolved when a unique meaning is determined. Context: it is not always possible to resolve overloading by looking only the arguments of a function Set of possible types Context (inherited attribute) necessary
based on CSE 504, Stony Brook University 17
Winter 2010
Overloading Example
function * (i, j: integer) return complex; function * (x, y: complex) return complex; * Has the following types: fcn(tuple(integer, integer), integer) fcn(tuple(integer, integer), complex) fcn(tuple(complex, complex), complex) int i, j; k = i * j;
Winter 2010 based on CSE 504, Stony Brook University 18
Narrowing Types
E E E id E E1(E2)
{E.types = E. types {E.types = lookup(id.entry)} {E.types = {s' | s E2.types and ss E1.types} t = E.unique S = {s | s E2.types and St E1.types} E2.unique = if S ={s} then S else error E1.unique = if S = {s} then St else error
Winter 2010 based on CSE 504, Stony Brook University 19
Polymorphic Functions
Defn: a piece of code (functions, operators) that can be executed with arguments of different types. Examples: Built in Operator indexing arrays, pointer manipulation Why use them: facilitate manipulation of data structures regardless of types. Example ML: fun length(lptr) = if null (lptr) then 0 else length(+l(lptr)) + 1
based on CSE 504, Stony Brook University 20
Winter 2010
Type Variables
Why: variables representing type expressions allow us to talk about unknown types.
Application: check consistent usage of identifiers in a language that does not require identifiers to be declared before usage.
Type Inference Problem: Determine the type of a language constant from the way it is used.
Winter 2010
Winter 2010
23
Subsripts i and o distinguish between the inner and outer occurrences of deref, respectively.
Winter 2010
24
Distinct occurrences of a p.f. in the same expression need not have arguments of the same type.
deref ( deref (q)) Replace with fresh variable and remove (i, o)
Use unification: check if s and t can be made structurally equivalent by replacing type vars by the type expression.
Winter 2010
25
Instance: S(t) is an instance of t written S(t) < t. Examples: pointer (integer) < pointer () , int real Unify: t1 t2 if S. S (t1) = S (t2) Most General Unifier S: A substitution S: S (t ) = S (t ) 1 2 S. S (t ) = S (t ) t. S(t) < S(t). 1 2
based on CSE 504, Stony Brook University 26
Winter 2010
fresh (t): replaces bound variables in t by fresh variables. Returns pointer to a node representing result type. fresh( .pointer() ) = pointer(1) 1. unify (m, n): unifies expressions represented by m and n.
Winter 2010
27
: 3 pointer : 2
: 1
o : 1
: 3 pointer : 2
i : 4
m : 6 pointer : 5
: 8
o : 1
Winter 2010
i : 4