From c1cc7e5f164b0119fcd60d6c9ade31fbfcff4b55 Mon Sep 17 00:00:00 2001
From: Kevin Ballard <kevin@sb.org>
Date: Mon, 16 Sep 2013 19:30:28 -0700
Subject: [PATCH 1/4] Add new syntax extension fourcc!()

fourcc!() allows you to embed FourCC (or OSType) values that are
evaluated as u32 literals. It takes a 4-byte ASCII string and produces
the u32 resulting in interpreting those 4 bytes as a u32, using either
the platform-native endianness, or explicitly as big or little endian.
---
 src/libsyntax/ext/base.rs                     |   3 +
 src/libsyntax/ext/fourcc.rs                   | 106 ++++++++++++++++++
 src/libsyntax/lib.rs                          |   1 +
 .../syntax-extension-fourcc-bad-len.rs        |  14 +++
 .../syntax-extension-fourcc-invalid-endian.rs |  13 +++
 .../syntax-extension-fourcc-non-ascii-str.rs  |  13 +++
 .../syntax-extension-fourcc-non-literal.rs    |  13 +++
 ...ax-extension-fourcc-unsupported-literal.rs |  13 +++
 src/test/run-pass/syntax-extension-fourcc.rs  |  30 +++++
 9 files changed, 206 insertions(+)
 create mode 100644 src/libsyntax/ext/fourcc.rs
 create mode 100644 src/test/compile-fail/syntax-extension-fourcc-bad-len.rs
 create mode 100644 src/test/compile-fail/syntax-extension-fourcc-invalid-endian.rs
 create mode 100644 src/test/compile-fail/syntax-extension-fourcc-non-ascii-str.rs
 create mode 100644 src/test/compile-fail/syntax-extension-fourcc-non-literal.rs
 create mode 100644 src/test/compile-fail/syntax-extension-fourcc-unsupported-literal.rs
 create mode 100644 src/test/run-pass/syntax-extension-fourcc.rs

diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs
index 848f4ba3871..bf240d8b47c 100644
--- a/src/libsyntax/ext/base.rs
+++ b/src/libsyntax/ext/base.rs
@@ -194,6 +194,9 @@ pub fn syntax_expander_table() -> SyntaxEnv {
     syntax_expanders.insert(intern("bytes"),
                             builtin_normal_expander(
                                     ext::bytes::expand_syntax_ext));
+    syntax_expanders.insert(intern("fourcc"),
+                            builtin_normal_tt_no_ctxt(
+                                    ext::fourcc::expand_syntax_ext));
     syntax_expanders.insert(intern("concat_idents"),
                             builtin_normal_expander(
                                     ext::concat_idents::expand_syntax_ext));
diff --git a/src/libsyntax/ext/fourcc.rs b/src/libsyntax/ext/fourcc.rs
new file mode 100644
index 00000000000..dd5452535a2
--- /dev/null
+++ b/src/libsyntax/ext/fourcc.rs
@@ -0,0 +1,106 @@
+// Copyright 2013 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.
+
+/* The compiler code necessary to support the fourcc! extension. */
+
+// fourcc!() is called with a single 4-character string, and an optional ident
+// that is either `big` or `little`. If the ident is omitted it is assumed to
+// be the platform-native value. It returns a u32.
+
+use ast;
+use attr::contains;
+use codemap::{Span, mk_sp};
+use ext::base::*;
+use ext::base;
+use ext::build::AstBuilder;
+use parse;
+use parse::token;
+
+use std::ascii::AsciiCast;
+
+pub fn expand_syntax_ext(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult {
+    let (expr, endian) = parse_tts(cx, tts);
+
+    let little = match endian {
+        None => target_endian_little(cx, sp),
+        Some(Ident{ident, span}) => match cx.str_of(ident).as_slice() {
+            "little" => true,
+            "big" => false,
+            _ => {
+                cx.span_err(span, "invalid endian directive in fourcc!");
+                target_endian_little(cx, sp)
+            }
+        }
+    };
+
+    let s = match expr.node {
+        // expression is a literal
+        ast::ExprLit(lit) => match lit.node {
+            // string literal
+            ast::lit_str(s) => {
+                if !s.is_ascii() {
+                    cx.span_err(expr.span, "non-ascii string literal in fourcc!");
+                } else if s.len() != 4 {
+                    cx.span_err(expr.span, "string literal with len != 4 in fourcc!");
+                }
+                s
+            }
+            _ => {
+                cx.span_err(expr.span, "unsupported literal in fourcc!");
+                return MRExpr(cx.expr_lit(sp, ast::lit_uint(0u64, ast::ty_u32)));
+            }
+        },
+        _ => {
+            cx.span_err(expr.span, "non-literal in fourcc!");
+            return MRExpr(cx.expr_lit(sp, ast::lit_uint(0u64, ast::ty_u32)));
+        }
+    };
+
+    let mut val = 0u32;
+    if little {
+        for byte in s.byte_rev_iter().take(4) {
+            val = (val << 8) | (byte as u32);
+        }
+    } else {
+        for byte in s.byte_iter().take(4) {
+            val = (val << 8) | (byte as u32);
+        }
+    }
+    let e = cx.expr_lit(sp, ast::lit_uint(val as u64, ast::ty_u32));
+    MRExpr(e)
+}
+
+struct Ident {
+    ident: ast::Ident,
+    span: Span
+}
+
+fn parse_tts(cx: @ExtCtxt, tts: &[ast::token_tree]) -> (@ast::Expr, Option<Ident>) {
+    let p = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), tts.to_owned());
+    let ex = p.parse_expr();
+    let id = if *p.token == token::EOF {
+        None
+    } else {
+        p.expect(&token::COMMA);
+        let lo = p.span.lo;
+        let ident = p.parse_ident();
+        let hi = p.last_span.hi;
+        Some(Ident{ident: ident, span: mk_sp(lo, hi)})
+    };
+    if *p.token != token::EOF {
+        p.unexpected();
+    }
+    (ex, id)
+}
+
+fn target_endian_little(cx: @ExtCtxt, sp: Span) -> bool {
+    let meta = cx.meta_name_value(sp, @"target_endian", ast::lit_str(@"little"));
+    contains(cx.cfg(), meta)
+}
diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs
index 613416bed1c..5999cf0d8fe 100644
--- a/src/libsyntax/lib.rs
+++ b/src/libsyntax/lib.rs
@@ -95,6 +95,7 @@ pub mod ext {
     pub mod bytes;
     pub mod concat;
     pub mod concat_idents;
+    pub mod fourcc;
     pub mod log_syntax;
     pub mod source_util;
 
diff --git a/src/test/compile-fail/syntax-extension-fourcc-bad-len.rs b/src/test/compile-fail/syntax-extension-fourcc-bad-len.rs
new file mode 100644
index 00000000000..11e2264001f
--- /dev/null
+++ b/src/test/compile-fail/syntax-extension-fourcc-bad-len.rs
@@ -0,0 +1,14 @@
+// Copyright 2013 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.
+
+fn main() {
+    let val = fourcc!("foo"); //~ ERROR string literal with len != 4 in fourcc!
+    let val2 = fourcc!("fooba"); //~ ERROR string literal with len != 4 in fourcc!
+}
diff --git a/src/test/compile-fail/syntax-extension-fourcc-invalid-endian.rs b/src/test/compile-fail/syntax-extension-fourcc-invalid-endian.rs
new file mode 100644
index 00000000000..ebad65ce740
--- /dev/null
+++ b/src/test/compile-fail/syntax-extension-fourcc-invalid-endian.rs
@@ -0,0 +1,13 @@
+// Copyright 2013 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.
+
+fn main() {
+    let val = fourcc!("foo ", bork); //~ ERROR invalid endian directive in fourcc!
+}
diff --git a/src/test/compile-fail/syntax-extension-fourcc-non-ascii-str.rs b/src/test/compile-fail/syntax-extension-fourcc-non-ascii-str.rs
new file mode 100644
index 00000000000..94a963298e2
--- /dev/null
+++ b/src/test/compile-fail/syntax-extension-fourcc-non-ascii-str.rs
@@ -0,0 +1,13 @@
+// Copyright 2013 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.
+
+fn main() {
+    let v = fourcc!("fooλ"); //~ ERROR non-ascii string literal in fourcc!
+}
diff --git a/src/test/compile-fail/syntax-extension-fourcc-non-literal.rs b/src/test/compile-fail/syntax-extension-fourcc-non-literal.rs
new file mode 100644
index 00000000000..baefd267e90
--- /dev/null
+++ b/src/test/compile-fail/syntax-extension-fourcc-non-literal.rs
@@ -0,0 +1,13 @@
+// Copyright 2013 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.
+
+fn main() {
+    let val = fourcc!(foo); //~ ERROR non-literal in fourcc!
+}
diff --git a/src/test/compile-fail/syntax-extension-fourcc-unsupported-literal.rs b/src/test/compile-fail/syntax-extension-fourcc-unsupported-literal.rs
new file mode 100644
index 00000000000..ee191a7d96d
--- /dev/null
+++ b/src/test/compile-fail/syntax-extension-fourcc-unsupported-literal.rs
@@ -0,0 +1,13 @@
+// Copyright 2013 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.
+
+fn main() {
+    let val = fourcc!(45f); //~ ERROR unsupported literal in fourcc!
+}
diff --git a/src/test/run-pass/syntax-extension-fourcc.rs b/src/test/run-pass/syntax-extension-fourcc.rs
new file mode 100644
index 00000000000..a5202144b77
--- /dev/null
+++ b/src/test/run-pass/syntax-extension-fourcc.rs
@@ -0,0 +1,30 @@
+// Copyright 2013 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.
+
+static static_val: u32 = fourcc!("foo ");
+static static_val_le: u32 = fourcc!("foo ", little);
+static static_val_be: u32 = fourcc!("foo ", big);
+
+fn main() {
+    let val = fourcc!("foo ");
+    let exp = if cfg!(target_endian = "big") { 0x666f6f20u32 } else { 0x206f6f66u32 };
+    assert_eq!(val, exp);
+
+    let val = fourcc!("foo ", big);
+    assert_eq!(val, 0x666f6f20u32);
+
+    let val = fourcc!("foo ", little);
+    assert_eq!(val, 0x206f6f66u32);
+
+    let exp = if cfg!(target_endian = "big") { 0x666f6f20u32 } else { 0x206f6f66u32 };
+    assert_eq!(static_val, exp);
+    assert_eq!(static_val_le, 0x206f6f66u32);
+    assert_eq!(static_val_be, 0x666f6f20u32);
+}

From 97078d43b20abc7510fde2e400500fed4c8b1eb3 Mon Sep 17 00:00:00 2001
From: Derek Guenther <dguenther9@gmail.com>
Date: Thu, 30 Jan 2014 19:05:04 -0600
Subject: [PATCH 2/4] Converted fourcc! to loadable syntax extension

---
 mk/crates.mk                                  |   3 +-
 src/libfourcc/lib.rs                          | 158 ++++++++++++++++++
 src/librustc/front/feature_gate.rs            |   5 +-
 src/libsyntax/ext/base.rs                     |   5 +-
 src/libsyntax/ext/fourcc.rs                   | 106 ------------
 src/libsyntax/lib.rs                          |   1 -
 .../syntax-extension-fourcc-bad-len.rs        |   9 +
 .../syntax-extension-fourcc-invalid-endian.rs |   9 +
 .../syntax-extension-fourcc-non-ascii-str.rs  |  11 +-
 .../syntax-extension-fourcc-non-literal.rs    |  11 +-
 ...ax-extension-fourcc-unsupported-literal.rs |  13 +-
 .../syntax-extension-fourcc.rs                |  12 +-
 12 files changed, 225 insertions(+), 118 deletions(-)
 create mode 100644 src/libfourcc/lib.rs
 delete mode 100644 src/libsyntax/ext/fourcc.rs
 rename src/test/{run-pass => run-pass-fulldeps}/syntax-extension-fourcc.rs (83%)

diff --git a/mk/crates.mk b/mk/crates.mk
index 05bc38193cd..81b2b390fa4 100644
--- a/mk/crates.mk
+++ b/mk/crates.mk
@@ -50,7 +50,7 @@
 ################################################################################
 
 TARGET_CRATES := std extra green rustuv native flate arena glob term semver \
-                 uuid serialize sync getopts collections
+                 uuid serialize sync getopts collections fourcc
 HOST_CRATES := syntax rustc rustdoc
 CRATES := $(TARGET_CRATES) $(HOST_CRATES)
 TOOLS := compiletest rustdoc rustc
@@ -74,6 +74,7 @@ DEPS_uuid := std serialize
 DEPS_sync := std
 DEPS_getopts := std
 DEPS_collections := std serialize
+DEPS_fourcc := syntax std
 
 TOOL_DEPS_compiletest := extra green rustuv getopts
 TOOL_DEPS_rustdoc := rustdoc green rustuv
diff --git a/src/libfourcc/lib.rs b/src/libfourcc/lib.rs
new file mode 100644
index 00000000000..aae47b87f07
--- /dev/null
+++ b/src/libfourcc/lib.rs
@@ -0,0 +1,158 @@
+// Copyright 2014 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.
+
+/*!
+Syntax extension to generate FourCCs.
+
+Once loaded, fourcc!() is called with a single 4-character string,
+and an optional ident that is either `big`, `little`, or `target`.
+The ident represents endianness, and specifies in which direction
+the characters should be read. If the ident is omitted, it is assumed
+to be `big`, i.e. left-to-right order. It returns a u32.
+
+# Examples
+
+To load the extension and use it:
+
+```rust,ignore
+#[phase(syntax)]
+extern mod fourcc;
+
+fn main() {
+    let val = fourcc!("\xC0\xFF\xEE!")
+    // val is 0xC0FFEE21
+    let big_val = fourcc!("foo ", big);
+    // big_val is 0x21EEFFC0
+}
+ ```
+
+# References
+
+* [Wikipedia: FourCC](http://en.wikipedia.org/wiki/FourCC)
+
+*/
+
+#[crate_id = "fourcc#0.10-pre"];
+#[crate_type = "rlib"];
+#[crate_type = "dylib"];
+#[license = "MIT/ASL2"];
+
+#[feature(macro_registrar, managed_boxes)];
+
+extern mod syntax;
+
+use syntax::ast;
+use syntax::ast::Name;
+use syntax::attr::contains;
+use syntax::codemap::{Span, mk_sp};
+use syntax::ext::base;
+use syntax::ext::base::{SyntaxExtension, BasicMacroExpander, NormalTT, ExtCtxt, MRExpr};
+use syntax::ext::build::AstBuilder;
+use syntax::parse;
+use syntax::parse::token;
+use syntax::parse::token::InternedString;
+
+#[macro_registrar]
+#[cfg(not(test))]
+pub fn macro_registrar(register: |Name, SyntaxExtension|) {
+    register(token::intern("fourcc"),
+        NormalTT(~BasicMacroExpander {
+            expander: expand_syntax_ext,
+            span: None,
+        },
+        None));
+}
+
+use std::ascii::AsciiCast;
+
+pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) -> base::MacResult {
+    let (expr, endian) = parse_tts(cx, tts);
+
+    let little = match endian {
+        None => target_endian_little(cx, sp),
+        Some(Ident{ident, span}) => match token::get_ident(ident.name).get() {
+            "little" => true,
+            "big" => false,
+            _ => {
+                cx.span_err(span, "invalid endian directive in fourcc!");
+                target_endian_little(cx, sp)
+            }
+        }
+    };
+
+    let s = match expr.node {
+        // expression is a literal
+        ast::ExprLit(lit) => match lit.node {
+            // string literal
+            ast::LitStr(ref s, _) => {
+                if !s.get().is_ascii() {
+                    cx.span_err(expr.span, "non-ascii string literal in fourcc!");
+                } else if s.get().len() != 4 {
+                    cx.span_err(expr.span, "string literal with len != 4 in fourcc!");
+                }
+                s
+            }
+            _ => {
+                cx.span_err(expr.span, "unsupported literal in fourcc!");
+                return MRExpr(cx.expr_lit(sp, ast::LitUint(0u64, ast::TyU32)));
+            }
+        },
+        _ => {
+            cx.span_err(expr.span, "non-literal in fourcc!");
+            return MRExpr(cx.expr_lit(sp, ast::LitUint(0u64, ast::TyU32)));
+        }
+    };
+
+    let mut val = 0u32;
+    if little {
+        for byte in s.get().bytes_rev().take(4) {
+            val = (val << 8) | (byte as u32);
+        }
+    } else {
+        for byte in s.get().bytes().take(4) {
+            val = (val << 8) | (byte as u32);
+        }
+    }
+    let e = cx.expr_lit(sp, ast::LitUint(val as u64, ast::TyU32));
+    MRExpr(e)
+}
+
+struct Ident {
+    ident: ast::Ident,
+    span: Span
+}
+
+fn parse_tts(cx: &ExtCtxt, tts: &[ast::TokenTree]) -> (@ast::Expr, Option<Ident>) {
+    let p = &mut parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), tts.to_owned());
+    let ex = p.parse_expr();
+    let id = if p.token == token::EOF {
+        None
+    } else {
+        p.expect(&token::COMMA);
+        let lo = p.span.lo;
+        let ident = p.parse_ident();
+        let hi = p.last_span.hi;
+        Some(Ident{ident: ident, span: mk_sp(lo, hi)})
+    };
+    if p.token != token::EOF {
+        p.unexpected();
+    }
+    (ex, id)
+}
+
+fn target_endian_little(cx: &ExtCtxt, sp: Span) -> bool {
+    let meta = cx.meta_name_value(sp, InternedString::new("target_endian"),
+        ast::LitStr(InternedString::new("little"), ast::CookedStr));
+    contains(cx.cfg(), meta)
+}
+
+// Fixes LLVM assert on Windows
+#[test]
+fn dummy_test() { }
diff --git a/src/librustc/front/feature_gate.rs b/src/librustc/front/feature_gate.rs
index 15056d9d2d8..f2e525932ad 100644
--- a/src/librustc/front/feature_gate.rs
+++ b/src/librustc/front/feature_gate.rs
@@ -210,10 +210,13 @@ impl Visitor<()> for Context {
             self.gate_feature("log_syntax", path.span, "`log_syntax!` is not \
                 stable enough for use and is subject to change");
         }
+
         else if id == self.sess.ident_of("trace_macros") {
             self.gate_feature("trace_macros", path.span, "`trace_macros` is not \
                 stable enough for use and is subject to change");
-        } else {
+        }
+
+        else {
             for &quote in quotes.iter() {
                 if id == self.sess.ident_of(quote) {
                   self.gate_feature("quote", path.span, quote + msg);
diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs
index bf240d8b47c..d22a1d697fc 100644
--- a/src/libsyntax/ext/base.rs
+++ b/src/libsyntax/ext/base.rs
@@ -1,4 +1,4 @@
-// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
+// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
 // file at the top-level directory of this distribution and at
 // http://rust-lang.org/COPYRIGHT.
 //
@@ -194,9 +194,6 @@ pub fn syntax_expander_table() -> SyntaxEnv {
     syntax_expanders.insert(intern("bytes"),
                             builtin_normal_expander(
                                     ext::bytes::expand_syntax_ext));
-    syntax_expanders.insert(intern("fourcc"),
-                            builtin_normal_tt_no_ctxt(
-                                    ext::fourcc::expand_syntax_ext));
     syntax_expanders.insert(intern("concat_idents"),
                             builtin_normal_expander(
                                     ext::concat_idents::expand_syntax_ext));
diff --git a/src/libsyntax/ext/fourcc.rs b/src/libsyntax/ext/fourcc.rs
deleted file mode 100644
index dd5452535a2..00000000000
--- a/src/libsyntax/ext/fourcc.rs
+++ /dev/null
@@ -1,106 +0,0 @@
-// Copyright 2013 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.
-
-/* The compiler code necessary to support the fourcc! extension. */
-
-// fourcc!() is called with a single 4-character string, and an optional ident
-// that is either `big` or `little`. If the ident is omitted it is assumed to
-// be the platform-native value. It returns a u32.
-
-use ast;
-use attr::contains;
-use codemap::{Span, mk_sp};
-use ext::base::*;
-use ext::base;
-use ext::build::AstBuilder;
-use parse;
-use parse::token;
-
-use std::ascii::AsciiCast;
-
-pub fn expand_syntax_ext(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult {
-    let (expr, endian) = parse_tts(cx, tts);
-
-    let little = match endian {
-        None => target_endian_little(cx, sp),
-        Some(Ident{ident, span}) => match cx.str_of(ident).as_slice() {
-            "little" => true,
-            "big" => false,
-            _ => {
-                cx.span_err(span, "invalid endian directive in fourcc!");
-                target_endian_little(cx, sp)
-            }
-        }
-    };
-
-    let s = match expr.node {
-        // expression is a literal
-        ast::ExprLit(lit) => match lit.node {
-            // string literal
-            ast::lit_str(s) => {
-                if !s.is_ascii() {
-                    cx.span_err(expr.span, "non-ascii string literal in fourcc!");
-                } else if s.len() != 4 {
-                    cx.span_err(expr.span, "string literal with len != 4 in fourcc!");
-                }
-                s
-            }
-            _ => {
-                cx.span_err(expr.span, "unsupported literal in fourcc!");
-                return MRExpr(cx.expr_lit(sp, ast::lit_uint(0u64, ast::ty_u32)));
-            }
-        },
-        _ => {
-            cx.span_err(expr.span, "non-literal in fourcc!");
-            return MRExpr(cx.expr_lit(sp, ast::lit_uint(0u64, ast::ty_u32)));
-        }
-    };
-
-    let mut val = 0u32;
-    if little {
-        for byte in s.byte_rev_iter().take(4) {
-            val = (val << 8) | (byte as u32);
-        }
-    } else {
-        for byte in s.byte_iter().take(4) {
-            val = (val << 8) | (byte as u32);
-        }
-    }
-    let e = cx.expr_lit(sp, ast::lit_uint(val as u64, ast::ty_u32));
-    MRExpr(e)
-}
-
-struct Ident {
-    ident: ast::Ident,
-    span: Span
-}
-
-fn parse_tts(cx: @ExtCtxt, tts: &[ast::token_tree]) -> (@ast::Expr, Option<Ident>) {
-    let p = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), tts.to_owned());
-    let ex = p.parse_expr();
-    let id = if *p.token == token::EOF {
-        None
-    } else {
-        p.expect(&token::COMMA);
-        let lo = p.span.lo;
-        let ident = p.parse_ident();
-        let hi = p.last_span.hi;
-        Some(Ident{ident: ident, span: mk_sp(lo, hi)})
-    };
-    if *p.token != token::EOF {
-        p.unexpected();
-    }
-    (ex, id)
-}
-
-fn target_endian_little(cx: @ExtCtxt, sp: Span) -> bool {
-    let meta = cx.meta_name_value(sp, @"target_endian", ast::lit_str(@"little"));
-    contains(cx.cfg(), meta)
-}
diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs
index 5999cf0d8fe..613416bed1c 100644
--- a/src/libsyntax/lib.rs
+++ b/src/libsyntax/lib.rs
@@ -95,7 +95,6 @@ pub mod ext {
     pub mod bytes;
     pub mod concat;
     pub mod concat_idents;
-    pub mod fourcc;
     pub mod log_syntax;
     pub mod source_util;
 
diff --git a/src/test/compile-fail/syntax-extension-fourcc-bad-len.rs b/src/test/compile-fail/syntax-extension-fourcc-bad-len.rs
index 11e2264001f..1fa198bb706 100644
--- a/src/test/compile-fail/syntax-extension-fourcc-bad-len.rs
+++ b/src/test/compile-fail/syntax-extension-fourcc-bad-len.rs
@@ -8,6 +8,15 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+// xfail-stage1
+// xfail-pretty
+// xfail-android
+
+#[feature(phase)];
+
+#[phase(syntax)]
+extern mod fourcc;
+
 fn main() {
     let val = fourcc!("foo"); //~ ERROR string literal with len != 4 in fourcc!
     let val2 = fourcc!("fooba"); //~ ERROR string literal with len != 4 in fourcc!
diff --git a/src/test/compile-fail/syntax-extension-fourcc-invalid-endian.rs b/src/test/compile-fail/syntax-extension-fourcc-invalid-endian.rs
index ebad65ce740..75a5483c6d7 100644
--- a/src/test/compile-fail/syntax-extension-fourcc-invalid-endian.rs
+++ b/src/test/compile-fail/syntax-extension-fourcc-invalid-endian.rs
@@ -8,6 +8,15 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+// xfail-stage1
+// xfail-pretty
+// xfail-android
+
+#[feature(phase)];
+
+#[phase(syntax)]
+extern mod fourcc;
+
 fn main() {
     let val = fourcc!("foo ", bork); //~ ERROR invalid endian directive in fourcc!
 }
diff --git a/src/test/compile-fail/syntax-extension-fourcc-non-ascii-str.rs b/src/test/compile-fail/syntax-extension-fourcc-non-ascii-str.rs
index 94a963298e2..18ec4547ddf 100644
--- a/src/test/compile-fail/syntax-extension-fourcc-non-ascii-str.rs
+++ b/src/test/compile-fail/syntax-extension-fourcc-non-ascii-str.rs
@@ -1,4 +1,4 @@
-// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
 // file at the top-level directory of this distribution and at
 // http://rust-lang.org/COPYRIGHT.
 //
@@ -8,6 +8,15 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+// xfail-stage1
+// xfail-pretty
+// xfail-android
+
+#[feature(phase)];
+
+#[phase(syntax)]
+extern mod fourcc;
+
 fn main() {
     let v = fourcc!("fooλ"); //~ ERROR non-ascii string literal in fourcc!
 }
diff --git a/src/test/compile-fail/syntax-extension-fourcc-non-literal.rs b/src/test/compile-fail/syntax-extension-fourcc-non-literal.rs
index baefd267e90..8e9dba30276 100644
--- a/src/test/compile-fail/syntax-extension-fourcc-non-literal.rs
+++ b/src/test/compile-fail/syntax-extension-fourcc-non-literal.rs
@@ -1,4 +1,4 @@
-// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
 // file at the top-level directory of this distribution and at
 // http://rust-lang.org/COPYRIGHT.
 //
@@ -8,6 +8,15 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+// xfail-stage1
+// xfail-pretty
+// xfail-android
+
+#[feature(phase)];
+
+#[phase(syntax)]
+extern mod fourcc;
+
 fn main() {
     let val = fourcc!(foo); //~ ERROR non-literal in fourcc!
 }
diff --git a/src/test/compile-fail/syntax-extension-fourcc-unsupported-literal.rs b/src/test/compile-fail/syntax-extension-fourcc-unsupported-literal.rs
index ee191a7d96d..65cc048bfb7 100644
--- a/src/test/compile-fail/syntax-extension-fourcc-unsupported-literal.rs
+++ b/src/test/compile-fail/syntax-extension-fourcc-unsupported-literal.rs
@@ -1,4 +1,4 @@
-// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
 // file at the top-level directory of this distribution and at
 // http://rust-lang.org/COPYRIGHT.
 //
@@ -8,6 +8,15 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+// xfail-stage1
+// xfail-pretty
+// xfail-android
+
+#[feature(phase)];
+
+#[phase(syntax)]
+extern mod fourcc;
+
 fn main() {
-    let val = fourcc!(45f); //~ ERROR unsupported literal in fourcc!
+    let val = fourcc!(45f32); //~ ERROR unsupported literal in fourcc!
 }
diff --git a/src/test/run-pass/syntax-extension-fourcc.rs b/src/test/run-pass-fulldeps/syntax-extension-fourcc.rs
similarity index 83%
rename from src/test/run-pass/syntax-extension-fourcc.rs
rename to src/test/run-pass-fulldeps/syntax-extension-fourcc.rs
index a5202144b77..0d46c28431d 100644
--- a/src/test/run-pass/syntax-extension-fourcc.rs
+++ b/src/test/run-pass-fulldeps/syntax-extension-fourcc.rs
@@ -1,4 +1,4 @@
-// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
 // file at the top-level directory of this distribution and at
 // http://rust-lang.org/COPYRIGHT.
 //
@@ -8,6 +8,16 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+// xfail-fast Feature gating doesn't work
+// xfail-stage1
+// xfail-pretty
+// xfail-android
+
+#[feature(phase)];
+
+#[phase(syntax)]
+extern mod fourcc;
+
 static static_val: u32 = fourcc!("foo ");
 static static_val_le: u32 = fourcc!("foo ", little);
 static static_val_be: u32 = fourcc!("foo ", big);

From 6381daab773ca97ef6553d4d244cc9a8f49475a4 Mon Sep 17 00:00:00 2001
From: Yuri Kunde Schlesner <yuriks@yuriks.net>
Date: Thu, 6 Feb 2014 17:41:49 -0200
Subject: [PATCH 3/4] Default fourcc! to big-endian.

It was decided that a consistent result across platforms would be the
most useful and least surprising. A "target" option has been added to
get the old behaviour of using the target platform's endianess.
---
 src/libfourcc/lib.rs                            |  3 ++-
 .../syntax-extension-fourcc.rs                  | 17 ++++++++++-------
 2 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/src/libfourcc/lib.rs b/src/libfourcc/lib.rs
index aae47b87f07..2b017b501c9 100644
--- a/src/libfourcc/lib.rs
+++ b/src/libfourcc/lib.rs
@@ -76,10 +76,11 @@ pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) ->
     let (expr, endian) = parse_tts(cx, tts);
 
     let little = match endian {
-        None => target_endian_little(cx, sp),
+        None => false,
         Some(Ident{ident, span}) => match token::get_ident(ident.name).get() {
             "little" => true,
             "big" => false,
+            "target" => target_endian_little(cx, sp),
             _ => {
                 cx.span_err(span, "invalid endian directive in fourcc!");
                 target_endian_little(cx, sp)
diff --git a/src/test/run-pass-fulldeps/syntax-extension-fourcc.rs b/src/test/run-pass-fulldeps/syntax-extension-fourcc.rs
index 0d46c28431d..be5e24d09d5 100644
--- a/src/test/run-pass-fulldeps/syntax-extension-fourcc.rs
+++ b/src/test/run-pass-fulldeps/syntax-extension-fourcc.rs
@@ -19,22 +19,25 @@
 extern mod fourcc;
 
 static static_val: u32 = fourcc!("foo ");
-static static_val_le: u32 = fourcc!("foo ", little);
 static static_val_be: u32 = fourcc!("foo ", big);
+static static_val_le: u32 = fourcc!("foo ", little);
+static static_val_target: u32 = fourcc!("foo ", target);
 
 fn main() {
-    let val = fourcc!("foo ");
-    let exp = if cfg!(target_endian = "big") { 0x666f6f20u32 } else { 0x206f6f66u32 };
-    assert_eq!(val, exp);
-
     let val = fourcc!("foo ", big);
     assert_eq!(val, 0x666f6f20u32);
+    assert_eq!(val, fourcc!("foo "));
 
     let val = fourcc!("foo ", little);
     assert_eq!(val, 0x206f6f66u32);
 
+    let val = fourcc!("foo ", target);
     let exp = if cfg!(target_endian = "big") { 0x666f6f20u32 } else { 0x206f6f66u32 };
-    assert_eq!(static_val, exp);
-    assert_eq!(static_val_le, 0x206f6f66u32);
+    assert_eq!(val, exp);
+
     assert_eq!(static_val_be, 0x666f6f20u32);
+    assert_eq!(static_val, static_val_be);
+    assert_eq!(static_val_le, 0x206f6f66u32);
+    let exp = if cfg!(target_endian = "big") { 0x666f6f20u32 } else { 0x206f6f66u32 };
+    assert_eq!(static_val_target, exp);
 }

From 337e62e4d65e24682ba323b65283a59715a0c8af Mon Sep 17 00:00:00 2001
From: Yuri Kunde Schlesner <yuriks@yuriks.net>
Date: Thu, 6 Feb 2014 19:14:42 -0200
Subject: [PATCH 4/4] Allow codepoints 128-255 in fourc!!

Codepoints with those values will be interpreted as bytes with their
raw codepoint value. ('\xAB' -> 0xABu8, etc.) Codepoints > 255 remain
forbidden.
---
 src/libfourcc/lib.rs                          | 27 ++++++++++---------
 .../syntax-extension-fourcc-non-ascii-str.rs  |  2 +-
 .../syntax-extension-fourcc.rs                |  2 ++
 3 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/src/libfourcc/lib.rs b/src/libfourcc/lib.rs
index 2b017b501c9..ea82d31bbe7 100644
--- a/src/libfourcc/lib.rs
+++ b/src/libfourcc/lib.rs
@@ -70,8 +70,6 @@ pub fn macro_registrar(register: |Name, SyntaxExtension|) {
         None));
 }
 
-use std::ascii::AsciiCast;
-
 pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) -> base::MacResult {
     let (expr, endian) = parse_tts(cx, tts);
 
@@ -93,9 +91,7 @@ pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) ->
         ast::ExprLit(lit) => match lit.node {
             // string literal
             ast::LitStr(ref s, _) => {
-                if !s.get().is_ascii() {
-                    cx.span_err(expr.span, "non-ascii string literal in fourcc!");
-                } else if s.get().len() != 4 {
+                if s.get().char_len() != 4 {
                     cx.span_err(expr.span, "string literal with len != 4 in fourcc!");
                 }
                 s
@@ -112,14 +108,19 @@ pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) ->
     };
 
     let mut val = 0u32;
-    if little {
-        for byte in s.get().bytes_rev().take(4) {
-            val = (val << 8) | (byte as u32);
-        }
-    } else {
-        for byte in s.get().bytes().take(4) {
-            val = (val << 8) | (byte as u32);
-        }
+    for codepoint in s.get().chars().take(4) {
+        let byte = if codepoint as u32 > 0xFF {
+            cx.span_err(expr.span, "fourcc! literal character out of range 0-255");
+            0u8
+        } else {
+            codepoint as u8
+        };
+
+        val = if little {
+            (val >> 8) | ((byte as u32) << 24)
+        } else {
+            (val << 8) | (byte as u32)
+        };
     }
     let e = cx.expr_lit(sp, ast::LitUint(val as u64, ast::TyU32));
     MRExpr(e)
diff --git a/src/test/compile-fail/syntax-extension-fourcc-non-ascii-str.rs b/src/test/compile-fail/syntax-extension-fourcc-non-ascii-str.rs
index 18ec4547ddf..5cc29b9d49f 100644
--- a/src/test/compile-fail/syntax-extension-fourcc-non-ascii-str.rs
+++ b/src/test/compile-fail/syntax-extension-fourcc-non-ascii-str.rs
@@ -18,5 +18,5 @@
 extern mod fourcc;
 
 fn main() {
-    let v = fourcc!("fooλ"); //~ ERROR non-ascii string literal in fourcc!
+    let v = fourcc!("fooλ"); //~ ERROR fourcc! literal character out of range 0-255
 }
diff --git a/src/test/run-pass-fulldeps/syntax-extension-fourcc.rs b/src/test/run-pass-fulldeps/syntax-extension-fourcc.rs
index be5e24d09d5..f22626fb3a1 100644
--- a/src/test/run-pass-fulldeps/syntax-extension-fourcc.rs
+++ b/src/test/run-pass-fulldeps/syntax-extension-fourcc.rs
@@ -40,4 +40,6 @@ fn main() {
     assert_eq!(static_val_le, 0x206f6f66u32);
     let exp = if cfg!(target_endian = "big") { 0x666f6f20u32 } else { 0x206f6f66u32 };
     assert_eq!(static_val_target, exp);
+
+    assert_eq!(fourcc!("\xC0\xFF\xEE!"), 0xC0FFEE21);
 }