Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Commit

Permalink
add constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
Alxandr committed Jul 24, 2015
1 parent 52feb9c commit 816d405
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
11 changes: 11 additions & 0 deletions sample/NativeSample/Class1.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
using System.Runtime.InteropServices;
using Microsoft.Framework.Runtime;
using YoloDev.Dnx.NativeUtils;

namespace NativeSample
{

abstract class FooBar : NativeMethods
{
public class TestAttribute : System.Attribute {}
readonly IRuntimeEnvironment _env;

[Test]
public FooBar(IRuntimeEnvironment env)
{
_env = env;
}

[NativeMethod("somelib", CallingConvention.Cdecl, BestFitMapping = false)]
public abstract void Test();

Expand Down
86 changes: 86 additions & 0 deletions src/YoloDev.Dnx.NativeUtils.Generator/NativeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public NativeGenerator(NativeModel api, Compilation compilation)

SyntaxTree Generate()
{
foreach (var ctor in _api.Symbol.Constructors)
{
GenerateCtor(ctor);
}

foreach (var method in _api.Methods)
{
GenerateMethod(method);
Expand All @@ -40,6 +45,64 @@ SyntaxTree Generate()
.WithMembers(new SyntaxList<MemberDeclarationSyntax>().Add(_ns.AddMembers(_class.AddMembers(_members.ToArray())))).NormalizeWhitespace());
}

void GenerateCtor(IMethodSymbol ctor)
{
var syntax = SyntaxFactory.ConstructorDeclaration(_class.Identifier)
.WithModifiers(GetModifiers(ctor.DeclaredAccessibility))
.WithBody(SyntaxFactory.Block())
.AddAttributeLists(SyntaxFactory.AttributeList().AddAttributes(ctor.GetAttributes().Select(RecreateAttribute).ToArray()));

var parameters = new List<ParameterSyntax> ();
var arguments = new List<ArgumentSyntax> ();

foreach (var p in ctor.Parameters)
{
var identifier = SyntaxFactory.Identifier(p.Name);
var type = SyntaxFactory.ParseTypeName(p.Type.ToDisplayString());
var parameter = SyntaxFactory.Parameter(identifier).WithType(type);
var argument = SyntaxFactory.Argument(SyntaxFactory.IdentifierName(identifier));

parameters.Add(parameter);
arguments.Add(argument);
}

syntax = syntax
.AddParameterListParameters(parameters.ToArray())
.WithInitializer(
SyntaxFactory.ConstructorInitializer(
SyntaxKind.BaseConstructorInitializer,
SyntaxFactory.ArgumentList().AddArguments(arguments.ToArray())
)
);

_members.Add(syntax);
}

static SyntaxTokenList GetModifiers(Accessibility accessibility)
{
var tokens = new List<SyntaxToken> ();
switch (accessibility)
{
case Accessibility.Public:
tokens.Add(SyntaxFactory.Token(SyntaxKind.PublicKeyword)); break;
case Accessibility.Private:
tokens.Add(SyntaxFactory.Token(SyntaxKind.PrivateKeyword)); break;
case Accessibility.Protected:
tokens.Add(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword)); break;
case Accessibility.Internal:
tokens.Add(SyntaxFactory.Token(SyntaxKind.InternalKeyword)); break;
case Accessibility.ProtectedAndInternal:
tokens.Add(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword));
tokens.Add(SyntaxFactory.Token(SyntaxKind.InternalKeyword)); break;
default:
// CommonAccessibility.ProtectedOrInternal can't be expressed in C#
// but is legal in metadata.
throw new NotSupportedException();
}

return SyntaxFactory.TokenList(tokens);
}

void GenerateMethod(IMethodSymbol method)
{
var name = method.Name;
Expand Down Expand Up @@ -164,6 +227,29 @@ static MethodDeclarationSyntax CreateMethod(IMethodSymbol baseSymbol, DelegateDe
return method;
}

static AttributeSyntax RecreateAttribute(AttributeData attr)
{
var args = new List<AttributeArgumentSyntax>();

foreach (var ctorArg in attr.ConstructorArguments)
{
var arg = SyntaxFactory.AttributeArgument(SyntaxFactory.ParseExpression(ctorArg.ToCSharpString()));
args.Add(arg);
}

foreach (var namedArg in attr.NamedArguments)
{
var arg = SyntaxFactory.AttributeArgument(SyntaxFactory.ParseExpression(namedArg.Value.ToCSharpString()))
.WithNameEquals(SyntaxFactory.NameEquals(namedArg.Key));
args.Add(arg);
}

var syntax = SyntaxFactory.Attribute(SyntaxFactory.ParseName(attr.AttributeClass.ToDisplayString()))
.AddArgumentListArguments(args.ToArray());

return syntax;
}

static ExpressionSyntax InstansiateAttribute(AttributeData attr)
{
var expr = SyntaxFactory.ObjectCreationExpression(SyntaxFactory.ParseTypeName(attr.AttributeClass.ToDisplayString()));
Expand Down

0 comments on commit 816d405

Please sign in to comment.