1 / 19

Type Checking

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

don
Download Presentation

Type Checking

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 itto 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

  2. Strong 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

  3. Strong 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

  4. Name 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)

  5. Structure 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)

  6. Type 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

  7. Scope • 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

  8. Static 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

  9. Shadowed 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

  10. Scope 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 (…)

  11. Static Scoping Diagram Example: • MAIN calls A and B • A calls C and D • B calls A and E

  12. Static 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

  13. Dynamic 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

  14. Scope Example MAIN • declaration of x SUB1 - declaration of x - ... call SUB2 ... SUB2 ... - reference to x - ... ... call SUB1 ... • MAIN calls Sub1 • Sub1 calls Sub2 • Sub2 uses x • Static scoping • Reference to x is to MAIN's x • Dynamic scoping • Reference to x is to SUB1's x

  15. Scope Evaluation • Advantage of dynamic scoping • convenience • flexibility • Disadvantage • poor readability • reliability

  16. Testing Lexical Scoping (setf fish ’(salmon tuna)) (defun ref-fish () fish) (defun test-lexical (fish) (list fish (ref-fish)))

  17. Testing Dynamic Scoping (defvar birds) (setf birds ’(eagle vulture)) (defun ref-birds () birds) (defun test-dynamic (birds) (list birds (ref-birds)))

  18. Scope and Lifetime • Scope and lifetime are sometimes closely related, but are different concepts • Consider a static variable in a C or C++ function

  19. Referencing 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

More Related