Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//! This modules handles hygiene information.
//!
//! Specifically, `ast` + `Hygiene` allows you to create a `Name`. Note that, at
//! this moment, this is horribly incomplete and handles only `$crate`.
use base_db::span::{MacroCallId, SyntaxContextId};

use crate::db::ExpandDatabase;

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct SyntaxContextData {
    pub outer_expn: Option<MacroCallId>,
    pub outer_transparency: Transparency,
    pub parent: SyntaxContextId,
    /// This context, but with all transparent and semi-transparent expansions filtered away.
    pub opaque: SyntaxContextId,
    /// This context, but with all transparent expansions filtered away.
    pub opaque_and_semitransparent: SyntaxContextId,
}

impl SyntaxContextData {
    pub fn root() -> Self {
        SyntaxContextData {
            outer_expn: None,
            outer_transparency: Transparency::Opaque,
            parent: SyntaxContextId::ROOT,
            opaque: SyntaxContextId::ROOT,
            opaque_and_semitransparent: SyntaxContextId::ROOT,
        }
    }
}

/// A property of a macro expansion that determines how identifiers
/// produced by that expansion are resolved.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Hash, Debug)]
pub enum Transparency {
    /// Identifier produced by a transparent expansion is always resolved at call-site.
    /// Call-site spans in procedural macros, hygiene opt-out in `macro` should use this.
    Transparent,
    /// Identifier produced by a semi-transparent expansion may be resolved
    /// either at call-site or at definition-site.
    /// If it's a local variable, label or `$crate` then it's resolved at def-site.
    /// Otherwise it's resolved at call-site.
    /// `macro_rules` macros behave like this, built-in macros currently behave like this too,
    /// but that's an implementation detail.
    SemiTransparent,
    /// Identifier produced by an opaque expansion is always resolved at definition-site.
    /// Def-site spans in procedural macros, identifiers from `macro` by default use this.
    Opaque,
}

pub(super) fn apply_mark(
    db: &dyn ExpandDatabase,
    ctxt: SyntaxContextId,
    call_id: MacroCallId,
    transparency: Transparency,
) -> SyntaxContextId {
    if transparency == Transparency::Opaque {
        return apply_mark_internal(db, ctxt, Some(call_id), transparency);
    }

    let call_site_ctxt = db.lookup_intern_macro_call(call_id).call_site;
    let mut call_site_ctxt = if transparency == Transparency::SemiTransparent {
        call_site_ctxt.normalize_to_macros_2_0(db)
    } else {
        call_site_ctxt.normalize_to_macro_rules(db)
    };

    if call_site_ctxt.is_root(db) {
        return apply_mark_internal(db, ctxt, Some(call_id), transparency);
    }

    // Otherwise, `expn_id` is a macros 1.0 definition and the call site is in a
    // macros 2.0 expansion, i.e., a macros 1.0 invocation is in a macros 2.0 definition.
    //
    // In this case, the tokens from the macros 1.0 definition inherit the hygiene
    // at their invocation. That is, we pretend that the macros 1.0 definition
    // was defined at its invocation (i.e., inside the macros 2.0 definition)
    // so that the macros 2.0 definition remains hygienic.
    //
    // See the example at `test/ui/hygiene/legacy_interaction.rs`.
    for (call_id, transparency) in ctxt.marks(db) {
        call_site_ctxt = apply_mark_internal(db, call_site_ctxt, call_id, transparency);
    }
    apply_mark_internal(db, call_site_ctxt, Some(call_id), transparency)
}

fn apply_mark_internal(
    db: &dyn ExpandDatabase,
    ctxt: SyntaxContextId,
    call_id: Option<MacroCallId>,
    transparency: Transparency,
) -> SyntaxContextId {
    let syntax_context_data = db.lookup_intern_syntax_context(ctxt);
    let mut opaque = syntax_context_data.opaque;
    let mut opaque_and_semitransparent = syntax_context_data.opaque_and_semitransparent;

    if transparency >= Transparency::Opaque {
        let parent = opaque;
        let new_opaque = SyntaxContextId::SELF_REF;
        // But we can't just grab the to be allocated ID either as that would not deduplicate
        // things!
        // So we need a new salsa store type here ...
        opaque = db.intern_syntax_context(SyntaxContextData {
            outer_expn: call_id,
            outer_transparency: transparency,
            parent,
            opaque: new_opaque,
            opaque_and_semitransparent: new_opaque,
        });
    }

    if transparency >= Transparency::SemiTransparent {
        let parent = opaque_and_semitransparent;
        let new_opaque_and_semitransparent = SyntaxContextId::SELF_REF;
        opaque_and_semitransparent = db.intern_syntax_context(SyntaxContextData {
            outer_expn: call_id,
            outer_transparency: transparency,
            parent,
            opaque,
            opaque_and_semitransparent: new_opaque_and_semitransparent,
        });
    }

    let parent = ctxt;
    db.intern_syntax_context(SyntaxContextData {
        outer_expn: call_id,
        outer_transparency: transparency,
        parent,
        opaque,
        opaque_and_semitransparent,
    })
}
pub trait SyntaxContextExt {
    fn is_root(self, db: &dyn ExpandDatabase) -> bool;
    fn normalize_to_macro_rules(self, db: &dyn ExpandDatabase) -> Self;
    fn normalize_to_macros_2_0(self, db: &dyn ExpandDatabase) -> Self;
    fn parent_ctxt(self, db: &dyn ExpandDatabase) -> Self;
    fn outer_mark(self, db: &dyn ExpandDatabase) -> (Option<MacroCallId>, Transparency);
    fn marks(self, db: &dyn ExpandDatabase) -> Vec<(Option<MacroCallId>, Transparency)>;
}

#[inline(always)]
fn handle_self_ref(p: SyntaxContextId, n: SyntaxContextId) -> SyntaxContextId {
    match n {
        SyntaxContextId::SELF_REF => p,
        _ => n,
    }
}

impl SyntaxContextExt for SyntaxContextId {
    fn is_root(self, db: &dyn ExpandDatabase) -> bool {
        db.lookup_intern_syntax_context(self).outer_expn.is_none()
    }
    fn normalize_to_macro_rules(self, db: &dyn ExpandDatabase) -> Self {
        handle_self_ref(self, db.lookup_intern_syntax_context(self).opaque_and_semitransparent)
    }
    fn normalize_to_macros_2_0(self, db: &dyn ExpandDatabase) -> Self {
        handle_self_ref(self, db.lookup_intern_syntax_context(self).opaque)
    }
    fn parent_ctxt(self, db: &dyn ExpandDatabase) -> Self {
        db.lookup_intern_syntax_context(self).parent
    }
    fn outer_mark(self, db: &dyn ExpandDatabase) -> (Option<MacroCallId>, Transparency) {
        let data = db.lookup_intern_syntax_context(self);
        (data.outer_expn, data.outer_transparency)
    }
    fn marks(mut self, db: &dyn ExpandDatabase) -> Vec<(Option<MacroCallId>, Transparency)> {
        let mut marks = Vec::new();
        while self != SyntaxContextId::ROOT {
            marks.push(self.outer_mark(db));
            self = self.parent_ctxt(db);
        }
        marks.reverse();
        marks
    }
}

// pub(super) fn with_ctxt_from_mark(db: &ExpandDatabase, file_id: HirFileId) {
//     self.with_ctxt_from_mark(expn_id, Transparency::Transparent)
// }
// pub(super) fn with_call_site_ctxt(db: &ExpandDatabase, file_id: HirFileId) {
//     self.with_ctxt_from_mark(expn_id, Transparency::Transparent)
// }