This document provides high-density technical context for AI assistants (and humans!) contributing to the Carbon Language project. If you are an AI assistant, read this first to avoid common pitfalls.
toolchain/: The C++ implementation of the compiler (Toolchain).
toolchain/check/: Semantic analysis (SemIR generation).toolchain/parse/: Parsing (Token -> Parse Tree).toolchain/lex/: Lexing (Source -> Tokens).toolchain/sem_ir/: Semantic Intermediate Representation (SemIR)
definitions.toolchain/lower/: Lowering to LLVM IR.proposals/: Evolution proposals.Note: The
explorercodebase (a prototype interpreter) has been moved to its own repository. You may see references to it in old proposals or documentation, but it is not part of the activetoolchaindevelopment in this repository.
We use Bazel.
bazel test //...bazel test //toolchain/check:check_testbazel build //toolchain/...Carbon tests often use file_test (for example,
//toolchain/testing/file_test). If you change compiler behavior, you likely
need to update expected test outputs. Do not manually edit thousands of lines
of expected output. Use the script:
./toolchain/autoupdate_testdata.py
# Or for a specific file:
./toolchain/autoupdate_testdata.py toolchain/check/testdata/my_test.carbon
Running pre-commit is mandatory.
pre-commit run -a
llvm::errs() << "debug info\n"; or
std::cerr.
std::cout (it may interfere with tool output).Print method or operator<<.inst.Print(llvm::errs())--sandbox_debug if needed,
but often running the binary directly from bazel-bin/ is easier for
debugging.Carbon's toolchain uses LLVM-style C++ with some specific conventions.
ErrorOr<T>: Return ErrorOr<T> for fallible operations.
if (auto result = Function(); result) { Use(*result); }llvm::Expected<T>: Similar to ErrorOr, used when interfacing with
LLVM.llvm::cast<T>(obj) (checked, asserts on failure).llvm::dyn_cast<T>(obj) (returns null on failure).llvm::isa<T>(obj) (boolean check).dynamic_cast and standard RTTI.llvm::SmallVector, llvm::StringRef, llvm::DenseMap.StringRef is a view; be careful with lifetimes.explorer references: The explorer prototype has been moved.
Ignore references to it in proposals or old docs; focus on toolchain.autoupdate_testdata.py
can do it for you.std::string unnecessarily: Prefer llvm::StringRef for
arguments.clang-format).