~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/translator/cli/src/query.cs

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Reflection;
 
3
using System.Collections.Generic;
 
4
 
 
5
public class Query
 
6
{
 
7
    public static Dictionary<Type, bool> PendingTypes = new Dictionary<Type, bool>();
 
8
 
 
9
    public static int Main(string[] argv)
 
10
    { 
 
11
        if (argv.Length != 1) {
 
12
            Console.Error.WriteLine("Usage: query full-qualified-name");
 
13
            return 1;
 
14
        }
 
15
        
 
16
        string name = argv[0];
 
17
        Type t = Type.GetType(name);
 
18
        if (t == null) {
 
19
            Console.Error.WriteLine("Cannot load type {0}", name);
 
20
            return 2;
 
21
        }
 
22
 
 
23
        if (!t.IsPublic) {
 
24
            Console.Error.WriteLine("Cannot load a non-public type");
 
25
            return 2;
 
26
        }
 
27
 
 
28
        PrintType(t);
 
29
        return 0;
 
30
    }
 
31
 
 
32
    private static void PrintType(Type t)
 
33
    {
 
34
        Console.WriteLine("Assembly = '{0}'", t.Assembly.FullName);
 
35
        Console.WriteLine("FullName = '{0}'", t.FullName);
 
36
        Console.WriteLine("BaseType = '{0}'", GetBaseType(t));
 
37
        Console.WriteLine("OOType = '{0}'", GetOOType(t));
 
38
        Console.WriteLine("IsArray = {0}", t.IsArray);
 
39
        if (t.IsArray)
 
40
            Console.WriteLine("ElementType = '{0}'", t.GetElementType().FullName);
 
41
        PrintMethods("StaticMethods", t.GetMethods(BindingFlags.Static|BindingFlags.Public|BindingFlags.DeclaredOnly));
 
42
        PrintMethods("Methods", t.GetMethods(BindingFlags.Instance|BindingFlags.Public|BindingFlags.DeclaredOnly));
 
43
        PendingTypes.Remove(t);
 
44
        PrintDepend();
 
45
    }
 
46
 
 
47
    private static string GetBaseType(Type t)
 
48
    {
 
49
        if (t == typeof(object))
 
50
            return "ROOT"; // special case for System.Object to avoid circular dependencies
 
51
        else if (t.BaseType == null)
 
52
             return "System.Object"; // the only known case is the BaseType of an interface
 
53
        else
 
54
            return t.BaseType.FullName;
 
55
    }
 
56
 
 
57
    private static string GetOOType(Type t)
 
58
    {
 
59
        if (t == null)
 
60
            return "";
 
61
        else if (t == typeof(void))
 
62
            return "ootype.Void";
 
63
        else if (t == typeof(int))
 
64
            return "ootype.Signed";
 
65
        else if (t == typeof(uint))
 
66
            return "ootype.Unsigned";
 
67
        else if (t == typeof(long))
 
68
            return "ootype.SignedLongLong";
 
69
        else if (t == typeof(ulong))
 
70
            return "ootype.UnsignedLongLong";
 
71
        else if (t == typeof(bool))
 
72
            return "ootype.Bool";
 
73
        else if (t == typeof(double))
 
74
            return "ootype.Float";
 
75
        else if (t == typeof(char))
 
76
            return "ootype.Char"; // maybe it should be unichar?
 
77
        else if (t == typeof(string))
 
78
            return "ootype.String";
 
79
        else {
 
80
            PendingTypes[t] = true;
 
81
            string name = t.FullName.Replace(".", "_"); // TODO: ensure unicity
 
82
            if (t.IsArray)
 
83
                name = name.Replace("[]", "___array___");
 
84
            return name;
 
85
        }
 
86
    }
 
87
 
 
88
    private static void PrintMethods(string varname, MethodInfo[] methods)
 
89
    {
 
90
        Console.WriteLine("{0} = [", varname);
 
91
        // MethodName, [ARGS], RESULT
 
92
        foreach(MethodInfo meth in methods) {
 
93
            if (IgnoreMethod(meth))
 
94
                continue;
 
95
            Console.Write("    ('{0}', [", meth.Name);
 
96
            foreach(ParameterInfo par in meth.GetParameters()) {
 
97
                Console.Write("'{0}'", GetOOType(par.ParameterType));
 
98
                Console.Write(", ");
 
99
            }
 
100
            Console.WriteLine("], '{0}'),", GetOOType(meth.ReturnType));
 
101
        }
 
102
        Console.WriteLine("  ]");
 
103
    }
 
104
 
 
105
    private static bool IgnoreMethod(MethodInfo meth)
 
106
    {
 
107
        if (!meth.IsPublic)
 
108
            return true;
 
109
 
 
110
        // ignore all SpecialName but properties getter/setter
 
111
        if (meth.IsSpecialName && !meth.Name.StartsWith("get_") && !meth.Name.StartsWith("set_"))
 
112
            return true;
 
113
 
 
114
        if (IgnoreType(meth.ReturnType))
 
115
            return true;
 
116
        foreach(ParameterInfo par in meth.GetParameters())
 
117
            if (IgnoreType(par.ParameterType))
 
118
                return true;
 
119
 
 
120
        return false;
 
121
    }
 
122
 
 
123
    private static bool IgnoreType(Type t)
 
124
    {
 
125
        return !t.IsPrimitive 
 
126
            && t != typeof(void)
 
127
            &&(t == typeof(System.ValueType) ||
 
128
               t == typeof(System.Array) ||
 
129
               t.FullName.StartsWith("System.Array+InternalArray") ||
 
130
               t.IsValueType ||
 
131
               t.IsByRef ||
 
132
               t.IsPointer ||
 
133
               t.IsGenericType ||
 
134
               t.IsGenericTypeDefinition);
 
135
    }
 
136
 
 
137
    private static void PrintDepend()
 
138
    {
 
139
        Console.Write("Depend = [");
 
140
        foreach(Type t in PendingTypes.Keys)
 
141
            Console.Write("'{0}', ", t.FullName);
 
142
        Console.WriteLine("]");
 
143
    }
 
144
}