2019-11-24 14:34:36 +00:00
|
|
|
//! Lowers syntax tree of a rust file into a raw representation of containing
|
|
|
|
//! items, *without* attaching them to a module structure.
|
|
|
|
//!
|
|
|
|
//! That is, raw items don't have semantics, just as syntax, but, unlike syntax,
|
|
|
|
//! they don't change with trivial source code edits, making them a great tool
|
|
|
|
//! for building salsa recomputation firewalls.
|
2019-09-30 08:58:53 +00:00
|
|
|
|
2019-07-04 20:05:17 +00:00
|
|
|
use std::{ops::Index, sync::Arc};
|
2019-03-02 20:59:04 +00:00
|
|
|
|
2019-10-30 15:56:20 +00:00
|
|
|
use hir_expand::{
|
|
|
|
ast_id_map::AstIdMap,
|
2019-10-30 16:10:53 +00:00
|
|
|
hygiene::Hygiene,
|
2019-10-30 15:56:20 +00:00
|
|
|
name::{AsName, Name},
|
|
|
|
};
|
2020-03-19 15:00:11 +00:00
|
|
|
use ra_arena::{Arena, Idx};
|
2019-12-21 18:40:20 +00:00
|
|
|
use ra_prof::profile;
|
2019-03-02 20:59:04 +00:00
|
|
|
use ra_syntax::{
|
2019-12-24 22:45:14 +00:00
|
|
|
ast::{self, AttrsOwner, NameOwner, VisibilityOwner},
|
2019-12-21 16:26:05 +00:00
|
|
|
AstNode,
|
2019-03-02 20:59:04 +00:00
|
|
|
};
|
2019-11-03 20:44:23 +00:00
|
|
|
use test_utils::tested_by;
|
2019-03-02 20:59:04 +00:00
|
|
|
|
2019-12-24 22:45:14 +00:00
|
|
|
use crate::{
|
2020-02-02 13:04:06 +00:00
|
|
|
attr::Attrs,
|
|
|
|
db::DefDatabase,
|
|
|
|
path::{ImportAlias, ModPath},
|
|
|
|
visibility::RawVisibility,
|
|
|
|
FileAstId, HirFileId, InFile,
|
2019-12-24 22:45:14 +00:00
|
|
|
};
|
2019-03-02 20:59:04 +00:00
|
|
|
|
2019-03-26 10:53:50 +00:00
|
|
|
/// `RawItems` is a set of top-level items in a file (except for impls).
|
|
|
|
///
|
|
|
|
/// It is the input to name resolution algorithm. `RawItems` are not invalidated
|
|
|
|
/// on most edits.
|
2019-03-13 13:38:02 +00:00
|
|
|
#[derive(Debug, Default, PartialEq, Eq)]
|
|
|
|
pub struct RawItems {
|
2020-03-19 15:00:11 +00:00
|
|
|
modules: Arena<ModuleData>,
|
|
|
|
imports: Arena<ImportData>,
|
|
|
|
defs: Arena<DefData>,
|
|
|
|
macros: Arena<MacroData>,
|
|
|
|
impls: Arena<ImplData>,
|
2019-03-02 20:59:04 +00:00
|
|
|
/// items for top-level module
|
|
|
|
items: Vec<RawItem>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RawItems {
|
2020-03-13 15:05:46 +00:00
|
|
|
pub(crate) fn raw_items_query(db: &dyn DefDatabase, file_id: HirFileId) -> Arc<RawItems> {
|
2019-12-21 18:40:20 +00:00
|
|
|
let _p = profile("raw_items_query");
|
2019-03-02 20:59:04 +00:00
|
|
|
let mut collector = RawItemsCollector {
|
|
|
|
raw_items: RawItems::default(),
|
2019-06-03 14:21:08 +00:00
|
|
|
source_ast_id_map: db.ast_id_map(file_id),
|
2019-09-26 17:59:38 +00:00
|
|
|
file_id,
|
2020-03-13 15:05:46 +00:00
|
|
|
hygiene: Hygiene::new(db.upcast(), file_id),
|
2019-03-02 20:59:04 +00:00
|
|
|
};
|
2019-05-13 22:42:59 +00:00
|
|
|
if let Some(node) = db.parse_or_expand(file_id) {
|
2019-09-10 19:12:37 +00:00
|
|
|
if let Some(source_file) = ast::SourceFile::cast(node.clone()) {
|
2019-07-19 07:43:01 +00:00
|
|
|
collector.process_module(None, source_file);
|
2019-09-10 19:12:37 +00:00
|
|
|
} else if let Some(item_list) = ast::MacroItems::cast(node) {
|
|
|
|
collector.process_module(None, item_list);
|
2019-05-13 22:42:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-21 16:26:05 +00:00
|
|
|
let raw_items = collector.raw_items;
|
|
|
|
Arc::new(raw_items)
|
2019-03-13 13:38:02 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 20:47:52 +00:00
|
|
|
pub(super) fn items(&self) -> &[RawItem] {
|
2019-03-13 13:38:02 +00:00
|
|
|
&self.items
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:00:11 +00:00
|
|
|
impl Index<Idx<ModuleData>> for RawItems {
|
2019-03-02 20:59:04 +00:00
|
|
|
type Output = ModuleData;
|
2020-03-19 15:00:11 +00:00
|
|
|
fn index(&self, idx: Idx<ModuleData>) -> &ModuleData {
|
2019-03-02 20:59:04 +00:00
|
|
|
&self.modules[idx]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-21 16:34:28 +00:00
|
|
|
impl Index<Import> for RawItems {
|
2019-03-02 20:59:04 +00:00
|
|
|
type Output = ImportData;
|
2019-12-21 16:34:28 +00:00
|
|
|
fn index(&self, idx: Import) -> &ImportData {
|
2019-03-02 20:59:04 +00:00
|
|
|
&self.imports[idx]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:00:11 +00:00
|
|
|
impl Index<Idx<DefData>> for RawItems {
|
2019-03-02 20:59:04 +00:00
|
|
|
type Output = DefData;
|
2020-03-19 15:00:11 +00:00
|
|
|
fn index(&self, idx: Idx<DefData>) -> &DefData {
|
2019-03-02 20:59:04 +00:00
|
|
|
&self.defs[idx]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:00:11 +00:00
|
|
|
impl Index<Idx<MacroData>> for RawItems {
|
2019-03-02 20:59:04 +00:00
|
|
|
type Output = MacroData;
|
2020-03-19 15:00:11 +00:00
|
|
|
fn index(&self, idx: Idx<MacroData>) -> &MacroData {
|
2019-03-02 20:59:04 +00:00
|
|
|
&self.macros[idx]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:00:11 +00:00
|
|
|
impl Index<Idx<ImplData>> for RawItems {
|
2019-11-15 16:32:56 +00:00
|
|
|
type Output = ImplData;
|
2020-03-19 15:00:11 +00:00
|
|
|
fn index(&self, idx: Idx<ImplData>) -> &ImplData {
|
2019-11-15 16:32:56 +00:00
|
|
|
&self.impls[idx]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-29 18:50:56 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
2019-11-08 21:23:11 +00:00
|
|
|
pub(super) struct RawItem {
|
2019-11-22 08:27:47 +00:00
|
|
|
pub(super) attrs: Attrs,
|
2019-11-08 20:47:52 +00:00
|
|
|
pub(super) kind: RawItemKind,
|
2019-09-29 18:50:56 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 13:38:02 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
2019-11-08 21:23:11 +00:00
|
|
|
pub(super) enum RawItemKind {
|
2020-03-19 15:00:11 +00:00
|
|
|
Module(Idx<ModuleData>),
|
2019-12-21 16:34:28 +00:00
|
|
|
Import(Import),
|
2020-03-19 15:00:11 +00:00
|
|
|
Def(Idx<DefData>),
|
|
|
|
Macro(Idx<MacroData>),
|
|
|
|
Impl(Idx<ImplData>),
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 13:38:02 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2019-11-08 21:23:11 +00:00
|
|
|
pub(super) enum ModuleData {
|
2019-12-26 14:49:13 +00:00
|
|
|
Declaration {
|
|
|
|
name: Name,
|
2019-12-26 14:57:14 +00:00
|
|
|
visibility: RawVisibility,
|
2019-12-26 14:49:13 +00:00
|
|
|
ast_id: FileAstId<ast::Module>,
|
|
|
|
},
|
|
|
|
Definition {
|
|
|
|
name: Name,
|
2019-12-26 14:57:14 +00:00
|
|
|
visibility: RawVisibility,
|
2019-12-26 14:49:13 +00:00
|
|
|
ast_id: FileAstId<ast::Module>,
|
|
|
|
items: Vec<RawItem>,
|
|
|
|
},
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 15:00:11 +00:00
|
|
|
pub(crate) type Import = Idx<ImportData>;
|
2019-12-21 16:34:28 +00:00
|
|
|
|
2019-03-16 14:17:50 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct ImportData {
|
2019-12-13 11:12:36 +00:00
|
|
|
pub(super) path: ModPath,
|
2020-02-02 13:04:06 +00:00
|
|
|
pub(super) alias: Option<ImportAlias>,
|
2019-11-08 20:47:52 +00:00
|
|
|
pub(super) is_glob: bool,
|
|
|
|
pub(super) is_prelude: bool,
|
|
|
|
pub(super) is_extern_crate: bool,
|
|
|
|
pub(super) is_macro_use: bool,
|
2019-12-26 14:57:14 +00:00
|
|
|
pub(super) visibility: RawVisibility,
|
2019-03-16 14:17:50 +00:00
|
|
|
}
|
2019-03-02 20:59:04 +00:00
|
|
|
|
2020-03-19 15:00:11 +00:00
|
|
|
// type Def = Idx<DefData>;
|
2019-03-02 20:59:04 +00:00
|
|
|
|
2019-03-13 13:38:02 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2019-11-08 21:23:11 +00:00
|
|
|
pub(super) struct DefData {
|
2019-11-08 20:47:52 +00:00
|
|
|
pub(super) name: Name,
|
|
|
|
pub(super) kind: DefKind,
|
2019-12-26 14:57:14 +00:00
|
|
|
pub(super) visibility: RawVisibility,
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
2019-11-08 21:23:11 +00:00
|
|
|
pub(super) enum DefKind {
|
2019-03-26 15:27:22 +00:00
|
|
|
Function(FileAstId<ast::FnDef>),
|
|
|
|
Struct(FileAstId<ast::StructDef>),
|
2019-11-25 14:30:50 +00:00
|
|
|
Union(FileAstId<ast::UnionDef>),
|
2019-03-26 15:27:22 +00:00
|
|
|
Enum(FileAstId<ast::EnumDef>),
|
|
|
|
Const(FileAstId<ast::ConstDef>),
|
|
|
|
Static(FileAstId<ast::StaticDef>),
|
|
|
|
Trait(FileAstId<ast::TraitDef>),
|
|
|
|
TypeAlias(FileAstId<ast::TypeAliasDef>),
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 14:10:33 +00:00
|
|
|
impl DefKind {
|
|
|
|
pub fn ast_id(&self) -> FileAstId<ast::ModuleItem> {
|
|
|
|
match self {
|
|
|
|
DefKind::Function(it) => it.upcast(),
|
|
|
|
DefKind::Struct(it) => it.upcast(),
|
|
|
|
DefKind::Union(it) => it.upcast(),
|
|
|
|
DefKind::Enum(it) => it.upcast(),
|
|
|
|
DefKind::Const(it) => it.upcast(),
|
|
|
|
DefKind::Static(it) => it.upcast(),
|
|
|
|
DefKind::Trait(it) => it.upcast(),
|
|
|
|
DefKind::TypeAlias(it) => it.upcast(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 13:38:02 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2019-11-08 21:23:11 +00:00
|
|
|
pub(super) struct MacroData {
|
2019-11-08 20:47:52 +00:00
|
|
|
pub(super) ast_id: FileAstId<ast::MacroCall>,
|
2019-12-13 11:12:36 +00:00
|
|
|
pub(super) path: ModPath,
|
2019-11-08 20:47:52 +00:00
|
|
|
pub(super) name: Option<Name>,
|
|
|
|
pub(super) export: bool,
|
2019-11-10 03:03:24 +00:00
|
|
|
pub(super) builtin: bool,
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 16:32:56 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub(super) struct ImplData {
|
2020-02-29 20:24:40 +00:00
|
|
|
pub(super) ast_id: FileAstId<ast::ImplDef>,
|
2019-11-15 16:32:56 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 15:41:50 +00:00
|
|
|
struct RawItemsCollector {
|
2019-03-02 20:59:04 +00:00
|
|
|
raw_items: RawItems,
|
2019-03-26 16:00:11 +00:00
|
|
|
source_ast_id_map: Arc<AstIdMap>,
|
2019-09-26 17:59:38 +00:00
|
|
|
file_id: HirFileId,
|
2019-10-30 15:41:50 +00:00
|
|
|
hygiene: Hygiene,
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 15:41:50 +00:00
|
|
|
impl RawItemsCollector {
|
2020-03-19 15:00:11 +00:00
|
|
|
fn process_module(
|
|
|
|
&mut self,
|
|
|
|
current_module: Option<Idx<ModuleData>>,
|
|
|
|
body: impl ast::ModuleItemOwner,
|
|
|
|
) {
|
2020-03-26 15:10:01 +00:00
|
|
|
for item in body.items() {
|
|
|
|
self.add_item(current_module, item)
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:00:11 +00:00
|
|
|
fn add_item(&mut self, current_module: Option<Idx<ModuleData>>, item: ast::ModuleItem) {
|
2019-09-29 18:50:56 +00:00
|
|
|
let attrs = self.parse_attrs(&item);
|
2019-12-26 14:57:14 +00:00
|
|
|
let visibility = RawVisibility::from_ast_with_hygiene(item.visibility(), &self.hygiene);
|
2019-08-19 11:04:51 +00:00
|
|
|
let (kind, name) = match item {
|
|
|
|
ast::ModuleItem::Module(module) => {
|
2019-03-02 20:59:04 +00:00
|
|
|
self.add_module(current_module, module);
|
|
|
|
return;
|
|
|
|
}
|
2019-08-19 11:04:51 +00:00
|
|
|
ast::ModuleItem::UseItem(use_item) => {
|
2019-03-02 20:59:04 +00:00
|
|
|
self.add_use_item(current_module, use_item);
|
|
|
|
return;
|
|
|
|
}
|
2019-08-19 11:04:51 +00:00
|
|
|
ast::ModuleItem::ExternCrateItem(extern_crate) => {
|
2019-03-02 20:59:04 +00:00
|
|
|
self.add_extern_crate_item(current_module, extern_crate);
|
|
|
|
return;
|
|
|
|
}
|
2020-02-29 20:24:40 +00:00
|
|
|
ast::ModuleItem::ImplDef(it) => {
|
2019-11-15 16:32:56 +00:00
|
|
|
self.add_impl(current_module, it);
|
2019-03-02 20:59:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-08-19 11:04:51 +00:00
|
|
|
ast::ModuleItem::StructDef(it) => {
|
2019-07-19 07:43:01 +00:00
|
|
|
let id = self.source_ast_id_map.ast_id(&it);
|
2019-05-23 17:18:47 +00:00
|
|
|
let name = it.name();
|
2019-11-25 14:30:50 +00:00
|
|
|
(DefKind::Struct(id), name)
|
|
|
|
}
|
|
|
|
ast::ModuleItem::UnionDef(it) => {
|
|
|
|
let id = self.source_ast_id_map.ast_id(&it);
|
|
|
|
let name = it.name();
|
|
|
|
(DefKind::Union(id), name)
|
2019-03-26 15:27:22 +00:00
|
|
|
}
|
2019-08-19 11:04:51 +00:00
|
|
|
ast::ModuleItem::EnumDef(it) => {
|
2019-07-19 07:43:01 +00:00
|
|
|
(DefKind::Enum(self.source_ast_id_map.ast_id(&it)), it.name())
|
2019-03-26 15:27:22 +00:00
|
|
|
}
|
2019-08-19 11:04:51 +00:00
|
|
|
ast::ModuleItem::FnDef(it) => {
|
2019-07-19 07:43:01 +00:00
|
|
|
(DefKind::Function(self.source_ast_id_map.ast_id(&it)), it.name())
|
2019-03-26 15:27:22 +00:00
|
|
|
}
|
2019-08-19 11:04:51 +00:00
|
|
|
ast::ModuleItem::TraitDef(it) => {
|
2019-07-19 07:43:01 +00:00
|
|
|
(DefKind::Trait(self.source_ast_id_map.ast_id(&it)), it.name())
|
2019-03-26 15:27:22 +00:00
|
|
|
}
|
2019-08-19 11:04:51 +00:00
|
|
|
ast::ModuleItem::TypeAliasDef(it) => {
|
2019-07-19 07:43:01 +00:00
|
|
|
(DefKind::TypeAlias(self.source_ast_id_map.ast_id(&it)), it.name())
|
2019-03-26 15:27:22 +00:00
|
|
|
}
|
2019-08-19 11:04:51 +00:00
|
|
|
ast::ModuleItem::ConstDef(it) => {
|
2019-07-19 07:43:01 +00:00
|
|
|
(DefKind::Const(self.source_ast_id_map.ast_id(&it)), it.name())
|
2019-03-26 15:27:22 +00:00
|
|
|
}
|
2019-08-19 11:04:51 +00:00
|
|
|
ast::ModuleItem::StaticDef(it) => {
|
2019-07-19 07:43:01 +00:00
|
|
|
(DefKind::Static(self.source_ast_id_map.ast_id(&it)), it.name())
|
2019-03-26 15:27:22 +00:00
|
|
|
}
|
2020-03-26 15:10:01 +00:00
|
|
|
ast::ModuleItem::MacroCall(it) => {
|
|
|
|
self.add_macro(current_module, it);
|
|
|
|
return;
|
|
|
|
}
|
2020-04-03 19:12:09 +00:00
|
|
|
ast::ModuleItem::ExternBlock(_) => {
|
|
|
|
// FIXME: add extern block
|
|
|
|
return;
|
|
|
|
}
|
2019-03-02 20:59:04 +00:00
|
|
|
};
|
|
|
|
if let Some(name) = name {
|
|
|
|
let name = name.as_name();
|
2019-12-24 22:45:14 +00:00
|
|
|
let def = self.raw_items.defs.alloc(DefData { name, kind, visibility });
|
2019-09-29 18:50:56 +00:00
|
|
|
self.push_item(current_module, attrs, RawItemKind::Def(def));
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:00:11 +00:00
|
|
|
fn add_module(&mut self, current_module: Option<Idx<ModuleData>>, module: ast::Module) {
|
2019-03-02 20:59:04 +00:00
|
|
|
let name = match module.name() {
|
|
|
|
Some(it) => it.as_name(),
|
|
|
|
None => return,
|
|
|
|
};
|
2019-09-29 18:50:56 +00:00
|
|
|
let attrs = self.parse_attrs(&module);
|
2019-12-26 14:57:14 +00:00
|
|
|
let visibility = RawVisibility::from_ast_with_hygiene(module.visibility(), &self.hygiene);
|
2019-07-06 11:04:56 +00:00
|
|
|
|
2019-07-19 07:43:01 +00:00
|
|
|
let ast_id = self.source_ast_id_map.ast_id(&module);
|
2020-04-10 08:11:05 +00:00
|
|
|
if module.semicolon_token().is_some() {
|
2019-12-26 14:49:13 +00:00
|
|
|
let item =
|
|
|
|
self.raw_items.modules.alloc(ModuleData::Declaration { name, visibility, ast_id });
|
2019-09-29 18:50:56 +00:00
|
|
|
self.push_item(current_module, attrs, RawItemKind::Module(item));
|
2019-03-02 20:59:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(item_list) = module.item_list() {
|
2019-03-13 13:38:02 +00:00
|
|
|
let item = self.raw_items.modules.alloc(ModuleData::Definition {
|
|
|
|
name,
|
2019-12-26 14:49:13 +00:00
|
|
|
visibility,
|
2019-03-26 14:25:14 +00:00
|
|
|
ast_id,
|
2019-03-13 13:38:02 +00:00
|
|
|
items: Vec::new(),
|
|
|
|
});
|
2019-03-02 20:59:04 +00:00
|
|
|
self.process_module(Some(item), item_list);
|
2019-09-29 18:50:56 +00:00
|
|
|
self.push_item(current_module, attrs, RawItemKind::Module(item));
|
2019-03-13 13:38:02 +00:00
|
|
|
return;
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
2019-11-03 20:44:23 +00:00
|
|
|
tested_by!(name_res_works_for_broken_modules);
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 15:00:11 +00:00
|
|
|
fn add_use_item(&mut self, current_module: Option<Idx<ModuleData>>, use_item: ast::UseItem) {
|
2019-09-29 22:52:15 +00:00
|
|
|
// FIXME: cfg_attr
|
2019-03-13 13:04:28 +00:00
|
|
|
let is_prelude = use_item.has_atom_attr("prelude_import");
|
2019-09-29 18:50:56 +00:00
|
|
|
let attrs = self.parse_attrs(&use_item);
|
2019-12-26 14:57:14 +00:00
|
|
|
let visibility = RawVisibility::from_ast_with_hygiene(use_item.visibility(), &self.hygiene);
|
2019-03-02 20:59:04 +00:00
|
|
|
|
2019-10-30 15:41:50 +00:00
|
|
|
let mut buf = Vec::new();
|
2019-12-13 11:12:36 +00:00
|
|
|
ModPath::expand_use_item(
|
2019-11-28 09:50:26 +00:00
|
|
|
InFile { value: use_item, file_id: self.file_id },
|
2019-10-30 15:41:50 +00:00
|
|
|
&self.hygiene,
|
2019-12-21 16:26:05 +00:00
|
|
|
|path, _use_tree, is_glob, alias| {
|
2019-09-26 17:59:38 +00:00
|
|
|
let import_data = ImportData {
|
|
|
|
path,
|
|
|
|
alias,
|
|
|
|
is_glob,
|
|
|
|
is_prelude,
|
|
|
|
is_extern_crate: false,
|
|
|
|
is_macro_use: false,
|
2019-12-24 22:45:14 +00:00
|
|
|
visibility: visibility.clone(),
|
2019-09-26 17:59:38 +00:00
|
|
|
};
|
2019-12-21 16:26:05 +00:00
|
|
|
buf.push(import_data);
|
2019-09-26 17:59:38 +00:00
|
|
|
},
|
2019-10-30 15:41:50 +00:00
|
|
|
);
|
2019-12-21 16:26:05 +00:00
|
|
|
for import_data in buf {
|
|
|
|
self.push_import(current_module, attrs.clone(), import_data);
|
2019-10-30 15:41:50 +00:00
|
|
|
}
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn add_extern_crate_item(
|
|
|
|
&mut self,
|
2020-03-19 15:00:11 +00:00
|
|
|
current_module: Option<Idx<ModuleData>>,
|
2019-07-19 07:43:01 +00:00
|
|
|
extern_crate: ast::ExternCrateItem,
|
2019-03-02 20:59:04 +00:00
|
|
|
) {
|
|
|
|
if let Some(name_ref) = extern_crate.name_ref() {
|
2019-12-13 11:12:36 +00:00
|
|
|
let path = ModPath::from_name_ref(&name_ref);
|
2019-12-24 22:45:14 +00:00
|
|
|
let visibility =
|
2019-12-26 14:57:14 +00:00
|
|
|
RawVisibility::from_ast_with_hygiene(extern_crate.visibility(), &self.hygiene);
|
2020-02-02 13:04:06 +00:00
|
|
|
let alias = extern_crate.alias().map(|a| {
|
2020-02-18 12:53:02 +00:00
|
|
|
a.name().map(|it| it.as_name()).map_or(ImportAlias::Underscore, ImportAlias::Alias)
|
2020-01-31 04:14:20 +00:00
|
|
|
});
|
2019-09-29 18:50:56 +00:00
|
|
|
let attrs = self.parse_attrs(&extern_crate);
|
2019-09-29 22:52:15 +00:00
|
|
|
// FIXME: cfg_attr
|
2019-08-29 17:56:00 +00:00
|
|
|
let is_macro_use = extern_crate.has_atom_attr("macro_use");
|
2019-04-02 14:58:04 +00:00
|
|
|
let import_data = ImportData {
|
2019-03-02 20:59:04 +00:00
|
|
|
path,
|
|
|
|
alias,
|
|
|
|
is_glob: false,
|
|
|
|
is_prelude: false,
|
|
|
|
is_extern_crate: true,
|
2019-08-29 17:56:00 +00:00
|
|
|
is_macro_use,
|
2019-12-24 22:45:14 +00:00
|
|
|
visibility,
|
2019-04-02 14:58:04 +00:00
|
|
|
};
|
2019-12-21 16:26:05 +00:00
|
|
|
self.push_import(current_module, attrs, import_data);
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:00:11 +00:00
|
|
|
fn add_macro(&mut self, current_module: Option<Idx<ModuleData>>, m: ast::MacroCall) {
|
2019-09-29 22:52:15 +00:00
|
|
|
let attrs = self.parse_attrs(&m);
|
2019-12-13 11:12:36 +00:00
|
|
|
let path = match m.path().and_then(|path| ModPath::from_src(path, &self.hygiene)) {
|
2019-03-26 10:09:39 +00:00
|
|
|
Some(it) => it,
|
2019-03-02 20:59:04 +00:00
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
let name = m.name().map(|it| it.as_name());
|
2019-07-19 07:43:01 +00:00
|
|
|
let ast_id = self.source_ast_id_map.ast_id(&m);
|
2019-09-29 22:52:15 +00:00
|
|
|
// FIXME: cfg_attr
|
2019-09-29 21:15:03 +00:00
|
|
|
let export = m.attrs().filter_map(|x| x.simple_name()).any(|name| name == "macro_export");
|
2019-09-12 12:41:16 +00:00
|
|
|
|
2019-11-10 03:03:24 +00:00
|
|
|
// FIXME: cfg_attr
|
|
|
|
let builtin =
|
|
|
|
m.attrs().filter_map(|x| x.simple_name()).any(|name| name == "rustc_builtin_macro");
|
|
|
|
|
|
|
|
let m = self.raw_items.macros.alloc(MacroData { ast_id, path, name, export, builtin });
|
2019-09-29 18:50:56 +00:00
|
|
|
self.push_item(current_module, attrs, RawItemKind::Macro(m));
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 15:00:11 +00:00
|
|
|
fn add_impl(&mut self, current_module: Option<Idx<ModuleData>>, imp: ast::ImplDef) {
|
2019-11-15 16:32:56 +00:00
|
|
|
let attrs = self.parse_attrs(&imp);
|
|
|
|
let ast_id = self.source_ast_id_map.ast_id(&imp);
|
|
|
|
let imp = self.raw_items.impls.alloc(ImplData { ast_id });
|
|
|
|
self.push_item(current_module, attrs, RawItemKind::Impl(imp))
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:00:11 +00:00
|
|
|
fn push_import(
|
|
|
|
&mut self,
|
|
|
|
current_module: Option<Idx<ModuleData>>,
|
|
|
|
attrs: Attrs,
|
|
|
|
data: ImportData,
|
|
|
|
) {
|
2019-12-21 16:26:05 +00:00
|
|
|
let import = self.raw_items.imports.alloc(data);
|
2019-09-29 18:50:56 +00:00
|
|
|
self.push_item(current_module, attrs, RawItemKind::Import(import))
|
2019-04-02 14:58:04 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 15:00:11 +00:00
|
|
|
fn push_item(
|
|
|
|
&mut self,
|
|
|
|
current_module: Option<Idx<ModuleData>>,
|
|
|
|
attrs: Attrs,
|
|
|
|
kind: RawItemKind,
|
|
|
|
) {
|
2019-03-02 20:59:04 +00:00
|
|
|
match current_module {
|
|
|
|
Some(module) => match &mut self.raw_items.modules[module] {
|
|
|
|
ModuleData::Definition { items, .. } => items,
|
|
|
|
ModuleData::Declaration { .. } => unreachable!(),
|
|
|
|
},
|
|
|
|
None => &mut self.raw_items.items,
|
|
|
|
}
|
2019-09-29 18:50:56 +00:00
|
|
|
.push(RawItem { attrs, kind })
|
|
|
|
}
|
|
|
|
|
2019-10-02 17:38:56 +00:00
|
|
|
fn parse_attrs(&self, item: &impl ast::AttrsOwner) -> Attrs {
|
2019-11-24 12:50:45 +00:00
|
|
|
Attrs::new(item, &self.hygiene)
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
}
|