Skip to main content

bootstrap/core/
compiler.rs

1use std::hash::{Hash, Hasher};
2
3use crate::core::config::TargetSelection;
4use crate::core::session::Session;
5
6/// A structure representing a Rust compiler.
7///
8/// Each compiler has a `stage` that it is associated with and a `host` that
9/// corresponds to the platform the compiler runs on.
10#[derive(Eq, PartialOrd, Ord, Clone, Copy, Debug)]
11pub struct Compiler {
12    pub(crate) stage: u32,
13    pub(crate) host: TargetSelection,
14    /// Indicates whether the compiler was forced to use a specific stage.
15    /// This field is ignored in `Hash` and `PartialEq` implementations as only the `stage`
16    /// and `host` fields are relevant for those.
17    pub(crate) forced_compiler: bool,
18}
19
20impl Hash for Compiler {
21    fn hash<H: Hasher>(&self, state: &mut H) {
22        self.stage.hash(state);
23        self.host.hash(state);
24    }
25}
26
27impl PartialEq for Compiler {
28    fn eq(&self, other: &Self) -> bool {
29        self.stage == other.stage && self.host == other.host
30    }
31}
32
33impl Compiler {
34    pub(crate) fn new(stage: u32, host: TargetSelection) -> Self {
35        Self { stage, host, forced_compiler: false }
36    }
37
38    pub(crate) fn forced_compiler(&mut self, forced_compiler: bool) {
39        self.forced_compiler = forced_compiler;
40    }
41
42    /// Returns `true` if this is a snapshot compiler for the session's configuration
43    pub(crate) fn is_snapshot(&self, sess: &Session) -> bool {
44        self.stage == 0 && self.host == sess.host_target
45    }
46
47    /// Indicates whether the compiler was forced to use a specific stage.
48    pub(crate) fn is_forced_compiler(&self) -> bool {
49        self.forced_compiler
50    }
51}