public override void CaseAPointerLvalue(APointerLvalue node) { InAPointerLvalue(node); if (node.GetBase() != null) { node.GetBase().Apply(this); } if (node.GetTokens() != null) { node.GetTokens().Apply(this); } OutAPointerLvalue(node); }
public override void OutAPointerLvalue(APointerLvalue node) { PType type = data.ExpTypes[node.GetBase()]; if (!(type is APointerType)) { errors.Add(new ErrorCollection.Error(node.GetTokens(), currentSourceFile, LocRM.GetString("ErrorText149"))); data.LvalueTypes[node] = type; } else { data.LvalueTypes[node] = ((APointerType) type).GetType(); } base.OutAPointerLvalue(node); }
public override void CaseAPointerLvalue(APointerLvalue node) { //Build the list currentPointer.Clear(); base.CaseAPointerLvalue(node); //Todo: insert runtime check here //if (currentPointer.Count == 0 || !setPointers.Contains(MakePointer(currentPointer))) if (!isSet) { PStm pStm = Util.GetAncestor<PStm>(node); if (pStm != null) { AABlock pBlock = (AABlock) pStm.Parent(); /* * if (<pointer> == null) * { * UIDisplayMessage(PlayerGroupAll, messageAreaDebug, StringToText(<filename>[<lineNr>:<pos>] + " null pointer exception")); * int i = 1 / 0; * } */ AASourceFile currentSourceFile = Util.GetAncestor<AASourceFile>(node); node.GetBase().Apply(new MoveMethodDeclsOut("pointerVar", data)); PExp pointer = Util.MakeClone(node.GetBase(), data); ABinopExp cond = new ABinopExp(pointer, new AEqBinop(new TEq("==")), new ANullExp()); AABlock ifBlock = new AABlock(); ASimpleInvokeExp playerGroupAllInvoke = new ASimpleInvokeExp(new TIdentifier("PlayerGroupAll"), new ArrayList()); AFieldLvalue messageAreaDebugLink = new AFieldLvalue(new TIdentifier("c_messageAreaDebug")); ALvalueExp messageAreaDebugLinkExp = new ALvalueExp(messageAreaDebugLink); ASimpleInvokeExp stringToTextInvoke = new ASimpleInvokeExp(new TIdentifier("StringToText"), new ArrayList() { new AStringConstExp( new TStringLiteral("\"" + currentSourceFile . GetName () . Text + "[" + node. GetTokens () . Line + "," + node. GetTokens () . Pos + "]: Null pointer exception\"")) }); ASimpleInvokeExp displayMessageInvoke = new ASimpleInvokeExp( new TIdentifier("UIDisplayMessage"), new ArrayList() {playerGroupAllInvoke, messageAreaDebugLinkExp, stringToTextInvoke}); ifBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), displayMessageInvoke)); ABinopExp iDeclInit = new ABinopExp(new AIntConstExp(new TIntegerLiteral("1")), new ADivideBinop(new TDiv("/")), new AIntConstExp(new TIntegerLiteral("0"))); AALocalDecl iDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("int"), null), new TIdentifier("i"), iDeclInit); ifBlock.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), iDecl)); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), new AIfThenStm(new TLParen("("), cond, new ABlockStm(new TLBrace("{"), ifBlock))); data.Locals[ifBlock] = new List<AALocalDecl>(){iDecl}; data.ExpTypes[cond.GetRight()] = new ANullType(); data.ExpTypes[cond] = new ANamedType(new TIdentifier("bool"), null); data.ExpTypes[playerGroupAllInvoke] = new ANamedType(new TIdentifier("playergroup"), null); data.ExpTypes[messageAreaDebugLinkExp] = data.LvalueTypes[messageAreaDebugLink] = data.ExpTypes[iDeclInit] = data.ExpTypes[iDeclInit.GetLeft()] = data.ExpTypes[iDeclInit.GetRight()] = new ANamedType(new TIdentifier("int"), null); data.ExpTypes[stringToTextInvoke] = new ANamedType(new TIdentifier("text"), null); data.ExpTypes[(PExp) stringToTextInvoke.GetArgs()[0]] = new ANamedType(new TIdentifier("string"), null); data.ExpTypes[displayMessageInvoke] = new AVoidType(new TVoid("void")); data.SimpleMethodLinks[playerGroupAllInvoke] = data.Libraries.Methods.Find(m => m.GetName().Text == playerGroupAllInvoke.GetName().Text); data.SimpleMethodLinks[displayMessageInvoke] = data.Libraries.Methods.Find(m => m.GetName().Text == displayMessageInvoke.GetName().Text); data.SimpleMethodLinks[stringToTextInvoke] = data.Libraries.Methods.Find(m => m.GetName().Text == stringToTextInvoke.GetName().Text); data.FieldLinks[messageAreaDebugLink] = data.Libraries.Fields.Find(f => f.GetName().Text == messageAreaDebugLink.GetName().Text); if (currentPointer.Count > 0) { setPointers.Add(MakePointer(currentPointer)); } } } currentPointer.Add(new PointerPointer()); currentPointer = MakePointer(currentPointer); isSet = Contains(setPointers, currentPointer); isExposed = Contains(exposedPointers, currentPointer); //If the currentPointer is in null pointers, report error.. and then again - we might not reach this statement - warning, and runtime check //If the currentPointer is not in setPointers, insert runtime check }
public override void CaseAPointerLvalue(APointerLvalue node) { hadPointer = false; nameExp = null; base.CaseAPointerLvalue(node); if (Util.IsIntPointer(node, data.LvalueTypes[node], data)) { if (nameExp != null) { //Create a data table get string exp nameExp = CreateDynaicGetStm("int"); } else { nameExp = node.GetBase(); } //Replace by str_Array[<base>]; //If this is a compared pointer, replace it by str_Array[<base> >> <<bitsLeft>>] GlobalStructVars vars; int allocateLimit; if (data.EnrichmentTypeLinks.ContainsKey(data.LvalueTypes[node])) { AEnrichmentDecl enrichmentDecl = data.EnrichmentTypeLinks[data.LvalueTypes[node]]; vars = CreateEnrichmentFields(node, enrichmentDecl, data); allocateLimit = int.Parse(enrichmentDecl.GetIntDim().Text); } else { AStructDecl structDecl = data.StructTypeLinks[(ANamedType)data.LvalueTypes[node]]; vars = CreateStructFields(node, structDecl, data); allocateLimit = int.Parse(structDecl.GetIntDim().Text); } AFieldLvalue array = new AFieldLvalue(new TIdentifier(vars.Array.GetName().Text)); ALvalueExp arrayExp = new ALvalueExp(array); PExp arrayIndex = nameExp; if (vars.IdentifierArray != null) { int usedBits = allocateLimit == 0 ? 0 : ((int)Math.Floor(Math.Log(allocateLimit, 2)) + 1); int bitsLeft = 31 - usedBits; int biggestIdentifier = (1 << (bitsLeft + 1)) - 1; AIntConstExp bitsLeftConst = new AIntConstExp(new TIntegerLiteral(bitsLeft.ToString())); arrayIndex = new ABinopExp(arrayIndex, new ARBitShiftBinop(new TRBitShift(">>")), bitsLeftConst); data.ExpTypes[bitsLeftConst] = data.ExpTypes[arrayIndex] = new ANamedType(new TIdentifier("int"), null); } AArrayLvalue replacer = new AArrayLvalue(new TLBracket("["), arrayExp, arrayIndex); node.ReplaceBy(replacer); data.FieldLinks[array] = vars.Array; data.LvalueTypes[array] = data.ExpTypes[arrayExp] = vars.Array.GetType(); data.LvalueTypes[replacer] = ((AArrayTempType) vars.Array.GetType()).GetType(); hadPointer = false; nameExp = null; return; } //if (Util.GetAncestor<AAssignmentExp>(node) != null || Util.GetAncestor<APArrayLengthLvalue>(node) != null) { if (nameExp != null) { //Create a data table get string exp nameExp = CreateDynaicGetStm("string"); } else { nameExp = node.GetBase(); } } hadPointer = true; CheckDynamicLvalue(node); //Todo: Insert runtime check that the pointer is actually not null / points to a delete object //(unless the same pointer was checked before, and it has not been assigned to since, and there has not been a method call since. }
public override void OutAPointerLvalue(APointerLvalue node) { data.LvalueTypes[node] = ((APointerType) data.ExpTypes[node.GetBase()]).GetType(); }