Ejemplo n.º 1
0
        public static string GetName(this ISymUnmanagedNamespace @namespace)
        {
            int numAvailable;

            @namespace.GetName(0, out numAvailable, null);
            if (numAvailable == 0)
            {
                return("");
            }

            var chars = new char[numAvailable];
            int numRead;

            @namespace.GetName(numAvailable, out numRead, chars);
            if (numRead != numAvailable)
            {
                throw new InvalidOperationException(string.Format("Read only {0} of {1} name characters.", numRead, numAvailable));
            }

            Debug.Assert(chars[numAvailable - 1] == 0);
            return(new string(chars, 0, numAvailable - 1));
        }
Ejemplo n.º 2
0
        private void WriteNamespace(ISymUnmanagedNamespace @namespace)
        {
            string rawName = @namespace.GetName();

            string           alias;
            string           externAlias;
            string           target;
            ImportTargetKind kind;
            ImportScope      scope;

            try
            {
                if (rawName.Length == 0)
                {
                    externAlias = null;
                    var parsingSucceeded = CDI.TryParseVisualBasicImportString(rawName, out alias, out target, out kind, out scope);
                    Debug.Assert(parsingSucceeded);
                }
                else
                {
                    switch (rawName[0])
                    {
                    case 'U':
                    case 'A':
                    case 'X':
                    case 'Z':
                    case 'E':
                    case 'T':
                        scope = ImportScope.Unspecified;
                        if (!CDI.TryParseCSharpImportString(rawName, out alias, out externAlias, out target, out kind))
                        {
                            throw new InvalidOperationException(string.Format("Invalid import '{0}'", rawName));
                        }
                        break;

                    default:
                        externAlias = null;
                        if (!CDI.TryParseVisualBasicImportString(rawName, out alias, out target, out kind, out scope))
                        {
                            throw new InvalidOperationException(string.Format("Invalid import '{0}'", rawName));
                        }
                        break;
                    }
                }
            }
            catch (ArgumentException) // TODO: filter
            {
                if ((options & PdbToXmlOptions.ThrowOnError) != 0)
                {
                    throw;
                }

                writer.WriteStartElement("invalid-custom-data");
                writer.WriteAttributeString("raw", rawName);
                writer.WriteEndElement();
                return;
            }

            switch (kind)
            {
            case ImportTargetKind.CurrentNamespace:
                Debug.Assert(alias == null);
                Debug.Assert(externAlias == null);
                Debug.Assert(scope == ImportScope.Unspecified);
                writer.WriteStartElement("currentnamespace");
                writer.WriteAttributeString("name", target);
                writer.WriteEndElement();     // </currentnamespace>
                break;

            case ImportTargetKind.DefaultNamespace:
                Debug.Assert(alias == null);
                Debug.Assert(externAlias == null);
                Debug.Assert(scope == ImportScope.Unspecified);
                writer.WriteStartElement("defaultnamespace");
                writer.WriteAttributeString("name", target);
                writer.WriteEndElement();     // </defaultnamespace>
                break;

            case ImportTargetKind.MethodToken:
                Debug.Assert(alias == null);
                Debug.Assert(externAlias == null);
                Debug.Assert(scope == ImportScope.Unspecified);
                int token = Convert.ToInt32(target);
                writer.WriteStartElement("importsforward");
                WriteMethodAttributes(token, isReference: true);
                writer.WriteEndElement();     // </importsforward>
                break;

            case ImportTargetKind.XmlNamespace:
                Debug.Assert(externAlias == null);
                writer.WriteStartElement("xmlnamespace");
                writer.WriteAttributeString("prefix", alias);
                writer.WriteAttributeString("name", target);
                WriteScopeAttribute(scope);
                writer.WriteEndElement();     // </xmlnamespace>
                break;

            case ImportTargetKind.NamespaceOrType:
                Debug.Assert(externAlias == null);
                writer.WriteStartElement("alias");
                writer.WriteAttributeString("name", alias);
                writer.WriteAttributeString("target", target);
                writer.WriteAttributeString("kind", "namespace"); // Strange, but retaining to avoid breaking tests.
                WriteScopeAttribute(scope);
                writer.WriteEndElement();                         // </alias>
                break;

            case ImportTargetKind.Namespace:
                if (alias != null)
                {
                    writer.WriteStartElement("alias");
                    writer.WriteAttributeString("name", alias);
                    if (externAlias != null)
                    {
                        writer.WriteAttributeString("qualifier", externAlias);
                    }
                    writer.WriteAttributeString("target", target);
                    writer.WriteAttributeString("kind", "namespace");
                    Debug.Assert(scope == ImportScope.Unspecified); // Only C# hits this case.
                    writer.WriteEndElement();                       // </alias>
                }
                else
                {
                    writer.WriteStartElement("namespace");
                    if (externAlias != null)
                    {
                        writer.WriteAttributeString("qualifier", externAlias);
                    }
                    writer.WriteAttributeString("name", target);
                    WriteScopeAttribute(scope);
                    writer.WriteEndElement();     // </namespace>
                }
                break;

            case ImportTargetKind.Type:
                Debug.Assert(externAlias == null);
                if (alias != null)
                {
                    writer.WriteStartElement("alias");
                    writer.WriteAttributeString("name", alias);
                    writer.WriteAttributeString("target", target);
                    writer.WriteAttributeString("kind", "type");
                    Debug.Assert(scope == ImportScope.Unspecified); // Only C# hits this case.
                    writer.WriteEndElement();                       // </alias>
                }
                else
                {
                    writer.WriteStartElement("type");
                    writer.WriteAttributeString("name", target);
                    WriteScopeAttribute(scope);
                    writer.WriteEndElement();     // </type>
                }
                break;

            case ImportTargetKind.Assembly:
                Debug.Assert(alias == null);
                Debug.Assert(scope == ImportScope.Unspecified);
                if (target == null)
                {
                    writer.WriteStartElement("extern");
                    writer.WriteAttributeString("alias", externAlias);
                    writer.WriteEndElement();     // </extern>
                }
                else
                {
                    writer.WriteStartElement("externinfo");
                    writer.WriteAttributeString("alias", externAlias);
                    writer.WriteAttributeString("assembly", target);
                    writer.WriteEndElement();     // </externinfo>
                }
                break;

            case ImportTargetKind.Defunct:
                Debug.Assert(alias == null);
                Debug.Assert(scope == ImportScope.Unspecified);
                writer.WriteStartElement("defunct");
                writer.WriteAttributeString("name", rawName);
                writer.WriteEndElement();     // </defunct>
                break;

            default:
                Debug.Assert(false, "Unexpected import kind '" + kind + "'");
                writer.WriteStartElement("unknown");
                writer.WriteAttributeString("name", rawName);
                writer.WriteEndElement();     // </unknown>
                break;
            }
        }
Ejemplo n.º 3
0
        private void WriteNamespace(ISymUnmanagedNamespace @namespace)
        {
            string rawName = @namespace.GetName();

            string alias;
            string externAlias;
            string target;
            ImportTargetKind kind;
            ImportScope scope;

            try
            {
                if (rawName.Length == 0)
                {
                    externAlias = null;
                    var parsingSucceeded = CDI.TryParseVisualBasicImportString(rawName, out alias, out target, out kind, out scope);
                    Debug.Assert(parsingSucceeded);
                }
                else
                {
                    switch (rawName[0])
                    {
                        case 'U':
                        case 'A':
                        case 'X':
                        case 'Z':
                        case 'E':
                            scope = ImportScope.Unspecified;
                            if (!CDI.TryParseCSharpImportString(rawName, out alias, out externAlias, out target, out kind))
                            {
                                throw new InvalidOperationException(string.Format("Invalid import '{0}'", rawName));
                            }
                            break;

                        default:
                            externAlias = null;
                            if (!CDI.TryParseVisualBasicImportString(rawName, out alias, out target, out kind, out scope))
                            {
                                throw new InvalidOperationException(string.Format("Invalid import '{0}'", rawName));
                            }
                            break;
                    }
                }
            }
            catch (ArgumentException) // TODO: filter
            {
                if ((options & PdbToXmlOptions.ThrowOnError) != 0)
                {
                    throw;
                }

                writer.WriteStartElement("invalid-custom-data");
                writer.WriteAttributeString("raw", rawName);
                writer.WriteEndElement();
                return;
            }

            switch (kind)
            {
                case ImportTargetKind.CurrentNamespace:
                    Debug.Assert(alias == null);
                    Debug.Assert(externAlias == null);
                    Debug.Assert(scope == ImportScope.Unspecified);
                    writer.WriteStartElement("currentnamespace");
                    writer.WriteAttributeString("name", target);
                    writer.WriteEndElement(); // </currentnamespace>
                    break;
                case ImportTargetKind.DefaultNamespace:
                    Debug.Assert(alias == null);
                    Debug.Assert(externAlias == null);
                    Debug.Assert(scope == ImportScope.Unspecified);
                    writer.WriteStartElement("defaultnamespace");
                    writer.WriteAttributeString("name", target);
                    writer.WriteEndElement(); // </defaultnamespace>
                    break;
                case ImportTargetKind.MethodToken:
                    Debug.Assert(alias == null);
                    Debug.Assert(externAlias == null);
                    Debug.Assert(scope == ImportScope.Unspecified);
                    int token = Convert.ToInt32(target);
                    writer.WriteStartElement("importsforward");
                    WriteMethodAttributes(token, isReference: true);
                    writer.WriteEndElement(); // </importsforward>
                    break;
                case ImportTargetKind.XmlNamespace:
                    Debug.Assert(externAlias == null);
                    writer.WriteStartElement("xmlnamespace");
                    writer.WriteAttributeString("prefix", alias);
                    writer.WriteAttributeString("name", target);
                    WriteScopeAttribute(scope);
                    writer.WriteEndElement(); // </xmlnamespace>
                    break;
                case ImportTargetKind.NamespaceOrType:
                    Debug.Assert(externAlias == null);
                    writer.WriteStartElement("alias");
                    writer.WriteAttributeString("name", alias);
                    writer.WriteAttributeString("target", target);
                    writer.WriteAttributeString("kind", "namespace"); // Strange, but retaining to avoid breaking tests.
                    WriteScopeAttribute(scope);
                    writer.WriteEndElement(); // </alias>
                    break;
                case ImportTargetKind.Namespace:
                    if (alias != null)
                    {
                        writer.WriteStartElement("alias");
                        writer.WriteAttributeString("name", alias);
                        if (externAlias != null) writer.WriteAttributeString("qualifier", externAlias);
                        writer.WriteAttributeString("target", target);
                        writer.WriteAttributeString("kind", "namespace");
                        Debug.Assert(scope == ImportScope.Unspecified); // Only C# hits this case.
                        writer.WriteEndElement(); // </alias>
                    }
                    else
                    {
                        writer.WriteStartElement("namespace");
                        if (externAlias != null) writer.WriteAttributeString("qualifier", externAlias);
                        writer.WriteAttributeString("name", target);
                        WriteScopeAttribute(scope);
                        writer.WriteEndElement(); // </namespace>
                    }
                    break;
                case ImportTargetKind.Type:
                    Debug.Assert(externAlias == null);
                    if (alias != null)
                    {
                        writer.WriteStartElement("alias");
                        writer.WriteAttributeString("name", alias);
                        writer.WriteAttributeString("target", target);
                        writer.WriteAttributeString("kind", "type");
                        Debug.Assert(scope == ImportScope.Unspecified); // Only C# hits this case.
                        writer.WriteEndElement(); // </alias>
                    }
                    else
                    {
                        writer.WriteStartElement("type");
                        writer.WriteAttributeString("name", target);
                        WriteScopeAttribute(scope);
                        writer.WriteEndElement(); // </type>
                    }
                    break;
                case ImportTargetKind.Assembly:
                    Debug.Assert(alias == null);
                    Debug.Assert(scope == ImportScope.Unspecified);
                    if (target == null)
                    {
                        writer.WriteStartElement("extern");
                        writer.WriteAttributeString("alias", externAlias);
                        writer.WriteEndElement(); // </extern>
                    }
                    else
                    {
                        writer.WriteStartElement("externinfo");
                        writer.WriteAttributeString("alias", externAlias);
                        writer.WriteAttributeString("assembly", target);
                        writer.WriteEndElement(); // </externinfo>
                    }
                    break;
                default:
                    Debug.Assert(false, "Unexpected import kind '" + kind + "'");
                    writer.WriteStartElement("unknown");
                    writer.WriteAttributeString("name", rawName);
                    writer.WriteEndElement(); // </unknown>
                    break;
            }
        }