Type checking
Type checking
Object types are checked by the type checker – a separate module in the
compiler, which flags type errors when a value is being used in an
inappropriate manner or the type rules of that type are being violated.
The type checker reports a type error in such cases, and then the
programmer must fix the wrong types. Whichever compiler is used for
compilation, the type rules for a language must be enforced.
Type checking is the process of verifying that the types of all variables,
expressions, and functions are according to the rules of the programming
language. It means operations operate on compatible data types, which
helps in preventing errors and hence ensures correctness of the program.
Type checking might be performed at compile time, called static type
checking, or it could be performed during execution, and this is termed
dynamic type checking. Both techniques have different advantages
concerning error detection and flexibility of programs.
Conversion
Tasks
It catches wrong return types, like return “70”, from a function that’s
declared to return an int.
Dynamic Type Checking is defined as the type checking being done at run
time. In Dynamic Type Checking, types are associated with values, not
variables. Implementations of dynamically type-checked languages
runtime objects are generally associated with each other through a type
tag, which is a reference to a type containing its type information.
Dynamic typing is more flexible. A static type system always
restricts what can be conveniently expressed. Dynamic typing results in
more compact programs since it is more flexible and does not require
types to be spelled out. Programming with a static type system often
requires more design and implementation effort.
Languages like Pascal and C have static type checking. Type checking is
used to check the correctness of the program before its execution. The
main purpose of type-checking is to check the correctness and data type
assignments and type-casting of the data types, whether it is syntactically
correct or not before their execution.
Static Type-Checking is also used to determine the amount of memory
needed to store the variable.
The token streams from the lexical analyzer are passed to the PARSER.
The PARSER will generate a syntax tree. When a program (source code) is
converted into a syntax tree, the type-checker plays a Crucial Role. So, by
seeing the syntax tree, you can tell whether each data type is handling
the correct variable or not. The Type-Checker will check and if any
modifications are present, then it will modify. It produces a syntax tree,
and after that, INTERMEDIATE CODE Generation is done.
Overloading
Types of Overloading
Operator Overloading
Function Overloading
Operator Overloading
Example: In Ada, the parentheses ‘()’ are overloaded, the ith element of
the expression A(i) of an Array A has a different meaning such as a ‘call to
function ‘A’ with argument ‘i’ or an explicit conversion of expression i to
type ‘A’. In most languages the arithmetic operators are overloaded.
Function Overloading
Example:
E-->E1(E2)
E.type:= if E2.type = s
else type_error
Conclusion