mirror of https://github.com/mono/CppSharp.git
1 changed files with 307 additions and 307 deletions
@ -1,310 +1,310 @@ |
|||||||
using System; |
using System; |
||||||
using System.Collections.Generic; |
using System.Collections.Generic; |
||||||
using System.Linq; |
using System.Linq; |
||||||
using CppSharp.AST; |
using CppSharp.AST; |
||||||
using Type = CppSharp.AST.Type; |
using Type = CppSharp.AST.Type; |
||||||
|
|
||||||
namespace CppSharp.Types |
namespace CppSharp.Types |
||||||
{ |
{ |
||||||
public enum CppTypePrintKind |
public enum CppTypePrintKind |
||||||
{ |
{ |
||||||
Local, |
Local, |
||||||
Qualified, |
Qualified, |
||||||
GlobalQualified |
GlobalQualified |
||||||
} |
} |
||||||
|
|
||||||
public class CppTypePrinter : ITypePrinter<string>, IDeclVisitor<string> |
public class CppTypePrinter : ITypePrinter<string>, IDeclVisitor<string> |
||||||
{ |
{ |
||||||
public CppTypePrintKind PrintKind; |
public CppTypePrintKind PrintKind; |
||||||
|
|
||||||
public CppTypePrinter(ITypeMapDatabase database) |
public CppTypePrinter(ITypeMapDatabase database) |
||||||
{ |
{ |
||||||
PrintKind = CppTypePrintKind.GlobalQualified; |
PrintKind = CppTypePrintKind.GlobalQualified; |
||||||
} |
} |
||||||
|
|
||||||
public string VisitTagType(TagType tag, TypeQualifiers quals) |
public string VisitTagType(TagType tag, TypeQualifiers quals) |
||||||
{ |
{ |
||||||
return tag.Declaration.Visit(this); |
return tag.Declaration.Visit(this); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitArrayType(ArrayType array, TypeQualifiers quals) |
public string VisitArrayType(ArrayType array, TypeQualifiers quals) |
||||||
{ |
{ |
||||||
var typeName = array.Type.Visit(this); |
var typeName = array.Type.Visit(this); |
||||||
|
|
||||||
switch (array.SizeType) |
switch (array.SizeType) |
||||||
{ |
{ |
||||||
case ArrayType.ArraySize.Constant: |
case ArrayType.ArraySize.Constant: |
||||||
return string.Format("{0}[{1}]", typeName, array.Size); |
return string.Format("{0}[{1}]", typeName, array.Size); |
||||||
case ArrayType.ArraySize.Variable: |
case ArrayType.ArraySize.Variable: |
||||||
case ArrayType.ArraySize.Dependent: |
case ArrayType.ArraySize.Dependent: |
||||||
return string.Format("{0}[]", typeName); |
return string.Format("{0}[]", typeName); |
||||||
} |
} |
||||||
|
|
||||||
throw new NotSupportedException(); |
throw new NotSupportedException(); |
||||||
} |
} |
||||||
|
|
||||||
static string ConvertModifierToString(PointerType.TypeModifier modifier) |
static string ConvertModifierToString(PointerType.TypeModifier modifier) |
||||||
{ |
{ |
||||||
switch (modifier) |
switch (modifier) |
||||||
{ |
{ |
||||||
case PointerType.TypeModifier.Value: return string.Empty; |
case PointerType.TypeModifier.Value: return string.Empty; |
||||||
case PointerType.TypeModifier.Pointer: return "*"; |
case PointerType.TypeModifier.Pointer: return "*"; |
||||||
case PointerType.TypeModifier.LVReference: return "&"; |
case PointerType.TypeModifier.LVReference: return "&"; |
||||||
case PointerType.TypeModifier.RVReference: return "&&"; |
case PointerType.TypeModifier.RVReference: return "&&"; |
||||||
} |
} |
||||||
|
|
||||||
return string.Empty; |
return string.Empty; |
||||||
} |
} |
||||||
|
|
||||||
public string VisitPointerType(PointerType pointer, TypeQualifiers quals) |
public string VisitPointerType(PointerType pointer, TypeQualifiers quals) |
||||||
{ |
{ |
||||||
var pointee = pointer.Pointee; |
var pointee = pointer.Pointee; |
||||||
|
|
||||||
var function = pointee as FunctionType; |
var function = pointee as FunctionType; |
||||||
if (function != null) |
if (function != null) |
||||||
{ |
{ |
||||||
var arguments = function.Parameters; |
var arguments = function.Parameters; |
||||||
var returnType = function.ReturnType; |
var returnType = function.ReturnType; |
||||||
var args = string.Empty; |
var args = string.Empty; |
||||||
|
|
||||||
if (arguments.Count > 0) |
if (arguments.Count > 0) |
||||||
args = VisitParameters(function.Parameters, hasNames: false); |
args = VisitParameters(function.Parameters, hasNames: false); |
||||||
|
|
||||||
return string.Format("{0} (*)({1})", returnType.Visit(this), args); |
return string.Format("{0} (*)({1})", returnType.Visit(this), args); |
||||||
} |
} |
||||||
|
|
||||||
var pointeeType = pointer.Pointee.Visit(this, quals); |
var pointeeType = pointer.Pointee.Visit(this, quals); |
||||||
var mod = ConvertModifierToString(pointer.Modifier); |
var mod = ConvertModifierToString(pointer.Modifier); |
||||||
|
|
||||||
var s = quals.IsConst ? "const " : string.Empty; |
var s = quals.IsConst ? "const " : string.Empty; |
||||||
s += string.Format("{0}{1}", pointeeType, mod); |
s += string.Format("{0}{1}", pointeeType, mod); |
||||||
|
|
||||||
return s; |
return s; |
||||||
} |
} |
||||||
|
|
||||||
public string VisitMemberPointerType(MemberPointerType member, TypeQualifiers quals) |
public string VisitMemberPointerType(MemberPointerType member, TypeQualifiers quals) |
||||||
{ |
{ |
||||||
return string.Empty; |
return string.Empty; |
||||||
} |
} |
||||||
|
|
||||||
public string VisitBuiltinType(BuiltinType builtin, TypeQualifiers quals) |
public string VisitBuiltinType(BuiltinType builtin, TypeQualifiers quals) |
||||||
{ |
{ |
||||||
return VisitPrimitiveType(builtin.Type); |
return VisitPrimitiveType(builtin.Type); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitPrimitiveType(PrimitiveType primitive) |
public string VisitPrimitiveType(PrimitiveType primitive) |
||||||
{ |
{ |
||||||
switch (primitive) |
switch (primitive) |
||||||
{ |
{ |
||||||
case PrimitiveType.Bool: return "bool"; |
case PrimitiveType.Bool: return "bool"; |
||||||
case PrimitiveType.Void: return "void"; |
case PrimitiveType.Void: return "void"; |
||||||
case PrimitiveType.WideChar: return "char"; |
case PrimitiveType.WideChar: return "char"; |
||||||
case PrimitiveType.Int8: return "char"; |
case PrimitiveType.Int8: return "char"; |
||||||
case PrimitiveType.UInt8: return "unsigned char"; |
case PrimitiveType.UInt8: return "unsigned char"; |
||||||
case PrimitiveType.Int16: return "short"; |
case PrimitiveType.Int16: return "short"; |
||||||
case PrimitiveType.UInt16: return "unsigned short"; |
case PrimitiveType.UInt16: return "unsigned short"; |
||||||
case PrimitiveType.Int32: return "int"; |
case PrimitiveType.Int32: return "int"; |
||||||
case PrimitiveType.UInt32: return "unsigned int"; |
case PrimitiveType.UInt32: return "unsigned int"; |
||||||
case PrimitiveType.Int64: return "long long"; |
case PrimitiveType.Int64: return "long long"; |
||||||
case PrimitiveType.UInt64: return "unsigned long long"; |
case PrimitiveType.UInt64: return "unsigned long long"; |
||||||
case PrimitiveType.Float: return "float"; |
case PrimitiveType.Float: return "float"; |
||||||
case PrimitiveType.Double: return "double"; |
case PrimitiveType.Double: return "double"; |
||||||
case PrimitiveType.IntPtr: return "void*"; |
case PrimitiveType.IntPtr: return "void*"; |
||||||
} |
} |
||||||
|
|
||||||
throw new NotSupportedException(); |
throw new NotSupportedException(); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitTypedefType(TypedefType typedef, TypeQualifiers quals) |
public string VisitTypedefType(TypedefType typedef, TypeQualifiers quals) |
||||||
{ |
{ |
||||||
return GetDeclName(typedef.Declaration); |
return GetDeclName(typedef.Declaration); |
||||||
} |
} |
||||||
|
|
||||||
public string GetDeclName(Declaration declaration) |
public string GetDeclName(Declaration declaration) |
||||||
{ |
{ |
||||||
switch (PrintKind) |
switch (PrintKind) |
||||||
{ |
{ |
||||||
case CppTypePrintKind.Local: |
case CppTypePrintKind.Local: |
||||||
return declaration.OriginalName; |
return declaration.OriginalName; |
||||||
case CppTypePrintKind.Qualified: |
case CppTypePrintKind.Qualified: |
||||||
return declaration.QualifiedOriginalName; |
return declaration.QualifiedOriginalName; |
||||||
case CppTypePrintKind.GlobalQualified: |
case CppTypePrintKind.GlobalQualified: |
||||||
return "::" + declaration.QualifiedOriginalName; |
return "::" + declaration.QualifiedOriginalName; |
||||||
} |
} |
||||||
|
|
||||||
throw new NotSupportedException(); |
throw new NotSupportedException(); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitDecayedType(DecayedType decayed, TypeQualifiers quals) |
public string VisitDecayedType(DecayedType decayed, TypeQualifiers quals) |
||||||
{ |
{ |
||||||
return decayed.Decayed.Visit(this); |
return decayed.Decayed.Visit(this); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitTemplateSpecializationType(TemplateSpecializationType template, TypeQualifiers quals) |
public string VisitTemplateSpecializationType(TemplateSpecializationType template, TypeQualifiers quals) |
||||||
{ |
{ |
||||||
return string.Format("{0}<{1}>", template.Template.TemplatedDecl.Visit(this), |
return string.Format("{0}<{1}>", template.Template.TemplatedDecl.Visit(this), |
||||||
string.Join(", ", |
string.Join(", ", |
||||||
template.Arguments.Where( |
template.Arguments.Where( |
||||||
a => a.Type.Type != null && |
a => a.Type.Type != null && |
||||||
!(a.Type.Type is DependentNameType)).Select(a => a.Type.Visit(this)))); |
!(a.Type.Type is DependentNameType)).Select(a => a.Type.Visit(this)))); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitTemplateParameterType(TemplateParameterType param, TypeQualifiers quals) |
public string VisitTemplateParameterType(TemplateParameterType param, TypeQualifiers quals) |
||||||
{ |
{ |
||||||
if (param.Parameter.Name == null) |
if (param.Parameter.Name == null) |
||||||
return string.Empty; |
return string.Empty; |
||||||
|
|
||||||
return param.Parameter.Name; |
return param.Parameter.Name; |
||||||
} |
} |
||||||
|
|
||||||
public string VisitTemplateParameterSubstitutionType( |
public string VisitTemplateParameterSubstitutionType( |
||||||
TemplateParameterSubstitutionType param, TypeQualifiers quals) |
TemplateParameterSubstitutionType param, TypeQualifiers quals) |
||||||
{ |
{ |
||||||
return param.Replacement.Visit(this); |
return param.Replacement.Visit(this); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitInjectedClassNameType(InjectedClassNameType injected, TypeQualifiers quals) |
public string VisitInjectedClassNameType(InjectedClassNameType injected, TypeQualifiers quals) |
||||||
{ |
{ |
||||||
return injected.Class.Visit(this); |
return injected.Class.Visit(this); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitDependentNameType(DependentNameType dependent, TypeQualifiers quals) |
public string VisitDependentNameType(DependentNameType dependent, TypeQualifiers quals) |
||||||
{ |
{ |
||||||
throw new System.NotImplementedException(); |
throw new System.NotImplementedException(); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitCILType(CILType type, TypeQualifiers quals) |
public string VisitCILType(CILType type, TypeQualifiers quals) |
||||||
{ |
{ |
||||||
return string.Empty; |
return string.Empty; |
||||||
} |
} |
||||||
|
|
||||||
public string VisitPrimitiveType(PrimitiveType type, TypeQualifiers quals) |
public string VisitPrimitiveType(PrimitiveType type, TypeQualifiers quals) |
||||||
{ |
{ |
||||||
throw new System.NotImplementedException(); |
throw new System.NotImplementedException(); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitDeclaration(Declaration decl, TypeQualifiers quals) |
public string VisitDeclaration(Declaration decl, TypeQualifiers quals) |
||||||
{ |
{ |
||||||
throw new System.NotImplementedException(); |
throw new System.NotImplementedException(); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitFunctionType(FunctionType function, TypeQualifiers quals) |
public string VisitFunctionType(FunctionType function, TypeQualifiers quals) |
||||||
{ |
{ |
||||||
var arguments = function.Parameters; |
var arguments = function.Parameters; |
||||||
var returnType = function.ReturnType; |
var returnType = function.ReturnType; |
||||||
var args = string.Empty; |
var args = string.Empty; |
||||||
|
|
||||||
if (arguments.Count > 0) |
if (arguments.Count > 0) |
||||||
args = VisitParameters(function.Parameters, hasNames: false); |
args = VisitParameters(function.Parameters, hasNames: false); |
||||||
|
|
||||||
return string.Format("{0} ({1})", returnType.Visit(this), args); |
return string.Format("{0} ({1})", returnType.Visit(this), args); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitParameters(IEnumerable<Parameter> @params, |
public string VisitParameters(IEnumerable<Parameter> @params, |
||||||
bool hasNames) |
bool hasNames) |
||||||
{ |
{ |
||||||
var args = new List<string>(); |
var args = new List<string>(); |
||||||
|
|
||||||
foreach (var param in @params) |
foreach (var param in @params) |
||||||
args.Add(VisitParameter(param, hasNames)); |
args.Add(VisitParameter(param, hasNames)); |
||||||
|
|
||||||
return string.Join(", ", args); |
return string.Join(", ", args); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitParameter(Parameter arg, bool hasName = true) |
public string VisitParameter(Parameter arg, bool hasName = true) |
||||||
{ |
{ |
||||||
var type = arg.Type.Visit(this, arg.QualifiedType.Qualifiers); |
var type = arg.Type.Visit(this, arg.QualifiedType.Qualifiers); |
||||||
var name = arg.Name; |
var name = arg.Name; |
||||||
|
|
||||||
if (hasName && !string.IsNullOrEmpty(name)) |
if (hasName && !string.IsNullOrEmpty(name)) |
||||||
return string.Format("{0} {1}", type, name); |
return string.Format("{0} {1}", type, name); |
||||||
|
|
||||||
return type; |
return type; |
||||||
} |
} |
||||||
|
|
||||||
public string VisitDelegate(FunctionType function) |
public string VisitDelegate(FunctionType function) |
||||||
{ |
{ |
||||||
throw new System.NotImplementedException(); |
throw new System.NotImplementedException(); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitDeclaration(Declaration decl) |
public string VisitDeclaration(Declaration decl) |
||||||
{ |
{ |
||||||
return GetDeclName(decl); |
return GetDeclName(decl); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitClassDecl(Class @class) |
public string VisitClassDecl(Class @class) |
||||||
{ |
{ |
||||||
return GetDeclName(@class); |
return GetDeclName(@class); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitFieldDecl(Field field) |
public string VisitFieldDecl(Field field) |
||||||
{ |
{ |
||||||
return VisitDeclaration(field); |
return VisitDeclaration(field); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitFunctionDecl(Function function) |
public string VisitFunctionDecl(Function function) |
||||||
{ |
{ |
||||||
return VisitDeclaration(function); |
return VisitDeclaration(function); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitMethodDecl(Method method) |
public string VisitMethodDecl(Method method) |
||||||
{ |
{ |
||||||
return VisitDeclaration(method); |
return VisitDeclaration(method); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitParameterDecl(Parameter parameter) |
public string VisitParameterDecl(Parameter parameter) |
||||||
{ |
{ |
||||||
return VisitParameter(parameter, hasName: false); |
return VisitParameter(parameter, hasName: false); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitTypedefDecl(TypedefDecl typedef) |
public string VisitTypedefDecl(TypedefDecl typedef) |
||||||
{ |
{ |
||||||
return VisitDeclaration(typedef); |
return VisitDeclaration(typedef); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitEnumDecl(Enumeration @enum) |
public string VisitEnumDecl(Enumeration @enum) |
||||||
{ |
{ |
||||||
return VisitDeclaration(@enum); |
return VisitDeclaration(@enum); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitVariableDecl(Variable variable) |
public string VisitVariableDecl(Variable variable) |
||||||
{ |
{ |
||||||
return VisitDeclaration(variable); |
return VisitDeclaration(variable); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitClassTemplateDecl(ClassTemplate template) |
public string VisitClassTemplateDecl(ClassTemplate template) |
||||||
{ |
{ |
||||||
return VisitDeclaration(template); |
return VisitDeclaration(template); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitFunctionTemplateDecl(FunctionTemplate template) |
public string VisitFunctionTemplateDecl(FunctionTemplate template) |
||||||
{ |
{ |
||||||
return VisitDeclaration(template); |
return VisitDeclaration(template); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitMacroDefinition(MacroDefinition macro) |
public string VisitMacroDefinition(MacroDefinition macro) |
||||||
{ |
{ |
||||||
throw new NotImplementedException(); |
throw new NotImplementedException(); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitNamespace(Namespace @namespace) |
public string VisitNamespace(Namespace @namespace) |
||||||
{ |
{ |
||||||
return VisitDeclaration(@namespace); |
return VisitDeclaration(@namespace); |
||||||
} |
} |
||||||
|
|
||||||
public string VisitEvent(Event @event) |
public string VisitEvent(Event @event) |
||||||
{ |
{ |
||||||
return string.Empty; |
return string.Empty; |
||||||
} |
} |
||||||
|
|
||||||
public string VisitProperty(Property property) |
public string VisitProperty(Property property) |
||||||
{ |
{ |
||||||
return VisitDeclaration(property); |
return VisitDeclaration(property); |
||||||
} |
} |
||||||
|
|
||||||
public string ToString(Type type) |
public string ToString(Type type) |
||||||
{ |
{ |
||||||
return type.Visit(this); |
return type.Visit(this); |
||||||
} |
} |
||||||
} |
} |
||||||
} |
} |
||||||
|
|||||||
Loading…
Reference in new issue