Skip to content

Commit 62f4709

Browse files
committed
rustc: Resolve def ids in external crates
1 parent cc221c4 commit 62f4709

File tree

1 file changed

+61
-4
lines changed

1 file changed

+61
-4
lines changed

src/comp/front/creader.rs

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,54 @@ impure fn move_to_item(&ebml.reader ebml_r, ast.def_id did) {
364364
log #fmt("move_to_item: item not found: %d:%d", did._0, did._1);
365365
}
366366

367-
impure fn get_item_kind(ast.def_id did, vec[u8] data) -> u8 {
367+
// Looks up an item in the given metadata and returns an EBML reader pointing
368+
// to the item data.
369+
impure fn lookup_item(ast.def_id did, vec[u8] data) -> ebml.reader {
368370
auto io_r = io.new_reader_(io.new_byte_buf_reader(data));
369371
auto ebml_r = ebml.create_reader(io_r);
370372
move_to_item(ebml_r, did);
373+
ret ebml_r;
374+
}
375+
376+
impure fn get_item_kind(&ebml.reader ebml_r) -> u8 {
377+
while (ebml.bytes_left(ebml_r) > 0u) {
378+
auto ebml_tag = ebml.peek(ebml_r);
379+
if (ebml_tag.id == metadata.tag_items_kind) {
380+
ebml.move_to_first_child(ebml_r);
381+
auto kind_ch = ebml.read_data(ebml_r).(0);
382+
383+
// Reset the EBML reader so the callee can use it to look up
384+
// additional info about the item.
385+
ebml.move_to_parent(ebml_r);
386+
ebml.move_to_parent(ebml_r);
387+
ebml.move_to_first_child(ebml_r);
388+
389+
ret kind_ch;
390+
}
391+
ebml.move_to_next_sibling(ebml_r);
392+
}
393+
394+
log "get_item_kind(): no kind found";
395+
fail;
396+
}
397+
398+
impure fn get_variant_tag_id(&ebml.reader ebml_r) -> ast.def_id {
399+
while (ebml.bytes_left(ebml_r) > 0u) {
400+
auto ebml_tag = ebml.peek(ebml_r);
401+
if (ebml_tag.id == metadata.tag_items_tag_id) {
402+
ebml.move_to_first_child(ebml_r);
403+
auto tid = parse_def_id(ebml.read_data(ebml_r));
404+
405+
// Be kind, rewind.
406+
ebml.move_to_parent(ebml_r);
407+
ebml.move_to_parent(ebml_r);
408+
ebml.move_to_first_child(ebml_r);
371409

372-
log "found item";
410+
ret tid;
411+
}
412+
}
413+
414+
log "get_variant_tag_id(): no tag ID found";
373415
fail;
374416
}
375417

@@ -459,9 +501,24 @@ fn lookup_def(session.session sess, &span sp, int cnum, vec[ast.ident] path)
459501
}
460502
}
461503

462-
// TODO: Look up item type, use that to determine the type of def.
463-
auto kind_ch = get_item_kind(did, data);
504+
auto ebml_r = lookup_item(did, data);
505+
auto kind_ch = get_item_kind(ebml_r);
506+
507+
did = tup(cnum, did._1);
508+
509+
// FIXME: It'd be great if we had u8 char literals.
510+
if (kind_ch == ('c' as u8)) { ret ast.def_const(did); }
511+
else if (kind_ch == ('f' as u8)) { ret ast.def_fn(did); }
512+
else if (kind_ch == ('y' as u8)) { ret ast.def_ty(did); }
513+
else if (kind_ch == ('o' as u8)) { ret ast.def_obj(did); }
514+
else if (kind_ch == ('t' as u8)) { ret ast.def_ty(did); }
515+
else if (kind_ch == ('v' as u8)) {
516+
auto tid = get_variant_tag_id(ebml_r);
517+
tid = tup(cnum, tid._1);
518+
ret ast.def_variant(tid, did);
519+
}
464520

521+
log #fmt("lookup_def(): unknown kind char: %d", kind_ch as int);
465522
fail;
466523
}
467524

0 commit comments

Comments
 (0)