Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions tools/generator/ClassGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,7 @@
using System.Xml.Linq;

namespace MonoDroid.Generation {
#if HAVE_CECIL
static class ManagedExtensions
{
public static string FullNameCorrected (this TypeReference t)
{
return t.FullName.Replace ('/', '.');
}
}

#if HAVE_CECIL
public class ManagedClassGen : ClassGen {
TypeDefinition t;
TypeReference nominal_base_type;
Expand Down Expand Up @@ -352,9 +344,9 @@ void GenMethods (StreamWriter sw, string indent, CodeGenerationOptions opt)
bool virt = m.IsVirtual;
m.IsVirtual = !IsFinal && virt;
if (m.IsAbstract && !m.IsInterfaceDefaultMethodOverride && !m.IsInterfaceDefaultMethod)
m.GenerateAbstractDeclaration (sw, indent, opt, null, this);
opt.CodeGenerator.WriteMethodAbstractDeclaration (m, sw, indent, opt, null, this);
else
m.Generate (sw, indent, opt, this, true);
opt.CodeGenerator.WriteMethod (m, sw, indent, opt, this, true);
opt.ContextGeneratedMethods.Add (m);
m.IsVirtual = virt;
}
Expand Down Expand Up @@ -497,7 +489,7 @@ public override void Generate (StreamWriter sw, string indent, CodeGenerationOpt
if (m.IsInterfaceDefaultMethod || m.IsStatic)
continue;
if (m.IsGeneric)
m.GenerateExplicitIface (sw, indent + "\t", opt, gs);
opt.CodeGenerator.WriteMethodExplicitIface (m, sw, indent + "\t", opt, gs);
}

var adapter = gs.Gen.AssemblyQualifiedName + "Invoker";
Expand Down Expand Up @@ -631,11 +623,11 @@ void GenerateInvoker (StreamWriter sw, IEnumerable<Method> methods, string inden
continue;
if (IsExplicitlyImplementedMethod (sig)) {
// sw.WriteLine ("// This invoker explicitly implements this method");
m.GenerateExplicitInterfaceInvoker (sw, indent, opt, gen);
opt.CodeGenerator.WriteMethodExplicitInterfaceInvoker (m, sw, indent, opt, gen);
} else {
// sw.WriteLine ("// This invoker overrides {0} method", gen.FullName);
m.IsOverride = true;
m.Generate (sw, indent, opt, this, false);
opt.CodeGenerator.WriteMethod (m, sw, indent, opt, this, false);
m.IsOverride = false;
}
}
Expand Down
343 changes: 343 additions & 0 deletions tools/generator/CodeGenerator.cs

Large diffs are not rendered by default.

29 changes: 19 additions & 10 deletions tools/generator/Ctor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@ public class ManagedCtor : Ctor {
bool is_acw;

public ManagedCtor (GenBase declaringType, MethodDefinition m)
: this (declaringType, m, new ManagedMethodBaseSupport (m))
{
}

ManagedCtor (GenBase declaringType, MethodDefinition m, ManagedMethodBaseSupport support)
: base (declaringType, support)
: base (declaringType)
{
this.m = m;
GenericArguments = m.GenericArguments ();
name = m.Name;
// If 'elem' is a constructor for a non-static nested type, then
// the type of the containing class must be inserted as the first
Expand All @@ -31,7 +27,7 @@ public ManagedCtor (GenBase declaringType, MethodDefinition m)
Parameters.AddFirst (Parameter.FromManagedType (m.DeclaringType.DeclaringType, DeclaringType.JavaName));
var regatt = m.CustomAttributes.FirstOrDefault (a => a.AttributeType.FullName == "Android.Runtime.RegisterAttribute");
is_acw = regatt != null;
foreach (var p in support.GetParameters (regatt))
foreach (var p in m.GetParameters (regatt))
Parameters.Add (p);
}

Expand All @@ -52,17 +48,26 @@ public override string Name {
public override string CustomAttributes {
get { return null; }
}

public override string AssemblyName => m.DeclaringType.Module.Assembly.FullName;

public override string Visibility => m.Visibility ();

public override string Deprecated => m.Deprecated ();
}
#endif // HAVE_CECIL

public class XmlCtor : Ctor {
XElement elem;
string name;
bool nonStaticNestedType;
bool missing_enclosing_class;
string custom_attributes;

public XmlCtor (GenBase declaringType, XElement elem) : base (declaringType, new XmlMethodBaseSupport (elem))
public XmlCtor (GenBase declaringType, XElement elem) : base (declaringType)
{
this.elem = elem;
GenericArguments = elem.GenericArguments ();
name = elem.XGetAttribute ("name");
int idx = name.LastIndexOf ('.');
if (idx > 0)
Expand Down Expand Up @@ -125,12 +130,16 @@ protected override bool OnValidate (CodeGenerationOptions opt, GenericParameterD
public override string CustomAttributes {
get { return custom_attributes; }
}

public override string Deprecated => elem.Deprecated ();

public override string Visibility => elem.Visibility ();
}

public abstract class Ctor : MethodBase {

protected Ctor (GenBase declaringType, IMethodBaseSupport support)
: base (declaringType, support)
protected Ctor (GenBase declaringType)
: base (declaringType)
{
}

Expand Down
14 changes: 7 additions & 7 deletions tools/generator/InterfaceGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,15 @@ void GenMethods (StreamWriter sw, string indent, CodeGenerationOptions opt)
foreach (Method m in Methods.Where (m => !m.IsStatic)) {
if (m.Name == Name || ContainsProperty (m.Name, true))
m.Name = "Invoke" + m.Name;
m.GenerateDeclaration (sw, indent, opt, this, AssemblyQualifiedName + "Invoker");
opt.CodeGenerator.WriteMethodDeclaration (m, sw, indent, opt, this, AssemblyQualifiedName + "Invoker");
}
}

void GenExtensionMethods (StreamWriter sw, string indent, CodeGenerationOptions opt)
{
foreach (Method m in Methods.Where (m => !m.IsStatic)) {
m.GenerateExtensionOverload (sw, indent, opt, FullName);
m.GenerateExtensionAsyncWrapper (sw, indent, opt, FullName);
opt.CodeGenerator.WriteMethodExtensionOverload (m, sw, indent, opt, FullName);
opt.CodeGenerator.WriteMethodExtensionAsyncWrapper (m, sw, indent, opt, FullName);
}
}

Expand Down Expand Up @@ -288,7 +288,7 @@ void GenerateInvoker (StreamWriter sw, IEnumerable<Method> methods, string inden
if (members.Contains (sig))
continue;
members.Add (sig);
m.GenerateInvoker (sw, indent, opt, this);
opt.CodeGenerator.WriteMethodInvoker (m, sw, indent, opt, this);
}
}

Expand Down Expand Up @@ -632,9 +632,9 @@ public void GenerateAbstractMembers (ClassGen gen, StreamWriter sw, string inden
if (mapped)
continue;
if (gen.ExplicitlyImplementedInterfaceMethods.Contains (sig))
m.GenerateExplicitInterfaceImplementation (sw, indent, opt, this);
opt.CodeGenerator.WriteMethodExplicitInterfaceImplementation (m, sw, indent, opt, this);
else
m.GenerateAbstractDeclaration (sw, indent, opt, this, gen);
opt.CodeGenerator.WriteMethodAbstractDeclaration (m, sw, indent, opt, this, gen);
opt.ContextGeneratedMethods.Add (m);
}
foreach (Property prop in Properties.Where (p => !p.Getter.IsStatic)) {
Expand Down Expand Up @@ -714,7 +714,7 @@ public override void Generate (StreamWriter sw, string indent, CodeGenerationOpt
}

foreach (var m in Methods.Where (m => m.IsStatic))
m.Generate (sw, indent + "\t", opt, this, true);
opt.CodeGenerator.WriteMethod (m, sw, indent + "\t", opt, this, true);

if (needsClassRef) {
sw.WriteLine ();
Expand Down
50 changes: 50 additions & 0 deletions tools/generator/ManagedExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Java.Interop.Tools.TypeNameMappings;
using Mono.Cecil;
using System.Collections.Generic;
using System.Linq;

namespace MonoDroid.Generation
{
#if HAVE_CECIL
internal static class ManagedExtensions
{
public static string FullNameCorrected (this TypeReference t) => t.FullName.Replace ('/', '.');

public static GenericParameterDefinitionList GenericArguments (this MethodDefinition m) =>
m.HasGenericParameters ? GenericParameterDefinitionList.FromMetadata (m.GenericParameters) : null;

public static string Deprecated (this MethodDefinition m)
{
var v = m.CustomAttributes.FirstOrDefault (a => a.AttributeType.FullName == "System.ObsoleteAttribute");
return v != null ? (string)v.ConstructorArguments [0].Value ?? "deprecated" : null;
}

public static string Visibility (this MethodDefinition m) =>
m.IsPublic ? "public" : m.IsFamilyOrAssembly ? "protected internal" : m.IsFamily ? "protected" : m.IsAssembly ? "internal" : "private";

public static IEnumerable<Parameter> GetParameters (this MethodDefinition m, CustomAttribute regatt)
{
var jnisig = (string)(regatt.ConstructorArguments.Count > 1 ? regatt.ConstructorArguments [1].Value : regatt.Properties.First (p => p.Name == "JniSignature").Argument.Value);
var types = jnisig == null ? null : JavaNativeTypeManager.FromSignature (jnisig);
var e = types?.GetEnumerator ();

foreach (var p in m.Parameters) {
if (e != null && !e.MoveNext ())
e = null;
// Here we do some tricky thing:
// Both java.io.InputStream and java.io.OutputStream could be mapped to
// System.IO.Stream. And when there is Stream in parameters, we have to
// determine which direction of the Stream it was - in or out.
// To do that, we inspect JNI Signature to handle that.
//
// We could *always* use this JNI information, *IF* there were no
// int->enum conversion. Sadly this is not true, we still have to expect
// custom enum types and cannot simply use JNI signature here.
var rawtype = e?.Current.Type;
var type = p.ParameterType.FullName == "System.IO.Stream" && e != null ? e.Current.Type : null;
yield return Parameter.FromManagedParameter (p, type, rawtype);
}
}
}
#endif
}
Loading