Skip to content

Commit 84685a9

Browse files
committed
transpile: Add AST node parent information
1 parent c367804 commit 84685a9

4 files changed

Lines changed: 405 additions & 23 deletions

File tree

c2rust-transpile/src/c_ast/conversion.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -388,17 +388,23 @@ impl ConversionContext {
388388

389389
/// Add a `CStmt` node into the `TypedAstContext`
390390
fn add_stmt(&mut self, id: ImporterId, stmt: CStmt) {
391-
self.typed_context.c_stmts.insert(CStmtId(id), stmt);
391+
let id = CStmtId(id);
392+
self.typed_context.add_stmt_parents(id, &stmt.kind);
393+
self.typed_context.c_stmts.insert(id, stmt);
392394
}
393395

394396
/// Add a `CExpr` node into the `TypedAstContext`
395397
fn add_expr(&mut self, id: ImporterId, expr: CExpr) {
396-
self.typed_context.c_exprs.insert(CExprId(id), expr);
398+
let id = CExprId(id);
399+
self.typed_context.add_expr_parents(id, &expr.kind);
400+
self.typed_context.c_exprs.insert(id, expr);
397401
}
398402

399403
/// Add a `CDecl` node into the `TypedAstContext`
400404
fn add_decl(&mut self, id: ImporterId, decl: CDecl) {
401-
self.typed_context.c_decls.insert(CDeclId(id), decl);
405+
let id = CDeclId(id);
406+
self.typed_context.add_decl_parents(id, &decl.kind);
407+
self.typed_context.c_decls.insert(id, decl);
402408
}
403409

404410
/// Clang has `Expression <: Statement`, but we want to make that explicit via the
@@ -561,7 +567,6 @@ impl ConversionContext {
561567
&'a mut self,
562568
untyped_context: &'a AstContext,
563569
node: &'a AstNode,
564-
new_id: ImporterId,
565570
) -> impl Iterator<Item = CDeclId> + 'a {
566571
use self::node_types::*;
567572

@@ -573,7 +578,6 @@ impl ConversionContext {
573578
.expect("child node not found");
574579

575580
let id = CDeclId(self.visit_node_type(decl, FIELD_DECL | ENUM_DECL | RECORD_DECL));
576-
self.typed_context.parents.insert(id, CDeclId(new_id));
577581

578582
if decl_node.tag == ASTEntryTag::TagFieldDecl {
579583
Some(id)
@@ -2164,9 +2168,7 @@ impl ConversionContext {
21642168
.iter()
21652169
.map(|id| {
21662170
let con = id.expect("Enum constant not found");
2167-
let id = CDeclId(self.visit_node_type(con, ENUM_CON));
2168-
self.typed_context.parents.insert(id, CDeclId(new_id));
2169-
id
2171+
CDeclId(self.visit_node_type(con, ENUM_CON))
21702172
})
21712173
.collect();
21722174

@@ -2269,10 +2271,7 @@ impl ConversionContext {
22692271
from_value(node.extras[6].clone()).expect("Expected struct alignment");
22702272

22712273
let fields: Option<Vec<CDeclId>> = if has_def {
2272-
Some(
2273-
self.visit_record_children(untyped_context, node, new_id)
2274-
.collect(),
2275-
)
2274+
Some(self.visit_record_children(untyped_context, node).collect())
22762275
} else {
22772276
None
22782277
};
@@ -2300,10 +2299,7 @@ impl ConversionContext {
23002299
let attrs = from_value::<Vec<Value>>(node.extras[2].clone())
23012300
.expect("Expected attribute array on record");
23022301
let fields: Option<Vec<CDeclId>> = if has_def {
2303-
Some(
2304-
self.visit_record_children(untyped_context, node, new_id)
2305-
.collect(),
2306-
)
2302+
Some(self.visit_record_children(untyped_context, node).collect())
23072303
} else {
23082304
None
23092305
};

0 commit comments

Comments
 (0)