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
foreach (var @event in @class.Events) foreach (var @event in @class.Events)
VisitEvent(@event); VisitEvent(@event);
foreach (var variable in @class.Variables)
VisitVariableDecl(variable);
return true; return true;
} }

2
src/Bridge/Variable.cs

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

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

@ -426,8 +426,21 @@ namespace Cxxi.Generators.CLI
{ {
if (variable.Ignore) continue; if (variable.Ignore) continue;
WriteLine("static property {0} {1};", variable.Type, if (variable.Access != AccessSpecifier.Public)
variable.Name); 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(); PopIndent();

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

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

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

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

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

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

1
src/Parser/Parser.cpp

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

Loading…
Cancel
Save