Пример #1
0
        public MarshallingInfo ParseMarshallingInfo(
            ITypeSymbol managedType,
            IEnumerable <AttributeData> useSiteAttributes,
            MarshallingInfo inner)
        {
            JSTypeFlags        jsType          = JSTypeFlags.None;
            List <JSTypeFlags> jsTypeArguments = new List <JSTypeFlags>();

            foreach (AttributeData useSiteAttribute in useSiteAttributes)
            {
                INamedTypeSymbol attributeClass = useSiteAttribute.AttributeClass !;
                if (attributeClass.IsGenericType && SymbolEqualityComparer.Default.Equals(_jsMarshalAsAttribute, attributeClass.ConstructUnboundGenericType()))
                {
                    INamedTypeSymbol?jsTypeArgs = attributeClass.TypeArguments[0] as INamedTypeSymbol;
                    if (jsTypeArgs.IsGenericType)
                    {
                        string gt   = jsTypeArgs.ConstructUnboundGenericType().ToDisplayString();
                        string name = gt.Substring(gt.IndexOf("JSType") + "JSType.".Length);
                        name = name.Substring(0, name.IndexOf("<"));

                        Enum.TryParse(name, out jsType);

                        foreach (var ta in jsTypeArgs.TypeArguments.Cast <INamedTypeSymbol>().Select(x => x.ToDisplayString()))
                        {
                            string      argName   = ta.Substring(ta.IndexOf("JSType") + "JSType.".Length);
                            JSTypeFlags jsTypeArg = JSTypeFlags.None;
                            Enum.TryParse(argName, out jsTypeArg);
                            jsTypeArguments.Add(jsTypeArg);
                        }
                    }
                    else
                    {
                        string st   = jsTypeArgs.ToDisplayString();
                        string name = st.Substring(st.IndexOf("JSType") + "JSType.".Length);
                        Enum.TryParse(name, out jsType);
                    }
                }
                if (SymbolEqualityComparer.Default.Equals(_marshalUsingAttribute, attributeClass))
                {
                    return(new JSMarshallingInfo(inner)
                    {
                        JSType = JSTypeFlags.Array,
                        JSTypeArguments = Array.Empty <JSTypeFlags>(),
                    });
                }
            }

            if (jsType == JSTypeFlags.None)
            {
                return(new JSMissingMarshallingInfo());
            }

            return(new JSMarshallingInfo(inner)
            {
                JSType = jsType,
                JSTypeArguments = jsTypeArguments.ToArray(),
            });
        }
Пример #2
0
            ExpressionSyntax GetExpressionForParam(TypePositionInfo paramInfo, out bool isIntType)
            {
                ExpressionSyntax numElementsExpression = GetIndexedNumElementsExpression(
                    context,
                    paramInfo,
                    out int numIndirectionLevels);

                ManagedTypeInfo type            = paramInfo.ManagedType;
                MarshallingInfo marshallingInfo = paramInfo.MarshallingAttributeInfo;

                for (int i = 0; i < numIndirectionLevels; i++)
                {
                    if (marshallingInfo is NativeLinearCollectionMarshallingInfo collectionInfo)
                    {
                        CustomTypeMarshallerData marshallerData = GetMarshallerDataForTypePositionInfo(collectionInfo.Marshallers, info);
                        type            = marshallerData.CollectionElementType;
                        marshallingInfo = marshallerData.CollectionElementMarshallingInfo;
                    }
                    else
                    {
                        throw new MarshallingNotSupportedException(info, context)
                              {
                                  NotSupportedDetails = SR.CollectionSizeParamTypeMustBeIntegral
                              };
                    }
                }

                if (type is not SpecialTypeInfo specialType || !specialType.SpecialType.IsIntegralType())
                {
                    throw new MarshallingNotSupportedException(info, context)
                          {
                              NotSupportedDetails = SR.CollectionSizeParamTypeMustBeIntegral
                          };
                }

                isIntType = specialType.SpecialType == SpecialType.System_Int32;
                return(isIntType
                    ? numElementsExpression
                    : CastExpression(
                           PredefinedType(Token(SyntaxKind.IntKeyword)),
                           ParenthesizedExpression(numElementsExpression)));
            }
Пример #3
0
        private MarshallingInfo ParseMarshallingInfo(
            ITypeSymbol managedType,
            IEnumerable <AttributeData> useSiteAttributes,
            ImmutableHashSet <string> inspectedElements)
        {
            Dictionary <int, AttributeData> marshallingAttributesByIndirectionDepth = new();
            int maxIndirectionLevelDataProvided = 0;

            foreach (AttributeData attribute in useSiteAttributes)
            {
                if (TryGetAttributeIndirectionLevel(attribute, out int indirectionLevel))
                {
                    if (marshallingAttributesByIndirectionDepth.ContainsKey(indirectionLevel))
                    {
                        _diagnostics.ReportInvalidMarshallingAttributeInfo(attribute, nameof(SR.DuplicateMarshallingInfo), indirectionLevel.ToString());
                        return(NoMarshallingInfo.Instance);
                    }
                    marshallingAttributesByIndirectionDepth.Add(indirectionLevel, attribute);
                    maxIndirectionLevelDataProvided = Math.Max(maxIndirectionLevelDataProvided, indirectionLevel);
                }
            }

            int             maxIndirectionDepthUsed = 0;
            MarshallingInfo info = GetMarshallingInfo(
                managedType,
                marshallingAttributesByIndirectionDepth,
                indirectionLevel: 0,
                inspectedElements,
                ref maxIndirectionDepthUsed);

            if (maxIndirectionDepthUsed < maxIndirectionLevelDataProvided)
            {
                _diagnostics.ReportInvalidMarshallingAttributeInfo(
                    marshallingAttributesByIndirectionDepth[maxIndirectionLevelDataProvided],
                    nameof(SR.ExtraneousMarshallingInfo),
                    maxIndirectionLevelDataProvided.ToString(),
                    maxIndirectionDepthUsed.ToString());
            }
            return(info);
        }