<<

Semantic Analysis •Last time: CS412/CS413 – Semantic errors related to scopes Introduction to – Symbol tables Tim Teitelbaum •This lecture: – Semantic errors related to types Lecture 13: Types and Type-Checking – concepts 21 Feb 05 – Types and type-checking

CS 412/413 Spring 2005 Introduction to Compilers 1 CS 412/413 Spring 2005 Introduction to Compilers 2

What Are Types? How to Ensure Type-Safety

•Typesdescribe the values computed during the • Bind (assign) types, then check types execution of the program •Type binding:defines type of constructs in the • Essentially, types are predicate on values, .g., program (e.g., variables, functions) “int x” in Java means “x ∈ [-231, 231)” – Can be either explicit (int x) or implicit (x = 1) – Type consistency (safety) = correctness with respect • Type errors: improper, type-inconsistent to the type bindings operations during program execution • Type checking: determine if the program correctly uses the type bindings •Type-safety: absence of type errors – Consists of a set of type-checking rules

CS 412/413 Spring 2005 Introduction to Compilers 3 CS 412/413 Spring 2005 Introduction to Compilers 4

Type Checking Static vs. Dynamic Typing • Type checking: static semantic checks to • Static and dynamic typing refer to type enforce the of the program definitions (i.e., bindings of types to variables, expressions, etc.) •Examples: – Statically typed language: types are defined and – Unary and binary operators (e.g., +, ==, [ ]) must checked at compile-time, and do not change during receive operands of the proper type the execution of the program – Functions must be invoked with the right number • E.g., , Java, Pascal and type of arguments – Return statements must agree with the return type – Dynamically typed language: types defined and checked at run-time, during program execution – In assignments, assigned value must be compatible • E.g., Lisp, with type of variable on LHS. – Class members accessed appropriately

CS 412/413 Spring 2005 Introduction to Compilers 5 CS 412/413 Spring 2005 Introduction to Compilers 6

1 Strong vs. Weak Typing Soundness

• Strong and weak typing refer to how much • Sound type systems: can statically ensure that type consistency is enforced the program is type-safe • Soundness implies strong typing – Strongly typed languages: guarantees that accepted programs are type-safe • Static type safety requires a conservative – Weakly typed languages: allow programs that approximation of the values that may occur contain type errors during all possible executions – May reject type-safe programs • Can achieve strong typing using either – Need to be expressive: reject as few type-safe static or dynamic typing programs as possible

CS 412/413 Spring 2005 Introduction to Compilers 7 CS 412/413 Spring 2005 Introduction to Compilers 8

Concept Summary Classification

Strong Typing Weak Typing

ML Pascal C • Static vs dynamic typing: when to define/check types? Static Typing -3 C++ • Strong vs weak typing: how many type errors? Java Scheme • Sound type systems: statically catch all type errors PostScript assembly code Dynamic Typing Smalltalk

CS 412/413 Spring 2005 Introduction to Compilers 9 CS 412/413 Spring 2005 Introduction to Compilers 10

Why Static Checking? Type Systems

• Efficient code • Type is predicate on value – Dynamic checks slow down the program

• Guarantees that all executions will be safe • Type expressions: describe the possible types in – Dynamic checking gives safety guarantees only for the program: int, string, array[], Object, etc. some execution of the program • Type system: defines types for language •But is conservative for sound systems constructs (e.g., expressions, statements) – Needs to be expressive: reject few type-safe programs

CS 412/413 Spring 2005 Introduction to Compilers 11 CS 412/413 Spring 2005 Introduction to Compilers 12

2 Type Expressions Type Expressions: Arrays • Language type systems have basic types • Various kinds of array types in different (also: primitive types, ground types) programming languages • Basic types examples: int, string, bool • array() : arrays without bounds – C, Java: T [ ], Modula-3: array of T • Build type expressions using basic types: • array(T, S) : array with size – Type constructors: – C: T[S], Modula-3: array[S] of T array types – May be indexed 0..S-1 structure types • array(T,L,U) : array with upper/lower bounds pointer types – Pascal: array[L .. U] of T –Type aliases • array(T, S1, …, Sn) : multi-dimensional arrays – Function types – : T(L1,…, Ln)

CS 412/413 Spring 2005 Introduction to Compilers 13 CS 412/413 Spring 2005 Introduction to Compilers 14

Type Expressions: Structures Type Expressions: Aliases

• More complex type constructor • Some languages allow type aliases (type definitions, equates) •Has form {id1: T1, … , idn: Tn} for some identifiers idi and types Ti – C: typedef int int_array[ ]; • Is essentially cartesian product: – Modula-3: type int_array = array of int; – Java doesn’t allow type aliases (id1 x T1 ) x … x (idn x Tn ) • Supports access operations on each field, • Aliases are not type constructors! with corresponding type – int_array is the same type as int [ ] • Structures in C: struct { int a; float b; } • Records in Pascal: record a: integer; b: real; end • Different type expressions may denote the • Objects: extension of structure types same type

CS 412/413 Spring 2005 Introduction to Compilers 15 CS 412/413 Spring 2005 Introduction to Compilers 16

Type Expressions: Pointers Type Expressions: Functions

• Pointer types characterize values that are •Type: T1×T2 × … × Tn → Tr addresses of variables of other types • Function value can be invoked with some argument expressions with types T , returns •Pointer(T): pointer to an object of type T i return type Tr

• C pointers: T* (e.g., int *x;) • C functions : int f(float x, float y) • Pascal pointers: ^T (e.g., x: ^integer;) • Java: methods have function types

• Some languages have first-class function types • Java: object references (C, ML, Modula-3, Pascal, not Java)

CS 412/413 Spring 2005 Introduction to Compilers 17 CS 412/413 Spring 2005 Introduction to Compilers 18

3 Implementation Type Comparison

• Use a separate class hierarchy for types: • Option 1: implement a method T1.Equals(T2) class BaseType extends Type { String name; } – Must compare type trees of T1 and T2 class IntType extends BaseType { … } – For object-oriented language: also need sub-typing: class BoolType extends Base Type { … } T1.SubtypeOf(T2) class ArrayType extends Type { Type elemType; } class FunctionType extends Type { … } • Option 2: use unique objects for each distinct type – each type expression (e.g. array[int] ) resolved to • Semantic analysis translates all type same type object everywhere expressions to type objects – Faster type comparison: can use == • Symbol table binds name to type object – Object-oriented: check subtyping of type objects

CS 412/413 Spring 2005 Introduction to Compilers 19 CS 412/413 Spring 2005 Introduction to Compilers 20

Creating Type Objects Processing Type Declarations • Build types while parsing – use a syntax- • Type declarations add new identifiers and directed definition: their types in the symbol tables • Class definitions must be added to symbol non terminal Type type table: type ::= BOOLEAN class_defn ::= CLASS ID:id { decls: } {: RESULT = new BoolType(id); :} • Forward references require multiple passes | ARRAY LBRACKET type:t RBRACKET over AST to collect legal names {: RESULT = new ArrayType(t); :} class A { B b; } class B { … } • Type objects = AST nodes for type expressions

CS 412/413 Spring 2005 Introduction to Compilers 21 CS 412/413 Spring 2005 Introduction to Compilers 22

Type-Checking Type-Checking • Type-checking = verify typing rules •Option 2: first build the AST, then implement type-checking by recursive traversal of the AST “operands of + must be integer expressions; the result nodes: is an integer expression” class Add extends Expr { •Option 1:Implement using syntax-directed Type typeCheck(Symtab s) { definitions (type-check during the parsing) Type t1 = e1.typeCheck(s), expr ::= expr:t1 PLUS expr:t2 t2 = e2.typeCheck(s); {: if (t1 == IntType && t2 == IntType) if (t1 == Int && t2 == Int) return Int; RESULT = IntType; else throw new TypeCheckError(“+”); else throw new TypeCheckError(“+”); } :} }

CS 412/413 Spring 2005 Introduction to Compilers 23 CS 412/413 Spring 2005 Introduction to Compilers 24

4 Type-Checking Identifiers Next Time: Static Semantics • Identifier expressions: lookup the type in the symbol table

class IdExpr extends Expr { • Static semantics = mathematical description of Identifier id; typing rules for the language Type typeCheck(Symtab s) { return s.lookupType(id); } • Static semantics formally defines types for all } legal language ASTs

• Using syntax-directed definitions for forward references: type-checking will fail

CS 412/413 Spring 2005 Introduction to Compilers 25 CS 412/413 Spring 2005 Introduction to Compilers 26

5