Esempio n. 1
0
        internal static MethodInfo GetEntryPointRuntimeMethod(IMethodSymbol entryPoint, Assembly assembly, CancellationToken cancellationToken)
        {
            string entryPointTypeName   = MetadataHelpers.BuildQualifiedName(entryPoint.ContainingNamespace.MetadataName, entryPoint.ContainingType.MetadataName);
            string entryPointMethodName = entryPoint.MetadataName;

            var entryPointType = assembly.GetType(entryPointTypeName, throwOnError: true, ignoreCase: false).GetTypeInfo();

            return(entryPointType.GetDeclaredMethod(entryPointMethodName));
        }
Esempio n. 2
0
 private static void AddTopLevelType(
     HashSet <string> names,
     Cci.INamespaceTypeDefinition type
     )
 {
     names?.Add(
         MetadataHelpers.BuildQualifiedName(
             type.NamespaceName,
             Cci.MetadataWriter.GetMangledName(type)
             )
         );
 }
Esempio n. 3
0
        private bool HasNameConflict(HashSet <string> namesOfTopLevelTypes, TEmbeddedType type, DiagnosticBag diagnostics)
        {
            Cci.INamespaceTypeDefinition def = type;

            if (namesOfTopLevelTypes.Contains(MetadataHelpers.BuildQualifiedName(def.NamespaceName, def.Name)))
            {
                // ERR_LocalTypeNameClash2/ERR_LocalTypeNameClash
                ReportNameCollisionWithAlreadyDeclaredType(type, diagnostics);
                return(true);
            }

            return(false);
        }
        public static AttributeData GetPhpExtensionAttribute(this Symbol symbol)
        {
            var attrs = symbol.GetAttributes();

            foreach (var a in attrs)
            {
                var fullname = MetadataHelpers.BuildQualifiedName((a.AttributeClass as NamedTypeSymbol)?.NamespaceName, a.AttributeClass.Name);
                if (fullname == CoreTypes.PhpExtensionAttributeFullName)
                {
                    return(a);
                }
            }

            return(null);
        }
        public static AttributeData GetPhpScriptAttribute(this TypeSymbol symbol)
        {
            var attrs = symbol.GetAttributes();

            for (int i = 0; i < attrs.Length; i++)
            {
                var a        = attrs[i];
                var fullname = MetadataHelpers.BuildQualifiedName((a.AttributeClass as NamedTypeSymbol)?.NamespaceName, a.AttributeClass.Name);
                if (fullname == CoreTypes.PhpScriptAttributeFullName)
                {
                    return(a);
                }
            }

            return(null);
        }
Esempio n. 6
0
        /// <summary>
        /// Gets [PhpType] attribute and its parameters.
        /// </summary>
        public static bool TryGetPhpTypeAttribute(this TypeSymbol symbol, out string typename, out string filename)
        {
            var attrs = symbol.GetAttributes();

            for (int i = 0; i < attrs.Length; i++)
            {
                var a        = attrs[i];
                var fullname = MetadataHelpers.BuildQualifiedName((a.AttributeClass as NamedTypeSymbol)?.NamespaceName, a.AttributeClass.Name);
                if (fullname == CoreTypes.PhpTypeAttributeFullName)
                {
                    var args = a.CommonConstructorArguments;
                    typename = (string)args[0].Value;
                    filename = (string)args[1].Value;
                    return(true);
                }
            }

            //
            typename = filename = null;
            return(false);
        }
Esempio n. 7
0
        /// <summary>
        /// Create symbols for nested types and initialize types map.
        /// </summary>
        protected void LazyInitializeTypes(IEnumerable <IGrouping <string, TypeDefinitionHandle> > typeGroups)
        {
            if (_types == null)
            {
                var moduleSymbol = ContainingPEModule;

                var children = ArrayBuilder <NamedTypeSymbol> .GetInstance();

                var skipCheckForPiaType = !moduleSymbol.Module.ContainsNoPiaLocalTypes();

                foreach (var g in typeGroups)
                {
                    foreach (var t in g)
                    {
                        if (skipCheckForPiaType || !moduleSymbol.Module.IsNoPiaLocalType(t))
                        {
                            children.Add(PENamedTypeSymbol.Create(moduleSymbol, this, t, g.Key));
                        }
                        else
                        {
                            // Pia ignored
                        }
                    }
                }

                var typesDict = children.ToDictionary(c => MetadataHelpers.BuildQualifiedName(c.NamespaceName, c.Name));
                children.Free();

                //if (noPiaLocalTypes != null)
                //{
                //    Interlocked.CompareExchange(ref _lazyNoPiaLocalTypes, noPiaLocalTypes, null);
                //}

                Interlocked.CompareExchange(ref _types, typesDict, null);
            }
        }
        /// <summary>
        /// Emits the compilation into given <see cref="ModuleBuilder"/> using Reflection.Emit APIs.
        /// </summary>
        /// <param name="compilation">Compilation.</param>
        /// <param name="cancellationToken">Can be used to cancel the emit process.</param>
        /// <param name="compiledAssemblyImage">Assembly image, returned only if we fallback to CCI writer.</param>
        /// <param name="entryPointTypeName">An entry point or null on failure.</param>
        /// <param name="entryPointMethodName">An entry point or null on failure.</param>
        /// <param name="diagnostics">Diagnostics.</param>
        /// <returns>True on success, false if a compilation error occurred or the compilation doesn't contain any code or declarations.</returns>
        /// <exception cref="InvalidOperationException">Referenced assembly can't be resolved.</exception>
        internal static bool Emit(
            this Compilation compilation,
            DiagnosticBag diagnostics,
            out string entryPointTypeName,
            out string entryPointMethodName,
            out byte[] compiledAssemblyImage,
            CancellationToken cancellationToken)
        {
            compiledAssemblyImage = null;

            var moduleBeingBuilt = compilation.CreateModuleBuilder(
                emitOptions: EmitOptions.Default,
                manifestResources: null,
                testData: null,
                diagnostics: diagnostics,
                cancellationToken: cancellationToken);

            if (moduleBeingBuilt == null)
            {
                entryPointTypeName   = null;
                entryPointMethodName = null;
                return(false);
            }

            if (!compilation.Compile(
                    moduleBeingBuilt,
                    win32Resources: null,
                    xmlDocStream: null,
                    emittingPdb: false,
                    diagnostics: diagnostics,
                    filterOpt: null,
                    cancellationToken: cancellationToken))
            {
                entryPointTypeName   = null;
                entryPointMethodName = null;
                return(false);
            }

            cancellationToken.ThrowIfCancellationRequested();

            DiagnosticBag metadataDiagnostics = DiagnosticBag.GetInstance();

            var context = new EmitContext((Cci.IModule)moduleBeingBuilt, null, metadataDiagnostics);

            using (var stream = new System.IO.MemoryStream())
            {
                Cci.PeWriter.WritePeToStream(
                    context,
                    compilation.MessageProvider,
                    () => stream,
                    nativePdbWriterOpt: null,
                    pdbPathOpt: null,
                    allowMissingMethodBodies: false,
                    deterministic: false,
                    cancellationToken: cancellationToken);

                compiledAssemblyImage = stream.ToArray();
            }

            var containingType = (Cci.INamespaceTypeReference)moduleBeingBuilt.EntryPoint.GetContainingType(context);

            entryPointTypeName   = MetadataHelpers.BuildQualifiedName(containingType.NamespaceName, Cci.MetadataWriter.GetMangledName(containingType));
            entryPointMethodName = moduleBeingBuilt.EntryPoint.Name;

            // translate metadata errors.
            return(compilation.FilterAndAppendAndFreeDiagnostics(diagnostics, ref metadataDiagnostics));
        }
Esempio n. 9
0
 static void AddTopLevelType(HashSet <string> names, Cci.INamespaceTypeDefinition type)
 // _namesOfTopLevelTypes are only used to generated exported types, which are not emitted in EnC deltas (hence generation 0):
 => names?.Add(MetadataHelpers.BuildQualifiedName(type.NamespaceName, Cci.MetadataWriter.GetMangledName(type, generation: 0)));