]> git.lizzy.rs Git - rust.git/commitdiff
Store attributes as strings
authorJonas Bushart <jonas@bushart.org>
Thu, 23 Feb 2017 22:24:12 +0000 (23:24 +0100)
committerAlex Crichton <alex@alexcrichton.com>
Fri, 10 Mar 2017 16:02:24 +0000 (08:02 -0800)
Remove the AST structure

src/librustc_save_analysis/external_data.rs

index 0cfa71a3499169199e42a1dc57dd39c8a8bd4bab..38d1df2abb8881015b1c94baa27b7845f0d7f68c 100644 (file)
@@ -11,8 +11,9 @@
 use rustc::hir::def_id::{CrateNum, DefId, DefIndex};
 use rustc::hir::map::Map;
 use rustc::ty::TyCtxt;
-use syntax::ast::{self, LitKind, NodeId, StrStyle};
+use syntax::ast::{self, NodeId};
 use syntax::codemap::CodeMap;
+use syntax::print::pprust;
 use syntax_pos::Span;
 
 use data::{self, Visibility, SigElement};
@@ -67,16 +68,22 @@ pub fn from_span(span: Span, cm: &CodeMap) -> SpanData {
 /// Represent an arbitrary attribute on a code element
 #[derive(Clone, Debug, RustcEncodable)]
 pub struct Attribute {
-    value: AttributeItem,
+    value: String,
     span: SpanData,
 }
 
 impl Lower for ast::Attribute {
     type Target = Attribute;
 
-    fn lower(self, tcx: TyCtxt) -> Attribute {
+    fn lower(mut self, tcx: TyCtxt) -> Attribute {
+        // strip #[] and #![] from the original attributes
+        self.style = ast::AttrStyle::Outer;
+        let value = pprust::attribute_to_string(&self);
+        // #[] are all ASCII which makes this slice save
+        let value = value[2..value.len()-1].to_string();
+
         Attribute {
-            value: self.value.lower(tcx),
+            value: value,
             span: SpanData::from_span(self.span, tcx.sess.codemap()),
         }
     }
@@ -90,76 +97,6 @@ fn lower(self, tcx: TyCtxt) -> Vec<Attribute> {
     }
 }
 
-/// A single item as part of an attribute
-#[derive(Clone, Debug, RustcEncodable)]
-pub struct AttributeItem {
-    name: LitKind,
-    kind: AttributeItemKind,
-    span: SpanData,
-}
-
-impl Lower for ast::MetaItem {
-    type Target = AttributeItem;
-
-    fn lower(self, tcx: TyCtxt) -> AttributeItem {
-        AttributeItem {
-            name: LitKind::Str(self.name, StrStyle::Cooked),
-            kind: self.node.lower(tcx),
-            span: SpanData::from_span(self.span, tcx.sess.codemap()),
-        }
-    }
-}
-
-impl Lower for ast::NestedMetaItem {
-    type Target = AttributeItem;
-
-    fn lower(self, tcx: TyCtxt) -> AttributeItem {
-        match self.node {
-            ast::NestedMetaItemKind::MetaItem(item) => item.lower(tcx),
-            ast::NestedMetaItemKind::Literal(lit) => {
-                AttributeItem {
-                    name: lit.node,
-                    kind: AttributeItemKind::Literal,
-                    span: SpanData::from_span(lit.span, tcx.sess.codemap()),
-                }
-            }
-        }
-    }
-}
-
-#[derive(Clone, Debug, RustcEncodable)]
-pub enum AttributeItemKind {
-    /// Word meta item.
-    ///
-    /// E.g. `test` as in `#[test]`
-    Literal,
-    /// Name value meta item.
-    ///
-    /// E.g. `feature = "foo"` as in `#[feature = "foo"]`
-    NameValue(LitKind, SpanData),
-    /// List meta item.
-    ///
-    /// E.g. the `derive(..)` as in `#[derive(..)]`
-    List(Vec<AttributeItem>),
-}
-
-impl Lower for ast::MetaItemKind {
-    type Target = AttributeItemKind;
-
-    fn lower(self, tcx: TyCtxt) -> AttributeItemKind {
-        match self {
-            ast::MetaItemKind::Word => AttributeItemKind::Literal,
-            ast::MetaItemKind::List(items) => {
-                AttributeItemKind::List(items.into_iter().map(|x| x.lower(tcx)).collect())
-            }
-            ast::MetaItemKind::NameValue(lit) => {
-                let span = SpanData::from_span(lit.span, tcx.sess.codemap());
-                AttributeItemKind::NameValue(lit.node, span)
-            }
-        }
-    }
-}
-
 #[derive(Debug, RustcEncodable)]
 pub struct CratePreludeData {
     pub crate_name: String,