Skip to content

Commit 092c507

Browse files
committed
ast: make Name its own type
1 parent f512779 commit 092c507

File tree

11 files changed

+149
-106
lines changed

11 files changed

+149
-106
lines changed

src/librustc/lint/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ impl UnusedMut {
11141114
match mode {
11151115
ast::BindByValue(ast::MutMutable) => {
11161116
if !token::get_ident(ident).get().starts_with("_") {
1117-
mutables.insert_or_update_with(ident.name as uint,
1117+
mutables.insert_or_update_with(ident.name.uint(),
11181118
vec!(id), |_, old| { old.push(id); });
11191119
}
11201120
}

src/librustc/metadata/decoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ fn item_name(intr: &IdentInterner, item: ebml::Doc) -> ast::Ident {
323323
let string = name.as_str_slice();
324324
match intr.find_equiv(&string) {
325325
None => token::str_to_ident(string),
326-
Some(val) => ast::Ident::new(val as ast::Name),
326+
Some(val) => ast::Ident::new(val),
327327
}
328328
}
329329

src/libsyntax/ast.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ impl Ident {
5252
pub fn new(name: Name) -> Ident { Ident {name: name, ctxt: EMPTY_CTXT}}
5353

5454
pub fn as_str<'a>(&'a self) -> &'a str {
55-
unsafe {
56-
// FIXME #12938: can't use copy_lifetime since &str isn't a &T
57-
::std::mem::transmute(token::get_ident(*self).get())
58-
}
55+
self.name.as_str()
5956
}
6057
}
6158

@@ -109,7 +106,26 @@ pub static ILLEGAL_CTXT : SyntaxContext = 1;
109106

110107
/// A name is a part of an identifier, representing a string or gensym. It's
111108
/// the result of interning.
112-
pub type Name = u32;
109+
#[deriving(Eq, Ord, PartialEq, PartialOrd, Hash, Encodable, Decodable, Clone, Show)]
110+
pub struct Name(pub u32);
111+
112+
impl Name {
113+
pub fn as_str<'a>(&'a self) -> &'a str {
114+
unsafe {
115+
// FIXME #12938: can't use copy_lifetime since &str isn't a &T
116+
::std::mem::transmute(token::get_name(*self).get())
117+
}
118+
}
119+
120+
pub fn uint(&self) -> uint {
121+
let Name(nm) = *self;
122+
nm as uint
123+
}
124+
125+
pub fn ident(&self) -> Ident {
126+
Ident { name: *self, ctxt: 0 }
127+
}
128+
}
113129

114130
/// A mark represents a unique id associated with a macro expansion
115131
pub type Mrk = u32;

src/libsyntax/ext/base.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,9 @@ impl<'a> ExtCtxt<'a> {
535535
pub fn ident_of(&self, st: &str) -> ast::Ident {
536536
str_to_ident(st)
537537
}
538+
pub fn name_of(&self, st: &str) -> ast::Name {
539+
token::intern(st)
540+
}
538541
}
539542

540543
/// Extract a string literal from the macro expanded version of `expr`,

src/libsyntax/ext/mtwt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ fn apply_rename_internal(id: Ident,
8282
to: Name,
8383
ctxt: SyntaxContext,
8484
table: &SCTable) -> SyntaxContext {
85-
let key = (ctxt,id,to);
86-
let new_ctxt = |_: &(SyntaxContext, Ident, Mrk)|
85+
let key = (ctxt, id, to);
86+
let new_ctxt = |_: &(SyntaxContext, Ident, Name)|
8787
idx_push(&mut *table.table.borrow_mut(), Rename(id, to, ctxt));
8888

8989
*table.rename_memo.borrow_mut().find_or_insert_with(key, new_ctxt)
@@ -142,7 +142,7 @@ pub fn clear_tables() {
142142
}
143143

144144
/// Add a value to the end of a vec, return its index
145-
fn idx_push<T>(vec: &mut Vec<T> , val: T) -> u32 {
145+
fn idx_push<T>(vec: &mut Vec<T>, val: T) -> u32 {
146146
vec.push(val);
147147
(vec.len() - 1) as u32
148148
}

src/libsyntax/ext/quote.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,15 @@ fn mk_ident(cx: &ExtCtxt, sp: Span, ident: ast::Ident) -> Gc<ast::Expr> {
363363
vec!(e_str))
364364
}
365365

366+
// Lift a name to the expr that evaluates to that name
367+
fn mk_name(cx: &ExtCtxt, sp: Span, ident: ast::Ident) -> Gc<ast::Expr> {
368+
let e_str = cx.expr_str(sp, token::get_ident(ident));
369+
cx.expr_method_call(sp,
370+
cx.expr_ident(sp, id_ext("ext_cx")),
371+
id_ext("name_of"),
372+
vec!(e_str))
373+
}
374+
366375
fn mk_ast_path(cx: &ExtCtxt, sp: Span, name: &str) -> Gc<ast::Expr> {
367376
let idents = vec!(id_ext("syntax"), id_ext("ast"), id_ext(name));
368377
cx.expr_path(cx.path_global(sp, idents))
@@ -401,37 +410,37 @@ fn mk_token(cx: &ExtCtxt, sp: Span, tok: &token::Token) -> Gc<ast::Expr> {
401410
}
402411

403412
LIT_BYTE(i) => {
404-
let e_byte = mk_ident(cx, sp, i);
413+
let e_byte = mk_name(cx, sp, i.ident());
405414

406415
return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_BYTE"), vec!(e_byte));
407416
}
408417

409418
LIT_CHAR(i) => {
410-
let e_char = mk_ident(cx, sp, i);
419+
let e_char = mk_name(cx, sp, i.ident());
411420

412421
return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_CHAR"), vec!(e_char));
413422
}
414423

415424
LIT_INTEGER(i) => {
416-
let e_int = mk_ident(cx, sp, i);
425+
let e_int = mk_name(cx, sp, i.ident());
417426
return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_INTEGER"), vec!(e_int));
418427
}
419428

420429
LIT_FLOAT(fident) => {
421-
let e_fident = mk_ident(cx, sp, fident);
430+
let e_fident = mk_name(cx, sp, fident.ident());
422431
return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_FLOAT"), vec!(e_fident));
423432
}
424433

425434
LIT_STR(ident) => {
426435
return cx.expr_call(sp,
427436
mk_token_path(cx, sp, "LIT_STR"),
428-
vec!(mk_ident(cx, sp, ident)));
437+
vec!(mk_name(cx, sp, ident.ident())));
429438
}
430439

431440
LIT_STR_RAW(ident, n) => {
432441
return cx.expr_call(sp,
433442
mk_token_path(cx, sp, "LIT_STR_RAW"),
434-
vec!(mk_ident(cx, sp, ident), cx.expr_uint(sp, n)));
443+
vec!(mk_name(cx, sp, ident.ident()), cx.expr_uint(sp, n)));
435444
}
436445

437446
IDENT(ident, b) => {
@@ -449,7 +458,7 @@ fn mk_token(cx: &ExtCtxt, sp: Span, tok: &token::Token) -> Gc<ast::Expr> {
449458
DOC_COMMENT(ident) => {
450459
return cx.expr_call(sp,
451460
mk_token_path(cx, sp, "DOC_COMMENT"),
452-
vec!(mk_ident(cx, sp, ident)));
461+
vec!(mk_name(cx, sp, ident.ident())));
453462
}
454463

455464
INTERPOLATED(_) => fail!("quote! with interpolated token"),

src/libsyntax/parse/attr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl<'a> ParserAttr for Parser<'a> {
4343
token::DOC_COMMENT(s) => {
4444
let attr = ::attr::mk_sugared_doc_attr(
4545
attr::mk_attr_id(),
46-
self.id_to_interned_str(s),
46+
self.id_to_interned_str(s.ident()),
4747
self.span.lo,
4848
self.span.hi
4949
);
@@ -139,7 +139,7 @@ impl<'a> ParserAttr for Parser<'a> {
139139
let Span { lo, hi, .. } = self.span;
140140
self.bump();
141141
attr::mk_sugared_doc_attr(attr::mk_attr_id(),
142-
self.id_to_interned_str(s),
142+
self.id_to_interned_str(s.ident()),
143143
lo,
144144
hi)
145145
}

src/libsyntax/parse/lexer/mod.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -216,18 +216,18 @@ impl<'a> StringReader<'a> {
216216
self.with_str_from_to(start, self.last_pos, f)
217217
}
218218

219-
/// Create an Ident from a given offset to the current offset, each
219+
/// Create a Name from a given offset to the current offset, each
220220
/// adjusted 1 towards each other (assumes that on either side there is a
221221
/// single-byte delimiter).
222-
pub fn ident_from(&self, start: BytePos) -> ast::Ident {
222+
pub fn name_from(&self, start: BytePos) -> ast::Name {
223223
debug!("taking an ident from {} to {}", start, self.last_pos);
224-
self.with_str_from(start, str_to_ident)
224+
self.with_str_from(start, token::intern)
225225
}
226226

227-
/// As ident_from, with an explicit endpoint.
228-
pub fn ident_from_to(&self, start: BytePos, end: BytePos) -> ast::Ident {
227+
/// As name_from, with an explicit endpoint.
228+
pub fn name_from_to(&self, start: BytePos, end: BytePos) -> ast::Name {
229229
debug!("taking an ident from {} to {}", start, end);
230-
self.with_str_from_to(start, end, str_to_ident)
230+
self.with_str_from_to(start, end, token::intern)
231231
}
232232

233233
/// Calls `f` with a string slice of the source text spanning from `start`
@@ -377,7 +377,7 @@ impl<'a> StringReader<'a> {
377377
return self.with_str_from(start_bpos, |string| {
378378
// but comments with only more "/"s are not
379379
let tok = if is_doc_comment(string) {
380-
token::DOC_COMMENT(str_to_ident(string))
380+
token::DOC_COMMENT(token::intern(string))
381381
} else {
382382
token::COMMENT
383383
};
@@ -421,7 +421,7 @@ impl<'a> StringReader<'a> {
421421
let start = self.last_pos;
422422
while !self.curr_is('\n') && !self.is_eof() { self.bump(); }
423423
return Some(TokenAndSpan {
424-
tok: token::SHEBANG(self.ident_from(start)),
424+
tok: token::SHEBANG(self.name_from(start)),
425425
sp: codemap::mk_sp(start, self.last_pos)
426426
});
427427
}
@@ -500,7 +500,7 @@ impl<'a> StringReader<'a> {
500500
self.translate_crlf(start_bpos, string,
501501
"bare CR not allowed in block doc-comment")
502502
} else { string.into_maybe_owned() };
503-
token::DOC_COMMENT(str_to_ident(string.as_slice()))
503+
token::DOC_COMMENT(token::intern(string.as_slice()))
504504
} else {
505505
token::COMMENT
506506
};
@@ -548,17 +548,17 @@ impl<'a> StringReader<'a> {
548548
}
549549
'u' | 'i' => {
550550
self.scan_int_suffix();
551-
return token::LIT_INTEGER(self.ident_from(start_bpos));
551+
return token::LIT_INTEGER(self.name_from(start_bpos));
552552
},
553553
'f' => {
554554
let last_pos = self.last_pos;
555555
self.scan_float_suffix();
556556
self.check_float_base(start_bpos, last_pos, base);
557-
return token::LIT_FLOAT(self.ident_from(start_bpos));
557+
return token::LIT_FLOAT(self.name_from(start_bpos));
558558
}
559559
_ => {
560560
// just a 0
561-
return token::LIT_INTEGER(self.ident_from(start_bpos));
561+
return token::LIT_INTEGER(self.name_from(start_bpos));
562562
}
563563
}
564564
} else if c.is_digit_radix(10) {
@@ -571,7 +571,7 @@ impl<'a> StringReader<'a> {
571571
self.err_span_(start_bpos, self.last_pos, "no valid digits found for number");
572572
// eat any suffix
573573
self.scan_int_suffix();
574-
return token::LIT_INTEGER(str_to_ident("0"));
574+
return token::LIT_INTEGER(token::intern("0"));
575575
}
576576

577577
// might be a float, but don't be greedy if this is actually an
@@ -589,25 +589,25 @@ impl<'a> StringReader<'a> {
589589
}
590590
let last_pos = self.last_pos;
591591
self.check_float_base(start_bpos, last_pos, base);
592-
return token::LIT_FLOAT(self.ident_from(start_bpos));
592+
return token::LIT_FLOAT(self.name_from(start_bpos));
593593
} else if self.curr_is('f') {
594594
// or it might be an integer literal suffixed as a float
595595
self.scan_float_suffix();
596596
let last_pos = self.last_pos;
597597
self.check_float_base(start_bpos, last_pos, base);
598-
return token::LIT_FLOAT(self.ident_from(start_bpos));
598+
return token::LIT_FLOAT(self.name_from(start_bpos));
599599
} else {
600600
// it might be a float if it has an exponent
601601
if self.curr_is('e') || self.curr_is('E') {
602602
self.scan_float_exponent();
603603
self.scan_float_suffix();
604604
let last_pos = self.last_pos;
605605
self.check_float_base(start_bpos, last_pos, base);
606-
return token::LIT_FLOAT(self.ident_from(start_bpos));
606+
return token::LIT_FLOAT(self.name_from(start_bpos));
607607
}
608608
// but we certainly have an integer!
609609
self.scan_int_suffix();
610-
return token::LIT_INTEGER(self.ident_from(start_bpos));
610+
return token::LIT_INTEGER(self.name_from(start_bpos));
611611
}
612612
}
613613

@@ -980,7 +980,7 @@ impl<'a> StringReader<'a> {
980980
start - BytePos(1), last_bpos,
981981
"unterminated character constant".to_string());
982982
}
983-
let id = if valid { self.ident_from(start) } else { str_to_ident("0") };
983+
let id = if valid { self.name_from(start) } else { token::intern("0") };
984984
self.bump(); // advance curr past token
985985
return token::LIT_CHAR(id);
986986
}
@@ -1010,8 +1010,8 @@ impl<'a> StringReader<'a> {
10101010
valid &= self.scan_char_or_byte(ch_start, ch, /* ascii_only = */ false, '"');
10111011
}
10121012
// adjust for the ACSII " at the start of the literal
1013-
let id = if valid { self.ident_from(start_bpos + BytePos(1)) }
1014-
else { str_to_ident("??") };
1013+
let id = if valid { self.name_from(start_bpos + BytePos(1)) }
1014+
else { token::intern("??") };
10151015
self.bump();
10161016
return token::LIT_STR(id);
10171017
}
@@ -1076,9 +1076,9 @@ impl<'a> StringReader<'a> {
10761076
}
10771077
self.bump();
10781078
let id = if valid {
1079-
self.ident_from_to(content_start_bpos, content_end_bpos)
1079+
self.name_from_to(content_start_bpos, content_end_bpos)
10801080
} else {
1081-
str_to_ident("??")
1081+
token::intern("??")
10821082
};
10831083
return token::LIT_STR_RAW(id, hash_count);
10841084
}
@@ -1168,7 +1168,7 @@ impl<'a> StringReader<'a> {
11681168
"unterminated byte constant".to_string());
11691169
}
11701170

1171-
let id = if valid { self.ident_from(start) } else { str_to_ident("??") };
1171+
let id = if valid { self.name_from(start) } else { token::intern("??") };
11721172
self.bump(); // advance curr past token
11731173
return token::LIT_BYTE(id);
11741174
}
@@ -1190,7 +1190,7 @@ impl<'a> StringReader<'a> {
11901190
self.bump();
11911191
valid &= self.scan_char_or_byte(ch_start, ch, /* ascii_only = */ true, '"');
11921192
}
1193-
let id = if valid { self.ident_from(start) } else { str_to_ident("??") };
1193+
let id = if valid { self.name_from(start) } else { token::intern("??") };
11941194
self.bump();
11951195
return token::LIT_BINARY(id);
11961196
}
@@ -1243,7 +1243,7 @@ impl<'a> StringReader<'a> {
12431243
self.bump();
12441244
}
12451245
self.bump();
1246-
return token::LIT_BINARY_RAW(self.ident_from_to(content_start_bpos, content_end_bpos),
1246+
return token::LIT_BINARY_RAW(self.name_from_to(content_start_bpos, content_end_bpos),
12471247
hash_count);
12481248
}
12491249
}

0 commit comments

Comments
 (0)