Beispiel #1
0
 /// <summary>
 /// Overridden convertor for arrays. Converts the <see cref="ArrayShapeSignatureToken"/>
 /// to its correct cref name equivelant.
 /// </summary>
 /// <param name="sb">The string being constructed containing the cref name.</param>
 /// <param name="resolvedType">The type the parameter has been resolved to</param>
 /// <param name="shape">The signiture token detailing the shape of the array.</param>
 internal virtual void ConvertArray(StringBuilder sb, TypeRef resolvedType, ArrayShapeSignatureToken shape)
 {
     GetTypeName(sb, resolvedType);
     sb.Append("[");
     for (int i = 0; i < shape.Rank; i++)
     {
         if (i != 0 && i != shape.Rank)
         {
             sb.Append(",");
         }
         bool hasLoBound = i < shape.LoBounds.Length;
         bool hasSize    = i < shape.Sizes.Length;
         if (hasLoBound)
         {
             sb.Append(shape.LoBounds[i]);
         }
         if (hasLoBound || hasSize)
         {
             sb.Append(":");
         }
         if (hasSize)
         {
             sb.Append(shape.Sizes[i]);
         }
     }
     sb.Append("]");
 }
Beispiel #2
0
        /// <summary>
        /// Method which performs the actual conversion of a signiture to a cref string.
        /// </summary>
        /// <param name="sb">The string builder to hold the converted text</param>
        /// <param name="assembly">The assembly the current parameter is defined in</param>
        /// <param name="param">The parameter definition, required for token resolution</param>
        /// <param name="currentToken">The current token to converty</param>
        /// <param name="elementType">The type of element the token represents</param>
        /// <param name="resolvedType">The resolved type for this token.</param>
        private void Convert(StringBuilder sb, AssemblyDef assembly, ParamDef param, SignatureToken currentToken, ElementTypes elementType, TypeRef resolvedType)
        {
            StringBuilder convertedSigniture = sb;

            if (currentToken.TokenType == SignatureTokens.Param)
            {
                ParamSignatureToken paramToken = currentToken as ParamSignatureToken;
                SignatureToken      childToken = paramToken.Tokens[paramToken.Tokens.Count - 1];

                if (childToken.TokenType == SignatureTokens.Type)
                {
                    currentToken = childToken;
                    elementType  = ((TypeSignatureToken)childToken).ElementType.ElementType;
                }
                else
                {
                    currentToken = childToken;
                    elementType  = ((ElementTypeSignatureToken)childToken).ElementType;
                }
            }

            TypeSignatureToken        typeToken    = currentToken as TypeSignatureToken;
            ElementTypeSignatureToken elementToken = currentToken as ElementTypeSignatureToken;

            switch (elementType)
            {
            case ElementTypes.Var: ConvertVar(convertedSigniture, typeToken.ElementType.Token, param); break;

            case ElementTypes.MVar: ConvertMVar(convertedSigniture, typeToken.ElementType.Token, param); break;

            case ElementTypes.SZArray:
                // TODO: Fix the custom modifier section
                if (typeToken.Tokens[0] is CustomModifierToken)
                {
                    NotImplementedException ex = new NotImplementedException("Custom modifiers are not implemented on SZArray");
                    ex.Data["token"] = currentToken;
                    throw ex;
                }

                ElementTypes szArrayElementType;
                if (typeToken.Tokens[1].TokenType == SignatureTokens.Type)
                {
                    szArrayElementType = ((TypeSignatureToken)typeToken.Tokens[1]).ElementType.ElementType;
                }
                else
                {
                    szArrayElementType = ((ElementTypeSignatureToken)typeToken.Tokens[1]).ElementType;
                }

                Convert(
                    sb,
                    assembly,
                    param,
                    typeToken.Tokens[1],
                    szArrayElementType,
                    resolvedType);

                convertedSigniture.Append("[]");
                break;

            case ElementTypes.Array:
                ArrayShapeSignatureToken shape = typeToken.Tokens.Last() as ArrayShapeSignatureToken;
                ConvertArray(convertedSigniture, resolvedType, shape);
                break;

            case ElementTypes.GenericInstance:
                TypeRef genericType = ((ElementTypeSignatureToken)typeToken.Tokens[1]).ResolveToken(assembly);
                GetTypeName(convertedSigniture, genericType);

                GenericArgumentCountSignatureToken argsCount = typeToken.GetGenericArgumentCount();
                bool isFirstArgument = true;
                if (argsCount.Count > 0)
                {
                    sb.Append(GenericStart);
                    for (int j = 0; j < argsCount.Count; j++)
                    {
                        if (isFirstArgument)
                        {
                            isFirstArgument = false;
                        }
                        else
                        {
                            sb.Append(",");
                        }

                        TypeRef      argResolvedType;
                        ElementTypes elType;
                        if (typeToken.Tokens[j + 3].TokenType == SignatureTokens.ElementType)
                        {
                            ElementTypeSignatureToken gESig = (ElementTypeSignatureToken)typeToken.Tokens[j + 3];
                            argResolvedType = gESig.ResolveToken(assembly);
                            elType          = gESig.ElementType;
                        }
                        else
                        {
                            TypeSignatureToken gTSig = (TypeSignatureToken)typeToken.Tokens[j + 3];
                            argResolvedType = gTSig.ResolveType(assembly, param);
                            elType          = gTSig.ElementType.ElementType;
                        }

                        Convert(
                            sb,
                            assembly,
                            param,
                            typeToken.Tokens[j + 3],
                            elType,
                            argResolvedType);
                    }
                    sb.Append(GenericEnd);
                }
                break;

            case ElementTypes.Class:
            case ElementTypes.ValueType:
            case ElementTypes.Char:
            case ElementTypes.I:
            case ElementTypes.I1:
            case ElementTypes.I2:
            case ElementTypes.I4:
            case ElementTypes.I8:
            case ElementTypes.Object:
            case ElementTypes.R4:
            case ElementTypes.R4_HFA:
            case ElementTypes.R8:
            case ElementTypes.R8_HFA:
            case ElementTypes.String:
            case ElementTypes.U:
            case ElementTypes.U1:
            case ElementTypes.U2:
            case ElementTypes.U4:
            case ElementTypes.U8:
            case ElementTypes.TypedByRef:
            case ElementTypes.Boolean:
                GetTypeName(convertedSigniture, resolvedType);
                break;

            case ElementTypes.Ptr:
                GetTypeName(convertedSigniture, resolvedType);
                convertedSigniture.Append("*");
                break;

            case ElementTypes.FunctionPointer:
                NotImplementedException fnPtrEx = new NotImplementedException("Function Pointer is not implemented yet.");
                fnPtrEx.Data["token"] = currentToken;
                throw fnPtrEx;
            }
        }