| 1 |
//! Everything related to types in our intermediate representation. |
| 2 |
|
| 3 |
use super::comp::CompInfo; |
| 4 |
use super::context::{BindgenContext, ItemId, TypeId}; |
| 5 |
use super::dot::DotAttributes; |
| 6 |
use super::enum_ty::Enum; |
| 7 |
use super::function::FunctionSig; |
| 8 |
use super::int::IntKind; |
| 9 |
use super::item::{IsOpaque, Item}; |
| 10 |
use super::layout::{Layout, Opaque}; |
| 11 |
use super::objc::ObjCInterface; |
| 12 |
use super::template::{ |
| 13 |
AsTemplateParam, TemplateInstantiation, TemplateParameters, |
| 14 |
}; |
| 15 |
use super::traversal::{EdgeKind, Trace, Tracer}; |
| 16 |
use crate::clang::{self, Cursor}; |
| 17 |
use crate::parse::{ParseError, ParseResult}; |
| 18 |
use std::borrow::Cow; |
| 19 |
use std::io; |
| 20 |
|
| 21 |
/// The base representation of a type in bindgen. |
| 22 |
/// |
| 23 |
/// A type has an optional name, which if present cannot be empty, a `layout` |
| 24 |
/// (size, alignment and packedness) if known, a `Kind`, which determines which |
| 25 |
/// kind of type it is, and whether the type is const. |
| 26 |
#[derive(Debug)] |
| 27 |
pub(crate) struct Type { |
| 28 |
/// The name of the type, or None if it was an unnamed struct or union. |
| 29 |
name: Option<String>, |
| 30 |
/// The layout of the type, if known. |
| 31 |
layout: Option<Layout>, |
| 32 |
/// The inner kind of the type |
| 33 |
kind: TypeKind, |
| 34 |
/// Whether this type is const-qualified. |
| 35 |
is_const: bool, |
| 36 |
} |
| 37 |
|
| 38 |
/// The maximum number of items in an array for which Rust implements common |
| 39 |
/// traits, and so if we have a type containing an array with more than this |
| 40 |
/// many items, we won't be able to derive common traits on that type. |
| 41 |
/// |
| 42 |
pub(crate) const RUST_DERIVE_IN_ARRAY_LIMIT: usize = 32; |
| 43 |
|
| 44 |
impl Type { |
| 45 |
/// Get the underlying `CompInfo` for this type as a mutable reference, or |
| 46 |
/// `None` if this is some other kind of type. |
| 47 |
pub(crate) fn as_comp_mut(&mut self) -> Option<&mut CompInfo> { |
| 48 |
match self.kind { |
| 49 |
TypeKind::Comp(ref mut ci) => Some(ci), |
| 50 |
_ => None, |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/// Construct a new `Type`. |
| 55 |
pub(crate) fn new( |
| 56 |
name: Option<String>, |
| 57 |
layout: Option<Layout>, |
| 58 |
kind: TypeKind, |
| 59 |
is_const: bool, |
| 60 |
) -> Self { |
| 61 |
Type { |
| 62 |
name, |
| 63 |
layout, |
| 64 |
kind, |
| 65 |
is_const, |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/// Which kind of type is this? |
| 70 |
pub(crate) fn kind(&self) -> &TypeKind { |
| 71 |
&self.kind |
| 72 |
} |
| 73 |
|
| 74 |
/// Get a mutable reference to this type's kind. |
| 75 |
pub(crate) fn kind_mut(&mut self) -> &mut TypeKind { |
| 76 |
&mut self.kind |
| 77 |
} |
| 78 |
|
| 79 |
/// Get this type's name. |
| 80 |
pub(crate) fn name(&self) -> Option<&str> { |
| 81 |
self.name.as_deref() |
| 82 |
} |
| 83 |
|
| 84 |
/// Whether this is a block pointer type. |
| 85 |
pub(crate) fn is_block_pointer(&self) -> bool { |
| 86 |
matches!(self.kind, TypeKind::BlockPointer(..)) |
| 87 |
} |
| 88 |
|
| 89 |
/// Is this an integer type, including `bool` or `char`? |
| 90 |
pub(crate) fn is_int(&self) -> bool { |
| 91 |
matches!(self.kind, TypeKind::Int(_)) |
| 92 |
} |
| 93 |
|
| 94 |
/// Is this a compound type? |
| 95 |
pub(crate) fn is_comp(&self) -> bool { |
| 96 |
matches!(self.kind, TypeKind::Comp(..)) |
| 97 |
} |
| 98 |
|
| 99 |
/// Is this a union? |
| 100 |
pub(crate) fn is_union(&self) -> bool { |
| 101 |
match self.kind { |
| 102 |
TypeKind::Comp(ref comp) => comp.is_union(), |
| 103 |
_ => false, |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/// Is this type of kind `TypeKind::TypeParam`? |
| 108 |
pub(crate) fn is_type_param(&self) -> bool { |
| 109 |
matches!(self.kind, TypeKind::TypeParam) |
| 110 |
} |
| 111 |
|
| 112 |
/// Is this a template instantiation type? |
| 113 |
pub(crate) fn is_template_instantiation(&self) -> bool { |
| 114 |
matches!(self.kind, TypeKind::TemplateInstantiation(..)) |
| 115 |
} |
| 116 |
|
| 117 |
/// Is this a function type? |
| 118 |
pub(crate) fn is_function(&self) -> bool { |
| 119 |
matches!(self.kind, TypeKind::Function(..)) |
| 120 |
} |
| 121 |
|
| 122 |
/// Is this an enum type? |
| 123 |
pub(crate) fn is_enum(&self) -> bool { |
| 124 |
matches!(self.kind, TypeKind::Enum(..)) |
| 125 |
} |
| 126 |
|
| 127 |
/// Is this either a builtin or named type? |
| 128 |
pub(crate) fn is_builtin_or_type_param(&self) -> bool { |
| 129 |
matches!( |
| 130 |
self.kind, |
| 131 |
TypeKind::Void | |
| 132 |
TypeKind::NullPtr | |
| 133 |
TypeKind::Function(..) | |
| 134 |
TypeKind::Array(..) | |
| 135 |
TypeKind::Reference(..) | |
| 136 |
TypeKind::Pointer(..) | |
| 137 |
TypeKind::Int(..) | |
| 138 |
TypeKind::Float(..) | |
| 139 |
TypeKind::TypeParam |
| 140 |
) |
| 141 |
} |
| 142 |
|
| 143 |
/// Creates a new named type, with name `name`. |
| 144 |
pub(crate) fn named(name: String) -> Self { |
| 145 |
let name = if name.is_empty() { None } else { Some(name) }; |
| 146 |
Self::new(name, None, TypeKind::TypeParam, false) |
| 147 |
} |
| 148 |
|
| 149 |
/// Is this a floating point type? |
| 150 |
pub(crate) fn is_float(&self) -> bool { |
| 151 |
matches!(self.kind, TypeKind::Float(..)) |
| 152 |
} |
| 153 |
|
| 154 |
/// Is this a boolean type? |
| 155 |
pub(crate) fn is_bool(&self) -> bool { |
| 156 |
matches!(self.kind, TypeKind::Int(IntKind::Bool)) |
| 157 |
} |
| 158 |
|
| 159 |
/// Is this an integer type? |
| 160 |
pub(crate) fn is_integer(&self) -> bool { |
| 161 |
matches!(self.kind, TypeKind::Int(..)) |
| 162 |
} |
| 163 |
|
| 164 |
/// Cast this type to an integer kind, or `None` if it is not an integer |
| 165 |
/// type. |
| 166 |
pub(crate) fn as_integer(&self) -> Option<IntKind> { |
| 167 |
match self.kind { |
| 168 |
TypeKind::Int(int_kind) => Some(int_kind), |
| 169 |
_ => None, |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/// Is this a `const` qualified type? |
| 174 |
pub(crate) fn is_const(&self) -> bool { |
| 175 |
self.is_const |
| 176 |
} |
| 177 |
|
| 178 |
/// Is this an unresolved reference? |
| 179 |
pub(crate) fn is_unresolved_ref(&self) -> bool { |
| 180 |
matches!(self.kind, TypeKind::UnresolvedTypeRef(_, _, _)) |
| 181 |
} |
| 182 |
|
| 183 |
/// Is this a incomplete array type? |
| 184 |
pub(crate) fn is_incomplete_array( |
| 185 |
&self, |
| 186 |
ctx: &BindgenContext, |
| 187 |
) -> Option<ItemId> { |
| 188 |
match self.kind { |
| 189 |
TypeKind::Array(item, len) => { |
| 190 |
if len == 0 { |
| 191 |
Some(item.into()) |
| 192 |
} else { |
| 193 |
None |
| 194 |
} |
| 195 |
} |
| 196 |
TypeKind::ResolvedTypeRef(inner) => { |
| 197 |
ctx.resolve_type(inner).is_incomplete_array(ctx) |
| 198 |
} |
| 199 |
_ => None, |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
/// What is the layout of this type? |
| 204 |
pub(crate) fn layout(&self, ctx: &BindgenContext) -> Option<Layout> { |
| 205 |
self.layout.or_else(|| { |
| 206 |
match self.kind { |
| 207 |
TypeKind::Comp(ref ci) => ci.layout(ctx), |
| 208 |
TypeKind::Array(inner, length) if length == 0 => Some( |
| 209 |
Layout::new(0, ctx.resolve_type(inner).layout(ctx)?.align), |
| 210 |
), |
| 211 |
// FIXME(emilio): This is a hack for anonymous union templates. |
| 212 |
// Use the actual pointer size! |
| 213 |
TypeKind::Pointer(..) => Some(Layout::new( |
| 214 |
ctx.target_pointer_size(), |
| 215 |
ctx.target_pointer_size(), |
| 216 |
)), |
| 217 |
TypeKind::ResolvedTypeRef(inner) => { |
| 218 |
ctx.resolve_type(inner).layout(ctx) |
| 219 |
} |
| 220 |
_ => None, |
| 221 |
} |
| 222 |
}) |
| 223 |
} |
| 224 |
|
| 225 |
/// Whether this named type is an invalid C++ identifier. This is done to |
| 226 |
/// avoid generating invalid code with some cases we can't handle, see: |
| 227 |
/// |
| 228 |
/// tests/headers/381-decltype-alias.hpp |
| 229 |
pub(crate) fn is_invalid_type_param(&self) -> bool { |
| 230 |
match self.kind { |
| 231 |
TypeKind::TypeParam => { |
| 232 |
let name = self.name().expect("Unnamed named type?"); |
| 233 |
!clang::is_valid_identifier(name) |
| 234 |
} |
| 235 |
_ => false, |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/// Takes `name`, and returns a suitable identifier representation for it. |
| 240 |
fn sanitize_name(name: &str) -> Cow<str> { |
| 241 |
if clang::is_valid_identifier(name) { |
| 242 |
return Cow::Borrowed(name); |
| 243 |
} |
| 244 |
|
| 245 |
let name = name.replace(|c| c == ' ' || c == ':' || c == '.', "_"); |
| 246 |
Cow::Owned(name) |
| 247 |
} |
| 248 |
|
| 249 |
/// Get this type's santizied name. |
| 250 |
pub(crate) fn sanitized_name<'a>( |
| 251 |
&'a self, |
| 252 |
ctx: &BindgenContext, |
| 253 |
) -> Option<Cow<'a, str>> { |
| 254 |
let name_info = match *self.kind() { |
| 255 |
TypeKind::Pointer(inner) => Some((inner, Cow::Borrowed("ptr"))), |
| 256 |
TypeKind::Reference(inner) => Some((inner, Cow::Borrowed("ref"))), |
| 257 |
TypeKind::Array(inner, length) => { |
| 258 |
Some((inner, format!("array{}", length).into())) |
| 259 |
} |
| 260 |
_ => None, |
| 261 |
}; |
| 262 |
if let Some((inner, prefix)) = name_info { |
| 263 |
ctx.resolve_item(inner) |
| 264 |
.expect_type() |
| 265 |
.sanitized_name(ctx) |
| 266 |
.map(|name| format!("{}_{}", prefix, name).into()) |
| 267 |
} else { |
| 268 |
self.name().map(Self::sanitize_name) |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
/// See safe_canonical_type. |
| 273 |
pub(crate) fn canonical_type<'tr>( |
| 274 |
&'tr self, |
| 275 |
ctx: &'tr BindgenContext, |
| 276 |
) -> &'tr Type { |
| 277 |
self.safe_canonical_type(ctx) |
| 278 |
.expect("Should have been resolved after parsing!") |
| 279 |
} |
| 280 |
|
| 281 |
/// Returns the canonical type of this type, that is, the "inner type". |
| 282 |
/// |
| 283 |
/// For example, for a `typedef`, the canonical type would be the |
| 284 |
/// `typedef`ed type, for a template instantiation, would be the template |
| 285 |
/// its specializing, and so on. Return None if the type is unresolved. |
| 286 |
pub(crate) fn safe_canonical_type<'tr>( |
| 287 |
&'tr self, |
| 288 |
ctx: &'tr BindgenContext, |
| 289 |
) -> Option<&'tr Type> { |
| 290 |
match self.kind { |
| 291 |
TypeKind::TypeParam | |
| 292 |
TypeKind::Array(..) | |
| 293 |
TypeKind::Vector(..) | |
| 294 |
TypeKind::Comp(..) | |
| 295 |
TypeKind::Opaque | |
| 296 |
TypeKind::Int(..) | |
| 297 |
TypeKind::Float(..) | |
| 298 |
TypeKind::Complex(..) | |
| 299 |
TypeKind::Function(..) | |
| 300 |
TypeKind::Enum(..) | |
| 301 |
TypeKind::Reference(..) | |
| 302 |
TypeKind::Void | |
| 303 |
TypeKind::NullPtr | |
| 304 |
TypeKind::Pointer(..) | |
| 305 |
TypeKind::BlockPointer(..) | |
| 306 |
TypeKind::ObjCId | |
| 307 |
TypeKind::ObjCSel | |
| 308 |
TypeKind::ObjCInterface(..) => Some(self), |
| 309 |
|
| 310 |
TypeKind::ResolvedTypeRef(inner) | |
| 311 |
TypeKind::Alias(inner) | |
| 312 |
TypeKind::TemplateAlias(inner, _) => { |
| 313 |
ctx.resolve_type(inner).safe_canonical_type(ctx) |
| 314 |
} |
| 315 |
TypeKind::TemplateInstantiation(ref inst) => ctx |
| 316 |
.resolve_type(inst.template_definition()) |
| 317 |
.safe_canonical_type(ctx), |
| 318 |
|
| 319 |
TypeKind::UnresolvedTypeRef(..) => None, |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
/// There are some types we don't want to stop at when finding an opaque |
| 324 |
/// item, so we can arrive to the proper item that needs to be generated. |
| 325 |
pub(crate) fn should_be_traced_unconditionally(&self) -> bool { |
| 326 |
matches!( |
| 327 |
self.kind, |
| 328 |
TypeKind::Comp(..) | |
| 329 |
TypeKind::Function(..) | |
| 330 |
TypeKind::Pointer(..) | |
| 331 |
TypeKind::Array(..) | |
| 332 |
TypeKind::Reference(..) | |
| 333 |
TypeKind::TemplateInstantiation(..) | |
| 334 |
TypeKind::ResolvedTypeRef(..) |
| 335 |
) |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
impl IsOpaque for Type { |
| 340 |
type Extra = Item; |
| 341 |
|
| 342 |
fn is_opaque(&self, ctx: &BindgenContext, item: &Item) -> bool { |
| 343 |
match self.kind { |
| 344 |
TypeKind::Opaque => true, |
| 345 |
TypeKind::TemplateInstantiation(ref inst) => { |
| 346 |
inst.is_opaque(ctx, item) |
| 347 |
} |
| 348 |
TypeKind::Comp(ref comp) => comp.is_opaque(ctx, &self.layout), |
| 349 |
TypeKind::ResolvedTypeRef(to) => to.is_opaque(ctx, &()), |
| 350 |
_ => false, |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
impl AsTemplateParam for Type { |
| 356 |
type Extra = Item; |
| 357 |
|
| 358 |
fn as_template_param( |
| 359 |
&self, |
| 360 |
ctx: &BindgenContext, |
| 361 |
item: &Item, |
| 362 |
) -> Option<TypeId> { |
| 363 |
self.kind.as_template_param(ctx, item) |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
impl AsTemplateParam for TypeKind { |
| 368 |
type Extra = Item; |
| 369 |
|
| 370 |
fn as_template_param( |
| 371 |
&self, |
| 372 |
ctx: &BindgenContext, |
| 373 |
item: &Item, |
| 374 |
) -> Option<TypeId> { |
| 375 |
match *self { |
| 376 |
TypeKind::TypeParam => Some(item.id().expect_type_id(ctx)), |
| 377 |
TypeKind::ResolvedTypeRef(id) => id.as_template_param(ctx, &()), |
| 378 |
_ => None, |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
impl DotAttributes for Type { |
| 384 |
fn dot_attributes<W>( |
| 385 |
&self, |
| 386 |
ctx: &BindgenContext, |
| 387 |
out: &mut W, |
| 388 |
) -> io::Result<()> |
| 389 |
where |
| 390 |
W: io::Write, |
| 391 |
{ |
| 392 |
if let Some(ref layout) = self.layout { |
| 393 |
writeln!( |
| 394 |
out, |
| 395 |
"<tr><td>size</td><td>{}</td></tr> |
| 396 |
<tr><td>align</td><td>{}</td></tr>", |
| 397 |
layout.size, layout.align |
| 398 |
)?; |
| 399 |
if layout.packed { |
| 400 |
writeln!(out, "<tr><td>packed</td><td>true</td></tr>")?; |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
if self.is_const { |
| 405 |
writeln!(out, "<tr><td>const</td><td>true</td></tr>")?; |
| 406 |
} |
| 407 |
|
| 408 |
self.kind.dot_attributes(ctx, out) |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
impl DotAttributes for TypeKind { |
| 413 |
fn dot_attributes<W>( |
| 414 |
&self, |
| 415 |
ctx: &BindgenContext, |
| 416 |
out: &mut W, |
| 417 |
) -> io::Result<()> |
| 418 |
where |
| 419 |
W: io::Write, |
| 420 |
{ |
| 421 |
writeln!( |
| 422 |
out, |
| 423 |
"<tr><td>type kind</td><td>{}</td></tr>", |
| 424 |
self.kind_name() |
| 425 |
)?; |
| 426 |
|
| 427 |
if let TypeKind::Comp(ref comp) = *self { |
| 428 |
comp.dot_attributes(ctx, out)?; |
| 429 |
} |
| 430 |
|
| 431 |
Ok(()) |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
impl TypeKind { |
| 436 |
fn kind_name(&self) -> &'static str { |
| 437 |
match *self { |
| 438 |
TypeKind::Void => "Void", |
| 439 |
TypeKind::NullPtr => "NullPtr", |
| 440 |
TypeKind::Comp(..) => "Comp", |
| 441 |
TypeKind::Opaque => "Opaque", |
| 442 |
TypeKind::Int(..) => "Int", |
| 443 |
TypeKind::Float(..) => "Float", |
| 444 |
TypeKind::Complex(..) => "Complex", |
| 445 |
TypeKind::Alias(..) => "Alias", |
| 446 |
TypeKind::TemplateAlias(..) => "TemplateAlias", |
| 447 |
TypeKind::Array(..) => "Array", |
| 448 |
TypeKind::Vector(..) => "Vector", |
| 449 |
TypeKind::Function(..) => "Function", |
| 450 |
TypeKind::Enum(..) => "Enum", |
| 451 |
TypeKind::Pointer(..) => "Pointer", |
| 452 |
TypeKind::BlockPointer(..) => "BlockPointer", |
| 453 |
TypeKind::Reference(..) => "Reference", |
| 454 |
TypeKind::TemplateInstantiation(..) => "TemplateInstantiation", |
| 455 |
TypeKind::UnresolvedTypeRef(..) => "UnresolvedTypeRef", |
| 456 |
TypeKind::ResolvedTypeRef(..) => "ResolvedTypeRef", |
| 457 |
TypeKind::TypeParam => "TypeParam", |
| 458 |
TypeKind::ObjCInterface(..) => "ObjCInterface", |
| 459 |
TypeKind::ObjCId => "ObjCId", |
| 460 |
TypeKind::ObjCSel => "ObjCSel", |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
#[test] |
| 466 |
fn is_invalid_type_param_valid() { |
| 467 |
let ty = Type::new(Some("foo".into()), None, TypeKind::TypeParam, false); |
| 468 |
assert!(!ty.is_invalid_type_param()) |
| 469 |
} |
| 470 |
|
| 471 |
#[test] |
| 472 |
fn is_invalid_type_param_valid_underscore_and_numbers() { |
| 473 |
let ty = Type::new( |
| 474 |
Some("_foo123456789_".into()), |
| 475 |
None, |
| 476 |
TypeKind::TypeParam, |
| 477 |
false, |
| 478 |
); |
| 479 |
assert!(!ty.is_invalid_type_param()) |
| 480 |
} |
| 481 |
|
| 482 |
#[test] |
| 483 |
fn is_invalid_type_param_valid_unnamed_kind() { |
| 484 |
let ty = Type::new(Some("foo".into()), None, TypeKind::Void, false); |
| 485 |
assert!(!ty.is_invalid_type_param()) |
| 486 |
} |
| 487 |
|
| 488 |
#[test] |
| 489 |
fn is_invalid_type_param_invalid_start() { |
| 490 |
let ty = Type::new(Some("1foo".into()), None, TypeKind::TypeParam, false); |
| 491 |
assert!(ty.is_invalid_type_param()) |
| 492 |
} |
| 493 |
|
| 494 |
#[test] |
| 495 |
fn is_invalid_type_param_invalid_remaing() { |
| 496 |
let ty = Type::new(Some("foo-".into()), None, TypeKind::TypeParam, false); |
| 497 |
assert!(ty.is_invalid_type_param()) |
| 498 |
} |
| 499 |
|
| 500 |
#[test] |
| 501 |
#[should_panic] |
| 502 |
fn is_invalid_type_param_unnamed() { |
| 503 |
let ty = Type::new(None, None, TypeKind::TypeParam, false); |
| 504 |
assert!(ty.is_invalid_type_param()) |
| 505 |
} |
| 506 |
|
| 507 |
#[test] |
| 508 |
fn is_invalid_type_param_empty_name() { |
| 509 |
let ty = Type::new(Some("".into()), None, TypeKind::TypeParam, false); |
| 510 |
assert!(ty.is_invalid_type_param()) |
| 511 |
} |
| 512 |
|
| 513 |
impl TemplateParameters for Type { |
| 514 |
fn self_template_params(&self, ctx: &BindgenContext) -> Vec<TypeId> { |
| 515 |
self.kind.self_template_params(ctx) |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
impl TemplateParameters for TypeKind { |
| 520 |
fn self_template_params(&self, ctx: &BindgenContext) -> Vec<TypeId> { |
| 521 |
match *self { |
| 522 |
TypeKind::ResolvedTypeRef(id) => { |
| 523 |
ctx.resolve_type(id).self_template_params(ctx) |
| 524 |
} |
| 525 |
TypeKind::Comp(ref comp) => comp.self_template_params(ctx), |
| 526 |
TypeKind::TemplateAlias(_, ref args) => args.clone(), |
| 527 |
|
| 528 |
TypeKind::Opaque | |
| 529 |
TypeKind::TemplateInstantiation(..) | |
| 530 |
TypeKind::Void | |
| 531 |
TypeKind::NullPtr | |
| 532 |
TypeKind::Int(_) | |
| 533 |
TypeKind::Float(_) | |
| 534 |
TypeKind::Complex(_) | |
| 535 |
TypeKind::Array(..) | |
| 536 |
TypeKind::Vector(..) | |
| 537 |
TypeKind::Function(_) | |
| 538 |
TypeKind::Enum(_) | |
| 539 |
TypeKind::Pointer(_) | |
| 540 |
TypeKind::BlockPointer(_) | |
| 541 |
TypeKind::Reference(_) | |
| 542 |
TypeKind::UnresolvedTypeRef(..) | |
| 543 |
TypeKind::TypeParam | |
| 544 |
TypeKind::Alias(_) | |
| 545 |
TypeKind::ObjCId | |
| 546 |
TypeKind::ObjCSel | |
| 547 |
TypeKind::ObjCInterface(_) => vec![], |
| 548 |
} |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
/// The kind of float this type represents. |
| 553 |
#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 554 |
pub(crate) enum FloatKind { |
| 555 |
/// A `float`. |
| 556 |
Float, |
| 557 |
/// A `double`. |
| 558 |
Double, |
| 559 |
/// A `long double`. |
| 560 |
LongDouble, |
| 561 |
/// A `__float128`. |
| 562 |
Float128, |
| 563 |
} |
| 564 |
|
| 565 |
/// The different kinds of types that we can parse. |
| 566 |
#[derive(Debug)] |
| 567 |
pub(crate) enum TypeKind { |
| 568 |
/// The void type. |
| 569 |
Void, |
| 570 |
|
| 571 |
/// The `nullptr_t` type. |
| 572 |
NullPtr, |
| 573 |
|
| 574 |
/// A compound type, that is, a class, struct, or union. |
| 575 |
Comp(CompInfo), |
| 576 |
|
| 577 |
/// An opaque type that we just don't understand. All usage of this shoulf |
| 578 |
/// result in an opaque blob of bytes generated from the containing type's |
| 579 |
/// layout. |
| 580 |
Opaque, |
| 581 |
|
| 582 |
/// An integer type, of a given kind. `bool` and `char` are also considered |
| 583 |
/// integers. |
| 584 |
Int(IntKind), |
| 585 |
|
| 586 |
/// A floating point type. |
| 587 |
Float(FloatKind), |
| 588 |
|
| 589 |
/// A complex floating point type. |
| 590 |
Complex(FloatKind), |
| 591 |
|
| 592 |
/// A type alias, with a name, that points to another type. |
| 593 |
Alias(TypeId), |
| 594 |
|
| 595 |
/// A templated alias, pointing to an inner type, just as `Alias`, but with |
| 596 |
/// template parameters. |
| 597 |
TemplateAlias(TypeId, Vec<TypeId>), |
| 598 |
|
| 599 |
/// A packed vector type: element type, number of elements |
| 600 |
Vector(TypeId, usize), |
| 601 |
|
| 602 |
/// An array of a type and a length. |
| 603 |
Array(TypeId, usize), |
| 604 |
|
| 605 |
/// A function type, with a given signature. |
| 606 |
Function(FunctionSig), |
| 607 |
|
| 608 |
/// An `enum` type. |
| 609 |
Enum(Enum), |
| 610 |
|
| 611 |
/// A pointer to a type. The bool field represents whether it's const or |
| 612 |
/// not. |
| 613 |
Pointer(TypeId), |
| 614 |
|
| 615 |
/// A pointer to an Apple block. |
| 616 |
BlockPointer(TypeId), |
| 617 |
|
| 618 |
/// A reference to a type, as in: int& foo(). |
| 619 |
Reference(TypeId), |
| 620 |
|
| 621 |
/// An instantiation of an abstract template definition with a set of |
| 622 |
/// concrete template arguments. |
| 623 |
TemplateInstantiation(TemplateInstantiation), |
| 624 |
|
| 625 |
/// A reference to a yet-to-resolve type. This stores the clang cursor |
| 626 |
/// itself, and postpones its resolution. |
| 627 |
/// |
| 628 |
/// These are gone in a phase after parsing where these are mapped to |
| 629 |
/// already known types, and are converted to ResolvedTypeRef. |
| 630 |
/// |
| 631 |
/// see tests/headers/typeref.hpp to see somewhere where this is a problem. |
| 632 |
UnresolvedTypeRef( |
| 633 |
clang::Type, |
| 634 |
clang::Cursor, |
| 635 |
/* parent_id */ |
| 636 |
Option<ItemId>, |
| 637 |
), |
| 638 |
|
| 639 |
/// An indirection to another type. |
| 640 |
/// |
| 641 |
/// These are generated after we resolve a forward declaration, or when we |
| 642 |
/// replace one type with another. |
| 643 |
ResolvedTypeRef(TypeId), |
| 644 |
|
| 645 |
/// A named type, that is, a template parameter. |
| 646 |
TypeParam, |
| 647 |
|
| 648 |
/// Objective C interface. Always referenced through a pointer |
| 649 |
ObjCInterface(ObjCInterface), |
| 650 |
|
| 651 |
/// Objective C 'id' type, points to any object |
| 652 |
ObjCId, |
| 653 |
|
| 654 |
/// Objective C selector type |
| 655 |
ObjCSel, |
| 656 |
} |
| 657 |
|
| 658 |
impl Type { |
| 659 |
/// This is another of the nasty methods. This one is the one that takes |
| 660 |
/// care of the core logic of converting a clang type to a `Type`. |
| 661 |
/// |
| 662 |
/// It's sort of nasty and full of special-casing, but hopefully the |
| 663 |
/// comments in every special case justify why they're there. |
| 664 |
pub(crate) fn from_clang_ty( |
| 665 |
potential_id: ItemId, |
| 666 |
ty: &clang::Type, |
| 667 |
location: Cursor, |
| 668 |
parent_id: Option<ItemId>, |
| 669 |
ctx: &mut BindgenContext, |
| 670 |
) -> Result<ParseResult<Self>, ParseError> { |
| 671 |
use clang_sys::*; |
| 672 |
{ |
| 673 |
let already_resolved = ctx.builtin_or_resolved_ty( |
| 674 |
potential_id, |
| 675 |
parent_id, |
| 676 |
ty, |
| 677 |
Some(location), |
| 678 |
); |
| 679 |
if let Some(ty) = already_resolved { |
| 680 |
debug!("{:?} already resolved: {:?}", ty, location); |
| 681 |
return Ok(ParseResult::AlreadyResolved(ty.into())); |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
let layout = ty.fallible_layout(ctx).ok(); |
| 686 |
let cursor = ty.declaration(); |
| 687 |
let is_anonymous = cursor.is_anonymous(); |
| 688 |
let mut name = if is_anonymous { |
| 689 |
None |
| 690 |
} else { |
| 691 |
Some(cursor.spelling()).filter(|n| !n.is_empty()) |
| 692 |
}; |
| 693 |
|
| 694 |
debug!( |
| 695 |
"from_clang_ty: {:?}, ty: {:?}, loc: {:?}", |
| 696 |
potential_id, ty, location |
| 697 |
); |
| 698 |
debug!("currently_parsed_types: {:?}", ctx.currently_parsed_types()); |
| 699 |
|
| 700 |
let canonical_ty = ty.canonical_type(); |
| 701 |
|
| 702 |
// Parse objc protocols as if they were interfaces |
| 703 |
let mut ty_kind = ty.kind(); |
| 704 |
match location.kind() { |
| 705 |
CXCursor_ObjCProtocolDecl | CXCursor_ObjCCategoryDecl => { |
| 706 |
ty_kind = CXType_ObjCInterface |
| 707 |
} |
| 708 |
_ => {} |
| 709 |
} |
| 710 |
|
| 711 |
// Objective C template type parameter |
| 712 |
// FIXME: This is probably wrong, we are attempting to find the |
| 713 |
// objc template params, which seem to manifest as a typedef. |
| 714 |
// We are rewriting them as ID to suppress multiple conflicting |
| 715 |
// typedefs at root level |
| 716 |
if ty_kind == CXType_Typedef { |
| 717 |
let is_template_type_param = |
| 718 |
ty.declaration().kind() == CXCursor_TemplateTypeParameter; |
| 719 |
let is_canonical_objcpointer = |
| 720 |
canonical_ty.kind() == CXType_ObjCObjectPointer; |
| 721 |
|
| 722 |
// We have found a template type for objc interface |
| 723 |
if is_canonical_objcpointer && is_template_type_param { |
| 724 |
// Objective-C generics are just ids with fancy name. |
| 725 |
// To keep it simple, just name them ids |
| 726 |
name = Some("id".to_owned()); |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
if location.kind() == CXCursor_ClassTemplatePartialSpecialization { |
| 731 |
// Sorry! (Not sorry) |
| 732 |
warn!( |
| 733 |
"Found a partial template specialization; bindgen does not \ |
| 734 |
support partial template specialization! Constructing \ |
| 735 |
opaque type instead." |
| 736 |
); |
| 737 |
return Ok(ParseResult::New( |
| 738 |
Opaque::from_clang_ty(&canonical_ty, ctx), |
| 739 |
None, |
| 740 |
)); |
| 741 |
} |
| 742 |
|
| 743 |
let kind = if location.kind() == CXCursor_TemplateRef || |
| 744 |
(ty.template_args().is_some() && ty_kind != CXType_Typedef) |
| 745 |
{ |
| 746 |
// This is a template instantiation. |
| 747 |
match TemplateInstantiation::from_ty(ty, ctx) { |
| 748 |
Some(inst) => TypeKind::TemplateInstantiation(inst), |
| 749 |
None => TypeKind::Opaque, |
| 750 |
} |
| 751 |
} else { |
| 752 |
match ty_kind { |
| 753 |
CXType_Unexposed |
| 754 |
if *ty != canonical_ty && |
| 755 |
canonical_ty.kind() != CXType_Invalid && |
| 756 |
ty.ret_type().is_none() && |
| 757 |
// Sometime clang desugars some types more than |
| 758 |
// what we need, specially with function |
| 759 |
// pointers. |
| 760 |
// |
| 761 |
// We should also try the solution of inverting |
| 762 |
// those checks instead of doing this, that is, |
| 763 |
// something like: |
| 764 |
// |
| 765 |
// CXType_Unexposed if ty.ret_type().is_some() |
| 766 |
// => { ... } |
| 767 |
// |
| 768 |
// etc. |
| 769 |
!canonical_ty.spelling().contains("type-parameter") => |
| 770 |
{ |
| 771 |
debug!("Looking for canonical type: {:?}", canonical_ty); |
| 772 |
return Self::from_clang_ty( |
| 773 |
potential_id, |
| 774 |
&canonical_ty, |
| 775 |
location, |
| 776 |
parent_id, |
| 777 |
ctx, |
| 778 |
); |
| 779 |
} |
| 780 |
CXType_Unexposed | CXType_Invalid => { |
| 781 |
// For some reason Clang doesn't give us any hint in some |
| 782 |
// situations where we should generate a function pointer (see |
| 783 |
// tests/headers/func_ptr_in_struct.h), so we do a guess here |
| 784 |
// trying to see if it has a valid return type. |
| 785 |
if ty.ret_type().is_some() { |
| 786 |
let signature = |
| 787 |
FunctionSig::from_ty(ty, &location, ctx)?; |
| 788 |
TypeKind::Function(signature) |
| 789 |
// Same here, with template specialisations we can safely |
| 790 |
// assume this is a Comp(..) |
| 791 |
} else if ty.is_fully_instantiated_template() { |
| 792 |
debug!( |
| 793 |
"Template specialization: {:?}, {:?} {:?}", |
| 794 |
ty, location, canonical_ty |
| 795 |
); |
| 796 |
let complex = CompInfo::from_ty( |
| 797 |
potential_id, |
| 798 |
ty, |
| 799 |
Some(location), |
| 800 |
ctx, |
| 801 |
) |
| 802 |
.expect("C'mon"); |
| 803 |
TypeKind::Comp(complex) |
| 804 |
} else { |
| 805 |
match location.kind() { |
| 806 |
CXCursor_CXXBaseSpecifier | |
| 807 |
CXCursor_ClassTemplate => { |
| 808 |
if location.kind() == CXCursor_CXXBaseSpecifier |
| 809 |
{ |
| 810 |
// In the case we're parsing a base specifier |
| 811 |
// inside an unexposed or invalid type, it means |
| 812 |
// that we're parsing one of two things: |
| 813 |
// |
| 814 |
// * A template parameter. |
| 815 |
// * A complex class that isn't exposed. |
| 816 |
// |
| 817 |
// This means, unfortunately, that there's no |
| 818 |
// good way to differentiate between them. |
| 819 |
// |
| 820 |
// Probably we could try to look at the |
| 821 |
// declaration and complicate more this logic, |
| 822 |
// but we'll keep it simple... if it's a valid |
| 823 |
// C++ identifier, we'll consider it as a |
| 824 |
// template parameter. |
| 825 |
// |
| 826 |
// This is because: |
| 827 |
// |
| 828 |
// * We expect every other base that is a |
| 829 |
// proper identifier (that is, a simple |
| 830 |
// struct/union declaration), to be exposed, |
| 831 |
// so this path can't be reached in that |
| 832 |
// case. |
| 833 |
// |
| 834 |
// * Quite conveniently, complex base |
| 835 |
// specifiers preserve their full names (that |
| 836 |
// is: Foo<T> instead of Foo). We can take |
| 837 |
// advantage of this. |
| 838 |
// |
| 839 |
// If we find some edge case where this doesn't |
| 840 |
// work (which I guess is unlikely, see the |
| 841 |
// different test cases[1][2][3][4]), we'd need |
| 842 |
// to find more creative ways of differentiating |
| 843 |
// these two cases. |
| 844 |
// |
| 845 |
// [1]: inherit_named.hpp |
| 846 |
// [2]: forward-inherit-struct-with-fields.hpp |
| 847 |
// [3]: forward-inherit-struct.hpp |
| 848 |
// [4]: inherit-namespaced.hpp |
| 849 |
if location.spelling().chars().all(|c| { |
| 850 |
c.is_alphanumeric() || c == '_' |
| 851 |
}) { |
| 852 |
return Err(ParseError::Recurse); |
| 853 |
} |
| 854 |
} else { |
| 855 |
name = Some(location.spelling()); |
| 856 |
} |
| 857 |
|
| 858 |
let complex = CompInfo::from_ty( |
| 859 |
potential_id, |
| 860 |
ty, |
| 861 |
Some(location), |
| 862 |
ctx, |
| 863 |
); |
| 864 |
match complex { |
| 865 |
Ok(complex) => TypeKind::Comp(complex), |
| 866 |
Err(_) => { |
| 867 |
warn!( |
| 868 |
"Could not create complex type \ |
| 869 |
from class template or base \ |
| 870 |
specifier, using opaque blob" |
| 871 |
); |
| 872 |
let opaque = |
| 873 |
Opaque::from_clang_ty(ty, ctx); |
| 874 |
return Ok(ParseResult::New( |
| 875 |
opaque, None, |
| 876 |
)); |
| 877 |
} |
| 878 |
} |
| 879 |
} |
| 880 |
CXCursor_TypeAliasTemplateDecl => { |
| 881 |
debug!("TypeAliasTemplateDecl"); |
| 882 |
|
| 883 |
// We need to manually unwind this one. |
| 884 |
let mut inner = Err(ParseError::Continue); |
| 885 |
let mut args = vec![]; |
| 886 |
|
| 887 |
location.visit(|cur| { |
| 888 |
match cur.kind() { |
| 889 |
CXCursor_TypeAliasDecl => { |
| 890 |
let current = cur.cur_type(); |
| 891 |
|
| 892 |
debug_assert_eq!( |
| 893 |
current.kind(), |
| 894 |
CXType_Typedef |
| 895 |
); |
| 896 |
|
| 897 |
name = Some(location.spelling()); |
| 898 |
|
| 899 |
let inner_ty = cur |
| 900 |
.typedef_type() |
| 901 |
.expect("Not valid Type?"); |
| 902 |
inner = Ok(Item::from_ty_or_ref( |
| 903 |
inner_ty, |
| 904 |
cur, |
| 905 |
Some(potential_id), |
| 906 |
ctx, |
| 907 |
)); |
| 908 |
} |
| 909 |
CXCursor_TemplateTypeParameter => { |
| 910 |
let param = Item::type_param( |
| 911 |
None, cur, ctx, |
| 912 |
) |
| 913 |
.expect( |
| 914 |
"Item::type_param shouldn't \ |
| 915 |
ever fail if we are looking \ |
| 916 |
at a TemplateTypeParameter", |
| 917 |
); |
| 918 |
args.push(param); |
| 919 |
} |
| 920 |
_ => {} |
| 921 |
} |
| 922 |
CXChildVisit_Continue |
| 923 |
}); |
| 924 |
|
| 925 |
let inner_type = match inner { |
| 926 |
Ok(inner) => inner, |
| 927 |
Err(..) => { |
| 928 |
warn!( |
| 929 |
"Failed to parse template alias \ |
| 930 |
{:?}", |
| 931 |
location |
| 932 |
); |
| 933 |
return Err(ParseError::Continue); |
| 934 |
} |
| 935 |
}; |
| 936 |
|
| 937 |
TypeKind::TemplateAlias(inner_type, args) |
| 938 |
} |
| 939 |
CXCursor_TemplateRef => { |
| 940 |
let referenced = location.referenced().unwrap(); |
| 941 |
let referenced_ty = referenced.cur_type(); |
| 942 |
|
| 943 |
debug!( |
| 944 |
"TemplateRef: location = {:?}; referenced = \ |
| 945 |
{:?}; referenced_ty = {:?}", |
| 946 |
location, |
| 947 |
referenced, |
| 948 |
referenced_ty |
| 949 |
); |
| 950 |
|
| 951 |
return Self::from_clang_ty( |
| 952 |
potential_id, |
| 953 |
&referenced_ty, |
| 954 |
referenced, |
| 955 |
parent_id, |
| 956 |
ctx, |
| 957 |
); |
| 958 |
} |
| 959 |
CXCursor_TypeRef => { |
| 960 |
let referenced = location.referenced().unwrap(); |
| 961 |
let referenced_ty = referenced.cur_type(); |
| 962 |
let declaration = referenced_ty.declaration(); |
| 963 |
|
| 964 |
debug!( |
| 965 |
"TypeRef: location = {:?}; referenced = \ |
| 966 |
{:?}; referenced_ty = {:?}", |
| 967 |
location, referenced, referenced_ty |
| 968 |
); |
| 969 |
|
| 970 |
let id = Item::from_ty_or_ref_with_id( |
| 971 |
potential_id, |
| 972 |
referenced_ty, |
| 973 |
declaration, |
| 974 |
parent_id, |
| 975 |
ctx, |
| 976 |
); |
| 977 |
return Ok(ParseResult::AlreadyResolved( |
| 978 |
id.into(), |
| 979 |
)); |
| 980 |
} |
| 981 |
CXCursor_NamespaceRef => { |
| 982 |
return Err(ParseError::Continue); |
| 983 |
} |
| 984 |
_ => { |
| 985 |
if ty.kind() == CXType_Unexposed { |
| 986 |
warn!( |
| 987 |
"Unexposed type {:?}, recursing inside, \ |
| 988 |
loc: {:?}", |
| 989 |
ty, |
| 990 |
location |
| 991 |
); |
| 992 |
return Err(ParseError::Recurse); |
| 993 |
} |
| 994 |
|
| 995 |
warn!("invalid type {:?}", ty); |
| 996 |
return Err(ParseError::Continue); |
| 997 |
} |
| 998 |
} |
| 999 |
} |
| 1000 |
} |
| 1001 |
CXType_Auto => { |
| 1002 |
if canonical_ty == *ty { |
| 1003 |
debug!("Couldn't find deduced type: {:?}", ty); |
| 1004 |
return Err(ParseError::Continue); |
| 1005 |
} |
| 1006 |
|
| 1007 |
return Self::from_clang_ty( |
| 1008 |
potential_id, |
| 1009 |
&canonical_ty, |
| 1010 |
location, |
| 1011 |
parent_id, |
| 1012 |
ctx, |
| 1013 |
); |
| 1014 |
} |
| 1015 |
// NOTE: We don't resolve pointers eagerly because the pointee type |
| 1016 |
// might not have been parsed, and if it contains templates or |
| 1017 |
// something else we might get confused, see the comment inside |
| 1018 |
// TypeRef. |
| 1019 |
// |
| 1020 |
// We might need to, though, if the context is already in the |
| 1021 |
// process of resolving them. |
| 1022 |
CXType_ObjCObjectPointer | |
| 1023 |
CXType_MemberPointer | |
| 1024 |
CXType_Pointer => { |
| 1025 |
let mut pointee = ty.pointee_type().unwrap(); |
| 1026 |
if *ty != canonical_ty { |
| 1027 |
let canonical_pointee = |
| 1028 |
canonical_ty.pointee_type().unwrap(); |
| 1029 |
// clang sometimes loses pointee constness here, see |
| 1030 |
// #2244. |
| 1031 |
if canonical_pointee.is_const() != pointee.is_const() { |
| 1032 |
pointee = canonical_pointee; |
| 1033 |
} |
| 1034 |
} |
| 1035 |
let inner = |
| 1036 |
Item::from_ty_or_ref(pointee, location, None, ctx); |
| 1037 |
TypeKind::Pointer(inner) |
| 1038 |
} |
| 1039 |
CXType_BlockPointer => { |
| 1040 |
let pointee = ty.pointee_type().expect("Not valid Type?"); |
| 1041 |
let inner = |
| 1042 |
Item::from_ty_or_ref(pointee, location, None, ctx); |
| 1043 |
TypeKind::BlockPointer(inner) |
| 1044 |
} |
| 1045 |
// XXX: RValueReference is most likely wrong, but I don't think we |
| 1046 |
// can even add bindings for that, so huh. |
| 1047 |
CXType_RValueReference | CXType_LValueReference => { |
| 1048 |
let inner = Item::from_ty_or_ref( |
| 1049 |
ty.pointee_type().unwrap(), |
| 1050 |
location, |
| 1051 |
None, |
| 1052 |
ctx, |
| 1053 |
); |
| 1054 |
TypeKind::Reference(inner) |
| 1055 |
} |
| 1056 |
// XXX DependentSizedArray is wrong |
| 1057 |
CXType_VariableArray | CXType_DependentSizedArray => { |
| 1058 |
let inner = Item::from_ty( |
| 1059 |
ty.elem_type().as_ref().unwrap(), |
| 1060 |
location, |
| 1061 |
None, |
| 1062 |
ctx, |
| 1063 |
) |
| 1064 |
.expect("Not able to resolve array element?"); |
| 1065 |
TypeKind::Pointer(inner) |
| 1066 |
} |
| 1067 |
CXType_IncompleteArray => { |
| 1068 |
let inner = Item::from_ty( |
| 1069 |
ty.elem_type().as_ref().unwrap(), |
| 1070 |
location, |
| 1071 |
None, |
| 1072 |
ctx, |
| 1073 |
) |
| 1074 |
.expect("Not able to resolve array element?"); |
| 1075 |
TypeKind::Array(inner, 0) |
| 1076 |
} |
| 1077 |
CXType_FunctionNoProto | CXType_FunctionProto => { |
| 1078 |
let signature = FunctionSig::from_ty(ty, &location, ctx)?; |
| 1079 |
TypeKind::Function(signature) |
| 1080 |
} |
| 1081 |
CXType_Typedef => { |
| 1082 |
let inner = cursor.typedef_type().expect("Not valid Type?"); |
| 1083 |
let inner_id = |
| 1084 |
Item::from_ty_or_ref(inner, location, None, ctx); |
| 1085 |
if inner_id == potential_id { |
| 1086 |
warn!( |
| 1087 |
"Generating oqaque type instead of self-referential \ |
| 1088 |
typedef"); |
| 1089 |
// This can happen if we bail out of recursive situations |
| 1090 |
// within the clang parsing. |
| 1091 |
TypeKind::Opaque |
| 1092 |
} else { |
| 1093 |
// Check if this type definition is an alias to a pointer of a `struct` / |
| 1094 |
// `union` / `enum` with the same name and add the `_ptr` suffix to it to |
| 1095 |
// avoid name collisions. |
| 1096 |
if let Some(ref mut name) = name { |
| 1097 |
if inner.kind() == CXType_Pointer && |
| 1098 |
!ctx.options().c_naming |
| 1099 |
{ |
| 1100 |
let pointee = inner.pointee_type().unwrap(); |
| 1101 |
if pointee.kind() == CXType_Elaborated && |
| 1102 |
pointee.declaration().spelling() == *name |
| 1103 |
{ |
| 1104 |
*name += "_ptr"; |
| 1105 |
} |
| 1106 |
} |
| 1107 |
} |
| 1108 |
TypeKind::Alias(inner_id) |
| 1109 |
} |
| 1110 |
} |
| 1111 |
CXType_Enum => { |
| 1112 |
let enum_ = Enum::from_ty(ty, ctx).expect("Not an enum?"); |
| 1113 |
|
| 1114 |
if !is_anonymous { |
| 1115 |
let pretty_name = ty.spelling(); |
| 1116 |
if clang::is_valid_identifier(&pretty_name) { |
| 1117 |
name = Some(pretty_name); |
| 1118 |
} |
| 1119 |
} |
| 1120 |
|
| 1121 |
TypeKind::Enum(enum_) |
| 1122 |
} |
| 1123 |
CXType_Record => { |
| 1124 |
let complex = CompInfo::from_ty( |
| 1125 |
potential_id, |
| 1126 |
ty, |
| 1127 |
Some(location), |
| 1128 |
ctx, |
| 1129 |
) |
| 1130 |
.expect("Not a complex type?"); |
| 1131 |
|
| 1132 |
if !is_anonymous { |
| 1133 |
// The pretty-printed name may contain typedefed name, |
| 1134 |
// but may also be "struct (anonymous at .h:1)" |
| 1135 |
let pretty_name = ty.spelling(); |
| 1136 |
if clang::is_valid_identifier(&pretty_name) { |
| 1137 |
name = Some(pretty_name); |
| 1138 |
} |
| 1139 |
} |
| 1140 |
|
| 1141 |
TypeKind::Comp(complex) |
| 1142 |
} |
| 1143 |
CXType_Vector => { |
| 1144 |
let inner = Item::from_ty( |
| 1145 |
ty.elem_type().as_ref().unwrap(), |
| 1146 |
location, |
| 1147 |
None, |
| 1148 |
ctx, |
| 1149 |
)?; |
| 1150 |
TypeKind::Vector(inner, ty.num_elements().unwrap()) |
| 1151 |
} |
| 1152 |
CXType_ConstantArray => { |
| 1153 |
let inner = Item::from_ty( |
| 1154 |
ty.elem_type().as_ref().unwrap(), |
| 1155 |
location, |
| 1156 |
None, |
| 1157 |
ctx, |
| 1158 |
) |
| 1159 |
.expect("Not able to resolve array element?"); |
| 1160 |
TypeKind::Array(inner, ty.num_elements().unwrap()) |
| 1161 |
} |
| 1162 |
CXType_Elaborated => { |
| 1163 |
return Self::from_clang_ty( |
| 1164 |
potential_id, |
| 1165 |
&ty.named(), |
| 1166 |
location, |
| 1167 |
parent_id, |
| 1168 |
ctx, |
| 1169 |
); |
| 1170 |
} |
| 1171 |
CXType_ObjCId => TypeKind::ObjCId, |
| 1172 |
CXType_ObjCSel => TypeKind::ObjCSel, |
| 1173 |
CXType_ObjCClass | CXType_ObjCInterface => { |
| 1174 |
let interface = ObjCInterface::from_ty(&location, ctx) |
| 1175 |
.expect("Not a valid objc interface?"); |
| 1176 |
if !is_anonymous { |
| 1177 |
name = Some(interface.rust_name()); |
| 1178 |
} |
| 1179 |
TypeKind::ObjCInterface(interface) |
| 1180 |
} |
| 1181 |
CXType_Dependent => { |
| 1182 |
return Err(ParseError::Continue); |
| 1183 |
} |
| 1184 |
_ => { |
| 1185 |
warn!( |
| 1186 |
"unsupported type: kind = {:?}; ty = {:?}; at {:?}", |
| 1187 |
ty.kind(), |
| 1188 |
ty, |
| 1189 |
location |
| 1190 |
); |
| 1191 |
return Err(ParseError::Continue); |
| 1192 |
} |
| 1193 |
} |
| 1194 |
}; |
| 1195 |
|
| 1196 |
name = name.filter(|n| !n.is_empty()); |
| 1197 |
|
| 1198 |
let is_const = ty.is_const() || |
| 1199 |
(ty.kind() == CXType_ConstantArray && |
| 1200 |
ty.elem_type() |
| 1201 |
.map_or(false, |element| element.is_const())); |
| 1202 |
|
| 1203 |
let ty = Type::new(name, layout, kind, is_const); |
| 1204 |
// TODO: maybe declaration.canonical()? |
| 1205 |
Ok(ParseResult::New(ty, Some(cursor.canonical()))) |
| 1206 |
} |
| 1207 |
} |
| 1208 |
|
| 1209 |
impl Trace for Type { |
| 1210 |
type Extra = Item; |
| 1211 |
|
| 1212 |
fn trace<T>(&self, context: &BindgenContext, tracer: &mut T, item: &Item) |
| 1213 |
where |
| 1214 |
T: Tracer, |
| 1215 |
{ |
| 1216 |
if self |
| 1217 |
.name() |
| 1218 |
.map_or(false, |name| context.is_stdint_type(name)) |
| 1219 |
{ |
| 1220 |
// These types are special-cased in codegen and don't need to be traversed. |
| 1221 |
return; |
| 1222 |
} |
| 1223 |
match *self.kind() { |
| 1224 |
TypeKind::Pointer(inner) | |
| 1225 |
TypeKind::Reference(inner) | |
| 1226 |
TypeKind::Array(inner, _) | |
| 1227 |
TypeKind::Vector(inner, _) | |
| 1228 |
TypeKind::BlockPointer(inner) | |
| 1229 |
TypeKind::Alias(inner) | |
| 1230 |
TypeKind::ResolvedTypeRef(inner) => { |
| 1231 |
tracer.visit_kind(inner.into(), EdgeKind::TypeReference); |
| 1232 |
} |
| 1233 |
TypeKind::TemplateAlias(inner, ref template_params) => { |
| 1234 |
tracer.visit_kind(inner.into(), EdgeKind::TypeReference); |
| 1235 |
for param in template_params { |
| 1236 |
tracer.visit_kind( |
| 1237 |
param.into(), |
| 1238 |
EdgeKind::TemplateParameterDefinition, |
| 1239 |
); |
| 1240 |
} |
| 1241 |
} |
| 1242 |
TypeKind::TemplateInstantiation(ref inst) => { |
| 1243 |
inst.trace(context, tracer, &()); |
| 1244 |
} |
| 1245 |
TypeKind::Comp(ref ci) => ci.trace(context, tracer, item), |
| 1246 |
TypeKind::Function(ref sig) => sig.trace(context, tracer, &()), |
| 1247 |
TypeKind::Enum(ref en) => { |
| 1248 |
if let Some(repr) = en.repr() { |
| 1249 |
tracer.visit(repr.into()); |
| 1250 |
} |
| 1251 |
} |
| 1252 |
TypeKind::UnresolvedTypeRef(_, _, Some(id)) => { |
| 1253 |
tracer.visit(id); |
| 1254 |
} |
| 1255 |
|
| 1256 |
TypeKind::ObjCInterface(ref interface) => { |
| 1257 |
interface.trace(context, tracer, &()); |
| 1258 |
} |
| 1259 |
|
| 1260 |
// None of these variants have edges to other items and types. |
| 1261 |
TypeKind::Opaque | |
| 1262 |
TypeKind::UnresolvedTypeRef(_, _, None) | |
| 1263 |
TypeKind::TypeParam | |
| 1264 |
TypeKind::Void | |
| 1265 |
TypeKind::NullPtr | |
| 1266 |
TypeKind::Int(_) | |
| 1267 |
TypeKind::Float(_) | |
| 1268 |
TypeKind::Complex(_) | |
| 1269 |
TypeKind::ObjCId | |
| 1270 |
TypeKind::ObjCSel => {} |
| 1271 |
} |
| 1272 |
} |
| 1273 |
} |