private void Apply(AAProgram ast)
        {
            int stage       = 1;
            int totalStages = 31;

            List <ANamedType> deleteUs = new List <ANamedType>();

            foreach (KeyValuePair <ANamedType, AStructDecl> pair in data.StructTypeLinks)
            {
                ANamedType  type = pair.Key;
                AStructDecl str  = pair.Value;
                if (data.Enums.ContainsKey(str))
                {
                    type.SetName(new AAName(new ArrayList()
                    {
                        new TIdentifier(data.Enums[str] ? "int" : "byte")
                    }));
                    deleteUs.Add(type);
                }
            }
            foreach (ANamedType type in deleteUs)
            {
                data.StructTypeLinks.Remove(type);
            }



            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text2"));
            }
            stage++;

            ast.Apply(new RemoveNamespaces());

            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text3"));
            }
            stage++;

            ast.Apply(new StaticStructMembers(data));


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text4"));
            }
            stage++;

            ast.Apply(new RemoveConstants(data));



            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text5"));
            }
            stage++;

            ast.Apply(new MainEntryFinder(this));



            if (mainEntry == null)
            {
                //errors.Add(new ErrorCollection.Error("No entry point found (void InitMap(){...})", true));

                //Generate main entry
                AASourceFile file =
                    ast.GetSourceFiles().Cast <AASourceFile>().FirstOrDefault(
                        sourceFile => !Util.GetAncestor <AASourceFile>(sourceFile).GetName().Text.Contains("\\"));
                if (file == null)
                {
                    //Make default sourcefile
                    file = new AASourceFile();
                    file.SetName(new TIdentifier("MapScript"));
                    ast.GetSourceFiles().Add(file);
                    data.LineCounts[file] = 1;
                }
                mainEntry = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null, new AVoidType(new TVoid("void")), new TIdentifier("InitMap"), new ArrayList(), new AABlock());
                file.GetDecl().Add(mainEntry);
                data.Methods.Add(new SharedData.DeclItem <AMethodDecl>(file, mainEntry));
            }
            else if (Util.GetAncestor <AASourceFile>(mainEntry).GetName().Text.Contains("\\"))
            {
                errors.Add(new ErrorCollection.Error(mainEntry.GetName(), Util.GetAncestor <AASourceFile>(mainEntry), LocRM.GetString("FT_Text6"), true));
            }

            ((AABlock)mainEntry.GetBlock()).GetStatements().Insert(0, mainEntryFieldInitBlock);



            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text7"));
            }
            stage++;

            ast.Apply(new StructInitializer(this));


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text8"));
            }
            stage++;

            ast.Apply(new TransformExpressionIfs(data));


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text9"));
            }
            stage++;

            ast.Apply(new RenameUnicode(data));



            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text10"));
            }
            stage++;

            TransformProperties.Phase1.Parse(this);


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text11"));
            }
            stage++;

            ast.Apply(new RemoveDeadCode());


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text12"));
            }
            stage++;

            ast.Apply(new TransformMethodDecls(this));


            if (errors.HasErrors)
            {
                return;
            }


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text13"));
            }
            stage++;



            MakeInitializerInvokes();


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text14"));
            }
            stage++;

            ast.Apply(new AssignFixup(this));


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text15"));
            }
            stage++;

            TransformProperties.Phase2.Parse(this);



            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text16"));
            }
            stage++;

            if (data.Invokes.Count > 0)
            {
                Invokes.Parse(this);
            }



            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text17"));
            }
            stage++;

            ast.Apply(new RemoveEmptyStructs(this));


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text18"));
            }
            stage++;



            ast.Apply(new Delegates(this));



            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text19"));
            }
            stage++;

            //ast.Apply(new AddUnneededRef(data));
            ast.Apply(new FixInlineMethods(this, false));


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text20"));
            }
            stage++;



            ast.Apply(new RemoveUnnededRef(data));


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text21"));
            }
            stage++;

            ast.Apply(new SplitStructTests(data));


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text22"));
            }
            stage++;

            new Pointers(data).Parse(ast);



            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text23"));
            }
            stage++;

            ast.Apply(new FixInlineMethods(this, true));


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text24"));
            }
            stage++;

            //Split local struct into primitives, to make optimizations easier
            ast.Apply(new StructSplitter(data));
            //ast.Apply(new BulkCopyFixup(this));
            //BulkCopyFixup.Parse(ast, this);


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text25"));
            }
            stage++;

            //Remove stupid assignments (assignments to a variable where that variable is not used before its next assignment
            ast.Apply(new RemoveUnusedVariables(this));



            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text26"));
            }
            stage++;



            ast.Apply(new ConstantFolding(data));

            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text27"));
            }
            stage++;



            //Assign fixup was here //Dahm grafiti painters
            //ast.Apply(new LivenessAnalysis(this));
            ast.Apply(new Optimizations.OptimizePhase(this, LocRM.GetString("FT_Text1") + (stage - 1) + " / " + totalStages + LocRM.GetString("FT_Text27")));


            ast.Apply(new FixByteArrayIndexes(data));


            if (Options.Compiler.MakeShortNames)
            {
                if (data.AllowPrintouts)
                {
                    Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text28"));
                }
                stage++;

                ast.Apply(new MakeShortNames(this));
            }
            else
            {
                if (data.AllowPrintouts)
                {
                    Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text29"));
                }
                stage++;

                //MakeUniqueNames.Parse(ast, this);
                ast.Apply(new MakeUniqueNamesV2());
            }



            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text30"));
            }
            stage++;
            //Remove uneeded blocks and check that names fit the decls
            ast.Apply(new RenameRefferences(this));


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text31"));
            }
            stage++;
            //Obfuscate strings
            ast.Apply(new ObfuscateStrings(this));


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text32"));
            }
            stage++;
            MergeSameMethods.Parse(this);


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text33"));
            }
            stage++;
            //Insert includes, and move methods, structs and fields around so they are visible
            FixIncludes.Apply(ast, this);
            if (Options.Compiler.OneOutputFile)
            {
                ((AASourceFile)mainEntry.Parent()).SetName(new TIdentifier("MapScript"));
            }
        }
Exemple #2
0
        private void Apply(AAProgram ast)
        {
            int stage       = 1;
            int totalStages = 31;

            List <ANamedType> deleteUs = new List <ANamedType>();

            foreach (KeyValuePair <ANamedType, AStructDecl> pair in data.StructTypeLinks)
            {
                ANamedType  type = pair.Key;
                AStructDecl str  = pair.Value;
                if (data.Enums.ContainsKey(str))
                {
                    type.SetName(new AAName(new ArrayList()
                    {
                        new TIdentifier(data.Enums[str] ? "int" : "byte")
                    }));
                    deleteUs.Add(type);
                }
            }
            foreach (ANamedType type in deleteUs)
            {
                data.StructTypeLinks.Remove(type);
            }



            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Removing namespaces");
            }
            stage++;

            ast.Apply(new RemoveNamespaces());

            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Moving static members out");
            }
            stage++;

            ast.Apply(new StaticStructMembers(data));


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Removing constant variables");
            }
            stage++;

            ast.Apply(new RemoveConstants(data));



            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Locating main entry");
            }
            stage++;

            ast.Apply(new MainEntryFinder(this));



            if (mainEntry == null)
            {
                //errors.Add(new ErrorCollection.Error("No entry point found (void InitMap(){...})", true));

                //Generate main entry
                AASourceFile file =
                    ast.GetSourceFiles().Cast <AASourceFile>().FirstOrDefault(
                        sourceFile => !Util.GetAncestor <AASourceFile>(sourceFile).GetName().Text.Contains("\\"));
                if (file == null)
                {
                    //Make default sourcefile
                    file = new AASourceFile();
                    file.SetName(new TIdentifier("MapScript"));
                    ast.GetSourceFiles().Add(file);
                    data.LineCounts[file] = 1;
                }
                mainEntry = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null, new AVoidType(new TVoid("void")), new TIdentifier("InitMap"), new ArrayList(), new AABlock());
                file.GetDecl().Add(mainEntry);
                data.Methods.Add(new SharedData.DeclItem <AMethodDecl>(file, mainEntry));
            }
            else if (Util.GetAncestor <AASourceFile>(mainEntry).GetName().Text.Contains("\\"))
            {
                errors.Add(new ErrorCollection.Error(mainEntry.GetName(), Util.GetAncestor <AASourceFile>(mainEntry), "The source file containing the main entry function should be placed in root folder to be able to overwrite MapScript.galaxy", true));
            }

            ((AABlock)mainEntry.GetBlock()).GetStatements().Insert(0, mainEntryFieldInitBlock);



            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Setting default values for struct variables");
            }
            stage++;

            ast.Apply(new StructInitializer(this));


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Transforming expression ifs");
            }
            stage++;

            ast.Apply(new TransformExpressionIfs(data));


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Renaming unicode identifiers and strings");
            }
            stage++;

            ast.Apply(new RenameUnicode(data));



            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Transforming properties to methods (Phase 1)");
            }
            stage++;

            TransformProperties.Phase1.Parse(this);


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Removing dead code");
            }
            stage++;

            ast.Apply(new RemoveDeadCode());


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Moving methods members out of structs");
            }
            stage++;

            ast.Apply(new TransformMethodDecls(this));


            if (errors.HasErrors)
            {
                return;
            }


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Invoking initializers");
            }
            stage++;



            MakeInitializerInvokes();


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Moving assignments out to their own statement");
            }
            stage++;

            ast.Apply(new AssignFixup(this));


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Transforming properties to methods (Phase 2)");
            }
            stage++;

            TransformProperties.Phase2.Parse(this);



            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Fixing invokes (sync/assync)");
            }
            stage++;

            if (data.Invokes.Count > 0)
            {
                Invokes.Parse(this);
            }



            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Removing empty structs");
            }
            stage++;

            ast.Apply(new RemoveEmptyStructs(this));


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Making delegates");
            }
            stage++;



            ast.Apply(new Delegates(this));



            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Transforming inline methods (Run 1)");
            }
            stage++;

            //ast.Apply(new AddUnneededRef(data));
            ast.Apply(new FixInlineMethods(this, false));


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Removing unneeded ref parameters");
            }
            stage++;



            ast.Apply(new RemoveUnnededRef(data));


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Expanding struct equality tests");
            }
            stage++;

            ast.Apply(new SplitStructTests(data));


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Transforming pointers");
            }
            stage++;

            new Pointers(data).Parse(ast);



            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Transforming inline methods (Run 2)");
            }
            stage++;

            ast.Apply(new FixInlineMethods(this, true));


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Splitting local structs into primitives");
            }
            stage++;

            //Split local struct into primitives, to make optimizations easier
            ast.Apply(new StructSplitter(data));
            //ast.Apply(new BulkCopyFixup(this));
            //BulkCopyFixup.Parse(ast, this);


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Removnig redundant assignments");
            }
            stage++;

            //Remove stupid assignments (assignments to a variable where that variable is not used before its next assignment
            ast.Apply(new RemoveUnusedVariables(this));



            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Folding constants");
            }
            stage++;



            ast.Apply(new ConstantFolding(data));

            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Optimizing");
            }
            stage++;



            //Assign fixup was here //Dahm grafiti painters
            //ast.Apply(new LivenessAnalysis(this));
            ast.Apply(new Optimizations.OptimizePhase(this, "Transforming code (" + (stage - 1) + " / " + totalStages + "): Optimizing"));


            ast.Apply(new FixByteArrayIndexes(data));


            if (Options.Compiler.MakeShortNames)
            {
                if (data.AllowPrintouts)
                {
                    Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Making short names");
                }
                stage++;

                ast.Apply(new MakeShortNames(this));
            }
            else
            {
                if (data.AllowPrintouts)
                {
                    Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Making unique names");
                }
                stage++;

                //MakeUniqueNames.Parse(ast, this);
                ast.Apply(new MakeUniqueNamesV2());
            }



            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Renaming references to variables whose names have changed");
            }
            stage++;
            //Remove uneeded blocks and check that names fit the decls
            ast.Apply(new RenameRefferences(this));


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Obfuscating strings");
            }
            stage++;
            //Obfuscate strings
            ast.Apply(new ObfuscateStrings(this));


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Mergeing methods if they are the same");
            }
            stage++;
            MergeSameMethods.Parse(this);


            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText("Transforming code (" + stage + " / " + totalStages + "): Generating includes (merging to one file if selected)");
            }
            stage++;
            //Insert includes, and move methods, structs and fields around so they are visible
            FixIncludes.Apply(ast, this);
            if (Options.Compiler.OneOutputFile)
            {
                ((AASourceFile)mainEntry.Parent()).SetName(new TIdentifier("MapScript"));
            }
        }
Exemple #3
0
        public static List <IList> GetVisibleDecls(Node node, bool includeUsings)
        {
            List <IList>          returner          = new List <IList>();
            AASourceFile          currentSourceFile = GetAncestor <AASourceFile>(node);
            List <List <string> > usedNamespaces    = new List <List <string> >();

            if (includeUsings)
            {
                foreach (AUsingDecl usingDecl in currentSourceFile.GetUsings())
                {
                    List <string> ns = new List <string>();
                    foreach (TIdentifier identifier in usingDecl.GetNamespace())
                    {
                        ns.Add(identifier.Text);
                    }
                    // ns.Reverse();
                    usedNamespaces.Add(ns);
                }
            }
            {
                List <string> currentNS = GetFullNamespace(node);
                if (currentNS.Count > 0)
                {
                    usedNamespaces.Add(currentNS);
                }
            }
            List <IList> currentList = new List <IList>();
            List <IList> nextList    = new List <IList>();
            AAProgram    program     = GetAncestor <AAProgram>(currentSourceFile);

            foreach (AASourceFile sourceFile in program.GetSourceFiles())
            {
                currentList.Add(sourceFile.GetDecl());
                returner.Add(sourceFile.GetDecl());
            }
            while (currentList.Count > 0)
            {
                foreach (IList declList in currentList)
                {
                    foreach (PDecl decl in declList)
                    {
                        if (decl is ANamespaceDecl)
                        {
                            ANamespaceDecl aDecl  = (ANamespaceDecl)decl;
                            List <string>  ns     = GetFullNamespace(decl);
                            bool           prefix = false;
                            bool           match  = false;
                            foreach (List <string> usedNamespace in usedNamespaces)
                            {
                                if (NamespacePrefix(ns, usedNamespace))
                                {
                                    prefix = true;
                                    if (NamespacesEquals(ns, usedNamespace))
                                    {
                                        match = true;
                                        break;
                                    }
                                }
                            }
                            if (prefix)
                            {
                                nextList.Add(aDecl.GetDecl());
                            }
                            if (match)
                            {
                                returner.Add(aDecl.GetDecl());
                            }
                        }
                    }
                }
                currentList = nextList;
                nextList    = new List <IList>();
            }
            return(returner);
        }