diff --git a/binder/Generators/C/CTypePrinter.cs b/binder/Generators/C/CTypePrinter.cs deleted file mode 100644 index 2d831ee..0000000 --- a/binder/Generators/C/CTypePrinter.cs +++ /dev/null @@ -1,486 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using CppSharp.AST; -using CppSharp.AST.Extensions; -using Type = CppSharp.AST.Type; - -namespace CppSharp.Types -{ - public enum TypePrinterContextKind - { - Normal, - Template - } - - public abstract class TypePrinterContext - { - protected TypePrinterContext() - { - Kind = TypePrinterContextKind.Normal; - } - - protected TypePrinterContext(TypePrinterContextKind kind) - { - Kind = kind; - } - - public string GetTemplateParameterList() - { - if (Kind == TypePrinterContextKind.Template) - { - var template = (Template)Declaration; - return string.Join(", ", template.Parameters.Select(p => p.Name)); - } - - var type = Type.Desugar(); - IEnumerable templateArgs; - var templateSpecializationType = type as TemplateSpecializationType; - if (templateSpecializationType != null) - templateArgs = templateSpecializationType.Arguments; - else - templateArgs = ((ClassTemplateSpecialization)((TagType)type).Declaration).Arguments; - - var paramsList = new List(); - foreach (var arg in templateArgs.Where(a => a.Kind == TemplateArgument.ArgumentKind.Type)) - { - var argType = arg.Type.Type.IsPointerToPrimitiveType() - ? new CILType(typeof(System.IntPtr)) - : arg.Type.Type; - paramsList.Add(argType.ToString()); - } - - return string.Join(", ", paramsList); - } - - public TypePrinterContextKind Kind; - public Declaration Declaration; - public Parameter Parameter; - public Type Type; - } - - public interface ITypePrinter - { - string ToString(Type type); - } - - public interface ITypePrinter : ITypePrinter, ITypeVisitor - { - T VisitParameters(IEnumerable @params, bool hasNames); - T VisitParameter(Parameter param, bool hasName = true); - - T VisitDelegate(FunctionType function); - } - - public enum CppTypePrintScopeKind - { - Local, - Qualified, - GlobalQualified - } - - public enum CppTypePrintFlavorKind - { - C, - Cpp, - } - - static class CLITypePrinter - { - public static string Visit(CILType type, TypeQualifiers quals) - { - if (type.Type == typeof(string)) - return "char*"; - - throw new NotImplementedException(); - } - } - - public class CppTypePrinter : ITypePrinter, IDeclVisitor - { - public CppTypePrintFlavorKind PrintFlavorKind; - public CppTypePrintScopeKind PrintScopeKind; - public bool PrintLogicalNames; - public bool PrintTypeQualifiers; - - public CppTypePrinter(bool printTypeQualifiers = true) - { - PrintFlavorKind = CppTypePrintFlavorKind.Cpp; - PrintScopeKind = CppTypePrintScopeKind.GlobalQualified; - PrintTypeQualifiers = printTypeQualifiers; - } - - public bool ResolveTypedefs { get; set; } - - public string VisitTagType(TagType tag, TypeQualifiers quals) - { - return tag.Declaration.Visit(this); - } - - public string VisitArrayType(ArrayType array, TypeQualifiers quals) - { - var typeName = array.Type.Visit(this); - - switch (array.SizeType) - { - case ArrayType.ArraySize.Constant: - return string.Format("{0}[{1}]", typeName, array.Size); - case ArrayType.ArraySize.Variable: - case ArrayType.ArraySize.Dependent: - case ArrayType.ArraySize.Incomplete: - return string.Format("{0}[]", typeName); - } - - throw new NotSupportedException(); - } - - static string ConvertModifierToString(PointerType.TypeModifier modifier) - { - switch (modifier) - { - case PointerType.TypeModifier.Value: return string.Empty; - case PointerType.TypeModifier.Pointer: return "*"; - case PointerType.TypeModifier.LVReference: return "&"; - case PointerType.TypeModifier.RVReference: return "&&"; - } - - return string.Empty; - } - - public string VisitPointerType(PointerType pointer, TypeQualifiers quals) - { - var pointee = pointer.Pointee; - - var function = pointee as FunctionType; - if (function != null) - { - var arguments = function.Parameters; - var returnType = function.ReturnType; - var args = string.Empty; - - if (arguments.Count > 0) - args = VisitParameters(function.Parameters, hasNames: false); - - return string.Format("{0} (*)({1})", returnType.Visit(this), args); - } - - var pointeeType = pointer.Pointee.Visit(this, quals); - var mod = ConvertModifierToString(pointer.Modifier); - - var s = PrintTypeQualifiers && quals.IsConst ? "const " : string.Empty; - s += string.Format("{0}{1}", pointeeType, mod); - - return s; - } - - public string VisitMemberPointerType(MemberPointerType member, TypeQualifiers quals) - { - return string.Empty; - } - - public string VisitBuiltinType(BuiltinType builtin, TypeQualifiers quals) - { - return VisitPrimitiveType(builtin.Type); - } - - public string VisitPrimitiveType(PrimitiveType primitive) - { - switch (primitive) - { - case PrimitiveType.Bool: return "bool"; - case PrimitiveType.Void: return "void"; - case PrimitiveType.Char16: - case PrimitiveType.WideChar: return "wchar_t"; - case PrimitiveType.Char: return "char"; - case PrimitiveType.UChar: return "unsigned char"; - case PrimitiveType.Short: return "short"; - case PrimitiveType.UShort: return "unsigned short"; - case PrimitiveType.Int: return "int"; - case PrimitiveType.UInt: return "unsigned int"; - case PrimitiveType.Long: return "long"; - case PrimitiveType.ULong: return "unsigned long"; - case PrimitiveType.LongLong: return "long long"; - case PrimitiveType.ULongLong: return "unsigned long long"; - case PrimitiveType.Float: return "float"; - case PrimitiveType.Double: return "double"; - case PrimitiveType.LongDouble: return "long double"; - case PrimitiveType.IntPtr: return "void*"; - case PrimitiveType.UIntPtr: return "uintptr_t"; - case PrimitiveType.Null: return "std::nullptr_t"; - } - - throw new NotSupportedException(); - } - - public string VisitTypedefType(TypedefType typedef, TypeQualifiers quals) - { - if (ResolveTypedefs) - { - var decl = typedef.Declaration; - FunctionType func; - return decl.Type.IsPointerTo(out func) ? VisitDeclaration(decl) : decl.Type.Visit(this); - } - return GetDeclName(typedef.Declaration); - } - - public string VisitAttributedType(AttributedType attributed, TypeQualifiers quals) - { - return attributed.Modified.Visit(this); - } - - public string VisitDecayedType(DecayedType decayed, TypeQualifiers quals) - { - return decayed.Decayed.Visit(this); - } - - public string VisitTemplateSpecializationType(TemplateSpecializationType template, TypeQualifiers quals) - { - var specialization = template.GetClassTemplateSpecialization(); - if (specialization == null) - return string.Empty; - - return VisitClassTemplateSpecializationDecl(specialization); - } - - public string VisitDependentTemplateSpecializationType( - DependentTemplateSpecializationType template, TypeQualifiers quals) - { - if (template.Desugared != null) - return template.Desugared.Visit(this); - return string.Empty; - } - - public string VisitTemplateParameterType(TemplateParameterType param, TypeQualifiers quals) - { - if (param.Parameter.Name == null) - return string.Empty; - - return param.Parameter.Name; - } - - public string VisitTemplateParameterSubstitutionType( - TemplateParameterSubstitutionType param, TypeQualifiers quals) - { - return param.Replacement.Visit(this); - } - - public string VisitInjectedClassNameType(InjectedClassNameType injected, TypeQualifiers quals) - { - return injected.Class.Visit(this); - } - - public string VisitDependentNameType(DependentNameType dependent, TypeQualifiers quals) - { - return dependent.Desugared != null ? dependent.Desugared.Visit(this) : string.Empty; - } - - public string VisitPackExpansionType(PackExpansionType packExpansionType, TypeQualifiers quals) - { - return string.Empty; - } - - public string VisitCILType(CILType type, TypeQualifiers quals) - { - return CLITypePrinter.Visit(type, quals); - } - - public string VisitPrimitiveType(PrimitiveType type, TypeQualifiers quals) - { - throw new System.NotImplementedException(); - } - - public string VisitDeclaration(Declaration decl, TypeQualifiers quals) - { - throw new System.NotImplementedException(); - } - - public string VisitFunctionType(FunctionType function, TypeQualifiers quals) - { - var arguments = function.Parameters; - var returnType = function.ReturnType; - var args = string.Empty; - - if (arguments.Count > 0) - args = VisitParameters(function.Parameters, hasNames: false); - - return string.Format("{0} ({1})", returnType.Visit(this), args); - } - - public string VisitParameters(IEnumerable @params, - bool hasNames) - { - var args = new List(); - - foreach (var param in @params) - args.Add(VisitParameter(param, hasNames)); - - return string.Join(", ", args); - } - - public string VisitParameter(Parameter arg, bool hasName = true) - { - var type = arg.Type.Visit(this, arg.QualifiedType.Qualifiers); - var name = arg.Name; - - if (hasName && !string.IsNullOrEmpty(name)) - return string.Format("{0} {1}", type, name); - - return type; - } - - public string VisitDelegate(FunctionType function) - { - throw new System.NotImplementedException(); - } - - public string GetDeclName(Declaration declaration) - { - switch (PrintScopeKind) - { - case CppTypePrintScopeKind.Local: - return PrintLogicalNames ? declaration.LogicalOriginalName - : declaration.OriginalName; - case CppTypePrintScopeKind.Qualified: - return PrintLogicalNames ? declaration.QualifiedLogicalOriginalName - : declaration.QualifiedOriginalName; - case CppTypePrintScopeKind.GlobalQualified: - return "::" + (PrintLogicalNames ? declaration.QualifiedLogicalOriginalName - : declaration.QualifiedOriginalName); - } - - throw new NotSupportedException(); - } - - public string VisitDeclaration(Declaration decl) - { - return GetDeclName(decl); - } - - public string VisitClassDecl(Class @class) - { - return VisitDeclaration(@class); - } - - public string VisitClassTemplateSpecializationDecl(ClassTemplateSpecialization specialization) - { - return string.Format("{0}<{1}>", specialization.TemplatedDecl.Visit(this), - string.Join(", ", - specialization.Arguments.Where( - a => a.Type.Type != null && - !(a.Type.Type is DependentNameType)).Select(a => a.Type.Visit(this)))); - } - - public string VisitFieldDecl(Field field) - { - return VisitDeclaration(field); - } - - public string VisitFunctionDecl(Function function) - { - return VisitDeclaration(function); - } - - public string VisitMethodDecl(Method method) - { - return VisitDeclaration(method); - } - - public string VisitParameterDecl(Parameter parameter) - { - return VisitParameter(parameter, hasName: false); - } - - public string VisitTypedefDecl(TypedefDecl typedef) - { - return VisitDeclaration(typedef); - } - - public string VisitTypeAliasDecl(TypeAlias typeAlias) - { - return VisitDeclaration(typeAlias); - } - - public string VisitEnumDecl(Enumeration @enum) - { - return VisitDeclaration(@enum); - } - - public string VisitEnumItemDecl(Enumeration.Item item) - { - return VisitDeclaration(item); - } - - public string VisitVariableDecl(Variable variable) - { - return VisitDeclaration(variable); - } - - public string VisitClassTemplateDecl(ClassTemplate template) - { - return VisitDeclaration(template); - } - - public string VisitFunctionTemplateDecl(FunctionTemplate template) - { - return VisitDeclaration(template); - } - - public string VisitMacroDefinition(MacroDefinition macro) - { - throw new NotImplementedException(); - } - - public string VisitNamespace(Namespace @namespace) - { - return VisitDeclaration(@namespace); - } - - public string VisitEvent(Event @event) - { - return string.Empty; - } - - public string VisitProperty(Property property) - { - return VisitDeclaration(property); - } - - public string VisitFriend(Friend friend) - { - throw new NotImplementedException(); - } - - public string ToString(Type type) - { - return type.Visit(this); - } - - public string VisitTemplateTemplateParameterDecl(TemplateTemplateParameter templateTemplateParameter) - { - return templateTemplateParameter.Name; - } - - public string VisitTemplateParameterDecl(TypeTemplateParameter templateParameter) - { - if (templateParameter.DefaultArgument.Type == null) - return templateParameter.Name; - - return string.Format("{0} = {1}", templateParameter.Name, - templateParameter.DefaultArgument.Visit(this)); - } - - public string VisitNonTypeTemplateParameterDecl(NonTypeTemplateParameter nonTypeTemplateParameter) - { - if (nonTypeTemplateParameter.DefaultArgument == null) - return nonTypeTemplateParameter.Name; - - return string.Format("{0} = {1}", nonTypeTemplateParameter.Name, - nonTypeTemplateParameter.DefaultArgument.String); - } - - public string VisitTypeAliasTemplateDecl(TypeAliasTemplate typeAliasTemplate) - { - throw new NotImplementedException(); - } - } -} diff --git a/binder/ReflectionVisitor.cs b/binder/ReflectionVisitor.cs deleted file mode 100644 index bbf3377..0000000 --- a/binder/ReflectionVisitor.cs +++ /dev/null @@ -1,95 +0,0 @@ -using IKVM.Reflection; -using System; - -namespace MonoManagedToNative.Generators -{ - public class ReflectionVisitor - { - public virtual T Visit(Assembly assembly) - { - foreach (var type in assembly.ExportedTypes) - { - Visit(type.GetTypeInfo()); - } - - return default(T); - } - - public T Visit(TypeInfo typeInfo) - { - if (typeInfo.IsClass) - return VisitClass(typeInfo); - else if (typeInfo.IsEnum) - return VisitEnum(typeInfo); - else if (typeInfo.IsInterface) - return VisitInterface(typeInfo); - else if (typeInfo.IsValueType) - return VisitStruct(typeInfo); - - throw new Exception("Could not visit type " + typeInfo.ToString()); - } - - public virtual void VisitMembers(TypeInfo type) - { - foreach (var ctor in type.DeclaredConstructors) - VisitConstructor(ctor); - - foreach (var method in type.DeclaredMethods) - VisitMethod(method); - - foreach (var field in type.DeclaredFields) - VisitField(field); - - foreach (var @event in type.DeclaredEvents) - VisitEvent(@event); - - foreach (var property in type.DeclaredProperties) - VisitProperty(property); - } - - public virtual T VisitClass(TypeInfo @class) - { - return default(T); - } - - public virtual T VisitEnum(TypeInfo @enum) - { - return default(T); - } - - public virtual T VisitInterface(TypeInfo @interface) - { - return default(T); - } - - public virtual T VisitStruct(TypeInfo @struct) - { - return default(T); - } - - public virtual T VisitConstructor(ConstructorInfo ctor) - { - return default(T); - } - - public virtual T VisitMethod(MethodInfo method) - { - return default(T); - } - - public virtual T VisitField(FieldInfo field) - { - return default(T); - } - - public virtual T VisitEvent(EventInfo @event) - { - return default(T); - } - - public virtual T VisitProperty(PropertyInfo @property) - { - return default(T); - } - } -}