Exemplo n.º 1
0
        //------------------------------------------------------------
        // ERRLOC Constructor (5)
        //
        /// <summary></summary>
        /// <param name="scope"></param>
        //------------------------------------------------------------
        internal ERRLOC(ImportScope scope, bool fullPath)
        {
            DebugUtil.Assert(scope != null);

            sourceData        = null;
            sourceFileName    = scope.GetFileName(fullPath);
            sourceMapFileName = sourceFileName;
        }
Exemplo n.º 2
0
 public ImportScopeEntry(PEFile module, MetadataReader metadata, bool isEmbedded, ImportScopeHandle handle)
 {
     this.offset = isEmbedded ? null : (int?)metadata.GetTableMetadataOffset(TableIndex.ImportScope)
                   + metadata.GetTableRowSize(TableIndex.ImportScope) * (MetadataTokens.GetRowNumber(handle) - 1);
     this.module     = module;
     this.metadata   = metadata;
     this.handle     = handle;
     this.localScope = metadata.GetImportScope(handle);
 }
Exemplo n.º 3
0
 private void WriteScopeAttribute(ImportScope scope)
 {
     if (scope == ImportScope.File)
     {
         writer.WriteAttributeString("importlevel", "file");
     }
     else if (scope == ImportScope.Project)
     {
         writer.WriteAttributeString("importlevel", "project");
     }
     else
     {
         Debug.Assert(scope == ImportScope.Unspecified, "Unexpected scope '" + scope + "'");
     }
 }
        public static bool TryCreateFromVisualBasicImportString(string importString, out ImportRecord record, out ImportScope scope)
        {
            ImportTargetKind targetKind;
            string alias;
            string targetString;
            if (CustomDebugInfoReader.TryParseVisualBasicImportString(importString, out alias, out targetString, out targetKind, out scope))
            {
                record = new NativeImportRecord(
                    targetKind,
                    externAlias: null,
                    alias: alias,
                    targetString: targetString);
                return true;
            }

            record = default(ImportRecord);
            return false;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Parse a string representing a VB import statement.
        /// </summary>
        /// <exception cref="ArgumentNullException"><paramref name="import"/> is null.</exception>
        /// <exception cref="ArgumentException">Format of <paramref name="import"/> is not valid.</exception>
        public static bool TryParseVisualBasicImportString(string import, out string alias, out string target, out ImportTargetKind kind, out ImportScope scope)
        {
            alias  = null;
            target = null;
            kind   = default(ImportTargetKind);
            scope  = default(ImportScope);

            if (import == null)
            {
                return(false);
            }

            // VB current namespace
            if (import.Length == 0)
            {
                alias  = null;
                target = import;
                kind   = ImportTargetKind.CurrentNamespace;
                scope  = ImportScope.Unspecified;
                return(true);
            }

            int pos = 0;

            switch (import[pos])
            {
            case '&':
            // Indicates the presence of embedded PIA types from a given assembly.  No longer required (as of Roslyn).
            case '$':
            case '#':
                // From ProcedureContext::LoadImportsAndDefaultNamespaceNormal:
                //   "Module Imports and extension types are no longer needed since we are not doing custom name lookup"
                alias  = null;
                target = import;
                kind   = ImportTargetKind.Defunct;
                scope  = ImportScope.Unspecified;
                return(true);

            case '*':     // VB default namespace
                // see PEBuilder.cpp in vb\language\CodeGen
                pos++;
                alias  = null;
                target = import.Substring(pos);
                kind   = ImportTargetKind.DefaultNamespace;
                scope  = ImportScope.Unspecified;
                return(true);

            case '@':     // VB cases other than default and current namespace
                // see PEBuilder.cpp in vb\language\CodeGen
                pos++;
                if (pos >= import.Length)
                {
                    return(false);
                }

                scope = ImportScope.Unspecified;
                switch (import[pos])
                {
                case 'F':
                    scope = ImportScope.File;
                    pos++;
                    break;

                case 'P':
                    scope = ImportScope.Project;
                    pos++;
                    break;
                }

                if (pos >= import.Length)
                {
                    return(false);
                }

                switch (import[pos])
                {
                case 'A':
                    pos++;

                    if (import[pos] != ':')
                    {
                        return(false);
                    }

                    pos++;

                    if (!TrySplit(import, pos, '=', out alias, out target))
                    {
                        return(false);
                    }

                    kind = ImportTargetKind.NamespaceOrType;
                    return(true);

                case 'X':
                    pos++;

                    if (import[pos] != ':')
                    {
                        return(false);
                    }

                    pos++;

                    if (!TrySplit(import, pos, '=', out alias, out target))
                    {
                        return(false);
                    }

                    kind = ImportTargetKind.XmlNamespace;
                    return(true);

                case 'T':
                    pos++;

                    if (import[pos] != ':')
                    {
                        return(false);
                    }

                    pos++;

                    alias  = null;
                    target = import.Substring(pos);
                    kind   = ImportTargetKind.Type;
                    return(true);

                case ':':
                    pos++;
                    alias  = null;
                    target = import.Substring(pos);
                    kind   = ImportTargetKind.Namespace;
                    return(true);

                default:
                    alias  = null;
                    target = import.Substring(pos);
                    kind   = ImportTargetKind.MethodToken;
                    return(true);
                }

            default:
                // VB current namespace
                alias  = null;
                target = import;
                kind   = ImportTargetKind.CurrentNamespace;
                scope  = ImportScope.Unspecified;
                return(true);
            }
        }
Exemplo n.º 6
0
        /// <exception cref="BadImageFormatException">Invalid data format.</exception>
        private static void PopulateImports(
            MetadataReader reader,
            ImportScope importScope,
            EESymbolProvider <TTypeSymbol, TLocalSymbol> symbolProvider,
            ArrayBuilder <ImportRecord> importGroupBuilder,
            ArrayBuilder <ExternAliasRecord> externAliasesBuilder
            )
        {
            foreach (ImportDefinition import in importScope.GetImports())
            {
                switch (import.Kind)
                {
                case ImportDefinitionKind.ImportNamespace:
                    importGroupBuilder.Add(
                        new ImportRecord(
                            ImportTargetKind.Namespace,
                            targetString: ReadUtf8String(reader, import.TargetNamespace)
                            )
                        );
                    break;

                case ImportDefinitionKind.ImportAssemblyNamespace:
                    importGroupBuilder.Add(
                        new ImportRecord(
                            ImportTargetKind.Namespace,
                            targetString: ReadUtf8String(reader, import.TargetNamespace),
                            targetAssembly: symbolProvider.GetReferencedAssembly(
                                import.TargetAssembly
                                )
                            )
                        );
                    break;

                case ImportDefinitionKind.ImportType:
                    importGroupBuilder.Add(
                        new ImportRecord(
                            ImportTargetKind.Type,
                            targetType: symbolProvider.GetType(import.TargetType)
                            )
                        );
                    break;

                case ImportDefinitionKind.ImportXmlNamespace:
                    importGroupBuilder.Add(
                        new ImportRecord(
                            ImportTargetKind.XmlNamespace,
                            alias: ReadUtf8String(reader, import.Alias),
                            targetString: ReadUtf8String(reader, import.TargetNamespace)
                            )
                        );
                    break;

                case ImportDefinitionKind.ImportAssemblyReferenceAlias:
                    importGroupBuilder.Add(
                        new ImportRecord(
                            ImportTargetKind.Assembly,
                            alias: ReadUtf8String(reader, import.Alias)
                            )
                        );
                    break;

                case ImportDefinitionKind.AliasAssemblyReference:
                    externAliasesBuilder.Add(
                        new ExternAliasRecord(
                            alias: ReadUtf8String(reader, import.Alias),
                            targetAssembly: symbolProvider.GetReferencedAssembly(
                                import.TargetAssembly
                                )
                            )
                        );
                    break;

                case ImportDefinitionKind.AliasNamespace:
                    importGroupBuilder.Add(
                        new ImportRecord(
                            ImportTargetKind.Namespace,
                            alias: ReadUtf8String(reader, import.Alias),
                            targetString: ReadUtf8String(reader, import.TargetNamespace)
                            )
                        );
                    break;

                case ImportDefinitionKind.AliasAssemblyNamespace:
                    importGroupBuilder.Add(
                        new ImportRecord(
                            ImportTargetKind.Namespace,
                            alias: ReadUtf8String(reader, import.Alias),
                            targetString: ReadUtf8String(reader, import.TargetNamespace),
                            targetAssembly: symbolProvider.GetReferencedAssembly(
                                import.TargetAssembly
                                )
                            )
                        );
                    break;

                case ImportDefinitionKind.AliasType:
                    importGroupBuilder.Add(
                        new ImportRecord(
                            ImportTargetKind.Type,
                            alias: ReadUtf8String(reader, import.Alias),
                            targetType: symbolProvider.GetType(import.TargetType)
                            )
                        );
                    break;
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Parse a string representing a VB import statement.
        /// </summary>
        /// <exception cref="ArgumentNullException"><paramref name="import"/> is null.</exception>
        /// <exception cref="ArgumentException">Format of <paramref name="import"/> is not valid.</exception>
        public static void ParseVisualBasicImportString(string import, out string alias, out string target, out ImportTargetKind kind, out ImportScope scope)
        {
            if (import == null)
            {
                throw new ArgumentNullException("import");
            }

            if (import.Length == 0) // VB current namespace
            {
                alias = null;
                target = import;
                kind = ImportTargetKind.CurrentNamespace;
                scope = ImportScope.Unspecified;
                return;
            }

            int pos = 0;
            switch (import[pos])
            {
                case '*': // VB default namespace
                    // see PEBuilder.cpp in vb\language\CodeGen
                    pos++;
                    alias = null;
                    target = import.Substring(pos);
                    kind = ImportTargetKind.DefaultNamespace;
                    scope = ImportScope.Unspecified;
                    return;
                case '@': // VB cases other than default and current namespace
                    // see PEBuilder.cpp in vb\language\CodeGen
                    pos++;
                    if (pos >= import.Length)
                    {
                        throw new ArgumentException(import, "import");
                    }

                    scope = ImportScope.Unspecified;
                    switch (import[pos])
                    {
                        case 'F':
                            scope = ImportScope.File;
                            pos++;
                            break;
                        case 'P':
                            scope = ImportScope.Project;
                            pos++;
                            break;
                    }

                    if (pos >= import.Length)
                    {
                        throw new ArgumentException(import, "import");
                    }

                    switch (import[pos])
                    {
                        case 'A':
                            pos++;
                            if (import[pos] != ':')
                            {
                                throw new ArgumentException(import, "import");
                            }
                            pos++;

                            if (!TrySplit(import, pos, '=', out alias, out target))
                            {
                                throw new ArgumentException(import, "import");
                            }

                            kind = ImportTargetKind.NamespaceOrType;
                            return;
                        case 'X':
                            pos++;
                            if (import[pos] != ':')
                            {
                                throw new ArgumentException(import, "import");
                            }
                            pos++;

                            if (!TrySplit(import, pos, '=', out alias, out target))
                            {
                                throw new ArgumentException(import, "import");
                            }

                            kind = ImportTargetKind.XmlNamespace;
                            return;
                        case 'T':
                            pos++;
                            if (import[pos] != ':')
                            {
                                throw new ArgumentException(import, "import");
                            }
                            pos++;

                            alias = null;
                            target = import.Substring(pos);
                            kind = ImportTargetKind.Type;
                            return;
                        case ':':
                            pos++;
                            alias = null;
                            target = import.Substring(pos);
                            kind = ImportTargetKind.Namespace;
                            return;
                        default:
                            alias = null;
                            target = import.Substring(pos);
                            kind = ImportTargetKind.MethodToken;
                            return;
                    }
                default: // VB current namespace
                    alias = null;
                    target = import;
                    kind = ImportTargetKind.CurrentNamespace;
                    scope = ImportScope.Unspecified;
                    return;
            }
        }
Exemplo n.º 8
0
        private static bool TryCreateImportRecordFromVisualBasicImportString(string importString, out ImportRecord record, out ImportScope scope)
        {
            ImportTargetKind targetKind;
            string           alias;
            string           targetString;

            if (CustomDebugInfoReader.TryParseVisualBasicImportString(importString, out alias, out targetString, out targetKind, out scope))
            {
                record = new ImportRecord(
                    targetKind: targetKind,
                    alias: alias,
                    targetType: null,
                    targetString: targetString,
                    targetAssembly: null,
                    targetAssemblyAlias: null);

                return(true);
            }

            record = default(ImportRecord);
            return(false);
        }
Exemplo n.º 9
0
        private static void ReadVisualBasicImportsDebugInfo(
            ISymUnmanagedReader reader,
            int methodToken,
            int methodVersion,
            out ImmutableArray <ImmutableArray <ImportRecord> > importRecordGroups,
            out string defaultNamespaceName)
        {
            importRecordGroups = ImmutableArray <ImmutableArray <ImportRecord> > .Empty;

            var importStrings = reader.GetVisualBasicImportStrings(methodToken, methodVersion);

            if (importStrings.IsDefault)
            {
                defaultNamespaceName = "";
                return;
            }

            defaultNamespaceName = null;
            var projectLevelImportRecords = ArrayBuilder <ImportRecord> .GetInstance();

            var fileLevelImportRecords = ArrayBuilder <ImportRecord> .GetInstance();

            foreach (string importString in importStrings)
            {
                Debug.Assert(importString != null);

                if (importString.Length > 0 && importString[0] == '*')
                {
                    string           alias  = null;
                    string           target = null;
                    ImportTargetKind kind   = 0;
                    ImportScope      scope  = 0;

                    if (!CustomDebugInfoReader.TryParseVisualBasicImportString(importString, out alias, out target, out kind, out scope))
                    {
                        Debug.WriteLine($"Unable to parse import string '{importString}'");
                        continue;
                    }
                    else if (kind == ImportTargetKind.Defunct)
                    {
                        continue;
                    }

                    Debug.Assert(alias == null); // The default namespace is never aliased.
                    Debug.Assert(target != null);
                    Debug.Assert(kind == ImportTargetKind.DefaultNamespace);

                    // We only expect to see one of these, but it looks like ProcedureContext::LoadImportsAndDefaultNamespaceNormal
                    // implicitly uses the last one if there are multiple.
                    Debug.Assert(defaultNamespaceName == null);

                    defaultNamespaceName = target;
                }
                else
                {
                    ImportRecord importRecord = null;
                    ImportScope  scope        = 0;

                    if (TryCreateImportRecordFromVisualBasicImportString(importString, out importRecord, out scope))
                    {
                        if (scope == ImportScope.Project)
                        {
                            projectLevelImportRecords.Add(importRecord);
                        }
                        else
                        {
                            Debug.Assert(scope == ImportScope.File || scope == ImportScope.Unspecified);
                            fileLevelImportRecords.Add(importRecord);
                        }
                    }
                    else
                    {
                        Debug.WriteLine($"Failed to parse import string {importString}");
                    }
                }
            }

            importRecordGroups = ImmutableArray.Create(
                fileLevelImportRecords.ToImmutableAndFree(),
                projectLevelImportRecords.ToImmutableAndFree());

            defaultNamespaceName = defaultNamespaceName ?? "";
        }
        /// <summary>
        /// Parse a string representing a VB import statement.
        /// </summary>
        /// <exception cref="ArgumentNullException"><paramref name="import"/> is null.</exception>
        /// <exception cref="ArgumentException">Format of <paramref name="import"/> is not valid.</exception>
        public static bool TryParseVisualBasicImportString(string import, out string alias, out string target, out ImportTargetKind kind, out ImportScope scope)
        {
            alias = null;
            target = null;
            kind = default(ImportTargetKind);
            scope = default(ImportScope);

            if (import == null)
            {
                return false;
            }

            // VB current namespace
            if (import.Length == 0)
            {
                alias = null;
                target = import;
                kind = ImportTargetKind.CurrentNamespace;
                scope = ImportScope.Unspecified;
                return true;
            }

            int pos = 0;
            switch (import[pos])
            {
                case '&':
                // Indicates the presence of embedded PIA types from a given assembly.  No longer required (as of Roslyn).
                case '$':
                case '#':
                    // From ProcedureContext::LoadImportsAndDefaultNamespaceNormal:
                    //   "Module Imports and extension types are no longer needed since we are not doing custom name lookup"
                    alias = null;
                    target = import;
                    kind = ImportTargetKind.Defunct;
                    scope = ImportScope.Unspecified;
                    return true;
                case '*': // VB default namespace
                    // see PEBuilder.cpp in vb\language\CodeGen
                    pos++;
                    alias = null;
                    target = import.Substring(pos);
                    kind = ImportTargetKind.DefaultNamespace;
                    scope = ImportScope.Unspecified;
                    return true;
                case '@': // VB cases other than default and current namespace
                    // see PEBuilder.cpp in vb\language\CodeGen
                    pos++;
                    if (pos >= import.Length)
                    {
                        return false;
                    }

                    scope = ImportScope.Unspecified;
                    switch (import[pos])
                    {
                        case 'F':
                            scope = ImportScope.File;
                            pos++;
                            break;
                        case 'P':
                            scope = ImportScope.Project;
                            pos++;
                            break;
                    }

                    if (pos >= import.Length)
                    {
                        return false;
                    }

                    switch (import[pos])
                    {
                        case 'A':
                            pos++;

                            if (import[pos] != ':')
                            {
                                return false;
                            }

                            pos++;

                            if (!TrySplit(import, pos, '=', out alias, out target))
                            {
                                return false;
                            }

                            kind = ImportTargetKind.NamespaceOrType;
                            return true;

                        case 'X':
                            pos++;

                            if (import[pos] != ':')
                            {
                                return false;
                            }

                            pos++;

                            if (!TrySplit(import, pos, '=', out alias, out target))
                            {
                                return false;
                            }

                            kind = ImportTargetKind.XmlNamespace;
                            return true;

                        case 'T':
                            pos++;

                            if (import[pos] != ':')
                            {
                                return false;
                            }

                            pos++;

                            alias = null;
                            target = import.Substring(pos);
                            kind = ImportTargetKind.Type;
                            return true;

                        case ':':
                            pos++;
                            alias = null;
                            target = import.Substring(pos);
                            kind = ImportTargetKind.Namespace;
                            return true;

                        default:
                            alias = null;
                            target = import.Substring(pos);
                            kind = ImportTargetKind.MethodToken;
                            return true;
                    }

                default:
                    // VB current namespace
                    alias = null;
                    target = import;
                    kind = ImportTargetKind.CurrentNamespace;
                    scope = ImportScope.Unspecified;
                    return true;
            }
        }
        public static bool TryCreateFromVisualBasicImportString(string importString, out ImportRecord record, out ImportScope scope)
        {
            ImportTargetKind targetKind;
            string           alias;
            string           targetString;

            if (CustomDebugInfoReader.TryParseVisualBasicImportString(importString, out alias, out targetString, out targetKind, out scope))
            {
                record = new NativeImportRecord(
                    targetKind,
                    externAlias: null,
                    alias: alias,
                    targetString: targetString);
                return(true);
            }

            record = default(ImportRecord);
            return(false);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Parse a string representing a VB import statement.
        /// </summary>
        /// <exception cref="ArgumentNullException"><paramref name="import"/> is null.</exception>
        /// <exception cref="ArgumentException">Format of <paramref name="import"/> is not valid.</exception>
        public static void ParseVisualBasicImportString(string import, out string alias, out string target, out ImportTargetKind kind, out ImportScope scope)
        {
            if (import == null)
            {
                throw new ArgumentNullException("import");
            }

            if (import.Length == 0) // VB current namespace
            {
                alias  = null;
                target = import;
                kind   = ImportTargetKind.CurrentNamespace;
                scope  = ImportScope.Unspecified;
                return;
            }

            int pos = 0;

            switch (import[pos])
            {
            case '*':     // VB default namespace
                // see PEBuilder.cpp in vb\language\CodeGen
                pos++;
                alias  = null;
                target = import.Substring(pos);
                kind   = ImportTargetKind.DefaultNamespace;
                scope  = ImportScope.Unspecified;
                return;

            case '@':     // VB cases other than default and current namespace
                // see PEBuilder.cpp in vb\language\CodeGen
                pos++;
                if (pos >= import.Length)
                {
                    throw new ArgumentException(import, "import");
                }

                scope = ImportScope.Unspecified;
                switch (import[pos])
                {
                case 'F':
                    scope = ImportScope.File;
                    pos++;
                    break;

                case 'P':
                    scope = ImportScope.Project;
                    pos++;
                    break;
                }

                if (pos >= import.Length)
                {
                    throw new ArgumentException(import, "import");
                }

                switch (import[pos])
                {
                case 'A':
                    pos++;
                    if (import[pos] != ':')
                    {
                        throw new ArgumentException(import, "import");
                    }
                    pos++;

                    if (!TrySplit(import, pos, '=', out alias, out target))
                    {
                        throw new ArgumentException(import, "import");
                    }

                    kind = ImportTargetKind.NamespaceOrType;
                    return;

                case 'X':
                    pos++;
                    if (import[pos] != ':')
                    {
                        throw new ArgumentException(import, "import");
                    }
                    pos++;

                    if (!TrySplit(import, pos, '=', out alias, out target))
                    {
                        throw new ArgumentException(import, "import");
                    }

                    kind = ImportTargetKind.XmlNamespace;
                    return;

                case 'T':
                    pos++;
                    if (import[pos] != ':')
                    {
                        throw new ArgumentException(import, "import");
                    }
                    pos++;

                    alias  = null;
                    target = import.Substring(pos);
                    kind   = ImportTargetKind.Type;
                    return;

                case ':':
                    pos++;
                    alias  = null;
                    target = import.Substring(pos);
                    kind   = ImportTargetKind.Namespace;
                    return;

                default:
                    alias  = null;
                    target = import.Substring(pos);
                    kind   = ImportTargetKind.MethodToken;
                    return;
                }

            default:     // VB current namespace
                alias  = null;
                target = import;
                kind   = ImportTargetKind.CurrentNamespace;
                scope  = ImportScope.Unspecified;
                return;
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Parse a string representing a VB import statement.
        /// </summary>
        /// <exception cref="ArgumentNullException"><paramref name="import"/> is null.</exception>
        /// <exception cref="ArgumentException">Format of <paramref name="import"/> is not valid.</exception>
        public static bool TryParseVisualBasicImportString(string import, out string alias, out string target, out ImportTargetKind kind, out ImportScope scope)
        {
            alias = null;
            target = null;
            kind = default(ImportTargetKind);
            scope = default(ImportScope);

            if (import == null)
            {
                return false;
            }

            if (import.Length == 0) // VB current namespace
            {
                alias = null;
                target = import;
                kind = ImportTargetKind.CurrentNamespace;
                scope = ImportScope.Unspecified;
                return true;
            }

            // TODO (acasey): looks like we missed some cases (e.g. '$', '#', '&')
            // See ProcedureContext::LoadImportsAndDefaultNamespaceNormal.

            int pos = 0;
            switch (import[pos])
            {
                case '*': // VB default namespace
                    // see PEBuilder.cpp in vb\language\CodeGen
                    pos++;
                    alias = null;
                    target = import.Substring(pos);
                    kind = ImportTargetKind.DefaultNamespace;
                    scope = ImportScope.Unspecified;
                    return true;
                case '@': // VB cases other than default and current namespace
                    // see PEBuilder.cpp in vb\language\CodeGen
                    pos++;
                    if (pos >= import.Length)
                    {
                        return false;
                    }

                    scope = ImportScope.Unspecified;
                    switch (import[pos])
                    {
                        case 'F':
                            scope = ImportScope.File;
                            pos++;
                            break;
                        case 'P':
                            scope = ImportScope.Project;
                            pos++;
                            break;
                    }

                    if (pos >= import.Length)
                    {
                        return false;
                    }

                    switch (import[pos])
                    {
                        case 'A':
                            pos++;
                            if (import[pos] != ':')
                            {
                                return false;
                            }
                            pos++;

                            if (!TrySplit(import, pos, '=', out alias, out target))
                            {
                                return false;
                            }

                            kind = ImportTargetKind.NamespaceOrType;
                            return true;
                        case 'X':
                            pos++;
                            if (import[pos] != ':')
                            {
                                return false;
                            }
                            pos++;

                            if (!TrySplit(import, pos, '=', out alias, out target))
                            {
                                return false;
                            }

                            kind = ImportTargetKind.XmlNamespace;
                            return true;
                        case 'T':
                            pos++;
                            if (import[pos] != ':')
                            {
                                return false;
                            }
                            pos++;

                            alias = null;
                            target = import.Substring(pos);
                            kind = ImportTargetKind.Type;
                            return true;
                        case ':':
                            pos++;
                            alias = null;
                            target = import.Substring(pos);
                            kind = ImportTargetKind.Namespace;
                            return true;
                        default:
                            alias = null;
                            target = import.Substring(pos);
                            kind = ImportTargetKind.MethodToken;
                            return true;
                    }
                default: // VB current namespace
                    alias = null;
                    target = import;
                    kind = ImportTargetKind.CurrentNamespace;
                    scope = ImportScope.Unspecified;
                    return true;
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Parse a string representing a VB import statement.
        /// </summary>
        /// <exception cref="ArgumentNullException"><paramref name="import"/> is null.</exception>
        /// <exception cref="ArgumentException">Format of <paramref name="import"/> is not valid.</exception>
        public static bool TryParseVisualBasicImportString(string import, out string alias, out string target, out ImportTargetKind kind, out ImportScope scope)
        {
            alias  = null;
            target = null;
            kind   = default(ImportTargetKind);
            scope  = default(ImportScope);

            if (import == null)
            {
                return(false);
            }

            if (import.Length == 0) // VB current namespace
            {
                alias  = null;
                target = import;
                kind   = ImportTargetKind.CurrentNamespace;
                scope  = ImportScope.Unspecified;
                return(true);
            }

            // TODO (acasey): looks like we missed some cases (e.g. '$', '#', '&')
            // See ProcedureContext::LoadImportsAndDefaultNamespaceNormal.

            int pos = 0;

            switch (import[pos])
            {
            case '*':     // VB default namespace
                // see PEBuilder.cpp in vb\language\CodeGen
                pos++;
                alias  = null;
                target = import.Substring(pos);
                kind   = ImportTargetKind.DefaultNamespace;
                scope  = ImportScope.Unspecified;
                return(true);

            case '@':     // VB cases other than default and current namespace
                // see PEBuilder.cpp in vb\language\CodeGen
                pos++;
                if (pos >= import.Length)
                {
                    return(false);
                }

                scope = ImportScope.Unspecified;
                switch (import[pos])
                {
                case 'F':
                    scope = ImportScope.File;
                    pos++;
                    break;

                case 'P':
                    scope = ImportScope.Project;
                    pos++;
                    break;
                }

                if (pos >= import.Length)
                {
                    return(false);
                }

                switch (import[pos])
                {
                case 'A':
                    pos++;
                    if (import[pos] != ':')
                    {
                        return(false);
                    }
                    pos++;

                    if (!TrySplit(import, pos, '=', out alias, out target))
                    {
                        return(false);
                    }

                    kind = ImportTargetKind.NamespaceOrType;
                    return(true);

                case 'X':
                    pos++;
                    if (import[pos] != ':')
                    {
                        return(false);
                    }
                    pos++;

                    if (!TrySplit(import, pos, '=', out alias, out target))
                    {
                        return(false);
                    }

                    kind = ImportTargetKind.XmlNamespace;
                    return(true);

                case 'T':
                    pos++;
                    if (import[pos] != ':')
                    {
                        return(false);
                    }
                    pos++;

                    alias  = null;
                    target = import.Substring(pos);
                    kind   = ImportTargetKind.Type;
                    return(true);

                case ':':
                    pos++;
                    alias  = null;
                    target = import.Substring(pos);
                    kind   = ImportTargetKind.Namespace;
                    return(true);

                default:
                    alias  = null;
                    target = import.Substring(pos);
                    kind   = ImportTargetKind.MethodToken;
                    return(true);
                }

            default:     // VB current namespace
                alias  = null;
                target = import;
                kind   = ImportTargetKind.CurrentNamespace;
                scope  = ImportScope.Unspecified;
                return(true);
            }
        }