vb6parse 1.0.0

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
//! Defines compilation settings enums and structs for VB6 projects.
//!
//! Includes settings for native code compilation and P-Code.
//! Each setting is represented as an enum with variants corresponding to possible values.
//! Provides methods to update individual settings while maintaining immutability.
//!
use std::str::FromStr;

use num_enum::TryFromPrimitive;
use serde::Serialize;
use strum_macros::{EnumIter, EnumMessage};

/// Represents whether unrounded floating point is allowed.
#[derive(
    Debug,
    PartialEq,
    Eq,
    Copy,
    Clone,
    Serialize,
    Default,
    TryFromPrimitive,
    EnumIter,
    EnumMessage,
    Hash,
    PartialOrd,
    Ord,
)]
#[repr(i16)]
pub enum UnroundedFloatingPoint {
    /// Do not use unrounded floating point.
    #[default]
    #[strum(message = "Do not use unrounded floating point")]
    DoNotAllow = 0,
    /// Use unrounded floating point.
    #[strum(message = "Use unrounded floating point")]
    Allow = -1,
}

impl TryFrom<&str> for UnroundedFloatingPoint {
    type Error = String;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        match value {
            "0" => Ok(UnroundedFloatingPoint::DoNotAllow),
            "-1" => Ok(UnroundedFloatingPoint::Allow),
            _ => Err(format!("Unknown UnroundedFloatingPoint value: '{value}'")),
        }
    }
}

/// Represents whether to check for the Pentium FDIV bug.
#[derive(
    Debug,
    PartialEq,
    Eq,
    Copy,
    Clone,
    Serialize,
    Default,
    TryFromPrimitive,
    EnumIter,
    EnumMessage,
    Hash,
    PartialOrd,
    Ord,
)]
#[repr(i16)]
pub enum PentiumFDivBugCheck {
    /// Check for the Pentium FDIV bug.
    #[strum(message = "Check for Pentium FDIV bug")]
    CheckPentiumFDivBug = 0,
    /// Do not check for the Pentium FDIV bug.
    #[default]
    #[strum(message = "Ignore Pentium FDIV bug")]
    NoPentiumFDivBugCheck = -1,
}

impl TryFrom<&str> for PentiumFDivBugCheck {
    type Error = String;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        match value {
            "0" => Ok(PentiumFDivBugCheck::CheckPentiumFDivBug),
            "-1" => Ok(PentiumFDivBugCheck::NoPentiumFDivBugCheck),
            _ => Err(format!("Unknown PentiumFDivBugCheck value: '{value}'")),
        }
    }
}

/// Represents whether to perform bounds checking.
#[derive(
    Debug,
    PartialEq,
    Eq,
    Copy,
    Clone,
    Serialize,
    Default,
    TryFromPrimitive,
    EnumIter,
    EnumMessage,
    Hash,
    PartialOrd,
    Ord,
)]
#[repr(i16)]
pub enum BoundsCheck {
    /// Perform bounds checking.
    #[default]
    #[strum(message = "Perform bounds checking")]
    CheckBounds = 0,
    /// Do not perform bounds checking.
    #[strum(message = "Do not perform bounds checking")]
    NoBoundsCheck = -1,
}

impl TryFrom<&str> for BoundsCheck {
    type Error = String;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        match value {
            "0" => Ok(BoundsCheck::CheckBounds),
            "-1" => Ok(BoundsCheck::NoBoundsCheck),
            _ => Err(format!("Unknown BoundsCheck value: '{value}'")),
        }
    }
}

/// Represents whether to perform overflow checking.
#[derive(
    Debug,
    PartialEq,
    Eq,
    Copy,
    Clone,
    Serialize,
    Default,
    TryFromPrimitive,
    EnumIter,
    EnumMessage,
    Hash,
    PartialOrd,
    Ord,
)]
#[repr(i16)]
pub enum OverflowCheck {
    /// Perform overflow checking.
    #[default]
    #[strum(message = "Check for overflow")]
    CheckOverflow = 0,
    /// Do not perform overflow checking.
    #[strum(message = "Do not check for overflow")]
    NoOverflowCheck = -1,
}

impl TryFrom<&str> for OverflowCheck {
    type Error = String;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        match value {
            "0" => Ok(OverflowCheck::CheckOverflow),
            "-1" => Ok(OverflowCheck::NoOverflowCheck),
            _ => Err(format!("Unknown OverflowCheck value: '{value}'")),
        }
    }
}

/// Represents whether to check for floating point errors.
#[derive(
    Debug,
    PartialEq,
    Eq,
    Copy,
    Clone,
    Serialize,
    Default,
    TryFromPrimitive,
    EnumIter,
    EnumMessage,
    Hash,
    PartialOrd,
    Ord,
)]
#[repr(i16)]
pub enum FloatingPointErrorCheck {
    /// Perform floating point error checking.
    #[default]
    #[strum(message = "Check for floating point errors")]
    CheckFloatingPointError = 0,
    /// Do not perform floating point error checking.
    #[strum(message = "Do not check for floating point errors")]
    NoFloatingPointErrorCheck = -1,
}

impl TryFrom<&str> for FloatingPointErrorCheck {
    type Error = String;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        match value {
            "0" => Ok(FloatingPointErrorCheck::CheckFloatingPointError),
            "-1" => Ok(FloatingPointErrorCheck::NoFloatingPointErrorCheck),
            _ => Err(format!("Unknown FloatingPointErrorCheck value: '{value}'")),
        }
    }
}

/// Represents whether to create `CodeView` debug information.
#[derive(
    Debug,
    PartialEq,
    Eq,
    Copy,
    Clone,
    Serialize,
    Default,
    TryFromPrimitive,
    EnumIter,
    EnumMessage,
    Hash,
    PartialOrd,
    Ord,
)]
#[repr(i16)]
pub enum CodeViewDebugInfo {
    /// Do not create `CodeView` debug information.
    #[default]
    #[strum(message = "Do not create CodeView debug info")]
    NotCreated = 0,
    /// Create `CodeView` debug information.
    #[strum(message = "Create CodeView debug info")]
    Created = -1,
}

impl TryFrom<&str> for CodeViewDebugInfo {
    type Error = String;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        match value {
            "0" => Ok(CodeViewDebugInfo::NotCreated),
            "-1" => Ok(CodeViewDebugInfo::Created),
            _ => Err(format!("Unknown CodeViewDebugInfo value: '{value}'")),
        }
    }
}

/// Represents whether to favor Pentium Pro optimizations.
#[derive(
    Debug,
    PartialEq,
    Eq,
    Copy,
    Clone,
    Serialize,
    Default,
    TryFromPrimitive,
    EnumIter,
    EnumMessage,
    Hash,
    PartialOrd,
    Ord,
)]
#[repr(i16)]
pub enum FavorPentiumPro {
    /// Do not favor Pentium Pro optimizations.
    #[default]
    #[strum(message = "Do not favor Pentium Pro optimizations")]
    False = 0,
    /// Favor Pentium Pro optimizations.
    #[strum(message = "Favor Pentium Pro optimizations")]
    True = -1,
}

impl TryFrom<&str> for FavorPentiumPro {
    type Error = String;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        match value {
            "0" => Ok(FavorPentiumPro::False),
            "-1" => Ok(FavorPentiumPro::True),
            _ => Err(format!("Unknown FavorPentiumPro value: '{value}'")),
        }
    }
}

/// Represents whether to assume aliasing.
#[derive(
    Debug,
    PartialEq,
    Eq,
    Copy,
    Clone,
    Serialize,
    Default,
    TryFromPrimitive,
    EnumIter,
    EnumMessage,
    Hash,
    PartialOrd,
    Ord,
)]
#[repr(i16)]
pub enum Aliasing {
    /// Assume aliasing.
    #[default]
    #[strum(message = "Assume aliasing")]
    AssumeAliasing = 0,
    /// Do not assume aliasing.
    #[strum(message = "Do not assume aliasing")]
    AssumeNoAliasing = -1,
}

impl TryFrom<&str> for Aliasing {
    type Error = String;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        match value {
            "0" => Ok(Aliasing::AssumeAliasing),
            "-1" => Ok(Aliasing::AssumeNoAliasing),
            _ => Err(format!("Unknown Aliasing value: '{value}'")),
        }
    }
}

/// Represents the optimization type for native code compilation.
#[derive(
    Debug,
    PartialEq,
    Eq,
    Copy,
    Clone,
    Serialize,
    Default,
    TryFromPrimitive,
    EnumIter,
    EnumMessage,
    Hash,
    PartialOrd,
    Ord,
)]
#[repr(i16)]
pub enum OptimizationType {
    /// Favor fast code optimizations.
    #[default]
    #[strum(message = "Favor fast code")]
    FavorFastCode = 0,
    /// Favor small code optimizations.
    #[strum(message = "Favor small code")]
    FavorSmallCode = 1,
    /// Do not optimize.
    #[strum(message = "Do not optimize")]
    NoOptimization = 2,
}

impl TryFrom<&str> for OptimizationType {
    type Error = String;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        match value {
            "0" => Ok(OptimizationType::FavorFastCode),
            "1" => Ok(OptimizationType::FavorSmallCode),
            "2" => Ok(OptimizationType::NoOptimization),
            _ => Err(format!("Unknown OptimizationType value: '{value}'")),
        }
    }
}

/// Settings specific to native code compilation.
#[derive(Debug, PartialEq, Eq, Copy, Clone, Serialize, Default, Hash, PartialOrd, Ord)]
pub struct NativeCodeSettings {
    /// Optimization type setting.
    pub optimization_type: OptimizationType,
    /// Whether to favor Pentium Pro optimizations.
    pub favor_pentium_pro: FavorPentiumPro,
    /// Whether to create `CodeView` debug information.
    pub code_view_debug_info: CodeViewDebugInfo,
    /// Whether to assume aliasing.
    pub aliasing: Aliasing,
    /// Whether to perform bounds checking.
    pub bounds_check: BoundsCheck,
    /// Whether to perform overflow checking.
    pub overflow_check: OverflowCheck,
    /// Whether to perform floating point error checking.
    pub floating_point_check: FloatingPointErrorCheck,
    /// Whether to check for the Pentium FDIV bug.
    pub pentium_fdiv_bug_check: PentiumFDivBugCheck,
    /// Whether to use unrounded floating point.
    pub unrounded_floating_point: UnroundedFloatingPoint,
}

/// Represents the compilation type and its associated settings.
#[derive(Debug, PartialEq, Eq, Copy, Clone, Serialize, Default, Hash, PartialOrd, Ord)]
pub enum CompilationType {
    /// Native code compilation with specific settings.
    /// Contains various optimization and checking settings.
    ///
    /// Saved as "0" (False) in project files.
    NativeCode(NativeCodeSettings),
    /// P-Code compilation.
    /// Saved as "-1" (True) in project files.
    #[default]
    PCode,
}

impl CompilationType {
    /// Updates the optimization type setting.
    ///
    /// # Arguments
    ///
    /// * `setting` - The new optimization type to set.
    ///
    /// # Returns
    ///
    /// * `CompilationType` - A new `CompilationType` with the updated setting.
    ///
    #[must_use]
    pub fn update_optimization_type(&mut self, setting: OptimizationType) -> CompilationType {
        match self {
            CompilationType::PCode => CompilationType::NativeCode(NativeCodeSettings {
                optimization_type: setting,
                ..Default::default()
            }),
            CompilationType::NativeCode(mut value) => {
                value.optimization_type = setting;
                CompilationType::NativeCode(value)
            }
        }
    }

    /// Updates the favor Pentium Pro setting.
    ///
    /// # Arguments
    ///
    /// * `setting` - The new favor Pentium Pro setting to set.
    ///
    /// # Returns
    ///
    /// * `CompilationType` - A new `CompilationType` with the updated setting.
    ///
    #[must_use]
    pub fn update_favor_pentium_pro(&mut self, setting: FavorPentiumPro) -> CompilationType {
        match self {
            CompilationType::PCode => CompilationType::NativeCode(NativeCodeSettings {
                favor_pentium_pro: setting,
                ..Default::default()
            }),
            CompilationType::NativeCode(mut value) => {
                value.favor_pentium_pro = setting;
                CompilationType::NativeCode(value)
            }
        }
    }

    /// Updates the `CodeView` debug info setting.
    ///
    /// # Arguments
    ///
    /// * `setting` - The new `CodeView` debug info setting to set.
    ///
    /// # Returns
    ///
    /// * `CompilationType` - A new `CompilationType` with the updated setting.
    ///
    #[must_use]
    pub fn update_code_view_debug_info(&mut self, setting: CodeViewDebugInfo) -> CompilationType {
        match self {
            CompilationType::PCode => CompilationType::NativeCode(NativeCodeSettings {
                code_view_debug_info: setting,
                ..Default::default()
            }),
            CompilationType::NativeCode(mut value) => {
                value.code_view_debug_info = setting;
                CompilationType::NativeCode(value)
            }
        }
    }

    /// Updates the aliasing setting.
    ///
    /// # Arguments
    ///
    /// * `setting` - The new aliasing setting to set.
    ///
    /// # Returns
    ///
    ///  * `CompilationType` - A new `CompilationType` with the updated setting.
    ///
    #[must_use]
    pub fn update_aliasing(&mut self, setting: Aliasing) -> CompilationType {
        match self {
            CompilationType::PCode => CompilationType::NativeCode(NativeCodeSettings {
                aliasing: setting,
                ..Default::default()
            }),
            CompilationType::NativeCode(mut value) => {
                value.aliasing = setting;
                CompilationType::NativeCode(value)
            }
        }
    }

    /// Updates the bounds check setting.
    ///
    /// # Arguments
    ///
    /// * `setting` - The new bounds check setting to set.
    ///
    /// # Returns
    ///
    /// * `CompilationType` - A new `CompilationType` with the updated setting.
    ///
    #[must_use]
    pub fn update_bounds_check(&mut self, setting: BoundsCheck) -> CompilationType {
        match self {
            CompilationType::PCode => CompilationType::NativeCode(NativeCodeSettings {
                bounds_check: setting,
                ..Default::default()
            }),
            CompilationType::NativeCode(mut value) => {
                value.bounds_check = setting;
                CompilationType::NativeCode(value)
            }
        }
    }

    /// Updates the overflow check setting.
    ///
    /// # Arguments
    ///
    /// * `setting` - The new overflow check setting to set.
    ///
    /// # Returns
    ///
    /// * `CompilationType` - A new `CompilationType` with the updated setting.
    ///
    #[must_use]
    pub fn update_overflow_check(&mut self, setting: OverflowCheck) -> CompilationType {
        match self {
            CompilationType::PCode => CompilationType::NativeCode(NativeCodeSettings {
                overflow_check: setting,
                ..Default::default()
            }),
            CompilationType::NativeCode(mut value) => {
                value.overflow_check = setting;
                CompilationType::NativeCode(value)
            }
        }
    }

    /// Updates the floating point error check setting.
    ///
    /// # Arguments
    ///
    /// * `setting` - The new floating point error check setting to set.
    ///
    /// # Returns
    ///
    /// * `CompilationType` - A new `CompilationType` with the updated setting.
    ///
    #[must_use]
    pub fn update_floating_point_check(
        &mut self,
        setting: FloatingPointErrorCheck,
    ) -> CompilationType {
        match self {
            CompilationType::PCode => CompilationType::NativeCode(NativeCodeSettings {
                floating_point_check: setting,
                ..Default::default()
            }),
            CompilationType::NativeCode(mut value) => {
                value.floating_point_check = setting;
                CompilationType::NativeCode(value)
            }
        }
    }

    /// Updates the Pentium FDIV bug check setting.
    ///
    /// # Arguments
    ///
    /// * `setting` - The new Pentium FDIV bug check setting to set.
    ///
    /// # Returns
    ///
    /// * `CompilationType` - A new `CompilationType` with the updated setting.
    ///
    #[must_use]
    pub fn update_pentium_fdiv_bug_check(self, setting: PentiumFDivBugCheck) -> CompilationType {
        match self {
            CompilationType::PCode => CompilationType::NativeCode(NativeCodeSettings {
                pentium_fdiv_bug_check: setting,
                ..Default::default()
            }),
            CompilationType::NativeCode(mut value) => {
                value.pentium_fdiv_bug_check = setting;
                CompilationType::NativeCode(value)
            }
        }
    }

    /// Updates the unrounded floating point setting.
    ///
    /// # Arguments
    ///
    /// * `setting` - The new unrounded floating point setting to set.
    ///
    /// # Returns
    ///
    /// * `CompilationType` - A new `CompilationType` with the updated setting.
    ///
    #[must_use]
    pub fn update_unrounded_floating_point(
        &mut self,
        setting: UnroundedFloatingPoint,
    ) -> CompilationType {
        match self {
            CompilationType::PCode => CompilationType::NativeCode(NativeCodeSettings {
                unrounded_floating_point: setting,
                ..Default::default()
            }),
            CompilationType::NativeCode(mut value) => {
                value.unrounded_floating_point = setting;
                CompilationType::NativeCode(value)
            }
        }
    }
}

impl FromStr for CompilationType {
    type Err = String;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value {
            "0" => Ok(CompilationType::default()),
            "-1" => Ok(CompilationType::PCode),
            _ => Err(format!("Unknown CompilationType value: '{value}'")),
        }
    }
}

impl TryFrom<&str> for CompilationType {
    type Error = String;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        match value {
            "0" => Ok(CompilationType::default()),
            "-1" => Ok(CompilationType::PCode),
            _ => Err(format!("Unknown CompilationType value: '{value}'")),
        }
    }
}