| 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 |
//! # Helpers for finding the named types defined in a UDL interface. |
| 6 |
//! |
| 7 |
//! This module provides the [`TypeFinder`] trait, an abstraction for walking |
| 8 |
//! the weedle parse tree, looking for type definitions, and accumulating them |
| 9 |
//! in a [`TypeCollector`]. |
| 10 |
//! |
| 11 |
//! The type-finding process only discovers very basic information about names |
| 12 |
//! and their corresponding types. For example, it can discover that "Foobar" |
| 13 |
//! names a Record, but it won't discover anything about the fields of that |
| 14 |
//! record. |
| 15 |
//! |
| 16 |
//! Factoring this functionality out into a separate phase makes the subsequent |
| 17 |
//! work of more *detailed* parsing of the UDL a lot simpler, we know how to resolve |
| 18 |
//! names to types when building up the full interface definition. |
| 19 |
|
| 20 |
use std::convert::TryFrom; |
| 21 |
|
| 22 |
use anyhow::{bail, Result}; |
| 23 |
|
| 24 |
use super::TypeCollector; |
| 25 |
use crate::attributes::{InterfaceAttributes, TypedefAttributes}; |
| 26 |
use uniffi_meta::{ObjectImpl, Type}; |
| 27 |
|
| 28 |
// We broke this, so try and be as helpful as possible. |
| 29 |
static ERR_TYPEDEF_EXTERN: &str = r#"`typedef extern` is no longer supported. |
| 30 |
You must replace `extern` with the type of the object: |
| 31 |
* "enum" for Enums. |
| 32 |
* "record", "dictionary" or "struct" for Records. |
| 33 |
* "object", "impl" or "interface" for objects. |
| 34 |
* "trait", "callback" or "trait_with_foreign" for traits. |
| 35 |
|
| 36 |
For example: |
| 37 |
[External="crate_name"] |
| 38 |
typedef extern ExternalEnum; |
| 39 |
|
| 40 |
Would be replaced with: |
| 41 |
[External="crate_name"] |
| 42 |
typedef enum ExternalEnum; |
| 43 |
|
| 44 |
See https://mozilla.github.io/uniffi-rs/next/types/remote_ext_types.html for more. |
| 45 |
|
| 46 |
External Custom Types must be declared in the same way, but with |
| 47 |
[Custom="crate_name"] instead of [Extern="crate_name"] |
| 48 |
|
| 49 |
[Custom="crate_name"] |
| 50 |
typedef string Url; |
| 51 |
|
| 52 |
See https://mozilla.github.io/uniffi-rs/next/types/custom_types.html for more."#; |
| 53 |
|
| 54 |
/// Trait to help with an early "type discovery" phase when processing the UDL. |
| 55 |
/// |
| 56 |
/// This trait does structural matching against weedle AST nodes from a parsed |
| 57 |
/// UDL file, looking for all the newly-defined types in the file and accumulating |
| 58 |
/// them in the given `TypeCollector`. |
| 59 |
pub(crate) trait TypeFinder { |
| 60 |
fn add_type_definitions_to(&self, types: &mut TypeCollector) -> Result<()>; |
| 61 |
} |
| 62 |
|
| 63 |
impl<T: TypeFinder> TypeFinder for &[T] { |
| 64 |
fn add_type_definitions_to(&self, types: &mut TypeCollector) -> Result<()> { |
| 65 |
for item in *self { |
| 66 |
item.add_type_definitions_to(types)?; |
| 67 |
} |
| 68 |
Ok(()) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
impl TypeFinder for weedle::Definition<'_> { |
| 73 |
fn add_type_definitions_to(&self, types: &mut TypeCollector) -> Result<()> { |
| 74 |
match self { |
| 75 |
weedle::Definition::Interface(d) => d.add_type_definitions_to(types), |
| 76 |
weedle::Definition::Dictionary(d) => d.add_type_definitions_to(types), |
| 77 |
weedle::Definition::Enum(d) => d.add_type_definitions_to(types), |
| 78 |
weedle::Definition::Typedef(d) => d.add_type_definitions_to(types), |
| 79 |
weedle::Definition::CallbackInterface(d) => d.add_type_definitions_to(types), |
| 80 |
_ => Ok(()), |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
impl TypeFinder for weedle::InterfaceDefinition<'_> { |
| 86 |
fn add_type_definitions_to(&self, types: &mut TypeCollector) -> Result<()> { |
| 87 |
let name = self.identifier.0.to_string(); |
| 88 |
let attrs = InterfaceAttributes::try_from(self.attributes.as_ref())?; |
| 89 |
// Some enum types are defined using an `interface` with a special attribute. |
| 90 |
if attrs.contains_enum_attr() || attrs.contains_error_attr() { |
| 91 |
types.add_type_definition( |
| 92 |
self.identifier.0, |
| 93 |
Type::Enum { |
| 94 |
name, |
| 95 |
module_path: types.module_path(), |
| 96 |
}, |
| 97 |
) |
| 98 |
} else { |
| 99 |
types.add_type_definition( |
| 100 |
self.identifier.0, |
| 101 |
Type::Object { |
| 102 |
name, |
| 103 |
module_path: types.module_path(), |
| 104 |
imp: attrs.object_impl()?, |
| 105 |
}, |
| 106 |
) |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
impl TypeFinder for weedle::DictionaryDefinition<'_> { |
| 112 |
fn add_type_definitions_to(&self, types: &mut TypeCollector) -> Result<()> { |
| 113 |
let name = self.identifier.0.to_string(); |
| 114 |
types.add_type_definition( |
| 115 |
self.identifier.0, |
| 116 |
Type::Record { |
| 117 |
name, |
| 118 |
module_path: types.module_path(), |
| 119 |
}, |
| 120 |
) |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
impl TypeFinder for weedle::EnumDefinition<'_> { |
| 125 |
fn add_type_definitions_to(&self, types: &mut TypeCollector) -> Result<()> { |
| 126 |
let name = self.identifier.0.to_string(); |
| 127 |
// Our error types are defined using an `enum` with a special attribute. |
| 128 |
types.add_type_definition( |
| 129 |
self.identifier.0, |
| 130 |
Type::Enum { |
| 131 |
name, |
| 132 |
module_path: types.module_path(), |
| 133 |
}, |
| 134 |
) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
impl TypeFinder for weedle::TypedefDefinition<'_> { |
| 139 |
fn add_type_definitions_to(&self, types: &mut TypeCollector) -> Result<()> { |
| 140 |
let attrs = TypedefAttributes::try_from(self.attributes.as_ref())?; |
| 141 |
let ty = if attrs.is_custom() { |
| 142 |
// A type which wraps a builtin with an `FfiConverter` implementation. |
| 143 |
// If local we will generate that implementation, if external we will reference it. |
| 144 |
let builtin = types.resolve_type_expression(&self.type_)?; |
| 145 |
let module_path = attrs |
| 146 |
.get_crate_name() |
| 147 |
.unwrap_or_else(|| types.module_path()); |
| 148 |
Type::Custom { |
| 149 |
module_path, |
| 150 |
name: self.identifier.0.to_string(), |
| 151 |
builtin: builtin.into(), |
| 152 |
} |
| 153 |
} else { |
| 154 |
let typedef_type = match &self.type_.type_ { |
| 155 |
weedle::types::Type::Single(weedle::types::SingleType::NonAny( |
| 156 |
weedle::types::NonAnyType::Identifier(weedle::types::MayBeNull { |
| 157 |
type_: i, |
| 158 |
.. |
| 159 |
}), |
| 160 |
)) => i.0, |
| 161 |
_ => bail!("Failed to get typedef type: {:?}", self), |
| 162 |
}; |
| 163 |
let name = self.identifier.0.to_string(); |
| 164 |
let module_path = attrs |
| 165 |
.get_crate_name() |
| 166 |
.unwrap_or_else(|| types.module_path()); |
| 167 |
match typedef_type { |
| 168 |
"dictionary" | "record" | "struct" => Type::Record { |
| 169 |
module_path, |
| 170 |
name, |
| 171 |
}, |
| 172 |
"enum" => Type::Enum { |
| 173 |
module_path, |
| 174 |
name, |
| 175 |
}, |
| 176 |
"interface" | "impl" => Type::Object { |
| 177 |
module_path, |
| 178 |
name, |
| 179 |
imp: ObjectImpl::Struct, |
| 180 |
}, |
| 181 |
"trait" => Type::Object { |
| 182 |
module_path, |
| 183 |
name, |
| 184 |
imp: ObjectImpl::Trait, |
| 185 |
}, |
| 186 |
"callback" | "trait_with_foreign" => Type::Object { |
| 187 |
module_path, |
| 188 |
name, |
| 189 |
imp: ObjectImpl::CallbackTrait, |
| 190 |
}, |
| 191 |
// "extern" gets a special error to help upgrading |
| 192 |
"extern" => bail!(ERR_TYPEDEF_EXTERN), |
| 193 |
_ => bail!("Can't work out the type - no attributes and unknown extern type '{typedef_type}'"), |
| 194 |
} |
| 195 |
}; |
| 196 |
types.add_type_definition(self.identifier.0, ty) |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
impl TypeFinder for weedle::CallbackInterfaceDefinition<'_> { |
| 201 |
fn add_type_definitions_to(&self, types: &mut TypeCollector) -> Result<()> { |
| 202 |
if self.attributes.is_some() { |
| 203 |
bail!("no callback interface attributes are currently supported"); |
| 204 |
} |
| 205 |
let name = self.identifier.0.to_string(); |
| 206 |
types.add_type_definition( |
| 207 |
self.identifier.0, |
| 208 |
Type::CallbackInterface { |
| 209 |
name, |
| 210 |
module_path: types.module_path(), |
| 211 |
}, |
| 212 |
) |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
#[cfg(test)] |
| 217 |
mod test { |
| 218 |
use super::*; |
| 219 |
use uniffi_meta::ObjectImpl; |
| 220 |
|
| 221 |
// A helper to take valid UDL and a closure to check what's in it. |
| 222 |
fn test_a_finding<F>(udl: &str, tester: F) |
| 223 |
where |
| 224 |
F: FnOnce(TypeCollector), |
| 225 |
{ |
| 226 |
let idl = weedle::parse(udl).unwrap(); |
| 227 |
let mut types = TypeCollector::default(); |
| 228 |
types.add_type_definitions_from(idl.as_ref()).unwrap(); |
| 229 |
tester(types); |
| 230 |
} |
| 231 |
|
| 232 |
#[test] |
| 233 |
fn test_type_finding() { |
| 234 |
test_a_finding( |
| 235 |
r#" |
| 236 |
callback interface TestCallbacks { |
| 237 |
string hello(u32 count); |
| 238 |
}; |
| 239 |
"#, |
| 240 |
|types| { |
| 241 |
assert!( |
| 242 |
matches!(types.get_type_definition("TestCallbacks").unwrap(), Type::CallbackInterface { name, .. } if name == "TestCallbacks") |
| 243 |
); |
| 244 |
}, |
| 245 |
); |
| 246 |
|
| 247 |
test_a_finding( |
| 248 |
r#" |
| 249 |
dictionary TestRecord { |
| 250 |
u32 field; |
| 251 |
}; |
| 252 |
"#, |
| 253 |
|types| { |
| 254 |
assert!( |
| 255 |
matches!(types.get_type_definition("TestRecord").unwrap(), Type::Record { name, .. } if name == "TestRecord") |
| 256 |
); |
| 257 |
}, |
| 258 |
); |
| 259 |
|
| 260 |
test_a_finding( |
| 261 |
r#" |
| 262 |
enum TestItems { "one", "two" }; |
| 263 |
|
| 264 |
[Error] |
| 265 |
enum TestError { "ErrorOne", "ErrorTwo" }; |
| 266 |
"#, |
| 267 |
|types| { |
| 268 |
assert!( |
| 269 |
matches!(types.get_type_definition("TestItems").unwrap(), Type::Enum { name, .. } if name == "TestItems") |
| 270 |
); |
| 271 |
assert!( |
| 272 |
matches!(types.get_type_definition("TestError").unwrap(), Type::Enum { name, .. } if name == "TestError") |
| 273 |
); |
| 274 |
}, |
| 275 |
); |
| 276 |
|
| 277 |
test_a_finding( |
| 278 |
r#" |
| 279 |
interface TestObject { |
| 280 |
constructor(); |
| 281 |
}; |
| 282 |
"#, |
| 283 |
|types| { |
| 284 |
assert!( |
| 285 |
matches!(types.get_type_definition("TestObject").unwrap(), Type::Object{ name, .. } if name == "TestObject") |
| 286 |
); |
| 287 |
}, |
| 288 |
); |
| 289 |
|
| 290 |
test_a_finding( |
| 291 |
r#" |
| 292 |
[External="crate-name"] |
| 293 |
typedef enum ExternalType; |
| 294 |
|
| 295 |
[External="crate-name"] |
| 296 |
typedef interface ExternalInterfaceType; |
| 297 |
|
| 298 |
[Custom] |
| 299 |
typedef string CustomType; |
| 300 |
"#, |
| 301 |
|types| { |
| 302 |
assert!( |
| 303 |
matches!(types.get_type_definition("ExternalType").unwrap(), Type::Enum { name, module_path, .. } |
| 304 |
if name == "ExternalType" && module_path == "crate-name") |
| 305 |
); |
| 306 |
assert!( |
| 307 |
matches!(types.get_type_definition("ExternalInterfaceType").unwrap(), Type::Object { name, module_path, imp: ObjectImpl::Struct, .. } |
| 308 |
if name == "ExternalInterfaceType" && module_path == "crate-name") |
| 309 |
); |
| 310 |
assert!( |
| 311 |
matches!(types.get_type_definition("CustomType").unwrap(), Type::Custom { name, builtin, ..} |
| 312 |
if name == "CustomType" && *builtin == Type::String) |
| 313 |
); |
| 314 |
}, |
| 315 |
); |
| 316 |
} |
| 317 |
|
| 318 |
#[test] |
| 319 |
fn test_extern_local_types() { |
| 320 |
test_a_finding( |
| 321 |
r#" |
| 322 |
typedef interface Interface; |
| 323 |
typedef impl Interface2; |
| 324 |
typedef trait Trait; |
| 325 |
typedef callback Callback; |
| 326 |
|
| 327 |
typedef dictionary R1; |
| 328 |
typedef record R2; |
| 329 |
typedef record R3; |
| 330 |
typedef enum Enum; |
| 331 |
"#, |
| 332 |
|types| { |
| 333 |
assert!(matches!( |
| 334 |
types.get_type_definition("Interface").unwrap(), |
| 335 |
Type::Object { name, module_path, imp: ObjectImpl::Struct } if name == "Interface" && module_path.is_empty())); |
| 336 |
assert!(matches!( |
| 337 |
types.get_type_definition("Interface2").unwrap(), |
| 338 |
Type::Object { name, module_path, imp: ObjectImpl::Struct } if name == "Interface2" && module_path.is_empty())); |
| 339 |
assert!(matches!( |
| 340 |
types.get_type_definition("Trait").unwrap(), |
| 341 |
Type::Object { name, module_path, imp: ObjectImpl::Trait } if name == "Trait" && module_path.is_empty())); |
| 342 |
assert!(matches!( |
| 343 |
types.get_type_definition("Callback").unwrap(), |
| 344 |
Type::Object { name, module_path, imp: ObjectImpl::CallbackTrait } if name == "Callback" && module_path.is_empty())); |
| 345 |
assert!(matches!( |
| 346 |
types.get_type_definition("R1").unwrap(), |
| 347 |
Type::Record { name, module_path } if name == "R1" && module_path.is_empty())); |
| 348 |
assert!(matches!( |
| 349 |
types.get_type_definition("R2").unwrap(), |
| 350 |
Type::Record { name, module_path } if name == "R2" && module_path.is_empty())); |
| 351 |
assert!(matches!( |
| 352 |
types.get_type_definition("R3").unwrap(), |
| 353 |
Type::Record { name, module_path } if name == "R3" && module_path.is_empty())); |
| 354 |
assert!(matches!( |
| 355 |
types.get_type_definition("Enum").unwrap(), |
| 356 |
Type::Enum { name, module_path } if name == "Enum" && module_path.is_empty())); |
| 357 |
}, |
| 358 |
); |
| 359 |
} |
| 360 |
|
| 361 |
fn get_err(udl: &str) -> String { |
| 362 |
let parsed = weedle::parse(udl).unwrap(); |
| 363 |
let mut types = TypeCollector::default(); |
| 364 |
let err = types |
| 365 |
.add_type_definitions_from(parsed.as_ref()) |
| 366 |
.unwrap_err(); |
| 367 |
err.to_string() |
| 368 |
} |
| 369 |
|
| 370 |
#[test] |
| 371 |
fn test_local_type_unknown_typedef() { |
| 372 |
let e = get_err("typedef xyz Foo;"); |
| 373 |
assert!(e.contains("unknown extern type 'xyz'")); |
| 374 |
} |
| 375 |
} |