@@ -76,12 +76,12 @@ fn comma_str(vec[@constr_arg_use] args) -> str {
76
76
ret res;
77
77
}
78
78
79
- fn constraint_to_str ( ty:: ctxt tcx, constr c) -> str {
80
- alt ( c. node ) {
81
- case ( ninit ( ?i, _ ) ) {
79
+ fn constraint_to_str ( & ty:: ctxt tcx, & constr c) -> str {
80
+ alt ( c. node . c ) {
81
+ case ( ninit ( ?i) ) {
82
82
ret "init(" + i + " [" + tcx. sess . span_str ( c. span ) + "])" ;
83
83
}
84
- case ( npred ( ?p, _ , ?args) ) {
84
+ case ( npred ( ?p, ?args) ) {
85
85
ret path_to_str ( p) + "(" + comma_str ( args) + ")"
86
86
+ "[" + tcx. sess . span_str ( c. span ) + "]" ;
87
87
}
@@ -101,7 +101,7 @@ fn bitv_to_str(fn_ctxt fcx, bitv::t v) -> str {
101
101
ret s;
102
102
}
103
103
104
- fn log_bitv ( fn_ctxt fcx, bitv:: t v) {
104
+ fn log_bitv ( & fn_ctxt fcx, & bitv:: t v) {
105
105
log ( bitv_to_str ( fcx, v) ) ;
106
106
}
107
107
@@ -208,43 +208,50 @@ fn print_idents(vec[ident] idents) -> () {
208
208
/* data structures */
209
209
210
210
/**********************************************************************/
211
- /* mapping from def_id to bit number and other data
212
- (ident/path/span are there for error-logging purposes) */
211
+ /* Two different data structures represent constraints in different
212
+ contexts: constraint and norm_constraint.
213
213
214
- /* FIXME very confused about why we have all these different types. */
214
+ constraint gets used to record constraints in a table keyed by def_ids.
215
+ cinit constraints represent a single constraint, for the initialization
216
+ state of a variable; a cpred constraint, with a single operator and a
217
+ list of possible argument lists, could represent several constraints at
218
+ once.
219
+
220
+ norm_constraint, in contrast, gets used when handling an instance
221
+ of a constraint rather than a definition of a constraint. It can
222
+ also be init or pred (ninit or npred), but the npred case just has
223
+ a single argument list.
224
+
225
+ The representation of constraints, where multiple instances of the
226
+ same predicate are collapsed into one entry in the table, makes it
227
+ easier to look up a specific instance.
228
+
229
+ Both types are in constrast with the constraint type defined in
230
+ front::ast, which is for predicate constraints only, and is what
231
+ gets generated by the parser. aux and ast share the same type
232
+ to represent predicate *arguments* however. This type
233
+ (constr_arg_general) is parameterized (see comments in front::ast).
234
+
235
+ Both types store an ident and span, for error-logging purposes.
236
+ */
215
237
216
238
type pred_desc_ = rec ( vec[ @constr_arg_use] args ,
217
239
uint bit_num ) ;
218
240
type pred_desc = spanned[ pred_desc_ ] ;
219
241
tag constraint {
220
- cinit( uint, span, def_id , ident) ;
221
- cpred ( path, def_id , @mutable vec[ pred_desc] ) ;
242
+ cinit( uint, span, ident) ;
243
+ cpred ( path, @mutable vec[ pred_desc] ) ;
222
244
}
223
- tag constr_ {
224
- ninit( ident, def_id ) ;
225
- npred ( path, def_id , vec[ @constr_arg_use] ) ;
245
+ tag constr__ {
246
+ ninit( ident) ;
247
+ npred ( path, vec[ @constr_arg_use] ) ;
226
248
}
249
+ type constr_ = rec ( def_id id, constr__ c) ;
227
250
type constr = spanned[ constr_ ] ;
228
251
type norm_constraint = rec ( uint bit_num ,
229
252
constr c) ;
230
- /* "constraint occurrence" to disambiguate
231
- between constraints. either "this is an
232
- init constraint", or the list of args for
233
- a pred. */
234
- tag constr_occ {
235
- occ_init;
236
- occ_args ( vec[ @constr_arg_use] ) ;
237
- }
238
-
239
253
type constr_map = @std:: map:: hashmap [ def_id, constraint] ;
240
254
241
- fn constr_id ( & constr c) -> def_id {
242
- ret ( alt ( c. node ) {
243
- case ( ninit ( _, ?i) ) { i }
244
- case ( npred ( _, ?i, _) ) { i }
245
- } )
246
- }
247
-
248
255
type fn_info = rec ( constr_map constrs, uint num_constraints , controlflow cf) ;
249
256
250
257
/* mapping from node ID to typestate annotation */
@@ -529,16 +536,16 @@ fn ann_to_def(&crate_ctxt ccx, &ann a) -> option::t[def] {
529
536
ret ccx. tcx . def_map . find ( a. id ) ;
530
537
}
531
538
532
- fn norm_a_constraint ( & constraint c) -> vec[ norm_constraint ] {
539
+ fn norm_a_constraint ( & def_id id , & constraint c) -> vec[ norm_constraint ] {
533
540
alt ( c) {
534
- case ( cinit ( ?n, ?sp, ?id , ? i) ) {
535
- ret [ rec ( bit_num=n, c=respan ( sp, ninit ( i , id ) ) ) ] ;
541
+ case ( cinit ( ?n, ?sp, ?i) ) {
542
+ ret [ rec ( bit_num=n, c=respan ( sp, rec ( id=id , c= ninit ( i ) ) ) ) ] ;
536
543
}
537
- case ( cpred ( ?p, ?id , ? descs) ) {
544
+ case ( cpred ( ?p, ?descs) ) {
538
545
let vec[ norm_constraint] res = [ ] ;
539
546
for ( pred_desc pd in * descs) {
540
547
vec:: push ( res, rec ( bit_num=pd. node . bit_num ,
541
- c=respan ( pd. span , npred ( p, id , pd. node . args ) ) ) ) ;
548
+ c=respan ( pd. span , rec ( id=id , c= npred ( p, pd. node . args ) ) ) ) ) ;
542
549
}
543
550
ret res;
544
551
}
@@ -551,7 +558,7 @@ fn constraints(&fn_ctxt fcx) -> vec[norm_constraint] {
551
558
let vec[ norm_constraint] res = [ ] ;
552
559
for each ( @tup( def_id, constraint) p in
553
560
fcx. enclosing . constrs . items ( ) ) {
554
- res += norm_a_constraint ( p. _1 ) ;
561
+ res += norm_a_constraint ( p. _0 , p . _1 ) ;
555
562
}
556
563
ret res;
557
564
}
@@ -572,13 +579,6 @@ fn match_args(&fn_ctxt fcx, vec[pred_desc] occs,
572
579
fcx. ccx . tcx . sess . bug ( "match_args: no match for occurring args" ) ;
573
580
}
574
581
575
- fn constr_to_constr_occ ( & ty:: ctxt tcx, & constr_ c) -> constr_occ {
576
- alt ( c) {
577
- case ( ninit ( _, _) ) { ret occ_init; }
578
- case ( npred ( _, _, ?args) ) { ret occ_args ( args) ; }
579
- }
580
- }
581
-
582
582
fn def_id_for_constr ( ty:: ctxt tcx, uint t) -> def_id {
583
583
alt ( tcx. def_map . find ( t) ) {
584
584
case ( none) {
@@ -627,8 +627,9 @@ fn expr_to_constr(ty::ctxt tcx, &@expr e) -> constr {
627
627
alt ( operator. node ) {
628
628
case ( expr_path ( ?p, ?a) ) {
629
629
ret respan ( e. span ,
630
- npred ( p, def_id_for_constr ( tcx, a. id ) ,
631
- exprs_to_constr_args ( tcx, args) ) ) ;
630
+ rec ( id=def_id_for_constr ( tcx, a. id ) ,
631
+ c=npred ( p,
632
+ exprs_to_constr_args ( tcx, args) ) ) ) ;
632
633
}
633
634
case ( _) {
634
635
tcx. sess . span_err ( operator. span , "Internal error: " +
@@ -648,20 +649,14 @@ fn pred_desc_to_str(&pred_desc p) -> str {
648
649
pretty:: ppaux:: constr_args_to_str_1 ( p. node . args ) + ">" ) ;
649
650
}
650
651
651
- fn substitute_constr_args_ ( & ty:: ctxt cx,
652
+ fn substitute_constr_args ( & ty:: ctxt cx,
652
653
& vec[ @expr] actuals , & @ast:: constr c)
653
- -> vec [ @ constr_arg_use ] {
654
+ -> constr__ {
654
655
let vec[ @constr_arg_use] res = [ ] ;
655
656
for ( @constr_arg a in c. node. args) {
656
657
res += [ substitute_arg ( cx, actuals, a) ] ;
657
658
}
658
- ret res;
659
-
660
- }
661
-
662
- fn substitute_constr_args ( & ty:: ctxt cx,
663
- & vec[ @expr] actuals , & @ast:: constr c) -> constr_occ {
664
- ret occ_args ( substitute_constr_args_ ( cx, actuals, c) ) ;
659
+ ret npred( c. node . path , res) ;
665
660
}
666
661
667
662
type subst = vec[ tup ( arg , @expr) ] ;
@@ -683,6 +678,16 @@ fn substitute_arg(&ty::ctxt cx, &vec[@expr] actuals, @ast::constr_arg a)
683
678
}
684
679
}
685
680
681
+ fn path_to_ident ( & ty:: ctxt cx, & path p) -> ident {
682
+ alt ( vec:: last ( p. node . idents ) ) {
683
+ case ( none) {
684
+ cx. sess . span_err ( p. span , "Malformed path" ) ;
685
+ }
686
+ case ( some ( ?i) ) {
687
+ ret i;
688
+ }
689
+ }
690
+ }
686
691
//
687
692
// Local Variables:
688
693
// mode: rust
0 commit comments