Example #1
0
            public override void CaseAMethodDecl(AMethodDecl node)
            {
                End        = Start = TextPoint.FromCompilerCoords(node.GetName().Line, node.GetName().Pos);
                ReturnType = Util.TypeToString(node.GetReturnType());
                Name       = node.GetName().Text;

                base.CaseAMethodDecl(node);
            }
        private static ModifyData GetModifyData(AMethodDecl method)
        {
            if (methodData.ContainsKey(method))
            {
                return(methodData[method]);
            }

            if (Util.GetAncestor <AAProgram>(method) == null)
            {
                ModifyData modifyData = new ModifyData();
                string     name       = method.GetName().Text.ToLower();
                if (name.Contains("datatable"))
                {
                    modifyData.DataTable.Reads  |= name.Contains("get");
                    modifyData.DataTable.Writes |= name.Contains("set") || name.Contains("remove") || name.Contains("clear");
                }
                else
                {
                    modifyData.GameData.Writes |= name.Contains("set") || name.Contains("create");
                    modifyData.GameData.Reads   = true;
                }
                if (name == "wait")
                {
                    modifyData.Waits = true;
                }
                methodData[method] = modifyData;
                return(modifyData);
            }
            return(null);
        }
Example #3
0
        public override void CaseAMethodDecl(AMethodDecl node)
        {
            Write("\n");
            if (node.GetStatic() != null)
            {
                Write("static ");
            }
            if (node.GetNative() != null)
            {
                Write("native ");
            }
            node.GetReturnType().Apply(this);
            Write(" " + node.GetName().Text + "(");
            bool first = true;

            foreach (AALocalDecl formal in node.GetFormals())
            {
                if (!first)
                {
                    Write(", ");
                }
                formal.Apply(this);
                first = false;
            }
            if (node.GetBlock() != null)
            {
                Write(")\n");
                node.GetBlock().Apply(this);
            }
            else
            {
                Write(");\n\n");
            }
        }
Example #4
0
 //Rename trigger refferences
 public override void OutAMethodDecl(AMethodDecl node)
 {
     if (finalTrans.data.TriggerDeclarations.ContainsKey(node))
     {
         foreach (TStringLiteral stringLiteral in finalTrans.data.TriggerDeclarations[node])
         {
             stringLiteral.Text = "\"" + node.GetName().Text + "\"";
         }
     }
 }
        public override void CaseAMethodDecl(AMethodDecl node)
        {
            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(prefix + " [" + ++methodNr + "/" + methodCount + " " + node.GetName().Text + "]");
            }
            if (node.GetName().Text.Contains("t2"))
            {
                node = node;
            }
            //Move locals to the start
            node.Apply(new MoveLocalsToStart(finalTrans.data));
            bool changes = true;

            while (changes)
            {
                changes = false;

                //Remove foo = foo
                RemoveSelfAssignments.Parse(node, data);

                //Create cfg
                ControlFlowGraph cfg = ControlFlowGraph.Create(node);
                RemoveDeadCode.Parse(cfg);
                LivenessAnalysis.CalculateLiveVariables(cfg, data);
                bool redoLivenessAnalysis;
                changes |= RemoveUnusedAssignments.Parse(cfg, data, out redoLivenessAnalysis);
                if (redoLivenessAnalysis)
                {
                    LivenessAnalysis.CalculateLiveVariables(cfg, data);
                }
                changes |= VariableJoiner.Parse(cfg, data);
                //This phase doesn't use liveness analysis
                changes |= RemoveUnusedLocals.Parse(cfg, data);


                while (true)
                {
                    bool changed = RemoveSingleUsedAssignments.Parse(cfg, data);
                    if (changed)
                    {
                        changes = true;
                        LivenessAnalysis.CalculateLiveVariables(cfg, data);
                        continue;
                    }
                    break;
                }


                changes |= StatementRemover.Parse(node);
            }
        }
Example #6
0
        public override void CaseAMethodDecl(AMethodDecl node)
        {
            if (node.GetNative() == null && node.GetBlock() == null)
            {
                return;
            }
            if (node.GetStatic() != null)
            {
                return;
            }

            string inputStr = "native " + TypeToString(node.GetReturnType()) + " " + node.GetName().Text +
                              "(";
            bool first = true;

            foreach (AALocalDecl formal in node.GetFormals())
            {
                if (!first)
                {
                    inputStr += ", ";
                }
                inputStr += TypeToString(formal.GetType()) + " " + formal.GetName().Text;
                first     = false;
            }
            inputStr += ");";

            writer.WriteLine(inputStr);

            AStructDecl        str = Util.GetAncestor <AStructDecl>(node);
            List <AMethodDecl> methodList;

            if (str != null)
            {
                methodList = StructMethods[str];
            }
            else
            {
                methodList = Methods;
            }
            string sig = Util.GetMethodSignature(node);

            if (methodList.Any(otherMethod => Util.GetMethodSignature(otherMethod) == sig))
            {
                return;
            }

            methodList.Add(node);
            node.SetBlock(null);
            node.Parent().RemoveChild(node);
        }
Example #7
0
 public override void CaseAMethodDecl(AMethodDecl node)
 {
     if (node.GetName().Text == "InitMap" && node.GetFormals().Count == 0)
     {
         if (finalTrans.multipleMainEntries)
         {
             multipleEntryCandidates.Add(new ErrorCollection.Error(node.GetName(), Util.GetAncestor <AASourceFile>(node.GetName()), "Candidate"));
         }
         else if (finalTrans.mainEntry != null)
         {
             multipleEntryCandidates.Add(new ErrorCollection.Error(finalTrans.mainEntry.GetName(), Util.GetAncestor <AASourceFile>(finalTrans.mainEntry.GetName()), "Candidate"));
             multipleEntryCandidates.Add(new ErrorCollection.Error(node.GetName(), Util.GetAncestor <AASourceFile>(node.GetName()), "Candidate"));
             //finalTrans.errors.Add(new ErrorCollection.Error(node.GetName(), Util.GetAncestor<AASourceFile>(node), "Found multiple candidates for a main entry", true));
             finalTrans.multipleMainEntries = true;
             finalTrans.mainEntry           = null;
         }
         else
         {
             finalTrans.mainEntry = node;
         }
     }
     base.CaseAMethodDecl(node);
 }
Example #8
0
        public override void CaseAAProgram(AAProgram node)
        {
            /*decls.AddRange(finalTrans.data.Structs.Select(str => str.Decl));
             * decls.AddRange(finalTrans.data.Methods.Select(str => str.Decl));
             * decls.AddRange(finalTrans.data.Fields.Select(str => str.Decl));
             * if (finalTrans.data.Locals.Count > 0)
             *  decls.AddRange(finalTrans.data.Locals.Select(str => str.Value).Aggregate(Aggregate));
             * if (finalTrans.data.Structs.Count > 0)
             *  decls.AddRange(finalTrans.data.StructFields.Values.Aggregate(Aggregate));*/
            base.CaseAAProgram(node);
            Random rand = new Random();

            while (decls.Count > 0)
            {
                int  i = rand.Next(decls.Count);
                Node n = decls[i];
                decls.RemoveAt(i);

                if (n is AFieldDecl)
                {
                    AFieldDecl an     = (AFieldDecl)n;
                    an.GetName().Text = nextName;
                }
                else if (n is AMethodDecl)
                {
                    AMethodDecl an = (AMethodDecl)n;
                    if (finalTrans.mainEntry == an || (an.GetTrigger() != null && finalTrans.data.HasUnknownTrigger))
                    {
                        continue;
                    }
                    an.GetName().Text = nextName;
                }
                else if (n is AALocalDecl)
                {
                    AALocalDecl an    = (AALocalDecl)n;
                    an.GetName().Text = nextName;
                }
                else if (n is AStructDecl)
                {
                    AStructDecl an    = (AStructDecl)n;
                    an.GetName().Text = nextName;
                }
                NextName();
            }
        }
Example #9
0
        public static string ToString(AMethodDecl methodDecl)
        {
            string str = TypeToString(methodDecl.GetReturnType()) + " " + methodDecl.GetName().Text + "(";

            foreach (AALocalDecl formal in methodDecl.GetFormals())
            {
                if (str.EndsWith("("))
                {
                    str += TypeToString(formal.GetType()) + " " + formal.GetName().Text;
                }
                else
                {
                    str += ", " + TypeToString(formal.GetType()) + " " + formal.GetName().Text;
                }
            }
            str += ")";
            return(str);
        }
Example #10
0
        public static string GetMethodSignature(AMethodDecl method)
        {
            StringBuilder str = new StringBuilder(method.GetName().Text);

            str.Append("(");
            bool first = true;

            foreach (AALocalDecl localDecl in method.GetFormals())
            {
                if (!first)
                {
                    str.Append(",");
                }
                str.Append(Util.TypeToStringBuilder(localDecl.GetType()));
                first = false;
            }
            str.Append(")");
            return(str.ToString());
        }
Example #11
0
        public MethodDescription(AMethodDecl method)
        {
            Parser parser = new Parser(method);


            Start      = parser.Start;
            End        = parser.End;
            ReturnType = parser.ReturnType;
            Name       = parser.Name;
            Formals    = parser.Formals;
            Locals     = parser.Locals;
            if (method.Parent() != null)
            {
                method.Parent().RemoveChild(method);
            }
            IsDelegate = method.GetDelegate() != null;
            //if (!IsDelegate)
            Decl       = method;
            IsStatic   = method.GetStatic() != null;
            Visibility = method.GetVisibilityModifier();
            realType   = (PType)method.GetReturnType().Clone();
            Position   = TextPoint.FromCompilerCoords(method.GetName());
        }
Example #12
0
        private void MakeInitializerInvokes()
        {
            AABlock      block = (AABlock)mainEntry.GetBlock();
            AASourceFile file  = Util.GetAncestor <AASourceFile>(mainEntry);
            AMethodDecl  invokeMethod;

            /* Add
             * void Invoke(string methodName)
             * {
             *     trigger initTrigger = TriggerCreate(methodName);
             *     TriggerExecute(initTrigger, false, true);
             *     TriggerDestroy(initTrigger);
             * }
             */

            {
                //void Invoke(string methodName)
                AALocalDecl parameter = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                        new ANamedType(new TIdentifier("string"), null),
                                                        new TIdentifier("methodName"), null);
                AABlock methodBody = new AABlock();
                invokeMethod = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null,
                                               new AVoidType(new TVoid("void")), new TIdentifier("Invoke"),
                                               new ArrayList()
                {
                    parameter
                }, methodBody);

                //trigger initTrigger = TriggerCreate(methodName);
                ALocalLvalue parameterLvalue = new ALocalLvalue(new TIdentifier("methodName"));
                data.LocalLinks[parameterLvalue] = parameter;
                ALvalueExp parameterLvalueExp = new ALvalueExp(parameterLvalue);
                data.LvalueTypes[parameterLvalue]     =
                    data.ExpTypes[parameterLvalueExp] = parameter.GetType();
                ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("TriggerCreate"), new ArrayList()
                {
                    parameterLvalueExp
                });
                data.ExpTypes[invoke] = new ANamedType(new TIdentifier("trigger"), null);
                foreach (AMethodDecl methodDecl in data.Libraries.Methods)
                {
                    if (methodDecl.GetName().Text == "TriggerCreate")
                    {
                        data.SimpleMethodLinks[invoke] = methodDecl;
                        break;
                    }
                }
                AALocalDecl initTriggerDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                              new ANamedType(new TIdentifier("trigger"), null),
                                                              new TIdentifier("initTrigger"), invoke);
                methodBody.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), initTriggerDecl));

                //TriggerExecute(initTrigger, false, true);
                ALocalLvalue initTriggerLvalue = new ALocalLvalue(new TIdentifier("initTrigger"));
                data.LocalLinks[initTriggerLvalue] = initTriggerDecl;
                ALvalueExp initTriggerLvalueExp = new ALvalueExp(initTriggerLvalue);
                data.LvalueTypes[initTriggerLvalue]     =
                    data.ExpTypes[initTriggerLvalueExp] = initTriggerDecl.GetType();
                ABooleanConstExp falseBool = new ABooleanConstExp(new AFalseBool());
                ABooleanConstExp trueBool  = new ABooleanConstExp(new ATrueBool());
                data.ExpTypes[falseBool]    =
                    data.ExpTypes[trueBool] = new ANamedType(new TIdentifier("bool"), null);
                invoke = new ASimpleInvokeExp(new TIdentifier("TriggerExecute"), new ArrayList()
                {
                    initTriggerLvalueExp, falseBool, trueBool
                });
                data.ExpTypes[invoke] = new AVoidType(new TVoid("void"));
                foreach (AMethodDecl methodDecl in data.Libraries.Methods)
                {
                    if (methodDecl.GetName().Text == "TriggerExecute")
                    {
                        data.SimpleMethodLinks[invoke] = methodDecl;
                        break;
                    }
                }
                methodBody.GetStatements().Add(new AExpStm(new TSemicolon(";"), invoke));

                //TriggerDestroy(initTrigger);
                initTriggerLvalue = new ALocalLvalue(new TIdentifier("initTrigger"));
                data.LocalLinks[initTriggerLvalue] = initTriggerDecl;
                initTriggerLvalueExp = new ALvalueExp(initTriggerLvalue);
                data.LvalueTypes[initTriggerLvalue]     =
                    data.ExpTypes[initTriggerLvalueExp] = initTriggerDecl.GetType();
                invoke = new ASimpleInvokeExp(new TIdentifier("TriggerDestroy"), new ArrayList()
                {
                    initTriggerLvalueExp
                });
                data.ExpTypes[invoke] = new AVoidType(new TVoid("void"));
                foreach (AMethodDecl methodDecl in data.Libraries.Methods)
                {
                    if (methodDecl.GetName().Text == "TriggerDestroy")
                    {
                        data.SimpleMethodLinks[invoke] = methodDecl;
                        break;
                    }
                }
                methodBody.GetStatements().Add(new AExpStm(new TSemicolon(";"), invoke));

                file.GetDecl().Add(invokeMethod);
            }

            for (int i = data.InitializerMethods.Count - 1; i >= 0; i--)
            {
                AMethodDecl method = data.InitializerMethods[i];
                //Turn method into a trigger
                method.SetReturnType(new ANamedType(new TIdentifier("bool"), null));
                method.GetFormals().Add(new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                        new ANamedType(new TIdentifier("bool"), null),
                                                        new TIdentifier("testConds"), null));
                method.GetFormals().Add(new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                        new ANamedType(new TIdentifier("bool"), null),
                                                        new TIdentifier("runActions"), null));
                method.SetTrigger(new TTrigger("trigger"));
                ((AABlock)method.GetBlock()).GetStatements().Add(new AVoidReturnStm(new TReturn("return")));
                TriggerConvertReturn returnConverter = new TriggerConvertReturn(data);
                method.Apply(returnConverter);
                data.TriggerDeclarations[method] = new List <TStringLiteral>();


                //Add Invoke(<name>); to main entry
                TStringLiteral literal = new TStringLiteral(method.GetName().Text);
                data.TriggerDeclarations[method].Add(literal);
                AStringConstExp stringConst = new AStringConstExp(literal);
                data.ExpTypes[stringConst] = new ANamedType(new TIdentifier("string"), null);
                ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("Invoke"), new ArrayList()
                {
                    stringConst
                });
                data.SimpleMethodLinks[invoke] = invokeMethod;
                data.ExpTypes[invoke]          = invokeMethod.GetReturnType();
                block.GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), invoke));



                //ASyncInvokeExp syncInvokeExp = new ASyncInvokeExp(new TSyncInvoke("Invoke"), new AAmbiguousNameLvalue(new ASimpleName(new TIdentifier(method.GetName().Text))), new ArrayList());
                //data.Invokes.Add(method, new List<InvokeStm>(){new InvokeStm(syncInvokeExp)});
                //data.ExpTypes[syncInvokeExp] = new AVoidType(new TVoid("void"));

                //block.GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), syncInvokeExp));
            }
            for (int i = data.InvokeOnIniti.Count - 1; i >= 0; i--)
            {
                AMethodDecl method = data.InvokeOnIniti[i];

                //Turn method into a trigger
                method.SetReturnType(new ANamedType(new TIdentifier("bool"), null));
                method.GetFormals().Add(new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                        new ANamedType(new TIdentifier("bool"), null),
                                                        new TIdentifier("testConds"), null));
                method.GetFormals().Add(new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                        new ANamedType(new TIdentifier("bool"), null),
                                                        new TIdentifier("runActions"), null));
                method.SetTrigger(new TTrigger("trigger"));
                ((AABlock)method.GetBlock()).GetStatements().Add(new AVoidReturnStm(new TReturn("return")));
                TriggerConvertReturn returnConverter = new TriggerConvertReturn(data);
                method.Apply(returnConverter);
                data.TriggerDeclarations[method] = new List <TStringLiteral>();


                //Add Invoke(<name>); to main entry
                TStringLiteral literal = new TStringLiteral(method.GetName().Text);
                data.TriggerDeclarations[method].Add(literal);
                AStringConstExp stringConst = new AStringConstExp(literal);
                data.ExpTypes[stringConst] = new ANamedType(new TIdentifier("string"), null);
                ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("Invoke"), new ArrayList()
                {
                    stringConst
                });
                data.SimpleMethodLinks[invoke] = invokeMethod;
                data.ExpTypes[invoke]          = invokeMethod.GetReturnType();
                block.GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), invoke));


                /*
                 * ASyncInvokeExp syncInvokeExp = new ASyncInvokeExp(new TSyncInvoke("Invoke"),  new AAmbiguousNameLvalue(new ASimpleName(new TIdentifier(method.GetName().Text))), new ArrayList());
                 * data.Invokes.Add(method, new List<InvokeStm>() { new InvokeStm(syncInvokeExp) });
                 * data.ExpTypes[syncInvokeExp] = new AVoidType(new TVoid("void"));
                 *
                 * block.GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), syncInvokeExp));*/
            }
            for (int i = data.FieldsToInitInMapInit.Count - 1; i >= 0; i--)
            {
                AFieldDecl field = data.FieldsToInitInMapInit[i];
                if (field.GetInit() == null)
                {
                    continue;
                }

                AFieldLvalue   lvalue     = new AFieldLvalue(new TIdentifier(field.GetName().Text));
                AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), lvalue, field.GetInit());

                data.ExpTypes[assignment]    =
                    data.LvalueTypes[lvalue] = field.GetType();
                data.FieldLinks[lvalue]      = field;

                block.GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), assignment));
            }
            block.RemoveChild(mainEntryFieldInitBlock);
            block.GetStatements().Insert(0, mainEntryFieldInitBlock);
        }
Example #13
0
        public static void Parse(FinalTransformations finalTrans)
        {
            SharedData data = finalTrans.data;

            for (int i = 0; i < data.Methods.Count; i++)
            {
                for (int j = i + 1; j < data.Methods.Count; j++)
                {
                    AMethodDecl m1       = data.Methods[i].Decl;
                    AMethodDecl m2       = data.Methods[j].Decl;
                    bool        switched = false;
                    if (finalTrans.mainEntry == m1 || m1.GetTrigger() != null)
                    {
                        if (finalTrans.mainEntry == m2 || m2.GetTrigger() != null)
                        {
                            continue;
                        }
                    }
                    else if (finalTrans.mainEntry == m2 || m2.GetTrigger() != null)
                    {
                        AMethodDecl temp = m1;
                        m1       = m2;
                        m2       = temp;
                        switched = true;
                    }

                    MergeSameMethods merger = new MergeSameMethods(m2, data);
                    m1.Apply(merger);
                    if (merger.canMerge)
                    {
                        merger.otherNode = m1;
                        m2.Apply(merger);
                        if (merger.canMerge)
                        {
                            var arr = data.SimpleMethodLinks.ToArray();
                            foreach (KeyValuePair <ASimpleInvokeExp, AMethodDecl> link in arr)
                            {
                                if (link.Value == m2)
                                {
                                    data.SimpleMethodLinks[link.Key] = m1;
                                    link.Key.SetName(new TIdentifier(m1.GetName().Text));
                                }
                            }


                            m2.Parent().RemoveChild(m2);
                            if (switched)
                            {
                                data.Methods.RemoveAt(i);
                                i--;
                                break;
                            }
                            else
                            {
                                data.Methods.RemoveAt(j);
                                j--;
                                continue;
                            }
                        }
                    }
                }
            }
        }
 public override void CaseAMethodDecl(AMethodDecl node)
 {
     node.GetName().Text = currentNamespace + node.GetName().Text;
 }
        //private List<ErrorCollection.Error> multipleEntryCandidates = new List<ErrorCollection.Error>();
        public override void CaseAMethodDecl(AMethodDecl node)
        {
            //Done in a previous iteration

            /*if (node.GetName().Text == "InitMap" && node.GetFormals().Count == 0)
             * {
             *  if (finalTrans.multipleMainEntries)
             *  {
             *      multipleEntryCandidates.Add(new ErrorCollection.Error(node.GetName(), Util.GetAncestor<AASourceFile>(node.GetName()), "Candidate"));
             *  }
             *  else if (finalTrans.mainEntry != null)
             *  {
             *      multipleEntryCandidates.Add(new ErrorCollection.Error(finalTrans.mainEntry.GetName(), Util.GetAncestor<AASourceFile>(finalTrans.mainEntry.GetName()), "Candidate"));
             *      multipleEntryCandidates.Add(new ErrorCollection.Error(node.GetName(), Util.GetAncestor<AASourceFile>(node.GetName()), "Candidate"));
             *      //finalTrans.errors.Add(new ErrorCollection.Error(node.GetName(), Util.GetAncestor<AASourceFile>(node), "Found multiple candidates for a main entry", true));
             *      finalTrans.multipleMainEntries = true;
             *      finalTrans.mainEntry = null;
             *  }
             *  else
             *      finalTrans.mainEntry = node;
             * }*/

            AStructDecl str = Util.GetAncestor <AStructDecl>(node);

            if (str != null)
            {
                if (node.GetStatic() == null)
                {
                    structMethods.Add(node);
                }
                //Move the method outside the struct
                str.RemoveChild(node.Parent());
                AASourceFile file = (AASourceFile)str.Parent();
                int          i    = file.GetDecl().IndexOf(str);
                file.GetDecl().Insert(i /* + 1*/, node);
                node.GetName().Text = GetUniqueStructMethodName(str.GetName().Text + "_" + node.GetName().Text);

                if (node.GetStatic() == null)
                {
                    //Add the struct as a parameter
                    PType structType = new ANamedType(new TIdentifier(str.GetName().Text), null);
                    finalTrans.data.StructTypeLinks[(ANamedType)structType] = str;
                    if (str.GetClassToken() != null)
                    {
                        structType = new APointerType(new TStar("*"), structType);
                    }
                    structFormal = new AALocalDecl(new APublicVisibilityModifier(), null,
                                                   str.GetClassToken() == null ? new TRef("ref") : null, null, null,
                                                   structType,
                                                   new TIdentifier("currentStruct", node.GetName().Line,
                                                                   node.GetName().Pos), null);
                    node.GetFormals().Add(structFormal);
                    data.Locals[(AABlock)node.GetBlock()].Add(structFormal);
                }
                else
                {
                    node.SetStatic(null);
                }
                finalTrans.data.Methods.Add(new SharedData.DeclItem <AMethodDecl>(file, node));
                if (node.GetStatic() == null)
                {
                    OldParentStruct[node] = str;
                }
                //Fix refferences to other struct stuff);
                base.CaseAMethodDecl(node);
                //Will visit later, since it's added after the struct
                //base.CaseAMethodDecl(node);
                //if (str.GetLocals().Count == 0)
                //    str.Parent().RemoveChild(str);
                return;
            }
            AEnrichmentDecl enrichment = Util.GetAncestor <AEnrichmentDecl>(node);

            if (enrichment != null)
            {
                if (node.GetStatic() == null)
                {
                    structMethods.Add(node);
                }
                //Move the method outside the struct
                enrichment.RemoveChild(node);
                AASourceFile file = (AASourceFile)enrichment.Parent();
                int          i    = file.GetDecl().IndexOf(enrichment);
                file.GetDecl().Insert(i /* + 1*/, node);
                node.GetName().Text = GetUniqueStructMethodName(Util.TypeToIdentifierString(enrichment.GetType()) + "_" + node.GetName().Text);

                if (node.GetStatic() == null)
                {
                    //Add the struct as a parameter
                    PType structType = Util.MakeClone(enrichment.GetType(), finalTrans.data);
                    structFormal = new AALocalDecl(new APublicVisibilityModifier(), null, new TRef("ref"), null, null,
                                                   structType,
                                                   new TIdentifier("currentEnrichment", node.GetName().Line,
                                                                   node.GetName().Pos), null);
                    node.GetFormals().Add(structFormal);
                }
                finalTrans.data.Methods.Add(new SharedData.DeclItem <AMethodDecl>(file, node));
                //Fix refferences to other struct stuff);
                base.CaseAMethodDecl(node);
                //Will visit later, since it's added after the struct
                //base.CaseAMethodDecl(node);
                //if (str.GetLocals().Count == 0)
                //    str.Parent().RemoveChild(str);
                return;
            }
            //Build a list of overloads
            List <AMethodDecl> overloads     = new List <AMethodDecl>();
            List <string>      prefixMatches = new List <string>();

            foreach (SharedData.DeclItem <AMethodDecl> declItem in finalTrans.data.Methods)
            {
                if (!Util.IsSameNamespace(declItem.Decl, node))
                {
                    continue;
                }
                if (declItem.Decl.GetName().Text == node.GetName().Text)
                {
                    overloads.Add(declItem.Decl);
                }
                if (declItem.Decl.GetName().Text.StartsWith(node.GetName().Text + "O"))
                {
                    prefixMatches.Add(declItem.Decl.GetName().Text);
                }
            }



            foreach (AMethodDecl method in finalTrans.data.Libraries.Methods)
            {
                if (method.GetBlock() != null || method.GetNative() != null)
                {
                    if (method.GetName().Text == node.GetName().Text)
                    {
                        overloads.Add(method);
                    }
                    if (method.GetName().Text.StartsWith(node.GetName().Text + "O"))
                    {
                        prefixMatches.Add(method.GetName().Text);
                    }
                }
            }

            //Add fields
            foreach (SharedData.DeclItem <AFieldDecl> declItem in finalTrans.data.Fields)
            {
                if (declItem.Decl.GetName().Text.StartsWith(node.GetName().Text + "O"))
                {
                    prefixMatches.Add(declItem.Decl.GetName().Text);
                }
            }

            foreach (AFieldDecl field in finalTrans.data.Libraries.Fields)
            {
                if (field.GetName().Text.StartsWith(node.GetName().Text + "O"))
                {
                    prefixMatches.Add(field.GetName().Text);
                }
            }

            //Dont want to hit another method by appending O#
            string postfix = "";

            while (true)
            {
                postfix += "O";
                if (prefixMatches.Any(text => text.StartsWith(node.GetName().Text + postfix)))
                {
                    continue;
                }
                break;
            }

            if (overloads.Count > 1)
            {
                int i = 0;
                foreach (AMethodDecl method in overloads)
                {
                    if (node == finalTrans.mainEntry || (node.GetTrigger() != null && finalTrans.data.HasUnknownTrigger))
                    {
                        continue;
                    }
                    i++;
                    method.GetName().Text += postfix + i;
                }
            }

            if (node != finalTrans.mainEntry && (node.GetTrigger() == null || !finalTrans.data.HasUnknownTrigger))
            {
                node.GetName().Text = namespacePrefix + node.GetName().Text;
            }


            base.CaseAMethodDecl(node);
        }
        public override void CaseAConstructorDecl(AConstructorDecl node)
        {
            AStructDecl     str        = Util.GetAncestor <AStructDecl>(node);
            AEnrichmentDecl enrichment = Util.GetAncestor <AEnrichmentDecl>(node);
            AMethodDecl     replacer   = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null, new AVoidType(new TVoid("void")),
                                                         node.GetName(), new ArrayList(), node.GetBlock());

            replacer.GetName().Text += "_Constructor";
            while (node.GetFormals().Count > 0)
            {
                replacer.GetFormals().Add(node.GetFormals()[0]);
            }

            //Move the method outside the struct
            AASourceFile file = Util.GetAncestor <AASourceFile>(node);

            if (str != null)
            {
                str.RemoveChild(node.Parent());
            }
            else
            {
                enrichment.RemoveChild(node);
            }
            int i = file.GetDecl().IndexOf(str ?? (PDecl)enrichment);

            file.GetDecl().Insert(i /* + 1*/, replacer);
            //Add the struct as a parameter
            PType type;

            if (str != null)
            {
                ANamedType structType = new ANamedType(new TIdentifier(str.GetName().Text), null);
                finalTrans.data.StructTypeLinks[structType] = str;
                type = structType;
            }
            else
            {
                type = Util.MakeClone(enrichment.GetType(), finalTrans.data);
            }
            finalTrans.data.ConstructorMap[node] = replacer;
            structFormal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new APointerType(new TStar("*"), type), new TIdentifier("currentStruct", replacer.GetName().Line, replacer.GetName().Pos), null);
            replacer.GetFormals().Add(structFormal);
            finalTrans.data.Methods.Add(new SharedData.DeclItem <AMethodDecl>(file, replacer));

            //Add return stm
            replacer.SetReturnType(new APointerType(new TStar("*"), Util.MakeClone(type, data)));
            replacer.Apply(new TransformConstructorReturns(structFormal, data));

            //Insert call to base constructor););
            if (finalTrans.data.ConstructorBaseLinks.ContainsKey(node))
            {
                AMethodDecl      baseConstructor = finalTrans.data.ConstructorMap[finalTrans.data.ConstructorBaseLinks[node]];
                ASimpleInvokeExp invoke          = new ASimpleInvokeExp(new TIdentifier(baseConstructor.GetName().Text), new ArrayList());
                while (node.GetBaseArgs().Count > 0)
                {
                    invoke.GetArgs().Add(node.GetBaseArgs()[0]);
                }
                AThisLvalue thisLvalue1 = new AThisLvalue(new TThis("this"));
                ALvalueExp  thisExp1    = new ALvalueExp(thisLvalue1);
                invoke.GetArgs().Add(thisExp1);

                AThisLvalue thisLvalue2 = new AThisLvalue(new TThis("this"));

                AAssignmentExp assignExp = new AAssignmentExp(new TAssign("="), thisLvalue2, invoke);

                ANamedType structType = new ANamedType(new TIdentifier(str.GetName().Text), null);
                finalTrans.data.StructTypeLinks[structType] = str;

                finalTrans.data.LvalueTypes[thisLvalue1]         =
                    finalTrans.data.LvalueTypes[thisLvalue2]     =
                        finalTrans.data.ExpTypes[thisExp1]       =
                            finalTrans.data.ExpTypes[assignExp]  =
                                finalTrans.data.ExpTypes[invoke] = new APointerType(new TStar("*"), structType);

                //finalTrans.data.ExpTypes[invoke] = new AVoidType(new TVoid("void"));
                finalTrans.data.SimpleMethodLinks[invoke] = baseConstructor;

                ((AABlock)replacer.GetBlock()).GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), assignExp));

                //Inline if base and current are two different kinds of pointer types (int/string)
                AStructDecl      baseStruct = null;
                AConstructorDecl baseC      = finalTrans.data.ConstructorBaseLinks[node];
                foreach (KeyValuePair <AStructDecl, List <AConstructorDecl> > pair in finalTrans.data.StructConstructors)
                {
                    bool found = false;
                    foreach (AConstructorDecl decl in pair.Value)
                    {
                        if (baseC == decl)
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found)
                    {
                        baseStruct = pair.Key;
                        break;
                    }
                }
                if ((str.GetIntDim() == null) != (baseStruct.GetIntDim() == null))
                {
                    //For the inilining, change the type to the type of the caller
                    AALocalDecl lastFormal = baseConstructor.GetFormals().OfType <AALocalDecl>().Last();
                    lastFormal.SetRef(new TRef("ref"));
                    APointerType oldType = (APointerType)lastFormal.GetType();

                    structType = new ANamedType(new TIdentifier(str.GetName().Text), null);
                    finalTrans.data.StructTypeLinks[structType] = str;

                    APointerType newType = new APointerType(new TStar("*"), structType);
                    lastFormal.SetType(newType);

                    foreach (
                        ALocalLvalue lvalue in
                        data.LocalLinks.Where(pair => pair.Value == lastFormal).Select(pair => pair.Key))
                    {
                        data.LvalueTypes[lvalue] = newType;
                        if (lvalue.Parent() is ALvalueExp)
                        {
                            data.ExpTypes[(PExp)lvalue.Parent()] = newType;
                            if (lvalue.Parent().Parent() is APointerLvalue)
                            {
                                data.LvalueTypes[(PLvalue)lvalue.Parent().Parent()] = newType.GetType();
                            }
                        }
                    }

                    FixInlineMethods.Inline(invoke, finalTrans);
                    lastFormal.SetRef(null);
                    foreach (
                        ALocalLvalue lvalue in
                        data.LocalLinks.Where(pair => pair.Value == lastFormal).Select(pair => pair.Key))
                    {
                        data.LvalueTypes[lvalue] = oldType;
                        if (lvalue.Parent() is ALvalueExp)
                        {
                            data.ExpTypes[(PExp)lvalue.Parent()] = oldType;
                            if (lvalue.Parent().Parent() is APointerLvalue)
                            {
                                data.LvalueTypes[(PLvalue)lvalue.Parent().Parent()] = oldType.GetType();
                            }
                        }
                    }

                    lastFormal.SetType(oldType);
                }

                //Inline it instead, Since the pointer implementations might not be the same (int vs string)

                /*AMethodDecl baseConstructor = finalTrans.data.ConstructorMap[finalTrans.data.ConstructorBaseLinks[node]];
                 *
                 * AABlock localsBlock = new AABlock(new ArrayList(), new TRBrace("}"));
                 * ABlockStm cloneBlock = new ABlockStm(new TLBrace("{"), (PBlock) baseConstructor.GetBlock().Clone());
                 * Dictionary<AALocalDecl, PLvalue> localMap = new Dictionary<AALocalDecl, PLvalue>();
                 * for (int argNr = 0; argNr < baseConstructor.GetFormals().Count; argNr++)
                 * {
                 *  AALocalDecl formal = (AALocalDecl) baseConstructor.GetFormals()[i];
                 *  PExp arg;
                 *  if (i < baseConstructor.GetFormals().Count - 1)
                 *      arg = (PExp)node.GetBaseArgs()[i];
                 *  else
                 *  {
                 *      AThisLvalue thisLvalue = new AThisLvalue(new TThis("this"));
                 *      ALvalueExp thisExp = new ALvalueExp(thisLvalue);
                 *
                 *      ANamedType structType = new ANamedType(new TIdentifier(str.GetName().Text), null);
                 *      finalTrans.data.StructTypeLinks[structType] = str;
                 *
                 *      finalTrans.data.LvalueTypes[thisLvalue] =
                 *          finalTrans.data.ExpTypes[thisExp] = new APointerType(new TStar("*"), structType);
                 *
                 *      arg = thisExp;
                 *  }
                 *
                 *  if (formal.GetRef() != null || formal.GetOut() != null)
                 *  {
                 *      //Use same variable
                 *      localMap[formal] = ((ALvalueExp) arg).GetLvalue();
                 *  }
                 *  else
                 *  {
                 *      //Make a new variable
                 *      AALocalDecl newLocal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                 *                                             Util.MakeClone(formal.GetType(), finalTrans.data),
                 *                                             new TIdentifier(formal.GetName().Text),
                 *                                             Util.MakeClone(arg, data));
                 *
                 *      ALocalLvalue newLocalRef = new ALocalLvalue(new TIdentifier(newLocal.GetName().Text));
                 *
                 *      localMap[formal] = newLocalRef;
                 *      data.LvalueTypes[newLocalRef] = newLocal.GetType();
                 *      data.LocalLinks[newLocalRef] = newLocal;
                 *
                 *      localsBlock.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), newLocal));
                 *  }
                 *
                 * }
                 *
                 * CloneMethod cloner = new CloneMethod(finalTrans.data, localMap, cloneBlock);
                 * baseConstructor.GetBlock().Apply(cloner);
                 *
                 * ((AABlock)cloneBlock.GetBlock()).GetStatements().Insert(0, new ABlockStm(new TLBrace("{"), localsBlock));
                 * ((AABlock)node.GetBlock()).GetStatements().Insert(0, cloneBlock);*/
            }

            //Fix refferences to other struct stuff);
            base.CaseAMethodDecl(replacer);

            //Add functionality to refference the current struct in a constructor
            //Want to do it as a pointer type, since the constructer can only be called for pointer types
        }
        /*private class IsThisOnLeftSide : DepthFirstAdapter
         * {
         *  private PType type;
         *  private SharedData data;
         *  public bool IsAssignedTo;
         *  private List<AMethodDecl> investigatedMethods = new List<AMethodDecl>();
         *
         *  public IsThisOnLeftSide(PType type, SharedData data)
         *  {
         *      this.type = type;
         *      this.data = data;
         *  }
         *
         *  //Check assignments, method invocations and nonstatic method invocations.
         *
         *  public override void CaseAMethodDecl(AMethodDecl node)
         *  {
         *      investigatedMethods.Add(node);
         *  }
         *
         *  public override void CaseAThisLvalue(AThisLvalue node)
         *  {
         *      if (IsAssignedTo)
         *          return;
         *
         *      Node iParent = GetClosestNodeOfType(node, typeof (AAssignmentExp),
         *                                          typeof (ASimpleInvokeExp),
         *                                          typeof (ANonstaticInvokeExp),
         *                                          typeof (AAsyncInvokeStm),
         *                                          typeof (ASyncInvokeExp),
         *                                          typeof(AArrayLvalue),
         *                                          typeof(APointerLvalue),
         *                                          typeof(APropertyLvalue),
         *                                          typeof(AStructLvalue));
         *      if (iParent == null)
         *          return;
         *
         *      if (iParent is AAssignmentExp)
         *      {
         *          AAssignmentExp aParent = (AAssignmentExp) iParent;
         *          if (Util.IsAncestor(node, aParent.GetLvalue()))
         *          {
         *              IsAssignedTo = true;
         *          }
         *          return;
         *      }
         *      if (iParent is ASimpleInvokeExp)
         *      {
         *          ASimpleInvokeExp aParent = (ASimpleInvokeExp) iParent;
         *          AMethodDecl method = data.SimpleMethodLinks[aParent];
         *          if (investigatedMethods.Contains(method))
         *              return;
         *
         *          if (Util.IsAncestor(node, aParent.GetLvalue()))
         *          {
         *              IsAssignedTo = true;
         *          }
         *          return;
         *      }
         *  }
         *
         *  private Node GetClosestNodeOfType(Node node, params Type[] types)
         *  {
         *      if (node == null)
         *          return null;
         *      if (types.Contains(node.GetType()))
         *          return node;
         *      return GetClosestNodeOfType(node.Parent(), types);
         *  }
         * }*/

        public override void CaseADeconstructorDecl(ADeconstructorDecl node)
        {
            AStructDecl     str        = Util.GetAncestor <AStructDecl>(node);
            AEnrichmentDecl enrichment = Util.GetAncestor <AEnrichmentDecl>(node);
            AMethodDecl     replacer   = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null, new AVoidType(new TVoid("void")),
                                                         node.GetName(), new ArrayList(), node.GetBlock());

            replacer.GetName().Text += "_Deconstructor";

            //Move the method outside the struct
            AASourceFile file = Util.GetAncestor <AASourceFile>(node);

            if (str != null)
            {
                str.RemoveChild(node.Parent());
            }

            /*else
             *  enrichment.RemoveChild(node);*/
            int i = file.GetDecl().IndexOf(str ?? (PDecl)enrichment);

            file.GetDecl().Insert(i /* + 1*/, replacer);
            //Add the struct as a parameter
            PType type;

            if (str != null)
            {
                ANamedType structType = new ANamedType(new TIdentifier(str.GetName().Text), null);
                finalTrans.data.StructTypeLinks[structType] = str;
                type = structType;
            }
            else
            {
                type = Util.MakeClone(enrichment.GetType(), finalTrans.data);
            }
            finalTrans.data.DeconstructorMap[node] = replacer;
            AALocalDecl structFormal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new APointerType(new TStar("*"), type), new TIdentifier("currentStruct", replacer.GetName().Line, replacer.GetName().Pos), null);

            replacer.GetFormals().Add(structFormal);
            finalTrans.data.Methods.Add(new SharedData.DeclItem <AMethodDecl>(file, replacer));

            //Call base deconstructor before each return
            if (str != null && str.GetBase() != null)
            {
                AStructDecl baseStruct = data.StructTypeLinks[(ANamedType)str.GetBase()];
                if (data.StructDeconstructor.ContainsKey(baseStruct))
                {
                    baseStruct.Apply(this);
                    replacer.Apply(new CallDeconstructors(baseStruct, structFormal, data));

                    /*AMethodDecl baseDeconstructor = data.DeconstructorMap[data.StructDeconstructor[baseStruct]];
                     *
                     *
                     * ALocalLvalue structFormalRef = new ALocalLvalue(new TIdentifier("currentStruct"));
                     * ALvalueExp structFormalRefExp = new ALvalueExp(structFormalRef);
                     * ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("baseDeconstructor"),
                     *                                             new ArrayList() {structFormalRefExp});
                     * AABlock block = (AABlock) replacer.GetBlock();
                     * block.GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), invoke));
                     *
                     * data.LocalLinks[structFormalRef] = structFormal;
                     * data.SimpleMethodLinks[invoke] = baseDeconstructor;
                     * data.LvalueTypes[structFormalRef] = data.ExpTypes[structFormalRefExp] = structFormal.GetType();
                     * data.ExpTypes[invoke] = baseDeconstructor.GetReturnType();*/
                }
            }
            this.structFormal = structFormal;
            base.CaseAMethodDecl(replacer);
        }
Example #18
0
            public override void CaseASimpleInvokeExp(ASimpleInvokeExp node)
            {
                Item currentFile = GetIncludeItem(node);

                //If the method invocation is in a moved field, refference that field instead
                if (Util.GetAncestor <AFieldDecl>(node) != null)
                {
                    Item i = allItems.OfType <FieldItem>().FirstOrDefault(item => item.FieldDecl == Util.GetAncestor <AFieldDecl>(node));
                    if (i != null)
                    {
                        currentFile = i;
                    }
                }

                AMethodDecl decl     = finalTrans.data.SimpleMethodLinks[node];
                Item        declItem = ((Item)allItems.OfType <MethodDeclItem>().FirstOrDefault(item => item.RealDecl == decl)) ??
                                       allItems.OfType <IncludeItem>().First(
                    item => item.Current == Util.GetAncestor <AASourceFile>(decl));

                List <Item> cPath = currentFile.Path;
                List <Item> dPath = declItem.Path;

                for (int i = 0; i < Math.Min(cPath.Count, dPath.Count); i++)
                {
                    Item cItem = cPath[i];
                    Item dItem = dPath[i];

                    if (cItem != dItem)
                    {//We have a fork in the decls. make sure that the decl is before the used
                        int cI = cPath[i - 1].Children.IndexOf(cItem);
                        int dI = dPath[i - 1].Children.IndexOf(dItem);
                        if (dI < cI)
                        {
                            break;
                        }
                        //Move the decl up before the used
                        if (!(declItem is MethodDeclItem))
                        {
                            declItem = new MethodDeclItem(decl, cPath[i - 1], new List <Item>());
                            allItems.Add(declItem);
                        }
                        else
                        {
                            declItem.Parent.Children.Remove(declItem);
                            declItem.Parent = cPath[i - 1];
                        }
                        cPath[i - 1].Children.Insert(cI, declItem);
                        break;
                    }
                    if (i == cPath.Count - 1)
                    {
                        if (i == dPath.Count - 1)
                        {
                            //The decl and use is in same file. Ensure that the decl is before
                            if (Util.TokenLessThan(decl.GetName(), node.GetName()))
                            {
                                break;
                            }
                            //Add the decl item
                            declItem = new MethodDeclItem(decl, cPath[i], new List <Item>());
                            allItems.Add(declItem);
                            cPath[i].Children.Add(declItem);
                            break;
                        }
                        else
                        {
                            //The decl is included here or somewhere deeper. But above the use
                            break;
                        }
                    }
                    else if (i == dPath.Count - 1)
                    {
                        //We have reached the file where the decl is, but the use is included deeper, so it is above. Insert decl
                        int cI = cPath[i].Children.IndexOf(cPath[i + 1]);
                        declItem = new MethodDeclItem(decl, cPath[i], new List <Item>());
                        allItems.Add(declItem);
                        cPath[i].Children.Insert(cI, declItem);
                        break;
                    }
                }

                base.CaseASimpleInvokeExp(node);
            }
Example #19
0
        private static void GetMatchingTypes(ANamedType node, List <string> names, List <ATypedefDecl> typeDefs, List <AStructDecl> structs, List <AMethodDecl> delegates, List <ANamespaceDecl> namespaces, List <TIdentifier> generics)
        {
            List <IList>  decls             = new List <IList>();
            List <string> currentNamespace  = Util.GetFullNamespace(node);
            AASourceFile  currentSourceFile = Util.GetAncestor <AASourceFile>(node);

            if (names.Count == 1)
            {
                string name = names[0];
                //Check generic vars
                AStructDecl currentStruct = Util.GetAncestor <AStructDecl>(node);
                if (currentStruct != null)
                {
                    foreach (TIdentifier genericVar in currentStruct.GetGenericVars())
                    {
                        if (genericVar.Text == name)
                        {
                            generics.Add(genericVar);
                        }
                    }
                }
                //Get all type decls and namespaces matching this name, visible from this location
                List <IList> visibleDecls = Util.GetVisibleDecls(node, ((AAName)node.GetName()).GetIdentifier().Count == 1);
                foreach (IList declList in visibleDecls)
                {
                    bool sameFile = false;
                    if (declList.Count > 0)
                    {
                        sameFile = currentSourceFile == Util.GetAncestor <AASourceFile>((PDecl)declList[0]);
                    }
                    foreach (PDecl decl in declList)
                    {
                        bool sameNS = Util.NamespacesEquals(currentNamespace, Util.GetFullNamespace(decl));
                        if (decl is ANamespaceDecl)
                        {
                            ANamespaceDecl aDecl = (ANamespaceDecl)decl;
                            if (aDecl.GetName().Text == name)
                            {
                                namespaces.Add(aDecl);
                            }
                            continue;
                        }
                        if (decl is ATypedefDecl)
                        {
                            if (Util.IsAncestor(node, decl))
                            {
                                continue;
                            }
                            ATypedefDecl aDecl = (ATypedefDecl)decl;
                            if (aDecl.GetStatic() != null && !sameFile ||
                                aDecl.GetVisibilityModifier() is APrivateVisibilityModifier && !sameNS)
                            {
                                continue;
                            }
                            ANamedType namedType = (ANamedType)aDecl.GetName();
                            AAName     aName     = (AAName)namedType.GetName();
                            string     n         = ((TIdentifier)aName.GetIdentifier()[0]).Text;
                            if (n == name)
                            {
                                typeDefs.Add(aDecl);
                            }
                            continue;
                        }
                        if (decl is AStructDecl)
                        {
                            AStructDecl aDecl = (AStructDecl)decl;
                            if (!sameNS && aDecl.GetVisibilityModifier() is APrivateVisibilityModifier)
                            {
                                continue;
                            }
                            if (aDecl.GetName().Text == name)
                            {
                                structs.Add(aDecl);
                            }
                            continue;
                        }
                        if (decl is AMethodDecl)
                        {
                            AMethodDecl aDecl = (AMethodDecl)decl;
                            if (!sameNS && aDecl.GetVisibilityModifier() is APrivateVisibilityModifier ||
                                !sameFile && aDecl.GetStatic() != null)
                            {
                                continue;
                            }
                            if (aDecl.GetDelegate() != null && aDecl.GetName().Text == name)
                            {
                                delegates.Add(aDecl);
                            }
                            continue;
                        }
                    }
                }
            }
            else
            {
                string name = names[names.Count - 1];
                List <ANamespaceDecl> baseNamespaces = new List <ANamespaceDecl>();
                List <string>         baseNames      = new List <string>();
                baseNames.AddRange(names);
                baseNames.RemoveAt(baseNames.Count - 1);
                GetMatchingTypes(node, baseNames, new List <ATypedefDecl>(), new List <AStructDecl>(), new List <AMethodDecl>(), baseNamespaces, generics);
                foreach (ANamespaceDecl ns in baseNamespaces)
                {
                    bool sameFile = currentSourceFile == Util.GetAncestor <AASourceFile>(ns);
                    foreach (PDecl decl in ns.GetDecl())
                    {
                        bool sameNS = Util.NamespacesEquals(currentNamespace, Util.GetFullNamespace(decl));
                        if (decl is ANamespaceDecl)
                        {
                            ANamespaceDecl aDecl = (ANamespaceDecl)decl;
                            if (aDecl.GetName().Text == name)
                            {
                                namespaces.Add(aDecl);
                            }
                            continue;
                        }
                        if (decl is ATypedefDecl)
                        {
                            ATypedefDecl aDecl     = (ATypedefDecl)decl;
                            ANamedType   namedType = (ANamedType)aDecl.GetName();
                            AAName       aName     = (AAName)namedType.GetName();
                            string       n         = ((TIdentifier)aName.GetIdentifier()[0]).Text;
                            if (n == name)
                            {
                                typeDefs.Add(aDecl);
                            }
                            continue;
                        }
                        if (decl is AStructDecl)
                        {
                            AStructDecl aDecl = (AStructDecl)decl;
                            if (!sameNS && aDecl.GetVisibilityModifier() is APrivateVisibilityModifier)
                            {
                                continue;
                            }
                            if (aDecl.GetName().Text == name)
                            {
                                structs.Add(aDecl);
                            }
                            continue;
                        }
                        if (decl is AMethodDecl)
                        {
                            AMethodDecl aDecl = (AMethodDecl)decl;
                            if (!sameNS && aDecl.GetVisibilityModifier() is APrivateVisibilityModifier ||
                                !sameFile && aDecl.GetStatic() != null)
                            {
                                continue;
                            }
                            if (aDecl.GetDelegate() != null && aDecl.GetName().Text == name)
                            {
                                delegates.Add(aDecl);
                            }
                            continue;
                        }
                    }
                }
            }
        }
Example #20
0
        public override void OutAMethodDecl(AMethodDecl node)
        {
            //Check locals against everything else
            foreach (AALocalDecl local in locals)
            {
                if (local.GetName().Text.StartsWith("_"))
                {
                    local.GetName().Text = "u" + local.GetName().Text;
                }
                bool restart = true;
                while (restart)
                {
                    restart = false;
                    //Other locals
                    foreach (AALocalDecl local2 in locals)
                    {
                        if (local != local2 && local.GetName().Text == local2.GetName().Text)
                        {
                            local.GetName().Text += "1";
                            local.GetName().Text += "2";
                            restart = true;
                            break;
                        }
                    }
                    if (restart)
                    {
                        continue;
                    }


                    //Methods
                    foreach (SharedData.DeclItem <AMethodDecl> declItem in finalTrans.data.Methods)
                    {
                        AMethodDecl decl = declItem.Decl;
                        if (decl.GetName().Text == local.GetName().Text)
                        {
                            decl.GetName().Text  += "M";
                            local.GetName().Text += "L";
                            restart = true;
                            break;
                        }
                    }
                    if (restart)
                    {
                        continue;
                    }

                    //Fields
                    foreach (SharedData.DeclItem <AFieldDecl> declItem in finalTrans.data.Fields)
                    {
                        AFieldDecl decl = declItem.Decl;
                        if (decl.GetName().Text == local.GetName().Text)
                        {
                            decl.GetName().Text  += "F";
                            local.GetName().Text += "L";
                            restart = true;
                            break;
                        }
                    }
                    if (restart)
                    {
                        continue;
                    }

                    //Structs
                    foreach (SharedData.DeclItem <AStructDecl> declItem in finalTrans.data.Structs)
                    {
                        AStructDecl decl = declItem.Decl;
                        if (decl.GetName().Text == local.GetName().Text)
                        {
                            decl.GetName().Text  += "S";
                            local.GetName().Text += "L";
                            restart = true;
                            break;
                        }
                    }
                    if (restart)
                    {
                        continue;
                    }
                }
            }
        }
Example #21
0
        public static void Parse(AAProgram ast, FinalTransformations finalTrans)
        {
            bool restart = true;

            while (restart)
            {
                restart = false;
                //Fix locals
                ast.Apply(new MakeUniqueNames(finalTrans));

                //Fix methods
                foreach (SharedData.DeclItem <AMethodDecl> declItem1 in finalTrans.data.Methods)
                {
                    AMethodDecl decl1 = declItem1.Decl;
                    if (decl1.GetName().Text.StartsWith("_"))
                    {
                        decl1.GetName().Text = "u" + decl1.GetName().Text;
                        restart = true;
                        break;
                    }
                    //Other methods
                    foreach (SharedData.DeclItem <AMethodDecl> declItem2 in finalTrans.data.Methods)
                    {
                        AMethodDecl decl2 = declItem2.Decl;
                        if (decl1 != decl2 && decl1.GetName().Text == decl2.GetName().Text)
                        {
                            decl1.GetName().Text += "1";
                            decl2.GetName().Text += "2";
                            restart = true;
                            break;
                        }
                    }
                    if (restart)
                    {
                        break;
                    }

                    //Fields
                    foreach (SharedData.DeclItem <AFieldDecl> declItem2 in finalTrans.data.Fields)
                    {
                        AFieldDecl decl2 = declItem2.Decl;
                        if (decl1.GetName().Text == decl2.GetName().Text)
                        {
                            decl1.GetName().Text += "M";
                            decl2.GetName().Text += "F";
                            restart = true;
                            break;
                        }
                    }
                    if (restart)
                    {
                        break;
                    }


                    //structs
                    foreach (SharedData.DeclItem <AStructDecl> declItem2 in finalTrans.data.Structs)
                    {
                        AStructDecl decl2 = declItem2.Decl;
                        if (decl1.GetName().Text == decl2.GetName().Text)
                        {
                            decl1.GetName().Text += "M";
                            decl2.GetName().Text += "S";
                            restart = true;
                            break;
                        }
                    }
                    if (restart)
                    {
                        break;
                    }
                }
                if (restart)
                {
                    continue;
                }


                //Fix fields
                foreach (SharedData.DeclItem <AFieldDecl> declItem1 in finalTrans.data.Fields)
                {
                    AFieldDecl decl1 = declItem1.Decl;
                    if (decl1.GetName().Text.StartsWith("_"))
                    {
                        decl1.GetName().Text = "u" + decl1.GetName().Text;
                        restart = true;
                        break;
                    }
                    //Other fields
                    foreach (SharedData.DeclItem <AFieldDecl> declItem2 in finalTrans.data.Fields)
                    {
                        AFieldDecl decl2 = declItem2.Decl;
                        if (decl1 != decl2 && decl1.GetName().Text == decl2.GetName().Text)
                        {
                            decl1.GetName().Text += "1";
                            decl2.GetName().Text += "2";
                            restart = true;
                            break;
                        }
                    }
                    if (restart)
                    {
                        break;
                    }


                    //structs
                    foreach (SharedData.DeclItem <AStructDecl> declItem2 in finalTrans.data.Structs)
                    {
                        AStructDecl decl2 = declItem2.Decl;
                        if (decl1.GetName().Text == decl2.GetName().Text)
                        {
                            decl1.GetName().Text += "F";
                            decl2.GetName().Text += "S";
                            restart = true;
                            break;
                        }
                    }
                    if (restart)
                    {
                        break;
                    }
                }
                if (restart)
                {
                    continue;
                }


                //Fix structs
                foreach (SharedData.DeclItem <AStructDecl> declItem1 in finalTrans.data.Structs)
                {
                    AStructDecl decl1 = declItem1.Decl;
                    if (decl1.GetName().Text.StartsWith("_"))
                    {
                        decl1.GetName().Text = "u" + decl1.GetName().Text;
                        restart = true;
                        break;
                    }
                    //Other fields
                    foreach (SharedData.DeclItem <AStructDecl> declItem2 in finalTrans.data.Structs)
                    {
                        AStructDecl decl2 = declItem2.Decl;
                        if (decl1 != decl2 && decl1.GetName().Text == decl2.GetName().Text)
                        {
                            decl1.GetName().Text += "1";
                            decl2.GetName().Text += "2";
                            restart = true;
                            break;
                        }
                    }
                    if (restart)
                    {
                        break;
                    }
                }
                if (restart)
                {
                    continue;
                }
            }
        }
Example #22
0
 public override void OutAMethodDecl(AMethodDecl node)
 {
     node.GetName().Text = node.GetName().Text.Replace("+", "Plus");
     node.GetName().Text = node.GetName().Text.Replace("+", "Plus");
     node.GetName().Text = node.GetName().Text.Replace("-", "Minus");
     node.GetName().Text = node.GetName().Text.Replace("*", "Mul");
     node.GetName().Text = node.GetName().Text.Replace("/", "Div");
     node.GetName().Text = node.GetName().Text.Replace("%", "Mod");
     node.GetName().Text = node.GetName().Text.Replace("==", "Equals");
     node.GetName().Text = node.GetName().Text.Replace("!=", "NotEquals");
     node.GetName().Text = node.GetName().Text.Replace("<", "LessThan");
     node.GetName().Text = node.GetName().Text.Replace("<=", "LessThanOrEqual");
     node.GetName().Text = node.GetName().Text.Replace(">", "GreaterThan");
     node.GetName().Text = node.GetName().Text.Replace(">=", "GreaterThanOrEqual");
     node.GetName().Text = node.GetName().Text.Replace("&", "And");
     node.GetName().Text = node.GetName().Text.Replace("|", "Or");
     node.GetName().Text = node.GetName().Text.Replace("^", "Xor");
     node.GetName().Text = node.GetName().Text.Replace("<<", "ShiftLeft");
     node.GetName().Text = node.GetName().Text.Replace(">>", "ShiftRight");
     decls.AddLast(new MethodDecl(node));
 }