Skip to content

Commit 27ea11e

Browse files
committed
librustc: Allow linkage attribute on any statics, not just foreign statics.
1 parent e82f60e commit 27ea11e

File tree

3 files changed

+50
-28
lines changed

3 files changed

+50
-28
lines changed

src/librustc/middle/trans/base.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use driver::config::{NoDebugInfo, FullDebugInfo};
3232
use driver::driver::{CrateAnalysis, CrateTranslation, ModuleTranslation};
3333
use driver::session::Session;
3434
use lint;
35-
use llvm::{BasicBlockRef, ValueRef, Vector, get_param};
35+
use llvm::{BasicBlockRef, Linkage, ValueRef, Vector, get_param};
3636
use llvm;
3737
use metadata::{csearch, encoder, loader};
3838
use middle::astencode;
@@ -2153,6 +2153,32 @@ impl<'a, 'tcx, 'v> Visitor<'v> for TransItemVisitor<'a, 'tcx> {
21532153
}
21542154
}
21552155

2156+
pub fn llvm_linkage_by_name(name: &str) -> Option<Linkage> {
2157+
// Use the names from src/llvm/docs/LangRef.rst here. Most types are only
2158+
// applicable to variable declarations and may not really make sense for
2159+
// Rust code in the first place but whitelist them anyway and trust that
2160+
// the user knows what s/he's doing. Who knows, unanticipated use cases
2161+
// may pop up in the future.
2162+
//
2163+
// ghost, dllimport, dllexport and linkonce_odr_autohide are not supported
2164+
// and don't have to be, LLVM treats them as no-ops.
2165+
match name {
2166+
"appending" => Some(llvm::AppendingLinkage),
2167+
"available_externally" => Some(llvm::AvailableExternallyLinkage),
2168+
"common" => Some(llvm::CommonLinkage),
2169+
"extern_weak" => Some(llvm::ExternalWeakLinkage),
2170+
"external" => Some(llvm::ExternalLinkage),
2171+
"internal" => Some(llvm::InternalLinkage),
2172+
"linkonce" => Some(llvm::LinkOnceAnyLinkage),
2173+
"linkonce_odr" => Some(llvm::LinkOnceODRLinkage),
2174+
"private" => Some(llvm::PrivateLinkage),
2175+
"weak" => Some(llvm::WeakAnyLinkage),
2176+
"weak_odr" => Some(llvm::WeakODRLinkage),
2177+
_ => None,
2178+
}
2179+
}
2180+
2181+
21562182
/// Enum describing the origin of an LLVM `Value`, for linkage purposes.
21572183
pub enum ValueOrigin {
21582184
/// The LLVM `Value` is in this context because the corresponding item was
@@ -2190,6 +2216,23 @@ pub fn update_linkage(ccx: &CrateContext,
21902216
OriginalTranslation => {},
21912217
}
21922218

2219+
match id {
2220+
Some(id) => {
2221+
let item = ccx.tcx().map.get(id);
2222+
if let ast_map::NodeItem(i) = item {
2223+
if let Some(name) = attr::first_attr_value_str_by_name(i.attrs[], "linkage") {
2224+
if let Some(linkage) = llvm_linkage_by_name(name.get()) {
2225+
llvm::SetLinkage(llval, linkage);
2226+
} else {
2227+
ccx.sess().span_fatal(i.span, "invalid linkage specified");
2228+
}
2229+
return;
2230+
}
2231+
}
2232+
}
2233+
_ => {}
2234+
}
2235+
21932236
match id {
21942237
Some(id) if ccx.reachable().contains(&id) => {
21952238
llvm::SetLinkage(llval, llvm::ExternalLinkage);

src/librustc/middle/trans/foreign.rs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111

1212
use back::{link};
13-
use llvm::{ValueRef, CallConv, Linkage, get_param};
13+
use llvm::{ValueRef, CallConv, get_param};
1414
use llvm;
1515
use middle::weak_lang_items;
16-
use middle::trans::base::push_ctxt;
16+
use middle::trans::base::{llvm_linkage_by_name, push_ctxt};
1717
use middle::trans::base;
1818
use middle::trans::build::*;
1919
use middle::trans::cabi;
@@ -101,31 +101,6 @@ pub fn llvm_calling_convention(ccx: &CrateContext,
101101
}
102102
}
103103

104-
pub fn llvm_linkage_by_name(name: &str) -> Option<Linkage> {
105-
// Use the names from src/llvm/docs/LangRef.rst here. Most types are only
106-
// applicable to variable declarations and may not really make sense for
107-
// Rust code in the first place but whitelist them anyway and trust that
108-
// the user knows what s/he's doing. Who knows, unanticipated use cases
109-
// may pop up in the future.
110-
//
111-
// ghost, dllimport, dllexport and linkonce_odr_autohide are not supported
112-
// and don't have to be, LLVM treats them as no-ops.
113-
match name {
114-
"appending" => Some(llvm::AppendingLinkage),
115-
"available_externally" => Some(llvm::AvailableExternallyLinkage),
116-
"common" => Some(llvm::CommonLinkage),
117-
"extern_weak" => Some(llvm::ExternalWeakLinkage),
118-
"external" => Some(llvm::ExternalLinkage),
119-
"internal" => Some(llvm::InternalLinkage),
120-
"linkonce" => Some(llvm::LinkOnceAnyLinkage),
121-
"linkonce_odr" => Some(llvm::LinkOnceODRLinkage),
122-
"private" => Some(llvm::PrivateLinkage),
123-
"weak" => Some(llvm::WeakAnyLinkage),
124-
"weak_odr" => Some(llvm::WeakODRLinkage),
125-
_ => None,
126-
}
127-
}
128-
129104
pub fn register_static(ccx: &CrateContext,
130105
foreign_item: &ast::ForeignItem) -> ValueRef {
131106
let ty = ty::node_id_to_type(ccx.tcx(), foreign_item.id);

src/libsyntax/feature_gate.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ impl<'a, 'v> Visitor<'v> for Context<'a> {
181181
"`#[thread_local]` is an experimental feature, and does not \
182182
currently handle destructors. There is no corresponding \
183183
`#[task_local]` mapping to the task model");
184+
} else if attr.name().equiv(&("linkage")) {
185+
self.gate_feature("linkage", i.span,
186+
"the `linkage` attribute is experimental \
187+
and not portable across platforms")
184188
}
185189
}
186190
match i.node {

0 commit comments

Comments
 (0)