Skip to content

Commit 1de15c4

Browse files
committed
codegen eii
1 parent 51a295e commit 1de15c4

File tree

59 files changed

+627
-96
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+627
-96
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,6 +1889,7 @@ pub struct MacroDef {
18891889
pub struct EIIMacroFor {
18901890
pub extern_item_path: Path,
18911891
pub impl_unsafe: bool,
1892+
pub span: Span,
18921893
}
18931894

18941895
#[derive(Clone, Encodable, Decodable, Debug, Copy, Hash, Eq, PartialEq)]

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ fn walk_mac<T: MutVisitor>(vis: &mut T, mac: &mut MacCall) {
747747

748748
fn walk_macro_def<T: MutVisitor>(vis: &mut T, macro_def: &mut MacroDef) {
749749
let MacroDef { body, macro_rules: _, eii_macro_for } = macro_def;
750-
if let Some(EIIMacroFor { extern_item_path, impl_unsafe: _ }) = eii_macro_for {
750+
if let Some(EIIMacroFor { extern_item_path, impl_unsafe: _, span: _ }) = eii_macro_for {
751751
vis.visit_path(extern_item_path);
752752
}
753753
visit_delim_args(vis, body);

compiler/rustc_ast/src/visit.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,9 @@ impl WalkItemKind for ItemKind {
440440
ItemKind::MacCall(mac) => try_visit!(visitor.visit_mac_call(mac)),
441441
ItemKind::MacroDef(ts) => {
442442
try_visit!(visitor.visit_mac_def(ts, id));
443-
if let Some(EIIMacroFor { extern_item_path, impl_unsafe: _ }) = &ts.eii_macro_for {
443+
if let Some(EIIMacroFor { extern_item_path, impl_unsafe: _, span: _ }) =
444+
&ts.eii_macro_for
445+
{
444446
try_visit!(visitor.visit_path(extern_item_path, id));
445447
}
446448
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_abi::ExternAbi;
22
use rustc_ast::ptr::P;
33
use rustc_ast::visit::AssocCtxt;
44
use rustc_ast::*;
5-
use rustc_attr_parsing::AttributeKind;
5+
use rustc_attr_parsing::{AttributeKind, EIIDecl};
66
use rustc_errors::ErrorGuaranteed;
77
use rustc_hir::def::{DefKind, Res};
88
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
@@ -24,7 +24,6 @@ use super::{
2424
AstOwner, FnDeclKind, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
2525
ResolverAstLoweringExt,
2626
};
27-
use crate::GenericArgsMode;
2827

2928
pub(super) struct ItemLowerer<'a, 'hir> {
3029
pub(super) tcx: TyCtxt<'hir>,
@@ -191,13 +190,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
191190
))]
192191
}
193192
ItemKind::MacroDef(MacroDef {
194-
eii_macro_for: Some(EIIMacroFor { extern_item_path, impl_unsafe }),
193+
eii_macro_for: Some(EIIMacroFor { extern_item_path, impl_unsafe, span }),
195194
..
196195
}) => {
197-
vec![hir::Attribute::Parsed(AttributeKind::EiiMacroFor {
196+
vec![hir::Attribute::Parsed(AttributeKind::EiiMacroFor(EIIDecl {
198197
eii_extern_item: self.lower_path_simple_eii(id, extern_item_path),
199198
impl_unsafe: *impl_unsafe,
200-
})]
199+
span: self.lower_span(*span),
200+
}))]
201201
}
202202
ItemKind::ExternCrate(..)
203203
| ItemKind::Use(..)

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl Deprecation {
139139
}
140140
}
141141

142-
#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
142+
#[derive(Copy, Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
143143
pub struct EIIImpl {
144144
pub eii_macro: DefId,
145145
pub impl_marked_unsafe: bool,
@@ -148,6 +148,14 @@ pub struct EIIImpl {
148148
pub is_default: bool,
149149
}
150150

151+
#[derive(Copy, Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
152+
pub struct EIIDecl {
153+
pub eii_extern_item: DefId,
154+
/// whether or not it is unsafe to implement this EII
155+
pub impl_unsafe: bool,
156+
pub span: Span,
157+
}
158+
151159
/// Represent parsed, *built in*, inert attributes.
152160
///
153161
/// That means attributes that are not actually ever expanded.
@@ -200,11 +208,8 @@ pub enum AttributeKind {
200208
comment: Symbol,
201209
},
202210
EiiImpl(ThinVec<EIIImpl>),
203-
EiiMacroFor {
204-
eii_extern_item: DefId,
205-
/// whether or not it is unsafe to implement this EII
206-
impl_unsafe: bool,
207-
},
211+
EiiMacroFor(EIIDecl),
212+
EiiMangleExtern,
208213
MacroTransparency(Transparency),
209214
Repr(ThinVec<(ReprAttr, Span)>),
210215
Stability {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use rustc_attr_data_structures::AttributeKind;
2+
use rustc_span::{Span, Symbol, sym};
3+
4+
use super::{AcceptContext, SingleAttributeParser};
5+
use crate::parser::ArgParser;
6+
7+
pub(crate) struct EiiMangleExternParser;
8+
9+
impl SingleAttributeParser for EiiMangleExternParser {
10+
const PATH: &'static [Symbol] = &[sym::eii_mangle_extern];
11+
12+
fn on_duplicate(_cx: &AcceptContext<'_>, _first_span: Span) {}
13+
fn convert(_cx: &AcceptContext<'_>, args: &ArgParser<'_>) -> Option<AttributeKind> {
14+
assert!(args.no_args());
15+
Some(AttributeKind::EiiMangleExtern)
16+
}
17+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};
1414
use crate::attributes::allow_unstable::{AllowConstFnUnstableParser, AllowInternalUnstableParser};
1515
use crate::attributes::confusables::ConfusablesParser;
1616
use crate::attributes::deprecation::DeprecationParser;
17+
use crate::attributes::eii::EiiMangleExternParser;
1718
use crate::attributes::repr::ReprParser;
1819
use crate::attributes::stability::{
1920
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
@@ -76,6 +77,7 @@ attribute_groups!(
7677
// tidy-alphabetical-start
7778
Single<ConstStabilityIndirectParser>,
7879
Single<DeprecationParser>,
80+
Single<EiiMangleExternParser>,
7981
Single<TransparencyParser>,
8082
// tidy-alphabetical-end
8183
];

compiler/rustc_builtin_macros/src/eii.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ fn eii_(
7777
let item = item.into_inner();
7878

7979
let ast::Item {
80-
attrs,
80+
mut attrs,
8181
id: _,
8282
span: item_span,
8383
vis,
@@ -184,6 +184,21 @@ fn eii_(
184184
}
185185

186186
// extern "…" { safe fn item(); }
187+
// #[eii_mangle_extern]
188+
attrs.push(ast::Attribute {
189+
kind: ast::AttrKind::Normal(P(ast::NormalAttr {
190+
item: ast::AttrItem {
191+
unsafety: ast::Safety::Default,
192+
path: ast::Path::from_ident(Ident::new(sym::eii_mangle_extern, span)),
193+
args: ast::AttrArgs::Empty,
194+
tokens: None,
195+
},
196+
tokens: None,
197+
})),
198+
id: ecx.sess.psess.attr_id_generator.mk_attr_id(),
199+
style: ast::AttrStyle::Outer,
200+
span,
201+
});
187202
let extern_block = Annotatable::Item(P(ast::Item {
188203
attrs: ast::AttrVec::default(),
189204
id: ast::DUMMY_NODE_ID,
@@ -264,6 +279,7 @@ fn eii_(
264279
eii_macro_for: Some(ast::EIIMacroFor {
265280
extern_item_path: ast::Path::from_ident(item_name),
266281
impl_unsafe,
282+
span: decl_span,
267283
}),
268284
}),
269285
tokens: None,
@@ -322,7 +338,7 @@ pub(crate) fn eii_macro_for(
322338
false
323339
};
324340

325-
d.eii_macro_for = Some(EIIMacroFor { extern_item_path, impl_unsafe });
341+
d.eii_macro_for = Some(EIIMacroFor { extern_item_path, impl_unsafe, span });
326342

327343
// Return the original item and the new methods.
328344
vec![item]

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,6 +1979,7 @@ fn add_post_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor
19791979
/// used in any sections, so the linker will therefore pick relevant rlibs for linking, but
19801980
/// unused `#[no_mangle]` or `#[used]` can still be discard by GC sections.
19811981
///
1982+
// TODO: does EII solves this?
19821983
/// There's a few internal crates in the standard library (aka libcore and
19831984
/// libstd) which actually have a circular dependence upon one another. This
19841985
/// currently arises through "weak lang items" where libcore requires things

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> DefIdMap<S
9999
// indicator
100100
if !Instance::mono(tcx, def_id.to_def_id()).def.generates_cgu_internal_copy(tcx)
101101
|| tcx.codegen_fn_attrs(def_id.to_def_id()).contains_extern_indicator()
102+
|| tcx.def_kind(def_id) == DefKind::EiiShim
102103
{
103104
Some(def_id)
104105
} else {
@@ -435,7 +436,8 @@ fn upstream_monomorphizations_provider(
435436
}
436437
ExportedSymbol::NonGeneric(..)
437438
| ExportedSymbol::ThreadLocalShim(..)
438-
| ExportedSymbol::NoDefId(..) => {
439+
| ExportedSymbol::NoDefId(..)
440+
| ExportedSymbol::Alias { .. } => {
439441
// These are no monomorphizations
440442
continue;
441443
}
@@ -581,6 +583,7 @@ pub(crate) fn symbol_name_for_instance_in_crate<'tcx>(
581583
instantiating_crate,
582584
)
583585
}
586+
ExportedSymbol::Alias { original: _, alternative_symbol } => alternative_symbol.to_string(),
584587
ExportedSymbol::NoDefId(symbol_name) => symbol_name.to_string(),
585588
}
586589
}
@@ -607,6 +610,10 @@ fn calling_convention_for_symbol<'tcx>(
607610
ExportedSymbol::NoDefId(..) => None,
608611
// ThreadLocalShim always follow the target's default symbol decoration scheme.
609612
ExportedSymbol::ThreadLocalShim(..) => None,
613+
// Aliases have the same calling convention as the thing they alias.
614+
ExportedSymbol::Alias { original, alternative_symbol: _ } => {
615+
Some(Instance::mono(tcx, original))
616+
}
610617
};
611618

612619
instance

0 commit comments

Comments
 (0)