PublicApiAnalyzer.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Microsoft.CodeAnalysis;
  4. namespace QuestPDF.InteropGenerators;
  5. /// <summary>
  6. /// Analyzes the QuestPDF public API and extracts methods suitable for interop
  7. /// </summary>
  8. public static class PublicApiAnalyzer
  9. {
  10. /// <summary>
  11. /// Collects all public extension methods from the assembly
  12. /// </summary>
  13. public static List<IMethodSymbol> CollectExtensionMethods(INamespaceSymbol rootNamespace)
  14. {
  15. var methods = new List<IMethodSymbol>();
  16. CollectExtensionMethodsRecursive(rootNamespace, methods);
  17. return methods;
  18. }
  19. /// <summary>
  20. /// Collects all public methods from Fluent API classes (descriptors, configurations, handlers, etc.)
  21. /// </summary>
  22. public static List<IMethodSymbol> CollectFluentApiMethods(INamespaceSymbol rootNamespace)
  23. {
  24. var methods = new List<IMethodSymbol>();
  25. CollectFluentApiMethodsRecursive(rootNamespace, methods);
  26. return methods;
  27. }
  28. /// <summary>
  29. /// Collects all interop-eligible methods: extension methods + fluent API class methods
  30. /// </summary>
  31. public static List<IMethodSymbol> CollectAllInteropMethods(INamespaceSymbol rootNamespace)
  32. {
  33. var methods = new List<IMethodSymbol>();
  34. CollectExtensionMethodsRecursive(rootNamespace, methods);
  35. CollectFluentApiMethodsRecursive(rootNamespace, methods);
  36. return methods;
  37. }
  38. private static void CollectExtensionMethodsRecursive(INamespaceSymbol ns, List<IMethodSymbol> methods)
  39. {
  40. foreach (var member in ns.GetMembers())
  41. {
  42. if (member is INamespaceSymbol childNs)
  43. {
  44. CollectExtensionMethodsRecursive(childNs, methods);
  45. }
  46. else if (member is INamedTypeSymbol type)
  47. {
  48. CollectFromType(type, methods);
  49. }
  50. }
  51. }
  52. private static void CollectFromType(INamedTypeSymbol type, List<IMethodSymbol> methods)
  53. {
  54. if (type.DeclaredAccessibility != Accessibility.Public || type.IsImplicitlyDeclared)
  55. return;
  56. foreach (var member in type.GetMembers())
  57. {
  58. if (member is IMethodSymbol { MethodKind: MethodKind.Ordinary, IsExtensionMethod: true, DeclaredAccessibility: Accessibility.Public } method
  59. && !method.IsImplicitlyDeclared)
  60. {
  61. methods.Add(method);
  62. }
  63. }
  64. foreach (var nestedType in type.GetTypeMembers())
  65. {
  66. CollectFromType(nestedType, methods);
  67. }
  68. }
  69. private static void CollectFluentApiMethodsRecursive(INamespaceSymbol ns, List<IMethodSymbol> methods)
  70. {
  71. foreach (var member in ns.GetMembers())
  72. {
  73. if (member is INamespaceSymbol childNs)
  74. {
  75. CollectFluentApiMethodsRecursive(childNs, methods);
  76. }
  77. else if (member is INamedTypeSymbol type)
  78. {
  79. CollectFluentApiMethodsFromType(type, methods);
  80. }
  81. }
  82. }
  83. private static void CollectFluentApiMethodsFromType(INamedTypeSymbol type, List<IMethodSymbol> methods)
  84. {
  85. if (type.DeclaredAccessibility != Accessibility.Public || type.IsImplicitlyDeclared)
  86. return;
  87. // Check if this is a Fluent API class (descriptors, configurations, handlers, etc.)
  88. if (!IsFluentApiClass(type))
  89. return;
  90. foreach (var member in type.GetMembers())
  91. {
  92. // Collect public instance methods (not extension methods, not constructors, not property accessors)
  93. if (member is IMethodSymbol { MethodKind: MethodKind.Ordinary, IsExtensionMethod: false, DeclaredAccessibility: Accessibility.Public, IsStatic: false } method
  94. && !method.IsImplicitlyDeclared)
  95. {
  96. methods.Add(method);
  97. }
  98. }
  99. // Process nested types
  100. foreach (var nestedType in type.GetTypeMembers())
  101. {
  102. CollectFluentApiMethodsFromType(nestedType, methods);
  103. }
  104. }
  105. /// <summary>
  106. /// Determines if a type is part of the Fluent API (descriptors, configurations, handlers, etc.)
  107. /// </summary>
  108. private static bool IsFluentApiClass(INamedTypeSymbol type)
  109. {
  110. var typeName = type.Name;
  111. var namespaceName = type.ContainingNamespace?.ToDisplayString() ?? "";
  112. // Check if it's in the Fluent namespace
  113. if (namespaceName.Contains("QuestPDF.Fluent"))
  114. return true;
  115. // Check for common Fluent API naming patterns
  116. if (typeName.EndsWith("Descriptor") ||
  117. typeName.EndsWith("Configuration") ||
  118. typeName.EndsWith("Handler") ||
  119. typeName.EndsWith("Builder") ||
  120. typeName.EndsWith("Settings"))
  121. return true;
  122. // Check if the type implements IContainer or IDocumentContainer
  123. foreach (var iface in type.AllInterfaces)
  124. {
  125. var ifaceName = iface.ToDisplayString();
  126. if (ifaceName.Contains("IContainer") || ifaceName.Contains("IDocumentContainer"))
  127. return true;
  128. }
  129. return false;
  130. }
  131. /// <summary>
  132. /// Checks if a method is supported for interop generation
  133. /// </summary>
  134. public static bool IsSupported(IMethodSymbol method)
  135. {
  136. // Async methods are not supported
  137. if (method.IsAsync)
  138. return false;
  139. // Check return type
  140. if (!IsSupportedType(method.ReturnType))
  141. return false;
  142. // Exclude methods that return Task or Task<T>
  143. if (IsTaskRelatedType(method.ReturnType))
  144. return false;
  145. // Check all parameter types
  146. foreach (var parameter in method.Parameters)
  147. {
  148. if (!IsSupportedType(parameter.Type))
  149. return false;
  150. // Exclude methods with Task-related parameters
  151. if (IsTaskRelatedType(parameter.Type))
  152. return false;
  153. // Ref/out parameters are not supported in UnmanagedCallersOnly
  154. if (parameter.RefKind != RefKind.None)
  155. return false;
  156. }
  157. // Generic methods are not supported
  158. if (method.IsGenericMethod)
  159. return false;
  160. return true;
  161. }
  162. /// <summary>
  163. /// Checks if a type is supported for interop
  164. /// </summary>
  165. public static bool IsSupportedType(ITypeSymbol type)
  166. {
  167. // Void is supported
  168. if (type.SpecialType == SpecialType.System_Void)
  169. return true;
  170. // Blittable primitive types
  171. if (type.SpecialType is
  172. SpecialType.System_Boolean or
  173. SpecialType.System_Byte or
  174. SpecialType.System_SByte or
  175. SpecialType.System_Int16 or
  176. SpecialType.System_UInt16 or
  177. SpecialType.System_Int32 or
  178. SpecialType.System_UInt32 or
  179. SpecialType.System_Int64 or
  180. SpecialType.System_UInt64 or
  181. SpecialType.System_Single or
  182. SpecialType.System_Double or
  183. SpecialType.System_IntPtr or
  184. SpecialType.System_UIntPtr)
  185. return true;
  186. // Pointers are supported
  187. if (type.TypeKind == TypeKind.Pointer)
  188. return true;
  189. // Function pointers are supported
  190. if (type.TypeKind == TypeKind.FunctionPointer)
  191. return true;
  192. // Value types (structs) that are blittable might be supported
  193. // For simplicity, we'll allow struct types but developers should ensure they're blittable
  194. if (type.TypeKind == TypeKind.Struct && !type.IsRefLikeType)
  195. return true;
  196. // Enums are supported (backed by primitive types)
  197. if (type.TypeKind == TypeKind.Enum)
  198. return true;
  199. // Classes and interfaces are supported via handle boxing (passed as nint)
  200. if (type.TypeKind is TypeKind.Class or TypeKind.Interface)
  201. return true;
  202. // Generic types, arrays, delegates are not supported
  203. if (type is INamedTypeSymbol { IsGenericType: true })
  204. return false;
  205. if (type.TypeKind == TypeKind.Delegate)
  206. return false;
  207. if (type.TypeKind == TypeKind.Array)
  208. return false;
  209. return false;
  210. }
  211. /// <summary>
  212. /// Checks if a type is a reference type (class or interface)
  213. /// </summary>
  214. public static bool IsReferenceType(ITypeSymbol type)
  215. {
  216. return type.TypeKind is TypeKind.Class or TypeKind.Interface;
  217. }
  218. /// <summary>
  219. /// Checks if a type is Task-related (Task, CancellationToken, etc.)
  220. /// </summary>
  221. public static bool IsTaskRelatedType(ITypeSymbol type)
  222. {
  223. var fullName = type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
  224. // Check for Task, Task<T>, ValueTask, ValueTask<T>
  225. if (fullName.StartsWith("global::System.Threading.Tasks.Task") ||
  226. fullName.StartsWith("global::System.Threading.Tasks.ValueTask"))
  227. return true;
  228. // Check for CancellationToken
  229. if (fullName == "global::System.Threading.CancellationToken")
  230. return true;
  231. // Check for IAsyncEnumerable<T> and IAsyncEnumerator<T>
  232. if (fullName.StartsWith("global::System.Collections.Generic.IAsyncEnumerable") ||
  233. fullName.StartsWith("global::System.Collections.Generic.IAsyncEnumerator"))
  234. return true;
  235. return false;
  236. }
  237. }