Skip to content

Commit 84ccb6d

Browse files
committed
Add Kotlin support to our binding process.
1 parent 73096d9 commit 84ccb6d

File tree

9 files changed

+614
-57
lines changed

9 files changed

+614
-57
lines changed

src/Xamarin.Android.Tools.Bytecode/ClassPath.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
44
using System.IO;
@@ -8,7 +8,6 @@
88
using System.Xml.Linq;
99
using System.Xml.XPath;
1010
using System.Text;
11-
using Xamarin.Android.Tools.Bytecode.Kotlin;
1211

1312
namespace Xamarin.Android.Tools.Bytecode {
1413

src/Xamarin.Android.Tools.Bytecode/Kotlin/KotlinClassMetadata.cs

Lines changed: 176 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Text;
45
using org.jetbrains.kotlin.metadata.jvm;
56
using Type = org.jetbrains.kotlin.metadata.jvm.Type;
67

@@ -48,23 +49,34 @@ internal static KotlinClass FromProtobuf (Class c, JvmNameResolver resolver)
4849
}
4950
}
5051

51-
public class KotlinConstructor
52+
public class KotlinMethodBase
5253
{
53-
public int Flags { get; set; }
54-
public List<KotlinValueParameter> ValueParameters { get; set; }
5554
public int [] VersionRequirements { get; set; }
5655

56+
public virtual string GetSignature () => string.Empty;
57+
}
58+
59+
public class KotlinConstructor : KotlinMethodBase
60+
{
61+
public KotlinConstructorFlags Flags { get; set; }
62+
public List<KotlinValueParameter> ValueParameters { get; set; }
63+
5764
internal static KotlinConstructor FromProtobuf (Constructor c, JvmNameResolver resolver)
5865
{
5966
if (c is null)
6067
return null;
6168

6269
return new KotlinConstructor {
63-
Flags = c.Flags,
70+
Flags = (KotlinConstructorFlags)c.Flags,
6471
ValueParameters = c.ValueParameters?.Select (vp => KotlinValueParameter.FromProtobuf (vp, resolver)).ToList (),
6572
VersionRequirements = c.VersionRequirements
6673
};
6774
}
75+
76+
public override string GetSignature ()
77+
{
78+
return $"({ValueParameters.GetSignature ()})V";
79+
}
6880
}
6981

7082
public class KotlinAnnotation
@@ -113,7 +125,7 @@ public class KotlinAnnotationArgumentValue
113125
public KotlinAnnotation Annotation { get; set; }
114126
public List<KotlinAnnotationArgumentValue> ArrayElements { get; set; }
115127
public int ArrayDimensionCount { get; set; }
116-
public int Flags { get; set; }
128+
public KotlinAnnotationFlags Flags { get; set; }
117129

118130
internal static KotlinAnnotationArgumentValue FromProtobuf (org.jetbrains.kotlin.metadata.jvm.Annotation.Argument.Value value, JvmNameResolver resolver)
119131
{
@@ -131,7 +143,7 @@ internal static KotlinAnnotationArgumentValue FromProtobuf (org.jetbrains.kotlin
131143
Annotation = KotlinAnnotation.FromProtobuf (value.Annotation, resolver),
132144
ArrayDimensionCount = value.ArrayDimensionCount,
133145
ArrayElements = value.ArrayElements?.Select (vp => KotlinAnnotationArgumentValue.FromProtobuf (vp, resolver)).ToList (),
134-
Flags = value.Flags
146+
Flags = (KotlinAnnotationFlags)value.Flags
135147
};
136148
}
137149
}
@@ -159,7 +171,7 @@ internal static KotlinEffect FromProtobuf (Effect ef, JvmNameResolver resolver)
159171

160172
public class KotlinExpression
161173
{
162-
public int Flags { get; set; }
174+
public KotlinExpressionFlags Flags { get; set; }
163175
public int ValueParameterReference { get; set; }
164176
public KotlinConstantValue ConstantValue { get; set; }
165177
public KotlinType IsInstanceType { get; set; }
@@ -173,7 +185,7 @@ internal static KotlinExpression FromProtobuf (Expression exp, JvmNameResolver r
173185
return null;
174186

175187
return new KotlinExpression {
176-
Flags = exp.Flags,
188+
Flags = (KotlinExpressionFlags)exp.Flags,
177189
ValueParameterReference = exp.ValueParameterReference,
178190
ConstantValue = (KotlinConstantValue) exp.constant_value,
179191
IsInstanceType = KotlinType.FromProtobuf (exp.IsInstanceType, resolver),
@@ -184,27 +196,26 @@ internal static KotlinExpression FromProtobuf (Expression exp, JvmNameResolver r
184196
}
185197
}
186198

187-
public class KotlinFunction
199+
public class KotlinFunction : KotlinMethodBase
188200
{
189-
public int Flags { get; set; }
190201
public string Name { get; set; }
202+
public KotlinFunctionFlags Flags { get; set; }
191203
public KotlinType ReturnType { get; set; }
192204
public int ReturnTypeId { get; set; }
193205
public List<KotlinTypeParameter> TypeParameters { get; set; }
194206
public KotlinType ReceiverType { get; set; }
195207
public int ReceiverTypeId { get; set; }
196-
public List<KotlinValueParameter> ValueParameters { get; set; }
197208
public KotlinTypeTable TypeTable { get; set; }
198-
public int [] VersionRequirements { get; set; }
199209
public KotlinContract Contract { get; set; }
210+
public List<KotlinValueParameter> ValueParameters { get; set; }
200211

201212
internal static KotlinFunction FromProtobuf (Function f, JvmNameResolver resolver)
202213
{
203214
if (f is null)
204215
return null;
205216

206217
return new KotlinFunction {
207-
Flags = f.Flags,
218+
Flags = (KotlinFunctionFlags)f.Flags,
208219
Name = resolver.GetString (f.Name),
209220
ReturnType = KotlinType.FromProtobuf (f.ReturnType, resolver),
210221
ReturnTypeId = f.ReturnTypeId,
@@ -215,6 +226,8 @@ internal static KotlinFunction FromProtobuf (Function f, JvmNameResolver resolve
215226
VersionRequirements = f.VersionRequirements
216227
};
217228
}
229+
230+
public override string ToString () => Name;
218231
}
219232

220233
public class KotlinContract
@@ -229,10 +242,10 @@ internal static KotlinContract FromProtobuf (Contract c, JvmNameResolver resolve
229242
}
230243
}
231244

232-
public class KotlinProperty
245+
public class KotlinProperty : KotlinMethodBase
233246
{
234-
public int Flags { get; set; }
235247
public string Name { get; set; }
248+
public KotlinPropertyFlags Flags { get; set; }
236249
public KotlinType ReturnType { get; set; }
237250
public int ReturnTypeId { get; set; }
238251
public List<KotlinTypeParameter> TypeParameters { get; set; }
@@ -241,15 +254,14 @@ public class KotlinProperty
241254
public KotlinValueParameter SetterValueParameter { get; set; }
242255
public int GetterFlags { get; set; }
243256
public int SetterFlags { get; set; }
244-
public int [] VersionRequirements { get; set; }
245257

246258
internal static KotlinProperty FromProtobuf (Property p, JvmNameResolver resolver)
247259
{
248260
if (p is null)
249261
return null;
250262

251263
return new KotlinProperty {
252-
Flags = p.Flags,
264+
Flags = (KotlinPropertyFlags)p.Flags,
253265
Name = resolver.GetString (p.Name),
254266
ReturnTypeId = p.ReturnTypeId,
255267
ReturnType = KotlinType.FromProtobuf (p.ReturnType, resolver),
@@ -268,18 +280,18 @@ public class KotlinType
268280
{
269281
public List<KotlinTypeArgument> Arguments { get; set; }
270282
public bool Nullable { get; set; }
271-
public int FlexibleTypeCapabilitiesId { get; set; }
283+
public int? FlexibleTypeCapabilitiesId { get; set; }
272284
public KotlinType FlexibleUpperBound { get; set; }
273285
public int FlexibleUpperBoundId { get; set; }
274286
public string ClassName { get; set; }
275-
public int TypeParameter { get; set; }
287+
public int? TypeParameter { get; set; }
276288
public string TypeParameterName { get; set; }
277289
public string TypeAliasName { get; set; }
278290
public KotlinType OuterType { get; set; }
279-
public int OuterTypeId { get; set; }
291+
public int? OuterTypeId { get; set; }
280292
public KotlinType AbbreviatedType { get; set; }
281-
public int AbbreviatedTypeId { get; set; }
282-
public int Flags { get; set; }
293+
public int? AbbreviatedTypeId { get; set; }
294+
public KotlinTypeFlags Flags { get; set; }
283295

284296
internal static KotlinType FromProtobuf (Type t, JvmNameResolver resolver)
285297
{
@@ -291,16 +303,21 @@ internal static KotlinType FromProtobuf (Type t, JvmNameResolver resolver)
291303
Nullable = t.Nullable,
292304
FlexibleTypeCapabilitiesId = t.FlexibleTypeCapabilitiesId,
293305
FlexibleUpperBound = FromProtobuf (t.FlexibleUpperBound, resolver),
294-
ClassName = t.ClassName > 0 ? resolver.GetString (t.ClassName) : null,
306+
ClassName = t.ClassName >= 0 ? resolver.GetString (t.ClassName.Value) : null,
295307
TypeParameter = t.TypeParameter,
296-
TypeParameterName = t.TypeParameterName > 0 ? resolver.GetString (t.TypeParameterName) : null,
308+
TypeParameterName = t.TypeParameterName >= 0 ? resolver.GetString (t.TypeParameterName.GetValueOrDefault ()) : null,
297309
OuterType = FromProtobuf (t.OuterType, resolver),
298310
OuterTypeId = t.OuterTypeId,
299311
AbbreviatedType = FromProtobuf (t.AbbreviatedType, resolver),
300312
AbbreviatedTypeId = t.AbbreviatedTypeId,
301-
Flags = t.Flags
313+
Flags = (KotlinTypeFlags)t.Flags
302314
};
303315
}
316+
317+
public string GetSignature ()
318+
{
319+
return KotlinUtilities.ConvertKotlinTypeSignature (this);
320+
}
304321
}
305322

306323
public class KotlinTypeAlias
@@ -436,7 +453,7 @@ internal static KotlinVersionRequirementTable FromProtobuf (VersionRequirementTa
436453

437454
public class KotlinValueParameter
438455
{
439-
public int Flags { get; set; }
456+
public KotlinParameterFlags Flags { get; set; }
440457
public string Name { get; set; }
441458
public KotlinType Type { get; set; }
442459
public int TypeId { get; set; }
@@ -449,14 +466,16 @@ internal static KotlinValueParameter FromProtobuf (ValueParameter vp, JvmNameRes
449466
return null;
450467

451468
return new KotlinValueParameter {
452-
Flags = vp.Flags,
469+
Flags = (KotlinParameterFlags)vp.Flags,
453470
Name = resolver.GetString (vp.Name),
454471
Type = KotlinType.FromProtobuf (vp.Type, resolver),
455472
TypeId = vp.TypeId,
456473
VarArgElementType = KotlinType.FromProtobuf (vp.VarargElementType, resolver),
457474
VarArgElementTypeId = vp.VarargElementTypeId
458475
};
459476
}
477+
478+
public string GetSignature () => Type.GetSignature ();
460479
}
461480

462481
public enum KotlinVariance
@@ -557,4 +576,134 @@ public enum KotlinClassFlags
557576
IsExpectClass = 0b_01000_000_00_000_0,
558577
IsInlineClass = 0b_10000_000_00_000_0
559578
}
579+
580+
[Flags]
581+
public enum KotlinConstructorFlags
582+
{
583+
HasAnnotations = 0b0_000_1,
584+
585+
Internal = 0b0_000_0,
586+
Private = 0b0_001_0,
587+
Protected = 0b0_010_0,
588+
Public = 0b0_011_0,
589+
PrivateToThis = 0b0_100_0,
590+
Local = 0b0_101_0,
591+
592+
IsSecondary = 0b1_000_0
593+
}
594+
595+
[Flags]
596+
public enum KotlinFunctionFlags
597+
{
598+
HasAnnotations = 0b00_00_000_1,
599+
600+
Internal = 0b00_00_000_0,
601+
Private = 0b00_00_001_0,
602+
Protected = 0b00_00_010_0,
603+
Public = 0b00_00_011_0,
604+
PrivateToThis = 0b00_00_100_0,
605+
Local = 0b00_00_101_0,
606+
607+
Final = 0b00_00_000_0,
608+
Open = 0b00_01_000_0,
609+
Abstract = 0b00_10_000_0,
610+
Sealed = 0b00_11_000_0,
611+
612+
Declaration = 0b00_00_000_0,
613+
FakeOverride = 0b01_00_000_0,
614+
Delegation = 0b10_00_000_0,
615+
Synthesized = 0b11_00_000_0,
616+
617+
IsOperator = 0b_0000001_000_00_000_0,
618+
IsInfix = 0b_0000010_000_00_000_0,
619+
IsInline = 0b_0000100_000_00_000_0,
620+
IsTailrec = 0b_0001000_000_00_000_0,
621+
IsExternalFunction = 0b_0010000_000_00_000_0,
622+
IsSuspend = 0b_0100000_000_00_000_0,
623+
IsExpectFunction = 0b_1000000_000_00_000_0
624+
}
625+
626+
[Flags]
627+
public enum KotlinPropertyFlags
628+
{
629+
HasAnnotations = 0b00_00_000_1,
630+
631+
Internal = 0b00_00_000_0,
632+
Private = 0b00_00_001_0,
633+
Protected = 0b00_00_010_0,
634+
Public = 0b00_00_011_0,
635+
PrivateToThis = 0b00_00_100_0,
636+
Local = 0b00_00_101_0,
637+
638+
Final = 0b00_00_000_0,
639+
Open = 0b00_01_000_0,
640+
Abstract = 0b00_10_000_0,
641+
Sealed = 0b00_11_000_0,
642+
643+
Declaration = 0b00_00_000_0,
644+
FakeOverride = 0b01_00_000_0,
645+
Delegation = 0b10_00_000_0,
646+
Synthesized = 0b11_00_000_0,
647+
648+
IsVar = 0b_000000001_000_00_000_0,
649+
HasGetter = 0b_000000010_000_00_000_0,
650+
HasSetter = 0b_000000100_000_00_000_0,
651+
IsConst = 0b_000001000_000_00_000_0,
652+
IsLateInit = 0b_000010000_000_00_000_0,
653+
HasConstant = 0b_000100000_000_00_000_0,
654+
IsExternalProperty = 0b_001000000_000_00_000_0,
655+
IsDelegated = 0b_010000000_000_00_000_0,
656+
IsExpectProperty = 0b_100000000_000_00_000_0
657+
}
658+
659+
[Flags]
660+
public enum KotlinParameterFlags
661+
{
662+
HasAnnotations = 0b000_1,
663+
664+
DeclaresDefaultValue = 0b001_0,
665+
IsCrossInline = 0b010_0,
666+
IsNoInline = 0b100_0
667+
}
668+
669+
[Flags]
670+
public enum KotlinAccessorFlags
671+
{
672+
HasAnnotations = 0b00_00_000_1,
673+
674+
Internal = 0b00_00_000_0,
675+
Private = 0b00_00_001_0,
676+
Protected = 0b00_00_010_0,
677+
Public = 0b00_00_011_0,
678+
PrivateToThis = 0b00_00_100_0,
679+
Local = 0b00_00_101_0,
680+
681+
Final = 0b00_00_000_0,
682+
Open = 0b00_01_000_0,
683+
Abstract = 0b00_10_000_0,
684+
Sealed = 0b00_11_000_0,
685+
686+
IsNotDefault = 0b001_00_000_0,
687+
IsExternalAccessor = 0b010_00_000_0,
688+
IsInlineAccessor = 0b100_00_000_0
689+
}
690+
691+
[Flags]
692+
public enum KotlinExpressionFlags
693+
{
694+
IsNegated = 0b01,
695+
IsNullCheckPredicate = 0b10
696+
}
697+
698+
[Flags]
699+
public enum KotlinAnnotationFlags
700+
{
701+
IsUnsigned = 0b01
702+
}
703+
704+
[Flags]
705+
public enum KotlinTypeFlags
706+
{
707+
SuspendType = 0b01
708+
}
560709
}

0 commit comments

Comments
 (0)