Pattern 20Computing Static Expression Types

Purpose

This pattern explains how to statically compute the type of expressions in languages with explicit type declarations like C.

You’ll be able to extrapolate from this pattern everything you’d need to build a static type analyzer for C, C++, Java, or C#. Every compiler for those languages implements an extended version of this pattern. So do static bug analyzers such as FindBugs[23] and Coverity.[24]

Discussion

Type computation is an extremely broad topic. To make things more concrete, we’ll focus on the type computation rules for Cymbol itemized here:

Subexpression

Result Type

true, false

boolean.

Character literal

char.

Integer literal

int.

Floating-point literal

float.

id

The declared type of the variable ...

Get Language Implementation Patterns now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.