Skip to content

Commit

Permalink
add placeholders for interface attributes
Browse files Browse the repository at this point in the history
the interface attributes are only recognized by the redIDL compiler but not the Rust compiler. The Rust compiler will spit out errors if there's no placeholder implementations for them
  • Loading branch information
tjhu committed Jun 10, 2021
1 parent 4a796f9 commit 6db5862
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 6 deletions.
4 changes: 2 additions & 2 deletions interface/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 20 additions & 3 deletions interface/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,33 @@ DOMAIN_CREATE_OUTPUT_PATH = ../kernel/src/generated_domain_create.rs
.PHONY: all
all: $(LIB_RS) $(MANIFEST_PATH)

# Copy Cargo.toml and fix dependency path
# Copy Cargo.toml, and fix dependency path, remove `interface_attribute_placeholder`, and add
# missing dependencies.
$(MANIFEST_PATH): Cargo.toml missing_dependencies.toml
mkdir -p $(OUTPUT_DIR)
sed -E 's/path\s*=\s*"/path = "..\//' $(word 1,$^) > $@
cat $(word 2,$^) >> $@

# Merge all files and inject macro for expansion
# Add placeholder dependencies and run cargo check.
# Then restore the Cargo.toml.
# TODO(issue #49): move the operation to a temp folder to prevent contaminating the source folder.
.PHONY: compiler_check
compiler_check:
cp Cargo.toml Cargo.toml.backup
cat Cargo.toml.backup placeholder_dependencies.toml > Cargo.toml
cargo check
mv Cargo.toml.backup Cargo.toml

# Merge all files and inject macro for expansion.
# Since the placeholder removes the interface attributes from the traits, we need to remove
# them from the file.
.PHONY: $(OUTPUT_DIR)/merged.rs
$(OUTPUT_DIR)/merged.rs:
$(OUTPUT_DIR)/merged.rs: compiler_check
mkdir -p $(OUTPUT_DIR)
cp src/lib.rs src/lib.rs.backup
tac src/lib.rs.backup | sed '/extern crate interface_attribute_placeholder;/I,+1 d' | tac > src/lib.rs
cargo expand > $@
mv src/lib.rs.backup src/lib.rs

# Inject use statements
$(LIB_RS): $(OUTPUT_DIR)/merged.rs
Expand All @@ -27,6 +43,7 @@ $(LIB_RS): $(OUTPUT_DIR)/merged.rs

.PHONY: clean
clean:
cargo clean
-rm -rf $(OUTPUT_DIR)
-rm -f $(DOMAIN_CREATE_OUTPUT_PATH)

Expand Down
2 changes: 2 additions & 0 deletions interface/placeholder_dependencies.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[dependencies.interface_attribute_placeholder]
path = "../lib/external/interface_attribute_placeholder"
2 changes: 2 additions & 0 deletions interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ extern crate alloc;
extern crate num_derive;
#[macro_use]
extern crate bitflags;
#[macro_use]
extern crate interface_attribute_placeholder;

pub mod bdev;
pub mod dom_c;
Expand Down
33 changes: 33 additions & 0 deletions lib/external/interface_attribute_placeholder/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions lib/external/interface_attribute_placeholder/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "interface_attribute_placeholder"
version = "0.1.0"
authors = ["Redleaf team <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
proc-macro = true

[dependencies]
quote = "1.0.9"
proc-macro2 = "1.0"
34 changes: 34 additions & 0 deletions lib/external/interface_attribute_placeholder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
///! Include multiple attributes that
use proc_macro::TokenStream;

macro_rules! generate_placeholder_attributes {
() => {};
($attr:ident) => {
#[doc = "Placeholder attribute; noop besides removing the attribute itself."]
#[proc_macro_attribute]
pub fn $attr(_attr: TokenStream, item: TokenStream) -> TokenStream {
item
}
};
($attr:ident, $($attrs:tt)*) => {
generate_placeholder_attributes!($attr);
generate_placeholder_attributes!($($attrs)*);
};
}

generate_placeholder_attributes! {
placeholder,
interface,
domain_create,
domain_create_blob,
}
// #[proc_macro_attribute]
// pub fn a(mut attr: TokenStream, item: TokenStream) -> TokenStream {
// let attr = proc_macro2::TokenStream::from(attr);
// let item = proc_macro2::TokenStream::from(item);
// let output = quote::quote! {
// #[$attr(#attr)]
// #item
// };
// output.into()
// }
2 changes: 1 addition & 1 deletion tools/redIDL

0 comments on commit 6db5862

Please sign in to comment.