sway_core/
compiler_generated.rs

1//! This module encapsulates generation of various elements generated internally by the compiler,
2//! e.g., unique names of variables in desugared code and similar.
3//! It also provides functions for inspecting such generated elements.
4
5/// The prefix for the compiler generated names of tuples.
6const TUPLE_VAR_NAME_PREFIX: &str = "__tuple_";
7
8pub(crate) fn generate_tuple_var_name(suffix: usize) -> String {
9    format!("{TUPLE_VAR_NAME_PREFIX}{suffix}")
10}
11
12pub fn is_generated_tuple_var_name(name: &str) -> bool {
13    name.starts_with(TUPLE_VAR_NAME_PREFIX)
14}
15
16/// The prefix for the compiler generated names of structs used in destructuring
17/// structs in `let` statements.
18const DESTRUCTURED_STRUCT_VAR_NAME_PREFIX: &str = "__destructured_struct_";
19
20pub(crate) fn generate_destructured_struct_var_name(suffix: usize) -> String {
21    format!("{DESTRUCTURED_STRUCT_VAR_NAME_PREFIX}{suffix}")
22}
23
24pub fn is_generated_destructured_struct_var_name(name: &str) -> bool {
25    name.starts_with(DESTRUCTURED_STRUCT_VAR_NAME_PREFIX)
26}
27
28/// The prefix for the compiler generated names of
29/// variables that store values matched in match expressions.
30const MATCHED_VALUE_VAR_NAME_PREFIX: &str = "__matched_value_";
31
32pub(crate) fn generate_matched_value_var_name(suffix: usize) -> String {
33    format!("{MATCHED_VALUE_VAR_NAME_PREFIX}{suffix}")
34}
35
36/// The prefix for the compiler generated names of
37/// variables that store 1-based index of the OR match
38/// alternative that gets matched, or zero if non of the
39/// OR alternatives get matched.
40const MATCHED_OR_VARIANT_INDEX_VAR_NAME_PREFIX: &str = "__matched_or_variant_index_";
41
42pub(crate) fn generate_matched_or_variant_index_var_name(suffix: usize) -> String {
43    format!("{MATCHED_OR_VARIANT_INDEX_VAR_NAME_PREFIX}{suffix}")
44}
45
46/// The prefix for the compiler generated names of
47/// tuple variables that store values of the variables declared
48/// in OR match alternatives.
49const MATCHED_OR_VARIANT_VARIABLES_VAR_NAME_PREFIX: &str = "__matched_or_variant_variables_";
50
51pub(crate) fn generate_matched_or_variant_variables_var_name(suffix: usize) -> String {
52    format!("{MATCHED_OR_VARIANT_VARIABLES_VAR_NAME_PREFIX}{suffix}")
53}
54
55pub fn is_generated_any_match_expression_var_name(name: &str) -> bool {
56    name.starts_with(MATCHED_VALUE_VAR_NAME_PREFIX)
57        || name.starts_with(MATCHED_OR_VARIANT_INDEX_VAR_NAME_PREFIX)
58        || name.starts_with(MATCHED_OR_VARIANT_VARIABLES_VAR_NAME_PREFIX)
59}
60
61/// A revert with this value signals that it was caused by an internal compiler error that
62/// occurred during the flattening of match arms that contain variables in OR match patterns.
63///
64/// The value is: 14757395258967588865
65pub(crate) const INVALID_MATCHED_OR_VARIABLE_INDEX_SIGNAL: u64 = 0xcccc_cccc_cccc_0001;
66
67/// A revert with this value signals that it was caused by an internal compiler error that
68/// occurred during the flattening of match arms that contain variables in OR match patterns.
69///
70/// The value is: 14757395258967588866
71pub(crate) const INVALID_DESUGARED_MATCHED_EXPRESSION_SIGNAL: u64 = 0xcccc_cccc_cccc_0002;