public override void InANamedType(ANamedType node) { if (!node.IsPrimitive())// !GalaxyKeywords.Primitives.words.Any(word => word == node.GetName().Text)) { AStructDecl decl = finalTrans.data.StructTypeLinks[node]; AddDepency(Util.GetAncestor <AASourceFile>(node), Util.GetAncestor <AASourceFile>(decl)); } }
public override void OutANamedType(ANamedType node) { if (!node.IsPrimitive())//!GalaxyKeywords.Primitives.words.Contains(node.GetName().Text)) { node.SetName( new AAName(new List <TIdentifier>() { new TIdentifier(finalTrans.data.StructTypeLinks[node].GetName().Text) })); } }
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); }
public override void DefaultIn(Node node) { if (!canMerge) { return; } if (node is AMethodDecl) { //First node - no need to fetch if (((AMethodDecl)node).GetFormals().Count != ((AMethodDecl)otherNode).GetFormals().Count) { canMerge = false; } return; } //Fetch corrosponding other node int index = 0; GetChildTypeIndex getChildTypeIndex = new GetChildTypeIndex() { Parent = node.Parent(), Child = node }; node.Parent().Apply(getChildTypeIndex); index = getChildTypeIndex.Index; GetChildTypeByIndex getChildTypeByIndex = new GetChildTypeByIndex() { Child = node, Index = index, Parent = otherNode }; otherNode.Apply(getChildTypeByIndex); otherNode = getChildTypeByIndex.Child; if (otherNode.GetType() != node.GetType()) { canMerge = false; return; } if (node is AALocalDecl) { locals.Add((AALocalDecl)node); otherLocals.Add((AALocalDecl)otherNode); return; } if (node is ANamedType) { ANamedType aNode = (ANamedType)node; ANamedType aOther = (ANamedType)otherNode; if (data.StructTypeLinks.ContainsKey(aNode) != data.StructTypeLinks.ContainsKey(aOther)) { canMerge = false; return; } if (data.StructTypeLinks.ContainsKey(aNode) && data.StructTypeLinks[aNode] != data.StructTypeLinks[aOther]) { canMerge = false; } if (!data.StructTypeLinks.ContainsKey(aNode) && aNode.IsSame(aOther, true))//aNode.GetName().Text != aOther.GetName().Text) { canMerge = false; } if (aNode.IsPrimitive() && !aOther.IsPrimitive(aNode.AsIdentifierString())) { canMerge = false; } return; } if (node is AABlock) { AABlock aNode = (AABlock)node; AABlock aOther = (AABlock)otherNode; if (aNode.GetStatements().Count != aOther.GetStatements().Count) { canMerge = false; } return; } if (node is AIntConstExp) { AIntConstExp aNode = (AIntConstExp)node; AIntConstExp aOther = (AIntConstExp)otherNode; if (aNode.GetIntegerLiteral().Text != aOther.GetIntegerLiteral().Text) { canMerge = false; } return; } if (node is AFixedConstExp) { AFixedConstExp aNode = (AFixedConstExp)node; AFixedConstExp aOther = (AFixedConstExp)otherNode; if (aNode.GetFixedLiteral().Text != aOther.GetFixedLiteral().Text) { canMerge = false; } return; } if (node is AStringConstExp) { AStringConstExp aNode = (AStringConstExp)node; AStringConstExp aOther = (AStringConstExp)otherNode; if (aNode.GetStringLiteral().Text != aOther.GetStringLiteral().Text) { canMerge = false; } return; } if (node is ACharConstExp) { ACharConstExp aNode = (ACharConstExp)node; ACharConstExp aOther = (ACharConstExp)otherNode; if (aNode.GetCharLiteral().Text != aOther.GetCharLiteral().Text) { canMerge = false; } return; } if (node is ASimpleInvokeExp) { ASimpleInvokeExp aNode = (ASimpleInvokeExp)node; ASimpleInvokeExp aOther = (ASimpleInvokeExp)otherNode; if (data.SimpleMethodLinks[aNode] != data.SimpleMethodLinks[aOther] && !(data.SimpleMethodLinks[aNode] == Util.GetAncestor <AMethodDecl>(aNode) && data.SimpleMethodLinks[aOther] == Util.GetAncestor <AMethodDecl>(aOther))) { canMerge = false; } return; } if (node is ALocalLvalue) { ALocalLvalue aNode = (ALocalLvalue)node; ALocalLvalue aOther = (ALocalLvalue)otherNode; if (locals.IndexOf(data.LocalLinks[aNode]) != otherLocals.IndexOf(data.LocalLinks[aOther])) { canMerge = false; } return; } if (node is AFieldLvalue) { AFieldLvalue aNode = (AFieldLvalue)node; AFieldLvalue aOther = (AFieldLvalue)otherNode; if (data.FieldLinks[aNode] != data.FieldLinks[aOther]) { canMerge = false; } return; } if (node is AStructLvalue) { AStructLvalue aNode = (AStructLvalue)node; AStructLvalue aOther = (AStructLvalue)otherNode; if (data.StructFieldLinks[aNode] != data.StructFieldLinks[aOther]) { canMerge = false; } return; } }
public override void CaseANamedType(ANamedType node) { //Remember.. if you are the child of a fieldDecl, that field may have been moved if (!node.IsPrimitive())//GalaxyKeywords.Primitives.words.Any(word => word == node.GetName().Text))) { AStructDecl decl = finalTrans.data.StructTypeLinks[node]; Item currentItem = GetIncludeItem(node); if (Util.GetAncestor <AFieldDecl>(node) != null) { Item i = allItems.OfType <FieldItem>().FirstOrDefault(item => item.FieldDecl == Util.GetAncestor <AFieldDecl>(node)); if (i != null) { currentItem = i; } } if (Util.GetAncestor <AStructDecl>(node) != null) { Item i = allItems.OfType <StructItem>().FirstOrDefault( item => item.StructDecl == Util.GetAncestor <AStructDecl>(node)); if (i != null) { currentItem = i; } } Item declItem = ((Item)allItems.OfType <StructItem>().FirstOrDefault(item => item.StructDecl == decl)) ?? allItems.OfType <IncludeItem>().First( item => item.Current == Util.GetAncestor <AASourceFile>(decl)); List <Item> cPath = currentItem.Path; List <Item> dPath = declItem.Path; for (int i = 0; i < Math.Min(cPath.Count, dPath.Count); i++) { if (cPath[i] != dPath[i]) {//FORK!!!! //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 StructItem) { declItem.Parent.Children.Remove(declItem); declItem.Parent = cPath[i - 1]; } else { declItem = new StructItem(decl, cPath[i - 1], new List <Item>()); allItems.Add(declItem); } cPath[i - 1].Children.Insert(cI, declItem); 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.GetToken())) { break; } //Add the decl item declItem = new StructItem(decl, cPath[i], new List <Item>()); allItems.Add(declItem); cPath[i].Children.Add(declItem); 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 StructItem(decl, cPath[i], new List <Item>()); allItems.Add(declItem); cPath[i].Children.Insert(cI, declItem); break; } } } base.CaseANamedType(node); }
public static bool TypesEqual(PType t1, PType t2, SharedData data) { if (t1.GetType() != t2.GetType()) { return(false); } if (t1 is AVoidType) { return(true); } if (t1 is AArrayTempType) { AArrayTempType aT1 = (AArrayTempType)t1; AArrayTempType aT2 = (AArrayTempType)t2; return(TypesEqual(aT1.GetType(), aT2.GetType(), data) && ReturnsTheSame(aT1.GetDimention(), aT2.GetDimention(), data)); } if (t1 is ADynamicArrayType) { ADynamicArrayType aT1 = (ADynamicArrayType)t1; ADynamicArrayType aT2 = (ADynamicArrayType)t2; return(TypesEqual(aT1.GetType(), aT2.GetType(), data)); } if (t1 is ANamedType) { ANamedType aT1 = (ANamedType)t1; ANamedType aT2 = (ANamedType)t2; if (aT1.IsPrimitive() && aT2.IsPrimitive()) { return(((AAName)aT1.GetName()).AsString() == ((AAName)aT2.GetName()).AsString()); } if (data.StructTypeLinks.ContainsKey(aT1) != data.StructTypeLinks.ContainsKey(aT2)) { return(false); } if (data.StructTypeLinks.ContainsKey(aT1) && data.StructTypeLinks[aT1] != data.StructTypeLinks[aT2]) { return(false); } if (data.DelegateTypeLinks.ContainsKey(aT1) != data.DelegateTypeLinks.ContainsKey(aT2)) { return(false); } if (data.DelegateTypeLinks.ContainsKey(aT1) && data.DelegateTypeLinks[aT1] != data.DelegateTypeLinks[aT2]) { return(false); } return(true); } if (t1 is ANullType) { return(true); } if (t1 is APointerType) { APointerType aT1 = (APointerType)t1; APointerType aT2 = (APointerType)t2; return(TypesEqual(aT1.GetType(), aT2.GetType(), data)); } if (t1 is AGenericType) { AGenericType aT1 = (AGenericType)t1; AGenericType aT2 = (AGenericType)t2; if (!TypesEqual(aT1.GetBase(), aT2.GetBase(), data)) { return(false); } if (aT1.GetGenericTypes().Count != aT2.GetGenericTypes().Count) { return(false); } for (int i = 0; i < aT1.GetGenericTypes().Count; i++) { if (!TypesEqual((PType)aT1.GetGenericTypes()[i], (PType)aT2.GetGenericTypes()[i], data)) { return(false); } } return(true); } throw new Exception("Util.TypesEqual: Unexpected type. Got + " + t1.GetType()); }