Exemplo n.º 1
0
    void CreateTypes(Node root)
    {
        foreach (Node n in root.Children)
        {
            if (n.IsTrue("incomplete") || !n.HasValue("name") || n.Attributes ["name"] == "::")
            {
                continue;
            }

            Namespace ns;
            switch (n.Type)
            {
            case "Class":
            case "Struct":
                ns = new Class(n);
                break;

            case "Enumeration":
                ns = new Enumeration(n);
                break;

            case "Namespace":
                ns = new Namespace(n);
                break;

            case "Typedef":
                var typeNode = GetTypeNode(n);
                if (typeNode.Type != "FunctionType")
                {
                    continue;
                }

                ns = new Function(n);
                NodeToNamespace[typeNode] = ns;
                break;

            default:
                continue;
            }

            NodeToNamespace [n] = ns;
            Lib.Namespaces.Add(ns);
        }

        foreach (Namespace ns in Lib.Namespaces)
        {
            SetParentNamespace(ns);
        }

        // NodeToNamespace mapping is complete. Lets make a copy.

        var mapping = new Dictionary <Node, Namespace> (NodeToNamespace);

        for (var i = 0; i < Lib.Namespaces.Count; i++)
        {
            Namespace ns = Lib.Namespaces [i];

            var filter = GetFilterOrDefault(ns);
            if (filter.Mode == FilterMode.Exclude)
            {
                NodeToNamespace.Remove(ns.Node);
            }

            if (filter.Mode != FilterMode.Include)
            {
                Lib.Namespaces.RemoveAt(i);
                i--;
                continue;
            }

            var klass = ns as Class;
            if (klass == null)
            {
                continue;
            }

            // Compute templates and name
            int index = klass.Name.IndexOf("<");
            if (index >= 0)
            {
                // Separate out the template class name and the template types
                string className     = klass.Name.Substring(0, index).Trim();
                string templateTypes = klass.Name.Substring(index).Trim();
                klass.TemplateName      = klass.Name;
                klass.TemplateClassName = className;
                klass.Name = CreateTemplateClassName(className, templateTypes);

                // Ensure outside references to this class use the proper name as well
                //klass.Node.Name = klass.Name;
            }

            // Compute bases
            foreach (Node bn in klass.Node.Children.Where(o => o.Type == "Base"))
            {
                var nodeForAttr = bn.NodeForAttr("type");
                if (!NodeToNamespace.ContainsKey(nodeForAttr))
                {
                    Console.WriteLine("FATAL ERROR: Base class '{0}' not found for type '{1}'", mapping[nodeForAttr].Name, klass.Name);
                    Environment.Exit(1);
                }
                Class baseClass = NodeToNamespace [nodeForAttr] as Class;
                Debug.Assert(baseClass != null);
                klass.BaseClasses.Add(baseClass);
                if (bn.IsTrue("virtual"))
                {
                    klass.VirtualBases.Add(baseClass);
                }
            }
        }
    }
Exemplo n.º 2
0
    CppType GetType(Node n, CppType modifiers)
    {
        var fundamental = CppTypes.Unknown;

        switch (n.Type)
        {
        case "Typedef":
            return(GetType(GetTypeNode(n), modifiers));

        case "ArrayType":
            CppModifiers mod = null;
            var          max = n.Attributes ["max"];
            var          min = n.Attributes ["min"];
            if (max != null && min != null)
            {
                var l = GetLongValue(max) - GetLongValue(min);
                mod = new CppModifiers.ArrayModifier((int)l + 1);
            }
            else
            {
                mod = CppModifiers.Array;
            }
            return(GetType(GetTypeNode(n), modifiers.Modify(mod)));

        case "PointerType":
            return(GetType(GetTypeNode(n), modifiers.Modify(CppModifiers.Pointer)));

        case "ReferenceType":
            return(GetType(GetTypeNode(n), modifiers.Modify(CppModifiers.Reference)));

        case "FundamentalType":
            return(modifiers.CopyTypeFrom(new CppType(n.Name)));

        case "CvQualifiedType":
            if (n.IsTrue("const"))
            {
                return(GetType(GetTypeNode(n), modifiers.Modify(CppModifiers.Const)));
            }
            else
            {
                throw new NotImplementedException();
            }

        case "Class":
            fundamental = CppTypes.Class;
            break;

        case "Struct":
            fundamental = CppTypes.Struct;
            break;

        case "Enumeration":
            fundamental = CppTypes.Enum;
            break;

        case "FunctionType":
            fundamental = CppTypes.Delegate;
            break;

        default:
            return(CppTypes.Unknown);
        }

        if (!NodeToNamespace.ContainsKey(n))
        {
            // FIXME: Do something better
            if (string.IsNullOrEmpty(n.Name))
            {
                Console.WriteLine("WARNING: Could not find type with id '{0}'", n.Id);
            }
            else
            {
                Console.WriteLine("WARNING: Could not find type '{0}'", n.Name);
            }

            return(CppTypes.Unknown);
        }

        return(modifiers.CopyTypeFrom(new CppType(fundamental, string.Join("::", NodeToNamespace [n].FullyQualifiedName))));
    }
Exemplo n.º 3
0
    CppType GetType(Node n, CppType modifiers)
    {
        var fundamental = CppTypes.Unknown;

        switch (n.Type)
        {
        case "Typedef":
            return(GetType(GetTypeNode(n), modifiers));

        case "ArrayType":
            CppModifiers mod = null;
            if (n.Attributes ["max"] != null && n.Attributes ["min"] != null)
            {
                mod = new CppModifiers.ArrayModifier(int.Parse(n.Attributes ["max"].TrimEnd('u')) - int.Parse(n.Attributes ["min"].TrimEnd('u')) + 1);
            }
            else
            {
                mod = CppModifiers.Array;
            }
            return(GetType(GetTypeNode(n), modifiers.Modify(mod)));

        case "PointerType":
            return(GetType(GetTypeNode(n), modifiers.Modify(CppModifiers.Pointer)));

        case "ReferenceType":
            return(GetType(GetTypeNode(n), modifiers.Modify(CppModifiers.Reference)));

        case "FundamentalType":
            return(modifiers.CopyTypeFrom(new CppType(n.Name)));

        case "CvQualifiedType":
            if (n.IsTrue("const"))
            {
                return(GetType(GetTypeNode(n), modifiers.Modify(CppModifiers.Const)));
            }
            else
            {
                throw new NotImplementedException();
            }

        case "Class":
            fundamental = CppTypes.Class;
            break;

        case "Struct":
            fundamental = CppTypes.Struct;
            break;

        case "Enumeration":
            fundamental = CppTypes.Enum;
            break;

        default:
            return(CppTypes.Unknown);
        }

        if (!NodeToNamespace.ContainsKey(n))
        {
            // FIXME: Do something better
            return(CppTypes.Unknown);
        }

        return(modifiers.CopyTypeFrom(new CppType(fundamental, string.Join("::", NodeToNamespace [n].FullyQualifiedName))));
    }