Skip to content

Commit d4e4edb

Browse files
committed
ir: Fix "this" argument generation to build a pointer to const, not a const pointer.
1 parent 74dcb20 commit d4e4edb

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

src/ir/context.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1925,9 +1925,44 @@ impl BindgenContext {
19251925
wrapped_id: TypeId,
19261926
parent_id: Option<ItemId>,
19271927
ty: &clang::Type,
1928+
) -> TypeId {
1929+
self.build_wrapper(
1930+
with_id,
1931+
wrapped_id,
1932+
parent_id,
1933+
ty,
1934+
ty.is_const(),
1935+
)
1936+
}
1937+
1938+
/// A wrapper over a type that adds a const qualifier explicitly.
1939+
///
1940+
/// Needed to handle const methods in C++, wrapping the type .
1941+
pub fn build_const_wrapper(
1942+
&mut self,
1943+
with_id: ItemId,
1944+
wrapped_id: TypeId,
1945+
parent_id: Option<ItemId>,
1946+
ty: &clang::Type,
1947+
) -> TypeId {
1948+
self.build_wrapper(
1949+
with_id,
1950+
wrapped_id,
1951+
parent_id,
1952+
ty,
1953+
/* is_const = */ true,
1954+
)
1955+
}
1956+
1957+
fn build_wrapper(
1958+
&mut self,
1959+
with_id: ItemId,
1960+
wrapped_id: TypeId,
1961+
parent_id: Option<ItemId>,
1962+
ty: &clang::Type,
1963+
is_const: bool,
19281964
) -> TypeId {
19291965
let spelling = ty.spelling();
1930-
let is_const = ty.is_const();
19311966
let layout = ty.fallible_layout().ok();
19321967
let type_kind = TypeKind::ResolvedTypeRef(wrapped_id);
19331968
let ty = Type::new(Some(spelling), layout, type_kind, is_const);

src/ir/function.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,14 +402,27 @@ impl FunctionSig {
402402
let is_virtual = is_method && cursor.method_is_virtual();
403403
let is_static = is_method && cursor.method_is_static();
404404
if !is_static && !is_virtual {
405-
let class = Item::parse(cursor.semantic_parent(), None, ctx)
405+
let parent = cursor.semantic_parent();
406+
let class = Item::parse(parent, None, ctx)
406407
.expect("Expected to parse the class");
407408
// The `class` most likely is not finished parsing yet, so use
408409
// the unchecked variant.
409410
let class = class.as_type_id_unchecked();
410411

412+
let class = if is_const {
413+
let const_class_id = ctx.next_item_id();
414+
ctx.build_const_wrapper(
415+
const_class_id,
416+
class,
417+
None,
418+
&parent.cur_type(),
419+
)
420+
} else {
421+
class
422+
};
423+
411424
let ptr =
412-
Item::builtin_type(TypeKind::Pointer(class), is_const, ctx);
425+
Item::builtin_type(TypeKind::Pointer(class), false, ctx);
413426
args.insert(0, (Some("this".into()), ptr));
414427
} else if is_virtual {
415428
let void = Item::builtin_type(TypeKind::Void, false, ctx);

0 commit comments

Comments
 (0)