Type Checking Error Checking Dynamic checking takes place while program is running Static checking takes place during compilation Type checks Flow-of-control checks Uniqueness checks Name-related checks CS 5300 - SJAllan 2 1
Types Every expression has an associated type Rules exist to assign a type to an expression, e.g.: If you add, subtract, or multiply two integer operands, the result is an integer If you take the address of an operand of type T, the type of the result is pointer to T In Pascal and C, types are either basic or constructed CS 5300 - SJAllan 3 Type Checking Verifying that the type of a construct matches that expect by its context Type information may be needed when code is generated. Why? A symbol that can represent different operations in different contexts is said to be overloaded Overloading may be accompanied by coercion of types CS 5300 - SJAllan 4 2
Polymorphism Different from overloading The body of a polymorphic function can be executed with arguments of several types CS 5300 - SJAllan 5 Type Expressions A "type expression" is the type of a programming language construct Type expressions include basic types, e.g. integer or real A special basic type, type_error, signals an error during type checking Some languages include a void type CS 5300 - SJAllan 6 3
Type Expressions Applying "type constructors" to type expressions forms new type expressions Arrays Records Pointers A function maps elements from one set (domain) to another set (range) mod has type "int x int int" function f(a, b: char) : integer; (f has type "char x char pointer(integer)") CS 5300 - SJAllan 7 Type Systems A type system is a collection of rules for assigning type expressions A type checker implements a type system Different type systems may be used by different compilers of the same language A type checker should adequately handle error recovery CS 5300 - SJAllan 8 4
Static vs. Dynamic Checks In principle, any check can be done dynamically if the target code carries type information A sound type system eliminates the need for dynamic checking A language is strongly typed of its compiler only accepts programs that have no type errors In practice, some checks can only be done dynamically (e.g., checking array indices) CS 5300 - SJAllan 9 Simple Type Checker P D ; E D D ; D id : T T char integer array [num] of T T E literal num id E mod E E[E] E The simple language above allows a sequence of declarations followed by a single expression A simple syntax-directed definition can specify a type checker for this language CS 5300 - SJAllan 10 5
Simple Type Checker P D ; E D D ; D D id : T T char T integer T T 1 T array[num] of T 1 addtype(id.entry, T.type) T.type := char T.type := integer T.type := pointer(t 1.type) T.type := array(num, T 1.type) CS 5300 - SJAllan 11 Simple Type Checker E literal E num E id E E 1 mod E 2 E E 1 [E 2 ] E E 1 E.type := char E.type := integer E.type := id.type E.type := if E 1.type = integer and E 2.type = integer then integer else type_error E.type := if E 2.type = integer and E 1.type = array(s,t) then t else type_error E.type := if E 1.type = pointer(t) then t else type_error CS 5300 - SJAllan 12 6
Type Checking of Statements S id := E S if E then S 1 S while E do S 1 S S 1 ; S 2 Statements S.type := if id.type = E.type then void else type_error S.type := if E.type = boolean then S 1.type else type_error S.type := if E.type = boolean then S 1.type else type_error S.type := if S 1.type = void and S 2.type = void then void else type_error CS 5300 - SJAllan 13 Type Checking of Functions E E 1 (E 2 ) subprogam_head function id arguments : standard_type ';' Statements id.type := arguments.type standard_type.type E.type := if E 2.type = s and E 1.type = s t then t else type_error CS 5300 - SJAllan 14 7
Type Systems A collection of rules for assigning type expressions to the various parts of a program A type checker implements a type system CS 5300 - SJAllan 15 Type Checkers Static Checking is done by the compiler Dynamic Checking is done at run-time A sound type system eliminates the need for dynamic checking for type errors because it allows us to determine statically that these errors cannot occur at run-time A langue is strongly typed if its compiler can guarantee that the programs it accepts will execute without errors CS 5300 - SJAllan 16 8
Error Recovery If an error is discovered, the type checker should do something reasonable At the very least, it should report the nature and location of the error It is desirable for the type checker to recover from errors, so it can check the rest of the input CS 5300 - SJAllan 17 A Simple Type Checker P D ; E D D ; D id : T T char integer array[num] of T T E literal num id E mod E E[E] E CS 5300 - SJAllan 18 9
Type Checking Scheme P D ; E D D ; D D id : T T char T integer T T 1 T array[num] of T 1 addtype(id.entry, T.type) T.type = char T.type = integer T.type = pointer(t 1.type) T.type = array(num.val, T 1.type) CS 5300 - SJAllan 19 Type System for Expressions E literal E num E id E E 1 mod E 2 E E 1 [E 2 ] E E 1 E.type = char E.type = integer E.type = lookup(id.entry) E.type = if E 1.type = integer and E 2.type = integer then integer else type_error E.type = if E 2.type = integer and E 1.type = array(s,t) then t else type_error E.type = if E 1.type = pointer(t) then t else type_error CS 5300 - SJAllan 20 10
Type System for Statements S id = E S if E then S 1 S while E do S 1 S S 1 ; S 2 S.type = if id.type = E.type then void else type_error S.type = if E.type = boolean then void else type_error S.type = if E.type = boolean then void else type_error S.type = if S 1.type = void and S 2.type = void then void else type_error CS 5300 - SJAllan 21 Type System for Functions T T 1 T 2 E E 1 (E 2 ) T.type = T 1.type T 2.type E.type = if E 2.type = s and E 1.type = s t then t else type_error CS 5300 - SJAllan 22 11
Equivalence of Types Name equivalence Each type name is viewed as a distinct type Two type expressions are name equivalence if and only if they are identical Structural equivalence Names are replaced by the type expressions they define So two type expressions are structurally equivalence if they represent two structurally equivalence type expressions when all names have been substituted out CS 5300 - SJAllan 23 Implicit (coercion) Type Conversion Conversion from one type to another implicitly done by the compiler Explicit Programming must write something to cause the conversion CS 5300 - SJAllan 24 12
Coercion of Expressions E num E num. num E id E E 1 op E 2 E.type = integer E.type = real E.type = lookup(id.entry) E.type = if E 1.type = integer and E 2.type = integer then integer else if E 1.type = integer and E 2.type = real then real else if E 1.type = real and E 2. type = integer then real else if E 1.type = real and E 2.type = real then real else type_error CS 5300 - SJAllan 25 13