|
1 | 1 | use crate::c_ast::c_decl::{CDecl, CDeclId, CDeclKind}; |
2 | | -use crate::c_ast::c_expr::{CBinOp, CExpr, CExprId, CExprKind, CUnTypeOp}; |
| 2 | +use crate::c_ast::c_expr::{CExpr, CExprId, CExprKind}; |
3 | 3 | use crate::c_ast::c_stmt::{CLabelId, CStmt, CStmtId}; |
4 | | -use crate::c_ast::c_type::{CQualTypeId, CType, CTypeId, CTypeKind}; |
5 | | -use crate::c_ast::iterators::{immediate_children_all_types, DFNodes, NodeVisitor, SomeId}; |
| 4 | +use crate::c_ast::c_type::{CType, CTypeId, CTypeKind}; |
| 5 | +use crate::c_ast::iterators::{DFNodes, SomeId}; |
6 | 6 | use indexmap::IndexMap; |
7 | 7 | use itertools::Itertools; |
8 | 8 | use std::cell::RefCell; |
@@ -435,111 +435,6 @@ impl TypedAstContext { |
435 | 435 | self.c_decls_top.retain(|x| wanted.contains(x)); |
436 | 436 | } |
437 | 437 |
|
438 | | - /// Bubble types of unary and binary operators up from their args into the expression type. |
439 | | - /// |
440 | | - /// In Clang 15 and below, the Clang AST resolves typedefs in the expression type of unary and |
441 | | - /// binary expressions. For example, a BinaryExpr node adding two `size_t` expressions will be |
442 | | - /// given an `unsigned long` type rather than the `size_t` typedef type. This behavior changed |
443 | | - /// in Clang 16. This method adjusts AST node types to match those produced by Clang 16 and |
444 | | - /// newer; on these later Clang versions, it should have no effect. |
445 | | - /// |
446 | | - /// This pass is necessary because we reify some typedef types (such as `size_t`) into their own |
447 | | - /// distinct Rust types. As such, we need to make sure we know the exact type to generate when |
448 | | - /// we translate an expr, not just its resolved type (looking through typedefs). |
449 | | - pub fn bubble_expr_types(&mut self) { |
450 | | - struct BubbleExprTypes<'a> { |
451 | | - ast_context: &'a mut TypedAstContext, |
452 | | - } |
453 | | - |
454 | | - impl<'a> NodeVisitor for BubbleExprTypes<'a> { |
455 | | - fn children(&mut self, id: SomeId) -> Vec<SomeId> { |
456 | | - immediate_children_all_types(self.ast_context, id) |
457 | | - } |
458 | | - |
459 | | - fn post(&mut self, id: SomeId) { |
460 | | - let e = match id { |
461 | | - SomeId::Expr(e) => e, |
462 | | - _ => return, |
463 | | - }; |
464 | | - |
465 | | - let new_ty = match self.ast_context.c_exprs[&e].kind { |
466 | | - CExprKind::Conditional(_ty, _cond, lhs, rhs) => { |
467 | | - let lhs_type_id = |
468 | | - self.ast_context.c_exprs[&lhs].kind.get_qual_type().unwrap(); |
469 | | - let rhs_type_id = |
470 | | - self.ast_context.c_exprs[&rhs].kind.get_qual_type().unwrap(); |
471 | | - |
472 | | - let lhs_resolved_ty = self.ast_context.resolve_type(lhs_type_id.ctype); |
473 | | - let rhs_resolved_ty = self.ast_context.resolve_type(rhs_type_id.ctype); |
474 | | - |
475 | | - if CTypeKind::PULLBACK_KINDS.contains(&lhs_resolved_ty.kind) { |
476 | | - Some(lhs_type_id) |
477 | | - } else if CTypeKind::PULLBACK_KINDS.contains(&rhs_resolved_ty.kind) { |
478 | | - Some(rhs_type_id) |
479 | | - } else { |
480 | | - None |
481 | | - } |
482 | | - } |
483 | | - CExprKind::Binary(_ty, op, lhs, rhs, _, _) => { |
484 | | - let rhs_type_id = |
485 | | - self.ast_context.c_exprs[&rhs].kind.get_qual_type().unwrap(); |
486 | | - let lhs_kind = &self.ast_context.c_exprs[&lhs].kind; |
487 | | - let lhs_type_id = lhs_kind.get_qual_type().unwrap(); |
488 | | - |
489 | | - let lhs_resolved_ty = self.ast_context.resolve_type(lhs_type_id.ctype); |
490 | | - let rhs_resolved_ty = self.ast_context.resolve_type(rhs_type_id.ctype); |
491 | | - |
492 | | - let neither_ptr = !lhs_resolved_ty.kind.is_pointer() |
493 | | - && !rhs_resolved_ty.kind.is_pointer(); |
494 | | - |
495 | | - if op.all_types_same() && neither_ptr { |
496 | | - if CTypeKind::PULLBACK_KINDS.contains(&lhs_resolved_ty.kind) { |
497 | | - Some(lhs_type_id) |
498 | | - } else { |
499 | | - Some(rhs_type_id) |
500 | | - } |
501 | | - } else if op == CBinOp::ShiftLeft || op == CBinOp::ShiftRight { |
502 | | - Some(lhs_type_id) |
503 | | - } else { |
504 | | - return; |
505 | | - } |
506 | | - } |
507 | | - CExprKind::Unary(_ty, op, e, _idk) => op.expected_result_type( |
508 | | - self.ast_context, |
509 | | - self.ast_context.c_exprs[&e].kind.get_qual_type().unwrap(), |
510 | | - ), |
511 | | - CExprKind::Paren(_ty, e) => self.ast_context.c_exprs[&e].kind.get_qual_type(), |
512 | | - CExprKind::UnaryType(_, op, _, _) => { |
513 | | - // All of these `CUnTypeOp`s should return `size_t`. |
514 | | - let kind = match op { |
515 | | - CUnTypeOp::SizeOf => CTypeKind::Size, |
516 | | - CUnTypeOp::AlignOf => CTypeKind::Size, |
517 | | - CUnTypeOp::PreferredAlignOf => CTypeKind::Size, |
518 | | - }; |
519 | | - let ty = self |
520 | | - .ast_context |
521 | | - .type_for_kind(&kind) |
522 | | - .expect("CTypeKind::Size should be size_t"); |
523 | | - Some(CQualTypeId::new(ty)) |
524 | | - } |
525 | | - _ => return, |
526 | | - }; |
527 | | - let ty = self |
528 | | - .ast_context |
529 | | - .c_exprs |
530 | | - .get_mut(&e) |
531 | | - .and_then(|e| e.kind.get_qual_type_mut()); |
532 | | - if let (Some(ty), Some(new_ty)) = (ty, new_ty) { |
533 | | - *ty = new_ty; |
534 | | - }; |
535 | | - } |
536 | | - } |
537 | | - |
538 | | - for decl in self.c_decls_top.clone() { |
539 | | - BubbleExprTypes { ast_context: self }.visit_tree(SomeId::Decl(decl)); |
540 | | - } |
541 | | - } |
542 | | - |
543 | 438 | /// Sort the top-level declarations by file and source location |
544 | 439 | /// so that we preserve the ordering of all declarations in each file. |
545 | 440 | /// This preserves the order when we emit the converted declarations. |
|
0 commit comments