Skip to content

Allow namespace override #370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions gen/src/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub(super) mod error;
mod file;
pub(super) mod fs;
pub(super) mod include;
mod namespace_organizer;
pub(super) mod out;
mod write;

Expand Down Expand Up @@ -109,22 +110,22 @@ pub(super) fn generate(syntax: File, opt: &Opt) -> Result<GeneratedCode> {
.ok_or(Error::NoBridgeMod)?;
let ref namespace = bridge.namespace;
let trusted = bridge.unsafety.is_some();
let ref apis = syntax::parse_items(errors, bridge.content, trusted);
let ref apis = syntax::parse_items(errors, bridge.content, trusted, namespace);
let ref types = Types::collect(errors, apis);
errors.propagate()?;
check::typecheck(errors, namespace, apis, types);
check::typecheck(errors, apis, types);
errors.propagate()?;
// Some callers may wish to generate both header and C++
// from the same token stream to avoid parsing twice. But others
// only need to generate one or the other.
Ok(GeneratedCode {
header: if opt.gen_header {
write::gen(namespace, apis, types, opt, true).content()
write::gen(apis, types, opt, true).content()
} else {
Vec::new()
},
implementation: if opt.gen_implementation {
write::gen(namespace, apis, types, opt, false).content()
write::gen(apis, types, opt, false).content()
} else {
Vec::new()
},
Expand Down
40 changes: 40 additions & 0 deletions gen/src/namespace_organizer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::syntax::Api;
use proc_macro2::Ident;
use std::collections::BTreeMap;

pub(crate) struct NamespaceEntries<'a> {
pub(crate) entries: Vec<&'a Api>,
pub(crate) children: BTreeMap<&'a Ident, NamespaceEntries<'a>>,
}

pub(crate) fn sort_by_namespace(apis: &[Api]) -> NamespaceEntries {
let api_refs = apis.iter().collect::<Vec<_>>();
sort_by_inner_namespace(api_refs, 0)
}

fn sort_by_inner_namespace(apis: Vec<&Api>, depth: usize) -> NamespaceEntries {
let mut root = NamespaceEntries {
entries: Vec::new(),
children: BTreeMap::new(),
};

let mut kids_by_child_ns = BTreeMap::new();
for api in apis {
if let Some(ns) = api.get_namespace() {
let first_ns_elem = ns.iter().nth(depth);
if let Some(first_ns_elem) = first_ns_elem {
let list = kids_by_child_ns.entry(first_ns_elem).or_insert(Vec::new());
list.push(api);
continue;
}
}
root.entries.push(api);
}

for (k, v) in kids_by_child_ns.into_iter() {
root.children
.insert(k, sort_by_inner_namespace(v, depth + 1));
}

root
}
5 changes: 1 addition & 4 deletions gen/src/out.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use crate::gen::include::Includes;
use crate::syntax::namespace::Namespace;
use std::cell::RefCell;
use std::fmt::{self, Arguments, Write};

pub(crate) struct OutFile {
pub namespace: Namespace,
pub header: bool,
pub include: Includes,
pub front: Content,
Expand All @@ -18,9 +16,8 @@ pub struct Content {
}

impl OutFile {
pub fn new(namespace: Namespace, header: bool) -> Self {
pub fn new(header: bool) -> Self {
OutFile {
namespace,
header,
include: Includes::new(),
front: Content::new(),
Expand Down
Loading