Exemplo n.º 1
0
        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))
            {
                PLocalDecl decl = new ADeclLocalDecl(new AConstructorDecl(new APublicVisibilityModifier(),
                                                                          new TIdentifier(node.GetName().Text),
                                                                          new ArrayList(),
                                                                          new ArrayList(),
                                                                          new AABlock(new ArrayList(), new TRBrace("}"))));
                node.GetLocals().Add(decl);
                decl.Apply(this);
            }

            //Add deconstructor if not present
            if (node.GetLocals().OfType<ADeclLocalDecl>().Select(localDecl => localDecl.GetDecl()).OfType<ADeconstructorDecl>().ToList().Count == 0)
            {
                PLocalDecl decl =
                    new ADeclLocalDecl(new ADeconstructorDecl(new APublicVisibilityModifier(),
                                                              new TIdentifier(node.GetName().Text), new ArrayList(),
                                                              new AABlock(new ArrayList(), new TRBrace("}"))));
                node.GetLocals().Add(decl);
                decl.Apply(this);
            }

            if (node.GetVisibilityModifier() is AProtectedVisibilityModifier)
                errors.Add(new ErrorCollection.Error(node.GetName(), LocRM.GetString("ErrorText201")));
        }
Exemplo n.º 2
0
 public override void CaseAStructDecl(AStructDecl node)
 {
     InAStructDecl(node);
     {
         Object[] temp = new Object[node.GetLocals().Count];
         node.GetLocals().CopyTo(temp, 0);
         for (int i = temp.Length - 1; i >= 0; i--)
         {
             ((PLocalDecl)temp[i]).Apply(this);
         }
     }
     if (node.GetBase() != null)
     {
         node.GetBase().Apply(this);
     }
     {
         Object[] temp = new Object[node.GetGenericVars().Count];
         node.GetGenericVars().CopyTo(temp, 0);
         for (int i = temp.Length - 1; i >= 0; i--)
         {
             ((TIdentifier)temp[i]).Apply(this);
         }
     }
     if (node.GetName() != null)
     {
         node.GetName().Apply(this);
     }
     if (node.GetEndToken() != null)
     {
         node.GetEndToken().Apply(this);
     }
     if (node.GetIntDim() != null)
     {
         node.GetIntDim().Apply(this);
     }
     if (node.GetDimention() != null)
     {
         node.GetDimention().Apply(this);
     }
     if (node.GetClassToken() != null)
     {
         node.GetClassToken().Apply(this);
     }
     if (node.GetVisibilityModifier() != null)
     {
         node.GetVisibilityModifier().Apply(this);
     }
     OutAStructDecl(node);
 }
 public override void CaseAStructDecl(AStructDecl node)
 {
     if (node.GetLocals().Count == 0)
         node.Parent().RemoveChild(node);
     else
         base.CaseAStructDecl(node);
 }
Exemplo n.º 4
0
        public override void OutAEnumDecl(AEnumDecl node)
        {
            AStructDecl replacer = new AStructDecl(node.GetVisibilityModifier(), null, null, null, node.GetEndToken(),
                                                   node.GetName(), new ArrayList(), null, new ArrayList());

            TIdentifier typeIdentifier = new TIdentifier("byte");
            ASwitchStm switchStm = new ASwitchStm(new TSwitch("switch"),
                                                  new ALvalueExp(
                                                      new AAmbiguousNameLvalue(new AAName(new ArrayList() {new TIdentifier("enum")}))),
                                                  new ArrayList());
                ;
            AMethodDecl toStringMethod = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null,
                                                         new ANamedType(new TIdentifier("string"), null),
                                                         new TIdentifier("toString"),
                                                         new ArrayList()
                                                             {
                                                                 new AALocalDecl(new APublicVisibilityModifier(), null,
                                                                                 null, null, null,
                                                                                 new ANamedType(typeIdentifier, null),
                                                                                 new TIdentifier("enum"), null)
                                                             },
                                                         new AABlock(
                                                             new ArrayList()
                                                                 {
                                                                     switchStm,
                                                                     new AValueReturnStm(new TReturn("return"), new ANullExp())
                                                                 }, new TRBrace("}")));
            replacer.GetLocals().Add(new ADeclLocalDecl(toStringMethod));

            int intVal = 0;
            int min = int.MaxValue;
            int max = int.MinValue;
            List<TIdentifier> types = new List<TIdentifier>(){typeIdentifier};
            Dictionary<int, List<AALocalDecl>> usedValues = new Dictionary<int, List<AALocalDecl>>();
            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);
                typeIdentifier = new TIdentifier("byte", value.GetName().Line, value.GetName().Pos);
                types.Add(typeIdentifier);
                switchStm.GetCases().Add(
                    new ASwitchCaseStm(new ACaseSwitchCaseType(new TCase("case"), (PExp) intConst.Clone()),
                                       new AABlock(
                                           new ArrayList()
                                               {
                                                   new AValueReturnStm(new TReturn("return"),
                                                                       new AStringConstExp(
                                                                           new TStringLiteral("\"" +
                                                                                              value.GetName().Text +
                                                                                              "\"")))
                                               }, new TRBrace("}"))));
                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 (!usedValues.ContainsKey(intVal - 1))
                    usedValues[intVal - 1] = new List<AALocalDecl>();
                usedValues[intVal - 1].Add(localDecl);
            }
            if (min < 0 || max > 255)
                foreach (TIdentifier identifier in types)
                {
                    identifier.Text = "int";
                }
            node.ReplaceBy(replacer);
            foreach (KeyValuePair<int, List<AALocalDecl>> pair in usedValues)
            {
                if (pair.Value.Count <= 1)
                    continue;
                int value = pair.Key;
                List<ErrorCollection.Error> subErrors = new List<ErrorCollection.Error>();
                foreach (AALocalDecl decl in pair.Value)
                {
                    subErrors.Add(new ErrorCollection.Error(decl.GetName(), LocRM.GetString("ErrorText179")));
                }
                errors.Add(new ErrorCollection.Error(replacer.GetName(), LocRM.GetString("ErrorText180") + value + ".", false, subErrors.ToArray()));
            }
            replacer.Apply(this);
            data.Enums.Add(replacer, min < 0 || max > 255);
        }
 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 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 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 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);
        }
Exemplo n.º 9
0
            //+20
            public static GlobalStructVars CreateStructFields(Node node, AStructDecl structDecl, SharedData data)
            {
                if (structFields.ContainsKey(structDecl))
                    return structFields[structDecl];

                ANamedType structType = new ANamedType(new TIdentifier(structDecl.GetName().Text), null);
                data.StructTypeLinks[structType] = structDecl;

                structFields[structDecl] = CreatePointerFields(node, structDecl.GetIntDim(), structDecl.GetName().Text,
                                                               structType, data, structDecl.GetLocals().Count > 0);
                return structFields[structDecl];
                /*
                AASourceFile file = Util.GetAncestor<AASourceFile>(node);

                ANamedType structType = new ANamedType(new TIdentifier(structDecl.GetName().Text), null);
                data.StructTypeLinks[structType] = structDecl;
                AFieldDecl array = new AFieldDecl(new APublicVisibilityModifier(), null, null,
                                                  new AArrayTempType(new TLBracket("["), structType,
                                                                     new AIntConstExp(
                                                                         new TIntegerLiteral(structDecl.GetIntDim().Text)),
                                                                     new TIntegerLiteral(structDecl.GetIntDim().Text)),
                                                                     new TIdentifier(structDecl.GetName().Text + "_array", data.LineCounts[file] + 20, 0),
                                                                     null);
                int length = (int) Math.Ceiling(float.Parse(structDecl.GetIntDim().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(structDecl.GetName().Text + "_used", data.LineCounts[file] + 20, 0),
                                                                     null);
                AFieldDecl index = new AFieldDecl(new APublicVisibilityModifier(), null, null,  new ANamedType(new TIdentifier("int"), null),
                                                                     new TIdentifier(structDecl.GetName().Text + "_index", data.LineCounts[file] + 20, 0),
                                                                     null);
                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);

                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));

                structFields[structDecl] = new GlobalStructVars(array, used, index);
                return structFields[structDecl];*/
            }