Skip to content

Commit

Permalink
Better integrating QualifiedName and Symbol.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
adetaylor committed Oct 24, 2020
1 parent f5adf22 commit 2b60a43
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
16 changes: 8 additions & 8 deletions gen/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
}

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

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

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

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

fn write_rust_box_extern(out: &mut OutFile, ident: &QualifiedIdent) {
let inner = ident.to_fully_qualified();
let instance = ident.to_bridge_name();
let instance = ident.to_symbol();

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

fn write_rust_box_impl(out: &mut OutFile, ident: &QualifiedIdent) {
let inner = ident.to_fully_qualified();
let instance = ident.to_bridge_name();
let instance = ident.to_symbol();

writeln!(out, "template <>");
writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
Expand Down
13 changes: 5 additions & 8 deletions macro/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ fn type_id(ident: &QualifiedIdent) -> TokenStream {
}

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

Expand Down Expand Up @@ -739,7 +739,7 @@ fn expand_rust_box(ident: &QualifiedIdent) -> TokenStream {
}

fn expand_rust_vec(elem: &QualifiedIdent) -> TokenStream {
let link_prefix = format!("cxxbridge05$rust_vec${}$", elem.to_bridge_name());
let link_prefix = format!("cxxbridge05$rust_vec${}$", elem.to_symbol());
let link_new = format!("{}new", link_prefix);
let link_drop = format!("{}drop", link_prefix);
let link_len = format!("{}len", link_prefix);
Expand Down Expand Up @@ -789,7 +789,7 @@ fn expand_unique_ptr(
explicit_impl: Option<&Impl>,
) -> TokenStream {
let name = ident.to_fully_qualified();
let prefix = format!("cxxbridge05$unique_ptr${}$", ident.to_bridge_name());
let prefix = format!("cxxbridge05$unique_ptr${}$", ident.to_symbol());
let link_null = format!("{}null", prefix);
let link_new = format!("{}new", prefix);
let link_raw = format!("{}raw", prefix);
Expand Down Expand Up @@ -868,13 +868,10 @@ fn expand_unique_ptr(
fn expand_cxx_vector(elem: &QualifiedIdent, explicit_impl: Option<&Impl>) -> TokenStream {
let _ = explicit_impl;
let name = elem.to_fully_qualified();
let prefix = format!("cxxbridge05$std$vector${}$", elem.to_bridge_name());
let prefix = format!("cxxbridge05$std$vector${}$", elem.to_symbol());
let link_size = format!("{}size", prefix);
let link_get_unchecked = format!("{}get_unchecked", prefix);
let unique_ptr_prefix = format!(
"cxxbridge05$unique_ptr$std$vector${}$",
elem.to_bridge_name()
);
let unique_ptr_prefix = format!("cxxbridge05$unique_ptr$std$vector${}$", elem.to_symbol());
let link_unique_ptr_null = format!("{}null", unique_ptr_prefix);
let link_unique_ptr_raw = format!("{}raw", unique_ptr_prefix);
let link_unique_ptr_get = format!("{}get", unique_ptr_prefix);
Expand Down
9 changes: 3 additions & 6 deletions syntax/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod types;
use self::discriminant::Discriminant;
use self::namespace::Namespace;
use self::parse::kw;
use self::symbol::Symbol;
use core::fmt::{Formatter, Result};
use proc_macro2::{Ident, Span, TokenStream};
use quote::{IdentFragment, ToTokens};
Expand Down Expand Up @@ -257,12 +258,8 @@ impl QualifiedIdent {
.join(sep)
}

pub fn to_include_guard(&self) -> String {
self.to_bridge_name()
}

pub fn to_bridge_name(&self) -> String {
self.join("$")
pub fn to_symbol(&self) -> Symbol {
Symbol::from_idents(self.iter_all_segments())
}

pub fn to_fully_qualified(&self) -> String {
Expand Down
21 changes: 15 additions & 6 deletions syntax/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ impl ToTokens for Symbol {
}
}

impl From<&Ident> for Symbol {
fn from(ident: &Ident) -> Self {
Symbol(ident.to_string())
}
}

impl Symbol {
fn push(&mut self, segment: &dyn Display) {
let len_before = self.0.len();
Expand All @@ -35,6 +29,21 @@ impl Symbol {
self.0.write_fmt(format_args!("{}", segment)).unwrap();
assert!(self.0.len() > len_before);
}

pub fn from_idents<'a, T: Iterator<Item = &'a Ident>>(it: T) -> Self {
let mut symbol = Symbol(String::new());
for segment in it {
segment.write(&mut symbol);
}
assert!(!symbol.0.is_empty());
symbol
}

/// For example, for taking a symbol and then making a new symbol
/// for a vec of that symbol.
pub fn prefix_with(&self, prefix: &str) -> Symbol {
Symbol(format!("{}{}", prefix, self.to_string()))
}
}

pub trait Segment {
Expand Down

0 comments on commit 2b60a43

Please sign in to comment.