type checking 66.648 compiler design lecture (02/25/98) computer science rensselaer polytechnic

18
Type Checking Type Checking 66.648 Compiler Design Lecture 66.648 Compiler Design Lecture (02/25/98) (02/25/98) Computer Science Computer Science Rensselaer Polytechnic Rensselaer Polytechnic

Upload: mervin-gilbert

Post on 18-Dec-2015

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Type Checking 66.648 Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic

Type CheckingType Checking

66.648 Compiler Design Lecture (02/25/98)66.648 Compiler Design Lecture (02/25/98)

Computer ScienceComputer Science

Rensselaer PolytechnicRensselaer Polytechnic

Page 2: Type Checking 66.648 Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic

Lecture OutlineLecture Outline

• Types and type checkingTypes and type checking

• Poly-morphic functionsPoly-morphic functions

• AdministrationAdministration

Page 3: Type Checking 66.648 Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic

ErrorsErrors

We are in chapter 6 of the text book. Please read We are in chapter 6 of the text book. Please read

that chapter.that chapter.

Compiler must check that the source program Compiler must check that the source program follows both the syntactic and semantic follows both the syntactic and semantic conventions of the source language.conventions of the source language.

Static checks - reported during the compilation Static checks - reported during the compilation phase.phase.

Dynamic checks - occur during the execution of Dynamic checks - occur during the execution of the program.the program.

Page 4: Type Checking 66.648 Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic

Static ChecksStatic Checks1. Type checks: A compiler should report an error 1. Type checks: A compiler should report an error

if an operator is applied to an incompatible if an operator is applied to an incompatible operand.operand.

2. Flow of control checks: Places for transfer of 2. Flow of control checks: Places for transfer of control should be specified.control should be specified.

3. Uniqueness checks: An object must be defined 3. Uniqueness checks: An object must be defined exactly (like the type of an identifier, statements exactly (like the type of an identifier, statements inside a case/switch statement.)inside a case/switch statement.)

4. Name Related checks: Same name must appear 4. Name Related checks: Same name must appear two or more times.two or more times.

Page 5: Type Checking 66.648 Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic

Procedure fooProcedure foo

end foo;end foo;

Type information gathered by a type checker may Type information gathered by a type checker may be needed when code is generated. be needed when code is generated.

In general operators could be overloaded. (What is In general operators could be overloaded. (What is the case in Java?)the case in Java?)

ExampleExample

Page 6: Type Checking 66.648 Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic

The design of a type checker for a language is The design of a type checker for a language is based on the information about the syntactic based on the information about the syntactic constructs in the language.constructs in the language.

Each expression has a type associated with it.Each expression has a type associated with it.

Types are either basic or constructed. Basic types Types are either basic or constructed. Basic types are atomic with no internal structure as far as are atomic with no internal structure as far as the program is constructed. Pointers, arrays, the program is constructed. Pointers, arrays, functions, structures can be treated as functions, structures can be treated as constructor types.constructor types.

Type SystemType System

Page 7: Type Checking 66.648 Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic

Type ExpressionsType Expressions

A type expression is either a basic type or is A type expression is either a basic type or is formed by applying an operator called type formed by applying an operator called type constructor to other expressions.constructor to other expressions.

1. A basic type is a type expression. “void” is also 1. A basic type is a type expression. “void” is also a basic type.a basic type.

2. A type name is a type expression.2. A type name is a type expression.

3. A type constructor applied to a type expression 3. A type constructor applied to a type expression is a type expression.is a type expression.

Page 8: Type Checking 66.648 Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic

ConstructorsConstructors

1. Array : If T is a type expression, then array(I,T) 1. Array : If T is a type expression, then array(I,T) is a type expression, where I is an index set.is a type expression, where I is an index set.

2. Products: If T_1 and T_2 are type expressions, 2. Products: If T_1 and T_2 are type expressions, then so is T_1 x T_2then so is T_1 x T_2

3. Structures: differs from the products as fields 3. Structures: differs from the products as fields have names. The struct type constructor have names. The struct type constructor applied to tuple formed from field names and applied to tuple formed from field names and field types.field types.

4. Pointers: If T is a type expression, then 4. Pointers: If T is a type expression, then pointer(T) is a type expression.pointer(T) is a type expression.

Page 9: Type Checking 66.648 Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic

Constructors -ContdConstructors -Contd

5. Functions: Functions in a programming 5. Functions: Functions in a programming language map a domain type D to a range type language map a domain type D to a range type R. Often, for implementation reasons, there are R. Often, for implementation reasons, there are limitations on the types a function can return.limitations on the types a function can return.

6. Objects: Objects are also type. We will talk 6. Objects: Objects are also type. We will talk about objects in the next lecture.about objects in the next lecture.

Type Expressions may contain variables whose Type Expressions may contain variables whose values are type expressions.values are type expressions.

Page 10: Type Checking 66.648 Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic

Type SystemsType SystemsA type system is a collection of rules for assigning A type system is a collection of rules for assigning

type expressions to the various parts of a type expressions to the various parts of a program. A type checker implements a type program. A type checker implements a type system.system.

The type of an array includes the index set of an The type of an array includes the index set of an array, so a function with an array argument can array, so a function with an array argument can only be applied to arrays with that index set.only be applied to arrays with that index set.

Page 11: Type Checking 66.648 Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic

Static and Dynamic Checking Static and Dynamic Checking of Typesof Types

A sound type system eliminates the need for A sound type system eliminates the need for dynamic checking for type errors. A language is dynamic checking for type errors. A language is strongly typed if its compiler can guarantee that strongly typed if its compiler can guarantee that the programs it accepts will execute without the programs it accepts will execute without type errors.type errors.

E.g. array bound checking.E.g. array bound checking.

Page 12: Type Checking 66.648 Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic

Error RecoveryError Recovery

Since type checking has the potential for catching Since type checking has the potential for catching errors in programs, it is important to do errors in programs, it is important to do something when an error is discovered.something when an error is discovered.

The inclusion of error handling may result in a The inclusion of error handling may result in a type system that goes beyond the one needed type system that goes beyond the one needed to specify correct programs.to specify correct programs.

Page 13: Type Checking 66.648 Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic

Specifications of a simple type Specifications of a simple type checkerchecker

The type of each identifier must be declared before The type of each identifier must be declared before the identifier is used. The type checker is a the identifier is used. The type checker is a translation scheme that synthesizes the type of translation scheme that synthesizes the type of each expression from the type of its sub-each expression from the type of its sub-expressions.expressions.

P --> D; EP --> D; E

D--> D; D | id: TD--> D; D | id: T

T--> char | integer | array[num] of T | * TT--> char | integer | array[num] of T | * T

E--> literal | num | id | E mod E | E[E] | E*E--> literal | num | id | E mod E | E[E] | E*

Base Types: char, integer, type-errorBase Types: char, integer, type-error

Page 14: Type Checking 66.648 Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic

Translation SchemeTranslation Scheme

P --> D; EP --> D; E

D--> D;DD--> D;D

D--> id :T { addtype(id.entry,T.type);}D--> id :T { addtype(id.entry,T.type);}

T--> char {T.type= char;}T--> char {T.type= char;}

T--> integer {T.type=integer;}T--> integer {T.type=integer;}

T-->*T_1 {T.type=pointer(T_1.type);}T-->*T_1 {T.type=pointer(T_1.type);}

T--> array[num] of T_1 { T.type T--> array[num] of T_1 { T.type =array(1..num.val,T_1.type); }=array(1..num.val,T_1.type); }

Page 15: Type Checking 66.648 Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic

Type Checking of ExpressionsType Checking of Expressions

E--> literal { E.type=char;}E--> literal { E.type=char;}

E--> num { E.type =integer;}E--> num { E.type =integer;}

E--> id { E.type =lookup(id.entry);}E--> id { E.type =lookup(id.entry);}

E--> E_1 mod E_2 { E.type =If (E_1.type ==integer) E--> E_1 mod E_2 { E.type =If (E_1.type ==integer) if (E_2. Type ==integer) integer; else type-error;if (E_2. Type ==integer) integer; else type-error;

E--> E_1[E_2] { E.type=if ((E_2.type==integer)&& E--> E_1[E_2] { E.type=if ((E_2.type==integer)&& (E_1.type==array(s,t)) t; else type-error;}(E_1.type==array(s,t)) t; else type-error;}

E--> *E_1 { E.type = if (E_1.type ==pointer(t)) t else E--> *E_1 { E.type = if (E_1.type ==pointer(t)) t else type-error;type-error;

Page 16: Type Checking 66.648 Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic

Type Checking for StatementsType Checking for Statements

S--> id=E { if (id.type==E.type) void; else type-S--> id=E { if (id.type==E.type) void; else type-error;}error;}

S--> if E then S { if (E.type==boolean) S_1.type; S--> if E then S { if (E.type==boolean) S_1.type; else type-error;}else type-error;}

S--> While E do S { if (E.type==boolean) S_1.type; S--> While E do S { if (E.type==boolean) S_1.type; else type-error; else type-error;

S--> S; S; { if (S_1.type==void) if (S_2.type ==void) S--> S; S; { if (S_1.type==void) if (S_2.type ==void) void; else type-error;}void; else type-error;}

Page 17: Type Checking 66.648 Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic

Type Checking of FunctionsType Checking of Functions

E--> E(E)E--> E(E)

T--> T ‘->’ T { T.type = T1.type -> T2.type}T--> T ‘->’ T { T.type = T1.type -> T2.type}

E--> E(E) {E.type = I f ((E_2.type ==s) && (E_1.type E--> E(E) {E.type = I f ((E_2.type ==s) && (E_1.type == s--> t)) t; else type-error; == s--> t)) t; else type-error;

Page 18: Type Checking 66.648 Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic

Comments and FeedbackComments and Feedback

Project 2 is out. Please start working. PLEASE do Project 2 is out. Please start working. PLEASE do not wait for the due date to come.not wait for the due date to come.

We are in chapter 6. We will do the rest of chapter We are in chapter 6. We will do the rest of chapter 6 in the next class. Please keep studying this 6 in the next class. Please keep studying this material. It may look difficult.material. It may look difficult.