Skip to content

Commit 0522955

Browse files
committed
auto merge of #8070 : luqmana/rust/nom, r=alexcrichton
Fixes #5972.
2 parents aa8f79d + c32b26b commit 0522955

File tree

2 files changed

+172
-160
lines changed

2 files changed

+172
-160
lines changed

src/librustc/middle/trans/base.rs

+167-154
Original file line numberDiff line numberDiff line change
@@ -2244,47 +2244,38 @@ pub fn trans_mod(ccx: @mut CrateContext, m: &ast::_mod) {
22442244

22452245
pub fn register_fn(ccx: @mut CrateContext,
22462246
sp: span,
2247-
path: path,
2248-
node_id: ast::node_id,
2249-
attrs: &[ast::Attribute])
2247+
sym: ~str,
2248+
node_id: ast::node_id)
22502249
-> ValueRef {
22512250
let t = ty::node_id_to_type(ccx.tcx, node_id);
2252-
register_fn_full(ccx, sp, path, node_id, attrs, t)
2251+
register_fn_full(ccx, sp, sym, node_id, t)
22532252
}
22542253

22552254
pub fn register_fn_full(ccx: @mut CrateContext,
22562255
sp: span,
2257-
path: path,
2256+
sym: ~str,
22582257
node_id: ast::node_id,
2259-
attrs: &[ast::Attribute],
22602258
node_type: ty::t)
22612259
-> ValueRef {
22622260
let llfty = type_of_fn_from_ty(ccx, node_type);
2263-
register_fn_fuller(ccx, sp, path, node_id, attrs, node_type,
2261+
register_fn_fuller(ccx, sp, sym, node_id, node_type,
22642262
lib::llvm::CCallConv, llfty)
22652263
}
22662264

22672265
pub fn register_fn_fuller(ccx: @mut CrateContext,
22682266
sp: span,
2269-
path: path,
2267+
sym: ~str,
22702268
node_id: ast::node_id,
2271-
attrs: &[ast::Attribute],
22722269
node_type: ty::t,
22732270
cc: lib::llvm::CallConv,
22742271
fn_ty: Type)
22752272
-> ValueRef {
22762273
debug!("register_fn_fuller creating fn for item %d with path %s",
22772274
node_id,
2278-
ast_map::path_to_str(path, token::get_ident_interner()));
2279-
2280-
let ps = if attr::contains_name(attrs, "no_mangle") {
2281-
path_elt_to_str(*path.last(), token::get_ident_interner())
2282-
} else {
2283-
mangle_exported_name(ccx, path, node_type)
2284-
};
2275+
ast_map::path_to_str(item_path(ccx, &node_id), token::get_ident_interner()));
22852276

2286-
let llfn = decl_fn(ccx.llmod, ps, cc, fn_ty);
2287-
ccx.item_symbols.insert(node_id, ps);
2277+
let llfn = decl_fn(ccx.llmod, sym, cc, fn_ty);
2278+
ccx.item_symbols.insert(node_id, sym);
22882279

22892280
// FIXME #4404 android JNI hacks
22902281
let is_entry = is_entry_fn(&ccx.sess, node_id) && (!*ccx.sess.building_library ||
@@ -2430,162 +2421,182 @@ pub fn fill_fn_pair(bcx: @mut Block, pair: ValueRef, llfn: ValueRef,
24302421
Store(bcx, llenvblobptr, env_cell);
24312422
}
24322423

2433-
pub fn item_path(ccx: &CrateContext, i: &ast::item) -> path {
2434-
let base = match ccx.tcx.items.get_copy(&i.id) {
2435-
ast_map::node_item(_, p) => p,
2436-
// separate map for paths?
2424+
pub fn item_path(ccx: &CrateContext, id: &ast::node_id) -> path {
2425+
match ccx.tcx.items.get_copy(id) {
2426+
ast_map::node_item(i, p) =>
2427+
vec::append((*p).clone(), [path_name(i.ident)]),
2428+
// separate map for paths?
24372429
_ => fail!("item_path")
2438-
};
2439-
vec::append((*base).clone(), [path_name(i.ident)])
2430+
}
2431+
}
2432+
2433+
fn exported_name(ccx: @mut CrateContext, path: path, ty: ty::t, attrs: &[ast::Attribute]) -> ~str {
2434+
if attr::contains_name(attrs, "no_mangle") {
2435+
path_elt_to_str(*path.last(), token::get_ident_interner())
2436+
} else {
2437+
mangle_exported_name(ccx, path, ty)
2438+
}
24402439
}
24412440

24422441
pub fn get_item_val(ccx: @mut CrateContext, id: ast::node_id) -> ValueRef {
24432442
debug!("get_item_val(id=`%?`)", id);
2443+
24442444
let val = ccx.item_vals.find_copy(&id);
24452445
match val {
2446-
Some(v) => v,
2447-
None => {
2448-
let mut exprt = false;
2449-
let item = ccx.tcx.items.get_copy(&id);
2450-
let val = match item {
2451-
ast_map::node_item(i, pth) => {
2452-
let my_path = vec::append((*pth).clone(), [path_name(i.ident)]);
2453-
let v = match i.node {
2454-
ast::item_static(_, m, expr) => {
2455-
let typ = ty::node_id_to_type(ccx.tcx, i.id);
2456-
let s = mangle_exported_name(ccx, my_path, typ);
2457-
// We need the translated value here, because for enums the
2458-
// LLVM type is not fully determined by the Rust type.
2459-
let v = consts::const_expr(ccx, expr);
2460-
ccx.const_values.insert(id, v);
2461-
exprt = m == ast::m_mutbl;
2462-
unsafe {
2463-
let llty = llvm::LLVMTypeOf(v);
2464-
let g = do s.as_c_str |buf| {
2465-
llvm::LLVMAddGlobal(ccx.llmod, llty, buf)
2446+
Some(v) => v,
2447+
None => {
2448+
let mut exprt = false;
2449+
let item = ccx.tcx.items.get_copy(&id);
2450+
let val = match item {
2451+
ast_map::node_item(i, pth) => {
2452+
2453+
let my_path = vec::append((*pth).clone(), [path_name(i.ident)]);
2454+
let ty = ty::node_id_to_type(ccx.tcx, i.id);
2455+
let sym = exported_name(ccx, my_path, ty, i.attrs);
2456+
2457+
let v = match i.node {
2458+
ast::item_static(_, m, expr) => {
2459+
// We need the translated value here, because for enums the
2460+
// LLVM type is not fully determined by the Rust type.
2461+
let v = consts::const_expr(ccx, expr);
2462+
ccx.const_values.insert(id, v);
2463+
exprt = m == ast::m_mutbl;
2464+
2465+
unsafe {
2466+
let llty = llvm::LLVMTypeOf(v);
2467+
let g = do sym.as_c_str |buf| {
2468+
llvm::LLVMAddGlobal(ccx.llmod, llty, buf)
2469+
};
2470+
2471+
ccx.item_symbols.insert(i.id, sym);
2472+
g
2473+
}
2474+
}
2475+
2476+
ast::item_fn(_, purity, _, _, _) => {
2477+
let llfn = if purity != ast::extern_fn {
2478+
register_fn_full(ccx, i.span, sym, i.id, ty)
2479+
} else {
2480+
foreign::register_foreign_fn(ccx, i.span, sym, i.id)
2481+
};
2482+
set_inline_hint_if_appr(i.attrs, llfn);
2483+
llfn
2484+
}
2485+
2486+
_ => fail!("get_item_val: weird result in table")
24662487
};
2467-
ccx.item_symbols.insert(i.id, s);
2468-
g
2469-
}
2470-
}
2471-
ast::item_fn(_, purity, _, _, _) => {
2472-
let llfn = if purity != ast::extern_fn {
2473-
register_fn(ccx, i.span, my_path, i.id, i.attrs)
2474-
} else {
2475-
foreign::register_foreign_fn(ccx,
2476-
i.span,
2477-
my_path,
2478-
i.id,
2479-
i.attrs)
2480-
};
2481-
set_inline_hint_if_appr(i.attrs, llfn);
2482-
llfn
2483-
}
2484-
_ => fail!("get_item_val: weird result in table")
2485-
};
2486-
match (attr::first_attr_value_str_by_name(i.attrs, "link_section")) {
2487-
Some(sect) => unsafe {
2488-
do sect.as_c_str |buf| {
2489-
llvm::LLVMSetSection(v, buf);
2488+
2489+
match (attr::first_attr_value_str_by_name(i.attrs, "link_section")) {
2490+
Some(sect) => unsafe {
2491+
do sect.as_c_str |buf| {
2492+
llvm::LLVMSetSection(v, buf);
2493+
}
2494+
},
2495+
None => ()
24902496
}
2491-
},
2492-
None => ()
2493-
}
2494-
v
2495-
}
2496-
ast_map::node_trait_method(trait_method, _, pth) => {
2497-
debug!("get_item_val(): processing a node_trait_method");
2498-
match *trait_method {
2499-
ast::required(_) => {
2500-
ccx.sess.bug("unexpected variant: required trait method in \
2501-
get_item_val()");
2502-
}
2503-
ast::provided(m) => {
2504-
exprt = true;
2505-
register_method(ccx, id, pth, m)
2506-
}
2507-
}
2508-
}
2509-
ast_map::node_method(m, _, pth) => {
2510-
register_method(ccx, id, pth, m)
2511-
}
2512-
ast_map::node_foreign_item(ni, _, _, pth) => {
2513-
exprt = true;
2514-
match ni.node {
2515-
ast::foreign_item_fn(*) => {
2516-
register_fn(ccx, ni.span,
2517-
vec::append((*pth).clone(),
2518-
[path_name(ni.ident)]),
2519-
ni.id,
2520-
ni.attrs)
2497+
2498+
v
25212499
}
2522-
ast::foreign_item_static(*) => {
2523-
let typ = ty::node_id_to_type(ccx.tcx, ni.id);
2524-
let ident = token::ident_to_str(&ni.ident);
2525-
let g = do ident.as_c_str |buf| {
2526-
unsafe {
2527-
let ty = type_of(ccx, typ);
2528-
llvm::LLVMAddGlobal(ccx.llmod, ty.to_ref(), buf)
2500+
2501+
ast_map::node_trait_method(trait_method, _, pth) => {
2502+
debug!("get_item_val(): processing a node_trait_method");
2503+
match *trait_method {
2504+
ast::required(_) => {
2505+
ccx.sess.bug("unexpected variant: required trait method in \
2506+
get_item_val()");
25292507
}
2530-
};
2531-
g
2508+
ast::provided(m) => {
2509+
exprt = true;
2510+
register_method(ccx, id, pth, m)
2511+
}
2512+
}
25322513
}
2533-
}
2534-
}
25352514

2536-
ast_map::node_variant(ref v, enm, pth) => {
2537-
let llfn;
2538-
match v.node.kind {
2539-
ast::tuple_variant_kind(ref args) => {
2540-
assert!(args.len() != 0u);
2541-
let pth = vec::append((*pth).clone(),
2542-
[path_name(enm.ident),
2543-
path_name((*v).node.name)]);
2544-
llfn = match enm.node {
2545-
ast::item_enum(_, _) => {
2546-
register_fn(ccx, (*v).span, pth, id, enm.attrs)
2547-
}
2548-
_ => fail!("node_variant, shouldn't happen")
2549-
};
2550-
}
2551-
ast::struct_variant_kind(_) => {
2552-
fail!("struct variant kind unexpected in get_item_val")
2515+
ast_map::node_method(m, _, pth) => {
2516+
register_method(ccx, id, pth, m)
25532517
}
2554-
}
2555-
set_inline_hint(llfn);
2556-
llfn
2557-
}
25582518

2559-
ast_map::node_struct_ctor(struct_def, struct_item, struct_path) => {
2560-
// Only register the constructor if this is a tuple-like struct.
2561-
match struct_def.ctor_id {
2562-
None => {
2563-
ccx.tcx.sess.bug("attempt to register a constructor of \
2564-
a non-tuple-like struct")
2519+
ast_map::node_foreign_item(ni, _, _, pth) => {
2520+
let ty = ty::node_id_to_type(ccx.tcx, ni.id);
2521+
exprt = true;
2522+
2523+
match ni.node {
2524+
ast::foreign_item_fn(*) => {
2525+
let path = vec::append((*pth).clone(), [path_name(ni.ident)]);
2526+
let sym = exported_name(ccx, path, ty, ni.attrs);
2527+
2528+
register_fn_full(ccx, ni.span, sym, ni.id, ty)
2529+
}
2530+
ast::foreign_item_static(*) => {
2531+
let ident = token::ident_to_str(&ni.ident);
2532+
let g = do ident.as_c_str |buf| {
2533+
unsafe {
2534+
let ty = type_of(ccx, ty);
2535+
llvm::LLVMAddGlobal(ccx.llmod, ty.to_ref(), buf)
2536+
}
2537+
};
2538+
g
2539+
}
2540+
}
25652541
}
2566-
Some(ctor_id) => {
2567-
let llfn = register_fn(ccx,
2568-
struct_item.span,
2569-
(*struct_path).clone(),
2570-
ctor_id,
2571-
struct_item.attrs);
2542+
2543+
ast_map::node_variant(ref v, enm, pth) => {
2544+
let llfn;
2545+
match v.node.kind {
2546+
ast::tuple_variant_kind(ref args) => {
2547+
assert!(args.len() != 0u);
2548+
let pth = vec::append((*pth).clone(),
2549+
[path_name(enm.ident),
2550+
path_name((*v).node.name)]);
2551+
let ty = ty::node_id_to_type(ccx.tcx, id);
2552+
let sym = exported_name(ccx, pth, ty, enm.attrs);
2553+
2554+
llfn = match enm.node {
2555+
ast::item_enum(_, _) => {
2556+
register_fn_full(ccx, (*v).span, sym, id, ty)
2557+
}
2558+
_ => fail!("node_variant, shouldn't happen")
2559+
};
2560+
}
2561+
ast::struct_variant_kind(_) => {
2562+
fail!("struct variant kind unexpected in get_item_val")
2563+
}
2564+
}
25722565
set_inline_hint(llfn);
25732566
llfn
25742567
}
2568+
2569+
ast_map::node_struct_ctor(struct_def, struct_item, struct_path) => {
2570+
// Only register the constructor if this is a tuple-like struct.
2571+
match struct_def.ctor_id {
2572+
None => {
2573+
ccx.tcx.sess.bug("attempt to register a constructor of \
2574+
a non-tuple-like struct")
2575+
}
2576+
Some(ctor_id) => {
2577+
let ty = ty::node_id_to_type(ccx.tcx, ctor_id);
2578+
let sym = exported_name(ccx, (*struct_path).clone(), ty,
2579+
struct_item.attrs);
2580+
let llfn = register_fn_full(ccx, struct_item.span, sym, ctor_id, ty);
2581+
set_inline_hint(llfn);
2582+
llfn
2583+
}
2584+
}
2585+
}
2586+
2587+
ref variant => {
2588+
ccx.sess.bug(fmt!("get_item_val(): unexpected variant: %?",
2589+
variant))
2590+
}
2591+
};
2592+
2593+
if !exprt && !ccx.reachable.contains(&id) {
2594+
lib::llvm::SetLinkage(val, lib::llvm::InternalLinkage);
25752595
}
2576-
}
25772596

2578-
ref variant => {
2579-
ccx.sess.bug(fmt!("get_item_val(): unexpected variant: %?",
2580-
variant))
2581-
}
2582-
};
2583-
if !exprt && !ccx.reachable.contains(&id) {
2584-
lib::llvm::SetLinkage(val, lib::llvm::InternalLinkage);
2597+
ccx.item_vals.insert(id, val);
2598+
val
25852599
}
2586-
ccx.item_vals.insert(id, val);
2587-
val
2588-
}
25892600
}
25902601
}
25912602

@@ -2599,7 +2610,9 @@ pub fn register_method(ccx: @mut CrateContext,
25992610
path.push(path_name(gensym_name("meth")));
26002611
path.push(path_name(m.ident));
26012612

2602-
let llfn = register_fn_full(ccx, m.span, path, id, m.attrs, mty);
2613+
let sym = exported_name(ccx, path, mty, m.attrs);
2614+
2615+
let llfn = register_fn_full(ccx, m.span, sym, id, mty);
26032616
set_inline_hint_if_appr(m.attrs, llfn);
26042617
llfn
26052618
}
@@ -2613,7 +2626,7 @@ pub fn trans_constant(ccx: &mut CrateContext, it: @ast::item) {
26132626
ast::def_id { crate: ast::local_crate,
26142627
node: it.id });
26152628
let mut i = 0;
2616-
let path = item_path(ccx, it);
2629+
let path = item_path(ccx, &it.id);
26172630
for (*enum_definition).variants.iter().advance |variant| {
26182631
let p = vec::append(path.clone(), [
26192632
path_name(variant.node.name),

0 commit comments

Comments
 (0)