mirror of https://github.com/mono/CppSharp.git
Browse Source
git-svn-id: https://mono-soc-2010.googlecode.com/svn/trunk/cppinterop@115 a470b8cb-0e6f-1642-1b45-71e107334c4bpull/1/head
14 changed files with 690 additions and 18 deletions
@ -0,0 +1,9 @@
@@ -0,0 +1,9 @@
|
||||
using System; |
||||
namespace Mono.VisualC.Code { |
||||
public enum Access { |
||||
Public, |
||||
Protected, |
||||
Private |
||||
} |
||||
} |
||||
|
@ -0,0 +1,29 @@
@@ -0,0 +1,29 @@
|
||||
using System; |
||||
using System.Reflection; |
||||
using System.Runtime.CompilerServices; |
||||
|
||||
// Information about this assembly is defined by the following attributes.
|
||||
// Change them to the values specific to your project.
|
||||
|
||||
[assembly: AssemblyTitle("Mono.VisualC.Code")] |
||||
[assembly: AssemblyDescription("")] |
||||
[assembly: AssemblyConfiguration("")] |
||||
[assembly: AssemblyCompany("")] |
||||
[assembly: AssemblyProduct("")] |
||||
[assembly: AssemblyCopyright("")] |
||||
[assembly: AssemblyTrademark("")] |
||||
[assembly: AssemblyCulture("")] |
||||
|
||||
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
|
||||
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
|
||||
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
|
||||
|
||||
[assembly: AssemblyVersion("1.0.*")] |
||||
|
||||
// The following attributes are used to specify the signing key for the assembly,
|
||||
// if desired. See the Mono documentation for more information about signing.
|
||||
|
||||
//[assembly: AssemblyDelaySign(false)]
|
||||
//[assembly: AssemblyKeyFile("")]
|
||||
|
||||
[assembly: CLSCompliant(true)] |
@ -0,0 +1,153 @@
@@ -0,0 +1,153 @@
|
||||
using System; |
||||
using System.IO; |
||||
using System.Linq; |
||||
using System.Collections.Generic; |
||||
using System.Reflection; |
||||
|
||||
using System.CodeDom; |
||||
using Mono.VisualC.Interop; |
||||
|
||||
namespace Mono.VisualC.Code.Atoms { |
||||
|
||||
public class Class : CodeContainer { |
||||
|
||||
// FIXME: This should be moved into Mono.VisualC.Interop and an attribute
|
||||
// for name mangling purposes (MSVC mangles differently depending on defined as class or struct).
|
||||
public enum Definition { |
||||
Class, |
||||
Struct |
||||
} |
||||
public struct BaseClass { |
||||
public Class Class; |
||||
public Access Access; |
||||
public bool IsVirtual; |
||||
} |
||||
|
||||
public string Name { get; set; } |
||||
public string StaticCppLibrary { get; set; } |
||||
|
||||
public IEnumerable<BaseClass> Bases { get; set; } |
||||
public Definition DefinedAs { get; set; } |
||||
|
||||
public Class (string name) |
||||
{ |
||||
Name = name; |
||||
Bases = Enumerable.Empty<BaseClass> (); |
||||
} |
||||
|
||||
internal protected override CodeObject InsideCodeNamespace (CodeNamespace ns) |
||||
{ |
||||
var wrapper = new CodeTypeDeclaration (Name) { TypeAttributes = TypeAttributes.Public }; |
||||
var iface = CreateInterface (); |
||||
var native = CreateNativeLayout (); |
||||
|
||||
wrapper.Members.Add (iface); |
||||
wrapper.Members.Add (native); |
||||
|
||||
// FIXME: For now, we'll have the managed wrapper extend from the first public base class
|
||||
string managedBase = Bases.Where (b => b.Access == Access.Public).Select (b => b.Class.Name).FirstOrDefault (); |
||||
if (managedBase == null) { |
||||
|
||||
managedBase = typeof (ICppObject).Name; |
||||
|
||||
// Add Native property
|
||||
var nativeField = new CodeMemberField (typeof (CppInstancePtr), "native_ptr") { Attributes = MemberAttributes.Family }; |
||||
var nativeProperty = new CodeMemberProperty { |
||||
Name = "Native", |
||||
Type = new CodeTypeReference (typeof (CppInstancePtr)), |
||||
HasSet = false, |
||||
Attributes = MemberAttributes.Public |
||||
}; |
||||
nativeProperty.GetStatements.Add (new CodeMethodReturnStatement (new CodeFieldReferenceExpression (new CodeThisReferenceExpression (), nativeField.Name))); |
||||
|
||||
wrapper.Members.Add (nativeField); |
||||
wrapper.Members.Add (nativeProperty); |
||||
} |
||||
wrapper.BaseTypes.Add (managedBase); |
||||
|
||||
// add static impl field
|
||||
var implField = new CodeMemberField (iface.Name, "impl") { Attributes = MemberAttributes.Static | MemberAttributes.Private }; |
||||
if (StaticCppLibrary != null) { |
||||
CodeTypeReference [] types = new CodeTypeReference [] { |
||||
new CodeTypeReference (iface.Name), |
||||
new CodeTypeReference (native.Name), |
||||
new CodeTypeReference (wrapper.Name) |
||||
}; |
||||
var getClassMethod = new CodeMethodReferenceExpression (new CodeTypeReferenceExpression (StaticCppLibrary), "GetClass", types); |
||||
implField.InitExpression = new CodeMethodInvokeExpression (getClassMethod, new CodePrimitiveExpression (Name)); |
||||
} |
||||
wrapper.Members.Add (implField); |
||||
|
||||
foreach (var atom in Atoms) |
||||
atom.Visit (wrapper); |
||||
|
||||
ns.Types.Add (wrapper); |
||||
return wrapper; |
||||
} |
||||
|
||||
private CodeTypeDeclaration CreateInterface () |
||||
{ |
||||
var iface = new CodeTypeDeclaration ("I" + Name) { |
||||
TypeAttributes = TypeAttributes.Interface | TypeAttributes.NestedPublic, |
||||
Attributes = MemberAttributes.Public, |
||||
IsInterface = true |
||||
}; |
||||
|
||||
iface.BaseTypes.Add (new CodeTypeReference (typeof (ICppClassOverridable<>).Name, new CodeTypeReference (Name))); |
||||
|
||||
foreach (var atom in Atoms) |
||||
atom.Visit (iface); |
||||
|
||||
return iface; |
||||
} |
||||
|
||||
private CodeTypeDeclaration CreateNativeLayout () |
||||
{ |
||||
var native = new CodeTypeDeclaration ("_" + Name) { |
||||
TypeAttributes = TypeAttributes.NestedPrivate | TypeAttributes.SequentialLayout, |
||||
Attributes = MemberAttributes.Private, |
||||
IsStruct = true |
||||
}; |
||||
|
||||
foreach (var atom in Atoms) |
||||
atom.Visit (native); |
||||
|
||||
return native; |
||||
} |
||||
|
||||
public override void Write (TextWriter writer) |
||||
{ |
||||
string declarator = (DefinedAs == Definition.Class? "class" : "struct"); |
||||
writer.Write ("{0} {1}", declarator, Name); |
||||
|
||||
var bce = Bases.GetEnumerator (); |
||||
|
||||
if (bce.MoveNext ()) { |
||||
writer.Write (" : "); |
||||
|
||||
while (true) { |
||||
var baseClass = bce.Current; |
||||
|
||||
if (baseClass.IsVirtual) writer.Write ("virtual "); |
||||
switch (baseClass.Access) { |
||||
case Access.Public: writer.Write ("public "); break; |
||||
case Access.Protected: writer.Write ("protected "); break; |
||||
case Access.Private: writer.Write ("private "); break; |
||||
} |
||||
|
||||
writer.Write (baseClass.Class.Name); |
||||
|
||||
if (!bce.MoveNext ()) |
||||
break; |
||||
|
||||
writer.Write (", "); |
||||
} |
||||
} |
||||
|
||||
writer.WriteLine (" {"); |
||||
base.Write (writer); |
||||
writer.WriteLine ("};"); |
||||
} |
||||
} |
||||
} |
||||
|
@ -0,0 +1,78 @@
@@ -0,0 +1,78 @@
|
||||
using System; |
||||
using System.IO; |
||||
using System.Collections.Generic; |
||||
using System.CodeDom; |
||||
|
||||
using Mono.VisualC.Interop; |
||||
|
||||
namespace Mono.VisualC.Code.Atoms { |
||||
|
||||
public class Method : CodeContainer { |
||||
public struct Parameter { |
||||
public string Name; |
||||
public string Type; |
||||
} |
||||
|
||||
public string Name { get; set; } |
||||
public Access Access { get; set; } |
||||
public bool IsVirtual { get; set; } |
||||
public bool IsStatic { get; set; } |
||||
|
||||
public string RetType { get; set; } |
||||
|
||||
public IEnumerable<Parameter> Parameters { get; set; } |
||||
|
||||
public Method (string name) |
||||
{ |
||||
Name = name; |
||||
} |
||||
|
||||
internal protected override CodeObject InsideCodeTypeDeclaration (CodeTypeDeclaration decl) |
||||
{ |
||||
CodeMemberMethod method = null; |
||||
|
||||
if (decl.IsInterface) { |
||||
method = new CodeMemberMethod (); |
||||
method.Name = Name; |
||||
Type managedReturn = new CppType (RetType).ToManagedType (); |
||||
if (managedReturn != null) |
||||
method.ReturnType = new CodeTypeReference (managedReturn); |
||||
else |
||||
method.ReturnType = new CodeTypeReference (RetType); |
||||
|
||||
if (IsVirtual) |
||||
method.CustomAttributes.Add (new CodeAttributeDeclaration ("Virtual")); |
||||
|
||||
if (IsStatic) |
||||
method.CustomAttributes.Add (new CodeAttributeDeclaration ("Static")); |
||||
else |
||||
method.Parameters.Add (new CodeParameterDeclarationExpression (typeof (CppInstancePtr), "this")); |
||||
|
||||
foreach (var param in Parameters) { |
||||
CodeParameterDeclarationExpression paramDecl; |
||||
Type managedType = new CppType (param.Type).ToManagedType (); |
||||
if (managedType != null) |
||||
paramDecl = new CodeParameterDeclarationExpression (managedType, param.Name); |
||||
else |
||||
paramDecl = new CodeParameterDeclarationExpression (param.Type, param.Name); |
||||
|
||||
if (!IsVirtual) |
||||
paramDecl.CustomAttributes.Add (new CodeAttributeDeclaration ("MangleAs", new CodeAttributeArgument (new CodePrimitiveExpression (param.Type)))); |
||||
|
||||
method.Parameters.Add (paramDecl); |
||||
} |
||||
} |
||||
|
||||
// FIXME: add wrapper method
|
||||
if (method != null) |
||||
decl.Members.Add (method); |
||||
return method; |
||||
} |
||||
|
||||
public override void Write (TextWriter writer) |
||||
{ |
||||
throw new NotImplementedException (); |
||||
} |
||||
} |
||||
} |
||||
|
@ -0,0 +1,82 @@
@@ -0,0 +1,82 @@
|
||||
|
||||
using System; |
||||
using System.Reflection; |
||||
using System.Linq; |
||||
using System.IO; |
||||
|
||||
using System.CodeDom; |
||||
|
||||
namespace Mono.VisualC.Code.Atoms { |
||||
|
||||
// FIXME: support conditional compilation
|
||||
public class PoundDefine<T> : CodeAtom { |
||||
public string Name {get; set;} |
||||
public T Value {get; set;} |
||||
private bool value_set = false; |
||||
|
||||
public PoundDefine(string name) { |
||||
Name = name; |
||||
} |
||||
public PoundDefine (string name, T value) { |
||||
Name = name; |
||||
Value = value; |
||||
value_set = true; |
||||
} |
||||
public override void Write (TextWriter writer) { |
||||
if (value_set) |
||||
writer.WriteLine ("#define {0} {1}", Name, Value); |
||||
else |
||||
writer.WriteLine ("#define {0}", Name); |
||||
} |
||||
|
||||
internal protected override CodeObject InsideCodeNamespace (CodeNamespace ns) |
||||
{ |
||||
if (!value_set) |
||||
return null; |
||||
|
||||
// create a new type Constants if it does not exist
|
||||
CodeTypeDeclaration constants = (from type in ns.Types.Cast<CodeTypeDeclaration> () |
||||
where type.Name.Equals ("Constants") select type).SingleOrDefault (); |
||||
if (constants == null) { |
||||
constants = new CodeTypeDeclaration ("Constants"); |
||||
constants.TypeAttributes = TypeAttributes.Public | TypeAttributes.Sealed; |
||||
ns.Types.Add (constants); |
||||
} |
||||
|
||||
return constants; |
||||
} |
||||
|
||||
internal protected override CodeObject InsideCodeTypeDeclaration (CodeTypeDeclaration decl) |
||||
{ |
||||
if (!value_set) |
||||
return null; |
||||
|
||||
var constant = new CodeMemberField (typeof (T), Name); |
||||
constant.Attributes = MemberAttributes.Public | MemberAttributes.Const; |
||||
constant.InitExpression = new CodePrimitiveExpression (Value); |
||||
decl.Members.Add (constant); |
||||
|
||||
return constant; |
||||
} |
||||
} |
||||
|
||||
public class PoundIfDef : CodeContainer { |
||||
public enum Condition { |
||||
Defined, |
||||
NotDefined |
||||
} |
||||
public Condition IfCondition {get; set;} |
||||
public string Name {get; set;} |
||||
public PoundIfDef (Condition condition, string name) { |
||||
IfCondition = condition; |
||||
Name = name; |
||||
} |
||||
|
||||
public override void Write (TextWriter writer) { |
||||
string directive = (IfCondition == Condition.Defined ? "#ifdef {0}" : "#ifndef {0}"); |
||||
writer.WriteLine (directive, Name); |
||||
base.Write (writer); |
||||
writer.WriteLine ("#endif"); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,43 @@
@@ -0,0 +1,43 @@
|
||||
using System; |
||||
using System.Linq; |
||||
using System.IO; |
||||
using System.Collections.Generic; |
||||
|
||||
using System.CodeDom; |
||||
|
||||
namespace Mono.VisualC.Code { |
||||
|
||||
public abstract class CodeAtom { |
||||
|
||||
internal protected virtual void Visit (CodeObject obj) |
||||
{ |
||||
CodeObject result = obj; |
||||
|
||||
while (result != null) { |
||||
if (result is CodeCompileUnit) { result = InsideCodeCompileUnit (result as CodeCompileUnit); continue; } |
||||
if (result is CodeNamespace) { result = InsideCodeNamespace (result as CodeNamespace); continue; } |
||||
if (result is CodeTypeDeclaration) { result = InsideCodeTypeDeclaration (result as CodeTypeDeclaration); continue; } |
||||
|
||||
break; |
||||
} |
||||
} |
||||
|
||||
internal protected virtual CodeObject InsideCodeCompileUnit (CodeCompileUnit ccu) |
||||
{ |
||||
return null; |
||||
} |
||||
|
||||
internal protected virtual CodeObject InsideCodeNamespace (CodeNamespace ns) |
||||
{ |
||||
return null; |
||||
} |
||||
|
||||
internal protected virtual CodeObject InsideCodeTypeDeclaration (CodeTypeDeclaration decl) |
||||
{ |
||||
return null; |
||||
} |
||||
|
||||
public abstract void Write (TextWriter writer); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,40 @@
@@ -0,0 +1,40 @@
|
||||
using System; |
||||
using System.IO; |
||||
using System.Collections.Generic; |
||||
|
||||
using System.CodeDom; |
||||
using System.CodeDom.Compiler; |
||||
|
||||
namespace Mono.VisualC.Code { |
||||
|
||||
public abstract class CodeContainer : CodeAtom { |
||||
|
||||
private LinkedList<CodeAtom> containedAtoms; |
||||
public string IndentString {get; set;} |
||||
|
||||
public CodeContainer (string indentString) |
||||
{ |
||||
containedAtoms = new LinkedList<CodeAtom> (); |
||||
IndentString = indentString; |
||||
} |
||||
|
||||
public CodeContainer() : this("\t") |
||||
{ |
||||
} |
||||
|
||||
public virtual LinkedList<CodeAtom> Atoms { |
||||
get { return containedAtoms; } |
||||
} |
||||
|
||||
public override void Write (TextWriter writer) |
||||
{ |
||||
IndentedTextWriter itw = new IndentedTextWriter (writer, IndentString); |
||||
itw.Indent = 1; |
||||
foreach (CodeAtom atom in containedAtoms) { |
||||
atom.Write (itw); |
||||
} |
||||
} |
||||
|
||||
} |
||||
} |
||||
|
@ -0,0 +1,70 @@
@@ -0,0 +1,70 @@
|
||||
|
||||
using System; |
||||
using System.IO; |
||||
using System.Collections.Generic; |
||||
|
||||
using System.CodeDom; |
||||
|
||||
namespace Mono.VisualC.Code { |
||||
public class CodeUnit : CodeContainer { |
||||
|
||||
public string ManagedNamespace { get; set; } |
||||
|
||||
public CodeUnit () : base (string.Empty) |
||||
{ |
||||
} |
||||
|
||||
public virtual CodeCompileUnit WrapperToCodeDom () |
||||
{ |
||||
CodeCompileUnit ccu = new CodeCompileUnit (); |
||||
Visit (ccu); |
||||
|
||||
return ccu; |
||||
} |
||||
|
||||
internal protected override CodeObject InsideCodeCompileUnit (CodeCompileUnit ccu) |
||||
{ |
||||
CodeNamespace ns = new CodeNamespace (ManagedNamespace); |
||||
ns.Imports.Add (new CodeNamespaceImport ("Mono.VisualC.Interop")); |
||||
ccu.Namespaces.Add (ns); |
||||
|
||||
return ns; |
||||
} |
||||
|
||||
internal protected override CodeObject InsideCodeNamespace (CodeNamespace ns) |
||||
{ |
||||
foreach (var atom in Atoms) |
||||
atom.Visit (ns); |
||||
|
||||
return null; |
||||
} |
||||
|
||||
public override string ToString () |
||||
{ |
||||
StringWriter str = new StringWriter (); |
||||
Write (str); |
||||
return str.ToString (); |
||||
} |
||||
|
||||
public override void Write (TextWriter writer) |
||||
{ |
||||
writer.WriteLine ("//------------------------------------------------------------------------------"); |
||||
writer.WriteLine ("// <auto-generated>"); |
||||
writer.WriteLine ("// This code was generated by a tool."); |
||||
writer.WriteLine ("// Runtime Version:{0}{1}//", Environment.Version, writer.NewLine); |
||||
writer.WriteLine ("// Changes to this file may cause incorrect behavior and will be lost if"); |
||||
writer.WriteLine ("// the code is regenerated."); |
||||
writer.WriteLine ("// </auto-generated>"); |
||||
writer.WriteLine ("//------------------------------------------------------------------------------{0}", writer.NewLine); |
||||
|
||||
base.Write (writer); |
||||
} |
||||
|
||||
public virtual void Save (string fileName) |
||||
{ |
||||
using (StreamWriter stream = File.CreateText (fileName)) { |
||||
Write (stream); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,60 @@
@@ -0,0 +1,60 @@
|
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
||||
<PropertyGroup> |
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> |
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> |
||||
<ProductVersion>8.0.50727</ProductVersion> |
||||
<SchemaVersion>2.0</SchemaVersion> |
||||
<ProjectGuid>{A22BF9D9-BBCB-4462-BE08-0F4D5280B180}</ProjectGuid> |
||||
<OutputType>Library</OutputType> |
||||
<AssemblyName>Mono.VisualC.Code</AssemblyName> |
||||
<RootNamespace>Mono.VisualC.Code</RootNamespace> |
||||
</PropertyGroup> |
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> |
||||
<DebugSymbols>true</DebugSymbols> |
||||
<DebugType>full</DebugType> |
||||
<Optimize>false</Optimize> |
||||
<OutputPath>bin\Debug</OutputPath> |
||||
<DefineConstants>DEBUG</DefineConstants> |
||||
<ErrorReport>prompt</ErrorReport> |
||||
<WarningLevel>4</WarningLevel> |
||||
<ConsolePause>false</ConsolePause> |
||||
</PropertyGroup> |
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> |
||||
<DebugType>none</DebugType> |
||||
<Optimize>false</Optimize> |
||||
<OutputPath>bin\Release</OutputPath> |
||||
<ErrorReport>prompt</ErrorReport> |
||||
<WarningLevel>4</WarningLevel> |
||||
<ConsolePause>false</ConsolePause> |
||||
</PropertyGroup> |
||||
<ItemGroup> |
||||
<Reference Include="System" /> |
||||
<Reference Include="System.Core" /> |
||||
</ItemGroup> |
||||
<ItemGroup> |
||||
<Compile Include="AssemblyInfo.cs" /> |
||||
<Compile Include="CodeAtom.cs" /> |
||||
<Compile Include="Atoms\Preprocessor.cs" /> |
||||
<Compile Include="CodeContainer.cs" /> |
||||
<Compile Include="Atoms\Class.cs" /> |
||||
<Compile Include="CodeUnit.cs" /> |
||||
<Compile Include="Access.cs" /> |
||||
<Compile Include="Atoms\Method.cs" /> |
||||
</ItemGroup> |
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> |
||||
<ItemGroup> |
||||
<Folder Include="Atoms\" /> |
||||
</ItemGroup> |
||||
<ProjectExtensions> |
||||
<MonoDevelop> |
||||
<Properties InternalTargetFrameworkVersion="3.5" /> |
||||
</MonoDevelop> |
||||
</ProjectExtensions> |
||||
<ItemGroup> |
||||
<ProjectReference Include="..\Mono.VisualC.Interop\Mono.VisualC.Interop.csproj"> |
||||
<Project>{4A864586-93C5-4DC1-8A80-F094A88506D7}</Project> |
||||
<Name>Mono.VisualC.Interop</Name> |
||||
</ProjectReference> |
||||
</ItemGroup> |
||||
</Project> |
@ -0,0 +1,20 @@
@@ -0,0 +1,20 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 9.00 |
||||
# Visual Studio 2005 |
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mono.VisualC.Code", "Mono.VisualC.Code.csproj", "{A22BF9D9-BBCB-4462-BE08-0F4D5280B180}" |
||||
EndProject |
||||
Global |
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution |
||||
Debug|Any CPU = Debug|Any CPU |
||||
Release|Any CPU = Release|Any CPU |
||||
EndGlobalSection |
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution |
||||
{A22BF9D9-BBCB-4462-BE08-0F4D5280B180}.Debug|Any CPU.ActiveCfg = Debug|Any CPU |
||||
{A22BF9D9-BBCB-4462-BE08-0F4D5280B180}.Debug|Any CPU.Build.0 = Debug|Any CPU |
||||
{A22BF9D9-BBCB-4462-BE08-0F4D5280B180}.Release|Any CPU.ActiveCfg = Release|Any CPU |
||||
{A22BF9D9-BBCB-4462-BE08-0F4D5280B180}.Release|Any CPU.Build.0 = Release|Any CPU |
||||
EndGlobalSection |
||||
GlobalSection(MonoDevelopProperties) = preSolution |
||||
StartupItem = Mono.VisualC.Code.csproj |
||||
EndGlobalSection |
||||
EndGlobal |
Loading…
Reference in new issue