type checking example
play

Type Checking (Example) int x, y; float z; z = x + y; /* + - PowerPoint PPT Presentation

Type Checking (Example) int x, y; float z; z = x + y; /* + takes ints, int assignable to real */ x = z + y; /* + takes float/int, but float not assignable to int */ Compiler prints foo.c, line 20: float not assignable to int Type


  1. Type Checking (Example) int x, y; float z; z = x + y; /* + takes ints, int assignable to real */ x = z + y; /* + takes float/int, but float not assignable to int */ Compiler prints “ foo.c, line 20: float not assignable to int ”

  2. Type Checking – From the Inside • “Big idea” in compilation is Syntax Directed Translation Expr ::= Expr Op Expr {: /* check code here */ :} • Elegance of the front-end is that syntax-action idea is explicit in code • Your job is to fill in inside the curly brackets ;)

  3. What is a Type ? • Set of values (1, 2, 3, ...) • Set of values with operations on them (+, /, *, ...) • What values can be used with what operations? (cannot / by 0) • Set of values, with operations, and rules for their combination • Really , though type is behavior , static type is static behavior

  4. Static Soundness “Impossible” class ASCIITable { // Example public static void main(String args[]) throws IOException { String asciiTab[256] = { ... "a", "b", "c" ... }; int charNum = System.in.read(); String c = asciiTab[charNum]; // charNum in bounds? System.out.println(c); } }

  5. Composite Types (Constructed) • Basic or atomic types are things like integer, real, boolean • Simplicity of these types isn’t up to complex programming, hence composite types • Declarations: int a[10]; float f(int a, int b); struct { int a; int b; }; int *px; • even some executable code f(1, x); /* arg list to func. call */

  6. What are the types? [1] int i, x, y[20]; [2] float z; [3] int f(int a, int b); [4] z = (x + y[i] ) * f ( i , x); [5] x = z + y[i]; What are the values of y ’s type ? Ops? Rules?

  7. Type Structure Guides SW Design class ArrayType : CompositeType { // pointers to children private Type elementType; private RangeType range; // accessors public … }

  8. What you can do now • Check #2: Detect an illegal assignment -- that is, an assignment of the form: A := Expr where A is either a type , a procedure , or a constant (i.e., anything other than a variable). This is an elaboration of a check from an earlier assignment. The old message should be replaced with this one. How might you go about this? Where start? When are you done?

  9. What you can do now • Some lingo: STO = Symbol Table Object • An STO will often be a variable with a type. For expressions, return a VarSTO that is a “fake” variable with a type, because later it will be a var: x := y + w * z t1 := w * z t2 := y + t1 x := t2 • For now, just think of passing a variable as an “easy” way to pass a type So what should you do to STO?

  10. What you can do now • Create a unique instance of each basic type and make it easily accessible TypeSpec ::= INTEGER {: RESULT = TypeValues.IntType; :} | REAL ... • A public static class field is essentially a global class TypeValues { public static IntegerType IntType = new IntegerType(); … } What class hierarchy might we have for basic types?

Recommend


More recommend