Skip to content

Commit 28beb06

Browse files
committed
Remove the need to count lang items
This solves horrible diffs where all you do is renumber literally everything.
1 parent b6f6e49 commit 28beb06

File tree

3 files changed

+68
-81
lines changed

3 files changed

+68
-81
lines changed

src/librustc/middle/lang_items.rs

+63-76
Original file line numberDiff line numberDiff line change
@@ -33,41 +33,28 @@ use std::hashmap::HashMap;
3333
use std::iter::Enumerate;
3434
use std::vec;
3535

36-
37-
// Get the last "argument" (has to be done recursively to avoid phoney local ambiguity error)
38-
macro_rules! last {
39-
( $first:expr, $( $remainder:expr, )+ ) => ( last!( $( $remainder, )+ ) );
40-
( $first:expr, ) => ( $first )
41-
}
42-
4336
// The actual lang items defined come at the end of this file in one handy table.
4437
// So you probably just want to nip down to the end.
4538
macro_rules! lets_do_this {
46-
// secondary rule to allow us to use `$num` as both an expression
47-
// and a pattern.
4839
(
49-
$( $num:tt, $variant:ident, $name:expr, $method:ident; )*
50-
) => {
51-
lets_do_this!(count = 1 + last!($($num,)*),
52-
$($num, $variant, $name, $method; )*)
53-
};
54-
55-
(
56-
count = $num_lang_items:expr, $( $num:pat, $variant:ident, $name:expr, $method:ident; )*
40+
$( $variant:ident, $name:expr, $method:ident; )*
5741
) => {
5842

43+
#[deriving(FromPrimitive)]
5944
pub enum LangItem {
6045
$($variant),*
6146
}
6247

6348
pub struct LanguageItems {
64-
items: [Option<ast::DefId>, ..$num_lang_items]
49+
items: ~[Option<ast::DefId>],
6550
}
6651

6752
impl LanguageItems {
6853
pub fn new() -> LanguageItems {
54+
fn foo(_: LangItem) -> Option<ast::DefId> { None }
55+
6956
LanguageItems {
70-
items: [ None, ..$num_lang_items ]
57+
items: ~[$(foo($variant)),*]
7158
}
7259
}
7360

@@ -76,9 +63,10 @@ impl LanguageItems {
7663
}
7764

7865
pub fn item_name(index: uint) -> &'static str {
79-
match index {
80-
$( $num => $name, )*
81-
_ => "???"
66+
let item: Option<LangItem> = FromPrimitive::from_uint(index);
67+
match item {
68+
$( Some($variant) => $name, )*
69+
None => "???"
8270
}
8371
}
8472

@@ -208,69 +196,68 @@ pub fn extract(attrs: &[ast::Attribute]) -> Option<@str> {
208196
}
209197

210198
pub fn collect_language_items(crate: &ast::Crate,
211-
session: Session)
212-
-> LanguageItems {
199+
session: Session) -> @LanguageItems {
213200
let mut collector = LanguageItemCollector::new(session);
214201
collector.collect(crate);
215202
let LanguageItemCollector { items, .. } = collector;
216203
session.abort_if_errors();
217-
items
204+
@items
218205
}
219206

220207
// End of the macro
221208
}
222209
}
223210

224211
lets_do_this! {
225-
// ID, Variant name, Name, Method name;
226-
0, FreezeTraitLangItem, "freeze", freeze_trait;
227-
1, SendTraitLangItem, "send", send_trait;
228-
2, SizedTraitLangItem, "sized", sized_trait;
229-
3, PodTraitLangItem, "pod", pod_trait;
230-
231-
4, DropTraitLangItem, "drop", drop_trait;
232-
233-
5, AddTraitLangItem, "add", add_trait;
234-
6, SubTraitLangItem, "sub", sub_trait;
235-
7, MulTraitLangItem, "mul", mul_trait;
236-
8, DivTraitLangItem, "div", div_trait;
237-
9, RemTraitLangItem, "rem", rem_trait;
238-
10, NegTraitLangItem, "neg", neg_trait;
239-
11, NotTraitLangItem, "not", not_trait;
240-
12, BitXorTraitLangItem, "bitxor", bitxor_trait;
241-
13, BitAndTraitLangItem, "bitand", bitand_trait;
242-
14, BitOrTraitLangItem, "bitor", bitor_trait;
243-
15, ShlTraitLangItem, "shl", shl_trait;
244-
16, ShrTraitLangItem, "shr", shr_trait;
245-
17, IndexTraitLangItem, "index", index_trait;
246-
247-
18, EqTraitLangItem, "eq", eq_trait;
248-
19, OrdTraitLangItem, "ord", ord_trait;
249-
250-
20, StrEqFnLangItem, "str_eq", str_eq_fn;
251-
21, UniqStrEqFnLangItem, "uniq_str_eq", uniq_str_eq_fn;
252-
22, FailFnLangItem, "fail_", fail_fn;
253-
23, FailBoundsCheckFnLangItem, "fail_bounds_check", fail_bounds_check_fn;
254-
24, ExchangeMallocFnLangItem, "exchange_malloc", exchange_malloc_fn;
255-
25, ClosureExchangeMallocFnLangItem, "closure_exchange_malloc", closure_exchange_malloc_fn;
256-
26, ExchangeFreeFnLangItem, "exchange_free", exchange_free_fn;
257-
27, MallocFnLangItem, "malloc", malloc_fn;
258-
28, FreeFnLangItem, "free", free_fn;
259-
29, StrDupUniqFnLangItem, "strdup_uniq", strdup_uniq_fn;
260-
261-
30, StartFnLangItem, "start", start_fn;
262-
263-
31, TyDescStructLangItem, "ty_desc", ty_desc;
264-
32, TyVisitorTraitLangItem, "ty_visitor", ty_visitor;
265-
33, OpaqueStructLangItem, "opaque", opaque;
266-
267-
34, EventLoopFactoryLangItem, "event_loop_factory", event_loop_factory;
268-
269-
35, TypeIdLangItem, "type_id", type_id;
270-
271-
36, EhPersonalityLangItem, "eh_personality", eh_personality_fn;
272-
273-
37, ManagedHeapLangItem, "managed_heap", managed_heap;
274-
38, ExchangeHeapLangItem, "exchange_heap", exchange_heap;
275-
39, GcLangItem, "gc", gc;
212+
// Variant name, Name, Method name;
213+
FreezeTraitLangItem, "freeze", freeze_trait;
214+
SendTraitLangItem, "send", send_trait;
215+
SizedTraitLangItem, "sized", sized_trait;
216+
PodTraitLangItem, "pod", pod_trait;
217+
218+
DropTraitLangItem, "drop", drop_trait;
219+
220+
AddTraitLangItem, "add", add_trait;
221+
SubTraitLangItem, "sub", sub_trait;
222+
MulTraitLangItem, "mul", mul_trait;
223+
DivTraitLangItem, "div", div_trait;
224+
RemTraitLangItem, "rem", rem_trait;
225+
NegTraitLangItem, "neg", neg_trait;
226+
NotTraitLangItem, "not", not_trait;
227+
BitXorTraitLangItem, "bitxor", bitxor_trait;
228+
BitAndTraitLangItem, "bitand", bitand_trait;
229+
BitOrTraitLangItem, "bitor", bitor_trait;
230+
ShlTraitLangItem, "shl", shl_trait;
231+
ShrTraitLangItem, "shr", shr_trait;
232+
IndexTraitLangItem, "index", index_trait;
233+
234+
EqTraitLangItem, "eq", eq_trait;
235+
OrdTraitLangItem, "ord", ord_trait;
236+
237+
StrEqFnLangItem, "str_eq", str_eq_fn;
238+
UniqStrEqFnLangItem, "uniq_str_eq", uniq_str_eq_fn;
239+
FailFnLangItem, "fail_", fail_fn;
240+
FailBoundsCheckFnLangItem, "fail_bounds_check", fail_bounds_check_fn;
241+
ExchangeMallocFnLangItem, "exchange_malloc", exchange_malloc_fn;
242+
ClosureExchangeMallocFnLangItem, "closure_exchange_malloc", closure_exchange_malloc_fn;
243+
ExchangeFreeFnLangItem, "exchange_free", exchange_free_fn;
244+
MallocFnLangItem, "malloc", malloc_fn;
245+
FreeFnLangItem, "free", free_fn;
246+
StrDupUniqFnLangItem, "strdup_uniq", strdup_uniq_fn;
247+
248+
StartFnLangItem, "start", start_fn;
249+
250+
TyDescStructLangItem, "ty_desc", ty_desc;
251+
TyVisitorTraitLangItem, "ty_visitor", ty_visitor;
252+
OpaqueStructLangItem, "opaque", opaque;
253+
254+
EventLoopFactoryLangItem, "event_loop_factory", event_loop_factory;
255+
256+
TypeIdLangItem, "type_id", type_id;
257+
258+
EhPersonalityLangItem, "eh_personality", eh_personality_fn;
259+
260+
ManagedHeapLangItem, "managed_heap", managed_heap;
261+
ExchangeHeapLangItem, "exchange_heap", exchange_heap;
262+
GcLangItem, "gc", gc;
276263
}

src/librustc/middle/resolve.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ fn namespace_error_to_str(ns: NamespaceError) -> &'static str {
779779
}
780780

781781
fn Resolver(session: Session,
782-
lang_items: LanguageItems,
782+
lang_items: @LanguageItems,
783783
crate_span: Span) -> Resolver {
784784
let graph_root = @NameBindings();
785785

@@ -837,7 +837,7 @@ fn Resolver(session: Session,
837837
/// The main resolver class.
838838
struct Resolver {
839839
session: @Session,
840-
lang_items: LanguageItems,
840+
lang_items: @LanguageItems,
841841

842842
intr: @IdentInterner,
843843

@@ -5656,7 +5656,7 @@ pub struct CrateMap {
56565656

56575657
/// Entry point to crate resolution.
56585658
pub fn resolve_crate(session: Session,
5659-
lang_items: LanguageItems,
5659+
lang_items: @LanguageItems,
56605660
crate: &Crate)
56615661
-> CrateMap {
56625662
let mut resolver = Resolver(session, lang_items, crate.span);

src/librustc/middle/ty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ struct ctxt_ {
309309
ty_param_defs: RefCell<HashMap<ast::NodeId, TypeParameterDef>>,
310310
adjustments: RefCell<HashMap<ast::NodeId, @AutoAdjustment>>,
311311
normalized_cache: RefCell<HashMap<t, t>>,
312-
lang_items: middle::lang_items::LanguageItems,
312+
lang_items: @middle::lang_items::LanguageItems,
313313
// A mapping of fake provided method def_ids to the default implementation
314314
provided_method_sources: RefCell<HashMap<ast::DefId, ast::DefId>>,
315315
supertraits: RefCell<HashMap<ast::DefId, @~[@TraitRef]>>,
@@ -957,7 +957,7 @@ pub fn mk_ctxt(s: session::Session,
957957
amap: ast_map::Map,
958958
freevars: freevars::freevar_map,
959959
region_maps: middle::region::RegionMaps,
960-
lang_items: middle::lang_items::LanguageItems)
960+
lang_items: @middle::lang_items::LanguageItems)
961961
-> ctxt {
962962
@ctxt_ {
963963
named_region_map: named_region_map,

0 commit comments

Comments
 (0)