private int FoldInt(PLvalue lvalue, ref bool valid) { if (!valid) { return(-1); } if (lvalue is ALocalLvalue) { ALocalLvalue aLvalue = (ALocalLvalue)lvalue; AALocalDecl decl = data.LocalLinks[aLvalue]; if (decl.GetConst() == null) { valid = false; return(-1); } return(FoldInt(decl.GetInit(), ref valid)); } if (lvalue is AFieldLvalue) { AFieldLvalue aLvalue = (AFieldLvalue)lvalue; AFieldDecl decl = data.FieldLinks[aLvalue]; if (decl.GetConst() == null) { valid = false; return(-1); } return(FoldInt(decl.GetInit(), ref valid)); } if (lvalue is AStructFieldLvalue) { AStructFieldLvalue aLvalue = (AStructFieldLvalue)lvalue; AALocalDecl decl = data.StructMethodFieldLinks[aLvalue]; if (decl.GetConst() == null) { valid = false; return(-1); } return(FoldInt(decl.GetInit(), ref valid)); } if (lvalue is AStructLvalue) { AStructLvalue aLvalue = (AStructLvalue)lvalue; AALocalDecl decl = data.StructFieldLinks[aLvalue]; if (decl.GetConst() == null) { valid = false; return(-1); } return(FoldInt(decl.GetInit(), ref valid)); } valid = false; return(-1); }
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.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"); }
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.GetConst() == null) { return; } initialFieldDecl = node; if (IsConstant(node.GetInit())) { List <AFieldLvalue> lvalues = new List <AFieldLvalue>(); lvalues.AddRange(data.FieldLinks.Where(link => link.Value == node).Select(link => link.Key)); foreach (AFieldLvalue lvalue in lvalues) { PExp parent = (PExp)lvalue.Parent(); parent.ReplaceBy(Util.MakeClone(node.GetInit(), data)); } node.Parent().RemoveChild(node); } initialFieldDecl = null; }
bool IsConstant(PLvalue lvalue) { if (lvalue is ALocalLvalue) { ALocalLvalue aLvalue = (ALocalLvalue)lvalue; AALocalDecl decl = data.LocalLinks[aLvalue]; if (decl == initialLocalDecl) { return(false); } return(decl.GetConst() != null && IsConstant(decl.GetInit())); } if (lvalue is AFieldLvalue) { AFieldLvalue aLvalue = (AFieldLvalue)lvalue; AFieldDecl decl = data.FieldLinks[aLvalue]; if (decl == initialFieldDecl) { return(false); } return(decl.GetConst() != null && IsConstant(decl.GetInit())); } if (lvalue is APropertyLvalue) { return(false); } if (lvalue is ANamespaceLvalue) { return(true); } if (lvalue is AStructFieldLvalue) { AStructFieldLvalue aLvalue = (AStructFieldLvalue)lvalue; AALocalDecl decl = data.StructMethodFieldLinks[aLvalue]; if (decl == initialLocalDecl) { return(false); } return(decl.GetConst() != null && IsConstant(decl.GetInit())); } if (lvalue is AStructLvalue) { AStructLvalue aLvalue = (AStructLvalue)lvalue; AALocalDecl decl = data.StructFieldLinks[aLvalue]; if (decl == initialLocalDecl) { return(false); } return(decl.GetConst() != null && IsConstant(decl.GetInit())); } if (lvalue is AArrayLvalue) { AArrayLvalue aLvalue = (AArrayLvalue)lvalue; return(IsConstant(aLvalue.GetIndex()) && IsConstant(aLvalue.GetBase())); } if (lvalue is APointerLvalue) { APointerLvalue aLvalue = (APointerLvalue)lvalue; return(IsConstant(aLvalue.GetBase())); } if (lvalue is APArrayLengthLvalue) { APArrayLengthLvalue aLvalue = (APArrayLengthLvalue)lvalue; return(data.ExpTypes[aLvalue.GetBase()] is AArrayTempType); } if (lvalue is AThisLvalue) { return(false); } if (lvalue is AValueLvalue) { return(false); } throw new Exception("Unexpected lvalue. Got " + lvalue); }
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())); } }