Browse Source

Extract default common visit code into base TypePrinter class.

pull/829/head
Joao Matos 8 years ago
parent
commit
3ac96ac8f2
  1. 56
      src/Generator/Generators/CLI/CLITypePrinter.cs
  2. 66
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  3. 48
      src/Generator/Generators/TypePrinter.cs
  4. 1
      tests/Common/Common.cs

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

@ -73,22 +73,11 @@ namespace CppSharp.Generators.CLI @@ -73,22 +73,11 @@ namespace CppSharp.Generators.CLI
return string.Format("System::Func<{0}{1}>", returnType.Visit(this), args);
}
public override TypePrinterResult VisitParameters(IEnumerable<Parameter> @params,
bool hasNames)
{
var args = new List<string>();
foreach (var param in @params)
args.Add(VisitParameter(param, hasNames).ToString());
return string.Join(", ", args);
}
public override TypePrinterResult VisitParameter(Parameter param,
bool hasName = true)
{
Parameter = param;
var type = param.Type.Visit(this, param.QualifiedType.Qualifiers);
var type = param.QualifiedType.Visit(this);
Parameter = null;
var str = string.Empty;
@ -167,19 +156,8 @@ namespace CppSharp.Generators.CLI @@ -167,19 +156,8 @@ namespace CppSharp.Generators.CLI
return pointer.QualifiedPointee.Visit(this);
}
public override TypePrinterResult VisitMemberPointerType(
MemberPointerType member, TypeQualifiers quals)
{
return member.QualifiedPointee.Visit(this);
}
public override TypePrinterResult VisitBuiltinType(BuiltinType builtin,
public override TypePrinterResult VisitPrimitiveType(PrimitiveType primitive,
TypeQualifiers quals)
{
return VisitPrimitiveType(builtin.Type);
}
public string VisitPrimitiveType(PrimitiveType primitive)
{
switch (primitive)
{
@ -237,18 +215,6 @@ namespace CppSharp.Generators.CLI @@ -237,18 +215,6 @@ namespace CppSharp.Generators.CLI
return decl.Type.Visit(this);
}
public override TypePrinterResult VisitAttributedType(AttributedType attributed,
TypeQualifiers quals)
{
return attributed.Modified.Visit(this);
}
public override TypePrinterResult VisitDecayedType(DecayedType decayed,
TypeQualifiers quals)
{
return decayed.Decayed.Visit(this);
}
public override TypePrinterResult VisitTemplateSpecializationType(
TemplateSpecializationType template, TypeQualifiers quals)
{
@ -323,18 +289,6 @@ namespace CppSharp.Generators.CLI @@ -323,18 +289,6 @@ namespace CppSharp.Generators.CLI
return result;
}
public override TypePrinterResult VisitPrimitiveType(PrimitiveType type,
TypeQualifiers quals)
{
return VisitPrimitiveType(type);
}
public override TypePrinterResult VisitDeclaration(Declaration decl,
TypeQualifiers quals)
{
return VisitDeclaration(decl);
}
public override TypePrinterResult VisitDeclaration(Declaration decl)
{
var names = new List<string>();
@ -369,12 +323,6 @@ namespace CppSharp.Generators.CLI @@ -369,12 +323,6 @@ namespace CppSharp.Generators.CLI
: string.Empty);
}
public override TypePrinterResult VisitClassTemplateSpecializationDecl(
ClassTemplateSpecialization specialization)
{
return VisitClassDecl(specialization);
}
public override TypePrinterResult VisitTypedefDecl(TypedefDecl typedef)
{
return typedef.Name;

66
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -52,7 +52,7 @@ namespace CppSharp.Generators.CSharp @@ -52,7 +52,7 @@ namespace CppSharp.Generators.CSharp
}
}
return tag.Declaration.Visit(this);
return base.VisitTagType(tag, quals);
}
public override TypePrinterResult VisitArrayType(ArrayType array,
@ -288,12 +288,6 @@ namespace CppSharp.Generators.CSharp @@ -288,12 +288,6 @@ namespace CppSharp.Generators.CSharp
return IntPtrType;
}
public override TypePrinterResult VisitBuiltinType(BuiltinType builtin,
TypeQualifiers quals)
{
return VisitPrimitiveType(builtin.Type, quals);
}
public override TypePrinterResult VisitTypedefType(TypedefType typedef,
TypeQualifiers quals)
{
@ -334,18 +328,6 @@ namespace CppSharp.Generators.CSharp @@ -334,18 +328,6 @@ namespace CppSharp.Generators.CSharp
return decl.Type.Visit(this);
}
public override TypePrinterResult VisitAttributedType(AttributedType attributed,
TypeQualifiers quals)
{
return attributed.Modified.Visit(this);
}
public override TypePrinterResult VisitDecayedType(DecayedType decayed,
TypeQualifiers quals)
{
return decayed.Decayed.Visit(this);
}
public override TypePrinterResult VisitTemplateSpecializationType(
TemplateSpecializationType template, TypeQualifiers quals)
{
@ -543,12 +525,6 @@ namespace CppSharp.Generators.CSharp @@ -543,12 +525,6 @@ namespace CppSharp.Generators.CSharp
throw new NotSupportedException();
}
public override TypePrinterResult VisitDeclaration(Declaration decl,
TypeQualifiers quals)
{
return VisitDeclaration(decl);
}
public override TypePrinterResult VisitDeclaration(Declaration decl)
{
return GetName(decl);
@ -586,26 +562,6 @@ namespace CppSharp.Generators.CSharp @@ -586,26 +562,6 @@ namespace CppSharp.Generators.CSharp
return ret;
}
public override TypePrinterResult VisitTypedefDecl(TypedefDecl typedef)
{
return VisitDeclaration(typedef);
}
public override TypePrinterResult VisitTypeAliasDecl(TypeAlias typeAlias)
{
return VisitDeclaration(typeAlias);
}
public override TypePrinterResult VisitEnumDecl(Enumeration @enum)
{
return VisitDeclaration(@enum);
}
public override TypePrinterResult VisitEnumItemDecl(Enumeration.Item item)
{
return VisitDeclaration(item);
}
string GetName(Declaration decl)
{
var names = new Stack<string>();
@ -651,33 +607,21 @@ namespace CppSharp.Generators.CSharp @@ -651,33 +607,21 @@ namespace CppSharp.Generators.CSharp
return $"global::{string.Join(".", names)}";
}
public override TypePrinterResult VisitVariableDecl(Variable variable)
{
return VisitDeclaration(variable);
}
public override TypePrinterResult VisitParameters(IEnumerable<Parameter> @params,
bool hasNames)
{
var args = new List<string>();
foreach (var param in @params.Where(
@params = @params.Where(
p => ContextKind == TypePrinterContextKind.Native ||
(p.Kind != ParameterKind.IndirectReturnType && !p.Ignore)))
{
Parameter = param;
args.Add(VisitParameter(param, hasNames).Type);
}
(p.Kind != ParameterKind.IndirectReturnType && !p.Ignore));
Parameter = null;
return string.Join(", ", args);
return base.VisitParameters(@params, hasNames);
}
public override TypePrinterResult VisitParameter(Parameter param, bool hasName)
{
var usage = ContextKind == TypePrinterContextKind.Native ?
string.Empty : GetParameterUsage(param.Usage);
var type = param.Type.Visit(this, param.QualifiedType.Qualifiers);
var type = param.QualifiedType.Visit(this);
var name = param.Name;
if (param.DefaultArgument == null || !Options.GenerateDefaultValuesForArguments)
return $"{usage}{type} {name}";

48
src/Generator/Generators/TypePrinter.cs

@ -76,13 +76,13 @@ namespace CppSharp.Generators @@ -76,13 +76,13 @@ namespace CppSharp.Generators
public virtual TypePrinterResult VisitAttributedType(AttributedType attributed,
TypeQualifiers quals)
{
throw new NotImplementedException();
return attributed.Modified.Visit(this);
}
public virtual TypePrinterResult VisitBuiltinType(BuiltinType builtin,
TypeQualifiers quals)
{
throw new NotImplementedException();
return VisitPrimitiveType(builtin.Type, quals);
}
public virtual TypePrinterResult VisitCILType(CILType type, TypeQualifiers quals)
@ -92,7 +92,7 @@ namespace CppSharp.Generators @@ -92,7 +92,7 @@ namespace CppSharp.Generators
public virtual TypePrinterResult VisitClassDecl(Class @class)
{
throw new NotImplementedException();
return VisitDeclaration(@class);
}
public virtual TypePrinterResult VisitClassTemplateDecl(ClassTemplate template)
@ -103,13 +103,13 @@ namespace CppSharp.Generators @@ -103,13 +103,13 @@ namespace CppSharp.Generators
public virtual TypePrinterResult VisitClassTemplateSpecializationDecl(
ClassTemplateSpecialization specialization)
{
throw new NotImplementedException();
return VisitClassDecl(specialization);
}
public virtual TypePrinterResult VisitDecayedType(DecayedType decayed,
TypeQualifiers quals)
{
throw new NotImplementedException();
return decayed.Decayed.Visit(this);
}
public virtual TypePrinterResult VisitDeclaration(Declaration decl)
@ -120,7 +120,7 @@ namespace CppSharp.Generators @@ -120,7 +120,7 @@ namespace CppSharp.Generators
public virtual TypePrinterResult VisitDeclaration(Declaration decl,
TypeQualifiers quals)
{
throw new NotImplementedException();
return VisitDeclaration(decl);
}
public virtual TypePrinterResult VisitDelegate(FunctionType function)
@ -142,12 +142,12 @@ namespace CppSharp.Generators @@ -142,12 +142,12 @@ namespace CppSharp.Generators
public virtual TypePrinterResult VisitEnumDecl(Enumeration @enum)
{
throw new NotImplementedException();
return VisitDeclaration(@enum);
}
public virtual TypePrinterResult VisitEnumItemDecl(Enumeration.Item item)
{
throw new NotImplementedException();
return VisitDeclaration(@item);
}
public virtual TypePrinterResult VisitEvent(Event @event)
@ -202,7 +202,7 @@ namespace CppSharp.Generators @@ -202,7 +202,7 @@ namespace CppSharp.Generators
public virtual TypePrinterResult VisitMemberPointerType(
MemberPointerType member, TypeQualifiers quals)
{
throw new NotImplementedException();
return member.QualifiedPointee.Visit(this);
}
public virtual TypePrinterResult VisitMethodDecl(Method method)
@ -230,18 +230,31 @@ namespace CppSharp.Generators @@ -230,18 +230,31 @@ namespace CppSharp.Generators
public virtual TypePrinterResult VisitParameter(Parameter param,
bool hasName = true)
{
throw new NotImplementedException();
Parameter = param;
var type = param.QualifiedType.Visit(this);
var name = hasName ? $" {param.Name}" : string.Empty;
Parameter = null;
return $"{type}{name}";
}
public virtual TypePrinterResult VisitParameterDecl(Parameter parameter)
{
throw new NotImplementedException();
return parameter.QualifiedType.Visit(this);
}
public virtual TypePrinterResult VisitParameters(IEnumerable<Parameter> @params,
bool hasNames = true)
{
throw new NotImplementedException();
var args = new List<string>();
foreach (var param in @params)
{
Parameter = param;
args.Add(VisitParameter(param, hasNames).Type);
}
Parameter = null;
return string.Join(", ", args);
}
public virtual TypePrinterResult VisitPointerType(PointerType pointer,
@ -263,7 +276,10 @@ namespace CppSharp.Generators @@ -263,7 +276,10 @@ namespace CppSharp.Generators
public virtual TypePrinterResult VisitTagType(TagType tag, TypeQualifiers quals)
{
throw new NotImplementedException();
if (tag.Declaration == null)
return string.Empty;
return tag.Declaration.Visit(this);
}
public virtual TypePrinterResult VisitTemplateParameterDecl(
@ -303,7 +319,7 @@ namespace CppSharp.Generators @@ -303,7 +319,7 @@ namespace CppSharp.Generators
public virtual TypePrinterResult VisitTypeAliasDecl(TypeAlias typeAlias)
{
throw new NotImplementedException();
return VisitDeclaration(typeAlias);
}
public virtual TypePrinterResult VisitTypeAliasTemplateDecl(
@ -314,7 +330,7 @@ namespace CppSharp.Generators @@ -314,7 +330,7 @@ namespace CppSharp.Generators
public virtual TypePrinterResult VisitTypedefDecl(TypedefDecl typedef)
{
throw new NotImplementedException();
return VisitDeclaration(typedef);
}
public TypePrinterResult VisitTypedefNameDecl(TypedefNameDecl typedef)
@ -342,7 +358,7 @@ namespace CppSharp.Generators @@ -342,7 +358,7 @@ namespace CppSharp.Generators
public virtual TypePrinterResult VisitVariableDecl(Variable variable)
{
throw new NotImplementedException();
return VisitDeclaration(variable);
}
public virtual TypePrinterResult VisitVarTemplateDecl(VarTemplate template)

1
tests/Common/Common.cs

@ -1,7 +1,6 @@ @@ -1,7 +1,6 @@
using System.Linq;
using CppSharp.AST;
using CppSharp.Generators;
using CppSharp.Generators.CLI;
using CppSharp.Generators.CSharp;
using CppSharp.Passes;
using CppSharp.Types;

Loading…
Cancel
Save