Skip to content

Commit 768e902

Browse files
committed
First attempt at global_asm! macro
1 parent 14481f7 commit 768e902

File tree

31 files changed

+180
-6
lines changed

31 files changed

+180
-6
lines changed

src/librustc/hir/def.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ pub enum Def {
5757
// Macro namespace
5858
Macro(DefId, MacroKind),
5959

60+
GlobalAsm(DefId),
61+
6062
// Both namespaces
6163
Err,
6264
}
@@ -144,7 +146,8 @@ impl Def {
144146
Def::Variant(id) | Def::VariantCtor(id, ..) | Def::Enum(id) | Def::TyAlias(id) |
145147
Def::AssociatedTy(id) | Def::TyParam(id) | Def::Struct(id) | Def::StructCtor(id, ..) |
146148
Def::Union(id) | Def::Trait(id) | Def::Method(id) | Def::Const(id) |
147-
Def::AssociatedConst(id) | Def::Local(id) | Def::Upvar(id, ..) | Def::Macro(id, ..) => {
149+
Def::AssociatedConst(id) | Def::Local(id) | Def::Upvar(id, ..) | Def::Macro(id, ..) |
150+
Def::GlobalAsm(id) => {
148151
id
149152
}
150153

@@ -185,6 +188,7 @@ impl Def {
185188
Def::Label(..) => "label",
186189
Def::SelfTy(..) => "self type",
187190
Def::Macro(..) => "macro",
191+
Def::GlobalAsm(..) => "global asm",
188192
Def::Err => "unresolved item",
189193
}
190194
}

src/librustc/hir/intravisit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
474474
visitor.visit_id(item.id);
475475
walk_list!(visitor, visit_foreign_item, &foreign_module.items);
476476
}
477+
ItemGlobalAsm(_) => {}
477478
ItemTy(ref typ, ref type_parameters) => {
478479
visitor.visit_id(item.id);
479480
visitor.visit_ty(typ);

src/librustc/hir/lowering.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,13 @@ impl<'a> LoweringContext<'a> {
646646
}
647647
}
648648

649+
fn lower_global_asm(&mut self, ga: &GlobalAsm) -> P<hir::GlobalAsm> {
650+
P(hir::GlobalAsm {
651+
asm: ga.asm,
652+
ctxt: ga.ctxt,
653+
})
654+
}
655+
649656
fn lower_variant(&mut self, v: &Variant) -> hir::Variant {
650657
Spanned {
651658
node: hir::Variant_ {
@@ -1288,6 +1295,7 @@ impl<'a> LoweringContext<'a> {
12881295
}
12891296
ItemKind::Mod(ref m) => hir::ItemMod(self.lower_mod(m)),
12901297
ItemKind::ForeignMod(ref nm) => hir::ItemForeignMod(self.lower_foreign_mod(nm)),
1298+
ItemKind::GlobalAsm(ref ga) => hir::ItemGlobalAsm(self.lower_global_asm(ga)),
12911299
ItemKind::Ty(ref t, ref generics) => {
12921300
hir::ItemTy(self.lower_ty(t), self.lower_generics(generics))
12931301
}

src/librustc/hir/map/def_collector.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
109109
DefPathData::ValueNs(i.ident.name.as_str()),
110110
ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.name.as_str()),
111111
ItemKind::Mac(..) => return self.visit_macro_invoc(i.id, false),
112+
ItemKind::GlobalAsm(..) => DefPathData::Misc,
112113
ItemKind::Use(ref view_path) => {
113114
match view_path.node {
114115
ViewPathGlob(..) => {}

src/librustc/hir/map/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,7 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
10771077
ItemFn(..) => "fn",
10781078
ItemMod(..) => "mod",
10791079
ItemForeignMod(..) => "foreign mod",
1080+
ItemGlobalAsm(..) => "global asm",
10801081
ItemTy(..) => "ty",
10811082
ItemEnum(..) => "enum",
10821083
ItemStruct(..) => "struct",

src/librustc/hir/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,12 @@ pub struct ForeignMod {
14951495
pub items: HirVec<ForeignItem>,
14961496
}
14971497

1498+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1499+
pub struct GlobalAsm {
1500+
pub asm: Symbol,
1501+
pub ctxt: SyntaxContext,
1502+
}
1503+
14981504
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
14991505
pub struct EnumDef {
15001506
pub variants: HirVec<Variant>,
@@ -1686,6 +1692,8 @@ pub enum Item_ {
16861692
ItemMod(Mod),
16871693
/// An external module
16881694
ItemForeignMod(ForeignMod),
1695+
/// Module-level inline assembly (from global_asm!)
1696+
ItemGlobalAsm(P<GlobalAsm>),
16891697
/// A type alias, e.g. `type Foo = Bar<u8>`
16901698
ItemTy(P<Ty>, Generics),
16911699
/// An enum definition, e.g. `enum Foo<A, B> {C<A>, D<B>}`
@@ -1720,6 +1728,7 @@ impl Item_ {
17201728
ItemFn(..) => "function",
17211729
ItemMod(..) => "module",
17221730
ItemForeignMod(..) => "foreign module",
1731+
ItemGlobalAsm(..) => "global asm",
17231732
ItemTy(..) => "type alias",
17241733
ItemEnum(..) => "enum",
17251734
ItemStruct(..) => "struct",

src/librustc/hir/print.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,11 @@ impl<'a> State<'a> {
633633
self.print_foreign_mod(nmod, &item.attrs)?;
634634
self.bclose(item.span)?;
635635
}
636+
hir::ItemGlobalAsm(ref ga) => {
637+
self.head(&visibility_qualified(&item.vis, "global asm"))?;
638+
word(&mut self.s, &ga.asm.as_str())?;
639+
self.end()?
640+
}
636641
hir::ItemTy(ref ty, ref params) => {
637642
self.ibox(indent_unit)?;
638643
self.ibox(0)?;

src/librustc/ich/impls_hir.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,19 @@ impl_stable_hash_for!(struct hir::InlineAsmOutput {
10141014
is_indirect
10151015
});
10161016

1017+
impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for hir::GlobalAsm {
1018+
fn hash_stable<W: StableHasherResult>(&self,
1019+
hcx: &mut StableHashingContext<'a, 'tcx>,
1020+
hasher: &mut StableHasher<W>) {
1021+
let hir::GlobalAsm {
1022+
asm,
1023+
ctxt: _
1024+
} = *self;
1025+
1026+
asm.hash_stable(hcx, hasher);
1027+
}
1028+
}
1029+
10171030
impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for hir::InlineAsm {
10181031
fn hash_stable<W: StableHasherResult>(&self,
10191032
hcx: &mut StableHashingContext<'a, 'tcx>,

src/librustc/middle/reachable.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
267267
hir::ItemMod(..) | hir::ItemForeignMod(..) |
268268
hir::ItemImpl(..) | hir::ItemTrait(..) |
269269
hir::ItemStruct(..) | hir::ItemEnum(..) |
270-
hir::ItemUnion(..) | hir::ItemDefaultImpl(..) => {}
270+
hir::ItemUnion(..) | hir::ItemDefaultImpl(..) |
271+
hir::ItemGlobalAsm(..) => {}
271272
}
272273
}
273274
hir_map::NodeTraitItem(trait_method) => {

src/librustc/middle/resolve_lifetime.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
314314
hir::ItemUse(..) |
315315
hir::ItemMod(..) |
316316
hir::ItemDefaultImpl(..) |
317-
hir::ItemForeignMod(..) => {
317+
hir::ItemForeignMod(..) |
318+
hir::ItemGlobalAsm(..) => {
318319
// These sorts of items have no lifetime parameters at all.
319320
intravisit::walk_item(self, item);
320321
}

src/librustc_metadata/decoder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ impl<'tcx> EntryKind<'tcx> {
429429
EntryKind::Trait(_) => Def::Trait(did),
430430
EntryKind::Enum(..) => Def::Enum(did),
431431
EntryKind::MacroDef(_) => Def::Macro(did, MacroKind::Bang),
432+
EntryKind::GlobalAsm => Def::GlobalAsm(did),
432433

433434
EntryKind::ForeignMod |
434435
EntryKind::Impl(_) |

src/librustc_metadata/encoder.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> EntryBuilder<'a, 'b, 'tcx> {
677677
return self.encode_info_for_mod(FromId(item.id, (m, &item.attrs, &item.vis)));
678678
}
679679
hir::ItemForeignMod(_) => EntryKind::ForeignMod,
680+
hir::ItemGlobalAsm(..) => EntryKind::GlobalAsm,
680681
hir::ItemTy(..) => EntryKind::Type,
681682
hir::ItemEnum(..) => EntryKind::Enum(get_repr_options(&tcx, def_id)),
682683
hir::ItemStruct(ref struct_def, _) => {
@@ -917,6 +918,7 @@ impl<'a, 'b, 'tcx> IndexBuilder<'a, 'b, 'tcx> {
917918
hir::ItemFn(..) |
918919
hir::ItemMod(..) |
919920
hir::ItemForeignMod(..) |
921+
hir::ItemGlobalAsm(..) |
920922
hir::ItemExternCrate(..) |
921923
hir::ItemUse(..) |
922924
hir::ItemDefaultImpl(..) |

src/librustc_metadata/schema.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ pub enum EntryKind<'tcx> {
267267
ForeignImmStatic,
268268
ForeignMutStatic,
269269
ForeignMod,
270+
GlobalAsm,
270271
Type,
271272
Enum(ReprOptions),
272273
Field,

src/librustc_privacy/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,10 @@ impl<'a, 'tcx> Visitor<'tcx> for EmbargoVisitor<'a, 'tcx> {
160160
self.prev_level
161161
}
162162
// Other `pub` items inherit levels from parents
163-
_ => {
163+
hir::ItemConst(..) | hir::ItemEnum(..) | hir::ItemExternCrate(..) |
164+
hir::ItemGlobalAsm(..) | hir::ItemFn(..) | hir::ItemMod(..) |
165+
hir::ItemStatic(..) | hir::ItemStruct(..) | hir::ItemTrait(..) |
166+
hir::ItemTy(..) | hir::ItemUnion(..) | hir::ItemUse(..) => {
164167
if item.vis == hir::Public { self.prev_level } else { None }
165168
}
166169
};
@@ -212,7 +215,9 @@ impl<'a, 'tcx> Visitor<'tcx> for EmbargoVisitor<'a, 'tcx> {
212215
}
213216
}
214217
}
215-
_ => {}
218+
hir::ItemUse(..) | hir::ItemStatic(..) | hir::ItemConst(..) |
219+
hir::ItemGlobalAsm(..) | hir::ItemTy(..) | hir::ItemMod(..) |
220+
hir::ItemFn(..) | hir::ItemExternCrate(..) | hir::ItemDefaultImpl(..) => {}
216221
}
217222

218223
// Mark all items in interfaces of reachable items as reachable
@@ -225,6 +230,8 @@ impl<'a, 'tcx> Visitor<'tcx> for EmbargoVisitor<'a, 'tcx> {
225230
hir::ItemUse(..) => {}
226231
// The interface is empty
227232
hir::ItemDefaultImpl(..) => {}
233+
// The interface is empty
234+
hir::ItemGlobalAsm(..) => {}
228235
// Visit everything
229236
hir::ItemConst(..) | hir::ItemStatic(..) |
230237
hir::ItemFn(..) | hir::ItemTy(..) => {
@@ -1092,6 +1099,8 @@ impl<'a, 'tcx> Visitor<'tcx> for PrivateItemsInPublicInterfacesVisitor<'a, 'tcx>
10921099
hir::ItemMod(..) => {}
10931100
// Checked in resolve
10941101
hir::ItemUse(..) => {}
1102+
// No subitems
1103+
hir::ItemGlobalAsm(..) => {}
10951104
// Subitems of these items have inherited publicity
10961105
hir::ItemConst(..) | hir::ItemStatic(..) | hir::ItemFn(..) |
10971106
hir::ItemTy(..) => {

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ impl<'a> Resolver<'a> {
268268
self.define(parent, ident, TypeNS, imported_binding);
269269
}
270270

271+
ItemKind::GlobalAsm(..) => {}
272+
271273
ItemKind::Mod(..) if item.ident == keywords::Invalid.ident() => {} // Crate root
272274

273275
ItemKind::Mod(..) => {

src/librustc_resolve/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1709,7 +1709,7 @@ impl<'a> Resolver<'a> {
17091709
}
17101710
}
17111711

1712-
ItemKind::ExternCrate(_) | ItemKind::MacroDef(..) => {
1712+
ItemKind::ExternCrate(_) | ItemKind::MacroDef(..) | ItemKind::GlobalAsm(_)=> {
17131713
// do nothing, these are just around to be encoded
17141714
}
17151715

src/librustc_save_analysis/dump_visitor.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
341341
Def::AssociatedTy(..) |
342342
Def::AssociatedConst(..) |
343343
Def::PrimTy(_) |
344+
Def::GlobalAsm(_) |
344345
Def::Err => {
345346
span_bug!(span,
346347
"process_def_kind for unexpected item: {:?}",

src/librustc_save_analysis/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
701701
Def::SelfTy(..) |
702702
Def::Label(..) |
703703
Def::Macro(..) |
704+
Def::GlobalAsm(..) |
704705
Def::Err => None,
705706
}
706707
}

src/librustc_trans/collector.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ impl<'b, 'a, 'v> ItemLikeVisitor<'v> for RootCollector<'b, 'a, 'v> {
811811
hir::ItemExternCrate(..) |
812812
hir::ItemUse(..) |
813813
hir::ItemForeignMod(..) |
814+
hir::ItemGlobalAsm(..) |
814815
hir::ItemTy(..) |
815816
hir::ItemDefaultImpl(..) |
816817
hir::ItemTrait(..) |

src/librustc_typeck/collect.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,7 @@ fn ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
10741074
ItemTrait(..) |
10751075
ItemMod(..) |
10761076
ItemForeignMod(..) |
1077+
ItemGlobalAsm(..) |
10771078
ItemExternCrate(..) |
10781079
ItemUse(..) => {
10791080
span_bug!(

src/librustc_typeck/variance/constraints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for ConstraintContext<'a, 'tcx> {
113113
hir::ItemFn(..) |
114114
hir::ItemMod(..) |
115115
hir::ItemForeignMod(..) |
116+
hir::ItemGlobalAsm(..) |
116117
hir::ItemTy(..) |
117118
hir::ItemImpl(..) |
118119
hir::ItemDefaultImpl(..) => {}

src/librustc_typeck/variance/terms.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for TermsContext<'a, 'tcx> {
251251
hir::ItemFn(..) |
252252
hir::ItemMod(..) |
253253
hir::ItemForeignMod(..) |
254+
hir::ItemGlobalAsm(..) |
254255
hir::ItemTy(..) => {}
255256
}
256257
}

src/librustdoc/visit_ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
373373
}
374374
// If we're inlining, skip private items.
375375
_ if self.inlining && item.vis != hir::Public => {}
376+
hir::ItemGlobalAsm(..) => {}
376377
hir::ItemExternCrate(ref p) => {
377378
let cstore = &self.cx.sess().cstore;
378379
om.extern_crates.push(ExternCrate {

src/libsyntax/ast.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,15 @@ pub struct ForeignMod {
15851585
pub items: Vec<ForeignItem>,
15861586
}
15871587

1588+
/// Global inline assembly
1589+
///
1590+
/// aka module-level assembly or file-scoped assembly
1591+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1592+
pub struct GlobalAsm {
1593+
pub asm: Symbol,
1594+
pub ctxt: SyntaxContext,
1595+
}
1596+
15881597
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
15891598
pub struct EnumDef {
15901599
pub variants: Vec<Variant>,
@@ -1812,6 +1821,8 @@ pub enum ItemKind {
18121821
///
18131822
/// E.g. `extern {}` or `extern "C" {}`
18141823
ForeignMod(ForeignMod),
1824+
/// Module-level inline assembly (from `global_asm!()`)
1825+
GlobalAsm(P<GlobalAsm>),
18151826
/// A type alias (`type` or `pub type`).
18161827
///
18171828
/// E.g. `type Foo = Bar<u8>;`
@@ -1864,6 +1875,7 @@ impl ItemKind {
18641875
ItemKind::Fn(..) => "function",
18651876
ItemKind::Mod(..) => "module",
18661877
ItemKind::ForeignMod(..) => "foreign module",
1878+
ItemKind::GlobalAsm(..) => "global asm",
18671879
ItemKind::Ty(..) => "type alias",
18681880
ItemKind::Enum(..) => "enum",
18691881
ItemKind::Struct(..) => "struct",

src/libsyntax/ext/expand.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,7 @@ impl<'feat> ExpansionConfig<'feat> {
10391039
feature_tests! {
10401040
fn enable_quotes = quote,
10411041
fn enable_asm = asm,
1042+
fn enable_global_asm = global_asm,
10421043
fn enable_log_syntax = log_syntax,
10431044
fn enable_concat_idents = concat_idents,
10441045
fn enable_trace_macros = trace_macros,

src/libsyntax/feature_gate.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@ declare_features! (
346346

347347
// Hack to document `-Z linker-flavor` in The Unstable Book
348348
(active, linker_flavor, "1.18.0", Some(41142)),
349+
350+
// Allows module-level inline assembly by way of global_asm!()
351+
(active, global_asm, "1.18.0", Some(35119)),
349352
);
350353

351354
declare_features! (
@@ -982,6 +985,9 @@ pub const EXPLAIN_STMT_ATTR_SYNTAX: &'static str =
982985
pub const EXPLAIN_ASM: &'static str =
983986
"inline assembly is not stable enough for use and is subject to change";
984987

988+
pub const EXPLAIN_GLOBAL_ASM: &'static str =
989+
"module-level inline assembly is experimental and subject to change";
990+
985991
pub const EXPLAIN_LOG_SYNTAX: &'static str =
986992
"`log_syntax!` is not stable enough for use and is subject to change";
987993

src/libsyntax/fold.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ pub trait Folder : Sized {
140140
noop_fold_foreign_mod(nm, self)
141141
}
142142

143+
fn fold_global_asm(&mut self, ga: P<GlobalAsm>) -> P<GlobalAsm> {
144+
noop_fold_global_asm(ga, self)
145+
}
146+
143147
fn fold_variant(&mut self, v: Variant) -> Variant {
144148
noop_fold_variant(v, self)
145149
}
@@ -412,6 +416,11 @@ pub fn noop_fold_foreign_mod<T: Folder>(ForeignMod {abi, items}: ForeignMod,
412416
}
413417
}
414418

419+
pub fn noop_fold_global_asm<T: Folder>(ga: P<GlobalAsm>,
420+
_: &mut T) -> P<GlobalAsm> {
421+
ga
422+
}
423+
415424
pub fn noop_fold_variant<T: Folder>(v: Variant, fld: &mut T) -> Variant {
416425
Spanned {
417426
node: Variant_ {
@@ -867,6 +876,7 @@ pub fn noop_fold_item_kind<T: Folder>(i: ItemKind, folder: &mut T) -> ItemKind {
867876
}
868877
ItemKind::Mod(m) => ItemKind::Mod(folder.fold_mod(m)),
869878
ItemKind::ForeignMod(nm) => ItemKind::ForeignMod(folder.fold_foreign_mod(nm)),
879+
ItemKind::GlobalAsm(ga) => ItemKind::GlobalAsm(folder.fold_global_asm(ga)),
870880
ItemKind::Ty(t, generics) => {
871881
ItemKind::Ty(folder.fold_ty(t), folder.fold_generics(generics))
872882
}

0 commit comments

Comments
 (0)