public override void OutAStructDecl(AStructDecl node)
 {
     if (node.GetClassToken() != null && node.GetIntDim() == null)
     {
         node.Parent().RemoveChild(node);
     }
     base.OutAStructDecl(node);
 }
Esempio n. 2
0
 public override void CaseAStructDecl(AStructDecl node)
 {
     if (node.GetLocals().Count == 0)
     {
         node.Parent().RemoveChild(node);
     }
     else
     {
         base.CaseAStructDecl(node);
     }
 }
Esempio n. 3
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);
 }
Esempio n. 4
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);
            }
        }
        //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);
        }