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

Add new syntax extension fourcc!() #9255

Closed
wants to merge 1 commit into from
Closed

Conversation

lilyball
Copy link
Contributor

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-natiev endianness, or explicitly as big or little endian.

@lilyball
Copy link
Contributor Author

r? @pcwalton

let little = match endian {
None => cfg!(target_endian = "little"),
Some(Ident{ident, span}) => {
if ident.name == intern("little") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be cleaner as "little" == cx.str_of(ident) or even

match cx.str_of(ident).as_slice() {
   "little" => true,
   "big" => false,
   _ => { .. }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the match. I'll make that adjustment.

@lilyball
Copy link
Contributor Author

All of the comments have been addressed. r? @alexcrichton or @pcwalton


fn target_endian_little(cx: @ExtCtxt, sp: Span) -> bool {
let lit = Spanned{node: ast::lit_str(@"little"), span: sp};
let meta = @Spanned{node: ast::MetaNameValue(@"target_endian", lit), span: sp};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let meta = cx.meta_name_value(sp, @"target_endian", ast::lit_str(@"little"));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's a lot simpler.

@alexcrichton
Copy link
Member

The code itself looks fine to me, but I'm worried about feature creep in general. This is a great example of where dynamically loadable syntax extensions would be awesome, but I'm not sure that we need something like this by default for all crates in the compiler.

What do others think about inclusion?

@lilyball
Copy link
Contributor Author

This would be great for dynamically-loaded, I agree. I was motivated to add it because @pcwalton said yesterday in IRC that a feature like this could be used to speed up the compiler.

16:38 FourCC support would be great ;)
16:38 for porting rustc to use atom trees
16:39 'TRAI'u32 'TYPE'u32 'FUNC'u32 'ITEM'u32 'INDX'u32
16:39 I want these to replace all the tag_foo_bar we have
16:40 you laugh, but I think it would speed up the compiler :) vuint reading is high in the profile
16:40 can't get much faster than OSTypes

although @thestinger did indicate that syntax extensions can't be used in match at the moment. Also as you'll note @pcwalton did suggest something like 'TRAI'u32 as the syntax, so maybe this should be rewritten as a new type of literal using the char literal syntax (I already want to add support for things like 'a'u8 anyway).

@lilyball
Copy link
Contributor Author

After attempting to implement 'TRAI' as a valid 4-char constant, I don't think it's possible to do that given our use of ' for lifetimes. 'foo,'bar could be lexed as two comma-separated lifetimes, or as a 4-char constant followed by the characters "bar" (which is, of course, invalid). So I think fourcc!() is the only way to do this. Perhaps we can fix macros to support syntax extensions?

@brson
Copy link
Contributor

brson commented Sep 18, 2013

I'm not in favor of putting this special-purpose macro directly in the compiler. Any syntax extensions added now need to be specced and maintained forever.

@brson
Copy link
Contributor

brson commented Sep 18, 2013

Since this is for rustc's use maybe there's some mechanism or convention we can add to discourage this for general use.

@brson
Copy link
Contributor

brson commented Sep 18, 2013

We already need a way to opt in to macro_rules! so we could use that same mechanism to opt in to fourcc!.

@brson
Copy link
Contributor

brson commented Sep 19, 2013

cc #9304

@emberian
Copy link
Member

Needs a rebase.

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.
@alexcrichton
Copy link
Member

With compiler features now in-flight, I'd be more comfortable if this waited for that to land and then rebased on top of it with a feature-gate instead.

@lilyball
Copy link
Contributor Author

lilyball commented Oct 3, 2013

Ooh #[feature] looks cool. I'll wait for that.

@emberian
Copy link
Member

emberian commented Oct 3, 2013

(for the record the build was interrupted, I didn't read the GH comments, was just remembering IRC discussion)

bors added a commit that referenced this pull request Oct 4, 2013
This implements the necessary logic for gating particular features off by default in the compiler. There are a number of issues which have been wanting this form of mechanism, and this initially gates features which we have open issues for.

Additionally, this should unblock #9255
bors added a commit that referenced this pull request Oct 6, 2013
This implements the necessary logic for gating particular features off by default in the compiler. There are a number of issues which have been wanting this form of mechanism, and this initially gates features which we have open issues for.

Additionally, this should unblock #9255
@alexcrichton
Copy link
Member

Closing due to inactivity, but feel free to reopen if more progress is made!

@dguenther dguenther mentioned this pull request Feb 4, 2014
4 tasks
bors added a commit that referenced this pull request Feb 9, 2014
I was looking into #9303 and was curious if this would still be valuable. @kballard had already done 99% of the work, so I brought the branch up to date and added a feature gate. Any feedback would be appreciated; I wasn't sure if this should be set up as a syntax extension with `#[macro_registrar]`, and if so, where it should be located.

Original PR is here: #9255

TODO:
* [x] Convert to loadable syntax extension
* [x] Default to big endian
* [x] Add `target` identifier
* [x] Expand to include code points 128-255
@lilyball lilyball deleted the fourcc branch February 15, 2014 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants