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); }
public override void OutADynamicArrayType(ADynamicArrayType node) { if (pointeredArrays.Contains(node)) { return; } pointeredArrays.Add(node); APointerType replacer = new APointerType(new TStar("*"), null); node.ReplaceBy(replacer); replacer.SetType(node); }
public static string TypeToIdentifierString(PType type) { if (type is AVoidType) { return("void"); } /*if (type is AArrayType) * { * AArrayType aType = (AArrayType)type; * return TypeToString(aType.GetType()) + "[" + aType.GetDimention().Text + "]"; * }*/ if (type is AArrayTempType) { AArrayTempType aType = (AArrayTempType)type; return(TypeToIdentifierString(aType.GetType()) + "Ar" + (aType.GetIntDim() == null ? "" : aType.GetIntDim().Text)); } if (type is ANamedType) { ANamedType aType = (ANamedType)type; return(((AAName)aType.GetName()).AsString().Replace('.', '_')); } if (type is APointerType) { APointerType aType = (APointerType)type; return("p" + TypeToIdentifierString(aType.GetType())); } if (type is ADynamicArrayType) { ADynamicArrayType aType = (ADynamicArrayType)type; return(TypeToIdentifierString(aType.GetType()) + "DAr"); } if (type is AGenericType) { AGenericType aType = (AGenericType)type; string ret = TypeToIdentifierString(aType.GetBase()) + "G_"; bool first = true; foreach (PType t in aType.GetGenericTypes()) { if (first) { first = false; } else { ret += "_"; } ret += TypeToIdentifierString(t); } return(ret + "_G"); } throw new Exception("Unknown type"); }
//private List<ErrorCollection.Error> multipleEntryCandidates = new List<ErrorCollection.Error>(); public override void CaseAMethodDecl(AMethodDecl node) { //Done in a previous iteration /*if (node.GetName().Text == "InitMap" && node.GetFormals().Count == 0) * { * if (finalTrans.multipleMainEntries) * { * multipleEntryCandidates.Add(new ErrorCollection.Error(node.GetName(), Util.GetAncestor<AASourceFile>(node.GetName()), "Candidate")); * } * else if (finalTrans.mainEntry != null) * { * multipleEntryCandidates.Add(new ErrorCollection.Error(finalTrans.mainEntry.GetName(), Util.GetAncestor<AASourceFile>(finalTrans.mainEntry.GetName()), "Candidate")); * multipleEntryCandidates.Add(new ErrorCollection.Error(node.GetName(), Util.GetAncestor<AASourceFile>(node.GetName()), "Candidate")); * //finalTrans.errors.Add(new ErrorCollection.Error(node.GetName(), Util.GetAncestor<AASourceFile>(node), "Found multiple candidates for a main entry", true)); * finalTrans.multipleMainEntries = true; * finalTrans.mainEntry = null; * } * else * finalTrans.mainEntry = node; * }*/ AStructDecl str = Util.GetAncestor <AStructDecl>(node); if (str != null) { if (node.GetStatic() == null) { structMethods.Add(node); } //Move the method outside the struct str.RemoveChild(node.Parent()); AASourceFile file = (AASourceFile)str.Parent(); int i = file.GetDecl().IndexOf(str); file.GetDecl().Insert(i /* + 1*/, node); node.GetName().Text = GetUniqueStructMethodName(str.GetName().Text + "_" + node.GetName().Text); if (node.GetStatic() == null) { //Add the struct as a parameter PType structType = new ANamedType(new TIdentifier(str.GetName().Text), null); finalTrans.data.StructTypeLinks[(ANamedType)structType] = str; if (str.GetClassToken() != null) { structType = new APointerType(new TStar("*"), structType); } structFormal = new AALocalDecl(new APublicVisibilityModifier(), null, str.GetClassToken() == null ? new TRef("ref") : null, null, null, structType, new TIdentifier("currentStruct", node.GetName().Line, node.GetName().Pos), null); node.GetFormals().Add(structFormal); data.Locals[(AABlock)node.GetBlock()].Add(structFormal); } else { node.SetStatic(null); } finalTrans.data.Methods.Add(new SharedData.DeclItem <AMethodDecl>(file, node)); if (node.GetStatic() == null) { OldParentStruct[node] = str; } //Fix refferences to other struct stuff); base.CaseAMethodDecl(node); //Will visit later, since it's added after the struct //base.CaseAMethodDecl(node); //if (str.GetLocals().Count == 0) // str.Parent().RemoveChild(str); return; } AEnrichmentDecl enrichment = Util.GetAncestor <AEnrichmentDecl>(node); if (enrichment != null) { if (node.GetStatic() == null) { structMethods.Add(node); } //Move the method outside the struct enrichment.RemoveChild(node); AASourceFile file = (AASourceFile)enrichment.Parent(); int i = file.GetDecl().IndexOf(enrichment); file.GetDecl().Insert(i /* + 1*/, node); node.GetName().Text = GetUniqueStructMethodName(Util.TypeToIdentifierString(enrichment.GetType()) + "_" + node.GetName().Text); if (node.GetStatic() == null) { //Add the struct as a parameter PType structType = Util.MakeClone(enrichment.GetType(), finalTrans.data); structFormal = new AALocalDecl(new APublicVisibilityModifier(), null, new TRef("ref"), null, null, structType, new TIdentifier("currentEnrichment", node.GetName().Line, node.GetName().Pos), null); node.GetFormals().Add(structFormal); } finalTrans.data.Methods.Add(new SharedData.DeclItem <AMethodDecl>(file, node)); //Fix refferences to other struct stuff); base.CaseAMethodDecl(node); //Will visit later, since it's added after the struct //base.CaseAMethodDecl(node); //if (str.GetLocals().Count == 0) // str.Parent().RemoveChild(str); return; } //Build a list of overloads List <AMethodDecl> overloads = new List <AMethodDecl>(); List <string> prefixMatches = new List <string>(); foreach (SharedData.DeclItem <AMethodDecl> declItem in finalTrans.data.Methods) { if (!Util.IsSameNamespace(declItem.Decl, node)) { continue; } if (declItem.Decl.GetName().Text == node.GetName().Text) { overloads.Add(declItem.Decl); } if (declItem.Decl.GetName().Text.StartsWith(node.GetName().Text + "O")) { prefixMatches.Add(declItem.Decl.GetName().Text); } } foreach (AMethodDecl method in finalTrans.data.Libraries.Methods) { if (method.GetBlock() != null || method.GetNative() != null) { if (method.GetName().Text == node.GetName().Text) { overloads.Add(method); } if (method.GetName().Text.StartsWith(node.GetName().Text + "O")) { prefixMatches.Add(method.GetName().Text); } } } //Add fields foreach (SharedData.DeclItem <AFieldDecl> declItem in finalTrans.data.Fields) { if (declItem.Decl.GetName().Text.StartsWith(node.GetName().Text + "O")) { prefixMatches.Add(declItem.Decl.GetName().Text); } } foreach (AFieldDecl field in finalTrans.data.Libraries.Fields) { if (field.GetName().Text.StartsWith(node.GetName().Text + "O")) { prefixMatches.Add(field.GetName().Text); } } //Dont want to hit another method by appending O# string postfix = ""; while (true) { postfix += "O"; if (prefixMatches.Any(text => text.StartsWith(node.GetName().Text + postfix))) { continue; } break; } if (overloads.Count > 1) { int i = 0; foreach (AMethodDecl method in overloads) { if (node == finalTrans.mainEntry || (node.GetTrigger() != null && finalTrans.data.HasUnknownTrigger)) { continue; } i++; method.GetName().Text += postfix + i; } } if (node != finalTrans.mainEntry && (node.GetTrigger() == null || !finalTrans.data.HasUnknownTrigger)) { node.GetName().Text = namespacePrefix + node.GetName().Text; } base.CaseAMethodDecl(node); }
public override void CaseAConstructorDecl(AConstructorDecl 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 += "_Constructor"; while (node.GetFormals().Count > 0) { replacer.GetFormals().Add(node.GetFormals()[0]); } //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.ConstructorMap[node] = replacer; 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)); //Add return stm replacer.SetReturnType(new APointerType(new TStar("*"), Util.MakeClone(type, data))); replacer.Apply(new TransformConstructorReturns(structFormal, data)); //Insert call to base constructor);); if (finalTrans.data.ConstructorBaseLinks.ContainsKey(node)) { AMethodDecl baseConstructor = finalTrans.data.ConstructorMap[finalTrans.data.ConstructorBaseLinks[node]]; ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier(baseConstructor.GetName().Text), new ArrayList()); while (node.GetBaseArgs().Count > 0) { invoke.GetArgs().Add(node.GetBaseArgs()[0]); } AThisLvalue thisLvalue1 = new AThisLvalue(new TThis("this")); ALvalueExp thisExp1 = new ALvalueExp(thisLvalue1); invoke.GetArgs().Add(thisExp1); AThisLvalue thisLvalue2 = new AThisLvalue(new TThis("this")); AAssignmentExp assignExp = new AAssignmentExp(new TAssign("="), thisLvalue2, invoke); ANamedType structType = new ANamedType(new TIdentifier(str.GetName().Text), null); finalTrans.data.StructTypeLinks[structType] = str; finalTrans.data.LvalueTypes[thisLvalue1] = finalTrans.data.LvalueTypes[thisLvalue2] = finalTrans.data.ExpTypes[thisExp1] = finalTrans.data.ExpTypes[assignExp] = finalTrans.data.ExpTypes[invoke] = new APointerType(new TStar("*"), structType); //finalTrans.data.ExpTypes[invoke] = new AVoidType(new TVoid("void")); finalTrans.data.SimpleMethodLinks[invoke] = baseConstructor; ((AABlock)replacer.GetBlock()).GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), assignExp)); //Inline if base and current are two different kinds of pointer types (int/string) AStructDecl baseStruct = null; AConstructorDecl baseC = finalTrans.data.ConstructorBaseLinks[node]; foreach (KeyValuePair <AStructDecl, List <AConstructorDecl> > pair in finalTrans.data.StructConstructors) { bool found = false; foreach (AConstructorDecl decl in pair.Value) { if (baseC == decl) { found = true; break; } } if (found) { baseStruct = pair.Key; break; } } if ((str.GetIntDim() == null) != (baseStruct.GetIntDim() == null)) { //For the inilining, change the type to the type of the caller AALocalDecl lastFormal = baseConstructor.GetFormals().OfType <AALocalDecl>().Last(); lastFormal.SetRef(new TRef("ref")); APointerType oldType = (APointerType)lastFormal.GetType(); structType = new ANamedType(new TIdentifier(str.GetName().Text), null); finalTrans.data.StructTypeLinks[structType] = str; APointerType newType = new APointerType(new TStar("*"), structType); lastFormal.SetType(newType); foreach ( ALocalLvalue lvalue in data.LocalLinks.Where(pair => pair.Value == lastFormal).Select(pair => pair.Key)) { data.LvalueTypes[lvalue] = newType; if (lvalue.Parent() is ALvalueExp) { data.ExpTypes[(PExp)lvalue.Parent()] = newType; if (lvalue.Parent().Parent() is APointerLvalue) { data.LvalueTypes[(PLvalue)lvalue.Parent().Parent()] = newType.GetType(); } } } FixInlineMethods.Inline(invoke, finalTrans); lastFormal.SetRef(null); foreach ( ALocalLvalue lvalue in data.LocalLinks.Where(pair => pair.Value == lastFormal).Select(pair => pair.Key)) { data.LvalueTypes[lvalue] = oldType; if (lvalue.Parent() is ALvalueExp) { data.ExpTypes[(PExp)lvalue.Parent()] = oldType; if (lvalue.Parent().Parent() is APointerLvalue) { data.LvalueTypes[(PLvalue)lvalue.Parent().Parent()] = oldType.GetType(); } } } lastFormal.SetType(oldType); } //Inline it instead, Since the pointer implementations might not be the same (int vs string) /*AMethodDecl baseConstructor = finalTrans.data.ConstructorMap[finalTrans.data.ConstructorBaseLinks[node]]; * * AABlock localsBlock = new AABlock(new ArrayList(), new TRBrace("}")); * ABlockStm cloneBlock = new ABlockStm(new TLBrace("{"), (PBlock) baseConstructor.GetBlock().Clone()); * Dictionary<AALocalDecl, PLvalue> localMap = new Dictionary<AALocalDecl, PLvalue>(); * for (int argNr = 0; argNr < baseConstructor.GetFormals().Count; argNr++) * { * AALocalDecl formal = (AALocalDecl) baseConstructor.GetFormals()[i]; * PExp arg; * if (i < baseConstructor.GetFormals().Count - 1) * arg = (PExp)node.GetBaseArgs()[i]; * else * { * AThisLvalue thisLvalue = new AThisLvalue(new TThis("this")); * ALvalueExp thisExp = new ALvalueExp(thisLvalue); * * ANamedType structType = new ANamedType(new TIdentifier(str.GetName().Text), null); * finalTrans.data.StructTypeLinks[structType] = str; * * finalTrans.data.LvalueTypes[thisLvalue] = * finalTrans.data.ExpTypes[thisExp] = new APointerType(new TStar("*"), structType); * * arg = thisExp; * } * * if (formal.GetRef() != null || formal.GetOut() != null) * { * //Use same variable * localMap[formal] = ((ALvalueExp) arg).GetLvalue(); * } * else * { * //Make a new variable * AALocalDecl newLocal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, * Util.MakeClone(formal.GetType(), finalTrans.data), * new TIdentifier(formal.GetName().Text), * Util.MakeClone(arg, data)); * * ALocalLvalue newLocalRef = new ALocalLvalue(new TIdentifier(newLocal.GetName().Text)); * * localMap[formal] = newLocalRef; * data.LvalueTypes[newLocalRef] = newLocal.GetType(); * data.LocalLinks[newLocalRef] = newLocal; * * localsBlock.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), newLocal)); * } * * } * * CloneMethod cloner = new CloneMethod(finalTrans.data, localMap, cloneBlock); * baseConstructor.GetBlock().Apply(cloner); * * ((AABlock)cloneBlock.GetBlock()).GetStatements().Insert(0, new ABlockStm(new TLBrace("{"), localsBlock)); * ((AABlock)node.GetBlock()).GetStatements().Insert(0, cloneBlock);*/ } //Fix refferences to other struct stuff); base.CaseAMethodDecl(replacer); //Add functionality to refference the current struct in a constructor //Want to do it as a pointer type, since the constructer can only be called for pointer types }
public static StringBuilder TypeToStringBuilder(PType type) { if (type is AVoidType) { return(new StringBuilder("void")); } /*if (type is AArrayType) * { * AArrayType aType = (AArrayType)type; * return TypeToString(aType.GetType()) + "[" + aType.GetDimention().Text + "]"; * }*/ if (type is AArrayTempType) { AArrayTempType aType = (AArrayTempType)type; StringBuilder builder = new StringBuilder(); builder.Append(TypeToStringBuilder(aType.GetType())); builder.Append("["); if (aType.GetIntDim() != null) { builder.Append(aType.GetIntDim().Text); } builder.Append("]"); return(builder); } if (type is ANamedType) { ANamedType aType = (ANamedType)type; StringBuilder builder = new StringBuilder(); foreach (TIdentifier identifier in ((AAName)aType.GetName()).GetIdentifier()) { if (builder.Length != 0) { builder.Append("."); } builder.Append(identifier.Text); } return(builder); } if (type is APointerType) { APointerType aType = (APointerType)type; StringBuilder builder = new StringBuilder(); builder.Append(TypeToStringBuilder(aType.GetType())); builder.Append("*"); return(builder); } if (type is ADynamicArrayType) { ADynamicArrayType aType = (ADynamicArrayType)type; StringBuilder builder = new StringBuilder(); builder.Append(TypeToStringBuilder(aType.GetType())); builder.Append("[]"); return(builder); } if (type is AGenericType) { AGenericType aType = (AGenericType)type; StringBuilder builder = new StringBuilder(); builder.Append(TypeToStringBuilder(aType.GetBase())); builder.Append("<"); bool first = true; foreach (PType t in aType.GetGenericTypes()) { if (first) { first = false; } else { builder.Append(", "); } builder.Append(TypeToStringBuilder(t)); } builder.Append(">"); return(builder); } throw new Exception("Unknown type. Got " + type); }
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()); }