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 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 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)
 {
     InAALocalDecl(node);
     if (node.GetInit() != null)
     {
         node.GetInit().Apply(this);
     }
     if (node.GetName() != null)
     {
         node.GetName().Apply(this);
     }
     if (node.GetType() != null)
     {
         node.GetType().Apply(this);
     }
     if (node.GetConst() != null)
     {
         node.GetConst().Apply(this);
     }
     if (node.GetOut() != null)
     {
         node.GetOut().Apply(this);
     }
     if (node.GetRef() != null)
     {
         node.GetRef().Apply(this);
     }
     if (node.GetStatic() != null)
     {
         node.GetStatic().Apply(this);
     }
     if (node.GetVisibilityModifier() != null)
     {
         node.GetVisibilityModifier().Apply(this);
     }
     OutAALocalDecl(node);
 }
        /*public override void CaseAShadyLocalDecl(AShadyLocalDecl node)
        {
            bool hasIdentifier = node. node.GetName() != null;
            if (hasIdentifier)
            {//Replace with a non shady local decl
                if (node.GetInit() != null)
                    errors.Add(new ErrorCollection.Error(node.GetName(), currentSourceFile, "Array declarations cannot be initialized", false));

                AALocalDecl localDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, node.GetConst(), node.GetType(), node.GetName(), null);
                node.ReplaceBy(localDecl);
                localDecl.Apply(this);
            }
            else
            {
                //If it is not a declaration, it is an assignment exp, so we must have a right side
                if (node.GetInit() == null)
                {
                    errors.Add(new ErrorCollection.Error(GetToken(node.GetType()), currentSourceFile, "Expected assignment", false));
                    node.Parent().RemoveChild(node);
                    return;
                }
                //If it says const first, we got an error
                if (node.GetConst() != null)
                    errors.Add(new ErrorCollection.Error(node.GetConst(), currentSourceFile, "Unexpected const", false));

                ALocalDeclStm parent = (ALocalDeclStm)node.Parent();
                PLvalue lvalue = DetypeArray(node.GetType());
                ASAssignmentExp exp = new ASAssignmentExp(node.GetAssignop(), lvalue, node.GetInit());
                AExpStm stm = new AExpStm(parent.GetToken(), exp);
                parent.ReplaceBy(stm);
                stm.Apply(this);
            }
        }*/
        public override void OutAALocalDecl(AALocalDecl node)
        {
            if (Util.GetAncestor<AABlock>(node) != null)
            {//We are a local defined inside a method
                if (node.GetConst() != null && node.GetInit() == null)
                    errors.Add(new ErrorCollection.Error(node.GetConst(), currentSourceFile, LocRM.GetString("ErrorText190"), false));
            }
            else if (Util.HasAncestor<AMethodDecl>(node) || Util.HasAncestor<AConstructorDecl>(node))
            {//We are in a parameter
                if (node.GetConst() != null)
                    errors.Add(new ErrorCollection.Error(node.GetConst(), currentSourceFile, LocRM.GetString("ErrorText191"), false));
            }
            else
            {
                //Struct var
                if (node.GetConst() != null && node.GetInit() == null)
                    errors.Add(new ErrorCollection.Error(node.GetConst(), currentSourceFile, LocRM.GetString("ErrorText190"), false));
            }

            base.OutAALocalDecl(node);
        }
        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);
        }