gccrs: Detect whether we are compiling core or any other crate
Enables gccrs to distinguish between compiling the 'core' crate and other crates for correct path generation.
This commit enhances gccrs’s ability to differentiate between compiling the standard library’s ‘core’ crate and other user crates. This distinction is crucial for generating correct canonical paths, particularly during macro expansion and trait derivation. For instance, when deriving traits in a regular crate, paths should refer to core::marker::Copy, but when compiling core itself, paths must correctly point to crate::marker::Copy.
In Details
The gccrs macro expansion and AST building infrastructure is updated to track the context of compilation. A new Source enum is introduced in ast/rust-ast-builder.h to discriminate AST node generation occurring within core versus other crates. The MacroExpander struct gains a public crate member, and various desugaring components now use this context to emit paths relative to the current crate or the core crate appropriately, resolving issues with builtin trait derivations and lang item references.
- macro expansion
- The process by which Rust macros are transformed into regular Rust code before compilation. This involves a specialized phase where macro invocations are replaced by their generated output.
- canonical path
- A fully qualified and unambiguous path to a module, type, or function within a Rust project. For example,
std::collections::HashMapis a canonical path. - trait derivation
- The process where the compiler automatically generates an implementation of a trait for a type, typically using the
#[derive(...)]attribute. For example,#[derive(Debug)]automatically implements theDebugtrait. - lang item
- Special, built-in items in the Rust language (like primitive types, traits like
Copy, or thepanic!function) that are fundamental to the language's operation and are handled specially by the compiler.