| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use std::collections::hash_map::Entry; |
| 6 |
use std::collections::HashMap; |
| 7 |
use std::str::FromStr; |
| 8 |
|
| 9 |
use crate::bindgen::config::{Config, Language}; |
| 10 |
use crate::bindgen::utilities::SynAttributeHelpers; |
| 11 |
|
| 12 |
// A system for specifying properties on items. Annotations are |
| 13 |
// given through document comments and parsed by this code. |
| 14 |
// |
| 15 |
// An annotation is in the form cbindgen:PROPERTY=VALUE |
| 16 |
// Where PROPERTY depends on the item |
| 17 |
// Where VALUE can be |
| 18 |
// * list - [Item1, Item2, Item3, ...] |
| 19 |
// * atom - Foo |
| 20 |
// * bool - true,false |
| 21 |
// Examples: |
| 22 |
// * cbindgen:field-names=[mHandle, mNamespace] |
| 23 |
// * cbindgen:function-postfix=WR_DESTRUCTOR_SAFE |
| 24 |
|
| 25 |
/// A value specified by an annotation. |
| 26 |
#[derive(Debug, Clone)] |
| 27 |
pub enum AnnotationValue { |
| 28 |
List(Vec<String>), |
| 29 |
Atom(Option<String>), |
| 30 |
Bool(bool), |
| 31 |
} |
| 32 |
|
| 33 |
/// A set of annotations specified by a document comment. |
| 34 |
#[derive(Debug, Default, Clone)] |
| 35 |
pub struct AnnotationSet { |
| 36 |
annotations: HashMap<String, AnnotationValue>, |
| 37 |
pub must_use: bool, |
| 38 |
} |
| 39 |
|
| 40 |
impl AnnotationSet { |
| 41 |
pub fn new() -> AnnotationSet { |
| 42 |
AnnotationSet { |
| 43 |
annotations: HashMap::new(), |
| 44 |
must_use: false, |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
pub fn is_empty(&self) -> bool { |
| 49 |
self.annotations.is_empty() && !self.must_use |
| 50 |
} |
| 51 |
|
| 52 |
pub(crate) fn must_use(&self, config: &Config) -> bool { |
| 53 |
self.must_use && config.language != Language::Cython |
| 54 |
} |
| 55 |
|
| 56 |
pub fn load(attrs: &[syn::Attribute]) -> Result<AnnotationSet, String> { |
| 57 |
let lines = attrs.get_comment_lines(); |
| 58 |
let lines: Vec<&str> = lines |
| 59 |
.iter() |
| 60 |
.filter_map(|line| { |
| 61 |
let line = line.trim_start(); |
| 62 |
if !line.starts_with("cbindgen:") { |
| 63 |
return None; |
| 64 |
} |
| 65 |
|
| 66 |
Some(line) |
| 67 |
}) |
| 68 |
.collect(); |
| 69 |
|
| 70 |
let must_use = attrs.has_attr_word("must_use"); |
| 71 |
|
| 72 |
let mut annotations = HashMap::new(); |
| 73 |
|
| 74 |
// Look at each line for an annotation |
| 75 |
for line in lines { |
| 76 |
debug_assert!(line.starts_with("cbindgen:")); |
| 77 |
|
| 78 |
// Remove the "cbindgen:" prefix |
| 79 |
let annotation = &line[9..]; |
| 80 |
|
| 81 |
// Split the annotation in two |
| 82 |
let parts: Vec<&str> = annotation.split('=').map(|x| x.trim()).collect(); |
| 83 |
|
| 84 |
if parts.len() > 2 { |
| 85 |
return Err(format!("Couldn't parse {}.", line)); |
| 86 |
} |
| 87 |
|
| 88 |
// Grab the name that this annotation is modifying |
| 89 |
let name = parts[0]; |
| 90 |
|
| 91 |
// If the annotation only has a name, assume it's setting a bool flag |
| 92 |
if parts.len() == 1 { |
| 93 |
annotations.insert(name.to_string(), AnnotationValue::Bool(true)); |
| 94 |
continue; |
| 95 |
} |
| 96 |
|
| 97 |
// Parse the value we're setting the name to |
| 98 |
let value = parts[1]; |
| 99 |
|
| 100 |
if let Some(x) = parse_list(value) { |
| 101 |
annotations.insert(name.to_string(), AnnotationValue::List(x)); |
| 102 |
continue; |
| 103 |
} |
| 104 |
if let Ok(x) = value.parse::<bool>() { |
| 105 |
annotations.insert(name.to_string(), AnnotationValue::Bool(x)); |
| 106 |
continue; |
| 107 |
} |
| 108 |
annotations.insert( |
| 109 |
name.to_string(), |
| 110 |
if value.is_empty() { |
| 111 |
AnnotationValue::Atom(None) |
| 112 |
} else { |
| 113 |
AnnotationValue::Atom(Some(value.to_string())) |
| 114 |
}, |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
Ok(AnnotationSet { |
| 119 |
annotations, |
| 120 |
must_use, |
| 121 |
}) |
| 122 |
} |
| 123 |
|
| 124 |
/// Adds an annotation value if none is specified. |
| 125 |
pub fn add_default(&mut self, name: &str, value: AnnotationValue) { |
| 126 |
if let Entry::Vacant(e) = self.annotations.entry(name.to_string()) { |
| 127 |
e.insert(value); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
pub fn list(&self, name: &str) -> Option<Vec<String>> { |
| 132 |
match self.annotations.get(name) { |
| 133 |
Some(AnnotationValue::List(x)) => Some(x.clone()), |
| 134 |
_ => None, |
| 135 |
} |
| 136 |
} |
| 137 |
pub fn atom(&self, name: &str) -> Option<Option<String>> { |
| 138 |
match self.annotations.get(name) { |
| 139 |
Some(AnnotationValue::Atom(x)) => Some(x.clone()), |
| 140 |
_ => None, |
| 141 |
} |
| 142 |
} |
| 143 |
pub fn bool(&self, name: &str) -> Option<bool> { |
| 144 |
match self.annotations.get(name) { |
| 145 |
Some(AnnotationValue::Bool(x)) => Some(*x), |
| 146 |
_ => None, |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
pub fn parse_atom<T>(&self, name: &str) -> Option<T> |
| 151 |
where |
| 152 |
T: Default + FromStr, |
| 153 |
{ |
| 154 |
match self.annotations.get(name) { |
| 155 |
Some(AnnotationValue::Atom(x)) => Some( |
| 156 |
x.as_ref() |
| 157 |
.map_or(T::default(), |y| y.parse::<T>().ok().unwrap()), |
| 158 |
), |
| 159 |
_ => None, |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/// Parse lists like "[x, y, z]". This is not implemented efficiently or well. |
| 165 |
fn parse_list(list: &str) -> Option<Vec<String>> { |
| 166 |
if list.len() < 2 { |
| 167 |
return None; |
| 168 |
} |
| 169 |
|
| 170 |
match (list.chars().next(), list.chars().last()) { |
| 171 |
(Some('['), Some(']')) => Some( |
| 172 |
list[1..list.len() - 1] |
| 173 |
.split(',') |
| 174 |
.map(|x| x.trim().to_string()) |
| 175 |
.collect(), |
| 176 |
), |
| 177 |
_ => None, |
| 178 |
} |
| 179 |
} |