/
generic.rs
1274 lines (1116 loc) · 45.7 KB
/
generic.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
/*!
Some code that abstracts away much of the boilerplate of writing
`deriving` instances for traits. Among other things it manages getting
access to the fields of the 4 different sorts of structs and enum
variants, as well as creating the method and impl ast instances.
Supported features (fairly exhaustive):
- Methods taking any number of parameters of any type, and returning
any type, other than vectors, bottom and closures.
- Generating `impl`s for types with type parameters and lifetimes
(e.g. `Option<T>`), the parameters are automatically given the
current trait as a bound. (This includes separate type parameters
and lifetimes for methods.)
- Additional bounds on the type parameters, e.g. the `Ord` instance
requires an explicit `Eq` bound at the
moment. (`TraitDef.additional_bounds`)
Unsupported: FIXME #6257: calling methods on reference fields,
e.g. deriving TotalEq/TotalOrd/Clone don't work on `struct A(&int)`,
because of how the auto-dereferencing happens.
The most important thing for implementers is the `Substructure` and
`SubstructureFields` objects. The latter groups 5 possibilities of the
arguments:
- `Struct`, when `Self` is a struct (including tuple structs, e.g
`struct T(int, char)`).
- `EnumMatching`, when `Self` is an enum and all the arguments are the
same variant of the enum (e.g. `Some(1)`, `Some(3)` and `Some(4)`)
- `EnumNonMatching` when `Self` is an enum and the arguments are not
the same variant (e.g. `None`, `Some(1)` and `None`). If
`const_nonmatching` is true, this will contain an empty list.
- `StaticEnum` and `StaticStruct` for static methods, where the type
being derived upon is either an enum or struct respectively. (Any
argument with type Self is just grouped among the non-self
arguments.)
In the first two cases, the values from the corresponding fields in
all the arguments are grouped together. In the `EnumNonMatching` case
this isn't possible (different variants have different fields), so the
fields are grouped by which argument they come from. There are no
fields with values in the static cases, so these are treated entirely
differently.
The non-static cases have `Option<ident>` in several places associated
with field `expr`s. This represents the name of the field it is
associated with. It is only not `None` when the associated field has
an identifier in the source code. For example, the `x`s in the
following snippet
~~~
struct A { x : int }
struct B(int);
enum C {
C0(int),
C1 { x: int }
}
~~~
The `int`s in `B` and `C0` don't have an identifier, so the
`Option<ident>`s would be `None` for them.
In the static cases, the structure is summarised, either into the just
spans of the fields or a list of spans and the field idents (for tuple
structs and record structs, respectively), or a list of these, for
enums (one for each variant). For empty struct and empty enum
variants, it is represented as a count of 0.
# Examples
The following simplified `Eq` is used for in-code examples:
~~~
trait Eq {
fn eq(&self, other: &Self);
}
impl Eq for int {
fn eq(&self, other: &int) -> bool {
*self == *other
}
}
~~~
Some examples of the values of `SubstructureFields` follow, using the
above `Eq`, `A`, `B` and `C`.
## Structs
When generating the `expr` for the `A` impl, the `SubstructureFields` is
~~~
Struct(~[FieldInfo {
span: <span of x>
name: Some(<ident of x>),
self_: <expr for &self.x>,
other: ~[<expr for &other.x]
}])
~~~
For the `B` impl, called with `B(a)` and `B(b)`,
~~~
Struct(~[FieldInfo {
span: <span of `int`>,
name: None,
<expr for &a>
~[<expr for &b>]
}])
~~~
## Enums
When generating the `expr` for a call with `self == C0(a)` and `other
== C0(b)`, the SubstructureFields is
~~~
EnumMatching(0, <ast::Variant for C0>,
~[FieldInfo {
span: <span of int>
name: None,
self_: <expr for &a>,
other: ~[<expr for &b>]
}])
~~~
For `C1 {x}` and `C1 {x}`,
~~~
EnumMatching(1, <ast::Variant for C1>,
~[FieldInfo {
span: <span of x>
name: Some(<ident of x>),
self_: <expr for &self.x>,
other: ~[<expr for &other.x>]
}])
~~~
For `C0(a)` and `C1 {x}` ,
~~~
EnumNonMatching(~[(0, <ast::Variant for B0>,
~[(<span of int>, None, <expr for &a>)]),
(1, <ast::Variant for B1>,
~[(<span of x>, Some(<ident of x>),
<expr for &other.x>)])])
~~~
(and vice versa, but with the order of the outermost list flipped.)
## Static
A static method on the above would result in,
~~~~
StaticStruct(<ast::StructDef of A>, Named(~[(<ident of x>, <span of x>)]))
StaticStruct(<ast::StructDef of B>, Unnamed(~[<span of x>]))
StaticEnum(<ast::EnumDef of C>, ~[(<ident of C0>, <span of C0>, Unnamed(~[<span of int>])),
(<ident of C1>, <span of C1>,
Named(~[(<ident of x>, <span of x>)]))])
~~~
*/
use ast;
use ast::{P, EnumDef, Expr, Ident, Generics, StructDef};
use ext::base::ExtCtxt;
use ext::build::AstBuilder;
use codemap;
use codemap::Span;
use opt_vec;
use parse::token::InternedString;
use parse::token;
use std::vec;
pub use self::ty::*;
mod ty;
pub struct TraitDef<'a> {
/// The span for the current #[deriving(Foo)] header.
span: Span,
/// Path of the trait, including any type parameters
path: Path<'a>,
/// Additional bounds required of any type parameters of the type,
/// other than the current trait
additional_bounds: ~[Ty<'a>],
/// Any extra lifetimes and/or bounds, e.g. `D: serialize::Decoder`
generics: LifetimeBounds<'a>,
methods: ~[MethodDef<'a>]
}
pub struct MethodDef<'a> {
/// name of the method
name: &'a str,
/// List of generics, e.g. `R: std::rand::Rng`
generics: LifetimeBounds<'a>,
/// Whether there is a self argument (outer Option) i.e. whether
/// this is a static function, and whether it is a pointer (inner
/// Option)
explicit_self: Option<Option<PtrTy<'a>>>,
/// Arguments other than the self argument
args: ~[Ty<'a>],
/// Return type
ret_ty: Ty<'a>,
/// Whether to mark this as #[inline]
inline: bool,
/// if the value of the nonmatching enums is independent of the
/// actual enum variants, i.e. can use _ => .. match.
const_nonmatching: bool,
combine_substructure: CombineSubstructureFunc<'a>
}
/// All the data about the data structure/method being derived upon.
pub struct Substructure<'a> {
/// ident of self
type_ident: Ident,
/// ident of the method
method_ident: Ident,
/// dereferenced access to any Self or Ptr(Self, _) arguments
self_args: &'a [@Expr],
/// verbatim access to any other arguments
nonself_args: &'a [@Expr],
fields: &'a SubstructureFields<'a>
}
/// Summary of the relevant parts of a struct/enum field.
pub struct FieldInfo {
span: Span,
/// None for tuple structs/normal enum variants, Some for normal
/// structs/struct enum variants.
name: Option<Ident>,
/// The expression corresponding to this field of `self`
/// (specifically, a reference to it).
self_: @Expr,
/// The expressions corresponding to references to this field in
/// the other Self arguments.
other: ~[@Expr]
}
/// Fields for a static method
pub enum StaticFields {
/// Tuple structs/enum variants like this
Unnamed(~[Span]),
/// Normal structs/struct variants.
Named(~[(Ident, Span)])
}
/// A summary of the possible sets of fields. See above for details
/// and examples
pub enum SubstructureFields<'a> {
Struct(~[FieldInfo]),
/**
Matching variants of the enum: variant index, ast::Variant,
fields: the field name is only non-`None` in the case of a struct
variant.
*/
EnumMatching(uint, &'a ast::Variant, ~[FieldInfo]),
/**
non-matching variants of the enum, [(variant index, ast::Variant,
[field span, field ident, fields])] (i.e. all fields for self are in the
first tuple, for other1 are in the second tuple, etc.)
*/
EnumNonMatching(&'a [(uint, P<ast::Variant>, ~[(Span, Option<Ident>, @Expr)])]),
/// A static method where Self is a struct.
StaticStruct(&'a ast::StructDef, StaticFields),
/// A static method where Self is an enum.
StaticEnum(&'a ast::EnumDef, ~[(Ident, Span, StaticFields)])
}
/**
Combine the values of all the fields together. The last argument is
all the fields of all the structures, see above for details.
*/
pub type CombineSubstructureFunc<'a> =
'a |&mut ExtCtxt, Span, &Substructure| -> @Expr;
/**
Deal with non-matching enum variants, the arguments are a list
representing each variant: (variant index, ast::Variant instance,
[variant fields]), and a list of the nonself args of the type
*/
pub type EnumNonMatchFunc<'a> =
'a |&mut ExtCtxt,
Span,
&[(uint, P<ast::Variant>, ~[(Span, Option<Ident>, @Expr)])],
&[@Expr]|
-> @Expr;
impl<'a> TraitDef<'a> {
pub fn expand(&self,
cx: &mut ExtCtxt,
_mitem: @ast::MetaItem,
in_items: ~[@ast::Item]) -> ~[@ast::Item] {
let mut result = ~[];
for item in in_items.iter() {
result.push(*item);
match item.node {
ast::ItemStruct(struct_def, ref generics) => {
result.push(self.expand_struct_def(cx,
struct_def,
item.ident,
generics));
}
ast::ItemEnum(ref enum_def, ref generics) => {
result.push(self.expand_enum_def(cx,
enum_def,
item.ident,
generics));
}
_ => ()
}
}
result
}
/**
*
* Given that we are deriving a trait `Tr` for a type `T<'a, ...,
* 'z, A, ..., Z>`, creates an impl like:
*
* impl<'a, ..., 'z, A:Tr B1 B2, ..., Z: Tr B1 B2> Tr for T<A, ..., Z> { ... }
*
* where B1, B2, ... are the bounds given by `bounds_paths`.'
*
*/
fn create_derived_impl(&self,
cx: &mut ExtCtxt,
type_ident: Ident, generics: &Generics,
methods: ~[@ast::Method]) -> @ast::Item {
let trait_path = self.path.to_path(cx, self.span, type_ident, generics);
let mut trait_generics = self.generics.to_generics(cx, self.span,
type_ident, generics);
// Copy the lifetimes
for l in generics.lifetimes.iter() {
trait_generics.lifetimes.push(*l)
};
// Create the type parameters.
for ty_param in generics.ty_params.iter() {
// I don't think this can be moved out of the loop, since
// a TyParamBound requires an ast id
let mut bounds = opt_vec::from(
// extra restrictions on the generics parameters to the type being derived upon
self.additional_bounds.map(|p| {
cx.typarambound(p.to_path(cx, self.span,
type_ident, generics))
}));
// require the current trait
bounds.push(cx.typarambound(trait_path.clone()));
trait_generics.ty_params.push(cx.typaram(ty_param.ident, bounds, None));
}
// Create the reference to the trait.
let trait_ref = cx.trait_ref(trait_path);
// Create the type parameters on the `self` path.
let self_ty_params = generics.ty_params.map(|ty_param| {
cx.ty_ident(self.span, ty_param.ident)
});
let self_lifetimes = generics.lifetimes.clone();
// Create the type of `self`.
let self_type = cx.ty_path(
cx.path_all(self.span, false, ~[ type_ident ], self_lifetimes,
opt_vec::take_vec(self_ty_params)), None);
let doc_attr = cx.attribute(
self.span,
cx.meta_name_value(self.span,
InternedString::new("doc"),
ast::LitStr(token::intern_and_get_ident(
"Automatically derived."),
ast::CookedStr)));
cx.item(
self.span,
::parse::token::special_idents::clownshoes_extensions,
~[doc_attr],
ast::ItemImpl(trait_generics, Some(trait_ref),
self_type, methods.map(|x| *x)))
}
fn expand_struct_def(&self,
cx: &mut ExtCtxt,
struct_def: &StructDef,
type_ident: Ident,
generics: &Generics) -> @ast::Item {
let methods = self.methods.map(|method_def| {
let (explicit_self, self_args, nonself_args, tys) =
method_def.split_self_nonself_args(
cx, self, type_ident, generics);
let body = if method_def.is_static() {
method_def.expand_static_struct_method_body(
cx,
self,
struct_def,
type_ident,
self_args, nonself_args)
} else {
method_def.expand_struct_method_body(cx,
self,
struct_def,
type_ident,
self_args, nonself_args)
};
method_def.create_method(cx, self,
type_ident, generics,
explicit_self, tys,
body)
});
self.create_derived_impl(cx, type_ident, generics, methods)
}
fn expand_enum_def(&self,
cx: &mut ExtCtxt,
enum_def: &EnumDef,
type_ident: Ident,
generics: &Generics) -> @ast::Item {
let methods = self.methods.map(|method_def| {
let (explicit_self, self_args, nonself_args, tys) =
method_def.split_self_nonself_args(cx, self,
type_ident, generics);
let body = if method_def.is_static() {
method_def.expand_static_enum_method_body(
cx,
self,
enum_def,
type_ident,
self_args, nonself_args)
} else {
method_def.expand_enum_method_body(cx,
self,
enum_def,
type_ident,
self_args, nonself_args)
};
method_def.create_method(cx, self,
type_ident, generics,
explicit_self, tys,
body)
});
self.create_derived_impl(cx, type_ident, generics, methods)
}
}
impl<'a> MethodDef<'a> {
fn call_substructure_method(&self,
cx: &mut ExtCtxt,
trait_: &TraitDef,
type_ident: Ident,
self_args: &[@Expr],
nonself_args: &[@Expr],
fields: &SubstructureFields)
-> @Expr {
let substructure = Substructure {
type_ident: type_ident,
method_ident: cx.ident_of(self.name),
self_args: self_args,
nonself_args: nonself_args,
fields: fields
};
(self.combine_substructure)(cx, trait_.span,
&substructure)
}
fn get_ret_ty(&self,
cx: &mut ExtCtxt,
trait_: &TraitDef,
generics: &Generics,
type_ident: Ident)
-> P<ast::Ty> {
self.ret_ty.to_ty(cx, trait_.span, type_ident, generics)
}
fn is_static(&self) -> bool {
self.explicit_self.is_none()
}
fn split_self_nonself_args(&self,
cx: &mut ExtCtxt,
trait_: &TraitDef,
type_ident: Ident,
generics: &Generics)
-> (ast::ExplicitSelf, ~[@Expr], ~[@Expr], ~[(Ident, P<ast::Ty>)]) {
let mut self_args = ~[];
let mut nonself_args = ~[];
let mut arg_tys = ~[];
let mut nonstatic = false;
let ast_explicit_self = match self.explicit_self {
Some(ref self_ptr) => {
let (self_expr, explicit_self) =
ty::get_explicit_self(cx, trait_.span, self_ptr);
self_args.push(self_expr);
nonstatic = true;
explicit_self
}
None => codemap::respan(trait_.span, ast::SelfStatic),
};
for (i, ty) in self.args.iter().enumerate() {
let ast_ty = ty.to_ty(cx, trait_.span, type_ident, generics);
let ident = cx.ident_of(format!("__arg_{}", i));
arg_tys.push((ident, ast_ty));
let arg_expr = cx.expr_ident(trait_.span, ident);
match *ty {
// for static methods, just treat any Self
// arguments as a normal arg
Self if nonstatic => {
self_args.push(arg_expr);
}
Ptr(~Self, _) if nonstatic => {
self_args.push(cx.expr_deref(trait_.span, arg_expr))
}
_ => {
nonself_args.push(arg_expr);
}
}
}
(ast_explicit_self, self_args, nonself_args, arg_tys)
}
fn create_method(&self,
cx: &mut ExtCtxt,
trait_: &TraitDef,
type_ident: Ident,
generics: &Generics,
explicit_self: ast::ExplicitSelf,
arg_types: ~[(Ident, P<ast::Ty>)],
body: @Expr) -> @ast::Method {
// create the generics that aren't for Self
let fn_generics = self.generics.to_generics(cx, trait_.span, type_ident, generics);
let self_arg = match explicit_self.node {
ast::SelfStatic => None,
_ => Some(ast::Arg::new_self(trait_.span, ast::MutImmutable))
};
let args = {
let args = arg_types.move_iter().map(|(name, ty)| {
cx.arg(trait_.span, name, ty)
});
self_arg.move_iter().chain(args).collect()
};
let ret_type = self.get_ret_ty(cx, trait_, generics, type_ident);
let method_ident = cx.ident_of(self.name);
let fn_decl = cx.fn_decl(args, ret_type);
let body_block = cx.block_expr(body);
let attrs = if self.inline {
~[
cx
.attribute(trait_.span,
cx
.meta_word(trait_.span,
InternedString::new(
"inline")))
]
} else {
~[]
};
// Create the method.
@ast::Method {
ident: method_ident,
attrs: attrs,
generics: fn_generics,
explicit_self: explicit_self,
purity: ast::ImpureFn,
decl: fn_decl,
body: body_block,
id: ast::DUMMY_NODE_ID,
span: trait_.span,
vis: ast::Inherited,
}
}
/**
~~~
#[deriving(Eq)]
struct A { x: int, y: int }
// equivalent to:
impl Eq for A {
fn eq(&self, __arg_1: &A) -> bool {
match *self {
A {x: ref __self_0_0, y: ref __self_0_1} => {
match *__arg_1 {
A {x: ref __self_1_0, y: ref __self_1_1} => {
__self_0_0.eq(__self_1_0) && __self_0_1.eq(__self_1_1)
}
}
}
}
}
}
~~~
*/
fn expand_struct_method_body(&self,
cx: &mut ExtCtxt,
trait_: &TraitDef,
struct_def: &StructDef,
type_ident: Ident,
self_args: &[@Expr],
nonself_args: &[@Expr])
-> @Expr {
let mut raw_fields = ~[]; // ~[[fields of self],
// [fields of next Self arg], [etc]]
let mut patterns = ~[];
for i in range(0u, self_args.len()) {
let (pat, ident_expr) = trait_.create_struct_pattern(cx, type_ident, struct_def,
format!("__self_{}", i),
ast::MutImmutable);
patterns.push(pat);
raw_fields.push(ident_expr);
}
// transpose raw_fields
let fields = match raw_fields {
[ref self_arg, .. rest] => {
self_arg.iter().enumerate().map(|(i, &(span, opt_id, field))| {
let other_fields = rest.map(|l| {
match &l[i] {
&(_, _, ex) => ex
}
});
FieldInfo {
span: span,
name: opt_id,
self_: field,
other: other_fields
}
}).collect()
}
[] => { cx.span_bug(trait_.span,
"no self arguments to non-static method \
in generic `deriving`") }
};
// body of the inner most destructuring match
let mut body = self.call_substructure_method(
cx,
trait_,
type_ident,
self_args,
nonself_args,
&Struct(fields));
// make a series of nested matches, to destructure the
// structs. This is actually right-to-left, but it shoudn't
// matter.
for (&arg_expr, &pat) in self_args.iter().zip(patterns.iter()) {
body = cx.expr_match(trait_.span, arg_expr,
~[ cx.arm(trait_.span, ~[pat], body) ])
}
body
}
fn expand_static_struct_method_body(&self,
cx: &mut ExtCtxt,
trait_: &TraitDef,
struct_def: &StructDef,
type_ident: Ident,
self_args: &[@Expr],
nonself_args: &[@Expr])
-> @Expr {
let summary = trait_.summarise_struct(cx, struct_def);
self.call_substructure_method(cx,
trait_,
type_ident,
self_args, nonself_args,
&StaticStruct(struct_def, summary))
}
/**
~~~
#[deriving(Eq)]
enum A {
A1
A2(int)
}
// is equivalent to (with const_nonmatching == false)
impl Eq for A {
fn eq(&self, __arg_1: &A) {
match *self {
A1 => match *__arg_1 {
A1 => true
A2(ref __arg_1_1) => false
},
A2(self_1) => match *__arg_1 {
A1 => false,
A2(ref __arg_1_1) => self_1.eq(__arg_1_1)
}
}
}
}
~~~
*/
fn expand_enum_method_body(&self,
cx: &mut ExtCtxt,
trait_: &TraitDef,
enum_def: &EnumDef,
type_ident: Ident,
self_args: &[@Expr],
nonself_args: &[@Expr])
-> @Expr {
let mut matches = ~[];
self.build_enum_match(cx, trait_, enum_def, type_ident,
self_args, nonself_args,
None, &mut matches, 0)
}
/**
Creates the nested matches for an enum definition recursively, i.e.
~~~
match self {
Variant1 => match other { Variant1 => matching, Variant2 => nonmatching, ... },
Variant2 => match other { Variant1 => nonmatching, Variant2 => matching, ... },
...
}
~~~
It acts in the most naive way, so every branch (and subbranch,
subsubbranch, etc) exists, not just the ones where all the variants in
the tree are the same. Hopefully the optimisers get rid of any
repetition, otherwise derived methods with many Self arguments will be
exponentially large.
`matching` is Some(n) if all branches in the tree above the
current position are variant `n`, `None` otherwise (including on
the first call).
*/
fn build_enum_match(&self,
cx: &mut ExtCtxt,
trait_: &TraitDef,
enum_def: &EnumDef,
type_ident: Ident,
self_args: &[@Expr],
nonself_args: &[@Expr],
matching: Option<uint>,
matches_so_far: &mut ~[(uint, P<ast::Variant>,
~[(Span, Option<Ident>, @Expr)])],
match_count: uint) -> @Expr {
if match_count == self_args.len() {
// we've matched against all arguments, so make the final
// expression at the bottom of the match tree
if matches_so_far.len() == 0 {
cx.span_bug(trait_.span,
"no self match on an enum in \
generic `deriving`");
}
// we currently have a vec of vecs, where each
// subvec is the fields of one of the arguments,
// but if the variants all match, we want this as
// vec of tuples, where each tuple represents a
// field.
let substructure;
// most arms don't have matching variants, so do a
// quick check to see if they match (even though
// this means iterating twice) instead of being
// optimistic and doing a pile of allocations etc.
match matching {
Some(variant_index) => {
// `ref` inside let matches is buggy. Causes havoc wih rusc.
// let (variant_index, ref self_vec) = matches_so_far[0];
let (variant, self_vec) = match matches_so_far[0] {
(_, v, ref s) => (v, s)
};
let mut enum_matching_fields = vec::from_elem(self_vec.len(), ~[]);
for triple in matches_so_far.tail().iter() {
match triple {
&(_, _, ref other_fields) => {
for (i, &(_, _, e)) in other_fields.iter().enumerate() {
enum_matching_fields[i].push(e);
}
}
}
}
let field_tuples =
self_vec.iter()
.zip(enum_matching_fields.iter())
.map(|(&(span, id, self_f), other)| {
FieldInfo {
span: span,
name: id,
self_: self_f,
other: (*other).clone()
}
}).collect();
substructure = EnumMatching(variant_index, variant, field_tuples);
}
None => {
substructure = EnumNonMatching(*matches_so_far);
}
}
self.call_substructure_method(cx, trait_, type_ident,
self_args, nonself_args,
&substructure)
} else { // there are still matches to create
let current_match_str = if match_count == 0 {
~"__self"
} else {
format!("__arg_{}", match_count)
};
let mut arms = ~[];
// the code for nonmatching variants only matters when
// we've seen at least one other variant already
if self.const_nonmatching && match_count > 0 {
// make a matching-variant match, and a _ match.
let index = match matching {
Some(i) => i,
None => cx.span_bug(trait_.span,
"non-matching variants when required to \
be matching in generic `deriving`")
};
// matching-variant match
let variant = enum_def.variants[index];
let (pattern, idents) = trait_.create_enum_variant_pattern(cx,
variant,
current_match_str,
ast::MutImmutable);
matches_so_far.push((index, variant, idents));
let arm_expr = self.build_enum_match(cx,
trait_,
enum_def,
type_ident,
self_args, nonself_args,
matching,
matches_so_far,
match_count + 1);
matches_so_far.pop().unwrap();
arms.push(cx.arm(trait_.span, ~[ pattern ], arm_expr));
if enum_def.variants.len() > 1 {
let e = &EnumNonMatching(&[]);
let wild_expr = self.call_substructure_method(cx, trait_, type_ident,
self_args, nonself_args,
e);
let wild_arm = cx.arm(
trait_.span,
~[ cx.pat_wild(trait_.span) ],
wild_expr);
arms.push(wild_arm);
}
} else {
// create an arm matching on each variant
for (index, &variant) in enum_def.variants.iter().enumerate() {
let (pattern, idents) = trait_.create_enum_variant_pattern(cx,
variant,
current_match_str,
ast::MutImmutable);
matches_so_far.push((index, variant, idents));
let new_matching =
match matching {
_ if match_count == 0 => Some(index),
Some(i) if index == i => Some(i),
_ => None
};
let arm_expr = self.build_enum_match(cx,
trait_,
enum_def,
type_ident,
self_args, nonself_args,
new_matching,
matches_so_far,
match_count + 1);
matches_so_far.pop().unwrap();
let arm = cx.arm(trait_.span, ~[ pattern ], arm_expr);
arms.push(arm);
}
}
// match foo { arm, arm, arm, ... }
cx.expr_match(trait_.span, self_args[match_count], arms)
}
}
fn expand_static_enum_method_body(&self,
cx: &mut ExtCtxt,
trait_: &TraitDef,
enum_def: &EnumDef,
type_ident: Ident,
self_args: &[@Expr],
nonself_args: &[@Expr])
-> @Expr {
let summary = enum_def.variants.map(|v| {
let ident = v.node.name;
let summary = match v.node.kind {
ast::TupleVariantKind(ref args) => {
Unnamed(args.map(|va| trait_.set_expn_info(cx, va.ty.span)))
}
ast::StructVariantKind(struct_def) => {
trait_.summarise_struct(cx, struct_def)
}
};
(ident, v.span, summary)
});
self.call_substructure_method(cx, trait_, type_ident,
self_args, nonself_args,
&StaticEnum(enum_def, summary))
}
}
#[deriving(Eq)] // dogfooding!
enum StructType {
Unknown, Record, Tuple
}
// general helper methods.
impl<'a> TraitDef<'a> {
fn set_expn_info(&self,
cx: &mut ExtCtxt,
mut to_set: Span) -> Span {
let trait_name = match self.path.path.last() {
None => cx.span_bug(self.span, "trait with empty path in generic `deriving`"),
Some(name) => *name
};
to_set.expn_info = Some(@codemap::ExpnInfo {
call_site: to_set,
callee: codemap::NameAndSpan {
name: format!("deriving({})", trait_name),
format: codemap::MacroAttribute,
span: Some(self.span)
}
});
to_set
}
fn summarise_struct(&self,
cx: &mut ExtCtxt,
struct_def: &StructDef) -> StaticFields {
let mut named_idents = ~[];
let mut just_spans = ~[];
for field in struct_def.fields.iter(){
let sp = self.set_expn_info(cx, field.span);
match field.node.kind {
ast::NamedField(ident, _) => named_idents.push((ident, sp)),