-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathSymbolTable.cs
More file actions
382 lines (331 loc) · 11.6 KB
/
SymbolTable.cs
File metadata and controls
382 lines (331 loc) · 11.6 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Collections.Concurrent;
using CodeGenerationTarget = Xamarin.Android.Binder.CodeGenerationTarget;
namespace MonoDroid.Generation {
public class SymbolTable {
// The symbols dictionary may contain shallow types (types that have not populated Ctors/Methods/Fields).
// If you make any changes to the SymbolTable class that accesses the symbols you need to keep
// that in mind. Also if you add any new public methods that expose symbols from the table you must
// EnsurePopulated them before letting them leave this class.
ConcurrentDictionary<string, List<ISymbol>> symbols = new ConcurrentDictionary<string, List<ISymbol>> ();
ISymbol char_seq;
ISymbol fileinstream_sym;
ISymbol fileoutstream_sym;
ISymbol instream_sym;
ISymbol outstream_sym;
ISymbol xmlpullparser_sym;
ISymbol xmlresourceparser_sym;
ISymbol string_sym;
CodeGenerationTarget target;
static readonly string[] InvariantSymbols = new string[]{
"Android.Graphics.Color",
"boolean",
"byte",
"char",
"double",
"float",
"int",
"long",
"short",
"ubyte",
"uint",
"ulong",
"ushort",
"void",
};
public IEnumerable<ISymbol> AllRegisteredSymbols (CodeGenerationOptions options)
{
if (options.UseShallowReferencedTypes)
throw new InvalidOperationException ("Not safe to retrieve all registered symbols when using shallow types.");
return symbols.Values.SelectMany (v => v);
}
public SymbolTable (CodeGenerationTarget target)
{
this.target = target;
AddType (new SimpleSymbol ("IntPtr.Zero", "void", "void", "V"));
AddType (new SimpleSymbol ("false", "boolean", "bool", "Z"));
AddType (new SimpleSymbol ("0", "byte", "sbyte", "B"));
AddType (new SimpleSymbol ("(char)0", "char", "char", "C"));
AddType (new SimpleSymbol ("0.0", "double", "double", "D"));
AddType (new SimpleSymbol ("0.0F", "float", "float", "F"));
AddType (new SimpleSymbol ("0", "int", "int", "I"));
AddType (new SimpleSymbol ("0L", "long", "long", "J"));
AddType (new SimpleSymbol ("0", "short", "short", "S"));
AddType (new SimpleSymbol ("0", "uint", "uint", "I", returnCast: "(uint)"));
AddType (new SimpleSymbol ("0", "ushort", "ushort", "S", returnCast: "(ushort)"));
AddType (new SimpleSymbol ("0", "ulong", "ulong", "J", returnCast: "(ulong)"));
AddType (new SimpleSymbol ("0", "ubyte", "byte", "B", returnCast: "(byte)"));
char_seq = new CharSequenceSymbol ();
string_sym = new StringSymbol ();
if (target == CodeGenerationTarget.JavaInterop1) {
return;
}
AddType ("Android.Graphics.Color", new ColorSymbol ());
instream_sym = new StreamSymbol ("InputStream");
outstream_sym = new StreamSymbol ("OutputStream");
fileinstream_sym = new StreamSymbol ("FileInputStream", "InputStream");
fileoutstream_sym = new StreamSymbol ("FileOutputStream", "OutputStream");
xmlpullparser_sym = new XmlPullParserSymbol ();
xmlresourceparser_sym = new XmlResourceParserSymbol ();
}
// Extract symbol information
// java_type: "foo.Bar<T1<T2>[]>[]..."
// returns: "foo.Bar"
// type_params: "<T1<T2>[]>"
// arrayRank: 2
// has_ellipsis: true
public string GetSymbolInfo (string java_type, out string type_params, out int arrayRank, out bool has_ellipsis)
{
type_params = string.Empty;
arrayRank = 0;
has_ellipsis = false;
if (string.IsNullOrEmpty (java_type))
return java_type;
var erased = new StringBuilder (java_type.Length);
int ltCount = 0;
for (int i = 0; i < java_type.Length; ++i) {
char c = java_type [i];
switch (c) {
case '<':
ltCount++;
type_params += c;
break;
case '>':
ltCount--;
type_params += c;
break;
case '[':
if (ltCount == 0)
arrayRank++;
goto case ']';
case ']':
if (ltCount > 0)
type_params += c;
break;
default:
if (ltCount == 0)
erased.Append (c);
else
type_params += c;
break;
}
}
has_ellipsis = erased.Length > 3 &&
erased [erased.Length-1] == '.' &&
erased [erased.Length-2] == '.' &&
erased [erased.Length-3] == '.';
if (has_ellipsis) {
arrayRank++;
erased.Length -= 3;
}
return erased.ToString ();
}
public void AddType (ISymbol symbol)
{
string dummy;
int ar;
bool he;
string key = symbol.IsEnum ? symbol.FullName : GetSymbolInfo (symbol.JavaName, out dummy, out ar, out he);
AddType (key, symbol);
}
bool ShouldAddType (string key)
{
if (!symbols.ContainsKey (key))
return true;
if (Array.BinarySearch (InvariantSymbols, key, StringComparer.OrdinalIgnoreCase) >= 0)
return false;
return true;
}
public void AddType (string key, ISymbol symbol)
{
if (!ShouldAddType (key))
return;
symbols.AddOrUpdate (key,
(value) => {
// Key not found, add it to Dictionary
lock (cache_population_lock)
all_symbols_cache = null;
return new List<ISymbol> { symbol };
},
(value, list) => {
// Key already exists, add it to List
if (!list.Any (v => object.ReferenceEquals (v, symbol)))
list.Add (symbol);
return list;
});
}
public ISymbol Lookup (string java_type, GenericParameterDefinitionList in_params)
{
string type_params;
int arrayRank;
bool has_ellipsis;
string key = GetSymbolInfo (java_type, out type_params, out arrayRank, out has_ellipsis);
// FIXME: we should make sure to differentiate those ref types IF we use those modifiers in the future.
switch (key [key.Length - 1]) {
case '&': // managed ref type
case '*': // managed (well, unmanaged...) pointer type
key = key.Substring (0, key.Length - 1);
break;
}
key = TypeNameUtilities.FilterPrimitiveFullName (key) ?? key;
if (target == CodeGenerationTarget.JavaInterop1) {
if (key == "java.lang.CharSequence") {
return CreateArray (char_seq, arrayRank, has_ellipsis);
}
if (key == "java.lang.String") {
return CreateArray (string_sym, arrayRank, has_ellipsis);
}
}
switch (this.target != CodeGenerationTarget.JavaInterop1 ? key : null) {
case "android.content.res.XmlResourceParser":
return CreateArray (xmlresourceparser_sym, arrayRank, has_ellipsis);
case "org.xmlpull.v1.XmlPullParser":
return CreateArray (xmlpullparser_sym, arrayRank, has_ellipsis);
case "java.io.FileInputStream":
return CreateArray (fileinstream_sym, arrayRank, has_ellipsis);
case "java.io.FileOutputStream":
return CreateArray (fileoutstream_sym, arrayRank, has_ellipsis);
case "java.io.InputStream":
return CreateArray (instream_sym, arrayRank, has_ellipsis);
case "java.io.OutputStream":
return CreateArray (outstream_sym, arrayRank, has_ellipsis);
case "java.lang.CharSequence":
return CreateArray (char_seq, arrayRank, has_ellipsis);
case "java.lang.String":
return CreateArray (string_sym, arrayRank, has_ellipsis);
case "java.util.List":
case "java.util.ArrayList":
case "System.Collections.IList":
return CreateArray (new CollectionSymbol (key, "IList", "Android.Runtime.JavaList", type_params), arrayRank, has_ellipsis);
case "java.util.Map":
case "java.util.HashMap":
case "java.util.SortedMap":
case "System.Collections.IDictionary":
return CreateArray (new CollectionSymbol (key, "IDictionary", "Android.Runtime.JavaDictionary", type_params), arrayRank, has_ellipsis);
case "java.util.Set":
return CreateArray (new CollectionSymbol (key, "ICollection", "Android.Runtime.JavaSet", type_params), arrayRank, has_ellipsis);
case "java.util.Collection":
case "System.Collections.ICollection":
return CreateArray (new CollectionSymbol (key, "ICollection", "Android.Runtime.JavaCollection", type_params), arrayRank, has_ellipsis);
default:
break;
}
ISymbol result;
var gpd = in_params != null ? in_params.FirstOrDefault (t => t.Name == key) : null;
if (gpd != null) {
result = new GenericTypeParameter (gpd);
}
else
result = Lookup (key + type_params);
return CreateArray (result, arrayRank, has_ellipsis);
}
ISymbol CreateArray (ISymbol symbol, int rank, bool has_ellipsis)
{
if (symbol == null)
return null;
ArraySymbol r = null;
while (rank-- > 0)
symbol = r = new ArraySymbol (symbol, target);
if (r != null)
r.IsParams = has_ellipsis;
return symbol;
}
ConcurrentDictionary<string, ISymbol> all_symbols_cache;
static object cache_population_lock = new object ();
public ISymbol Lookup (string java_type)
{
string type_params;
int ar;
bool he;
string key = GetSymbolInfo (java_type, out type_params, out ar, out he);
ISymbol sym;
List<ISymbol> values;
if (symbols.TryGetValue (key, out values)) {
sym = values [values.Count-1];
} else {
// Note we're potentially searching shallow types, but this is only looking at the type name
// Anything we find we will populate before returning to the user
lock (cache_population_lock) {
if (all_symbols_cache == null)
all_symbols_cache = new ConcurrentDictionary<string, ISymbol> (symbols.Values.SelectMany (v => v).GroupBy (s => s.FullName).ToDictionary (s => s.Key, s => s.FirstOrDefault ()));
if (!all_symbols_cache.TryGetValue (key, out sym)) {
// We may be looking for a type like:
// - System.Collections.Generic.IList<Java.Util.Locale.LanguageRange>
// Our key is "System.Collections.Generic.IList", but it's stored in
// the symbol table with the arity so we need to look for
// "System.Collections.Generic.IList`1" to find a match
key = AddArity (key, type_params);
all_symbols_cache.TryGetValue (key, out sym);
}
}
}
ISymbol result;
if (sym != null) {
if (type_params.Length > 0) {
GenBase gen = sym as GenBase;
EnsurePopulated (gen);
if (gen != null && gen.IsGeneric)
result = new GenericSymbol (gen, type_params);
// In other case, it is still valid to derive from non-generic type.
// Generics are likely removed but we should not invalidate such derived classes.
else
result = gen;
}
// disable this condition; consider that "java.lang.Class[]" can be specified as a parameter type
//else if (sym is GenBase && (sym as GenBase).IsGeneric)
// return null;
else
result = sym;
} else
return null;
if (result is GenBase gen_base)
EnsurePopulated (gen_base);
return result;
}
private string AddArity (string key, string typeParams)
{
if (string.IsNullOrWhiteSpace (typeParams) || !typeParams.StartsWith ("<", StringComparison.Ordinal) || !typeParams.EndsWith (">", StringComparison.Ordinal))
return key;
var nested_count = 0;
var arity = 1;
// Remove the outer <>
typeParams = typeParams.Substring (1, typeParams.Length - 2);
foreach (var c in typeParams) {
if (c == '>')
nested_count--;
if (c == '<')
nested_count++;
if (nested_count == 0 && c == ',')
arity++;
}
return $"{key}`{arity}";
}
public void Dump ()
{
foreach (var p in symbols) {
if (p.Key.StartsWith ("System", StringComparison.Ordinal))
continue;
foreach (var s in p.Value) {
Console.Error.WriteLine ("[{0}]: {1} {2}", p.Key, s.GetType (), s.FullName);
}
}
}
static readonly object populate_lock = new object ();
void EnsurePopulated (GenBase gen)
{
if (gen == null || !gen.IsShallow)
return;
foreach (var nested in gen.NestedTypes)
EnsurePopulated (nested);
// We need to fully populate this shallow type
lock (populate_lock) {
gen.PopulateAction ();
gen.IsShallow = false;
gen.PopulateAction = null;
}
}
}
}