I have spent many hours with Roslyn – this blogpost is result of my exploring of its infrastructure:
Syntactic model – Working just with actual file
Semantic model – Connection to another source files. Usually trigger compilation, so it’s expensive to hardware resources.
SemanticModel.GetDeclaredSymbol() accepts declaration syntax and provides the corresponding symbol.
SemanticModel.GetSymbolInfo() accepts expression syntax (eg. InvocationExpressionSyntax) and returns a symbol.
BatchFixer – make automatic fix on all occurences in solutions.
Syntax Tree – hiearchical representation of c# code. Is accessible with LINQ
Syntax nodes – declarations, statements (StatementSyntax), clauses, and expressions
Syntax token – terminals of language grammar, representing the smallest syntactic fragments. Never in role parents of other nodes or tokens. Consist of keywords, identifiers, literals, and punctuation.
Syntax Walker – goes through whole syntax tree. Useable for colorization or convert from one language to another. It’s important to pass the appropriate SyntaxWalkerDepth argument to CSharpSyntaxWalker. Otherwise, the overridden VisitToken() method is never called. Works only for semantic model – less expensive for hardware resources.
Syntax Rewriter – change syntax tokens or nodes. How will be change depending on returned value:
- The original, unchanged node, token or trivia.
- Null, signalling the node, token or trivia is to be removed.
- A new syntax node, token or trivia.
Annotations – piece of metadata we can attach to a piece of syntax. mark to identify one SyntaxNode with other after replace. Two main properties: Kind – type of affected code element. Data – closer identification. So instead of looking for the exact instance of our annotation on each node, we could search for annotations based on their kind
DocumentEditor – allow temporary disable immutable feature, make multiple changes to document and get the resulting document after the changes have been applied.
Data Flow Analysis – works on semantic. Is able to recognize for example code outside of for and inner. For these purposes have DataFlowAnalysis class special property: AlwaysAssigned, ReadInside, WrittenOutside, WrittenInside, VariablesDeclared
Control flow analysis – for analyze the various entry and exit points – reachable code, etc. Instance returns from SemanticModel.AnalyzeControlFlow()
MSBuildWorkspace – can’t write new projects or solutions
Working with Visual Studio
Analyzers – check source code for oversighted mistakes -for example when not used return value of immutable DateTime. Has configurable manner for displaying info, warning and error messages. Advanced code analyzers built on Roslyn are StyleCopAnalyzers, Code Cracker or the Roslyn Analyzers.
Code Fix – integrated support VS handle our defined code as potencially fixable and on demand making automatic fix.
Another Roslyn technologies
Scripting API – based on Microsoft.CodeAnalysis.Scripting Nuget package. Support async evaluate or run code
Symbol Visitor – analogue of SyntaxVisitor. For such searching is in performance reasons preferred SymbolFinder
Emit API – compiler platform. Can save compiled code to storage or do work with them only in memory.