public override void OutAFieldLvalue(AFieldLvalue node) { node.GetName().Text = finalTrans.data.FieldLinks[node].GetName().Text; }
public override void OutAFieldLvalue(AFieldLvalue node) { if (folding) { AFieldDecl field = data.FieldLinks[node]; //Must be int and must be const if (!(field.GetType() is ANamedType && ((ANamedType)field.GetType()).IsPrimitive("int"))) { errors.Add( new ErrorCollection.Error(node.GetName(), LocRM.GetString("ErrorText59"), false, new ErrorCollection.Error(field.GetName(), LocRM.GetString("ErrorText60"))), true); throw new ParserException(node.GetName(), "TypeLinking.OutAFieldLvalue"); } if (field.GetConst() == null) { if (!isANewExp) { errors.Add( new ErrorCollection.Error(node.GetName(), LocRM.GetString("ErrorText61"), false), true); throw new ParserException(node.GetName(), "TypeLinking.OutAFieldLvalue"); } } if (field.GetInit() == null)//An error will be given earlier - constant fields must have an initializer { throw new ParserException(node.GetName(), "TypeLinking.OutAFieldLvalue"); } field.GetInit().Apply(this); } else base.OutAFieldLvalue(node); }
public override void CaseAFieldLvalue(AFieldLvalue node) { InAFieldLvalue(node); if (node.GetName() != null) { node.GetName().Apply(this); } OutAFieldLvalue(node); }
public override void CaseAFieldLvalue(AFieldLvalue node) { Item currentFile = GetIncludeItem(node); //If the field is in a moved field, refference that field instead if (Util.GetAncestor<AFieldDecl>(node) != null) { Item i = allItems.OfType<FieldItem>().FirstOrDefault(item => item.FieldDecl == Util.GetAncestor<AFieldDecl>(node)); if (i != null) currentFile = i; } AFieldDecl decl = finalTrans.data.FieldLinks[node]; Item declItem = ((Item) allItems.OfType<FieldItem>().FirstOrDefault(item => item.FieldDecl == decl)) ?? allItems.OfType<IncludeItem>().First( item => item.Current == Util.GetAncestor<AASourceFile>(decl)); List<Item> cPath = currentFile.Path; List<Item> dPath = declItem.Path; bool movedIt = false; for (int i = 0; i < Math.Min(cPath.Count, dPath.Count); i++) { if (cPath[i] != dPath[i]) { //We have a fork. make sure that the field is visible int cI = cPath[i - 1].Children.IndexOf(cPath[i]); int dI = dPath[i - 1].Children.IndexOf(dPath[i]); if (dI < cI) {//The decl is okay break; } //Move the decl up if (declItem is FieldItem) { declItem.Parent.Children.Remove(declItem); declItem.Parent = cPath[i - 1]; } else { declItem = new FieldItem(decl, cPath[i - 1], new List<Item>()); allItems.Add(declItem); } cPath[i - 1].Children.Insert(cI, declItem); movedIt = true; break; } if (i == cPath.Count - 1) { if (i == dPath.Count - 1) { //The decl and use is in same file. Ensure that the decl is before if (Util.TokenLessThan(decl.GetName(), node.GetName())) break; //Add the decl item declItem = new FieldItem(decl, cPath[i], new List<Item>()); allItems.Add(declItem); cPath[i].Children.Add(declItem); movedIt = true; break; } else { //The decl is included here or somewhere deeper. But above the use break; } } else if (i == dPath.Count - 1) { //We have reached the file where the decl is, but the use is included deeper, so it is above. Insert decl int cI = cPath[i].Children.IndexOf(cPath[i + 1]); declItem = new FieldItem(decl, cPath[i], new List<Item>()); allItems.Add(declItem); cPath[i].Children.Insert(cI, declItem); movedIt = true; break; } } //In case we have a struct type field, we must make sure the struct is still on top if (movedIt) CaseAFieldDecl(decl); base.CaseAFieldLvalue(node); }
public override void OutACastExp(ACastExp node) { string toType = ((AAName)((ANamedType) node.GetType()).GetName()).AsString(); string fromType; PType fromPType = data.ExpTypes[node.GetExp()]; AStructDecl toEnum = null; AStructDecl fromEnum = null; if (data.StructTypeLinks.ContainsKey((ANamedType)node.GetType())) { AStructDecl str = data.StructTypeLinks[(ANamedType)node.GetType()]; if (data.Enums.ContainsKey(str)) toEnum = str; } if (fromPType is ANamedType) { fromType = ((AAName)((ANamedType)fromPType).GetName()).AsString(); //Namespace ignored if (data.StructTypeLinks.ContainsKey((ANamedType) fromPType)) { AStructDecl str = data.StructTypeLinks[(ANamedType) fromPType]; if (data.Enums.ContainsKey(str)) fromEnum = str; } } else { errors.Add(new ErrorCollection.Error(node.GetToken(), currentSourceFile, LocRM.GetString("ErrorText121"))); throw new ParserException(node.GetToken(), "Invalid cast"); } if (toEnum != null && (fromType == "int" || fromType == "byte")) { ANamedType type = new ANamedType(new TIdentifier(toEnum.GetName().Text), null); data.StructTypeLinks[type] = toEnum; data.ExpTypes[node.GetExp()] = type; node.ReplaceBy(node.GetExp()); return; } if (fromEnum != null && (toType == "int" || toType == "byte")) { int enumDefinitions = 0; foreach (PLocalDecl local in fromEnum.GetLocals()) { if (local is AALocalDecl) enumDefinitions++; } string typeName = enumDefinitions > 255 ? "int" : "byte"; ANamedType type = new ANamedType(new TIdentifier(typeName), null); data.ExpTypes[node.GetExp()] = new ANamedType(new TIdentifier(typeName), null); node.ReplaceBy(node.GetExp()); return; } if (fromEnum != null && toType == "string") { AMethodDecl targetMethod = data.StructMethods[fromEnum][0]; ASimpleInvokeExp invokeExp = new ASimpleInvokeExp(new TIdentifier("toString"), new ArrayList(){node.GetExp()}); data.SimpleMethodLinks[invokeExp] = targetMethod; data.ExpTypes[invokeExp] = targetMethod.GetReturnType(); node.ReplaceBy(invokeExp); return; } ASimpleInvokeExp replacementMethod = null; switch (toType) { case "string": switch (fromType) { case "wave": replacementMethod = new ASimpleInvokeExp(new TIdentifier("AIWaveToString"), new ArrayList{node.GetExp()}); break; case "fixed"://Implicit AFieldLvalue precisionArg = new AFieldLvalue(new TIdentifier("c_fixedPrecisionAny")); ALvalueExp exp = new ALvalueExp(precisionArg); data.FieldLinks[precisionArg] = data.Libraries.Fields.First(field => field.GetName().Text == precisionArg.GetName().Text); replacementMethod = new ASimpleInvokeExp(new TIdentifier("FixedToString"), new ArrayList { node.GetExp(), exp}); break; case "int"://Implicit case "byte"://Implicit replacementMethod = new ASimpleInvokeExp(new TIdentifier("IntToString"), new ArrayList { node.GetExp()}); break; case "bool"://Implicit replacementMethod = new ASimpleInvokeExp(new TIdentifier("libNtve_gf_ConvertBooleanToString"), new ArrayList { node.GetExp() }); break; case "color"://Implicit replacementMethod = new ASimpleInvokeExp(new TIdentifier("libNtve_gf_ConvertColorToString"), new ArrayList { node.GetExp() }); break; } break; case "text": switch (fromType) { case "wave": replacementMethod = new ASimpleInvokeExp(new TIdentifier("AIWaveToText"), new ArrayList { node.GetExp() }); break; case "fixed"://Implicit AFieldLvalue precisionArg = new AFieldLvalue(new TIdentifier("c_fixedPrecisionAny")); ALvalueExp exp = new ALvalueExp(precisionArg); data.FieldLinks[precisionArg] = data.Libraries.Fields.First(field => field.GetName().Text == precisionArg.GetName().Text); replacementMethod = new ASimpleInvokeExp(new TIdentifier("FixedToText"), new ArrayList { node.GetExp(), exp }); break; case "int"://Implicit case "byte": replacementMethod = new ASimpleInvokeExp(new TIdentifier("IntToText"), new ArrayList { node.GetExp() }); break; case "bool"://Implicit replacementMethod = new ASimpleInvokeExp(new TIdentifier("libNtve_gf_ConvertBooleanToText"), new ArrayList { node.GetExp() }); break; case "string"://Implicit replacementMethod = new ASimpleInvokeExp(new TIdentifier("StringToText"), new ArrayList { node.GetExp() }); break; } break; case "int": switch (fromType) { case "bool": replacementMethod = new ASimpleInvokeExp(new TIdentifier("BoolToInt"), new ArrayList {node.GetExp()}); break; case "fixed": replacementMethod = new ASimpleInvokeExp(new TIdentifier("FixedToInt"), new ArrayList { node.GetExp() }); break; case "string": replacementMethod = new ASimpleInvokeExp(new TIdentifier("StringToInt"), new ArrayList { node.GetExp() }); break; } break; case "fixed": switch (fromType) { case "int"://Already implicit replacementMethod = new ASimpleInvokeExp(new TIdentifier("IntToFixed"), new ArrayList { node.GetExp() }); break; case "string": replacementMethod = new ASimpleInvokeExp(new TIdentifier("StringToFixed"), new ArrayList { node.GetExp() }); break; } break; case "bool": switch (fromType) { case "int": case "byte": case "fixed": //Replace by //exp != 0 AIntConstExp zero = new AIntConstExp(new TIntegerLiteral("0")); ABinopExp binop = new ABinopExp(node.GetExp(), new ANeBinop(new TNeq("!=")), zero); node.ReplaceBy(binop); binop.Apply(this); return; } break; } if (replacementMethod == null) { errors.Add(new ErrorCollection.Error(node.GetToken(), currentSourceFile, LocRM.GetString("ErrorText122") + fromType + LocRM.GetString("ErrorText123") + toType)); throw new ParserException(node.GetToken(), LocRM.GetString("ErrorText121")); } data.SimpleMethodLinks[replacementMethod] = data.Libraries.Methods.First(method => method.GetName().Text == replacementMethod.GetName().Text); data.ExpTypes[replacementMethod] = data.SimpleMethodLinks[replacementMethod].GetReturnType(); node.ReplaceBy(replacementMethod); for (int i = 1; i < replacementMethod.GetArgs().Count; i++) { ((Node)replacementMethod.GetArgs()[i]).Apply(this); } }
public override void OutAFieldLvalue(AFieldLvalue node) { //Since we dont apply the replacement of an OutAAmbiguousNameLvalue, this node needs a link //Eks: global.<field> //Look for fields AFieldDecl f = null; APropertyDecl p = null; if (data.FieldLinks.ContainsKey(node)) f = data.FieldLinks[node]; else //if (!data.FieldLinks.ContainsKey(node) ) { List<IList> visibleDecls = Util.GetVisibleDecls(node, true); AASourceFile currentFile = Util.GetAncestor<AASourceFile>(node); List<string> currentNamespace = Util.GetFullNamespace(node); List<PDecl> candidates = new List<PDecl>(); foreach (IList declList in visibleDecls) { bool isSameFile = false; bool isSameNamespace = false; if (declList.Count > 0) { isSameFile = currentFile == Util.GetAncestor<AASourceFile>((PDecl)declList[0]); isSameNamespace = Util.NamespacesEquals(currentNamespace, Util.GetFullNamespace((PDecl)declList[0])); } foreach (PDecl decl in declList) { if (decl is AFieldDecl) { AFieldDecl field = (AFieldDecl)decl; if (field.GetName().Text != node.GetName().Text) continue; if (!isSameNamespace && field.GetVisibilityModifier() is APrivateVisibilityModifier || !isSameFile && field.GetStatic() != null) continue; candidates.Add(decl); } else if (decl is APropertyDecl) { APropertyDecl property = (APropertyDecl)decl; if (property.GetName().Text != node.GetName().Text) continue; if (!isSameNamespace && property.GetVisibilityModifier() is APrivateVisibilityModifier || !isSameFile && property.GetStatic() != null) continue; candidates.Add(decl); } } } //Lib fields foreach (AFieldDecl field in data.Libraries.Fields) { if (field.GetName().Text != node.GetName().Text) continue; if (field.GetStatic() != null) continue; candidates.Add(field); } if (candidates.Count == 0) { errors.Add( new ErrorCollection.Error(node.GetName(), currentSourceFile, node.GetName().Text + LocRM.GetString("ErrorText169"), false), true); throw new ParserException(node.GetName(), "TypeLinking.OutAFieldLvalue"); } if (candidates.Count > 1) { List<ErrorCollection.Error> subErrors = new List<ErrorCollection.Error>(); foreach (PDecl decl in candidates) { if (decl is AFieldDecl) { AFieldDecl field = (AFieldDecl)decl; subErrors.Add(new ErrorCollection.Error(field.GetName(), LocRM.GetString("ErrorText60"))); } else if (decl is APropertyDecl) { APropertyDecl field = (APropertyDecl)decl; subErrors.Add(new ErrorCollection.Error(field.GetName(), LocRM.GetString("ErrorText62"))); } } errors.Add( new ErrorCollection.Error(node.GetName(), currentSourceFile, node.GetName().Text + LocRM.GetString("ErrorText170"), false, subErrors.ToArray()), true); throw new ParserException(node.GetName(), "TypeLinking.OutAFieldLvalue"); } if (candidates[0] is AFieldDecl) f = (AFieldDecl)candidates[0]; else p = (APropertyDecl)candidates[0]; } if (f != null) { AFieldDecl field = f; data.FieldLinks[node] = field; if (foldIntegerConstants) { if (!(field.GetType() is ANamedType && ((ANamedType)field.GetType()).IsPrimitive("int"))) { errors.Add( new ErrorCollection.Error(node.GetName(), LocRM.GetString("ErrorText168"), false, new ErrorCollection.Error(field.GetName(), LocRM.GetString("ErrorText60"))), true); throw new ParserException(node.GetName(), "TypeLinking.OutAFieldLvalue"); } if (field.GetConst() == null) { foldingFailed = true; if (!isInANewExp) { errors.Add( new ErrorCollection.Error(node.GetName(), currentSourceFile, LocRM.GetString("ErrorText168"), false), true); throw new ParserException(node.GetName(), "TypeLinking.OutAFieldLvalue"); } } if (field.GetInit() == null)//An error will be given earlier - constant fields must have an initializer { throw new ParserException(node.GetName(), "TypeLinking.OutAFieldLvalue"); } field.GetInit().Apply(this); } } else { APropertyDecl property = p; APropertyLvalue replacer = new APropertyLvalue(node.GetName()); data.PropertyLinks.Add(replacer, property); node.ReplaceBy(replacer); data.PropertyLinks[replacer] = property; if (foldIntegerConstants) { foldingFailed = true; if (isInANewExp) { errors.Add( new ErrorCollection.Error(node.GetName(), currentSourceFile, LocRM.GetString("ErrorText168"), false), true); throw new ParserException(node.GetName(), "TypeLinking.OutAFieldLvalue"); } } } }
public override void CaseADelegateInvokeExp(ADelegateInvokeExp node) { //Build a list of the possible methods AASourceFile currentFile = Util.GetAncestor<AASourceFile>(node); List<AMethodDecl> methods = new List<AMethodDecl>(); ANamedType type = (ANamedType) finalTrans.data.ExpTypes[node.GetReceiver()]; AMethodDecl delegateMethod = finalTrans.data.DelegateTypeLinks[type]; foreach (KeyValuePair<ADelegateExp, AMethodDecl> delegateCreationPair in finalTrans.data.DelegateCreationMethod) { if (TypeChecking.Assignable(delegateCreationPair.Key.GetType(), type)) { if (!methods.Contains(delegateCreationPair.Value)) methods.Add(delegateCreationPair.Value); } } MoveMethodDeclsOut mover; if (methods.Count == 0) { //Can only remove it if the return value is unused if (!(node.Parent() is AExpStm)) { finalTrans.errors.Add(new ErrorCollection.Error(node.GetToken(), currentFile, LocRM.GetString("Delegates_Text1"))); throw new ParserException(node.GetToken(), "Delegates.OutADelegateInvokeExp"); } mover = new MoveMethodDeclsOut("delegateVar", finalTrans.data); foreach (Node arg in node.GetArgs()) { arg.Apply(mover); } node.Parent().Parent().RemoveChild(node.Parent()); foreach (PStm stm in mover.NewStatements) { stm.Apply(this); } return; } if (methods.Count == 1) { ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("renameMe"), new ArrayList()); while (node.GetArgs().Count > 0) { invoke.GetArgs().Add(node.GetArgs()[0]); } //If we have a struct method, add the pointer from the delegate if (finalTrans.data.StructMethods.Any(str => str.Value.Contains(methods[0]))) { AStructDecl targetStr = finalTrans.data.StructMethods.First(str => str.Value.Contains(methods[0])).Key; AMethodDecl getPointerDecl = GetPointerMethod(targetStr.GetDimention() != null); ASimpleInvokeExp getPointerInvoke = new ASimpleInvokeExp(new TIdentifier("renameMe"), new ArrayList(){node.GetReceiver()}); invoke.GetArgs().Add(getPointerInvoke); finalTrans.data.SimpleMethodLinks[getPointerInvoke] = getPointerDecl; finalTrans.data.ExpTypes[getPointerInvoke] = getPointerDecl.GetReturnType(); } finalTrans.data.SimpleMethodLinks[invoke] = methods[0]; finalTrans.data.ExpTypes[invoke] = methods[0].GetReturnType(); node.ReplaceBy(invoke); return; } //Multiple methods. Make /* * <Methods moved out from reciever> * string delegate = GetMethodPart(<reciever>); * if (delegate == "...") * { * Foo(...); * } * else if (delegate == "...") * { * Bar(..., GetPointerPart(<reciever>); * } * else if(...) * ... * else * { * UIDisplayMessage(PlayerGroupAll(), c_messageAreaDebug, StringToText("[<file>:<line>]: No methods matched delegate.")); * int i = 1/0; * return; * } * */ AABlock block = new AABlock(new ArrayList(), new TRBrace("}")); mover = new MoveMethodDeclsOut("delegateVar", finalTrans.data); node.GetReceiver().Apply(mover); AMethodDecl methodPartMethod = GetMethodMethod(); ASimpleInvokeExp methodPartInvoke = new ASimpleInvokeExp(new TIdentifier("GetMethodPart"), new ArrayList() { Util.MakeClone(node.GetReceiver(), finalTrans.data) }); finalTrans.data.SimpleMethodLinks[methodPartInvoke] = methodPartMethod; finalTrans.data.ExpTypes[methodPartInvoke] = methodPartMethod.GetReturnType(); AALocalDecl methodPartDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("string"), null), new TIdentifier("methodPart"), methodPartInvoke); block.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), methodPartDecl)); //If the invoke's return value is used, get the lvalue PLvalue leftSide; if (node.Parent() is AALocalDecl) { leftSide = new ALocalLvalue(new TIdentifier("renameMe")); finalTrans.data.LocalLinks[(ALocalLvalue) leftSide] = (AALocalDecl) node.Parent(); finalTrans.data.LvalueTypes[leftSide] = new ANamedType(new TIdentifier("string"), null); PStm pStm = Util.GetAncestor<PStm>(node); AABlock pBlock = (AABlock) pStm.Parent(); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm) + 1, new ABlockStm(new TLBrace("{"), block)); node.Parent().RemoveChild(node); } else if (node.Parent() is AAssignmentExp) { AAssignmentExp assignExp = (AAssignmentExp) node.Parent(); leftSide = assignExp.GetLvalue(); leftSide.Apply(mover); PStm pStm = Util.GetAncestor<PStm>(node); pStm.ReplaceBy(new ABlockStm(new TLBrace("{"), block)); } else if (node.Parent() is AExpStm) { //No assignments needed leftSide = null; node.Parent().ReplaceBy(new ABlockStm(new TLBrace("{"), block)); } else { //Create a new local AALocalDecl leftSideDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(delegateMethod.GetReturnType(), finalTrans.data), new TIdentifier("delegateVar"), null); ALocalLvalue leftSideLink = new ALocalLvalue(new TIdentifier("delegateVar")); ALvalueExp leftSideLinkExp = new ALvalueExp(leftSideLink); PStm pStm = Util.GetAncestor<PStm>(node); AABlock pBlock = (AABlock)pStm.Parent(); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), new ABlockStm(new TLBrace("{"), block)); node.ReplaceBy(leftSideLinkExp); finalTrans.data.LocalLinks[leftSideLink] = leftSideDecl; finalTrans.data.LvalueTypes[leftSideLink] = finalTrans.data.ExpTypes[leftSideLinkExp] = leftSideDecl.GetType(); leftSide = leftSideLink; block.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), leftSideDecl)); } ABlockStm elseBranch; //Make final else branch /* { * UIDisplayMessage(PlayerGroupAll(), c_messageAreaDebug, StringToText("<file>[<line>, <pos>]: No methods matched delegate.")); * IntToString(1/0); * return; * } */ { AABlock innerBlock = new AABlock(new ArrayList(), new TRBrace("}")); ASimpleInvokeExp playerGroupInvoke = new ASimpleInvokeExp(new TIdentifier("PlayerGroupAll"), new ArrayList()); AFieldLvalue messageAreaLink = new AFieldLvalue(new TIdentifier("c_messageAreaDebug")); ALvalueExp messageAreaLinkExp = new ALvalueExp(messageAreaLink); AStringConstExp stringConst = new AStringConstExp( new TStringLiteral("\"" + currentFile.GetName().Text.Replace('\\', '/') + "[" + node.GetToken().Line + ", " + node.GetToken().Pos + "]: Got a null delegate.\"")); ASimpleInvokeExp stringToTextInvoke = new ASimpleInvokeExp(new TIdentifier("StringToText"), new ArrayList() {stringConst}); ASimpleInvokeExp displayMessageInvoke = new ASimpleInvokeExp(new TIdentifier("UIDisplayMessage"), new ArrayList() { playerGroupInvoke, messageAreaLinkExp, stringToTextInvoke }); AIntConstExp intConst1 = new AIntConstExp(new TIntegerLiteral("1")); AIntConstExp intConst2 = new AIntConstExp(new TIntegerLiteral("0")); ABinopExp binop = new ABinopExp(intConst1, new ADivideBinop(new TDiv("/")), intConst2); ASimpleInvokeExp intToStringInvoke = new ASimpleInvokeExp(new TIdentifier("IntToString"), new ArrayList() {binop}); innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), displayMessageInvoke)); innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), intToStringInvoke)); //innerBlock.GetStatements().Add(new AVoidReturnStm(new TReturn("return"))); elseBranch = new ABlockStm(new TLBrace("{"), innerBlock); finalTrans.data.SimpleMethodLinks[playerGroupInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == playerGroupInvoke.GetName().Text); finalTrans.data.SimpleMethodLinks[stringToTextInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringToTextInvoke.GetName().Text); finalTrans.data.SimpleMethodLinks[displayMessageInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == displayMessageInvoke.GetName().Text); finalTrans.data.SimpleMethodLinks[intToStringInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == intToStringInvoke.GetName().Text); finalTrans.data.FieldLinks[messageAreaLink] = finalTrans.data.Libraries.Fields.First(m => m.GetName().Text == messageAreaLink.GetName().Text); finalTrans.data.ExpTypes[playerGroupInvoke] = finalTrans.data.SimpleMethodLinks[playerGroupInvoke].GetReturnType(); finalTrans.data.LvalueTypes[messageAreaLink] = finalTrans.data.ExpTypes[messageAreaLinkExp] = finalTrans.data.FieldLinks[messageAreaLink].GetType(); finalTrans.data.ExpTypes[stringToTextInvoke] = finalTrans.data.SimpleMethodLinks[stringToTextInvoke].GetReturnType(); finalTrans.data.ExpTypes[stringConst] = finalTrans.data.ExpTypes[intToStringInvoke] = new ANamedType(new TIdentifier("string"), null); finalTrans.data.ExpTypes[displayMessageInvoke] = new AVoidType(); finalTrans.data.ExpTypes[intConst1] = finalTrans.data.ExpTypes[intConst2] = finalTrans.data.ExpTypes[binop] = new ANamedType(new TIdentifier("int"), null); } foreach (AMethodDecl method in methods) { /* * if (delegate == "...") * { * Foo(...); * } * else if (delegate == "...") * { * Bar(..., GetPointerPart(<reciever>); * } * else if(...) * ... */ AABlock innerBlock = new AABlock(new ArrayList(), new TRBrace("}")); ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier(method.GetName().Text), new ArrayList()); for (int i = 0; i < node.GetArgs().Count; i++) { PExp arg = (PExp) node.GetArgs()[i]; invoke.GetArgs().Add(Util.MakeClone(arg, finalTrans.data)); } //If we have a struct method, add the pointer from the delegate if (finalTrans.data.StructMethods.Any(str => str.Value.Contains(method))) { AStructDecl targetStr = finalTrans.data.StructMethods.First(str => str.Value.Contains(method)).Key; AMethodDecl getPointerDecl = GetPointerMethod(targetStr.GetDimention() != null); ASimpleInvokeExp getPointerInvoke = new ASimpleInvokeExp(new TIdentifier("renameMe"), new ArrayList() { Util.MakeClone(node.GetReceiver(), data) }); invoke.GetArgs().Add(getPointerInvoke); finalTrans.data.SimpleMethodLinks[getPointerInvoke] = getPointerDecl; finalTrans.data.ExpTypes[getPointerInvoke] = getPointerDecl.GetReturnType(); } finalTrans.data.SimpleMethodLinks[invoke] = method; finalTrans.data.ExpTypes[invoke] = method.GetReturnType(); if (leftSide == null) { innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), invoke)); } else { AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), Util.MakeClone(leftSide, finalTrans.data), invoke); finalTrans.data.ExpTypes[assignment] = finalTrans.data.ExpTypes[invoke]; innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment)); } ALocalLvalue methodPartLink = new ALocalLvalue(new TIdentifier("methodPart")); ALvalueExp methodPartLinkExp = new ALvalueExp(methodPartLink); AStringConstExp stringConst = new AStringConstExp(new TStringLiteral("\"" + GetName(method) + "\"")); finalTrans.data.LocalLinks[methodPartLink] = methodPartDecl; finalTrans.data.LvalueTypes[methodPartLink] = finalTrans.data.ExpTypes[methodPartLinkExp] = finalTrans.data.ExpTypes[stringConst] = new ANamedType(new TIdentifier("string"), null); ABinopExp binop = new ABinopExp(methodPartLinkExp, new AEqBinop(new TEq("==")), stringConst); finalTrans.data.ExpTypes[binop] = new ANamedType(new TIdentifier("bool"), null); AIfThenElseStm ifThenElse = new AIfThenElseStm(new TLParen("("), binop, new ABlockStm(new TLBrace("{"), innerBlock), elseBranch); elseBranch = new ABlockStm(new TLBrace("{"), new AABlock(new ArrayList() { ifThenElse }, new TRBrace("}"))); } block.GetStatements().Add(elseBranch); }
private static AMethodDecl CreateNewObjectMethodP(Node node, TIntegerLiteral intLiteral, string prefix, GlobalStructVars vars, SharedData data) { if (intLiteral == null) return CreateNewObjectMethod(node, data); //if (createStructMethod.ContainsKey(structDecl)) // return createStructMethod[structDecl]; /* int CreateStr() { int i = Str_index; while (Str_used[i / 31] & 1 << (i % 31)) { i = i + 1; if (i >= 42) { i = 0; } if (i == Str_index) { UIDisplayMessage(PlayerGroupAll(), c_messageAreaDebug, StringToText("Error: Unable to allocate more than 42 dynamic Str types")); IntToString(1/0); } } Str_used[i / 31] = Str_used[i / 31] + Power2(i % 31); Str_index = i; <<if it is being compared with null at any point in time>> <<usedBits := floor(log2(42))+1>> <<bitsLeft := 31 - usedBits>> <<biggestIdentifier := 2^(bitsLeft + 1) - 1>> identifierArray[i] = identifierNext; i = (i << bitsLeft) + identifierNext; identifierNext = identifierNext%biggestIdentifier + 1; return i; } */ AASourceFile file = Util.GetAncestor<AASourceFile>(node); AIntConstExp intConst1 = new AIntConstExp(new TIntegerLiteral("31")); AIntConstExp intConst2 = new AIntConstExp(new TIntegerLiteral("31")); AIntConstExp intConst3 = new AIntConstExp(new TIntegerLiteral("1")); AIntConstExp intConst4 = new AIntConstExp(new TIntegerLiteral(intLiteral.Text)); AIntConstExp intConst5 = new AIntConstExp(new TIntegerLiteral("0")); AIntConstExp intConst6 = new AIntConstExp(new TIntegerLiteral("1")); AIntConstExp intConst7 = new AIntConstExp(new TIntegerLiteral("0")); AIntConstExp intConst8 = new AIntConstExp(new TIntegerLiteral("31")); AIntConstExp intConst9 = new AIntConstExp(new TIntegerLiteral("31")); AIntConstExp intConst10 = new AIntConstExp(new TIntegerLiteral("31")); AIntConstExp intConst11 = new AIntConstExp(new TIntegerLiteral("1")); AIntConstExp intConst12 = new AIntConstExp(new TIntegerLiteral("1")); AFieldLvalue strIndexRef1 = new AFieldLvalue(new TIdentifier(vars.Index.GetName().Text)); AFieldLvalue strIndexRef2 = new AFieldLvalue(new TIdentifier(vars.Index.GetName().Text)); AFieldLvalue strIndexRef3 = new AFieldLvalue(new TIdentifier(vars.Index.GetName().Text)); ALvalueExp strIndexRef1Exp = new ALvalueExp(strIndexRef1); ALvalueExp strIndexRef2Exp = new ALvalueExp(strIndexRef2); AFieldLvalue strUsedRef1 = new AFieldLvalue(new TIdentifier(vars.Used.GetName().Text)); AFieldLvalue strUsedRef2 = new AFieldLvalue(new TIdentifier(vars.Used.GetName().Text)); AFieldLvalue strUsedRef3 = new AFieldLvalue(new TIdentifier(vars.Used.GetName().Text)); ALvalueExp strUsedRef1Exp = new ALvalueExp(strUsedRef1); ALvalueExp strUsedRef2Exp = new ALvalueExp(strUsedRef2); ALvalueExp strUsedRef3Exp = new ALvalueExp(strUsedRef3); AALocalDecl iDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("int"), null), new TIdentifier("i"), strIndexRef1Exp); ALocalLvalue iRef1 = new ALocalLvalue(new TIdentifier("i")); ALocalLvalue iRef2 = new ALocalLvalue(new TIdentifier("i")); ALocalLvalue iRef3 = new ALocalLvalue(new TIdentifier("i")); ALocalLvalue iRef4 = new ALocalLvalue(new TIdentifier("i")); ALocalLvalue iRef5 = new ALocalLvalue(new TIdentifier("i")); ALocalLvalue iRef6 = new ALocalLvalue(new TIdentifier("i")); ALocalLvalue iRef7 = new ALocalLvalue(new TIdentifier("i")); ALocalLvalue iRef8 = new ALocalLvalue(new TIdentifier("i")); ALocalLvalue iRef9 = new ALocalLvalue(new TIdentifier("i")); ALocalLvalue iRef10 = new ALocalLvalue(new TIdentifier("i")); ALocalLvalue iRef11 = new ALocalLvalue(new TIdentifier("i")); ALocalLvalue iRef12 = new ALocalLvalue(new TIdentifier("i")); ALvalueExp iRef1Exp = new ALvalueExp(iRef1); ALvalueExp iRef2Exp = new ALvalueExp(iRef2); ALvalueExp iRef4Exp = new ALvalueExp(iRef4); ALvalueExp iRef5Exp = new ALvalueExp(iRef5); ALvalueExp iRef7Exp = new ALvalueExp(iRef7); ALvalueExp iRef8Exp = new ALvalueExp(iRef8); ALvalueExp iRef9Exp = new ALvalueExp(iRef9); ALvalueExp iRef10Exp = new ALvalueExp(iRef10); ALvalueExp iRef11Exp = new ALvalueExp(iRef11); ALvalueExp iRef12Exp = new ALvalueExp(iRef12); ABinopExp binop1 = new ABinopExp(iRef1Exp, new ADivideBinop(new TDiv("/")), intConst1); ABinopExp binop2 = new ABinopExp(iRef2Exp, new AModuloBinop(new TMod("%")), intConst2); ABinopExp binop3 = new ABinopExp(null, new AAndBinop(new TAnd("&")), null); ABinopExp binop4 = new ABinopExp(iRef4Exp, new APlusBinop(new TPlus("+")), intConst3); ABinopExp binop5 = new ABinopExp(iRef5Exp, new AGeBinop(new TGteq(">=")), intConst4); ABinopExp binop6 = new ABinopExp(iRef7Exp, new AEqBinop(new TEq("==")), strIndexRef2Exp); ABinopExp binop7 = new ABinopExp(intConst6, new ADivideBinop(new TDiv("/")), intConst7); ABinopExp binop8 = new ABinopExp(iRef8Exp, new ADivideBinop(new TDiv("/")), intConst8); ABinopExp binop9 = new ABinopExp(iRef9Exp, new ADivideBinop(new TDiv("/")), intConst9); ABinopExp binop10 = new ABinopExp(iRef10Exp, new AModuloBinop(new TMod("%")), intConst10); ABinopExp binop11 = new ABinopExp(null, new APlusBinop(new TPlus("+")), null); ABinopExp binop12 = new ABinopExp(intConst11, new ALBitShiftBinop(new TLBitShift("<<")), binop2); ABinopExp binop13 = new ABinopExp(intConst12, new ALBitShiftBinop(new TLBitShift("<<")), binop10); binop3.SetRight(binop12); binop11.SetRight(binop13); //ASimpleInvokeExp power2Invoke1 = new ASimpleInvokeExp(new TIdentifier("Power2"), new ArrayList() { binop2 }); //ASimpleInvokeExp power2Invoke2 = new ASimpleInvokeExp(new TIdentifier("Power2"), new ArrayList() { binop10 }); //binop3.SetRight(power2Invoke1); //binop11.SetRight(power2Invoke2); AArrayLvalue arrayIndex1 = new AArrayLvalue(new TLBracket("["), strUsedRef1Exp, binop1); AArrayLvalue arrayIndex2 = new AArrayLvalue(new TLBracket("["), strUsedRef2Exp, binop8); AArrayLvalue arrayIndex3 = new AArrayLvalue(new TLBracket("["), strUsedRef3Exp, binop9); ALvalueExp arrayIndex1Exp = new ALvalueExp(arrayIndex1); ALvalueExp arrayIndex3Exp = new ALvalueExp(arrayIndex3); binop3.SetLeft(arrayIndex1Exp); binop11.SetLeft(arrayIndex3Exp); AAssignmentExp assignement1 = new AAssignmentExp(new TAssign("="), iRef3, binop4); AAssignmentExp assignement2 = new AAssignmentExp(new TAssign("="), iRef6, intConst5); AAssignmentExp assignement3 = new AAssignmentExp(new TAssign("="), arrayIndex2, binop11); AAssignmentExp assignement4 = new AAssignmentExp(new TAssign("="), strIndexRef3, iRef11Exp); ASimpleInvokeExp playerGroupAllInvoke = new ASimpleInvokeExp(new TIdentifier("PlayerGroupAll"), new ArrayList()); AFieldLvalue messageAreaDebugRef = new AFieldLvalue(new TIdentifier("c_messageAreaDebug")); ALvalueExp messageAreaDebugRefExp = new ALvalueExp(messageAreaDebugRef); AStringConstExp stringConst = new AStringConstExp( new TStringLiteral("\"Galaxy++ Error: Unable to allocate more than " + intLiteral.Text + " dynamic " + prefix + " types.\"")); ASimpleInvokeExp stringToTextInvoke = new ASimpleInvokeExp(new TIdentifier("StringToText"), new ArrayList() { stringConst }); ASimpleInvokeExp displayMessageInvoke = new ASimpleInvokeExp(new TIdentifier("UIDisplayMessage"), new ArrayList() { playerGroupAllInvoke, messageAreaDebugRefExp, stringToTextInvoke }); ASimpleInvokeExp intToStringInvoke = new ASimpleInvokeExp(new TIdentifier("IntToString"), new ArrayList() { binop7 }); AABlock methodBlock = new AABlock( new ArrayList() { new ALocalDeclStm(new TSemicolon(";"), iDecl), new AWhileStm(new TLParen("("), binop3, new ABlockStm(new TLBrace("{"), new AABlock( new ArrayList() { new AExpStm (new TSemicolon (";"), assignement1), new AIfThenStm (new TLParen ("("), binop5, new ABlockStm (new TLBrace ("{"), new AABlock (new ArrayList () { new AExpStm (new TSemicolon (";"), assignement2) }, new TRBrace ("}")))), new AIfThenStm (new TLParen ("("), binop6, new ABlockStm (new TLBrace ("{"), new AABlock (new ArrayList () { new AExpStm (new TSemicolon (";"), displayMessageInvoke), new AExpStm (new TSemicolon (";"), intToStringInvoke) }, new TRBrace ("}")))) }, new TRBrace( "}")))), new AExpStm(new TSemicolon(";"), assignement3), new AExpStm(new TSemicolon(";"), assignement4) }, new TRBrace("}")); if (vars.IdentifierArray != null) { /* <<if it is being compared with null at any point in time>> <<usedBits := floor(log2(42))+1>> <<bitsLeft := 31 - usedBits>> <<biggestIdentifier := 2^(bitsLeft + 1) - 1>> identifierArray[i] = identifierNext; i = (i << bitsLeft) + identifierNext; identifierNext = identifierNext%biggestIdentifier + 1; */ int usedLimit = int.Parse(intLiteral.Text); int usedBits = usedLimit == 0 ? 0 : ((int)Math.Floor(Math.Log(usedLimit, 2)) + 1); int bitsLeft = 31 - usedBits; int biggestIdentifier = (1 << (bitsLeft + 1)) - 1; AIntConstExp bitsLeftConst = new AIntConstExp(new TIntegerLiteral(bitsLeft.ToString())); AIntConstExp biggestIdentifierConst = new AIntConstExp(new TIntegerLiteral(biggestIdentifier.ToString())); AIntConstExp oneIntConst = new AIntConstExp(new TIntegerLiteral("1")); ALocalLvalue secondIRef1 = new ALocalLvalue(new TIdentifier("i")); ALocalLvalue secondIRef2 = new ALocalLvalue(new TIdentifier("i")); ALocalLvalue secondIRef3 = new ALocalLvalue(new TIdentifier("i")); ALvalueExp secondIRef2Exp = new ALvalueExp(secondIRef2); ALvalueExp secondIRef3Exp = new ALvalueExp(secondIRef3); AFieldLvalue identierNExtRef1 = new AFieldLvalue(new TIdentifier("identiferNext")); AFieldLvalue identierNExtRef2 = new AFieldLvalue(new TIdentifier("identiferNext")); AFieldLvalue identierNExtRef3 = new AFieldLvalue(new TIdentifier("identiferNext")); AFieldLvalue identierNExtRef4 = new AFieldLvalue(new TIdentifier("identiferNext")); ALvalueExp identierNExtRef1Exp = new ALvalueExp(identierNExtRef1); ALvalueExp identierNExtRef3Exp = new ALvalueExp(identierNExtRef3); ALvalueExp identierNExtRef4Exp = new ALvalueExp(identierNExtRef4); AFieldLvalue identifierArrayRef = new AFieldLvalue(new TIdentifier("identifierArray")); ALvalueExp identifierArrayRefExp = new ALvalueExp(identifierArrayRef); AArrayLvalue arrayLvalue = new AArrayLvalue(new TLBracket("["), identifierArrayRefExp, secondIRef3Exp); AAssignmentExp secondAssignment3 = new AAssignmentExp(new TAssign("="), arrayLvalue, identierNExtRef4Exp); methodBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), secondAssignment3)); ABinopExp secondBinop1 = new ABinopExp(secondIRef2Exp, new ALBitShiftBinop(new TLBitShift("<<")), bitsLeftConst); ABinopExp secondBinop2 = new ABinopExp(secondBinop1, new APlusBinop(new TPlus("+")), identierNExtRef1Exp); AAssignmentExp secondAssignment1 = new AAssignmentExp(new TAssign("="), secondIRef1, secondBinop2); methodBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), secondAssignment1)); ABinopExp secondBinop3 = new ABinopExp(identierNExtRef3Exp, new AModuloBinop(new TMod("%")), biggestIdentifierConst); ABinopExp secondBinop4 = new ABinopExp(secondBinop3, new APlusBinop(new TPlus("+")), oneIntConst); AAssignmentExp secondAssignment2 = new AAssignmentExp(new TAssign("="), identierNExtRef2, secondBinop4); methodBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), secondAssignment2)); data.LvalueTypes[secondIRef1] = data.LvalueTypes[secondIRef2] = data.ExpTypes[bitsLeftConst] = data.ExpTypes[biggestIdentifierConst] = data.ExpTypes[oneIntConst] = data.ExpTypes[secondIRef2Exp] = data.LvalueTypes[identierNExtRef1] = data.LvalueTypes[identierNExtRef2] = data.LvalueTypes[identierNExtRef3] = data.ExpTypes[identierNExtRef1Exp] = data.ExpTypes[identierNExtRef3Exp] = data.ExpTypes[secondBinop1] = data.ExpTypes[secondBinop2] = data.ExpTypes[secondAssignment1] = data.ExpTypes[secondBinop3] = data.ExpTypes[secondBinop4] = data.ExpTypes[secondAssignment2] = data.LvalueTypes[secondIRef3] = data.LvalueTypes[identierNExtRef4] = data.ExpTypes[secondIRef3Exp] = data.ExpTypes[identierNExtRef4Exp] = data.LvalueTypes[arrayLvalue] = data.ExpTypes[secondAssignment3] = new ANamedType(new TIdentifier("int"), null); data.LvalueTypes[identifierArrayRef] = data.ExpTypes[identifierArrayRefExp] = vars.IdentifierArray.GetType(); data.LocalLinks[secondIRef1] = data.LocalLinks[secondIRef2] = data.LocalLinks[secondIRef3] = iDecl; data.FieldLinks[identierNExtRef1] = data.FieldLinks[identierNExtRef2] = data.FieldLinks[identierNExtRef3] = data.FieldLinks[identierNExtRef4] = vars.IdentifierNext; data.FieldLinks[identifierArrayRef] = vars.IdentifierArray; } methodBlock.GetStatements().Add(new AValueReturnStm(new TReturn("return"), iRef12Exp)); AMethodDecl method = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null, new ANamedType(new TIdentifier("int"), null), new TIdentifier("Create" + prefix, data.LineCounts[file] + 18, 0), new ArrayList(), methodBlock); file.GetDecl().Add(method); data.Methods.Add(new SharedData.DeclItem<AMethodDecl>(file, method)); data.LocalLinks[iRef1] = data.LocalLinks[iRef2] = data.LocalLinks[iRef3] = data.LocalLinks[iRef4] = data.LocalLinks[iRef5] = data.LocalLinks[iRef6] = data.LocalLinks[iRef7] = data.LocalLinks[iRef8] = data.LocalLinks[iRef9] = data.LocalLinks[iRef10] = data.LocalLinks[iRef11] = data.LocalLinks[iRef12] = iDecl; data.FieldLinks[strUsedRef1] = data.FieldLinks[strUsedRef2] = data.FieldLinks[strUsedRef3] = vars.Used; data.FieldLinks[strIndexRef1] = data.FieldLinks[strIndexRef2] = data.FieldLinks[strIndexRef3] = vars.Index; //data.SimpleMethodLinks[power2Invoke1] = // data.SimpleMethodLinks[power2Invoke2] = CreatePower2Method(node, data); data.FieldLinks[messageAreaDebugRef] = data.Libraries.Fields.First(f => f.GetName().Text == messageAreaDebugRef.GetName().Text); data.SimpleMethodLinks[displayMessageInvoke] = data.Libraries.Methods.First(m => m.GetName().Text == displayMessageInvoke.GetName().Text); data.SimpleMethodLinks[playerGroupAllInvoke] = data.Libraries.Methods.First(m => m.GetName().Text == playerGroupAllInvoke.GetName().Text); data.SimpleMethodLinks[stringToTextInvoke] = data.Libraries.Methods.First(m => m.GetName().Text == stringToTextInvoke.GetName().Text); data.SimpleMethodLinks[intToStringInvoke] = data.Libraries.Methods.First(m => m.GetName().Text == intToStringInvoke.GetName().Text); data.ExpTypes[intConst1] = data.ExpTypes[intConst2] = data.ExpTypes[intConst3] = data.ExpTypes[intConst4] = data.ExpTypes[intConst5] = data.ExpTypes[intConst6] = data.ExpTypes[intConst7] = data.ExpTypes[intConst8] = data.ExpTypes[intConst9] = data.ExpTypes[intConst10] = data.ExpTypes[intConst11] = data.ExpTypes[intConst12] = data.LvalueTypes[strIndexRef1] = data.LvalueTypes[strIndexRef2] = data.LvalueTypes[strIndexRef3] = data.ExpTypes[strIndexRef1Exp] = data.ExpTypes[strIndexRef2Exp] = data.LvalueTypes[iRef1] = data.LvalueTypes[iRef2] = data.LvalueTypes[iRef3] = data.LvalueTypes[iRef4] = data.LvalueTypes[iRef5] = data.LvalueTypes[iRef6] = data.LvalueTypes[iRef7] = data.LvalueTypes[iRef8] = data.LvalueTypes[iRef9] = data.LvalueTypes[iRef10] = data.LvalueTypes[iRef11] = data.LvalueTypes[iRef12] = data.ExpTypes[iRef1Exp] = data.ExpTypes[iRef2Exp] = data.ExpTypes[iRef4Exp] = data.ExpTypes[iRef5Exp] = data.ExpTypes[iRef7Exp] = data.ExpTypes[iRef8Exp] = data.ExpTypes[iRef9Exp] = data.ExpTypes[iRef10Exp] = data.ExpTypes[iRef11Exp] = data.ExpTypes[iRef12Exp] = data.LvalueTypes[arrayIndex1] = data.LvalueTypes[arrayIndex2] = data.LvalueTypes[arrayIndex3] = data.ExpTypes[arrayIndex1Exp] = data.ExpTypes[arrayIndex3Exp] = data.ExpTypes[binop1] = data.ExpTypes[binop2] = data.ExpTypes[binop3] = data.ExpTypes[binop4] = data.ExpTypes[binop7] = data.ExpTypes[binop8] = data.ExpTypes[binop9] = data.ExpTypes[binop10] = data.ExpTypes[binop11] = data.ExpTypes[binop12] = data.ExpTypes[binop13] = //data.ExpTypes[power2Invoke1] = //data.ExpTypes[power2Invoke2] = data.ExpTypes[intToStringInvoke] = data.ExpTypes[assignement1] = data.ExpTypes[assignement2] = data.ExpTypes[assignement3] = data.ExpTypes[assignement4] = data.LvalueTypes[messageAreaDebugRef] = data.ExpTypes[messageAreaDebugRefExp] = new ANamedType(new TIdentifier("int"), null); data.LvalueTypes[strUsedRef1] = data.LvalueTypes[strUsedRef2] = data.LvalueTypes[strUsedRef3] = data.ExpTypes[strUsedRef1Exp] = data.ExpTypes[strUsedRef2Exp] = data.ExpTypes[strUsedRef3Exp] = vars.Used.GetType(); data.ExpTypes[binop5] = data.ExpTypes[binop6] = new ANamedType(new TIdentifier("int"), null); data.ExpTypes[stringConst] = data.ExpTypes[intToStringInvoke] = new ANamedType(new TIdentifier("string"), null); data.ExpTypes[stringToTextInvoke] = new ANamedType(new TIdentifier("text"), null); data.ExpTypes[playerGroupAllInvoke] = new ANamedType(new TIdentifier("playergroup"), null); data.ExpTypes[displayMessageInvoke] = new AVoidType(new TVoid("void")); return method; }
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 }