Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
Merge branch 'rust'
Browse files Browse the repository at this point in the history
# Conflicts:
#	syntex_syntax/src/ext/expand.rs
#	syntex_syntax/src/ext/tt/macro_parser.rs
  • Loading branch information
erickt committed Aug 28, 2015
2 parents 53c2ed4 + 86be954 commit 898ac75
Show file tree
Hide file tree
Showing 15 changed files with 268 additions and 202 deletions.
60 changes: 1 addition & 59 deletions syntex_syntax/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
use print::pprust;
use ptr::P;

use std::cell::Cell;
use std::fmt;
use std::rc::Rc;
use serialize::{Encodable, Decodable, Encoder, Decoder};
Expand Down Expand Up @@ -371,37 +370,7 @@ pub type CrateNum = u32;

pub type NodeId = u32;

#[derive(Clone, Eq, Ord, PartialOrd, PartialEq, RustcEncodable,
RustcDecodable, Hash, Copy)]
pub struct DefId {
pub krate: CrateNum,
pub node: NodeId,
}

fn default_def_id_debug(_: DefId, _: &mut fmt::Formatter) -> fmt::Result { Ok(()) }

thread_local!(pub static DEF_ID_DEBUG: Cell<fn(DefId, &mut fmt::Formatter) -> fmt::Result> =
Cell::new(default_def_id_debug));

impl fmt::Debug for DefId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "DefId {{ krate: {}, node: {} }}",
self.krate, self.node));
DEF_ID_DEBUG.with(|def_id_debug| def_id_debug.get()(*self, f))
}
}

impl DefId {
/// Read the node id, asserting that this def-id is krate-local.
pub fn local_id(&self) -> NodeId {
assert_eq!(self.krate, LOCAL_CRATE);
self.node
}
}

/// Item definitions in the currently-compiled crate would have the CrateNum
/// LOCAL_CRATE in their DefId.
pub const LOCAL_CRATE: CrateNum = 0;
/// Node id used to represent the root of the crate.
pub const CRATE_NODE_ID: NodeId = 0;

/// When parsing and doing expansions, we initially give all AST nodes this AST
Expand Down Expand Up @@ -1216,16 +1185,6 @@ pub enum LitIntType {
UnsuffixedIntLit(Sign)
}

impl LitIntType {
pub fn suffix_len(&self) -> usize {
match *self {
UnsuffixedIntLit(_) => 0,
SignedIntLit(s, _) => s.suffix_len(),
UnsignedIntLit(u) => u.suffix_len()
}
}
}

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum Lit_ {
/// A string literal (`"foo"`)
Expand Down Expand Up @@ -1333,12 +1292,6 @@ impl fmt::Display for IntTy {
}

impl IntTy {
pub fn suffix_len(&self) -> usize {
match *self {
TyIs | TyI8 => 2,
TyI16 | TyI32 | TyI64 => 3,
}
}
pub fn bit_width(&self) -> Option<usize> {
Some(match *self {
TyIs => return None,
Expand All @@ -1360,12 +1313,6 @@ pub enum UintTy {
}

impl UintTy {
pub fn suffix_len(&self) -> usize {
match *self {
TyUs | TyU8 => 2,
TyU16 | TyU32 | TyU64 => 3,
}
}
pub fn bit_width(&self) -> Option<usize> {
Some(match *self {
TyUs => return None,
Expand Down Expand Up @@ -1408,11 +1355,6 @@ impl fmt::Display for FloatTy {
}

impl FloatTy {
pub fn suffix_len(&self) -> usize {
match *self {
TyF32 | TyF64 => 3, // add F128 handling here
}
}
pub fn bit_width(&self) -> usize {
match *self {
TyF32 => 32,
Expand Down
8 changes: 1 addition & 7 deletions syntex_syntax/src/ast_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ pub fn path_name_i(idents: &[Ident]) -> String {
idents.iter().map(|i| i.to_string()).collect::<Vec<String>>().connect("::")
}

pub fn local_def(id: NodeId) -> DefId {
ast::DefId { krate: LOCAL_CRATE, node: id }
}

pub fn is_local(did: ast::DefId) -> bool { did.krate == LOCAL_CRATE }

pub fn stmt_id(s: &Stmt) -> NodeId {
match s.node {
StmtDecl(_, id) => id,
Expand Down Expand Up @@ -444,7 +438,7 @@ impl<'a, 'v, O: IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O> {
visit::FkMethod(_, sig, _) => {
self.visit_generics_helper(&sig.generics)
}
visit::FkFnBlock => {}
visit::FkClosure => {}
}

for argument in &function_declaration.inputs {
Expand Down
43 changes: 36 additions & 7 deletions syntex_syntax/src/codemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ use std::io::{self, Read};

use serialize::{Encodable, Decodable, Encoder, Decoder};

use parse::token::intern;
use ast::Name;

// _____________________________________________________________________________
// Pos, BytePos, CharPos
Expand Down Expand Up @@ -257,21 +259,38 @@ pub struct FileMapAndBytePos { pub fm: Rc<FileMap>, pub pos: BytePos }
//

/// The source of expansion.
#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
#[derive(Clone, Hash, Debug, PartialEq, Eq)]
pub enum ExpnFormat {
/// e.g. #[derive(...)] <item>
MacroAttribute,
MacroAttribute(Name),
/// e.g. `format!()`
MacroBang,
MacroBang(Name),
/// Syntax sugar expansion performed by the compiler (libsyntax::expand).
CompilerExpansion,
CompilerExpansion(CompilerExpansionFormat),
}

#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
pub enum CompilerExpansionFormat {
IfLet,
PlacementIn,
WhileLet,
ForLoop,
Closure,
}

impl CompilerExpansionFormat {
pub fn name(self) -> &'static str {
match self {
CompilerExpansionFormat::IfLet => "if let expansion",
CompilerExpansionFormat::PlacementIn => "placement-in expansion",
CompilerExpansionFormat::WhileLet => "while let expansion",
CompilerExpansionFormat::ForLoop => "for loop expansion",
CompilerExpansionFormat::Closure => "closure expansion",
}
}
}
#[derive(Clone, Hash, Debug)]
pub struct NameAndSpan {
/// The name of the macro that was invoked to create the thing
/// with this Span.
pub name: String,
/// The format with which the macro was invoked.
pub format: ExpnFormat,
/// Whether the macro is allowed to use #[unstable]/feature-gated
Expand All @@ -284,6 +303,16 @@ pub struct NameAndSpan {
pub span: Option<Span>
}

impl NameAndSpan {
pub fn name(&self) -> Name {
match self.format {
ExpnFormat::MacroAttribute(s) => s,
ExpnFormat::MacroBang(s) => s,
ExpnFormat::CompilerExpansion(ce) => intern(ce.name()),
}
}
}

/// Extra information for tracking spans of macro and syntax sugar expansion
#[derive(Hash, Debug)]
pub struct ExpnInfo {
Expand Down
8 changes: 4 additions & 4 deletions syntex_syntax/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,14 +729,14 @@ impl EmitterWriter {
let ss = ei.callee.span.map_or(String::new(),
|span| cm.span_to_string(span));
let (pre, post) = match ei.callee.format {
codemap::MacroAttribute => ("#[", "]"),
codemap::MacroBang => ("", "!"),
codemap::CompilerExpansion => ("", ""),
codemap::MacroAttribute(..) => ("#[", "]"),
codemap::MacroBang(..) => ("", "!"),
codemap::CompilerExpansion(..) => ("", ""),
};
try!(self.print_diagnostic(&ss, Note,
&format!("in expansion of {}{}{}",
pre,
ei.callee.name,
ei.callee.name(),
post),
None));
let ss = cm.span_to_string(ei.call_site);
Expand Down
42 changes: 42 additions & 0 deletions syntex_syntax/src/entry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use attr;
use ast::{Item, ItemFn};

pub enum EntryPointType {
None,
MainNamed,
MainAttr,
Start,
OtherMain, // Not an entry point, but some other function named main
}

pub fn entry_point_type(item: &Item, depth: usize) -> EntryPointType {
match item.node {
ItemFn(..) => {
if attr::contains_name(&item.attrs, "start") {
EntryPointType::Start
} else if attr::contains_name(&item.attrs, "main") {
EntryPointType::MainAttr
} else if item.ident.name == "main" {
if depth == 1 {
// This is a top-level function so can be 'main'
EntryPointType::MainNamed
} else {
EntryPointType::OtherMain
}
} else {
EntryPointType::None
}
}
_ => EntryPointType::None,
}
}
5 changes: 2 additions & 3 deletions syntex_syntax/src/ext/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use codemap::Span;
use ext::base;
use ext::base::*;
use feature_gate;
use parse::token::InternedString;
use parse::token::{intern, InternedString};
use parse::token;
use ptr::P;
use str::slice_shift_char;
Expand Down Expand Up @@ -212,8 +212,7 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
let expn_id = cx.codemap().record_expansion(codemap::ExpnInfo {
call_site: sp,
callee: codemap::NameAndSpan {
name: "asm".to_string(),
format: codemap::MacroBang,
format: codemap::MacroBang(intern("asm")),
span: None,
allow_internal_unstable: false,
},
Expand Down
9 changes: 5 additions & 4 deletions syntex_syntax/src/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,13 +647,14 @@ impl<'a> ExtCtxt<'a> {
loop {
if self.codemap().with_expn_info(expn_id, |info| {
info.map_or(None, |i| {
if i.callee.name == "include" {
if i.callee.name() == "include" {
// Stop going up the backtrace once include! is encountered
return None;
}
expn_id = i.call_site.expn_id;
if i.callee.format != CompilerExpansion {
last_macro = Some(i.call_site)
match i.callee.format {
CompilerExpansion(..) => (),
_ => last_macro = Some(i.call_site),
}
return Some(());
})
Expand All @@ -677,7 +678,7 @@ impl<'a> ExtCtxt<'a> {
if self.recursion_count > self.ecfg.recursion_limit {
panic!(self.span_fatal(ei.call_site,
&format!("recursion limit reached while expanding the macro `{}`",
ei.callee.name)));
ei.callee.name())));
}

let mut call_site = ei.call_site;
Expand Down
13 changes: 9 additions & 4 deletions syntex_syntax/src/ext/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ use codemap::Span;
use diagnostic::SpanHandler;
use fold::MoveMap;
use owned_slice::OwnedSlice;
use parse::token::InternedString;
use parse::token::{intern, InternedString};
use parse::token::special_idents;
use ptr::P;

Expand Down Expand Up @@ -617,7 +617,13 @@ impl<'a> TraitDef<'a> {
attr::mark_used(&attr);
let opt_trait_ref = Some(trait_ref);
let ident = ast_util::impl_pretty_name(&opt_trait_ref, Some(&*self_type));
let mut a = vec![attr];
let unused_qual = cx.attribute(
self.span,
cx.meta_list(self.span,
InternedString::new("allow"),
vec![cx.meta_word(self.span,
InternedString::new("unused_qualifications"))]));
let mut a = vec![attr, unused_qual];
a.extend(self.attributes.iter().cloned());
cx.item(
self.span,
Expand Down Expand Up @@ -1430,8 +1436,7 @@ impl<'a> TraitDef<'a> {
to_set.expn_id = cx.codemap().record_expansion(codemap::ExpnInfo {
call_site: to_set,
callee: codemap::NameAndSpan {
name: format!("derive({})", trait_name),
format: codemap::MacroAttribute,
format: codemap::MacroAttribute(intern(&format!("derive({})", trait_name))),
span: Some(self.span),
allow_internal_unstable: false,
}
Expand Down
Loading

0 comments on commit 898ac75

Please sign in to comment.