1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! # Generic Parameter Handling
//!
//! This module provides utilities for extracting and analyzing generic parameters
//! from Rust syntax structures. It offers a unified interface for working with
//! different kinds of generic parameters (lifetimes, types, const parameters)
//! in procedural macros.
//!
//! ## Overview
//!
//! Generic parameter handling is scattered across multiple functions in the
//! documentation macro system. This module provides the foundational utilities,
//! while more complex analysis (such as bound extraction) is handled in
//! [`analyze_generics`].
//!
//! ## Parameter Categories
//!
//! Rust supports three kinds of generic parameters:
//!
//! 1. **Lifetime Parameters**: `'a`, `'static`, etc.
//! 2. **Type Parameters**: `T`, `U`, `A`, `B`, etc.
//! 3. **Const Parameters**: `const N: usize`, etc.
//!
//! Different functions in this module extract different subsets of these parameters.
//!
//! ## Available Interface
//!
//! The module provides free functions for generic analysis:
//!
//! ### Type Parameters Only - [`get_type_parameters`]
//!
//! Extracts only type parameter names, filtering out lifetimes and const parameters.
//! This is the most commonly used operation in the documentation system.
//!
//! **Use when:** You need type parameter names for type variable tracking,
//! generic substitution, or HM type conversion.
//!
//! **Example:**
//! ```rust,ignore
//! // Given: fn foo<'a, T, U, const N: usize>(x: &'a T) -> [U; N]
//! let generics: syn::Generics = /* ... */;
//! let type_params = get_type_parameters(&generics);
//! // Returns: ["T", "U"]
//! // Omits: 'a (lifetime), N (const)
//! ```
//!
//! **Used in:**
//! - [`function_utils::type_to_hm`] - Building the `generic_names` set for type conversion
//! - [`document_module::resolution::get_self_type_info`] - Extracting impl block type parameters
//! - [`document_module::resolution::normalize_type`] - Type parameter normalization
//!
//! ### All Parameters - [`get_all_parameters`]
//!
//! Extracts names of all generic parameters including lifetimes, types, and const parameters.
//!
//! **Use when:** You need the complete list of generic parameter names for
//! comprehensive analysis or display purposes.
//!
//! **Example:**
//! ```rust,ignore
//! // Given: fn foo<'a, T, U, const N: usize>(x: &'a T) -> [U; N]
//! let generics: syn::Generics = /* ... */;
//! let all_params = get_all_parameters(&generics);
//! // Returns: ["'a", "T", "U", "N"]
//! // Includes all parameter kinds
//! ```
//!
//! **Used in:**
//! - [`document_module::generation::process_doc_type_params`] - Comprehensive parameter tracking
//! - Useful for debugging and diagnostic purposes
//!
//! ## Relationship to Other Generic Handling
//!
//! This module provides **name extraction only**. More complex generic parameter
//! analysis is performed elsewhere:
//!
//! ### [`analyze_generics`]
//!
//! **Purpose:** Analyzes type parameter bounds and extracts function signatures
//! from trait bounds (e.g., `F: Fn(A) -> B`).
//!
//! **Returns:** A `HashMap<String, HMType>` mapping type parameter names to their
//! function signatures (if they have `Fn`-trait bounds).
//!
//! **Example:**
//! ```rust,ignore
//! fn map<A, B, F>(f: F, fa: Self) -> Self
//! where
//! F: Fn(A) -> B,
//! A: Clone,
//! {
//! // analyze_generics returns:
//! // { "F": Arrow(Variable("A"), Variable("B")) }
//! // Note: A and B have no Fn bounds, so they're not in the map
//! }
//! ```
//!
//! ### [`document_module::resolution::merge_generics`]
//!
//! **Purpose:** Merges generic parameters from impl blocks with function signatures,
//! maintaining proper ordering (lifetimes, types, consts).
//!
//! **Example:**
//! ```rust,ignore
//! impl<'a, T> Functor for CatList<T> {
//! fn map<'b, U, F>(self, f: F) -> Self
//! // merge_generics produces: <'a, 'b, T, U, F>
//! // Order: lifetimes first, then types
//! }
//! ```
//!
//! ## Usage Patterns
//!
//! ### Pattern 1: Type Variable Tracking
//!
//! The most common pattern is to extract type parameters for tracking which
//! identifiers are type variables vs. concrete types:
//!
//! ```rust,ignore
//! fn process_signature(sig: &syn::Signature) {
//! // Extract type parameter names as a set
//! let generic_names = get_type_parameters_set(&sig.generics);
//!
//! // Now use generic_names to distinguish type variables from concrete types
//! for ty in &sig.inputs {
//! if let Some(ident) = ty.path.get_ident() {
//! if generic_names.contains(&ident) {
//! // This is a type variable (e.g., T, A)
//! } else {
//! // This is a concrete type (e.g., Option, Vec)
//! }
//! }
//! }
//! }
//! ```
//!
//! ### Pattern 2: Impl Block Analysis
//!
//! Extract type parameters from impl blocks for Self resolution:
//!
//! ```rust,ignore
//! fn analyze_impl(item_impl: &syn::ItemImpl) {
//! // For: impl<A, B> Functor for CatList<A>
//! let impl_type_params = get_type_parameters(&item_impl.generics);
//! // Returns: ["A", "B"]
//!
//! // Can now track which parameters belong to the impl
//! // vs. individual methods
//! }
//! ```
//!
//! ### Pattern 3: Combined with Bound Analysis
//!
//! Often combined with bound analysis for complete generic understanding:
//!
//! ```rust,ignore
//! use crate::analysis::generics::{get_type_parameters_set, analyze_fn_bounds};
//!
//! fn analyze_function(sig: &syn::Signature, config: &Config) {
//! // Get type parameter names as a set
//! let generic_names = get_type_parameters_set(&sig.generics);
//!
//! // Get function bounds for those parameters
//! let fn_bounds = analyze_fn_bounds(sig, config);
//!
//! // Now we have:
//! // - generic_names: Which identifiers are type variables
//! // - fn_bounds: What function signatures they represent (if any)
//! }
//! ```
//!
//! ## Design Rationale
//!
//! ### Why Free Functions?
//!
//! The module uses free functions instead of a struct because:
//!
//! 1. **Simplicity**: Most operations are pure transformations of `syn::Generics`.
//!
//! 2. **Rust Idioms**: Procedural macro analysis often uses free functions or
//! trait-based extensions for syntax tree nodes.
//!
//! 3. **Reduced Boilerplate**: Eliminates the need to instantiate an analyzer
//! struct for simple extractions.
//!
//! ### Why Separate Functions?
//!
//! The module provides separate functions ([`get_type_parameters`],
//! [`get_all_parameters`], [`get_type_parameters_set`],
//! [`analyze_fn_bounds`]) because:
//!
//! 1. **Type parameters are special**: In HM-style type signatures, only type
//! parameters appear as variables. Lifetimes and const parameters are not
//! part of the HM type system.
//!
//! 2. **Performance**: Most call sites only need type parameters, so dedicated
//! functions avoid unnecessary allocations and conversions.
//!
//! 3. **Clarity**: Explicit function names make the intent clear at call sites.
//!
//! ### Why analyze_fn_bounds Takes Extra Parameters?
//!
//! The [`analyze_fn_bounds`] function requires a `Signature` and `Config` because:
//!
//! 1. **Where Clauses**: Function bounds can appear in where clauses, which are part of the
//! signature, not just the `Generics`.
//!
//! 2. **Context-Dependent**: Bound analysis requires configuration (e.g., which traits to ignore)
//! and cross-references between parameters.
//!
//! 3. **Separation of Concerns**: Simple extraction (type params, all params) doesn't need
//! external context, while semantic analysis (bounds) does.
use ;
/// Extracts only type parameter names.
///
/// This returns type parameters like `T`, `U`, filtering out lifetimes
/// and const parameters.
///
/// * `generics` - The generics to extract type parameters from
///
/// ### Returns
///
/// A vector of type parameter names as strings.
///
/// ### Example
///
/// For `<'a, T, U, const N: usize>`, returns `["T", "U"]`.
/// Extracts all generic parameter names.
///
/// This returns all parameters including lifetimes, type parameters,
/// and const parameters.
///
/// * `generics` - The generics to extract parameters from
///
/// ### Returns
///
/// A vector of all parameter names as strings.
///
/// ### Example
///
/// For `<'a, T, U, const N: usize>`, returns `["'a", "T", "U", "N"]`.
/// Extracts type parameter names as a HashSet.
///
/// This is a convenience function for when you need to perform membership
/// checks to determine if an identifier is a type variable.
///
/// * `generics` - The generics to extract type parameters from
///
/// ### Returns
///
/// A `HashSet<String>` of type parameter names.
///
/// ### Example
///
/// ```rust,ignore
/// let type_params = type_parameters_to_set(&sig.generics);
///
/// if type_params.contains("T") {
/// // "T" is a type variable
/// }
/// ```
/// Analyzes function trait bounds for generic parameters.
///
/// This function extracts function signatures from trait bounds like
/// `F: Fn(A) -> B`, returning a map from type parameter names to
/// their HM type representations.
///
/// * `sig` - The function signature containing the generics and where clause
/// * `config` - Configuration for type conversion
///
/// ### Returns
///
/// A `HashMap<String, HMType>` mapping type parameter names to their
/// function signatures (only includes parameters with `Fn*` trait bounds).
///
/// ### Example
///
/// For a function signature:
/// ```rust,ignore
/// fn map<A, B, F>(f: F, fa: Self) -> Self
/// where
/// F: Fn(A) -> B,
/// A: Clone,
/// ```
///
/// Returns:
/// ```rust,ignore
/// {
/// "F": Arrow(Variable("a"), Variable("b"))
/// }
/// ```
///
/// Note: `A` and `B` have no `Fn*` bounds, so they're not in the result.
// -- Bounds collection helpers --
/// Collect all trait bounds for a named type parameter from both inline
/// generic parameter bounds and where-clause predicates.
///
/// This eliminates the common pattern of scanning inline bounds then
/// where-clause bounds separately, which appears throughout the
/// dispatch analysis module.
/// Iterate all where-clause predicates that bind a simple type parameter
/// (i.e., `WherePredicate::Type` with a single-ident bounded type).
///
/// Yields `(param_name, bounds)` pairs. Skips predicates with complex
/// bounded types (qualified paths, tuples, etc.).
/// Extract the last segment's ident from a trait bound's path.
///
/// Returns the trait name (e.g., `Functor` from `crate::classes::Functor`
/// or `Kind_abc123` from a qualified path).
/// Check if a bounded type is a simple identifier matching the given name.
/// Extract lifetime references from generic parameters.
/// Extract type parameter idents from generic parameters.
/// Analyze a function signature to extract generic names and function bounds.
///
/// Returns a tuple of (generic_names, fn_bounds) for use in type conversion.