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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.