From a8245f5a38d0aa9cc392a1893fcbefb29bf77c2b Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 7 May 2019 09:55:12 +1000 Subject: [PATCH] Add lots of static `Symbols`. These will be used in the subsequent commits. Many of them are attributes. The commit also adds the ability to handle symbols that aren't identifiers (e.g. "proc-macro"). --- src/librustc_macros/src/symbols.rs | 43 +++- src/libsyntax_pos/symbol.rs | 388 ++++++++++++++++++++++++++++- 2 files changed, 415 insertions(+), 16 deletions(-) diff --git a/src/librustc_macros/src/symbols.rs b/src/librustc_macros/src/symbols.rs index 169c5e13718..a4c7daf088f 100644 --- a/src/librustc_macros/src/symbols.rs +++ b/src/librustc_macros/src/symbols.rs @@ -11,7 +11,7 @@ use quote::quote; #[allow(non_camel_case_types)] mod kw { syn::custom_keyword!(Keywords); - syn::custom_keyword!(Other); + syn::custom_keyword!(Symbols); } struct Keyword { @@ -33,14 +33,24 @@ impl Parse for Keyword { } } -struct Symbol(Ident); +struct Symbol { + name: Ident, + value: Option, +} impl Parse for Symbol { fn parse(input: ParseStream<'_>) -> Result { - let ident: Ident = input.parse()?; + let name = input.parse()?; + let value = match input.parse::() { + Ok(_) => Some(input.parse()?), + Err(_) => None, + }; input.parse::()?; - Ok(Symbol(ident)) + Ok(Symbol { + name, + value, + }) } } @@ -69,7 +79,7 @@ impl Parse for Input { braced!(content in input); let keywords = content.parse()?; - input.parse::()?; + input.parse::()?; let content; braced!(content in input); let symbols = content.parse()?; @@ -116,19 +126,22 @@ pub fn symbols(input: TokenStream) -> TokenStream { } for symbol in &input.symbols.0 { - let value = &symbol.0; - let value_str = value.to_string(); - check_dup(&value_str); + let name = &symbol.name; + let value = match &symbol.value { + Some(value) => value.value(), + None => name.to_string(), + }; + check_dup(&value); prefill_stream.extend(quote! { - #value_str, + #value, }); symbols_stream.extend(quote! { - pub const #value: Symbol = Symbol::new(#counter); + pub const #name: Symbol = Symbol::new(#counter); }); counter += 1; } - TokenStream::from(quote! { + let tt = TokenStream::from(quote! { macro_rules! keywords { () => { #keyword_stream @@ -159,5 +172,11 @@ pub fn symbols(input: TokenStream) -> TokenStream { ]) } } - }) + }); + + // To see the generated code generated, uncomment this line, recompile, and + // run the resulting output through `rustfmt`. + //eprintln!("{}", tt); + + tt } diff --git a/src/libsyntax_pos/symbol.rs b/src/libsyntax_pos/symbol.rs index c0c9c2a1000..5459f3904fa 100644 --- a/src/libsyntax_pos/symbol.rs +++ b/src/libsyntax_pos/symbol.rs @@ -99,88 +99,375 @@ symbols! { Union: "union", } - // Other symbols that can be referred to with syntax_pos::symbols::* - Other { + // Symbols that can be referred to with syntax_pos::symbols::*. The symbol + // is the stringified identifier unless otherwise specified (e.g. + // `proc_dash_macro` represents "proc-macro"). + Symbols { + aarch64_target_feature, + abi, + abi_amdgpu_kernel, + abi_msp430_interrupt, + abi_ptx, + abi_sysv64, + abi_thiscall, + abi_unadjusted, + abi_vectorcall, + abi_x86_interrupt, + aborts, + advanced_slice_patterns, + adx_target_feature, alias, align, + alignstack, + all, + allocator, + allocator_internals, alloc_error_handler, allow, + allowed, allow_fail, allow_internal_unsafe, allow_internal_unstable, + allow_internal_unstable_backcompat_hack, + always, + any, + arbitrary_self_types, + arm_target_feature, + asm, + associated_consts, + associated_type_defaults, + associated_types, + async_await, + attr, + attributes, + attr_literals, + augmented_assignments, automatically_derived, + avx512_target_feature, + await_macro, + bin, + bind_by_move_pattern_guards, + block, + borrowck_graphviz_postflow, + borrowck_graphviz_preflow, + box_patterns, + box_syntax, + braced_empty_structs, + C, + cdylib, cfg, cfg_attr, + cfg_attr_multi, + cfg_target_feature, + cfg_target_has_atomic, + cfg_target_thread_local, + cfg_target_vendor, + clone, + clone_closures, + clone_from, + closure_to_fn_coercion, + cmpxchg16b_target_feature, cold, + compile_error, compiler_builtins, + concat_idents, + conservative_impl_trait, + console, + const_compare_raw_pointers, + const_fn, + const_fn_union, + const_generics, + const_indexing, + const_let, + const_panic, + const_raw_ptr_deref, + const_raw_ptr_to_usize_cast, + const_transmute, + contents, + convert, + copy_closures, + core, + core_intrinsics, crate_id, + crate_in_paths, crate_name, crate_type, + crate_visibility_modifier, + custom_attribute, + custom_derive, + custom_inner_attributes, + custom_test_frameworks, + c_variadic, + decl_macro, default_lib_allocator, + default_type_parameter_fallback, + default_type_params, deny, deprecated, derive, doc, + doc_alias, + doc_cfg, + doc_keyword, + doc_masked, + doc_spotlight, + document_private_items, + dotdoteq_in_patterns, + dotdot_in_tuple_patterns, + dropck_eyepatch, + dropck_parametricity, + drop_types_in_const, + dylib, + dyn_trait, + eh_personality, + eh_unwind_resume, + enable, + Err, + except, + exclusive_range_pattern, + exhaustive_integer_patterns, + exhaustive_patterns, + existential_type, + expected, export_name, + extern_absolute_paths, + external_doc, + extern_crate_item_prelude, + extern_crate_self, + extern_in_paths, + extern_prelude, + extern_types, + f16c_target_feature, feature, ffi_returns_twice, + field_init_shorthand, + file, + fn_must_use, forbid, + format_args_nl, + from, + From, + from_error, + from_generator, + from_ok, fundamental, + future, + Future, + generators, + generic_associated_types, + generic_param_attrs, global_allocator, + global_asm, + globs, + hexagon_target_feature, + hidden, + homogeneous_aggregate, + html_favicon_url, + html_logo_url, + html_no_source, + html_playground_url, + html_root_url, + i128, + i128_type, + i16, + i32, + i64, + i8, + ident, + if_let, + if_while_or_patterns, ignore, + impl_header_lifetime_elision, + impl_trait_in_bindings, + import_shadowing, + in_band_lifetimes, include, + inclusive_range_syntax, + infer_outlives_requirements, + infer_static_outlives_requirements, inline, + intel, + into_iter, + IntoIterator, + into_result, + intrinsics, + irrefutable_let_patterns, + isize, + issue, + issue_5723_bootstrap, + issue_tracker_base_url, + item_like_imports, + iter, + Iterator, keyword, + kind, + label, + label_break_value, lang, + lang_items, + lib, link, + linkage, link_args, + link_cfg, + link_llvm_intrinsics, link_name, link_section, - linkage, + lint_reasons, + local_inner_macros, + log_syntax, + loop_break_value, + macro_at_most_once_rep, macro_escape, macro_export, + macro_lifetime_matcher, + macro_literal_matcher, + macro_reexport, + macro_rules, + macros_in_extern, macro_use, + macro_vis_matcher, main, + managed_boxes, marker, + marker_trait_attr, masked, + match_beginning_vert, + match_default_bindings, may_dangle, + message, + min_const_fn, + min_const_unsafe_fn, + mips_target_feature, + mmx_target_feature, + module, + more_struct_aliases, + movbe_target_feature, must_use, naked, + naked_functions, + name, needs_allocator, needs_panic_runtime, + negate_unsigned, + never, + never_type, + next, + nll, no_builtins, no_core, + no_crate_inject, no_debug, + no_default_passes, no_implicit_prelude, + no_inline, no_link, no_main, no_mangle, + non_ascii_idents, + None, + non_exhaustive, + non_modrs_mods, + no_stack_check, no_start, no_std, - non_exhaustive, + not, + note, + Ok, omit_gdb_pretty_printer_section, + on, + on_unimplemented, + oom, + ops, optimize, + optimize_attribute, + optin_builtin_traits, + option, + Option, + opt_out_copy, + overlapping_marker_traits, + packed, panic_handler, + panic_impl, + panic_implementation, panic_runtime, + passes, path, + pattern_parentheses, + Pending, + pin, + Pin, + platform_intrinsics, plugin, plugin_registrar, + plugins, + Poll, + poll_with_tls_context, + powerpc_target_feature, + precise_pointer_size_matching, + prelude, prelude_import, + primitive, + proc_dash_macro: "proc-macro", proc_macro, proc_macro_attribute, proc_macro_derive, + proc_macro_expr, + proc_macro_gen, + proc_macro_hygiene, + proc_macro_mod, + proc_macro_non_items, + proc_macro_path_invoc, profiler_runtime, + pub_restricted, + pushpop_unsafe, + quad_precision_float, + question_mark, + quote, + Range, + RangeFrom, + RangeFull, + RangeInclusive, + RangeTo, + RangeToInclusive, + raw_identifiers, + Ready, + reason, recursion_limit, reexport_test_harness_main, + reflect, + relaxed_adts, repr, + repr128, + repr_align, + repr_align_enum, + repr_packed, + repr_simd, + repr_transparent, + re_rebalance_coherence, + result, + Result, + Return, + rlib, + rtm_target_feature, + rust, + rust_2015_preview, + rust_2018_preview, + rust_begin_unwind, + rustc_allocator_nounwind, + rustc_allow_const_fn_ptr, rustc_args_required_const, + rustc_attrs, rustc_clean, rustc_const_unstable, rustc_conversion_suggestion, rustc_copy_clone_marker, rustc_def_path, rustc_deprecated, + rustc_diagnostic_macros, rustc_dirty, + rustc_doc_only_macro, + rustc_dump_env_program_clauses, rustc_dump_program_clauses, rustc_dump_user_substs, rustc_error, @@ -191,13 +478,21 @@ symbols! { rustc_layout_scalar_valid_range_end, rustc_layout_scalar_valid_range_start, rustc_mir, + rustc_object_lifetime_default, rustc_on_unimplemented, rustc_outlives, rustc_paren_sugar, rustc_partition_codegened, rustc_partition_reused, + rustc_peek, + rustc_peek_definite_init, + rustc_peek_maybe_init, + rustc_peek_maybe_uninit, + rustc_private, rustc_proc_macro_decls, + rustc_promotable, rustc_regions, + rustc_stable, rustc_std_internal_symbol, rustc_symbol_name, rustc_synthetic, @@ -205,23 +500,108 @@ symbols! { rustc_then_this_would_need, rustc_transparent_macro, rustc_variance, + rustdoc, + rust_eh_personality, + rust_eh_unwind_resume, + rust_oom, + __rust_unstable_column, + rvalue_static_promotion, sanitizer_runtime, + self_in_typedefs, + self_struct_ctor, + Send, should_panic, simd, + simd_ffi, + since, + size, + slice_patterns, + slicing_syntax, + Some, + specialization, + speed, spotlight, + sse4a_target_feature, stable, + staged_api, start, + static_in_const, + staticlib, + static_nobundle, + static_recursion, + std, + stmt_expr_attributes, + stop_after_dataflow, + struct_field_attributes, + struct_inherit, structural_match, + struct_variant, + suggestion, target_feature, + target_has_atomic, + target_thread_local, + task, + tbm_target_feature, + termination_trait, + termination_trait_test, + test, + test_2018_feature, + test_accepted_feature, + test_removed_feature, test_runner, thread_local, + tool_attributes, + tool_lints, + trace_macros, + trait_alias, + transmute, + transparent, + trivial_bounds, + Try, + try_blocks, + tuple_indexing, + ty, + type_alias_enum_variants, + type_ascription, type_length_limit, + type_macros, + u128, + u16, + u32, + u64, + u8, + unboxed_closures, + underscore_const_names, + underscore_imports, + underscore_lifetimes, + uniform_paths, + universal_impl_trait, + unmarked_api, + unrestricted_attribute_tokens, unsafe_destructor_blind_to_params, + unsafe_no_drop_flag, + unsized_locals, + unsized_tuple_coercion, unstable, + untagged_unions, unwind, + unwind_attributes, used, + use_extern_macros, + use_nested_groups, + usize, + v1, + vis, + visible_private_types, + volatile, warn, + warn_directory_ownership, + wasm_import_module, + wasm_target_feature, + while_let, + windows, windows_subsystem, + Yield, } }