internal MarshalPseudoCustomAttributeData WithTranslatedTypes <TTypeSymbol, TArg>(
            Func <TTypeSymbol, TArg, TTypeSymbol> translator,
            TArg arg
            ) where TTypeSymbol : ITypeSymbolInternal
        {
            if (
                _marshalType != Cci.Constants.UnmanagedType_SafeArray ||
                _marshalTypeNameOrSymbol == null
                )
            {
                return(this);
            }

            var translatedType = translator((TTypeSymbol)_marshalTypeNameOrSymbol, arg);

            if ((object)translatedType == (object)_marshalTypeNameOrSymbol)
            {
                return(this);
            }

            var result = new MarshalPseudoCustomAttributeData();

            result.SetMarshalAsSafeArray((Cci.VarEnum)_marshalArrayElementType, translatedType);
            return(result);
        }
示例#2
0
        MarshalPseudoCustomAttributeData IMarshalAsAttributeTarget.GetOrCreateData()
        {
            VerifySealed(expected: false);
            if (_lazyMarshalAsData == null)
            {
                _lazyMarshalAsData = new MarshalPseudoCustomAttributeData();
                SetDataStored();
            }

            return(_lazyMarshalAsData);
        }
        private static void DecodeMarshalAsArray(ref DecodeWellKnownAttributeArguments <TAttributeSyntax, TAttributeData, TAttributeLocation> arguments, CommonMessageProvider messageProvider, bool isFixed)
        {
            Debug.Assert((object)arguments.AttributeSyntaxOpt != null);

            UnmanagedType?elementType    = null;
            int?          elementCount   = isFixed ? 1 : (int?)null;
            short?        parameterIndex = null;
            bool          hasErrors      = false;

            int position = 1;

            foreach (System.Collections.Generic.KeyValuePair <string, TypedConstant> namedArg in arguments.Attribute.NamedArguments)
            {
                switch (namedArg.Key)
                {
                // array:
                case "ArraySubType":
                    elementType = namedArg.Value.DecodeValue <UnmanagedType>(SpecialType.System_Enum);

                    // for some reason, Dev10 metadata writer disallows CustomMarshaler type as an element type of non-fixed arrays
                    if (!isFixed && elementType == Cci.Constants.UnmanagedType_CustomMarshaler ||
                        (int)elementType < 0 ||
                        (int)elementType > MarshalPseudoCustomAttributeData.MaxMarshalInteger)
                    {
                        messageProvider.ReportInvalidNamedArgument(arguments.Diagnostics, arguments.AttributeSyntaxOpt, position, arguments.Attribute.AttributeClass, namedArg.Key);
                        hasErrors = true;
                    }

                    break;

                case "SizeConst":
                    elementCount = namedArg.Value.DecodeValue <int>(SpecialType.System_Int32);
                    if (elementCount < 0 || elementCount > MarshalPseudoCustomAttributeData.MaxMarshalInteger)
                    {
                        messageProvider.ReportInvalidNamedArgument(arguments.Diagnostics, arguments.AttributeSyntaxOpt, position, arguments.Attribute.AttributeClass, namedArg.Key);
                        hasErrors = true;
                    }

                    break;

                case "SizeParamIndex":
                    if (isFixed)
                    {
                        goto case "SafeArraySubType";
                    }

                    parameterIndex = namedArg.Value.DecodeValue <short>(SpecialType.System_Int16);
                    if (parameterIndex < 0)
                    {
                        messageProvider.ReportInvalidNamedArgument(arguments.Diagnostics, arguments.AttributeSyntaxOpt, position, arguments.Attribute.AttributeClass, namedArg.Key);
                        hasErrors = true;
                    }

                    break;

                case "SafeArraySubType":
                    messageProvider.ReportParameterNotValidForType(arguments.Diagnostics, arguments.AttributeSyntaxOpt, position);
                    hasErrors = true;
                    break;
                    // other parameters ignored with no error
                }

                position++;
            }

            if (!hasErrors)
            {
                MarshalPseudoCustomAttributeData data = arguments.GetOrCreateData <TWellKnownAttributeData>().GetOrCreateData();
                if (isFixed)
                {
                    data.SetMarshalAsFixedArray(elementType, elementCount);
                }
                else
                {
                    data.SetMarshalAsArray(elementType, elementCount, parameterIndex);
                }
            }
        }