summaryrefslogtreecommitdiffstats
path: root/tools/Sandcastle/Source/Reflection/ApiVisitor.cs
blob: 7b5d9af0ecfc7e74d13c2cf2af1d2742d726409f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright (c) Microsoft Corporation.  All rights reserved.
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

using System.Compiler;


namespace Microsoft.Ddue.Tools.Reflection {

    public class ApiVisitor : IDisposable {

        private static Comparison < Member > memberComparison = new Comparison < Member >(CompareMembers);

        // Sorting logic

        private static Comparison < Namespace > namespaceComparison = new Comparison < Namespace >(CompareMembers);

        private static Comparison < TypeNode > typeComparison = new Comparison < TypeNode >(CompareMembers);

        private List < AssemblyNode > accessoryAssemblies = new List < AssemblyNode >();

        // Disposal logic

        private List < AssemblyNode > assemblies = new List < AssemblyNode >();

        // object model store

        private Dictionary < string, Namespace > catalog = new Dictionary < string, Namespace >();

        private ApiFilter filter;

        // Keep track of any metadata load errors

        private Dictionary < string, Exception > loadErrors = new Dictionary < string, Exception >();

        // Revised assembly storage

        private AssemblyResolver resolver = new AssemblyResolver();

        protected ApiVisitor(ApiFilter filter) {
            this.filter = filter;
        }

        protected ApiVisitor() : this(new NoFilter()) { }

        public AssemblyNode[] AccessoryAssemblies {
            get {
                return (accessoryAssemblies.ToArray());
            }
        }

        public ApiFilter ApiFilter {
            get {
                return (filter);
            }
            set {
                filter = value;
            }
        }

        public AssemblyNode[] Assemblies {
            get {
                return (assemblies.ToArray());
            }
        }

        public Dictionary < string, Exception > LoadErrors {
            get {
                return (loadErrors);
            }
        }

        public AssemblyResolver Resolver {
            get {
                return (resolver);
            }
            set {
                resolver = value;
            }
        }

        public void Dispose() {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing) {
            if (disposing) {
                foreach (AssemblyNode assembly in assemblies) {
                    //				Console.WriteLine(loadedModule.Name);
                    assembly.Dispose();
                }
                foreach (AssemblyNode accessoryAssembly in accessoryAssemblies) {
                    accessoryAssembly.Dispose();
                }
            }
        }

        public void LoadAccessoryAssemblies(string filePattern) {

            // get the full path to the relevent directory
            string directoryPath = Path.GetDirectoryName(filePattern);
            if ((directoryPath == null) || (directoryPath.Length == 0)) directoryPath = Environment.CurrentDirectory;
            directoryPath = Path.GetFullPath(directoryPath);

            // get the file name, which may contain wildcards
            string filePath = Path.GetFileName(filePattern);

            // look up the files and load them
            string[] files = Directory.GetFiles(directoryPath, filePath);
            foreach (string file in files) {
                LoadAccessoryAssembly(file);
            }
        }

        // Accessory modules

        //private IDictionary cache = new Hashtable();

        public void LoadAccessoryAssembly(string filePath) {
            AssemblyNode assembly = AssemblyNode.GetAssembly(filePath, null, false, false, false, false); // this causes non-classes to register as classes
            if (assembly != null) {
                if (assembly.Name == "mscorlib") ResetMscorlib(assembly);

                resolver.Add(assembly);
                //assembly.AssemblyReferenceResolutionAfterProbingFailed += unresolvedModuleHandler;
                accessoryAssemblies.Add(assembly);
            }
        }

        public void LoadAssemblies(string filePattern) {

            // get the full path to the relevent directory
            string directoryPath = Path.GetDirectoryName(filePattern);
            if ((directoryPath == null) || (directoryPath.Length == 0)) directoryPath = Environment.CurrentDirectory;
            directoryPath = Path.GetFullPath(directoryPath);

            // get the file name, which may contain wildcards
            string filePath = Path.GetFileName(filePattern);

            // look up the files and load them
            string[] files = Directory.GetFiles(directoryPath, filePath);
            foreach (string file in files) {
                LoadAssembly(file);
            }

        }

        // Parsing logic

        public void LoadAssembly(string filePath) {
            //Console.WriteLine("loading {0}", filePath);
            //AssemblyNode assembly = AssemblyNode.GetAssembly(filePath);   // this causes mscorlib to be missing members
            //AssemblyNode assembly = AssemblyNode.GetAssembly(filePath, cache); // this causes compact framework non-classes to register as classes
            //AssemblyNode assembly = AssemblyNode.GetAssembly(filePath, cache, false, false, true, false); // this causes missing mscorlib members
            //AssemblyNode assembly = AssemblyNode.GetAssembly(filePath, cache, false, false, false, false); // this causes non-classes to register as classes
            //AssemblyNode assembly = AssemblyNode.GetAssembly(filePath, null, false, false, true, false); // this causes missing mscorlib members
            AssemblyNode assembly = AssemblyNode.GetAssembly(filePath, null, false, false, false, false); // this causes non-classes to register as classes

            if (assembly != null) {
                if (assembly.Name == "mscorlib") ResetMscorlib(assembly);

                // Console.WriteLine("assembly = {0}", assembly.Name);
                resolver.Add(assembly);
                //assembly.AssemblyReferenceResolutionAfterProbingFailed += unresolvedModuleHandler;
                assemblies.Add(assembly);
                //Console.WriteLine("{0} has {1} types", assembly.Name, assembly.ExportedTypes.Count);
                //StoreTypes(assembly.Types);
            }

        }

        // Visit Object Model

        public void VisitApis() {

            // store types
            // we have to do this after all assemblies are registered because the resolution may not work unless
            // all the assemblies we need are in the resolver cache
            //Console.WriteLine("storing types");
            foreach (AssemblyNode assembly in assemblies) {
                //Console.WriteLine("assembly {0}", assembly.Name);
                //Console.WriteLine("has {0} types", assembly.Types.Count);
                StoreTypes(assembly.Types);
                //Console.WriteLine("done with assembly");
            }
            //Console.WriteLine("done storing types");

            //Console.WriteLine("visiting namespaces");
            NamespaceList spaces = new NamespaceList();
            foreach (Namespace space in catalog.Values) {
                if (filter.IsExposedNamespace(space)) spaces.Add(space);
            }
            VisitNamespaces(spaces);
        }

        protected virtual void VisitEntity(Member entity) {
            // inherit and insert logic here
        }

        protected virtual void VisitMember(Member member) {
            VisitEntity(member);
        }

        protected virtual void VisitMembers(MemberList members) {
            // sort members by name
            Member[] sorted_members = new Member[members.Count];
            for (int i = 0; i < members.Count; i++) sorted_members[i] = members[i];
            Array.Sort < Member >(sorted_members, memberComparison);
            // visit them
            foreach (Member member in sorted_members) {
                // don't visit nested types, as they are already visited
                if (member is TypeNode) continue;
                if (filter.IsExposedMember(member))
                {
                    VisitMember(member);
                }
            }
        }

        protected virtual void VisitNamespace(Namespace space) {
            //Console.WriteLine("Visit Entity {0}",space.FullName);
            VisitEntity(space);
            TypeNodeList types = space.Types;
            VisitTypes(types);
        }

        protected virtual void VisitNamespaces(NamespaceList spaces) {
            // sort namespaces by name
            Namespace[] sorted_spaces = new Namespace[spaces.Count];
            for (int i = 0; i < spaces.Count; i++) sorted_spaces[i] = spaces[i];
            Array.Sort < Namespace >(sorted_spaces, namespaceComparison);
            // visit them
            foreach (Namespace space in sorted_spaces) {
                if (filter.IsExposedNamespace(space)) VisitNamespace(space);
            }
        }

        protected virtual void VisitType(TypeNode type) {
            //Console.WriteLine(type.FullName);
            VisitEntity(type);
            MemberList members = type.Members;
            //Console.WriteLine("{0}: {1}", type.FullName, members.Length);
            VisitMembers(members);
        }

        protected virtual void VisitTypes(TypeNodeList types) {
            // sort types by name
            TypeNode[] sorted_types = new TypeNode[types.Count];
            for (int i = 0; i < types.Count; i++) sorted_types[i] = types[i];
            Array.Sort < TypeNode >(sorted_types, typeComparison);
            // visit them
            foreach (TypeNode type in sorted_types) {
                //Console.WriteLine("visiting {0}", type.Name);
                //visit this type if it is exposed, or has members that are set as exposed
                if (filter.IsExposedType(type) || filter.HasExposedMembers(type))
                    VisitType(type); //visit type and members
            }
        }

        private static int CompareMembers(Member a, Member b) {
            return (String.Compare(a.FullName, b.FullName));
        }

        private static int CompareNamespaces(Namespace a, Namespace b) {
            return (String.Compare(a.Name.Name, b.Name.Name));
        }

        private static int CompareTypes(TypeNode a, TypeNode b) {
            return (String.Compare(a.Name.Name, b.Name.Name));
        }

        private static string GetNamespaceName(TypeNode type) {
            TypeNode parent = type.DeclaringType;
            if (parent != null) {
                return (GetNamespaceName(parent));
            } else {
                if (type.Namespace == null) {
                    return (String.Empty);
                } else {
                    return (type.Namespace.Name);
                }
            }
        }

        private void ResetMscorlib(AssemblyNode assembly) {
            TargetPlatform.Clear();
            CoreSystemTypes.Clear();
            CoreSystemTypes.SystemAssembly = assembly;
            CoreSystemTypes.Initialize(true, false);
        }

        private void StoreType(TypeNode type) {
            //Console.WriteLine("type: {0} ", type.Name);
            /*
            if (type.Name == null) {
                // CCI seems to occasionally construct corrupted, phantom types, which we should reject 
                // Console.WriteLine("unidentified type rejected");
                return;
            }
            */
            string spaceName = GetNamespaceName(type);
            //Console.WriteLine("in space: {0}", spaceName);
            Namespace space;
            if (!catalog.TryGetValue(spaceName, out space)) {
                space = new Namespace(new Identifier(spaceName));
                catalog.Add(spaceName, space);
            }
            if (space.Types == null) Console.WriteLine("null type list");
            space.Types.Add(type);

            //Console.WriteLine("getting members");
            MemberList members = type.Members;
            //Console.WriteLine("got {0} members", members.Count);
            for (int i = 0; i < members.Count; i++) {
                TypeNode nestedType = members[i] as TypeNode;
                if (nestedType != null) {
                    //Console.WriteLine("nested type {0}", type.FullName);
                    StoreType(nestedType);
                }
            }
            //Console.WriteLine("done storing type");
        }

        private void StoreTypes(TypeNodeList types) {
            //Console.WriteLine("{0} types", types.Length);
            for (int i = 0; i < types.Count; i++) {
                StoreType(types[i]);
            }
        }

    }


}