private PType LvalueToType(PLvalue lvalue, IList dynamicOpList, TLt genericToken, IList genericTypes) { PType type = LvalueToType(lvalue); if (genericToken != null) { type = new AGenericType(genericToken, type, new ArrayList()); while (genericTypes.Count > 0) { ((AGenericType)type).GetGenericTypes().Add(genericTypes[0]); } } foreach (PShadyDynamicOps op in dynamicOpList) { if (op is APointerShadyDynamicOps) { APointerShadyDynamicOps aop = (APointerShadyDynamicOps)op; type = new APointerType(aop.GetToken(), type); } else if (op is AArrayShadyDynamicOps) { AArrayShadyDynamicOps aop = (AArrayShadyDynamicOps)op; if (aop.GetExp() == null) { type = new ADynamicArrayType(aop.GetToken(), type); } else { type = new AArrayTempType(aop.GetToken(), type, aop.GetExp(), null); } } } return(type); }
private PType LvalueToType(PLvalue lvalue) { if (lvalue is AStructLvalue) { /*AStructLvalue structLvalue = (AStructLvalue) lvalue; * TIdentifier typeName = structLvalue.GetName(); * if (structLvalue.GetReceiver() is ALvalueExp && ((ALvalueExp)structLvalue.GetReceiver()).GetLvalue() is AAmbiguousNameLvalue) * { * AAmbiguousNameLvalue ambiguousNameLvalue = (AAmbiguousNameLvalue) ((ALvalueExp) structLvalue.GetReceiver()).GetLvalue(); * return new ANamedType(typeName, ((ASimpleName)ambiguousNameLvalue.GetAmbiguous()).GetIdentifier()); * } * else*/ { //errors.Add(new ErrorCollection.Error(((AStructLvalue)lvalue).GetName(), currentSourceFile, "Invalid type name. Must be \"<namespace>.<struct name>\".")); throw new ParserException(null, null); } } if (lvalue is AArrayLvalue) { AArrayLvalue aLvalue = (AArrayLvalue)lvalue; if (!(aLvalue.GetBase() is ALvalueExp)) { //errors.Add(new ErrorCollection.Error(GetToken(lvalue), currentSourceFile, "Whatever that is, it's not allowed in a type")); throw new ParserException(null, null); } PLvalue newLvalue = ((ALvalueExp)aLvalue.GetBase()).GetLvalue(); return(new AArrayTempType(aLvalue.GetToken(), LvalueToType(newLvalue), aLvalue.GetIndex(), null)); } //it must be an AAmbiguousNameLvalue then return(new ANamedType(((AAmbiguousNameLvalue)lvalue).GetAmbiguous())); }
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 static PLvalue MakeClone(PLvalue lvalue, SharedData data) { if (lvalue == null) { return(null); } PLvalue clone = (PLvalue)lvalue.Clone(); MakeCloneRefferences(clone, lvalue, data); return(clone); }
public override void CaseAAssignmentExp(AAssignmentExp node) { if (!(node.Parent() is AExpStm)) { PStm parentStm = Util.GetAncestor <PStm>(node); MoveMethodDeclsOut mover = new MoveMethodDeclsOut("multipleAssignmentsVar", finalTrans.data); node.GetLvalue().Apply(mover); PLvalue lvalue = Util.MakeClone(node.GetLvalue(), finalTrans.data); ALvalueExp exp = new ALvalueExp(lvalue); finalTrans.data.ExpTypes[exp] = finalTrans.data.LvalueTypes[lvalue]; node.ReplaceBy(exp); AExpStm stm = new AExpStm(new TSemicolon(";"), node); AABlock block = (AABlock)parentStm.Parent(); //block.GetStatements().Insert(block.GetStatements().IndexOf(parentStm), localDeclStm); block.GetStatements().Insert(block.GetStatements().IndexOf(parentStm), stm); //localDeclStm.Apply(this); stm.Apply(this); if (parentStm is AWhileStm && Util.IsAncestor(exp, ((AWhileStm)parentStm).GetCondition())) { AWhileStm aStm = (AWhileStm)parentStm; //Copy assignment before continues //Before each continue in the while, and at the end. //Add continue statement, if not present block = (AABlock)((ABlockStm)aStm.GetBody()).GetBlock(); if (block.GetStatements().Count == 0 || !(block.GetStatements()[block.GetStatements().Count - 1] is AContinueStm)) { block.GetStatements().Add(new AContinueStm(new TContinue("continue"))); } //Get all continue statements in the while ContinueFinder finder = new ContinueFinder(); block.Apply(finder); foreach (AContinueStm continueStm in finder.Continues) { stm = new AExpStm(new TSemicolon(";"), Util.MakeClone(node, finalTrans.data)); block.GetStatements().Insert(block.GetStatements().IndexOf(continueStm), stm); stm.Apply(this); } } return; } base.CaseAAssignmentExp(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 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 DefaultIn(Node 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 = currentCloneNode }; currentCloneNode.Apply(getChildTypeByIndex); currentCloneNode = getChildTypeByIndex.Child; //currentCloneNode should now be the clone corrosponding to node. /*finalTrans.data.ExpTypes * finalTrans.data.FieldLinks * finalTrans.data.LocalLinks * finalTrans.data.Locals//Lets forget about this one * finalTrans.data.LvalueTypes * finalTrans.data.SimpleMethodLinks * finalTrans.data.StructFieldLinks * finalTrans.data.StructMethodLinks * finalTrans.data.StructTypeLinks*/ if (node is ANewExp && finalTrans.data.ConstructorLinks.ContainsKey((ANewExp)node)) { finalTrans.data.ConstructorLinks[(ANewExp)currentCloneNode] = finalTrans.data.ConstructorLinks[(ANewExp)node]; } if (node is PExp) { finalTrans.data.ExpTypes[(PExp)currentCloneNode] = finalTrans.data.ExpTypes[(PExp)node]; } if (node is AStringConstExp && finalTrans.data.TriggerDeclarations.Any(p => p.Value.Contains(((AStringConstExp)node).GetStringLiteral()))) { finalTrans.data.TriggerDeclarations.First( p => p.Value.Contains(((AStringConstExp)node).GetStringLiteral())).Value.Add( ((AStringConstExp)currentCloneNode).GetStringLiteral()); } if (node is AFieldLvalue) { finalTrans.data.FieldLinks[(AFieldLvalue)currentCloneNode] = finalTrans.data.FieldLinks[(AFieldLvalue)node]; } if (node is ALocalLvalue) { AALocalDecl originalFormal = finalTrans.data.LocalLinks[(ALocalLvalue)node]; PLvalue replacer = Util.MakeClone(localMap[originalFormal], finalTrans.data); currentCloneNode.ReplaceBy(replacer); currentCloneNode = replacer; if (localExpMap.ContainsKey(originalFormal)) { ReplaceUsAfter[replacer] = localExpMap[originalFormal]; } } if (node is AALocalDecl) { ALocalLvalue replacer = new ALocalLvalue(new TIdentifier("IwillGetRenamedLater")); finalTrans.data.LvalueTypes[replacer] = ((AALocalDecl)currentCloneNode).GetType(); finalTrans.data.LocalLinks[replacer] = (AALocalDecl)currentCloneNode; localMap.Add((AALocalDecl)node, replacer); } if (node is PLvalue) { finalTrans.data.LvalueTypes[(PLvalue)currentCloneNode] = finalTrans.data.LvalueTypes[(PLvalue)node]; } if (node is ASimpleInvokeExp) { finalTrans.data.SimpleMethodLinks[(ASimpleInvokeExp)currentCloneNode] = finalTrans.data.SimpleMethodLinks[(ASimpleInvokeExp)node]; } if (node is AStructLvalue) { finalTrans.data.StructFieldLinks[(AStructLvalue)currentCloneNode] = finalTrans.data.StructFieldLinks[(AStructLvalue)node]; } if (node is ANonstaticInvokeExp) { finalTrans.data.StructMethodLinks[(ANonstaticInvokeExp)currentCloneNode] = finalTrans.data.StructMethodLinks[(ANonstaticInvokeExp)node]; } if (node is ANamedType && finalTrans.data.StructTypeLinks.Keys.Contains(node)) { finalTrans.data.StructTypeLinks[(ANamedType)currentCloneNode] = finalTrans.data.StructTypeLinks[(ANamedType)node]; } if (node is ANamedType && finalTrans.data.DelegateTypeLinks.Keys.Contains(node)) { finalTrans.data.DelegateTypeLinks[(ANamedType)currentCloneNode] = finalTrans.data.DelegateTypeLinks[(ANamedType)node]; } if (node is APropertyLvalue && finalTrans.data.PropertyLinks.ContainsKey((APropertyLvalue)node)) { finalTrans.data.PropertyLinks[(APropertyLvalue)currentCloneNode] = finalTrans.data.PropertyLinks[(APropertyLvalue)node]; } }
private void RegiseterUse(PLvalue lvalue, List<AALocalDecl> tail) { if (tail == null) tail = new List<AALocalDecl>(); if (lvalue.Parent().Parent() is AStructLvalue) { AStructLvalue parent = (AStructLvalue)lvalue.Parent().Parent(); AALocalDecl local = data.StructFieldLinks[parent]; AALocalDecl baseLocal = local; if (data.EnheritanceLocalMap.ContainsKey(baseLocal)) baseLocal = data.EnheritanceLocalMap[baseLocal]; AStructDecl pStruct = (AStructDecl) local.Parent(); List<AALocalDecl> localsToTail = new List<AALocalDecl>(); localsToTail.AddRange( data.EnheritanceLocalMap.Where( pair => pair.Value == baseLocal && Util.Extends(pStruct, (AStructDecl) pair.Key.Parent(), data)) .Select(pair => pair.Key)); //If local == baseLocal, nothing is stored in enhritancelocalmap for them. if (local == baseLocal) localsToTail.Add(local); foreach (AALocalDecl l in localsToTail) { List<AALocalDecl> newTail = new List<AALocalDecl>(); newTail.AddRange(tail); newTail.Add(l); RegiseterUse(parent, newTail); } return; } UsedParameters.Add(tail); }
public ADelegateExp( TDelegate _token_, PType _type_, PLvalue _lvalue_ ) { SetToken(_token_); SetType(_type_); SetLvalue(_lvalue_); }
public AAsyncInvokeStm( TAsyncInvoke _token_, PLvalue _name_, IList _args_ ) { SetToken(_token_); SetName(_name_); this._args_ = new TypedList(new Args_Cast(this)); this._args_.Clear(); this._args_.AddAll(_args_); }
internal override void RemoveChild(Node child) { if (_const_ == child) { _const_ = null; return; } if (_pre_pointers_.Contains(child)) { _pre_pointers_.Remove(child); return; } if (_lvalue_ == child) { _lvalue_ = null; return; } if (_generic_token_ == child) { _generic_token_ = null; return; } if (_generic_types_.Contains(child)) { _generic_types_.Remove(child); return; } if (_post_pointers_.Contains(child)) { _post_pointers_.Remove(child); return; } if (_local_decl_right_.Contains(child)) { _local_decl_right_.Remove(child); return; } }
internal override void RemoveChild(Node child) { if (_assignop_ == child) { _assignop_ = null; return; } if (_lvalue_ == child) { _lvalue_ = null; return; } if (_exp_ == child) { _exp_ = null; return; } }
public ALvalueExp( PLvalue _lvalue_ ) { SetLvalue(_lvalue_); }
private void CheckDynamicLvalue(PLvalue node) { if (node.Parent() is ALvalueExp) { ALvalueExp parent = (ALvalueExp)node.Parent(); if (parent.Parent() is AAssignmentExp) return; if (!(parent.Parent() is PLvalue)) {//Then this should be a data table get //If this is a bulk copy type, move it out to a new variable. //var pointerVar; //pointerVar = dataTableGet(...) //... PType type = data.ExpTypes[parent]; if (Util.IsBulkCopy(type)) { PStm pStm = Util.GetAncestor<PStm>(node); AABlock pBlock = (AABlock)pStm.Parent(); AAssignmentExp assignment; if (parent.Parent() is AALocalDecl) { //Turn into assignment ALocalLvalue leftSide = new ALocalLvalue(new TIdentifier("tempName")); data.LocalLinks[leftSide] = (AALocalDecl) parent.Parent(); assignment = new AAssignmentExp(new TAssign("="), leftSide, parent); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm) + 1, new AExpStm(new TSemicolon(";"), assignment)); data.ExpTypes[assignment] = data.LvalueTypes[leftSide] = data.LocalLinks[leftSide].GetType(); assignment.Apply(this); return; } pStm = Util.GetAncestor<PStm>(node); AALocalDecl localDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(type, data), new TIdentifier("pointerVar"), null); ALocalLvalue lvalue = new ALocalLvalue(new TIdentifier("pointerVar")); ALocalLvalue lvalue2 = new ALocalLvalue(new TIdentifier("pointerVar")); ALvalueExp lvalue2Exp = new ALvalueExp(lvalue2); parent.ReplaceBy(lvalue2Exp); assignment = new AAssignmentExp(new TAssign("="), lvalue, parent); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), new ALocalDeclStm(new TSemicolon(";"), localDecl)); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), new AExpStm(new TSemicolon(";"), assignment)); data.LocalLinks[lvalue] = localDecl; data.LocalLinks[lvalue2] = localDecl; data.ExpTypes[assignment] = data.ExpTypes[lvalue2Exp] = data.LvalueTypes[lvalue] = data.LvalueTypes[lvalue2] = type; assignmentRightSideSet = true; assignment.Apply(this); } else { string s = "string"; if (type is ANamedType) s = ((ANamedType) type).AsIdentifierString(); else if (type is APointerType && Util.IsIntPointer(node, ((APointerType)type).GetType(), data)) { s = "int"; } parent.ReplaceBy(CreateDynaicGetStm(s)); } hadPointer = false; } } }
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); }
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); }
private List<PStm> MakeAssignments(PLvalue lvalue, PExp exp, List<AALocalDecl> declChain) { PType type = data.LvalueTypes[lvalue]; //data.ExpTypes[exp]; PType oldType = data.ExpTypes[exp]; List<PStm> returner = new List<PStm>(); if (type is ADynamicArrayType ||type is AArrayTempType) { AArrayTempType aType; if (type is ADynamicArrayType) aType = (AArrayTempType) data.LvalueTypes[lvalue]; else aType = (AArrayTempType) type; for (int j = 0; j < int.Parse(aType.GetIntDim().Text); j++) { ALvalueExp bulkCopyRefExp = new ALvalueExp(Util.MakeClone(lvalue, data)); AIntConstExp index1 = new AIntConstExp(new TIntegerLiteral(j.ToString())); AArrayLvalue leftSideIndex = new AArrayLvalue(new TLBracket("{"), bulkCopyRefExp, index1); AIntConstExp index2 = new AIntConstExp(new TIntegerLiteral(j.ToString())); AArrayLvalue rightSideIndex = new AArrayLvalue(new TLBracket("{"), Util.MakeClone(exp, data), index2); ALvalueExp rightSideExp = new ALvalueExp(rightSideIndex); data.ExpTypes[bulkCopyRefExp] = data.LvalueTypes[lvalue]; data.ExpTypes[index1] = data.ExpTypes[index2] = new ANamedType(new TIdentifier("int"), null); data.LvalueTypes[leftSideIndex] = data.LvalueTypes[rightSideIndex] = data.ExpTypes[rightSideExp] = aType.GetType(); returner.AddRange(MakeAssignments(leftSideIndex, rightSideExp, declChain)); } } else if (type is ANamedType) { ANamedType aType = (ANamedType) type; if (Util.IsBulkCopy(type)) { AStructDecl str = data.StructTypeLinks[aType]; foreach (AALocalDecl localDecl in str.GetLocals()) { ALvalueExp bulkCopyRefExp = new ALvalueExp(Util.MakeClone(lvalue, data)); AStructLvalue leftSide = new AStructLvalue(bulkCopyRefExp, new ADotDotType(new TDot(".")), new TIdentifier(localDecl.GetName().Text)); AStructLvalue rightSide = new AStructLvalue(Util.MakeClone(exp, data), new ADotDotType(new TDot(".")), new TIdentifier(localDecl.GetName().Text)); ALvalueExp rightSideExp = new ALvalueExp(rightSide); List<AALocalDecl> newDeclChain = new List<AALocalDecl>(); newDeclChain.AddRange(declChain); newDeclChain.Add(localDecl); data.StructFieldLinks[leftSide] = data.StructFieldLinks[rightSide] = localDecl; data.ExpTypes[bulkCopyRefExp] = data.LvalueTypes[lvalue]; data.LvalueTypes[leftSide] = data.LvalueTypes[rightSide] = data.ExpTypes[rightSideExp] = localDecl.GetType(); returner.AddRange(MakeAssignments(leftSide, rightSideExp, newDeclChain)); } } else { //Primitive, find out if it is used if (declChain[0] == null || UsedParameters[Util.GetAncestor<AMethodDecl>(declChain[0])].Where(usedParameter => usedParameter.Count <= declChain.Count).Any( usedParameter => !usedParameter.Where((t, i) => t != declChain[i]).Any())) { AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), Util.MakeClone(lvalue, data), Util.MakeClone(exp, data)); data.ExpTypes[assignment] = data.LvalueTypes[lvalue]; returner.Add(new AExpStm(new TSemicolon(";"), assignment)); } } } else if (type is APointerType) { //Assign as primitive, find out if it is used if (declChain[0] == null || UsedParameters[Util.GetAncestor<AMethodDecl>(declChain[0])].Where(usedParameter => usedParameter.Count <= declChain.Count).Any( usedParameter => !usedParameter.Where((t, i) => t != declChain[i]).Any())) { AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), Util.MakeClone(lvalue, data), Util.MakeClone(exp, data)); data.ExpTypes[assignment] = data.LvalueTypes[lvalue]; returner.Add(new AExpStm(new TSemicolon(";"), assignment)); } } else { throw new Exception("Unexpected type. Got " + (type == null ? "null" : type.ToString())); } return returner; }
private PType LvalueToType(PLvalue lvalue) { if (lvalue is AStructLvalue) { /*AStructLvalue structLvalue = (AStructLvalue) lvalue; TIdentifier typeName = structLvalue.GetName(); if (structLvalue.GetReceiver() is ALvalueExp && ((ALvalueExp)structLvalue.GetReceiver()).GetLvalue() is AAmbiguousNameLvalue) { AAmbiguousNameLvalue ambiguousNameLvalue = (AAmbiguousNameLvalue) ((ALvalueExp) structLvalue.GetReceiver()).GetLvalue(); return new ANamedType(typeName, ((ASimpleName)ambiguousNameLvalue.GetAmbiguous()).GetIdentifier()); } else*/ { //errors.Add(new ErrorCollection.Error(((AStructLvalue)lvalue).GetName(), currentSourceFile, "Invalid type name. Must be \"<namespace>.<struct name>\".")); throw new ParserException(null, null); } } if (lvalue is AArrayLvalue) { AArrayLvalue aLvalue = (AArrayLvalue)lvalue; if (!(aLvalue.GetBase() is ALvalueExp)) { //errors.Add(new ErrorCollection.Error(GetToken(lvalue), currentSourceFile, "Whatever that is, it's not allowed in a type")); throw new ParserException(null, null); } PLvalue newLvalue = ((ALvalueExp)aLvalue.GetBase()).GetLvalue(); return new AArrayTempType(aLvalue.GetToken(), LvalueToType(newLvalue), aLvalue.GetIndex(), null); } //it must be an AAmbiguousNameLvalue then return new ANamedType(((AAmbiguousNameLvalue)lvalue).GetAmbiguous()); }
public override void DefaultIn(Node 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 = currentCloneNode }; currentCloneNode.Apply(getChildTypeByIndex); currentCloneNode = getChildTypeByIndex.Child; //currentCloneNode should now be the clone corrosponding to node. /*finalTrans.data.ExpTypes * finalTrans.data.FieldLinks * finalTrans.data.LocalLinks * finalTrans.data.Locals//Lets not forget about this one * finalTrans.data.LvalueTypes * finalTrans.data.SimpleMethodLinks * finalTrans.data.StructFieldLinks * finalTrans.data.StructMethodLinks * finalTrans.data.StructTypeLinks*/ if (node is AABlock) { data.Locals[(AABlock)currentCloneNode] = new List <AALocalDecl>(); } if (node is PExp) { data.ExpTypes[(PExp)currentCloneNode] = data.ExpTypes[(PExp)node]; } if (node is AFieldLvalue) { data.FieldLinks[(AFieldLvalue)currentCloneNode] = data.FieldLinks[(AFieldLvalue)node]; } if (node is ALocalLvalue) { PLvalue replacer = Util.MakeClone(localMap[data.LocalLinks[(ALocalLvalue)node]], data); currentCloneNode.ReplaceBy(replacer); currentCloneNode = replacer; } if (node is AALocalDecl) { ALocalLvalue replacer = new ALocalLvalue(new TIdentifier("IwillGetRenamedLater")); data.LvalueTypes[replacer] = ((AALocalDecl)currentCloneNode).GetType(); data.LocalLinks[replacer] = (AALocalDecl)currentCloneNode; AABlock pBlock = Util.GetAncestor <AABlock>(currentCloneNode) ?? (AABlock)Util.GetAncestor <AMethodDecl>(currentCloneNode).GetBlock(); data.Locals[Util.GetAncestor <AABlock>(currentCloneNode)].Add((AALocalDecl)currentCloneNode); localMap.Add((AALocalDecl)node, replacer); } if (node is PLvalue) { data.LvalueTypes[(PLvalue)currentCloneNode] = data.LvalueTypes[(PLvalue)node]; } if (node is ASimpleInvokeExp) { data.SimpleMethodLinks[(ASimpleInvokeExp)currentCloneNode] = data.SimpleMethodLinks[(ASimpleInvokeExp)node]; } if (node is AStructLvalue) { data.StructFieldLinks[(AStructLvalue)currentCloneNode] = data.StructFieldLinks[(AStructLvalue)node]; } if (node is ANonstaticInvokeExp) { data.StructMethodLinks[(ANonstaticInvokeExp)currentCloneNode] = data.StructMethodLinks[(ANonstaticInvokeExp)node]; } if (node is ANamedType && data.StructTypeLinks.Keys.Contains(node)) { data.StructTypeLinks[(ANamedType)currentCloneNode] = data.StructTypeLinks[(ANamedType)node]; } if (extraCheck != null) { extraCheck(node, currentCloneNode); } if (node is PType && data.EnrichmentTypeLinks.ContainsKey((PType)node)) { data.EnrichmentTypeLinks[(PType)currentCloneNode] = data.EnrichmentTypeLinks[(PType)node]; } if (node is AStringConstExp) { if (data.StringsDontJoinRight.Contains(node)) { data.StringsDontJoinRight.Add((AStringConstExp)currentCloneNode); } } }
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); } } }
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()); } }
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); }
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 ASAssignmentExp( PAssignop _assignop_, PLvalue _lvalue_, PExp _exp_ ) { SetAssignop(_assignop_); SetLvalue(_lvalue_); SetExp(_exp_); }
public AIncDecExp( PLvalue _lvalue_, PIncDecOp _inc_dec_op_ ) { SetLvalue(_lvalue_); SetIncDecOp(_inc_dec_op_); }
public AShadySAssignmentExp( TConst _const_, IList _pre_pointers_, PLvalue _lvalue_, TLt _generic_token_, IList _generic_types_, IList _post_pointers_, IList _local_decl_right_ ) { SetConst(_const_); this._pre_pointers_ = new TypedList(new PrePointers_Cast(this)); this._pre_pointers_.Clear(); this._pre_pointers_.AddAll(_pre_pointers_); SetLvalue(_lvalue_); SetGenericToken(_generic_token_); this._generic_types_ = new TypedList(new GenericTypes_Cast(this)); this._generic_types_.Clear(); this._generic_types_.AddAll(_generic_types_); this._post_pointers_ = new TypedList(new PostPointers_Cast(this)); this._post_pointers_.Clear(); this._post_pointers_.AddAll(_post_pointers_); this._local_decl_right_ = new TypedList(new LocalDeclRight_Cast(this)); this._local_decl_right_.Clear(); this._local_decl_right_.AddAll(_local_decl_right_); }
internal override void RemoveChild(Node child) { if (_lvalue_ == child) { _lvalue_ = null; return; } if (_inc_dec_op_ == child) { _inc_dec_op_ = null; return; } }
public AAssignmentExp( TAssign _token_, PLvalue _lvalue_, PExp _exp_ ) { SetToken(_token_); SetLvalue(_lvalue_); SetExp(_exp_); }
private static Token GetToken(PLvalue lvalue) { if (lvalue is AStructLvalue) return ((AStructLvalue)lvalue).GetName(); if (lvalue is AArrayLvalue) return ((AArrayLvalue)lvalue).GetToken(); return (TIdentifier)((AAName)((AAmbiguousNameLvalue)lvalue).GetAmbiguous()).GetIdentifier()[0]; }
internal override void RemoveChild(Node child) { if (_token_ == child) { _token_ = null; return; } if (_name_ == child) { _name_ = null; return; } if (_args_.Contains(child)) { _args_.Remove(child); return; } }
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 }); }
internal override void RemoveChild(Node child) { if (_token_ == child) { _token_ = null; return; } if (_type_ == child) { _type_ = null; return; } if (_lvalue_ == child) { _lvalue_ = null; return; } }
private void MakeAssignmentRightDynamic(PLvalue leftSide, PExp rightSide, PType type, AABlock block, ref int index) { if (Util.IsBulkCopy(type)) { if (type is ANamedType) { ANamedType aType = (ANamedType)type; AStructDecl structDecl = data.StructTypeLinks[aType]; foreach (AALocalDecl localDecl in structDecl.GetLocals()) { AStructLvalue newleftSide = new AStructLvalue(new ALvalueExp(leftSide), new ADotDotType(new TDot(".")), new TIdentifier(localDecl.GetName().Text)); ABinopExp newrightSide = new ABinopExp(rightSide, new APlusBinop(new TPlus("+")), new AStringConstExp( new TStringLiteral("\"." + localDecl.GetName().Text + "\""))); data.ExpTypes[newrightSide] = data.ExpTypes[newrightSide.GetRight()] = new ANamedType(new TIdentifier("string"), null); data.ExpTypes[newleftSide.GetReceiver()] = type; data.LvalueTypes[newleftSide] = localDecl.GetType(); data.StructFieldLinks[newleftSide] = localDecl; MakeAssignmentRightDynamic(newleftSide, newrightSide, localDecl.GetType(), block, ref index); } } else {//Is array type. Can Only be a constant array type AArrayTempType aType = (AArrayTempType)type; for (int i = 0; i < int.Parse(aType.GetIntDim().Text); i++) { AArrayLvalue newleftSide = new AArrayLvalue(new TLBracket("["), new ALvalueExp(leftSide), new AIntConstExp(new TIntegerLiteral(i.ToString()))); ABinopExp newrightSide = new ABinopExp(rightSide, new APlusBinop(new TPlus("+")), new AStringConstExp( new TStringLiteral("\"[" + i + "]\""))); data.ExpTypes[newrightSide] = data.ExpTypes[newrightSide.GetRight()] = new ANamedType(new TIdentifier("string"), null); data.ExpTypes[newleftSide.GetBase()] = type; data.ExpTypes[newleftSide.GetIndex()] = new ANamedType(new TIdentifier("int"), null); data.LvalueTypes[newleftSide] = aType.GetType(); MakeAssignmentRightDynamic(newleftSide, newrightSide, aType.GetType(), block, ref index); } } } else { ANamedType aType;// = type is APointerType ? new ANamedType(new TIdentifier("string"), null) : (ANamedType)type; if (type is APointerType) { if (Util.IsIntPointer(type, ((APointerType)type).GetType(), data)) aType = new ANamedType(new TIdentifier("int"), null); else aType = new ANamedType(new TIdentifier("string"), null); } else { aType = (ANamedType) type; } string capitalType = Util.Capitalize(aType.AsIdentifierString());//Char.ToUpper(aType.GetName().Text[0]) + aType.GetName().Text.Substring(1); leftSide = Util.MakeClone(leftSide, data); rightSide = Util.MakeClone(rightSide, data); ABooleanConstExp trueConst1 = new ABooleanConstExp(new ATrueBool()); //ABooleanConstExp trueConst2 = new ABooleanConstExp(new ATrueBool()); ASimpleInvokeExp innerInvoke = new ASimpleInvokeExp(new TIdentifier("DataTableGet" + capitalType), new ArrayList() { trueConst1, rightSide }); //ASimpleInvokeExp outerInvoke = new ASimpleInvokeExp(new TIdentifier("DataTableSet" + capitalType), new ArrayList() { trueConst2, leftSide, innerInvoke }); AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), leftSide, innerInvoke); block.GetStatements().Insert(index, new AExpStm(new TSemicolon(";"), assignment)); index++; data.ExpTypes[trueConst1] = new ANamedType(new TIdentifier("bool"), null); data.ExpTypes[innerInvoke] = aType; data.ExpTypes[assignment] = aType; data.SimpleMethodLinks[innerInvoke] = data.Libraries.Methods.First(m => m.GetName().Text == innerInvoke.GetName().Text); } }
public void SetLvalue(PLvalue node) { if (_lvalue_ != null) { _lvalue_.Parent(null); } if (node != null) { if (node.Parent() != null) { node.Parent().RemoveChild(node); } node.Parent(this); } _lvalue_ = node; }
private PType LvalueToType(PLvalue lvalue, IList dynamicOpList, TLt genericToken, IList genericTypes) { PType type = LvalueToType(lvalue); if (genericToken != null) { type = new AGenericType(genericToken, type, new ArrayList()); while (genericTypes.Count > 0) { ((AGenericType) type).GetGenericTypes().Add(genericTypes[0]); } } foreach (PShadyDynamicOps op in dynamicOpList) { if (op is APointerShadyDynamicOps) { APointerShadyDynamicOps aop = (APointerShadyDynamicOps) op; type = new APointerType(aop.GetToken(), type); } else if (op is AArrayShadyDynamicOps) { AArrayShadyDynamicOps aop = (AArrayShadyDynamicOps)op; if (aop.GetExp() == null) type = new ADynamicArrayType(aop.GetToken(), type); else type = new AArrayTempType(aop.GetToken(), type, aop.GetExp(), null); } } return type; }
private PType LvalueToType(PLvalue lvalue) { if (lvalue is AStructLvalue) { /*AStructLvalue structLvalue = (AStructLvalue) lvalue; TIdentifier typeName = structLvalue.GetName(); if (structLvalue.GetReceiver() is ALvalueExp && ((ALvalueExp)structLvalue.GetReceiver()).GetLvalue() is AAmbiguousNameLvalue) { AAmbiguousNameLvalue ambiguousNameLvalue = (AAmbiguousNameLvalue) ((ALvalueExp) structLvalue.GetReceiver()).GetLvalue(); return new ANamedType(typeName, ((ASimpleName)ambiguousNameLvalue.GetAmbiguous()).GetIdentifier()); } else*/ { errors.Add(new ErrorCollection.Error(((AStructLvalue)lvalue).GetName(), currentSourceFile, LocRM.GetString("ErrorText177"))); throw new ParserException(null, null); } } if (lvalue is AArrayLvalue) { AArrayLvalue aLvalue = (AArrayLvalue)lvalue; if (!(aLvalue.GetBase() is ALvalueExp)) { errors.Add(new ErrorCollection.Error(GetToken(lvalue), currentSourceFile, LocRM.GetString("ErrorText176"))); throw new ParserException(null, null); } PLvalue newLvalue = ((ALvalueExp)aLvalue.GetBase()).GetLvalue(); return new AArrayTempType(aLvalue.GetToken(), LvalueToType(newLvalue), aLvalue.GetIndex(), null); } //it must be an AAmbiguousNameLvalue then return new ANamedType(((AAmbiguousNameLvalue)lvalue).GetAmbiguous()); }
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); }