Skip to content

Commit

Permalink
Updated the generator to generate VirtualBaseAttributes to signify cl…
Browse files Browse the repository at this point in the history
…asses with virtual bases. This will eventually be used to properly initialize the vtables of the virtual bases so that those tests may pass.

Signed-off-by: Steve <amn3sia@gmail.com>
  • Loading branch information
stbrowne committed Feb 16, 2012
1 parent 8c1102e commit 7253693
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
37 changes: 36 additions & 1 deletion src/Mono.Cxxi/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,45 @@
using System;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;

namespace Mono.Cxxi {

#region Interface method attributes
#region Interface method attributes

[AttributeUsage(AttributeTargets.Class)]
public class VirtualBaseAttribute : Attribute
{

public VirtualBaseAttribute(string type)
: this(Type.GetType(type))
{
}

public VirtualBaseAttribute(Type t)
{
this.VirtualBaseType = t;
}

public Type VirtualBaseType
{
get;
private set;
}

public static bool IsVirtualBaseOf<T>(T instance, Type virtualBase)
where T : class
{
return IsVirtualBaseOf<T>(virtualBase);
}

public static bool IsVirtualBaseOf<T>(Type virtualBase)
where T : class
{
return typeof(T).GetCustomAttributes(typeof(VirtualBaseAttribute), false)
.Cast<VirtualBaseAttribute>().Any(v => v.VirtualBaseType == virtualBase);
}
}

[AttributeUsage (AttributeTargets.Method)]
public class ConstructorAttribute : Attribute {}
Expand Down
11 changes: 8 additions & 3 deletions src/generator/Class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public class Class : Namespace {
public Class (Node n)
: base (n)
{
BaseClasses = new List<Class> ();
BaseClasses = new List<Class> ();
VirtualBases = new List<Class> ();
Fields = new List<Field> ();
Properties = new List<Property> ();
Methods = new List<Method> ();
Expand All @@ -46,8 +47,12 @@ public Class (Node n)
}

public List<Class> BaseClasses {
get; set;
}
get; set;
}

public List<Class> VirtualBases {
get; set;
}

public List<Class> NestedClasses {
get; set;
Expand Down
2 changes: 2 additions & 0 deletions src/generator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ void CreateTypes (Node root) {
Class baseClass = NodeToNamespace [bn.NodeForAttr ("type")] as Class;
Debug.Assert (baseClass != null);
klass.BaseClasses.Add (baseClass);
if (bn.IsTrue("virtual"))
klass.VirtualBases.Add (baseClass);
}
}
}
Expand Down
20 changes: 17 additions & 3 deletions src/generator/Templates/CSharp/CSharpClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ private void WriteParameters (IList<Parameter> parameters, bool writeType, bool

Write (CSharpLanguage.SafeIdentifier (parameters [i].Name));
}
}

private string GetVirtualBasesString()
{
if (Class.VirtualBases.Count == 0)
return string.Empty;

// Should probably use a StringBuilder in here
string virtualBases = "[";
Class.VirtualBases.ForEach((c) => virtualBases += "VirtualBase(typeof(" + string.Join(".", c.FullyQualifiedName) + ")), ");
return virtualBases.Substring(0, virtualBases.Length - 2) + "]" + Environment.NewLine;
}

private string GetBaseString ()
Expand Down Expand Up @@ -179,8 +190,11 @@ public override string TransformText() {
#line 29 "/Users/alex/OpenSource/cppinterop/src/generator/Templates/CSharp/CSharpClass.tt"
} /* if !Nested */

#line default
#line hidden
#line default
#line hidden

#line 30 "/Users/alex/OpenSource/cppinterop/src/generator/Templates/CSharp/CSharpClass.tt"
this.Write("\t" + GetVirtualBasesString());

#line 30 "/Users/alex/OpenSource/cppinterop/src/generator/Templates/CSharp/CSharpClass.tt"
this.Write("\tpublic partial class ");
Expand All @@ -197,7 +211,7 @@ public override string TransformText() {
#line 30 "/Users/alex/OpenSource/cppinterop/src/generator/Templates/CSharp/CSharpClass.tt"
this.Write(" : ");

#line default
#line default
#line hidden

#line 30 "/Users/alex/OpenSource/cppinterop/src/generator/Templates/CSharp/CSharpClass.tt"
Expand Down
12 changes: 11 additions & 1 deletion src/generator/Templates/CSharp/CSharpClass.tt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ using Mono.Cxxi;

namespace <#= @namespace #> {
<# } /* if !Nested */ #>
public partial class <#= wrapper #> : <#= GetBaseString () #> {
<#= GetVirtualBasesString () #> public partial class <#= wrapper #> : <#= GetBaseString () #> {

private static readonly <#= iface #> impl = Libs.<#= Generator.Lib.BaseName #>.GetClass<<#= iface + "," + layout + "," + wrapper #>> ("<#= Class.Name #>");
<# if (!hasBase) { #>
Expand Down Expand Up @@ -391,6 +391,16 @@ private void WriteParameters (IList<Parameter> parameters, bool writeType, bool

Write (CSharpLanguage.SafeIdentifier (parameters [i].Name));
}
}

private string GetVirtualBasesString()
{
if (Class.VirtualBases.Count == 0)
return string.Empty;

string virtualBases = "[";
Class.VirtualBases.ForEach((c) => virtualBases += "VirtualBase(typeof(" + string.Join(".", c.FullyQualifiedName) + ")), ");
return virtualBases.Substring(0, virtualBases.Length - 2) + "]" + Environment.NewLine;
}

private string GetBaseString ()
Expand Down

0 comments on commit 7253693

Please sign in to comment.