Пример #1
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;
                }
            }
        }
Пример #2
0
        /// <exception cref="BadImageFormatException">Invalid data format.</exception>
        private static void PopulateImports(
            MetadataReader reader,
            ImportScopeHandle handle,
            EESymbolProvider <TTypeSymbol, TLocalSymbol> symbolProvider,
            bool isVisualBasicMethod,
            ArrayBuilder <ImmutableArray <ImportRecord> > importGroupsBuilder,
            ArrayBuilder <ExternAliasRecord> externAliasesBuilder)
        {
            var importGroupBuilder = ArrayBuilder <ImportRecord> .GetInstance();

            while (!handle.IsNil)
            {
                var importScope = reader.GetImportScope(handle);

                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;
                    }
                }

                // VB always expects two import groups (even if they are empty).
                // TODO: consider doing this for C# as well and handle empty groups in the binder.
                if (isVisualBasicMethod || importGroupBuilder.Count > 0)
                {
                    importGroupsBuilder.Add(importGroupBuilder.ToImmutable());
                    importGroupBuilder.Clear();
                }

                handle = importScope.Parent;
            }

            importGroupBuilder.Free();
        }