There's more...

The current binder implementation of the switch operator has a bunch of pending work items, primarily related to more comprehensive semantic validation and error generation. The validation items to be implemented are:

  1. Add semantic validation that the expression of the switch operator has a type as per the requirements of a switch governing type; otherwise, generate a compile time error.
  2. Add new compiler diagnostics for argument list validations. For example:
    • Ensure that the number of expressions in values is one greater than the number of expression in labels; otherwise, generate a compile time error.
    • Ensure that the labels are all compile time constants with implicit conversions to the switch governing type. If not, generate required compile time errors.
  3. Validate that the types of the expressions in Values are implicitly convertible to a common type Z, which is the type of the expression.

These items are left as an exercise for the reader. For further guidance on implementing a new semantic error in the compiler code base, refer to recipe, Implementing a new semantic error in the C# compiler code base, in Chapter 8, Contribute Simple Functionality to Roslyn C# Compiler Open Source Code.

We also added basic stub implementations for the following pieces, which need further enhancements:

  1. IOperation support the switch operator: This will involve creating a new OperationKind (http://source.roslyn.io/#Microsoft.CodeAnalysis/Operations/IOperationKind.cs,bf7324631c03b2e7) for the switch expression, adding a new interface, say ISwitchChoiceExpression, with the following API shape and then implementing this interface on the BoundSwitchOperator.
 /// <summary>
/// Represents a C# switch operator.
/// </summary>
/// <remarks>
/// This interface is reserved for implementation by its associated APIs. We reserve the right to
/// change it in the future.
/// </remarks>
public interface ISwitchChoiceExpression : IOperation
{
/// <summary>
/// Switch expression to be tested.
/// </summary>
IOperation SwitchExpression { get; }

/// <summary>
/// List of labels to compare the switch expression against.
/// </summary>
ImmutableArray<IOperation> SwitchLabels { get; }

/// <summary>
/// List of values corresponding to the labels.
/// </summary>
ImmutableArray<IOperation> SwitchValues { get; }
}
  1. Add lowering support for the switch operator: This is covered in the next recipe.
  2. Add flow analysis support for the switch operator: This is not covered in this book, but should be implemented to ensure we report proper flow analysis diagnostics in code involving the switch operator.
..................Content has been hidden....................

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