about summary refs log tree commit diff
path: root/src/libsyntax/ast.rs
blob: 46fd7be656e7baeb6ca2287e42c943b350f83f64 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
// Copyright 2012 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.

// The Rust abstract syntax tree.

use std::serialization::{Serializable,
                         Deserializable,
                         Serializer,
                         Deserializer};
use codemap::{span, FileName};
use parse::token;

#[auto_serialize]
#[auto_deserialize]
type spanned<T> = {node: T, span: span};


/* can't import macros yet, so this is copied from token.rs. See its comment
 * there. */
macro_rules! interner_key (
    () => (cast::transmute::<(uint, uint), &fn(+v: @@token::ident_interner)>(
        (-3 as uint, 0u)))
)

// FIXME(#3534): Replace with the struct-based newtype when it's been
// implemented.
struct ident { repr: uint }

impl<S: Serializer> ident: Serializable<S> {
    fn serialize(&self, s: &S) {
        let intr = match unsafe {
            task::local_data::local_data_get(interner_key!())
        } {
            None => fail ~"serialization: TLS interner not set up",
            Some(intr) => intr
        };

        s.emit_owned_str(*(*intr).get(*self));
    }
}

impl<D: Deserializer> ident: Deserializable<D> {
    static fn deserialize(d: &D) -> ident {
        let intr = match unsafe {
            task::local_data::local_data_get(interner_key!())
        } {
            None => fail ~"deserialization: TLS interner not set up",
            Some(intr) => intr
        };

        (*intr).intern(@d.read_owned_str())
    }
}

impl ident: cmp::Eq {
    pure fn eq(&self, other: &ident) -> bool { (*self).repr == other.repr }
    pure fn ne(&self, other: &ident) -> bool { !(*self).eq(other) }
}

impl ident: to_bytes::IterBytes {
    pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
        self.repr.iter_bytes(lsb0, f)
    }
}

// Functions may or may not have names.
type fn_ident = Option<ident>;

#[auto_serialize]
#[auto_deserialize]
type path = {span: span,
             global: bool,
             idents: ~[ident],
             rp: Option<@region>,
             types: ~[@Ty]};

type crate_num = int;

type node_id = int;

#[auto_serialize]
#[auto_deserialize]
type def_id = {crate: crate_num, node: node_id};

impl def_id : cmp::Eq {
    pure fn eq(&self, other: &def_id) -> bool {
        (*self).crate == (*other).crate && (*self).node == (*other).node
    }
    pure fn ne(&self, other: &def_id) -> bool { !(*self).eq(other) }
}

const local_crate: crate_num = 0;
const crate_node_id: node_id = 0;

#[auto_serialize]
#[auto_deserialize]
// The AST represents all type param bounds as types.
// typeck::collect::compute_bounds matches these against
// the "special" built-in traits (see middle::lang_items) and
// detects Copy, Send, Owned, and Const.
enum ty_param_bound = @Ty;

#[auto_serialize]
#[auto_deserialize]
type ty_param = {ident: ident, id: node_id, bounds: @~[ty_param_bound]};

#[auto_serialize]
#[auto_deserialize]
enum def {
    def_fn(def_id, purity),
    def_static_method(/* method */ def_id,
                      /* trait */  Option<def_id>,
                      purity),
    def_self(node_id),
    def_mod(def_id),
    def_foreign_mod(def_id),
    def_const(def_id),
    def_arg(node_id, mode),
    def_local(node_id, bool /* is_mutbl */),
    def_variant(def_id /* enum */, def_id /* variant */),
    def_ty(def_id),
    def_prim_ty(prim_ty),
    def_ty_param(def_id, uint),
    def_binding(node_id, binding_mode),
    def_use(def_id),
    def_upvar(node_id,  // id of closed over var
              @def,     // closed over def
              node_id,  // expr node that creates the closure
              node_id), // id for the block/body of the closure expr
    def_class(def_id),
    def_typaram_binder(node_id), /* class, impl or trait that has ty params */
    def_region(node_id),
    def_label(node_id)
}

impl def : cmp::Eq {
    pure fn eq(&self, other: &def) -> bool {
        match (*self) {
            def_fn(e0a, e1a) => {
                match (*other) {
                    def_fn(e0b, e1b) => e0a == e0b && e1a == e1b,
                    _ => false
                }
            }
            def_static_method(e0a, e1a, e2a) => {
                match (*other) {
                    def_static_method(e0b, e1b, e2b) =>
                    e0a == e0b && e1a == e1b && e2a == e2b,
                    _ => false
                }
            }
            def_self(e0a) => {
                match (*other) {
                    def_self(e0b) => e0a == e0b,
                    _ => false
                }
            }
            def_mod(e0a) => {
                match (*other) {
                    def_mod(e0b) => e0a == e0b,
                    _ => false
                }
            }
            def_foreign_mod(e0a) => {
                match (*other) {
                    def_foreign_mod(e0b) => e0a == e0b,
                    _ => false
                }
            }
            def_const(e0a) => {
                match (*other) {
                    def_const(e0b) => e0a == e0b,
                    _ => false
                }
            }
            def_arg(e0a, e1a) => {
                match (*other) {
                    def_arg(e0b, e1b) => e0a == e0b && e1a == e1b,
                    _ => false
                }
            }
            def_local(e0a, e1a) => {
                match (*other) {
                    def_local(e0b, e1b) => e0a == e0b && e1a == e1b,
                    _ => false
                }
            }
            def_variant(e0a, e1a) => {
                match (*other) {
                    def_variant(e0b, e1b) => e0a == e0b && e1a == e1b,
                    _ => false
                }
            }
            def_ty(e0a) => {
                match (*other) {
                    def_ty(e0b) => e0a == e0b,
                    _ => false
                }
            }
            def_prim_ty(e0a) => {
                match (*other) {
                    def_prim_ty(e0b) => e0a == e0b,
                    _ => false
                }
            }
            def_ty_param(e0a, e1a) => {
                match (*other) {
                    def_ty_param(e0b, e1b) => e0a == e0b && e1a == e1b,
                    _ => false
                }
            }
            def_binding(e0a, e1a) => {
                match (*other) {
                    def_binding(e0b, e1b) => e0a == e0b && e1a == e1b,
                    _ => false
                }
            }
            def_use(e0a) => {
                match (*other) {
                    def_use(e0b) => e0a == e0b,
                    _ => false
                }
            }
            def_upvar(e0a, e1a, e2a, e3a) => {
                match (*other) {
                    def_upvar(e0b, e1b, e2b, e3b) =>
                        e0a == e0b && e1a == e1b && e2a == e2b && e3a == e3b,
                    _ => false
                }
            }
            def_class(e0a) => {
                match (*other) {
                    def_class(e0b) => e0a == e0b,
                    _ => false
                }
            }
            def_typaram_binder(e0a) => {
                match (*other) {
                    def_typaram_binder(e1a) => e0a == e1a,
                    _ => false
                }
            }
            def_region(e0a) => {
                match (*other) {
                    def_region(e0b) => e0a == e0b,
                    _ => false
                }
            }
            def_label(e0a) => {
                match (*other) {
                    def_label(e0b) => e0a == e0b,
                    _ => false
                }
            }
        }
    }
    pure fn ne(&self, other: &def) -> bool { !(*self).eq(other) }
}

// The set of meta_items that define the compilation environment of the crate,
// used to drive conditional compilation
type crate_cfg = ~[@meta_item];

type crate = spanned<crate_>;

type crate_ =
    {module: _mod,
     attrs: ~[attribute],
     config: crate_cfg};

type meta_item = spanned<meta_item_>;

#[auto_serialize]
#[auto_deserialize]
enum meta_item_ {
    meta_word(~str),
    meta_list(~str, ~[@meta_item]),
    meta_name_value(~str, lit),
}

type blk = spanned<blk_>;

#[auto_serialize]
#[auto_deserialize]
type blk_ = {view_items: ~[@view_item],
             stmts: ~[@stmt],
             expr: Option<@expr>,
             id: node_id,
             rules: blk_check_mode};

#[auto_serialize]
#[auto_deserialize]
type pat = {id: node_id, node: pat_, span: span};

#[auto_serialize]
#[auto_deserialize]
type field_pat = {ident: ident, pat: @pat};

#[auto_serialize]
#[auto_deserialize]
enum binding_mode {
    bind_by_value,
    bind_by_move,
    bind_by_ref(ast::mutability),
    bind_by_implicit_ref
}

impl binding_mode : to_bytes::IterBytes {
    pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
        match *self {
          bind_by_value => 0u8.iter_bytes(lsb0, f),

          bind_by_move => 1u8.iter_bytes(lsb0, f),

          bind_by_ref(ref m) =>
          to_bytes::iter_bytes_2(&2u8, m, lsb0, f),

          bind_by_implicit_ref =>
          3u8.iter_bytes(lsb0, f),
        }
    }
}

impl binding_mode : cmp::Eq {
    pure fn eq(&self, other: &binding_mode) -> bool {
        match (*self) {
            bind_by_value => {
                match (*other) {
                    bind_by_value => true,
                    _ => false
                }
            }
            bind_by_move => {
                match (*other) {
                    bind_by_move => true,
                    _ => false
                }
            }
            bind_by_ref(e0a) => {
                match (*other) {
                    bind_by_ref(e0b) => e0a == e0b,
                    _ => false
                }
            }
            bind_by_implicit_ref => {
                match (*other) {
                    bind_by_implicit_ref => true,
                    _ => false
                }
            }
        }
    }
    pure fn ne(&self, other: &binding_mode) -> bool { !(*self).eq(other) }
}

#[auto_serialize]
#[auto_deserialize]
enum pat_ {
    pat_wild,
    // A pat_ident may either be a new bound variable,
    // or a nullary enum (in which case the second field
    // is None).
    // In the nullary enum case, the parser can't determine
    // which it is. The resolver determines this, and
    // records this pattern's node_id in an auxiliary
    // set (of "pat_idents that refer to nullary enums")
    pat_ident(binding_mode, @path, Option<@pat>),
    pat_enum(@path, Option<~[@pat]>), // "none" means a * pattern where
                                  // we don't bind the fields to names
    pat_rec(~[field_pat], bool),
    pat_struct(@path, ~[field_pat], bool),
    pat_tup(~[@pat]),
    pat_box(@pat),
    pat_uniq(@pat),
    pat_region(@pat), // borrowed pointer pattern
    pat_lit(@expr),
    pat_range(@expr, @expr),
}

#[auto_serialize]
#[auto_deserialize]
enum mutability { m_mutbl, m_imm, m_const, }

impl mutability : to_bytes::IterBytes {
    pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
        (*self as u8).iter_bytes(lsb0, f)
    }
}

impl mutability : cmp::Eq {
    pure fn eq(&self, other: &mutability) -> bool {
        ((*self) as uint) == ((*other) as uint)
    }
    pure fn ne(&self, other: &mutability) -> bool { !(*self).eq(other) }
}

#[auto_serialize]
#[auto_deserialize]
pub enum Proto {
    ProtoBare,     // bare functions (deprecated)
    ProtoUniq,     // ~fn
    ProtoBox,      // @fn
    ProtoBorrowed, // &fn
}

impl Proto : cmp::Eq {
    pure fn eq(&self, other: &Proto) -> bool {
        ((*self) as uint) == ((*other) as uint)
    }
    pure fn ne(&self, other: &Proto) -> bool { !(*self).eq(other) }
}

impl Proto : to_bytes::IterBytes {
    pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
        (*self as uint).iter_bytes(lsb0, f);
    }
}

#[auto_serialize]
#[auto_deserialize]
enum vstore {
    // FIXME (#3469): Change uint to @expr (actually only constant exprs)
    vstore_fixed(Option<uint>),   // [1,2,3,4]
    vstore_uniq,                  // ~[1,2,3,4]
    vstore_box,                   // @[1,2,3,4]
    vstore_slice(@region)         // &[1,2,3,4](foo)?
}

#[auto_serialize]
#[auto_deserialize]
enum expr_vstore {
    // FIXME (#3469): Change uint to @expr (actually only constant exprs)
    expr_vstore_fixed(Option<uint>),   // [1,2,3,4]
    expr_vstore_uniq,                  // ~[1,2,3,4]
    expr_vstore_box,                   // @[1,2,3,4]
    expr_vstore_mut_box,               // @mut [1,2,3,4]
    expr_vstore_slice                  // &[1,2,3,4]
}

pure fn is_blockish(p: ast::Proto) -> bool {
    match p {
        ProtoBorrowed => true,
        ProtoBare | ProtoUniq | ProtoBox => false
    }
}

#[auto_serialize]
#[auto_deserialize]
enum binop {
    add,
    subtract,
    mul,
    div,
    rem,
    and,
    or,
    bitxor,
    bitand,
    bitor,
    shl,
    shr,
    eq,
    lt,
    le,
    ne,
    ge,
    gt,
}

impl binop : cmp::Eq {
    pure fn eq(&self, other: &binop) -> bool {
        ((*self) as uint) == ((*other) as uint)
    }
    pure fn ne(&self, other: &binop) -> bool { !(*self).eq(other) }
}

#[auto_serialize]
#[auto_deserialize]
enum unop {
    box(mutability),
    uniq(mutability),
    deref,
    not,
    neg
}

impl unop : cmp::Eq {
    pure fn eq(&self, other: &unop) -> bool {
        match (*self) {
            box(e0a) => {
                match (*other) {
                    box(e0b) => e0a == e0b,
                    _ => false
                }
            }
            uniq(e0a) => {
                match (*other) {
                    uniq(e0b) => e0a == e0b,
                    _ => false
                }
            }
            deref => {
                match (*other) {
                    deref => true,
                    _ => false
                }
            }
            not => {
                match (*other) {
                    not => true,
                    _ => false
                }
            }
            neg => {
                match (*other) {
                    neg => true,
                    _ => false
                }
            }
        }
    }
    pure fn ne(&self, other: &unop) -> bool {
        !(*self).eq(other)
    }
}

// Generally, after typeck you can get the inferred value
// using ty::resolved_T(...).
#[auto_serialize]
#[auto_deserialize]
enum inferable<T> {
    expl(T),
    infer(node_id)
}

impl<T: to_bytes::IterBytes> inferable<T> : to_bytes::IterBytes {
    pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
        match *self {
          expl(ref t) =>
          to_bytes::iter_bytes_2(&0u8, t, lsb0, f),

          infer(ref n) =>
          to_bytes::iter_bytes_2(&1u8, n, lsb0, f),
        }
    }
}

impl<T:cmp::Eq> inferable<T> : cmp::Eq {
    pure fn eq(&self, other: &inferable<T>) -> bool {
        match (*self) {
            expl(e0a) => {
                match (*other) {
                    expl(e0b) => e0a == e0b,
                    _ => false
                }
            }
            infer(e0a) => {
                match (*other) {
                    infer(e0b) => e0a == e0b,
                    _ => false
                }
            }
        }
    }
    pure fn ne(&self, other: &inferable<T>) -> bool { !(*self).eq(other) }
}

// "resolved" mode: the real modes.
#[auto_serialize]
#[auto_deserialize]
enum rmode { by_ref, by_val, by_move, by_copy }

impl rmode : to_bytes::IterBytes {
    pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
        (*self as u8).iter_bytes(lsb0, f)
    }
}


impl rmode : cmp::Eq {
    pure fn eq(&self, other: &rmode) -> bool {
        ((*self) as uint) == ((*other) as uint)
    }
    pure fn ne(&self, other: &rmode) -> bool { !(*self).eq(other) }
}

// inferable mode.
type mode = inferable<rmode>;

type stmt = spanned<stmt_>;

#[auto_serialize]
#[auto_deserialize]
enum stmt_ {
    stmt_decl(@decl, node_id),

    // expr without trailing semi-colon (must have unit type):
    stmt_expr(@expr, node_id),

    // expr with trailing semi-colon (may have any type):
    stmt_semi(@expr, node_id),

    // bool: is there a trailing sem-colon?
    stmt_mac(mac, bool),
}

// FIXME (pending discussion of #1697, #2178...): local should really be
// a refinement on pat.
#[auto_serialize]
#[auto_deserialize]
type local_ =  {is_mutbl: bool, ty: @Ty, pat: @pat,
                init: Option<@expr>, id: node_id};

type local = spanned<local_>;

type decl = spanned<decl_>;

#[auto_serialize]
#[auto_deserialize]
enum decl_ { decl_local(~[@local]), decl_item(@item), }

#[auto_serialize]
#[auto_deserialize]
type arm = {pats: ~[@pat], guard: Option<@expr>, body: blk};

#[auto_serialize]
#[auto_deserialize]
type field_ = {mutbl: mutability, ident: ident, expr: @expr};

type field = spanned<field_>;

#[auto_serialize]
#[auto_deserialize]
enum blk_check_mode { default_blk, unsafe_blk, }

impl blk_check_mode : cmp::Eq {
    pure fn eq(&self, other: &blk_check_mode) -> bool {
        match ((*self), (*other)) {
            (default_blk, default_blk) => true,
            (unsafe_blk, unsafe_blk) => true,
            (default_blk, _) => false,
            (unsafe_blk, _) => false,
        }
    }
    pure fn ne(&self, other: &blk_check_mode) -> bool { !(*self).eq(other) }
}

#[auto_serialize]
#[auto_deserialize]
type expr = {id: node_id, callee_id: node_id, node: expr_, span: span};
// Extra node ID is only used for index, assign_op, unary, binary, method call

#[auto_serialize]
#[auto_deserialize]
enum log_level { error, debug, log_other }
// 0 = error, 1 = debug, 2 = log_other

#[auto_serialize]
#[auto_deserialize]
enum expr_ {
    expr_vstore(@expr, expr_vstore),
    expr_vec(~[@expr], mutability),
    expr_rec(~[field], Option<@expr>),
    expr_call(@expr, ~[@expr], bool), // True iff last argument is a block
    expr_method_call(@expr, ident, ~[@Ty], ~[@expr], bool), // Ditto
    expr_tup(~[@expr]),
    expr_binary(binop, @expr, @expr),
    expr_unary(unop, @expr),
    expr_lit(@lit),
    expr_cast(@expr, @Ty),
    expr_if(@expr, blk, Option<@expr>),
    expr_while(@expr, blk),
    /* Conditionless loop (can be exited with break, cont, ret, or fail)
       Same semantics as while(true) { body }, but typestate knows that the
       (implicit) condition is always true. */
    expr_loop(blk, Option<ident>),
    expr_match(@expr, ~[arm]),
    expr_fn(Proto, fn_decl, blk, capture_clause),
    expr_fn_block(fn_decl, blk, capture_clause),
    // Inner expr is always an expr_fn_block. We need the wrapping node to
    // easily type this (a function returning nil on the inside but bool on
    // the outside).
    expr_loop_body(@expr),
    // Like expr_loop_body but for 'do' blocks
    expr_do_body(@expr),
    expr_block(blk),

    expr_copy(@expr),
    expr_unary_move(@expr),
    expr_assign(@expr, @expr),
    expr_swap(@expr, @expr),
    expr_assign_op(binop, @expr, @expr),
    expr_field(@expr, ident, ~[@Ty]),
    expr_index(@expr, @expr),
    expr_path(@path),
    expr_addr_of(mutability, @expr),
    expr_fail(Option<@expr>),
    expr_break(Option<ident>),
    expr_again(Option<ident>),
    expr_ret(Option<@expr>),
    expr_log(log_level, @expr, @expr),

    /* just an assert */
    expr_assert(@expr),

    expr_mac(mac),

    // A struct literal expression.
    expr_struct(@path, ~[field], Option<@expr>),

    // A vector literal constructed from one repeated element.
    expr_repeat(@expr /* element */, @expr /* count */, mutability),

    // No-op: used solely so we can pretty-print faithfully
    expr_paren(@expr)
}

#[auto_serialize]
#[auto_deserialize]
type capture_item_ = {
    id: int,
    is_move: bool,
    name: ident, // Currently, can only capture a local var.
    span: span
};

type capture_item = @capture_item_;

type capture_clause = @~[capture_item];

//
// When the main rust parser encounters a syntax-extension invocation, it
// parses the arguments to the invocation as a token-tree. This is a very
// loose structure, such that all sorts of different AST-fragments can
// be passed to syntax extensions using a uniform type.
//
// If the syntax extension is an MBE macro, it will attempt to match its
// LHS "matchers" against the provided token tree, and if it finds a
// match, will transcribe the RHS token tree, splicing in any captured
// macro_parser::matched_nonterminals into the tt_nonterminals it finds.
//
// The RHS of an MBE macro is the only place a tt_nonterminal or tt_seq
// makes any real sense. You could write them elsewhere but nothing
// else knows what to do with them, so you'll probably get a syntax
// error.
//
#[auto_serialize]
#[auto_deserialize]
#[doc="For macro invocations; parsing is delegated to the macro"]
enum token_tree {
    tt_tok(span, token::Token),
    tt_delim(~[token_tree]),
    // These only make sense for right-hand-sides of MBE macros
    tt_seq(span, ~[token_tree], Option<token::Token>, bool),
    tt_nonterminal(span, ident)
}

//
// Matchers are nodes defined-by and recognized-by the main rust parser and
// language, but they're only ever found inside syntax-extension invocations;
// indeed, the only thing that ever _activates_ the rules in the rust parser
// for parsing a matcher is a matcher looking for the 'matchers' nonterminal
// itself. Matchers represent a small sub-language for pattern-matching
// token-trees, and are thus primarily used by the macro-defining extension
// itself.
//
// match_tok
// ---------
//
//     A matcher that matches a single token, denoted by the token itself. So
//     long as there's no $ involved.
//
//
// match_seq
// ---------
//
//     A matcher that matches a sequence of sub-matchers, denoted various
//     possible ways:
//
//             $(M)*       zero or more Ms
//             $(M)+       one or more Ms
//             $(M),+      one or more comma-separated Ms
//             $(A B C);*  zero or more semi-separated 'A B C' seqs
//
//
// match_nonterminal
// -----------------
//
//     A matcher that matches one of a few interesting named rust
//     nonterminals, such as types, expressions, items, or raw token-trees. A
//     black-box matcher on expr, for example, binds an expr to a given ident,
//     and that ident can re-occur as an interpolation in the RHS of a
//     macro-by-example rule. For example:
//
//        $foo:expr   =>     1 + $foo    // interpolate an expr
//        $foo:tt     =>     $foo        // interpolate a token-tree
//        $foo:tt     =>     bar! $foo   // only other valid interpolation
//                                       // is in arg position for another
//                                       // macro
//
// As a final, horrifying aside, note that macro-by-example's input is
// also matched by one of these matchers. Holy self-referential! It is matched
// by an match_seq, specifically this one:
//
//                   $( $lhs:matchers => $rhs:tt );+
//
// If you understand that, you have closed to loop and understand the whole
// macro system. Congratulations.
//
type matcher = spanned<matcher_>;

#[auto_serialize]
#[auto_deserialize]
enum matcher_ {
    // match one token
    match_tok(token::Token),
    // match repetitions of a sequence: body, separator, zero ok?,
    // lo, hi position-in-match-array used:
    match_seq(~[matcher], Option<token::Token>, bool, uint, uint),
    // parse a Rust NT: name to bind, name of NT, position in match array:
    match_nonterminal(ident, ident, uint)
}

type mac = spanned<mac_>;

type mac_arg = Option<@expr>;

#[auto_serialize]
#[auto_deserialize]
type mac_body_ = {span: span};

type mac_body = Option<mac_body_>;

#[auto_serialize]
#[auto_deserialize]
enum mac_ {
    mac_invoc(@path, mac_arg, mac_body), // old macro-invocation
    mac_invoc_tt(@path,~[token_tree]),   // new macro-invocation
    mac_ellipsis,                        // old pattern-match (obsolete)

    // the span is used by the quoter/anti-quoter ...
    mac_aq(span /* span of quote */, @expr), // anti-quote
    mac_var(uint)
}

type lit = spanned<lit_>;

#[auto_serialize]
#[auto_deserialize]
enum lit_ {
    lit_str(@~str),
    lit_int(i64, int_ty),
    lit_uint(u64, uint_ty),
    lit_int_unsuffixed(i64),
    lit_float(@~str, float_ty),
    lit_float_unsuffixed(@~str),
    lit_nil,
    lit_bool(bool),
}

impl ast::lit_: cmp::Eq {
    pure fn eq(&self, other: &ast::lit_) -> bool {
        match ((*self), *other) {
            (lit_str(a), lit_str(b)) => a == b,
            (lit_int(val_a, ty_a), lit_int(val_b, ty_b)) => {
                val_a == val_b && ty_a == ty_b
            }
            (lit_uint(val_a, ty_a), lit_uint(val_b, ty_b)) => {
                val_a == val_b && ty_a == ty_b
            }
            (lit_int_unsuffixed(a), lit_int_unsuffixed(b)) => a == b,
            (lit_float(val_a, ty_a), lit_float(val_b, ty_b)) => {
                val_a == val_b && ty_a == ty_b
            }
            (lit_float_unsuffixed(a), lit_float_unsuffixed(b)) => a == b,
            (lit_nil, lit_nil) => true,
            (lit_bool(a), lit_bool(b)) => a == b,
            (lit_str(_), _) => false,
            (lit_int(*), _) => false,
            (lit_uint(*), _) => false,
            (lit_int_unsuffixed(*), _) => false,
            (lit_float(*), _) => false,
            (lit_float_unsuffixed(*), _) => false,
            (lit_nil, _) => false,
            (lit_bool(_), _) => false
        }
    }
    pure fn ne(&self, other: &ast::lit_) -> bool { !(*self).eq(other) }
}

// NB: If you change this, you'll probably want to change the corresponding
// type structure in middle/ty.rs as well.
#[auto_serialize]
#[auto_deserialize]
type mt = {ty: @Ty, mutbl: mutability};

#[auto_serialize]
#[auto_deserialize]
type ty_field_ = {ident: ident, mt: mt};

type ty_field = spanned<ty_field_>;

#[auto_serialize]
#[auto_deserialize]
type ty_method = {ident: ident, attrs: ~[attribute], purity: purity,
                  decl: fn_decl, tps: ~[ty_param], self_ty: self_ty,
                  id: node_id, span: span};

#[auto_serialize]
#[auto_deserialize]
// A trait method is either required (meaning it doesn't have an
// implementation, just a signature) or provided (meaning it has a default
// implementation).
enum trait_method {
    required(ty_method),
    provided(@method),
}

#[auto_serialize]
#[auto_deserialize]
enum int_ty { ty_i, ty_char, ty_i8, ty_i16, ty_i32, ty_i64, }

impl int_ty : to_bytes::IterBytes {
    pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
        (*self as u8).iter_bytes(lsb0, f)
    }
}

impl int_ty : cmp::Eq {
    pure fn eq(&self, other: &int_ty) -> bool {
        match ((*self), (*other)) {
            (ty_i, ty_i) => true,
            (ty_char, ty_char) => true,
            (ty_i8, ty_i8) => true,
            (ty_i16, ty_i16) => true,
            (ty_i32, ty_i32) => true,
            (ty_i64, ty_i64) => true,
            (ty_i, _) => false,
            (ty_char, _) => false,
            (ty_i8, _) => false,
            (ty_i16, _) => false,
            (ty_i32, _) => false,
            (ty_i64, _) => false,
        }
    }
    pure fn ne(&self, other: &int_ty) -> bool { !(*self).eq(other) }
}

#[auto_serialize]
#[auto_deserialize]
enum uint_ty { ty_u, ty_u8, ty_u16, ty_u32, ty_u64, }

impl uint_ty : to_bytes::IterBytes {
    pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
        (*self as u8).iter_bytes(lsb0, f)
    }
}

impl uint_ty : cmp::Eq {
    pure fn eq(&self, other: &uint_ty) -> bool {
        match ((*self), (*other)) {
            (ty_u, ty_u) => true,
            (ty_u8, ty_u8) => true,
            (ty_u16, ty_u16) => true,
            (ty_u32, ty_u32) => true,
            (ty_u64, ty_u64) => true,
            (ty_u, _) => false,
            (ty_u8, _) => false,
            (ty_u16, _) => false,
            (ty_u32, _) => false,
            (ty_u64, _) => false
        }
    }
    pure fn ne(&self, other: &uint_ty) -> bool { !(*self).eq(other) }
}

#[auto_serialize]
#[auto_deserialize]
enum float_ty { ty_f, ty_f32, ty_f64, }

impl float_ty : to_bytes::IterBytes {
    pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
        (*self as u8).iter_bytes(lsb0, f)
    }
}

impl float_ty : cmp::Eq {
    pure fn eq(&self, other: &float_ty) -> bool {
        match ((*self), (*other)) {
            (ty_f, ty_f) | (ty_f32, ty_f32) | (ty_f64, ty_f64) => true,
            (ty_f, _) | (ty_f32, _) | (ty_f64, _) => false
        }
    }
    pure fn ne(&self, other: &float_ty) -> bool { !(*self).eq(other) }
}

#[auto_serialize]
#[auto_deserialize]
type Ty = {id: node_id, node: ty_, span: span};

// Not represented directly in the AST, referred to by name through a ty_path.
#[auto_serialize]
#[auto_deserialize]
enum prim_ty {
    ty_int(int_ty),
    ty_uint(uint_ty),
    ty_float(float_ty),
    ty_str,
    ty_bool,
}

impl prim_ty : cmp::Eq {
    pure fn eq(&self, other: &prim_ty) -> bool {
        match (*self) {
            ty_int(e0a) => {
                match (*other) {
                    ty_int(e0b) => e0a == e0b,
                    _ => false
                }
            }
            ty_uint(e0a) => {
                match (*other) {
                    ty_uint(e0b) => e0a == e0b,
                    _ => false
                }
            }
            ty_float(e0a) => {
                match (*other) {
                    ty_float(e0b) => e0a == e0b,
                    _ => false
                }
            }
            ty_str => {
                match (*other) {
                    ty_str => true,
                    _ => false
                }
            }
            ty_bool => {
                match (*other) {
                    ty_bool => true,
                    _ => false
                }
            }
        }
    }
    pure fn ne(&self, other: &prim_ty) -> bool { !(*self).eq(other) }
}

#[auto_serialize]
#[auto_deserialize]
type region = {id: node_id, node: region_};

#[auto_serialize]
#[auto_deserialize]
enum region_ {
    re_anon,
    re_static,
    re_self,
    re_named(ident)
}

#[auto_serialize]
#[auto_deserialize]
enum Onceness {
    Once,
    Many
}

impl Onceness : cmp::Eq {
    pure fn eq(&self, other: &Onceness) -> bool {
        match ((*self), *other) {
            (Once, Once) | (Many, Many) => true,
            _ => false
        }
    }
    pure fn ne(&self, other: &Onceness) -> bool {
        !(*self).eq(other)
    }
}

#[auto_serialize]
#[auto_deserialize]
struct TyFn {
    proto: Proto,
    region: Option<@region>,
    purity: purity,
    onceness: Onceness,
    bounds: @~[ty_param_bound],
    decl: fn_decl
}

#[auto_serialize]
#[auto_deserialize]
enum ty_ {
    ty_nil,
    ty_bot, /* bottom type */
    ty_box(mt),
    ty_uniq(mt),
    ty_vec(mt),
    ty_fixed_length_vec(mt, uint),
    ty_ptr(mt),
    ty_rptr(@region, mt),
    ty_rec(~[ty_field]),
    ty_fn(@TyFn),
    ty_tup(~[@Ty]),
    ty_path(@path, node_id),
    ty_mac(mac),
    // ty_infer means the type should be inferred instead of it having been
    // specified. This should only appear at the "top level" of a type and not
    // nested in one.
    ty_infer,
}

// Equality and byte-iter (hashing) can be quite approximate for AST types.
// since we only care about this for normalizing them to "real" types.
impl Ty : cmp::Eq {
    pure fn eq(&self, other: &Ty) -> bool {
        ptr::addr_of(&(*self)) == ptr::addr_of(&(*other))
    }
    pure fn ne(&self, other: &Ty) -> bool {
        ptr::addr_of(&(*self)) != ptr::addr_of(&(*other))
    }
}

impl Ty : to_bytes::IterBytes {
    pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
        to_bytes::iter_bytes_2(&self.span.lo, &self.span.hi, lsb0, f);
    }
}


#[auto_serialize]
#[auto_deserialize]
type arg = {mode: mode, ty: @Ty, pat: @pat, id: node_id};

#[auto_serialize]
#[auto_deserialize]
type fn_decl =
    {inputs: ~[arg],
     output: @Ty,
     cf: ret_style};

#[auto_serialize]
#[auto_deserialize]
enum purity {
    pure_fn, // declared with "pure fn"
    unsafe_fn, // declared with "unsafe fn"
    impure_fn, // declared with "fn"
    extern_fn, // declared with "extern fn"
}

impl purity : to_bytes::IterBytes {
    pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
        (*self as u8).iter_bytes(lsb0, f)
    }
}

impl purity : cmp::Eq {
    pure fn eq(&self, other: &purity) -> bool {
        ((*self) as uint) == ((*other) as uint)
    }
    pure fn ne(&self, other: &purity) -> bool { !(*self).eq(other) }
}

#[auto_serialize]
#[auto_deserialize]
enum ret_style {
    noreturn, // functions with return type _|_ that always
              // raise an error or exit (i.e. never return to the caller)
    return_val, // everything else
}

impl ret_style : to_bytes::IterBytes {
    pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
        (*self as u8).iter_bytes(lsb0, f)
    }
}

impl ret_style : cmp::Eq {
    pure fn eq(&self, other: &ret_style) -> bool {
        match ((*self), (*other)) {
            (noreturn, noreturn) => true,
            (return_val, return_val) => true,
            (noreturn, _) => false,
            (return_val, _) => false,
        }
    }
    pure fn ne(&self, other: &ret_style) -> bool { !(*self).eq(other) }
}

#[auto_serialize]
#[auto_deserialize]
enum self_ty_ {
    sty_static,                         // no self: static method
    sty_by_ref,                         // old by-reference self: ``
    sty_value,                          // by-value self: `self`
    sty_region(mutability),             // by-region self: `&self`
    sty_box(mutability),                // by-managed-pointer self: `@self`
    sty_uniq(mutability)                // by-unique-pointer self: `~self`
}

impl self_ty_ : cmp::Eq {
    pure fn eq(&self, other: &self_ty_) -> bool {
        match (*self) {
            sty_static => {
                match (*other) {
                    sty_static => true,
                    _ => false
                }
            }
            sty_by_ref => {
                match (*other) {
                    sty_by_ref => true,
                    _ => false
                }
            }
            sty_value => {
                match (*other) {
                    sty_value => true,
                    _ => false
                }
            }
            sty_region(e0a) => {
                match (*other) {
                    sty_region(e0b) => e0a == e0b,
                    _ => false
                }
            }
            sty_box(e0a) => {
                match (*other) {
                    sty_box(e0b) => e0a == e0b,
                    _ => false
                }
            }
            sty_uniq(e0a) => {
                match (*other) {
                    sty_uniq(e0b) => e0a == e0b,
                    _ => false
                }
            }
        }
    }
    pure fn ne(&self, other: &self_ty_) -> bool { !(*self).eq(other) }
}

type self_ty = spanned<self_ty_>;

#[auto_serialize]
#[auto_deserialize]
type method = {ident: ident, attrs: ~[attribute],
               tps: ~[ty_param], self_ty: self_ty,
               purity: purity, decl: fn_decl, body: blk,
               id: node_id, span: span, self_id: node_id,
               vis: visibility};

#[auto_serialize]
#[auto_deserialize]
type _mod = {view_items: ~[@view_item], items: ~[@item]};

#[auto_serialize]
#[auto_deserialize]
enum foreign_abi {
    foreign_abi_rust_intrinsic,
    foreign_abi_cdecl,
    foreign_abi_stdcall,
}

// Foreign mods can be named or anonymous
#[auto_serialize]
#[auto_deserialize]
enum foreign_mod_sort { named, anonymous }

impl foreign_mod_sort : cmp::Eq {
    pure fn eq(&self, other: &foreign_mod_sort) -> bool {
        ((*self) as uint) == ((*other) as uint)
    }
    pure fn ne(&self, other: &foreign_mod_sort) -> bool { !(*self).eq(other) }
}

impl foreign_abi : cmp::Eq {
    pure fn eq(&self, other: &foreign_abi) -> bool {
        match ((*self), (*other)) {
            (foreign_abi_rust_intrinsic, foreign_abi_rust_intrinsic) => true,
            (foreign_abi_cdecl, foreign_abi_cdecl) => true,
            (foreign_abi_stdcall, foreign_abi_stdcall) => true,
            (foreign_abi_rust_intrinsic, _) => false,
            (foreign_abi_cdecl, _) => false,
            (foreign_abi_stdcall, _) => false,
        }
    }
    pure fn ne(&self, other: &foreign_abi) -> bool { !(*self).eq(other) }
}

#[auto_serialize]
#[auto_deserialize]
type foreign_mod =
    {sort: foreign_mod_sort,
     abi: ident,
     view_items: ~[@view_item],
     items: ~[@foreign_item]};

#[auto_serialize]
#[auto_deserialize]
type variant_arg = {ty: @Ty, id: node_id};

#[auto_serialize]
#[auto_deserialize]
enum variant_kind {
    tuple_variant_kind(~[variant_arg]),
    struct_variant_kind(@struct_def),
    enum_variant_kind(enum_def)
}

#[auto_serialize]
#[auto_deserialize]
type enum_def_ = { variants: ~[variant], common: Option<@struct_def> };

#[auto_serialize]
#[auto_deserialize]
enum enum_def = enum_def_;

#[auto_serialize]
#[auto_deserialize]
type variant_ = {name: ident, attrs: ~[attribute], kind: variant_kind,
                 id: node_id, disr_expr: Option<@expr>, vis: visibility};

type variant = spanned<variant_>;

#[auto_serialize]
#[auto_deserialize]
type path_list_ident_ = {name: ident, id: node_id};

type path_list_ident = spanned<path_list_ident_>;

#[auto_serialize]
#[auto_deserialize]
enum namespace { module_ns, type_value_ns }

impl namespace : cmp::Eq {
    pure fn eq(&self, other: &namespace) -> bool {
        ((*self) as uint) == ((*other) as uint)
    }
    pure fn ne(&self, other: &namespace) -> bool { !(*self).eq(other) }
}

type view_path = spanned<view_path_>;

#[auto_serialize]
#[auto_deserialize]
enum view_path_ {

    // quux = foo::bar::baz
    //
    // or just
    //
    // foo::bar::baz  (with 'baz =' implicitly on the left)
    view_path_simple(ident, @path, namespace, node_id),

    // foo::bar::*
    view_path_glob(@path, node_id),

    // foo::bar::{a,b,c}
    view_path_list(@path, ~[path_list_ident], node_id)
}

#[auto_serialize]
#[auto_deserialize]
type view_item = {node: view_item_, attrs: ~[attribute],
                  vis: visibility, span: span};

#[auto_serialize]
#[auto_deserialize]
enum view_item_ {
    view_item_use(ident, ~[@meta_item], node_id),
    view_item_import(~[@view_path]),
    view_item_export(~[@view_path])
}

// Meta-data associated with an item
type attribute = spanned<attribute_>;

// Distinguishes between attributes that decorate items and attributes that
// are contained as statements within items. These two cases need to be
// distinguished for pretty-printing.
#[auto_serialize]
#[auto_deserialize]
enum attr_style { attr_outer, attr_inner, }

impl attr_style : cmp::Eq {
    pure fn eq(&self, other: &attr_style) -> bool {
        ((*self) as uint) == ((*other) as uint)
    }
    pure fn ne(&self, other: &attr_style) -> bool { !(*self).eq(other) }
}

// doc-comments are promoted to attributes that have is_sugared_doc = true
#[auto_serialize]
#[auto_deserialize]
type attribute_ = {style: attr_style, value: meta_item, is_sugared_doc: bool};

/*
  trait_refs appear in both impls and in classes that implement traits.
  resolve maps each trait_ref's ref_id to its defining trait; that's all
  that the ref_id is for. The impl_id maps to the "self type" of this impl.
  If this impl is an item_impl, the impl_id is redundant (it could be the
  same as the impl's node id). If this impl is actually an impl_class, then
  conceptually, the impl_id stands in for the pair of (this class, this
  trait)
 */
#[auto_serialize]
#[auto_deserialize]
type trait_ref = {path: @path, ref_id: node_id, impl_id: node_id};

#[auto_serialize]
#[auto_deserialize]
enum visibility { public, private, inherited }

impl visibility : cmp::Eq {
    pure fn eq(&self, other: &visibility) -> bool {
        match ((*self), (*other)) {
            (public, public) => true,
            (private, private) => true,
            (inherited, inherited) => true,
            (public, _) => false,
            (private, _) => false,
            (inherited, _) => false,
        }
    }
    pure fn ne(&self, other: &visibility) -> bool { !(*self).eq(other) }
}

#[auto_serialize]
#[auto_deserialize]
type struct_field_ = {
    kind: struct_field_kind,
    id: node_id,
    ty: @Ty
};

type struct_field = spanned<struct_field_>;

#[auto_serialize]
#[auto_deserialize]
enum struct_field_kind {
    named_field(ident, class_mutability, visibility),
    unnamed_field   // element of a tuple-like struct
}

impl struct_field_kind : cmp::Eq {
    pure fn eq(&self, other: &struct_field_kind) -> bool {
        match (*self) {
            named_field(ident_a, class_mutability_a, visibility_a) => {
                match *other {
                    named_field(ident_b, class_mutability_b, visibility_b)
                            => {
                        ident_a == ident_b &&
                        class_mutability_a == class_mutability_b &&
                        visibility_a == visibility_b
                    }
                    unnamed_field => false
                }
            }
            unnamed_field => {
                match *other {
                    named_field(*) => false,
                    unnamed_field => true
                }
            }
        }
    }
    pure fn ne(&self, other: &struct_field_kind) -> bool {
        !(*self).eq(other)
    }
}

#[auto_serialize]
#[auto_deserialize]
type struct_def = {
    traits: ~[@trait_ref],   /* traits this struct implements */
    fields: ~[@struct_field], /* fields */
    methods: ~[@method],    /* methods */
    /* (not including ctor or dtor) */
    /* dtor is optional */
    dtor: Option<class_dtor>,
    /* ID of the constructor. This is only used for tuple- or enum-like
     * structs. */
    ctor_id: Option<node_id>
};

/*
  FIXME (#3300): Should allow items to be anonymous. Right now
  we just use dummy names for anon items.
 */
#[auto_serialize]
#[auto_deserialize]
type item = {ident: ident, attrs: ~[attribute],
             id: node_id, node: item_,
             vis: visibility, span: span};

#[auto_serialize]
#[auto_deserialize]
enum item_ {
    item_const(@Ty, @expr),
    item_fn(fn_decl, purity, ~[ty_param], blk),
    item_mod(_mod),
    item_foreign_mod(foreign_mod),
    item_ty(@Ty, ~[ty_param]),
    item_enum(enum_def, ~[ty_param]),
    item_class(@struct_def, ~[ty_param]),
    item_trait(~[ty_param], ~[@trait_ref], ~[trait_method]),
    item_impl(~[ty_param],
              Option<@trait_ref>, /* (optional) trait this impl implements */
              @Ty, /* self */
              ~[@method]),
    item_mac(mac),
}

#[auto_serialize]
#[auto_deserialize]
enum class_mutability { class_mutable, class_immutable }

impl class_mutability : to_bytes::IterBytes {
    pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) {
        (*self as u8).iter_bytes(lsb0, f)
    }
}

impl class_mutability : cmp::Eq {
    pure fn eq(&self, other: &class_mutability) -> bool {
        match ((*self), (*other)) {
            (class_mutable, class_mutable) => true,
            (class_immutable, class_immutable) => true,
            (class_mutable, _) => false,
            (class_immutable, _) => false,
        }
    }
    pure fn ne(&self, other: &class_mutability) -> bool { !(*self).eq(other) }
}

type class_ctor = spanned<class_ctor_>;

#[auto_serialize]
#[auto_deserialize]
type class_ctor_ = {id: node_id,
                    attrs: ~[attribute],
                    self_id: node_id,
                    dec: fn_decl,
                    body: blk};

type class_dtor = spanned<class_dtor_>;

#[auto_serialize]
#[auto_deserialize]
type class_dtor_ = {id: node_id,
                    attrs: ~[attribute],
                    self_id: node_id,
                    body: blk};

#[auto_serialize]
#[auto_deserialize]
type foreign_item =
    {ident: ident,
     attrs: ~[attribute],
     node: foreign_item_,
     id: node_id,
     span: span,
     vis: visibility};

#[auto_serialize]
#[auto_deserialize]
enum foreign_item_ {
    foreign_item_fn(fn_decl, purity, ~[ty_param]),
    foreign_item_const(@Ty)
}

// The data we save and restore about an inlined item or method.  This is not
// part of the AST that we parse from a file, but it becomes part of the tree
// that we trans.
#[auto_serialize]
#[auto_deserialize]
enum inlined_item {
    ii_item(@item),
    ii_method(def_id /* impl id */, @method),
    ii_foreign(@foreign_item),
    ii_dtor(class_dtor, ident, ~[ty_param], def_id /* parent id */)
}


//
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
//