Skip to content

Commit 28f5671

Browse files
committed
Better integrating QualifiedName and Symbol.
These two types are almost, but not quite, the same: * QualifiedName is a C++ name in a particular namespace. * Symbol is an already-mangled name. This commit makes this relationship a bit more clear. As Symbols (with the same mangling) are used for both include guards and actual exported symbol names, that distinction is also dropped from the QualifiedName API.
1 parent d5b3935 commit 28f5671

File tree

4 files changed

+31
-28
lines changed

4 files changed

+31
-28
lines changed

gen/src/write.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
343343
}
344344

345345
fn write_struct(out: &mut OutFile, strct: &Struct) {
346-
let guard = format!("CXXBRIDGE05_STRUCT_{}", strct.ident.to_include_guard());
346+
let guard = format!("CXXBRIDGE05_STRUCT_{}", strct.ident.to_symbol());
347347
writeln!(out, "#ifndef {}", guard);
348348
writeln!(out, "#define {}", guard);
349349
for line in strct.doc.to_string().lines() {
@@ -373,7 +373,7 @@ fn write_struct_using(out: &mut OutFile, ident: &QualifiedIdent) {
373373
}
374374

375375
fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
376-
let guard = format!("CXXBRIDGE05_STRUCT_{}", ety.ident.to_include_guard());
376+
let guard = format!("CXXBRIDGE05_STRUCT_{}", ety.ident.to_symbol());
377377
writeln!(out, "#ifndef {}", guard);
378378
writeln!(out, "#define {}", guard);
379379
for line in ety.doc.to_string().lines() {
@@ -398,7 +398,7 @@ fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&Ex
398398
}
399399

400400
fn write_enum(out: &mut OutFile, enm: &Enum) {
401-
let guard = format!("CXXBRIDGE05_ENUM_{}", enm.ident.to_include_guard());
401+
let guard = format!("CXXBRIDGE05_ENUM_{}", enm.ident.to_symbol());
402402
writeln!(out, "#ifndef {}", guard);
403403
writeln!(out, "#define {}", guard);
404404
for line in enm.doc.to_string().lines() {
@@ -1065,10 +1065,10 @@ fn to_typename(ty: &Type) -> String {
10651065

10661066
// Only called for legal referent types of unique_ptr and element types of
10671067
// std::vector and Vec.
1068-
fn to_mangled(ty: &Type) -> String {
1068+
fn to_mangled(ty: &Type) -> Symbol {
10691069
match ty {
1070-
Type::Ident(ident) => ident.to_bridge_name(),
1071-
Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(&ptr.inner)),
1070+
Type::Ident(ident) => ident.to_symbol(),
1071+
Type::CxxVector(ptr) => to_mangled(&ptr.inner).prefix_with("std$vector$"),
10721072
_ => unreachable!(),
10731073
}
10741074
}
@@ -1131,7 +1131,7 @@ fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
11311131

11321132
fn write_rust_box_extern(out: &mut OutFile, ident: &QualifiedIdent) {
11331133
let inner = ident.to_fully_qualified();
1134-
let instance = ident.to_bridge_name();
1134+
let instance = ident.to_symbol();
11351135

11361136
writeln!(out, "#ifndef CXXBRIDGE05_RUST_BOX_{}", instance);
11371137
writeln!(out, "#define CXXBRIDGE05_RUST_BOX_{}", instance);
@@ -1185,7 +1185,7 @@ fn write_rust_vec_extern(out: &mut OutFile, element: &QualifiedIdent) {
11851185

11861186
fn write_rust_box_impl(out: &mut OutFile, ident: &QualifiedIdent) {
11871187
let inner = ident.to_fully_qualified();
1188-
let instance = ident.to_bridge_name();
1188+
let instance = ident.to_symbol();
11891189

11901190
writeln!(out, "template <>");
11911191
writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);

macro/src/expand.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ fn type_id(ident: &QualifiedIdent) -> TokenStream {
710710
}
711711

712712
fn expand_rust_box(ident: &QualifiedIdent) -> TokenStream {
713-
let link_prefix = format!("cxxbridge05$box${}$", ident.to_bridge_name());
713+
let link_prefix = format!("cxxbridge05$box${}$", ident.to_symbol());
714714
let link_uninit = format!("{}uninit", link_prefix);
715715
let link_drop = format!("{}drop", link_prefix);
716716

@@ -739,7 +739,7 @@ fn expand_rust_box(ident: &QualifiedIdent) -> TokenStream {
739739
}
740740

741741
fn expand_rust_vec(elem: &QualifiedIdent) -> TokenStream {
742-
let link_prefix = format!("cxxbridge05$rust_vec${}$", elem.to_bridge_name());
742+
let link_prefix = format!("cxxbridge05$rust_vec${}$", elem.to_symbol());
743743
let link_new = format!("{}new", link_prefix);
744744
let link_drop = format!("{}drop", link_prefix);
745745
let link_len = format!("{}len", link_prefix);
@@ -789,7 +789,7 @@ fn expand_unique_ptr(
789789
explicit_impl: Option<&Impl>,
790790
) -> TokenStream {
791791
let name = ident.to_fully_qualified();
792-
let prefix = format!("cxxbridge05$unique_ptr${}$", ident.to_bridge_name());
792+
let prefix = format!("cxxbridge05$unique_ptr${}$", ident.to_symbol());
793793
let link_null = format!("{}null", prefix);
794794
let link_new = format!("{}new", prefix);
795795
let link_raw = format!("{}raw", prefix);
@@ -868,13 +868,10 @@ fn expand_unique_ptr(
868868
fn expand_cxx_vector(elem: &QualifiedIdent, explicit_impl: Option<&Impl>) -> TokenStream {
869869
let _ = explicit_impl;
870870
let name = elem.to_fully_qualified();
871-
let prefix = format!("cxxbridge05$std$vector${}$", elem.to_bridge_name());
871+
let prefix = format!("cxxbridge05$std$vector${}$", elem.to_symbol());
872872
let link_size = format!("{}size", prefix);
873873
let link_get_unchecked = format!("{}get_unchecked", prefix);
874-
let unique_ptr_prefix = format!(
875-
"cxxbridge05$unique_ptr$std$vector${}$",
876-
elem.to_bridge_name()
877-
);
874+
let unique_ptr_prefix = format!("cxxbridge05$unique_ptr$std$vector${}$", elem.to_symbol());
878875
let link_unique_ptr_null = format!("{}null", unique_ptr_prefix);
879876
let link_unique_ptr_raw = format!("{}raw", unique_ptr_prefix);
880877
let link_unique_ptr_get = format!("{}get", unique_ptr_prefix);

syntax/mod.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub mod types;
2323
use self::discriminant::Discriminant;
2424
use self::namespace::Namespace;
2525
use self::parse::kw;
26+
use self::symbol::Symbol;
2627
use core::fmt::{Formatter, Result};
2728
use proc_macro2::{Ident, Span, TokenStream};
2829
use quote::{IdentFragment, ToTokens};
@@ -257,12 +258,8 @@ impl QualifiedIdent {
257258
.join(sep)
258259
}
259260

260-
pub fn to_include_guard(&self) -> String {
261-
self.to_bridge_name()
262-
}
263-
264-
pub fn to_bridge_name(&self) -> String {
265-
self.join("$")
261+
pub fn to_symbol(&self) -> Symbol {
262+
Symbol::from_idents(self.iter_all_segments())
266263
}
267264

268265
pub fn to_fully_qualified(&self) -> String {

syntax/symbol.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ impl ToTokens for Symbol {
2020
}
2121
}
2222

23-
impl From<&Ident> for Symbol {
24-
fn from(ident: &Ident) -> Self {
25-
Symbol(ident.to_string())
26-
}
27-
}
28-
2923
impl Symbol {
3024
fn push(&mut self, segment: &dyn Display) {
3125
let len_before = self.0.len();
@@ -35,6 +29,21 @@ impl Symbol {
3529
self.0.write_fmt(format_args!("{}", segment)).unwrap();
3630
assert!(self.0.len() > len_before);
3731
}
32+
33+
pub fn from_idents<'a, T: Iterator<Item = &'a Ident>>(it: T) -> Self {
34+
let mut symbol = Symbol(String::new());
35+
for segment in it {
36+
segment.write(&mut symbol);
37+
}
38+
assert!(!symbol.0.is_empty());
39+
symbol
40+
}
41+
42+
/// For example, for taking a symbol and then making a new symbol
43+
/// for a vec of that symbol.
44+
pub fn prefix_with(&self, prefix: &str) -> Symbol {
45+
Symbol(format!("{}{}", prefix, self.to_string()))
46+
}
3847
}
3948

4049
pub trait Segment {

0 commit comments

Comments
 (0)