forked from dotnet/java-interop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenericParameterDefinition.cs
125 lines (110 loc) · 3.44 KB
/
GenericParameterDefinition.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Mono.Cecil;
using Xamarin.Android.Tools;
namespace MonoDroid.Generation
{
// Represents a generic parameter definition in GenBase.
public class GenericParameterDefinition
{
public GenericParameterDefinition (string name, string [] constraints)
{
Name = name;
ConstraintExpressions = constraints;
}
public string Name { get; set; }
public string [] ConstraintExpressions { get; private set; }
// post-validation information.
public ISymbol [] Constraints { get; private set; }
bool validated, is_valid;
public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params)
{
if (ConstraintExpressions == null || ConstraintExpressions.Length == 0)
return true;
if (validated)
return is_valid;
var syms = new List<ISymbol> ();
foreach (var c in ConstraintExpressions) {
var sym = SymbolTable.Lookup (c, type_params);
if (sym == null) {
Report.Warning (0, Report.WarningGenericParameterDefinition + 0, "Unknown generic argument constraint type {0} {1}.", c, opt.ContextString);
validated = true;
return false;
}
syms.Add (sym);
}
Constraints = syms.ToArray ();
validated = is_valid = true;
return true;
}
}
public class GenericParameterDefinitionList : List<GenericParameterDefinition>
{
public static GenericParameterDefinitionList Merge (GenericParameterDefinitionList l1, GenericParameterDefinitionList l2)
{
if (l1 == null)
return l2;
if (l2 == null)
return l1;
var ret = new GenericParameterDefinitionList ();
ret.AddRange (l1);
ret.AddRange (l2);
return ret;
}
public static GenericParameterDefinitionList FromMetadata (IEnumerable<GenericParameter> types)
{
GenericParameterDefinitionList ret = null;
foreach (var p in types) {
ret = ret ?? new GenericParameterDefinitionList ();
ret.Add (new GenericParameterDefinition (
p.FullNameCorrected (),
(from a in p.Constraints
select a.FullNameCorrected ()).ToArray ()));
}
return ret;
}
public static GenericParameterDefinitionList FromXml (XmlNode tps)
{
var ret = new GenericParameterDefinitionList ();
var tpl = tps.SelectNodes ("typeParameter");
foreach (XmlElement n in tpl) {
var csts = new List<string> ();
foreach (XmlElement x in n.SelectNodes ("genericConstraints/genericConstraint"))
csts.Add (x.XGetAttribute ("type"));
ret.Add (new GenericParameterDefinition (n.XGetAttribute ("name"), csts.ToArray ()));
}
return ret;
}
public string ToGeneratedAttributeString ()
{
var typeArgList = this.Select (t => t.Name + (t.ConstraintExpressions.Any () ? " extends " + string.Join (" & ", t.ConstraintExpressions) : null));
return "[global::Java.Interop.JavaTypeParameters (new string [] {\"" +
string.Join ("\", \"", typeArgList) + "\"})]";
}
public int IndexOf (string name)
{
int i = 0;
foreach (var e in this) {
if (e.Name == name)
return i;
else
i++;
}
return -1;
}
public string GetSignature ()
{
return Count == 0 ? null : '<' + String.Join (",", (from p in this select p.Name).ToArray ()) + '>';
}
public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params)
{
foreach (var pd in this)
if (!pd.Validate (opt, type_params))
return false;
return true;
}
}
}