public override void OutAAProgram(AAProgram node) { foreach (KeyValuePair <AArrayLengthLvalue, AArrayTempType> pair in data.ArrayLengthTypes) { AIntConstExp intConst = new AIntConstExp(new TIntegerLiteral(pair.Value.GetIntDim().Text)); data.ExpTypes[intConst] = new ANamedType(new TIdentifier("int"), null); ALvalueExp exp = Util.GetAncestor <ALvalueExp>(pair.Key); exp.ReplaceBy(intConst); } base.OutAAProgram(node); }
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 }); }
/* * 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(); } } }