public override void CaseALocalDeclStm(ALocalDeclStm node) { AMethodDecl pMethod = Util.GetAncestor <AMethodDecl>(node); AALocalDecl decl = (AALocalDecl)node.GetLocalDecl(); if (decl.GetInit() == null) { ALocalLvalue lvalue = new ALocalLvalue(new TIdentifier(decl.GetName().Text)); data.LocalLinks[lvalue] = decl; data.LvalueTypes[lvalue] = decl.GetType(); List <PStm> statements = AssignDefault(lvalue); AABlock pBlock = (AABlock)node.Parent(); foreach (PStm statement in statements) { pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(node), statement); } pBlock.RemoveChild(node); } else { //Make an assignment expression before moving ALocalLvalue lvalue = new ALocalLvalue(new TIdentifier(decl.GetName().Text)); data.LvalueTypes[lvalue] = decl.GetType(); AAssignmentExp exp = new AAssignmentExp(new TAssign("="), lvalue, decl.GetInit()); AExpStm expStm = new AExpStm(new TSemicolon(";"), exp); node.ReplaceBy(expStm); data.LvalueTypes[lvalue] = decl.GetType(); data.ExpTypes[exp] = decl.GetType(); data.LocalLinks[lvalue] = decl; } AABlock block = (AABlock)pMethod.GetBlock(); block.GetStatements().Insert(0, node); }
public override void CaseAALocalDecl(AALocalDecl node) { if (node.GetConst() == null) { return; } initialLocalDecl = node; if (IsConstant(node.GetInit())) { { List <ALocalLvalue> lvalues = new List <ALocalLvalue>(); lvalues.AddRange(data.LocalLinks.Where(link => link.Value == node).Select(link => link.Key)); foreach (ALocalLvalue lvalue in lvalues) { PExp parent = (PExp)lvalue.Parent(); parent.ReplaceBy(Util.MakeClone(node.GetInit(), data)); } } { List <AStructLvalue> lvalues = new List <AStructLvalue>(); lvalues.AddRange(data.StructFieldLinks.Where(link => link.Value == node).Select(link => link.Key)); foreach (AStructLvalue lvalue in lvalues) { PExp parent = (PExp)lvalue.Parent(); parent.ReplaceBy(Util.MakeClone(node.GetInit(), data)); } } { List <AStructFieldLvalue> lvalues = new List <AStructFieldLvalue>(); lvalues.AddRange( data.StructMethodFieldLinks.Where(link => link.Value == node).Select(link => link.Key)); foreach (AStructFieldLvalue lvalue in lvalues) { PExp parent = (PExp)lvalue.Parent(); parent.ReplaceBy(Util.MakeClone(node.GetInit(), data)); } } if (node.Parent() is ALocalDeclStm) { node.Parent().Parent().RemoveChild(node.Parent()); } else { node.Parent().RemoveChild(node); } } initialLocalDecl = null; }
public override void CaseAALocalDecl(AALocalDecl node) { if (node.GetConst() != null) { Write("const "); } node.GetType().Apply(this); Write(" " + node.GetName().Text); if (node.GetInit() != null) { Write(" = "); node.GetInit().Apply(this); } }
public VariableDescription(AALocalDecl localDecl, VariableTypes type) { Name = localDecl.GetName().Text; Type = Util.TypeToString(localDecl.GetType()); switch (type) { case VariableTypes.LocalVariable: PlacementPrefix = "Local"; break; case VariableTypes.Parameter: PlacementPrefix = "Parameter"; break; case VariableTypes.StructVariable: PlacementPrefix = "Struct field"; break; default: PlacementPrefix = ""; break; } VariableType = type; Const = localDecl.GetConst() != null; IsStatic = localDecl.GetStatic() != null; Visibility = localDecl.GetVisibilityModifier(); realType = (PType)localDecl.GetType().Clone(); init = localDecl.GetInit(); Line = localDecl.GetName().Line; Position = TextPoint.FromCompilerCoords(localDecl.GetName()); }
public override void OutAStructFieldLvalue(AStructFieldLvalue node) { if (folding) { if (data.StructMethodPropertyLinks.ContainsKey(node)) { errors.Add( new ErrorCollection.Error(node.GetName(), LocRM.GetString("ErrorText61"), false, new ErrorCollection.Error( data.StructMethodPropertyLinks[node].GetName(), LocRM.GetString("ErrorText62"))), true); throw new ParserException(node.GetName(), "TypeLinking.OutAStructFieldLvalue"); } AALocalDecl field = data.StructMethodFieldLinks[node]; 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.OutAStructFieldLvalue"); } if (field.GetConst() == null) { if (!isANewExp) { errors.Add( new ErrorCollection.Error(node.GetName(), LocRM.GetString("ErrorText62"), false, new ErrorCollection.Error(field.GetName(), LocRM.GetString("ErrorText60"))), 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); } }
private int FoldInt(PLvalue lvalue, ref bool valid) { if (!valid) { return(-1); } if (lvalue is ALocalLvalue) { ALocalLvalue aLvalue = (ALocalLvalue)lvalue; AALocalDecl decl = data.LocalLinks[aLvalue]; if (decl.GetConst() == null) { valid = false; return(-1); } return(FoldInt(decl.GetInit(), ref valid)); } if (lvalue is AFieldLvalue) { AFieldLvalue aLvalue = (AFieldLvalue)lvalue; AFieldDecl decl = data.FieldLinks[aLvalue]; if (decl.GetConst() == null) { valid = false; return(-1); } return(FoldInt(decl.GetInit(), ref valid)); } if (lvalue is AStructFieldLvalue) { AStructFieldLvalue aLvalue = (AStructFieldLvalue)lvalue; AALocalDecl decl = data.StructMethodFieldLinks[aLvalue]; if (decl.GetConst() == null) { valid = false; return(-1); } return(FoldInt(decl.GetInit(), ref valid)); } if (lvalue is AStructLvalue) { AStructLvalue aLvalue = (AStructLvalue)lvalue; AALocalDecl decl = data.StructFieldLinks[aLvalue]; if (decl.GetConst() == null) { valid = false; return(-1); } return(FoldInt(decl.GetInit(), ref valid)); } valid = false; return(-1); }
public override void OutALocalLvalue(ALocalLvalue node) { if (folding) { AALocalDecl local = data.LocalLinks[node]; if (local.GetConst() == null) { if (!isANewExp) { errors.Add( new ErrorCollection.Error(node.GetName(), LocRM.GetString("ErrorText61"), false), true); throw new ParserException(null, null); } } if (local.GetInit() == null)//An error will be given earlier { throw new ParserException(null, null); } local.GetInit().Apply(this); } }
public override void OutALocalLvalue(ALocalLvalue node) { if (folding) { AALocalDecl local = data.LocalLinks[node]; if (local.GetConst() == null) { if (!isANewExp) { errors.Add( new ErrorCollection.Error(node.GetName(), "Dimensions of array types must be constant expressions.", false), true); throw new ParserException(null, null); } } if (local.GetInit() == null)//An error will be given earlier { throw new ParserException(null, null); } local.GetInit().Apply(this); } }
public override void OutAALocalDecl(AALocalDecl node) { if (!Util.HasAncestor <AABlock>(node) && !Util.HasAncestor <AMethodDecl>(node)) { //OutStructFieldDecl(node); return; } if (node.GetInit() != null) { return; } AABlock pBlock; int insertIndex; PLvalue lvalue; if (Util.HasAncestor <AABlock>(node)) { //A local variable pBlock = Util.GetAncestor <AABlock>(node); insertIndex = pBlock.GetStatements().IndexOf(Util.GetAncestor <PStm>(node)) + 1; lvalue = new ALocalLvalue(new TIdentifier(node.GetName().Text)); data.LocalLinks[(ALocalLvalue)lvalue] = node; data.LvalueTypes[lvalue] = node.GetType(); } else { //Parameter //Parameters will be set from the caller return; pBlock = (AABlock)Util.GetAncestor <AMethodDecl>(node).GetBlock(); insertIndex = 0; lvalue = new ALocalLvalue(new TIdentifier(node.GetName().Text)); data.LocalLinks[(ALocalLvalue)lvalue] = node; data.LvalueTypes[lvalue] = node.GetType(); } AABlock block = new AABlock(new ArrayList(), new TRBrace("}")); MakeAssignments(block, node.GetType(), lvalue, true); if (block.GetStatements().Count != 0) { pBlock.GetStatements().Insert(insertIndex, new ABlockStm(new TLBrace("{"), block)); } }
bool IsConstant(PLvalue lvalue) { if (lvalue is ALocalLvalue) { ALocalLvalue aLvalue = (ALocalLvalue)lvalue; AALocalDecl decl = data.LocalLinks[aLvalue]; if (decl == initialLocalDecl) { return(false); } return(decl.GetConst() != null && IsConstant(decl.GetInit())); } if (lvalue is AFieldLvalue) { AFieldLvalue aLvalue = (AFieldLvalue)lvalue; AFieldDecl decl = data.FieldLinks[aLvalue]; if (decl == initialFieldDecl) { return(false); } return(decl.GetConst() != null && IsConstant(decl.GetInit())); } if (lvalue is APropertyLvalue) { return(false); } if (lvalue is ANamespaceLvalue) { return(true); } if (lvalue is AStructFieldLvalue) { AStructFieldLvalue aLvalue = (AStructFieldLvalue)lvalue; AALocalDecl decl = data.StructMethodFieldLinks[aLvalue]; if (decl == initialLocalDecl) { return(false); } return(decl.GetConst() != null && IsConstant(decl.GetInit())); } if (lvalue is AStructLvalue) { AStructLvalue aLvalue = (AStructLvalue)lvalue; AALocalDecl decl = data.StructFieldLinks[aLvalue]; if (decl == initialLocalDecl) { return(false); } return(decl.GetConst() != null && IsConstant(decl.GetInit())); } if (lvalue is AArrayLvalue) { AArrayLvalue aLvalue = (AArrayLvalue)lvalue; return(IsConstant(aLvalue.GetIndex()) && IsConstant(aLvalue.GetBase())); } if (lvalue is APointerLvalue) { APointerLvalue aLvalue = (APointerLvalue)lvalue; return(IsConstant(aLvalue.GetBase())); } if (lvalue is APArrayLengthLvalue) { APArrayLengthLvalue aLvalue = (APArrayLengthLvalue)lvalue; return(data.ExpTypes[aLvalue.GetBase()] is AArrayTempType); } if (lvalue is AThisLvalue) { return(false); } if (lvalue is AValueLvalue) { return(false); } throw new Exception("Unexpected lvalue. Got " + lvalue); }
public override void CaseAALocalDecl(AALocalDecl node) { //Convert a static struct field into a global variable. All refferences to it are structFieldLvalues. if (node.GetStatic() == null) { return; } AStructDecl str = (AStructDecl)node.Parent(); if (data.StructFields[str].Contains(node)) { data.StructFields[str].Remove(node); } AFieldDecl replacementField; //Don't enhrit static fields. if (data.EnheritanceLocalMap.ContainsKey(node)) { str.RemoveChild(node); AALocalDecl realVar = data.EnheritanceLocalMap[node]; if (convertionMap.ContainsKey(realVar)) { //Already converted to a field replacementField = convertionMap[realVar]; foreach (AStructFieldLvalue lvalue in data.StructMethodFieldLinks.Where(link => link.Value == node).Select(link => link.Key)) { AFieldLvalue newLvalue = new AFieldLvalue(new TIdentifier(replacementField.GetName().Text)); data.FieldLinks[newLvalue] = replacementField; data.LvalueTypes[newLvalue] = replacementField.GetType(); lvalue.ReplaceBy(newLvalue); } } else { List <AStructFieldLvalue> refferences = new List <AStructFieldLvalue>(); refferences.AddRange(data.StructMethodFieldLinks.Where(link => link.Value == node).Select(link => link.Key)); foreach (AStructFieldLvalue lvalue in refferences) { data.StructMethodFieldLinks[lvalue] = realVar; } } return; } replacementField = new AFieldDecl(new APublicVisibilityModifier(), null, node.GetConst(), node.GetType(), node.GetName(), node.GetInit()); replacementField.GetName().Text = str.GetName().Text + "_" + replacementField.GetName().Text; AASourceFile file = Util.GetAncestor <AASourceFile>(node); file.GetDecl().Insert(file.GetDecl().IndexOf(node.Parent()), replacementField); data.Fields.Add(new SharedData.DeclItem <AFieldDecl>(file, replacementField)); if (ContainsNewExp(replacementField.GetInit())) { data.FieldsToInitInMapInit.Add(replacementField); } foreach (AStructFieldLvalue lvalue in data.StructMethodFieldLinks.Where(link => link.Value == node).Select(link => link.Key)) { AFieldLvalue newLvalue = new AFieldLvalue(new TIdentifier(replacementField.GetName().Text)); data.FieldLinks[newLvalue] = replacementField; data.LvalueTypes[newLvalue] = replacementField.GetType(); lvalue.ReplaceBy(newLvalue); } convertionMap.Add(node, replacementField); node.Parent().RemoveChild(node); }