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(); } }
public override void CaseAMethodDecl(AMethodDecl node) { if (node.GetInline() == null && node.GetTrigger() == null && !(data.ConstructorMap.ContainsValue(node) && !inlineConstructors) && node != finalTrans.mainEntry) { CountStatements counter = new CountStatements(); node.Apply(counter); if (counter.Count <= 2) { //Don't inline if it has a recurssive call to itself FindRecurssiveCall recurssiveCallSearcher = new FindRecurssiveCall(node, data); node.Apply(recurssiveCallSearcher); if (!recurssiveCallSearcher.InlinedCallToItself) { node.SetInline(new TInline("inline")); } } } base.CaseAMethodDecl(node); }
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) { if (processStructs || processFieldsOnly || processMethodsOnly) { base.CaseAMethodDecl(node); return; } bool removed = true; List <AALocalDecl> couldntRemove = new List <AALocalDecl>(); while (removed) { removed = false; definedLocals.Clear(); usedLocals.Clear(); assignedToLocals.Clear(); base.CaseAMethodDecl(node); usedLocals.AddRange(finalTrans.data.GeneratedVariables); foreach (AALocalDecl definedLocal in definedLocals) { if (!usedLocals.Contains(definedLocal) && !couldntRemove.Contains(definedLocal)) { if ((Util.GetAncestor <AABlock>(definedLocal) != null || node.GetTrigger() == null) && finalTrans.data.UserLocals.Contains(definedLocal)) { children.Add(new ErrorCollection.Error(definedLocal.GetName(), Util.GetAncestor <AASourceFile>(node), LocRM.GetString("ErrorText67") + definedLocal.GetName().Text, true)); } removed = true; //Remove decl); if (definedLocal.Parent() is ALocalDeclStm) { ALocalDeclStm localDeclStm = (ALocalDeclStm)definedLocal.Parent(); RemoveVariableStatement(localDeclStm, definedLocal.GetInit(), localDeclStm.GetToken().Line, localDeclStm.GetToken().Pos); } //Dont remove parameters else { couldntRemove.Add(definedLocal); } //Remove assignments); foreach (AAssignmentExp assignmentExp in assignedToLocals[definedLocal]) { if (assignmentExp.Parent() is AExpStm) { AExpStm stm = (AExpStm)assignmentExp.Parent(); RemoveVariableStatement(stm, assignmentExp.GetExp(), stm.GetToken().Line, stm.GetToken().Pos); continue; } PExp exp = assignmentExp.GetExp(); assignmentExp.ReplaceBy(exp); } } } } }
//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); }