Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macros have scope #5120

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 33 additions & 29 deletions src/libcore/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,42 +186,46 @@ fn SipState(key0: u64, key1: u64) -> SipState {
state
}

// sadly, these macro definitions can't appear later,
// because they're needed in the following defs;
// this design could be improved.

macro_rules! u8to64_le (
($buf:expr, $i:expr) =>
($buf[0+$i] as u64 |
$buf[1+$i] as u64 << 8 |
$buf[2+$i] as u64 << 16 |
$buf[3+$i] as u64 << 24 |
$buf[4+$i] as u64 << 32 |
$buf[5+$i] as u64 << 40 |
$buf[6+$i] as u64 << 48 |
$buf[7+$i] as u64 << 56)
)

macro_rules! rotl (
($x:expr, $b:expr) =>
(($x << $b) | ($x >> (64 - $b)))
)

macro_rules! compress (
($v0:expr, $v1:expr, $v2:expr, $v3:expr) =>
({
$v0 += $v1; $v1 = rotl!($v1, 13); $v1 ^= $v0;
$v0 = rotl!($v0, 32);
$v2 += $v3; $v3 = rotl!($v3, 16); $v3 ^= $v2;
$v0 += $v3; $v3 = rotl!($v3, 21); $v3 ^= $v0;
$v2 += $v1; $v1 = rotl!($v1, 17); $v1 ^= $v2;
$v2 = rotl!($v2, 32);
})
)


impl io::Writer for SipState {

// Methods for io::writer
#[inline(always)]
fn write(&self, msg: &[const u8]) {

macro_rules! u8to64_le (
($buf:expr, $i:expr) =>
($buf[0+$i] as u64 |
$buf[1+$i] as u64 << 8 |
$buf[2+$i] as u64 << 16 |
$buf[3+$i] as u64 << 24 |
$buf[4+$i] as u64 << 32 |
$buf[5+$i] as u64 << 40 |
$buf[6+$i] as u64 << 48 |
$buf[7+$i] as u64 << 56)
);

macro_rules! rotl (
($x:expr, $b:expr) =>
(($x << $b) | ($x >> (64 - $b)))
);

macro_rules! compress (
($v0:expr, $v1:expr, $v2:expr, $v3:expr) =>
({
$v0 += $v1; $v1 = rotl!($v1, 13); $v1 ^= $v0;
$v0 = rotl!($v0, 32);
$v2 += $v3; $v3 = rotl!($v3, 16); $v3 ^= $v2;
$v0 += $v3; $v3 = rotl!($v3, 21); $v3 ^= $v0;
$v2 += $v1; $v1 = rotl!($v1, 17); $v1 ^= $v2;
$v2 = rotl!($v2, 32);
})
);

let length = msg.len();
self.length += length;

Expand Down
5 changes: 0 additions & 5 deletions src/librustc/middle/trans/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,6 @@ use syntax::ast_util;
use syntax::codemap::span;
use syntax::print::pprust::pat_to_str;

pub fn macros() {
// FIXME(#3114): Macro import/export.
include!("macros.rs");
}

// An option identifying a literal: either a unit-like struct or an
// expression.
pub enum Lit {
Expand Down
5 changes: 0 additions & 5 deletions src/librustc/middle/trans/controlflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ use middle::trans::datum::*;

use core::str;

pub fn macros() {
// FIXME(#3114): Macro import/export.
include!("macros.rs");
}

pub fn trans_block(bcx: block, b: &ast::blk, dest: expr::Dest) -> block {
let _icx = bcx.insn_ctxt("trans_block");
let mut bcx = bcx;
Expand Down
2 changes: 0 additions & 2 deletions src/librustc/middle/trans/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ use syntax::codemap::spanned;
// These are passed around by the code generating functions to track the
// destination of a computation's value.

fn macros() { include!("macros.rs"); } // FIXME(#3114): Macro import/export.

pub enum Dest {
SaveIn(ValueRef),
Ignore,
Expand Down
11 changes: 5 additions & 6 deletions src/librustc/middle/trans/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

{
#[macro_escape];

macro_rules! unpack_datum(
($bcx: ident, $inp: expr) => (
Expand All @@ -18,7 +18,7 @@ macro_rules! unpack_datum(
db.datum
}
)
);
)

macro_rules! unpack_result(
($bcx: ident, $inp: expr) => (
Expand All @@ -28,7 +28,7 @@ macro_rules! unpack_result(
db.val
}
)
);
)

macro_rules! trace_span(
($bcx: ident, $sp: expr, $str: expr) => (
Expand All @@ -39,7 +39,7 @@ macro_rules! trace_span(
}
}
)
);
)

macro_rules! trace(
($bcx: ident, $str: expr) => (
Expand All @@ -50,6 +50,5 @@ macro_rules! trace(
}
}
)
);
)

}
5 changes: 0 additions & 5 deletions src/librustc/middle/trans/meth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ use syntax::ast_util::local_def;
use syntax::print::pprust::expr_to_str;
use syntax::{ast, ast_map};

pub fn macros() {
// FIXME(#3114): Macro import/export.
include!("macros.rs");
}

/**
The main "translation" pass for methods. Generates code
for non-monomorphized methods only. Other methods will
Expand Down
5 changes: 0 additions & 5 deletions src/librustc/middle/typeck/infer/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ use syntax::ast::{Onceness, purity, ret_style};
use syntax::ast;
use syntax::codemap::span;

pub fn macros() {
// FIXME(#3114): Macro import/export.
include!("macros.rs");
}

pub trait Combine {
fn infcx(&self) -> @mut InferCtxt;
fn tag(&self) -> ~str;
Expand Down
5 changes: 0 additions & 5 deletions src/librustc/middle/typeck/infer/lub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ use std::list;
use syntax::ast::{Many, Once, extern_fn, m_const, impure_fn, noreturn};
use syntax::ast::{pure_fn, ret_style, return_val, unsafe_fn};

pub fn macros() {
// FIXME(#3114): Macro import/export.
include!("macros.rs");
}

pub enum Lub = CombineFields; // least-upper-bound: common supertype

pub impl Lub {
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/middle/typeck/infer/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

{
#[macro_escape];

macro_rules! if_ok(
($inp: expr) => (
Expand All @@ -17,6 +17,5 @@ macro_rules! if_ok(
Err(e) => { return Err(e); }
}
)
);
)

}
1 change: 1 addition & 0 deletions src/librustc/middle/typeck/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ use syntax::codemap;
use syntax::ast_util;
use syntax::codemap::span;

pub mod macros;
pub mod combine;
pub mod glb;
pub mod lattice;
Expand Down
4 changes: 0 additions & 4 deletions src/librustc/middle/typeck/infer/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ use std::list::Nil;
use std::list;
use syntax::ast::{m_const, purity, ret_style};

pub fn macros() {
// FIXME(#3114): Macro import/export.
include!("macros.rs");
}

pub enum Sub = CombineFields; // "subtype", "subregion" etc

Expand Down
1 change: 1 addition & 0 deletions src/librustc/rustc.rc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use back_ = back;

pub mod middle {
pub mod trans {
pub mod macros;
pub mod inline;
pub mod monomorphize;
pub mod controlflow;
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ fn nmoddoc_from_mod(
ast::foreign_item_const(*) => {} // XXX: Not implemented.
}
}
doc:: NmodDoc {
doc::NmodDoc {
item: itemdoc,
fns: fns,
index: None
Expand Down
32 changes: 31 additions & 1 deletion src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,37 @@ macro_rules! interner_key (
(-3 as uint, 0u)))
)

// an identifier contains an index into the interner
// table and a SyntaxContext to track renaming and
// macro expansion per Flatt et al., "Macros
// That Work Together"
#[deriving_eq]
pub struct ident { repr: uint }
pub struct ident { repr: Name }

// a SyntaxContext represents a chain of macro-expandings
// and renamings. Each macro expansion corresponds to
// a fresh uint
#[deriving_eq]
pub enum SyntaxContext {
MT,
Mark (Mrk,~SyntaxContext),
Rename (~ident,Name,~SyntaxContext)
}

/*
// ** this is going to have to apply to paths, not to idents.
// Returns true if these two identifiers access the same
// local binding or top-level binding... that's what it
// should do. For now, it just compares the names.
pub fn free_ident_eq (a : ident, b: ident) -> bool{
a.repr == b.repr
}
*/
// a name represents a string, interned
type Name = uint;
// a mark represents a unique id associated
// with a macro expansion
type Mrk = uint;

pub impl<S:Encoder> Encodable<S> for ident {
fn encode(&self, s: &S) {
Expand Down Expand Up @@ -1230,6 +1259,7 @@ pub enum item_ {
Option<@trait_ref>, // (optional) trait this impl implements
@Ty, // self
~[@method]),
// a macro invocation (which includes macro definition)
item_mac(mac),
}

Expand Down
36 changes: 20 additions & 16 deletions src/libsyntax/ext/auto_encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,8 @@ mod test {
CallToEmitEnumVariantArg(uint),
CallToEmitUint(uint),
CallToEmitNil,
CallToEmitStruct(~str,uint),
CallToEmitField(~str,uint),
// all of the ones I was too lazy to handle:
CallToOther
}
Expand Down Expand Up @@ -1251,11 +1253,11 @@ mod test {
fn emit_rec(&self, f: fn()) {
self.add_unknown_to_log(); f();
}
fn emit_struct(&self, _name: &str, +_len: uint, f: fn()) {
self.add_unknown_to_log(); f();
fn emit_struct(&self, name: &str, +len: uint, f: fn()) {
self.add_to_log(CallToEmitStruct (name.to_str(),len)); f();
}
fn emit_field(&self, _name: &str, +_idx: uint, f: fn()) {
self.add_unknown_to_log(); f();
fn emit_field(&self, name: &str, +idx: uint, f: fn()) {
self.add_to_log(CallToEmitField (name.to_str(),idx)); f();
}

fn emit_tup(&self, +_len: uint, f: fn()) {
Expand All @@ -1267,23 +1269,12 @@ mod test {
}


#[auto_decode]
#[auto_encode]
struct Node {id: uint}

fn to_call_log (val: Encodable<TestEncoder>) -> ~[call] {
let mut te = TestEncoder {call_log: ~[]};
val.encode(&te);
te.call_log
}
/*
#[test] fn encode_test () {
check_equal (to_call_log(Node{id:34}
as Encodable::<std::json::Encoder>),
~[CallToEnum (~"Node"),
CallToEnumVariant]);
}
*/

#[auto_encode]
enum Written {
Book(uint,uint),
Expand All @@ -1300,4 +1291,17 @@ mod test {
CallToEmitEnumVariantArg (1),
CallToEmitUint (44)]);
}

pub enum BPos = uint;

#[auto_encode]
pub struct HasPos { pos : BPos }

#[test] fn encode_newtype_test () {
check_equal (to_call_log (HasPos {pos:BPos(48)}
as Encodable::<TestEncoder>),
~[CallToEmitStruct(~"HasPos",1),
CallToEmitField(~"pos",0),
CallToEmitUint(48)]);
}
}
Loading