Skip to content

Commit 68d1781

Browse files
mrhotaAJ Gardner
authored andcommitted
First attempt at global_asm! macro
1 parent e1cec5d commit 68d1781

File tree

31 files changed

+184
-7
lines changed

31 files changed

+184
-7
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,14 @@ impl<'a> LoweringContext<'a> {
642642
}
643643
}
644644

645+
fn lower_global_asm(&mut self, ga: &GlobalAsm) -> P<hir::GlobalAsm> {
646+
P(hir::GlobalAsm {
647+
asm: ga.asm,
648+
asm_str_style: ga.asm_str_style,
649+
expn_id: ga.expn_id,
650+
})
651+
}
652+
645653
fn lower_variant(&mut self, v: &Variant) -> hir::Variant {
646654
Spanned {
647655
node: hir::Variant_ {
@@ -1284,6 +1292,7 @@ impl<'a> LoweringContext<'a> {
12841292
}
12851293
ItemKind::Mod(ref m) => hir::ItemMod(self.lower_mod(m)),
12861294
ItemKind::ForeignMod(ref nm) => hir::ItemForeignMod(self.lower_foreign_mod(nm)),
1295+
ItemKind::GlobalAsm(ref ga) => hir::ItemGlobalAsm(self.lower_global_asm(ga)),
12871296
ItemKind::Ty(ref t, ref generics) => {
12881297
hir::ItemTy(self.lower_ty(t), self.lower_generics(generics))
12891298
}

src/librustc/hir/map/def_collector.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
120120
DefPathData::ValueNs(i.ident.name.as_str()),
121121
ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.name.as_str()),
122122
ItemKind::Mac(..) => return self.visit_macro_invoc(i.id, false),
123+
ItemKind::GlobalAsm(..) => DefPathData::Misc,
123124
ItemKind::Use(ref view_path) => {
124125
match view_path.node {
125126
ViewPathGlob(..) => {}

src/librustc/hir/map/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,7 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
10561056
ItemFn(..) => "fn",
10571057
ItemMod(..) => "mod",
10581058
ItemForeignMod(..) => "foreign mod",
1059+
ItemGlobalAsm(..) => "global asm",
10591060
ItemTy(..) => "ty",
10601061
ItemEnum(..) => "enum",
10611062
ItemStruct(..) => "struct",

src/librustc/hir/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,13 @@ pub struct ForeignMod {
14901490
pub items: HirVec<ForeignItem>,
14911491
}
14921492

1493+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1494+
pub struct GlobalAsm {
1495+
pub asm: Symbol,
1496+
pub asm_str_style: StrStyle,
1497+
pub expn_id: ExpnId
1498+
}
1499+
14931500
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
14941501
pub struct EnumDef {
14951502
pub variants: HirVec<Variant>,
@@ -1681,6 +1688,8 @@ pub enum Item_ {
16811688
ItemMod(Mod),
16821689
/// An external module
16831690
ItemForeignMod(ForeignMod),
1691+
/// Module-level inline assembly (from global_asm!)
1692+
ItemGlobalAsm(P<GlobalAsm>),
16841693
/// A type alias, e.g. `type Foo = Bar<u8>`
16851694
ItemTy(P<Ty>, Generics),
16861695
/// An enum definition, e.g. `enum Foo<A, B> {C<A>, D<B>}`
@@ -1715,6 +1724,7 @@ impl Item_ {
17151724
ItemFn(..) => "function",
17161725
ItemMod(..) => "module",
17171726
ItemForeignMod(..) => "foreign module",
1727+
ItemGlobalAsm(..) => "global asm",
17181728
ItemTy(..) => "type alias",
17191729
ItemEnum(..) => "enum",
17201730
ItemStruct(..) => "struct",

src/librustc/hir/print.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,11 @@ impl<'a> State<'a> {
630630
self.print_foreign_mod(nmod, &item.attrs)?;
631631
self.bclose(item.span)?;
632632
}
633+
hir::ItemGlobalAsm(ref ga) => {
634+
self.head(&visibility_qualified(&item.vis, "global asm"))?;
635+
word(&mut self.s, &ga.asm.as_str())?;
636+
self.end()?
637+
}
633638
hir::ItemTy(ref ty, ref params) => {
634639
self.ibox(indent_unit)?;
635640
self.ibox(0)?;

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_incremental/calculate_svh/svh_visitor.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ enum SawItemComponent {
354354
SawItemFn(Unsafety, Constness, Abi),
355355
SawItemMod,
356356
SawItemForeignMod(Abi),
357+
SawItemGlobalAsm,
357358
SawItemTy,
358359
SawItemEnum,
359360
SawItemStruct,
@@ -372,6 +373,7 @@ fn saw_item(node: &Item_) -> SawItemComponent {
372373
ItemFn(_, unsafety, constness, abi, _, _) => SawItemFn(unsafety, constness, abi),
373374
ItemMod(..) => SawItemMod,
374375
ItemForeignMod(ref fm) => SawItemForeignMod(fm.abi),
376+
ItemGlobalAsm(..) => SawItemGlobalAsm,
375377
ItemTy(..) => SawItemTy,
376378
ItemEnum(..) => SawItemEnum,
377379
ItemStruct(..) => SawItemStruct,
@@ -921,7 +923,8 @@ impl<'a, 'hash, 'tcx> StrictVersionHashVisitor<'a, 'hash, 'tcx> {
921923
Def::AssociatedConst(..) |
922924
Def::Local(..) |
923925
Def::Upvar(..) |
924-
Def::Macro(..) => {
926+
Def::Macro(..) |
927+
Def::GlobalAsm(..) => {
925928
DefHash::SawDefId.hash(self.st);
926929
self.hash_def_id(def.def_id());
927930
}

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
@@ -655,6 +655,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
655655
return self.encode_info_for_mod(FromId(item.id, (m, &item.attrs, &item.vis)));
656656
}
657657
hir::ItemForeignMod(_) => EntryKind::ForeignMod,
658+
hir::ItemGlobalAsm(..) => EntryKind::GlobalAsm,
658659
hir::ItemTy(..) => EntryKind::Type,
659660
hir::ItemEnum(..) => EntryKind::Enum(get_repr_options(&tcx, def_id)),
660661
hir::ItemStruct(ref struct_def, _) => {
@@ -895,6 +896,7 @@ impl<'a, 'b, 'tcx> IndexBuilder<'a, 'b, 'tcx> {
895896
hir::ItemFn(..) |
896897
hir::ItemMod(..) |
897898
hir::ItemForeignMod(..) |
899+
hir::ItemGlobalAsm(..) |
898900
hir::ItemExternCrate(..) |
899901
hir::ItemUse(..) |
900902
hir::ItemDefaultImpl(..) |

src/librustc_metadata/schema.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ pub enum EntryKind<'tcx> {
227227
ForeignImmStatic,
228228
ForeignMutStatic,
229229
ForeignMod,
230+
GlobalAsm,
230231
Type,
231232
Enum(ReprOptions),
232233
Field,

src/librustc_privacy/lib.rs

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

219224
// Mark all items in interfaces of reachable items as reachable
@@ -226,6 +231,8 @@ impl<'a, 'tcx> Visitor<'tcx> for EmbargoVisitor<'a, 'tcx> {
226231
hir::ItemUse(..) => {}
227232
// The interface is empty
228233
hir::ItemDefaultImpl(..) => {}
234+
// The interface is empty
235+
hir::ItemGlobalAsm(..) => {}
229236
// Visit everything
230237
hir::ItemConst(..) | hir::ItemStatic(..) |
231238
hir::ItemFn(..) | hir::ItemTy(..) => {
@@ -1093,6 +1100,8 @@ impl<'a, 'tcx> Visitor<'tcx> for PrivateItemsInPublicInterfacesVisitor<'a, 'tcx>
10931100
hir::ItemMod(..) => {}
10941101
// Checked in resolve
10951102
hir::ItemUse(..) => {}
1103+
// No subitems
1104+
hir::ItemGlobalAsm(..) => {}
10961105
// Subitems of these items have inherited publicity
10971106
hir::ItemConst(..) | hir::ItemStatic(..) | hir::ItemFn(..) |
10981107
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
@@ -1707,7 +1707,7 @@ impl<'a> Resolver<'a> {
17071707
}
17081708
}
17091709

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

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
@@ -657,6 +657,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
657657
Def::SelfTy(..) |
658658
Def::Label(..) |
659659
Def::Macro(..) |
660+
Def::GlobalAsm(..) |
660661
Def::Err => None,
661662
}
662663
}

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
@@ -355,6 +355,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
355355
}
356356
// If we're inlining, skip private items.
357357
_ if self.inlining && item.vis != hir::Public => {}
358+
hir::ItemGlobalAsm(..) => {}
358359
hir::ItemExternCrate(ref p) => {
359360
let cstore = &self.cx.sess().cstore;
360361
om.extern_crates.push(ExternCrate {

src/libsyntax/ast.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,16 @@ pub struct ForeignMod {
16241624
pub items: Vec<ForeignItem>,
16251625
}
16261626

1627+
/// Global inline assembly
1628+
///
1629+
/// aka module-level assembly or file-scoped assembly
1630+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1631+
pub struct GlobalAsm {
1632+
pub asm: Symbol,
1633+
pub asm_str_style: StrStyle,
1634+
pub expn_id: ExpnId,
1635+
}
1636+
16271637
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
16281638
pub struct EnumDef {
16291639
pub variants: Vec<Variant>,
@@ -1851,6 +1861,8 @@ pub enum ItemKind {
18511861
///
18521862
/// E.g. `extern {}` or `extern "C" {}`
18531863
ForeignMod(ForeignMod),
1864+
/// Module-level inline assembly (from `global_asm!()`)
1865+
GlobalAsm(P<GlobalAsm>),
18541866
/// A type alias (`type` or `pub type`).
18551867
///
18561868
/// E.g. `type Foo = Bar<u8>;`
@@ -1903,6 +1915,7 @@ impl ItemKind {
19031915
ItemKind::Fn(..) => "function",
19041916
ItemKind::Mod(..) => "module",
19051917
ItemKind::ForeignMod(..) => "foreign module",
1918+
ItemKind::GlobalAsm(..) => "global asm",
19061919
ItemKind::Ty(..) => "type alias",
19071920
ItemKind::Enum(..) => "enum",
19081921
ItemKind::Struct(..) => "struct",

src/libsyntax/ext/expand.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,7 @@ impl<'feat> ExpansionConfig<'feat> {
10561056
feature_tests! {
10571057
fn enable_quotes = quote,
10581058
fn enable_asm = asm,
1059+
fn enable_global_asm = global_asm,
10591060
fn enable_log_syntax = log_syntax,
10601061
fn enable_concat_idents = concat_idents,
10611062
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
@@ -342,6 +342,9 @@ declare_features! (
342342

343343
// See rust-lang/rfcs#1414. Allows code like `let x: &'static u32 = &42` to work.
344344
(active, rvalue_static_promotion, "1.15.1", Some(38865)),
345+
346+
// Allows module-level inline assembly by way of global_asm!()
347+
(active, global_asm, "1.17.0", Some(35119)),
345348
);
346349

347350
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

0 commit comments

Comments
 (0)