public VariableDescription(AFieldDecl fieldDecl) { Name = fieldDecl.GetName().Text; Type = Util.TypeToString(fieldDecl.GetType()); PlacementPrefix = "Field"; VariableType = VariableTypes.Field; Const = fieldDecl.GetConst() != null; IsStatic = fieldDecl.GetStatic() != null; Visibility = fieldDecl.GetVisibilityModifier(); realType = (PType)fieldDecl.GetType().Clone(); init = fieldDecl.GetInit(); Line = fieldDecl.GetName().Line; Position = TextPoint.FromCompilerCoords(fieldDecl.GetName()); }
public override void CaseAFieldDecl(AFieldDecl node) { if (node.GetType() is ANamedType) { ANamedType type = (ANamedType)node.GetType(); if (finalTrans.data.StructTypeLinks.ContainsKey(type)) { AStructDecl strDecl = finalTrans.data.StructTypeLinks[type]; if (strDecl.GetLocals().Cast <PLocalDecl>().Select(decl => decl is AALocalDecl).Count() == 0) { node.Parent().RemoveChild(node); return; } } } base.CaseAFieldDecl(node); }
public override void OutAFieldDecl(AFieldDecl node) { if (node.GetInit() != null) { return; } //Field - init in main entry AABlock pBlock = (AABlock)finalTrans.mainEntryFieldInitBlock.GetBlock(); int insertIndex = 0; AFieldLvalue lvalue = new AFieldLvalue(new TIdentifier(node.GetName().Text)); data.FieldLinks[lvalue] = node; data.LvalueTypes[lvalue] = node.GetType(); AABlock block = new AABlock(new ArrayList(), new TRBrace("}")); MakeAssignments(block, node.GetType(), lvalue, true); if (block.GetStatements().Count != 0) { pBlock.GetStatements().Insert(insertIndex, new ABlockStm(new TLBrace("{"), block)); } }
public override void OutAFieldLvalue(AFieldLvalue node) { if (folding) { AFieldDecl field = data.FieldLinks[node]; //Must be int and must be const if (!(field.GetType() is ANamedType && ((ANamedType)field.GetType()).IsPrimitive("int"))) { errors.Add( new ErrorCollection.Error(node.GetName(), LocRM.GetString("ErrorText59"), false, new ErrorCollection.Error(field.GetName(), LocRM.GetString("ErrorText60"))), true); throw new ParserException(node.GetName(), "TypeLinking.OutAFieldLvalue"); } if (field.GetConst() == null) { if (!isANewExp) { errors.Add( new ErrorCollection.Error(node.GetName(), LocRM.GetString("ErrorText61"), false), true); throw new ParserException(node.GetName(), "TypeLinking.OutAFieldLvalue"); } } if (field.GetInit() == null)//An error will be given earlier - constant fields must have an initializer { throw new ParserException(node.GetName(), "TypeLinking.OutAFieldLvalue"); } field.GetInit().Apply(this); } else { base.OutAFieldLvalue(node); } }
public override void CaseAFieldDecl(AFieldDecl node) { if (node.GetStatic() != null) { return; } writer.WriteLine(TypeToString(node.GetType()) + " " + node.GetName().Text + ";"); if (Fields.Any(decl => decl.GetName().Text == node.GetName().Text)) { return; } Fields.Add(node); //node.SetInit(null); node.Parent().RemoveChild(node); }
public override void CaseAFieldDecl(AFieldDecl node) { if (node.GetStatic() != null) { Write("static "); } if (node.GetConst() != null) { Write("const "); } node.GetType().Apply(this); Write(" " + node.GetName().Text); if (node.GetInit() != null) { Write(" = "); node.GetInit().Apply(this); } Write(";\n\n"); }
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); }
public override void OutAAProgram(AAProgram node) { if (!processStructs) { if (!processFieldsOnly) { unusedMethods.RemoveAll(method => method.GetTrigger() != null); if (finalTrans.mainEntry != null) { unusedMethods.Remove(finalTrans.mainEntry); } if (finalTrans.data.DeobfuscateMethod != null) { unusedMethods.Remove(finalTrans.data.DeobfuscateMethod); } foreach (AMethodDecl unusedMethod in unusedMethods) { if (firstMethodRun && finalTrans.data.UserMethods.Contains(unusedMethod)) { children.Add(new ErrorCollection.Error(unusedMethod.GetName(), Util.GetAncestor <AASourceFile>(unusedMethod), LocRM.GetString("ErrorText219") + unusedMethod.GetName().Text, true)); } if (Options.Compiler.RemoveUnusedMethods) { unusedMethod.Parent().RemoveChild(unusedMethod); } } firstMethodRun = false; if (Options.Compiler.RemoveUnusedMethods) { finalTrans.data.Methods.RemoveAll(declItem => unusedMethods.Contains(declItem.Decl)); if (unusedMethods.Count > 0) { //We removed a method. this may cause other methods to be unused processMethodsOnly = true; unusedMethods.Clear(); unusedMethods.AddRange(finalTrans.data.Methods.Select((declItem) => declItem.Decl)); base.CaseAAProgram(node); return; } } unusedMethods.Clear(); processMethodsOnly = false; } if (!processFieldsOnly) { fieldsWithMethodCalls.Clear(); usedFields.Clear(); processFieldsOnly = true; base.CaseAAProgram(node); return; } usedFields.AddRange(finalTrans.data.ObfuscationFields); List <SharedData.DeclItem <AFieldDecl> > removedFields = new List <SharedData.DeclItem <AFieldDecl> >(); foreach (SharedData.DeclItem <AFieldDecl> declItem in finalTrans.data.Fields) { AFieldDecl fieldDecl = declItem.Decl; if (fieldDecl.GetConst() == null && !usedFields.Contains(fieldDecl)) { if (!reportedFields.Contains(declItem.Decl)) { if (firstFieldRun && finalTrans.data.UserFields.Contains(fieldDecl)) { children.Add(new ErrorCollection.Error(fieldDecl.GetName(), Util.GetAncestor <AASourceFile>(fieldDecl), LocRM.GetString("ErrorText65") + fieldDecl.GetName().Text, true)); } reportedFields.Add(declItem.Decl); } if (Options.Compiler.RemoveUnusedFields || (fieldDecl.GetType() is AArrayTempType && ((AArrayTempType)fieldDecl.GetType()).GetIntDim().Text == "0")) { //We cannot remove it if there is a method call in it if (fieldsWithMethodCalls.Contains(fieldDecl)) { continue; } //Remove assignments to the field foreach (AAssignmentExp assignmentExp in assignedToFields[fieldDecl]) { 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); } removedFields.Add(declItem); fieldDecl.Parent().RemoveChild(fieldDecl); } } } firstFieldRun = false; foreach (var removedField in removedFields) { finalTrans.data.Fields.Remove(removedField); } /* if (Options.Compiler.RemoveUnusedFields) * finalTrans.data.Fields.RemoveAll( * declItem => * (!usedFields.Contains(declItem.Decl)) && (!fieldsWithMethodCalls.Contains(declItem.Decl)));*/ if (removedFields.Count > 0) { //Other fields may have become unused fieldsWithMethodCalls.Clear(); usedFields.Clear(); processFieldsOnly = true; base.CaseAAProgram(node); return; } //Remove empty arrays from struct fields foreach (var pair in finalTrans.data.StructFields) { for (int i = 0; i < pair.Value.Count; i++) { AALocalDecl field = pair.Value[i]; if (field.GetType() is AArrayTempType && ((AArrayTempType)field.GetType()).GetIntDim().Text == "0") { field.Parent().RemoveChild(field); pair.Value.RemoveAt(i); --i; } } } } //Remove unused structs processFieldsOnly = false; if (!processStructs) { processStructs = true; base.CaseAAProgram(node); return; } foreach (SharedData.DeclItem <AStructDecl> declItem in finalTrans.data.Structs) { if (!usedStructs.Contains(declItem.Decl)) { if (firstStructRun) { children.Add(new ErrorCollection.Error(declItem.Decl.GetName(), Util.GetAncestor <AASourceFile>(declItem.Decl), LocRM.GetString("ErrorText64") + declItem.Decl.GetName().Text, true)); } if (Options.Compiler.RemoveUnusedStructs) { if (declItem.Decl != null && declItem.Decl.Parent() != null) { declItem.Decl.Parent().RemoveChild(declItem.Decl); } } } } if (Options.Compiler.RemoveUnusedStructs) { finalTrans.data.Structs.RemoveAll(declItem => !usedStructs.Contains(declItem.Decl)); } if (children.Count > 0) { finalTrans.errors.Add(new ErrorCollection.Error(children[0], LocRM.GetString("ErrorText66"), children.ToArray())); } }
public override void CaseAALocalDecl(AALocalDecl node) { //Convert a static struct field into a global variable. All refferences to it are structFieldLvalues. if (node.GetStatic() == null) { return; } AStructDecl str = (AStructDecl)node.Parent(); if (data.StructFields[str].Contains(node)) { data.StructFields[str].Remove(node); } AFieldDecl replacementField; //Don't enhrit static fields. if (data.EnheritanceLocalMap.ContainsKey(node)) { str.RemoveChild(node); AALocalDecl realVar = data.EnheritanceLocalMap[node]; if (convertionMap.ContainsKey(realVar)) { //Already converted to a field replacementField = convertionMap[realVar]; foreach (AStructFieldLvalue lvalue in data.StructMethodFieldLinks.Where(link => link.Value == node).Select(link => link.Key)) { AFieldLvalue newLvalue = new AFieldLvalue(new TIdentifier(replacementField.GetName().Text)); data.FieldLinks[newLvalue] = replacementField; data.LvalueTypes[newLvalue] = replacementField.GetType(); lvalue.ReplaceBy(newLvalue); } } else { List <AStructFieldLvalue> refferences = new List <AStructFieldLvalue>(); refferences.AddRange(data.StructMethodFieldLinks.Where(link => link.Value == node).Select(link => link.Key)); foreach (AStructFieldLvalue lvalue in refferences) { data.StructMethodFieldLinks[lvalue] = realVar; } } return; } replacementField = new AFieldDecl(new APublicVisibilityModifier(), null, node.GetConst(), node.GetType(), node.GetName(), node.GetInit()); replacementField.GetName().Text = str.GetName().Text + "_" + replacementField.GetName().Text; AASourceFile file = Util.GetAncestor <AASourceFile>(node); file.GetDecl().Insert(file.GetDecl().IndexOf(node.Parent()), replacementField); data.Fields.Add(new SharedData.DeclItem <AFieldDecl>(file, replacementField)); if (ContainsNewExp(replacementField.GetInit())) { data.FieldsToInitInMapInit.Add(replacementField); } foreach (AStructFieldLvalue lvalue in data.StructMethodFieldLinks.Where(link => link.Value == node).Select(link => link.Key)) { AFieldLvalue newLvalue = new AFieldLvalue(new TIdentifier(replacementField.GetName().Text)); data.FieldLinks[newLvalue] = replacementField; data.LvalueTypes[newLvalue] = replacementField.GetType(); lvalue.ReplaceBy(newLvalue); } convertionMap.Add(node, replacementField); node.Parent().RemoveChild(node); }