You are on page 1of 27

CHAPTER 6 TYPE CHECKING

1.Static Checking
 Check that the source program follows both
the syntactic and semantic conventions of the
source language
 Examples of static check
 Type checks (incompatible operand)
 Flow-of-control checks (break statement)
 Uniqueness checks (uniquely declared identifier)
 Name-related checks

1
CHAPTER 6 TYPE CHECKING

2.Position of type checker

Intermedi
Token Syntax Type Syntax
parser ate code Intermediate
stream tree tree
checker generator representation
Notes: 1)A type checker verifies that the type of
a construct matches that expected by its
context.
2)Type information gathered by a type
checker may be needed when code is generated.
3)A symbol that can represent different
operations in different context is said to be 2
“overloaded”
CHAPTER 6 TYPE CHECKING

1. Type Expressions
 Denote the type of a language construct
1) A basic type is a type expression.
2) A type name is a type expression.
3) A type constructor applied to type
expression is a type expression. Constructors
include:
Arrays, products, records,pointers, functions
4) Type expressions may contain variables
whose values are type expressions.
Notes: A convenient way to represent a type 3
expression is to use a graph
CHAPTER 6 TYPE CHECKING
2. Type Systems

 A type system is a collection of rules for assigning


type expressions to the various parts of a program.

Note: 1)A type checker implements a type system

2)The type systems are specified in a syntax-directed


manner

3)Different type systems may be used by different


compilers or processors of the same language
4
CHAPTER 6 TYPE CHECKING

3. Static and Dynamic Checking of Types


 Static Checking (Done by a compiler)
 Dynamic Checking (Checking after run)
 Strongly typed language (if its compiler can
guarantee that the programs it accepts will execute
without type error)
4. Error Recovery

5
SPECIFICATION OF A SIMPLE TYPE CHECKER

1. A simple language
1)Grammar for source language
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

6
SPECIFICATION OF A SIMPLE TYPE CHECKER

1. A simple language
2)The part of a translation scheme that saves the type of an
identifier.
PD;E
DD;D
D id:T {addtype(id.entry, T.type)}
T char {T.type=char}
T  integer {T.type=integer}
T  array [num] of T1 {T.type=array(1..num,T1.type)}
T  T1 {T.type=pointer(T1.type)}

7
SPECIFICATION OF A SIMPLE TYPE CHECKER
2. Type Checking of Expressions
E literal {E.type=char}

E num {E.type=integer}

E id {E.type=lookup(id.entry)}

E E1 mod E2
{E.type= if (E1.type==integer) && (E1.type==integer)
integer
else
type_error}

8
SPECIFICATION OF A SIMPLE TYPE CHECKER
2. Type Checking of Expressions
E E1[E2] {E.type= if (E2.type==integer) &&
(E1.type==array(s,t) t else type_error}

E E1 {E.type= if (E1.type==pointer(t)) t


else type_error}

9
SPECIFICATION OF A SIMPLE TYPE CHECKER

3. Type Checking of Statements


S id:=E {S.type= if (id.type==E.type)
void
else type_error}
S if E then S1 {S.type= if (E.type==boolean)
S1.type
else type_error}

10
SPECIFICATION OF A SIMPLE TYPE CHECKER

3. Type Checking of Statements


S while E do S1 {S.type= if (E.type==boolean)
S1.type
else type_error}
S  S1;S2 {S.type =
if (S1.type==void) && (S2.type== void)
void
else type_error}

11
PASCAL DECLARATIONS
type A = array [1..10] of integer;
type B = array [1..10] of integer;
var x,y: A;
var u,v: B;
var w: array of [1..10] of integer;
Functions: maps elements of one set (domain) to
another set
(range)
E.g., mod: int  int int

Function f (a, b:char):  integer;

Type of f: char  char  pointer (integer)


12
SPECIFICATION OF A SIMPLE TYPE CHECKER
4. Type Checking of Functions

T  T1 ‘’ T2 {T.type= T1 .type T2 .type}

E E1 (E2)
{E.type= if (E2.type==s) &&(E1.type==st)
t
else type_error}

13
TYPE EQUIVALENCE:1

 Types MUST be checked for compatibility and


equivalent
 Examples include
 Assignment statements
 Formal and actual parameters of procedure/function
calls
 Compilers must detect and report any situation
that violates the type incompatibility
 e.g., found integers but compilers expected real
14
TYPE EQUIVALENCE:2

 An important element of any type system


is the mechanism to decide if two
different type declarations are equivalent
 To compare two types, need to understand
the notion of type equivalence
 two types are equivalent iff values of their
types have the same representations
 (i.e., one can be used where the other is required,
and v.s.)

15
TYPE EQUIVALENCE:3
 There are two kinds of type equivalence
 Name equivalence
 Structural equivalence

 STRUCTURAL EQUIVALENCE
 says two types are the same if they are made up of
the same basic types and constructors.
 (difficult to implement)
 NAME EQUIVALENCE
 says two types are the same if their constituents
have the SAME NAMES.
16
 (used for almost all languages)
NAME EQUIVALENCE

Two types are name equivalent


if they have the SAME NAME
e.g.
 Type t1 = Array [1..10] of integer;
 Type t2 = Array [1..10] of integer;
 are they name equivalent? No
because they have distinct type
definitions (names)
17
MORE ON NAME EQUIVALENCE

TYPE t3 = Array [1..10] of Integer;


TYPE t4=t3;
 Are they equivalent? Yes
Name equivalence check is easy by
compiler by just comparing the
pointers

18
EQUIVALENCE OF TYPE EXPRESSIONS

 Names for type expression


type link=  cell

 Cycles in representations of types


type link=  cell;
cell=record
info:integer;
next :link
end;
19
The graph for this type therefore contains a cycle.
STRUCTURAL EQUIVALENCE
 Two types are structurally equivalent iff
they have the same structure
 same set of fields
 same order
 corresponding fields having the equivalent types
 Difficult to implement (parallel traversal of two
type descriptors is needed)
 e.g.,
 TYPE t5 = RECORD c: integer; p:t5; END
 TYPE t6 = RECORD c: integer; p:t6; END
 Examples of languages support structural
type are
 C, C++, and Algol 68
20
NAME & STRUCTURAL EQUIVALENCE
 Most languages allow association of names with type
expressions. This makes type equivalence trickier.
 Example from Pascal:
type link = cell;
var next: link;
last: link;
p: cell;
q,r: cell;
 Do next, last, p, q, and r have the same type?

 In Pascal, it depends on the implementation!

 In structural equivalence, the types would be the


same.
21
 But NAME EQUIVALENCE requires identical
NAMES.
STRUCTURAL EQUIVALENCE
boolean sequiv( s, t )
{
if s and t are the same basic type
return TRUE;
else if s == array( s1, s2 ) and t == array( t1, t2 )
return sequiv( s1, t1 ) and sequiv( s2, t2 )
else s == s1 x s2 and t = t1 x t2 then
return sequiv( s1, t1 ) and sequiv( s2, t2 )
else if s == pointer( s1 ) and t == pointer( t1 )
return sequiv( s1, t1 )
else if s == s1 s2 and t == t1 t2 then
return sequiv( s1, t1 ) and sequiv( s2, t2 )
return false
} 22

Try: int foo( int, float )


RELAXING STRUCTURAL EQUIVALENCE

 We don’t always want strict structural equivalence.

 E.g. for arrays, we want to write functions that accept


arrays of any length.
 To accomplish this, we would modify sequiv() to accept
any bounds:

else if s == array( s1, s2 ) and t == array( t1, t2 )
return sequiv( s2, t2 )

23
CYCLIC TYPES
 The situation in C is slightly different, since it is
impossible to refer to an undeclared name.
typedef struct _cell {
int info;
struct _cell *next;
} cell;
typedef *cell link;
 But the name link is just shorthand for
(struct _cell *).
 C uses name equivalence for structs to avoid recursion
(after expanding typedef’s).
 But it uses structural equivalence elsewhere.

24
TYPE CONVERSION
 Coercions
 Implicit type conversions
 Suppose we encounter an expression

 x+i where x has type float and i has


type int.
 CPU instructions for addition could take EITHER float OR int
as operands, but not a mix.
 This means the compiler must sometimes convert the
operands of arithmetic expressions to ensure that
operands are consistent with operators.
 With postfix as an intermediate language for expressions,
we could express the conversion as follows:
25
x i inttoreal float+
 where real+ is the floating point addition operation.
TYPE COERCION
 If type conversion is done by the compiler without the
programmer requesting it, it is called IMPLICIT
conversion or type COERCION.
 EXPLICIT conversions are those that the
programmer
specifices, e.g.
x = (int)y * 2;
 Implicit conversion of CONSTANT expressions
should be done at compile time.

26
TYPE CHECKING EXAMPLE WITH COERCION
Production Semantic Rule
E -> num E.type := integer
E -> num . num E.type := real
E -> id E.type := lookup( id.entry )
E -> E1 op E2 E.type := if E1.type == integer and E2.type == integer
then integer
else if E1.type == integer and E2.type == real
then real
else if E1.type == real and E2.type == integer
then real
else if E1.type == real and E2.type == real
then real
else type_error
27

You might also like