6

Type Checking

What you will learn in this chapter

  • What are data types?
  • What do we mean by type checking?
  • Which components of a program need to be type checked?
  • How do we infer the type for a component?
  • What is the formalism used for type checking?
  • What is type conversion and coercion and how is it done?
  • How is overloading of operators and functions type checked?

Key Words

type checking, type inference, formal semantics, type conversion and coercion, overloading

We view type checking as evaluation with “abstract values”. Whereas dynamic semantics – evaluation, deals with dynamically computed values like 0, 1, etc., static semantics – type checking – deals with abstractions like int. A type system is sound if it correctly approximates the dynamic behavior and predicts its outcome. For example, if the static semantics predicts that a term has the type int, the dynamic evaluation of the term, if it terminates, will yield an integer.

As indicated in Chapter 5, type checking is one important static semantics check. Many of the programmer errors can be detected and faster progress to a correctly running program is achieved by type checking. We shall now discuss this aspect of semantic analysis in detail.

Some languages like Java and Ada are strongly typed and its compiler en forces strict type checking. On the other extreme, languages like BCPL, or to some extent, Perl, have no type system and any value can be used anywhere in the program if it is semantically allowed, i.e., a reasonable meaning can be found. The C language stands somewhere in the middle of these two extremes.

The most usual type checks applied to a program are:

Identifiers: Do we know the type of this identifier?

Expression: Are the operands of each operator in an expression of the correct type?

Assignment: Can we assign the type of value of an expression, for example the RHS, to a variable with some other type, say on the LHS?

Statements: Is an expression appearing within a statement, of the correct type? For example, the condition expression within a while-loop should have type Boolean.

Conversion, Coercion: Is it possible to convert or coerce the type of an expression to the required type at this point in the program construct? Note that if such conversion or coercion is indeed allowed, then the semantic analysis phase will have to generate extra code to execute that conversion at run-time.

The types of identifiers, program constructs, values and rules about how these types are manipulated form the type system of a language.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.60.249