コード例 #1
0
 public virtual void InADeleteStm(ADeleteStm node)
 {
     DefaultIn(node);
 }
コード例 #2
0
 public virtual void OutADeleteStm(ADeleteStm node)
 {
     DefaultOut(node);
 }
コード例 #3
0
 public virtual void CaseADeleteStm(ADeleteStm node)
 {
     DefaultCase(node);
 }
コード例 #4
0
 public override void CaseADeleteStm(ADeleteStm node)
 {
     InADeleteStm(node);
     if (node.GetExp() != null)
     {
         node.GetExp().Apply(this);
     }
     if (node.GetToken() != null)
     {
         node.GetToken().Apply(this);
     }
     OutADeleteStm(node);
 }
コード例 #5
0
        public override void OutADeleteStm(ADeleteStm node)
        {
            //Child must be a pointer
            if (!(data.ExpTypes[node.GetExp()] is APointerType))
            {
                errors.Add(new ErrorCollection.Error(node.GetToken(), currentSourceFile, LocRM.GetString("ErrorText145")));
            }

            //Deconstructor must be visible
            else
            {
                APointerType pointer = (APointerType) data.ExpTypes[node.GetExp()];
                if (pointer.GetType() is ANamedType && data.StructTypeLinks.ContainsKey((ANamedType) pointer.GetType()))
                {
                    //Struct deconstructor
                    AStructDecl currentStr = Util.GetAncestor<AStructDecl>(node);
                    AStructDecl str = data.StructTypeLinks[(ANamedType) pointer.GetType()];
                    while (str != null)
                    {
                        ADeconstructorDecl deconstructor = data.StructDeconstructor[str];
                        if (deconstructor.GetVisibilityModifier() is APrivateVisibilityModifier &&
                            currentStr != str)
                        {
                            errors.Add(new ErrorCollection.Error(node.GetToken(),
                                                                 LocRM.GetString("ErrorText146"),
                                                                 false,
                                                                 new[]
                                                                     {
                                                                         new ErrorCollection.Error(
                                                                             deconstructor.GetName(), LocRM.GetString("ErrorText147"))
                                                                     }));
                        }
                        else if (deconstructor.GetVisibilityModifier() is AProtectedVisibilityModifier &&
                            !Util.Extends(str, currentStr, data))
                        {
                            errors.Add(new ErrorCollection.Error(node.GetToken(),
                                                                 LocRM.GetString("ErrorText148"),
                                                                 false,
                                                                 new[]
                                                                     {
                                                                         new ErrorCollection.Error(
                                                                             deconstructor.GetName(), LocRM.GetString("ErrorText147"))
                                                                     }));
                        }
                        if (str.GetBase() == null)
                            break;
                        str = data.StructTypeLinks[(ANamedType) str.GetBase()];
                    }
                }
                else
                {
                    //Enrichment deconstructor
                    AEnrichmentDecl currentEnrichment = Util.GetAncestor<AEnrichmentDecl>(node);
                    List<IList> visibleDecls = Util.GetVisibleDecls(node, true);
                    foreach (IList declList in visibleDecls)
                    {
                        foreach (PDecl decl in declList)
                        {
                            if (decl is AEnrichmentDecl)
                            {
                                AEnrichmentDecl enrichment = (AEnrichmentDecl)decl;
                                if (!Util.TypesEqual(pointer.GetType(), enrichment.GetType(), data))
                                    continue;
                                foreach (PDecl enrichmentDecl in enrichment.GetDecl())
                                {
                                    if (enrichmentDecl is ADeconstructorDecl)
                                    {
                                        ADeconstructorDecl deconstructor = (ADeconstructorDecl)enrichmentDecl;
                                        if (deconstructor.GetVisibilityModifier() is APrivateVisibilityModifier &&
                                            currentEnrichment != enrichment)
                                        {
                                            errors.Add(new ErrorCollection.Error(node.GetToken(),
                                                                                 LocRM.GetString("ErrorText146"),
                                                                                 false,
                                                                                 new[]
                                                                     {
                                                                         new ErrorCollection.Error(
                                                                             deconstructor.GetName(), LocRM.GetString("ErrorText147"))
                                                                     }));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            base.OutADeleteStm(node);
        }
コード例 #6
0
 ArrayList New303()
 {
     ArrayList nodeList = new ArrayList();
     ArrayList nodeArrayList3 = (ArrayList) Pop();
     ArrayList nodeArrayList2 = (ArrayList) Pop();
     ArrayList nodeArrayList1 = (ArrayList) Pop();
     TDelete tdeleteNode2 = (TDelete)nodeArrayList1[0];
     PExp pexpNode3 = (PExp)nodeArrayList2[0];
     ADeleteStm pstmNode1 = new ADeleteStm (
       tdeleteNode2,
       pexpNode3
     );
     nodeList.Add(pstmNode1);
     return nodeList;
 }
コード例 #7
0
            public override void CaseADeleteStm(ADeleteStm node)
            {
                List<Node> visitMeNext = new List<Node>();
                APointerType pointer = (APointerType) data.ExpTypes[node.GetExp()];

                //Call deconstructor if it exists
                {
                    AMethodDecl deconstructor = null;
                    if (pointer.GetType() is ANamedType &&
                        data.StructTypeLinks.ContainsKey((ANamedType) pointer.GetType()))
                    {
                        AStructDecl str = data.StructTypeLinks[(ANamedType) pointer.GetType()];

                        deconstructor =
                            data.DeconstructorMap[data.StructDeconstructor[str]];

                    }
                    else //Look for enrichment deconstructor
                    {
                        foreach (AEnrichmentDecl enrichment in data.Enrichments)
                        {
                            if (Util.TypesEqual(pointer.GetType(), enrichment.GetType(), data))
                            {
                                foreach (PDecl decl in enrichment.GetDecl())
                                {
                                    if (decl is ADeconstructorDecl)
                                    {
                                        deconstructor = data.DeconstructorMap[(ADeconstructorDecl) decl];
                                        break;
                                    }
                                }
                                if (deconstructor != null)
                                    break;
                            }
                        }
                    }
                    if (deconstructor != null)
                    {
                        /*
                         * Convert delete <exp>; to
                         *
                         * var deleteVar = <exp>;
                         * Deconstructor(deleteVar);
                         * delete deleteVar;
                         */

                        AALocalDecl deleteVarDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null,
                                                                    null,
                                                                    new ANamedType(
                                                                        new TIdentifier(Util.IsIntPointer(node,
                                                                                                          pointer.
                                                                                                              GetType(),
                                                                                                          data)
                                                                                            ? "int"
                                                                                            : "string"), null),
                                                                    new TIdentifier("deleteVar"), node.GetExp());
                        ALocalLvalue deleteVarRef = new ALocalLvalue(new TIdentifier("deleteVar"));
                        ALvalueExp deleteVarRefExp = new ALvalueExp(deleteVarRef);
                        node.SetExp(deleteVarRefExp);

                        AABlock pBlock = (AABlock) node.Parent();
                        pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(node),
                                                      new ALocalDeclStm(new TSemicolon(";"), deleteVarDecl));

                        data.LocalLinks[deleteVarRef] = deleteVarDecl;
                        data.LvalueTypes[deleteVarRef] =
                            data.ExpTypes[deleteVarRefExp] = data.ExpTypes[deleteVarDecl.GetInit()];

                        deleteVarRef = new ALocalLvalue(new TIdentifier("deleteVar"));
                        deleteVarRefExp = new ALvalueExp(deleteVarRef);

                        ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier(deconstructor.GetName().Text),
                                                                       new ArrayList() {deleteVarRefExp});
                        pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(node),
                                                      new AExpStm(new TSemicolon(";"), invoke));

                        data.LocalLinks[deleteVarRef] = deleteVarDecl;
                        data.LvalueTypes[deleteVarRef] =
                            data.ExpTypes[deleteVarRefExp] = data.ExpTypes[deleteVarDecl.GetInit()];
                        data.SimpleMethodLinks[invoke] = deconstructor;
                        data.ExpTypes[invoke] = deconstructor.GetReturnType();
                        visitMeNext.Add(deleteVarDecl);

                    }
                }

                if (pointer.GetType() is AArrayTempType || pointer.GetType() is ADynamicArrayType)
                {
                    //If struct array, delete all struct data
                    PType baseType;
                    if (pointer.GetType() is AArrayTempType)
                        baseType = ((AArrayTempType) pointer.GetType()).GetType();
                    else
                        baseType = ((ADynamicArrayType)pointer.GetType()).GetType();

                    PExp pointerString = node.GetExp();
                    if (Util.IsBulkCopy(baseType))
                    {
                        node.GetExp().Apply(new MoveMethodDeclsOut("pointerVar", data));
                        pointerString = node.GetExp();
                        ANamedType aBaseType = (ANamedType) baseType;
                        /* Add the following
                         *
                         * string deleteMe = node.getExp(); <-- no
                         * int i = 0;
                         * while (i < array.length)
                         * {
                         *      DeleteStruct<name>(deleteMe + StringToInt(i));
                         *      i = i + 1;
                         * }
                         *
                         */
                        //string deleteMe = node.getExp();
                        /*AALocalDecl deleteMeVar = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                                  new ANamedType(new TIdentifier("string"), null),
                                                                  new TIdentifier("deleteMe"), node.GetExp());*/
                        //pointerString = deleteMeVar;
                        //int i = 0;
                        AALocalDecl iVar = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                           new ANamedType(new TIdentifier("int"), null),
                                                           new TIdentifier("i"),
                                                           new AIntConstExp(new TIntegerLiteral("0")));
                        //i < array.length
                        ASimpleInvokeExp lenghCall = new ASimpleInvokeExp(new TIdentifier("DataTableGetInt"), new ArrayList());
                        lenghCall.GetArgs().Add(new ABooleanConstExp(new ATrueBool()));
                        //ALocalLvalue deleteMeUse1 = new ALocalLvalue(new TIdentifier("deleteMeVar"));
                        ABinopExp arrayLengthString = new ABinopExp(Util.MakeClone(pointerString, data),
                                                                    new APlusBinop(new TPlus("+")),
                                                                    new AStringConstExp(
                                                                        new TStringLiteral("\"\\\\Length\"")));
                        lenghCall.GetArgs().Add(arrayLengthString);
                        ALocalLvalue iUse1 = new ALocalLvalue(new TIdentifier("i"));
                        ABinopExp cond = new ABinopExp(new ALvalueExp(iUse1), new ALtBinop(new TLt("<")), lenghCall);

                        //DeleteStruct<name>(deleteMe + StringToInt(i));
                        //ALocalLvalue deleteMeUse2 = new ALocalLvalue(new TIdentifier("deleteMeVar"));
                        ALocalLvalue iUse2 = new ALocalLvalue(new TIdentifier("i"));
                        ASimpleInvokeExp intToString = new ASimpleInvokeExp(new TIdentifier("IntToString"), new ArrayList());
                        intToString.GetArgs().Add(new ALvalueExp(iUse2));
                        ABinopExp binopExp1 = new ABinopExp(Util.MakeClone(pointerString, data), new APlusBinop(new TPlus("+")), new AStringConstExp(new TStringLiteral("\"[\"")));
                        ABinopExp binopExp2 = new ABinopExp(binopExp1, new APlusBinop(new TPlus("+")), intToString);
                        ABinopExp binopExp3 = new ABinopExp(binopExp2, new APlusBinop(new TPlus("+")), new AStringConstExp(new TStringLiteral("\"]\"")));
                        ASimpleInvokeExp deleteStructInvoke = new ASimpleInvokeExp(new TIdentifier("DeleteStruct" + aBaseType.AsIdentifierString()), new ArrayList());
                        deleteStructInvoke.GetArgs().Add(binopExp3);
                        //i = i + 1;
                        ALocalLvalue iUse3 = new ALocalLvalue(new TIdentifier("i"));
                        ALocalLvalue iUse4 = new ALocalLvalue(new TIdentifier("i"));
                        ABinopExp binopExp = new ABinopExp(new ALvalueExp(iUse4), new APlusBinop(new TPlus("+")), new AIntConstExp(new TIntegerLiteral("1")));
                        AAssignmentExp assign = new AAssignmentExp(new TAssign("="), iUse3, binopExp);

                        //While (...){...}
                        AABlock innerWhile = new AABlock();
                        innerWhile.GetStatements().Add(new AExpStm(new TSemicolon(";"), deleteStructInvoke));
                        innerWhile.GetStatements().Add(new AExpStm(new TSemicolon(";"), assign));
                        AWhileStm whileStm = new AWhileStm(new TLParen("("), cond, new ABlockStm(new TLBrace("{"), innerWhile));

                        AABlock pBlock = (AABlock) node.Parent();
                        //pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(node), new ALocalDeclStm(new TSemicolon(";"), deleteMeVar));
                        pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(node), new ALocalDeclStm(new TSemicolon(";"), iVar));
                        pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(node), whileStm);
                        //visitMeNext.Add(deleteMeVar);
                        visitMeNext.Add(whileStm);

                        data.ExpTypes[iVar.GetInit()] =
                            data.LvalueTypes[iUse1] =
                            data.LvalueTypes[iUse2] =
                            data.LvalueTypes[iUse3] =
                            data.LvalueTypes[iUse4] =
                            data.ExpTypes[cond.GetLeft()] =
                            data.ExpTypes[lenghCall] =
                            data.ExpTypes[(PExp) intToString.GetArgs()[0]] =
                            data.ExpTypes[binopExp.GetLeft()] =
                            data.ExpTypes[binopExp.GetRight()] =
                            data.ExpTypes[binopExp] =
                            data.ExpTypes[assign] = new ANamedType(new TIdentifier("int"), null);
                        data.ExpTypes[(PExp) lenghCall.GetArgs()[0]] =
                            data.ExpTypes[cond] = new ANamedType(new TIdentifier("bool"), null);
                        data.ExpTypes[lenghCall] =
                           // data.LvalueTypes[deleteMeUse1] =
                           // data.LvalueTypes[deleteMeUse2] =
                            data.ExpTypes[arrayLengthString.GetLeft()] =
                            data.ExpTypes[arrayLengthString.GetRight()] =
                            data.ExpTypes[arrayLengthString] =
                            data.ExpTypes[intToString] =
                            data.ExpTypes[binopExp1] =
                            data.ExpTypes[binopExp1.GetLeft()] =
                            data.ExpTypes[binopExp1.GetRight()] =
                            data.ExpTypes[binopExp2] =
                            data.ExpTypes[binopExp3] =
                            data.ExpTypes[binopExp3.GetRight()] =
                            data.ExpTypes[lenghCall] =
                            data.ExpTypes[lenghCall] =
                            data.ExpTypes[lenghCall] =
                            data.ExpTypes[lenghCall] = new ANamedType(new TIdentifier("string"), null);
                        data.ExpTypes[deleteStructInvoke] = new AVoidType(new TVoid("void"));

                        data.Locals[pBlock].Add(iVar);
                        //data.Locals[pBlock].Add(deleteMeVar);

                        data.LocalLinks[iUse1] =
                            data.LocalLinks[iUse2] =
                            data.LocalLinks[iUse3] =
                            data.LocalLinks[iUse4] = iVar;

                        //data.LocalLinks[deleteMeUse1] =
                        //    data.LocalLinks[deleteMeUse2] = deleteMeVar;

                        data.SimpleMethodLinks[lenghCall] =
                            data.Libraries.Methods.First(method => method.GetName().Text == lenghCall.GetName().Text);
                        data.SimpleMethodLinks[intToString] =
                            data.Libraries.Methods.First(method => method.GetName().Text == intToString.GetName().Text);

                        AStructDecl structDecl = data.StructTypeLinks[aBaseType];

                        if (!deleteStructMethod.ContainsKey(structDecl))
                            CreateDeleteStructMethod(node, structDecl, data);
                        data.SimpleMethodLinks[deleteStructInvoke] = deleteStructMethod[structDecl];
                    }

                    /*
                     * Convert delete <exp>
                     * to
                     * DeleteArray(<exp>);
                     *
                     */

                    ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("DeleteArray"), new ArrayList(){pointerString});
                    /*if (pointerString == null)
                    {
                        invoke.GetArgs().Add(node.GetExp());
                    }
                    else
                    {
                        ALocalLvalue local = new ALocalLvalue(new TIdentifier("pointerString"));
                        invoke.GetArgs().Add(new ALvalueExp(local));

                        data.LocalLinks[local] = pointerString;
                        data.LvalueTypes[local] =
                            data.ExpTypes[(PExp) invoke.GetArgs()[0]] = new ANamedType(new TIdentifier("string"), null);
                    }*/
                    data.ExpTypes[invoke] = new AVoidType(new TVoid("void"));
                    if (deleteArrayMethod == null)
                        CreateDeleteArrayMethod(node, data);
                    data.SimpleMethodLinks[invoke] = deleteArrayMethod;
                    visitMeNext.Add(invoke);

                    node.ReplaceBy(new AExpStm(new TSemicolon(";"), invoke));
                }
                else
                {
                    //Not array type
                    PExp pointerString = node.GetExp();
                    bool isIntPointer = Util.IsIntPointer(node, pointer.GetType(), data);
                    bool createdStructDelete = false;
                    if (Util.IsBulkCopy(pointer.GetType()))
                    {
                        node.GetExp().Apply(new MoveMethodDeclsOut("pointerVar", data));
                        pointerString = node.GetExp();

                        ANamedType aBaseType = (ANamedType) pointer.GetType();
                        /* Insert
                         *
                         * string deleteMeVar = node.getExp();
                         * DeleteStruct<name>(deleteMeVar);
                         *
                         */

                       /* AALocalDecl deleteMeVar = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                                  new ANamedType(new TIdentifier("string"), null),
                                                                  new TIdentifier("deleteMe"), node.GetExp());*/
                        //pointerString = deleteMeVar;
                        //ALocalLvalue deleteMeUse = new ALocalLvalue(new TIdentifier("deleteMeVar"));

                        PExp deleteExp = Util.MakeClone(pointerString, data);
                        AStructDecl structDecl = data.StructTypeLinks[aBaseType];
                        if (isIntPointer)
                        {
                            GlobalStructVars vars = CreateStructFields(node, structDecl, data);
                            int allocateLimit = int.Parse(structDecl.GetIntDim().Text);
                            if (vars.IdentifierArray != null)
                            {
                                int usedBits = allocateLimit == 0
                                                   ? 0
                                                   : ((int) Math.Floor(Math.Log(allocateLimit, 2)) + 1);
                                int bitsLeft = 31 - usedBits;
                                int biggestIdentifier = (1 << (bitsLeft + 1)) - 1;

                                AIntConstExp bitsLeftConst = new AIntConstExp(new TIntegerLiteral(bitsLeft.ToString()));
                                deleteExp = new ABinopExp(deleteExp, new ARBitShiftBinop(new TRBitShift(">>")),
                                                          bitsLeftConst);

                                data.ExpTypes[bitsLeftConst] =
                                    data.ExpTypes[deleteExp] = new ANamedType(new TIdentifier("int"), null);
                            }
                        }

                        ASimpleInvokeExp deleteStructInvoke = new ASimpleInvokeExp(new TIdentifier("DeleteStruct" + aBaseType.AsIdentifierString()), new ArrayList());
                        deleteStructInvoke.GetArgs().Add(deleteExp);

                        AABlock pBlock = (AABlock)node.Parent();
                        //pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(node), new ALocalDeclStm(new TSemicolon(";"), deleteMeVar));
                        pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(node), new AExpStm(new TSemicolon(";"), deleteStructInvoke));
                        //visitMeNext.Add(deleteMeVar);
                        visitMeNext.Add(deleteStructInvoke);

                        /*data.ExpTypes[(PExp) deleteStructInvoke.GetArgs()[0]] =
                            data.LvalueTypes[deleteMeUse] = new ANamedType(new TIdentifier("string"), null);*/
                        data.ExpTypes[deleteStructInvoke] = new AVoidType(new TVoid("void"));

                        //data.Locals[pBlock].Add(deleteMeVar);

                        //data.LocalLinks[deleteMeUse] = deleteMeVar;

                        if (!deleteStructMethod.ContainsKey(structDecl))
                            CreateDeleteStructMethod(node, structDecl, data);
                        data.SimpleMethodLinks[deleteStructInvoke] = deleteStructMethod[structDecl];
                        createdStructDelete = true;
                    }
                    if (!isIntPointer)
                    {
                        /*
                         * Convert delete <exp>
                         * to
                         * DeleteSimple(<exp>);
                         *
                         */

                        ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("DeleteObject"),
                                                                       new ArrayList() {pointerString});
                        /*if (pointerString == null)
                        {
                            invoke.GetArgs().Add(node.GetExp());
                        }
                        else
                        {
                            ALocalLvalue local = new ALocalLvalue(new TIdentifier("pointerString"));
                            invoke.GetArgs().Add(new ALvalueExp(local));

                            data.LocalLinks[local] = pointerString;
                            data.LvalueTypes[local] =
                                data.ExpTypes[(PExp)invoke.GetArgs()[0]] = new ANamedType(new TIdentifier("string"), null);
                        }*/
                        data.ExpTypes[invoke] = new AVoidType(new TVoid("void"));
                        if (deleteObjectMethod == null)
                            CreateDeleteObjectMethod(node, data);
                        data.SimpleMethodLinks[invoke] = deleteObjectMethod;
                        visitMeNext.Add(invoke);

                        node.ReplaceBy(new AExpStm(new TSemicolon(";"), invoke));
                    }
                    else if (createdStructDelete)
                    {
                        node.Parent().RemoveChild(node);
                    }
                    else
                    {
                        //There is an enrichment

                        PExp deleteExp = pointerString;
                        AEnrichmentDecl enrichmentDecl = data.EnrichmentTypeLinks[pointer.GetType()];
                        if (isIntPointer)
                        {
                            GlobalStructVars vars = CreateEnrichmentFields(node, enrichmentDecl, data);
                            int allocateLimit = int.Parse(enrichmentDecl.GetIntDim().Text);
                            if (vars.IdentifierArray != null)
                            {
                                int usedBits = allocateLimit == 0
                                                   ? 0
                                                   : ((int)Math.Floor(Math.Log(allocateLimit, 2)) + 1);
                                int bitsLeft = 31 - usedBits;

                                AIntConstExp bitsLeftConst = new AIntConstExp(new TIntegerLiteral(bitsLeft.ToString()));
                                deleteExp = new ABinopExp(deleteExp, new ARBitShiftBinop(new TRBitShift(">>")),
                                                          bitsLeftConst);

                                data.ExpTypes[bitsLeftConst] =
                                    data.ExpTypes[deleteExp] = new ANamedType(new TIdentifier("int"), null);
                            }
                        }

                        ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("DeleteObject"),
                                                                       new ArrayList() { deleteExp });

                        data.ExpTypes[invoke] = new AVoidType(new TVoid("void"));
                        data.SimpleMethodLinks[invoke] = CreateDeleteEnrichmentMethod(node, enrichmentDecl, data);
                        visitMeNext.Add(invoke);

                        node.ReplaceBy(new AExpStm(new TSemicolon(";"), invoke));

                    }
                }
                bool hadPointerPreviously = hadPointer;
                PExp previosNameExp = nameExp;
                for (int i = 0; i < visitMeNext.Count; i++)
                {
                    hadPointer = false;
                    nameExp = null;
                    visitMeNext[i].Apply(this);
                }
                hadPointer = hadPointerPreviously;
                nameExp = previosNameExp;
            }