Compiler Frontend: Scan, Parse, Typecheck
Compiler Frontend: Scan, Parse, Typecheck
Overview
go build is a pipeline. The frontend turns source text into a type-checked AST (and related forms) before IR/SSA. Understanding this half explains compile errors, go/types, and why some “optimizations” never see your source.
Series map: Internals for Interns — Compiler (scanner → type checker). Companion: 209 SSA/PGO.
Diagram: frontend stages
.go source
│
v
scanner → tokens → parser → AST
│
v
type checker
│
v
export / IR bridge
Scanner (lexer)
- Characters → tokens (
package,IDENT,INT,+, …) - Tracks positions for error messages and stack traces later
- No meaning yet—
fmtandPrintlnare just idents and a dot
Parser
- Tokens → AST (
*ast.File, decls, stmts, exprs) - Enforces grammar; produces structure without full type knowledge
go/parserin the standard library mirrors the idea for tools
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "main.go", src, 0)Type checker
- Resolves identifiers, assigns types, checks assignability
- Builds method sets, interfaces satisfaction, constant folding
- Emits the errors you actually fix day to day
undeclared name / cannot use X as Y / missing method M
Tools (gopls, go vet, many linters) reuse go/types rather than reimplementing rules.
Unified IR / export data (bridge)
After typecheck, the compiler serializes a compact representation for:
- Build cache reuse
- Export data for importers of the package
- Feeding the mid-end (IR) without re-parsing forever
What the frontend will not do
| Expectation | Reality |
|---|---|
| Cross-package inlining decisions | Later (IR/SSA) |
| Register allocation | Backend |
| Fix algorithm bugs | Never |
Experiment
go install golang.org/x/tools/cmd/goimports@latest # optional tools
# Dump AST for a file with the go/ast printer pattern, or:
go doc go/parser
go doc go/typesWrite a 10-line program that uses go/parser + ast.Inspect to count *ast.CallExpr nodes in a file.
Try next: Feed the type checker a file with a deliberate interface mismatch; print types.Config.Check errors.