Skip to content

Commit b598958

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

File tree

3 files changed

+81
-94
lines changed

3 files changed

+81
-94
lines changed

src/librustc/middle/lang_items.rs

+76-89
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,82 +196,81 @@ 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;
276-
277-
40, CovariantTypeItem, "covariant_type", covariant_type;
278-
41, ContravariantTypeItem, "contravariant_type", contravariant_type;
279-
42, InvariantTypeItem, "invariant_type", invariant_type;
280-
281-
43, CovariantLifetimeItem, "covariant_lifetime", covariant_lifetime;
282-
44, ContravariantLifetimeItem, "contravariant_lifetime", contravariant_lifetime;
283-
45, InvariantLifetimeItem, "invariant_lifetime", invariant_lifetime;
284-
285-
46, NoFreezeItem, "no_freeze_bound", no_freeze_bound;
286-
47, NoSendItem, "no_send_bound", no_send_bound;
287-
48, NoPodItem, "no_pod_bound", no_pod_bound;
288-
49, ManagedItem, "managed_bound", managed_bound;
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;
263+
264+
CovariantTypeItem, "covariant_type", covariant_type;
265+
ContravariantTypeItem, "contravariant_type", contravariant_type;
266+
InvariantTypeItem, "invariant_type", invariant_type;
267+
268+
CovariantLifetimeItem, "covariant_lifetime", covariant_lifetime;
269+
ContravariantLifetimeItem, "contravariant_lifetime", contravariant_lifetime;
270+
InvariantLifetimeItem, "invariant_lifetime", invariant_lifetime;
271+
272+
NoFreezeItem, "no_freeze_bound", no_freeze_bound;
273+
NoSendItem, "no_send_bound", no_send_bound;
274+
NoPodItem, "no_pod_bound", no_pod_bound;
275+
ManagedItem, "managed_bound", managed_bound;
289276
}

src/librustc/middle/resolve.rs

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

767767
fn Resolver(session: Session,
768-
lang_items: LanguageItems,
768+
lang_items: @LanguageItems,
769769
crate_span: Span) -> Resolver {
770770
let graph_root = @NameBindings();
771771

@@ -823,7 +823,7 @@ fn Resolver(session: Session,
823823
/// The main resolver class.
824824
struct Resolver {
825825
session: @Session,
826-
lang_items: LanguageItems,
826+
lang_items: @LanguageItems,
827827

828828
intr: @IdentInterner,
829829

@@ -5550,7 +5550,7 @@ pub struct CrateMap {
55505550

55515551
/// Entry point to crate resolution.
55525552
pub fn resolve_crate(session: Session,
5553-
lang_items: LanguageItems,
5553+
lang_items: @LanguageItems,
55545554
crate: &Crate)
55555555
-> CrateMap {
55565556
let mut resolver = Resolver(session, lang_items, crate.span);

src/librustc/middle/ty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ pub struct ctxt_ {
299299
ty_param_defs: RefCell<HashMap<ast::NodeId, TypeParameterDef>>,
300300
adjustments: RefCell<HashMap<ast::NodeId, @AutoAdjustment>>,
301301
normalized_cache: RefCell<HashMap<t, t>>,
302-
lang_items: middle::lang_items::LanguageItems,
302+
lang_items: @middle::lang_items::LanguageItems,
303303
// A mapping of fake provided method def_ids to the default implementation
304304
provided_method_sources: RefCell<HashMap<ast::DefId, ast::DefId>>,
305305
supertraits: RefCell<HashMap<ast::DefId, @~[@TraitRef]>>,
@@ -947,7 +947,7 @@ pub fn mk_ctxt(s: session::Session,
947947
amap: ast_map::Map,
948948
freevars: freevars::freevar_map,
949949
region_maps: middle::region::RegionMaps,
950-
lang_items: middle::lang_items::LanguageItems)
950+
lang_items: @middle::lang_items::LanguageItems)
951951
-> ctxt {
952952
@ctxt_ {
953953
named_region_map: named_region_map,

0 commit comments

Comments
 (0)