IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
Ldtoken.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2010 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24
25using System;
26using System.Xml.Linq;
27
29using IKVM.Reflection;
31using IKVM.Runtime;
32
33using Type = IKVM.Reflection.Type;
34
36{
37
38 [Instruction("ldtoken")]
39 public sealed class Ldtoken : Instruction
40 {
41
47 public static new Ldtoken Read(XElement element)
48 {
49 var inst = new Ldtoken();
50 Load(inst, element);
51 return inst;
52 }
53
59 public static void Load(Ldtoken inst, XElement element)
60 {
61 Load((Instruction)inst, element);
62 inst.Type = (string)element.Attribute("type");
63 inst.Class = (string)element.Attribute("class");
64 inst.Method = (string)element.Attribute("method");
65 inst.Field = (string)element.Attribute("field");
66 inst.Sig = (string)element.Attribute("sig");
67 }
68
69 public string Type { get; set; }
70
71 public string Class { get; set; }
72
73 public string Method { get; set; }
74
75 public string Field { get; set; }
76
77 public string Sig { get; set; }
78
79 internal override void Generate(CodeGenContext context, CodeEmitter ilgen)
80 {
81 if (!Validate(context))
82 {
83 return;
84 }
85
86 MemberInfo member = Resolve(context);
87 Type type = member as Type;
88 MethodInfo method = member as MethodInfo;
89 ConstructorInfo constructor = member as ConstructorInfo;
90 FieldInfo field = member as FieldInfo;
91
92 if (type != null)
93 {
94 ilgen.Emit(OpCodes.Ldtoken, type);
95 }
96 else if (method != null)
97 {
98 ilgen.Emit(OpCodes.Ldtoken, method);
99 }
100 else if (constructor != null)
101 {
102 ilgen.Emit(OpCodes.Ldtoken, constructor);
103 }
104 else if (field != null)
105 {
106 ilgen.Emit(OpCodes.Ldtoken, field);
107 }
108 else
109 {
110 context.ClassLoader.Diagnostics.MapXmlUnableToResolveOpCode(ToString());
111 }
112 }
113
114 private bool Validate(CodeGenContext context)
115 {
116 if (Type != null && Class == null)
117 {
118 if (Method != null || Field != null || Sig != null)
119 {
120 context.ClassLoader.Diagnostics.MapXmlError("not implemented: cannot use 'type' attribute with 'method' or 'field' attribute for ldtoken");
121 return false;
122 }
123 return true;
124 }
125 else if (Class != null && Type == null)
126 {
127 if (Method == null && Field == null)
128 {
129 if (Sig != null)
130 {
131 context.ClassLoader.Diagnostics.MapXmlError("cannot specify 'sig' attribute without either 'method' or 'field' attribute for ldtoken");
132 }
133 return true;
134 }
135 if (Method != null && Field != null)
136 {
137 context.ClassLoader.Diagnostics.MapXmlError("cannot specify both 'method' and 'field' attribute for ldtoken");
138 return false;
139 }
140 return true;
141 }
142 else
143 {
144 context.ClassLoader.Diagnostics.MapXmlError("must specify either 'type' or 'class' attribute for ldtoken");
145 return false;
146 }
147 }
148
149 private MemberInfo Resolve(CodeGenContext context)
150 {
151 if (Type != null)
152 {
153 if (Class != null || Method != null || Field != null || Sig != null)
154 {
155 throw new NotImplementedException();
156 }
157 return context.ClassLoader.Context.StaticCompiler.GetTypeForMapXml(context.ClassLoader, Type);
158 }
159 else if (Class != null)
160 {
161 var tw = context.ClassLoader.TryLoadClassByName(Class);
162 if (tw == null)
163 {
164 return null;
165 }
166 else if (Method != null)
167 {
168 var mw = tw.GetMethod(Method, Sig, false);
169 if (mw == null)
170 {
171 return null;
172 }
173 return mw.GetMethod();
174 }
175 else if (Field != null)
176 {
177 var fw = tw.GetFieldWrapper(Field, Sig);
178 if (fw == null)
179 {
180 return null;
181 }
182 return fw.GetField();
183 }
184 else
185 {
186 return tw.TypeAsBaseType;
187 }
188 }
189 else
190 {
191 return null;
192 }
193 }
194
195 }
196
197}
IKVM.Reflection.ConstructorInfo ConstructorInfo
IKVM.Reflection.FieldInfo FieldInfo
IKVM.Reflection.MethodInfo MethodInfo
global::java.lang.Class Class
virtual IDiagnosticHandler Diagnostics
Gets the IDiagnosticHandler events originated by this class loader should be sent to.
Base class for MapXML instruction instances.
static new Ldtoken Read(XElement element)
Reads the XML element into a new Ldtoken instance.
Definition Ldtoken.cs:47
static void Load(Ldtoken inst, XElement element)
Loads the XML element into the instruction.
Definition Ldtoken.cs:59
IKVM.Runtime.ClassFile.Method.Instruction Instruction
Definition compiler.cs:44
void MapXmlUnableToResolveOpCode(string arg0)
The 'MapXmlUnableToResolveOpCode' diagnostic.