<<

Semantic Analysis CS412/413 • Last time: œ Semantic errors related to scopes œ Symbol tables Introduction to Radu Rugina • This lecture: œ Semantic errors related to types œ concepts Lecture 12: Types and Type-Checking 14 Feb 03 œ Types and type-checking

CS 412/413 Spring 2003 Introduction to Compilers 2

What Are Types? How to Ensure Type-Safety

• Types= describe the values computed during • Bind (assign) types, then check types the execution of the program • Type binding:defines type of constructs in the • Essentially, types are predicate on values .g. program (e.g. variables, functions) —intx“ 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 2003 Introduction to Compilers 3 CS 412/413 Spring 2003 Introduction to Compilers 4

Type Checking Static vs. Dynamic Checking

• Type checking= semantic checks to enforce the • Static type checking= perform type checking at of the program compile-time • Examples: • Dynamic type checking= ensure the correct œ Unary and binary operators (e.g. +, ==, [ ]) must usage of types at run-time receive operands of the proper type œ Check type requirements during program execution œ Functions must be invoked with the right number and type of arguments • Example dynamic checks: œ Return statements must agree with the return type œ Array bounds checking œ In assignments, assigned value must be compatible with type of variable on LHS. œ Null pointer dereferences œ Class members accessed appropriately

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

1 Static vs. Dynamic Typing Strong vs. Weak Typing • Static and dynamic typing refer to type • Strong and weak typing refer to how much definitions (i.e. bindings of types to variables, type consistency is enforced expressions, etc.) • Statically typed language: types are defined at • Strongly typed languages: guarantees that compile-time and do not change during the accepted programs are type-safe execution of the program • Weakly typed languages: allow programs œ E.g. , Java, Pascal which contain type errors • Dynamically typed language: types defined at run-time, during program execution • Can achieve strong typing using either œ E.g. Lisp, static or dynamic typing

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

Soundness Concept Summary

• Sound type systems: can statically ensure that the program is type-safe • Static vsdynamic checking: when to check types? • Soundness implies strong typing • Static vsdynamic typing: when to define types? • Static type safety requires a conservative approximation of the values that may occur • Strong vsweak typing: how many type errors? during all possible executions œ May reject type-safe programs • Sound type systems: statically catch all type errors œ Need to be expressive: reject as few type-safe programs as possible

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

Classification Why Static Checking?

Strong Typing Weak Typing • Efficient code œ Dynamic checks slow down the program ML Pascal C Static Typing • Guarantees that all executions will be safe Java -3 C++ œ Dynamic checking gives safety guarantees only for some execution of the program Scheme PostScript assembly code Dynamic Typing • But is conservative for sound systems œ Needs to be expressive: reject few type-safe programs Smalltalk

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

2 Type Systems Type Expressions • Language type systems have basic types • Type is predicate on value (also: primitive types, ground types) • Basic types examples: int, string, bool • Type expressions:describe the possible types in the program:int, string, array[], Object, etc. • Build type expressionsusing basic types: œ Type constructors: • Type system:defines types for language array types constructs (e.g. expressions, statements) structure types pointer types œ Type aliases œ Function types

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

Type Expressions: Arrays Type Expressions: Structures

• Various kinds of array types in different • More complex type constructor programming languages • Has form {id: , … , id: T } for some • array(T) : arrays without bounds 1 1 n n identifiersidi and types Ti œ C, Java: T [ ], Modula-3: array of T • Is essentially cartesian product: • array(T, S) : array with size (id1 x T1 ) x … x (idn x Tn ) œ C: T[S], Modula-3: array[S] of T • Supports access operations on each field, œ May be indexed 0..S-1 with corresponding type • array(T,L,U) : array with upper/lower bounds • Structures in C: struct{ inta; float b; } œ Pascal: array[L .. U] of T • Records in Pascal: record a: integer; b: real; end • array(T, S , …, S ) : multi-dimensional arrays 1 n • Objects: extension of structure types œ : T(L1,…, Ln)

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

Type Expressions: Aliases Type Expressions: Pointers

• Some languages allow type aliases (type • Pointer types characterize values that are definitions, equates) addresses of variables of other types œ C: typedef int int_array[ ]; œ Modula-3: type int_array = array of int; • Pointer(T) : pointer to an object of type T œ Java doesn‘t allow type aliases • C pointers: T* (e.g. int*x;) • Aliases are not type constructors! œ int_array is the same type as int [ ] • Pascal pointers: ^T (e.g. x: ^integer;)

• Different type expressions may denote the • Java: object references same type

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

3 Type Expressions: Functions Implementation × × × → • Use a separate class hierarchy for types: • Type: T1 T2 … Tn Tr • Function value can be invoked with some classBaseTypeextends Type { String name; } class IntType extends BaseType { … } argument expressions with types Ti, returns class BoolType extends Base Type { … } return type Tr class ArrayTypeextends Type { TypeelemType; } • C functions : int f(float x, float y) classFunctionTypeextends Type { … } • Java: methods have function types • Semantic analysis translates all type • Some languages have first-class function types expressions to type objects (C, ML, Modula-3, Pascal, not Java) • Symbol table binds name to type object

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

Type Comparison Creating Type Objects

• Option 1: implement a method T1.Equals(T2) • Build types while parsing œ use a syntax- œ Must compare type trees of T1 and T2 directed definition: œ For object-oriented language: also need sub-typing: non terminal Type type T1.SubtypeOf(T2) type ::= BOOLEAN

• Option 2: useunique objects for each distinct type {: RESULT = new BoolType(id); :} œ each type expression (e.g. array[int] ) resolved to | ARRAY LBRACKET type:t RBRACKET same type object everywhere {: RESULT = newArrayType(t); :} œ Faster type comparison: can use == œ Object-oriented: check subtyping of type objects • Type objects = AST nodes for type expressions

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

Processing Type Declarations Type-Checking • Type declarations add new identifiers and • Type-checking = verify typing rules their types in the symbol tables —operands of + must be integer expressions; the result • Class definitions must be added to symbol is an integer expression“ table: • Option 1:Implement using syntax-directed class_defn ::= CLASS ID:id { decls: } definitions (type-check during the parsing) • Forward references require multiple passes over AST to collect legal names expr ::= expr:t1 PLUS expr:t2 {: if (t1 ==IntType&& t2 ==IntType) class A { B b; } RESULT =IntType; class B { … } else throw newTypeCheckError(—+“); :}

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

4 Type-Checking Type-Checking Identifiers • Option 2: first build the AST, then implement • Identifier expressions: lookup the type in the type-checking by recursive traversal of the AST symbol table nodes: class IdExpr extendsExpr { class Add extendsExpr { Identifier id; Type typeCheck(Symtabs) { Type typeCheck(Symtabs) Type t1 = e1.typeCheck(s), t2 = e2.typeCheck(s); { return s.lookupType(id); } if (t1 ==Int&& t2 ==Int) return Int; } else throw newTypeCheckError(—+“); } • Using syntax-directed definitions for forward } references: type-checking will fail

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

Next Time: Static Semantics

• Static semantics= mathematical description of typing rules for the language

• Static semantics formally defines types for all legal language ASTs

CS 412/413 Spring 2003 Introduction to Compilers 27

5