GetAttributes(this SyntaxNode syntax)
+ {
+ switch (syntax)
+ {
+ case StructDeclarationSyntax s:
+ return s.AttributeLists;
+ case TypeParameterSyntax s:
+ return s.AttributeLists;
+ default:
+ return default;
+ }
+ }
+
+ ///
+ /// test whether op is copyable or not even when it is a non-copyable instance.
+ ///
+ public static bool CanCopy(this IOperation op)
+ {
+ var k = op.Kind;
+
+ if (k == OperationKind.Conversion)
+ {
+ var operandKind = ((IConversionOperation)op).Operand.Kind;
+ // default literal (invalid if LangVersion < 7.1)
+ if (operandKind == OperationKind.DefaultValue || operandKind == OperationKind.Invalid) return true;
+ }
+
+ if (k == OperationKind.LocalReference || k == OperationKind.FieldReference || k == OperationKind.PropertyReference || k == OperationKind.ArrayElementReference)
+ {
+ //need help: how to get ref-ness from IOperation?
+ var parent = op.Syntax.Parent.Kind();
+ if (parent == SyntaxKind.RefExpression) return true;
+ }
+
+ if (k == OperationKind.Conditional)
+ {
+ var cond = (IConditionalOperation)op;
+ return cond.WhenFalse.CanCopy() && cond.WhenFalse.CanCopy();
+ }
+
+ return k == OperationKind.ObjectCreation
+ || k == OperationKind.DefaultValue
+ || k == OperationKind.Literal
+ || k == OperationKind.Invocation
+ // workaround for https://github.com/dotnet/roslyn/issues/49751
+ || !IsValid(k) && op.Syntax is InvocationExpressionSyntax;
+
+ //todo: should return value be OK?
+ //todo: move semantics
+ }
+
+ static bool IsValid(OperationKind kind)
+ => kind != OperationKind.None && kind != OperationKind.Invalid;
+
+ public static ISymbol GetSymbol(this IOperation op)
+ {
+ if (op.Kind == OperationKind.LocalReference)
+ return ((ILocalReferenceOperation)op).Local;
+ if (op.Kind == OperationKind.ParameterReference)
+ return ((IParameterReferenceOperation)op).Parameter;
+ throw new NotSupportedException(op.Kind.ToString());
+ }
+ }
+}
pFad - Phonifier reborn
Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy