예제 #1
0
            public override void OutASimpleInvokeExp(ASimpleInvokeExp node)
            {
                if (currentMethod == null)
                {
                    return;
                }

                AMethodDecl targetMethod = data.SimpleMethodLinks[node];

                if (node.Parent() is APointerLvalue || node.Parent() is AStructLvalue)
                {
                    //Move node out
                    MoveOut(node, targetMethod.GetReturnType());
                }


                //Only if it is not a library method
                if (Util.HasAncestor <AAProgram>(node))
                {
                    if (!dependancies[currentMethod].Contains(targetMethod))
                    {
                        dependancies[currentMethod].Add(targetMethod);
                    }
                }
            }
        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);
        }
예제 #3
0
 public override void CaseALocalLvalue(ALocalLvalue node)
 {
     if (Util.HasAncestor <AMethodDecl>(node) &&                                         //Is in a method
         data.LocalLinks[node].Parent() == Util.GetAncestor <AMethodDecl>(node))         //Is a link to a formal in that method
     {
         if (Util.HasAncestor <AAssignmentExp>(node) &&                                  //Is in an assignement
             Util.IsAncestor(node, Util.GetAncestor <AAssignmentExp>(node).GetLvalue())) //Is left side of the assignment
         {
             AssignedFormals.Add(data.LocalLinks[node]);
         }
         else if (Util.HasAncestor <ASimpleInvokeExp>(node))
         {
             ASimpleInvokeExp invoke = Util.GetAncestor <ASimpleInvokeExp>(node);
             AMethodDecl      method = data.SimpleMethodLinks[invoke];
             for (int i = 0; i < invoke.GetArgs().Count; i++)
             {
                 AALocalDecl formal = (AALocalDecl)method.GetFormals()[i];
                 if ((formal.GetRef() != null || formal.GetOut() != null) && Util.IsAncestor(node, (Node)invoke.GetArgs()[i]))
                 {
                     AssignedFormals.Add(data.LocalLinks[node]);
                     return;
                 }
             }
         }
     }
 }
        public override void CaseASimpleInvokeExp(ASimpleInvokeExp node)
        {
            PExp  expNode = (PExp)node;
            PType type    = data.ExpTypes[expNode];

            if (type is APointerType)
            {
                type = new ANamedType(new TIdentifier("string"), null);
            }
            ALocalLvalue local = new ALocalLvalue(new TIdentifier("tempName", 0, 0));
            ALvalueExp   exp   = new ALvalueExp(local);
            PStm         stm   = Util.GetAncestor <PStm>(node);
            AABlock      block = (AABlock)stm.Parent();

            node.ReplaceBy(exp);
            AALocalDecl localDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                    Util.MakeClone(type, data),
                                                    new TIdentifier(varName, 0, 0), expNode);
            ALocalDeclStm newStm = new ALocalDeclStm(new TSemicolon(";"), localDecl);

            block.GetStatements().Insert(block.GetStatements().IndexOf(stm), newStm);
            NewStatements.Add(newStm);

            data.LvalueTypes[local] = type;
            data.ExpTypes[exp]      = type;
            data.LocalLinks[local]  = localDecl;
            //localDecl.Apply(this);
            exp.Apply(this);
            return;
        }
예제 #5
0
        private AMethodDecl GetIntPointerMethod()
        {
            if (GetIntPointerPartMethod != null)
            {
                return(GetIntPointerPartMethod);
            }

            /*
             *  int GetIntPointerPart(string delegate)
             *  {
             *      return IntToString(GetPointerPart(delegate));
             *  }
             */

            AASourceFile sourceFile     = Util.GetAncestor <AASourceFile>(finalTrans.mainEntry);
            AALocalDecl  delegateFormal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                          new ANamedType(new TIdentifier("string"), null),
                                                          new TIdentifier("delegate"), null);
            ALocalLvalue delegateRef1    = new ALocalLvalue(new TIdentifier("delegate"));
            ALvalueExp   delegateRef1Exp = new ALvalueExp(delegateRef1);

            ASimpleInvokeExp getPointerPartInvoke = new ASimpleInvokeExp(new TIdentifier("GetPointerPart"), new ArrayList()
            {
                delegateRef1Exp
            });

            ASimpleInvokeExp StringToIntInvoke = new ASimpleInvokeExp(new TIdentifier("StringToInt"), new ArrayList()
            {
                getPointerPartInvoke
            });

            GetIntPointerPartMethod = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null,
                                                      new ANamedType(new TIdentifier("int"), null),
                                                      new TIdentifier("GetPointerPart", finalTrans.data.LineCounts[sourceFile] + 1, 1), new ArrayList()
            {
                delegateFormal
            },
                                                      new AABlock(
                                                          new ArrayList()
            {
                new AValueReturnStm(new TReturn("return"), StringToIntInvoke)
            },
                                                          new TRBrace("}")));
            sourceFile.GetDecl().Add(GetIntPointerPartMethod);
            data.Methods.Add(new SharedData.DeclItem <AMethodDecl>(sourceFile, GetIntPointerPartMethod));

            finalTrans.data.LocalLinks[delegateRef1]               = delegateFormal;
            finalTrans.data.LvalueTypes[delegateRef1]              =
                finalTrans.data.ExpTypes[delegateRef1Exp]          =
                    finalTrans.data.ExpTypes[getPointerPartInvoke] = new ANamedType(new TIdentifier("string"), null);
            finalTrans.data.ExpTypes[StringToIntInvoke]            = new ANamedType(new TIdentifier("int"), null);


            finalTrans.data.SimpleMethodLinks[getPointerPartInvoke] = GetStringPointerMethod();
            finalTrans.data.SimpleMethodLinks[StringToIntInvoke]    =
                finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == StringToIntInvoke.GetName().Text);

            return(GetIntPointerPartMethod);
        }
예제 #6
0
        //Rename method invocations
        public override void OutASimpleInvokeExp(ASimpleInvokeExp node)
        {
            AMethodDecl method = finalTrans.data.SimpleMethodLinks[node];

            if (method == null)
            {
                method = null;
            }
            node.GetName().Text = finalTrans.data.SimpleMethodLinks[node].GetName().Text;
        }
예제 #7
0
            public override void CaseAContinueStm(AContinueStm node)
            {
                AABlock          pBlock        = (AABlock)node.Parent();
                ALocalLvalue     replaceVarRef = new ALocalLvalue(new TIdentifier("whileVar"));
                ASimpleInvokeExp clone         = (ASimpleInvokeExp)Util.MakeClone(this.node, data);
                AAssignmentExp   assignment    = new AAssignmentExp(new TAssign("="), replaceVarRef, clone);

                data.LocalLinks[replaceVarRef] = replaceVarDecl;
                data.ExpTypes[assignment]      = data.LvalueTypes[replaceVarRef] = replaceVarDecl.GetType();
                pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(node), new AExpStm(new TSemicolon(";"), assignment));
                replacementExpressions.Add(clone);
            }
예제 #8
0
        public override void CaseADelegateExp(ADelegateExp node)
        {
            /* Replace delegate<Type>(method)
             * With
             *
             * "method"
             *
             * or
             *
             * "method:reciever"
             */
            AMethodDecl    method = finalTrans.data.DelegateCreationMethod[node];
            string         d      = GetName(method);
            PExp           replacer;
            APointerLvalue reciever = finalTrans.data.DelegateRecieveres[node];

            if (reciever != null)
            {
                d += ":";
                AStringConstExp leftSide = new AStringConstExp(new TStringLiteral("\"" + d + "\""));
                replacer = new ABinopExp(leftSide, new APlusBinop(new TPlus("+")), reciever.GetBase());
                finalTrans.data.ExpTypes[leftSide]     =
                    finalTrans.data.ExpTypes[replacer] = new ANamedType(new TIdentifier("string"), null);

                if (Util.IsIntPointer(reciever, data.LvalueTypes[reciever], data))
                {
                    ASimpleInvokeExp intToStringInvoke = new ASimpleInvokeExp(new TIdentifier("IntToString"),
                                                                              new ArrayList()
                    {
                        ((ABinopExp)replacer).GetRight()
                    });
                    ((ABinopExp)replacer).SetRight(intToStringInvoke);

                    finalTrans.data.SimpleMethodLinks[intToStringInvoke] =
                        data.Libraries.Methods.First(m => m.GetName().Text == intToStringInvoke.GetName().Text);
                    data.ExpTypes[intToStringInvoke] = new ANamedType(new TIdentifier("string"), null);
                }
            }
            else
            {
                replacer = new AStringConstExp(new TStringLiteral("\"" + d + "\""));
                finalTrans.data.ExpTypes[replacer] = new ANamedType(new TIdentifier("string"), null);
            }
            MoveMethodDeclsOut mover = new MoveMethodDeclsOut("delegateVar", finalTrans.data);

            node.Apply(mover);
            node.ReplaceBy(replacer);
            foreach (PStm stm in mover.NewStatements)
            {
                stm.Apply(this);
            }
        }
예제 #9
0
            public override void OutASimpleInvokeExp(ASimpleInvokeExp node)
            {
                if (currentMethod == null)
                {
                    return;
                }
                AMethodDecl decl = data.SimpleMethodLinks[node];

                if (!UsedMethods[currentMethod].Contains(decl))
                {
                    UsedMethods[currentMethod].Add(decl);
                }
            }
예제 #10
0
                public override void CaseASimpleInvokeExp(ASimpleInvokeExp node)
                {
                    AMethodDecl target = data.SimpleMethodLinks[node];

                    if (target == initialMethod)
                    {
                        InlinedCallToItself = true;
                    }
                    else if (target.GetInline() != null)
                    {
                        target.Apply(this);
                    }
                    base.CaseASimpleInvokeExp(node);
                }
예제 #11
0
        public override void OutASimpleInvokeExp(ASimpleInvokeExp node)
        {
            AMethodDecl decl = finalTrans.data.SimpleMethodLinks[node];

            if (decl.GetInline() != null && Util.HasAncestor <AABlock>(node))
            {
                foreach (AABlock block in Inline(node, finalTrans))
                {
                    block.Apply(this);
                }
            }

            /*else
             *  base.CaseASimpleInvokeExp(node);*/
        }
예제 #12
0
        public override void CaseASimpleInvokeExp(ASimpleInvokeExp node)
        {
            Write(node.GetName().Text + "(");
            bool first = true;

            foreach (PExp exp in node.GetArgs())
            {
                if (!first)
                {
                    Write(", ");
                }
                exp.Apply(this);
                first = false;
            }
            Write(")");
        }
예제 #13
0
        public override void CaseASimpleInvokeExp(ASimpleInvokeExp node)
        {
            Value += node.GetName().Text + "(";
            bool first = true;

            foreach (PExp arg in node.GetArgs())
            {
                if (!first)
                {
                    Value += ", ";
                }
                else
                {
                    first = false;
                }
                arg.Apply(this);
            }
            Value += ")";
        }
예제 #14
0
        public override void CaseASimpleInvokeExp(ASimpleInvokeExp node)
        {
            AMethodDecl target = data.SimpleMethodLinks[node];

            for (int i = 0; i < node.GetArgs().Count; i++)
            {
                PExp arg = (PExp)node.GetArgs()[i];
                currentLocal = null;
                arg.Apply(this);
                if (currentLocal != null && target.GetFormals().Cast <AALocalDecl>().ToList()[i].GetRef() != null)
                {
                    ALocalLvalue local = currentLocal;
                    target.Apply(this);
                    if (target.GetFormals().Cast <AALocalDecl>().ToList()[i].GetRef() != null)
                    {
                        AALocalDecl decl = data.LocalLinks[local];
                        NeededRefs[Util.GetAncestor <AMethodDecl>(node)].Add(decl);
                    }
                }
            }
        }
            public override void CaseAVoidReturnStm(AVoidReturnStm node)
            {
                AMethodDecl baseDeconstructor = data.DeconstructorMap[data.StructDeconstructor[baseStruct]];


                ALocalLvalue     structFormalRef    = new ALocalLvalue(new TIdentifier("currentStruct"));
                ALvalueExp       structFormalRefExp = new ALvalueExp(structFormalRef);
                ASimpleInvokeExp invoke             = new ASimpleInvokeExp(new TIdentifier("baseDeconstructor"),
                                                                           new ArrayList()
                {
                    structFormalRefExp
                });
                AABlock block = (AABlock)node.Parent();

                block.GetStatements().Insert(block.GetStatements().IndexOf(node), new AExpStm(new TSemicolon(";"), invoke));

                data.LocalLinks[structFormalRef]  = structFormal;
                data.SimpleMethodLinks[invoke]    = baseDeconstructor;
                data.LvalueTypes[structFormalRef] = data.ExpTypes[structFormalRefExp] = structFormal.GetType();
                data.ExpTypes[invoke]             = baseDeconstructor.GetReturnType();
            }
        public override void CaseAConstructorDecl(AConstructorDecl node)
        {
            AStructDecl     str        = Util.GetAncestor <AStructDecl>(node);
            AEnrichmentDecl enrichment = Util.GetAncestor <AEnrichmentDecl>(node);
            AMethodDecl     replacer   = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null, new AVoidType(new TVoid("void")),
                                                         node.GetName(), new ArrayList(), node.GetBlock());

            replacer.GetName().Text += "_Constructor";
            while (node.GetFormals().Count > 0)
            {
                replacer.GetFormals().Add(node.GetFormals()[0]);
            }

            //Move the method outside the struct
            AASourceFile file = Util.GetAncestor <AASourceFile>(node);

            if (str != null)
            {
                str.RemoveChild(node.Parent());
            }
            else
            {
                enrichment.RemoveChild(node);
            }
            int i = file.GetDecl().IndexOf(str ?? (PDecl)enrichment);

            file.GetDecl().Insert(i /* + 1*/, replacer);
            //Add the struct as a parameter
            PType type;

            if (str != null)
            {
                ANamedType structType = new ANamedType(new TIdentifier(str.GetName().Text), null);
                finalTrans.data.StructTypeLinks[structType] = str;
                type = structType;
            }
            else
            {
                type = Util.MakeClone(enrichment.GetType(), finalTrans.data);
            }
            finalTrans.data.ConstructorMap[node] = replacer;
            structFormal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new APointerType(new TStar("*"), type), new TIdentifier("currentStruct", replacer.GetName().Line, replacer.GetName().Pos), null);
            replacer.GetFormals().Add(structFormal);
            finalTrans.data.Methods.Add(new SharedData.DeclItem <AMethodDecl>(file, replacer));

            //Add return stm
            replacer.SetReturnType(new APointerType(new TStar("*"), Util.MakeClone(type, data)));
            replacer.Apply(new TransformConstructorReturns(structFormal, data));

            //Insert call to base constructor););
            if (finalTrans.data.ConstructorBaseLinks.ContainsKey(node))
            {
                AMethodDecl      baseConstructor = finalTrans.data.ConstructorMap[finalTrans.data.ConstructorBaseLinks[node]];
                ASimpleInvokeExp invoke          = new ASimpleInvokeExp(new TIdentifier(baseConstructor.GetName().Text), new ArrayList());
                while (node.GetBaseArgs().Count > 0)
                {
                    invoke.GetArgs().Add(node.GetBaseArgs()[0]);
                }
                AThisLvalue thisLvalue1 = new AThisLvalue(new TThis("this"));
                ALvalueExp  thisExp1    = new ALvalueExp(thisLvalue1);
                invoke.GetArgs().Add(thisExp1);

                AThisLvalue thisLvalue2 = new AThisLvalue(new TThis("this"));

                AAssignmentExp assignExp = new AAssignmentExp(new TAssign("="), thisLvalue2, invoke);

                ANamedType structType = new ANamedType(new TIdentifier(str.GetName().Text), null);
                finalTrans.data.StructTypeLinks[structType] = str;

                finalTrans.data.LvalueTypes[thisLvalue1]         =
                    finalTrans.data.LvalueTypes[thisLvalue2]     =
                        finalTrans.data.ExpTypes[thisExp1]       =
                            finalTrans.data.ExpTypes[assignExp]  =
                                finalTrans.data.ExpTypes[invoke] = new APointerType(new TStar("*"), structType);

                //finalTrans.data.ExpTypes[invoke] = new AVoidType(new TVoid("void"));
                finalTrans.data.SimpleMethodLinks[invoke] = baseConstructor;

                ((AABlock)replacer.GetBlock()).GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), assignExp));

                //Inline if base and current are two different kinds of pointer types (int/string)
                AStructDecl      baseStruct = null;
                AConstructorDecl baseC      = finalTrans.data.ConstructorBaseLinks[node];
                foreach (KeyValuePair <AStructDecl, List <AConstructorDecl> > pair in finalTrans.data.StructConstructors)
                {
                    bool found = false;
                    foreach (AConstructorDecl decl in pair.Value)
                    {
                        if (baseC == decl)
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found)
                    {
                        baseStruct = pair.Key;
                        break;
                    }
                }
                if ((str.GetIntDim() == null) != (baseStruct.GetIntDim() == null))
                {
                    //For the inilining, change the type to the type of the caller
                    AALocalDecl lastFormal = baseConstructor.GetFormals().OfType <AALocalDecl>().Last();
                    lastFormal.SetRef(new TRef("ref"));
                    APointerType oldType = (APointerType)lastFormal.GetType();

                    structType = new ANamedType(new TIdentifier(str.GetName().Text), null);
                    finalTrans.data.StructTypeLinks[structType] = str;

                    APointerType newType = new APointerType(new TStar("*"), structType);
                    lastFormal.SetType(newType);

                    foreach (
                        ALocalLvalue lvalue in
                        data.LocalLinks.Where(pair => pair.Value == lastFormal).Select(pair => pair.Key))
                    {
                        data.LvalueTypes[lvalue] = newType;
                        if (lvalue.Parent() is ALvalueExp)
                        {
                            data.ExpTypes[(PExp)lvalue.Parent()] = newType;
                            if (lvalue.Parent().Parent() is APointerLvalue)
                            {
                                data.LvalueTypes[(PLvalue)lvalue.Parent().Parent()] = newType.GetType();
                            }
                        }
                    }

                    FixInlineMethods.Inline(invoke, finalTrans);
                    lastFormal.SetRef(null);
                    foreach (
                        ALocalLvalue lvalue in
                        data.LocalLinks.Where(pair => pair.Value == lastFormal).Select(pair => pair.Key))
                    {
                        data.LvalueTypes[lvalue] = oldType;
                        if (lvalue.Parent() is ALvalueExp)
                        {
                            data.ExpTypes[(PExp)lvalue.Parent()] = oldType;
                            if (lvalue.Parent().Parent() is APointerLvalue)
                            {
                                data.LvalueTypes[(PLvalue)lvalue.Parent().Parent()] = oldType.GetType();
                            }
                        }
                    }

                    lastFormal.SetType(oldType);
                }

                //Inline it instead, Since the pointer implementations might not be the same (int vs string)

                /*AMethodDecl baseConstructor = finalTrans.data.ConstructorMap[finalTrans.data.ConstructorBaseLinks[node]];
                 *
                 * AABlock localsBlock = new AABlock(new ArrayList(), new TRBrace("}"));
                 * ABlockStm cloneBlock = new ABlockStm(new TLBrace("{"), (PBlock) baseConstructor.GetBlock().Clone());
                 * Dictionary<AALocalDecl, PLvalue> localMap = new Dictionary<AALocalDecl, PLvalue>();
                 * for (int argNr = 0; argNr < baseConstructor.GetFormals().Count; argNr++)
                 * {
                 *  AALocalDecl formal = (AALocalDecl) baseConstructor.GetFormals()[i];
                 *  PExp arg;
                 *  if (i < baseConstructor.GetFormals().Count - 1)
                 *      arg = (PExp)node.GetBaseArgs()[i];
                 *  else
                 *  {
                 *      AThisLvalue thisLvalue = new AThisLvalue(new TThis("this"));
                 *      ALvalueExp thisExp = new ALvalueExp(thisLvalue);
                 *
                 *      ANamedType structType = new ANamedType(new TIdentifier(str.GetName().Text), null);
                 *      finalTrans.data.StructTypeLinks[structType] = str;
                 *
                 *      finalTrans.data.LvalueTypes[thisLvalue] =
                 *          finalTrans.data.ExpTypes[thisExp] = new APointerType(new TStar("*"), structType);
                 *
                 *      arg = thisExp;
                 *  }
                 *
                 *  if (formal.GetRef() != null || formal.GetOut() != null)
                 *  {
                 *      //Use same variable
                 *      localMap[formal] = ((ALvalueExp) arg).GetLvalue();
                 *  }
                 *  else
                 *  {
                 *      //Make a new variable
                 *      AALocalDecl newLocal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                 *                                             Util.MakeClone(formal.GetType(), finalTrans.data),
                 *                                             new TIdentifier(formal.GetName().Text),
                 *                                             Util.MakeClone(arg, data));
                 *
                 *      ALocalLvalue newLocalRef = new ALocalLvalue(new TIdentifier(newLocal.GetName().Text));
                 *
                 *      localMap[formal] = newLocalRef;
                 *      data.LvalueTypes[newLocalRef] = newLocal.GetType();
                 *      data.LocalLinks[newLocalRef] = newLocal;
                 *
                 *      localsBlock.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), newLocal));
                 *  }
                 *
                 * }
                 *
                 * CloneMethod cloner = new CloneMethod(finalTrans.data, localMap, cloneBlock);
                 * baseConstructor.GetBlock().Apply(cloner);
                 *
                 * ((AABlock)cloneBlock.GetBlock()).GetStatements().Insert(0, new ABlockStm(new TLBrace("{"), localsBlock));
                 * ((AABlock)node.GetBlock()).GetStatements().Insert(0, cloneBlock);*/
            }

            //Fix refferences to other struct stuff);
            base.CaseAMethodDecl(replacer);

            //Add functionality to refference the current struct in a constructor
            //Want to do it as a pointer type, since the constructer can only be called for pointer types
        }
예제 #17
0
        private AMethodDecl GetStringPointerMethod()
        {
            if (GetStringPointerPartMethod != null)
            {
                return(GetStringPointerPartMethod);
            }

            /*
             *  string GetPointerPart(string delegate)
             *  {
             *      return StringSub(delegate, StringFind(delegate, ":", false) + 1, StringLength(delegate));
             *  }
             */

            AASourceFile sourceFile     = Util.GetAncestor <AASourceFile>(finalTrans.mainEntry);
            AALocalDecl  delegateFormal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                          new ANamedType(new TIdentifier("string"), null),
                                                          new TIdentifier("delegate"), null);
            ALocalLvalue delegateRef1    = new ALocalLvalue(new TIdentifier("delegate"));
            ALocalLvalue delegateRef2    = new ALocalLvalue(new TIdentifier("delegate"));
            ALocalLvalue delegateRef3    = new ALocalLvalue(new TIdentifier("delegate"));
            ALvalueExp   delegateRef1Exp = new ALvalueExp(delegateRef1);
            ALvalueExp   delegateRef2Exp = new ALvalueExp(delegateRef2);
            ALvalueExp   delegateRef3Exp = new ALvalueExp(delegateRef3);

            AStringConstExp  stringConst      = new AStringConstExp(new TStringLiteral("\":\""));
            ABooleanConstExp booleanConst     = new ABooleanConstExp(new AFalseBool());
            ASimpleInvokeExp stringFindInvoke = new ASimpleInvokeExp(new TIdentifier("StringFind"), new ArrayList()
            {
                delegateRef2Exp, stringConst, booleanConst
            });
            AIntConstExp intConst = new AIntConstExp(new TIntegerLiteral("1"));
            ABinopExp    binop    = new ABinopExp(stringFindInvoke, new APlusBinop(new TPlus("+")), intConst);

            ASimpleInvokeExp stringLengthInvoke = new ASimpleInvokeExp(new TIdentifier("StringLength"), new ArrayList()
            {
                delegateRef3Exp
            });

            ASimpleInvokeExp stringSubInvoke = new ASimpleInvokeExp(new TIdentifier("StringSub"), new ArrayList()
            {
                delegateRef1Exp, binop, stringLengthInvoke
            });

            GetStringPointerPartMethod = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null,
                                                         new ANamedType(new TIdentifier("string"), null),
                                                         new TIdentifier("GetPointerPart", finalTrans.data.LineCounts[sourceFile] + 1, 1), new ArrayList()
            {
                delegateFormal
            },
                                                         new AABlock(
                                                             new ArrayList()
            {
                new AValueReturnStm(new TReturn("return"), stringSubInvoke)
            },
                                                             new TRBrace("}")));
            sourceFile.GetDecl().Add(GetStringPointerPartMethod);
            data.Methods.Add(new SharedData.DeclItem <AMethodDecl>(sourceFile, GetStringPointerPartMethod));

            finalTrans.data.LocalLinks[delegateRef1]                              =
                finalTrans.data.LocalLinks[delegateRef2]                          =
                    finalTrans.data.LocalLinks[delegateRef3]                      = delegateFormal;
            finalTrans.data.LvalueTypes[delegateRef1]                             =
                finalTrans.data.LvalueTypes[delegateRef2]                         =
                    finalTrans.data.LvalueTypes[delegateRef3]                     =
                        finalTrans.data.ExpTypes[delegateRef1Exp]                 =
                            finalTrans.data.ExpTypes[delegateRef2Exp]             =
                                finalTrans.data.ExpTypes[delegateRef3Exp]         =
                                    finalTrans.data.ExpTypes[stringConst]         =
                                        finalTrans.data.ExpTypes[stringSubInvoke] = new ANamedType(new TIdentifier("string"), null);
            finalTrans.data.ExpTypes[booleanConst]                   = new ANamedType(new TIdentifier("bool"), null);
            finalTrans.data.ExpTypes[intConst]                       =
                finalTrans.data.ExpTypes[binop]                      =
                    finalTrans.data.ExpTypes[stringFindInvoke]       =
                        finalTrans.data.ExpTypes[stringLengthInvoke] = new ANamedType(new TIdentifier("int"), null);


            finalTrans.data.SimpleMethodLinks[stringFindInvoke] =
                finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringFindInvoke.GetName().Text);
            finalTrans.data.SimpleMethodLinks[stringLengthInvoke] =
                finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringLengthInvoke.GetName().Text);
            finalTrans.data.SimpleMethodLinks[stringSubInvoke] =
                finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringSubInvoke.GetName().Text);

            return(GetStringPointerPartMethod);
        }
예제 #18
0
        private List <PStm> AssignDefault(PLvalue lvalue)
        {
            List <PStm> returner  = new List <PStm>();
            PType       type      = data.LvalueTypes[lvalue];
            PExp        rightSide = null;

            if (type is ANamedType)
            {
                ANamedType aType = (ANamedType)type;
                if (aType.IsPrimitive("string"))//aType.GetName().Text == "string")
                {
                    rightSide = new AStringConstExp(new TStringLiteral("\"\""));
                    data.ExpTypes[rightSide] = new ANamedType(new TIdentifier("string"), null);
                }
                else if (aType.IsPrimitive(GalaxyKeywords.NullablePrimitives.words)) //GalaxyKeywords.NullablePrimitives.words.Contains(aType.GetName().Text))
                {
                    rightSide = new ANullExp();
                    data.ExpTypes[rightSide] = new ANamedType(new TIdentifier("null"), null);
                }
                else if (aType.IsPrimitive(new [] { "int", "byte", "fixed" }))

                /*aType.GetName().Text == "int" ||
                *  aType.GetName().Text == "byte" ||
                *  aType.GetName().Text == "fixed")*/
                {
                    rightSide = new AIntConstExp(new TIntegerLiteral("0"));
                    data.ExpTypes[rightSide] = type;
                }
                else if (aType.IsPrimitive("bool"))//aType.GetName().Text == "bool")
                {
                    rightSide = new ABooleanConstExp(new AFalseBool());
                    data.ExpTypes[rightSide] = type;
                }
                else if (aType.IsPrimitive("color"))//aType.GetName().Text == "color")
                {
                    PExp             arg1   = new AIntConstExp(new TIntegerLiteral("0"));
                    PExp             arg2   = new AIntConstExp(new TIntegerLiteral("0"));
                    PExp             arg3   = new AIntConstExp(new TIntegerLiteral("0"));
                    ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("Color"), new ArrayList()
                    {
                        arg1, arg2, arg3
                    });
                    rightSide = invoke;
                    data.ExpTypes[rightSide]       = type;
                    data.ExpTypes[arg1]            =
                        data.ExpTypes[arg2]        =
                            data.ExpTypes[arg3]    = new ANamedType(new TIdentifier("int"), null);
                    data.SimpleMethodLinks[invoke] =
                        data.Libraries.Methods.First(func => func.GetName().Text == invoke.GetName().Text);
                }
                else if (aType.IsPrimitive("char"))//aType.GetName().Text == "char")
                {
                    //Dunno?!
                    rightSide = new ACharConstExp(new TCharLiteral("'\0'"));
                    data.ExpTypes[rightSide] = type;
                }
                else //Struct
                {
                    AStructDecl str = data.StructTypeLinks[aType];
                    foreach (AALocalDecl localDecl in str.GetLocals())
                    {
                        ALvalueExp    reciever  = new ALvalueExp(Util.MakeClone(lvalue, data));
                        AStructLvalue newLvalue = new AStructLvalue(reciever, new ADotDotType(new TDot(".")), new TIdentifier(localDecl.GetName().Text));
                        data.StructFieldLinks[newLvalue] = localDecl;
                        data.ExpTypes[reciever]          = type;
                        data.LvalueTypes[newLvalue]      = localDecl.GetType();
                        returner.AddRange(AssignDefault(newLvalue));
                    }
                    return(returner);
                }
                AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), Util.MakeClone(lvalue, data), rightSide);
                data.ExpTypes[assignment] = type;
                return(new List <PStm>()
                {
                    new AExpStm(new TSemicolon(";"), assignment)
                });
            }
            if (type is AArrayTempType)
            {
                AArrayTempType aType = (AArrayTempType)type;
                for (int i = 0; i < int.Parse(aType.GetIntDim().Text); i++)
                {
                    ALvalueExp   reciever  = new ALvalueExp(Util.MakeClone(lvalue, data));
                    AArrayLvalue newLvalue = new AArrayLvalue(new TLBracket("["), reciever, new AIntConstExp(new TIntegerLiteral(i.ToString())));
                    data.ExpTypes[reciever]             = type;
                    data.LvalueTypes[newLvalue]         = aType.GetType();
                    data.ExpTypes[newLvalue.GetIndex()] = new ANamedType(new TIdentifier("int"), null);
                    returner.AddRange(AssignDefault(newLvalue));
                }
                return(returner);
            }

            throw new Exception("Unexpected type. (LivenessAnalasys.AssignDefault), got " + type);
        }
        public override void CaseASimpleInvokeExp(ASimpleInvokeExp node)
        {
            AMethodDecl decl = finalTrans.data.SimpleMethodLinks[node];

            if (structMethods.Contains(decl))
            {     //The target is a struct method that has been moved out
                if (node.GetArgs().Count < decl.GetFormals().Count&& Util.HasAncestor <AStructDecl>(node))
                { //If this is the case, we Must be inside the same struct as the target. - Not with enheritance
                    ALocalLvalue local = new ALocalLvalue(new TIdentifier("tempName"));
                    ALvalueExp   exp   = new ALvalueExp(local);
                    finalTrans.data.LvalueTypes[local] =
                        finalTrans.data.ExpTypes[exp]  = structFormal.GetType();
                    finalTrans.data.LocalLinks[local]  = structFormal;

                    //If we're calling from class to struct, we must depointer it
                    AStructDecl currentStruct =
                        finalTrans.data.StructMethods.First(
                            pair => pair.Value.Contains(Util.GetAncestor <AMethodDecl>(node))).Key;
                    AStructDecl baseStruct =
                        finalTrans.data.StructMethods.First(
                            pair => pair.Value.Contains(decl)).Key;
                    if (currentStruct.GetClassToken() != baseStruct.GetClassToken()) //It's not possible to call from struct to class
                    {
                        APointerLvalue pointerLvalue = new APointerLvalue(new TStar("*"), exp);
                        exp = new ALvalueExp(pointerLvalue);

                        finalTrans.data.LvalueTypes[pointerLvalue] =
                            finalTrans.data.ExpTypes[exp]          = ((APointerType)structFormal.GetType()).GetType();
                    }

                    node.GetArgs().Add(exp);
                }
            }
            else if (Util.GetAncestor <AStructDecl>(decl) != null && OldParentStruct.ContainsKey(Util.GetAncestor <AMethodDecl>(node)))
            {     //The target is a struct method that hasn't been moved out
                if (Util.GetAncestor <AStructDecl>(decl) == OldParentStruct[Util.GetAncestor <AMethodDecl>(node)] && decl.GetStatic() == null)
                { //We have an internal struct call. Expect to have one too many args
                    if (node.GetArgs().Count == decl.GetFormals().Count)
                    {
                        ALocalLvalue local = new ALocalLvalue(new TIdentifier("tempName"));
                        ALvalueExp   exp   = new ALvalueExp(local);
                        finalTrans.data.LvalueTypes[local] =
                            finalTrans.data.ExpTypes[exp]  = structFormal.GetType();
                        finalTrans.data.LocalLinks[local]  = structFormal;

                        //If we're calling from class to struct, we must depointer it
                        AStructDecl currentStruct =
                            finalTrans.data.StructMethods.First(
                                pair => pair.Value.Contains(Util.GetAncestor <AMethodDecl>(node))).Key;
                        AStructDecl baseStruct =
                            finalTrans.data.StructMethods.First(
                                pair => pair.Value.Contains(decl)).Key;
                        if (currentStruct.GetClassToken() != baseStruct.GetClassToken()) //It's not possible to call from struct to class
                        {
                            APointerLvalue pointerLvalue = new APointerLvalue(new TStar("*"), exp);
                            exp = new ALvalueExp(pointerLvalue);

                            finalTrans.data.LvalueTypes[pointerLvalue] =
                                finalTrans.data.ExpTypes[exp]          = ((APointerType)structFormal.GetType()).GetType();
                        }

                        node.GetArgs().Add(exp);
                    }
                }
            }
            base.CaseASimpleInvokeExp(node);
        }
예제 #20
0
        public override void CaseADelegateInvokeExp(ADelegateInvokeExp node)
        {
            //Build a list of the possible methods
            AASourceFile       currentFile    = Util.GetAncestor <AASourceFile>(node);
            List <AMethodDecl> methods        = new List <AMethodDecl>();
            ANamedType         type           = (ANamedType)finalTrans.data.ExpTypes[node.GetReceiver()];
            AMethodDecl        delegateMethod = finalTrans.data.DelegateTypeLinks[type];

            foreach (KeyValuePair <ADelegateExp, AMethodDecl> delegateCreationPair in finalTrans.data.DelegateCreationMethod)
            {
                if (TypeChecking.Assignable(delegateCreationPair.Key.GetType(), type))
                {
                    if (!methods.Contains(delegateCreationPair.Value))
                    {
                        methods.Add(delegateCreationPair.Value);
                    }
                }
            }
            MoveMethodDeclsOut mover;

            if (methods.Count == 0)
            {
                //Can only remove it if the return value is unused
                if (!(node.Parent() is AExpStm))
                {
                    finalTrans.errors.Add(new ErrorCollection.Error(node.GetToken(),
                                                                    currentFile,
                                                                    "No possible methods found for delegate invoke."));
                    throw new ParserException(node.GetToken(), "Delegates.OutADelegateInvokeExp");
                }

                mover = new MoveMethodDeclsOut("delegateVar", finalTrans.data);
                foreach (Node arg in node.GetArgs())
                {
                    arg.Apply(mover);
                }
                node.Parent().Parent().RemoveChild(node.Parent());
                foreach (PStm stm in mover.NewStatements)
                {
                    stm.Apply(this);
                }
                return;
            }
            if (methods.Count == 1)
            {
                ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("renameMe"), new ArrayList());
                while (node.GetArgs().Count > 0)
                {
                    invoke.GetArgs().Add(node.GetArgs()[0]);
                }

                //If we have a struct method, add the pointer from the delegate
                if (finalTrans.data.StructMethods.Any(str => str.Value.Contains(methods[0])))
                {
                    AStructDecl      targetStr        = finalTrans.data.StructMethods.First(str => str.Value.Contains(methods[0])).Key;
                    AMethodDecl      getPointerDecl   = GetPointerMethod(targetStr.GetDimention() != null);
                    ASimpleInvokeExp getPointerInvoke = new ASimpleInvokeExp(new TIdentifier("renameMe"), new ArrayList()
                    {
                        node.GetReceiver()
                    });
                    invoke.GetArgs().Add(getPointerInvoke);

                    finalTrans.data.SimpleMethodLinks[getPointerInvoke] = getPointerDecl;
                    finalTrans.data.ExpTypes[getPointerInvoke]          = getPointerDecl.GetReturnType();
                }

                finalTrans.data.SimpleMethodLinks[invoke] = methods[0];
                finalTrans.data.ExpTypes[invoke]          = methods[0].GetReturnType();
                node.ReplaceBy(invoke);
                return;
            }
            //Multiple methods. Make

            /*
             * <Methods moved out from reciever>
             * string delegate = GetMethodPart(<reciever>);
             * if (delegate == "...")
             * {
             *    Foo(...);
             * }
             * else if (delegate == "...")
             * {
             *    Bar(..., GetPointerPart(<reciever>);
             * }
             * else if(...)
             * ...
             * else
             * {
             *     UIDisplayMessage(PlayerGroupAll(), c_messageAreaDebug, StringToText("[<file>:<line>]: No methods matched delegate."));
             *     int i = 1/0;
             *     return;
             * }
             *
             */
            AABlock block = new AABlock(new ArrayList(), new TRBrace("}"));

            mover = new MoveMethodDeclsOut("delegateVar", finalTrans.data);
            node.GetReceiver().Apply(mover);
            AMethodDecl      methodPartMethod = GetMethodMethod();
            ASimpleInvokeExp methodPartInvoke = new ASimpleInvokeExp(new TIdentifier("GetMethodPart"),
                                                                     new ArrayList()
            {
                Util.MakeClone(node.GetReceiver(),
                               finalTrans.data)
            });

            finalTrans.data.SimpleMethodLinks[methodPartInvoke] = methodPartMethod;
            finalTrans.data.ExpTypes[methodPartInvoke]          = methodPartMethod.GetReturnType();
            AALocalDecl methodPartDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                         new ANamedType(new TIdentifier("string"), null),
                                                         new TIdentifier("methodPart"), methodPartInvoke);

            block.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), methodPartDecl));
            //If the invoke's return value is used, get the lvalue
            PLvalue leftSide;

            if (node.Parent() is AALocalDecl)
            {
                leftSide = new ALocalLvalue(new TIdentifier("renameMe"));
                finalTrans.data.LocalLinks[(ALocalLvalue)leftSide] = (AALocalDecl)node.Parent();
                finalTrans.data.LvalueTypes[leftSide] = new ANamedType(new TIdentifier("string"), null);
                PStm    pStm   = Util.GetAncestor <PStm>(node);
                AABlock pBlock = (AABlock)pStm.Parent();
                pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm) + 1, new ABlockStm(new TLBrace("{"), block));
                node.Parent().RemoveChild(node);
            }
            else if (node.Parent() is AAssignmentExp)
            {
                AAssignmentExp assignExp = (AAssignmentExp)node.Parent();
                leftSide = assignExp.GetLvalue();
                leftSide.Apply(mover);

                PStm pStm = Util.GetAncestor <PStm>(node);
                pStm.ReplaceBy(new ABlockStm(new TLBrace("{"), block));
            }
            else if (node.Parent() is AExpStm)
            {
                //No assignments needed
                leftSide = null;
                node.Parent().ReplaceBy(new ABlockStm(new TLBrace("{"), block));
            }
            else
            {
                //Create a new local
                AALocalDecl leftSideDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                           Util.MakeClone(delegateMethod.GetReturnType(),
                                                                          finalTrans.data),
                                                           new TIdentifier("delegateVar"), null);
                ALocalLvalue leftSideLink    = new ALocalLvalue(new TIdentifier("delegateVar"));
                ALvalueExp   leftSideLinkExp = new ALvalueExp(leftSideLink);


                PStm    pStm   = Util.GetAncestor <PStm>(node);
                AABlock pBlock = (AABlock)pStm.Parent();
                pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), new ABlockStm(new TLBrace("{"), block));

                node.ReplaceBy(leftSideLinkExp);

                finalTrans.data.LocalLinks[leftSideLink]      = leftSideDecl;
                finalTrans.data.LvalueTypes[leftSideLink]     =
                    finalTrans.data.ExpTypes[leftSideLinkExp] = leftSideDecl.GetType();

                leftSide = leftSideLink;
                block.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), leftSideDecl));
            }

            ABlockStm elseBranch;

            //Make final else branch

            /* {
             *     UIDisplayMessage(PlayerGroupAll(), c_messageAreaDebug, StringToText("<file>[<line>, <pos>]: No methods matched delegate."));
             *     IntToString(1/0);
             *     return;
             * }
             */
            {
                AABlock          innerBlock         = new AABlock(new ArrayList(), new TRBrace("}"));
                ASimpleInvokeExp playerGroupInvoke  = new ASimpleInvokeExp(new TIdentifier("PlayerGroupAll"), new ArrayList());
                AFieldLvalue     messageAreaLink    = new AFieldLvalue(new TIdentifier("c_messageAreaDebug"));
                ALvalueExp       messageAreaLinkExp = new ALvalueExp(messageAreaLink);
                AStringConstExp  stringConst        =
                    new AStringConstExp(
                        new TStringLiteral("\"" + currentFile.GetName().Text.Replace('\\', '/') + "[" +
                                           node.GetToken().Line + ", " + node.GetToken().Pos +
                                           "]: Got a null delegate.\""));
                ASimpleInvokeExp stringToTextInvoke = new ASimpleInvokeExp(new TIdentifier("StringToText"),
                                                                           new ArrayList()
                {
                    stringConst
                });
                ASimpleInvokeExp displayMessageInvoke = new ASimpleInvokeExp(new TIdentifier("UIDisplayMessage"),
                                                                             new ArrayList()
                {
                    playerGroupInvoke,
                    messageAreaLinkExp,
                    stringToTextInvoke
                });

                AIntConstExp     intConst1         = new AIntConstExp(new TIntegerLiteral("1"));
                AIntConstExp     intConst2         = new AIntConstExp(new TIntegerLiteral("0"));
                ABinopExp        binop             = new ABinopExp(intConst1, new ADivideBinop(new TDiv("/")), intConst2);
                ASimpleInvokeExp intToStringInvoke = new ASimpleInvokeExp(new TIdentifier("IntToString"),
                                                                          new ArrayList()
                {
                    binop
                });

                innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), displayMessageInvoke));
                innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), intToStringInvoke));
                //innerBlock.GetStatements().Add(new AVoidReturnStm(new TReturn("return")));

                elseBranch = new ABlockStm(new TLBrace("{"), innerBlock);


                finalTrans.data.SimpleMethodLinks[playerGroupInvoke] =
                    finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == playerGroupInvoke.GetName().Text);
                finalTrans.data.SimpleMethodLinks[stringToTextInvoke] =
                    finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringToTextInvoke.GetName().Text);
                finalTrans.data.SimpleMethodLinks[displayMessageInvoke] =
                    finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == displayMessageInvoke.GetName().Text);
                finalTrans.data.SimpleMethodLinks[intToStringInvoke] =
                    finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == intToStringInvoke.GetName().Text);
                finalTrans.data.FieldLinks[messageAreaLink] =
                    finalTrans.data.Libraries.Fields.First(m => m.GetName().Text == messageAreaLink.GetName().Text);

                finalTrans.data.ExpTypes[playerGroupInvoke] =
                    finalTrans.data.SimpleMethodLinks[playerGroupInvoke].GetReturnType();
                finalTrans.data.LvalueTypes[messageAreaLink]     =
                    finalTrans.data.ExpTypes[messageAreaLinkExp] =
                        finalTrans.data.FieldLinks[messageAreaLink].GetType();
                finalTrans.data.ExpTypes[stringToTextInvoke] =
                    finalTrans.data.SimpleMethodLinks[stringToTextInvoke].GetReturnType();
                finalTrans.data.ExpTypes[stringConst]           =
                    finalTrans.data.ExpTypes[intToStringInvoke] = new ANamedType(new TIdentifier("string"), null);
                finalTrans.data.ExpTypes[displayMessageInvoke]  = new AVoidType();

                finalTrans.data.ExpTypes[intConst1]     =
                    finalTrans.data.ExpTypes[intConst2] =
                        finalTrans.data.ExpTypes[binop] = new ANamedType(new TIdentifier("int"), null);
            }

            foreach (AMethodDecl method in methods)
            {
                /*  * if (delegate == "...")
                 * {
                 *    Foo(...);
                 * }
                 * else if (delegate == "...")
                 * {
                 *    Bar(..., GetPointerPart(<reciever>);
                 * }
                 * else if(...)
                 * ...
                 */
                AABlock          innerBlock = new AABlock(new ArrayList(), new TRBrace("}"));
                ASimpleInvokeExp invoke     = new ASimpleInvokeExp(new TIdentifier(method.GetName().Text), new ArrayList());
                for (int i = 0; i < node.GetArgs().Count; i++)
                {
                    PExp arg = (PExp)node.GetArgs()[i];
                    invoke.GetArgs().Add(Util.MakeClone(arg, finalTrans.data));
                }
                //If we have a struct method, add the pointer from the delegate
                if (finalTrans.data.StructMethods.Any(str => str.Value.Contains(method)))
                {
                    AStructDecl      targetStr        = finalTrans.data.StructMethods.First(str => str.Value.Contains(method)).Key;
                    AMethodDecl      getPointerDecl   = GetPointerMethod(targetStr.GetDimention() != null);
                    ASimpleInvokeExp getPointerInvoke = new ASimpleInvokeExp(new TIdentifier("renameMe"), new ArrayList()
                    {
                        Util.MakeClone(node.GetReceiver(), data)
                    });
                    invoke.GetArgs().Add(getPointerInvoke);

                    finalTrans.data.SimpleMethodLinks[getPointerInvoke] = getPointerDecl;
                    finalTrans.data.ExpTypes[getPointerInvoke]          = getPointerDecl.GetReturnType();
                }

                finalTrans.data.SimpleMethodLinks[invoke] = method;
                finalTrans.data.ExpTypes[invoke]          = method.GetReturnType();

                if (leftSide == null)
                {
                    innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), invoke));
                }
                else
                {
                    AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), Util.MakeClone(leftSide, finalTrans.data), invoke);
                    finalTrans.data.ExpTypes[assignment] = finalTrans.data.ExpTypes[invoke];
                    innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment));
                }
                ALocalLvalue    methodPartLink    = new ALocalLvalue(new TIdentifier("methodPart"));
                ALvalueExp      methodPartLinkExp = new ALvalueExp(methodPartLink);
                AStringConstExp stringConst       = new AStringConstExp(new TStringLiteral("\"" + GetName(method) + "\""));
                finalTrans.data.LocalLinks[methodPartLink]      = methodPartDecl;
                finalTrans.data.LvalueTypes[methodPartLink]     =
                    finalTrans.data.ExpTypes[methodPartLinkExp] =
                        finalTrans.data.ExpTypes[stringConst]   = new ANamedType(new TIdentifier("string"), null);


                ABinopExp binop = new ABinopExp(methodPartLinkExp, new AEqBinop(new TEq("==")), stringConst);
                finalTrans.data.ExpTypes[binop] = new ANamedType(new TIdentifier("bool"), null);

                AIfThenElseStm ifThenElse = new AIfThenElseStm(new TLParen("("), binop, new ABlockStm(new TLBrace("{"), innerBlock), elseBranch);

                elseBranch = new ABlockStm(new TLBrace("{"), new AABlock(new ArrayList()
                {
                    ifThenElse
                }, new TRBrace("}")));
            }

            block.GetStatements().Add(elseBranch);
        }
예제 #21
0
 public CloneBeforeContinue(ASimpleInvokeExp node, AALocalDecl replaceVarDecl, SharedData data)
 {
     this.node           = node;
     this.replaceVarDecl = replaceVarDecl;
     this.data           = data;
 }
예제 #22
0
            public override void CaseASimpleInvokeExp(ASimpleInvokeExp node)
            {
                Item currentFile = GetIncludeItem(node);

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

                AMethodDecl decl     = finalTrans.data.SimpleMethodLinks[node];
                Item        declItem = ((Item)allItems.OfType <MethodDeclItem>().FirstOrDefault(item => item.RealDecl == decl)) ??
                                       allItems.OfType <IncludeItem>().First(
                    item => item.Current == Util.GetAncestor <AASourceFile>(decl));

                List <Item> cPath = currentFile.Path;
                List <Item> dPath = declItem.Path;

                for (int i = 0; i < Math.Min(cPath.Count, dPath.Count); i++)
                {
                    Item cItem = cPath[i];
                    Item dItem = dPath[i];

                    if (cItem != dItem)
                    {//We have a fork in the decls. make sure that the decl is before the used
                        int cI = cPath[i - 1].Children.IndexOf(cItem);
                        int dI = dPath[i - 1].Children.IndexOf(dItem);
                        if (dI < cI)
                        {
                            break;
                        }
                        //Move the decl up before the used
                        if (!(declItem is MethodDeclItem))
                        {
                            declItem = new MethodDeclItem(decl, cPath[i - 1], new List <Item>());
                            allItems.Add(declItem);
                        }
                        else
                        {
                            declItem.Parent.Children.Remove(declItem);
                            declItem.Parent = cPath[i - 1];
                        }
                        cPath[i - 1].Children.Insert(cI, declItem);
                        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 MethodDeclItem(decl, cPath[i], new List <Item>());
                            allItems.Add(declItem);
                            cPath[i].Children.Add(declItem);
                            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 MethodDeclItem(decl, cPath[i], new List <Item>());
                        allItems.Add(declItem);
                        cPath[i].Children.Insert(cI, declItem);
                        break;
                    }
                }

                base.CaseASimpleInvokeExp(node);
            }
예제 #23
0
        public override void DefaultIn(Node node)
        {
            if (!canMerge)
            {
                return;
            }
            if (node is AMethodDecl)
            {
                //First node - no need to fetch
                if (((AMethodDecl)node).GetFormals().Count != ((AMethodDecl)otherNode).GetFormals().Count)
                {
                    canMerge = false;
                }
                return;
            }
            //Fetch corrosponding other node
            int index = 0;
            GetChildTypeIndex getChildTypeIndex = new GetChildTypeIndex()
            {
                Parent = node.Parent(), Child = node
            };

            node.Parent().Apply(getChildTypeIndex);
            index = getChildTypeIndex.Index;
            GetChildTypeByIndex getChildTypeByIndex = new GetChildTypeByIndex()
            {
                Child = node, Index = index, Parent = otherNode
            };

            otherNode.Apply(getChildTypeByIndex);
            otherNode = getChildTypeByIndex.Child;

            if (otherNode.GetType() != node.GetType())
            {
                canMerge = false;
                return;
            }

            if (node is AALocalDecl)
            {
                locals.Add((AALocalDecl)node);
                otherLocals.Add((AALocalDecl)otherNode);
                return;
            }

            if (node is ANamedType)
            {
                ANamedType aNode  = (ANamedType)node;
                ANamedType aOther = (ANamedType)otherNode;
                if (data.StructTypeLinks.ContainsKey(aNode) != data.StructTypeLinks.ContainsKey(aOther))
                {
                    canMerge = false;
                    return;
                }
                if (data.StructTypeLinks.ContainsKey(aNode) && data.StructTypeLinks[aNode] != data.StructTypeLinks[aOther])
                {
                    canMerge = false;
                }
                if (!data.StructTypeLinks.ContainsKey(aNode) && aNode.IsSame(aOther, true))//aNode.GetName().Text != aOther.GetName().Text)
                {
                    canMerge = false;
                }
                if (aNode.IsPrimitive() && !aOther.IsPrimitive(aNode.AsIdentifierString()))
                {
                    canMerge = false;
                }
                return;
            }

            if (node is AABlock)
            {
                AABlock aNode  = (AABlock)node;
                AABlock aOther = (AABlock)otherNode;
                if (aNode.GetStatements().Count != aOther.GetStatements().Count)
                {
                    canMerge = false;
                }
                return;
            }

            if (node is AIntConstExp)
            {
                AIntConstExp aNode  = (AIntConstExp)node;
                AIntConstExp aOther = (AIntConstExp)otherNode;
                if (aNode.GetIntegerLiteral().Text != aOther.GetIntegerLiteral().Text)
                {
                    canMerge = false;
                }
                return;
            }

            if (node is AFixedConstExp)
            {
                AFixedConstExp aNode  = (AFixedConstExp)node;
                AFixedConstExp aOther = (AFixedConstExp)otherNode;
                if (aNode.GetFixedLiteral().Text != aOther.GetFixedLiteral().Text)
                {
                    canMerge = false;
                }
                return;
            }

            if (node is AStringConstExp)
            {
                AStringConstExp aNode  = (AStringConstExp)node;
                AStringConstExp aOther = (AStringConstExp)otherNode;
                if (aNode.GetStringLiteral().Text != aOther.GetStringLiteral().Text)
                {
                    canMerge = false;
                }
                return;
            }

            if (node is ACharConstExp)
            {
                ACharConstExp aNode  = (ACharConstExp)node;
                ACharConstExp aOther = (ACharConstExp)otherNode;
                if (aNode.GetCharLiteral().Text != aOther.GetCharLiteral().Text)
                {
                    canMerge = false;
                }
                return;
            }

            if (node is ASimpleInvokeExp)
            {
                ASimpleInvokeExp aNode  = (ASimpleInvokeExp)node;
                ASimpleInvokeExp aOther = (ASimpleInvokeExp)otherNode;
                if (data.SimpleMethodLinks[aNode] != data.SimpleMethodLinks[aOther] &&
                    !(data.SimpleMethodLinks[aNode] == Util.GetAncestor <AMethodDecl>(aNode) &&
                      data.SimpleMethodLinks[aOther] == Util.GetAncestor <AMethodDecl>(aOther)))
                {
                    canMerge = false;
                }
                return;
            }

            if (node is ALocalLvalue)
            {
                ALocalLvalue aNode  = (ALocalLvalue)node;
                ALocalLvalue aOther = (ALocalLvalue)otherNode;
                if (locals.IndexOf(data.LocalLinks[aNode]) != otherLocals.IndexOf(data.LocalLinks[aOther]))
                {
                    canMerge = false;
                }
                return;
            }

            if (node is AFieldLvalue)
            {
                AFieldLvalue aNode  = (AFieldLvalue)node;
                AFieldLvalue aOther = (AFieldLvalue)otherNode;
                if (data.FieldLinks[aNode] != data.FieldLinks[aOther])
                {
                    canMerge = false;
                }
                return;
            }

            if (node is AStructLvalue)
            {
                AStructLvalue aNode  = (AStructLvalue)node;
                AStructLvalue aOther = (AStructLvalue)otherNode;
                if (data.StructFieldLinks[aNode] != data.StructFieldLinks[aOther])
                {
                    canMerge = false;
                }
                return;
            }
        }
예제 #24
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 CaseASimpleInvokeExp(ASimpleInvokeExp node)
 {
     Invokes.Add(node);
 }
예제 #26
0
        private AMethodDecl GetMethodMethod()
        {
            if (GetMethodPartMethod != null)
            {
                return(GetMethodPartMethod);
            }

            /*
             *  string GetMethodPart(string delegate)
             *  {
             *          int i = StringFind(delegate, ":", false);
             *          if (i == -1)
             *          {
             *                  return delegate;
             *          }
             *          return StringSub(delegate, 1, i - 1);
             *  }
             */
            AASourceFile sourceFile = Util.GetAncestor <AASourceFile>(finalTrans.mainEntry);

            AALocalDecl delegateFormal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                         new ANamedType(new TIdentifier("string"), null),
                                                         new TIdentifier("delegate"), null);
            ALocalLvalue delegateRef1    = new ALocalLvalue(new TIdentifier("delegate"));
            ALocalLvalue delegateRef2    = new ALocalLvalue(new TIdentifier("delegate"));
            ALocalLvalue delegateRef3    = new ALocalLvalue(new TIdentifier("delegate"));
            ALvalueExp   delegateRef1Exp = new ALvalueExp(delegateRef1);
            ALvalueExp   delegateRef2Exp = new ALvalueExp(delegateRef2);
            ALvalueExp   delegateRef3Exp = new ALvalueExp(delegateRef3);

            AStringConstExp  stringConst  = new AStringConstExp(new TStringLiteral("\":\""));
            ABooleanConstExp booleanConst = new ABooleanConstExp(new AFalseBool());
            AIntConstExp     intConst1    = new AIntConstExp(new TIntegerLiteral("-1"));
            AIntConstExp     intConst2    = new AIntConstExp(new TIntegerLiteral("1"));
            AIntConstExp     intConst3    = new AIntConstExp(new TIntegerLiteral("1"));

            ASimpleInvokeExp stringFindInvoke = new ASimpleInvokeExp(new TIdentifier("StringFind"),
                                                                     new ArrayList()
            {
                delegateRef1Exp, stringConst, booleanConst
            });
            AALocalDecl iDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("int"), null),
                                                new TIdentifier("i"), stringFindInvoke);
            ALocalLvalue iRef1    = new ALocalLvalue(new TIdentifier("i"));
            ALocalLvalue iRef2    = new ALocalLvalue(new TIdentifier("i"));
            ALvalueExp   iRef1Exp = new ALvalueExp(iRef1);
            ALvalueExp   iRef2Exp = new ALvalueExp(iRef2);

            ABinopExp  binop1 = new ABinopExp(iRef1Exp, new AEqBinop(new TEq("==")), intConst1);
            AIfThenStm ifThen = new AIfThenStm(new TLParen("("), binop1,
                                               new ABlockStm(new TLBrace("{"),
                                                             new AABlock(
                                                                 new ArrayList()
            {
                new AValueReturnStm(new TReturn("return"),
                                    delegateRef2Exp)
            },
                                                                 new TRBrace("}"))));

            ABinopExp        binop2          = new ABinopExp(iRef2Exp, new AMinusBinop(new TMinus("-")), intConst3);
            ASimpleInvokeExp stringSubInvoke = new ASimpleInvokeExp(new TIdentifier("StringSub"),
                                                                    new ArrayList()
            {
                delegateRef3Exp, intConst2, binop2
            });

            GetMethodPartMethod = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null,
                                                  new ANamedType(new TIdentifier("string"), null),
                                                  new TIdentifier("GetMethodPart", finalTrans.data.LineCounts[sourceFile] + 1, 1), new ArrayList()
            {
                delegateFormal
            },
                                                  new AABlock(
                                                      new ArrayList()
            {
                new ALocalDeclStm(new TSemicolon(";"), iDecl),
                ifThen,
                new AValueReturnStm(new TReturn("return"), stringSubInvoke)
            },
                                                      new TRBrace("}")));
            sourceFile.GetDecl().Add(GetMethodPartMethod);

            finalTrans.data.LocalLinks[delegateRef1]         =
                finalTrans.data.LocalLinks[delegateRef2]     =
                    finalTrans.data.LocalLinks[delegateRef3] = delegateFormal;
            finalTrans.data.LocalLinks[iRef1]                                     =
                finalTrans.data.LocalLinks[iRef2]                                 = iDecl;
            finalTrans.data.LvalueTypes[delegateRef1]                             =
                finalTrans.data.LvalueTypes[delegateRef2]                         =
                    finalTrans.data.LvalueTypes[delegateRef3]                     =
                        finalTrans.data.ExpTypes[delegateRef1Exp]                 =
                            finalTrans.data.ExpTypes[delegateRef2Exp]             =
                                finalTrans.data.ExpTypes[delegateRef3Exp]         =
                                    finalTrans.data.ExpTypes[stringConst]         =
                                        finalTrans.data.ExpTypes[stringSubInvoke] = new ANamedType(new TIdentifier("string"), null);
            finalTrans.data.LvalueTypes[iRef1]                                    =
                finalTrans.data.LvalueTypes[iRef2]                                =
                    finalTrans.data.ExpTypes[iRef1Exp]                            =
                        finalTrans.data.ExpTypes[iRef2Exp]                        =
                            finalTrans.data.ExpTypes[stringFindInvoke]            =
                                finalTrans.data.ExpTypes[intConst1]               =
                                    finalTrans.data.ExpTypes[intConst2]           =
                                        finalTrans.data.ExpTypes[intConst3]       =
                                            finalTrans.data.ExpTypes[binop2]      = new ANamedType(new TIdentifier("int"), null);
            finalTrans.data.ExpTypes[booleanConst]                                =
                finalTrans.data.ExpTypes[binop1]                                  = new ANamedType(new TIdentifier("bool"), null);


            finalTrans.data.SimpleMethodLinks[stringFindInvoke] =
                finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringFindInvoke.GetName().Text);
            finalTrans.data.SimpleMethodLinks[stringSubInvoke] =
                finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringSubInvoke.GetName().Text);

            return(GetMethodPartMethod);
        }
예제 #27
0
        public static List <AABlock> Inline(ASimpleInvokeExp node, FinalTransformations finalTrans)
        {
            /*if (Util.GetAncestor<AMethodDecl>(node) != null && Util.GetAncestor<AMethodDecl>(node).GetName().Text == "UIChatFrame_LeaveChannel")
             *  node = node;*/

            SharedData data = finalTrans.data;
            //If this node is inside the condition of a while, replace it with a new local var,
            //make a clone before the while, one before each continue in the while, and one at the end of the while
            //(unless the end is a return or break)
            AABlock pBlock;

            if (Util.HasAncestor <AWhileStm>(node))
            {
                AWhileStm whileStm = Util.GetAncestor <AWhileStm>(node);
                if (Util.IsAncestor(node, whileStm.GetCondition()))
                {
                    List <ASimpleInvokeExp> toInline = new List <ASimpleInvokeExp>();
                    //Above while
                    AALocalDecl replaceVarDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                                 Util.MakeClone(data.ExpTypes[node], data),
                                                                 new TIdentifier("whileVar"), null);
                    ALocalLvalue replaceVarRef    = new ALocalLvalue(new TIdentifier("whileVar"));
                    ALvalueExp   replaceVarRefExp = new ALvalueExp(replaceVarRef);
                    data.LocalLinks[replaceVarRef]  = replaceVarDecl;
                    data.ExpTypes[replaceVarRefExp] = data.LvalueTypes[replaceVarRef] = replaceVarDecl.GetType();
                    node.ReplaceBy(replaceVarRefExp);
                    replaceVarDecl.SetInit(node);
                    pBlock = (AABlock)whileStm.Parent();
                    pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(whileStm), new ALocalDeclStm(new TSemicolon(";"), replaceVarDecl));
                    toInline.Add(node);


                    //In the end of the while
                    PStm lastStm = whileStm.GetBody();
                    while (lastStm is ABlockStm)
                    {
                        AABlock block = (AABlock)((ABlockStm)lastStm).GetBlock();
                        if (block.GetStatements().Count == 0)
                        {
                            lastStm = null;
                            break;
                        }
                        lastStm = (PStm)block.GetStatements()[block.GetStatements().Count - 1];
                    }
                    if (lastStm == null || !(lastStm is AValueReturnStm || lastStm is AVoidReturnStm || lastStm is ABreakStm))
                    {
                        lastStm = whileStm.GetBody();
                        AABlock block;
                        if (lastStm is ABlockStm)
                        {
                            block = (AABlock)((ABlockStm)lastStm).GetBlock();
                        }
                        else
                        {
                            block = new AABlock(new ArrayList(), new TRBrace("}"));
                            block.GetStatements().Add(lastStm);
                            whileStm.SetBody(new ABlockStm(new TLBrace("{"), block));
                        }

                        replaceVarRef = new ALocalLvalue(new TIdentifier("whileVar"));
                        ASimpleInvokeExp clone = (ASimpleInvokeExp)Util.MakeClone(node, data);
                        toInline.Add(clone);
                        AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), replaceVarRef, clone);
                        data.LocalLinks[replaceVarRef] = replaceVarDecl;
                        data.ExpTypes[assignment]      = data.LvalueTypes[replaceVarRef] = replaceVarDecl.GetType();
                        block.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment));
                    }

                    //After each continue
                    CloneBeforeContinue cloner = new CloneBeforeContinue(node, replaceVarDecl, data);
                    whileStm.GetBody().Apply(cloner);
                    toInline.AddRange(cloner.replacementExpressions);
                    List <AABlock> visitBlocks = new List <AABlock>();
                    foreach (ASimpleInvokeExp invoke in toInline)
                    {
                        visitBlocks.AddRange(Inline(invoke, finalTrans));
                    }
                    return(visitBlocks);
                }
            }



            AMethodDecl           decl = finalTrans.data.SimpleMethodLinks[node];
            FindAssignedToFormals assignedToFormalsFinder = new FindAssignedToFormals(finalTrans.data);

            decl.Apply(assignedToFormalsFinder);
            List <AALocalDecl> assignedToFormals = assignedToFormalsFinder.AssignedFormals;


            /*
             * inline int foo(int a)
             * {
             *      int b = 2;
             *      int c;
             *      ...
             *      while(...)
             *      {
             *          ...
             *          break;
             *          ...
             *          return c;
             *      }
             *      ...
             *      return 2;
             * }
             *
             * bar(foo(<arg for a>));
             * ->
             *
             * {
             *      bool inlineMethodReturned = false;
             *      int inlineReturner;
             *      int a = <arg for a>;
             *      while (!inlineMethodReturned)
             *      {
             *          int b = 2;
             *          int c;
             *          ...
             *          while(...)
             *          {
             *              ...
             *              break
             *              ...
             *              inlineReturner = c;
             *              inlineMethodReturned = true;
             *              break;
             *          }
             *          if (inlineMethodReturned)
             *          {
             *              break;
             *          }
             *          ...
             *          inlineReturner = 2;
             *          inlineMethodReturned = true;
             *          break;
             *          break;
             *      }
             *      bar(inlineReturner);
             * }
             *
             *
             */


            AABlock outerBlock = new AABlock();
            PExp    exp        = new ABooleanConstExp(new AFalseBool());

            finalTrans.data.ExpTypes[exp] = new ANamedType(new TIdentifier("bool"), null);
            AALocalDecl hasMethodReturnedVar = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("bool"), null),
                                                               new TIdentifier("hasInlineReturned"), exp);

            finalTrans.data.GeneratedVariables.Add(hasMethodReturnedVar);
            PStm stm = new ALocalDeclStm(new TSemicolon(";"), hasMethodReturnedVar);

            outerBlock.GetStatements().Add(stm);

            AALocalDecl methodReturnerVar = null;

            if (!(decl.GetReturnType() is AVoidType))
            {
                methodReturnerVar = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(decl.GetReturnType(), finalTrans.data),
                                                    new TIdentifier("inlineReturner"), null);
                stm = new ALocalDeclStm(new TSemicolon(";"), methodReturnerVar);
                outerBlock.GetStatements().Add(stm);
            }

            AABlock afterBlock = new AABlock();

            //A dictionary from the formals of the inline method to a cloneable replacement lvalue
            Dictionary <AALocalDecl, PLvalue> Parameters    = new Dictionary <AALocalDecl, PLvalue>();
            Dictionary <AALocalDecl, PExp>    ParameterExps = new Dictionary <AALocalDecl, PExp>();

            for (int i = 0; i < decl.GetFormals().Count; i++)
            {
                AALocalDecl formal = (AALocalDecl)decl.GetFormals()[i];
                PExp        arg    = (PExp)node.GetArgs()[0];
                PLvalue     lvalue;
                //if ref, dont make a new var
                if (formal.GetRef() != null && arg is ALvalueExp)
                {
                    arg.Apply(new MoveMethodDeclsOut("inlineVar", finalTrans.data));
                    arg.Parent().RemoveChild(arg);
                    lvalue = ((ALvalueExp)arg).GetLvalue();
                }
                else if (!assignedToFormals.Contains(formal) && Util.IsLocal(arg, finalTrans.data))
                {
                    lvalue = new ALocalLvalue(new TIdentifier("I hope I dont make it"));
                    finalTrans.data.LvalueTypes[lvalue] = formal.GetType();
                    finalTrans.data.LocalLinks[(ALocalLvalue)lvalue] = formal;
                    ParameterExps[formal] = arg;
                    arg.Parent().RemoveChild(arg);
                }
                else
                {
                    AAssignmentExp assExp = null;
                    if (formal.GetOut() != null)
                    {
                        //Dont initialize with arg, but assign arg after
                        arg.Apply(new MoveMethodDeclsOut("inlineVar", finalTrans.data));
                        lvalue = ((ALvalueExp)arg).GetLvalue();
                        assExp = new AAssignmentExp(new TAssign("="), lvalue, null);
                        finalTrans.data.ExpTypes[assExp] = finalTrans.data.LvalueTypes[lvalue];
                        arg.Parent().RemoveChild(arg);
                        arg = null;
                    }
                    AALocalDecl parameter = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(formal.GetType(), finalTrans.data),
                                                            new TIdentifier(formal.GetName().Text),
                                                            arg);
                    stm = new ALocalDeclStm(new TSemicolon(";"), parameter);
                    outerBlock.GetStatements().Add(stm);

                    lvalue = new ALocalLvalue(new TIdentifier(parameter.GetName().Text));
                    finalTrans.data.LvalueTypes[lvalue] = parameter.GetType();
                    finalTrans.data.LocalLinks[(ALocalLvalue)lvalue] = parameter;


                    if (formal.GetOut() != null)
                    {
                        //Dont initialize with arg, but assign arg after
                        ALvalueExp lvalueExp = new ALvalueExp(Util.MakeClone(lvalue, finalTrans.data));
                        finalTrans.data.ExpTypes[lvalueExp] = finalTrans.data.LvalueTypes[lvalue];
                        assExp.SetExp(lvalueExp);
                        afterBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assExp));
                    }
                }
                Parameters.Add(formal, lvalue);
            }

            AABlock innerBlock = (AABlock)decl.GetBlock().Clone();

            exp = new ABooleanConstExp(new ATrueBool());
            finalTrans.data.ExpTypes[exp] = new ANamedType(new TIdentifier("bool"), null);
            ABlockStm innerBlockStm = new ABlockStm(new TLBrace("{"), innerBlock);

            bool needWhile = CheckIfWhilesIsNeeded.IsWhileNeeded(decl.GetBlock());

            if (needWhile)
            {
                stm = new AWhileStm(new TLParen("("), exp, innerBlockStm);
            }
            else
            {
                stm = innerBlockStm;
            }
            outerBlock.GetStatements().Add(stm);

            outerBlock.GetStatements().Add(new ABlockStm(new TLBrace("{"), afterBlock));

            //Clone method contents to inner block.
            CloneMethod cloneFixer = new CloneMethod(finalTrans, Parameters, ParameterExps, innerBlockStm);

            decl.GetBlock().Apply(cloneFixer);
            foreach (KeyValuePair <PLvalue, PExp> pair in cloneFixer.ReplaceUsAfter)
            {
                PLvalue    lvalue       = pair.Key;
                PExp       replacement  = Util.MakeClone(pair.Value, finalTrans.data);
                ALvalueExp lvalueParent = (ALvalueExp)lvalue.Parent();
                lvalueParent.ReplaceBy(replacement);
            }
            innerBlockStm.Apply(new FixTypes(finalTrans.data));

            innerBlock.Apply(new FixReturnsAndWhiles(hasMethodReturnedVar, methodReturnerVar, finalTrans.data, needWhile));

            GetNonBlockStm stmFinder = new GetNonBlockStm(false);

            innerBlock.Apply(stmFinder);
            if (needWhile && (stmFinder.Stm == null || !(stmFinder.Stm is ABreakStm)))
            {
                innerBlock.GetStatements().Add(new ABreakStm(new TBreak("break")));
            }

            //Insert before current statement
            ABlockStm outerBlockStm = new ABlockStm(new TLBrace("{"), outerBlock);

            PStm pStm = Util.GetAncestor <PStm>(node);

            pBlock = (AABlock)pStm.Parent();

            pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), outerBlockStm);

            if (node.Parent() == pStm && pStm is AExpStm)
            {
                pBlock.RemoveChild(pStm);
            }
            else
            {
                PLvalue lvalue = new ALocalLvalue(new TIdentifier(methodReturnerVar.GetName().Text));
                finalTrans.data.LvalueTypes[lvalue] = methodReturnerVar.GetType();
                finalTrans.data.LocalLinks[(ALocalLvalue)lvalue] = methodReturnerVar;
                exp = new ALvalueExp(lvalue);
                finalTrans.data.ExpTypes[exp] = methodReturnerVar.GetType();

                node.ReplaceBy(exp);
            }
            return(new List <AABlock>()
            {
                outerBlock
            });
        }
        public override void CaseANonstaticInvokeExp(ANonstaticInvokeExp node)
        {
            PExp  reciever = node.GetReceiver();
            PType type     = finalTrans.data.ExpTypes[reciever];

            //If the reciever is not a var, put it in a new var.
            if (!(reciever is ALvalueExp))
            {
                AALocalDecl  localDecl   = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(type, data), new TIdentifier("nonstaticInvokeVar"), reciever);
                ALocalLvalue localRef    = new ALocalLvalue(new TIdentifier("nonstaticInvokeVar"));
                ALvalueExp   localRefExp = new ALvalueExp(localRef);
                node.SetReceiver(localRefExp);
                PStm    pStm   = Util.GetAncestor <PStm>(node);
                AABlock pBlock = (AABlock)pStm.Parent();
                pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), new ALocalDeclStm(new TSemicolon(";"), localDecl));
                reciever = localRefExp;

                data.LvalueTypes[localRef]     =
                    data.ExpTypes[localRefExp] = type;
                data.LocalLinks[localRef]      = localDecl;

                localDecl.Apply(this);
            }

            if (type is ANamedType && finalTrans.data.StructTypeLinks.ContainsKey((ANamedType)type))
            {
                //ANamedType type = (ANamedType) finalTrans.data.ExpTypes[reciever];
                if (finalTrans.data.StructTypeLinks[(ANamedType)type].GetClassToken() != null)
                {
                    //Pass the pointer
                    ALvalueExp     lvalueExp     = (ALvalueExp)reciever;
                    APointerLvalue pointerLvalue = (APointerLvalue)lvalueExp.GetLvalue();
                    reciever = pointerLvalue.GetBase();
                }

                AMethodDecl      method       = finalTrans.data.StructMethodLinks[node];
                ASimpleInvokeExp simpleInvoke = new ASimpleInvokeExp();
                simpleInvoke.SetName(node.GetName());
                PExp[] exps = new PExp[node.GetArgs().Count];
                node.GetArgs().CopyTo(exps, 0);
                foreach (PExp exp in exps)
                {
                    simpleInvoke.GetArgs().Add(exp);
                }
                simpleInvoke.GetArgs().Add(reciever);
                node.ReplaceBy(simpleInvoke);
                finalTrans.data.SimpleMethodLinks[simpleInvoke] = method;
                finalTrans.data.StructMethodLinks.Remove(node);
                finalTrans.data.ExpTypes[simpleInvoke] = method.GetReturnType();
                finalTrans.data.ExpTypes.Remove(node);
                simpleInvoke.Apply(this);
            }
            else
            {//Enrichment
                /*AEnrichmentDecl enrichment = finalTrans.data.EnrichmentTypeLinks[type];
                 *
                 * foreach (AEnrichmentDecl enrichmentDecl in finalTrans.data.Enrichments)
                 * {
                 *  if (Util.IsVisible(node, enrichmentDecl) &&
                 *      Util.IsDeclVisible(enrichmentDecl, Util.GetAncestor<AASourceFile>(node)) &&
                 *      Util.TypesEqual(type, enrichmentDecl.GetType(), finalTrans.data))
                 *  {
                 *      enrichment = enrichmentDecl;
                 *      break;
                 *  }
                 * }
                 * if (enrichment == null)
                 * {
                 *  finalTrans.errors.Add(new ErrorCollection.Error(node.GetName(), "TransFormMethodDecls.NonStaticInvoke: Expected enrichment - this is a bug. It should have been caught earlier"));
                 *  throw new ParserException(node.GetName(), "");
                 * }*/
                AMethodDecl      method       = finalTrans.data.StructMethodLinks[node];
                ASimpleInvokeExp simpleInvoke = new ASimpleInvokeExp();
                simpleInvoke.SetName(node.GetName());
                PExp[] exps = new PExp[node.GetArgs().Count];
                node.GetArgs().CopyTo(exps, 0);
                foreach (PExp exp in exps)
                {
                    simpleInvoke.GetArgs().Add(exp);
                }
                simpleInvoke.GetArgs().Add(reciever);
                node.ReplaceBy(simpleInvoke);
                finalTrans.data.SimpleMethodLinks[simpleInvoke] = method;
                finalTrans.data.StructMethodLinks.Remove(node);
                finalTrans.data.ExpTypes[simpleInvoke] = method.GetReturnType();
                finalTrans.data.ExpTypes.Remove(node);
                simpleInvoke.Apply(this);
            }
        }
예제 #29
0
            public override void InASimpleInvokeExp(ASimpleInvokeExp node)
            {
                AMethodDecl decl = finalTrans.data.SimpleMethodLinks[node];

                AddDepency(Util.GetAncestor <AASourceFile>(node), Util.GetAncestor <AASourceFile>(decl));
            }
예제 #30
0
            /*
             * Apply after assignement fixup
             * Assume no i++
             *
             * Convert usages to method invocations.
             */

            public static void Parse(FinalTransformations finalTrans)
            {
                SharedData data = finalTrans.data;

                foreach (KeyValuePair <APropertyLvalue, APropertyDecl> pair in data.PropertyLinks)
                {
                    APropertyLvalue lvalue   = pair.Key;
                    APropertyDecl   property = pair.Value;

                    if (Util.GetAncestor <AAProgram>(lvalue) == null)
                    {
                        continue;
                    }

                    if (lvalue.Parent() is AAssignmentExp)
                    {
                        AAssignmentExp   assignment = (AAssignmentExp)lvalue.Parent();
                        ASimpleInvokeExp invoke     =
                            new ASimpleInvokeExp(
                                new TIdentifier("Set" + property.GetName().Text, lvalue.GetName().Line,
                                                lvalue.GetName().Pos), new ArrayList()
                        {
                            assignment.GetExp()
                        });
                        assignment.ReplaceBy(invoke);
                        data.SimpleMethodLinks[invoke] = data.Setters[property];
                        data.ExpTypes[invoke]          = new AVoidType(new TVoid("void"));
                    }
                    else
                    {
                        ALvalueExp       exp    = (ALvalueExp)lvalue.Parent();
                        ASimpleInvokeExp invoke =
                            new ASimpleInvokeExp(
                                new TIdentifier("Get" + property.GetName().Text, lvalue.GetName().Line,
                                                lvalue.GetName().Pos), new ArrayList()
                        {
                        });
                        exp.ReplaceBy(invoke);
                        data.SimpleMethodLinks[invoke] = data.Getters[property];
                        data.ExpTypes[invoke]          = property.GetType();
                    }
                }
                foreach (KeyValuePair <AStructLvalue, APropertyDecl> pair in data.StructPropertyLinks)
                {
                    AStructLvalue   lvalue         = pair.Key;
                    APropertyDecl   property       = pair.Value;
                    AEnrichmentDecl enrichmentDecl = null;
                    AStructDecl     structDecl     = null;
                    if (data.EnrichmentTypeLinks.ContainsKey(data.ExpTypes[lvalue.GetReceiver()]))
                    {
                        enrichmentDecl = data.EnrichmentTypeLinks[data.ExpTypes[lvalue.GetReceiver()]];
                    }
                    if (enrichmentDecl == null)
                    {
                        structDecl = data.StructTypeLinks[(ANamedType)data.ExpTypes[lvalue.GetReceiver()]];
                    }

                    if (Util.GetAncestor <AAProgram>(lvalue) == null)
                    {
                        continue;
                    }

                    PExp structArg;
                    if (structDecl == null || structDecl.GetClassToken() == null)
                    {
                        structArg = lvalue.GetReceiver();
                    }
                    else
                    {
                        //Send pointer
                        ALvalueExp     lvalueExp    = (ALvalueExp)lvalue.GetReceiver();
                        APointerLvalue pointerValue = (APointerLvalue)lvalueExp.GetLvalue();
                        structArg = pointerValue.GetBase();
                    }

                    if (lvalue.Parent() is AAssignmentExp)
                    {
                        AAssignmentExp   assignment = (AAssignmentExp)lvalue.Parent();
                        ASimpleInvokeExp invoke     =
                            new ASimpleInvokeExp(
                                new TIdentifier("Set" + property.GetName().Text, lvalue.GetName().Line,
                                                lvalue.GetName().Pos), new ArrayList()
                        {
                            assignment.GetExp(), structArg
                        });
                        assignment.ReplaceBy(invoke);
                        data.SimpleMethodLinks[invoke] = data.Setters[property];
                        data.ExpTypes[invoke]          = new AVoidType(new TVoid("void"));
                    }
                    else
                    {
                        ALvalueExp       exp    = (ALvalueExp)lvalue.Parent();
                        ASimpleInvokeExp invoke =
                            new ASimpleInvokeExp(
                                new TIdentifier("Get" + property.GetName().Text, lvalue.GetName().Line,
                                                lvalue.GetName().Pos), new ArrayList()
                        {
                            structArg
                        });
                        exp.ReplaceBy(invoke);
                        data.SimpleMethodLinks[invoke] = data.Getters[property];
                        data.ExpTypes[invoke]          = property.GetType();
                    }
                }
                foreach (KeyValuePair <AArrayLvalue, Util.Pair <APropertyDecl, bool> > pair in data.ArrayPropertyLinks)
                {
                    AArrayLvalue    lvalue         = pair.Key;
                    APropertyDecl   property       = pair.Value.First;
                    bool            implicitMatch  = pair.Value.Second;
                    AEnrichmentDecl enrichmentDecl = null;
                    AStructDecl     structDecl     = null;

                    if (OldEnrichmentParents.ContainsKey(property))
                    {
                        enrichmentDecl = OldEnrichmentParents[property];
                    }
                    else
                    {
                        structDecl = OldStructParents[property];
                    }
                    if (Util.GetAncestor <AAProgram>(lvalue) == null)
                    {
                        continue;
                    }

                    PExp structArg;
                    if (structDecl == null || structDecl.GetClassToken() == null)
                    {
                        structArg = lvalue.GetBase();
                    }
                    else
                    {
                        //Send pointer
                        if (implicitMatch)
                        {
                            structArg = lvalue.GetBase();
                        }
                        else
                        {
                            ALvalueExp     lvalueExp    = (ALvalueExp)lvalue.GetBase();
                            APointerLvalue pointerValue = (APointerLvalue)lvalueExp.GetLvalue();
                            structArg = pointerValue.GetBase();
                        }
                    }

                    /*  if (!(structArg is ALvalueExp &&
                     *    (((ALvalueExp)structArg).GetLvalue() is ALocalLvalue || ((ALvalueExp)structArg).GetLvalue() is AFieldLvalue ||
                     *    ((ALvalueExp)structArg).GetLvalue() is AStructLvalue || ((ALvalueExp)structArg).GetLvalue() is AStructFieldLvalue))
                     * {
                     *    //Make new local
                     *    AALocalDecl decl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                     *                                       Util.MakeClone(data.ExpTypes[structArg], data),
                     *                                       new TIdentifier("propertyVar"), structArg);
                     *    ALocalLvalue declRef = new ALocalLvalue(new TIdentifier("propertyVar"));
                     *    structArg = new ALvalueExp(declRef);
                     *    PStm stm = Util.GetAncestor<PStm>(lvalue);
                     * }*/

                    if (lvalue.Parent() is AAssignmentExp)
                    {
                        AAssignmentExp   assignment = (AAssignmentExp)lvalue.Parent();
                        ASimpleInvokeExp invoke     =
                            new ASimpleInvokeExp(
                                new TIdentifier("SetThis", lvalue.GetToken().Line,
                                                lvalue.GetToken().Pos), new ArrayList()
                        {
                            lvalue.GetIndex(), assignment.GetExp(), structArg
                        });
                        assignment.ReplaceBy(invoke);
                        data.SimpleMethodLinks[invoke] = data.Setters[property];
                        data.ExpTypes[invoke]          = new AVoidType(new TVoid("void"));
                    }
                    else
                    {
                        ALvalueExp       exp    = (ALvalueExp)lvalue.Parent();
                        ASimpleInvokeExp invoke =
                            new ASimpleInvokeExp(
                                new TIdentifier("GetThis", lvalue.GetToken().Line,
                                                lvalue.GetToken().Pos), new ArrayList()
                        {
                            lvalue.GetIndex(), structArg
                        });
                        exp.ReplaceBy(invoke);
                        data.SimpleMethodLinks[invoke] = data.Getters[property];
                        data.ExpTypes[invoke]          = property.GetType();
                    }
                }
            }