– boolean, char, integer, float, void, subrange... Type Checking• Static type checking – is perfomed by the compiler – applied to typed language • Dynamic type checking – is done when th
Trang 1Type Checking
Dr Nguyen Hua Phung
Faculty of CSE
HCMUT
Trang 2• Type systems
– Type expressions
– Static and dynamic type checking
• Equivalence of type expressions
• Type conversions
Trang 3Type Expressions
• A basic type is a type expression
– boolean, char, integer, float, void, subrange.
• A type name is a type expression
• A type constructor applied to type expressions is
a type expression Including:
– Arrays: array(I,T) where I: index type, T:element type – Products: T1 × T2
– Records: record((name1 × T1) × (name2 × T2) ×…)
– Pointers: pointer(T)
– Functions: T1 → T2
• A type variable is a type expression
Trang 4• typedef int siso; ⇒ siso
• int t[10]; ⇒ array(0 9,int)
• int foo(int a,float b) ⇒ (int × float) → int
• struct {int a;int b} ⇒ record((a × int) × (b × int))
• int *p ⇒ pointer(int)
• template <class T> struct vd {T a; T b[3];}
⇒ record((a × T) × (b × array(0 2,T)))
Trang 5Type Checking
• Static type checking
– is perfomed by the compiler
– applied to typed language
• Dynamic type checking
– is done when the target program runs
– target code needs to carry the type of an
element
Trang 6Equivalence of Type Expressions
• Name equivalence
• Structural equivalence
• Structural equivalence under naming
rec1 = record
a : integer;
b : real;
end;
rec2 = record
c : real;
d : integer;
end;
rec3 = record
c : integer;
d : real;
end;
rec4 = record
a : integer;
b : real; end;
rec1 and rec4 are structural equivalence under naming
rec1 and rec3 are structural equivalence
Trang 7Static Type Checking for Structural Equivalence
function sequiv(Type s,Type t):boolean
begin
if (s and t are the same basic type) 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 × s2 and t = t1 × 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) ; else
return false;
Trang 8Object-Oriented Approach
Type
BasicType
NumericType BooleanType
PointerType ArrayType ProductType FunctionType
class ArrayType extends Type {
RangeType index;
Type element;
Trang 9Object-Oriented Approach (cont’d)
abstract class Type {
abstract boolean isLeftCompatibleTo(Type t) ;
}
abstract class BasicType extends Type {
boolean isLeftCompatibleTo(Type t) {
if (this.getClass().equals(t.getClass()))
return true;
return false;
}
}
class FloatType extends NumericType {
boolean isLeftCompatibleTo(Type t) {
if (t instanceof NumericType)
return true;
return false;
}
}
Trang 10Object-Oriented Approach (cont’d)
class ArrayType {
RangeType index ;
Type element ;
boolean isLeftCompatibleTo(Type t) {
if (t instanceof ArrayType) {
ArrayType at=(ArrayType) t;
return index.isLeftCompatibleTo(at.index) &&
element.isLeftCompatibleTo(at.element);
} else
return false;
}
}
Trang 11Static Type Checking for Assignment Expression
Expr → Term = Expr
{ if (!Term.type.isLeftCompatibleTo(Expr.type))
throw new TypeMismatch(=);
}
Expr → Term
Term → INTLITERAL
Term → FLOATLITERAL
Term → id
Term → ( Expr )
{Expr.type = Term.type;}
{Term.type = new IntType();} {Term.type = new FloatType();} {sym = lookup(id.Lexeme);
Term.type = sym.type;}
{Term.type = Expr.type}
Trang 12Implicit Assignment
int foo(int a, float b) {
…
return a;
}
int goo() {
…
foo(x,y);
…
}
foo = a
a = x; b = y
Trang 13Implicit Assignment (cont’d)
==(int i1, int i2)
• ==
• a == b
==(boolean b1, boolean b2)
i1 = a; i2 = b
b1 = a; b2 = b
Trang 14Type Conversions
• Converts from one type to another.
• implicit if it is done automatically by the
compiler ⇒ type coercions
float x = 3;
• explicit if the programmer must write
something to cause the conversion.