Skip to content

Commit 9e61d5c

Browse files
committed
First attempt at global_asm! macro
1 parent 6f10e2f commit 9e61d5c

File tree

31 files changed

+184
-7
lines changed

31 files changed

+184
-7
lines changed

src/librustc/hir/def.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ pub enum Def {
5656
// Macro namespace
5757
Macro(DefId, MacroKind),
5858

59+
GlobalAsm(DefId),
60+
5961
// Both namespaces
6062
Err,
6163
}
@@ -142,7 +144,8 @@ impl Def {
142144
Def::Variant(id) | Def::VariantCtor(id, ..) | Def::Enum(id) | Def::TyAlias(id) |
143145
Def::AssociatedTy(id) | Def::TyParam(id) | Def::Struct(id) | Def::StructCtor(id, ..) |
144146
Def::Union(id) | Def::Trait(id) | Def::Method(id) | Def::Const(id) |
145-
Def::AssociatedConst(id) | Def::Local(id) | Def::Upvar(id, ..) | Def::Macro(id, ..) => {
147+
Def::AssociatedConst(id) | Def::Local(id) | Def::Upvar(id, ..) | Def::Macro(id, ..) |
148+
Def::GlobalAsm(id) => {
146149
id
147150
}
148151

@@ -183,6 +186,7 @@ impl Def {
183186
Def::Label(..) => "label",
184187
Def::SelfTy(..) => "self type",
185188
Def::Macro(..) => "macro",
189+
Def::GlobalAsm(..) => "global asm",
186190
Def::Err => "unresolved item",
187191
}
188192
}

src/librustc/hir/intravisit.rs

+1
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

+9
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,14 @@ impl<'a> LoweringContext<'a> {
486486
}
487487
}
488488

489+
fn lower_global_asm(&mut self, ga: &GlobalAsm) -> P<hir::GlobalAsm> {
490+
P(hir::GlobalAsm {
491+
asm: ga.asm,
492+
asm_str_style: ga.asm_str_style,
493+
expn_id: ga.expn_id,
494+
})
495+
}
496+
489497
fn lower_variant(&mut self, v: &Variant) -> hir::Variant {
490498
Spanned {
491499
node: hir::Variant_ {
@@ -1098,6 +1106,7 @@ impl<'a> LoweringContext<'a> {
10981106
}
10991107
ItemKind::Mod(ref m) => hir::ItemMod(self.lower_mod(m)),
11001108
ItemKind::ForeignMod(ref nm) => hir::ItemForeignMod(self.lower_foreign_mod(nm)),
1109+
ItemKind::GlobalAsm(ref ga) => hir::ItemGlobalAsm(self.lower_global_asm(ga)),
11011110
ItemKind::Ty(ref t, ref generics) => {
11021111
hir::ItemTy(self.lower_ty(t), self.lower_generics(generics))
11031112
}

src/librustc/hir/map/def_collector.rs

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
110110
DefPathData::ValueNs(i.ident.name.as_str()),
111111
ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.name.as_str()),
112112
ItemKind::Mac(..) => return self.visit_macro_invoc(i.id, false),
113+
ItemKind::GlobalAsm(..) => DefPathData::Misc,
113114
ItemKind::Use(ref view_path) => {
114115
match view_path.node {
115116
ViewPathGlob(..) => {}

src/librustc/hir/map/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,7 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
10521052
ItemFn(..) => "fn",
10531053
ItemMod(..) => "mod",
10541054
ItemForeignMod(..) => "foreign mod",
1055+
ItemGlobalAsm(..) => "global asm",
10551056
ItemTy(..) => "ty",
10561057
ItemEnum(..) => "enum",
10571058
ItemStruct(..) => "struct",

src/librustc/hir/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,13 @@ pub struct ForeignMod {
14051405
pub items: HirVec<ForeignItem>,
14061406
}
14071407

1408+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1409+
pub struct GlobalAsm {
1410+
pub asm: Symbol,
1411+
pub asm_str_style: StrStyle,
1412+
pub expn_id: ExpnId
1413+
}
1414+
14081415
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
14091416
pub struct EnumDef {
14101417
pub variants: HirVec<Variant>,
@@ -1584,6 +1591,8 @@ pub enum Item_ {
15841591
ItemMod(Mod),
15851592
/// An external module
15861593
ItemForeignMod(ForeignMod),
1594+
/// Module-level inline assembly (from global_asm!)
1595+
ItemGlobalAsm(P<GlobalAsm>),
15871596
/// A type alias, e.g. `type Foo = Bar<u8>`
15881597
ItemTy(P<Ty>, Generics),
15891598
/// An enum definition, e.g. `enum Foo<A, B> {C<A>, D<B>}`
@@ -1618,6 +1627,7 @@ impl Item_ {
16181627
ItemFn(..) => "function",
16191628
ItemMod(..) => "module",
16201629
ItemForeignMod(..) => "foreign module",
1630+
ItemGlobalAsm(..) => "global asm",
16211631
ItemTy(..) => "type alias",
16221632
ItemEnum(..) => "enum",
16231633
ItemStruct(..) => "struct",

src/librustc/hir/print.rs

+5
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
265265
hir::ItemMod(..) | hir::ItemForeignMod(..) |
266266
hir::ItemImpl(..) | hir::ItemTrait(..) |
267267
hir::ItemStruct(..) | hir::ItemEnum(..) |
268-
hir::ItemUnion(..) | hir::ItemDefaultImpl(..) => {}
268+
hir::ItemUnion(..) | hir::ItemDefaultImpl(..) |
269+
hir::ItemGlobalAsm(..) => {}
269270
}
270271
}
271272
hir_map::NodeTraitItem(trait_method) => {

src/librustc/middle/resolve_lifetime.rs

+2-1
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

+4-1
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ enum SawItemComponent {
366366
SawItemFn(Unsafety, Constness, Abi),
367367
SawItemMod,
368368
SawItemForeignMod(Abi),
369+
SawItemGlobalAsm,
369370
SawItemTy,
370371
SawItemEnum,
371372
SawItemStruct,
@@ -384,6 +385,7 @@ fn saw_item(node: &Item_) -> SawItemComponent {
384385
ItemFn(_, unsafety, constness, abi, _, _) => SawItemFn(unsafety, constness, abi),
385386
ItemMod(..) => SawItemMod,
386387
ItemForeignMod(ref fm) => SawItemForeignMod(fm.abi),
388+
ItemGlobalAsm(..) => SawItemGlobalAsm,
387389
ItemTy(..) => SawItemTy,
388390
ItemEnum(..) => SawItemEnum,
389391
ItemStruct(..) => SawItemStruct,
@@ -933,7 +935,8 @@ impl<'a, 'hash, 'tcx> StrictVersionHashVisitor<'a, 'hash, 'tcx> {
933935
Def::AssociatedConst(..) |
934936
Def::Local(..) |
935937
Def::Upvar(..) |
936-
Def::Macro(..) => {
938+
Def::Macro(..) |
939+
Def::GlobalAsm(..) => {
937940
DefHash::SawDefId.hash(self.st);
938941
self.hash_def_id(def.def_id());
939942
}

src/librustc_metadata/decoder.rs

+1
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

+2
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
657657
return self.encode_info_for_mod(FromId(item.id, (m, &item.attrs, &item.vis)));
658658
}
659659
hir::ItemForeignMod(_) => EntryKind::ForeignMod,
660+
hir::ItemGlobalAsm(..) => EntryKind::GlobalAsm,
660661
hir::ItemTy(..) => EntryKind::Type,
661662
hir::ItemEnum(..) => EntryKind::Enum(get_repr_options(&tcx, def_id)),
662663
hir::ItemStruct(ref struct_def, _) => {
@@ -889,6 +890,7 @@ impl<'a, 'b, 'tcx> IndexBuilder<'a, 'b, 'tcx> {
889890
hir::ItemFn(..) |
890891
hir::ItemMod(..) |
891892
hir::ItemForeignMod(..) |
893+
hir::ItemGlobalAsm(..) |
892894
hir::ItemExternCrate(..) |
893895
hir::ItemUse(..) |
894896
hir::ItemDefaultImpl(..) |

src/librustc_metadata/schema.rs

+1
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

+11-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ impl<'a, 'tcx> Visitor<'tcx> for EmbargoVisitor<'a, 'tcx> {
140140
self.prev_level
141141
}
142142
// Other `pub` items inherit levels from parents
143-
_ => {
143+
hir::ItemConst(..) | hir::ItemEnum(..) | hir::ItemExternCrate(..) |
144+
hir::ItemGlobalAsm(..) | hir::ItemFn(..) | hir::ItemMod(..) |
145+
hir::ItemStatic(..) | hir::ItemStruct(..) | hir::ItemTrait(..) |
146+
hir::ItemTy(..) | hir::ItemUnion(..) | hir::ItemUse(..) => {
144147
if item.vis == hir::Public { self.prev_level } else { None }
145148
}
146149
};
@@ -192,7 +195,9 @@ impl<'a, 'tcx> Visitor<'tcx> for EmbargoVisitor<'a, 'tcx> {
192195
}
193196
}
194197
}
195-
_ => {}
198+
hir::ItemUse(..) | hir::ItemStatic(..) | hir::ItemConst(..) |
199+
hir::ItemGlobalAsm(..) | hir::ItemTy(..) | hir::ItemMod(..) |
200+
hir::ItemFn(..) | hir::ItemExternCrate(..) | hir::ItemDefaultImpl(..) => {}
196201
}
197202

198203
// Mark all items in interfaces of reachable items as reachable
@@ -205,6 +210,8 @@ impl<'a, 'tcx> Visitor<'tcx> for EmbargoVisitor<'a, 'tcx> {
205210
hir::ItemUse(..) => {}
206211
// The interface is empty
207212
hir::ItemDefaultImpl(..) => {}
213+
// The interface is empty
214+
hir::ItemGlobalAsm(..) => {}
208215
// Visit everything
209216
hir::ItemConst(..) | hir::ItemStatic(..) |
210217
hir::ItemFn(..) | hir::ItemTy(..) => {
@@ -1069,6 +1076,8 @@ impl<'a, 'tcx> Visitor<'tcx> for PrivateItemsInPublicInterfacesVisitor<'a, 'tcx>
10691076
hir::ItemMod(..) => {}
10701077
// Checked in resolve
10711078
hir::ItemUse(..) => {}
1079+
// No subitems
1080+
hir::ItemGlobalAsm(..) => {}
10721081
// Subitems of these items have inherited publicity
10731082
hir::ItemConst(..) | hir::ItemStatic(..) | hir::ItemFn(..) |
10741083
hir::ItemTy(..) => {

src/librustc_resolve/build_reduced_graph.rs

+2
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1701,7 +1701,7 @@ impl<'a> Resolver<'a> {
17011701
}
17021702
}
17031703

1704-
ItemKind::ExternCrate(_) | ItemKind::MacroDef(..) => {
1704+
ItemKind::ExternCrate(_) | ItemKind::MacroDef(..) | ItemKind::GlobalAsm(_)=> {
17051705
// do nothing, these are just around to be encoded
17061706
}
17071707

src/librustc_save_analysis/dump_visitor.rs

+1
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
339339
Def::AssociatedTy(..) |
340340
Def::AssociatedConst(..) |
341341
Def::PrimTy(_) |
342+
Def::GlobalAsm(_) |
342343
Def::Err => {
343344
span_bug!(span,
344345
"process_def_kind for unexpected item: {:?}",

src/librustc_save_analysis/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
653653
Def::SelfTy(..) |
654654
Def::Label(..) |
655655
Def::Macro(..) |
656+
Def::GlobalAsm(..) |
656657
Def::Err => None,
657658
}
658659
}

src/librustc_trans/collector.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,7 @@ impl<'b, 'a, 'v> ItemLikeVisitor<'v> for RootCollector<'b, 'a, 'v> {
11571157
hir::ItemExternCrate(..) |
11581158
hir::ItemUse(..) |
11591159
hir::ItemForeignMod(..) |
1160+
hir::ItemGlobalAsm(..) |
11601161
hir::ItemTy(..) |
11611162
hir::ItemDefaultImpl(..) |
11621163
hir::ItemTrait(..) |

src/librustc_typeck/collect.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,7 @@ fn ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
10801080
ItemTrait(..) |
10811081
ItemMod(..) |
10821082
ItemForeignMod(..) |
1083+
ItemGlobalAsm(..) |
10831084
ItemExternCrate(..) |
10841085
ItemUse(..) => {
10851086
span_bug!(

src/librustc_typeck/variance/constraints.rs

+1
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

+1
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

+1
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

+13
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,16 @@ pub struct ForeignMod {
16071607
pub items: Vec<ForeignItem>,
16081608
}
16091609

1610+
/// Global inline assembly
1611+
///
1612+
/// aka module-level assembly or file-scoped assembly
1613+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1614+
pub struct GlobalAsm {
1615+
pub asm: Symbol,
1616+
pub asm_str_style: StrStyle,
1617+
pub expn_id: ExpnId,
1618+
}
1619+
16101620
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
16111621
pub struct EnumDef {
16121622
pub variants: Vec<Variant>,
@@ -1823,6 +1833,8 @@ pub enum ItemKind {
18231833
///
18241834
/// E.g. `extern {}` or `extern "C" {}`
18251835
ForeignMod(ForeignMod),
1836+
/// Module-level inline assembly (from `global_asm!()`)
1837+
GlobalAsm(P<GlobalAsm>),
18261838
/// A type alias (`type` or `pub type`).
18271839
///
18281840
/// E.g. `type Foo = Bar<u8>;`
@@ -1875,6 +1887,7 @@ impl ItemKind {
18751887
ItemKind::Fn(..) => "function",
18761888
ItemKind::Mod(..) => "module",
18771889
ItemKind::ForeignMod(..) => "foreign module",
1890+
ItemKind::GlobalAsm(..) => "global asm",
18781891
ItemKind::Ty(..) => "type alias",
18791892
ItemKind::Enum(..) => "enum",
18801893
ItemKind::Struct(..) => "struct",

src/libsyntax/ext/expand.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,7 @@ impl<'feat> ExpansionConfig<'feat> {
10781078
feature_tests! {
10791079
fn enable_quotes = quote,
10801080
fn enable_asm = asm,
1081+
fn enable_global_asm = global_asm,
10811082
fn enable_log_syntax = log_syntax,
10821083
fn enable_concat_idents = concat_idents,
10831084
fn enable_trace_macros = trace_macros,

src/libsyntax/feature_gate.rs

+6
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ declare_features! (
342342

343343
// Allows the `catch {...}` expression
344344
(active, catch_expr, "1.17.0", Some(31436)),
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! (
@@ -981,6 +984,9 @@ pub const EXPLAIN_STMT_ATTR_SYNTAX: &'static str =
981984
pub const EXPLAIN_ASM: &'static str =
982985
"inline assembly is not stable enough for use and is subject to change";
983986

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

0 commit comments

Comments
 (0)