frontend 0.4.1

rustc's frontend with no LLVM and no std: parsing through MIR, as a library
//! This crate contains implementations of built-in macros and other code generating facilities
//! injecting code into the crate before it is lowered to HIR.

// tidy-alphabetical-start
// tidy-alphabetical-end

#![allow(internal_features)]
// `#![no_std]`: these arrive with the standard prelude and name no path, so a `std::`
// search cannot see them - and a `#[derive]` can use them without the name appearing
// in this file at all, which is why they are not trimmed by inspection.
// Real std remains: `env!`/`option_env!` are literally `std::env::var`, and `include!`
// resolves a `Path`. Both are the macro's semantics, not an implementation detail.

// ---------------------------------------------------------------------------------------------
// STD IS BANNED IN THIS CRATE.
//
// `#![no_std]` above is the ban and the compiler is the enforcer: without `extern crate std;`
// there is no `std` in the extern prelude, so any `std::` path fails to resolve and the build
// stops. Do not add that line back to make an error go away - the error is the point. Whatever
// needed `std` either has a `core`/`alloc` equivalent, belongs in `ekostd`, or is a
// dependency that has to be replaced.
//
// The prelude is the part a grep cannot see: `Vec`, `String`, `Box`, `format!`, `vec!`,
// `thread_local!` and `println!` name no path. Under `#![no_std]` they resolve through `alloc`
// and `eko` instead, which is why those imports appear at the top of every file here.
// ---------------------------------------------------------------------------------------------
#[macro_use]
use alloc::borrow::ToOwned;
use alloc::boxed::Box;
use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;

use alloc::sync::Arc;

use crate::rustc_expand::base::{MacroExpanderFn, ResolverExpand, SyntaxExtensionKind};
use crate::rustc_expand::proc_macro::BangProcMacro;
use crate::rustc_span::sym;

use crate::rustc_builtin_macros::deriving::*;

mod alloc_error_handler;
mod assert;
mod autodiff;
mod cfg;
mod cfg_accessible;
mod cfg_eval;
mod cfg_select;
mod compile_error;
mod concat;
mod concat_bytes;
mod define_opaque;
mod derive;
mod deriving;
mod diagnostics;
mod direct_const_arg;
mod edition_panic;
mod eii;
mod env;
mod format;
mod format_foreign;
mod global_allocator;
mod iter;
mod log_syntax;
mod offload;
mod pattern_type;
mod source_util;
mod test;
mod test_binder_constraints;
mod trace_macros;
mod view_type;

pub mod asm;
pub mod cmdline_attrs;
pub mod contracts;
pub mod proc_macro_harness;
pub mod standard_library_imports;
pub mod test_harness;
pub mod util;

pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
    let mut register = |name, kind| resolver.register_builtin_macro(name, kind);
    // Local `macro_rules!` (were decl_macro `macro`s). Defined after `register` is bound, so
    // mixed-site hygiene still lets the bodies call it; the paths they use are this module's
    // imports, which are also in scope at the only call sites below.
    macro_rules! register_bang {
        ($($name:ident: $f:expr,)*) => {
            $(register(sym::$name, SyntaxExtensionKind::LegacyBang(Arc::new($f as MacroExpanderFn)));)*
        };
    }
    macro_rules! register_attr {
        ($($name:ident: $f:expr,)*) => {
            $(register(sym::$name, SyntaxExtensionKind::LegacyAttr(Arc::new($f)));)*
        };
    }
    macro_rules! register_derive {
        ($($name:ident: $f:expr,)*) => {
            $(register(sym::$name, SyntaxExtensionKind::LegacyDerive(Arc::new(BuiltinDerive($f))));)*
        };
    }

    register_bang! {
        // tidy-alphabetical-start
        asm: asm::expand_asm,
        assert: assert::expand_assert,
        cfg: cfg::expand_cfg,
        cfg_select: cfg_select::expand_cfg_select,
        column: source_util::expand_column,
        compile_error: compile_error::expand_compile_error,
        concat: concat::expand_concat,
        concat_bytes: concat_bytes::expand_concat_bytes,
        const_format_args: format::expand_format_args,
        core_panic: edition_panic::expand_panic,
        direct_const_arg: direct_const_arg::expand,
        env: env::expand_env,
        file: source_util::expand_file,
        format_args: format::expand_format_args,
        format_args_nl: format::expand_format_args_nl,
        global_asm: asm::expand_global_asm,
        include: source_util::expand_include,
        include_bytes: source_util::expand_include_bytes,
        include_str: source_util::expand_include_str,
        iter: iter::expand,
        line: source_util::expand_line,
        log_syntax: log_syntax::expand_log_syntax,
        module_path: source_util::expand_mod,
        naked_asm: asm::expand_naked_asm,
        option_env: env::expand_option_env,
        pattern_type: pattern_type::expand,
        std_panic: edition_panic::expand_panic,
        stringify: source_util::expand_stringify,
        test_binder_constraints: test_binder_constraints::expand,
        trace_macros: trace_macros::expand_trace_macros,
        unreachable: edition_panic::expand_unreachable,
        view_type: view_type::expand,
        // tidy-alphabetical-end
    }

    register_attr! {
        // tidy-alphabetical-start
        alloc_error_handler: alloc_error_handler::expand,
        autodiff_forward: autodiff::expand_forward,
        autodiff_reverse: autodiff::expand_reverse,
        bench: test::expand_bench,
        cfg_accessible: cfg_accessible::Expander,
        cfg_eval: cfg_eval::expand,
        define_opaque: define_opaque::expand,
        derive: derive::Expander { is_const: false },
        derive_const: derive::Expander { is_const: true },
        eii: eii::eii,
        eii_declaration: eii::eii_declaration,
        eii_shared_macro: eii::eii_shared_macro,
        global_allocator: global_allocator::expand,
        offload_kernel: offload::expand_kernel,
        test: test::expand_test,
        test_case: test::expand_test_case,
        unsafe_eii: eii::unsafe_eii,
        // tidy-alphabetical-end
    }

    register_derive! {
        Clone: clone::expand_deriving_clone,
        CoerceShared: reborrow::expand_deriving_coerce_shared,
        Copy: bounds::expand_deriving_copy,
        ConstParamTy: bounds::expand_deriving_const_param_ty,
        Debug: debug::expand_deriving_debug,
        Default: default::expand_deriving_default,
        Eq: eq::expand_deriving_eq,
        Hash: hash::expand_deriving_hash,
        Ord: ord::expand_deriving_ord,
        PartialEq: partial_eq::expand_deriving_partial_eq,
        PartialOrd: partial_ord::expand_deriving_partial_ord,
        CoercePointee: coerce_pointee::expand_deriving_coerce_pointee,
        Reborrow: reborrow::expand_deriving_reborrow,
        From: from::expand_deriving_from,
    }

    let client = crate::rustc_proc_macro::bridge::client::Client::expand1(crate::rustc_proc_macro::quote);
    register(sym::quote, SyntaxExtensionKind::Bang(Arc::new(BangProcMacro { client })));
    let requires = SyntaxExtensionKind::Attr(Arc::new(contracts::ExpandRequires));
    register(sym::contracts_requires, requires);
    let ensures = SyntaxExtensionKind::Attr(Arc::new(contracts::ExpandEnsures));
    register(sym::contracts_ensures, ensures);
}