-
Notifications
You must be signed in to change notification settings - Fork 746
Avoid generating fields/methods for opaque objects #936
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
Merged
Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -1551,57 +1551,61 @@ impl CodeGenerator for CompInfo { | |
// | ||
// Also, we need to generate the vtable in such a way it "inherits" from | ||
// the parent too. | ||
let is_opaque = item.is_opaque(ctx, &()); | ||
let mut fields = vec![]; | ||
let mut struct_layout = | ||
StructLayoutTracker::new(ctx, self, &canonical_name); | ||
if self.needs_explicit_vtable(ctx, item) { | ||
let vtable = | ||
Vtable::new(item.id(), self.methods(), self.base_members()); | ||
vtable.codegen(ctx, result, item); | ||
|
||
let vtable_type = vtable | ||
.try_to_rust_ty(ctx, &()) | ||
.expect("vtable to Rust type conversion is infallible") | ||
.to_ptr(true, ctx.span()); | ||
if !is_opaque { | ||
if self.needs_explicit_vtable(ctx, item) { | ||
let vtable = | ||
Vtable::new(item.id(), self.methods(), self.base_members()); | ||
vtable.codegen(ctx, result, item); | ||
|
||
let vtable_field = StructFieldBuilder::named("vtable_") | ||
.pub_() | ||
.build_ty(vtable_type); | ||
let vtable_type = vtable | ||
.try_to_rust_ty(ctx, &()) | ||
.expect("vtable to Rust type conversion is infallible") | ||
.to_ptr(true, ctx.span()); | ||
|
||
struct_layout.saw_vtable(); | ||
let vtable_field = StructFieldBuilder::named("vtable_") | ||
.pub_() | ||
.build_ty(vtable_type); | ||
|
||
fields.push(vtable_field); | ||
} | ||
struct_layout.saw_vtable(); | ||
|
||
for (i, base) in self.base_members().iter().enumerate() { | ||
// Virtual bases are already taken into account by the vtable | ||
// pointer. | ||
// | ||
// FIXME(emilio): Is this always right? | ||
if base.is_virtual() { | ||
continue; | ||
fields.push(vtable_field); | ||
} | ||
|
||
let base_ty = ctx.resolve_type(base.ty); | ||
// NB: We won't include unsized types in our base chain because they | ||
// would contribute to our size given the dummy field we insert for | ||
// unsized types. | ||
if base_ty.is_unsized(ctx, &base.ty) { | ||
continue; | ||
} | ||
for (i, base) in self.base_members().iter().enumerate() { | ||
// Virtual bases are already taken into account by the vtable | ||
// pointer. | ||
// | ||
// FIXME(emilio): Is this always right? | ||
if base.is_virtual() { | ||
continue; | ||
} | ||
|
||
let inner = base.ty.to_rust_ty_or_opaque(ctx, &()); | ||
let field_name = if i == 0 { | ||
"_base".into() | ||
} else { | ||
format!("_base_{}", i) | ||
}; | ||
let base_ty = ctx.resolve_type(base.ty); | ||
// NB: We won't include unsized types in our base chain because they | ||
// would contribute to our size given the dummy field we insert for | ||
// unsized types. | ||
if base_ty.is_unsized(ctx, &base.ty) { | ||
continue; | ||
} | ||
|
||
struct_layout.saw_base(base_ty); | ||
let inner = base.ty.to_rust_ty_or_opaque(ctx, &()); | ||
let field_name = if i == 0 { | ||
"_base".into() | ||
} else { | ||
format!("_base_{}", i) | ||
}; | ||
|
||
struct_layout.saw_base(base_ty); | ||
|
||
let field = | ||
StructFieldBuilder::named(field_name).pub_().build_ty(inner); | ||
fields.extend(Some(field)); | ||
let field = | ||
StructFieldBuilder::named(field_name).pub_().build_ty(inner); | ||
fields.extend(Some(field)); | ||
} | ||
} | ||
if is_union { | ||
result.saw_union(); | ||
|
@@ -1610,34 +1614,34 @@ impl CodeGenerator for CompInfo { | |
} | ||
} | ||
|
||
let layout = item.kind().expect_type().layout(ctx); | ||
|
||
let fields_should_be_private = | ||
item.annotations().private_fields().unwrap_or(false); | ||
let struct_accessor_kind = item.annotations() | ||
.accessor_kind() | ||
.unwrap_or(FieldAccessorKind::None); | ||
|
||
let mut methods = vec![]; | ||
let mut anon_field_names = AnonFieldNames::default(); | ||
let codegen_depth = item.codegen_depth(ctx); | ||
for field in self.fields() { | ||
field.codegen( | ||
ctx, | ||
fields_should_be_private, | ||
codegen_depth, | ||
struct_accessor_kind, | ||
self, | ||
&mut anon_field_names, | ||
result, | ||
&mut struct_layout, | ||
&mut fields, | ||
&mut methods, | ||
(), | ||
); | ||
if !is_opaque { | ||
let mut anon_field_names = AnonFieldNames::default(); | ||
let codegen_depth = item.codegen_depth(ctx); | ||
let fields_should_be_private = | ||
item.annotations().private_fields().unwrap_or(false); | ||
let struct_accessor_kind = item.annotations() | ||
.accessor_kind() | ||
.unwrap_or(FieldAccessorKind::None); | ||
for field in self.fields() { | ||
field.codegen( | ||
ctx, | ||
fields_should_be_private, | ||
codegen_depth, | ||
struct_accessor_kind, | ||
self, | ||
&mut anon_field_names, | ||
result, | ||
&mut struct_layout, | ||
&mut fields, | ||
&mut methods, | ||
(), | ||
); | ||
} | ||
} | ||
|
||
if is_union { | ||
let layout = item.kind().expect_type().layout(ctx); | ||
if is_union && !is_opaque { | ||
let layout = layout.expect("Unable to get layout information?"); | ||
let ty = BlobTyBuilder::new(layout).build(); | ||
|
||
|
@@ -1654,11 +1658,12 @@ impl CodeGenerator for CompInfo { | |
fields.push(field); | ||
} | ||
|
||
// Yeah, sorry about that. | ||
let is_opaque = item.is_opaque(ctx, &()); | ||
if is_opaque { | ||
fields.clear(); | ||
methods.clear(); | ||
// Opaque item should not have generated methods, fields | ||
debug_assert!(fields.is_empty()); | ||
debug_assert!(methods.is_empty()); | ||
// fields.clear(); | ||
// methods.clear(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: these commented out lines should be removed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
|
||
match layout { | ||
Some(l) => { | ||
|
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.
Nitpick: missing a period at the end of this sentence.