Move C++-specific to_typename to C++ code generator

This commit is contained in:
David Tolnay 2020-04-24 19:50:51 -07:00
parent 3b40b6f8f0
commit 2eca4a0ce9
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
3 changed files with 29 additions and 33 deletions

View File

@ -4,7 +4,6 @@ use crate::syntax::atom::Atom::{self, *};
use crate::syntax::mangled::to_mangled;
use crate::syntax::namespace::Namespace;
use crate::syntax::symbol::Symbol;
use crate::syntax::typename::to_typename;
use crate::syntax::{mangle, Api, ExternFn, ExternType, Signature, Struct, Type, Types, Var};
use proc_macro2::Ident;
use std::collections::HashMap;
@ -889,6 +888,35 @@ fn write_space_after_type(out: &mut OutFile, ty: &Type) {
}
}
fn to_typename(namespace: &Namespace, ty: &Type) -> String {
match ty {
Type::Ident(ident) => {
let mut inner = String::new();
// Do not apply namespace to built-in type
let is_user_type = Atom::from(ident).is_none();
if is_user_type {
for name in namespace {
inner += name;
inner += "::";
}
}
if let Some(ti) = Atom::from(ident) {
inner += ti.to_cxx();
} else {
inner += &ident.to_string();
};
inner
}
Type::RustBox(ptr) => format!("rust_box<{}>", to_typename(namespace, &ptr.inner)),
Type::RustVec(ptr) => format!("rust_vec<{}>", to_typename(namespace, &ptr.inner)),
Type::UniquePtr(ptr) => {
format!("::std::unique_ptr<{}>", to_typename(namespace, &ptr.inner))
}
Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
_ => unimplemented!(),
}
}
fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
fn allow_unique_ptr(ident: &Ident) -> bool {
Atom::from(ident).is_none()

View File

@ -14,7 +14,6 @@ mod parse;
pub mod set;
pub mod symbol;
mod tokens;
pub mod typename;
pub mod types;
use self::parse::kw;

View File

@ -1,31 +0,0 @@
use crate::syntax::namespace::Namespace;
use crate::syntax::{Atom, Type};
pub fn to_typename(namespace: &Namespace, ty: &Type) -> String {
match ty {
Type::Ident(ident) => {
let mut inner = String::new();
// Do not apply namespace to built-in type
let is_user_type = Atom::from(ident).is_none();
if is_user_type {
for name in namespace {
inner += name;
inner += "::";
}
}
if let Some(ti) = Atom::from(ident) {
inner += ti.to_cxx();
} else {
inner += &ident.to_string();
};
inner
}
Type::RustBox(ptr) => format!("rust_box<{}>", to_typename(namespace, &ptr.inner)),
Type::RustVec(ptr) => format!("rust_vec<{}>", to_typename(namespace, &ptr.inner)),
Type::UniquePtr(ptr) => {
format!("::std::unique_ptr<{}>", to_typename(namespace, &ptr.inner))
}
Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
_ => unimplemented!(),
}
}