2018-07-25 11:42:01 +01:00
|
|
|
//! Because some WebIDL constructs are defined in multiple places
|
|
|
|
//! (keyword `partial` is used to add to an existing construct),
|
|
|
|
//! We need to first walk the webidl to collect all non-partial
|
|
|
|
//! constructs so that we have containers in which to put the
|
|
|
|
//! partial ones.
|
|
|
|
//!
|
|
|
|
//! Only `interface`s, `dictionary`s, `enum`s and `mixin`s can
|
|
|
|
//! be partial.
|
|
|
|
|
2018-08-03 14:39:33 -07:00
|
|
|
use std::collections::{BTreeMap, BTreeSet};
|
2018-07-13 21:46:36 -07:00
|
|
|
|
2018-08-03 14:39:33 -07:00
|
|
|
use weedle::argument::Argument;
|
|
|
|
use weedle::attribute::ExtendedAttribute;
|
|
|
|
use weedle::interface::StringifierOrStatic;
|
|
|
|
use weedle::mixin::MixinMembers;
|
|
|
|
use weedle;
|
2018-07-13 21:46:36 -07:00
|
|
|
|
|
|
|
use super::Result;
|
2018-08-03 14:39:33 -07:00
|
|
|
use util;
|
2018-08-04 10:00:30 -07:00
|
|
|
use util::camel_case_ident;
|
2018-07-13 21:46:36 -07:00
|
|
|
|
2018-07-25 11:42:01 +01:00
|
|
|
/// Collection of constructs that may use partial.
|
2018-07-13 21:46:36 -07:00
|
|
|
#[derive(Default)]
|
2018-08-03 14:39:33 -07:00
|
|
|
pub(crate) struct FirstPassRecord<'src> {
|
|
|
|
pub(crate) interfaces: BTreeMap<&'src str, InterfaceData<'src>>,
|
|
|
|
pub(crate) dictionaries: BTreeSet<&'src str>,
|
|
|
|
pub(crate) enums: BTreeSet<&'src str>,
|
2018-07-25 11:42:01 +01:00
|
|
|
/// The mixins, mapping their name to the webidl ast node for the mixin.
|
2018-08-03 14:39:33 -07:00
|
|
|
pub(crate) mixins: BTreeMap<&'src str, Vec<&'src MixinMembers<'src>>>,
|
|
|
|
pub(crate) typedefs: BTreeMap<&'src str, &'src weedle::types::Type<'src>>,
|
2018-08-10 17:06:11 +01:00
|
|
|
pub(crate) namespaces: BTreeMap<&'src str, NamespaceData<'src>>,
|
2018-07-13 21:46:36 -07:00
|
|
|
}
|
|
|
|
|
2018-07-30 17:41:22 +03:00
|
|
|
/// We need to collect interface data during the first pass, to be used later.
|
|
|
|
#[derive(Default)]
|
2018-08-03 14:39:33 -07:00
|
|
|
pub(crate) struct InterfaceData<'src> {
|
2018-07-30 17:41:22 +03:00
|
|
|
/// Whether only partial interfaces were encountered
|
|
|
|
pub(crate) partial: bool,
|
2018-08-05 17:32:39 +03:00
|
|
|
pub(crate) global: bool,
|
2018-08-03 14:39:33 -07:00
|
|
|
pub(crate) operations: BTreeMap<OperationId<'src>, OperationData<'src>>,
|
2018-08-04 10:00:30 -07:00
|
|
|
pub(crate) superclass: Option<&'src str>,
|
2018-07-30 17:41:22 +03:00
|
|
|
}
|
|
|
|
|
2018-08-10 17:06:11 +01:00
|
|
|
impl<'src> Default for InterfaceData<'src> {
|
|
|
|
fn default() -> Self {
|
|
|
|
InterfaceData {
|
|
|
|
partial: true,
|
|
|
|
global: false,
|
|
|
|
operations: Default::default(),
|
|
|
|
superclass: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// We need to collect namespace data during the first pass, to be used later.
|
|
|
|
#[derive(Default)]
|
|
|
|
pub(crate) struct NamespaceData<'src> {
|
|
|
|
/// Whether only partial namespaces were encountered
|
|
|
|
pub(crate) partial: bool,
|
|
|
|
pub(crate) operations: BTreeMap<Option<&'src str>, OperationData<'src>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'src> Default for NamespaceData<'src> {
|
|
|
|
fn default() -> Self {
|
|
|
|
NamespaceData {
|
|
|
|
partial: true,
|
|
|
|
operations: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-30 17:41:22 +03:00
|
|
|
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
2018-08-03 14:39:33 -07:00
|
|
|
pub(crate) enum OperationId<'src> {
|
2018-07-30 17:41:22 +03:00
|
|
|
Constructor,
|
2018-08-06 22:07:36 +03:00
|
|
|
Operation(Option<&'src str>),
|
2018-08-07 00:06:04 +03:00
|
|
|
IndexingGetter,
|
|
|
|
IndexingSetter,
|
|
|
|
IndexingDeleter,
|
2018-07-30 17:41:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
2018-08-03 14:39:33 -07:00
|
|
|
pub(crate) struct OperationData<'src> {
|
2018-07-30 17:41:22 +03:00
|
|
|
pub(crate) overloaded: bool,
|
|
|
|
/// Map from argument names to whether they are the same for multiple overloads
|
2018-08-03 14:39:33 -07:00
|
|
|
pub(crate) argument_names_same: BTreeMap<Vec<&'src str>, bool>,
|
2018-07-13 21:46:36 -07:00
|
|
|
}
|
|
|
|
|
2018-07-25 11:42:01 +01:00
|
|
|
/// Implemented on an AST node to populate the `FirstPassRecord` struct.
|
2018-08-03 14:39:33 -07:00
|
|
|
pub(crate) trait FirstPass<'src, Ctx> {
|
2018-07-25 11:42:01 +01:00
|
|
|
/// Populate `record` with any constructs in `self`.
|
2018-08-03 14:39:33 -07:00
|
|
|
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, ctx: Ctx) -> Result<()>;
|
2018-07-13 21:46:36 -07:00
|
|
|
}
|
|
|
|
|
2018-08-03 14:39:33 -07:00
|
|
|
impl<'src> FirstPass<'src, ()> for [weedle::Definition<'src>] {
|
|
|
|
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, (): ()) -> Result<()> {
|
2018-07-13 21:46:36 -07:00
|
|
|
for def in self {
|
2018-07-30 17:41:22 +03:00
|
|
|
def.first_pass(record, ())?;
|
2018-07-13 21:46:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 14:39:33 -07:00
|
|
|
impl<'src> FirstPass<'src, ()> for weedle::Definition<'src> {
|
|
|
|
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, (): ()) -> Result<()> {
|
|
|
|
use weedle::Definition::*;
|
2018-07-13 21:46:36 -07:00
|
|
|
|
|
|
|
match self {
|
2018-07-30 17:41:22 +03:00
|
|
|
Dictionary(dictionary) => dictionary.first_pass(record, ()),
|
|
|
|
Enum(enum_) => enum_.first_pass(record, ()),
|
|
|
|
Interface(interface) => interface.first_pass(record, ()),
|
2018-08-03 14:39:33 -07:00
|
|
|
PartialInterface(interface) => interface.first_pass(record, ()),
|
|
|
|
InterfaceMixin(mixin) => mixin.first_pass(record, ()),
|
|
|
|
PartialInterfaceMixin(mixin) => mixin.first_pass(record, ()),
|
2018-08-10 17:06:11 +01:00
|
|
|
Namespace(namespace) => namespace.first_pass(record, ()),
|
|
|
|
PartialNamespace(namespace) => namespace.first_pass(record, ()),
|
2018-08-04 14:01:35 +03:00
|
|
|
Typedef(typedef) => typedef.first_pass(record, ()),
|
2018-07-13 21:46:36 -07:00
|
|
|
_ => {
|
|
|
|
// Other definitions aren't currently used in the first pass
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 14:39:33 -07:00
|
|
|
impl<'src> FirstPass<'src, ()> for weedle::DictionaryDefinition<'src> {
|
|
|
|
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, (): ()) -> Result<()> {
|
|
|
|
if !record.dictionaries.insert(self.identifier.0) {
|
|
|
|
warn!("encountered multiple dictionary declarations of {}", self.identifier.0);
|
2018-07-13 21:46:36 -07:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 14:39:33 -07:00
|
|
|
impl<'src> FirstPass<'src, ()> for weedle::EnumDefinition<'src> {
|
|
|
|
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, (): ()) -> Result<()> {
|
|
|
|
if !record.enums.insert(self.identifier.0) {
|
|
|
|
warn!("Encountered multiple enum declarations of {}", self.identifier.0);
|
2018-07-13 21:46:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 17:06:11 +01:00
|
|
|
/// Helper function to add an operation to an interface.
|
|
|
|
fn first_pass_interface_operation<'src>(
|
2018-08-03 14:39:33 -07:00
|
|
|
record: &mut FirstPassRecord<'src>,
|
|
|
|
self_name: &'src str,
|
|
|
|
id: OperationId<'src>,
|
|
|
|
arguments: &[Argument<'src>],
|
2018-07-30 17:41:22 +03:00
|
|
|
) -> Result<()> {
|
2018-08-03 14:39:33 -07:00
|
|
|
let mut names = Vec::with_capacity(arguments.len());
|
|
|
|
for argument in arguments {
|
|
|
|
match argument {
|
|
|
|
Argument::Single(arg) => names.push(arg.identifier.0),
|
|
|
|
Argument::Variadic(_) => return Ok(()),
|
|
|
|
}
|
|
|
|
}
|
2018-07-30 17:41:22 +03:00
|
|
|
record
|
|
|
|
.interfaces
|
|
|
|
.get_mut(self_name)
|
|
|
|
.unwrap()
|
|
|
|
.operations
|
|
|
|
.entry(id)
|
|
|
|
.and_modify(|operation_data| operation_data.overloaded = true)
|
2018-08-03 14:39:33 -07:00
|
|
|
.or_insert_with(Default::default)
|
2018-07-30 17:41:22 +03:00
|
|
|
.argument_names_same
|
2018-08-03 14:39:33 -07:00
|
|
|
.entry(names)
|
2018-07-30 17:41:22 +03:00
|
|
|
.and_modify(|same_argument_names| *same_argument_names = true)
|
|
|
|
.or_insert(false);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-08-03 14:39:33 -07:00
|
|
|
impl<'src> FirstPass<'src, ()> for weedle::InterfaceDefinition<'src> {
|
|
|
|
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, (): ()) -> Result<()> {
|
|
|
|
{
|
|
|
|
let interface = record
|
|
|
|
.interfaces
|
|
|
|
.entry(self.identifier.0)
|
|
|
|
.or_insert_with(Default::default);
|
|
|
|
interface.partial = false;
|
2018-08-04 10:00:30 -07:00
|
|
|
interface.superclass = self.inheritance.map(|s| s.identifier.0);
|
2018-07-13 21:46:36 -07:00
|
|
|
}
|
|
|
|
|
2018-08-03 14:39:33 -07:00
|
|
|
if util::is_chrome_only(&self.attributes) {
|
2018-07-30 17:41:22 +03:00
|
|
|
return Ok(())
|
|
|
|
}
|
|
|
|
|
2018-08-03 14:39:33 -07:00
|
|
|
if let Some(attrs) = &self.attributes {
|
|
|
|
for attr in &attrs.body.list {
|
|
|
|
attr.first_pass(record, self.identifier.0)?;
|
|
|
|
}
|
2018-07-30 17:41:22 +03:00
|
|
|
}
|
|
|
|
|
2018-08-03 14:39:33 -07:00
|
|
|
for member in &self.members.body {
|
|
|
|
member.first_pass(record, self.identifier.0)?;
|
2018-07-13 21:46:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 14:39:33 -07:00
|
|
|
impl<'src> FirstPass<'src, ()> for weedle::PartialInterfaceDefinition<'src> {
|
|
|
|
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, (): ()) -> Result<()> {
|
2018-07-30 17:41:22 +03:00
|
|
|
record
|
|
|
|
.interfaces
|
2018-08-03 14:39:33 -07:00
|
|
|
.entry(self.identifier.0)
|
2018-07-30 17:41:22 +03:00
|
|
|
.or_insert_with(||
|
|
|
|
InterfaceData {
|
|
|
|
partial: true,
|
|
|
|
operations: Default::default(),
|
2018-08-05 17:32:39 +03:00
|
|
|
global: false,
|
2018-08-04 10:00:30 -07:00
|
|
|
superclass: None,
|
2018-07-30 17:41:22 +03:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2018-08-03 14:39:33 -07:00
|
|
|
if util::is_chrome_only(&self.attributes) {
|
2018-07-30 17:41:22 +03:00
|
|
|
return Ok(())
|
|
|
|
}
|
|
|
|
|
2018-08-03 14:39:33 -07:00
|
|
|
for member in &self.members.body {
|
|
|
|
member.first_pass(record, self.identifier.0)?;
|
2018-07-30 17:41:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 14:39:33 -07:00
|
|
|
impl<'src> FirstPass<'src, &'src str> for ExtendedAttribute<'src> {
|
|
|
|
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, self_name: &'src str) -> Result<()> {
|
2018-07-30 17:41:22 +03:00
|
|
|
match self {
|
2018-08-03 14:39:33 -07:00
|
|
|
ExtendedAttribute::ArgList(list) if list.identifier.0 == "Constructor" => {
|
2018-08-10 17:06:11 +01:00
|
|
|
first_pass_interface_operation(
|
2018-08-03 14:39:33 -07:00
|
|
|
record,
|
|
|
|
self_name,
|
|
|
|
OperationId::Constructor,
|
|
|
|
&list.args.body.list,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
ExtendedAttribute::NoArgs(name) if (name.0).0 == "Constructor" => {
|
2018-08-10 17:06:11 +01:00
|
|
|
first_pass_interface_operation(
|
2018-08-03 14:39:33 -07:00
|
|
|
record,
|
|
|
|
self_name,
|
|
|
|
OperationId::Constructor,
|
|
|
|
&[],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
ExtendedAttribute::NamedArgList(list)
|
|
|
|
if list.lhs_identifier.0 == "NamedConstructor" =>
|
|
|
|
{
|
2018-08-10 17:06:11 +01:00
|
|
|
first_pass_interface_operation(
|
2018-08-03 14:39:33 -07:00
|
|
|
record,
|
|
|
|
self_name,
|
|
|
|
OperationId::Constructor,
|
|
|
|
&list.args.body.list,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
ExtendedAttribute::Ident(id) if id.lhs_identifier.0 == "Global" => {
|
|
|
|
record.interfaces.get_mut(self_name).unwrap().global = true;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
ExtendedAttribute::IdentList(id) if id.identifier.0 == "Global" => {
|
|
|
|
record.interfaces.get_mut(self_name).unwrap().global = true;
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-07-30 17:41:22 +03:00
|
|
|
_ => Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 14:39:33 -07:00
|
|
|
impl<'src> FirstPass<'src, &'src str> for weedle::interface::InterfaceMember<'src> {
|
|
|
|
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, self_name: &'src str) -> Result<()> {
|
2018-07-30 17:41:22 +03:00
|
|
|
match self {
|
2018-08-03 14:39:33 -07:00
|
|
|
weedle::interface::InterfaceMember::Operation(op) => {
|
|
|
|
op.first_pass(record, self_name)
|
|
|
|
}
|
2018-07-30 17:41:22 +03:00
|
|
|
_ => Ok(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 14:39:33 -07:00
|
|
|
impl<'src> FirstPass<'src, &'src str> for weedle::interface::OperationInterfaceMember<'src> {
|
|
|
|
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, self_name: &'src str) -> Result<()> {
|
2018-08-06 22:07:36 +03:00
|
|
|
if !self.specials.is_empty() && self.specials.len() != 1 {
|
2018-08-03 14:39:33 -07:00
|
|
|
warn!("Unsupported webidl operation {:?}", self);
|
|
|
|
return Ok(())
|
|
|
|
}
|
|
|
|
if let Some(StringifierOrStatic::Stringifier(_)) = self.modifier {
|
|
|
|
warn!("Unsupported webidl operation {:?}", self);
|
|
|
|
return Ok(())
|
2018-07-30 17:41:22 +03:00
|
|
|
}
|
2018-08-10 17:06:11 +01:00
|
|
|
first_pass_interface_operation(
|
2018-08-05 23:32:31 +03:00
|
|
|
record,
|
|
|
|
self_name,
|
2018-08-06 22:07:36 +03:00
|
|
|
match self.identifier.map(|s| s.0) {
|
|
|
|
None => match self.specials.get(0) {
|
|
|
|
None => OperationId::Operation(None),
|
2018-08-10 17:06:11 +01:00
|
|
|
Some(weedle::interface::Special::Getter(weedle::term::Getter))
|
|
|
|
=> OperationId::IndexingGetter,
|
|
|
|
Some(weedle::interface::Special::Setter(weedle::term::Setter))
|
|
|
|
=> OperationId::IndexingSetter,
|
|
|
|
Some(weedle::interface::Special::Deleter(weedle::term::Deleter))
|
|
|
|
=> OperationId::IndexingDeleter,
|
|
|
|
Some(weedle::interface::Special::LegacyCaller(weedle::term::LegacyCaller))
|
|
|
|
=> return Ok(()),
|
2018-08-05 23:32:31 +03:00
|
|
|
},
|
|
|
|
Some(ref name) => OperationId::Operation(Some(name.clone())),
|
|
|
|
},
|
2018-08-03 14:39:33 -07:00
|
|
|
&self.args.body.list,
|
2018-08-05 23:32:31 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 14:39:33 -07:00
|
|
|
impl<'src> FirstPass<'src, ()> for weedle::InterfaceMixinDefinition<'src>{
|
|
|
|
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, (): ()) -> Result<()> {
|
|
|
|
record
|
2018-07-13 21:46:36 -07:00
|
|
|
.mixins
|
2018-08-03 14:39:33 -07:00
|
|
|
.entry(self.identifier.0)
|
|
|
|
.or_insert_with(Default::default)
|
|
|
|
.push(&self.members.body);
|
2018-07-13 21:46:36 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 14:39:33 -07:00
|
|
|
impl<'src> FirstPass<'src, ()> for weedle::PartialInterfaceMixinDefinition<'src> {
|
|
|
|
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, (): ()) -> Result<()> {
|
|
|
|
record
|
2018-07-13 21:46:36 -07:00
|
|
|
.mixins
|
2018-08-03 14:39:33 -07:00
|
|
|
.entry(self.identifier.0)
|
|
|
|
.or_insert_with(Default::default)
|
|
|
|
.push(&self.members.body);
|
2018-07-13 21:46:36 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2018-08-04 14:01:35 +03:00
|
|
|
|
2018-08-03 14:39:33 -07:00
|
|
|
impl<'src> FirstPass<'src, ()> for weedle::TypedefDefinition<'src> {
|
|
|
|
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, (): ()) -> Result<()> {
|
|
|
|
if util::is_chrome_only(&self.attributes) {
|
2018-08-04 14:01:35 +03:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2018-08-03 14:39:33 -07:00
|
|
|
if record.typedefs.insert(self.identifier.0, &self.type_.type_).is_some() {
|
|
|
|
warn!("Encountered multiple declarations of {}", self.identifier.0);
|
2018-08-04 14:01:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2018-08-04 10:00:30 -07:00
|
|
|
|
|
|
|
impl<'a> FirstPassRecord<'a> {
|
|
|
|
pub fn all_superclasses<'me>(&'me self, interface: &str)
|
|
|
|
-> impl Iterator<Item = String> + 'me
|
|
|
|
{
|
|
|
|
let mut set = BTreeSet::new();
|
|
|
|
self.fill_superclasses(interface, &mut set);
|
|
|
|
set.into_iter()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fill_superclasses(&self, interface: &str, set: &mut BTreeSet<String>) {
|
|
|
|
let data = match self.interfaces.get(interface) {
|
|
|
|
Some(data) => data,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
let superclass = match &data.superclass {
|
|
|
|
Some(class) => class,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
if set.insert(camel_case_ident(superclass)) {
|
|
|
|
self.fill_superclasses(superclass, set);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|