Skip to content

Commit 240a611

Browse files
committed
transpile: Split off TypedAstContext::set_prenamed_decls
1 parent 3558805 commit 240a611

2 files changed

Lines changed: 57 additions & 50 deletions

File tree

c2rust-transpile/src/c_ast/mod.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,54 @@ impl TypedAstContext {
996996
}
997997
}
998998

999+
/// Identifies typedefs that name unnamed types.
1000+
/// Later, the two declarations can be collapsed into a single name and declaration,
1001+
/// eliminating the typedef altogether.
1002+
pub fn set_prenamed_decls(&mut self) {
1003+
let mut prenamed_decls: IndexMap<CDeclId, CDeclId> = IndexMap::new();
1004+
1005+
for (&decl_id, decl) in self.iter_decls() {
1006+
if let CDeclKind::Typedef { ref name, typ, .. } = decl.kind {
1007+
if let Some(subdecl_id) = self.resolve_type(typ.ctype).kind.as_underlying_decl() {
1008+
use CDeclKind::*;
1009+
let is_unnamed = match self[subdecl_id].kind {
1010+
Struct { name: None, .. }
1011+
| Union { name: None, .. }
1012+
| Enum { name: None, .. } => true,
1013+
1014+
// Detect case where typedef and struct share the same name.
1015+
// In this case the purpose of the typedef was simply to eliminate
1016+
// the need for the 'struct' tag when referring to the type name.
1017+
Struct {
1018+
name: Some(ref target_name),
1019+
..
1020+
}
1021+
| Union {
1022+
name: Some(ref target_name),
1023+
..
1024+
}
1025+
| Enum {
1026+
name: Some(ref target_name),
1027+
..
1028+
} => name == target_name,
1029+
1030+
_ => false,
1031+
};
1032+
1033+
if is_unnamed
1034+
&& !prenamed_decls
1035+
.values()
1036+
.any(|decl_id| *decl_id == subdecl_id)
1037+
{
1038+
prenamed_decls.insert(decl_id, subdecl_id);
1039+
}
1040+
}
1041+
}
1042+
}
1043+
1044+
self.prenamed_decls = prenamed_decls;
1045+
}
1046+
9991047
pub fn prune_unwanted_decls(&mut self, want_unused_functions: bool) {
10001048
// Starting from a set of root declarations, walk each one to find declarations it
10011049
// depends on. Then walk each of those, recursively.

c2rust-transpile/src/translator/mod.rs

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -739,60 +739,19 @@ pub fn translate(
739739

740740
// Identify typedefs that name unnamed types and collapse the two declarations
741741
// into a single name and declaration, eliminating the typedef altogether.
742-
let mut prenamed_decls: IndexMap<CDeclId, CDeclId> = IndexMap::new();
743-
for (&decl_id, decl) in t.ast_context.iter_decls() {
744-
if let CDeclKind::Typedef { ref name, typ, .. } = decl.kind {
745-
if let Some(subdecl_id) = t
746-
.ast_context
747-
.resolve_type(typ.ctype)
748-
.kind
749-
.as_underlying_decl()
750-
{
751-
use CDeclKind::*;
752-
let is_unnamed = match t.ast_context[subdecl_id].kind {
753-
Struct { name: None, .. }
754-
| Union { name: None, .. }
755-
| Enum { name: None, .. } => true,
756-
757-
// Detect case where typedef and struct share the same name.
758-
// In this case the purpose of the typedef was simply to eliminate
759-
// the need for the 'struct' tag when referring to the type name.
760-
Struct {
761-
name: Some(ref target_name),
762-
..
763-
}
764-
| Union {
765-
name: Some(ref target_name),
766-
..
767-
}
768-
| Enum {
769-
name: Some(ref target_name),
770-
..
771-
} => name == target_name,
772-
773-
_ => false,
774-
};
775-
776-
if is_unnamed
777-
&& !prenamed_decls
778-
.values()
779-
.any(|decl_id| *decl_id == subdecl_id)
780-
{
781-
prenamed_decls.insert(decl_id, subdecl_id);
742+
t.ast_context.set_prenamed_decls();
782743

783-
t.type_converter
784-
.borrow_mut()
785-
.declare_decl_name(decl_id, name);
786-
t.type_converter
787-
.borrow_mut()
788-
.alias_decl_name(subdecl_id, decl_id);
789-
}
790-
}
744+
for (&decl_id, &subdecl_id) in &t.ast_context.prenamed_decls {
745+
if let CDeclKind::Typedef { ref name, .. } = t.ast_context[decl_id].kind {
746+
t.type_converter
747+
.borrow_mut()
748+
.declare_decl_name(decl_id, name);
749+
t.type_converter
750+
.borrow_mut()
751+
.alias_decl_name(subdecl_id, decl_id);
791752
}
792753
}
793754

794-
t.ast_context.prenamed_decls = prenamed_decls;
795-
796755
// Helper function that returns true if there is either a matching typedef or its
797756
// corresponding struct/union/enum
798757
fn contains(prenamed_decls: &IndexMap<CDeclId, CDeclId>, decl_id: &CDeclId) -> bool {

0 commit comments

Comments
 (0)