Skip to content

Commit

Permalink
Renames.
Browse files Browse the repository at this point in the history
  • Loading branch information
adetaylor committed Jan 8, 2022
1 parent 8e1fe4b commit ae44e9f
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 51 deletions.
8 changes: 5 additions & 3 deletions engine/src/conversion/analysis/pod/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
api::{AnalysisPhase, Api, ApiName, CppVisibility, StructDetails, TypeKind, UnanalyzedApi},
convert_error::{ConvertErrorWithContext, ErrorContext},
error_reporter::convert_apis,
parse::AutocxxBindgenAnnotations,
parse::BindgenSemanticAttributes,
ConvertError,
},
types::{Namespace, QualifiedName},
Expand Down Expand Up @@ -116,7 +116,8 @@ fn analyze_enum(
name: ApiName,
mut item: ItemEnum,
) -> Result<Box<dyn Iterator<Item = Api<PodPhase>>>, ConvertErrorWithContext> {
AutocxxBindgenAnnotations::remove_bindgen_attrs(&mut item.attrs, name.name.get_final_ident())?;
let metadata = BindgenSemanticAttributes::new_retaining_others(&mut item.attrs);
metadata.check_for_fatal_attrs(&name.name.get_final_ident())?;
Ok(Box::new(std::iter::once(Api::Enum { name, item })))
}

Expand All @@ -135,7 +136,8 @@ fn analyze_struct(
Some(ErrorContext::Item(id)),
));
}
AutocxxBindgenAnnotations::remove_bindgen_attrs(&mut details.item.attrs, id.clone())?;
let metadata = BindgenSemanticAttributes::new_retaining_others(&mut details.item.attrs);
metadata.check_for_fatal_attrs(&id)?;
let bases = get_bases(&details.item);
let mut field_deps = HashSet::new();
let type_kind = if byvalue_checker.is_pod(&name.name) {
Expand Down
6 changes: 3 additions & 3 deletions engine/src/conversion/analysis/tdef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
api::{AnalysisPhase, Api, ApiName, TypedefKind, UnanalyzedApi},
convert_error::{ConvertErrorWithContext, ErrorContext},
error_reporter::convert_apis,
parse::AutocxxBindgenAnnotations,
parse::BindgenSemanticAttributes,
ConvertError,
},
types::QualifiedName,
Expand Down Expand Up @@ -91,8 +91,8 @@ fn get_replacement_typedef(
extra_apis: &mut Vec<UnanalyzedApi>,
) -> Result<Api<TypedefPhase>, ConvertErrorWithContext> {
let mut converted_type = ity.clone();
let id = ity.ident.clone();
AutocxxBindgenAnnotations::remove_bindgen_attrs(&mut converted_type.attrs, id)?;
let metadata = BindgenSemanticAttributes::new_retaining_others(&mut converted_type.attrs);
metadata.check_for_fatal_attrs(&ity.ident)?;
let type_conversion_results = type_converter.convert_type(
(*ity.ty).clone(),
name.name.get_namespace(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,48 @@ use crate::conversion::{
/// The set of all annotations that autocxx_bindgen has added
/// for our benefit.
#[derive(Debug)]
pub(crate) struct AutocxxBindgenAnnotations(Vec<AutocxxBindgenAttribute>);
pub(crate) struct BindgenSemanticAttributes(Vec<BindgenSemanticAttribute>);

impl BindgenSemanticAttributes {
// Remove `bindgen_` attributes. They don't have a corresponding macro defined anywhere,
// so they will cause compilation errors if we leave them in.
// We may return an error if one of the bindgen attributes shows that the
// item can't be processed.
pub(crate) fn new_retaining_others(attrs: &mut Vec<Attribute>) -> Self {
let metadata = Self::new(attrs);
attrs.retain(|a| !(a.path.segments.last().unwrap().ident == "cpp_semantics"));
metadata
}

impl AutocxxBindgenAnnotations {
/// Interprets any `autocxx::bindgen_annotation` within this item's
/// attributes.
pub(crate) fn new(attrs: &[Attribute]) -> Self {
let s = Self(
Self(
attrs
.iter()
.filter_map(|attr| {
if attr.path.segments.last().unwrap().ident == "bindgen_annotation" {
let r: Result<AutocxxBindgenAttribute, syn::Error> = attr.parse_args();
if attr.path.segments.last().unwrap().ident == "cpp_semantics" {
let r: Result<BindgenSemanticAttribute, syn::Error> = attr.parse_args();
r.ok()
} else {
None
}
})
.collect(),
);
s
)
}

/// Some attributes indicate we can never handle a given item. Check for those.
pub(crate) fn check_for_fatal_attrs(
&self,
id_for_context: &Ident,
) -> Result<(), ConvertErrorWithContext> {
if self.has_attr("unused_template_param") {
Err(ConvertErrorWithContext(
ConvertError::UnusedTemplateParam,
Some(ErrorContext::Item(id_for_context.clone())),
))
} else {
Ok(())
}
}

/// Whether the given attribute is present.
Expand Down Expand Up @@ -127,34 +149,15 @@ impl AutocxxBindgenAnnotations {
}
(ref_params, ref_return)
}

// Remove `bindgen_` attributes. They don't have a corresponding macro defined anywhere,
// so they will cause compilation errors if we leave them in.
// We may return an error if one of the bindgen attributes shows that the
// item can't be processed.
pub(crate) fn remove_bindgen_attrs(
attrs: &mut Vec<Attribute>,
id: Ident,
) -> Result<(), ConvertErrorWithContext> {
let annotations = Self::new(&attrs);
if annotations.has_attr("unused_template_param") {
return Err(ConvertErrorWithContext(
ConvertError::UnusedTemplateParam,
Some(ErrorContext::Item(id)),
));
}
attrs.retain(|a| !(a.path.segments.last().unwrap().ident == "bindgen_annotation"));
Ok(())
}
}

#[derive(Debug)]
struct AutocxxBindgenAttribute {
struct BindgenSemanticAttribute {
annotation_name: Ident,
body: Option<TokenStream>,
}

impl AutocxxBindgenAttribute {
impl BindgenSemanticAttribute {
fn is_ident(&self, name: &str) -> bool {
self.annotation_name == name
}
Expand All @@ -164,7 +167,7 @@ impl AutocxxBindgenAttribute {
}
}

impl Parse for AutocxxBindgenAttribute {
impl Parse for BindgenSemanticAttribute {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let annotation_name: Ident = input.parse()?;
if input.peek(syn::token::Paren) {
Expand Down
4 changes: 2 additions & 2 deletions engine/src/conversion/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

mod bindgen_annotations;
mod bindgen_semantic_attributes;
mod parse_bindgen;
mod parse_foreign_mod;

pub(crate) use bindgen_annotations::AutocxxBindgenAnnotations;
pub(crate) use bindgen_semantic_attributes::BindgenSemanticAttributes;
pub(crate) use parse_bindgen::ParseBindgen;
18 changes: 10 additions & 8 deletions engine/src/conversion/parse/parse_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ use crate::{
use autocxx_parser::IncludeCppConfig;
use syn::{parse_quote, Fields, Ident, Item, TypePath, UseTree};

use super::{super::utilities::generate_utilities, bindgen_annotations::AutocxxBindgenAnnotations};
use super::{
super::utilities::generate_utilities, bindgen_semantic_attributes::BindgenSemanticAttributes,
};

use super::parse_foreign_mod::ParseForeignMod;

Expand All @@ -42,14 +44,14 @@ pub(crate) struct ParseBindgen<'a> {
apis: Vec<UnanalyzedApi>,
}

fn api_name(ns: &Namespace, id: Ident, attrs: &AutocxxBindgenAnnotations) -> ApiName {
fn api_name(ns: &Namespace, id: Ident, attrs: &BindgenSemanticAttributes) -> ApiName {
ApiName::new_with_cpp_name(ns, id, attrs.get_original_name())
}

pub(crate) fn api_name_qualified(
ns: &Namespace,
id: Ident,
attrs: &AutocxxBindgenAnnotations,
attrs: &BindgenSemanticAttributes,
) -> Result<ApiName, ConvertErrorWithContext> {
match validate_ident_ok_for_cxx(&id.to_string()) {
Err(e) => {
Expand Down Expand Up @@ -161,7 +163,7 @@ impl<'a> ParseBindgen<'a> {
return Ok(());
}
let is_forward_declaration = Self::spot_forward_declaration(&s.fields);
let annotations = AutocxxBindgenAnnotations::new(&s.attrs);
let annotations = BindgenSemanticAttributes::new(&s.attrs);
// cxx::bridge can't cope with type aliases to generic
// types at the moment.
let name = api_name_qualified(ns, s.ident.clone(), &annotations)?;
Expand All @@ -188,7 +190,7 @@ impl<'a> ParseBindgen<'a> {
Ok(())
}
Item::Enum(e) => {
let annotations = AutocxxBindgenAnnotations::new(&e.attrs);
let annotations = BindgenSemanticAttributes::new(&e.attrs);
let api = UnanalyzedApi::Enum {
name: api_name_qualified(ns, e.ident.clone(), &annotations)?,
item: e,
Expand Down Expand Up @@ -250,7 +252,7 @@ impl<'a> ParseBindgen<'a> {
Some(ErrorContext::Item(new_id.clone())),
));
}
let annotations = AutocxxBindgenAnnotations::new(&use_item.attrs);
let annotations = BindgenSemanticAttributes::new(&use_item.attrs);
self.apis.push(UnanalyzedApi::Typedef {
name: api_name(ns, new_id.clone(), &annotations),
item: TypedefKind::Use(parse_quote! {
Expand All @@ -272,15 +274,15 @@ impl<'a> ParseBindgen<'a> {
Ok(())
}
Item::Const(const_item) => {
let annotations = AutocxxBindgenAnnotations::new(&const_item.attrs);
let annotations = BindgenSemanticAttributes::new(&const_item.attrs);
self.apis.push(UnanalyzedApi::Const {
name: api_name(ns, const_item.ident.clone(), &annotations),
const_item,
});
Ok(())
}
Item::Type(ity) => {
let annotations = AutocxxBindgenAnnotations::new(&ity.attrs);
let annotations = BindgenSemanticAttributes::new(&ity.attrs);
self.apis.push(UnanalyzedApi::Typedef {
name: api_name(ns, ity.ident.clone(), &annotations),
item: TypedefKind::Type(ity),
Expand Down
4 changes: 2 additions & 2 deletions engine/src/conversion/parse/parse_foreign_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
use std::collections::HashMap;
use syn::{Block, Expr, ExprCall, ForeignItem, Ident, ImplItem, ItemImpl, Stmt, Type};

use super::bindgen_annotations::AutocxxBindgenAnnotations;
use super::bindgen_semantic_attributes::BindgenSemanticAttributes;

/// Parses a given bindgen-generated 'mod' into suitable
/// [Api]s. In bindgen output, a given mod concerns
Expand Down Expand Up @@ -72,7 +72,7 @@ impl ParseForeignMod {
fn parse_foreign_item(&mut self, i: ForeignItem) -> Result<(), ConvertErrorWithContext> {
match i {
ForeignItem::Fn(item) => {
let annotations = AutocxxBindgenAnnotations::new(&item.attrs);
let annotations = BindgenSemanticAttributes::new(&item.attrs);
let (reference_args, return_type_is_reference) =
annotations.get_reference_parameters_and_return();
let doc_attr = get_doc_attr(&item.attrs);
Expand Down
2 changes: 1 addition & 1 deletion macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub fn extern_rust_function(attr: TokenStream, input: TokenStream) -> TokenStrea
/// normally be compiled by rustc before it undergoes further processing.
#[proc_macro_error]
#[proc_macro_attribute]
pub fn bindgen_annotation(_attr: TokenStream, _input: TokenStream) -> TokenStream {
pub fn cpp_semantics(_attr: TokenStream, _input: TokenStream) -> TokenStream {
abort!(
Span::call_site(),
"Please do not attempt to compile this code. \n\
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ macro_rules! usage {
pub use autocxx_macro::include_cpp_impl;

#[doc(hidden)]
pub use autocxx_macro::bindgen_annotation;
pub use autocxx_macro::cpp_semantics;

macro_rules! ctype_wrapper {
($r:ident, $c:expr, $d:expr) => {
Expand Down Expand Up @@ -814,6 +814,7 @@ pub mod prelude {
pub use crate::c_ulonglong;
pub use crate::c_ushort;
pub use crate::c_void;
pub use crate::cpp_semantics;
pub use crate::include_cpp;
pub use crate::PinMut;
pub use moveit::moveit;
Expand Down

0 comments on commit ae44e9f

Please sign in to comment.