| 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::HashMap; |
| 6 | use std::io::Write; |
| 7 | |
| 8 | use syn::ext::IdentExt; |
| 9 | |
| 10 | use crate::bindgen::config::{Config, Language}; |
| 11 | use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver; |
| 12 | use crate::bindgen::dependencies::Dependencies; |
| 13 | use crate::bindgen::ir::{ |
| 14 | AnnotationSet, Cfg, ConditionWrite, Documentation, Field, GenericArgument, GenericParams, Item, |
| 15 | ItemContainer, Path, ToCondition, Type, |
| 16 | }; |
| 17 | use crate::bindgen::library::Library; |
| 18 | use crate::bindgen::mangle; |
| 19 | use crate::bindgen::monomorph::Monomorphs; |
| 20 | use crate::bindgen::writer::{Source, SourceWriter}; |
| 21 | |
| 22 | /// A type alias that is represented as a C typedef |
| 23 | #[derive (Debug, Clone)] |
| 24 | pub struct Typedef { |
| 25 | pub path: Path, |
| 26 | pub export_name: String, |
| 27 | pub generic_params: GenericParams, |
| 28 | pub aliased: Type, |
| 29 | pub cfg: Option<Cfg>, |
| 30 | pub annotations: AnnotationSet, |
| 31 | pub documentation: Documentation, |
| 32 | } |
| 33 | |
| 34 | impl Typedef { |
| 35 | pub fn load(item: &syn::ItemType, mod_cfg: Option<&Cfg>) -> Result<Typedef, String> { |
| 36 | if let Some(x) = Type::load(&item.ty)? { |
| 37 | let path = Path::new(item.ident.unraw().to_string()); |
| 38 | Ok(Typedef::new( |
| 39 | path, |
| 40 | GenericParams::load(&item.generics)?, |
| 41 | x, |
| 42 | Cfg::append(mod_cfg, Cfg::load(&item.attrs)), |
| 43 | AnnotationSet::load(&item.attrs)?, |
| 44 | Documentation::load(&item.attrs), |
| 45 | )) |
| 46 | } else { |
| 47 | Err("Cannot have a typedef of a zero sized type." .to_owned()) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | pub fn new( |
| 52 | path: Path, |
| 53 | generic_params: GenericParams, |
| 54 | aliased: Type, |
| 55 | cfg: Option<Cfg>, |
| 56 | annotations: AnnotationSet, |
| 57 | documentation: Documentation, |
| 58 | ) -> Self { |
| 59 | let export_name = path.name().to_owned(); |
| 60 | Self { |
| 61 | path, |
| 62 | export_name, |
| 63 | generic_params, |
| 64 | aliased, |
| 65 | cfg, |
| 66 | annotations, |
| 67 | documentation, |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | pub fn simplify_standard_types(&mut self, config: &Config) { |
| 72 | self.aliased.simplify_standard_types(config); |
| 73 | } |
| 74 | |
| 75 | pub fn transfer_annotations(&mut self, out: &mut HashMap<Path, AnnotationSet>) { |
| 76 | if self.annotations.is_empty() { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | if let Some(alias_path) = self.aliased.get_root_path() { |
| 81 | if out.contains_key(&alias_path) { |
| 82 | warn!( |
| 83 | "Multiple typedef's with annotations for {}. Ignoring annotations from {}." , |
| 84 | alias_path, self.path |
| 85 | ); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | out.insert(alias_path, self.annotations.clone()); |
| 90 | self.annotations = AnnotationSet::new(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | pub fn is_generic(&self) -> bool { |
| 95 | self.generic_params.len() > 0 |
| 96 | } |
| 97 | |
| 98 | pub fn add_monomorphs(&self, library: &Library, out: &mut Monomorphs) { |
| 99 | // Generic structs can instantiate monomorphs only once they've been |
| 100 | // instantiated. See `instantiate_monomorph` for more details. |
| 101 | if self.is_generic() { |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | self.aliased.add_monomorphs(library, out); |
| 106 | } |
| 107 | |
| 108 | pub fn mangle_paths(&mut self, monomorphs: &Monomorphs) { |
| 109 | self.aliased.mangle_paths(monomorphs); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | impl Item for Typedef { |
| 114 | fn path(&self) -> &Path { |
| 115 | &self.path |
| 116 | } |
| 117 | |
| 118 | fn export_name(&self) -> &str { |
| 119 | &self.export_name |
| 120 | } |
| 121 | |
| 122 | fn cfg(&self) -> Option<&Cfg> { |
| 123 | self.cfg.as_ref() |
| 124 | } |
| 125 | |
| 126 | fn annotations(&self) -> &AnnotationSet { |
| 127 | &self.annotations |
| 128 | } |
| 129 | |
| 130 | fn annotations_mut(&mut self) -> &mut AnnotationSet { |
| 131 | &mut self.annotations |
| 132 | } |
| 133 | |
| 134 | fn container(&self) -> ItemContainer { |
| 135 | ItemContainer::Typedef(self.clone()) |
| 136 | } |
| 137 | |
| 138 | fn rename_for_config(&mut self, config: &Config) { |
| 139 | config.export.rename(&mut self.export_name); |
| 140 | self.aliased.rename_for_config(config, &self.generic_params); |
| 141 | } |
| 142 | |
| 143 | fn collect_declaration_types(&self, resolver: &mut DeclarationTypeResolver) { |
| 144 | resolver.add_none(&self.path); |
| 145 | } |
| 146 | |
| 147 | fn resolve_declaration_types(&mut self, resolver: &DeclarationTypeResolver) { |
| 148 | self.aliased.resolve_declaration_types(resolver); |
| 149 | } |
| 150 | |
| 151 | fn add_dependencies(&self, library: &Library, out: &mut Dependencies) { |
| 152 | self.aliased |
| 153 | .add_dependencies_ignoring_generics(&self.generic_params, library, out); |
| 154 | } |
| 155 | |
| 156 | fn instantiate_monomorph( |
| 157 | &self, |
| 158 | generic_values: &[GenericArgument], |
| 159 | library: &Library, |
| 160 | out: &mut Monomorphs, |
| 161 | ) { |
| 162 | let mappings = self.generic_params.call(self.path.name(), generic_values); |
| 163 | |
| 164 | let mangled_path = mangle::mangle_path( |
| 165 | &self.path, |
| 166 | generic_values, |
| 167 | &library.get_config().export.mangle, |
| 168 | ); |
| 169 | |
| 170 | let monomorph = Typedef::new( |
| 171 | mangled_path, |
| 172 | GenericParams::default(), |
| 173 | self.aliased.specialize(&mappings), |
| 174 | self.cfg.clone(), |
| 175 | self.annotations.clone(), |
| 176 | self.documentation.clone(), |
| 177 | ); |
| 178 | |
| 179 | out.insert_typedef(library, self, monomorph, generic_values.to_owned()); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | impl Source for Typedef { |
| 184 | fn write<F: Write>(&self, config: &Config, out: &mut SourceWriter<F>) { |
| 185 | let condition = self.cfg.to_condition(config); |
| 186 | condition.write_before(config, out); |
| 187 | |
| 188 | self.documentation.write(config, out); |
| 189 | |
| 190 | self.generic_params.write(config, out); |
| 191 | |
| 192 | match config.language { |
| 193 | Language::Cxx => { |
| 194 | write!(out, "using {} = " , self.export_name()); |
| 195 | self.aliased.write(config, out); |
| 196 | } |
| 197 | Language::C | Language::Cython => { |
| 198 | write!(out, " {} " , config.language.typedef()); |
| 199 | Field::from_name_and_type(self.export_name().to_owned(), self.aliased.clone()) |
| 200 | .write(config, out); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | out.write(";" ); |
| 205 | |
| 206 | condition.write_after(config, out); |
| 207 | } |
| 208 | } |
| 209 | |