1
Fork 0

Impl-ize interner.

This commit is contained in:
Paul Stansifer 2012-07-17 11:22:11 -07:00
parent a7125971c6
commit e040ab8423
7 changed files with 52 additions and 54 deletions

View file

@ -9,7 +9,6 @@ import tuple::extensions;
import ast::ident; import ast::ident;
import util::interner; import util::interner;
import interner::{intern, get};
import print::pprust; import print::pprust;
import pprust::{item_to_str, ty_to_str}; import pprust::{item_to_str, ty_to_str};
import ext::base::{mk_ctxt, ext_ctxt}; import ext::base::{mk_ctxt, ext_ctxt};

View file

@ -1,4 +1,4 @@
import util::interner::{interner,intern}; import util::interner::interner;
import diagnostic::span_handler; import diagnostic::span_handler;
import codemap::span; import codemap::span;
import ext::tt::transcribe::{tt_reader, new_tt_reader, dup_tt_reader, import ext::tt::transcribe::{tt_reader, new_tt_reader, dup_tt_reader,
@ -217,7 +217,7 @@ fn consume_any_line_comment(rdr: string_reader)
bump(rdr); bump(rdr);
} }
ret some({ ret some({
tok: token::DOC_COMMENT(intern(*rdr.interner, @acc)), tok: token::DOC_COMMENT(rdr.interner.intern(@acc)),
sp: ast_util::mk_sp(start_chpos, rdr.chpos) sp: ast_util::mk_sp(start_chpos, rdr.chpos)
}); });
} else { } else {
@ -262,7 +262,7 @@ fn consume_block_comment(rdr: string_reader)
bump(rdr); bump(rdr);
bump(rdr); bump(rdr);
ret some({ ret some({
tok: token::DOC_COMMENT(intern(*rdr.interner, @acc)), tok: token::DOC_COMMENT(rdr.interner.intern(@acc)),
sp: ast_util::mk_sp(start_chpos, rdr.chpos) sp: ast_util::mk_sp(start_chpos, rdr.chpos)
}); });
} }
@ -395,13 +395,11 @@ fn scan_number(c: char, rdr: string_reader) -> token::token {
if c == '3' && n == '2' { if c == '3' && n == '2' {
bump(rdr); bump(rdr);
bump(rdr); bump(rdr);
ret token::LIT_FLOAT(intern(*rdr.interner, @num_str), ret token::LIT_FLOAT(rdr.interner.intern(@num_str), ast::ty_f32);
ast::ty_f32);
} else if c == '6' && n == '4' { } else if c == '6' && n == '4' {
bump(rdr); bump(rdr);
bump(rdr); bump(rdr);
ret token::LIT_FLOAT(intern(*rdr.interner, @num_str), ret token::LIT_FLOAT(rdr.interner.intern(@num_str), ast::ty_f64);
ast::ty_f64);
/* FIXME (#2252): if this is out of range for either a /* FIXME (#2252): if this is out of range for either a
32-bit or 64-bit float, it won't be noticed till the 32-bit or 64-bit float, it won't be noticed till the
back-end. */ back-end. */
@ -410,8 +408,7 @@ fn scan_number(c: char, rdr: string_reader) -> token::token {
} }
} }
if is_float { if is_float {
ret token::LIT_FLOAT(intern(*rdr.interner, @num_str), ret token::LIT_FLOAT(rdr.interner.intern(@num_str), ast::ty_f);
ast::ty_f);
} else { } else {
if str::len(num_str) == 0u { if str::len(num_str) == 0u {
rdr.fatal(~"no valid digits found for number"); rdr.fatal(~"no valid digits found for number");
@ -459,7 +456,7 @@ fn next_token_inner(rdr: string_reader) -> token::token {
let is_mod_name = c == ':' && nextch(rdr) == ':'; let is_mod_name = c == ':' && nextch(rdr) == ':';
// FIXME: perform NFKC normalization here. (Issue #2253) // FIXME: perform NFKC normalization here. (Issue #2253)
ret token::IDENT(intern(*rdr.interner, @accum_str), is_mod_name); ret token::IDENT(rdr.interner.intern(@accum_str), is_mod_name);
} }
if is_dec_digit(c) { if is_dec_digit(c) {
ret scan_number(c, rdr); ret scan_number(c, rdr);
@ -623,7 +620,7 @@ fn next_token_inner(rdr: string_reader) -> token::token {
} }
} }
bump(rdr); bump(rdr);
ret token::LIT_STR(intern(*rdr.interner, @accum_str)); ret token::LIT_STR(rdr.interner.intern(@accum_str));
} }
'-' { '-' {
if nextch(rdr) == '>' { if nextch(rdr) == '>' {

View file

@ -224,7 +224,7 @@ class parser {
self.sess.span_diagnostic.span_warn(copy self.span, m) self.sess.span_diagnostic.span_warn(copy self.span, m)
} }
pure fn get_str(i: token::str_num) -> @~str { pure fn get_str(i: token::str_num) -> @~str {
interner::get(*self.reader.interner(), i) (*self.reader.interner()).get(i)
} }
fn get_id() -> node_id { next_node_id(self.sess) } fn get_id() -> node_id { next_node_id(self.sess) }
@ -2121,8 +2121,7 @@ class parser {
} }
if self.eat_keyword(~"of") { if self.eat_keyword(~"of") {
let for_atom = interner::intern(*self.reader.interner(), let for_atom = (*self.reader.interner()).intern(@~"for");
@~"for");
traits = self.parse_trait_ref_list traits = self.parse_trait_ref_list
(token::IDENT(for_atom, false)); (token::IDENT(for_atom, false));
if traits.len() >= 1 && option::is_none(ident_old) { if traits.len() >= 1 && option::is_none(ident_old) {

View file

@ -165,24 +165,16 @@ fn to_str(in: interner<@~str>, t: token) -> ~str {
LIT_INT_UNSUFFIXED(i) { LIT_INT_UNSUFFIXED(i) {
int::to_str(i as int, 10u) int::to_str(i as int, 10u)
} }
LIT_FLOAT(s, t) { LIT_FLOAT(s, t) { *in.get(s) + ast_util::float_ty_to_str(t) }
*interner::get(in, s) + LIT_STR(s) { ~"\"" + str::escape_default( *in.get(s)) + ~"\"" }
ast_util::float_ty_to_str(t)
}
LIT_STR(s) {
~"\""
+ str::escape_default(*interner::get(in, s))
+ ~"\""
}
/* Name components */ /* Name components */
IDENT(s, _) { IDENT(s, _) { *in.get(s) }
*interner::get(in, s)
}
UNDERSCORE { ~"_" } UNDERSCORE { ~"_" }
/* Other */ /* Other */
DOC_COMMENT(s) { *interner::get(in, s) } DOC_COMMENT(s) { *in.get(s) }
EOF { ~"<eof>" } EOF { ~"<eof>" }
INTERPOLATED(nt) { INTERPOLATED(nt) {
~"an interpolated " + ~"an interpolated " +

View file

@ -9,6 +9,7 @@ import ast::{required, provided};
import ast_util::operator_prec; import ast_util::operator_prec;
import dvec::{dvec, extensions}; import dvec::{dvec, extensions};
import parse::classify::*; import parse::classify::*;
import util::interner;
// The ps is stored here to prevent recursive type. // The ps is stored here to prevent recursive type.
enum ann_node { enum ann_node {
@ -27,6 +28,7 @@ fn no_ann() -> pp_ann {
type ps = type ps =
@{s: pp::printer, @{s: pp::printer,
cm: option<codemap>, cm: option<codemap>,
//in: interner::interner<@~str>,
comments: option<~[comments::cmnt]>, comments: option<~[comments::cmnt]>,
literals: option<~[comments::lit]>, literals: option<~[comments::lit]>,
mut cur_cmnt: uint, mut cur_cmnt: uint,
@ -47,6 +49,7 @@ fn end(s: ps) {
fn rust_printer(writer: io::writer) -> ps { fn rust_printer(writer: io::writer) -> ps {
ret @{s: pp::mk_printer(writer, default_columns), ret @{s: pp::mk_printer(writer, default_columns),
cm: none::<codemap>, cm: none::<codemap>,
//in: interner::mk::<@~str>(|x| str::hash(*x), |x,y| str::eq(*x, *y)),
comments: none::<~[comments::cmnt]>, comments: none::<~[comments::cmnt]>,
literals: none::<~[comments::lit]>, literals: none::<~[comments::lit]>,
mut cur_cmnt: 0u, mut cur_cmnt: 0u,
@ -63,7 +66,8 @@ const default_columns: uint = 78u;
// Requires you to pass an input filename and reader so that // Requires you to pass an input filename and reader so that
// it can scan the input text for comments and literals to // it can scan the input text for comments and literals to
// copy forward. // copy forward.
fn print_crate(cm: codemap, span_diagnostic: diagnostic::span_handler, fn print_crate(cm: codemap, //in: interner::interner<@~str>,
span_diagnostic: diagnostic::span_handler,
crate: @ast::crate, filename: ~str, in: io::reader, crate: @ast::crate, filename: ~str, in: io::reader,
out: io::writer, ann: pp_ann, is_expanded: bool) { out: io::writer, ann: pp_ann, is_expanded: bool) {
let r = comments::gather_comments_and_literals(span_diagnostic, let r = comments::gather_comments_and_literals(span_diagnostic,
@ -71,6 +75,7 @@ fn print_crate(cm: codemap, span_diagnostic: diagnostic::span_handler,
let s = let s =
@{s: pp::mk_printer(out, default_columns), @{s: pp::mk_printer(out, default_columns),
cm: some(cm), cm: some(cm),
//in: in,
comments: some(r.cmnts), comments: some(r.cmnts),
// If the code is post expansion, don't use the table of // If the code is post expansion, don't use the table of
// literals, since it doesn't correspond with the literals // literals, since it doesn't correspond with the literals

View file

@ -5,7 +5,7 @@ import std::map;
import std::map::{hashmap, hashfn, eqfn}; import std::map::{hashmap, hashfn, eqfn};
import dvec::{dvec, extensions}; import dvec::{dvec, extensions};
type interner<T: const> = type hash_interner<T: const> =
{map: hashmap<T, uint>, {map: hashmap<T, uint>,
vect: dvec<T>, vect: dvec<T>,
hasher: hashfn<T>, hasher: hashfn<T>,
@ -13,28 +13,34 @@ type interner<T: const> =
fn mk<T: const copy>(hasher: hashfn<T>, eqer: eqfn<T>) -> interner<T> { fn mk<T: const copy>(hasher: hashfn<T>, eqer: eqfn<T>) -> interner<T> {
let m = map::hashmap::<T, uint>(hasher, eqer); let m = map::hashmap::<T, uint>(hasher, eqer);
ret {map: m, vect: dvec(), hasher: hasher, eqer: eqer}; let hi: hash_interner<T> =
{map: m, vect: dvec(), hasher: hasher, eqer: eqer};
ret hi as interner::<T>;
} }
fn intern<T: const copy>(itr: interner<T>, val: T) -> uint { /* when traits can extend traits, we should extend index<uint,T> to get [] */
alt itr.map.find(val) { iface interner<T: const copy> {
some(idx) { ret idx; } fn intern(T) -> uint;
none { pure fn get(uint) -> T;
let new_idx = itr.vect.len(); fn len() -> uint;
itr.map.insert(val, new_idx); }
itr.vect.push(val);
ret new_idx; impl <T: const copy> of interner<T> for hash_interner<T> {
} fn intern(val: T) -> uint {
alt self.map.find(val) {
some(idx) { ret idx; }
none {
let new_idx = self.vect.len();
self.map.insert(val, new_idx);
self.vect.push(val);
ret new_idx;
}
}
} }
}
// |get| isn't "pure" in the traditional sense, because it can go from // this isn't "pure" in the traditional sense, because it can go from
// failing to returning a value as items are interned. But for typestate, // failing to returning a value as items are interned. But for typestate,
// where we first check a pred and then rely on it, ceasing to fail is ok. // where we first check a pred and then rely on it, ceasing to fail is ok.
pure fn get<T: const copy>(itr: interner<T>, idx: uint) -> T { pure fn get(idx: uint) -> T { self.vect.get_elt(idx) }
unchecked { fn len() -> uint { ret self.vect.len(); }
itr.vect.get_elt(idx) }
}
}
fn len<T: const>(itr: interner<T>) -> uint { ret itr.vect.len(); }

View file

@ -335,7 +335,7 @@ fn shape_of(ccx: @crate_ctxt, t: ty::t) -> ~[u8] {
else { ~[shape_struct] }, sub = ~[]; else { ~[shape_struct] }, sub = ~[];
do option::iter(m_dtor_did) |dtor_did| { do option::iter(m_dtor_did) |dtor_did| {
let ri = @{did: dtor_did, parent_id: some(did), tps: tps}; let ri = @{did: dtor_did, parent_id: some(did), tps: tps};
let id = interner::intern(ccx.shape_cx.resources, ri); let id = ccx.shape_cx.resources.intern(ri);
add_u16(s, id as u16); add_u16(s, id as u16);
}; };
for ty::class_items_as_mutable_fields(ccx.tcx, did, substs).each |f| { for ty::class_items_as_mutable_fields(ccx.tcx, did, substs).each |f| {
@ -569,9 +569,9 @@ fn gen_enum_shapes(ccx: @crate_ctxt) -> ValueRef {
fn gen_resource_shapes(ccx: @crate_ctxt) -> ValueRef { fn gen_resource_shapes(ccx: @crate_ctxt) -> ValueRef {
let mut dtors = ~[]; let mut dtors = ~[];
let len = interner::len(ccx.shape_cx.resources); let len = ccx.shape_cx.resources.len();
for uint::range(0u, len) |i| { for uint::range(0u, len) |i| {
let ri = interner::get(ccx.shape_cx.resources, i); let ri = ccx.shape_cx.resources.get(i);
for ri.tps.each() |s| { assert !ty::type_has_params(s); } for ri.tps.each() |s| { assert !ty::type_has_params(s); }
do option::iter(ri.parent_id) |id| { do option::iter(ri.parent_id) |id| {
dtors += ~[trans::base::get_res_dtor(ccx, ri.did, id, ri.tps)]; dtors += ~[trans::base::get_res_dtor(ccx, ri.did, id, ri.tps)];