Skip to content

Chapter 10: Support for static and extern storage class specifiers#9

Choose a tag to compare

@Fare9 Fare9 released this 07 Feb 09:29
· 7 commits to main since this release
3b685fe

Summary

  • Added support for static and extern storage class specifiers across the compiler pipeline (lexer, parser, semantic analysis, IR, and code generation).
  • Introduced a scoped symbol table with linkage and storage duration tracking, replacing the previous flat symbol handling. A GlobalSymbolTable keeps global data for
    functions and static variables, while local scopes track automatic-storage variables.
  • Implemented C linkage resolution rules: external vs. internal linkage, tentative definitions, and redeclaration merging at file scope.
  • Handled static local variables with unique name mangling to allow different functions to declare static variables with the same name without collisions.

Key changes

  • Lexer: Added static and extern keyword tokens.
  • Parser: Extended declaration parsing to consume optional storage class specifiers and propagate them to AST nodes (VarDeclaration, FunctionDeclaration).
  • AST: Added StorageClass enum, storage class and unique-name fields on declarations, and Linkage/ScopeType/InitialValue enums for semantic attributes.
  • Sema (Scope & Symbol Table): Created SymbolEntry with variant attributes (FunAttr, StaticAttr, LocalAttr). Implemented linkage conflict detection, file-scope
    variable merging, and static-local variable renaming.
  • IR & CodeGen: Updated IR generation and x64 backend to emit static variables in the data/bss segment with correct visibility (global vs. local linkage).
  • Diagnostics: Added new error messages for conflicting linkage, duplicate definitions, and invalid storage class usage (e.g. static/extern in for-loop init).

Test plan

  • All existing Chapter 1-9 test suites continue to pass.
  • Chapter 10 validation tests pass, covering static globals, static locals, extern declarations, linkage conflicts, and tentative definitions.
image image image image image