Пример #1
0
        public IAssemblyReference Map(R.IAssemblySymbol assembly)
        {
            Contract.Requires(assembly != null);
            Contract.Ensures(Contract.Result <IAssemblyReference>() != null);

            IAssemblyReference cciAssembly = null;

            if (!assemblySymbolCache.TryGetValue(assembly, out cciAssembly))
            {
                var an = assembly.Identity;
                IEnumerable <byte> pkt = an.PublicKeyToken.AsEnumerable();
                if (pkt == null)
                {
                    pkt = new byte[0];
                }
                var identity = new Microsoft.Cci.AssemblyIdentity(
                    this.nameTable.GetNameFor(an.Name),
                    an.CultureName == null ? "" : an.CultureName, // REVIEW: This can't be right
                    an.Version,
                    pkt,
                    an.Location == null ? "unknown://location" : an.Location
                    );
                cciAssembly = new Microsoft.Cci.Immutable.AssemblyReference(this.host, identity);
                assemblySymbolCache[assembly] = cciAssembly;
            }
            Contract.Assume(cciAssembly != null);
            return(cciAssembly);
        }
Пример #2
0
        /// <summary>
        /// Translates the metadata "backbone" of the Roslyn assembly, creating a CCI
        /// assembly that is held onto by the returned reference mapper. The mapper
        /// can be used during a traversal of any syntax tree whose semantic model
        /// corresponds to the <paramref name="assemblySymbol"/>. The mapper will
        /// return the equivalent CCI node that corresponds to the semantic node it
        /// is given.
        /// </summary>
        public static ReferenceMapper TranslateAssembly(IMetadataHost host, R.IAssemblySymbol assemblySymbol)
        {
            Contract.Requires(host != null);
            Contract.Requires(assemblySymbol != null);
            Contract.Ensures(Contract.Result <ReferenceMapper>() != null);

            var rm = new ReferenceMapper(host, assemblySymbol);

            rm.TranslateMetadata(assemblySymbol);
            return(rm);
        }
Пример #3
0
 private ReferenceMapper(IMetadataHost host, R.IAssemblySymbol assemblySymbol) {
   this.host = host;
   this.nameTable = host.NameTable;
   this.assemblyBeingTranslated = assemblySymbol;
 }
Пример #4
0
 private ReferenceMapper(IMetadataHost host, R.IAssemblySymbol assemblySymbol)
 {
     this.host      = host;
     this.nameTable = host.NameTable;
     this.assemblyBeingTranslated = assemblySymbol;
 }
Пример #5
0
        private Assembly TranslateMetadata(R.IAssemblySymbol assemblySymbol)
        {
            Contract.Requires(assemblySymbol != null);
            Contract.Ensures(Contract.Result <Assembly>() != null);

            IAssemblyReference cciAssemblyReference = null;
            Assembly           cciAssembly          = null;

            if (assemblySymbolCache.TryGetValue(assemblySymbol, out cciAssemblyReference))
            {
                cciAssembly = cciAssemblyReference as Assembly;
                System.Diagnostics.Debug.Assert(cciAssembly != null);
                return(cciAssembly);
            }

            var coreAssembly = host.LoadAssembly(host.CoreAssemblySymbolicIdentity);
            var name         = assemblySymbol.Identity.Name;
            var iname        = nameTable.GetNameFor(name);

            cciAssembly = new Assembly()
            {
                Attributes           = this.TranslateMetadata(assemblySymbol.GetAttributes()),
                Name                 = iname,
                Locations            = Helper.WrapLocations(assemblySymbol.Locations),
                ModuleName           = iname,
                Kind                 = ModuleKind.DynamicallyLinkedLibrary,
                RequiresStartupStub  = this.host.PointerSize == 4,
                TargetRuntimeVersion = coreAssembly.TargetRuntimeVersion,
                Version              = assemblySymbol.Identity.Version,
            };
            cciAssembly.AssemblyReferences.Add(coreAssembly);
            this.assemblySymbolCache.Add(assemblySymbol, cciAssembly);
            this.module = cciAssembly;

            var rootUnitNamespace = new RootUnitNamespace();

            cciAssembly.UnitNamespaceRoot = rootUnitNamespace;
            rootUnitNamespace.Unit        = cciAssembly;
            this.namespaceSymbolCache.Add(assemblySymbol.GlobalNamespace, rootUnitNamespace);

            var moduleClass = new NamespaceTypeDefinition()
            {
                ContainingUnitNamespace = rootUnitNamespace,
                InternFactory           = host.InternFactory,
                IsClass = true,
                Name    = nameTable.GetNameFor("<Module>"),
            };

            cciAssembly.AllTypes.Add(moduleClass);


            foreach (var m in assemblySymbol.GlobalNamespace.GetMembers())
            {
                var namespaceSymbol = m as NamespaceSymbol;
                if (namespaceSymbol != null)
                {
                    var cciNtd = TranslateMetadata(namespaceSymbol);
                    rootUnitNamespace.Members.Add(cciNtd);
                    continue;
                }

                var typeSymbol = m as TypeSymbol;
                if (typeSymbol != null)
                {
                    var namedType = TranslateMetadata((R.INamedTypeSymbol)typeSymbol);
                    // TODO: fix
                    //namedType.Attributes = TranslateMetadata(typeSymbol.GetAttributes());
                    var cciType = (INamespaceTypeDefinition)namedType;
                    rootUnitNamespace.Members.Add(cciType);
                    //cciAssembly.AllTypes.Add(cciType);
                    continue;
                }
            }

            //if (this.entryPoint != null) {
            //  cciAssembly.Kind = ModuleKind.ConsoleApplication;
            //  cciAssembly.EntryPoint = this.entryPoint;
            //}

            return(cciAssembly);
        }