Skip to main content

vyre_libs/parsing/c/preprocess/gpu_pipeline/
conditional_events.rs

1//! Conditional-state evidence emitted by the GPU preprocessor driver.
2
3/// Conditional directive kind observed by the GPU preprocessor driver.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum ConditionalEventKind {
6    /// `#ifdef`.
7    Ifdef,
8    /// `#ifndef`.
9    Ifndef,
10    /// `#if`.
11    If,
12    /// `#elif`.
13    Elif,
14    /// `#else`.
15    Else,
16    /// `#endif`.
17    Endif,
18}
19
20/// Residency class for conditional preprocessing evidence.
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub enum ConditionalEventResidency {
23    /// Directive row and payload were extracted by GPU kernels.
24    GpuResidentDirective,
25    /// Conditional truth was evaluated by GPU kernels.
26    GpuResidentTruth,
27    /// Conditional truth came from the live host macro table after GPU payload
28    /// extraction identified the macro name.
29    HostLiveMacroTable,
30    /// Compact conditional stack state was threaded by the host driver.
31    HostStackThreading,
32}
33
34/// Conditional-state event emitted by the GPU-resident preprocessor driver.
35#[derive(Debug, Clone, PartialEq, Eq)]
36pub struct ConditionalEvent {
37    /// File that contained the conditional directive.
38    pub file: std::path::PathBuf,
39    /// Conditional directive kind.
40    pub kind: ConditionalEventKind,
41    /// Directive row in the classified token stream.
42    pub directive_row: u32,
43    /// Byte offset of the directive token in the filtered source.
44    pub directive_byte_offset: u32,
45    /// Stack depth before applying this directive.
46    pub depth_before: u32,
47    /// Stack depth after applying this directive.
48    pub depth_after: u32,
49    /// Parent active state before applying this directive.
50    pub parent_active: bool,
51    /// Truth value for `#if`, `#elif`, `#ifdef`, and `#ifndef` when evaluated.
52    pub truth: Option<bool>,
53    /// Current active state after applying this directive.
54    pub current_active: bool,
55    /// Whether any branch in this conditional group has been taken after applying this directive.
56    pub branch_taken: bool,
57    /// Residency of directive row/payload extraction.
58    pub directive_residency: ConditionalEventResidency,
59    /// Residency of truth evaluation or stack transition.
60    pub state_residency: ConditionalEventResidency,
61}
62
63/// Pushes one conditional event after validating compact integer fields.
64#[allow(clippy::too_many_arguments)]
65pub(super) fn push_conditional_event(
66    events: &mut Vec<ConditionalEvent>,
67    file_path: &std::path::Path,
68    kind: ConditionalEventKind,
69    directive_row: usize,
70    directive_byte_offset: usize,
71    depth_before: usize,
72    depth_after: usize,
73    parent_active: bool,
74    truth: Option<bool>,
75    current_active: bool,
76    branch_taken: bool,
77    state_residency: ConditionalEventResidency,
78) -> Result<(), String> {
79    events.push(ConditionalEvent {
80        file: file_path.to_path_buf(),
81        kind,
82        directive_row: u32::try_from(directive_row).map_err(|_| {
83            "vyre-libs::gpu_pipeline: conditional directive row exceeds u32. Fix: shard preprocessing before conditional-event evidence export.".to_string()
84        })?,
85        directive_byte_offset: u32::try_from(directive_byte_offset).map_err(|_| {
86            "vyre-libs::gpu_pipeline: conditional directive byte offset exceeds u32. Fix: shard preprocessing before conditional-event evidence export.".to_string()
87        })?,
88        depth_before: u32::try_from(depth_before).map_err(|_| {
89            "vyre-libs::gpu_pipeline: conditional depth exceeds u32. Fix: reject pathological conditional nesting before evidence export.".to_string()
90        })?,
91        depth_after: u32::try_from(depth_after).map_err(|_| {
92            "vyre-libs::gpu_pipeline: conditional depth exceeds u32. Fix: reject pathological conditional nesting before evidence export.".to_string()
93        })?,
94        parent_active,
95        truth,
96        current_active,
97        branch_taken,
98        directive_residency: ConditionalEventResidency::GpuResidentDirective,
99        state_residency,
100    });
101    Ok(())
102}