Skip to content

Commit

Permalink
Apply const parameter metadata (#2384)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Mar 14, 2023
1 parent 1b99e9b commit 649ace3
Show file tree
Hide file tree
Showing 70 changed files with 697 additions and 616 deletions.
1 change: 1 addition & 0 deletions .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ jobs:
cargo clippy -p test_component &&
cargo clippy -p test_component_client &&
cargo clippy -p test_const_fields &&
cargo clippy -p test_const_params &&
cargo clippy -p test_const_ptrs &&
cargo clippy -p test_core &&
cargo clippy -p test_debug &&
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ jobs:
cargo test --target ${{ matrix.target }} -p test_component &&
cargo test --target ${{ matrix.target }} -p test_component_client &&
cargo test --target ${{ matrix.target }} -p test_const_fields &&
cargo test --target ${{ matrix.target }} -p test_const_params &&
cargo test --target ${{ matrix.target }} -p test_const_ptrs &&
cargo test --target ${{ matrix.target }} -p test_core &&
cargo test --target ${{ matrix.target }} -p test_debug &&
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;

pub fn gen(gen: &Gen, def: Field) -> TokenStream {
let name = to_ident(gen.reader.field_name(def));
let ty = gen.reader.field_type(def, None).to_const();
let ty = gen.reader.field_type(def, None).to_const_type();
let cfg = gen.reader.field_cfg(def);
let doc = gen.cfg_doc(&cfg);
let features = gen.cfg_features(&cfg);
Expand Down
21 changes: 17 additions & 4 deletions crates/libs/metadata/src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ impl<'a> Reader<'a> {
let def = self.type_from_blob(&mut blob, enclosing, &[]).expect("Type not found");

if self.field_is_const(row) {
def.to_const()
def.to_const_type().to_const_ptr()
} else {
def
}
Expand Down Expand Up @@ -563,15 +563,25 @@ impl<'a> Reader<'a> {
blob.read_usize();
blob.read_usize();

let return_type = self.type_from_blob(&mut blob, None, generics);
let mut return_type = self.type_from_blob(&mut blob, None, generics);

let mut params: Vec<SignatureParam> = self
.method_def_params(row)
.filter_map(|param| {
if self.param_sequence(param) == 0 {
if self.param_is_const(param) {
return_type = return_type.clone().map(|ty| ty.to_const_type());
}
None
} else {
let ty = self.type_from_blob(&mut blob, None, generics).expect("Parameter type not found");
let ty = if !self.param_flags(param).contains(ParamAttributes::OUTPUT) { ty.to_const() } else { ty };
let is_output = self.param_flags(param).contains(ParamAttributes::OUTPUT);
let mut ty = self.type_from_blob(&mut blob, None, generics).expect("Parameter type not found");
if self.param_is_const(param) || !is_output {
ty = ty.to_const_type();
}
if !is_output {
ty = ty.to_const_ptr();
}
let kind = self.param_kind(param);
Some(SignatureParam { def: param, ty, kind })
}
Expand Down Expand Up @@ -788,6 +798,9 @@ impl<'a> Reader<'a> {
}
None
}
pub fn param_is_const(&self, row: Param) -> bool {
self.param_attributes(row).any(|attribute| self.attribute_name(attribute) == "ConstAttribute")
}

//
// TypeDef table queries
Expand Down
17 changes: 12 additions & 5 deletions crates/libs/metadata/src/reader/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,25 @@ impl Type {
}
}

/// Converts the `Type` to an equivalent `const` variant if appropriate, typically used when the
/// mutability is informed by something outside of the type signature.
pub fn to_const(self) -> Self {
/// Converts the `Type` to an equivalent `const` variant if appropriate.
pub fn to_const_type(self) -> Self {
match self {
Self::MutPtr(p) => Self::ConstPtr(p).to_const(),
Self::ConstPtr((kind, pointers)) => Self::ConstPtr((Box::new(kind.to_const()), pointers)),
Self::MutPtr((kind, pointers)) => Self::MutPtr((Box::new(kind.to_const_type()), pointers)),
Self::ConstPtr((kind, pointers)) => Self::ConstPtr((Box::new(kind.to_const_type()), pointers)),
Self::PSTR => Self::PCSTR,
Self::PWSTR => Self::PCWSTR,
_ => self,
}
}

/// Converts a mutable pointer type, if appropriate, to a const pointer type.
pub fn to_const_ptr(self) -> Self {
match self {
Self::MutPtr((kind, pointers)) => Self::ConstPtr((kind, pointers)),
_ => self,
}
}

/// Removes one level of indirection, typically used when transforming a logical return or array parameter
/// from its underlying type signature.
pub fn deref(&self) -> Self {
Expand Down
Loading

0 comments on commit 649ace3

Please sign in to comment.