本文整理汇总了C#中ExpressionSyntax.CSharpKind方法的典型用法代码示例。如果您正苦于以下问题:C# ExpressionSyntax.CSharpKind方法的具体用法?C# ExpressionSyntax.CSharpKind怎么用?C# ExpressionSyntax.CSharpKind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ExpressionSyntax
的用法示例。
在下文中一共展示了ExpressionSyntax.CSharpKind方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DecomposeName
/// <summary>
/// Decomposes a name or member access expression into its component parts.
/// </summary>
/// <param name="expression">The name or member access expression.</param>
/// <param name="qualifier">The qualifier (or left-hand-side) of the name expression. This may be null if there is no qualifier.</param>
/// <param name="name">The name of the expression.</param>
/// <param name="arity">The number of generic type parameters.</param>
private static void DecomposeName(ExpressionSyntax expression, out ExpressionSyntax qualifier, out string name, out int arity)
{
switch (expression.CSharpKind())
{
case SyntaxKind.SimpleMemberAccessExpression:
case SyntaxKind.PointerMemberAccessExpression:
var max = (MemberAccessExpressionSyntax)expression;
qualifier = max.Expression;
name = max.Name.Identifier.ValueText;
arity = max.Name.Arity;
break;
case SyntaxKind.QualifiedName:
var qn = (QualifiedNameSyntax)expression;
qualifier = qn.Left;
name = qn.Right.Identifier.ValueText;
arity = qn.Arity;
break;
case SyntaxKind.AliasQualifiedName:
var aq = (AliasQualifiedNameSyntax)expression;
qualifier = aq.Alias;
name = aq.Name.Identifier.ValueText;
arity = aq.Name.Arity;
break;
case SyntaxKind.GenericName:
var gx = (GenericNameSyntax)expression;
qualifier = null;
name = gx.Identifier.ValueText;
arity = gx.Arity;
break;
case SyntaxKind.IdentifierName:
var nx = (IdentifierNameSyntax)expression;
qualifier = null;
name = nx.Identifier.ValueText;
arity = 0;
break;
default:
qualifier = null;
name = null;
arity = 0;
break;
}
}
示例2: IsNullableTypeInPointerExpression
private static bool IsNullableTypeInPointerExpression(ExpressionSyntax expression, ExpressionSyntax simplifiedNode)
{
// Note: nullable type syntax is not allowed in pointer type syntax
if (simplifiedNode.CSharpKind() == SyntaxKind.NullableType &&
simplifiedNode.DescendantNodes().Any(n => n is PointerTypeSyntax))
{
return true;
}
return false;
}
示例3: WillConflictWithExistingLocal
private static bool WillConflictWithExistingLocal(ExpressionSyntax expression, ExpressionSyntax simplifiedNode)
{
if (simplifiedNode.CSharpKind() == SyntaxKind.IdentifierName && !SyntaxFacts.IsInNamespaceOrTypeContext(expression))
{
var identifierName = (IdentifierNameSyntax)simplifiedNode;
var enclosingDeclarationSpace = FindImmediatelyEnclosingLocalVariableDeclarationSpace(expression);
var enclosingMemberDeclaration = expression.FirstAncestorOrSelf<MemberDeclarationSyntax>();
if (enclosingDeclarationSpace != null && enclosingMemberDeclaration != null)
{
var locals = enclosingMemberDeclaration.GetLocalDeclarationMap()[identifierName.Identifier.ValueText];
foreach (var token in locals)
{
if (token.GetAncestors<SyntaxNode>().Contains(enclosingDeclarationSpace))
{
return true;
}
}
}
}
return false;
}
示例4: ConstantType
public static TypeSyntax ConstantType(ExpressionSyntax value)
{
var valueStr = value.ToString();
switch (value.CSharpKind())
{
case SyntaxKind.NumericLiteralExpression:
{
int val;
double dval;
if (int.TryParse(valueStr, out val))
return Compiler.Int;
else if (double.TryParse(valueStr, out dval))
return Compiler.Double;
break;
}
case SyntaxKind.StringLiteralExpression:
return Compiler.String;
case SyntaxKind.TrueLiteralExpression:
case SyntaxKind.FalseLiteralExpression:
return Compiler.Boolean;
}
return SyntaxFactory.ParseTypeName(valueStr);
}
示例5: IsAssignmentOfPropertyValueParameterToBackingField
private static bool IsAssignmentOfPropertyValueParameterToBackingField(
ExpressionSyntax expression,
IFieldSymbol backingField,
SemanticModel semanticModel)
{
if (expression.CSharpKind() != SyntaxKind.SimpleAssignmentExpression)
{
return false;
}
var assignment = (BinaryExpressionSyntax)expression;
return IsBackingField(assignment.Left, backingField, semanticModel)
&& IsPropertyValueParameter(assignment.Right, semanticModel);
}
示例6: FlattenCondition
public void FlattenCondition(ExpressionSyntax expr, string prefix, bool ss_jump_if_value, string ss_jump_label, List<FlatStatement> instructions)
{
if (expr is LiteralExpressionSyntax)
{
switch (expr.CSharpKind())
{
case SyntaxKind.TrueLiteralExpression:
if (ss_jump_if_value)
instructions.Add(FlatStatement.JMP(FlatOperand.LabelRef(ss_jump_label)));
return;
case SyntaxKind.FalseLiteralExpression:
if (!ss_jump_if_value)
instructions.Add(FlatStatement.JMP(FlatOperand.LabelRef(ss_jump_label)));
return;
}
throw new NotImplementedException("literal expression " + expr.CSharpKind().ToString());
}
if (expr is BinaryExpressionSyntax)
{
BinaryExpressionSyntax condition = expr as BinaryExpressionSyntax;
switch (condition.CSharpKind())
{
case SyntaxKind.LogicalAndExpression:
{
FlattenCondition(condition.Left, prefix, ss_jump_if_value, ss_jump_label, instructions);
// fell through because left side didnt match. check right side
FlattenCondition(condition.Right, prefix, ss_jump_if_value, ss_jump_label, instructions);
// fell through because right side didnt match either.
return;
}
break;
case SyntaxKind.LogicalOrExpression:
{
string ifEnterLabel = prefix + MakeUniqueLabelPrefix("shortcircuit");
FlattenCondition(condition.Left, prefix, !ss_jump_if_value, ifEnterLabel, instructions); // short circuit if left side matches
// fell through because left side didn't match. check right side
FlattenCondition(condition.Right, prefix, ss_jump_if_value, ss_jump_label, instructions);
instructions.Add(FlatStatement.LABEL(FlatOperand.LabelRef(ifEnterLabel)));
return;
}
break;
case SyntaxKind.IsExpression:
{
FlatOperand fop_result = ResolveExpression(expr, null, instructions);
if (ss_jump_if_value)
instructions.Add(FlatStatement.JNZ(FlatOperand.LabelRef(ss_jump_label), fop_result));
else
instructions.Add(FlatStatement.JZ(FlatOperand.LabelRef(ss_jump_label), fop_result));
}
return;
}
FlatOperand opnd_left = ResolveExpression(condition.Left, null, instructions);
FlatOperand opnd_right = ResolveExpression(condition.Right, null, instructions);
switch (condition.CSharpKind())
{
case SyntaxKind.GreaterThanOrEqualExpression:
if (ss_jump_if_value)
instructions.Add(FlatStatement.JGE(FlatOperand.LabelRef(ss_jump_label), opnd_left, opnd_right));
else
instructions.Add(FlatStatement.JL(FlatOperand.LabelRef(ss_jump_label), opnd_left, opnd_right));
return;
case SyntaxKind.GreaterThanExpression:
if (ss_jump_if_value)
instructions.Add(FlatStatement.JG(FlatOperand.LabelRef(ss_jump_label), opnd_left, opnd_right));
else
instructions.Add(FlatStatement.JLE(FlatOperand.LabelRef(ss_jump_label), opnd_left, opnd_right));
return;
case SyntaxKind.LessThanExpression:
if (ss_jump_if_value)
instructions.Add(FlatStatement.JL(FlatOperand.LabelRef(ss_jump_label), opnd_left, opnd_right));
else
instructions.Add(FlatStatement.JGE(FlatOperand.LabelRef(ss_jump_label), opnd_left, opnd_right));
return;
case SyntaxKind.LessThanOrEqualExpression:
if (ss_jump_if_value)
instructions.Add(FlatStatement.JLE(FlatOperand.LabelRef(ss_jump_label), opnd_left, opnd_right));
else
instructions.Add(FlatStatement.JG(FlatOperand.LabelRef(ss_jump_label), opnd_left, opnd_right));
return;
case SyntaxKind.NotEqualsExpression:
if (ss_jump_if_value)
instructions.Add(FlatStatement.JNE(FlatOperand.LabelRef(ss_jump_label), opnd_left, opnd_right));
else
instructions.Add(FlatStatement.JE(FlatOperand.LabelRef(ss_jump_label), opnd_left, opnd_right));
return;
case SyntaxKind.EqualsExpression:
if (ss_jump_if_value)
instructions.Add(FlatStatement.JE(FlatOperand.LabelRef(ss_jump_label), opnd_left, opnd_right));
else
instructions.Add(FlatStatement.JNE(FlatOperand.LabelRef(ss_jump_label), opnd_left, opnd_right));
return;
default:
throw new NotImplementedException("binary expression " + condition.CSharpKind());
}
//.........这里部分代码省略.........
示例7: FullyQualifyIdentifierName
private ExpressionSyntax FullyQualifyIdentifierName(
ISymbol symbol,
ExpressionSyntax rewrittenNode,
ExpressionSyntax originalNode,
bool replaceNode,
bool isInsideCref,
bool omitLeftHandSide)
{
Debug.Assert(!replaceNode || rewrittenNode.CSharpKind() == SyntaxKind.IdentifierName);
//// TODO: use and expand Generate*Syntax(isymbol) to not depend on symbol display any more.
//// See GenerateExpressionSyntax();
var result = rewrittenNode;
// only if this symbol has a containing type or namespace there is work for us to do.
if (replaceNode || symbol.ContainingType != null || symbol.ContainingNamespace != null)
{
ImmutableArray<SymbolDisplayPart> displayParts;
ExpressionSyntax left = null;
// we either need to create an AliasQualifiedName if the symbol is directly contained in the global namespace,
// otherwise it a QualifiedName.
if (!replaceNode && symbol.ContainingType == null && symbol.ContainingNamespace.IsGlobalNamespace)
{
return rewrittenNode.CopyAnnotationsTo(
SyntaxFactory.AliasQualifiedName(
SyntaxFactory.IdentifierName(SyntaxFactory.Token(SyntaxKind.GlobalKeyword)),
(SimpleNameSyntax)rewrittenNode.WithLeadingTrivia(null))
.WithLeadingTrivia(rewrittenNode.GetLeadingTrivia()));
}
displayParts = replaceNode
? symbol.ToDisplayParts(TypeNameFormatWithGenerics)
: ((ISymbol)symbol.ContainingType ?? (ISymbol)symbol.ContainingNamespace).ToDisplayParts(TypeNameFormatWithGenerics);
rewrittenNode = TryAddTypeArgumentToIdentifierName(rewrittenNode, symbol);
// Replaces the '<' token with the '{' token since we are inside crefs
rewrittenNode = TryReplaceAngleBracesWithCurlyBraces(rewrittenNode, isInsideCref);
result = rewrittenNode;
if (!omitLeftHandSide)
{
left = SyntaxFactory.ParseTypeName(displayParts.ToDisplayString());
// Replaces the '<' token with the '{' token since we are inside crefs
left = TryReplaceAngleBracesWithCurlyBraces(left, isInsideCref);
if (replaceNode)
{
return left
.WithLeadingTrivia(rewrittenNode.GetLeadingTrivia())
.WithTrailingTrivia(rewrittenNode.GetTrailingTrivia());
}
// now create syntax for the combination of left and right syntax, or a simple replacement in case of an identifier
var parent = originalNode.Parent;
var leadingTrivia = rewrittenNode.GetLeadingTrivia();
rewrittenNode = rewrittenNode.WithLeadingTrivia(null);
switch (parent.CSharpKind())
{
case SyntaxKind.QualifiedName:
var qualifiedParent = (QualifiedNameSyntax)parent;
result = rewrittenNode.CopyAnnotationsTo(
SyntaxFactory.QualifiedName(
(NameSyntax)left,
(SimpleNameSyntax)rewrittenNode));
break;
case SyntaxKind.SimpleMemberAccessExpression:
var memberAccessParent = (MemberAccessExpressionSyntax)parent;
result = rewrittenNode.CopyAnnotationsTo(
SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
left,
(SimpleNameSyntax)rewrittenNode));
break;
default:
Debug.Assert(rewrittenNode is SimpleNameSyntax);
if (SyntaxFacts.IsInNamespaceOrTypeContext(originalNode))
{
var right = (SimpleNameSyntax)rewrittenNode;
result = rewrittenNode.CopyAnnotationsTo(SyntaxFactory.QualifiedName((NameSyntax)left, right.WithAdditionalAnnotations(Simplifier.SpecialTypeAnnotation)));
}
else if (originalNode.Parent is CrefSyntax)
{
var right = (SimpleNameSyntax)rewrittenNode;
result = rewrittenNode.CopyAnnotationsTo(SyntaxFactory.QualifiedName((NameSyntax)left, right));
}
else
{
//.........这里部分代码省略.........
开发者ID:modulexcite,项目名称:pattern-matching-csharp,代码行数:101,代码来源:CSharpSimplificationService.Expander.cs
示例8: TryAddTypeArgumentToIdentifierName
private ExpressionSyntax TryAddTypeArgumentToIdentifierName(ExpressionSyntax newNode, ISymbol symbol)
{
if (newNode.CSharpKind() == SyntaxKind.IdentifierName && symbol.Kind == SymbolKind.Method)
{
if (((IMethodSymbol)symbol).TypeArguments.Length != 0)
{
var typeArguments = ((IMethodSymbol)symbol).TypeArguments;
var genericName = SyntaxFactory.GenericName(
((IdentifierNameSyntax)newNode).Identifier,
SyntaxFactory.TypeArgumentList(
SyntaxFactory.SeparatedList(
typeArguments.Select(p => SyntaxFactory.ParseTypeName(p.ToDisplayParts(TypeNameFormatWithGenerics).ToDisplayString())))))
.WithLeadingTrivia(newNode.GetLeadingTrivia())
.WithTrailingTrivia(newNode.GetTrailingTrivia())
.WithAdditionalAnnotations(Simplifier.Annotation);
genericName = newNode.CopyAnnotationsTo(genericName);
return genericName;
}
}
return newNode;
}