c4ce86de6888ab1a83a720c233e59d10b9131c8c
[mono.git] / mcs / tools / linker / Mono.Linker.Steps / ResolveFromXApiStep.cs
1 //
2 // ResolveFromXApiStep.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@novell.com)
6 //
7 // (C) 2007 Novell, Inc.
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.Xml.XPath;
30
31 using Mono.Linker;
32
33 using Mono.Cecil;
34
35 namespace Mono.Linker.Steps {
36
37         public class ResolveFromXApiStep : ResolveStep, IXApiVisitor {
38
39                 static readonly string _name = "name";
40                 static readonly string _ns = string.Empty;
41
42                 XPathDocument _document;
43
44                 public ResolveFromXApiStep (XPathDocument document)
45                 {
46                         _document = document;
47                 }
48
49                 protected override void Process ()
50                 {
51                         XApiReader reader = new XApiReader (_document, this);
52                         reader.Process (Context);
53                 }
54
55                 public void OnAssembly (XPathNavigator nav, AssemblyDefinition assembly)
56                 {
57                 }
58
59                 public void OnAttribute (XPathNavigator nav)
60                 {
61                         string name = GetName (nav);
62
63                         TypeDefinition type = Context.GetType (name);
64                         if (type != null)
65                                 MarkType (type);
66                 }
67
68                 public void OnClass (XPathNavigator nav, TypeDefinition type)
69                 {
70                         MarkType (type);
71                 }
72
73                 public void OnInterface (XPathNavigator nav, TypeDefinition type)
74                 {
75                         MarkType (type);
76                 }
77
78                 public void OnField (XPathNavigator nav, FieldDefinition field)
79                 {
80                         MarkField (field);
81                 }
82
83                 public void OnMethod (XPathNavigator nav, MethodDefinition method)
84                 {
85                         MarkMethod (method);
86                 }
87
88                 public void OnConstructor (XPathNavigator nav, MethodDefinition method)
89                 {
90                         MarkMethod (method);
91                 }
92
93                 public void OnProperty (XPathNavigator nav, PropertyDefinition property)
94                 {
95                 }
96
97                 public void OnEvent (XPathNavigator nav, EventDefinition evt)
98                 {
99                         if (evt.AddMethod != null)
100                                 MarkMethod (evt.AddMethod);
101                         if (evt.InvokeMethod != null)
102                                 MarkMethod (evt.InvokeMethod);
103                         if (evt.RemoveMethod != null)
104                                 MarkMethod (evt.RemoveMethod);
105                 }
106
107                 static string GetName (XPathNavigator nav)
108                 {
109                         return GetAttribute (nav, _name);
110                 }
111
112                 static string GetAttribute (XPathNavigator nav, string attribute)
113                 {
114                         return nav.GetAttribute (attribute, _ns);
115                 }
116
117                 void MarkType (TypeDefinition type)
118                 {
119                         InternalMark (type);
120                 }
121
122                 void MarkField (FieldDefinition field)
123                 {
124                         InternalMark (field);
125                 }
126
127                 void InternalMark (IMetadataTokenProvider provider)
128                 {
129                         Annotations.Mark (provider);
130                         Annotations.SetPublic (provider);
131                 }
132
133                 void MarkMethod (MethodDefinition method)
134                 {
135                         InternalMark (method);
136                         Annotations.SetAction (method, MethodAction.Parse);
137                 }
138         }
139 }