gobject-ast 0.1.2

AST representation of GObject C code
Documentation
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
use std::collections::HashMap;

use serde::Serialize;

use crate::model::{
    CallExpression, DefineValue, ExportMacro, Expression, FunctionDoc, ParamSpecAssignment,
    Property, PropertyDoc, Signal, SignalDoc, SourceLocation, Statement, TypeInfo, VariableDecl,
};

#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum Parameter {
    Regular {
        #[serde(skip_serializing_if = "Option::is_none")]
        name: Option<String>,
        type_info: TypeInfo,
        location: SourceLocation,
    },
    Variadic,
}

#[derive(Debug, Clone, Serialize)]
pub struct FunctionDeclItem {
    pub name: String,
    pub return_type: TypeInfo,
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub is_static: bool,
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub is_inline: bool,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub parameters: Vec<Parameter>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub export_macros: Vec<ExportMacro>,
    pub location: SourceLocation,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub doc: Option<FunctionDoc>,
}

#[derive(Debug, Clone, Serialize)]
pub struct FunctionDefItem {
    pub name: String,
    pub return_type: TypeInfo,
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub is_static: bool,
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub is_inline: bool,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub parameters: Vec<Parameter>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub body_statements: Vec<Statement>,
    pub location: SourceLocation,
    #[serde(skip)]
    pub body_location: Option<SourceLocation>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub doc: Option<FunctionDoc>,
}

impl FunctionDefItem {
    /// Find all calls to specific functions in the body
    /// Returns references to all CallExpression nodes that match any of the
    /// given function names
    pub fn find_calls<'a>(&'a self, function_names: &[&str]) -> Vec<&'a CallExpression> {
        self.find_calls_matching(|name| function_names.contains(&name))
    }

    /// Find all calls matching a predicate in the body
    pub fn find_calls_matching<F>(&self, predicate: F) -> Vec<&CallExpression>
    where
        F: Fn(&str) -> bool,
    {
        let mut exprs: Vec<&Expression> = Vec::new();
        for stmt in &self.body_statements {
            stmt.walk_expressions(&mut |expr| exprs.push(expr));
        }

        let mut results = Vec::new();
        for expr in exprs {
            expr.walk(&mut |e| {
                if let Expression::Call(call) = e
                    && call.function_name_str().is_some_and(&predicate)
                {
                    results.push(call);
                }
            });
        }
        results
    }

    /// Extract signal registrations from the function body.
    /// Populates `enum_value` when the signal is assigned via
    /// `signals[ENUM] = g_signal_new(...)`.
    pub fn find_signal_registrations(&self, type_name: &str) -> Vec<Signal> {
        let mut signals = Vec::new();
        let mut seen_names = std::collections::HashSet::new();

        // First pass: assignments like `signals[ENUM] = g_signal_new(...)`
        for (i, stmt) in self.body_statements.iter().enumerate() {
            for assignment in stmt.iter_assignments() {
                let Expression::Call(call) = &*assignment.rhs else {
                    continue;
                };
                if !call.function_contains("g_signal_new") {
                    continue;
                }
                let Some(mut signal) = Signal::from_g_signal_new_call(call) else {
                    continue;
                };
                if let Expression::Subscript(sub) = &*assignment.lhs
                    && let Expression::Identifier(id) = &*sub.index
                {
                    signal.enum_value = Some(id.name.clone());
                }
                if i > 0
                    && let Statement::Comment(c) = &self.body_statements[i - 1]
                {
                    signal.doc = SignalDoc::from_comment_for(c, type_name, &signal.name);
                }
                seen_names.insert(signal.name.clone());
                signals.push(signal);
            }
        }

        // Second pass: standalone g_signal_new calls not already captured
        for (i, stmt) in self.body_statements.iter().enumerate() {
            for call in stmt.iter_calls() {
                if !call.function_name().starts_with("g_signal_new") {
                    continue;
                }
                let Some(name) = call.extract_string_from_arg(0) else {
                    continue;
                };
                if seen_names.contains(&name) {
                    continue;
                }
                if let Some(mut signal) = Signal::from_g_signal_new_call(call) {
                    if i > 0
                        && let Statement::Comment(c) = &self.body_statements[i - 1]
                    {
                        signal.doc = SignalDoc::from_comment_for(c, type_name, &signal.name);
                    }
                    signals.push(signal);
                }
            }
        }

        signals
    }

    /// Iterate all local variable declarations in the function body recursively
    pub fn iter_local_declarations(&self) -> impl Iterator<Item = &VariableDecl> {
        self.body_statements
            .iter()
            .flat_map(Statement::iter_declarations)
    }

    /// Collect all return values from the function body
    pub fn collect_return_values(&self) -> Vec<&Expression> {
        self.body_statements
            .iter()
            .flat_map(Statement::iter_returns)
            .filter_map(|r| r.value.as_ref())
            .collect()
    }

    /// Check if any variable of the given type is directly returned from the
    /// function
    pub fn is_var_returned(&self, type_info: &TypeInfo) -> bool {
        for stmt in &self.body_statements {
            for ret in stmt.iter_returns() {
                if let Some(Expression::Identifier(id)) = &ret.value {
                    // Find the declaration of this identifier in all body statements
                    for body_stmt in &self.body_statements {
                        for decl in body_stmt.iter_declarations() {
                            if decl.name == id.name
                                && decl.type_info.base_type == type_info.base_type
                                && decl.type_info.is_pointer() == type_info.is_pointer()
                            {
                                return true;
                            }
                        }
                    }
                }
            }
        }
        false
    }

    /// Check if any variable of the given type is passed to a cleanup call
    /// (g_object_unref, g_free, etc.)
    pub fn is_var_passed_to_cleanup(&self, type_info: &TypeInfo) -> bool {
        for stmt in &self.body_statements {
            for call in stmt.iter_calls() {
                if call.is_cleanup_call()
                    && let Some(arg) = call.get_arg(0)
                    && let Expression::Identifier(id) = arg
                {
                    // Find the declaration of this identifier
                    for body_stmt in &self.body_statements {
                        for decl in body_stmt.iter_declarations() {
                            if decl.name == id.name
                                && decl.type_info.base_type == type_info.base_type
                                && decl.type_info.is_pointer() == type_info.is_pointer()
                            {
                                return true;
                            }
                        }
                    }
                }
            }
        }
        false
    }

    /// Check if the named variable is passed to a specific function at a
    /// specific argument position
    pub fn is_var_passed_to_function(
        &self,
        var_name: &str,
        func_name: &str,
        arg_index: usize,
    ) -> bool {
        self.body_statements.iter().any(|stmt| {
            stmt.iter_calls().any(|call| {
                call.is_function(func_name)
                    && call.get_arg(arg_index).is_some_and(
                        |arg| matches!(arg, Expression::Identifier(id) if id.name == var_name),
                    )
            })
        })
    }

    /// Check if any variable of the given type is allocated via an allocation
    /// call Uses `call.is_allocation_call()` to detect allocations by
    /// default
    pub fn is_var_allocated(&self, type_info: &TypeInfo) -> bool {
        self.is_var_allocated_with(type_info, CallExpression::is_allocation_call)
    }

    /// Check if any variable of the given type is allocated via a custom
    /// allocation predicate
    pub fn is_var_allocated_with(
        &self,
        type_info: &TypeInfo,
        is_allocation: impl Fn(&CallExpression) -> bool,
    ) -> bool {
        for stmt in &self.body_statements {
            let mut found = false;
            stmt.walk(&mut |s| {
                match s {
                    // Check init: Type *var = allocation_call()
                    Statement::Declaration(decl)
                        if decl.type_info.base_type == type_info.base_type
                            && decl.type_info.is_pointer() == type_info.is_pointer() =>
                    {
                        let is_alloc_init = match &decl.initializer {
                            Some(Expression::Call(call)) => is_allocation(call),
                            Some(Expression::AllocCall(_)) => true,
                            _ => false,
                        };
                        if is_alloc_init {
                            found = true;
                        }
                    }
                    // Check assignment: var = allocation_call()
                    Statement::Expression(expr_stmt) => {
                        if let Expression::Assignment(assign) = expr_stmt.as_ref()
                            && let Expression::Identifier(id) = &*assign.lhs
                        {
                            let is_alloc_rhs = match &*assign.rhs {
                                Expression::Call(call) => is_allocation(call),
                                Expression::AllocCall(_) => true,
                                _ => false,
                            };
                            if is_alloc_rhs {
                                // Find the declaration of the assigned variable
                                for body_stmt in &self.body_statements {
                                    for decl in body_stmt.iter_declarations() {
                                        if decl.name == id.name
                                            && decl.type_info.base_type == type_info.base_type
                                            && decl.type_info.is_pointer() == type_info.is_pointer()
                                        {
                                            found = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    _ => {}
                }
            });
            if found {
                return true;
            }
        }
        false
    }

    /// Find all g_object_class_install_properties calls in the function body
    pub fn find_install_properties_calls(&self) -> Vec<&CallExpression> {
        self.find_calls(&["g_object_class_install_properties"])
    }

    /// Map every named parameter and local variable to its `TypeInfo`.
    /// Parameters appear first; local declarations in body order after that,
    /// so an inner-scope shadowing declaration overwrites the outer one.
    pub fn local_var_types(&self) -> std::collections::HashMap<&str, &TypeInfo> {
        let mut map = std::collections::HashMap::new();
        for param in &self.parameters {
            if let Parameter::Regular {
                name: Some(name),
                type_info,
                ..
            } = param
            {
                map.insert(name.as_str(), type_info);
            }
        }
        for stmt in &self.body_statements {
            stmt.walk(&mut |s| {
                if let Statement::Declaration(decl) = s {
                    map.insert(decl.name.as_str(), &decl.type_info);
                }
            });
        }
        map
    }

    /// Get a parameter by name
    pub fn get_param_by_name(&self, name: &str) -> Option<&Parameter> {
        self.parameters
            .iter()
            .find(|p| matches!(p, Parameter::Regular { name: Some(n), .. } if n == name))
    }

    /// Find all param_spec assignments in the function body
    /// Handles array pattern (props[PROP_X] = ...), variable pattern
    /// (param_spec = ...), and override pattern
    /// (g_object_class_override_property(...))
    pub(crate) fn find_param_spec_assignments(
        &self,
        type_name: &str,
        defines: &HashMap<String, DefineValue>,
    ) -> Vec<ParamSpecAssignment> {
        let mut assignments = Vec::new();
        let mut array_assignments: HashMap<&str, Vec<usize>> = HashMap::new();
        let mut variable_assignments: HashMap<&str, Vec<usize>> = HashMap::new();

        // First pass: collect all assignments
        for (i, stmt) in self.body_statements.iter().enumerate() {
            stmt.walk(&mut |s| {
                match s {
                    // Declaration: GParamSpec *pspec = g_param_spec_*()
                    Statement::Declaration(decl)
                        if matches!(
                            &decl.initializer,
                            Some(Expression::Call(c)) if c.function_contains("_param_spec_")
                        ) =>
                    {
                        let Some(Expression::Call(param_call)) = &decl.initializer else {
                            unreachable!()
                        };
                        let Some(mut property) =
                            Property::from_param_spec_call(param_call, defines)
                        else {
                            return;
                        };
                        if i > 0
                            && let Statement::Comment(c) = &self.body_statements[i - 1]
                        {
                            property.doc =
                                PropertyDoc::from_comment_for(c, type_name, &property.name);
                        }
                        let idx = assignments.len();
                        variable_assignments
                            .entry(&*decl.name)
                            .or_default()
                            .push(idx);
                        assignments.push(ParamSpecAssignment::Variable {
                            variable_name: decl.name.clone(),
                            statement_location: s.location().clone(),
                            call: param_call.clone(),
                            property,
                            install_call: None,
                        });
                    }
                    Statement::Expression(expr_stmt) => {
                        match expr_stmt.as_ref() {
                            // Assignment: props[PROP_X] = g_param_spec_*() or spec =
                            // g_param_spec_*()
                            Expression::Assignment(assignment) => {
                                if let Expression::Call(param_call) = &*assignment.rhs {
                                    let func_name = param_call.function_name();
                                    if !func_name.contains("_param_spec_") {
                                        return;
                                    }

                                    let Some(mut property) =
                                        Property::from_param_spec_call(param_call, defines)
                                    else {
                                        return;
                                    };
                                    if i > 0
                                        && let Statement::Comment(c) = &self.body_statements[i - 1]
                                    {
                                        property.doc = PropertyDoc::from_comment_for(
                                            c,
                                            type_name,
                                            &property.name,
                                        );
                                    }

                                    // Check LHS: array subscript or variable?
                                    if let Expression::Subscript(subscript) = &*assignment.lhs {
                                        if let Some(array_name) =
                                            subscript.array.location().as_str()
                                            && let Some(enum_value) =
                                                subscript.index.location().as_str()
                                        {
                                            let idx = assignments.len();
                                            array_assignments
                                                .entry(array_name)
                                                .or_default()
                                                .push(idx);
                                            assignments.push(ParamSpecAssignment::ArraySubscript {
                                                array_name: array_name.to_owned(),
                                                enum_value: enum_value.to_owned(),
                                                statement_location: s.location().clone(),
                                                call: param_call.clone(),
                                                property,
                                                install_call: None,
                                            });
                                        }
                                    } else if let Some(var_name) =
                                        assignment.lhs.location().as_str()
                                    {
                                        let idx = assignments.len();
                                        variable_assignments.entry(var_name).or_default().push(idx);
                                        assignments.push(ParamSpecAssignment::Variable {
                                            variable_name: var_name.to_owned(),
                                            statement_location: s.location().clone(),
                                            call: param_call.clone(),
                                            property,
                                            install_call: None,
                                        });
                                    }
                                }
                            }
                            // Direct call: g_object_class_override_property(class, PROP_X, "name")
                            Expression::Call(call) => {
                                if call.function_contains("override_property")
                                    && let Some(mut property) =
                                        Property::from_override_property_call(call, defines)
                                    && let Some(enum_arg) = call.get_arg(1)
                                    && let Some(enum_value) = enum_arg.location().as_str()
                                {
                                    if i > 0
                                        && let Statement::Comment(c) = &self.body_statements[i - 1]
                                    {
                                        property.doc = PropertyDoc::from_comment_for(
                                            c,
                                            type_name,
                                            &property.name,
                                        );
                                    }
                                    assignments.push(ParamSpecAssignment::OverrideProperty {
                                        enum_value: enum_value.to_owned(),
                                        statement_location: s.location().clone(),
                                        call: call.clone(),
                                        property,
                                    });
                                }
                            }
                            _ => {}
                        }
                    }
                    _ => {}
                }
            });
        }

        // Second pass: find install calls and link them to assignments
        for (i, stmt) in self.body_statements.iter().enumerate() {
            stmt.walk(&mut |s| {
                if let Statement::Expression(expr_stmt) = s
                    && let Expression::Call(call) = expr_stmt.as_ref()
                {
                    // g_object_class_install_properties(class, N_PROPS, array)
                    if call.function_contains("install_properties") {
                        if let Some(array_arg) = call.get_arg(2)
                            && let Some(array_name) = array_arg.location().as_str()
                            && let Some(indices) = array_assignments.get(&array_name)
                        {
                            for &idx in indices {
                                if let ParamSpecAssignment::ArraySubscript {
                                    install_call, ..
                                } = &mut assignments[idx]
                                {
                                    *install_call = Some(call.clone());
                                }
                            }
                        }
                    }
                    // g_object_class_install_property(class, PROP_X, spec) — 3 args
                    // g_object_interface_install_property(iface, spec) — 2 args
                    else if call.function_contains("install_property") {
                        let is_interface = call.function_contains("interface_install_property");
                        let spec_arg_idx = if is_interface { 1 } else { 2 };

                        if let Some(spec_expr) = call.get_arg(spec_arg_idx) {
                            if let Expression::Call(spec_call) = spec_expr
                                && spec_call.function_contains("_param_spec_")
                                && let Some(mut property) =
                                    Property::from_param_spec_call(spec_call, defines)
                            {
                                let enum_value = if is_interface {
                                    String::new()
                                } else if let Some(enum_arg) = call.get_arg(1)
                                    && let Some(ev) = enum_arg.location().as_str()
                                {
                                    ev.to_owned()
                                } else {
                                    return;
                                };

                                if i > 0
                                    && let Statement::Comment(c) = &self.body_statements[i - 1]
                                {
                                    property.doc = PropertyDoc::from_comment(c);
                                }
                                assignments.push(ParamSpecAssignment::DirectInstall {
                                    enum_value,
                                    statement_location: s.location().clone(),
                                    call: spec_call.clone(),
                                    property,
                                    install_call: call.clone(),
                                });
                            } else if let Some(var_name) = spec_expr.location().as_str()
                                && let Some(indices) = variable_assignments.get(var_name)
                            {
                                let indices = indices.clone();
                                for idx in indices {
                                    if let ParamSpecAssignment::Variable { install_call, .. } =
                                        &mut assignments[idx]
                                        && install_call.is_none()
                                    {
                                        *install_call = Some(call.clone());
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            });
        }

        assignments.sort_by_key(|a| a.statement_location().start_byte);
        assignments
    }
}