public override void CaseASimpleInvokeExp(ASimpleInvokeExp node)
        {
            if (processStructs)
            {
                return;
            }

            AMethodDecl decl = finalTrans.data.SimpleMethodLinks[node];

            if (!processFieldsOnly && unusedMethods.Contains(decl))
            {
                unusedMethods.Remove(decl);
            }

            AFieldDecl field = Util.GetAncestor <AFieldDecl>(node);

            if (!processMethodsOnly && field != null)
            {
                if (!fieldsWithMethodCalls.Contains(field))
                {
                    fieldsWithMethodCalls.Add(field);
                }
            }

            base.CaseASimpleInvokeExp(node);
        }
Example #2
0
        public override void InAFieldDecl(AFieldDecl node)
        {
            decls.Add(node);

            /*node.GetName().Text = nextName;
             * NextName();*/
        }
 public override void CaseAAssignmentExp(AAssignmentExp node)
 {
     if (!processMethodsOnly && !processStructs)
     {
         if (!processFieldsOnly)
         {
             if (node.GetLvalue() is ALocalLvalue)
             {
                 ALocalLvalue lvalue = (ALocalLvalue)node.GetLvalue();
                 assignedToLocals[finalTrans.data.LocalLinks[lvalue]].Add(node);
                 node.GetExp().Apply(this);
                 return;
             }
         }
         if (node.GetLvalue() is AFieldLvalue)
         {
             AFieldLvalue lvalue = (AFieldLvalue)node.GetLvalue();
             AFieldDecl   decl   = finalTrans.data.FieldLinks[lvalue];
             if (!assignedToFields[decl].Contains(node))
             {
                 assignedToFields[decl].Add(node);
             }
             node.GetExp().Apply(this);
             return;
         }
     }
     base.CaseAAssignmentExp(node);
 }
Example #4
0
 public FieldDecl(AFieldDecl decl)
 {
     Decl = decl;
     if (Name.StartsWith("_"))
     {
         Name = "u" + Name;
     }
 }
        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 void Add(AFieldDecl field, bool read, bool written)
 {
     if (Fields.ContainsKey(field))
     {
         Fields[field].Union(read, written);
     }
     else
     {
         Fields[field] = new Values(read, written);
     }
 }
Example #7
0
 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 CaseAFieldLvalue(AFieldLvalue node)
        {
            if (processStructs)
            {
                return;
            }

            AFieldDecl decl = finalTrans.data.FieldLinks[node];

            if (!usedFields.Contains(decl))
            {
                usedFields.Add(decl);
            }
            base.CaseAFieldLvalue(node);
        }
Example #9
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 #10
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 #11
0
            public override void OutAFieldLvalue(AFieldLvalue node)
            {
                if (currentMethod == null)
                {
                    return;
                }

                AFieldDecl field = data.FieldLinks[node];

                Node n = node;

                while (true)
                {
                    n = Util.GetNearestAncestor(n.Parent(), typeof(AMethodDecl), typeof(AAssignmentExp), typeof(AArrayLvalue));
                    if (n is AMethodDecl)
                    {
                        break;
                    }
                    if (n is AAssignmentExp)
                    {
                        if (Util.IsAncestor(node, ((AAssignmentExp)n).GetLvalue()))
                        {
                            if (!WrittenFields[currentMethod].Contains(field))
                            {
                                WrittenFields[currentMethod].Add(field);
                            }
                            return;
                        }
                        break;
                    }
                    if (n is AArrayLvalue)
                    {
                        if (Util.IsAncestor(node, ((AArrayLvalue)n).GetBase()))
                        {
                            continue;
                        }
                        break;
                    }
                    break;
                }

                if (!ReadFields[currentMethod].Contains(field))
                {
                    ReadFields[currentMethod].Add(field);
                }
            }
Example #12
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();
            }
        }
        /// <summary>
        /// Crawls the StarCraft 2 MPQs to grab the *.galaxy files inside. These files are parsed for (native) functions and constants.
        /// </summary>
        /// <returns>The functions and constants of StarCraft 2 in form of a LibraryData</returns>
        LibraryData loadLibraries()
        {
            // parse the files for functions and constants
            CrawlAndParseMpqs();

            // convert the functions and constants to a LibraryData format
            LibraryData lib = new LibraryData();

            // create the methods with from crawled info
            foreach (ParsedFunction function in functions)
            {
                AMethodDecl method = new AMethodDecl();
                if (function.IsNative)
                {
                    method.SetNative(new TNative("native"));
                }
                method.SetName(new TIdentifier(function.Name));
                method.SetReturnType(new ANamedType(new TIdentifier(function.ReturnType), null));

                // add function parameter
                foreach (var parameter in function.Parameters)
                {
                    method.GetFormals().Add(new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                            new ANamedType(new TIdentifier(parameter.Item1), null),
                                                            new TIdentifier(parameter.Item2), null));
                }

                lib.Methods.Add(method);
            }

            // create the constants from the crawled info
            foreach (ParsedConstant constant in constants)
            {
                AFieldDecl field = new AFieldDecl(new APublicVisibilityModifier(), null, new TConst("const"),
                                                  new ANamedType(new TIdentifier(constant.Type), null),
                                                  new TIdentifier(constant.Name), new AStringConstExp(new TStringLiteral(constant.Value)));
                lib.Fields.Add(field);
            }

            functions.Clear();
            constants.Clear();

            return(lib);
        }
Example #14
0
 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");
 }
Example #15
0
        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));
            }
        }
Example #16
0
        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);
            }
        }
Example #17
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;
        }
Example #18
0
        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);
        }
Example #19
0
 public override void OutAFieldDecl(AFieldDecl node)
 {
     data.Fields.Add(new SharedData.DeclItem <AFieldDecl>(currentSourceFile, node));
     data.UserFields.Add(node);
     base.OutAFieldDecl(node);
 }
Example #20
0
 public Field(AFieldDecl fieldDecl)
 {
     FieldDecl = fieldDecl;
 }
Example #21
0
 public VariableDecl(AFieldDecl field, AALocalDecl local, VariableDecl @base)
 {
     Field = field;
     Local = local;
     Base  = @base;
 }
 public override void CaseAFieldDecl(AFieldDecl node)
 {
     node.GetName().Text = namespacePrefix + node.GetName().Text;
 }
 public override void CaseAFieldDecl(AFieldDecl node)
 {
     node.GetName().Text = currentNamespace + node.GetName().Text;
 }
        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()));
            }
        }
Example #25
0
 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);
 }
Example #26
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 #27
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 #28
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);
        }
            public override void OutAFieldDecl(AFieldDecl node)
            {
                VariableDescription field = new VariableDescription(node);

                Fields.Add(field);
            }
Example #30
0
            public override void CaseAFieldLvalue(AFieldLvalue node)
            {
                Item currentFile = GetIncludeItem(node);

                //If the field 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;
                    }
                }

                AFieldDecl decl     = finalTrans.data.FieldLinks[node];
                Item       declItem = ((Item)allItems.OfType <FieldItem>().FirstOrDefault(item => item.FieldDecl == decl)) ??
                                      allItems.OfType <IncludeItem>().First(
                    item => item.Current == Util.GetAncestor <AASourceFile>(decl));
                List <Item> cPath = currentFile.Path;
                List <Item> dPath = declItem.Path;

                bool movedIt = false;

                for (int i = 0; i < Math.Min(cPath.Count, dPath.Count); i++)
                {
                    if (cPath[i] != dPath[i])
                    {
                        //We have a fork. make sure that the field is visible
                        int cI = cPath[i - 1].Children.IndexOf(cPath[i]);
                        int dI = dPath[i - 1].Children.IndexOf(dPath[i]);

                        if (dI < cI)
                        {//The decl is okay
                            break;
                        }

                        //Move the decl up
                        if (declItem is FieldItem)
                        {
                            declItem.Parent.Children.Remove(declItem);
                            declItem.Parent = cPath[i - 1];
                        }
                        else
                        {
                            declItem = new FieldItem(decl, cPath[i - 1], new List <Item>());
                            allItems.Add(declItem);
                        }
                        cPath[i - 1].Children.Insert(cI, declItem);
                        movedIt = true;
                        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 FieldItem(decl, cPath[i], new List <Item>());
                            allItems.Add(declItem);
                            cPath[i].Children.Add(declItem);
                            movedIt = true;
                            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 FieldItem(decl, cPath[i], new List <Item>());
                        allItems.Add(declItem);
                        cPath[i].Children.Insert(cI, declItem);
                        movedIt = true;
                        break;
                    }
                }

                //In case we have a struct type field, we must make sure the struct is still on top
                if (movedIt)
                {
                    CaseAFieldDecl(decl);
                }
                base.CaseAFieldLvalue(node);
            }