1
0
mirror of https://github.com/twiglet/cs2j.git synced 2025-01-18 13:15:17 +01:00

Add delegates

This commit is contained in:
Kevin Glynn 2010-10-21 16:31:09 +02:00
parent 948f702ad1
commit 916eca5eac
3 changed files with 62 additions and 22 deletions
CSharpTranslator/antlr3
cs2jTest/TestDLLs
src
cs2j/CLR
cs2jTemplateGen

@ -1,12 +1,18 @@
using System; using System;
namespace cs2jTest.Various.Features namespace cs2jTest.Various.Features
{ {
// delegate declaration
public delegate string MyDelegate(int i);
public class Various public class Various
{ {
public Various () public Various ()
{ {
} }
private int myInt;
public string TestRWProperty {get; set;} public string TestRWProperty {get; set;}
public string get_TestRWProperty() { public string get_TestRWProperty() {

@ -214,6 +214,21 @@ namespace RusticiSoftware.Translator.CLR
Java = java; Java = java;
} }
protected string mkJavaParams(IList<ParamRepTemplate> pars) {
StringBuilder parStr = new StringBuilder();
parStr.Append("(");
foreach (ParamRepTemplate p in pars) {
parStr.Append("${"+p.Name+"},");
}
if (parStr[parStr.Length-1] == ',') {
parStr.Remove(parStr.Length-1,1);
}
parStr.Append(")");
return parStr.ToString();
}
#region Equality #region Equality
public bool Equals (TranslationBase other) public bool Equals (TranslationBase other)
@ -281,25 +296,12 @@ namespace RusticiSoftware.Translator.CLR
} }
} }
protected string mkJavaParams() {
StringBuilder parStr = new StringBuilder();
parStr.Append("(");
foreach (ParamRepTemplate p in Params) {
parStr.Append("${"+p.Name+"},");
}
if (parStr[parStr.Length-1] == ',') {
parStr.Remove(parStr.Length-1,1);
}
parStr.Append(")");
return parStr.ToString();
}
public override string mkJava() { public override string mkJava() {
string constructorName = "CONSTRUCTOR"; string constructorName = "CONSTRUCTOR";
if (SurroundingTypeName != null) { if (SurroundingTypeName != null) {
constructorName = SurroundingTypeName.Substring(SurroundingTypeName.LastIndexOf('.') + 1); constructorName = SurroundingTypeName.Substring(SurroundingTypeName.LastIndexOf('.') + 1);
} }
return "new " + constructorName + mkJavaParams(); return "new " + constructorName + mkJavaParams(Params);
} }
public override string[] mkImports() { public override string[] mkImports() {
@ -434,7 +436,7 @@ namespace RusticiSoftware.Translator.CLR
methStr.Append("${this}."); methStr.Append("${this}.");
} }
methStr.Append(Name); methStr.Append(Name);
return methStr.ToString() + mkJavaParams(); return methStr.ToString() + mkJavaParams(Params);
} }
#region Equality #region Equality
@ -1104,6 +1106,10 @@ namespace RusticiSoftware.Translator.CLR
_params = args; _params = args;
} }
public override string mkJava() {
return "${delegate}.Invoke" + mkJavaParams(Params);
}
public override TypeRep mkEmptyRep () public override TypeRep mkEmptyRep ()
{ {
return new DelegateRep (); return new DelegateRep ();

@ -65,12 +65,12 @@ namespace cs2j.Template.Utils
} }
private void buildParameters(ConstructorRepTemplate c, MethodBase m) { private void buildParameters(IList<ParamRepTemplate> ps, MethodBase m) {
foreach (ParameterInfo p in m.GetParameters()) { foreach (ParameterInfo p in m.GetParameters()) {
ParamRepTemplate paramRep = new ParamRepTemplate(); ParamRepTemplate paramRep = new ParamRepTemplate();
paramRep.Type = TypeHelper.buildTypeName(p.ParameterType); paramRep.Type = TypeHelper.buildTypeName(p.ParameterType);
paramRep.Name = p.Name; paramRep.Name = p.Name;
c.Params.Add(paramRep); ps.Add(paramRep);
} }
} }
@ -113,7 +113,7 @@ namespace cs2j.Template.Utils
} }
methRep.TypeParams = tParams; methRep.TypeParams = tParams;
} }
buildParameters(methRep, m); buildParameters(methRep.Params, m);
if (m.IsStatic) { if (m.IsStatic) {
methRep.IsStatic = true; methRep.IsStatic = true;
} }
@ -146,7 +146,7 @@ namespace cs2j.Template.Utils
// Grab Constructors // Grab Constructors
foreach (ConstructorInfo c in t.GetConstructors()) { foreach (ConstructorInfo c in t.GetConstructors()) {
ConstructorRepTemplate consRep = new ConstructorRepTemplate(); ConstructorRepTemplate consRep = new ConstructorRepTemplate();
buildParameters(consRep, c); buildParameters(consRep.Params, c);
consRep.SurroundingTypeName = klass.TypeName; consRep.SurroundingTypeName = klass.TypeName;
klass.Constructors.Add(consRep); klass.Constructors.Add(consRep);
} }
@ -170,6 +170,27 @@ namespace cs2j.Template.Utils
} }
private void buildDelegate(DelegateRepTemplate d, Type t) {
if (t.IsGenericType) {
d.TypeName = t.GetGenericTypeDefinition().FullName;
string[] tParams = new string[t.GetGenericArguments().Length];
for (int i = 0; i < t.GetGenericArguments().Length; i++) {
tParams[i] = t.GetGenericArguments()[i].Name;
}
d.TypeParams = tParams;
}
else {
d.TypeName = t.FullName;
}
MethodInfo invoker = t.GetMethod("Invoke");
if (invoker == null)
throw new Exception("Unexpected: class " + t.FullName + " inherits from System.Delegate but doesn't have an Invoke method");
buildParameters(d.Params, invoker);
d.Return = TypeHelper.buildTypeName(invoker.ReturnType);
}
private IList<TypeRepTemplate> mkTemplates(string typeName) { private IList<TypeRepTemplate> mkTemplates(string typeName) {
List<TypeRepTemplate> rets = new List<TypeRepTemplate>(); List<TypeRepTemplate> rets = new List<TypeRepTemplate>();
@ -181,9 +202,16 @@ namespace cs2j.Template.Utils
} }
TypeRepTemplate retRep = null; TypeRepTemplate retRep = null;
if (t.IsClass) { if (t.IsClass) {
ClassRepTemplate classRep = new ClassRepTemplate(); if (t.IsSubclassOf(typeof(System.Delegate))) {
buildClass(classRep, t); DelegateRepTemplate delRep = new DelegateRepTemplate();
retRep = classRep; buildDelegate(delRep, t);
retRep = delRep;
}
else {
ClassRepTemplate classRep = new ClassRepTemplate();
buildClass(classRep, t);
retRep = classRep;
}
} }
else if (t.IsInterface) { else if (t.IsInterface) {
InterfaceRepTemplate intRep = new InterfaceRepTemplate(); InterfaceRepTemplate intRep = new InterfaceRepTemplate();