public GeneratorWalker(CodeGen codeGen)
            {
                Debug.Assert(codeGen.typeGen != null);

                assembly = codeGen.typeGen.myAssembly;

                compctx = codeGen.Context;
                moduleInit = codeGen;
                outerScope = codeGen.typeGen;
                namespaces = new List<string>();

                //!!! need to get type names already declared...
                PushNewType(codeGen.typeGen, codeGen, null, null);

                assemblies = new List<Assembly>();
                if (RemoteCompiler.Instance != null) {
                    StringCollection files = RemoteCompiler.Instance.References;
                    foreach (string file in files) {
                        Assembly asm = IronPython.Hosting.PythonCompiler.LoadAssembly(file);
                        assemblies.Add(asm);
                    }
                }
            }
Пример #2
0
        public void Compile()
        {
            string fullPath = Path.GetFullPath(outputAssembly);
            string outDir = Path.GetDirectoryName(fullPath);
            string fileName = Path.GetFileName(outputAssembly);

            PythonCompilerSink sink = new PythonCompilerSink(compilerSink);

            assemblyGen = new AssemblyGen(
                Path.GetFileNameWithoutExtension(outputAssembly),
                outDir, fileName, includeDebugInformation, staticTypes, executable, machine
                );

            bool entryPointSet = false;

            // set default main file
            if (mainFile == null && sourceFiles.Count == 1 && targetKind != PEFileKinds.Dll) {
                mainFile = sourceFiles[0];
            }

            foreach (string sourceFile in sourceFiles) {
                bool createMainMethod = sourceFile == mainFile;
                CompilePythonModule(sourceFile, sink, createMainMethod);

                if (sink.Errors > 0) return;

                if (createMainMethod) {
                    entryPointSet = true;
                }
            }

            if (resourceFiles != null) {
                foreach (ResourceFile rf in resourceFiles) {
                    assemblyGen.AddResourceFile(rf.Name, rf.File, rf.PublicResource ? ResourceAttributes.Public : ResourceAttributes.Private);
                }
            }

            if (targetKind != PEFileKinds.Dll && !entryPointSet) {
                sink.AddError("", string.Format("Need an entry point for target kind {0}", targetKind), String.Empty, CodeSpan.Empty, -1, Severity.Error);
            }

            assemblyGen.Dump();
        }
Пример #3
0
 public TypeGen(AssemblyGen myAssembly, TypeBuilder myType)
 {
     this.myAssembly = myAssembly;
     this.myType = myType;
 }
        private static PythonModule DoGenerateModule(SystemState state, CompilerContext context, GlobalSuite gs, string moduleName, string sourceFileName, string outSuffix)
        {
            string fullPath;
            string outDir;
            string fileName;

            if (sourceFileName == "<stdin>") {
                fullPath = Environment.CurrentDirectory;
                outDir = Environment.CurrentDirectory;
                fileName = "__stdin__";
            } else {
                fullPath = Path.GetFullPath(sourceFileName);
                outDir = Options.BinariesDirectory == null ? Path.GetDirectoryName(fullPath) : Options.BinariesDirectory;
                fileName = Path.GetFileNameWithoutExtension(sourceFileName);
            }

            AssemblyGen ag = new AssemblyGen(moduleName + outSuffix, outDir, fileName + outSuffix + ".exe", true);
            ag.SetPythonSourceFile(fullPath);

            TypeGen tg = GenerateModuleType(moduleName, ag);
            CodeGen cg = GenerateModuleInitialize(context, gs, tg);

            CodeGen main = GenerateModuleEntryPoint(tg, cg, moduleName, null);
            ag.SetEntryPoint(main.MethodInfo, PEFileKinds.ConsoleApplication);
            ag.AddPythonModuleAttribute(tg, moduleName);

            Type ret = tg.FinishType();
            Assembly assm = ag.DumpAndLoad();
            ret = assm.GetType(moduleName);

            PythonModule pmod = CompiledModule.Load(moduleName, ret, state);
            return pmod;
        }
        internal static TypeGen GenerateModuleType(string moduleName, AssemblyGen ag)
        {
            TypeGen tg = ag.DefinePublicType(moduleName, typeof(CompiledModule));

            // Type inheriting from CompiledModule are Dicts. Mark the type with "[PythonType(typeof(Dict))]".
            ConstructorInfo PythonTypeAttributeCtor = typeof(PythonTypeAttribute).GetConstructor(new Type[1] { typeof(Type) });
            tg.myType.SetCustomAttribute(new CustomAttributeBuilder(PythonTypeAttributeCtor, new object[1] { typeof(Dict) }));

            tg.AddModuleField(typeof(PythonModule));
            tg.DefaultConstructor = tg.myType.DefineDefaultConstructor(MethodAttributes.Public);

            return tg;
        }
        public static void DumpSnippets()
        {
            if (Options.SaveAndReloadBinaries) {
                snippetAssembly.Dump();
            }
            snippetAssembly = CreateNewSnippetAssembly("snippets", Options.ILDebug);

            if (debuggableSnippetAssembly != null) {
                if (Options.SaveAndReloadBinaries)
                    debuggableSnippetAssembly.Dump();
                debuggableSnippetAssembly = null;
            }
        }