Example #1
0
 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);
 }
Example #2
0
 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);
 }
Example #3
0
        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;
        }
        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()));
            }
        }