| 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::io::Write; |
| 6 |
|
| 7 |
use syn::ext::IdentExt; |
| 8 |
|
| 9 |
use crate::bindgen::cdecl; |
| 10 |
use crate::bindgen::config::Config; |
| 11 |
use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver; |
| 12 |
use crate::bindgen::dependencies::Dependencies; |
| 13 |
use crate::bindgen::ir::{AnnotationSet, Cfg, Documentation, Item, ItemContainer, Path, Type}; |
| 14 |
use crate::bindgen::library::Library; |
| 15 |
use crate::bindgen::writer::{Source, SourceWriter}; |
| 16 |
|
| 17 |
#[derive(Debug, Clone)] |
| 18 |
pub struct Static { |
| 19 |
pub path: Path, |
| 20 |
pub export_name: String, |
| 21 |
pub ty: Type, |
| 22 |
pub mutable: bool, |
| 23 |
pub cfg: Option<Cfg>, |
| 24 |
pub annotations: AnnotationSet, |
| 25 |
pub documentation: Documentation, |
| 26 |
} |
| 27 |
|
| 28 |
impl Static { |
| 29 |
pub fn load(item: &syn::ItemStatic, mod_cfg: Option<&Cfg>) -> Result<Static, String> { |
| 30 |
let ty = Type::load(&item.ty)?; |
| 31 |
|
| 32 |
if ty.is_none() { |
| 33 |
return Err("Cannot have a zero sized static definition.".to_owned()); |
| 34 |
} |
| 35 |
|
| 36 |
Ok(Static::new( |
| 37 |
Path::new(item.ident.unraw().to_string()), |
| 38 |
ty.unwrap(), |
| 39 |
item.mutability.is_some(), |
| 40 |
Cfg::append(mod_cfg, Cfg::load(&item.attrs)), |
| 41 |
AnnotationSet::load(&item.attrs)?, |
| 42 |
Documentation::load(&item.attrs), |
| 43 |
)) |
| 44 |
} |
| 45 |
|
| 46 |
pub fn new( |
| 47 |
path: Path, |
| 48 |
ty: Type, |
| 49 |
mutable: bool, |
| 50 |
cfg: Option<Cfg>, |
| 51 |
annotations: AnnotationSet, |
| 52 |
documentation: Documentation, |
| 53 |
) -> Self { |
| 54 |
let export_name = path.name().to_owned(); |
| 55 |
Self { |
| 56 |
path, |
| 57 |
export_name, |
| 58 |
ty, |
| 59 |
mutable, |
| 60 |
cfg, |
| 61 |
annotations, |
| 62 |
documentation, |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
pub fn simplify_standard_types(&mut self, config: &Config) { |
| 67 |
self.ty.simplify_standard_types(config); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
impl Item for Static { |
| 72 |
fn path(&self) -> &Path { |
| 73 |
&self.path |
| 74 |
} |
| 75 |
|
| 76 |
fn export_name(&self) -> &str { |
| 77 |
&self.export_name |
| 78 |
} |
| 79 |
|
| 80 |
fn cfg(&self) -> Option<&Cfg> { |
| 81 |
self.cfg.as_ref() |
| 82 |
} |
| 83 |
|
| 84 |
fn annotations(&self) -> &AnnotationSet { |
| 85 |
&self.annotations |
| 86 |
} |
| 87 |
|
| 88 |
fn annotations_mut(&mut self) -> &mut AnnotationSet { |
| 89 |
&mut self.annotations |
| 90 |
} |
| 91 |
|
| 92 |
fn container(&self) -> ItemContainer { |
| 93 |
ItemContainer::Static(self.clone()) |
| 94 |
} |
| 95 |
|
| 96 |
fn rename_for_config(&mut self, config: &Config) { |
| 97 |
self.ty.rename_for_config(config, &Default::default()); |
| 98 |
} |
| 99 |
|
| 100 |
fn resolve_declaration_types(&mut self, resolver: &DeclarationTypeResolver) { |
| 101 |
self.ty.resolve_declaration_types(resolver); |
| 102 |
} |
| 103 |
|
| 104 |
fn add_dependencies(&self, library: &Library, out: &mut Dependencies) { |
| 105 |
self.ty.add_dependencies(library, out); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
impl Source for Static { |
| 110 |
fn write<F: Write>(&self, config: &Config, out: &mut SourceWriter<F>) { |
| 111 |
out.write("extern "); |
| 112 |
if let Type::Ptr { is_const: true, .. } = self.ty { |
| 113 |
} else if !self.mutable { |
| 114 |
out.write("const "); |
| 115 |
} |
| 116 |
cdecl::write_field(out, &self.ty, &self.export_name, config); |
| 117 |
out.write(";"); |
| 118 |
} |
| 119 |
} |