|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use proc_macro2::{Ident, TokenStream}; |
| 16 | +use syn::{ |
| 17 | + parenthesized, |
| 18 | + parse::{Parse, Parser}, |
| 19 | + Attribute, LitStr, |
| 20 | +}; |
| 21 | + |
| 22 | +use crate::conversion::{ |
| 23 | + api::{CppVisibility, Layout, References, Virtualness}, |
| 24 | + convert_error::{ConvertErrorWithContext, ErrorContext}, |
| 25 | + ConvertError, |
| 26 | +}; |
| 27 | + |
| 28 | +/// The set of all annotations that autocxx_bindgen has added |
| 29 | +/// for our benefit. |
| 30 | +#[derive(Debug)] |
| 31 | +pub(crate) struct BindgenSemanticAttributes(Vec<BindgenSemanticAttribute>); |
| 32 | + |
| 33 | +impl BindgenSemanticAttributes { |
| 34 | + // Remove `bindgen_` attributes. They don't have a corresponding macro defined anywhere, |
| 35 | + // so they will cause compilation errors if we leave them in. |
| 36 | + // We may return an error if one of the bindgen attributes shows that the |
| 37 | + // item can't be processed. |
| 38 | + pub(crate) fn new_retaining_others(attrs: &mut Vec<Attribute>) -> Self { |
| 39 | + let metadata = Self::new(attrs); |
| 40 | + attrs.retain(|a| !(a.path.segments.last().unwrap().ident == "cpp_semantics")); |
| 41 | + metadata |
| 42 | + } |
| 43 | + |
| 44 | + pub(crate) fn new(attrs: &[Attribute]) -> Self { |
| 45 | + Self( |
| 46 | + attrs |
| 47 | + .iter() |
| 48 | + .filter_map(|attr| { |
| 49 | + if attr.path.segments.last().unwrap().ident == "cpp_semantics" { |
| 50 | + let r: Result<BindgenSemanticAttribute, syn::Error> = attr.parse_args(); |
| 51 | + r.ok() |
| 52 | + } else { |
| 53 | + None |
| 54 | + } |
| 55 | + }) |
| 56 | + .collect(), |
| 57 | + ) |
| 58 | + } |
| 59 | + |
| 60 | + /// Some attributes indicate we can never handle a given item. Check for those. |
| 61 | + pub(crate) fn check_for_fatal_attrs( |
| 62 | + &self, |
| 63 | + id_for_context: &Ident, |
| 64 | + ) -> Result<(), ConvertErrorWithContext> { |
| 65 | + if self.has_attr("unused_template_param") { |
| 66 | + Err(ConvertErrorWithContext( |
| 67 | + ConvertError::UnusedTemplateParam, |
| 68 | + Some(ErrorContext::Item(id_for_context.clone())), |
| 69 | + )) |
| 70 | + } else { |
| 71 | + Ok(()) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// Whether the given attribute is present. |
| 76 | + pub(super) fn has_attr(&self, attr_name: &str) -> bool { |
| 77 | + self.0.iter().any(|a| a.is_ident(attr_name)) |
| 78 | + } |
| 79 | + |
| 80 | + /// The C++ visibility of the item. |
| 81 | + pub(super) fn get_cpp_visibility(&self) -> CppVisibility { |
| 82 | + if self.has_attr("visibility_private") { |
| 83 | + CppVisibility::Private |
| 84 | + } else if self.has_attr("visibility_protected") { |
| 85 | + CppVisibility::Protected |
| 86 | + } else { |
| 87 | + CppVisibility::Public |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /// Whether the item is virtual. |
| 92 | + pub(super) fn get_virtualness(&self) -> Virtualness { |
| 93 | + if self.has_attr("pure_virtual") { |
| 94 | + Virtualness::PureVirtual |
| 95 | + } else if self.has_attr("bindgen_virtual") { |
| 96 | + Virtualness::Virtual |
| 97 | + } else { |
| 98 | + Virtualness::None |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + fn parse_if_present<T: Parse>(&self, annotation: &str) -> Option<T> { |
| 103 | + self.0 |
| 104 | + .iter() |
| 105 | + .find(|a| a.is_ident(annotation)) |
| 106 | + .map(|a| a.parse_args().unwrap()) |
| 107 | + } |
| 108 | + |
| 109 | + fn string_if_present(&self, annotation: &str) -> Option<String> { |
| 110 | + let ls: Option<LitStr> = self.parse_if_present(annotation); |
| 111 | + ls.map(|ls| ls.value()) |
| 112 | + } |
| 113 | + |
| 114 | + /// The in-memory layout of the item. |
| 115 | + pub(super) fn get_layout(&self) -> Option<Layout> { |
| 116 | + self.parse_if_present("layout") |
| 117 | + } |
| 118 | + |
| 119 | + /// The original C++ name, which bindgen may have changed. |
| 120 | + pub(super) fn get_original_name(&self) -> Option<String> { |
| 121 | + self.string_if_present("original_name") |
| 122 | + } |
| 123 | + |
| 124 | + fn get_bindgen_special_member_annotation(&self) -> Option<String> { |
| 125 | + self.string_if_present("special_member") |
| 126 | + } |
| 127 | + |
| 128 | + /// Whether this is a move constructor. |
| 129 | + pub(super) fn is_move_constructor(&self) -> bool { |
| 130 | + self.get_bindgen_special_member_annotation() |
| 131 | + .map_or(false, |val| val == "move_ctor") |
| 132 | + } |
| 133 | + |
| 134 | + /// Any reference parameters or return values. |
| 135 | + pub(super) fn get_reference_parameters_and_return(&self) -> References { |
| 136 | + let mut results = References::default(); |
| 137 | + for a in &self.0 { |
| 138 | + if a.is_ident("ret_type_reference") { |
| 139 | + results.ref_return = true; |
| 140 | + } else if a.is_ident("ret_type_rvalue_reference") { |
| 141 | + results.rvalue_ref_return = true; |
| 142 | + } else if a.is_ident("arg_type_reference") { |
| 143 | + let r: Result<Ident, syn::Error> = a.parse_args(); |
| 144 | + if let Ok(ls) = r { |
| 145 | + results.ref_params.insert(ls); |
| 146 | + } |
| 147 | + } else if a.is_ident("arg_type_rvalue_reference") { |
| 148 | + let r: Result<Ident, syn::Error> = a.parse_args(); |
| 149 | + if let Ok(ls) = r { |
| 150 | + results.rvalue_ref_params.insert(ls); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + results |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +#[derive(Debug)] |
| 159 | +struct BindgenSemanticAttribute { |
| 160 | + annotation_name: Ident, |
| 161 | + body: Option<TokenStream>, |
| 162 | +} |
| 163 | + |
| 164 | +impl BindgenSemanticAttribute { |
| 165 | + fn is_ident(&self, name: &str) -> bool { |
| 166 | + self.annotation_name == name |
| 167 | + } |
| 168 | + |
| 169 | + fn parse_args<T: Parse>(&self) -> Result<T, syn::Error> { |
| 170 | + T::parse.parse2(self.body.as_ref().unwrap().clone()) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +impl Parse for BindgenSemanticAttribute { |
| 175 | + fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> { |
| 176 | + let annotation_name: Ident = input.parse()?; |
| 177 | + if input.peek(syn::token::Paren) { |
| 178 | + let body_contents; |
| 179 | + parenthesized!(body_contents in input); |
| 180 | + Ok(Self { |
| 181 | + annotation_name, |
| 182 | + body: Some(body_contents.parse()?), |
| 183 | + }) |
| 184 | + } else if !input.is_empty() { |
| 185 | + Err(input.error("expected nothing")) |
| 186 | + } else { |
| 187 | + Ok(Self { |
| 188 | + annotation_name, |
| 189 | + body: None, |
| 190 | + }) |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments