| applyTo | src/{Compilers,Dependencies,ExpressionEvaluator,Tools}/**/*.{cs,vb} |
|---|
Roslyn follows a layered compiler architecture:
- Lexer → Parser → Syntax Trees → Semantic Analysis → Lowering/Rewriting → Symbol Tables → Emit
- Core abstraction:
Compilationis immutable and reusable. Create new compilations viaAddSyntaxTrees(),RemoveSyntaxTrees(),ReplaceSyntaxTree()for incremental changes - Internal vs Public APIs: Use
InternalSyntaxnamespace for performance-critical parsing;Microsoft.CodeAnalysisfor public consumption
src/Compilers/Core/Portable/- Language-agnostic compiler infrastructuresrc/Compilers/CSharp/Portable/- C# compiler implementationsrc/Compilers/VisualBasic/Portable/- VB compiler implementationsrc/Compilers/Server/-VBCSCompilerbuild serversrc/Dependencies/- High-performance collections (PooledObjects,Threading)src/ExpressionEvaluator/- Debugger expression evaluation (uses specialLexerMode.DebuggerSyntax)src/Tools/- Compiler and infrastructure tooling (BuildBoss,dotnet-roslyn-tools, format tools, analyzers) and benchmark harnesses;IdeCoreBenchmarks/BenchmarkDotNet.Directory.Packages.propssupplies the package policy copied beside generated BDN runners
src/Compilers/CSharp/Portable/Errors/ErrorCode.cs- All C# compiler error codessrc/Compilers/CSharp/Portable/Errors/MessageID.cs- Language feature version gatingsrc/Compilers/CSharp/Portable/LanguageVersion.cs- Public language-version enum and parsing/mapping logicsrc/Compilers/CSharp/Portable/Syntax/Syntax.xml- Syntax tree node definitions (generated code source)src/Compilers/CSharp/Portable/BoundTree/BoundNodes.xml- Bound tree node definitions (generated code source)docs/wiki/Roslyn-Overview.md- Architecture deep-dive
Several core data structures are generated from XML definitions — never edit the generated .cs or .vb files directly:
- Syntax trees:
src/Compilers/CSharp/Portable/Syntax/Syntax.xml - Bound trees:
src/Compilers/CSharp/Portable/BoundTree/BoundNodes.xml - After modifying these XML files, regenerate and build:
dotnet run --file eng/generate-compiler-code.cs # Choose the project matching the C# or VB syntax you changed: dotnet build src/Compilers/CSharp/Portable/Microsoft.CodeAnalysis.CSharp.csproj dotnet build src/Compilers/VisualBasic/Portable/Microsoft.CodeAnalysis.VisualBasic.vbproj
- MEF is not used in the compiler layer.
ExportLanguageService/ImportingConstructorand the IDE service model are IDE-layer concepts — ignore them here. - Null checks: validate internal-API preconditions with
Debug.Assert(...)(a violated internal precondition may NRE in release); validate public APIs with explicit null checking when appropriate, throwing a dedicated exception with a localized string. - Immutability is via
Compilation(AddSyntaxTrees/RemoveSyntaxTrees/ReplaceSyntaxTree), not the workspaceDocument/Solutionmodel.
- Avoid LINQ in hot paths - use manual enumeration or
structenumerators - Avoid
foreachover collections without struct enumerators - Use object pools extensively - see patterns in
src/Dependencies/PooledObjects/ - Prefer
Debug.Assert()over exceptions for internal validation
# Full build (use VS Code tasks when available)
./build.sh
# Build specific components
dotnet build Compilers.slnf # Compiler-only build
dotnet build src/Compilers/CSharp/csc/AnyCpu/ # C# compiler
# Generate compiler code after changes
dotnet run --file eng/generate-compiler-code.csExpression Evaluator uses special parsing modes:
LexerMode.DebuggerSyntaxfor expression evaluationIsInFieldKeywordContextflag for context-aware parsingConsumeFullTextparameter for complete expression parsing
Compiler tasks are in src/Compilers/Core/MSBuildTask/:
Csc.cs- C# compiler taskVbc.cs- VB compiler taskManagedCompiler.cs- Base compiler task functionality
- Lexer/Parser optimizations: Use
InternalSyntaxtypes for performance-critical code - Immutable data structures: Roslyn heavily uses immutable collections and copy-on-write semantics
- Caching:
Compilationobjects cache semantic information - reuse when possible - Threading: Most compiler operations are thread-safe through immutability
Navigate the symbol hierarchy:
var compilation = CreateCompilation(source);
var globalNamespace = compilation.GlobalNamespace;
var typeSymbol = globalNamespace.GetTypeMembers("MyClass").Single();
var methodSymbol = typeSymbol.GetMembers("MyMethod").Single();Symbol equality is complex due to generics and substitution - always test with multiple generic scenarios.