rustc_attr_parsing/attributes/
instruction_set.rs

1use rustc_hir::attrs::InstructionSetAttr;
2
3use super::prelude::*;
4use crate::session_diagnostics;
5
6pub(crate) struct InstructionSetParser;
7
8impl<S: Stage> SingleAttributeParser<S> for InstructionSetParser {
9    const PATH: &[Symbol] = &[sym::instruction_set];
10    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowListWarnRest(&[
11        Allow(Target::Fn),
12        Allow(Target::Closure),
13        Allow(Target::Method(MethodKind::Inherent)),
14        Allow(Target::Method(MethodKind::TraitImpl)),
15        Allow(Target::Method(MethodKind::Trait { body: true })),
16    ]);
17    const TEMPLATE: AttributeTemplate = template!(List: &["set"], "https://doc.rust-lang.org/reference/attributes/codegen.html#the-instruction_set-attribute");
18    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
19    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
20
21    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
22        const POSSIBLE_SYMBOLS: &[Symbol] = &[sym::arm_a32, sym::arm_t32];
23        const POSSIBLE_ARM_SYMBOLS: &[Symbol] = &[sym::a32, sym::t32];
24        let Some(maybe_meta_item) = args.list().and_then(MetaItemListParser::single) else {
25            cx.expected_specific_argument(cx.attr_span, POSSIBLE_SYMBOLS);
26            return None;
27        };
28
29        let Some(meta_item) = maybe_meta_item.meta_item() else {
30            cx.expected_specific_argument(maybe_meta_item.span(), POSSIBLE_SYMBOLS);
31            return None;
32        };
33
34        let mut segments = meta_item.path().segments();
35
36        let Some(architecture) = segments.next() else {
37            cx.expected_specific_argument(meta_item.span(), POSSIBLE_SYMBOLS);
38            return None;
39        };
40
41        let Some(instruction_set) = segments.next() else {
42            cx.expected_specific_argument(architecture.span, POSSIBLE_SYMBOLS);
43            return None;
44        };
45
46        let instruction_set = match architecture.name {
47            sym::arm => {
48                if !cx.sess.target.has_thumb_interworking {
49                    cx.dcx().emit_err(session_diagnostics::UnsupportedInstructionSet {
50                        span: cx.attr_span,
51                        instruction_set: sym::arm,
52                        current_target: &cx.sess.opts.target_triple,
53                    });
54                    return None;
55                }
56                match instruction_set.name {
57                    sym::a32 => InstructionSetAttr::ArmA32,
58                    sym::t32 => InstructionSetAttr::ArmT32,
59                    _ => {
60                        cx.expected_specific_argument(instruction_set.span, POSSIBLE_ARM_SYMBOLS);
61                        return None;
62                    }
63                }
64            }
65            _ => {
66                cx.expected_specific_argument(architecture.span, POSSIBLE_SYMBOLS);
67                return None;
68            }
69        };
70
71        Some(AttributeKind::InstructionSet(instruction_set))
72    }
73}