| David Tolnay | 75ea17c | 2020-12-06 21:08:34 -0800 | [diff] [blame^] | 1 | use crate::syntax::{Namespace, Pair, RustName, Symbol, Types}; |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 2 | use proc_macro2::{Ident, Span}; |
| David Tolnay | 8faec77 | 2020-11-02 00:18:19 -0800 | [diff] [blame] | 3 | use std::iter; |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 4 | use syn::Token; |
| 5 | |
| 6 | impl Pair { |
| David Tolnay | 8faec77 | 2020-11-02 00:18:19 -0800 | [diff] [blame] | 7 | // Use this constructor when the item can't have a different name in Rust |
| 8 | // and C++. |
| David Tolnay | d7a3a18 | 2020-11-01 20:45:14 -0800 | [diff] [blame] | 9 | pub fn new(namespace: Namespace, ident: Ident) -> Self { |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 10 | Self { |
| David Tolnay | 8faec77 | 2020-11-02 00:18:19 -0800 | [diff] [blame] | 11 | namespace, |
| 12 | cxx: ident.clone(), |
| 13 | rust: ident, |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 14 | } |
| 15 | } |
| 16 | |
| David Tolnay | 8faec77 | 2020-11-02 00:18:19 -0800 | [diff] [blame] | 17 | // Use this constructor when attributes such as #[rust_name] can be used to |
| 18 | // potentially give a different name in Rust vs C++. |
| David Tolnay | d7a3a18 | 2020-11-01 20:45:14 -0800 | [diff] [blame] | 19 | pub fn new_from_differing_names( |
| 20 | namespace: Namespace, |
| 21 | cxx_ident: Ident, |
| 22 | rust_ident: Ident, |
| 23 | ) -> Self { |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 24 | Self { |
| David Tolnay | 8faec77 | 2020-11-02 00:18:19 -0800 | [diff] [blame] | 25 | namespace, |
| 26 | cxx: cxx_ident, |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 27 | rust: rust_ident, |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 28 | } |
| 29 | } |
| David Tolnay | 8faec77 | 2020-11-02 00:18:19 -0800 | [diff] [blame] | 30 | |
| 31 | pub fn to_symbol(&self) -> Symbol { |
| 32 | Symbol::from_idents(self.iter_all_segments()) |
| 33 | } |
| 34 | |
| 35 | pub fn to_fully_qualified(&self) -> String { |
| 36 | format!("::{}", self.join("::")) |
| 37 | } |
| 38 | |
| 39 | fn iter_all_segments(&self) -> impl Iterator<Item = &Ident> { |
| 40 | self.namespace.iter().chain(iter::once(&self.cxx)) |
| 41 | } |
| 42 | |
| 43 | fn join(&self, sep: &str) -> String { |
| 44 | self.iter_all_segments() |
| 45 | .map(|s| s.to_string()) |
| 46 | .collect::<Vec<_>>() |
| 47 | .join(sep) |
| 48 | } |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| David Tolnay | 75ea17c | 2020-12-06 21:08:34 -0800 | [diff] [blame^] | 51 | impl RustName { |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 52 | pub fn new(ident: Ident) -> Self { |
| 53 | Self { rust: ident } |
| 54 | } |
| 55 | |
| David Tolnay | 5804bb7 | 2020-12-06 21:06:15 -0800 | [diff] [blame] | 56 | pub fn from_ref(ident: &Ident) -> &Self { |
| 57 | unsafe { &*(ident as *const Ident as *const Self) } |
| 58 | } |
| 59 | |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 60 | pub fn make_self(span: Span) -> Self { |
| 61 | Self { |
| 62 | rust: Token.into(), |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | pub fn is_self(&self) -> bool { |
| 67 | self.rust == "Self" |
| 68 | } |
| 69 | |
| 70 | pub fn span(&self) -> Span { |
| 71 | self.rust.span() |
| 72 | } |
| 73 | |
| 74 | pub fn to_symbol(&self, types: &Types) -> Symbol { |
| 75 | types.resolve(self).to_symbol() |
| 76 | } |
| 77 | } |