public override void CaseATempCastExp(ATempCastExp node) { //The cast type must be a single identifier if (node.GetType() is ALvalueExp) { ALvalueExp lvalueExp = (ALvalueExp)node.GetType(); if (lvalueExp.GetLvalue() is AAmbiguousNameLvalue) { AAmbiguousNameLvalue ambiguousLvalue = (AAmbiguousNameLvalue)lvalueExp.GetLvalue(); if (ambiguousLvalue.GetAmbiguous() is AAName) { AAName simpleName = (AAName)ambiguousLvalue.GetAmbiguous(); if (simpleName.GetIdentifier().Count == 1) { ACastExp castExp = new ACastExp(node.GetToken(), new ANamedType(simpleName), node.GetExp()); node.ReplaceBy(castExp); castExp.Apply(this); return; } } } } PExp exp = node.GetExp(); node.ReplaceBy(exp); exp.Apply(this); }
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); }
bool IsConstant(PExp exp) { if (exp is ABinopExp) { ABinopExp aExp = (ABinopExp)exp; return(IsConstant(aExp.GetLeft()) && IsConstant(aExp.GetRight())); } if (exp is AUnopExp) { AUnopExp aExp = (AUnopExp)exp; return(IsConstant(aExp.GetExp())); } if (exp is AIncDecExp) { AIncDecExp aExp = (AIncDecExp)exp; return(IsConstant(aExp.GetLvalue())); } if (exp is AIntConstExp || exp is AHexConstExp || exp is AOctalConstExp || exp is AFixedConstExp || exp is AStringConstExp || exp is ACharConstExp || exp is ABooleanConstExp || exp is ANullExp || exp is AAssignmentExp || exp is ADelegateExp) { return(true); } if (exp is ASimpleInvokeExp || exp is ANonstaticInvokeExp || exp is ASyncInvokeExp || exp is ANewExp || exp is ADelegateInvokeExp) { return(false); } if (exp is ALvalueExp) { ALvalueExp aExp = (ALvalueExp)exp; return(IsConstant(aExp.GetLvalue())); } if (exp is AParenExp) { AParenExp aExp = (AParenExp)exp; return(IsConstant(aExp.GetExp())); } if (exp is ACastExp) { ACastExp aExp = (ACastExp)exp; return(IsConstant(aExp.GetExp())); } if (exp is AIfExp) { AIfExp aExp = (AIfExp)exp; return(IsConstant(aExp.GetCond()) && IsConstant(aExp.GetThen()) && IsConstant(aExp.GetElse())); } if (exp == null) { return(false); } throw new Exception("Unexpected exp. Got " + exp); }
public override void OutALvalueExp(ALvalueExp node) { data.ExpTypes[node] = data.LvalueTypes[node.GetLvalue()]; }
/* * 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 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); } }
private static void MakeCloneRefferences(PExp clone, PExp exp, SharedData data) { data.ExpTypes[clone] = data.ExpTypes[exp]; if (exp is AIntConstExp || exp is AHexConstExp || exp is AOctalConstExp || exp is AFixedConstExp || exp is AStringConstExp || exp is ACharConstExp || exp is ABooleanConstExp || exp is ANullExp) { //No more required } else if (exp is AIncDecExp) { AIncDecExp aExp = (AIncDecExp)exp; AIncDecExp aClone = (AIncDecExp)clone; MakeCloneRefferences(aClone.GetLvalue(), aExp.GetLvalue(), data); } else if (exp is ABinopExp) { ABinopExp aExp = (ABinopExp)exp; ABinopExp aClone = (ABinopExp)clone; MakeCloneRefferences(aClone.GetLeft(), aExp.GetLeft(), data); MakeCloneRefferences(aClone.GetRight(), aExp.GetRight(), data); } else if (exp is AUnopExp) { AUnopExp aExp = (AUnopExp)exp; AUnopExp aClone = (AUnopExp)clone; MakeCloneRefferences(aClone.GetExp(), aExp.GetExp(), data); } else if (exp is ASimpleInvokeExp) { ASimpleInvokeExp aExp = (ASimpleInvokeExp)exp; ASimpleInvokeExp aClone = (ASimpleInvokeExp)clone; data.SimpleMethodLinks[aClone] = data.SimpleMethodLinks[aExp]; for (int i = 0; i < aExp.GetArgs().Count; i++) { MakeCloneRefferences((PExp)aClone.GetArgs()[i], (PExp)aExp.GetArgs()[i], data); } } else if (exp is ANonstaticInvokeExp) { ANonstaticInvokeExp aExp = (ANonstaticInvokeExp)exp; ANonstaticInvokeExp aClone = (ANonstaticInvokeExp)clone; data.StructMethodLinks[aClone] = data.StructMethodLinks[aExp]; for (int i = 0; i < aExp.GetArgs().Count; i++) { MakeCloneRefferences((PExp)aClone.GetArgs()[i], (PExp)aExp.GetArgs()[i], data); } MakeCloneRefferences(aClone.GetReceiver(), aExp.GetReceiver(), data); } else if (exp is ALvalueExp) { ALvalueExp aExp = (ALvalueExp)exp; ALvalueExp aClone = (ALvalueExp)clone; MakeCloneRefferences(aClone.GetLvalue(), aExp.GetLvalue(), data); } else if (exp is AAssignmentExp) { AAssignmentExp aExp = (AAssignmentExp)exp; AAssignmentExp aClone = (AAssignmentExp)clone; MakeCloneRefferences(aClone.GetLvalue(), aExp.GetLvalue(), data); MakeCloneRefferences(aClone.GetExp(), aExp.GetExp(), data); } else if (exp is AParenExp) { AParenExp aExp = (AParenExp)exp; AParenExp aClone = (AParenExp)clone; MakeCloneRefferences(aClone.GetExp(), aExp.GetExp(), data); } else if (exp is AStringConstExp) { AStringConstExp aExp = (AStringConstExp)exp; AStringConstExp aClone = (AStringConstExp)clone; if (data.ObfuscatedStrings.ContainsKey(aExp)) { data.ObfuscatedStrings[aClone] = data.ObfuscatedStrings[aExp]; } if (data.StringsDontJoinRight.Contains(aExp)) { data.StringsDontJoinRight.Add(aClone); } } else if (exp is ANewExp) { ANewExp aExp = (ANewExp)exp; ANewExp aClone = (ANewExp)clone; if (data.ConstructorLinks.ContainsKey(aExp)) { data.ConstructorLinks[aClone] = data.ConstructorLinks[aExp]; } MakeCloneRefferences(aClone.GetType(), aExp.GetType(), data); for (int i = 0; i < aExp.GetArgs().Count; i++) { MakeCloneRefferences((PExp)aClone.GetArgs()[i], (PExp)aExp.GetArgs()[i], data); } } else if (exp is ACastExp) { ACastExp aExp = (ACastExp)exp; ACastExp aClone = (ACastExp)clone; MakeCloneRefferences(aClone.GetType(), aExp.GetType(), data); MakeCloneRefferences(aClone.GetExp(), aExp.GetExp(), data); } else if (exp is ADelegateExp) { ADelegateExp aExp = (ADelegateExp)exp; ADelegateExp aClone = (ADelegateExp)clone; if (data.DelegateCreationMethod.ContainsKey(aExp)) { data.DelegateCreationMethod[aClone] = data.DelegateCreationMethod[aExp]; } MakeCloneRefferences(aClone.GetType(), aExp.GetType(), data); MakeCloneRefferences(aClone.GetLvalue(), aExp.GetLvalue(), data); } else if (exp is ADelegateInvokeExp) { ADelegateInvokeExp aExp = (ADelegateInvokeExp)exp; ADelegateInvokeExp aClone = (ADelegateInvokeExp)clone; MakeCloneRefferences(aClone.GetReceiver(), aExp.GetReceiver(), data); for (int i = 0; i < aExp.GetArgs().Count; i++) { MakeCloneRefferences((PExp)aClone.GetArgs()[i], (PExp)aExp.GetArgs()[i], data); } } else if (exp is AIfExp) { AIfExp aExp = (AIfExp)exp; AIfExp aClone = (AIfExp)clone; MakeCloneRefferences(aClone.GetCond(), aExp.GetCond(), data); MakeCloneRefferences(aClone.GetThen(), aExp.GetThen(), data); MakeCloneRefferences(aClone.GetElse(), aExp.GetElse(), data); } else { throw new Exception("Unexpect exp. Got " + exp.GetType()); } }
public static bool ReturnsTheSame(PExp left, PExp right, SharedData data) { if (left.GetType() != right.GetType()) { return(false); } if (left is ABinopExp) { ABinopExp aLeft = (ABinopExp)left; ABinopExp aRight = (ABinopExp)right; if (aLeft.GetBinop().GetType() != aRight.GetBinop().GetType()) { return(false); } return(ReturnsTheSame(aLeft.GetLeft(), aRight.GetLeft(), data) && ReturnsTheSame(aLeft.GetRight(), aRight.GetRight(), data)); } if (left is AUnopExp) { AUnopExp aLeft = (AUnopExp)left; AUnopExp aRight = (AUnopExp)right; if (aLeft.GetUnop().GetType() != aRight.GetUnop().GetType()) { return(false); } return(ReturnsTheSame(aLeft.GetExp(), aRight.GetExp(), data)); } if (left is AIntConstExp) { AIntConstExp aLeft = (AIntConstExp)left; AIntConstExp aRight = (AIntConstExp)right; return(int.Parse(aLeft.GetIntegerLiteral().Text) == int.Parse(aRight.GetIntegerLiteral().Text)); } if (left is AFixedConstExp) { AFixedConstExp aLeft = (AFixedConstExp)left; AFixedConstExp aRight = (AFixedConstExp)right; return(aLeft.GetFixedLiteral().Text == aRight.GetFixedLiteral().Text); } if (left is AStringConstExp) { AStringConstExp aLeft = (AStringConstExp)left; AStringConstExp aRight = (AStringConstExp)right; return(aLeft.GetStringLiteral().Text == aRight.GetStringLiteral().Text); } if (left is ACharConstExp) { ACharConstExp aLeft = (ACharConstExp)left; ACharConstExp aRight = (ACharConstExp)right; return(aLeft.GetCharLiteral().Text == aRight.GetCharLiteral().Text); } if (left is ABooleanConstExp) { ABooleanConstExp aLeft = (ABooleanConstExp)left; ABooleanConstExp aRight = (ABooleanConstExp)right; return(aLeft.GetBool().GetType() == aRight.GetBool().GetType()); } if (left is ASimpleInvokeExp) { //A method might not return the same thing each time it is called return(false); } if (left is ALvalueExp) { ALvalueExp aLeft = (ALvalueExp)left; ALvalueExp aRight = (ALvalueExp)right; return(ReturnsTheSame(aLeft.GetLvalue(), aRight.GetLvalue(), data)); } if (left is AParenExp) { AParenExp aLeft = (AParenExp)left; AParenExp aRight = (AParenExp)right; return(ReturnsTheSame(aLeft.GetExp(), aRight.GetExp(), data)); } throw new Exception("Util.ReturnsTheSame. Unexpected type, got " + left.GetType()); }