public VariableDescription(AFieldDecl fieldDecl)
 {
     Name = fieldDecl.GetName().Text;
     Type = Util.TypeToString(fieldDecl.GetType());
     PlacementPrefix = "Field";
     VariableType = VariableTypes.Field;
     Const = fieldDecl.GetConst() != null;
     IsStatic = fieldDecl.GetStatic() != null;
     Visibility = fieldDecl.GetVisibilityModifier();
     realType = (PType)fieldDecl.GetType().Clone();
     init = fieldDecl.GetInit();
     Line = fieldDecl.GetName().Line;
     Position = TextPoint.FromCompilerCoords(fieldDecl.GetName());
 }
 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 CaseAFieldDecl(AFieldDecl node)
 {
     if (node.GetStatic() != null)
         return;
     writer.WriteLine(TypeToString(node.GetType()) + " " + node.GetName().Text + ";");
     if (Fields.Any(decl => decl.GetName().Text == node.GetName().Text))
     {
         return;
     }
     Fields.Add(node);
     //node.SetInit(null);
     node.Parent().RemoveChild(node);
 }
Exemplo n.º 4
0
 public override void CaseAFieldDecl(AFieldDecl node)
 {
     InAFieldDecl(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.GetStatic() != null)
     {
         node.GetStatic().Apply(this);
     }
     if (node.GetVisibilityModifier() != null)
     {
         node.GetVisibilityModifier().Apply(this);
     }
     OutAFieldDecl(node);
 }
        public override void OutAFieldDecl(AFieldDecl node)
        {
            if (node.GetInit() != null)
            {
                PType from = data.ExpTypes[node.GetInit()];
                PType to = node.GetType();
                if (!Assignable(from, to))
                {
                    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
            {
                //Is public context
                if (node.GetVisibilityModifier() is APublicVisibilityModifier)
                {
                    PType type = node.GetType();
                    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.OutAFieldDecl(node);
        }
 public override void CaseAFieldDecl(AFieldDecl node)
 {
     if (node.GetStatic() != null) Write("static ");
     if (node.GetConst() != null) Write("const ");
     node.GetType().Apply(this);
     Write(" " + node.GetName().Text);
     if (node.GetInit() != null)
     {
         Write(" = ");
         node.GetInit().Apply(this);
     }
     Write(";\n\n");
 }
        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);
        }
        public override void OutAFieldDecl(AFieldDecl node)
        {
            if (node.GetInit() != null)
                return;

            //Field - init in main entry
            AABlock pBlock = (AABlock) finalTrans.mainEntryFieldInitBlock.GetBlock();
            int insertIndex = 0;
            AFieldLvalue lvalue = new AFieldLvalue(new TIdentifier(node.GetName().Text));
            data.FieldLinks[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));
        }
Exemplo n.º 9
0
            public override void CaseAFieldDecl(AFieldDecl node)
            {
                PType type = node.GetType();
                if (node.GetInit() is ANullExp)
                {
                    if (type is APointerType)
                    {
                        bool add = true;
                        foreach (PType pType in TypesWithIdentifierArray)
                        {
                            if (Util.TypesEqual(((APointerType)type).GetType(), pType, data))
                            {
                                add = false;
                                break;
                            }
                        }
                        if (add)
                            TypesWithIdentifierArray.Add(((APointerType)type).GetType());
                    }
                }

                base.CaseAFieldDecl(node);
            }
Exemplo n.º 10
0
            public override void CaseAFieldDecl(AFieldDecl node)
            {
                PType type = node.GetType();
                if (node.GetInit() is ANullExp &&
                    type is APointerType &&
                    Util.IsIntPointer(node, ((APointerType)type).GetType(), data))
                {
                    AIntConstExp replacer = new AIntConstExp(new TIntegerLiteral("0"));
                    data.ExpTypes[replacer] = new ANamedType(new TIdentifier("int"), null);
                    node.GetInit().ReplaceBy(replacer);
                }

                base.CaseAFieldDecl(node);
            }
Exemplo n.º 11
0
            //Removed check on top, and get type
            public static GlobalStructVars CreatePointerFields(Node node, TIntegerLiteral literal, string prefix, PType type, SharedData data, bool createArray = true)
            {
                AASourceFile file = Util.GetAncestor<AASourceFile>(node);

                AFieldDecl array = new AFieldDecl(new APublicVisibilityModifier(), null, null,
                                                  new AArrayTempType(new TLBracket("["), type,
                                                                     new AIntConstExp(
                                                                         new TIntegerLiteral(literal.Text)),
                                                                     new TIntegerLiteral(literal.Text)),
                                                                     new TIdentifier(prefix + "_array", data.LineCounts[file] + 20, 0),
                                                                     null);
                int length = (int)Math.Ceiling(float.Parse(literal.Text) / 31);
                AFieldDecl used = new AFieldDecl(new APublicVisibilityModifier(), null, null,
                                                  new AArrayTempType(new TLBracket("["), new ANamedType(new TIdentifier("int"), null),
                                                                     new AIntConstExp(
                                                                         new TIntegerLiteral(length.ToString())),
                                                                     new TIntegerLiteral(length.ToString())),
                                                                     new TIdentifier(prefix + "_used", data.LineCounts[file] + 20, 0),
                                                                     null);
                AFieldDecl index = new AFieldDecl(new APublicVisibilityModifier(), null, null, new ANamedType(new TIdentifier("int"), null),
                                                                     new TIdentifier(prefix + "_index", data.LineCounts[file] + 20, 0),
                                                                     null);
                bool addIdentiferArray = false;
                foreach (PType t in intPointersWithCmp)
                {
                    if (Util.TypesEqual(type, t, data))
                    {
                        addIdentiferArray = true;
                        break;
                    }
                }
                AFieldDecl identiferCount = null;
                AFieldDecl identiferArray = null;
                if (addIdentiferArray)
                {
                    identiferArray = new AFieldDecl(new APublicVisibilityModifier(), null, null,
                                                 new AArrayTempType(new TLBracket("["), new ANamedType(new TIdentifier("int"), null),
                                                                    new AIntConstExp(
                                                                        new TIntegerLiteral(literal.Text)),
                                                                    new TIntegerLiteral(literal.Text)),
                                                                    new TIdentifier(prefix + "_identifierArray", data.LineCounts[file] + 20, 0),
                                                                    null);
                    identiferCount = new AFieldDecl(new APublicVisibilityModifier(), null, null, new ANamedType(new TIdentifier("int"), null),
                                                                         new TIdentifier(prefix + "_identiferNext", data.LineCounts[file] + 20, 0),
                                                                         new AIntConstExp(new TIntegerLiteral("1")));
                    file.GetDecl().Add(identiferArray);
                    file.GetDecl().Add(identiferCount);

                    data.Fields.Add(new SharedData.DeclItem<AFieldDecl>(file, identiferArray));
                    data.Fields.Add(new SharedData.DeclItem<AFieldDecl>(file, identiferCount));

                    data.ExpTypes[((AArrayTempType)identiferArray.GetType()).GetDimention()] =
                        data.ExpTypes[identiferCount.GetInit()] =
                        new ANamedType(new TIdentifier("int"), null);
                }
                if (createArray)
                    file.GetDecl().Add(array);
                file.GetDecl().Add(used);
                file.GetDecl().Add(index);

                data.ExpTypes[((AArrayTempType)array.GetType()).GetDimention()] =
                    data.ExpTypes[((AArrayTempType)used.GetType()).GetDimention()] =
                    new ANamedType(new TIdentifier("int"), null);

                if (createArray)
                    data.Fields.Add(new SharedData.DeclItem<AFieldDecl>(file, array));
                data.Fields.Add(new SharedData.DeclItem<AFieldDecl>(file, used));
                data.Fields.Add(new SharedData.DeclItem<AFieldDecl>(file, index));

                return new GlobalStructVars(createArray ? array : null, used, index, identiferArray, identiferCount);
            }
Exemplo n.º 12
0
 private List<List<PointerType>> GetAllVariables(AFieldDecl fieldDecl, PType type, bool wasPointer = false)
 {
     List<List<PointerType>> returner = new List<List<PointerType>>();
     if (type is APointerType)
     {
         returner.Add(type == fieldDecl.GetType() && !wasPointer
                          ? new List<PointerType>() { new FieldDeclPointer(fieldDecl) }
                          : new List<PointerType>() { new PointerPointer() });
         foreach (List<PointerType> pointer in GetAllVariables(fieldDecl, ((APointerType)type).GetType(), true))
         {
             pointer.Insert(0, type == fieldDecl.GetType() && !wasPointer
                                     ? new FieldDeclPointer(fieldDecl)
                                     : (PointerType)new PointerPointer());
             returner.Add(pointer);
         }
     }
     else if (type is ANamedType && Util.IsBulkCopy(type))
     {
         AStructDecl structDecl = data.StructTypeLinks[(ANamedType)type];
         foreach (AALocalDecl local in structDecl.GetLocals())
         {
             List<List<PointerType>> returnedStuff = GetAllVariables(local, local.GetType());
             foreach (List<PointerType> pointers in returnedStuff)
             {
                 pointers.Insert(0, wasPointer ? (PointerType)new PointerPointer() : new FieldDeclPointer(fieldDecl));
                 returner.Add(pointers);
             }
         }
     }
     return returner;
 }