Exemple #1
0
        private AStructDecl Lookup(ANamedType type)
        {
            //Look for structs
            List <AStructDecl> structs = new List <AStructDecl>();
            bool matchingPrimitive;

            LinkNamedTypes.GetMatchingTypes(type, new List <ATypedefDecl>(), structs, new List <AMethodDecl>(), new List <TIdentifier>(), out matchingPrimitive);

            if (structs.Count == 0)
            {
                errors.Add(new ErrorCollection.Error(type.GetToken(), "No type found named " + ((AAName)type.GetName()).AsString()), true);
                throw new ParserException(null, null);
            }
            else if (structs.Count > 1)
            {
                List <ErrorCollection.Error> subError = new List <ErrorCollection.Error>();
                foreach (AStructDecl structDecl in structs)
                {
                    subError.Add(new ErrorCollection.Error(structDecl.GetName(), "Matching " + Util.GetTypeName(structDecl)));
                }
                errors.Add(
                    new ErrorCollection.Error(type.GetToken(),
                                              "Found multiple structs/classes matching " + ((AAName)type.GetName()).AsString(),
                                              false, subError.ToArray()), true);
            }

            return(structs[0]);
        }
Exemple #2
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);
 }
Exemple #3
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);
 }
Exemple #4
0
        public override void OutANamedType(ANamedType node)
        {
            if (node.Parent() is ATypedefDecl && ((ATypedefDecl)node.Parent()).GetName() == node)
            {
                return;
            }

            //Link named type to their definition (structs)
            List <ATypedefDecl> typeDefs  = new List <ATypedefDecl>();
            List <AStructDecl>  structs   = new List <AStructDecl>();
            List <AMethodDecl>  delegates = new List <AMethodDecl>();
            List <TIdentifier>  generics  = new List <TIdentifier>();
            bool matchPrimitive;

            GetMatchingTypes(node, typeDefs, structs, delegates, generics, out matchPrimitive);

            int matches = typeDefs.Count + structs.Count + delegates.Count + (matchPrimitive ? 1 : 0) + generics.Count;

            if (matches == 0)
            {
                errors.Add(new ErrorCollection.Error(node.GetToken(), "Could not find any types matching " + ((AAName)node.GetName()).AsString()), true);
            }
            else if (generics.Count != 1 && matches > 1)
            {
                List <ErrorCollection.Error> subError = new List <ErrorCollection.Error>();
                if (matchPrimitive)
                {
                    subError.Add(new ErrorCollection.Error(node.GetToken(), "Matches primitive " + ((AAName)node.GetName()).AsString()));
                }
                foreach (ATypedefDecl typeDef in typeDefs)
                {
                    subError.Add(new ErrorCollection.Error(typeDef.GetToken(), "Matching typedef"));
                }
                foreach (AStructDecl structDecl in structs)
                {
                    subError.Add(new ErrorCollection.Error(structDecl.GetName(), "Matching " + Util.GetTypeName(structDecl)));
                }
                foreach (AMethodDecl methodDecl in delegates)
                {
                    subError.Add(new ErrorCollection.Error(methodDecl.GetName(), "Matching delegate"));
                }
                foreach (TIdentifier identifier in generics)
                {
                    subError.Add(new ErrorCollection.Error(identifier, "Matching generic"));
                }
                errors.Add(
                    new ErrorCollection.Error(node.GetToken(),
                                              "Found multiple types matching " + ((AAName)node.GetName()).AsString(),
                                              false, subError.ToArray()), true);
            }
            else
            {
                if (generics.Count == 1)
                {
                    data.GenericLinks[node] = generics[0];
                    return;
                }
                if (typeDefs.Count == 1)
                {
                    ATypedefDecl typeDef = typeDefs[0];
                    //data.TypeDefLinks[node] = typeDef;
                    PType type = (PType)typeDef.GetType().Clone();
                    node.ReplaceBy(type);
                    type.Apply(this);
                    return;
                }
                if (structs.Count == 1)
                {
                    data.StructTypeLinks[node] = structs[0];
                }
                else if (delegates.Count == 1)
                {
                    data.DelegateTypeLinks[node] = delegates[0];
                }
                if (!matchPrimitive && !(structs.Count == 1 && data.Enums.ContainsKey(structs[0])) && node.Parent() is AEnrichmentDecl) //Not allowed to enrich a struct, class or delegate
                {
                    errors.Add(new ErrorCollection.Error(node.GetToken(), "You can not enrich this type."));
                }
            }
        }
Exemple #5
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);
            }