Skip to content

Commit 7ff102a

Browse files
committed
auto merge of #8927 : thestinger/rust/repr, r=huonw
2 parents 6a3dd30 + 67a8ea5 commit 7ff102a

File tree

8 files changed

+91
-50
lines changed

8 files changed

+91
-50
lines changed

src/librustc/middle/trans/reflect.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ impl Reflector {
5151
C_int(self.bcx.ccx(), i)
5252
}
5353

54+
pub fn c_bool(&mut self, b: bool) -> ValueRef {
55+
C_bool(b)
56+
}
57+
5458
pub fn c_slice(&mut self, s: @str) -> ValueRef {
5559
// We're careful to not use first class aggregates here because that
5660
// will kick us off fast isel. (Issue #4352.)
@@ -146,6 +150,7 @@ impl Reflector {
146150
// Entrypoint
147151
pub fn visit_ty(&mut self, t: ty::t) {
148152
let bcx = self.bcx;
153+
let tcx = bcx.ccx().tcx;
149154
debug!("reflect::visit_ty %s", ty_to_str(bcx.ccx().tcx, t));
150155

151156
match ty::get(t).sty {
@@ -248,17 +253,20 @@ impl Reflector {
248253
}
249254

250255
ty::ty_struct(did, ref substs) => {
251-
let bcx = self.bcx;
252-
let tcx = bcx.ccx().tcx;
253256
let fields = ty::struct_fields(tcx, did, substs);
257+
let mut named_fields = false;
258+
if !fields.is_empty() {
259+
named_fields = fields[0].ident != special_idents::unnamed_field;
260+
}
254261

255262
let extra = ~[self.c_slice(ty_to_str(tcx, t).to_managed()),
263+
self.c_bool(named_fields),
256264
self.c_uint(fields.len())] + self.c_size_and_align(t);
257265
do self.bracketed("class", extra) |this| {
258266
for (i, field) in fields.iter().enumerate() {
259267
let extra = ~[this.c_uint(i),
260-
this.c_slice(
261-
bcx.ccx().sess.str_of(field.ident))]
268+
this.c_slice(bcx.ccx().sess.str_of(field.ident)),
269+
this.c_bool(named_fields)]
262270
+ this.c_mt(&field.mt);
263271
this.visit("class_field", extra);
264272
}
@@ -270,7 +278,6 @@ impl Reflector {
270278
// let the visitor tell us if it wants to visit only a particular
271279
// variant?
272280
ty::ty_enum(did, ref substs) => {
273-
let bcx = self.bcx;
274281
let ccx = bcx.ccx();
275282
let repr = adt::represent_type(bcx.ccx(), t);
276283
let variants = ty::substd_enum_variants(ccx.tcx, did, substs);
@@ -336,8 +343,12 @@ impl Reflector {
336343
}
337344
}
338345

339-
// Miscallaneous extra types
340-
ty::ty_trait(_, _, _, _, _) => self.leaf("trait"),
346+
ty::ty_trait(_, _, _, _, _) => {
347+
let extra = [self.c_slice(ty_to_str(tcx, t).to_managed())];
348+
self.visit("trait", extra);
349+
}
350+
351+
// Miscellaneous extra types
341352
ty::ty_infer(_) => self.leaf("infer"),
342353
ty::ty_err => self.leaf("err"),
343354
ty::ty_param(ref p) => {

src/libstd/reflect.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -331,25 +331,28 @@ impl<V:TyVisitor + MovePtr> TyVisitor for MovePtrAdaptor<V> {
331331
true
332332
}
333333

334-
fn visit_enter_class(&mut self, name: &str, n_fields: uint, sz: uint, align: uint) -> bool {
334+
fn visit_enter_class(&mut self, name: &str, named_fields: bool, n_fields: uint, sz: uint,
335+
align: uint) -> bool {
335336
self.align(align);
336-
if ! self.inner.visit_enter_class(name, n_fields, sz, align) {
337+
if ! self.inner.visit_enter_class(name, named_fields, n_fields, sz, align) {
337338
return false;
338339
}
339340
true
340341
}
341342

342-
fn visit_class_field(&mut self, i: uint, name: &str, mtbl: uint, inner: *TyDesc) -> bool {
343+
fn visit_class_field(&mut self, i: uint, name: &str, named: bool, mtbl: uint,
344+
inner: *TyDesc) -> bool {
343345
unsafe { self.align((*inner).align); }
344-
if ! self.inner.visit_class_field(i, name, mtbl, inner) {
346+
if ! self.inner.visit_class_field(i, name, named, mtbl, inner) {
345347
return false;
346348
}
347349
unsafe { self.bump((*inner).size); }
348350
true
349351
}
350352

351-
fn visit_leave_class(&mut self, name: &str, n_fields: uint, sz: uint, align: uint) -> bool {
352-
if ! self.inner.visit_leave_class(name, n_fields, sz, align) {
353+
fn visit_leave_class(&mut self, name: &str, named_fields: bool, n_fields: uint, sz: uint,
354+
align: uint) -> bool {
355+
if ! self.inner.visit_leave_class(name, named_fields, n_fields, sz, align) {
353356
return false;
354357
}
355358
true
@@ -450,9 +453,9 @@ impl<V:TyVisitor + MovePtr> TyVisitor for MovePtrAdaptor<V> {
450453
true
451454
}
452455

453-
fn visit_trait(&mut self) -> bool {
456+
fn visit_trait(&mut self, name: &str) -> bool {
454457
self.align_to::<@TyVisitor>();
455-
if ! self.inner.visit_trait() { return false; }
458+
if ! self.inner.visit_trait(name) { return false; }
456459
self.bump_past::<@TyVisitor>();
457460
true
458461
}

src/libstd/repr.rs

+37-11
Original file line numberDiff line numberDiff line change
@@ -413,31 +413,40 @@ impl<'self> TyVisitor for ReprVisitor<'self> {
413413
true
414414
}
415415

416-
fn visit_enter_class(&mut self, name: &str, n_fields: uint,
416+
fn visit_enter_class(&mut self, name: &str, named_fields: bool, n_fields: uint,
417417
_sz: uint, _align: uint) -> bool {
418418
self.writer.write(name.as_bytes());
419419
if n_fields != 0 {
420-
self.writer.write(['{' as u8]);
420+
if named_fields {
421+
self.writer.write(['{' as u8]);
422+
} else {
423+
self.writer.write(['(' as u8]);
424+
}
421425
}
422426
true
423427
}
424428

425-
fn visit_class_field(&mut self, i: uint, name: &str,
426-
mtbl: uint, inner: *TyDesc) -> bool {
429+
fn visit_class_field(&mut self, i: uint, name: &str, named: bool,
430+
_mtbl: uint, inner: *TyDesc) -> bool {
427431
if i != 0 {
428432
self.writer.write(", ".as_bytes());
429433
}
430-
self.write_mut_qualifier(mtbl);
431-
self.writer.write(name.as_bytes());
432-
self.writer.write(": ".as_bytes());
434+
if named {
435+
self.writer.write(name.as_bytes());
436+
self.writer.write(": ".as_bytes());
437+
}
433438
self.visit_inner(inner);
434439
true
435440
}
436441

437-
fn visit_leave_class(&mut self, _name: &str, n_fields: uint,
442+
fn visit_leave_class(&mut self, _name: &str, named_fields: bool, n_fields: uint,
438443
_sz: uint, _align: uint) -> bool {
439444
if n_fields != 0 {
440-
self.writer.write(['}' as u8]);
445+
if named_fields {
446+
self.writer.write(['}' as u8]);
447+
} else {
448+
self.writer.write([')' as u8]);
449+
}
441450
}
442451
true
443452
}
@@ -552,21 +561,31 @@ impl<'self> TyVisitor for ReprVisitor<'self> {
552561
}
553562

554563
fn visit_enter_fn(&mut self, _purity: uint, _proto: uint,
555-
_n_inputs: uint, _retstyle: uint) -> bool { true }
564+
_n_inputs: uint, _retstyle: uint) -> bool {
565+
self.writer.write("fn(".as_bytes());
566+
true
567+
}
556568

557569
fn visit_fn_input(&mut self, _i: uint, _mode: uint, _inner: *TyDesc) -> bool {
570+
// FIXME: #8917: should print out the parameter types here, separated by commas
558571
true
559572
}
560573

561574
fn visit_fn_output(&mut self, _retstyle: uint, _inner: *TyDesc) -> bool {
575+
self.writer.write(")".as_bytes());
576+
// FIXME: #8917: should print out the output type here, as `-> T`
562577
true
563578
}
564579

565580
fn visit_leave_fn(&mut self, _purity: uint, _proto: uint,
566581
_n_inputs: uint, _retstyle: uint) -> bool { true }
567582

568583

569-
fn visit_trait(&mut self) -> bool { true }
584+
fn visit_trait(&mut self, name: &str) -> bool {
585+
self.writer.write(name.as_bytes());
586+
true
587+
}
588+
570589
fn visit_param(&mut self, _i: uint) -> bool { true }
571590
fn visit_self(&mut self) -> bool { true }
572591
fn visit_type(&mut self) -> bool { true }
@@ -597,6 +616,7 @@ struct P {a: int, b: float}
597616

598617
#[test]
599618
fn test_repr() {
619+
use prelude::*;
600620
use str;
601621
use str::Str;
602622
use rt::io::Decorator;
@@ -654,6 +674,12 @@ fn test_repr() {
654674
exact_test(&(10u64, ~"hello"),
655675
"(10u64, ~\"hello\")");
656676

677+
exact_test(&(&println), "&fn()");
678+
exact_test(&(~5 as ~ToStr), "~to_str::ToStr:Send");
679+
657680
struct Foo;
658681
exact_test(&(~[Foo, Foo]), "~[repr::test_repr::Foo, repr::test_repr::Foo]");
682+
683+
struct Bar(int, int);
684+
exact_test(&(Bar(2, 2)), "repr::test_repr::Bar(2, 2)");
659685
}

src/libstd/unstable/intrinsics.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ pub trait TyVisitor {
220220
fn visit_leave_rec(&mut self, n_fields: uint,
221221
sz: uint, align: uint) -> bool;
222222

223-
fn visit_enter_class(&mut self, name: &str, n_fields: uint,
223+
fn visit_enter_class(&mut self, name: &str, named_fields: bool, n_fields: uint,
224224
sz: uint, align: uint) -> bool;
225-
fn visit_class_field(&mut self, i: uint, name: &str,
225+
fn visit_class_field(&mut self, i: uint, name: &str, named: bool,
226226
mtbl: uint, inner: *TyDesc) -> bool;
227-
fn visit_leave_class(&mut self, name: &str, n_fields: uint,
227+
fn visit_leave_class(&mut self, name: &str, named_fields: bool, n_fields: uint,
228228
sz: uint, align: uint) -> bool;
229229

230230
fn visit_enter_tup(&mut self, n_fields: uint,
@@ -256,7 +256,7 @@ pub trait TyVisitor {
256256
fn visit_leave_fn(&mut self, purity: uint, proto: uint,
257257
n_inputs: uint, retstyle: uint) -> bool;
258258

259-
fn visit_trait(&mut self) -> bool;
259+
fn visit_trait(&mut self, name: &str) -> bool;
260260
fn visit_param(&mut self, i: uint) -> bool;
261261
fn visit_self(&mut self) -> bool;
262262
fn visit_type(&mut self) -> bool;

src/libsyntax/parse/token.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ fn mk_fresh_ident_interner() -> @ident_interner {
436436
"blk", // 26
437437
"static", // 27
438438
"__foreign_mod__", // 28
439-
"__field__", // 29
439+
"<unnamed_field>", // 29
440440
"C", // 30
441441
"Self", // 31
442442

src/test/debug-info/tuple-struct.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434

3535

3636
// This test case mainly makes sure that no field names are generated for tuple structs (as opposed
37-
// to all fields having the name "__field__"). Otherwise they are handled the same a normal structs.
37+
// to all fields having the name "<unnamed_field>"). Otherwise they are handled the same a normal
38+
// structs.
3839

3940
struct NoPadding16(u16, i16);
4041
struct NoPadding32(i32, f32, u32);

src/test/run-pass/reflect-visit-data.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -317,26 +317,26 @@ impl<V:TyVisitor + movable_ptr> TyVisitor for ptr_visit_adaptor<V> {
317317
true
318318
}
319319

320-
fn visit_enter_class(&mut self, name: &str, n_fields: uint, sz: uint, align: uint)
321-
-> bool {
320+
fn visit_enter_class(&mut self, name: &str, named_fields: bool, n_fields: uint, sz: uint,
321+
align: uint) -> bool {
322322
self.align(align);
323-
if ! self.inner.visit_enter_class(name, n_fields, sz, align) {
323+
if ! self.inner.visit_enter_class(name, named_fields, n_fields, sz, align) {
324324
return false;
325325
}
326326
true
327327
}
328328

329-
fn visit_class_field(&mut self, i: uint, name: &str,
329+
fn visit_class_field(&mut self, i: uint, name: &str, named: bool,
330330
mtbl: uint, inner: *TyDesc) -> bool {
331-
if ! self.inner.visit_class_field(i, name, mtbl, inner) {
331+
if ! self.inner.visit_class_field(i, name, named, mtbl, inner) {
332332
return false;
333333
}
334334
true
335335
}
336336

337-
fn visit_leave_class(&mut self, name: &str, n_fields: uint, sz: uint, align: uint)
338-
-> bool {
339-
if ! self.inner.visit_leave_class(name, n_fields, sz, align) {
337+
fn visit_leave_class(&mut self, name: &str, named_fields: bool, n_fields: uint, sz: uint,
338+
align: uint) -> bool {
339+
if ! self.inner.visit_leave_class(name, named_fields, n_fields, sz, align) {
340340
return false;
341341
}
342342
true
@@ -428,9 +428,9 @@ impl<V:TyVisitor + movable_ptr> TyVisitor for ptr_visit_adaptor<V> {
428428
true
429429
}
430430

431-
fn visit_trait(&mut self) -> bool {
431+
fn visit_trait(&mut self, name: &str) -> bool {
432432
self.align_to::<@TyVisitor>();
433-
if ! self.inner.visit_trait() { return false; }
433+
if ! self.inner.visit_trait(name) { return false; }
434434
self.bump_past::<@TyVisitor>();
435435
true
436436
}
@@ -565,13 +565,13 @@ impl TyVisitor for my_visitor {
565565
fn visit_leave_rec(&mut self, _n_fields: uint,
566566
_sz: uint, _align: uint) -> bool { true }
567567

568-
fn visit_enter_class(&mut self, _name: &str, _n_fields: uint,
568+
fn visit_enter_class(&mut self, _name: &str, _named_fields: bool, _n_fields: uint,
569569
_sz: uint, _align: uint) -> bool { true }
570-
fn visit_class_field(&mut self, _i: uint, _name: &str,
570+
fn visit_class_field(&mut self, _i: uint, _name: &str, _named: bool,
571571
_mtbl: uint, inner: *TyDesc) -> bool {
572572
self.visit_inner(inner)
573573
}
574-
fn visit_leave_class(&mut self, _name: &str, _n_fields: uint,
574+
fn visit_leave_class(&mut self, _name: &str, _named_fields: bool, _n_fields: uint,
575575
_sz: uint, _align: uint) -> bool { true }
576576

577577
fn visit_enter_tup(&mut self, _n_fields: uint,
@@ -616,7 +616,7 @@ impl TyVisitor for my_visitor {
616616
_n_inputs: uint, _retstyle: uint) -> bool { true }
617617

618618

619-
fn visit_trait(&mut self) -> bool { true }
619+
fn visit_trait(&mut self, _name: &str) -> bool { true }
620620
fn visit_param(&mut self, _i: uint) -> bool { true }
621621
fn visit_self(&mut self) -> bool { true }
622622
fn visit_type(&mut self) -> bool { true }

src/test/run-pass/reflect-visit-type.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ impl TyVisitor for MyVisitor {
9999
fn visit_leave_rec(&mut self, _n_fields: uint,
100100
_sz: uint, _align: uint) -> bool { true }
101101

102-
fn visit_enter_class(&mut self, _name: &str, _n_fields: uint,
102+
fn visit_enter_class(&mut self, _name: &str, _named_fields: bool, _n_fields: uint,
103103
_sz: uint, _align: uint) -> bool { true }
104-
fn visit_class_field(&mut self, _i: uint, _name: &str,
104+
fn visit_class_field(&mut self, _i: uint, _name: &str, _named: bool,
105105
_mtbl: uint, _inner: *TyDesc) -> bool { true }
106-
fn visit_leave_class(&mut self, _name: &str, _n_fields: uint,
106+
fn visit_leave_class(&mut self, _name: &str, _named_fields: bool, _n_fields: uint,
107107
_sz: uint, _align: uint) -> bool { true }
108108

109109
fn visit_enter_tup(&mut self, _n_fields: uint,
@@ -139,7 +139,7 @@ impl TyVisitor for MyVisitor {
139139
_n_inputs: uint, _retstyle: uint) -> bool { true }
140140

141141

142-
fn visit_trait(&mut self) -> bool { true }
142+
fn visit_trait(&mut self, _name: &str) -> bool { true }
143143
fn visit_param(&mut self, _i: uint) -> bool { true }
144144
fn visit_self(&mut self) -> bool { true }
145145
fn visit_type(&mut self) -> bool { true }

0 commit comments

Comments
 (0)