public override void OutAStructDecl(AStructDecl node) { //Insert parameterless constructor if ( !node.GetLocals().OfType <ADeclLocalDecl>().Select(localDecl => localDecl.GetDecl()).OfType <AConstructorDecl>().Any(constructor => constructor.GetFormals().Count == 0)) { node.GetLocals().Add( new ADeclLocalDecl(new AConstructorDecl(new APublicVisibilityModifier(), new TIdentifier(node.GetName().Text), new ArrayList(), new ArrayList(), new AABlock(new ArrayList(), new TRBrace("}"))))); } }
public override void OutAStructDecl(AStructDecl node) { //Insert init in each constructor AThisLvalue thisLvalue = new AThisLvalue(new TThis("this")); ALvalueExp thisExp = new ALvalueExp(thisLvalue); APointerLvalue pointerLvalue = new APointerLvalue(new TStar("*"), thisExp); ANamedType namedType = new ANamedType(new TIdentifier(node.GetName().Text), null); data.StructTypeLinks[namedType] = node; data.LvalueTypes[thisLvalue] = data.ExpTypes[thisExp] = new APointerType(new TStar("*"), namedType); data.LvalueTypes[pointerLvalue] = namedType; foreach (AConstructorDecl constructor in node.GetLocals().OfType <ADeclLocalDecl>().Select(decl => decl.GetDecl()).OfType <AConstructorDecl>()) { AABlock block = new AABlock(new ArrayList(), new TRBrace("}")); MakeAssignments(block, namedType, pointerLvalue, false); ((AABlock)constructor.GetBlock()).GetStatements().Insert(0, new ABlockStm(new TLBrace("{"), block)); } base.OutAStructDecl(node); }
public override void CaseAStructDecl(AStructDecl node) { Write("struct " + node.GetName().Text + "\n{\n"); foreach (AALocalDecl local in node.GetLocals().OfType <AALocalDecl>()) { local.Apply(this); Write(";\n"); } Write("};\n"); }
public override void CaseAStructDecl(AStructDecl node) { if (node.GetLocals().Count == 0) { node.Parent().RemoveChild(node); } else { base.CaseAStructDecl(node); } }
public override void CaseAALocalDecl(AALocalDecl node) { if (node.GetType() is ANamedType) { ANamedType type = (ANamedType)node.GetType(); if (finalTrans.data.StructTypeLinks.ContainsKey(type)) { AStructDecl strDecl = finalTrans.data.StructTypeLinks[type]; if (strDecl.GetLocals().Cast <PLocalDecl>().Select(decl => decl is AALocalDecl).Count() == 0) { MoveMethodDeclsOut mover = new MoveMethodDeclsOut("removedStructVar", finalTrans.data); node.Apply(mover); foreach (PStm stm in mover.NewStatements) { stm.Apply(this); } PStm pStm = Util.GetAncestor <PStm>(node); if (pStm == null) { strDecl = Util.GetAncestor <AStructDecl>(node); node.Parent().RemoveChild(node); if (strDecl != null && strDecl.GetLocals().Cast <PLocalDecl>().Select(decl => decl is AALocalDecl).Count() == 0) { reqRerun = true; } } else { pStm.Parent().RemoveChild(pStm); } return; } } } base.CaseAALocalDecl(node); }
private void MakeAssignments(AABlock block, PType type, PLvalue leftSide, bool onEnhritedFields) { if (type is ANamedType && data.StructTypeLinks.ContainsKey((ANamedType)type)) { AStructDecl str = data.StructTypeLinks[(ANamedType)type]; foreach (AALocalDecl field in str.GetLocals().OfType <AALocalDecl>()) { if (!onEnhritedFields && data.EnheritanceLocalMap.ContainsKey(field)) { continue; } ALvalueExp lvalueExp = new ALvalueExp(Util.MakeClone(leftSide, data)); data.ExpTypes[lvalueExp] = data.LvalueTypes[leftSide]; AStructLvalue newLeftSide = new AStructLvalue(lvalueExp, new ADotDotType(new TDot(".")), new TIdentifier(field.GetName().Text)); data.StructFieldLinks[newLeftSide] = field; data.LvalueTypes[newLeftSide] = field.GetType(); if (field.GetInit() != null) { AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), newLeftSide, Util.MakeClone(field.GetInit(), data)); data.ExpTypes[assignment] = data.LvalueTypes[newLeftSide]; block.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment)); } else { MakeAssignments(block, field.GetType(), newLeftSide, onEnhritedFields); } } } else if (type is AArrayTempType) { AArrayTempType aType = (AArrayTempType)type; for (int i = 0; i < int.Parse(aType.GetIntDim().Text); i++) { AIntConstExp index = new AIntConstExp(new TIntegerLiteral(i.ToString())); data.ExpTypes[index] = new ANamedType(new TIdentifier("int"), null); ALvalueExp lvalueExp = new ALvalueExp(Util.MakeClone(leftSide, data)); data.ExpTypes[lvalueExp] = data.LvalueTypes[leftSide]; AArrayLvalue newLeftSide = new AArrayLvalue(new TLBracket("["), lvalueExp, index); data.LvalueTypes[newLeftSide] = aType.GetType(); MakeAssignments(block, aType.GetType(), newLeftSide, onEnhritedFields); } } }
//Convert struct variables to a collection of local variables public override void CaseAALocalDecl(AALocalDecl node) { if (node.GetType() is ANamedType && data.StructTypeLinks.ContainsKey((ANamedType)node.GetType()) && Util.HasAncestor <PStm>(node)) { //Can not have init - it would be bulk copy AStructDecl str = data.StructTypeLinks[(ANamedType)node.GetType()]; Dictionary <AALocalDecl, AALocalDecl> variableMap = new Dictionary <AALocalDecl, AALocalDecl>(); PStm pStm = (PStm)node.Parent(); AABlock pBlock = (AABlock)pStm.Parent(); foreach (AALocalDecl structLocal in str.GetLocals()) { AALocalDecl replacementLocal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(structLocal.GetType(), data), new TIdentifier(node.GetName().Text + "_" + structLocal.GetName().Text), null); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), new ALocalDeclStm(new TSemicolon(";"), replacementLocal)); AALocalDecl baseLocal = structLocal; if (data.EnheritanceLocalMap.ContainsKey(baseLocal)) { baseLocal = data.EnheritanceLocalMap[baseLocal]; } List <AALocalDecl> localsToAdd = new List <AALocalDecl>(); localsToAdd.AddRange(data.EnheritanceLocalMap.Where(pair => pair.Value == baseLocal).Select(pair => pair.Key)); localsToAdd.Add(baseLocal); foreach (AALocalDecl localDecl in localsToAdd) { variableMap[localDecl] = replacementLocal; } } List <ALocalLvalue> uses = new List <ALocalLvalue>(); uses.AddRange(data.LocalLinks.Where(k => k.Value == node && Util.GetAncestor <AAProgram>(k.Key) != null).Select(k => k.Key)); foreach (ALocalLvalue lvalue in uses) { AStructLvalue structLocalRef = (AStructLvalue)lvalue.Parent().Parent(); AALocalDecl replacementLocal = variableMap[data.StructFieldLinks[structLocalRef]]; ALocalLvalue replacementLvalue = new ALocalLvalue(new TIdentifier(replacementLocal.GetName().Text)); data.LocalLinks[replacementLvalue] = replacementLocal; data.LvalueTypes[replacementLvalue] = replacementLocal.GetType(); structLocalRef.ReplaceBy(replacementLvalue); } foreach (AALocalDecl replacementLocal in variableMap.Select(k => k.Value)) { replacementLocal.Apply(this); } } base.CaseAALocalDecl(node); }
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 CaseAFieldDecl(AFieldDecl node) { if (node.GetType() is ANamedType) { ANamedType type = (ANamedType)node.GetType(); if (finalTrans.data.StructTypeLinks.ContainsKey(type)) { AStructDecl strDecl = finalTrans.data.StructTypeLinks[type]; if (strDecl.GetLocals().Cast <PLocalDecl>().Select(decl => decl is AALocalDecl).Count() == 0) { node.Parent().RemoveChild(node); return; } } } base.CaseAFieldDecl(node); }
public override void OutABinopExp(ABinopExp node) { if (data.ExpTypes[node.GetLeft()] is ANamedType && data.StructTypeLinks.ContainsKey((ANamedType)data.ExpTypes[node.GetLeft()])) { AStructDecl str = data.StructTypeLinks[(ANamedType)data.ExpTypes[node.GetLeft()]]; PExp replacementExp = new ABooleanConstExp(new ATrueBool()); data.ExpTypes[replacementExp] = new ANamedType(new TIdentifier("bool"), null); foreach (AALocalDecl local in str.GetLocals().OfType <AALocalDecl>()) { AStructLvalue leftSide = new AStructLvalue(Util.MakeClone(node.GetLeft(), data), new ADotDotType(new TDot(".")), new TIdentifier(local.GetName().Text)); ALvalueExp leftSideExp = new ALvalueExp(leftSide); AStructLvalue rightSide = new AStructLvalue(Util.MakeClone(node.GetRight(), data), new ADotDotType(new TDot(".")), new TIdentifier(local.GetName().Text)); ALvalueExp rightSideExp = new ALvalueExp(rightSide); data.StructFieldLinks[leftSide] = data.StructFieldLinks[rightSide] = local; data.LvalueTypes[leftSide] = data.ExpTypes[leftSideExp] = data.LvalueTypes[rightSide] = data.ExpTypes[rightSideExp] = local.GetType(); ABinopExp binop = new ABinopExp(leftSideExp, (PBinop)node.GetBinop().Clone(), rightSideExp); data.ExpTypes[binop] = data.ExpTypes[node]; if (replacementExp is ABooleanConstExp) { replacementExp = binop; } else { replacementExp = new ABinopExp(replacementExp, new ALazyAndBinop(new TAndAnd("&&")), binop); data.ExpTypes[replacementExp] = new ANamedType(new TIdentifier("bool"), null); } } node.ReplaceBy(replacementExp); replacementExp.Apply(this); } base.OutABinopExp(node); }
public override void DefaultIn(Node node) { if (node is PExp) { PExp exp = (PExp)node; if (finalTrans.data.ExpTypes[exp] is ANamedType) { ANamedType type = (ANamedType)finalTrans.data.ExpTypes[exp]; if (finalTrans.data.StructTypeLinks.ContainsKey(type)) { AStructDecl strDecl = finalTrans.data.StructTypeLinks[type]; if (strDecl.GetLocals().Cast <PLocalDecl>().Select(decl => decl is AALocalDecl).Count() == 0) { if (node.Parent() is AAssignmentExp) { node = node.Parent().Parent(); } MoveMethodDeclsOut mover = new MoveMethodDeclsOut("removedStructVar", finalTrans.data); node.Apply(mover); foreach (PStm pStm in mover.NewStatements) { pStm.Apply(this); } node.Parent().RemoveChild(node); if (node.Parent() is ABinopExp) { ABinopExp parent = (ABinopExp)node.Parent(); ABooleanConstExp replacer; if (parent.GetBinop() is ANeBinop || parent.GetBinop() is AGtBinop || parent.GetBinop() is ALtBinop) { replacer = new ABooleanConstExp(new AFalseBool()); } else { replacer = new ABooleanConstExp(new ATrueBool()); } finalTrans.data.ExpTypes[replacer] = new ANamedType(new TIdentifier("bool"), null); parent.ReplaceBy(replacer); } } } } } }
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 CaseAStructDecl(AStructDecl node) { if (checkedStructs.Contains(node)) { return; } checkedStructs.Add(node); //Set where they originate from. foreach (PLocalDecl localDecl in node.GetLocals()) { if (localDecl is AALocalDecl) { fieldOriginatesFrom[(AALocalDecl)localDecl] = node; } else //Is DeclLocalDecl { ADeclLocalDecl aLocalDecl = (ADeclLocalDecl)localDecl; PDecl decl = aLocalDecl.GetDecl(); if (decl is AMethodDecl) { methodOriginatesFrom[(AMethodDecl)decl] = node; } else if (decl is APropertyDecl) { propertyOriginatesFrom[(APropertyDecl)decl] = node; } else if (decl is AThisArrayPropertyDecl) { arrayPropertyOriginatesFrom[(AThisArrayPropertyDecl)decl] = node; } } } if (node.GetBase() == null) { return; } AStructDecl baseStr = Lookup((ANamedType)node.GetBase()); if (!checkedStructs.Contains(baseStr)) { CaseAStructDecl(baseStr); } CheckEnheritanceList(node, new List <AStructDecl>()); //A struct may not enhrit from a class if (node.GetClassToken() == null && baseStr.GetClassToken() != null) { errors.Add(new ErrorCollection.Error(node.GetName(), "A struct can not enherit from a class.", false, new ErrorCollection.Error(baseStr.GetName(), "Enherited class"))); } //Copy everything in base struct to here (Except from constructors) List <PLocalDecl> stuffToAdd = new List <PLocalDecl>(); foreach (AALocalDecl baseLocalVar in data.StructFields[baseStr]) { //Check that it is not overwritten foreach (AALocalDecl localVar in node.GetLocals().OfType <AALocalDecl>()) { if (baseLocalVar.GetName().Text == localVar.GetName().Text) { errors.Add(new ErrorCollection.Error(localVar.GetName(), "It is not possible to override fields.", false, new ErrorCollection.Error( fieldOriginatesFrom[baseLocalVar].GetName(), "Overridden " + Util.GetTypeName(fieldOriginatesFrom[baseLocalVar])))); throw new ParserException(null, null); } } foreach (APropertyDecl localProperty in data.StructProperties[node]) { if (localProperty.GetName().Text == baseLocalVar.GetName().Text) { errors.Add(new ErrorCollection.Error(localProperty.GetName(), "It is not possible to override fields.", false, new ErrorCollection.Error( fieldOriginatesFrom[baseLocalVar].GetName(), "Overridden " + Util.GetTypeName(fieldOriginatesFrom[baseLocalVar])))); throw new ParserException(null, null); } } //Insert at top AALocalDecl newLocalVar = (AALocalDecl)baseLocalVar.Clone(); baseLocalVar.Apply(new FixNamedRefferences(newLocalVar, data)); stuffToAdd.Add(newLocalVar); data.StructFields[node].Add(newLocalVar); fieldOriginatesFrom[newLocalVar] = fieldOriginatesFrom[baseLocalVar]; if (data.EnheritanceLocalMap.ContainsKey(baseLocalVar)) { data.EnheritanceLocalMap[newLocalVar] = data.EnheritanceLocalMap[baseLocalVar]; } else { data.EnheritanceLocalMap[newLocalVar] = baseLocalVar; } } for (int i = stuffToAdd.Count - 1; i >= 0; i--) { node.GetLocals().Insert(0, stuffToAdd[i]); } //Methods foreach (AMethodDecl baseMethod in data.StructMethods[baseStr]) { //Check that it is not overwritten foreach (AMethodDecl localMethod in node.GetLocals().OfType <ADeclLocalDecl>().Select(l => l.GetDecl()).OfType <AMethodDecl>()) { if (Util.GetMethodSignature(baseMethod) == Util.GetMethodSignature(localMethod)) { errors.Add(new ErrorCollection.Error(localMethod.GetName(), "It is not possible to override methods.", false, new ErrorCollection.Error( methodOriginatesFrom[baseMethod].GetName(), "Overridden " + Util.GetTypeName(methodOriginatesFrom[baseMethod])))); throw new ParserException(null, null); } } data.StructMethods[node].Add(baseMethod); } //Properties foreach (APropertyDecl baseProperty in data.StructProperties[baseStr]) { //Check that it is not overwritten foreach (APropertyDecl localProperty in data.StructProperties[node]) { if (localProperty.GetName().Text == baseProperty.GetName().Text) { errors.Add(new ErrorCollection.Error(localProperty.GetName(), "It is not possible to override properties.", false, new ErrorCollection.Error( propertyOriginatesFrom[baseProperty].GetName(), "Overridden " + Util.GetTypeName(propertyOriginatesFrom[baseProperty])))); throw new ParserException(null, null); } } foreach (AALocalDecl localVar in node.GetLocals().OfType <AALocalDecl>()) { if (baseProperty.GetName().Text == localVar.GetName().Text) { errors.Add(new ErrorCollection.Error(localVar.GetName(), "It is not possible to override properties.", false, new ErrorCollection.Error( propertyOriginatesFrom[baseProperty].GetName(), "Overridden " + Util.GetTypeName(propertyOriginatesFrom[baseProperty])))); throw new ParserException(null, null); } } data.StructProperties[node].Add(baseProperty); } }