2018-04-15 16:17:18 +02:00
|
|
|
//! Machinery for hygienic macros, inspired by the `MTWT[1]` paper.
|
2014-02-25 04:47:19 +08:00
|
|
|
//!
|
2018-04-15 16:17:18 +02:00
|
|
|
//! `[1]` Matthew Flatt, Ryan Culpepper, David Darais, and Robert Bruce Findler. 2012.
|
2017-12-31 17:17:01 +01:00
|
|
|
//! *Macros that work together: Compile-time bindings, partial expansion,
|
2014-02-25 04:47:19 +08:00
|
|
|
//! and definition contexts*. J. Funct. Program. 22, 2 (March 2012), 181-216.
|
2018-05-14 07:17:56 +02:00
|
|
|
//! DOI=10.1017/S0956796812000093 <https://doi.org/10.1017/S0956796812000093>
|
2014-02-25 04:47:19 +08:00
|
|
|
|
2019-06-03 13:08:15 +10:00
|
|
|
// Hygiene data is stored in a global variable and accessed via TLS, which
|
|
|
|
// means that accesses are somewhat expensive. (`HygieneData::with`
|
|
|
|
// encapsulates a single access.) Therefore, on hot code paths it is worth
|
|
|
|
// ensuring that multiple HygieneData accesses are combined into a single
|
|
|
|
// `HygieneData::with`.
|
|
|
|
//
|
2019-07-16 01:04:05 +03:00
|
|
|
// This explains why `HygieneData`, `SyntaxContext` and `ExpnId` have interfaces
|
2019-06-03 13:08:15 +10:00
|
|
|
// with a certain amount of redundancy in them. For example,
|
2019-08-13 23:56:42 +03:00
|
|
|
// `SyntaxContext::outer_expn_data` combines `SyntaxContext::outer` and
|
|
|
|
// `ExpnId::expn_data` so that two `HygieneData` accesses can be performed within
|
2019-06-03 13:08:15 +10:00
|
|
|
// a single `HygieneData::with` call.
|
|
|
|
//
|
|
|
|
// It also explains why many functions appear in `HygieneData` and again in
|
2019-07-16 01:04:05 +03:00
|
|
|
// `SyntaxContext` or `ExpnId`. For example, `HygieneData::outer` and
|
2019-06-03 13:08:15 +10:00
|
|
|
// `SyntaxContext::outer` do the same thing, but the former is for use within a
|
|
|
|
// `HygieneData::with` call while the latter is for use outside such a call.
|
|
|
|
// When modifying this file it is important to understand this distinction,
|
|
|
|
// because getting it wrong can lead to nested `HygieneData::with` calls that
|
|
|
|
// trigger runtime aborts. (Fortunately these are obvious and easy to fix.)
|
|
|
|
|
2019-02-04 03:42:27 +09:00
|
|
|
use crate::GLOBALS;
|
2019-06-30 03:05:52 +03:00
|
|
|
use crate::{Span, DUMMY_SP};
|
2019-04-06 00:15:49 +02:00
|
|
|
use crate::edition::Edition;
|
2019-05-11 17:41:37 +03:00
|
|
|
use crate::symbol::{kw, Symbol};
|
2017-03-17 04:04:41 +00:00
|
|
|
|
2019-07-23 18:50:47 +03:00
|
|
|
use rustc_serialize::{Encodable, Decodable, Encoder, Decoder};
|
2019-06-20 10:34:51 +03:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2019-02-08 10:21:21 +01:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2019-07-05 03:09:24 +03:00
|
|
|
use std::fmt;
|
2014-02-25 04:47:19 +08:00
|
|
|
|
2019-07-16 01:42:58 +03:00
|
|
|
/// A `SyntaxContext` represents a chain of pairs `(ExpnId, Transparency)` named "marks".
|
2019-06-30 14:57:34 +03:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2018-06-30 19:35:00 +03:00
|
|
|
pub struct SyntaxContext(u32);
|
2014-02-25 04:47:19 +08:00
|
|
|
|
2019-06-30 14:57:34 +03:00
|
|
|
#[derive(Debug)]
|
2018-06-30 19:35:00 +03:00
|
|
|
struct SyntaxContextData {
|
2019-07-16 01:42:58 +03:00
|
|
|
outer_expn: ExpnId,
|
2019-07-16 02:59:53 +03:00
|
|
|
outer_transparency: Transparency,
|
|
|
|
parent: SyntaxContext,
|
2019-07-16 01:42:58 +03:00
|
|
|
/// This context, but with all transparent and semi-transparent expansions filtered away.
|
2018-06-30 19:35:00 +03:00
|
|
|
opaque: SyntaxContext,
|
2019-07-16 01:42:58 +03:00
|
|
|
/// This context, but with all transparent expansions filtered away.
|
2018-06-30 19:35:00 +03:00
|
|
|
opaque_and_semitransparent: SyntaxContext,
|
2019-04-22 21:29:11 +03:00
|
|
|
/// Name of the crate to which `$crate` with this context would resolve.
|
2018-12-09 17:46:12 +03:00
|
|
|
dollar_crate_name: Symbol,
|
2014-02-25 04:47:19 +08:00
|
|
|
}
|
|
|
|
|
2019-07-16 01:42:58 +03:00
|
|
|
/// A unique ID associated with a macro invocation and expansion.
|
2019-06-30 14:57:34 +03:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
|
2019-07-16 01:04:05 +03:00
|
|
|
pub struct ExpnId(u32);
|
2014-02-25 04:47:19 +08:00
|
|
|
|
2018-06-20 00:54:17 +03:00
|
|
|
/// A property of a macro expansion that determines how identifiers
|
|
|
|
/// produced by that expansion are resolved.
|
2019-06-17 23:55:22 +03:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Hash, Debug, RustcEncodable, RustcDecodable)]
|
2018-06-20 00:54:17 +03:00
|
|
|
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,
|
2017-12-12 01:14:45 -08:00
|
|
|
}
|
|
|
|
|
2019-07-16 01:04:05 +03:00
|
|
|
impl ExpnId {
|
2019-08-13 23:56:42 +03:00
|
|
|
pub fn fresh(expn_data: Option<ExpnData>) -> Self {
|
|
|
|
HygieneData::with(|data| data.fresh_expn(expn_data))
|
2016-06-20 10:28:32 +00:00
|
|
|
}
|
2016-09-02 09:12:47 +00:00
|
|
|
|
2019-07-16 01:42:58 +03:00
|
|
|
/// The ID of the theoretical expansion that generates freshly parsed, unexpanded AST.
|
2017-12-07 16:46:31 +01:00
|
|
|
#[inline]
|
2016-09-07 23:21:59 +00:00
|
|
|
pub fn root() -> Self {
|
2019-07-16 01:04:05 +03:00
|
|
|
ExpnId(0)
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 16:46:31 +01:00
|
|
|
#[inline]
|
2016-12-01 22:49:32 +00:00
|
|
|
pub fn as_u32(self) -> u32 {
|
2016-09-02 09:12:47 +00:00
|
|
|
self.0
|
|
|
|
}
|
2017-03-16 10:23:33 +00:00
|
|
|
|
2017-12-07 16:46:31 +01:00
|
|
|
#[inline]
|
2019-07-16 01:04:05 +03:00
|
|
|
pub fn from_u32(raw: u32) -> ExpnId {
|
|
|
|
ExpnId(raw)
|
2017-03-16 10:23:33 +00:00
|
|
|
}
|
2017-03-17 04:04:41 +00:00
|
|
|
|
2017-12-07 16:46:31 +01:00
|
|
|
#[inline]
|
2019-08-13 23:56:42 +03:00
|
|
|
pub fn expn_data(self) -> ExpnData {
|
|
|
|
HygieneData::with(|data| data.expn_data(self).clone())
|
2017-03-17 04:04:41 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 16:46:31 +01:00
|
|
|
#[inline]
|
2019-08-13 23:56:42 +03:00
|
|
|
pub fn set_expn_data(self, expn_data: ExpnData) {
|
2019-07-06 20:25:34 +03:00
|
|
|
HygieneData::with(|data| {
|
2019-08-13 23:56:42 +03:00
|
|
|
let old_expn_data = &mut data.expn_data[self.0 as usize];
|
|
|
|
assert!(old_expn_data.is_none(), "expansion data is reset for an expansion ID");
|
|
|
|
*old_expn_data = Some(expn_data);
|
2019-07-06 20:25:34 +03:00
|
|
|
})
|
2017-03-22 08:39:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 01:04:05 +03:00
|
|
|
pub fn is_descendant_of(self, ancestor: ExpnId) -> bool {
|
2019-05-30 08:59:22 +10:00
|
|
|
HygieneData::with(|data| data.is_descendant_of(self, ancestor))
|
2017-03-17 04:04:41 +00:00
|
|
|
}
|
2018-04-21 18:20:17 -04:00
|
|
|
|
2019-07-16 02:59:53 +03:00
|
|
|
/// `expn_id.outer_expn_is_descendant_of(ctxt)` is equivalent to but faster than
|
|
|
|
/// `expn_id.is_descendant_of(ctxt.outer_expn())`.
|
|
|
|
pub fn outer_expn_is_descendant_of(self, ctxt: SyntaxContext) -> bool {
|
|
|
|
HygieneData::with(|data| data.is_descendant_of(self, data.outer_expn(ctxt)))
|
2019-05-27 13:38:23 +10:00
|
|
|
}
|
2014-02-25 04:47:19 +08:00
|
|
|
}
|
|
|
|
|
2018-06-20 00:08:14 +03:00
|
|
|
#[derive(Debug)]
|
2018-06-30 19:35:00 +03:00
|
|
|
crate struct HygieneData {
|
2019-08-13 23:56:42 +03:00
|
|
|
/// Each expansion should have an associated expansion data, but sometimes there's a delay
|
|
|
|
/// between creation of an expansion ID and obtaining its data (e.g. macros are collected
|
2019-08-13 03:34:46 +03:00
|
|
|
/// first and then resolved later), so we use an `Option` here.
|
2019-08-13 23:56:42 +03:00
|
|
|
expn_data: Vec<Option<ExpnData>>,
|
2019-07-16 01:42:58 +03:00
|
|
|
syntax_context_data: Vec<SyntaxContextData>,
|
|
|
|
syntax_context_map: FxHashMap<(SyntaxContext, ExpnId, Transparency), SyntaxContext>,
|
2014-02-25 04:47:19 +08:00
|
|
|
}
|
|
|
|
|
2016-06-22 08:03:42 +00:00
|
|
|
impl HygieneData {
|
2019-07-07 16:45:41 +03:00
|
|
|
crate fn new(edition: Edition) -> Self {
|
2016-06-22 08:03:42 +00:00
|
|
|
HygieneData {
|
2019-08-13 23:56:42 +03:00
|
|
|
expn_data: vec![Some(ExpnData::default(ExpnKind::Root, DUMMY_SP, edition))],
|
2019-07-16 01:42:58 +03:00
|
|
|
syntax_context_data: vec![SyntaxContextData {
|
|
|
|
outer_expn: ExpnId::root(),
|
2019-07-16 02:59:53 +03:00
|
|
|
outer_transparency: Transparency::Opaque,
|
|
|
|
parent: SyntaxContext(0),
|
2018-06-24 19:54:23 +03:00
|
|
|
opaque: SyntaxContext(0),
|
|
|
|
opaque_and_semitransparent: SyntaxContext(0),
|
2019-05-11 17:41:37 +03:00
|
|
|
dollar_crate_name: kw::DollarCrate,
|
2017-12-12 01:14:45 -08:00
|
|
|
}],
|
2019-07-16 01:42:58 +03:00
|
|
|
syntax_context_map: FxHashMap::default(),
|
2016-06-22 08:03:42 +00:00
|
|
|
}
|
2014-02-25 04:47:19 +08:00
|
|
|
}
|
|
|
|
|
2016-06-22 08:03:42 +00:00
|
|
|
fn with<T, F: FnOnce(&mut HygieneData) -> T>(f: F) -> T {
|
2018-03-07 02:44:10 +01:00
|
|
|
GLOBALS.with(|globals| f(&mut *globals.hygiene_data.borrow_mut()))
|
2016-06-22 08:03:42 +00:00
|
|
|
}
|
2019-05-30 08:59:22 +10:00
|
|
|
|
2019-08-13 23:56:42 +03:00
|
|
|
fn fresh_expn(&mut self, expn_data: Option<ExpnData>) -> ExpnId {
|
|
|
|
self.expn_data.push(expn_data);
|
2019-07-16 01:42:58 +03:00
|
|
|
ExpnId(self.expn_data.len() as u32 - 1)
|
2019-07-06 21:02:45 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 23:56:42 +03:00
|
|
|
fn expn_data(&self, expn_id: ExpnId) -> &ExpnData {
|
2019-08-13 03:34:46 +03:00
|
|
|
self.expn_data[expn_id.0 as usize].as_ref()
|
2019-08-13 23:56:42 +03:00
|
|
|
.expect("no expansion data for an expansion ID")
|
2019-05-30 08:59:22 +10:00
|
|
|
}
|
|
|
|
|
2019-07-16 01:42:58 +03:00
|
|
|
fn is_descendant_of(&self, mut expn_id: ExpnId, ancestor: ExpnId) -> bool {
|
|
|
|
while expn_id != ancestor {
|
|
|
|
if expn_id == ExpnId::root() {
|
2019-05-30 08:59:22 +10:00
|
|
|
return false;
|
|
|
|
}
|
2019-08-13 23:56:42 +03:00
|
|
|
expn_id = self.expn_data(expn_id).parent;
|
2019-05-30 08:59:22 +10:00
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
2019-06-01 07:19:58 +10:00
|
|
|
|
|
|
|
fn modern(&self, ctxt: SyntaxContext) -> SyntaxContext {
|
2019-07-16 01:42:58 +03:00
|
|
|
self.syntax_context_data[ctxt.0 as usize].opaque
|
2019-06-01 07:19:58 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
fn modern_and_legacy(&self, ctxt: SyntaxContext) -> SyntaxContext {
|
2019-07-16 01:42:58 +03:00
|
|
|
self.syntax_context_data[ctxt.0 as usize].opaque_and_semitransparent
|
2019-06-01 07:19:58 +10:00
|
|
|
}
|
|
|
|
|
2019-07-16 02:59:53 +03:00
|
|
|
fn outer_expn(&self, ctxt: SyntaxContext) -> ExpnId {
|
2019-07-16 01:42:58 +03:00
|
|
|
self.syntax_context_data[ctxt.0 as usize].outer_expn
|
2019-06-01 07:19:58 +10:00
|
|
|
}
|
|
|
|
|
2019-08-23 00:27:46 +03:00
|
|
|
fn outer_mark(&self, ctxt: SyntaxContext) -> (ExpnId, Transparency) {
|
|
|
|
let data = &self.syntax_context_data[ctxt.0 as usize];
|
|
|
|
(data.outer_expn, data.outer_transparency)
|
2019-06-01 07:19:58 +10:00
|
|
|
}
|
|
|
|
|
2019-07-16 02:59:53 +03:00
|
|
|
fn parent_ctxt(&self, ctxt: SyntaxContext) -> SyntaxContext {
|
|
|
|
self.syntax_context_data[ctxt.0 as usize].parent
|
2019-06-01 07:19:58 +10:00
|
|
|
}
|
2019-06-01 07:28:15 +10:00
|
|
|
|
2019-08-23 01:31:01 +03:00
|
|
|
fn remove_mark(&self, ctxt: &mut SyntaxContext) -> (ExpnId, Transparency) {
|
|
|
|
let outer_mark = self.outer_mark(*ctxt);
|
2019-07-16 02:59:53 +03:00
|
|
|
*ctxt = self.parent_ctxt(*ctxt);
|
2019-08-23 01:31:01 +03:00
|
|
|
outer_mark
|
2019-06-01 07:28:15 +10:00
|
|
|
}
|
2019-06-01 07:44:14 +10:00
|
|
|
|
2019-07-16 01:04:05 +03:00
|
|
|
fn marks(&self, mut ctxt: SyntaxContext) -> Vec<(ExpnId, Transparency)> {
|
2019-06-02 08:27:36 +10:00
|
|
|
let mut marks = Vec::new();
|
2019-08-11 01:44:55 +03:00
|
|
|
while ctxt != SyntaxContext::root() {
|
2019-08-23 00:27:46 +03:00
|
|
|
marks.push(self.outer_mark(ctxt));
|
2019-07-16 02:59:53 +03:00
|
|
|
ctxt = self.parent_ctxt(ctxt);
|
2019-06-02 08:27:36 +10:00
|
|
|
}
|
|
|
|
marks.reverse();
|
|
|
|
marks
|
|
|
|
}
|
|
|
|
|
2019-06-03 12:07:51 +10:00
|
|
|
fn walk_chain(&self, mut span: Span, to: SyntaxContext) -> Span {
|
2019-08-11 01:08:30 +03:00
|
|
|
while span.from_expansion() && span.ctxt() != to {
|
2019-08-13 23:56:42 +03:00
|
|
|
span = self.expn_data(self.outer_expn(span.ctxt())).call_site;
|
2019-06-03 12:07:51 +10:00
|
|
|
}
|
|
|
|
span
|
|
|
|
}
|
|
|
|
|
2019-07-16 01:42:58 +03:00
|
|
|
fn adjust(&self, ctxt: &mut SyntaxContext, expn_id: ExpnId) -> Option<ExpnId> {
|
2019-06-01 07:44:14 +10:00
|
|
|
let mut scope = None;
|
2019-07-16 02:59:53 +03:00
|
|
|
while !self.is_descendant_of(expn_id, self.outer_expn(*ctxt)) {
|
2019-08-23 01:31:01 +03:00
|
|
|
scope = Some(self.remove_mark(ctxt).0);
|
2019-06-01 07:44:14 +10:00
|
|
|
}
|
|
|
|
scope
|
|
|
|
}
|
2019-06-02 08:16:46 +10:00
|
|
|
|
2019-08-23 01:31:01 +03:00
|
|
|
fn apply_mark(
|
|
|
|
&mut self, ctxt: SyntaxContext, expn_id: ExpnId, transparency: Transparency
|
|
|
|
) -> SyntaxContext {
|
2019-07-16 01:42:58 +03:00
|
|
|
assert_ne!(expn_id, ExpnId::root());
|
2019-06-02 08:23:54 +10:00
|
|
|
if transparency == Transparency::Opaque {
|
2019-07-16 01:42:58 +03:00
|
|
|
return self.apply_mark_internal(ctxt, expn_id, transparency);
|
2019-06-02 08:23:54 +10:00
|
|
|
}
|
|
|
|
|
2019-08-13 23:56:42 +03:00
|
|
|
let call_site_ctxt = self.expn_data(expn_id).call_site.ctxt();
|
2019-06-02 08:23:54 +10:00
|
|
|
let mut call_site_ctxt = if transparency == Transparency::SemiTransparent {
|
|
|
|
self.modern(call_site_ctxt)
|
|
|
|
} else {
|
|
|
|
self.modern_and_legacy(call_site_ctxt)
|
|
|
|
};
|
|
|
|
|
2019-08-11 01:44:55 +03:00
|
|
|
if call_site_ctxt == SyntaxContext::root() {
|
2019-07-16 01:42:58 +03:00
|
|
|
return self.apply_mark_internal(ctxt, expn_id, transparency);
|
2019-06-02 08:23:54 +10:00
|
|
|
}
|
|
|
|
|
2019-07-16 01:42:58 +03:00
|
|
|
// Otherwise, `expn_id` is a macros 1.0 definition and the call site is in a
|
2019-06-02 08:23:54 +10:00
|
|
|
// 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.
|
|
|
|
//
|
2019-07-27 02:26:27 +03:00
|
|
|
// See the example at `test/ui/hygiene/legacy_interaction.rs`.
|
2019-07-16 01:42:58 +03:00
|
|
|
for (expn_id, transparency) in self.marks(ctxt) {
|
|
|
|
call_site_ctxt = self.apply_mark_internal(call_site_ctxt, expn_id, transparency);
|
2019-06-02 08:23:54 +10:00
|
|
|
}
|
2019-07-16 01:42:58 +03:00
|
|
|
self.apply_mark_internal(call_site_ctxt, expn_id, transparency)
|
2019-06-02 08:23:54 +10:00
|
|
|
}
|
|
|
|
|
2019-07-16 01:42:58 +03:00
|
|
|
fn apply_mark_internal(
|
|
|
|
&mut self, ctxt: SyntaxContext, expn_id: ExpnId, transparency: Transparency
|
|
|
|
) -> SyntaxContext {
|
|
|
|
let syntax_context_data = &mut self.syntax_context_data;
|
|
|
|
let mut opaque = syntax_context_data[ctxt.0 as usize].opaque;
|
2019-06-02 08:16:46 +10:00
|
|
|
let mut opaque_and_semitransparent =
|
2019-07-16 01:42:58 +03:00
|
|
|
syntax_context_data[ctxt.0 as usize].opaque_and_semitransparent;
|
2019-06-02 08:16:46 +10:00
|
|
|
|
|
|
|
if transparency >= Transparency::Opaque {
|
2019-07-16 02:59:53 +03:00
|
|
|
let parent = opaque;
|
|
|
|
opaque = *self.syntax_context_map.entry((parent, expn_id, transparency))
|
2019-07-16 01:42:58 +03:00
|
|
|
.or_insert_with(|| {
|
|
|
|
let new_opaque = SyntaxContext(syntax_context_data.len() as u32);
|
|
|
|
syntax_context_data.push(SyntaxContextData {
|
|
|
|
outer_expn: expn_id,
|
2019-07-16 02:59:53 +03:00
|
|
|
outer_transparency: transparency,
|
|
|
|
parent,
|
2019-06-02 08:16:46 +10:00
|
|
|
opaque: new_opaque,
|
|
|
|
opaque_and_semitransparent: new_opaque,
|
|
|
|
dollar_crate_name: kw::DollarCrate,
|
|
|
|
});
|
|
|
|
new_opaque
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if transparency >= Transparency::SemiTransparent {
|
2019-07-16 02:59:53 +03:00
|
|
|
let parent = opaque_and_semitransparent;
|
2019-06-02 08:16:46 +10:00
|
|
|
opaque_and_semitransparent =
|
2019-07-16 02:59:53 +03:00
|
|
|
*self.syntax_context_map.entry((parent, expn_id, transparency))
|
2019-07-16 01:42:58 +03:00
|
|
|
.or_insert_with(|| {
|
2019-06-02 08:16:46 +10:00
|
|
|
let new_opaque_and_semitransparent =
|
2019-07-16 01:42:58 +03:00
|
|
|
SyntaxContext(syntax_context_data.len() as u32);
|
|
|
|
syntax_context_data.push(SyntaxContextData {
|
|
|
|
outer_expn: expn_id,
|
2019-07-16 02:59:53 +03:00
|
|
|
outer_transparency: transparency,
|
|
|
|
parent,
|
2019-06-02 08:16:46 +10:00
|
|
|
opaque,
|
|
|
|
opaque_and_semitransparent: new_opaque_and_semitransparent,
|
|
|
|
dollar_crate_name: kw::DollarCrate,
|
|
|
|
});
|
|
|
|
new_opaque_and_semitransparent
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-16 02:59:53 +03:00
|
|
|
let parent = ctxt;
|
|
|
|
*self.syntax_context_map.entry((parent, expn_id, transparency)).or_insert_with(|| {
|
2019-06-02 08:16:46 +10:00
|
|
|
let new_opaque_and_semitransparent_and_transparent =
|
2019-07-16 01:42:58 +03:00
|
|
|
SyntaxContext(syntax_context_data.len() as u32);
|
|
|
|
syntax_context_data.push(SyntaxContextData {
|
|
|
|
outer_expn: expn_id,
|
2019-07-16 02:59:53 +03:00
|
|
|
outer_transparency: transparency,
|
|
|
|
parent,
|
2019-06-02 08:16:46 +10:00
|
|
|
opaque,
|
|
|
|
opaque_and_semitransparent,
|
|
|
|
dollar_crate_name: kw::DollarCrate,
|
|
|
|
});
|
|
|
|
new_opaque_and_semitransparent_and_transparent
|
|
|
|
})
|
|
|
|
}
|
2014-03-09 00:18:58 +02:00
|
|
|
}
|
2014-02-25 04:47:19 +08:00
|
|
|
|
2019-07-16 02:59:53 +03:00
|
|
|
pub fn clear_syntax_context_map() {
|
2019-07-16 01:42:58 +03:00
|
|
|
HygieneData::with(|data| data.syntax_context_map = FxHashMap::default());
|
2014-11-28 21:56:09 -07:00
|
|
|
}
|
|
|
|
|
2019-06-03 12:07:51 +10:00
|
|
|
pub fn walk_chain(span: Span, to: SyntaxContext) -> Span {
|
|
|
|
HygieneData::with(|data| data.walk_chain(span, to))
|
|
|
|
}
|
|
|
|
|
2019-07-05 03:09:24 +03:00
|
|
|
pub fn update_dollar_crate_names(mut get_name: impl FnMut(SyntaxContext) -> Symbol) {
|
|
|
|
// The new contexts that need updating are at the end of the list and have `$crate` as a name.
|
|
|
|
let (len, to_update) = HygieneData::with(|data| (
|
2019-07-16 01:42:58 +03:00
|
|
|
data.syntax_context_data.len(),
|
|
|
|
data.syntax_context_data.iter().rev()
|
2019-07-05 03:09:24 +03:00
|
|
|
.take_while(|scdata| scdata.dollar_crate_name == kw::DollarCrate).count()
|
|
|
|
));
|
|
|
|
// The callback must be called from outside of the `HygieneData` lock,
|
|
|
|
// since it will try to acquire it too.
|
|
|
|
let range_to_update = len - to_update .. len;
|
|
|
|
let names: Vec<_> =
|
|
|
|
range_to_update.clone().map(|idx| get_name(SyntaxContext::from_u32(idx as u32))).collect();
|
|
|
|
HygieneData::with(|data| range_to_update.zip(names.into_iter()).for_each(|(idx, name)| {
|
2019-07-16 01:42:58 +03:00
|
|
|
data.syntax_context_data[idx].dollar_crate_name = name;
|
2019-07-05 03:09:24 +03:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2016-06-22 08:03:42 +00:00
|
|
|
impl SyntaxContext {
|
Increase `Span` from 4 bytes to 8 bytes.
This increases the size of some important types, such as `ast::Expr` and
`mir::Statement`. However, it drastically reduces how much the interner
is used, and the fields are more natural sizes that don't require bit
operations to extract.
As a result, instruction counts drop across a range of workloads, by as
much as 12% for incremental "check" builds of `script-servo`.
Peak memory usage goes up a little for some cases, but down by more for
some other cases -- as much as 18% for non-incremental builds of
`packed-simd`.
The commit also:
- removes the `repr(packed)`, because it has negligible effect, but can
cause undefined behaviour;
- replaces explicit impls of common traits (`Copy`, `PartialEq`, etc.)
with derived ones.
2019-04-04 15:46:33 +11:00
|
|
|
#[inline]
|
2019-08-11 01:44:55 +03:00
|
|
|
pub const fn root() -> Self {
|
2016-06-22 08:03:42 +00:00
|
|
|
SyntaxContext(0)
|
|
|
|
}
|
|
|
|
|
Increase `Span` from 4 bytes to 8 bytes.
This increases the size of some important types, such as `ast::Expr` and
`mir::Statement`. However, it drastically reduces how much the interner
is used, and the fields are more natural sizes that don't require bit
operations to extract.
As a result, instruction counts drop across a range of workloads, by as
much as 12% for incremental "check" builds of `script-servo`.
Peak memory usage goes up a little for some cases, but down by more for
some other cases -- as much as 18% for non-incremental builds of
`packed-simd`.
The commit also:
- removes the `repr(packed)`, because it has negligible effect, but can
cause undefined behaviour;
- replaces explicit impls of common traits (`Copy`, `PartialEq`, etc.)
with derived ones.
2019-04-04 15:46:33 +11:00
|
|
|
#[inline]
|
2018-06-30 19:35:00 +03:00
|
|
|
crate fn as_u32(self) -> u32 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
|
Increase `Span` from 4 bytes to 8 bytes.
This increases the size of some important types, such as `ast::Expr` and
`mir::Statement`. However, it drastically reduces how much the interner
is used, and the fields are more natural sizes that don't require bit
operations to extract.
As a result, instruction counts drop across a range of workloads, by as
much as 12% for incremental "check" builds of `script-servo`.
Peak memory usage goes up a little for some cases, but down by more for
some other cases -- as much as 18% for non-incremental builds of
`packed-simd`.
The commit also:
- removes the `repr(packed)`, because it has negligible effect, but can
cause undefined behaviour;
- replaces explicit impls of common traits (`Copy`, `PartialEq`, etc.)
with derived ones.
2019-04-04 15:46:33 +11:00
|
|
|
#[inline]
|
2018-06-30 19:35:00 +03:00
|
|
|
crate fn from_u32(raw: u32) -> SyntaxContext {
|
|
|
|
SyntaxContext(raw)
|
|
|
|
}
|
|
|
|
|
2019-07-16 01:42:58 +03:00
|
|
|
/// Extend a syntax context with a given expansion and transparency.
|
2019-08-23 01:31:01 +03:00
|
|
|
pub fn apply_mark(self, expn_id: ExpnId, transparency: Transparency) -> SyntaxContext {
|
|
|
|
HygieneData::with(|data| data.apply_mark(self, expn_id, transparency))
|
2016-06-22 08:03:42 +00:00
|
|
|
}
|
2017-03-17 04:04:41 +00:00
|
|
|
|
2018-04-21 18:00:09 -04:00
|
|
|
/// Pulls a single mark off of the syntax context. This effectively moves the
|
|
|
|
/// context up one macro definition level. That is, if we have a nested macro
|
|
|
|
/// definition as follows:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// macro_rules! f {
|
|
|
|
/// macro_rules! g {
|
|
|
|
/// ...
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// and we have a SyntaxContext that is referring to something declared by an invocation
|
|
|
|
/// of g (call it g1), calling remove_mark will result in the SyntaxContext for the
|
|
|
|
/// invocation of f that created g1.
|
|
|
|
/// Returns the mark that was removed.
|
2019-07-16 01:04:05 +03:00
|
|
|
pub fn remove_mark(&mut self) -> ExpnId {
|
2019-08-23 01:31:01 +03:00
|
|
|
HygieneData::with(|data| data.remove_mark(self).0)
|
2017-03-22 08:39:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 01:04:05 +03:00
|
|
|
pub fn marks(self) -> Vec<(ExpnId, Transparency)> {
|
2019-06-02 08:27:36 +10:00
|
|
|
HygieneData::with(|data| data.marks(self))
|
2017-11-29 01:05:31 -08:00
|
|
|
}
|
|
|
|
|
2017-03-22 08:39:51 +00:00
|
|
|
/// Adjust this context for resolution in a scope created by the given expansion.
|
|
|
|
/// For example, consider the following three resolutions of `f`:
|
2017-12-31 17:17:01 +01:00
|
|
|
///
|
2017-03-22 08:39:51 +00:00
|
|
|
/// ```rust
|
|
|
|
/// mod foo { pub fn f() {} } // `f`'s `SyntaxContext` is empty.
|
|
|
|
/// m!(f);
|
|
|
|
/// macro m($f:ident) {
|
|
|
|
/// mod bar {
|
2019-07-16 01:04:05 +03:00
|
|
|
/// pub fn f() {} // `f`'s `SyntaxContext` has a single `ExpnId` from `m`.
|
2017-03-22 08:39:51 +00:00
|
|
|
/// pub fn $f() {} // `$f`'s `SyntaxContext` is empty.
|
|
|
|
/// }
|
2019-07-16 01:04:05 +03:00
|
|
|
/// foo::f(); // `f`'s `SyntaxContext` has a single `ExpnId` from `m`
|
2017-03-22 08:39:51 +00:00
|
|
|
/// //^ Since `mod foo` is outside this expansion, `adjust` removes the mark from `f`,
|
|
|
|
/// //| and it resolves to `::foo::f`.
|
2019-07-16 01:04:05 +03:00
|
|
|
/// bar::f(); // `f`'s `SyntaxContext` has a single `ExpnId` from `m`
|
2017-03-22 08:39:51 +00:00
|
|
|
/// //^ Since `mod bar` not outside this expansion, `adjust` does not change `f`,
|
|
|
|
/// //| and it resolves to `::bar::f`.
|
|
|
|
/// bar::$f(); // `f`'s `SyntaxContext` is empty.
|
|
|
|
/// //^ Since `mod bar` is not outside this expansion, `adjust` does not change `$f`,
|
|
|
|
/// //| and it resolves to `::bar::$f`.
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// This returns the expansion whose definition scope we use to privacy check the resolution,
|
2018-11-27 02:59:49 +00:00
|
|
|
/// or `None` if we privacy check as usual (i.e., not w.r.t. a macro definition scope).
|
2019-07-16 01:42:58 +03:00
|
|
|
pub fn adjust(&mut self, expn_id: ExpnId) -> Option<ExpnId> {
|
|
|
|
HygieneData::with(|data| data.adjust(self, expn_id))
|
2017-03-22 08:39:51 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 16:10:03 +10:00
|
|
|
/// Like `SyntaxContext::adjust`, but also modernizes `self`.
|
2019-07-16 01:42:58 +03:00
|
|
|
pub fn modernize_and_adjust(&mut self, expn_id: ExpnId) -> Option<ExpnId> {
|
2019-06-03 16:10:03 +10:00
|
|
|
HygieneData::with(|data| {
|
|
|
|
*self = data.modern(*self);
|
2019-07-16 01:42:58 +03:00
|
|
|
data.adjust(self, expn_id)
|
2019-06-03 16:10:03 +10:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-22 08:39:51 +00:00
|
|
|
/// Adjust this context for resolution in a scope created by the given expansion
|
|
|
|
/// via a glob import with the given `SyntaxContext`.
|
2017-12-31 17:17:01 +01:00
|
|
|
/// For example:
|
|
|
|
///
|
2017-03-22 08:39:51 +00:00
|
|
|
/// ```rust
|
|
|
|
/// m!(f);
|
|
|
|
/// macro m($i:ident) {
|
|
|
|
/// mod foo {
|
2019-07-16 01:04:05 +03:00
|
|
|
/// pub fn f() {} // `f`'s `SyntaxContext` has a single `ExpnId` from `m`.
|
2017-03-22 08:39:51 +00:00
|
|
|
/// pub fn $i() {} // `$i`'s `SyntaxContext` is empty.
|
|
|
|
/// }
|
|
|
|
/// n(f);
|
|
|
|
/// macro n($j:ident) {
|
|
|
|
/// use foo::*;
|
|
|
|
/// f(); // `f`'s `SyntaxContext` has a mark from `m` and a mark from `n`
|
|
|
|
/// //^ `glob_adjust` removes the mark from `n`, so this resolves to `foo::f`.
|
|
|
|
/// $i(); // `$i`'s `SyntaxContext` has a mark from `n`
|
|
|
|
/// //^ `glob_adjust` removes the mark from `n`, so this resolves to `foo::$i`.
|
|
|
|
/// $j(); // `$j`'s `SyntaxContext` has a mark from `m`
|
|
|
|
/// //^ This cannot be glob-adjusted, so this is a resolution error.
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// This returns `None` if the context cannot be glob-adjusted.
|
|
|
|
/// Otherwise, it returns the scope to use when privacy checking (see `adjust` for details).
|
2019-07-16 01:42:58 +03:00
|
|
|
pub fn glob_adjust(&mut self, expn_id: ExpnId, glob_span: Span) -> Option<Option<ExpnId>> {
|
2019-06-01 08:35:01 +10:00
|
|
|
HygieneData::with(|data| {
|
|
|
|
let mut scope = None;
|
|
|
|
let mut glob_ctxt = data.modern(glob_span.ctxt());
|
2019-07-16 02:59:53 +03:00
|
|
|
while !data.is_descendant_of(expn_id, data.outer_expn(glob_ctxt)) {
|
2019-08-23 01:31:01 +03:00
|
|
|
scope = Some(data.remove_mark(&mut glob_ctxt).0);
|
|
|
|
if data.remove_mark(self).0 != scope.unwrap() {
|
2019-06-01 08:35:01 +10:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
2019-07-16 01:42:58 +03:00
|
|
|
if data.adjust(self, expn_id).is_some() {
|
2017-03-22 08:39:51 +00:00
|
|
|
return None;
|
|
|
|
}
|
2019-06-01 08:35:01 +10:00
|
|
|
Some(scope)
|
|
|
|
})
|
2017-03-22 08:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Undo `glob_adjust` if possible:
|
2017-12-31 17:17:01 +01:00
|
|
|
///
|
2017-03-22 08:39:51 +00:00
|
|
|
/// ```rust
|
|
|
|
/// if let Some(privacy_checking_scope) = self.reverse_glob_adjust(expansion, glob_ctxt) {
|
|
|
|
/// assert!(self.glob_adjust(expansion, glob_ctxt) == Some(privacy_checking_scope));
|
|
|
|
/// }
|
|
|
|
/// ```
|
2019-07-16 01:42:58 +03:00
|
|
|
pub fn reverse_glob_adjust(&mut self, expn_id: ExpnId, glob_span: Span)
|
2019-07-16 01:04:05 +03:00
|
|
|
-> Option<Option<ExpnId>> {
|
2019-06-01 08:35:01 +10:00
|
|
|
HygieneData::with(|data| {
|
2019-07-16 01:42:58 +03:00
|
|
|
if data.adjust(self, expn_id).is_some() {
|
2019-06-01 08:35:01 +10:00
|
|
|
return None;
|
|
|
|
}
|
2017-03-22 08:39:51 +00:00
|
|
|
|
2019-06-01 08:35:01 +10:00
|
|
|
let mut glob_ctxt = data.modern(glob_span.ctxt());
|
|
|
|
let mut marks = Vec::new();
|
2019-07-16 02:59:53 +03:00
|
|
|
while !data.is_descendant_of(expn_id, data.outer_expn(glob_ctxt)) {
|
2019-06-01 08:35:01 +10:00
|
|
|
marks.push(data.remove_mark(&mut glob_ctxt));
|
|
|
|
}
|
2017-03-22 08:39:51 +00:00
|
|
|
|
2019-08-23 01:31:01 +03:00
|
|
|
let scope = marks.last().map(|mark| mark.0);
|
|
|
|
while let Some((expn_id, transparency)) = marks.pop() {
|
|
|
|
*self = data.apply_mark(*self, expn_id, transparency);
|
2019-06-01 08:35:01 +10:00
|
|
|
}
|
|
|
|
Some(scope)
|
|
|
|
})
|
2017-03-22 08:39:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 01:42:58 +03:00
|
|
|
pub fn hygienic_eq(self, other: SyntaxContext, expn_id: ExpnId) -> bool {
|
2019-06-03 09:43:20 +10:00
|
|
|
HygieneData::with(|data| {
|
|
|
|
let mut self_modern = data.modern(self);
|
2019-07-16 01:42:58 +03:00
|
|
|
data.adjust(&mut self_modern, expn_id);
|
2019-06-03 09:43:20 +10:00
|
|
|
self_modern == data.modern(other)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-07 16:46:31 +01:00
|
|
|
#[inline]
|
2017-03-22 08:39:51 +00:00
|
|
|
pub fn modern(self) -> SyntaxContext {
|
2019-06-01 07:19:58 +10:00
|
|
|
HygieneData::with(|data| data.modern(self))
|
2018-06-24 19:54:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn modern_and_legacy(self) -> SyntaxContext {
|
2019-06-01 07:19:58 +10:00
|
|
|
HygieneData::with(|data| data.modern_and_legacy(self))
|
2017-03-22 08:39:51 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 16:46:31 +01:00
|
|
|
#[inline]
|
2019-07-16 02:59:53 +03:00
|
|
|
pub fn outer_expn(self) -> ExpnId {
|
|
|
|
HygieneData::with(|data| data.outer_expn(self))
|
2017-03-17 04:04:41 +00:00
|
|
|
}
|
2018-12-09 17:46:12 +03:00
|
|
|
|
2019-08-13 23:56:42 +03:00
|
|
|
/// `ctxt.outer_expn_data()` is equivalent to but faster than
|
|
|
|
/// `ctxt.outer_expn().expn_data()`.
|
2019-05-27 13:52:11 +10:00
|
|
|
#[inline]
|
2019-08-13 23:56:42 +03:00
|
|
|
pub fn outer_expn_data(self) -> ExpnData {
|
|
|
|
HygieneData::with(|data| data.expn_data(data.outer_expn(self)).clone())
|
2019-05-27 13:52:11 +10:00
|
|
|
}
|
|
|
|
|
2019-06-03 09:21:27 +10:00
|
|
|
#[inline]
|
2019-08-23 00:27:46 +03:00
|
|
|
pub fn outer_mark_with_data(self) -> (ExpnId, Transparency, ExpnData) {
|
2019-06-03 09:21:27 +10:00
|
|
|
HygieneData::with(|data| {
|
2019-08-23 00:27:46 +03:00
|
|
|
let (expn_id, transparency) = data.outer_mark(self);
|
|
|
|
(expn_id, transparency, data.expn_data(expn_id).clone())
|
2019-06-03 09:21:27 +10:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-09 17:46:12 +03:00
|
|
|
pub fn dollar_crate_name(self) -> Symbol {
|
2019-07-16 01:42:58 +03:00
|
|
|
HygieneData::with(|data| data.syntax_context_data[self.0 as usize].dollar_crate_name)
|
2018-12-09 17:46:12 +03:00
|
|
|
}
|
2014-02-25 04:47:19 +08:00
|
|
|
}
|
|
|
|
|
2016-06-22 08:03:42 +00:00
|
|
|
impl fmt::Debug for SyntaxContext {
|
2019-02-04 03:42:27 +09:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-06-22 08:03:42 +00:00
|
|
|
write!(f, "#{}", self.0)
|
|
|
|
}
|
2016-06-26 03:32:45 +00:00
|
|
|
}
|
2017-03-17 04:04:41 +00:00
|
|
|
|
2019-07-06 21:02:45 +03:00
|
|
|
impl Span {
|
|
|
|
/// Creates a fresh expansion with given properties.
|
|
|
|
/// Expansions are normally created by macros, but in some cases expansions are created for
|
|
|
|
/// other compiler-generated code to set per-span properties like allowed unstable features.
|
|
|
|
/// The returned span belongs to the created expansion and has the new properties,
|
|
|
|
/// but its location is inherited from the current span.
|
2019-08-13 23:56:42 +03:00
|
|
|
pub fn fresh_expansion(self, expn_data: ExpnData) -> Span {
|
2019-08-25 20:58:03 +01:00
|
|
|
self.fresh_expansion_with_transparency(expn_data, Transparency::Transparent)
|
2019-08-23 00:27:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fresh_expansion_with_transparency(
|
|
|
|
self, expn_data: ExpnData, transparency: Transparency
|
|
|
|
) -> Span {
|
2019-07-06 21:02:45 +03:00
|
|
|
HygieneData::with(|data| {
|
2019-08-13 23:56:42 +03:00
|
|
|
let expn_id = data.fresh_expn(Some(expn_data));
|
2019-08-23 01:31:01 +03:00
|
|
|
self.with_ctxt(data.apply_mark(SyntaxContext::root(), expn_id, transparency))
|
2019-07-06 21:02:45 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 15:58:56 +03:00
|
|
|
/// A subset of properties from both macro definition and macro call available through global data.
|
|
|
|
/// Avoid using this if you have access to the original definition or call structures.
|
2019-06-30 14:57:34 +03:00
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
2019-08-13 23:56:42 +03:00
|
|
|
pub struct ExpnData {
|
2019-06-17 23:55:22 +03:00
|
|
|
// --- The part unique to each expansion.
|
2019-08-13 03:34:46 +03:00
|
|
|
/// The kind of this expansion - macro or compiler desugaring.
|
|
|
|
pub kind: ExpnKind,
|
|
|
|
/// The expansion that produced this expansion.
|
|
|
|
pub parent: ExpnId,
|
2017-03-17 04:04:41 +00:00
|
|
|
/// The location of the actual macro invocation or syntax sugar , e.g.
|
|
|
|
/// `let x = foo!();` or `if let Some(y) = x {}`
|
|
|
|
///
|
2018-11-27 02:59:49 +00:00
|
|
|
/// This may recursively refer to other macro invocations, e.g., if
|
2017-03-17 04:04:41 +00:00
|
|
|
/// `foo!()` invoked `bar!()` internally, and there was an
|
|
|
|
/// expression inside `bar!`; the call_site of the expression in
|
|
|
|
/// the expansion would point to the `bar!` invocation; that
|
2019-08-13 23:56:42 +03:00
|
|
|
/// call_site span would have its own ExpnData, with the call_site
|
2017-03-17 04:04:41 +00:00
|
|
|
/// pointing to the `foo!` invocation.
|
|
|
|
pub call_site: Span,
|
2019-06-17 23:55:22 +03:00
|
|
|
|
|
|
|
// --- The part specific to the macro/desugaring definition.
|
2019-08-13 23:39:48 +03:00
|
|
|
// --- It may be reasonable to share this part between expansions with the same definition,
|
|
|
|
// --- but such sharing is known to bring some minor inconveniences without also bringing
|
|
|
|
// --- noticeable perf improvements (PR #62898).
|
2019-06-30 03:05:52 +03:00
|
|
|
/// The span of the macro definition (possibly dummy).
|
2018-06-23 21:41:39 +03:00
|
|
|
/// This span serves only informational purpose and is not used for resolution.
|
2019-06-30 03:05:52 +03:00
|
|
|
pub def_site: Span,
|
2019-02-03 12:55:00 +01:00
|
|
|
/// List of #[unstable]/feature-gated features that the macro is allowed to use
|
|
|
|
/// internally without forcing the whole crate to opt-in
|
2017-03-17 04:04:41 +00:00
|
|
|
/// to them.
|
2019-02-08 10:21:21 +01:00
|
|
|
pub allow_internal_unstable: Option<Lrc<[Symbol]>>,
|
2017-08-08 18:21:20 +03:00
|
|
|
/// Whether the macro is allowed to use `unsafe` internally
|
|
|
|
/// even if the user crate has `#![forbid(unsafe_code)]`.
|
|
|
|
pub allow_internal_unsafe: bool,
|
2018-06-11 14:21:36 +03:00
|
|
|
/// Enables the macro helper hack (`ident!(...)` -> `$crate::ident!(...)`)
|
|
|
|
/// for a given macro.
|
|
|
|
pub local_inner_macros: bool,
|
2018-04-28 02:08:16 +03:00
|
|
|
/// Edition of the crate in which the macro is defined.
|
|
|
|
pub edition: Edition,
|
2017-03-17 04:04:41 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 23:56:42 +03:00
|
|
|
impl ExpnData {
|
|
|
|
/// Constructs expansion data with default properties.
|
|
|
|
pub fn default(kind: ExpnKind, call_site: Span, edition: Edition) -> ExpnData {
|
|
|
|
ExpnData {
|
2019-06-19 01:08:45 +03:00
|
|
|
kind,
|
2019-08-13 03:34:46 +03:00
|
|
|
parent: ExpnId::root(),
|
|
|
|
call_site,
|
2019-06-30 03:05:52 +03:00
|
|
|
def_site: DUMMY_SP,
|
2019-06-17 22:18:56 +03:00
|
|
|
allow_internal_unstable: None,
|
|
|
|
allow_internal_unsafe: false,
|
|
|
|
local_inner_macros: false,
|
|
|
|
edition,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-06 21:02:45 +03:00
|
|
|
pub fn allow_unstable(kind: ExpnKind, call_site: Span, edition: Edition,
|
2019-08-13 23:56:42 +03:00
|
|
|
allow_internal_unstable: Lrc<[Symbol]>) -> ExpnData {
|
|
|
|
ExpnData {
|
2019-07-06 21:02:45 +03:00
|
|
|
allow_internal_unstable: Some(allow_internal_unstable),
|
2019-08-13 23:56:42 +03:00
|
|
|
..ExpnData::default(kind, call_site, edition)
|
2019-06-17 22:18:56 +03:00
|
|
|
}
|
|
|
|
}
|
2019-08-11 03:00:05 +03:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn is_root(&self) -> bool {
|
|
|
|
if let ExpnKind::Root = self.kind { true } else { false }
|
|
|
|
}
|
2019-06-17 22:18:56 +03:00
|
|
|
}
|
|
|
|
|
2019-06-30 15:58:56 +03:00
|
|
|
/// Expansion kind.
|
2019-06-30 14:57:34 +03:00
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
2019-06-19 01:08:45 +03:00
|
|
|
pub enum ExpnKind {
|
2019-07-16 01:04:05 +03:00
|
|
|
/// No expansion, aka root expansion. Only `ExpnId::root()` has this kind.
|
2019-07-07 16:45:41 +03:00
|
|
|
Root,
|
2019-06-30 15:58:56 +03:00
|
|
|
/// Expansion produced by a macro.
|
|
|
|
Macro(MacroKind, Symbol),
|
2019-08-25 19:59:51 +01:00
|
|
|
/// Transform done by the compiler on the AST.
|
|
|
|
AstPass(AstPass),
|
2017-03-17 04:04:41 +00:00
|
|
|
/// Desugaring done by the compiler during HIR lowering.
|
2019-06-19 01:08:45 +03:00
|
|
|
Desugaring(DesugaringKind)
|
2017-08-12 19:43:43 -05:00
|
|
|
}
|
|
|
|
|
2019-06-19 01:08:45 +03:00
|
|
|
impl ExpnKind {
|
|
|
|
pub fn descr(&self) -> Symbol {
|
2018-06-23 21:41:39 +03:00
|
|
|
match *self {
|
2019-07-07 16:45:41 +03:00
|
|
|
ExpnKind::Root => kw::PathRoot,
|
2019-06-30 15:58:56 +03:00
|
|
|
ExpnKind::Macro(_, descr) => descr,
|
2019-08-25 19:59:51 +01:00
|
|
|
ExpnKind::AstPass(kind) => Symbol::intern(kind.descr()),
|
2019-06-30 15:58:56 +03:00
|
|
|
ExpnKind::Desugaring(kind) => Symbol::intern(kind.descr()),
|
2018-06-23 21:41:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 01:00:49 +03:00
|
|
|
/// The kind of macro invocation or definition.
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
|
|
|
pub enum MacroKind {
|
|
|
|
/// A bang macro `foo!()`.
|
|
|
|
Bang,
|
|
|
|
/// An attribute macro `#[foo]`.
|
|
|
|
Attr,
|
|
|
|
/// A derive macro `#[derive(Foo)]`
|
|
|
|
Derive,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MacroKind {
|
|
|
|
pub fn descr(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
MacroKind::Bang => "macro",
|
|
|
|
MacroKind::Attr => "attribute macro",
|
|
|
|
MacroKind::Derive => "derive macro",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn article(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
MacroKind::Attr => "an",
|
|
|
|
_ => "a",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-25 19:59:51 +01:00
|
|
|
/// The kind of AST transform.
|
|
|
|
#[derive(Clone, Copy, PartialEq, Debug, RustcEncodable, RustcDecodable)]
|
|
|
|
pub enum AstPass {
|
|
|
|
StdImports,
|
|
|
|
TestHarness,
|
|
|
|
ProcMacroHarness,
|
|
|
|
PluginMacroDefs,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AstPass {
|
|
|
|
fn descr(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
AstPass::StdImports => "standard library imports",
|
|
|
|
AstPass::TestHarness => "test harness",
|
|
|
|
AstPass::ProcMacroHarness => "proc macro harness",
|
|
|
|
AstPass::PluginMacroDefs => "plugin macro definitions",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-12 19:43:43 -05:00
|
|
|
/// The kind of compiler desugaring.
|
2019-06-30 14:57:34 +03:00
|
|
|
#[derive(Clone, Copy, PartialEq, Debug, RustcEncodable, RustcDecodable)]
|
2019-06-19 01:08:45 +03:00
|
|
|
pub enum DesugaringKind {
|
2019-03-11 16:43:27 +01:00
|
|
|
/// We desugar `if c { i } else { e }` to `match $ExprKind::Use(c) { true => i, _ => e }`.
|
|
|
|
/// However, we do not want to blame `c` for unreachability but rather say that `i`
|
|
|
|
/// is unreachable. This desugaring kind allows us to avoid blaming `c`.
|
2019-06-20 10:29:42 +02:00
|
|
|
/// This also applies to `while` loops.
|
|
|
|
CondTemporary,
|
2017-08-12 19:43:43 -05:00
|
|
|
QuestionMark,
|
2018-07-21 18:47:02 -07:00
|
|
|
TryBlock,
|
2018-05-22 14:31:56 +02:00
|
|
|
/// Desugaring of an `impl Trait` in return type position
|
2019-08-01 00:41:54 +01:00
|
|
|
/// to an `type Foo = impl Trait;` and replacing the
|
2018-05-22 14:31:56 +02:00
|
|
|
/// `impl Trait` with `Foo`.
|
2019-08-01 00:41:54 +01:00
|
|
|
OpaqueTy,
|
2018-06-06 15:50:59 -07:00
|
|
|
Async,
|
2019-04-18 12:55:23 -07:00
|
|
|
Await,
|
2018-07-15 20:52:41 -07:00
|
|
|
ForLoop,
|
2017-08-12 19:43:43 -05:00
|
|
|
}
|
|
|
|
|
2019-06-19 01:08:45 +03:00
|
|
|
impl DesugaringKind {
|
2019-07-07 13:02:05 +03:00
|
|
|
/// The description wording should combine well with "desugaring of {}".
|
|
|
|
fn descr(self) -> &'static str {
|
2019-06-30 15:58:56 +03:00
|
|
|
match self {
|
2019-07-07 13:02:05 +03:00
|
|
|
DesugaringKind::CondTemporary => "`if` or `while` condition",
|
|
|
|
DesugaringKind::Async => "`async` block or function",
|
|
|
|
DesugaringKind::Await => "`await` expression",
|
|
|
|
DesugaringKind::QuestionMark => "operator `?`",
|
|
|
|
DesugaringKind::TryBlock => "`try` block",
|
2019-08-01 00:41:54 +01:00
|
|
|
DesugaringKind::OpaqueTy => "`impl Trait`",
|
2019-07-07 13:02:05 +03:00
|
|
|
DesugaringKind::ForLoop => "`for` loop",
|
2019-06-30 15:58:56 +03:00
|
|
|
}
|
2017-08-12 19:43:43 -05:00
|
|
|
}
|
2017-03-17 04:04:41 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 03:34:46 +03:00
|
|
|
impl Encodable for ExpnId {
|
|
|
|
fn encode<E: Encoder>(&self, _: &mut E) -> Result<(), E::Error> {
|
|
|
|
Ok(()) // FIXME(jseyfried) intercrate hygiene
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decodable for ExpnId {
|
|
|
|
fn decode<D: Decoder>(_: &mut D) -> Result<Self, D::Error> {
|
|
|
|
Ok(ExpnId::root()) // FIXME(jseyfried) intercrate hygiene
|
|
|
|
}
|
|
|
|
}
|