コード例 #1
0
        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);
        }
コード例 #2
0
 public override void CaseAALocalDecl(AALocalDecl node)
 {
     //It wont enter methods
     //Repeated fields in structs are syntax errors
     AStructDecl str = Util.GetAncestor<AStructDecl>(node);
     StructFields[str].Add(node);
     node.Parent().RemoveChild(node);
 }
コード例 #3
0
        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;
        }
コード例 #4
0
        //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);
        }
コード例 #5
0
            public override void CaseAALocalDecl(AALocalDecl node)
            {
                //If parent is a methoddecl, we are a parameter
                if (node.Parent() is AMethodDecl)
                {
                    VariableDescription variable = new VariableDescription(node, VariableDescription.VariableTypes.Parameter);
                    Formals.Add(variable);
                }
                else
                {
                    VariableDescription variable = new VariableDescription(node, VariableDescription.VariableTypes.LocalVariable);
                    Locals.Add(variable);
                }

                base.CaseAALocalDecl(node);
            }
コード例 #6
0
        public override void OutAALocalDecl(AALocalDecl node)
        {
            if (node.GetInit() != null)
            {
                PType from = data.ExpTypes[node.GetInit()];
                PType to = node.GetType();
                if (!Assignable(from, to))
                {
                    if (ImplicitAssignable(from, to))
                    {
                        ANamedType namedTo = (ANamedType)to;
                        ACastExp cast = new ACastExp(new TLParen("("), new ANamedType(new TIdentifier(((AAName)namedTo.GetName()).AsString()), null), node.GetInit());
                        node.SetInit(cast);
                        OutACastExp(cast);
                        to = from;
                    }
                    else
                        errors.Add(new ErrorCollection.Error(node.GetName(), currentSourceFile,
                                                             LocRM.GetString("ErrorText151") + Util.TypeToString(from) +
                                                             LocRM.GetString("ErrorText152") + Util.TypeToString(to)));
                }
            }

            //If the return type or the type of any formals is a private struct, and the method is a public context, give an error
            if (node.Parent() is AStructDecl)
            {
                AStructDecl pStruct = Util.GetAncestor<AStructDecl>(node);
                //Is public context
                if ( pStruct.GetVisibilityModifier() is APublicVisibilityModifier && !(node.GetVisibilityModifier() is APrivateVisibilityModifier))
                {
                    PType type = node.GetType();
                    int i = 0;
                    FindPrivateTypes finder = new FindPrivateTypes(data);
                    type.Apply(finder);

                    if (finder.PrivateTypes.Count > 0)
                    {
                        List<ErrorCollection.Error> subErrors = new List<ErrorCollection.Error>();
                        List<PDecl> usedDecls = new List<PDecl>();
                        foreach (ANamedType namedType in finder.PrivateTypes)
                        {
                            if (data.StructTypeLinks.ContainsKey(namedType))
                            {
                                AStructDecl decl = data.StructTypeLinks[namedType];
                                if (usedDecls.Contains(decl))
                                    continue;
                                usedDecls.Add(decl);
                                subErrors.Add(new ErrorCollection.Error(decl.GetName(), LocRM.GetString("ErrorText64")));
                            }
                            else if (data.DelegateTypeLinks.ContainsKey(namedType))
                            {
                                AMethodDecl decl = data.DelegateTypeLinks[namedType];
                                if (usedDecls.Contains(decl))
                                    continue;
                                usedDecls.Add(decl);
                                subErrors.Add(new ErrorCollection.Error(decl.GetName(), LocRM.GetString("ErrorText154")));
                            }
                        }

                        errors.Add(new ErrorCollection.Error(node.GetName(), LocRM.GetString("ErrorText155"), false, subErrors.ToArray()));
                    }
                }
            }
            base.OutAALocalDecl(node);
        }
コード例 #7
0
        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);
        }