public static int ModuleNameHashCode(ModuleDesc module)
        {
            IAssemblyDesc assembly = module.Assembly;

            Debug.Assert(assembly == module);
            return(NameHashCode(assembly.GetName().Name));
        }
示例#2
0
        public AssemblyReferenceHandle GetAssemblyRef(IAssemblyDesc assemblyDesc)
        {
            if (_assemblyRefs.TryGetValue(assemblyDesc, out var handle))
            {
                return(handle);
            }
            AssemblyName name            = assemblyDesc.GetName();
            StringHandle assemblyName    = _metadataBuilder.GetOrAddString(name.Name);
            StringHandle cultureName     = (name.CultureName != null) ? _metadataBuilder.GetOrAddString(name.CultureName) : default(StringHandle);
            BlobHandle   publicTokenBlob = name.GetPublicKeyToken() != null?_metadataBuilder.GetOrAddBlob(name.GetPublicKeyToken()) : default(BlobHandle);

            AssemblyFlags flags = default(AssemblyFlags);

            if (name.Flags.HasFlag(AssemblyNameFlags.Retargetable))
            {
                flags |= AssemblyFlags.Retargetable;
            }
            if (name.ContentType == AssemblyContentType.WindowsRuntime)
            {
                flags |= AssemblyFlags.WindowsRuntime;
            }

            var referenceHandle = _metadataBuilder.AddAssemblyReference(assemblyName, name.Version, cultureName, publicTokenBlob, flags, default(BlobHandle));

            _assemblyRefs.Add(assemblyDesc, referenceHandle);
            return(referenceHandle);
        }
示例#3
0
        private void AppendAssemblyName(StringBuilder sb, IAssemblyDesc assembly)
        {
            if (assembly == null)
            {
                return;
            }

            sb.Append(',');
            AppendEscapedIdentifier(sb, assembly.GetName().Name);
        }
        public AssemblyReferenceHandle GetAssemblyRef(IAssemblyDesc assemblyDesc)
        {
            if (_assemblyRefs.TryGetValue(assemblyDesc, out var handle))
            {
                return(handle);
            }
            AssemblyName name            = assemblyDesc.GetName();
            var          referenceHandle = GetAssemblyRef(name);

            _assemblyRefs.Add(assemblyDesc, referenceHandle);
            return(referenceHandle);
        }
示例#5
0
            public static string Module(ModuleDesc module)
            {
                if (module == null)
                {
                    return("?");
                }

                IAssemblyDesc assembly = module as IAssemblyDesc;

                if (assembly != null)
                {
                    return(assembly.GetName().FullName);
                }
                else
                {
                    Debug.Assert(false, "Multi-module assemblies");
                    return(module.ToString());
                }
            }
示例#6
0
        private AssemblyReferenceHandle MakeAssemblyReferenceHandle(IAssemblyDesc assemblyRef)
        {
            AssemblyReferenceHandle handle;

            if (!_assemblyRefHandles.TryGetValue(assemblyRef, out handle))
            {
                AssemblyName assemblyName = assemblyRef.GetName();

                handle = _metadataBuilder.AddAssemblyReference(
                    _metadataBuilder.GetOrAddString(assemblyName.Name),
                    assemblyName.Version,
                    default(StringHandle),
                    _metadataBuilder.GetOrAddBlob(ImmutableArray.Create <byte>(assemblyName.GetPublicKeyToken())),
                    default(AssemblyFlags),
                    default(BlobHandle));

                _assemblyRefHandles[assemblyRef] = handle;
            }

            return(handle);
        }
示例#7
0
        internal override ModuleDesc ResolveModule(IAssemblyDesc referencingModule, string fileName, bool throwIfNotFound = true)
        {
            // The referencing module is not getting verified currently.
            // However, netmodules are resolved in the context of assembly, not in the global context.
            EcmaModule module = CacheResolvedAssemblyOrNetmodule(_resolver.ResolveModule(referencingModule.GetName(), fileName), fileName, referencingModule, throwIfNotFound);

            if (module.MetadataReader.IsAssembly)
            {
                throw new VerifierException($"The module '{fileName}' is not expected to be an assembly");
            }
            return(module);
        }
        public override MethodIL EmitIL()
        {
            ILEmitter    emitter    = new ILEmitter();
            ILCodeStream codeStream = emitter.NewCodeStream();

            // Allow the class library to run explicitly ordered class constructors first thing in start-up.
            if (_libraryInitializers != null)
            {
                foreach (MethodDesc method in _libraryInitializers)
                {
                    codeStream.Emit(ILOpcode.call, emitter.NewToken(method));
                }
            }

            MetadataType startup = Context.GetHelperType("StartupCodeHelpers");

            // Initialize command line args if the class library supports this
            string initArgsName = (Context.Target.OperatingSystem == TargetOS.Windows)
                                ? "InitializeCommandLineArgsW"
                                : "InitializeCommandLineArgs";
            MethodDesc initArgs = startup.GetMethod(initArgsName, null);

            if (initArgs != null)
            {
                codeStream.Emit(ILOpcode.ldarg_0); // argc
                codeStream.Emit(ILOpcode.ldarg_1); // argv
                codeStream.Emit(ILOpcode.call, emitter.NewToken(initArgs));
            }

            // Initialize the entrypoint assembly if the class library supports this
            MethodDesc initEntryAssembly = startup.GetMethod("InitializeEntryAssembly", null);

            if (initEntryAssembly != null)
            {
                IAssemblyDesc entrypointAssembly = ((MetadataType)_mainMethod.WrappedMethod.OwningType).Module as IAssemblyDesc;
                Debug.Assert(entrypointAssembly != null, "Multi-module assembly?");
                codeStream.Emit(ILOpcode.ldstr, emitter.NewToken(entrypointAssembly.GetName().FullName));
                codeStream.Emit(ILOpcode.call, emitter.NewToken(initEntryAssembly));
            }

            // Call program Main
            if (_mainMethod.Signature.Length > 0)
            {
                // TODO: better exception
                if (initArgs == null)
                {
                    throw new Exception("Main() has parameters, but the class library doesn't support them");
                }

                codeStream.Emit(ILOpcode.call, emitter.NewToken(startup.GetKnownMethod("GetMainMethodArguments", null)));
            }
            codeStream.Emit(ILOpcode.call, emitter.NewToken(_mainMethod));

            MethodDesc setLatchedExitCode = startup.GetMethod("SetLatchedExitCode", null);
            MethodDesc shutdown           = startup.GetMethod("Shutdown", null);

            // The class library either supports "advanced shutdown", or doesn't. No half-implementations allowed.
            Debug.Assert((setLatchedExitCode != null) == (shutdown != null));

            if (setLatchedExitCode != null)
            {
                // If the main method has a return value, save it
                if (!_mainMethod.Signature.ReturnType.IsVoid)
                {
                    codeStream.Emit(ILOpcode.call, emitter.NewToken(setLatchedExitCode));
                }

                // Ask the class library to shut down and return exit code.
                codeStream.Emit(ILOpcode.call, emitter.NewToken(shutdown));
            }
            else
            {
                // This is a class library that doesn't have SetLatchedExitCode/Shutdown.
                // If the main method returns void, we simply use 0 exit code.
                if (_mainMethod.Signature.ReturnType.IsVoid)
                {
                    codeStream.EmitLdc(0);
                }
            }

            codeStream.Emit(ILOpcode.ret);

            return(emitter.Link(this));
        }