Escolar Documentos
Profissional Documentos
Cultura Documentos
Type system
Content Outline
Specification of a simple type checker Type expression Type conversion Operator and function overloading Polymorphic Functions
2
Type Checking
A compiler has to do semantic checks in addition to syntactic checks. Semantic Checks Type checking is one of these static checking operations. A type system is a collection of rules for assigning type expressions to the parts of a program. A type checker implements a type system. A sound type system eliminates run-time type checking for type errors. A programming language is strongly-typed, if every program its compiler accepts will execute without type errors.
we may not do all type checking at compile-time. Some systems also use dynamic type checking too. Static done during compilation Dynamic done during run-time
In practice, some of type checking operations are done at run-time (so, most of the programming languages are not strongly-typed). Ex: int x[100]; x[i] most of the compilers cannot guarantee that i will be between 0 and 99
The type of a language construct is denoted by a type expression. A type expression can be: A basic type a primitive data type such as integer, real, char, boolean, type-error to signal a type error void : no type A type name a name can be used to denote a type expression. A type constructor applies to other type expressions. arrays: If T is a type expression, then array(I,T) is a type expression where I denotes index range. Ex: array(0..99,int) products: If T1 and T2 are type expressions, then their cartesian product T1 x T2 is a type expression. Ex: int x int pointers: If T is a type expression, then pointer(T) is a type expression. Ex: pointer(int) functions: We may treat functions in a programming language as mapping from a domain type D to a range type R. So, the type of a function can be denoted by the type expression DR where D are R type expressions. Ex: intint represents the type of a function which takes an int value as parameter, and its return type is also int.
Type Expression
A Simple Type Checking System P D;E D D;D D id:T { addtype(id.entry,T.type) } T char { T.type=char } T int { T.type=int } T real { T.type=real } T T1 { T.type=pointer(T1.type) } T array[intnum] of T1 { T.type=array(1..intnum.val,T1.type) }
E E E E E
id charliteral { E.type=char } intliteral { E.type=int } realliteral { E.type=real } E1 + E2 { if (E1.type=int and E2.type=int) then E.type=int else if (E1.type=int and E2.type=real) then E.type=real else if (E1.type=real and E2.type=int) then E.type=real else if (E1.type=real and E2.type=real) then E.type=real else E.type=type-error } E E1 [E2] { if (E2.type=int and E1.type=array(s,t)) then E.type=t else E.type=type-error } Bina 6 E Nusantara University{ if (E1.type=pointer(t)) then E.type=t E1
{ if (E.type=boolean then
{ if (E.type=boolean then
7
S.type=S1.type
argument types
Bina Nusantara University
Structural Equivalence Algorithm (sequiv): if (s and t are same basic types) then return true else if (s=array(s1,s2) and t=array(t1,t2)) then return (sequiv(s1,t1) and sequiv(s2,t2)) else if (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)) then return (sequiv(s1,t1)) else if (s = s1 s2 and t = t1 t2) then return (sequiv(s1,t1) and sequiv(s2,t2)) else return false
Bina Nusantara University
How do we treat type names? Get equivalent type expression for a type name (then use structural equivalence), or Treat a type name as a basic type.
10
but this means that the type expression link is different than the type expression cell.
Bina Nusantara University
11
Type Conversions
x+y
or double)?
What kind of codes we have to produce, if the type of x is double and the type of y is int?
12
Static Checking
Static checking aims to ensure, at compile time, that syntactic and semantic constraints of the source language are obeyed. E.g.:
Type checks: operators and operands must have compatible types. Flow-of-control checks: control transfer statements must have legitimate targets (e.g., break/continue statements). Uniqueness checks: a language may dictate unique occurrences in some situations, e.g., variable declarations, case labels in switch statements.
Bina Nusantara University
14
A type expression denotes (i.e., is a syntactic representation of) the type of a program entity:
A base type is a type expression (e.g., boolean, char, int, float); A type name is a type expression; A type constructor applied to type expressions is a type expression, e.g.:
arrays: if T is a type expression then so is array(T); records: if T1, , Tn are type expressions and f1, , fn is a list of (unique) identifiers, then record(f1:T1, , fn:Tn) is a type expression; pointers: if T is a type expression then so is ptr(T); functions: if T, T1, , Tn are type expressions, then so is (T1, , Tn) T.
15
Program Code Type Expression f ()ptr(ptr(char)) f() ptr(ptr(char)) *f() ptr(char) *f() array(char) (*f()) array(char) 2 int (*f())[2] char
Rule symbol table lookup if e : T1T2 and e1 : T1 then e(e1) : T2 if e : ptr(T) then *e : T if e : ptr(T) then e : array(T) if e : T then (e) : T base type if e1 : array(T) and e2 : int then e1[e2] : T
What about:
Bina Nusantara University
1. Structural equivalence:
17
Basic types are given predefined internal values; Named types can be represented via pointers into a hash table. A composite type expression f (T1,,Tn) is represented as a node identifying the constructor f and with pointers to the nodes for T1, , Tn.
E.g.: int x[10][20]:
18
Production E id E intcon E E1 + E2
/* arithmetic type conversions */ Type result_type(Type t1, Type t2) { if (t1 == error || t2 == error) return error; if (t1 == t2) return t1; if (t1 == double || t2 == double) return double; if (t1 == float || t2 == float) return float; Bina Nusantara University
Return types:
currently: the type of the expression down the road:
type location code to evaluate the expression
19
20
Function calls:
E id ( expr_list ) { if (id.return_type == VOID) E.type = error; else if ( chk_arg_types(id, expr_list) )
match formals in number, type */
/* actuals
21
Operator Overloading
Overloading refers to the use of the same syntax to refer to different operations, depending on the operand types. E.g.: in Java, + can refer to integer addition, floating point addition, or string concatenation. The compiler uses operand type information to resolve the overloading, i.e., figure out which operation is actually referred to. 23