public void AnalyzeAttribute(SyntaxNodeAnalysisContext context) { AttributeSyntax syntax = (AttributeSyntax)context.Node; ISymbol attributedSymbol = context.ContainingSymbol !; AttributeData?attr = syntax.FindAttributeData(attributedSymbol); if (attr?.AttributeClass?.ToDisplayString() == TypeNames.CustomMarshallerAttribute && attr.AttributeConstructor is not null) { DiagnosticReporter managedTypeReporter = DiagnosticReporter.CreateForLocation(syntax.FindArgumentWithNameOrArity("managedType", 0).FindTypeExpressionOrNullLocation(), context.ReportDiagnostic); INamedTypeSymbol entryType = (INamedTypeSymbol)attributedSymbol; INamedTypeSymbol?managedTypeInAttribute = (INamedTypeSymbol?)attr.ConstructorArguments[0].Value; if (managedTypeInAttribute is null) { managedTypeReporter.CreateAndReportDiagnostic(ManagedTypeMustBeNonNullRule, entryType.ToDisplayString()); } if (!ManualTypeMarshallingHelper.TryResolveManagedType( entryType, managedTypeInAttribute, ManualTypeMarshallingHelper.IsLinearCollectionEntryPoint(entryType), (entryType, managedType) => managedTypeReporter.CreateAndReportDiagnostic(ManagedTypeMustBeClosedOrMatchArityRule, managedType, entryType), out ITypeSymbol managedType)) { return; } DiagnosticReporter marshallerTypeReporter = DiagnosticReporter.CreateForLocation(syntax.FindArgumentWithNameOrArity("marshallerType", 2).FindTypeExpressionOrNullLocation(), context.ReportDiagnostic); ITypeSymbol? marshallerTypeInAttribute = (ITypeSymbol?)attr.ConstructorArguments[2].Value; if (marshallerTypeInAttribute is null) { marshallerTypeReporter.CreateAndReportDiagnostic(MarshallerTypeMustBeNonNullRule); } if (!ManualTypeMarshallingHelper.TryResolveMarshallerType( entryType, marshallerTypeInAttribute, (entryType, marshallerType) => marshallerTypeReporter.CreateAndReportDiagnostic(MarshallerTypeMustBeClosedOrMatchArityRule, marshallerType, entryType), out ITypeSymbol marshallerType)) { return; } AnalyzeMarshallerType( marshallerTypeReporter, (INamedTypeSymbol)managedType, (MarshalMode)attr.ConstructorArguments[1].Value, (INamedTypeSymbol?)marshallerType, ManualTypeMarshallingHelper.IsLinearCollectionEntryPoint(entryType)); } }
public void AnalyzeAttribute(SyntaxNodeAnalysisContext context) { AttributeSyntax syntax = (AttributeSyntax)context.Node; ISymbol attributedSymbol = context.ContainingSymbol !; AttributeData?attr = syntax.FindAttributeData(attributedSymbol); if (attr?.AttributeClass?.ToDisplayString() == TypeNames.NativeMarshallingAttribute && attr.AttributeConstructor is not null) { INamedTypeSymbol?entryType = (INamedTypeSymbol?)attr.ConstructorArguments[0].Value; AnalyzeManagedTypeMarshallingInfo( GetSymbolType(attributedSymbol), DiagnosticReporter.CreateForLocation(syntax.FindArgumentWithNameOrArity("nativeType", 0).FindTypeExpressionOrNullLocation(), context.ReportDiagnostic), entryType); } }
private void AnalyzeManagedTypeMarshallingInfo( ITypeSymbol managedType, DiagnosticReporter diagnosticFactory, INamedTypeSymbol?entryType) { if (entryType is null) { diagnosticFactory.CreateAndReportDiagnostic( MarshallerEntryPointTypeMustBeNonNullRule, managedType.ToDisplayString()); return; } if (!ManualTypeMarshallingHelper.HasEntryPointMarshallerAttribute(entryType)) { diagnosticFactory.CreateAndReportDiagnostic( MarshallerEntryPointTypeMustHaveCustomMarshallerAttributeWithMatchingManagedTypeRule, entryType.ToDisplayString(), managedType.ToDisplayString()); return; } bool isLinearCollectionMarshaller = ManualTypeMarshallingHelper.IsLinearCollectionEntryPoint(entryType); if (entryType.IsUnboundGenericType) { if (managedType is not INamedTypeSymbol namedManagedType) { diagnosticFactory.CreateAndReportDiagnostic( GenericEntryPointMarshallerTypeMustBeClosedOrMatchArityRule, entryType.ToDisplayString(), managedType.ToDisplayString()); return; } if (!ManualTypeMarshallingHelper.TryResolveEntryPointType( namedManagedType, entryType, isLinearCollectionMarshaller, (managedType, entryType) => diagnosticFactory.CreateAndReportDiagnostic( GenericEntryPointMarshallerTypeMustBeClosedOrMatchArityRule, entryType.ToDisplayString(), managedType.ToDisplayString()), out ITypeSymbol resolvedEntryType)) { return; } entryType = (INamedTypeSymbol)resolvedEntryType; } if (!ManualTypeMarshallingHelper.TryGetMarshallersFromEntryTypeIgnoringElements( entryType, managedType, _compilation, (entryType, managedType) => diagnosticFactory.CreateAndReportDiagnostic( GenericEntryPointMarshallerTypeMustBeClosedOrMatchArityRule, entryType.ToDisplayString(), managedType.ToDisplayString()), out _)) { diagnosticFactory.CreateAndReportDiagnostic( MarshallerEntryPointTypeMustHaveCustomMarshallerAttributeWithMatchingManagedTypeRule, entryType.ToDisplayString(), managedType.ToDisplayString()); } }
#pragma warning disable CA1822 // Mark members as static private void AnalyzeMarshallerType(DiagnosticReporter diagnosticFactory, INamedTypeSymbol?managedType, MarshalMode mode, INamedTypeSymbol?marshallerType, bool isLinearCollectionMarshaller) #pragma warning restore CA1822 // Mark members as static { // TODO: Implement for the V2 shapes }