| David Tolnay | 415f232 | 2021-02-17 15:48:13 -0800 | [diff] [blame] | 1 | // Mangled symbol arrangements: |
| 2 | // |
| 3 | // (a) One-off internal symbol. |
| 4 | // pattern: {CXXBRIDGE} $ {NAME} |
| 5 | // examples: |
| 6 | // - cxxbridge1$exception |
| 7 | // defining characteristics: |
| 8 | // - 2 segments |
| 9 | // - starts with cxxbridge |
| 10 | // |
| 11 | // (b) Behavior on a builtin binding without generic parameter. |
| 12 | // pattern: {CXXBRIDGE} $ {TYPE} $ {NAME} |
| 13 | // examples: |
| 14 | // - cxxbridge1$string$len |
| 15 | // defining characteristics: |
| 16 | // - 3 segments |
| 17 | // - starts with cxxbridge |
| 18 | // |
| 19 | // (c) Behavior on a builtin binding with generic parameter. |
| 20 | // pattern: {CXXBRIDGE} $ {TYPE} $ {PARAM...} $ {NAME} |
| 21 | // examples: |
| 22 | // - cxxbridge1$box$org$rust$Struct$alloc |
| 23 | // - cxxbridge1$unique_ptr$std$vector$u8$drop |
| 24 | // defining characteristics: |
| 25 | // - 4+ segments |
| 26 | // - starts with cxxbridge |
| 27 | // |
| 28 | // (d) User-defined extern function. |
| 29 | // pattern: {NAMESPACE...} $ {CXXBRIDGE} $ {NAME} |
| 30 | // examples: |
| 31 | // - cxxbridge1$new_client |
| 32 | // - org$rust$cxxbridge1$new_client |
| 33 | // defining characteristics: |
| 34 | // - cxxbridge is second from end |
| 35 | // FIXME: conflict with (a) if they collide with one of our one-off symbol names in the global namespace |
| 36 | // |
| 37 | // (e) User-defined extern member function. |
| 38 | // pattern: {NAMESPACE...} $ {CXXBRIDGE} $ {TYPE} $ {NAME} |
| 39 | // examples: |
| 40 | // - org$cxxbridge1$Struct$get |
| 41 | // defining characteristics: |
| 42 | // - cxxbridge is third from end |
| 43 | // FIXME: conflict with (b) if e.g. user binds a type in global namespace that collides with our builtin type names |
| 44 | // |
| 45 | // (f) Operator overload. |
| 46 | // pattern: {NAMESPACE...} $ {CXXBRIDGE} $ {TYPE} $ operator $ {NAME} |
| 47 | // examples: |
| 48 | // - org$rust$cxxbridge1$Struct$operator$eq |
| 49 | // defining characteristics: |
| 50 | // - second segment from end is `operator` (not possible in type or namespace names) |
| 51 | // |
| 52 | // (g) Closure trampoline. |
| 53 | // pattern: {NAMESPACE...} $ {CXXBRIDGE} $ {TYPE?} $ {NAME} $ {ARGUMENT} $ {DIRECTION} |
| 54 | // examples: |
| 55 | // - org$rust$cxxbridge1$Struct$invoke$f$0 |
| 56 | // defining characteristics: |
| 57 | // - last symbol is `0` (C half) or `1` (Rust half) which are not legal identifiers on their own |
| David Tolnay | 6c39d4b | 2021-02-17 15:49:52 -0800 | [diff] [blame] | 58 | // |
| 59 | // |
| 60 | // Mangled preprocessor variable arrangements: |
| 61 | // |
| 62 | // (A) One-off internal variable. |
| 63 | // pattern: {CXXBRIDGE} _ {NAME} |
| 64 | // examples: |
| 65 | // - CXXBRIDGE1_PANIC |
| 66 | // - CXXBRIDGE1_RUST_STRING |
| 67 | // defining characteristics: |
| 68 | // - NAME does not begin with STRUCT or ENUM |
| 69 | // |
| 70 | // (B) Guard around user-defined type. |
| 71 | // pattern: {CXXBRIDGE} _ {STRUCT or ENUM} _ {NAMESPACE...} $ {TYPE} |
| 72 | // examples: |
| 73 | // - CXXBRIDGE1_STRUCT_org$rust$Struct |
| 74 | // - CXXBRIDGE1_ENUM_Enabled |
| David Tolnay | 415f232 | 2021-02-17 15:48:13 -0800 | [diff] [blame] | 75 | |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 76 | use crate::syntax::symbol::{self, Symbol}; |
| David Tolnay | b960ed2 | 2020-11-27 14:34:30 -0800 | [diff] [blame] | 77 | use crate::syntax::{ExternFn, Pair, Types}; |
| David Tolnay | 5ea922a | 2020-04-19 21:58:06 -0700 | [diff] [blame] | 78 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 79 | const CXXBRIDGE: &str = "cxxbridge1"; |
| David Tolnay | 5ea922a | 2020-04-19 21:58:06 -0700 | [diff] [blame] | 80 | |
| 81 | macro_rules! join { |
| David Tolnay | ad3db80 | 2020-11-27 14:45:49 -0800 | [diff] [blame] | 82 | ($($segment:expr),+ $(,)?) => { |
| 83 | symbol::join(&[$(&$segment),+]) |
| David Tolnay | 5ea922a | 2020-04-19 21:58:06 -0700 | [diff] [blame] | 84 | }; |
| 85 | } |
| David Tolnay | 3caa50a | 2020-04-19 21:25:34 -0700 | [diff] [blame] | 86 | |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 87 | pub fn extern_fn(efn: &ExternFn, types: &Types) -> Symbol { |
| David Tolnay | 5ea922a | 2020-04-19 21:58:06 -0700 | [diff] [blame] | 88 | match &efn.receiver { |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 89 | Some(receiver) => { |
| 90 | let receiver_ident = types.resolve(&receiver.ty); |
| 91 | join!( |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 92 | efn.name.namespace, |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 93 | CXXBRIDGE, |
| David Tolnay | 1e5fe23 | 2021-01-01 18:11:40 -0800 | [diff] [blame] | 94 | receiver_ident.name.cxx, |
| David Tolnay | ad3db80 | 2020-11-27 14:45:49 -0800 | [diff] [blame] | 95 | efn.name.rust, |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 96 | ) |
| 97 | } |
| David Tolnay | 17a934c | 2020-11-02 00:40:04 -0800 | [diff] [blame] | 98 | None => join!(efn.name.namespace, CXXBRIDGE, efn.name.rust), |
| David Tolnay | 5ea922a | 2020-04-19 21:58:06 -0700 | [diff] [blame] | 99 | } |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| David Tolnay | b960ed2 | 2020-11-27 14:34:30 -0800 | [diff] [blame] | 102 | pub fn operator(receiver: &Pair, operator: &'static str) -> Symbol { |
| David Tolnay | 7da3820 | 2020-11-27 17:36:16 -0800 | [diff] [blame] | 103 | join!( |
| 104 | receiver.namespace, |
| 105 | CXXBRIDGE, |
| 106 | receiver.cxx, |
| 107 | "operator", |
| 108 | operator, |
| 109 | ) |
| David Tolnay | b960ed2 | 2020-11-27 14:34:30 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 112 | // The C half of a function pointer trampoline. |
| David Tolnay | 84ed6ad | 2021-01-01 15:30:14 -0800 | [diff] [blame] | 113 | pub fn c_trampoline(efn: &ExternFn, var: &Pair, types: &Types) -> Symbol { |
| 114 | join!(extern_fn(efn, types), var.rust, 0) |
| David Tolnay | 891061b | 2020-04-19 22:42:33 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // The Rust half of a function pointer trampoline. |
| David Tolnay | 84ed6ad | 2021-01-01 15:30:14 -0800 | [diff] [blame] | 118 | pub fn r_trampoline(efn: &ExternFn, var: &Pair, types: &Types) -> Symbol { |
| 119 | join!(extern_fn(efn, types), var.rust, 1) |
| David Tolnay | 3caa50a | 2020-04-19 21:25:34 -0700 | [diff] [blame] | 120 | } |