@@ -44,7 +44,9 @@ tag scope {
44
44
tag import_state {
45
45
todo( @ast : : view_item, list[ scope] ) ;
46
46
resolving ( span) ;
47
- resolved ( option:: t[ def] /* value */ , option:: t[ def] /* type */ ) ;
47
+ resolved ( option:: t[ def] /* value */ ,
48
+ option:: t[ def] /* type */ ,
49
+ option:: t[ def] /* module */ ) ;
48
50
}
49
51
50
52
type ext_hash = hashmap [ tup ( def_id, str, namespace) , def] ;
@@ -97,6 +99,7 @@ tag dir { inside; outside; }
97
99
tag namespace {
98
100
ns_value;
99
101
ns_type;
102
+ ns_module;
100
103
}
101
104
102
105
fn resolve_crate ( session sess, @ast:: crate crate) -> def_map {
@@ -159,7 +162,7 @@ fn resolve_imports(&env e) {
159
162
case ( todo ( ?item, ?sc) ) {
160
163
resolve_import ( e, item, sc) ;
161
164
}
162
- case ( resolved ( _, _) ) { }
165
+ case ( resolved ( _, _, _ ) ) { }
163
166
}
164
167
}
165
168
}
@@ -324,51 +327,45 @@ fn resolve_import(&env e, &@ast::view_item it, &list[scope] sc) {
324
327
if ( n_idents == 1 u) {
325
328
register ( e, defid, it. span , end_id,
326
329
lookup_in_scope ( e, sc, it. span , end_id, ns_value) ,
327
- lookup_in_scope ( e, sc, it. span , end_id, ns_type) ) ;
330
+ lookup_in_scope ( e, sc, it. span , end_id, ns_type) ,
331
+ lookup_in_scope ( e, sc, it. span , end_id, ns_module) ) ;
328
332
} else {
329
- auto dcur = lookup_in_scope_strict ( e, sc, it. span , ids. ( 0 ) , ns_value) ;
333
+ auto dcur = lookup_in_scope_strict ( e, sc, it. span , ids. ( 0 ) ,
334
+ ns_module) ;
330
335
auto i = 1 u;
331
336
while ( true ) {
332
- if ( !is_module ( dcur) ) {
333
- e. sess . span_err ( it. span , ids. ( i -1 u) +
334
- " is not a module or crate" ) ;
335
- }
336
337
if ( i == n_idents - 1 u) {
337
338
register ( e, defid, it. span , end_id,
338
339
lookup_in_mod ( e, dcur, end_id, ns_value, outside) ,
339
- lookup_in_mod ( e, dcur, end_id, ns_type, outside) ) ;
340
+ lookup_in_mod ( e, dcur, end_id, ns_type, outside) ,
341
+ lookup_in_mod ( e, dcur, end_id, ns_module, outside) ) ;
340
342
break ;
341
343
} else {
342
344
dcur = lookup_in_mod_strict ( e, dcur, it. span , ids. ( i) ,
343
- ns_value , outside) ;
345
+ ns_module , outside) ;
344
346
i += 1 u;
345
347
}
346
348
}
347
349
}
348
350
349
351
fn register ( & env e, def_id defid, & span sp, & ident id,
350
- & option:: t[ def] val , & option:: t[ def] typ ) {
351
- if ( option:: is_none ( val) && option:: is_none ( typ) ) {
352
+ & option:: t[ def] val , & option:: t[ def] typ ,
353
+ & option:: t[ def] md ) {
354
+ if ( option:: is_none ( val) && option:: is_none ( typ) &&
355
+ option:: is_none ( md) ) {
352
356
unresolved ( e, sp, id, "import" ) ;
353
357
}
354
- e. imports . insert ( defid. _1 , resolved ( val, typ) ) ;
358
+ e. imports . insert ( defid. _1 , resolved ( val, typ, md ) ) ;
355
359
}
356
360
}
357
361
358
362
// Utilities
359
363
360
- fn is_module ( def d) -> bool {
361
- alt ( d) {
362
- case ( ast:: def_mod ( _) ) { ret true ; }
363
- case ( ast:: def_native_mod ( _) ) { ret true ; }
364
- case ( _) { ret false ; }
365
- }
366
- }
367
-
368
364
fn ns_name ( namespace ns) -> str {
369
365
alt ( ns) {
370
366
case ( ns_type) { ret "typename" ; }
371
367
case ( ns_value) { ret "name" ; }
368
+ case ( ns_module) { ret "modulename" ; }
372
369
}
373
370
}
374
371
@@ -381,20 +378,14 @@ fn unresolved(&env e, &span sp, &ident id, &str kind) {
381
378
fn lookup_path_strict ( & env e, & list[ scope] sc , & span sp, vec[ ident] idents ,
382
379
namespace ns) -> def {
383
380
auto n_idents = _vec:: len ( idents) ;
384
- auto dcur = lookup_in_scope_strict ( e, sc, sp, idents. ( 0 ) , ns) ;
381
+ auto headns = if ( n_idents == 1 u) { ns } else { ns_module } ;
382
+ auto dcur = lookup_in_scope_strict ( e, sc, sp, idents. ( 0 ) , headns) ;
385
383
auto i = 1 u;
386
384
while ( i < n_idents) {
387
- if ( !is_module ( dcur) ) {
388
- e. sess . span_err ( sp, idents. ( i -1 u) +
389
- " can't be dereferenced as a module" ) ;
390
- }
391
- dcur = lookup_in_mod_strict ( e, dcur, sp, idents. ( i) , ns, outside) ;
385
+ auto curns = if ( n_idents == i + 1 u) { ns } else { ns_module } ;
386
+ dcur = lookup_in_mod_strict ( e, dcur, sp, idents. ( i) , curns, outside) ;
392
387
i += 1 u;
393
388
}
394
- if ( is_module ( dcur) ) {
395
- e. sess . span_err ( sp, _str:: connect ( idents, "::" ) +
396
- " is a module, not a " + ns_name ( ns) ) ;
397
- }
398
389
ret dcur;
399
390
}
400
391
@@ -566,29 +557,37 @@ fn lookup_in_pat(&ident id, &ast::pat pat) -> option::t[def] {
566
557
567
558
fn lookup_in_fn ( & ident id, & ast:: fn_decl decl, & vec[ ast:: ty_param ] ty_params ,
568
559
namespace ns) -> option:: t [ def ] {
569
- if ( ns == ns_value) {
570
- for ( ast:: arg a in decl. inputs) {
571
- if ( _str:: eq ( a. ident , id) ) {
572
- ret some ( ast:: def_arg ( a. id ) ) ;
560
+ alt ( ns) {
561
+ case ( ns_value) {
562
+ for ( ast:: arg a in decl. inputs) {
563
+ if ( _str:: eq ( a. ident , id) ) {
564
+ ret some ( ast:: def_arg ( a. id ) ) ;
565
+ }
573
566
}
567
+ ret none[ def] ;
574
568
}
575
- ret none[ def] ;
576
- } else {
577
- ret lookup_in_ty_params ( id, ty_params) ;
569
+ case ( ns_type) {
570
+ ret lookup_in_ty_params ( id, ty_params) ;
571
+ }
572
+ case ( _) { ret none[ def] ; }
578
573
}
579
574
}
580
575
581
576
fn lookup_in_obj ( & ident id, & ast:: _obj ob, & vec[ ast:: ty_param ] ty_params ,
582
577
namespace ns) -> option:: t [ def ] {
583
- if ( ns == ns_value) {
584
- for ( ast:: obj_field f in ob. fields) {
585
- if ( _str:: eq ( f. ident , id) ) {
586
- ret some ( ast:: def_obj_field ( f. id ) ) ;
578
+ alt ( ns) {
579
+ case ( ns_value) {
580
+ for ( ast:: obj_field f in ob. fields) {
581
+ if ( _str:: eq ( f. ident , id) ) {
582
+ ret some ( ast:: def_obj_field ( f. id ) ) ;
583
+ }
587
584
}
585
+ ret none[ def] ;
588
586
}
589
- ret none[ def] ;
590
- } else {
591
- ret lookup_in_ty_params ( id, ty_params) ;
587
+ case ( ns_type) {
588
+ ret lookup_in_ty_params ( id, ty_params) ;
589
+ }
590
+ case ( _) { ret none[ def] ; }
592
591
}
593
592
}
594
593
@@ -611,7 +610,7 @@ fn lookup_in_block(&ident id, &ast::block_ b, namespace ns)
611
610
if ( _str:: eq ( name, id) ) {
612
611
ret some ( ast:: def_ty ( defid) ) ;
613
612
}
614
- } else {
613
+ } else if ( ns == ns_value ) {
615
614
for ( ast:: variant v in variants) {
616
615
if ( _str:: eq ( v. node . name , id) ) {
617
616
ret some ( ast:: def_variant (
@@ -623,7 +622,7 @@ fn lookup_in_block(&ident id, &ast::block_ b, namespace ns)
623
622
case ( _) {
624
623
if ( _str:: eq ( ast:: item_ident ( it) , id) ) {
625
624
auto found = found_def_item ( it, ns) ;
626
- if ( !option:: is_none ( found) ) { ret found; }
625
+ if ( !option:: is_none ( found) ) { ret found; }
627
626
}
628
627
}
629
628
}
@@ -645,10 +644,10 @@ fn found_def_item(@ast::item i, namespace ns) -> option::t[def] {
645
644
if ( ns == ns_value) { ret some ( ast:: def_fn ( defid) ) ; }
646
645
}
647
646
case ( ast:: item_mod ( _, _, ?defid) ) {
648
- ret some ( ast:: def_mod ( defid) ) ;
647
+ if ( ns == ns_module ) { ret some ( ast:: def_mod ( defid) ) ; }
649
648
}
650
649
case ( ast:: item_native_mod ( _, _, ?defid) ) {
651
- ret some ( ast:: def_native_mod ( defid) ) ;
650
+ if ( ns == ns_module ) { ret some ( ast:: def_native_mod ( defid) ) ; }
652
651
}
653
652
case ( ast:: item_ty ( _, _, _, ?defid, _) ) {
654
653
if ( ns == ns_type) { ret some ( ast:: def_ty ( defid) ) ; }
@@ -657,8 +656,11 @@ fn found_def_item(@ast::item i, namespace ns) -> option::t[def] {
657
656
if ( ns == ns_type) { ret some ( ast:: def_ty ( defid) ) ; }
658
657
}
659
658
case ( ast:: item_obj ( _, _, _, ?odid, _) ) {
660
- if ( ns == ns_value) { ret some ( ast:: def_obj ( odid. ctor ) ) ; }
661
- else { ret some ( ast:: def_obj ( odid. ty ) ) ; }
659
+ alt ( ns) {
660
+ case ( ns_value) { ret some ( ast:: def_obj ( odid. ctor ) ) ; }
661
+ case ( ns_type) { ret some ( ast:: def_obj ( odid. ty ) ) ; }
662
+ case ( _) { }
663
+ }
662
664
}
663
665
case ( _) { }
664
666
}
@@ -725,9 +727,10 @@ fn lookup_import(&env e, def_id defid, namespace ns) -> option::t[def] {
725
727
case ( resolving ( ?sp) ) {
726
728
e. sess . span_err ( sp, "cyclic import" ) ;
727
729
}
728
- case ( resolved ( ?val, ?typ) ) {
730
+ case ( resolved ( ?val, ?typ, ?md ) ) {
729
731
ret alt ( ns) { case ( ns_value) { val }
730
- case ( ns_type) { typ } } ;
732
+ case ( ns_type) { typ }
733
+ case ( ns_module) { md } } ;
731
734
}
732
735
}
733
736
}
@@ -917,31 +920,30 @@ fn index_nmod(&ast::native_mod md) -> nmod_index {
917
920
918
921
// External lookups
919
922
920
- // FIXME creader should handle multiple namespaces
921
- fn check_def_by_ns ( def d, namespace ns) -> bool {
923
+ fn ns_for_def ( def d) -> namespace {
922
924
ret alt ( d) {
923
- case ( ast:: def_fn ( ?id) ) { ns == ns_value }
924
- case ( ast:: def_obj ( ?id) ) { ns == ns_value }
925
- case ( ast:: def_obj_field ( ?id) ) { ns == ns_value }
926
- case ( ast:: def_mod ( ?id) ) { true }
927
- case ( ast:: def_native_mod ( ?id) ) { true }
928
- case ( ast:: def_const ( ?id) ) { ns == ns_value }
929
- case ( ast:: def_arg ( ?id) ) { ns == ns_value }
930
- case ( ast:: def_local ( ?id) ) { ns == ns_value }
931
- case ( ast:: def_variant ( _, ?id) ) { ns == ns_value }
932
- case ( ast:: def_ty ( ?id) ) { ns == ns_type }
933
- case ( ast:: def_binding ( ?id) ) { ns == ns_type }
934
- case ( ast:: def_use ( ?id) ) { true }
935
- case ( ast:: def_native_ty ( ?id) ) { ns == ns_type }
936
- case ( ast:: def_native_fn ( ?id) ) { ns == ns_value }
925
+ case ( ast:: def_fn ( ?id) ) { ns_value }
926
+ case ( ast:: def_obj ( ?id) ) { ns_value }
927
+ case ( ast:: def_obj_field ( ?id) ) { ns_value }
928
+ case ( ast:: def_mod ( ?id) ) { ns_module }
929
+ case ( ast:: def_native_mod ( ?id) ) { ns_module }
930
+ case ( ast:: def_const ( ?id) ) { ns_value }
931
+ case ( ast:: def_arg ( ?id) ) { ns_value }
932
+ case ( ast:: def_local ( ?id) ) { ns_value }
933
+ case ( ast:: def_variant ( _, ?id) ) { ns_value }
934
+ case ( ast:: def_ty ( ?id) ) { ns_type }
935
+ case ( ast:: def_binding ( ?id) ) { ns_type }
936
+ case ( ast:: def_use ( ?id) ) { ns_module }
937
+ case ( ast:: def_native_ty ( ?id) ) { ns_type }
938
+ case ( ast:: def_native_fn ( ?id) ) { ns_value }
937
939
} ;
938
940
}
939
941
940
942
fn lookup_external ( & env e, int cnum , vec[ ident] ids , namespace ns)
941
943
-> option:: t [ def ] {
942
944
for ( def d in creader:: lookup_defs( e. sess, cnum, ids) ) {
943
945
e. ext_map . insert ( ast:: def_id_of_def ( d) , ids) ;
944
- if ( check_def_by_ns ( d , ns ) ) { ret some ( d) ; }
946
+ if ( ns == ns_for_def ( d ) ) { ret some ( d) ; }
945
947
}
946
948
ret none[ def] ;
947
949
}
0 commit comments