1 Type Checking Type checking ensures that the operands and the operator are of compatible types...

19
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible type is either legal for the operator, or language rules allow it to be converted to a legal type Coercion Automatic conversion Type error Application of an operator to an operand of incorrect type Nearly all type checking can be static for static type bindings Type checking must be dynamic for dynamic type bindings

Transcript of 1 Type Checking Type checking ensures that the operands and the operator are of compatible types...

Page 1: 1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.

1Type Checking• Type checking ensures that the operands and the operator are of

compatible types• Generalized to include subprograms and assignments

• Compatible type is either – legal for the operator, or

– language rules allow it to be converted to a legal type

• Coercion – Automatic conversion

• Type error– Application of an operator to an operand of incorrect type

• Nearly all type checking can be static for static type bindings• Type checking must be dynamic for dynamic type bindings

Page 2: 1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.

2Strong Typing

• Strongly typed programming language – PL where type errors are always detected

• Advantages: – Reliability– Detection of the misuses of variables that result in type errors

• Disadvantages:– Increased size of code– Slower development - declarations– Reduced writability– Exception handling may be move complicated

Page 3: 1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.

3Strong Typing in PLs

• Examples:– FORTRAN 77 is not: parameters, EQUIVALENCE– Pascal is not: variant records– C and C++ are not: parameter type checking can be avoided;

unions are not type checked– Ada is, almost (UNCHECKED CONVERSION is loophole)– Java is similar to Ada

• Coercion rules affect strong typing– can weaken it considerably (C++ versus Ada)

• Java has just half the assignment coercions of C++• Java’s strong typing is still less effective than in Ada

Page 4: 1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.

4Name Type Compatibility

• Variables have compatible types if they are either– in the same declaration – or in declarations that use the same type name

• Easy to implement but highly restrictive– Sub-ranges of integer are not compatible with integer!– Formal parameters must be the same type as their

corresponding actual parameters (Pascal)

Page 5: 1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.

5Structure Type Compatibility

• Variables have compatible types if their types have identical structures– More flexible– But harder to implement

• Because of structured types

• Others are much simpler

• Questions:– Structurally the same record types with different field names?

– Array types with the same base type but different subscripts? E.g. [1..10] vs. [0..9]

– Enumeration types whose components are spelled differently?

• With structural type compatibility, you can not differentiate between types of the same structure – E.g. different units of speed, when both are floating point)

Page 6: 1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.

6Type Compatibility Examples

• Pascal: usually structure, but in some cases name is used (formal parameters)

• C: structure, except for records• Ada: restricted form of name

– Derived types allow types with the same structure to be different

– Anonymous types are all unique, even in: A, B : array (1..10) of INTEGER

• See Sebesta 5.7

Page 7: 1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.

7Scope

• The range of statements where a variable is visible, i.e. can be accessed

• Local variables are visible in the program unit where they are declared

• Non-local variables are visible in a program unit but not declared there

• Scope rules determine how names are associated with variables

Page 8: 1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.

8Static Scope

• Also called Lexical Scope• Based on structure of program text• To connect a name to a variable, one (you or the

compiler) must find the declaration• Search process:

– Search declarations, first locally, – Then in increasingly larger outer enclosing scopes– Until declaration for the name is found

• Enclosing static scopes (to a specific scope) are called its static ancestors;

• The nearest static ancestor is called a static parent

Page 9: 1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.

9Shadowed Variables

• Variables are shadowed (hidden) in part of the code where there is a more immediate ancestor (closer in scope) with the same name

• C++ and Ada allow access to hidden variables using longer names– In Ada: <unit> .<name> – In C++: <class_name> ::<name>

• Common Lisp– Packages variables accessible via longer name or package

imported

• Java– this.<name> – <class_name>.this.<name> – super.<name> // shadowed field in superclass

Page 10: 1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.

10Scope Blocks• Static scopes can be created inside program units

• Started with ALGOL 60!C and C++:

for (int index; …) {

int x;

}

• Scope block – statements in between {…}, and – condition expression in preceding or following (…)

Page 11: 1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.

11Static Scoping Diagram

Example: • MAIN calls A and B • A calls C and D• B calls A and E

Page 12: 1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.

12Static Scope Problems• Suppose the specs are

changed so that D must access data in B

• Solutions:– Put D in B

• but then C can 't call D and D can't access A's variables

– Move the data from B that D needs to MAIN

• but then all procedures can access them

• Same problem for procedure access

• Static scoping may encourages many global variables

Page 13: 1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.

13Dynamic Scope

• Uses calling sequences of program units, not their textual layout– I.e. temporal versus spatial

• References are connected to declarations by searching back through the chain of subprogram calls in execution– I.e. search through the dynamic stack for the most

recent variable with the name

Page 14: 1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.

14Scope ExampleMAIN

- declaration of x

SUB1

- declaration of x -

...

call SUB2

...

SUB2

...

- reference to x -

...

...

call SUB1

...

MAIN calls Sub1Sub1 calls Sub2Sub2 uses x

Static scopingReference to x is to MAIN's x

Dynamic scopingReference to x is to SUB1's x

Page 15: 1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.

15Scope Evaluation

• Advantage of dynamic scoping– convenience– flexibility

• Disadvantage– poor readability– reliability

Page 16: 1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.

16Testing Lexical Scoping(setf fish ’(salmon tuna))

(defun ref-fish () fish)

(defun test-lexical (fish)

(list fish (ref-fish)))

Page 17: 1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.

17Testing Dynamic Scoping

(defvar birds)

(setf birds ’(eagle vulture))

(defun ref-birds () birds)

(defun test-dynamic (birds)

(list birds (ref-birds)))

Page 18: 1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.

18Scope and Lifetime

• Scope and lifetime are sometimes closely related, but are different concepts

• Consider a static variable in a C or C++ function

Page 19: 1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.

19Referencing Environments

• Referencing environment (RE) of a statement– all names that are visible in the statement

• In statically-scoped language RE is– Local variables, and– All of the visible variables in all of the enclosing scopes

• In Dynamically-scoped languages RE is– Local variables, and– All visible variables in all active subprograms– Active

• execution of the subprogram has begun and • is not yet terminated