Skip to content

Commit b38b9da

Browse files
authored
Merge pull request #846 from alexcrichton/no-modules
Remove `Module` node from the backend AST
2 parents 6ce5e6e + 9daa115 commit b38b9da

File tree

3 files changed

+52
-68
lines changed

3 files changed

+52
-68
lines changed

crates/backend/src/ast.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ pub struct Program {
1919
pub structs: Vec<Struct>,
2020
/// rust consts
2121
pub consts: Vec<Const>,
22-
/// rust submodules
23-
pub modules: Vec<Module>,
2422
/// "dictionaries", generated for WebIDL, which are basically just "typed
2523
/// objects" in the sense that they represent a JS object with a particular
2624
/// shape in JIT parlance.
@@ -250,18 +248,6 @@ pub enum ConstValue {
250248
Null,
251249
}
252250

253-
/// A rust module
254-
///
255-
/// This exists to give the ability to namespace js imports.
256-
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
257-
#[derive(Clone)]
258-
pub struct Module {
259-
pub vis: syn::Visibility,
260-
pub name: Ident,
261-
/// js -> rust interfaces
262-
pub imports: Vec<Import>,
263-
}
264-
265251
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
266252
#[derive(Clone)]
267253
pub struct Dictionary {
@@ -284,8 +270,6 @@ impl Program {
284270
structs: self.structs.iter().map(|a| a.shared()).collect(),
285271
enums: self.enums.iter().map(|a| a.shared()).collect(),
286272
imports: self.imports.iter()
287-
// add in imports from inside modules
288-
.chain(self.modules.iter().flat_map(|m| m.imports.iter()))
289273
.map(|a| a.shared())
290274
.collect::<Result<_, Diagnostic>>()?,
291275
version: shared::version(),

crates/backend/src/codegen.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@ impl TryToTokens for ast::Program {
6969
for c in self.consts.iter() {
7070
c.to_tokens(tokens);
7171
}
72-
for m in self.modules.iter() {
73-
if let Err(e) = m.try_to_tokens(tokens) {
74-
errors.push(e);
75-
}
76-
}
7772
for d in self.dictionaries.iter() {
7873
d.to_tokens(tokens);
7974
}
@@ -1101,30 +1096,6 @@ impl ToTokens for ast::Const {
11011096
}
11021097
}
11031098

1104-
impl<'a> TryToTokens for ast::Module {
1105-
fn try_to_tokens(&self, tokens: &mut TokenStream) -> Result<(), Diagnostic> {
1106-
for import in &self.imports {
1107-
DescribeImport(&import.kind).to_tokens(tokens);
1108-
}
1109-
let vis = &self.vis;
1110-
let name = &self.name;
1111-
let mut errors = Vec::new();
1112-
let mut body = TokenStream::new();
1113-
for import in &self.imports {
1114-
if let Err(e) = import.kind.try_to_tokens(&mut body) {
1115-
errors.push(e);
1116-
}
1117-
}
1118-
Diagnostic::from_vec(errors)?;
1119-
(quote!{
1120-
#vis mod #name {
1121-
#body
1122-
}
1123-
}).to_tokens(tokens);
1124-
Ok(())
1125-
}
1126-
}
1127-
11281099
impl ToTokens for ast::Dictionary {
11291100
fn to_tokens(&self, tokens: &mut TokenStream) {
11301101
let name = &self.name;

crates/webidl/src/lib.rs

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ use std::env;
3434
use std::fs;
3535
use std::iter::FromIterator;
3636

37-
use backend::ast;
3837
use backend::TryToTokens;
39-
use backend::defined::{ImportedTypeDefinitions, RemoveUndefinedImports};
38+
use backend::ast;
4039
use backend::defined::ImportedTypeReferences;
40+
use backend::defined::{ImportedTypeDefinitions, RemoveUndefinedImports};
4141
use backend::util::{ident_ty, rust_ident, raw_ident, wrap_import_function};
4242
use proc_macro2::{Ident, Span};
43+
use quote::ToTokens;
4344
use weedle::attribute::{ExtendedAttributeList};
4445
use weedle::dictionary::DictionaryMember;
4546
use weedle::interface::InterfaceMember;
@@ -51,9 +52,14 @@ use idl_type::ToIdlType;
5152

5253
pub use error::{Error, ErrorKind, Result};
5354

55+
struct Program {
56+
main: backend::ast::Program,
57+
submodules: Vec<(String, backend::ast::Program)>,
58+
}
59+
5460
/// Parse a string of WebIDL source text into a wasm-bindgen AST.
5561
fn parse(webidl_source: &str, allowed_types: Option<&[&str]>)
56-
-> Result<backend::ast::Program>
62+
-> Result<Program>
5763
{
5864
let definitions = match weedle::parse(webidl_source) {
5965
Ok(def) => def,
@@ -80,6 +86,7 @@ fn parse(webidl_source: &str, allowed_types: Option<&[&str]>)
8086
first_pass_record.builtin_idents = builtin_idents();
8187
definitions.first_pass(&mut first_pass_record, ())?;
8288
let mut program = Default::default();
89+
let mut submodules = Vec::new();
8390

8491
let allowed_types = allowed_types.map(|list| {
8592
list.iter().cloned().collect::<HashSet<_>>()
@@ -102,7 +109,8 @@ fn parse(webidl_source: &str, allowed_types: Option<&[&str]>)
102109
}
103110
}
104111
for (name, n) in first_pass_record.namespaces.iter() {
105-
first_pass_record.append_ns(&mut program, name, n);
112+
let prog = first_pass_record.append_ns(name, n);
113+
submodules.push((snake_case_ident(name).to_string(), prog));
106114
}
107115
for (name, d) in first_pass_record.interfaces.iter() {
108116
if filter(name) {
@@ -127,7 +135,10 @@ fn parse(webidl_source: &str, allowed_types: Option<&[&str]>)
127135
}
128136
}
129137

130-
Ok(program)
138+
Ok(Program {
139+
main: program,
140+
submodules: submodules,
141+
})
131142
}
132143

133144
/// Compile the given WebIDL source text into Rust source text containing
@@ -152,7 +163,7 @@ fn builtin_idents() -> BTreeSet<Ident> {
152163
}
153164

154165
/// Run codegen on the AST to generate rust code.
155-
fn compile_ast(mut ast: backend::ast::Program) -> String {
166+
fn compile_ast(mut ast: Program) -> String {
156167
// Iteratively prune all entries from the AST which reference undefined
157168
// fields. Each pass may remove definitions of types and so we need to
158169
// reexecute this pass to see if we need to keep removing types until we
@@ -162,13 +173,24 @@ fn compile_ast(mut ast: backend::ast::Program) -> String {
162173
let track = env::var_os("__WASM_BINDGEN_DUMP_FEATURES");
163174
loop {
164175
let mut defined = builtin.clone();
165-
ast.imported_type_definitions(&mut |id| {
166-
defined.insert(id.clone());
167-
if track.is_some() {
168-
all_definitions.insert(id.clone());
176+
{
177+
let mut cb = |id: &Ident| {
178+
defined.insert(id.clone());
179+
if track.is_some() {
180+
all_definitions.insert(id.clone());
181+
}
182+
};
183+
ast.main.imported_type_definitions(&mut cb);
184+
for (_, m) in ast.submodules.iter() {
185+
m.imported_type_references(&mut cb);
169186
}
170-
});
171-
if !ast.remove_undefined_imports(&|id| defined.contains(id)) {
187+
}
188+
let changed =
189+
ast.main.remove_undefined_imports(&|id| defined.contains(id)) ||
190+
ast.submodules.iter_mut().any(|(_, m)| {
191+
m.remove_undefined_imports(&|id| defined.contains(id))
192+
});
193+
if !changed {
172194
break
173195
}
174196
}
@@ -181,9 +203,21 @@ fn compile_ast(mut ast: backend::ast::Program) -> String {
181203
}
182204

183205
let mut tokens = proc_macro2::TokenStream::new();
184-
if let Err(e) = ast.try_to_tokens(&mut tokens) {
206+
if let Err(e) = ast.main.try_to_tokens(&mut tokens) {
185207
e.panic();
186208
}
209+
for (name, m) in ast.submodules.iter() {
210+
let mut m_tokens = proc_macro2::TokenStream::new();
211+
if let Err(e) = m.try_to_tokens(&mut m_tokens) {
212+
e.panic();
213+
}
214+
215+
let name = Ident::new(name, Span::call_site());
216+
217+
(quote! {
218+
pub mod #name { #m_tokens }
219+
}).to_tokens(&mut tokens);
220+
}
187221
tokens.to_string()
188222
}
189223

@@ -340,26 +374,21 @@ impl<'src> FirstPassRecord<'src> {
340374

341375
fn append_ns(
342376
&'src self,
343-
program: &mut backend::ast::Program,
344377
name: &'src str,
345378
ns: &'src first_pass::NamespaceData<'src>,
346-
) {
347-
let mut module = backend::ast::Module {
348-
vis: public(),
349-
name: rust_ident(snake_case_ident(name).as_str()),
350-
imports: Default::default(),
351-
};
379+
) -> backend::ast::Program {
380+
let mut ret = Default::default();
352381

353382
for (id, data) in ns.operations.iter() {
354-
self.append_ns_member(&mut module, name, id, data);
383+
self.append_ns_member(&mut ret, name, id, data);
355384
}
356385

357-
program.modules.push(module);
386+
return ret
358387
}
359388

360389
fn append_ns_member(
361390
&self,
362-
module: &mut backend::ast::Module,
391+
module: &mut backend::ast::Program,
363392
self_name: &'src str,
364393
id: &OperationId<'src>,
365394
data: &OperationData<'src>,

0 commit comments

Comments
 (0)