public static void Parse(AStructDecl str, AStructDecl clone, List <PType> types, SharedData data) { FixGenericLinks fixer = new FixGenericLinks(clone, types, data) { original = str, clone = clone }; str.Apply(fixer); }
public override void OutAEnumDecl(AEnumDecl node) { AStructDecl replacer = new AStructDecl(node.GetVisibilityModifier(), null, null, null, node.GetEndToken(), node.GetName(), new ArrayList(), null, new ArrayList()); int intVal = 0; //int min = int.MaxValue; //int max = int.MinValue; //List<TIdentifier> types = new List<TIdentifier>(); foreach (AAEnumLocal value in node.GetValues()) { AIntConstExp intConst; if (value.GetValue() != null) { intConst = (AIntConstExp)value.GetValue(); intVal = int.Parse(intConst.GetIntegerLiteral().Text) + 1; } else { intConst = new AIntConstExp(new TIntegerLiteral(intVal.ToString(), value.GetName().Line, value.GetName().Pos)); intVal++; } // min = Math.Min(intVal - 1, min); // max = Math.Max(intVal - 1, max); TIdentifier typeIdentifier = new TIdentifier(replacer.GetName().Text, value.GetName().Line, value.GetName().Pos); // types.Add(typeIdentifier); AALocalDecl localDecl = new AALocalDecl(new APublicVisibilityModifier(), new TStatic("static", value.GetName().Line, value.GetName().Pos), null, null, new TConst("const", value.GetName().Line, value.GetName().Pos), new ANamedType(typeIdentifier, null), value.GetName(), intConst); replacer.GetLocals().Add(localDecl); } /* if (min < 0 || max > 255) * foreach (TIdentifier identifier in types) * { * identifier.Text = "int"; * }*/ node.ReplaceBy(replacer); replacer.Apply(this); replacer.GetName().Text = "enum " + replacer.GetName().Text; }
public override void OutAAProgram(AAProgram node) { foreach (var pair in Refferences) { if (structsWithGenerics.Contains(pair.Key) && pair.Value.Count > 0) { needAnotherPass = true; } } foreach (var pair in Refferences) { AStructDecl str = pair.Key; if (!copies.ContainsKey(str)) { copies[str] = new List <List <PType> >(); } IList declList; Node parent = str.Parent(); if (parent is AASourceFile) { declList = ((AASourceFile)parent).GetDecl(); } else { declList = ((ANamespaceDecl)parent).GetDecl(); } //AASourceFile pFile = (AASourceFile) str.Parent(); foreach (AGenericType refference in pair.Value) { AStructDecl clone = null; bool addList = true; foreach (List <PType> list in copies[str]) { bool listEqual = true; for (int i = 0; i < list.Count; i++) { if (!Util.TypesEqual(list[i], (PType)refference.GetGenericTypes()[i], data)) { listEqual = false; break; } } if (listEqual) { addList = false; clone = clones[list]; break; } } if (addList) { List <PType> list = new List <PType>(); foreach (PType type in refference.GetGenericTypes()) { list.Add(type); } copies[str].Add(list); clone = (AStructDecl)str.Clone(); declList.Insert(declList.IndexOf(str), clone); clone.Apply(new EnviromentBuilding(errors, data)); clone.Apply(new EnviromentChecking(errors, data, false)); clone.Apply(new LinkNamedTypes(errors, data)); /* * data.Structs.Add(new SharedData.DeclItem<AStructDecl>(pFile, clone)); * data.StructFields[clone] = new List<AALocalDecl>(); * data.StructMethods[clone] = new List<AMethodDecl>(); * data.StructProperties[clone] = new List<APropertyDecl>(); * data.StructConstructors[clone] = new List<AConstructorDecl>(); * foreach (PLocalDecl localDecl in clone.GetLocals()) * { * if (localDecl is AALocalDecl) * { * data.StructFields[clone].Add((AALocalDecl)localDecl); * } * else * { * PDecl decl = ((ADeclLocalDecl) localDecl).GetDecl(); * if (decl is AMethodDecl) * { * data.StructMethods[clone].Add((AMethodDecl) decl); * } * else if (decl is APropertyDecl) * { * data.StructProperties[clone].Add((APropertyDecl)decl); * } * else * { * data.StructConstructors[clone].Add((AConstructorDecl) decl); * } * } * }*/ clones[list] = clone; clone.setGenericVars(new ArrayList()); FixGenericLinks.Parse(str, clone, list, data); clone.GetName().Text = Util.TypeToIdentifierString(refference); } //Change refference to clone ANamedType baseRef = (ANamedType)refference.GetBase(); List <string> cloneNs = Util.GetFullNamespace(clone); cloneNs.Add(clone.GetName().Text); AAName aName = (AAName)baseRef.GetName(); aName.GetIdentifier().Clear(); foreach (var n in cloneNs) { aName.GetIdentifier().Add(new TIdentifier(n)); } data.StructTypeLinks[baseRef] = clone; refference.ReplaceBy(baseRef); } if (!needAnotherPass) { parent.RemoveChild(str); } } if (needAnotherPass) { Refferences.Clear(); structsWithGenerics.Clear(); needAnotherPass = false; CaseAAProgram(node); } }
public Parser(AStructDecl structDecl) { structDecl.Apply(this); }
/*private class IsThisOnLeftSide : DepthFirstAdapter * { * private PType type; * private SharedData data; * public bool IsAssignedTo; * private List<AMethodDecl> investigatedMethods = new List<AMethodDecl>(); * * public IsThisOnLeftSide(PType type, SharedData data) * { * this.type = type; * this.data = data; * } * * //Check assignments, method invocations and nonstatic method invocations. * * public override void CaseAMethodDecl(AMethodDecl node) * { * investigatedMethods.Add(node); * } * * public override void CaseAThisLvalue(AThisLvalue node) * { * if (IsAssignedTo) * return; * * Node iParent = GetClosestNodeOfType(node, typeof (AAssignmentExp), * typeof (ASimpleInvokeExp), * typeof (ANonstaticInvokeExp), * typeof (AAsyncInvokeStm), * typeof (ASyncInvokeExp), * typeof(AArrayLvalue), * typeof(APointerLvalue), * typeof(APropertyLvalue), * typeof(AStructLvalue)); * if (iParent == null) * return; * * if (iParent is AAssignmentExp) * { * AAssignmentExp aParent = (AAssignmentExp) iParent; * if (Util.IsAncestor(node, aParent.GetLvalue())) * { * IsAssignedTo = true; * } * return; * } * if (iParent is ASimpleInvokeExp) * { * ASimpleInvokeExp aParent = (ASimpleInvokeExp) iParent; * AMethodDecl method = data.SimpleMethodLinks[aParent]; * if (investigatedMethods.Contains(method)) * return; * * if (Util.IsAncestor(node, aParent.GetLvalue())) * { * IsAssignedTo = true; * } * return; * } * } * * private Node GetClosestNodeOfType(Node node, params Type[] types) * { * if (node == null) * return null; * if (types.Contains(node.GetType())) * return node; * return GetClosestNodeOfType(node.Parent(), types); * } * }*/ public override void CaseADeconstructorDecl(ADeconstructorDecl node) { AStructDecl str = Util.GetAncestor <AStructDecl>(node); AEnrichmentDecl enrichment = Util.GetAncestor <AEnrichmentDecl>(node); AMethodDecl replacer = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null, new AVoidType(new TVoid("void")), node.GetName(), new ArrayList(), node.GetBlock()); replacer.GetName().Text += "_Deconstructor"; //Move the method outside the struct AASourceFile file = Util.GetAncestor <AASourceFile>(node); if (str != null) { str.RemoveChild(node.Parent()); } /*else * enrichment.RemoveChild(node);*/ int i = file.GetDecl().IndexOf(str ?? (PDecl)enrichment); file.GetDecl().Insert(i /* + 1*/, replacer); //Add the struct as a parameter PType type; if (str != null) { ANamedType structType = new ANamedType(new TIdentifier(str.GetName().Text), null); finalTrans.data.StructTypeLinks[structType] = str; type = structType; } else { type = Util.MakeClone(enrichment.GetType(), finalTrans.data); } finalTrans.data.DeconstructorMap[node] = replacer; AALocalDecl structFormal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new APointerType(new TStar("*"), type), new TIdentifier("currentStruct", replacer.GetName().Line, replacer.GetName().Pos), null); replacer.GetFormals().Add(structFormal); finalTrans.data.Methods.Add(new SharedData.DeclItem <AMethodDecl>(file, replacer)); //Call base deconstructor before each return if (str != null && str.GetBase() != null) { AStructDecl baseStruct = data.StructTypeLinks[(ANamedType)str.GetBase()]; if (data.StructDeconstructor.ContainsKey(baseStruct)) { baseStruct.Apply(this); replacer.Apply(new CallDeconstructors(baseStruct, structFormal, data)); /*AMethodDecl baseDeconstructor = data.DeconstructorMap[data.StructDeconstructor[baseStruct]]; * * * ALocalLvalue structFormalRef = new ALocalLvalue(new TIdentifier("currentStruct")); * ALvalueExp structFormalRefExp = new ALvalueExp(structFormalRef); * ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("baseDeconstructor"), * new ArrayList() {structFormalRefExp}); * AABlock block = (AABlock) replacer.GetBlock(); * block.GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), invoke)); * * data.LocalLinks[structFormalRef] = structFormal; * data.SimpleMethodLinks[invoke] = baseDeconstructor; * data.LvalueTypes[structFormalRef] = data.ExpTypes[structFormalRefExp] = structFormal.GetType(); * data.ExpTypes[invoke] = baseDeconstructor.GetReturnType();*/ } } this.structFormal = structFormal; base.CaseAMethodDecl(replacer); }