Compiler Design

Type Systems & Data Abstraction


Type Systems & Data Abstraction

Advanced features in programming languages require specialized support from the compiler. Two fundamental areas are Type Systems and Data Abstraction.

A type system is a set of rules that assigns types to expressions and variables to prevent type errors.

  • Static typing: Types checked at compile time (e.g., C, Java, C++).
  • Dynamic typing: Types checked at runtime (e.g., Python, JavaScript).
  • Strong typing: No implicit, unsafe type conversions allowed.
  • Weak typing: Implicit conversions allowed (e.g., C allows char to int).

Compiler Tasks:

  • Type checking: Verify operations are applied to compatible types.
  • Type inference: Deduce types without explicit annotations.
  • Type coercion: Automatically convert one type to another when safe.

Data abstraction separates the interface (what a type does) from its implementation (how it does it).

Compiler Challenges:

  • Layout computation: Determine size and field offsets of record/struct types in memory.
  • Name mangling: Generate unique internal names for overloaded functions (crucial in C++).
  • Separate compilation: Modules are compiled independently using only interface files (headers), requiring a linker to resolve final addresses.