Skip to content

Commit a02b10a

Browse files
committed
Refactored ast_map and friends, mainly to have Paths without storing them.
1 parent 22c34f3 commit a02b10a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+1985
-2571
lines changed

src/libfourcc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) ->
7474

7575
let little = match endian {
7676
None => false,
77-
Some(Ident{ident, span}) => match token::get_ident(ident.name).get() {
77+
Some(Ident{ident, span}) => match token::get_ident(ident).get() {
7878
"little" => true,
7979
"big" => false,
8080
"target" => target_endian_little(cx, sp),

src/librustc/back/link.rs

+52-78
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ use serialize::hex::ToHex;
3838
use extra::tempfile::TempDir;
3939
use syntax::abi;
4040
use syntax::ast;
41-
use syntax::ast_map::{PathMod, PathName, PathPrettyName};
41+
use syntax::ast_map::{PathElem, PathElems, PathName};
4242
use syntax::ast_map;
4343
use syntax::attr;
4444
use syntax::attr::AttrMetaMethods;
4545
use syntax::crateid::CrateId;
46+
use syntax::parse::token;
4647

4748
#[deriving(Clone, Eq, TotalOrd, TotalEq)]
4849
pub enum OutputType {
@@ -531,11 +532,8 @@ fn truncated_hash_result(symbol_hasher: &mut Sha256) -> ~str {
531532

532533

533534
// This calculates STH for a symbol, as defined above
534-
pub fn symbol_hash(tcx: ty::ctxt,
535-
symbol_hasher: &mut Sha256,
536-
t: ty::t,
537-
link_meta: &LinkMeta)
538-
-> ~str {
535+
fn symbol_hash(tcx: ty::ctxt, symbol_hasher: &mut Sha256,
536+
t: ty::t, link_meta: &LinkMeta) -> ~str {
539537
// NB: do *not* use abbrevs here as we want the symbol names
540538
// to be independent of one another in the crate.
541539

@@ -551,13 +549,10 @@ pub fn symbol_hash(tcx: ty::ctxt,
551549
hash
552550
}
553551

554-
pub fn get_symbol_hash(ccx: &CrateContext, t: ty::t) -> ~str {
555-
{
556-
let type_hashcodes = ccx.type_hashcodes.borrow();
557-
match type_hashcodes.get().find(&t) {
558-
Some(h) => return h.to_str(),
559-
None => {}
560-
}
552+
fn get_symbol_hash(ccx: &CrateContext, t: ty::t) -> ~str {
553+
match ccx.type_hashcodes.borrow().get().find(&t) {
554+
Some(h) => return h.to_str(),
555+
None => {}
561556
}
562557

563558
let mut type_hashcodes = ccx.type_hashcodes.borrow_mut();
@@ -615,8 +610,9 @@ pub fn sanitize(s: &str) -> ~str {
615610
return result;
616611
}
617612

618-
pub fn mangle(sess: Session, ss: ast_map::Path,
619-
hash: Option<&str>, vers: Option<&str>) -> ~str {
613+
pub fn mangle<PI: Iterator<PathElem>>(mut path: PI,
614+
hash: Option<&str>,
615+
vers: Option<&str>) -> ~str {
620616
// Follow C++ namespace-mangling style, see
621617
// http://en.wikipedia.org/wiki/Name_mangling for more info.
622618
//
@@ -625,49 +621,27 @@ pub fn mangle(sess: Session, ss: ast_map::Path,
625621
// when using unix's linker. Perhaps one day when we just use a linker from LLVM
626622
// we won't need to do this name mangling. The problem with name mangling is
627623
// that it seriously limits the available characters. For example we can't
628-
// have things like @T or ~[T] in symbol names when one would theoretically
624+
// have things like &T or ~[T] in symbol names when one would theoretically
629625
// want them for things like impls of traits on that type.
630626
//
631627
// To be able to work on all platforms and get *some* reasonable output, we
632628
// use C++ name-mangling.
633629

634630
let mut n = ~"_ZN"; // _Z == Begin name-sequence, N == nested
635631

636-
let push = |n: &mut ~str, s: &str| {
632+
fn push(n: &mut ~str, s: &str) {
637633
let sani = sanitize(s);
638634
n.push_str(format!("{}{}", sani.len(), sani));
639-
};
635+
}
640636

641637
// First, connect each component with <len, name> pairs.
642-
for s in ss.iter() {
643-
match *s {
644-
PathName(s) | PathMod(s) | PathPrettyName(s, _) => {
645-
push(&mut n, sess.str_of(s))
646-
}
647-
}
638+
for e in path {
639+
push(&mut n, token::get_name(e.name()).get().as_slice())
648640
}
649641

650-
// next, if any identifiers are "pretty" and need extra information tacked
651-
// on, then use the hash to generate two unique characters. For now
652-
// hopefully 2 characters is enough to avoid collisions.
653-
static EXTRA_CHARS: &'static str =
654-
"abcdefghijklmnopqrstuvwxyz\
655-
ABCDEFGHIJKLMNOPQRSTUVWXYZ\
656-
0123456789";
657-
let mut hash = match hash { Some(s) => s.to_owned(), None => ~"" };
658-
for s in ss.iter() {
659-
match *s {
660-
PathPrettyName(_, extra) => {
661-
let hi = (extra >> 32) as u32 as uint;
662-
let lo = extra as u32 as uint;
663-
hash.push_char(EXTRA_CHARS[hi % EXTRA_CHARS.len()] as char);
664-
hash.push_char(EXTRA_CHARS[lo % EXTRA_CHARS.len()] as char);
665-
}
666-
_ => {}
667-
}
668-
}
669-
if hash.len() > 0 {
670-
push(&mut n, hash);
642+
match hash {
643+
Some(s) => push(&mut n, s),
644+
None => {}
671645
}
672646
match vers {
673647
Some(s) => push(&mut n, s),
@@ -678,10 +652,7 @@ pub fn mangle(sess: Session, ss: ast_map::Path,
678652
n
679653
}
680654

681-
pub fn exported_name(sess: Session,
682-
path: ast_map::Path,
683-
hash: &str,
684-
vers: &str) -> ~str {
655+
pub fn exported_name(path: PathElems, hash: &str, vers: &str) -> ~str {
685656
// The version will get mangled to have a leading '_', but it makes more
686657
// sense to lead with a 'v' b/c this is a version...
687658
let vers = if vers.len() > 0 && !char::is_XID_start(vers.char_at(0)) {
@@ -690,53 +661,56 @@ pub fn exported_name(sess: Session,
690661
vers.to_owned()
691662
};
692663

693-
mangle(sess, path, Some(hash), Some(vers.as_slice()))
664+
mangle(path, Some(hash), Some(vers.as_slice()))
694665
}
695666

696-
pub fn mangle_exported_name(ccx: &CrateContext,
697-
path: ast_map::Path,
698-
t: ty::t) -> ~str {
699-
let hash = get_symbol_hash(ccx, t);
700-
return exported_name(ccx.sess, path,
701-
hash,
702-
ccx.link_meta.crateid.version_or_default());
667+
pub fn mangle_exported_name(ccx: &CrateContext, path: PathElems,
668+
t: ty::t, id: ast::NodeId) -> ~str {
669+
let mut hash = get_symbol_hash(ccx, t);
670+
671+
// Paths can be completely identical for different nodes,
672+
// e.g. `fn foo() { { fn a() {} } { fn a() {} } }`, so we
673+
// generate unique characters from the node id. For now
674+
// hopefully 3 characters is enough to avoid collisions.
675+
static EXTRA_CHARS: &'static str =
676+
"abcdefghijklmnopqrstuvwxyz\
677+
ABCDEFGHIJKLMNOPQRSTUVWXYZ\
678+
0123456789";
679+
let id = id as uint;
680+
let extra1 = id % EXTRA_CHARS.len();
681+
let id = id / EXTRA_CHARS.len();
682+
let extra2 = id % EXTRA_CHARS.len();
683+
let id = id / EXTRA_CHARS.len();
684+
let extra3 = id % EXTRA_CHARS.len();
685+
hash.push_char(EXTRA_CHARS[extra1] as char);
686+
hash.push_char(EXTRA_CHARS[extra2] as char);
687+
hash.push_char(EXTRA_CHARS[extra3] as char);
688+
689+
exported_name(path, hash, ccx.link_meta.crateid.version_or_default())
703690
}
704691

705692
pub fn mangle_internal_name_by_type_only(ccx: &CrateContext,
706693
t: ty::t,
707694
name: &str) -> ~str {
708695
let s = ppaux::ty_to_short_str(ccx.tcx, t);
696+
let path = [PathName(token::intern(name)),
697+
PathName(token::intern(s))];
709698
let hash = get_symbol_hash(ccx, t);
710-
return mangle(ccx.sess,
711-
~[PathName(ccx.sess.ident_of(name)),
712-
PathName(ccx.sess.ident_of(s))],
713-
Some(hash.as_slice()),
714-
None);
699+
mangle(ast_map::Values(path.iter()), Some(hash.as_slice()), None)
715700
}
716701

717702
pub fn mangle_internal_name_by_type_and_seq(ccx: &CrateContext,
718703
t: ty::t,
719704
name: &str) -> ~str {
720705
let s = ppaux::ty_to_str(ccx.tcx, t);
706+
let path = [PathName(token::intern(s)),
707+
gensym_name(name)];
721708
let hash = get_symbol_hash(ccx, t);
722-
let (_, name) = gensym_name(name);
723-
return mangle(ccx.sess,
724-
~[PathName(ccx.sess.ident_of(s)), name],
725-
Some(hash.as_slice()),
726-
None);
727-
}
728-
729-
pub fn mangle_internal_name_by_path_and_seq(ccx: &CrateContext,
730-
mut path: ast_map::Path,
731-
flav: &str) -> ~str {
732-
let (_, name) = gensym_name(flav);
733-
path.push(name);
734-
mangle(ccx.sess, path, None, None)
709+
mangle(ast_map::Values(path.iter()), Some(hash.as_slice()), None)
735710
}
736711

737-
pub fn mangle_internal_name_by_path(ccx: &CrateContext,
738-
path: ast_map::Path) -> ~str {
739-
mangle(ccx.sess, path, None, None)
712+
pub fn mangle_internal_name_by_path_and_seq(path: PathElems, flav: &str) -> ~str {
713+
mangle(path.chain(Some(gensym_name(flav)).move_iter()), None, None)
740714
}
741715

742716
pub fn output_lib_filename(lm: &LinkMeta) -> ~str {

src/librustc/driver/driver.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ pub fn phase_3_run_analysis_passes(sess: Session,
264264
|_| middle::resolve_lifetime::krate(sess, krate));
265265

266266
time(time_passes, "looking for entry point", (),
267-
|_| middle::entry::find_entry_point(sess, krate, ast_map));
267+
|_| middle::entry::find_entry_point(sess, krate, &ast_map));
268268

269269
sess.macro_registrar_fn.with_mut(|r| *r =
270270
time(time_passes, "looking for macro registrar", (), |_|
@@ -288,7 +288,7 @@ pub fn phase_3_run_analysis_passes(sess: Session,
288288
middle::const_eval::process_crate(krate, ty_cx));
289289

290290
time(time_passes, "const checking", (), |_|
291-
middle::check_const::check_crate(sess, krate, ast_map, def_map,
291+
middle::check_const::check_crate(sess, krate, def_map,
292292
method_map, ty_cx));
293293

294294
let maps = (external_exports, last_private_map);
@@ -638,7 +638,6 @@ pub fn pretty_print_input(sess: Session,
638638
let mut rdr = MemReader::new(src.as_bytes().to_owned());
639639
let stdout = io::stdout();
640640
pprust::print_crate(sess.codemap,
641-
token::get_ident_interner(),
642641
sess.span_diagnostic,
643642
&krate,
644643
source_name(input),
@@ -1135,7 +1134,6 @@ pub fn early_error(msg: &str) -> ! {
11351134
pub fn list_metadata(sess: Session, path: &Path,
11361135
out: &mut io::Writer) -> io::IoResult<()> {
11371136
metadata::loader::list_file_metadata(
1138-
token::get_ident_interner(),
11391137
session::sess_os_to_meta_os(sess.targ_cfg.os), path, out)
11401138
}
11411139

src/librustc/driver/session.rs

+1-20
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ use syntax::ast::{IntTy, UintTy};
2222
use syntax::codemap::Span;
2323
use syntax::diagnostic;
2424
use syntax::parse::ParseSess;
25-
use syntax::{ast, codemap};
26-
use syntax::abi;
27-
use syntax::parse::token;
25+
use syntax::{abi, ast, codemap};
2826
use syntax;
2927

3028
use std::cell::{Cell, RefCell};
@@ -301,23 +299,6 @@ impl Session_ {
301299
pub fn show_span(&self) -> bool {
302300
self.debugging_opt(SHOW_SPAN)
303301
}
304-
305-
// DEPRECATED. This function results in a lot of allocations when they
306-
// are not necessary.
307-
pub fn str_of(&self, id: ast::Ident) -> ~str {
308-
let string = token::get_ident(id.name);
309-
string.get().to_str()
310-
}
311-
312-
// pointless function, now...
313-
pub fn ident_of(&self, st: &str) -> ast::Ident {
314-
token::str_to_ident(st)
315-
}
316-
317-
// pointless function, now...
318-
pub fn intr(&self) -> @syntax::parse::token::IdentInterner {
319-
token::get_ident_interner()
320-
}
321302
}
322303

323304
/// Some reasonable defaults

src/librustc/front/assign_node_ids_and_map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ impl ast_map::FoldOps for NodeIdAssigner {
2525
}
2626

2727
pub fn assign_node_ids_and_map(sess: Session, krate: ast::Crate) -> (ast::Crate, ast_map::Map) {
28-
ast_map::map_crate(sess.diagnostic(), krate, NodeIdAssigner { sess: sess })
28+
ast_map::map_crate(krate, NodeIdAssigner { sess: sess })
2929
}

src/librustc/front/feature_gate.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ impl Context {
9999

100100
impl Visitor<()> for Context {
101101
fn visit_ident(&mut self, sp: Span, id: ast::Ident, _: ()) {
102-
let string = token::get_ident(id.name);
103-
let s = string.get();
104-
105-
if !s.is_ascii() {
102+
if !token::get_ident(id).get().is_ascii() {
106103
self.gate_feature("non_ascii_idents", sp,
107104
"non-ascii idents are not fully supported.");
108105
}
@@ -196,29 +193,29 @@ impl Visitor<()> for Context {
196193
let msg = " is not stable enough for use and are subject to change";
197194

198195

199-
if id == self.sess.ident_of("macro_rules") {
196+
if id == token::str_to_ident("macro_rules") {
200197
self.gate_feature("macro_rules", path.span, "macro definitions are \
201198
not stable enough for use and are subject to change");
202199
}
203200

204-
else if id == self.sess.ident_of("asm") {
201+
else if id == token::str_to_ident("asm") {
205202
self.gate_feature("asm", path.span, "inline assembly is not \
206203
stable enough for use and is subject to change");
207204
}
208205

209-
else if id == self.sess.ident_of("log_syntax") {
206+
else if id == token::str_to_ident("log_syntax") {
210207
self.gate_feature("log_syntax", path.span, "`log_syntax!` is not \
211208
stable enough for use and is subject to change");
212209
}
213210

214-
else if id == self.sess.ident_of("trace_macros") {
211+
else if id == token::str_to_ident("trace_macros") {
215212
self.gate_feature("trace_macros", path.span, "`trace_macros` is not \
216213
stable enough for use and is subject to change");
217214
}
218215

219216
else {
220217
for &quote in quotes.iter() {
221-
if id == self.sess.ident_of(quote) {
218+
if id == token::str_to_ident(quote) {
222219
self.gate_feature("quote", path.span, quote + msg);
223220
}
224221
}

src/librustc/front/std_inject.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub fn with_version(krate: &str) -> Option<(InternedString, ast::StrStyle)> {
7373
impl fold::Folder for StandardLibraryInjector {
7474
fn fold_crate(&mut self, krate: ast::Crate) -> ast::Crate {
7575
let mut vis = ~[ast::ViewItem {
76-
node: ast::ViewItemExternMod(self.sess.ident_of("std"),
76+
node: ast::ViewItemExternMod(token::str_to_ident("std"),
7777
with_version("std"),
7878
ast::DUMMY_NODE_ID),
7979
attrs: ~[
@@ -90,15 +90,15 @@ impl fold::Folder for StandardLibraryInjector {
9090

9191
if use_uv(&krate) && !self.sess.building_library.get() {
9292
vis.push(ast::ViewItem {
93-
node: ast::ViewItemExternMod(self.sess.ident_of("green"),
93+
node: ast::ViewItemExternMod(token::str_to_ident("green"),
9494
with_version("green"),
9595
ast::DUMMY_NODE_ID),
9696
attrs: ~[],
9797
vis: ast::Inherited,
9898
span: DUMMY_SP
9999
});
100100
vis.push(ast::ViewItem {
101-
node: ast::ViewItemExternMod(self.sess.ident_of("rustuv"),
101+
node: ast::ViewItemExternMod(token::str_to_ident("rustuv"),
102102
with_version("rustuv"),
103103
ast::DUMMY_NODE_ID),
104104
attrs: ~[],
@@ -163,12 +163,12 @@ impl fold::Folder for PreludeInjector {
163163
global: false,
164164
segments: ~[
165165
ast::PathSegment {
166-
identifier: self.sess.ident_of("std"),
166+
identifier: token::str_to_ident("std"),
167167
lifetimes: opt_vec::Empty,
168168
types: opt_vec::Empty,
169169
},
170170
ast::PathSegment {
171-
identifier: self.sess.ident_of("prelude"),
171+
identifier: token::str_to_ident("prelude"),
172172
lifetimes: opt_vec::Empty,
173173
types: opt_vec::Empty,
174174
},

0 commit comments

Comments
 (0)