Skip to content

Commit bdd034b

Browse files
author
bors-servo
authored
Auto merge of rust-lang#461 - emilio:clean-naming, r=fitzgen
ir: Cleanup name duplication in aliases and named types. It's just dumb. r? @fitzgen
2 parents 5fc3081 + a46971c commit bdd034b

File tree

3 files changed

+47
-64
lines changed

3 files changed

+47
-64
lines changed

src/codegen/mod.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ impl CodeGenerator for Type {
507507
TypeKind::TemplateRef(..) |
508508
TypeKind::Function(..) |
509509
TypeKind::ResolvedTypeRef(..) |
510-
TypeKind::Named(..) => {
510+
TypeKind::Named => {
511511
// These items don't need code generation, they only need to be
512512
// converted to rust types in fields, arguments, and such.
513513
return;
@@ -517,8 +517,8 @@ impl CodeGenerator for Type {
517517
}
518518
// NB: The code below will pick the correct
519519
// applicable_template_args.
520-
TypeKind::TemplateAlias(ref spelling, inner, _) |
521-
TypeKind::Alias(ref spelling, inner) => {
520+
TypeKind::TemplateAlias(inner, _) |
521+
TypeKind::Alias(inner) => {
522522
let inner_item = ctx.resolve_item(inner);
523523
let name = item.canonical_name(ctx);
524524

@@ -534,6 +534,7 @@ impl CodeGenerator for Type {
534534

535535
// If this is a known named type, disallow generating anything
536536
// for it too.
537+
let spelling = self.name().expect("Unnamed alias?");
537538
if utils::type_from_named(ctx, spelling, inner).is_some() {
538539
return;
539540
}
@@ -2043,14 +2044,15 @@ impl ToRustTy for Type {
20432044
P(inner_ty)
20442045
}
20452046
TypeKind::ResolvedTypeRef(inner) => inner.to_rust_ty(ctx),
2046-
TypeKind::TemplateAlias(ref spelling, inner, _) |
2047-
TypeKind::Alias(ref spelling, inner) => {
2047+
TypeKind::TemplateAlias(inner, _) |
2048+
TypeKind::Alias(inner) => {
20482049
let applicable_named_args =
20492050
item.applicable_template_args(ctx)
20502051
.into_iter()
20512052
.filter(|arg| ctx.resolve_type(*arg).is_named())
20522053
.collect::<Vec<_>>();
20532054

2055+
let spelling = self.name().expect("Unnamed alias?");
20542056
if item.is_opaque(ctx) && !applicable_named_args.is_empty() {
20552057
// Pray if there's no available layout.
20562058
let layout = self.layout(ctx).unwrap_or_else(Layout::zero);
@@ -2104,7 +2106,7 @@ impl ToRustTy for Type {
21042106
ty.to_ptr(is_const, ctx.span())
21052107
}
21062108
}
2107-
TypeKind::Named(..) => {
2109+
TypeKind::Named => {
21082110
let name = item.canonical_name(ctx);
21092111
let ident = ctx.rust_ident(&name);
21102112
quote_ty!(ctx.ext_cx(), $ident)

src/ir/item.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -561,23 +561,23 @@ impl Item {
561561
parent_template_args.iter().any(|parent_item| {
562562
let parent_ty = ctx.resolve_type(*parent_item);
563563
match (parent_ty.kind(), item_ty.kind()) {
564-
(&TypeKind::Named(ref n), &TypeKind::Named(ref i)) => {
565-
n == i
564+
(&TypeKind::Named, &TypeKind::Named) => {
565+
parent_ty.name() == item_ty.name()
566566
}
567567
_ => false,
568568
}
569569
})
570570
}
571571

572572
match *ty.kind() {
573-
TypeKind::Named(..) => vec![self.id()],
573+
TypeKind::Named => vec![self.id()],
574574
TypeKind::Array(inner, _) |
575575
TypeKind::Pointer(inner) |
576576
TypeKind::Reference(inner) |
577577
TypeKind::ResolvedTypeRef(inner) => {
578578
ctx.resolve_item(inner).applicable_template_args(ctx)
579579
}
580-
TypeKind::Alias(_, inner) => {
580+
TypeKind::Alias(inner) => {
581581
let parent_args = ctx.resolve_item(self.parent_id())
582582
.applicable_template_args(ctx);
583583
let inner = ctx.resolve_item(inner);
@@ -594,7 +594,7 @@ impl Item {
594594
}
595595
// XXX Is this completely correct? Partial template specialization
596596
// is hard anyways, sigh...
597-
TypeKind::TemplateAlias(_, _, ref args) |
597+
TypeKind::TemplateAlias(_, ref args) |
598598
TypeKind::TemplateRef(_, ref args) => args.clone(),
599599
// In a template specialization we've got all we want.
600600
TypeKind::Comp(ref ci) if ci.is_template_specialization() => {

src/ir/ty.rs

+34-53
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,6 @@ impl Type {
6565
&self.kind
6666
}
6767

68-
/// Overrides the kind of the item. This is mostly a template alias
69-
/// implementation detail, and debug assertions guard it like so.
70-
pub fn set_kind(&mut self, kind: TypeKind) {
71-
if cfg!(debug_assertions) {
72-
match (&self.kind, &kind) {
73-
(&TypeKind::Alias(ref alias_name, alias_inner),
74-
&TypeKind::TemplateAlias(ref name, inner, _)) => {
75-
assert_eq!(alias_name, name);
76-
assert_eq!(alias_inner, inner);
77-
}
78-
_ => panic!("Unexpected kind in `set_kind`!"),
79-
};
80-
}
81-
self.kind = kind;
82-
}
83-
8468
/// Get a mutable reference to this type's kind.
8569
pub fn kind_mut(&mut self) -> &mut TypeKind {
8670
&mut self.kind
@@ -102,7 +86,7 @@ impl Type {
10286
/// Is this a named type?
10387
pub fn is_named(&self) -> bool {
10488
match self.kind {
105-
TypeKind::Named(..) => true,
89+
TypeKind::Named => true,
10690
_ => false,
10791
}
10892
}
@@ -143,17 +127,15 @@ impl Type {
143127
TypeKind::BlockPointer |
144128
TypeKind::Int(..) |
145129
TypeKind::Float(..) |
146-
TypeKind::Named(..) => true,
130+
TypeKind::Named => true,
147131
_ => false,
148132
}
149133
}
150134

151135
/// Creates a new named type, with name `name`.
152136
pub fn named(name: String) -> Self {
153137
assert!(!name.is_empty());
154-
// TODO: stop duplicating the name, it's stupid.
155-
let kind = TypeKind::Named(name.clone());
156-
Self::new(Some(name), None, kind, false)
138+
Self::new(Some(name), None, TypeKind::Named, false)
157139
}
158140

159141
/// Is this a floating point type?
@@ -230,8 +212,8 @@ impl Type {
230212
// FIXME: Can we do something about template parameters? Huh...
231213
match self.kind {
232214
TypeKind::TemplateRef(t, _) |
233-
TypeKind::TemplateAlias(_, t, _) |
234-
TypeKind::Alias(_, t) |
215+
TypeKind::TemplateAlias(t, _) |
216+
TypeKind::Alias(t) |
235217
TypeKind::ResolvedTypeRef(t) => ctx.resolve_type(t).has_vtable(ctx),
236218
TypeKind::Comp(ref info) => info.has_vtable(ctx),
237219
_ => false,
@@ -243,8 +225,8 @@ impl Type {
243225
pub fn has_destructor(&self, ctx: &BindgenContext) -> bool {
244226
match self.kind {
245227
TypeKind::TemplateRef(t, _) |
246-
TypeKind::TemplateAlias(_, t, _) |
247-
TypeKind::Alias(_, t) |
228+
TypeKind::TemplateAlias(t, _) |
229+
TypeKind::Alias(t) |
248230
TypeKind::ResolvedTypeRef(t) => {
249231
ctx.resolve_type(t).has_destructor(ctx)
250232
}
@@ -259,16 +241,16 @@ impl Type {
259241
ty: &Type)
260242
-> bool {
261243
let name = match *ty.kind() {
262-
TypeKind::Named(ref name) => name,
244+
TypeKind::Named => ty.name(),
263245
ref other @ _ => unreachable!("Not a named type: {:?}", other),
264246
};
265247

266248
match self.kind {
267-
TypeKind::Named(ref this_name) => this_name == name,
249+
TypeKind::Named => self.name() == name,
268250
TypeKind::ResolvedTypeRef(t) |
269251
TypeKind::Array(t, _) |
270252
TypeKind::Pointer(t) |
271-
TypeKind::Alias(_, t) => {
253+
TypeKind::Alias(t) => {
272254
ctx.resolve_type(t)
273255
.signature_contains_named_type(ctx, ty)
274256
}
@@ -280,7 +262,7 @@ impl Type {
280262
ctx.resolve_type(sig.return_type())
281263
.signature_contains_named_type(ctx, ty)
282264
}
283-
TypeKind::TemplateAlias(_, _, ref template_args) |
265+
TypeKind::TemplateAlias(_, ref template_args) |
284266
TypeKind::TemplateRef(_, ref template_args) => {
285267
template_args.iter().any(|arg| {
286268
ctx.resolve_type(*arg)
@@ -298,8 +280,8 @@ impl Type {
298280
/// tests/headers/381-decltype-alias.hpp
299281
pub fn is_invalid_named_type(&self) -> bool {
300282
match self.kind {
301-
TypeKind::Named(ref name) => {
302-
assert!(!name.is_empty());
283+
TypeKind::Named => {
284+
let name = self.name().expect("Unnamed named type?");
303285
let mut chars = name.chars();
304286
let first = chars.next().unwrap();
305287
let mut remaining = chars;
@@ -330,7 +312,7 @@ impl Type {
330312
ctx: &'tr BindgenContext)
331313
-> Option<&'tr Type> {
332314
match self.kind {
333-
TypeKind::Named(..) |
315+
TypeKind::Named |
334316
TypeKind::Array(..) |
335317
TypeKind::Comp(..) |
336318
TypeKind::Int(..) |
@@ -345,8 +327,8 @@ impl Type {
345327
TypeKind::Pointer(..) => Some(self),
346328

347329
TypeKind::ResolvedTypeRef(inner) |
348-
TypeKind::Alias(_, inner) |
349-
TypeKind::TemplateAlias(_, inner, _) |
330+
TypeKind::Alias(inner) |
331+
TypeKind::TemplateAlias(inner, _) |
350332
TypeKind::TemplateRef(inner, _) => {
351333
ctx.resolve_type(inner).safe_canonical_type(ctx)
352334
}
@@ -379,8 +361,8 @@ impl CanDeriveDebug for Type {
379361
len <= RUST_DERIVE_IN_ARRAY_LIMIT && t.can_derive_debug(ctx, ())
380362
}
381363
TypeKind::ResolvedTypeRef(t) |
382-
TypeKind::TemplateAlias(_, t, _) |
383-
TypeKind::Alias(_, t) => t.can_derive_debug(ctx, ()),
364+
TypeKind::TemplateAlias(t, _) |
365+
TypeKind::Alias(t) => t.can_derive_debug(ctx, ()),
384366
TypeKind::Comp(ref info) => {
385367
info.can_derive_debug(ctx, self.layout(ctx))
386368
}
@@ -399,9 +381,9 @@ impl<'a> CanDeriveCopy<'a> for Type {
399381
t.can_derive_copy_in_array(ctx, ())
400382
}
401383
TypeKind::ResolvedTypeRef(t) |
402-
TypeKind::TemplateAlias(_, t, _) |
384+
TypeKind::TemplateAlias(t, _) |
403385
TypeKind::TemplateRef(t, _) |
404-
TypeKind::Alias(_, t) => t.can_derive_copy(ctx, ()),
386+
TypeKind::Alias(t) => t.can_derive_copy(ctx, ()),
405387
TypeKind::Comp(ref info) => {
406388
info.can_derive_copy(ctx, (item, self.layout(ctx)))
407389
}
@@ -415,10 +397,10 @@ impl<'a> CanDeriveCopy<'a> for Type {
415397
-> bool {
416398
match self.kind {
417399
TypeKind::ResolvedTypeRef(t) |
418-
TypeKind::TemplateAlias(_, t, _) |
419-
TypeKind::Alias(_, t) |
400+
TypeKind::TemplateAlias(t, _) |
401+
TypeKind::Alias(t) |
420402
TypeKind::Array(t, _) => t.can_derive_copy_in_array(ctx, ()),
421-
TypeKind::Named(..) => false,
403+
TypeKind::Named => false,
422404
_ => self.can_derive_copy(ctx, item),
423405
}
424406
}
@@ -460,11 +442,11 @@ pub enum TypeKind {
460442
Complex(FloatKind),
461443

462444
/// A type alias, with a name, that points to another type.
463-
Alias(String, ItemId),
445+
Alias(ItemId),
464446

465447
/// A templated alias, pointing to an inner type, just as `Alias`, but with
466448
/// template parameters.
467-
TemplateAlias(String, ItemId, Vec<ItemId>),
449+
TemplateAlias(ItemId, Vec<ItemId>),
468450

469451
/// An array of a type and a lenght.
470452
Array(ItemId, usize),
@@ -509,7 +491,7 @@ pub enum TypeKind {
509491
ResolvedTypeRef(ItemId),
510492

511493
/// A named type, that is, a template parameter.
512-
Named(String),
494+
Named,
513495
}
514496

515497
impl Type {
@@ -527,12 +509,12 @@ impl Type {
527509
size == 0 || ctx.resolve_type(inner).is_unsized(ctx)
528510
}
529511
TypeKind::ResolvedTypeRef(inner) |
530-
TypeKind::Alias(_, inner) |
531-
TypeKind::TemplateAlias(_, inner, _) |
512+
TypeKind::Alias(inner) |
513+
TypeKind::TemplateAlias(inner, _) |
532514
TypeKind::TemplateRef(inner, _) => {
533515
ctx.resolve_type(inner).is_unsized(ctx)
534516
}
535-
TypeKind::Named(..) |
517+
TypeKind::Named |
536518
TypeKind::Int(..) |
537519
TypeKind::Float(..) |
538520
TypeKind::Complex(..) |
@@ -725,8 +707,7 @@ impl Type {
725707
}
726708
};
727709

728-
TypeKind::TemplateAlias(name.clone(),
729-
inner_type,
710+
TypeKind::TemplateAlias(inner_type,
730711
args)
731712
}
732713
CXCursor_TemplateRef => {
@@ -873,7 +854,7 @@ impl Type {
873854
let inner = cursor.typedef_type().expect("Not valid Type?");
874855
let inner =
875856
Item::from_ty_or_ref(inner, location, parent_id, ctx);
876-
TypeKind::Alias(ty.spelling(), inner)
857+
TypeKind::Alias(inner)
877858
}
878859
CXType_Enum => {
879860
let enum_ = Enum::from_ty(ty, ctx).expect("Not an enum?");
@@ -935,12 +916,12 @@ impl TypeCollector for Type {
935916
TypeKind::Pointer(inner) |
936917
TypeKind::Reference(inner) |
937918
TypeKind::Array(inner, _) |
938-
TypeKind::Alias(_, inner) |
919+
TypeKind::Alias(inner) |
939920
TypeKind::ResolvedTypeRef(inner) => {
940921
types.insert(inner);
941922
}
942923

943-
TypeKind::TemplateAlias(_, inner, ref template_args) |
924+
TypeKind::TemplateAlias(inner, ref template_args) |
944925
TypeKind::TemplateRef(inner, ref template_args) => {
945926
types.insert(inner);
946927
for &item in template_args {
@@ -962,7 +943,7 @@ impl TypeCollector for Type {
962943

963944
// None of these variants have edges to other items and types.
964945
TypeKind::UnresolvedTypeRef(_, _, None) |
965-
TypeKind::Named(_) |
946+
TypeKind::Named |
966947
TypeKind::Void |
967948
TypeKind::NullPtr |
968949
TypeKind::Int(_) |

0 commit comments

Comments
 (0)