public override void CaseAStructLvalue(AStructLvalue node) { //Only do namespace node.GetReceiver().Apply(this); node.GetDotType().Apply(this); Value += node.GetName().Text; }
public InvokeStm(AAsyncInvokeStm node) { Node = node; if (node.GetName() is AAmbiguousNameLvalue) { AAmbiguousNameLvalue ambigious = (AAmbiguousNameLvalue)node.GetName(); AAName name = (AAName)ambigious.GetAmbiguous(); Name = (TIdentifier)name.GetIdentifier()[name.GetIdentifier().Count - 1]; name.GetIdentifier().RemoveAt(name.GetIdentifier().Count - 1); if (name.GetIdentifier().Count == 0) { Base = null; } else { Base = name; } } else { AStructLvalue lvalue = (AStructLvalue)node.GetName(); Name = lvalue.GetName(); Base = lvalue.GetReceiver(); } }
public InvokeStm(ASyncInvokeExp node) { Node = node; if (node.GetName() is AAmbiguousNameLvalue) { AAmbiguousNameLvalue ambigious = (AAmbiguousNameLvalue)node.GetName(); AAName name = (AAName)ambigious.GetAmbiguous(); /*List<List<Node>>[] targets; * List<ANamespaceDecl> namespaces = new List<ANamespaceDecl>(); * /*bool b; * TypeLinking.GetTargets(name, out targets, namespaces, data, null, out b); * for (int i = 0; i < 3; i++) * { * * }*/ Name = (TIdentifier)name.GetIdentifier()[name.GetIdentifier().Count - 1]; name.GetIdentifier().RemoveAt(name.GetIdentifier().Count - 1); if (name.GetIdentifier().Count == 0) { Base = null; } else { Base = name; } } else { AStructLvalue lvalue = (AStructLvalue)node.GetName(); Name = lvalue.GetName(); Base = lvalue.GetReceiver(); } }
public override void CaseAStructFieldLvalue(AStructFieldLvalue node) { //replace strField1 //with <structFormal>.strField1 AStructDecl str = finalTrans.data.StructTypeLinks[(ANamedType)structFormal.GetType()]; ALocalLvalue parameterRefference = new ALocalLvalue(new TIdentifier("tempName")); finalTrans.data.LocalLinks[parameterRefference] = structFormal; finalTrans.data.LvalueTypes[parameterRefference] = structFormal.GetType(); ALvalueExp exp = new ALvalueExp(parameterRefference); finalTrans.data.ExpTypes[exp] = structFormal.GetType(); AStructLvalue replacer = new AStructLvalue(exp, new ADotDotType(new TDot(".")), node.GetName()); foreach (AALocalDecl structField in finalTrans.data.StructFields[str]) { if (structField.GetName().Text == replacer.GetName().Text) { finalTrans.data.StructFieldLinks[replacer] = structField; finalTrans.data.LvalueTypes[replacer] = structField.GetType(); break; } } foreach (APropertyDecl property in finalTrans.data.StructProperties[str]) { if (property.GetName().Text == replacer.GetName().Text) { finalTrans.data.StructPropertyLinks[replacer] = property; finalTrans.data.LvalueTypes[replacer] = property.GetType(); break; } } node.ReplaceBy(replacer); }
private List <PStm> MakeStatements(PExp exp, int line, int pos) { List <PStm> list = new List <PStm>(); if (exp is ASimpleInvokeExp) { list.Add(new AExpStm(new TSemicolon(";", line, pos), exp)); return(list); } if (exp is AAssignmentExp) { list.Add(new AExpStm(new TSemicolon(";", line, pos), exp)); return(list); } if (exp is ANonstaticInvokeExp) { list.Add(new AExpStm(new TSemicolon(";", line, pos), exp)); return(list); } if (exp is ABinopExp) { ABinopExp aExp = (ABinopExp)exp; list.AddRange(MakeStatements(aExp.GetLeft(), line, pos)); list.AddRange(MakeStatements(aExp.GetRight(), line, pos)); return(list); } if (exp is AUnopExp) { AUnopExp aExp = (AUnopExp)exp; list.AddRange(MakeStatements(aExp.GetExp(), line, pos)); return(list); } if (exp is AParenExp) { AParenExp aExp = (AParenExp)exp; list.AddRange(MakeStatements(aExp.GetExp(), line, pos)); return(list); } if (exp is ALvalueExp) { ALvalueExp aExp = (ALvalueExp)exp; PLvalue lvalue = aExp.GetLvalue(); if (lvalue is AStructLvalue) { AStructLvalue aLvalue = (AStructLvalue)lvalue; list.AddRange(MakeStatements(aLvalue.GetReceiver(), line, pos)); return(list); } if (lvalue is AArrayLvalue) { AArrayLvalue aLvalue = (AArrayLvalue)lvalue; list.AddRange(MakeStatements(aLvalue.GetBase(), line, pos)); list.AddRange(MakeStatements(aLvalue.GetIndex(), line, pos)); return(list); } } return(list); }
private int FoldInt(PLvalue lvalue, ref bool valid) { if (!valid) { return(-1); } if (lvalue is ALocalLvalue) { ALocalLvalue aLvalue = (ALocalLvalue)lvalue; AALocalDecl decl = data.LocalLinks[aLvalue]; if (decl.GetConst() == null) { valid = false; return(-1); } return(FoldInt(decl.GetInit(), ref valid)); } if (lvalue is AFieldLvalue) { AFieldLvalue aLvalue = (AFieldLvalue)lvalue; AFieldDecl decl = data.FieldLinks[aLvalue]; if (decl.GetConst() == null) { valid = false; return(-1); } return(FoldInt(decl.GetInit(), ref valid)); } if (lvalue is AStructFieldLvalue) { AStructFieldLvalue aLvalue = (AStructFieldLvalue)lvalue; AALocalDecl decl = data.StructMethodFieldLinks[aLvalue]; if (decl.GetConst() == null) { valid = false; return(-1); } return(FoldInt(decl.GetInit(), ref valid)); } if (lvalue is AStructLvalue) { AStructLvalue aLvalue = (AStructLvalue)lvalue; AALocalDecl decl = data.StructFieldLinks[aLvalue]; if (decl.GetConst() == null) { valid = false; return(-1); } return(FoldInt(decl.GetInit(), ref valid)); } valid = false; return(-1); }
public override void CaseAStructLvalue(AStructLvalue node) { //a->b => (*a).b if (node.GetDotType() is AArrowDotType) { TArrow arrow = ((AArrowDotType)node.GetDotType()).GetToken(); node.SetReceiver(new ALvalueExp(new APointerLvalue(new TStar("*", arrow.Line, arrow.Pos), node.GetReceiver()))); node.SetDotType(new ADotDotType(new TDot(".", arrow.Line, arrow.Pos))); } base.CaseAStructLvalue(node); }
//Convert struct variables to a collection of local variables public override void CaseAALocalDecl(AALocalDecl node) { if (node.GetType() is ANamedType && data.StructTypeLinks.ContainsKey((ANamedType)node.GetType()) && Util.HasAncestor <PStm>(node)) { //Can not have init - it would be bulk copy AStructDecl str = data.StructTypeLinks[(ANamedType)node.GetType()]; Dictionary <AALocalDecl, AALocalDecl> variableMap = new Dictionary <AALocalDecl, AALocalDecl>(); PStm pStm = (PStm)node.Parent(); AABlock pBlock = (AABlock)pStm.Parent(); foreach (AALocalDecl structLocal in str.GetLocals()) { AALocalDecl replacementLocal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(structLocal.GetType(), data), new TIdentifier(node.GetName().Text + "_" + structLocal.GetName().Text), null); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), new ALocalDeclStm(new TSemicolon(";"), replacementLocal)); AALocalDecl baseLocal = structLocal; if (data.EnheritanceLocalMap.ContainsKey(baseLocal)) { baseLocal = data.EnheritanceLocalMap[baseLocal]; } List <AALocalDecl> localsToAdd = new List <AALocalDecl>(); localsToAdd.AddRange(data.EnheritanceLocalMap.Where(pair => pair.Value == baseLocal).Select(pair => pair.Key)); localsToAdd.Add(baseLocal); foreach (AALocalDecl localDecl in localsToAdd) { variableMap[localDecl] = replacementLocal; } } List <ALocalLvalue> uses = new List <ALocalLvalue>(); uses.AddRange(data.LocalLinks.Where(k => k.Value == node && Util.GetAncestor <AAProgram>(k.Key) != null).Select(k => k.Key)); foreach (ALocalLvalue lvalue in uses) { AStructLvalue structLocalRef = (AStructLvalue)lvalue.Parent().Parent(); AALocalDecl replacementLocal = variableMap[data.StructFieldLinks[structLocalRef]]; ALocalLvalue replacementLvalue = new ALocalLvalue(new TIdentifier(replacementLocal.GetName().Text)); data.LocalLinks[replacementLvalue] = replacementLocal; data.LvalueTypes[replacementLvalue] = replacementLocal.GetType(); structLocalRef.ReplaceBy(replacementLvalue); } foreach (AALocalDecl replacementLocal in variableMap.Select(k => k.Value)) { replacementLocal.Apply(this); } } base.CaseAALocalDecl(node); }
private void MakeAssignments(AABlock block, PType type, PLvalue leftSide, bool onEnhritedFields) { if (type is ANamedType && data.StructTypeLinks.ContainsKey((ANamedType)type)) { AStructDecl str = data.StructTypeLinks[(ANamedType)type]; foreach (AALocalDecl field in str.GetLocals().OfType <AALocalDecl>()) { if (!onEnhritedFields && data.EnheritanceLocalMap.ContainsKey(field)) { continue; } ALvalueExp lvalueExp = new ALvalueExp(Util.MakeClone(leftSide, data)); data.ExpTypes[lvalueExp] = data.LvalueTypes[leftSide]; AStructLvalue newLeftSide = new AStructLvalue(lvalueExp, new ADotDotType(new TDot(".")), new TIdentifier(field.GetName().Text)); data.StructFieldLinks[newLeftSide] = field; data.LvalueTypes[newLeftSide] = field.GetType(); if (field.GetInit() != null) { AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), newLeftSide, Util.MakeClone(field.GetInit(), data)); data.ExpTypes[assignment] = data.LvalueTypes[newLeftSide]; block.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment)); } else { MakeAssignments(block, field.GetType(), newLeftSide, onEnhritedFields); } } } else if (type is AArrayTempType) { AArrayTempType aType = (AArrayTempType)type; for (int i = 0; i < int.Parse(aType.GetIntDim().Text); i++) { AIntConstExp index = new AIntConstExp(new TIntegerLiteral(i.ToString())); data.ExpTypes[index] = new ANamedType(new TIdentifier("int"), null); ALvalueExp lvalueExp = new ALvalueExp(Util.MakeClone(leftSide, data)); data.ExpTypes[lvalueExp] = data.LvalueTypes[leftSide]; AArrayLvalue newLeftSide = new AArrayLvalue(new TLBracket("["), lvalueExp, index); data.LvalueTypes[newLeftSide] = aType.GetType(); MakeAssignments(block, aType.GetType(), newLeftSide, onEnhritedFields); } } }
public override void OutABinopExp(ABinopExp node) { if (data.ExpTypes[node.GetLeft()] is ANamedType && data.StructTypeLinks.ContainsKey((ANamedType)data.ExpTypes[node.GetLeft()])) { AStructDecl str = data.StructTypeLinks[(ANamedType)data.ExpTypes[node.GetLeft()]]; PExp replacementExp = new ABooleanConstExp(new ATrueBool()); data.ExpTypes[replacementExp] = new ANamedType(new TIdentifier("bool"), null); foreach (AALocalDecl local in str.GetLocals().OfType <AALocalDecl>()) { AStructLvalue leftSide = new AStructLvalue(Util.MakeClone(node.GetLeft(), data), new ADotDotType(new TDot(".")), new TIdentifier(local.GetName().Text)); ALvalueExp leftSideExp = new ALvalueExp(leftSide); AStructLvalue rightSide = new AStructLvalue(Util.MakeClone(node.GetRight(), data), new ADotDotType(new TDot(".")), new TIdentifier(local.GetName().Text)); ALvalueExp rightSideExp = new ALvalueExp(rightSide); data.StructFieldLinks[leftSide] = data.StructFieldLinks[rightSide] = local; data.LvalueTypes[leftSide] = data.ExpTypes[leftSideExp] = data.LvalueTypes[rightSide] = data.ExpTypes[rightSideExp] = local.GetType(); ABinopExp binop = new ABinopExp(leftSideExp, (PBinop)node.GetBinop().Clone(), rightSideExp); data.ExpTypes[binop] = data.ExpTypes[node]; if (replacementExp is ABooleanConstExp) { replacementExp = binop; } else { replacementExp = new ABinopExp(replacementExp, new ALazyAndBinop(new TAndAnd("&&")), binop); data.ExpTypes[replacementExp] = new ANamedType(new TIdentifier("bool"), null); } } node.ReplaceBy(replacementExp); replacementExp.Apply(this); } base.OutABinopExp(node); }
public static bool ReturnsTheSame(PLvalue left, PLvalue right, SharedData data) { if (left.GetType() != right.GetType()) { return(false); } if (left is ALocalLvalue) { ALocalLvalue aLeft = (ALocalLvalue)left; ALocalLvalue aRight = (ALocalLvalue)right; return(data.LocalLinks[aLeft] == data.LocalLinks[aRight]); } if (left is AFieldLvalue) { AFieldLvalue aLeft = (AFieldLvalue)left; AFieldLvalue aRight = (AFieldLvalue)right; return(data.FieldLinks[aLeft] == data.FieldLinks[aRight]); } if (left is AStructLvalue) { AStructLvalue aLeft = (AStructLvalue)left; AStructLvalue aRight = (AStructLvalue)right; if (data.StructFieldLinks[aLeft] != data.StructFieldLinks[aRight]) { return(false); } return(ReturnsTheSame(aLeft.GetReceiver(), aRight.GetReceiver(), data)); } if (left is AArrayLvalue) { AArrayLvalue aLeft = (AArrayLvalue)left; AArrayLvalue aRight = (AArrayLvalue)right; return(ReturnsTheSame(aLeft.GetIndex(), aRight.GetIndex(), data) && ReturnsTheSame(aLeft.GetBase(), aRight.GetBase(), data)); } throw new Exception("Util.ReturnsTheSame. Unexpected type, got " + left.GetType()); }
public override void OutAStructLvalue(AStructLvalue node) { node.GetName().Text = finalTrans.data.StructFieldLinks[node].GetName().Text; }
public override void CaseAStructLvalue(AStructLvalue node) { node.GetReceiver().Apply(this); Write("." + node.GetName().Text); }
public override void OutAStructLvalue(AStructLvalue node) { currentDecl = new VariableDecl(null, data.StructFieldLinks[node], currentDecl); }
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); }
/* * 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(); } } }
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; } }
private static void MakeCloneRefferences(PLvalue clone, PLvalue lvalue, SharedData data) { data.LvalueTypes[clone] = data.LvalueTypes[lvalue]; if (lvalue is ALocalLvalue) { data.LocalLinks[(ALocalLvalue)clone] = data.LocalLinks[(ALocalLvalue)lvalue]; } else if (lvalue is AFieldLvalue) { data.FieldLinks[(AFieldLvalue)clone] = data.FieldLinks[(AFieldLvalue)lvalue]; } else if (lvalue is AStructLvalue) { AStructLvalue aLvalue = (AStructLvalue)lvalue; AStructLvalue aClone = (AStructLvalue)clone; if (data.StructFieldLinks.ContainsKey(aLvalue)) { data.StructFieldLinks[aClone] = data.StructFieldLinks[aLvalue]; } else { data.StructPropertyLinks[aClone] = data.StructPropertyLinks[aLvalue]; } MakeCloneRefferences(aClone.GetReceiver(), aLvalue.GetReceiver(), data); } else if (lvalue is AStructFieldLvalue) { AStructFieldLvalue aLvalue = (AStructFieldLvalue)lvalue; AStructFieldLvalue aClone = (AStructFieldLvalue)clone; if (data.StructMethodFieldLinks.ContainsKey(aLvalue)) { data.StructMethodFieldLinks[aClone] = data.StructMethodFieldLinks[aLvalue]; } if (data.StructMethodPropertyLinks.ContainsKey(aLvalue)) { data.StructMethodPropertyLinks[aClone] = data.StructMethodPropertyLinks[aLvalue]; } } else if (lvalue is AArrayLvalue) { AArrayLvalue aLvalue = (AArrayLvalue)lvalue; AArrayLvalue aClone = (AArrayLvalue)clone; MakeCloneRefferences(aClone.GetBase(), aLvalue.GetBase(), data); MakeCloneRefferences(aClone.GetIndex(), aLvalue.GetIndex(), data); } else if (lvalue is APointerLvalue) { APointerLvalue aLvalue = (APointerLvalue)lvalue; APointerLvalue aClone = (APointerLvalue)clone; MakeCloneRefferences(aClone.GetBase(), aLvalue.GetBase(), data); } else if (lvalue is AThisLvalue || lvalue is AValueLvalue) { //AThisLvalue aLvalue = (AThisLvalue)lvalue; //Do nothing more } else if (lvalue is APropertyLvalue) { APropertyLvalue aLvalue = (APropertyLvalue)lvalue; APropertyLvalue aClone = (APropertyLvalue)clone; data.PropertyLinks[aClone] = data.PropertyLinks[aLvalue]; } else { throw new Exception("Unexpect lvalue. Got " + lvalue.GetType()); } }
bool IsConstant(PLvalue lvalue) { if (lvalue is ALocalLvalue) { ALocalLvalue aLvalue = (ALocalLvalue)lvalue; AALocalDecl decl = data.LocalLinks[aLvalue]; if (decl == initialLocalDecl) { return(false); } return(decl.GetConst() != null && IsConstant(decl.GetInit())); } if (lvalue is AFieldLvalue) { AFieldLvalue aLvalue = (AFieldLvalue)lvalue; AFieldDecl decl = data.FieldLinks[aLvalue]; if (decl == initialFieldDecl) { return(false); } return(decl.GetConst() != null && IsConstant(decl.GetInit())); } if (lvalue is APropertyLvalue) { return(false); } if (lvalue is ANamespaceLvalue) { return(true); } if (lvalue is AStructFieldLvalue) { AStructFieldLvalue aLvalue = (AStructFieldLvalue)lvalue; AALocalDecl decl = data.StructMethodFieldLinks[aLvalue]; if (decl == initialLocalDecl) { return(false); } return(decl.GetConst() != null && IsConstant(decl.GetInit())); } if (lvalue is AStructLvalue) { AStructLvalue aLvalue = (AStructLvalue)lvalue; AALocalDecl decl = data.StructFieldLinks[aLvalue]; if (decl == initialLocalDecl) { return(false); } return(decl.GetConst() != null && IsConstant(decl.GetInit())); } if (lvalue is AArrayLvalue) { AArrayLvalue aLvalue = (AArrayLvalue)lvalue; return(IsConstant(aLvalue.GetIndex()) && IsConstant(aLvalue.GetBase())); } if (lvalue is APointerLvalue) { APointerLvalue aLvalue = (APointerLvalue)lvalue; return(IsConstant(aLvalue.GetBase())); } if (lvalue is APArrayLengthLvalue) { APArrayLengthLvalue aLvalue = (APArrayLengthLvalue)lvalue; return(data.ExpTypes[aLvalue.GetBase()] is AArrayTempType); } if (lvalue is AThisLvalue) { return(false); } if (lvalue is AValueLvalue) { return(false); } throw new Exception("Unexpected lvalue. Got " + lvalue); }