Example #1
0
 public SymbolBase(DiagnosticBag diagnostics)
 {
     _diagnostics    = diagnostics.CreateChildBag(this);
     _allDiagnostics = new Lazy <ImmutableArray <Diagnostic> >(() =>
     {
         _diagnostics.EnsureAllDiagnosticsCollectedForSymbol();
         return(_diagnostics.ToImmutableArray());
     });
 }
Example #2
0
        public MetadataAssembly(Assembly assembly, ImmutableArray <byte> assemblyBytes, ImmutableArray <IAssembly> dependencies)
        {
            _diagnostics   = new DiagnosticBag(this);
            _assembly      = assembly;
            _assemblyBytes = assemblyBytes;
            if (assembly.FullName is null)
            {
                _diagnostics.Add(new Diagnostic(
                                     new Location(),
                                     ErrorCode.InvalidMetadataAssembly,
                                     ImmutableArray.Create <object?>("Metadata Assembly has no name")));
            }

            var assemblyNameAttributes = assembly.GetAttributes <AssemblyNameAttribute>();

            if (assemblyNameAttributes.Length != 1)
            {
                _diagnostics.Add(new Diagnostic(
                                     new Location(),
                                     ErrorCode.InvalidMetadataAssembly,
                                     ImmutableArray.Create <object?>($"Metadata Assembly must have exactly one {nameof(AssemblyNameAttribute)}")));
                Name = new QualifiedName("");
            }
            else
            {
                Name = QualifiedName.Parse(assemblyNameAttributes[0].Name);
            }

            var assemblyFileVersionAttributes = assembly.GetAttributes <AssemblyFileVersionAttribute>();

            if (assemblyFileVersionAttributes.Length != 1)
            {
                _diagnostics.Add(new Diagnostic(
                                     new Location(),
                                     ErrorCode.InvalidMetadataAssembly,
                                     ImmutableArray.Create <object?>($"Metadata Assembly must have exactly one {nameof(AssemblyFileVersionAttribute)}")));
                Version = new Version(0, 0, 0);
            }
            else
            {
                var versionAttribute = assemblyFileVersionAttributes[0];
                if (!Version.TryParse(versionAttribute.Version, out var version))
                {
                    _diagnostics.Add(new Diagnostic(
                                         new Location(),
                                         ErrorCode.InvalidMetadataAssembly,
                                         ImmutableArray.Create <object?>($"Version `{version}` is invalid")));
                    Version = new Version(0, 0, 0);
                }
                else
                {
                    Version = version;
                }
            }

            _referencedAssemblies = new Lazy <ImmutableArray <IAssembly> >(
                () => ((IAssembly)this).CalculateReferencedAssemblies(dependencies, _diagnostics).ToImmutableArray());
            _referencedAssembliesAndSelf = new Lazy <ImmutableArray <IAssembly> >(
                () => ReferencedAssemblies.Add(this));

            _methodsByName    = new Lazy <IReadOnlyDictionary <QualifiedName, IMethod> >(GenerateMethods);
            _methods          = new Lazy <ImmutableArray <IMethod> >(() => _methodsByName.Value.Values.ToImmutableArray());
            _interfacesByName = new Lazy <IReadOnlyDictionary <QualifiedName, IInterface> >(GenerateInterfaces);
            _interfaces       = new Lazy <ImmutableArray <IInterface> >(() => _interfacesByName.Value.Values.ToImmutableArray());
            _allDiagnostics   = new Lazy <ImmutableArray <Diagnostic> >(() =>
            {
                _diagnostics.EnsureAllDiagnosticsCollectedForSymbol();
                return(_diagnostics.ToImmutableArray());
            });
        }