New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Follow multiple transparent links in Option layout #699
Open
dcreager
wants to merge
2
commits into
mozilla:master
Choose a base branch
from
dcreager:option-transparent
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
use std::collections::HashMap; | ||
|
||
use crate::bindgen::ir::{GenericParams, ItemMap, Path, Struct, Type, Typedef}; | ||
|
||
/// Keeps track of named types that have the same underlying representation as some other type. | ||
/// This happens via `#[repr(transparent)]` structs and via typedefs. | ||
#[derive(Default)] | ||
pub struct TransparentTypes { | ||
transparent: HashMap<Path, (Type, GenericParams)>, | ||
} | ||
|
||
impl TransparentTypes { | ||
pub fn add_structs(&mut self, structs: &ItemMap<Struct>) { | ||
structs.for_all_items(|s| { | ||
if s.is_transparent { | ||
self.transparent.insert( | ||
s.path.clone(), | ||
(s.fields[0].ty.clone(), s.generic_params.clone()), | ||
); | ||
} | ||
}); | ||
} | ||
|
||
pub fn add_typedefs(&mut self, structs: &ItemMap<Typedef>) { | ||
structs.for_all_items(|t| { | ||
self.transparent.insert( | ||
t.path.clone(), | ||
(t.aliased.clone(), t.generic_params.clone()), | ||
); | ||
}); | ||
} | ||
|
||
pub fn is_transparent(&self, ty: &Type) -> Option<Type> { | ||
let generic_path = match ty { | ||
Type::Path(p) => p, | ||
_ => return None, | ||
}; | ||
let (resolved, generic_params) = self.transparent.get(generic_path.path())?; | ||
let mappings = generic_params | ||
.iter() | ||
.zip(generic_path.generics()) | ||
.collect::<Vec<_>>(); | ||
Some(resolved.specialize(&mappings)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,6 @@ cdef extern from *: | |
cdef extern from *: | ||
|
||
ctypedef struct Bar: | ||
Option_Foo foo; | ||
uint64_t foo; | ||
|
||
void root(Bar f); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,6 @@ cdef extern from *: | |
cdef extern from *: | ||
|
||
cdef struct Bar: | ||
Option_Foo foo; | ||
uint64_t foo; | ||
|
||
void root(Bar f); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <stdarg.h> | ||
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <stdlib.h> | ||
|
||
typedef int32_t (*DoFn)(int32_t x, int32_t y); | ||
|
||
typedef struct StructWithOptionalFunctionPointer { | ||
DoFn func; | ||
int32_t (*maybe_func)(int32_t x, int32_t y); | ||
} StructWithOptionalFunctionPointer; | ||
|
||
typedef uint32_t *NonNullAlias_u32; | ||
|
||
typedef struct StructWithOptionalNonNullPointer { | ||
NonNullAlias_u32 data; | ||
uint32_t *maybe_data; | ||
} StructWithOptionalNonNullPointer; | ||
|
||
void root(struct StructWithOptionalFunctionPointer swofp, | ||
struct StructWithOptionalNonNullPointer swonnp); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So it is quite a bit unfortunate to have to go through all items again creating this data structure when we already have a
<Path, Struct>
etc hashmap inItemMap
, it just feels like a lot of upfront work for something that could be much more efficient.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I definitely agree. At first I tried passing
&ItemMap<Struct>
and&ItemMap<Typedef>
references in to thesimplify_standard_types
methods, but that runs afoul of the borrow checker, since we're calling them from within afor_all_items_mut
loop that is updating the contents of thoseItemMap
s. Do you have any suggestions for how to break that double-borrow?