How it works...

Semantic analysis, also context sensitive analysis, is a process in compiler construction, usually after parsing, to gather necessary semantic information from the source code.

C# binder is the second phase of the compiler tool chain that operates on syntax trees, nodes, tokens, and trivia that comes out from the parser and analyzes the semantics of the code according to the C# language specification. This phase produces BoundTrees and reports semantic diagnostics. A bound tree is essentially an abstract syntax tree with rich semantic information associated with each node in the tree. All the semantic information provided by the SemanticModel APIs at the CodeAnalysis layer is from the bound nodes associated with the syntax. The primary entry points for binding statements and expressions are Binder.BindStatement and Binder.BindExpression, respectively.

Use http://source.roslyn.io/ for rich semantic search and navigation of Roslyn source code. Note that the version of source code indexed at this website corresponds to the latest sources in the master branch of the Roslyn repo, and may differ from the sources for the Visual-Studio-2017 tag.

In this recipe, we showed you how to make the following changes in the binder to add a new diagnostic CS0823:

  1. Add a new error code to the ErrorCode enum.
  2. Add new resource strings for the compiler diagnostic message.
  3. Add a new semantic diagnostic to the method to bind the implicit variable initializer when the initializer is a binary expression involving non-apparent implicit conversions.

C# local rewriter or lowering is the third phase of the compiler tool chain that simplifies the bound trees into very simple bound nodes. Additionally, it also performs flow analysis and reports flow analysis diagnostics (such as unreachable code). Then output of the local rewriter is fed to the code generator that generates MSIL for the simplified bound tree.
In this recipe, we extended the existing local rewriting diagnostic pass to report CS1717 for self-assigning property access expressions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset