public void ReportInvalidMarshallingAttributeInfo(
     AttributeData attributeData,
     string reasonResourceName,
     params string[] reasonArgs)
 {
     _diagnostics.Add(
         attributeData.CreateDiagnostic(
             GeneratorDiagnostics.MarshallingAttributeConfigurationNotSupported,
             new LocalizableResourceString(reasonResourceName, SR.ResourceManager, typeof(FxResources.Microsoft.Interop.JavaScript.JSImportGenerator.SR), reasonArgs)));
 }
Beispiel #2
0
 public void ReportInvalidMarshallingAttributeInfo(
     AttributeData attributeData,
     string reasonResourceName,
     params string[] reasonArgs)
 {
     _diagnostics.Add(
         attributeData.CreateDiagnostic(
             GeneratorDiagnostics.MarshallingAttributeConfigurationNotSupported,
             new LocalizableResourceString(reasonResourceName, Resources.ResourceManager, typeof(Resources), reasonArgs)));
 }
 /// <summary>
 /// Report diagnostic for invalid configuration for string marshalling.
 /// </summary>
 /// <param name="attributeData">Attribute specifying the invalid configuration</param>
 /// <param name="methodName">Name of the method</param>
 /// <param name="detailsMessage">Specific reason the configuration is invalid</param>
 public void ReportInvalidStringMarshallingConfiguration(
     AttributeData attributeData,
     string methodName,
     string detailsMessage)
 {
     _diagnostics.Add(
         attributeData.CreateDiagnostic(
             GeneratorDiagnostics.InvalidStringMarshallingConfiguration,
             methodName,
             detailsMessage));
 }
 /// <summary>
 /// Report diagnostic for configuration that is not supported by the DLL import source generator
 /// </summary>
 /// <param name="attributeData">Attribute specifying the unsupported configuration</param>
 /// <param name="configurationName">Name of the configuration</param>
 /// <param name="unsupportedValue">[Optiona] Unsupported configuration value</param>
 public void ReportConfigurationNotSupported(
     AttributeData attributeData,
     string configurationName,
     string?unsupportedValue = null)
 {
     if (unsupportedValue == null)
     {
         _diagnostics.Add(
             attributeData.CreateDiagnostic(
                 GeneratorDiagnostics.ConfigurationNotSupported,
                 configurationName));
     }
     else
     {
         _diagnostics.Add(
             attributeData.CreateDiagnostic(
                 GeneratorDiagnostics.ConfigurationValueNotSupported,
                 unsupportedValue,
                 configurationName));
     }
 }
            private void AnalyzeNativeMarshalerType(SymbolAnalysisContext context, ITypeSymbol type, AttributeData nativeMarshalerAttributeData, bool isNativeMarshallingAttribute)
            {
                if (nativeMarshalerAttributeData.ConstructorArguments.Length == 0)
                {
                    // This is a MarshalUsing with just count information.
                    return;
                }

                if (nativeMarshalerAttributeData.ConstructorArguments[0].IsNull)
                {
                    context.ReportDiagnostic(
                        nativeMarshalerAttributeData.CreateDiagnostic(
                            NativeTypeMustBeNonNullRule,
                            type.ToDisplayString()));
                    return;
                }

                ITypeSymbol nativeType = (ITypeSymbol)nativeMarshalerAttributeData.ConstructorArguments[0].Value !;
                ISymbol     nativeTypeDiagnosticsTargetSymbol = nativeType;

                if (nativeType is not INamedTypeSymbol marshalerType)
                {
                    context.ReportDiagnostic(
                        GetDiagnosticLocations(context, nativeType, nativeMarshalerAttributeData).CreateDiagnostic(
                            NativeTypeMustHaveRequiredShapeRule,
                            nativeType.ToDisplayString(),
                            type.ToDisplayString()));
                    return;
                }

                DiagnosticDescriptor requiredShapeRule = NativeTypeMustHaveRequiredShapeRule;

                ManualTypeMarshallingHelper.NativeTypeMarshallingVariant variant = ManualTypeMarshallingHelper.NativeTypeMarshallingVariant.Standard;
                if (marshalerType.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(_genericContiguousCollectionMarshallerAttribute, a.AttributeClass)))
                {
                    variant           = ManualTypeMarshallingHelper.NativeTypeMarshallingVariant.ContiguousCollection;
                    requiredShapeRule = CollectionNativeTypeMustHaveRequiredShapeRule;
                    if (!ManualTypeMarshallingHelper.TryGetManagedValuesProperty(marshalerType, out _) ||
                        !ManualTypeMarshallingHelper.HasNativeValueStorageProperty(marshalerType, _spanOfByte))
                    {
                        context.ReportDiagnostic(
                            GetDiagnosticLocations(context, marshalerType, nativeMarshalerAttributeData).CreateDiagnostic(
                                requiredShapeRule,
                                nativeType.ToDisplayString(),
                                type.ToDisplayString()));
                        return;
                    }
                }

                if (!nativeType.IsValueType)
                {
                    context.ReportDiagnostic(
                        GetDiagnosticLocations(context, nativeType, nativeMarshalerAttributeData).CreateDiagnostic(
                            requiredShapeRule,
                            nativeType.ToDisplayString(),
                            type.ToDisplayString()));
                    return;
                }

                if (marshalerType.IsUnboundGenericType)
                {
                    if (!isNativeMarshallingAttribute)
                    {
                        context.ReportDiagnostic(
                            nativeMarshalerAttributeData.CreateDiagnostic(
                                NativeGenericTypeMustBeClosedOrMatchArityRule,
                                nativeType.ToDisplayString(),
                                type.ToDisplayString()));
                        return;
                    }
                    if (type is not INamedTypeSymbol namedType || marshalerType.TypeArguments.Length != namedType.TypeArguments.Length)
                    {
                        context.ReportDiagnostic(
                            nativeMarshalerAttributeData.CreateDiagnostic(
                                NativeGenericTypeMustBeClosedOrMatchArityRule,
                                nativeType.ToDisplayString(),
                                type.ToDisplayString()));
                        return;
                    }
                    // Construct the marshaler type around the same type arguments as the managed type.
                    nativeType = marshalerType = marshalerType.ConstructedFrom.Construct(namedType.TypeArguments, namedType.TypeArgumentNullableAnnotations);
                }

                bool hasConstructor = false;
                bool hasCallerAllocSpanConstructor = false;

                foreach (IMethodSymbol ctor in marshalerType.Constructors)
                {
                    if (ctor.IsStatic)
                    {
                        continue;
                    }

                    hasConstructor = hasConstructor || ManualTypeMarshallingHelper.IsManagedToNativeConstructor(ctor, type, variant);

                    if (!hasCallerAllocSpanConstructor && ManualTypeMarshallingHelper.IsCallerAllocatedSpanConstructor(ctor, type, _spanOfByte, variant))
                    {
                        hasCallerAllocSpanConstructor = true;
                        IFieldSymbol bufferSizeField = nativeType.GetMembers(ManualTypeMarshallingHelper.BufferSizeFieldName).OfType <IFieldSymbol>().FirstOrDefault();
                        if (bufferSizeField is null or {
                            DeclaredAccessibility: not Accessibility.Public
                        } or {
                            IsConst: false
                        } or {
                            Type: not {
                                SpecialType: SpecialType.System_Int32
                            }
                        })