202 lines
9.3 KiB
C#
202 lines
9.3 KiB
C#
using System;
|
|
using System.Collections.Immutable;
|
|
using Microsoft.CodeAnalysis.CSharp.Symbols;
|
|
using Microsoft.CodeAnalysis.CSharp.Symbols.PublicModel;
|
|
using Microsoft.CodeAnalysis.PooledObjects;
|
|
|
|
namespace Microsoft.CodeAnalysis.CSharp;
|
|
|
|
public static class SymbolDisplay
|
|
{
|
|
public static string ToDisplayString(ISymbol symbol, SymbolDisplayFormat? format = null)
|
|
{
|
|
format = format ?? SymbolDisplayFormat.CSharpErrorMessageFormat;
|
|
return ToDisplayString(symbol, null, -1, format, minimal: false);
|
|
}
|
|
|
|
public static string ToDisplayString(ITypeSymbol symbol, NullableFlowState nullableFlowState, SymbolDisplayFormat? format = null)
|
|
{
|
|
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0002: Unknown result type (might be due to invalid IL or missing references)
|
|
return ToDisplayString(symbol, NullableFlowStateExtensions.ToAnnotation(nullableFlowState), format);
|
|
}
|
|
|
|
public static string ToDisplayString(ITypeSymbol symbol, NullableAnnotation nullableAnnotation, SymbolDisplayFormat? format = null)
|
|
{
|
|
//IL_000d: Unknown result type (might be due to invalid IL or missing references)
|
|
format = format ?? SymbolDisplayFormat.CSharpErrorMessageFormat;
|
|
symbol = symbol.WithNullableAnnotation(nullableAnnotation);
|
|
return ToDisplayString((ISymbol)(object)symbol, null, -1, format, minimal: false);
|
|
}
|
|
|
|
private static string ToDisplayString(ISymbol symbol, SemanticModel? semanticModelOpt, int positionOpt, SymbolDisplayFormat format, bool minimal)
|
|
{
|
|
ArrayBuilder<SymbolDisplayPart> instance = ArrayBuilder<SymbolDisplayPart>.GetInstance();
|
|
PopulateDisplayParts(instance, symbol, semanticModelOpt, positionOpt, format, minimal);
|
|
string result = SymbolDisplayExtensions.ToDisplayString(instance);
|
|
instance.Free();
|
|
return result;
|
|
}
|
|
|
|
public static string ToMinimalDisplayString(ISymbol symbol, SemanticModel semanticModel, int position, SymbolDisplayFormat? format = null)
|
|
{
|
|
if (format == null)
|
|
{
|
|
format = SymbolDisplayFormat.MinimallyQualifiedFormat;
|
|
}
|
|
return ToDisplayString(symbol, semanticModel, position, format, minimal: true);
|
|
}
|
|
|
|
public static string ToMinimalDisplayString(ITypeSymbol symbol, NullableFlowState nullableFlowState, SemanticModel semanticModel, int position, SymbolDisplayFormat? format = null)
|
|
{
|
|
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0002: Unknown result type (might be due to invalid IL or missing references)
|
|
return ToMinimalDisplayString(symbol, NullableFlowStateExtensions.ToAnnotation(nullableFlowState), semanticModel, position, format);
|
|
}
|
|
|
|
public static string ToMinimalDisplayString(ITypeSymbol symbol, NullableAnnotation nullableAnnotation, SemanticModel semanticModel, int position, SymbolDisplayFormat? format = null)
|
|
{
|
|
//IL_000c: Unknown result type (might be due to invalid IL or missing references)
|
|
if (format == null)
|
|
{
|
|
format = SymbolDisplayFormat.MinimallyQualifiedFormat;
|
|
}
|
|
symbol = symbol.WithNullableAnnotation(nullableAnnotation);
|
|
return ToDisplayString((ISymbol)(object)symbol, semanticModel, position, format, minimal: true);
|
|
}
|
|
|
|
public static ImmutableArray<SymbolDisplayPart> ToDisplayParts(ISymbol symbol, SymbolDisplayFormat? format = null)
|
|
{
|
|
format = format ?? SymbolDisplayFormat.CSharpErrorMessageFormat;
|
|
return ToDisplayParts(symbol, null, -1, format, minimal: false);
|
|
}
|
|
|
|
public static ImmutableArray<SymbolDisplayPart> ToDisplayParts(ITypeSymbol symbol, NullableFlowState nullableFlowState, SymbolDisplayFormat? format = null)
|
|
{
|
|
//IL_000d: Unknown result type (might be due to invalid IL or missing references)
|
|
format = format ?? SymbolDisplayFormat.CSharpErrorMessageFormat;
|
|
return ToDisplayParts(symbol, nullableFlowState, null, -1, format, minimal: false);
|
|
}
|
|
|
|
public static ImmutableArray<SymbolDisplayPart> ToDisplayParts(ITypeSymbol symbol, NullableAnnotation nullableAnnotation, SymbolDisplayFormat? format = null)
|
|
{
|
|
//IL_000b: Unknown result type (might be due to invalid IL or missing references)
|
|
if (format == null)
|
|
{
|
|
format = SymbolDisplayFormat.CSharpErrorMessageFormat;
|
|
}
|
|
return ToDisplayParts((ISymbol)(object)symbol.WithNullableAnnotation(nullableAnnotation), null, -1, format, minimal: false);
|
|
}
|
|
|
|
public static ImmutableArray<SymbolDisplayPart> ToMinimalDisplayParts(ISymbol symbol, SemanticModel semanticModel, int position, SymbolDisplayFormat? format = null)
|
|
{
|
|
if (format == null)
|
|
{
|
|
format = SymbolDisplayFormat.MinimallyQualifiedFormat;
|
|
}
|
|
return ToDisplayParts(symbol, semanticModel, position, format, minimal: true);
|
|
}
|
|
|
|
public static ImmutableArray<SymbolDisplayPart> ToMinimalDisplayParts(ITypeSymbol symbol, NullableFlowState nullableFlowState, SemanticModel semanticModel, int position, SymbolDisplayFormat? format = null)
|
|
{
|
|
//IL_000c: Unknown result type (might be due to invalid IL or missing references)
|
|
if (format == null)
|
|
{
|
|
format = SymbolDisplayFormat.MinimallyQualifiedFormat;
|
|
}
|
|
return ToDisplayParts(symbol, nullableFlowState, semanticModel, position, format, minimal: true);
|
|
}
|
|
|
|
public static ImmutableArray<SymbolDisplayPart> ToMinimalDisplayParts(ITypeSymbol symbol, NullableAnnotation nullableAnnotation, SemanticModel semanticModel, int position, SymbolDisplayFormat? format = null)
|
|
{
|
|
//IL_000c: Unknown result type (might be due to invalid IL or missing references)
|
|
if (format == null)
|
|
{
|
|
format = SymbolDisplayFormat.MinimallyQualifiedFormat;
|
|
}
|
|
return ToDisplayParts((ISymbol)(object)symbol.WithNullableAnnotation(nullableAnnotation), semanticModel, position, format, minimal: true);
|
|
}
|
|
|
|
private static ImmutableArray<SymbolDisplayPart> ToDisplayParts(ITypeSymbol symbol, NullableFlowState nullableFlowState, SemanticModel? semanticModelOpt, int positionOpt, SymbolDisplayFormat format, bool minimal)
|
|
{
|
|
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0002: Unknown result type (might be due to invalid IL or missing references)
|
|
return ToDisplayParts((ISymbol)(object)symbol.WithNullableAnnotation(NullableFlowStateExtensions.ToAnnotation(nullableFlowState)), semanticModelOpt, positionOpt, format, minimal);
|
|
}
|
|
|
|
private static ImmutableArray<SymbolDisplayPart> ToDisplayParts(ISymbol symbol, SemanticModel? semanticModelOpt, int positionOpt, SymbolDisplayFormat format, bool minimal)
|
|
{
|
|
ArrayBuilder<SymbolDisplayPart> instance = ArrayBuilder<SymbolDisplayPart>.GetInstance();
|
|
PopulateDisplayParts(instance, symbol, semanticModelOpt, positionOpt, format, minimal);
|
|
return instance.ToImmutableAndFree();
|
|
}
|
|
|
|
private static ArrayBuilder<SymbolDisplayPart> PopulateDisplayParts(ArrayBuilder<SymbolDisplayPart> builder, ISymbol symbol, SemanticModel? semanticModelOpt, int positionOpt, SymbolDisplayFormat format, bool minimal)
|
|
{
|
|
//IL_005f: Unknown result type (might be due to invalid IL or missing references)
|
|
if (symbol == null)
|
|
{
|
|
throw new ArgumentNullException("symbol");
|
|
}
|
|
if (minimal)
|
|
{
|
|
if (semanticModelOpt == null)
|
|
{
|
|
throw new ArgumentException(CSharpResources.SyntaxTreeSemanticModelMust);
|
|
}
|
|
if (positionOpt < 0 || positionOpt > semanticModelOpt.SyntaxTree.Length)
|
|
{
|
|
throw new ArgumentOutOfRangeException(CSharpResources.PositionNotWithinTree);
|
|
}
|
|
}
|
|
if ((symbol as Microsoft.CodeAnalysis.CSharp.Symbols.PublicModel.MethodSymbol)?.UnderlyingMethodSymbol is SynthesizedSimpleProgramEntryPointSymbol)
|
|
{
|
|
builder.Add(new SymbolDisplayPart((SymbolDisplayPartKind)15, symbol, "<top-level-statements-entry-point>"));
|
|
}
|
|
else
|
|
{
|
|
SymbolDisplayVisitor symbolDisplayVisitor = new SymbolDisplayVisitor(builder, format, semanticModelOpt, positionOpt);
|
|
symbol.Accept((SymbolVisitor)(object)symbolDisplayVisitor);
|
|
}
|
|
return builder;
|
|
}
|
|
|
|
public static string FormatPrimitive(object obj, bool quoteStrings, bool useHexadecimalNumbers)
|
|
{
|
|
//IL_0002: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0008: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0009: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0012: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_000d: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_000f: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0010: Unknown result type (might be due to invalid IL or missing references)
|
|
ObjectDisplayOptions val = (ObjectDisplayOptions)16;
|
|
if (quoteStrings)
|
|
{
|
|
val = (ObjectDisplayOptions)(val | 8);
|
|
}
|
|
if (useHexadecimalNumbers)
|
|
{
|
|
val = (ObjectDisplayOptions)(val | 4);
|
|
}
|
|
return ObjectDisplay.FormatPrimitive(obj, val);
|
|
}
|
|
|
|
public static string FormatLiteral(string value, bool quote)
|
|
{
|
|
//IL_000a: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_000c: Unknown result type (might be due to invalid IL or missing references)
|
|
ObjectDisplayOptions options = (ObjectDisplayOptions)(0x10 | (quote ? 8 : 0));
|
|
return ObjectDisplay.FormatLiteral(value, options);
|
|
}
|
|
|
|
public static string FormatLiteral(char c, bool quote)
|
|
{
|
|
//IL_000a: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_000c: Unknown result type (might be due to invalid IL or missing references)
|
|
ObjectDisplayOptions options = (ObjectDisplayOptions)(0x10 | (quote ? 8 : 0));
|
|
return ObjectDisplay.FormatLiteral(c, options);
|
|
}
|
|
}
|