Browse Source

Merge pull request #8 from esdrubal/master

Improvements to C++/CLI static variables code generation
pull/1/head
João Matos 12 years ago
parent
commit
346e788ab8
  1. 3
      src/Bridge/ASTVisitor.cs
  2. 2
      src/Bridge/Variable.cs
  3. 17
      src/Generator/Generators/CLI/CLIHeadersTemplate.cs
  4. 2
      src/Generator/Generators/CLI/CLIMarshal.cs
  5. 48
      src/Generator/Generators/CLI/CLISourcesTemplate.cs
  6. 2
      src/Generator/Generators/CLI/CLITypePrinter.cs
  7. 1
      src/Parser/Parser.cpp

3
src/Bridge/ASTVisitor.cs

@ -157,6 +157,9 @@ namespace Cxxi @@ -157,6 +157,9 @@ namespace Cxxi
foreach (var @event in @class.Events)
VisitEvent(@event);
foreach (var variable in @class.Variables)
VisitVariableDecl(variable);
return true;
}

2
src/Bridge/Variable.cs

@ -8,6 +8,8 @@ namespace Cxxi @@ -8,6 +8,8 @@ namespace Cxxi
return visitor.VisitVariableDecl(this);
}
public AccessSpecifier Access { get; set; }
public Type Type { get { return QualifiedType.Type; } }
public QualifiedType QualifiedType { get; set; }
}

17
src/Generator/Generators/CLI/CLIHeadersTemplate.cs

@ -426,8 +426,21 @@ namespace Cxxi.Generators.CLI @@ -426,8 +426,21 @@ namespace Cxxi.Generators.CLI
{
if (variable.Ignore) continue;
WriteLine("static property {0} {1};", variable.Type,
variable.Name);
if (variable.Access != AccessSpecifier.Public)
continue;
var type = variable.Type;
WriteLine("static property {0} {1}", type, variable.Name);
WriteStartBraceIndent();
WriteLine("{0} get();", type);
if (!variable.QualifiedType.Qualifiers.IsConst)
WriteLine("void set({0});", type);
WriteCloseBraceIndent();
}
PopIndent();

2
src/Generator/Generators/CLI/CLIMarshal.cs

@ -268,7 +268,7 @@ namespace Cxxi.Generators.CLI @@ -268,7 +268,7 @@ namespace Cxxi.Generators.CLI
public override bool VisitVariableDecl(Variable variable)
{
throw new NotImplementedException();
return variable.Type.Visit(this, variable.QualifiedType.Qualifiers);
}
private string ToCLITypeName(Declaration decl)

48
src/Generator/Generators/CLI/CLISourcesTemplate.cs

@ -173,6 +173,9 @@ namespace Cxxi.Generators.CLI @@ -173,6 +173,9 @@ namespace Cxxi.Generators.CLI
if (variable.Ignore)
continue;
if (variable.Access != AccessSpecifier.Public)
continue;
GenerateDeclarationCommon(variable);
GenerateVariable(variable, @class);
}
@ -218,20 +221,20 @@ namespace Cxxi.Generators.CLI @@ -218,20 +221,20 @@ namespace Cxxi.Generators.CLI
{
var @class = field.Class;
GenerateFieldPropertyGetter(field, @class);
GenerateFieldPropertySetter(field, @class);
GeneratePropertyGetter(field, @class);
GeneratePropertySetter(field, @class);
}
private void GenerateFieldPropertySetter(Field field, Class @class)
private void GeneratePropertySetter<T>(T decl, Class @class) where T : Declaration, ITypedDecl
{
WriteLine("void {0}::{1}::set({2} value)", QualifiedIdentifier(@class),
field.Name, field.Type);
decl.Name, decl.Type);
WriteStartBraceIndent();
var param = new Parameter
{
Name = "value",
QualifiedType = field.QualifiedType
QualifiedType = decl.QualifiedType
};
var ctx = new MarshalContext(Driver)
@ -243,8 +246,13 @@ namespace Cxxi.Generators.CLI @@ -243,8 +246,13 @@ namespace Cxxi.Generators.CLI
var marshal = new CLIMarshalManagedToNativePrinter(ctx);
param.Visit(marshal);
var variable = string.Format("((::{0}*)NativePtr)->{1}",
@class.QualifiedOriginalName, field.OriginalName);
string variable;
if (decl is Variable)
variable = string.Format("::{0}::{1}",
@class.QualifiedOriginalName, decl.OriginalName);
else
variable = string.Format("((::{0}*)NativePtr)->{1}",
@class.QualifiedOriginalName, decl.OriginalName);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
@ -255,24 +263,29 @@ namespace Cxxi.Generators.CLI @@ -255,24 +263,29 @@ namespace Cxxi.Generators.CLI
NewLine();
}
private void GenerateFieldPropertyGetter(Field field, Class @class)
private void GeneratePropertyGetter<T>(T decl, Class @class) where T : Declaration, ITypedDecl
{
WriteLine("{0} {1}::{2}::get()", field.Type, QualifiedIdentifier(@class),
field.Name);
WriteLine("{0} {1}::{2}::get()", decl.Type, QualifiedIdentifier(@class),
decl.Name);
WriteStartBraceIndent();
var variable = string.Format("((::{0}*)NativePtr)->{1}",
@class.QualifiedOriginalName, field.OriginalName);
string variable;
if (decl is Variable)
variable = string.Format("::{0}::{1}",
@class.QualifiedOriginalName, decl.OriginalName);
else
variable = string.Format("((::{0}*)NativePtr)->{1}",
@class.QualifiedOriginalName, decl.OriginalName);
var ctx = new MarshalContext(Driver)
{
ArgName = field.Name,
ArgName = decl.Name,
ReturnVarName = variable,
ReturnType = field.Type
ReturnType = decl.Type
};
var marshal = new CLIMarshalNativeToManagedPrinter(ctx);
field.Visit(marshal);
decl.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
@ -397,7 +410,10 @@ namespace Cxxi.Generators.CLI @@ -397,7 +410,10 @@ namespace Cxxi.Generators.CLI
private void GenerateVariable(Variable variable, Class @class)
{
GeneratePropertyGetter(variable, @class);
if (!variable.QualifiedType.Qualifiers.IsConst)
GeneratePropertySetter(variable, @class);
}
private void GenerateClassConstructor(Class @class, bool isIntPtr)
@ -671,7 +687,7 @@ namespace Cxxi.Generators.CLI @@ -671,7 +687,7 @@ namespace Cxxi.Generators.CLI
foreach(var paramInfo in @params)
{
var param = paramInfo.Param;
if(param.Usage != ParameterUsage.Out && param.Usage != ParameterUsage.Ref)
if(param.Usage != ParameterUsage.Out && param.Usage != ParameterUsage.InOut)
continue;
var nativeVarName = paramInfo.Name;

2
src/Generator/Generators/CLI/CLITypePrinter.cs

@ -128,7 +128,7 @@ namespace Cxxi.Generators.CLI @@ -128,7 +128,7 @@ namespace Cxxi.Generators.CLI
str += type;
if(param.Usage == ParameterUsage.Out ||
param.Usage == ParameterUsage.Ref)
param.Usage == ParameterUsage.InOut)
str += "%";
if (hasName && !string.IsNullOrEmpty(name))

1
src/Parser/Parser.cpp

@ -1319,6 +1319,7 @@ Cxxi::Variable^ Parser::WalkVariable(clang::VarDecl *VD) @@ -1319,6 +1319,7 @@ Cxxi::Variable^ Parser::WalkVariable(clang::VarDecl *VD)
auto Var = gcnew Cxxi::Variable();
Var->Name = marshalString<E_UTF8>(VD->getName());
Var->Access = ConvertToAccess(VD->getAccess());
auto TL = VD->getTypeSourceInfo()->getTypeLoc();
Var->QualifiedType = GetQualifiedType(VD->getType(), WalkType(VD->getType(), &TL));

Loading…
Cancel
Save