How to Ensure Type-Safety Type Checking Static Vs. Dynamic Typing
Total Page:16
File Type:pdf, Size:1020Kb
Semantic Analysis •Last time: CS412/CS413 – Semantic errors related to scopes Introduction to Compilers – Symbol tables –Name resolution Tim Teitelbaum •This lecture: – Semantic errors related to types Lecture 13: Types and Type-Checking – Type system concepts 19 Feb 07 – Types and type-checking CS 412/413 Spring 2007 Introduction to Compilers 1 CS 412/413 Spring 2007 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 for constructs in • Essentially, types are predicate on values the program (e.g., variables, functions) – E.g., “int x” in Java means “x ∈ [-231, 231)” • Can be either explicit (int x) or implicit (x = 1) – Think: “type = set of possible values” • Type consistency (safety) = correctness with respect to the type bindings • Type errors: improper, type-inconsistent operations during program execution – Type checking: determine if the program correctly uses the type bindings •Type-safety: absence of type errors at run time • Enforce a set of type-checking rules CS 412/413 Spring 2007 Introduction to Compilers 3 CS 412/413 Spring 2007 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 type safety 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., C, 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, Scheme, Smalltalk with type of variable on LHS. – Class members accessed appropriately CS 412/413 Spring 2007 Introduction to Compilers 5 CS 412/413 Spring 2007 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 approximation of the values that may occur – Weakly typed languages: allow programs that contain type errors during all possible executions – May reject type-safe programs – Need to be expressive: reject as few type-safe • Can achieve strong typing using either programs as possible static or dynamic typing CS 412/413 Spring 2007 Introduction to Compilers 7 CS 412/413 Spring 2007 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 Java Modula-3 C++ • Strong vs weak typing: how many type errors? Scheme • Sound type systems: statically catch all type errors PostScript assembly code Dynamic Typing Smalltalk CS 412/413 Spring 2007 Introduction to Compilers 9 CS 412/413 Spring 2007 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 2007 Introduction to Compilers 11 CS 412/413 Spring 2007 Introduction to Compilers 12 2 Type Expressions Array Types • Languages have basic types • Various kinds of array types in different programming languages (a.k.a. primitive types or ground types) – E.g., int, char, boolean • array(T) : array with elements of type T and no bounds –C, Java: int [ ], Modula-3: array of integer • Build type expressions using basic types: – Type constructors • array(T, S) : array with size –Type aliases –C: int[10], Modula-3: array[10] of integer – May be indexed 0..size-1 • array(T,L,U) : array with upper/lower bounds – Pascal or Ada: array[2 .. 5] of integer • array(T, S1, …, Sn) : multi-dimensional arrays –FORTRAN: real(3,5) CS 412/413 Spring 2007 Introduction to Compilers 13 CS 412/413 Spring 2007 Introduction to Compilers 14 Record Types Pointer Types • A record is {id1: T1, … , idn: Tn} for some • Pointer types characterize values that are identifiers idi and types Ti addresses of variables of other types • Supports access operations on each field, •Pointer(T): pointer to an object of type T with corresponding type • C: struct { int a; float b; } • C pointers: T* (e.g., int *x;) •Pascal: record a: integer; b: real; end • Pascal pointers: ^T (e.g., x: ^integer;) • Objects: generalize the notion of records • Java: object references CS 412/413 Spring 2007 Introduction to Compilers 15 CS 412/413 Spring 2007 Introduction to Compilers 16 Function Types Type Aliases •Type: T1×T2 × … × Tn → Tr • Some languages allow type aliases (type • Function value can be invoked with some argument definitions, equates) expressions with types Ti, returns return type Tr – C: typedef int int_array[ ]; – Modula-3: type int_array = array of int; • C functions: int pow(int x, int y) – Java doesn’t allow type aliases type: int × int → int • Java: methods have function types • Aliases are not type constructors! • Some languages have first-class functions – int_array is the same type as int [ ] – usually in functional languages, e.g., ML, LISP – C and C++ have function pointers • Different type expressions may denote the – Java doesn’t same type CS 412/413 Spring 2007 Introduction to Compilers 17 CS 412/413 Spring 2007 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 { … } – 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 2007 Introduction to Compilers 19 CS 412/413 Spring 2007 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:d } {: RESULT = new BoolType(); :} • 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 2007 Introduction to Compilers 21 CS 412/413 Spring 2007 Introduction to Compilers 22 Type-Checking Type-Checking • Type-checking = verify typing rules Option 2: implement type-checking by an AST visitor “operands of + must be integer expressions; the result class typeCheck implements Visitor { is an integer expression” Object visit(Add e, Object symbolTable) { Type t1 = (int) e.e1.accept(this, symbolTable); •Option 1:Implement using syntax-directed Type t2 = (int) e.e2.accept(this, symbolTable); if (t1 == Int && t2 == Int) return Int; definitions (type-check during the parsing) else throw new TypeCheckError(“+”); } expr ::= expr:t1 PLUS expr:t2 Object visit(Num e, Object symbolTable) { {: if (t1 == IntType && t2 == IntType) return Int; RESULT = IntType; } else throw new TypeCheckError(“+”); Object visit(Id e, Object symbolTable) { return (SymbolTable)symbolTable.lookupType(e); :} } } CS 412/413 Spring 2007 Introduction to Compilers 23 CS 412/413 Spring 2007 Introduction to Compilers 24 4 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 2007 Introduction to Compilers 25 5.