Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        public override void OutADeconstructorDecl(ADeconstructorDecl node)
        {
            AStructDecl parentStruct = Util.GetAncestor <AStructDecl>(node);

            if (parentStruct != null)
            {
                if (data.StructDeconstructor.ContainsKey(parentStruct) && data.StructDeconstructor[parentStruct] != node)
                {
                    errors.Add(new ErrorCollection.Error(node.GetName(),
                                                         LocRM.GetString("ErrorText13") +
                                                         Util.GetTypeName(parentStruct) + ".", false,
                                                         new[]
                    {
                        new ErrorCollection.Error(
                            data.StructDeconstructor[parentStruct].GetName(),
                            LocRM.GetString("ErrorText15"))
                    }));
                }

                data.StructDeconstructor[parentStruct] = node;
                if (parentStruct.GetName().Text != node.GetName().Text)
                {
                    errors.Add(new ErrorCollection.Error(node.GetName(), currentSourceFile,
                                                         LocRM.GetString("ErrorText16")));
                }
            }
            base.OutADeconstructorDecl(node);
        }
Ejemplo n.º 3
0
        public void Join(LibraryData other)
        {
            int count = Methods.Count;

            foreach (AMethodDecl method in other.Methods)
            {
                bool add = true;
                for (int i = 0; i < count; i++)
                {
                    if (Util.GetMethodSignature(Methods[i]) != Util.GetMethodSignature(method))
                    {
                        add = false;
                        break;
                    }
                }
                if (add)
                {
                    Methods.Add(method);
                }
            }

            count = Fields.Count;
            foreach (AFieldDecl field in other.Fields)
            {
                bool add = true;
                for (int i = 0; i < count; i++)
                {
                    if (Fields[i].GetName().Text != field.GetName().Text)
                    {
                        add = false;
                        break;
                    }
                }
                if (add)
                {
                    Fields.Add(field);
                }
            }

            count = Structs.Count;
            for (int j = 0; j < other.Structs.Count; j++)
            {
                bool        add = true;
                AStructDecl str = other.Structs[j];
                for (int i = 0; i < count; i++)
                {
                    if (Structs[i].GetName().Text != str.GetName().Text)
                    {
                        add = false;
                        break;
                    }
                }
                if (add)
                {
                    Structs.Add(str);
                    StructMethods.Add(str, other.StructMethods[str]);
                    StructFields.Add(str, other.StructFields[str]);
                }
            }
        }
Ejemplo n.º 4
0
        public override void OutADeconstructorDecl(ADeconstructorDecl node)
        {
            AStructDecl parentStruct = Util.GetAncestor <AStructDecl>(node);

            if (parentStruct != null)
            {
                if (data.StructDeconstructor.ContainsKey(parentStruct) && data.StructDeconstructor[parentStruct] != node)
                {
                    errors.Add(new ErrorCollection.Error(node.GetName(),
                                                         "You can only define one deconstructor in a " +
                                                         Util.GetTypeName(parentStruct) + ".", false,
                                                         new[]
                    {
                        new ErrorCollection.Error(
                            data.StructDeconstructor[parentStruct].GetName(),
                            "Other deconstructor")
                    }));
                }

                data.StructDeconstructor[parentStruct] = node;
                if (parentStruct.GetName().Text != node.GetName().Text)
                {
                    errors.Add(new ErrorCollection.Error(node.GetName(), currentSourceFile,
                                                         "Deconstructors must have same name as the enclosing struct."));
                }
            }
            base.OutADeconstructorDecl(node);
        }
Ejemplo n.º 5
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());



            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;
        }
Ejemplo n.º 6
0
 //Check that there are no circular enhritance
 private void CheckEnheritanceList(AStructDecl str, List <AStructDecl> list)
 {
     if (list.Contains(str))
     {
         List <ErrorCollection.Error> subErrors = new List <ErrorCollection.Error>();
         for (int i = list.IndexOf(str); i < list.Count; i++)
         {
             subErrors.Add(new ErrorCollection.Error(list[i].GetName(), (list[i].GetClassToken() == null ? "Struct" : "class") + " in cycle."));
         }
         errors.Add(new ErrorCollection.Error(str.GetName(), "You can not make a cycle of enheritance.", false, subErrors.ToArray()));
         throw new ParserException(str.GetName(), "");
     }
     list.Add(str);
     if (str.GetBase() != null)
     {
         CheckEnheritanceList(Lookup((ANamedType)str.GetBase()), list);
     }
 }
Ejemplo n.º 7
0
 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");
 }
Ejemplo n.º 8
0
 public override void CaseAStructDecl(AStructDecl node)
 {
     if (Structs.Any(structDecl => structDecl.GetName().Text == node.GetName().Text))
     {
         return;
     }
     Structs.Add(node);
     StructMethods.Add(node, new List <AMethodDecl>());
     StructFields.Add(node, new List <AALocalDecl>());
     base.CaseAStructDecl(node);
     node.Parent().RemoveChild(node);
 }
Ejemplo n.º 9
0
        public StructDescription(AStructDecl structDecl)
        {
            Parser parser = new Parser(structDecl);

            Name   = parser.Name;
            IsEnum = Name.StartsWith("enum ");
            if (IsEnum)
            {
                Name = Name.Substring(5);
                foreach (VariableDescription field in parser.Fields)
                {
                    field.PlacementPrefix = "Enum Field";
                }
            }
            Fields = parser.Fields;

            Methods        = parser.Methods;
            Constructors   = parser.Constructors;
            Deconstructors = parser.Deconstructors;
            LineFrom       = structDecl.GetName().Line;
            LineTo         = structDecl.GetEndToken().Line;
            if (structDecl.GetBase() is AGenericType)
            {
                BaseRef = (ANamedType)((AGenericType)structDecl.GetBase()).GetBase();
            }
            else
            {
                BaseRef = (ANamedType)structDecl.GetBase();
            }
            structDecl.RemoveChild(BaseRef);
            foreach (TIdentifier identifier in structDecl.GetGenericVars())
            {
                GenericVars.Add(identifier.Text);
            }
            IsClass    = structDecl.GetClassToken() != null;
            Visibility = (PVisibilityModifier)structDecl.GetVisibilityModifier().Clone();
            Position   = TextPoint.FromCompilerCoords(structDecl.GetName());
        }
Ejemplo n.º 10
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))
     {
         node.GetLocals().Add(
             new ADeclLocalDecl(new AConstructorDecl(new APublicVisibilityModifier(),
                                                     new TIdentifier(node.GetName().Text), new ArrayList(),
                                                     new ArrayList(),
                                                     new AABlock(new ArrayList(), new TRBrace("}")))));
     }
 }
Ejemplo n.º 11
0
        public override void OutAConstructorDecl(AConstructorDecl node)
        {
            AStructDecl parentStruct = Util.GetAncestor <AStructDecl>(node);

            if (parentStruct != null)
            {
                data.StructConstructors[parentStruct].Add(node);
                if (parentStruct.GetName().Text != node.GetName().Text)
                {
                    errors.Add(new ErrorCollection.Error(node.GetName(), currentSourceFile,
                                                         "Constructors must have same name as the enclosing struct."));
                }
            }
            base.OutAConstructorDecl(node);
        }
Ejemplo n.º 12
0
        public override void OutAConstructorDecl(AConstructorDecl node)
        {
            AStructDecl parentStruct = Util.GetAncestor <AStructDecl>(node);

            if (parentStruct != null)
            {
                data.StructConstructors[parentStruct].Add(node);
                if (parentStruct.GetName().Text != node.GetName().Text)
                {
                    errors.Add(new ErrorCollection.Error(node.GetName(), currentSourceFile,
                                                         LocRM.GetString("ErrorText12")));
                }
            }
            base.OutAConstructorDecl(node);
        }
Ejemplo n.º 13
0
        public override void CaseAAProgram(AAProgram node)
        {
            /*decls.AddRange(finalTrans.data.Structs.Select(str => str.Decl));
             * decls.AddRange(finalTrans.data.Methods.Select(str => str.Decl));
             * decls.AddRange(finalTrans.data.Fields.Select(str => str.Decl));
             * if (finalTrans.data.Locals.Count > 0)
             *  decls.AddRange(finalTrans.data.Locals.Select(str => str.Value).Aggregate(Aggregate));
             * if (finalTrans.data.Structs.Count > 0)
             *  decls.AddRange(finalTrans.data.StructFields.Values.Aggregate(Aggregate));*/
            base.CaseAAProgram(node);
            Random rand = new Random();

            while (decls.Count > 0)
            {
                int  i = rand.Next(decls.Count);
                Node n = decls[i];
                decls.RemoveAt(i);

                if (n is AFieldDecl)
                {
                    AFieldDecl an     = (AFieldDecl)n;
                    an.GetName().Text = nextName;
                }
                else if (n is AMethodDecl)
                {
                    AMethodDecl an = (AMethodDecl)n;
                    if (finalTrans.mainEntry == an || (an.GetTrigger() != null && finalTrans.data.HasUnknownTrigger))
                    {
                        continue;
                    }
                    an.GetName().Text = nextName;
                }
                else if (n is AALocalDecl)
                {
                    AALocalDecl an    = (AALocalDecl)n;
                    an.GetName().Text = nextName;
                }
                else if (n is AStructDecl)
                {
                    AStructDecl an    = (AStructDecl)n;
                    an.GetName().Text = nextName;
                }
                NextName();
            }
        }
Ejemplo n.º 14
0
        private static void GetMatchingTypes(ANamedType node, List <string> names, List <ATypedefDecl> typeDefs, List <AStructDecl> structs, List <AMethodDecl> delegates, List <ANamespaceDecl> namespaces, List <TIdentifier> generics)
        {
            List <IList>  decls             = new List <IList>();
            List <string> currentNamespace  = Util.GetFullNamespace(node);
            AASourceFile  currentSourceFile = Util.GetAncestor <AASourceFile>(node);

            if (names.Count == 1)
            {
                string name = names[0];
                //Check generic vars
                AStructDecl currentStruct = Util.GetAncestor <AStructDecl>(node);
                if (currentStruct != null)
                {
                    foreach (TIdentifier genericVar in currentStruct.GetGenericVars())
                    {
                        if (genericVar.Text == name)
                        {
                            generics.Add(genericVar);
                        }
                    }
                }
                //Get all type decls and namespaces matching this name, visible from this location
                List <IList> visibleDecls = Util.GetVisibleDecls(node, ((AAName)node.GetName()).GetIdentifier().Count == 1);
                foreach (IList declList in visibleDecls)
                {
                    bool sameFile = false;
                    if (declList.Count > 0)
                    {
                        sameFile = currentSourceFile == Util.GetAncestor <AASourceFile>((PDecl)declList[0]);
                    }
                    foreach (PDecl decl in declList)
                    {
                        bool sameNS = Util.NamespacesEquals(currentNamespace, Util.GetFullNamespace(decl));
                        if (decl is ANamespaceDecl)
                        {
                            ANamespaceDecl aDecl = (ANamespaceDecl)decl;
                            if (aDecl.GetName().Text == name)
                            {
                                namespaces.Add(aDecl);
                            }
                            continue;
                        }
                        if (decl is ATypedefDecl)
                        {
                            if (Util.IsAncestor(node, decl))
                            {
                                continue;
                            }
                            ATypedefDecl aDecl = (ATypedefDecl)decl;
                            if (aDecl.GetStatic() != null && !sameFile ||
                                aDecl.GetVisibilityModifier() is APrivateVisibilityModifier && !sameNS)
                            {
                                continue;
                            }
                            ANamedType namedType = (ANamedType)aDecl.GetName();
                            AAName     aName     = (AAName)namedType.GetName();
                            string     n         = ((TIdentifier)aName.GetIdentifier()[0]).Text;
                            if (n == name)
                            {
                                typeDefs.Add(aDecl);
                            }
                            continue;
                        }
                        if (decl is AStructDecl)
                        {
                            AStructDecl aDecl = (AStructDecl)decl;
                            if (!sameNS && aDecl.GetVisibilityModifier() is APrivateVisibilityModifier)
                            {
                                continue;
                            }
                            if (aDecl.GetName().Text == name)
                            {
                                structs.Add(aDecl);
                            }
                            continue;
                        }
                        if (decl is AMethodDecl)
                        {
                            AMethodDecl aDecl = (AMethodDecl)decl;
                            if (!sameNS && aDecl.GetVisibilityModifier() is APrivateVisibilityModifier ||
                                !sameFile && aDecl.GetStatic() != null)
                            {
                                continue;
                            }
                            if (aDecl.GetDelegate() != null && aDecl.GetName().Text == name)
                            {
                                delegates.Add(aDecl);
                            }
                            continue;
                        }
                    }
                }
            }
            else
            {
                string name = names[names.Count - 1];
                List <ANamespaceDecl> baseNamespaces = new List <ANamespaceDecl>();
                List <string>         baseNames      = new List <string>();
                baseNames.AddRange(names);
                baseNames.RemoveAt(baseNames.Count - 1);
                GetMatchingTypes(node, baseNames, new List <ATypedefDecl>(), new List <AStructDecl>(), new List <AMethodDecl>(), baseNamespaces, generics);
                foreach (ANamespaceDecl ns in baseNamespaces)
                {
                    bool sameFile = currentSourceFile == Util.GetAncestor <AASourceFile>(ns);
                    foreach (PDecl decl in ns.GetDecl())
                    {
                        bool sameNS = Util.NamespacesEquals(currentNamespace, Util.GetFullNamespace(decl));
                        if (decl is ANamespaceDecl)
                        {
                            ANamespaceDecl aDecl = (ANamespaceDecl)decl;
                            if (aDecl.GetName().Text == name)
                            {
                                namespaces.Add(aDecl);
                            }
                            continue;
                        }
                        if (decl is ATypedefDecl)
                        {
                            ATypedefDecl aDecl     = (ATypedefDecl)decl;
                            ANamedType   namedType = (ANamedType)aDecl.GetName();
                            AAName       aName     = (AAName)namedType.GetName();
                            string       n         = ((TIdentifier)aName.GetIdentifier()[0]).Text;
                            if (n == name)
                            {
                                typeDefs.Add(aDecl);
                            }
                            continue;
                        }
                        if (decl is AStructDecl)
                        {
                            AStructDecl aDecl = (AStructDecl)decl;
                            if (!sameNS && aDecl.GetVisibilityModifier() is APrivateVisibilityModifier)
                            {
                                continue;
                            }
                            if (aDecl.GetName().Text == name)
                            {
                                structs.Add(aDecl);
                            }
                            continue;
                        }
                        if (decl is AMethodDecl)
                        {
                            AMethodDecl aDecl = (AMethodDecl)decl;
                            if (!sameNS && aDecl.GetVisibilityModifier() is APrivateVisibilityModifier ||
                                !sameFile && aDecl.GetStatic() != null)
                            {
                                continue;
                            }
                            if (aDecl.GetDelegate() != null && aDecl.GetName().Text == name)
                            {
                                delegates.Add(aDecl);
                            }
                            continue;
                        }
                    }
                }
            }
        }
Ejemplo n.º 15
0
        public override void OutAMethodDecl(AMethodDecl node)
        {
            //Check locals against everything else
            foreach (AALocalDecl local in locals)
            {
                if (local.GetName().Text.StartsWith("_"))
                {
                    local.GetName().Text = "u" + local.GetName().Text;
                }
                bool restart = true;
                while (restart)
                {
                    restart = false;
                    //Other locals
                    foreach (AALocalDecl local2 in locals)
                    {
                        if (local != local2 && local.GetName().Text == local2.GetName().Text)
                        {
                            local.GetName().Text += "1";
                            local.GetName().Text += "2";
                            restart = true;
                            break;
                        }
                    }
                    if (restart)
                    {
                        continue;
                    }


                    //Methods
                    foreach (SharedData.DeclItem <AMethodDecl> declItem in finalTrans.data.Methods)
                    {
                        AMethodDecl decl = declItem.Decl;
                        if (decl.GetName().Text == local.GetName().Text)
                        {
                            decl.GetName().Text  += "M";
                            local.GetName().Text += "L";
                            restart = true;
                            break;
                        }
                    }
                    if (restart)
                    {
                        continue;
                    }

                    //Fields
                    foreach (SharedData.DeclItem <AFieldDecl> declItem in finalTrans.data.Fields)
                    {
                        AFieldDecl decl = declItem.Decl;
                        if (decl.GetName().Text == local.GetName().Text)
                        {
                            decl.GetName().Text  += "F";
                            local.GetName().Text += "L";
                            restart = true;
                            break;
                        }
                    }
                    if (restart)
                    {
                        continue;
                    }

                    //Structs
                    foreach (SharedData.DeclItem <AStructDecl> declItem in finalTrans.data.Structs)
                    {
                        AStructDecl decl = declItem.Decl;
                        if (decl.GetName().Text == local.GetName().Text)
                        {
                            decl.GetName().Text  += "S";
                            local.GetName().Text += "L";
                            restart = true;
                            break;
                        }
                    }
                    if (restart)
                    {
                        continue;
                    }
                }
            }
        }
        public override void CaseAConstructorDecl(AConstructorDecl node)
        {
            AStructDecl     str        = Util.GetAncestor <AStructDecl>(node);
            AEnrichmentDecl enrichment = Util.GetAncestor <AEnrichmentDecl>(node);
            AMethodDecl     replacer   = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null, new AVoidType(new TVoid("void")),
                                                         node.GetName(), new ArrayList(), node.GetBlock());

            replacer.GetName().Text += "_Constructor";
            while (node.GetFormals().Count > 0)
            {
                replacer.GetFormals().Add(node.GetFormals()[0]);
            }

            //Move the method outside the struct
            AASourceFile file = Util.GetAncestor <AASourceFile>(node);

            if (str != null)
            {
                str.RemoveChild(node.Parent());
            }
            else
            {
                enrichment.RemoveChild(node);
            }
            int i = file.GetDecl().IndexOf(str ?? (PDecl)enrichment);

            file.GetDecl().Insert(i /* + 1*/, replacer);
            //Add the struct as a parameter
            PType type;

            if (str != null)
            {
                ANamedType structType = new ANamedType(new TIdentifier(str.GetName().Text), null);
                finalTrans.data.StructTypeLinks[structType] = str;
                type = structType;
            }
            else
            {
                type = Util.MakeClone(enrichment.GetType(), finalTrans.data);
            }
            finalTrans.data.ConstructorMap[node] = replacer;
            structFormal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new APointerType(new TStar("*"), type), new TIdentifier("currentStruct", replacer.GetName().Line, replacer.GetName().Pos), null);
            replacer.GetFormals().Add(structFormal);
            finalTrans.data.Methods.Add(new SharedData.DeclItem <AMethodDecl>(file, replacer));

            //Add return stm
            replacer.SetReturnType(new APointerType(new TStar("*"), Util.MakeClone(type, data)));
            replacer.Apply(new TransformConstructorReturns(structFormal, data));

            //Insert call to base constructor););
            if (finalTrans.data.ConstructorBaseLinks.ContainsKey(node))
            {
                AMethodDecl      baseConstructor = finalTrans.data.ConstructorMap[finalTrans.data.ConstructorBaseLinks[node]];
                ASimpleInvokeExp invoke          = new ASimpleInvokeExp(new TIdentifier(baseConstructor.GetName().Text), new ArrayList());
                while (node.GetBaseArgs().Count > 0)
                {
                    invoke.GetArgs().Add(node.GetBaseArgs()[0]);
                }
                AThisLvalue thisLvalue1 = new AThisLvalue(new TThis("this"));
                ALvalueExp  thisExp1    = new ALvalueExp(thisLvalue1);
                invoke.GetArgs().Add(thisExp1);

                AThisLvalue thisLvalue2 = new AThisLvalue(new TThis("this"));

                AAssignmentExp assignExp = new AAssignmentExp(new TAssign("="), thisLvalue2, invoke);

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

                finalTrans.data.LvalueTypes[thisLvalue1]         =
                    finalTrans.data.LvalueTypes[thisLvalue2]     =
                        finalTrans.data.ExpTypes[thisExp1]       =
                            finalTrans.data.ExpTypes[assignExp]  =
                                finalTrans.data.ExpTypes[invoke] = new APointerType(new TStar("*"), structType);

                //finalTrans.data.ExpTypes[invoke] = new AVoidType(new TVoid("void"));
                finalTrans.data.SimpleMethodLinks[invoke] = baseConstructor;

                ((AABlock)replacer.GetBlock()).GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), assignExp));

                //Inline if base and current are two different kinds of pointer types (int/string)
                AStructDecl      baseStruct = null;
                AConstructorDecl baseC      = finalTrans.data.ConstructorBaseLinks[node];
                foreach (KeyValuePair <AStructDecl, List <AConstructorDecl> > pair in finalTrans.data.StructConstructors)
                {
                    bool found = false;
                    foreach (AConstructorDecl decl in pair.Value)
                    {
                        if (baseC == decl)
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found)
                    {
                        baseStruct = pair.Key;
                        break;
                    }
                }
                if ((str.GetIntDim() == null) != (baseStruct.GetIntDim() == null))
                {
                    //For the inilining, change the type to the type of the caller
                    AALocalDecl lastFormal = baseConstructor.GetFormals().OfType <AALocalDecl>().Last();
                    lastFormal.SetRef(new TRef("ref"));
                    APointerType oldType = (APointerType)lastFormal.GetType();

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

                    APointerType newType = new APointerType(new TStar("*"), structType);
                    lastFormal.SetType(newType);

                    foreach (
                        ALocalLvalue lvalue in
                        data.LocalLinks.Where(pair => pair.Value == lastFormal).Select(pair => pair.Key))
                    {
                        data.LvalueTypes[lvalue] = newType;
                        if (lvalue.Parent() is ALvalueExp)
                        {
                            data.ExpTypes[(PExp)lvalue.Parent()] = newType;
                            if (lvalue.Parent().Parent() is APointerLvalue)
                            {
                                data.LvalueTypes[(PLvalue)lvalue.Parent().Parent()] = newType.GetType();
                            }
                        }
                    }

                    FixInlineMethods.Inline(invoke, finalTrans);
                    lastFormal.SetRef(null);
                    foreach (
                        ALocalLvalue lvalue in
                        data.LocalLinks.Where(pair => pair.Value == lastFormal).Select(pair => pair.Key))
                    {
                        data.LvalueTypes[lvalue] = oldType;
                        if (lvalue.Parent() is ALvalueExp)
                        {
                            data.ExpTypes[(PExp)lvalue.Parent()] = oldType;
                            if (lvalue.Parent().Parent() is APointerLvalue)
                            {
                                data.LvalueTypes[(PLvalue)lvalue.Parent().Parent()] = oldType.GetType();
                            }
                        }
                    }

                    lastFormal.SetType(oldType);
                }

                //Inline it instead, Since the pointer implementations might not be the same (int vs string)

                /*AMethodDecl baseConstructor = finalTrans.data.ConstructorMap[finalTrans.data.ConstructorBaseLinks[node]];
                 *
                 * AABlock localsBlock = new AABlock(new ArrayList(), new TRBrace("}"));
                 * ABlockStm cloneBlock = new ABlockStm(new TLBrace("{"), (PBlock) baseConstructor.GetBlock().Clone());
                 * Dictionary<AALocalDecl, PLvalue> localMap = new Dictionary<AALocalDecl, PLvalue>();
                 * for (int argNr = 0; argNr < baseConstructor.GetFormals().Count; argNr++)
                 * {
                 *  AALocalDecl formal = (AALocalDecl) baseConstructor.GetFormals()[i];
                 *  PExp arg;
                 *  if (i < baseConstructor.GetFormals().Count - 1)
                 *      arg = (PExp)node.GetBaseArgs()[i];
                 *  else
                 *  {
                 *      AThisLvalue thisLvalue = new AThisLvalue(new TThis("this"));
                 *      ALvalueExp thisExp = new ALvalueExp(thisLvalue);
                 *
                 *      ANamedType structType = new ANamedType(new TIdentifier(str.GetName().Text), null);
                 *      finalTrans.data.StructTypeLinks[structType] = str;
                 *
                 *      finalTrans.data.LvalueTypes[thisLvalue] =
                 *          finalTrans.data.ExpTypes[thisExp] = new APointerType(new TStar("*"), structType);
                 *
                 *      arg = thisExp;
                 *  }
                 *
                 *  if (formal.GetRef() != null || formal.GetOut() != null)
                 *  {
                 *      //Use same variable
                 *      localMap[formal] = ((ALvalueExp) arg).GetLvalue();
                 *  }
                 *  else
                 *  {
                 *      //Make a new variable
                 *      AALocalDecl newLocal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                 *                                             Util.MakeClone(formal.GetType(), finalTrans.data),
                 *                                             new TIdentifier(formal.GetName().Text),
                 *                                             Util.MakeClone(arg, data));
                 *
                 *      ALocalLvalue newLocalRef = new ALocalLvalue(new TIdentifier(newLocal.GetName().Text));
                 *
                 *      localMap[formal] = newLocalRef;
                 *      data.LvalueTypes[newLocalRef] = newLocal.GetType();
                 *      data.LocalLinks[newLocalRef] = newLocal;
                 *
                 *      localsBlock.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), newLocal));
                 *  }
                 *
                 * }
                 *
                 * CloneMethod cloner = new CloneMethod(finalTrans.data, localMap, cloneBlock);
                 * baseConstructor.GetBlock().Apply(cloner);
                 *
                 * ((AABlock)cloneBlock.GetBlock()).GetStatements().Insert(0, new ABlockStm(new TLBrace("{"), localsBlock));
                 * ((AABlock)node.GetBlock()).GetStatements().Insert(0, cloneBlock);*/
            }

            //Fix refferences to other struct stuff);
            base.CaseAMethodDecl(replacer);

            //Add functionality to refference the current struct in a constructor
            //Want to do it as a pointer type, since the constructer can only be called for pointer types
        }
Ejemplo n.º 17
0
        public static void Parse(AAProgram ast, FinalTransformations finalTrans)
        {
            bool restart = true;

            while (restart)
            {
                restart = false;
                //Fix locals
                ast.Apply(new MakeUniqueNames(finalTrans));

                //Fix methods
                foreach (SharedData.DeclItem <AMethodDecl> declItem1 in finalTrans.data.Methods)
                {
                    AMethodDecl decl1 = declItem1.Decl;
                    if (decl1.GetName().Text.StartsWith("_"))
                    {
                        decl1.GetName().Text = "u" + decl1.GetName().Text;
                        restart = true;
                        break;
                    }
                    //Other methods
                    foreach (SharedData.DeclItem <AMethodDecl> declItem2 in finalTrans.data.Methods)
                    {
                        AMethodDecl decl2 = declItem2.Decl;
                        if (decl1 != decl2 && decl1.GetName().Text == decl2.GetName().Text)
                        {
                            decl1.GetName().Text += "1";
                            decl2.GetName().Text += "2";
                            restart = true;
                            break;
                        }
                    }
                    if (restart)
                    {
                        break;
                    }

                    //Fields
                    foreach (SharedData.DeclItem <AFieldDecl> declItem2 in finalTrans.data.Fields)
                    {
                        AFieldDecl decl2 = declItem2.Decl;
                        if (decl1.GetName().Text == decl2.GetName().Text)
                        {
                            decl1.GetName().Text += "M";
                            decl2.GetName().Text += "F";
                            restart = true;
                            break;
                        }
                    }
                    if (restart)
                    {
                        break;
                    }


                    //structs
                    foreach (SharedData.DeclItem <AStructDecl> declItem2 in finalTrans.data.Structs)
                    {
                        AStructDecl decl2 = declItem2.Decl;
                        if (decl1.GetName().Text == decl2.GetName().Text)
                        {
                            decl1.GetName().Text += "M";
                            decl2.GetName().Text += "S";
                            restart = true;
                            break;
                        }
                    }
                    if (restart)
                    {
                        break;
                    }
                }
                if (restart)
                {
                    continue;
                }


                //Fix fields
                foreach (SharedData.DeclItem <AFieldDecl> declItem1 in finalTrans.data.Fields)
                {
                    AFieldDecl decl1 = declItem1.Decl;
                    if (decl1.GetName().Text.StartsWith("_"))
                    {
                        decl1.GetName().Text = "u" + decl1.GetName().Text;
                        restart = true;
                        break;
                    }
                    //Other fields
                    foreach (SharedData.DeclItem <AFieldDecl> declItem2 in finalTrans.data.Fields)
                    {
                        AFieldDecl decl2 = declItem2.Decl;
                        if (decl1 != decl2 && decl1.GetName().Text == decl2.GetName().Text)
                        {
                            decl1.GetName().Text += "1";
                            decl2.GetName().Text += "2";
                            restart = true;
                            break;
                        }
                    }
                    if (restart)
                    {
                        break;
                    }


                    //structs
                    foreach (SharedData.DeclItem <AStructDecl> declItem2 in finalTrans.data.Structs)
                    {
                        AStructDecl decl2 = declItem2.Decl;
                        if (decl1.GetName().Text == decl2.GetName().Text)
                        {
                            decl1.GetName().Text += "F";
                            decl2.GetName().Text += "S";
                            restart = true;
                            break;
                        }
                    }
                    if (restart)
                    {
                        break;
                    }
                }
                if (restart)
                {
                    continue;
                }


                //Fix structs
                foreach (SharedData.DeclItem <AStructDecl> declItem1 in finalTrans.data.Structs)
                {
                    AStructDecl decl1 = declItem1.Decl;
                    if (decl1.GetName().Text.StartsWith("_"))
                    {
                        decl1.GetName().Text = "u" + decl1.GetName().Text;
                        restart = true;
                        break;
                    }
                    //Other fields
                    foreach (SharedData.DeclItem <AStructDecl> declItem2 in finalTrans.data.Structs)
                    {
                        AStructDecl decl2 = declItem2.Decl;
                        if (decl1 != decl2 && decl1.GetName().Text == decl2.GetName().Text)
                        {
                            decl1.GetName().Text += "1";
                            decl2.GetName().Text += "2";
                            restart = true;
                            break;
                        }
                    }
                    if (restart)
                    {
                        break;
                    }
                }
                if (restart)
                {
                    continue;
                }
            }
        }
Ejemplo n.º 18
0
 public override void OutANamedType(ANamedType node)
 {
     //If using a generic type, you must us it as a generic
     if (data.StructTypeLinks.ContainsKey(node))
     {
         AStructDecl str = data.StructTypeLinks[node];
         if (str.GetGenericVars().Count > 0)
         {
             if (!(node.Parent() is AGenericType))
             {
                 errors.Add(new ErrorCollection.Error(node.GetToken(),
                                                      "Target struct is a generic type. You must specify types for the generics.",
                                                      false,
                                                      new ErrorCollection.Error(str.GetName(),
                                                                                "Matching " +
                                                                                Util.GetTypeName(str))));
             }
         }
     }
     base.OutANamedType(node);
 }
Ejemplo n.º 19
0
 public override void CaseAStructDecl(AStructDecl node)
 {
     node.GetName().Text = currentNamespace + node.GetName().Text;
 }
Ejemplo n.º 20
0
        public override void OutAAProgram(AAProgram node)
        {
            foreach (var pair in Refferences)
            {
                if (structsWithGenerics.Contains(pair.Key) && pair.Value.Count > 0)
                {
                    needAnotherPass = true;
                }
            }
            foreach (var pair in Refferences)
            {
                AStructDecl str = pair.Key;
                if (!copies.ContainsKey(str))
                {
                    copies[str] = new List <List <PType> >();
                }
                IList declList;
                Node  parent = str.Parent();
                if (parent is AASourceFile)
                {
                    declList = ((AASourceFile)parent).GetDecl();
                }
                else
                {
                    declList = ((ANamespaceDecl)parent).GetDecl();
                }
                //AASourceFile pFile = (AASourceFile) str.Parent();
                foreach (AGenericType refference in pair.Value)
                {
                    AStructDecl clone   = null;
                    bool        addList = true;
                    foreach (List <PType> list in copies[str])
                    {
                        bool listEqual = true;
                        for (int i = 0; i < list.Count; i++)
                        {
                            if (!Util.TypesEqual(list[i], (PType)refference.GetGenericTypes()[i], data))
                            {
                                listEqual = false;
                                break;
                            }
                        }
                        if (listEqual)
                        {
                            addList = false;
                            clone   = clones[list];
                            break;
                        }
                    }
                    if (addList)
                    {
                        List <PType> list = new List <PType>();
                        foreach (PType type in refference.GetGenericTypes())
                        {
                            list.Add(type);
                        }
                        copies[str].Add(list);

                        clone = (AStructDecl)str.Clone();
                        declList.Insert(declList.IndexOf(str), clone);


                        clone.Apply(new EnviromentBuilding(errors, data));
                        clone.Apply(new EnviromentChecking(errors, data, false));
                        clone.Apply(new LinkNamedTypes(errors, data));

                        /*
                         * data.Structs.Add(new SharedData.DeclItem<AStructDecl>(pFile, clone));
                         * data.StructFields[clone] = new List<AALocalDecl>();
                         * data.StructMethods[clone] = new List<AMethodDecl>();
                         * data.StructProperties[clone] = new List<APropertyDecl>();
                         * data.StructConstructors[clone] = new List<AConstructorDecl>();
                         * foreach (PLocalDecl localDecl in clone.GetLocals())
                         * {
                         *  if (localDecl is AALocalDecl)
                         *  {
                         *      data.StructFields[clone].Add((AALocalDecl)localDecl);
                         *  }
                         *  else
                         *  {
                         *      PDecl decl = ((ADeclLocalDecl) localDecl).GetDecl();
                         *      if (decl is AMethodDecl)
                         *      {
                         *          data.StructMethods[clone].Add((AMethodDecl) decl);
                         *      }
                         *      else if (decl is APropertyDecl)
                         *      {
                         *          data.StructProperties[clone].Add((APropertyDecl)decl);
                         *      }
                         *      else
                         *      {
                         *          data.StructConstructors[clone].Add((AConstructorDecl) decl);
                         *      }
                         *  }
                         * }*/

                        clones[list] = clone;


                        clone.setGenericVars(new ArrayList());

                        FixGenericLinks.Parse(str, clone, list, data);
                        clone.GetName().Text = Util.TypeToIdentifierString(refference);
                    }
                    //Change refference to clone
                    ANamedType    baseRef = (ANamedType)refference.GetBase();
                    List <string> cloneNs = Util.GetFullNamespace(clone);
                    cloneNs.Add(clone.GetName().Text);
                    AAName aName = (AAName)baseRef.GetName();
                    aName.GetIdentifier().Clear();
                    foreach (var n in cloneNs)
                    {
                        aName.GetIdentifier().Add(new TIdentifier(n));
                    }
                    data.StructTypeLinks[baseRef] = clone;
                    refference.ReplaceBy(baseRef);
                }

                if (!needAnotherPass)
                {
                    parent.RemoveChild(str);
                }
            }
            if (needAnotherPass)
            {
                Refferences.Clear();
                structsWithGenerics.Clear();
                needAnotherPass = false;
                CaseAAProgram(node);
            }
        }
Ejemplo n.º 21
0
        public override void OutAGenericType(AGenericType node)
        {
            if (!(node.GetBase() is ANamedType))
            {
                errors.Add(new ErrorCollection.Error(node.GetToken(), LocRM.GetString("ErrorText44")));
                return;
            }
            ANamedType Base = (ANamedType)node.GetBase();

            if (!data.StructTypeLinks.ContainsKey(Base))
            {
                errors.Add(new ErrorCollection.Error(node.GetToken(), LocRM.GetString("ErrorText44")));
                return;
            }
            AStructDecl str = data.StructTypeLinks[Base];

            if (str.GetGenericVars().Count != node.GetGenericTypes().Count)
            {
                errors.Add(new ErrorCollection.Error(node.GetToken(), LocRM.GetString("ErrorText45"),
                                                     false, new ErrorCollection.Error(str.GetName(), LocRM.GetString("ErrorText46") + Util.GetTypeName(str))));
                return;
            }
            LookForGenericVar finder = new LookForGenericVar();

            foreach (PType genericType in node.GetGenericTypes())
            {
                genericType.Apply(finder);
                if (finder.ContainsGenericVar || finder.ContainsNestedGenerics)
                {
                    //if (finder.ContainsGenericVar)
                    structsWithGenerics.Add(Util.GetAncestor <AStructDecl>(node));
                    if (finder.ContainsNestedGenerics)
                    {
                        if (!Util.HasAncestor <AStructDecl>(node) || Util.GetAncestor <AStructDecl>(node).GetGenericVars().Count == 0)
                        {
                            needAnotherPass = true;
                        }
                    }
                    return;
                }
            }
            if (!Refferences.ContainsKey(str))
            {
                Refferences[str] = new List <AGenericType>();
            }
            Refferences[str].Add(node);
            base.OutAGenericType(node);
        }
Ejemplo n.º 22
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);
        }
Ejemplo n.º 23
0
            public override void CaseAStructDecl(AStructDecl node)
            {
                Name = node.GetName().Text;

                base.CaseAStructDecl(node);
            }
Ejemplo n.º 24
0
 public override void OutANamedType(ANamedType node)
 {
     //If using a generic type, you must us it as a generic
     if (data.StructTypeLinks.ContainsKey(node))
     {
         AStructDecl str = data.StructTypeLinks[node];
         if (str.GetGenericVars().Count > 0)
         {
             if (!(node.Parent() is AGenericType))
             {
                 errors.Add(new ErrorCollection.Error(node.GetToken(),
                                                      LocRM.GetString("ErrorText46"),
                                                      false,
                                                      new ErrorCollection.Error(str.GetName(),
                                                                                LocRM.GetString("ErrorText48") +
                                                                                Util.GetTypeName(str))));
             }
         }
     }
     base.OutANamedType(node);
 }
        /*private class IsThisOnLeftSide : DepthFirstAdapter
         * {
         *  private PType type;
         *  private SharedData data;
         *  public bool IsAssignedTo;
         *  private List<AMethodDecl> investigatedMethods = new List<AMethodDecl>();
         *
         *  public IsThisOnLeftSide(PType type, SharedData data)
         *  {
         *      this.type = type;
         *      this.data = data;
         *  }
         *
         *  //Check assignments, method invocations and nonstatic method invocations.
         *
         *  public override void CaseAMethodDecl(AMethodDecl node)
         *  {
         *      investigatedMethods.Add(node);
         *  }
         *
         *  public override void CaseAThisLvalue(AThisLvalue node)
         *  {
         *      if (IsAssignedTo)
         *          return;
         *
         *      Node iParent = GetClosestNodeOfType(node, typeof (AAssignmentExp),
         *                                          typeof (ASimpleInvokeExp),
         *                                          typeof (ANonstaticInvokeExp),
         *                                          typeof (AAsyncInvokeStm),
         *                                          typeof (ASyncInvokeExp),
         *                                          typeof(AArrayLvalue),
         *                                          typeof(APointerLvalue),
         *                                          typeof(APropertyLvalue),
         *                                          typeof(AStructLvalue));
         *      if (iParent == null)
         *          return;
         *
         *      if (iParent is AAssignmentExp)
         *      {
         *          AAssignmentExp aParent = (AAssignmentExp) iParent;
         *          if (Util.IsAncestor(node, aParent.GetLvalue()))
         *          {
         *              IsAssignedTo = true;
         *          }
         *          return;
         *      }
         *      if (iParent is ASimpleInvokeExp)
         *      {
         *          ASimpleInvokeExp aParent = (ASimpleInvokeExp) iParent;
         *          AMethodDecl method = data.SimpleMethodLinks[aParent];
         *          if (investigatedMethods.Contains(method))
         *              return;
         *
         *          if (Util.IsAncestor(node, aParent.GetLvalue()))
         *          {
         *              IsAssignedTo = true;
         *          }
         *          return;
         *      }
         *  }
         *
         *  private Node GetClosestNodeOfType(Node node, params Type[] types)
         *  {
         *      if (node == null)
         *          return null;
         *      if (types.Contains(node.GetType()))
         *          return node;
         *      return GetClosestNodeOfType(node.Parent(), types);
         *  }
         * }*/

        public override void CaseADeconstructorDecl(ADeconstructorDecl node)
        {
            AStructDecl     str        = Util.GetAncestor <AStructDecl>(node);
            AEnrichmentDecl enrichment = Util.GetAncestor <AEnrichmentDecl>(node);
            AMethodDecl     replacer   = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null, new AVoidType(new TVoid("void")),
                                                         node.GetName(), new ArrayList(), node.GetBlock());

            replacer.GetName().Text += "_Deconstructor";

            //Move the method outside the struct
            AASourceFile file = Util.GetAncestor <AASourceFile>(node);

            if (str != null)
            {
                str.RemoveChild(node.Parent());
            }

            /*else
             *  enrichment.RemoveChild(node);*/
            int i = file.GetDecl().IndexOf(str ?? (PDecl)enrichment);

            file.GetDecl().Insert(i /* + 1*/, replacer);
            //Add the struct as a parameter
            PType type;

            if (str != null)
            {
                ANamedType structType = new ANamedType(new TIdentifier(str.GetName().Text), null);
                finalTrans.data.StructTypeLinks[structType] = str;
                type = structType;
            }
            else
            {
                type = Util.MakeClone(enrichment.GetType(), finalTrans.data);
            }
            finalTrans.data.DeconstructorMap[node] = replacer;
            AALocalDecl structFormal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new APointerType(new TStar("*"), type), new TIdentifier("currentStruct", replacer.GetName().Line, replacer.GetName().Pos), null);

            replacer.GetFormals().Add(structFormal);
            finalTrans.data.Methods.Add(new SharedData.DeclItem <AMethodDecl>(file, replacer));

            //Call base deconstructor before each return
            if (str != null && str.GetBase() != null)
            {
                AStructDecl baseStruct = data.StructTypeLinks[(ANamedType)str.GetBase()];
                if (data.StructDeconstructor.ContainsKey(baseStruct))
                {
                    baseStruct.Apply(this);
                    replacer.Apply(new CallDeconstructors(baseStruct, structFormal, data));

                    /*AMethodDecl baseDeconstructor = data.DeconstructorMap[data.StructDeconstructor[baseStruct]];
                     *
                     *
                     * ALocalLvalue structFormalRef = new ALocalLvalue(new TIdentifier("currentStruct"));
                     * ALvalueExp structFormalRefExp = new ALvalueExp(structFormalRef);
                     * ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("baseDeconstructor"),
                     *                                             new ArrayList() {structFormalRefExp});
                     * AABlock block = (AABlock) replacer.GetBlock();
                     * block.GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), invoke));
                     *
                     * data.LocalLinks[structFormalRef] = structFormal;
                     * data.SimpleMethodLinks[invoke] = baseDeconstructor;
                     * data.LvalueTypes[structFormalRef] = data.ExpTypes[structFormalRefExp] = structFormal.GetType();
                     * data.ExpTypes[invoke] = baseDeconstructor.GetReturnType();*/
                }
            }
            this.structFormal = structFormal;
            base.CaseAMethodDecl(replacer);
        }
 public override void InAStructDecl(AStructDecl node)
 {
     node.GetName().Text = namespacePrefix + node.GetName().Text;
 }
        //private List<ErrorCollection.Error> multipleEntryCandidates = new List<ErrorCollection.Error>();
        public override void CaseAMethodDecl(AMethodDecl node)
        {
            //Done in a previous iteration

            /*if (node.GetName().Text == "InitMap" && node.GetFormals().Count == 0)
             * {
             *  if (finalTrans.multipleMainEntries)
             *  {
             *      multipleEntryCandidates.Add(new ErrorCollection.Error(node.GetName(), Util.GetAncestor<AASourceFile>(node.GetName()), "Candidate"));
             *  }
             *  else if (finalTrans.mainEntry != null)
             *  {
             *      multipleEntryCandidates.Add(new ErrorCollection.Error(finalTrans.mainEntry.GetName(), Util.GetAncestor<AASourceFile>(finalTrans.mainEntry.GetName()), "Candidate"));
             *      multipleEntryCandidates.Add(new ErrorCollection.Error(node.GetName(), Util.GetAncestor<AASourceFile>(node.GetName()), "Candidate"));
             *      //finalTrans.errors.Add(new ErrorCollection.Error(node.GetName(), Util.GetAncestor<AASourceFile>(node), "Found multiple candidates for a main entry", true));
             *      finalTrans.multipleMainEntries = true;
             *      finalTrans.mainEntry = null;
             *  }
             *  else
             *      finalTrans.mainEntry = node;
             * }*/

            AStructDecl str = Util.GetAncestor <AStructDecl>(node);

            if (str != null)
            {
                if (node.GetStatic() == null)
                {
                    structMethods.Add(node);
                }
                //Move the method outside the struct
                str.RemoveChild(node.Parent());
                AASourceFile file = (AASourceFile)str.Parent();
                int          i    = file.GetDecl().IndexOf(str);
                file.GetDecl().Insert(i /* + 1*/, node);
                node.GetName().Text = GetUniqueStructMethodName(str.GetName().Text + "_" + node.GetName().Text);

                if (node.GetStatic() == null)
                {
                    //Add the struct as a parameter
                    PType structType = new ANamedType(new TIdentifier(str.GetName().Text), null);
                    finalTrans.data.StructTypeLinks[(ANamedType)structType] = str;
                    if (str.GetClassToken() != null)
                    {
                        structType = new APointerType(new TStar("*"), structType);
                    }
                    structFormal = new AALocalDecl(new APublicVisibilityModifier(), null,
                                                   str.GetClassToken() == null ? new TRef("ref") : null, null, null,
                                                   structType,
                                                   new TIdentifier("currentStruct", node.GetName().Line,
                                                                   node.GetName().Pos), null);
                    node.GetFormals().Add(structFormal);
                    data.Locals[(AABlock)node.GetBlock()].Add(structFormal);
                }
                else
                {
                    node.SetStatic(null);
                }
                finalTrans.data.Methods.Add(new SharedData.DeclItem <AMethodDecl>(file, node));
                if (node.GetStatic() == null)
                {
                    OldParentStruct[node] = str;
                }
                //Fix refferences to other struct stuff);
                base.CaseAMethodDecl(node);
                //Will visit later, since it's added after the struct
                //base.CaseAMethodDecl(node);
                //if (str.GetLocals().Count == 0)
                //    str.Parent().RemoveChild(str);
                return;
            }
            AEnrichmentDecl enrichment = Util.GetAncestor <AEnrichmentDecl>(node);

            if (enrichment != null)
            {
                if (node.GetStatic() == null)
                {
                    structMethods.Add(node);
                }
                //Move the method outside the struct
                enrichment.RemoveChild(node);
                AASourceFile file = (AASourceFile)enrichment.Parent();
                int          i    = file.GetDecl().IndexOf(enrichment);
                file.GetDecl().Insert(i /* + 1*/, node);
                node.GetName().Text = GetUniqueStructMethodName(Util.TypeToIdentifierString(enrichment.GetType()) + "_" + node.GetName().Text);

                if (node.GetStatic() == null)
                {
                    //Add the struct as a parameter
                    PType structType = Util.MakeClone(enrichment.GetType(), finalTrans.data);
                    structFormal = new AALocalDecl(new APublicVisibilityModifier(), null, new TRef("ref"), null, null,
                                                   structType,
                                                   new TIdentifier("currentEnrichment", node.GetName().Line,
                                                                   node.GetName().Pos), null);
                    node.GetFormals().Add(structFormal);
                }
                finalTrans.data.Methods.Add(new SharedData.DeclItem <AMethodDecl>(file, node));
                //Fix refferences to other struct stuff);
                base.CaseAMethodDecl(node);
                //Will visit later, since it's added after the struct
                //base.CaseAMethodDecl(node);
                //if (str.GetLocals().Count == 0)
                //    str.Parent().RemoveChild(str);
                return;
            }
            //Build a list of overloads
            List <AMethodDecl> overloads     = new List <AMethodDecl>();
            List <string>      prefixMatches = new List <string>();

            foreach (SharedData.DeclItem <AMethodDecl> declItem in finalTrans.data.Methods)
            {
                if (!Util.IsSameNamespace(declItem.Decl, node))
                {
                    continue;
                }
                if (declItem.Decl.GetName().Text == node.GetName().Text)
                {
                    overloads.Add(declItem.Decl);
                }
                if (declItem.Decl.GetName().Text.StartsWith(node.GetName().Text + "O"))
                {
                    prefixMatches.Add(declItem.Decl.GetName().Text);
                }
            }



            foreach (AMethodDecl method in finalTrans.data.Libraries.Methods)
            {
                if (method.GetBlock() != null || method.GetNative() != null)
                {
                    if (method.GetName().Text == node.GetName().Text)
                    {
                        overloads.Add(method);
                    }
                    if (method.GetName().Text.StartsWith(node.GetName().Text + "O"))
                    {
                        prefixMatches.Add(method.GetName().Text);
                    }
                }
            }

            //Add fields
            foreach (SharedData.DeclItem <AFieldDecl> declItem in finalTrans.data.Fields)
            {
                if (declItem.Decl.GetName().Text.StartsWith(node.GetName().Text + "O"))
                {
                    prefixMatches.Add(declItem.Decl.GetName().Text);
                }
            }

            foreach (AFieldDecl field in finalTrans.data.Libraries.Fields)
            {
                if (field.GetName().Text.StartsWith(node.GetName().Text + "O"))
                {
                    prefixMatches.Add(field.GetName().Text);
                }
            }

            //Dont want to hit another method by appending O#
            string postfix = "";

            while (true)
            {
                postfix += "O";
                if (prefixMatches.Any(text => text.StartsWith(node.GetName().Text + postfix)))
                {
                    continue;
                }
                break;
            }

            if (overloads.Count > 1)
            {
                int i = 0;
                foreach (AMethodDecl method in overloads)
                {
                    if (node == finalTrans.mainEntry || (node.GetTrigger() != null && finalTrans.data.HasUnknownTrigger))
                    {
                        continue;
                    }
                    i++;
                    method.GetName().Text += postfix + i;
                }
            }

            if (node != finalTrans.mainEntry && (node.GetTrigger() == null || !finalTrans.data.HasUnknownTrigger))
            {
                node.GetName().Text = namespacePrefix + node.GetName().Text;
            }


            base.CaseAMethodDecl(node);
        }
Ejemplo n.º 28
0
        public override void CaseAStructDecl(AStructDecl node)
        {
            if (checkedStructs.Contains(node))
            {
                return;
            }
            checkedStructs.Add(node);

            //Set where they originate from.
            foreach (PLocalDecl localDecl in node.GetLocals())
            {
                if (localDecl is AALocalDecl)
                {
                    fieldOriginatesFrom[(AALocalDecl)localDecl] = node;
                }
                else //Is DeclLocalDecl
                {
                    ADeclLocalDecl aLocalDecl = (ADeclLocalDecl)localDecl;
                    PDecl          decl       = aLocalDecl.GetDecl();
                    if (decl is AMethodDecl)
                    {
                        methodOriginatesFrom[(AMethodDecl)decl] = node;
                    }
                    else if (decl is APropertyDecl)
                    {
                        propertyOriginatesFrom[(APropertyDecl)decl] = node;
                    }
                    else if (decl is AThisArrayPropertyDecl)
                    {
                        arrayPropertyOriginatesFrom[(AThisArrayPropertyDecl)decl] = node;
                    }
                }
            }


            if (node.GetBase() == null)
            {
                return;
            }

            AStructDecl baseStr = Lookup((ANamedType)node.GetBase());

            if (!checkedStructs.Contains(baseStr))
            {
                CaseAStructDecl(baseStr);
            }
            CheckEnheritanceList(node, new List <AStructDecl>());
            //A struct may not enhrit from a class
            if (node.GetClassToken() == null && baseStr.GetClassToken() != null)
            {
                errors.Add(new ErrorCollection.Error(node.GetName(), "A struct can not enherit from a class.", false,
                                                     new ErrorCollection.Error(baseStr.GetName(), "Enherited class")));
            }

            //Copy everything in base struct to here (Except from constructors)
            List <PLocalDecl> stuffToAdd = new List <PLocalDecl>();

            foreach (AALocalDecl baseLocalVar in data.StructFields[baseStr])
            {
                //Check that it is not overwritten
                foreach (AALocalDecl localVar in node.GetLocals().OfType <AALocalDecl>())
                {
                    if (baseLocalVar.GetName().Text == localVar.GetName().Text)
                    {
                        errors.Add(new ErrorCollection.Error(localVar.GetName(),
                                                             "It is not possible to override fields.", false,
                                                             new ErrorCollection.Error(
                                                                 fieldOriginatesFrom[baseLocalVar].GetName(),
                                                                 "Overridden " +
                                                                 Util.GetTypeName(fieldOriginatesFrom[baseLocalVar]))));
                        throw new ParserException(null, null);
                    }
                }
                foreach (APropertyDecl localProperty in data.StructProperties[node])
                {
                    if (localProperty.GetName().Text == baseLocalVar.GetName().Text)
                    {
                        errors.Add(new ErrorCollection.Error(localProperty.GetName(),
                                                             "It is not possible to override fields.", false,
                                                             new ErrorCollection.Error(
                                                                 fieldOriginatesFrom[baseLocalVar].GetName(),
                                                                 "Overridden " +
                                                                 Util.GetTypeName(fieldOriginatesFrom[baseLocalVar]))));
                        throw new ParserException(null, null);
                    }
                }
                //Insert at top
                AALocalDecl newLocalVar = (AALocalDecl)baseLocalVar.Clone();
                baseLocalVar.Apply(new FixNamedRefferences(newLocalVar, data));
                stuffToAdd.Add(newLocalVar);
                data.StructFields[node].Add(newLocalVar);
                fieldOriginatesFrom[newLocalVar] = fieldOriginatesFrom[baseLocalVar];
                if (data.EnheritanceLocalMap.ContainsKey(baseLocalVar))
                {
                    data.EnheritanceLocalMap[newLocalVar] = data.EnheritanceLocalMap[baseLocalVar];
                }
                else
                {
                    data.EnheritanceLocalMap[newLocalVar] = baseLocalVar;
                }
            }
            for (int i = stuffToAdd.Count - 1; i >= 0; i--)
            {
                node.GetLocals().Insert(0, stuffToAdd[i]);
            }
            //Methods
            foreach (AMethodDecl baseMethod in data.StructMethods[baseStr])
            {
                //Check that it is not overwritten
                foreach (AMethodDecl localMethod in node.GetLocals().OfType <ADeclLocalDecl>().Select(l => l.GetDecl()).OfType <AMethodDecl>())
                {
                    if (Util.GetMethodSignature(baseMethod) == Util.GetMethodSignature(localMethod))
                    {
                        errors.Add(new ErrorCollection.Error(localMethod.GetName(),
                                                             "It is not possible to override methods.", false,
                                                             new ErrorCollection.Error(
                                                                 methodOriginatesFrom[baseMethod].GetName(),
                                                                 "Overridden " +
                                                                 Util.GetTypeName(methodOriginatesFrom[baseMethod]))));
                        throw new ParserException(null, null);
                    }
                }
                data.StructMethods[node].Add(baseMethod);
            }
            //Properties
            foreach (APropertyDecl baseProperty in data.StructProperties[baseStr])
            {
                //Check that it is not overwritten
                foreach (APropertyDecl localProperty in data.StructProperties[node])
                {
                    if (localProperty.GetName().Text == baseProperty.GetName().Text)
                    {
                        errors.Add(new ErrorCollection.Error(localProperty.GetName(),
                                                             "It is not possible to override properties.", false,
                                                             new ErrorCollection.Error(
                                                                 propertyOriginatesFrom[baseProperty].GetName(),
                                                                 "Overridden " +
                                                                 Util.GetTypeName(propertyOriginatesFrom[baseProperty]))));
                        throw new ParserException(null, null);
                    }
                }
                foreach (AALocalDecl localVar in node.GetLocals().OfType <AALocalDecl>())
                {
                    if (baseProperty.GetName().Text == localVar.GetName().Text)
                    {
                        errors.Add(new ErrorCollection.Error(localVar.GetName(),
                                                             "It is not possible to override properties.", false,
                                                             new ErrorCollection.Error(
                                                                 propertyOriginatesFrom[baseProperty].GetName(),
                                                                 "Overridden " +
                                                                 Util.GetTypeName(propertyOriginatesFrom[baseProperty]))));
                        throw new ParserException(null, null);
                    }
                }
                data.StructProperties[node].Add(baseProperty);
            }
        }
Ejemplo n.º 29
0
            public override void CaseANamedType(ANamedType node)
            {
                //Remember.. if you are the child of a fieldDecl, that field may have been moved
                if (!node.IsPrimitive())//GalaxyKeywords.Primitives.words.Any(word => word == node.GetName().Text)))
                {
                    AStructDecl decl        = finalTrans.data.StructTypeLinks[node];
                    Item        currentItem = GetIncludeItem(node);
                    if (Util.GetAncestor <AFieldDecl>(node) != null)
                    {
                        Item i = allItems.OfType <FieldItem>().FirstOrDefault(item => item.FieldDecl == Util.GetAncestor <AFieldDecl>(node));
                        if (i != null)
                        {
                            currentItem = i;
                        }
                    }
                    if (Util.GetAncestor <AStructDecl>(node) != null)
                    {
                        Item i =
                            allItems.OfType <StructItem>().FirstOrDefault(
                                item => item.StructDecl == Util.GetAncestor <AStructDecl>(node));
                        if (i != null)
                        {
                            currentItem = i;
                        }
                    }
                    Item declItem = ((Item)allItems.OfType <StructItem>().FirstOrDefault(item => item.StructDecl == decl)) ??
                                    allItems.OfType <IncludeItem>().First(
                        item => item.Current == Util.GetAncestor <AASourceFile>(decl));

                    List <Item> cPath = currentItem.Path;
                    List <Item> dPath = declItem.Path;

                    for (int i = 0; i < Math.Min(cPath.Count, dPath.Count); i++)
                    {
                        if (cPath[i] != dPath[i])
                        {//FORK!!!!
                            //We have a fork. make sure that the field is visible
                            int cI = cPath[i - 1].Children.IndexOf(cPath[i]);
                            int dI = dPath[i - 1].Children.IndexOf(dPath[i]);

                            if (dI < cI)
                            {//The decl is okay
                                break;
                            }

                            //Move the decl up
                            if (declItem is StructItem)
                            {
                                declItem.Parent.Children.Remove(declItem);
                                declItem.Parent = cPath[i - 1];
                            }
                            else
                            {
                                declItem = new StructItem(decl, cPath[i - 1], new List <Item>());
                                allItems.Add(declItem);
                            }
                            cPath[i - 1].Children.Insert(cI, declItem);
                            break;
                        }
                        if (i == cPath.Count - 1)
                        {
                            if (i == dPath.Count - 1)
                            {
                                //The decl and use is in same file. Ensure that the decl is before
                                if (Util.TokenLessThan(decl.GetName(), node.GetToken()))
                                {
                                    break;
                                }
                                //Add the decl item
                                declItem = new StructItem(decl, cPath[i], new List <Item>());
                                allItems.Add(declItem);
                                cPath[i].Children.Add(declItem);
                                break;
                            }
                            else
                            {
                                //The decl is included here or somewhere deeper. But above the use
                                break;
                            }
                        }
                        else if (i == dPath.Count - 1)
                        {
                            //We have reached the file where the decl is, but the use is included deeper, so it is above. Insert decl
                            int cI = cPath[i].Children.IndexOf(cPath[i + 1]);
                            declItem = new StructItem(decl, cPath[i], new List <Item>());
                            allItems.Add(declItem);
                            cPath[i].Children.Insert(cI, declItem);
                            break;
                        }
                    }
                }
                base.CaseANamedType(node);
            }