Skip to content

Commit 07a994b

Browse files
committed
---
yaml --- r: 149854 b: refs/heads/try2 c: b63cd00 h: refs/heads/master v: v3
1 parent 78f4d3b commit 07a994b

35 files changed

+469
-117
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 952380904b8e925851d96b55a959232ef360aede
8+
refs/heads/try2: b63cd004fc22089fbec7d74fc5a54536d093b1b4
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/mk/crates.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
TARGET_CRATES := std extra green rustuv native flate arena glob term semver \
5353
uuid serialize sync getopts collections num test time
54-
HOST_CRATES := syntax rustc rustdoc fourcc
54+
HOST_CRATES := syntax rustc rustdoc fourcc hexfloat
5555
CRATES := $(TARGET_CRATES) $(HOST_CRATES)
5656
TOOLS := compiletest rustdoc rustc
5757

@@ -76,6 +76,7 @@ DEPS_sync := std
7676
DEPS_getopts := std
7777
DEPS_collections := std
7878
DEPS_fourcc := syntax std
79+
DEPS_hexfloat := syntax std
7980
DEPS_num := std
8081
DEPS_test := std extra collections getopts serialize term
8182
DEPS_time := std serialize

branches/try2/src/libhexfloat/lib.rs

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
/*!
12+
Syntax extension to create floating point literals from hexadecimal strings
13+
14+
Once loaded, hexfloat!() is called with a string containing the hexadecimal
15+
floating-point literal, and an optional type (f32 or f64).
16+
If the type is omitted, the literal is treated the same as a normal unsuffixed
17+
literal.
18+
19+
# Examples
20+
21+
To load the extension and use it:
22+
23+
```rust,ignore
24+
#[phase(syntax)]
25+
extern crate hexfloat;
26+
27+
fn main() {
28+
let val = hexfloat!("0x1.ffffb4", f32);
29+
}
30+
```
31+
32+
# References
33+
34+
* [ExploringBinary: hexadecimal floating point constants]
35+
(http://www.exploringbinary.com/hexadecimal-floating-point-constants/)
36+
37+
*/
38+
39+
#[crate_id = "hexfloat#0.10-pre"];
40+
#[crate_type = "rlib"];
41+
#[crate_type = "dylib"];
42+
#[license = "MIT/ASL2"];
43+
44+
#[feature(macro_registrar, managed_boxes)];
45+
46+
extern crate syntax;
47+
48+
use syntax::ast;
49+
use syntax::ast::Name;
50+
use syntax::codemap::{Span, mk_sp};
51+
use syntax::ext::base;
52+
use syntax::ext::base::{SyntaxExtension, BasicMacroExpander, NormalTT, ExtCtxt, MRExpr};
53+
use syntax::ext::build::AstBuilder;
54+
use syntax::parse;
55+
use syntax::parse::token;
56+
57+
#[macro_registrar]
58+
pub fn macro_registrar(register: |Name, SyntaxExtension|) {
59+
register(token::intern("hexfloat"),
60+
NormalTT(~BasicMacroExpander {
61+
expander: expand_syntax_ext,
62+
span: None,
63+
},
64+
None));
65+
}
66+
67+
//Check if the literal is valid (as LLVM expects),
68+
//and return a descriptive error if not.
69+
fn hex_float_lit_err(s: &str) -> Option<(uint, ~str)> {
70+
let mut chars = s.chars().peekable();
71+
let mut i = 0;
72+
if chars.peek() == Some(&'-') { chars.next(); i+= 1 }
73+
if chars.next() != Some('0') { return Some((i, ~"Expected '0'")); } i+=1;
74+
if chars.next() != Some('x') { return Some((i, ~"Expected 'x'")); } i+=1;
75+
let mut d_len = 0;
76+
for _ in chars.take_while(|c| c.is_digit_radix(16)) { chars.next(); i+=1; d_len += 1;}
77+
if chars.next() != Some('.') { return Some((i, ~"Expected '.'")); } i+=1;
78+
let mut f_len = 0;
79+
for _ in chars.take_while(|c| c.is_digit_radix(16)) { chars.next(); i+=1; f_len += 1;}
80+
if d_len == 0 && f_len == 0 {
81+
return Some((i, ~"Expected digits before or after decimal point"));
82+
}
83+
if chars.next() != Some('p') { return Some((i, ~"Expected 'p'")); } i+=1;
84+
if chars.peek() == Some(&'-') { chars.next(); i+= 1 }
85+
let mut e_len = 0;
86+
for _ in chars.take_while(|c| c.is_digit()) { chars.next(); i+=1; e_len += 1}
87+
if e_len == 0 {
88+
return Some((i, ~"Expected exponent digits"));
89+
}
90+
match chars.next() {
91+
None => None,
92+
Some(_) => Some((i, ~"Expected end of string"))
93+
}
94+
}
95+
96+
pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) -> base::MacResult {
97+
let (expr, ty_lit) = parse_tts(cx, tts);
98+
99+
let ty = match ty_lit {
100+
None => None,
101+
Some(Ident{ident, span}) => match token::get_ident(ident).get() {
102+
"f32" => Some(ast::TyF32),
103+
"f64" => Some(ast::TyF64),
104+
_ => {
105+
cx.span_err(span, "invalid floating point type in hexfloat!");
106+
None
107+
}
108+
}
109+
};
110+
111+
let s = match expr.node {
112+
// expression is a literal
113+
ast::ExprLit(lit) => match lit.node {
114+
// string literal
115+
ast::LitStr(ref s, _) => {
116+
s.clone()
117+
}
118+
_ => {
119+
cx.span_err(expr.span, "unsupported literal in hexfloat!");
120+
return base::MacResult::dummy_expr(sp);
121+
}
122+
},
123+
_ => {
124+
cx.span_err(expr.span, "non-literal in hexfloat!");
125+
return base::MacResult::dummy_expr(sp);
126+
}
127+
};
128+
129+
{
130+
let err = hex_float_lit_err(s.get());
131+
match err {
132+
Some((err_pos, err_str)) => {
133+
let pos = expr.span.lo + syntax::codemap::Pos::from_uint(err_pos + 1);
134+
let span = syntax::codemap::mk_sp(pos,pos);
135+
cx.span_err(span, format!("invalid hex float literal in hexfloat!: {}", err_str));
136+
return base::MacResult::dummy_expr(sp);
137+
}
138+
_ => ()
139+
}
140+
}
141+
142+
let lit = match ty {
143+
None => ast::LitFloatUnsuffixed(s),
144+
Some (ty) => ast::LitFloat(s, ty)
145+
};
146+
MRExpr(cx.expr_lit(sp, lit))
147+
}
148+
149+
struct Ident {
150+
ident: ast::Ident,
151+
span: Span
152+
}
153+
154+
fn parse_tts(cx: &ExtCtxt, tts: &[ast::TokenTree]) -> (@ast::Expr, Option<Ident>) {
155+
let p = &mut parse::new_parser_from_tts(cx.parse_sess(),
156+
cx.cfg(),
157+
tts.iter()
158+
.map(|x| (*x).clone())
159+
.collect());
160+
let ex = p.parse_expr();
161+
let id = if p.token == token::EOF {
162+
None
163+
} else {
164+
p.expect(&token::COMMA);
165+
let lo = p.span.lo;
166+
let ident = p.parse_ident();
167+
let hi = p.last_span.hi;
168+
Some(Ident{ident: ident, span: mk_sp(lo, hi)})
169+
};
170+
if p.token != token::EOF {
171+
p.unexpected();
172+
}
173+
(ex, id)
174+
}
175+
176+
// FIXME (10872): This is required to prevent an LLVM assert on Windows
177+
#[test]
178+
fn dummy_test() { }

branches/try2/src/librustc/middle/borrowck/gather_loans/gather_moves.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,22 @@ pub fn gather_assignment(bccx: &BorrowckCtxt,
9494
assignee_loan_path,
9595
assignment_id,
9696
assignment_span,
97-
assignee_id);
97+
assignee_id,
98+
false);
99+
}
100+
101+
pub fn gather_move_and_assignment(bccx: &BorrowckCtxt,
102+
move_data: &MoveData,
103+
assignment_id: ast::NodeId,
104+
assignment_span: Span,
105+
assignee_loan_path: @LoanPath,
106+
assignee_id: ast::NodeId) {
107+
move_data.add_assignment(bccx.tcx,
108+
assignee_loan_path,
109+
assignment_id,
110+
assignment_span,
111+
assignee_id,
112+
true);
98113
}
99114

100115
fn check_is_legal_to_move_from(bccx: &BorrowckCtxt,

branches/try2/src/librustc/middle/borrowck/gather_loans/mod.rs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -214,20 +214,19 @@ fn gather_loans_in_expr(this: &mut GatherLoanCtxt,
214214
visit::walk_expr(this, ex, ());
215215
}
216216

217-
ast::ExprAssign(l, _) | ast::ExprAssignOp(_, l, _) => {
218-
let l_cmt = this.bccx.cat_expr(l);
219-
match opt_loan_path(l_cmt) {
220-
Some(l_lp) => {
221-
gather_moves::gather_assignment(this.bccx, &this.move_data,
222-
ex.id, ex.span,
223-
l_lp, l.id);
224-
}
225-
None => {
226-
// This can occur with e.g. `*foo() = 5`. In such
227-
// cases, there is no need to check for conflicts
228-
// with moves etc, just ignore.
229-
}
230-
}
217+
ast::ExprAssign(l, _) => {
218+
with_assignee_loan_path(
219+
this.bccx, l,
220+
|lp| gather_moves::gather_assignment(this.bccx, &this.move_data,
221+
ex.id, ex.span, lp, l.id));
222+
visit::walk_expr(this, ex, ());
223+
}
224+
225+
ast::ExprAssignOp(_, l, _) => {
226+
with_assignee_loan_path(
227+
this.bccx, l,
228+
|lp| gather_moves::gather_move_and_assignment(this.bccx, &this.move_data,
229+
ex.id, ex.span, lp, l.id));
231230
visit::walk_expr(this, ex, ());
232231
}
233232

@@ -288,17 +287,10 @@ fn gather_loans_in_expr(this: &mut GatherLoanCtxt,
288287

289288
ast::ExprInlineAsm(ref ia) => {
290289
for &(_, out) in ia.outputs.iter() {
291-
let out_cmt = this.bccx.cat_expr(out);
292-
match opt_loan_path(out_cmt) {
293-
Some(out_lp) => {
294-
gather_moves::gather_assignment(this.bccx, &this.move_data,
295-
ex.id, ex.span,
296-
out_lp, out.id);
297-
}
298-
None => {
299-
// See the comment for ExprAssign.
300-
}
301-
}
290+
with_assignee_loan_path(
291+
this.bccx, out,
292+
|lp| gather_moves::gather_assignment(this.bccx, &this.move_data,
293+
ex.id, ex.span, lp, out.id));
302294
}
303295
visit::walk_expr(this, ex, ());
304296
}
@@ -309,6 +301,18 @@ fn gather_loans_in_expr(this: &mut GatherLoanCtxt,
309301
}
310302
}
311303

304+
fn with_assignee_loan_path(bccx: &BorrowckCtxt, expr: &ast::Expr, op: |@LoanPath|) {
305+
let cmt = bccx.cat_expr(expr);
306+
match opt_loan_path(cmt) {
307+
Some(lp) => op(lp),
308+
None => {
309+
// This can occur with e.g. `*foo() = 5`. In such
310+
// cases, there is no need to check for conflicts
311+
// with moves etc, just ignore.
312+
}
313+
}
314+
}
315+
312316
impl<'a> GatherLoanCtxt<'a> {
313317
pub fn tcx(&self) -> ty::ctxt { self.bccx.tcx }
314318

branches/try2/src/librustc/middle/borrowck/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ impl BorrowckCtxt {
535535
move_data::Declared => {
536536
self.tcx.sess.span_err(
537537
use_span,
538-
format!("{} of possibly uninitialized value: `{}`",
538+
format!("{} of possibly uninitialized variable: `{}`",
539539
verb,
540540
self.loan_path_to_str(lp)));
541541
}

branches/try2/src/librustc/middle/borrowck/move_data.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,25 @@ use util::ppaux::Repr;
3333

3434
pub struct MoveData {
3535
/// Move paths. See section "Move paths" in `doc.rs`.
36-
paths: RefCell<Vec<MovePath> >,
36+
paths: RefCell<Vec<MovePath>>,
3737

3838
/// Cache of loan path to move path index, for easy lookup.
3939
path_map: RefCell<HashMap<@LoanPath, MovePathIndex>>,
4040

4141
/// Each move or uninitialized variable gets an entry here.
42-
moves: RefCell<Vec<Move> >,
42+
moves: RefCell<Vec<Move>>,
4343

4444
/// Assignments to a variable, like `x = foo`. These are assigned
4545
/// bits for dataflow, since we must track them to ensure that
4646
/// immutable variables are assigned at most once along each path.
47-
var_assignments: RefCell<Vec<Assignment> >,
47+
var_assignments: RefCell<Vec<Assignment>>,
4848

4949
/// Assignments to a path, like `x.f = foo`. These are not
5050
/// assigned dataflow bits, but we track them because they still
5151
/// kill move bits.
52-
path_assignments: RefCell<Vec<Assignment> >,
52+
path_assignments: RefCell<Vec<Assignment>>,
53+
54+
/// Assignments to a variable or path, like `x = foo`, but not `x += foo`.
5355
assignee_ids: RefCell<HashSet<ast::NodeId>>,
5456
}
5557

@@ -392,7 +394,8 @@ impl MoveData {
392394
lp: @LoanPath,
393395
assign_id: ast::NodeId,
394396
span: Span,
395-
assignee_id: ast::NodeId) {
397+
assignee_id: ast::NodeId,
398+
is_also_move: bool) {
396399
/*!
397400
* Adds a new record for an assignment to `lp` that occurs at
398401
* location `id` with the given `span`.
@@ -403,7 +406,7 @@ impl MoveData {
403406

404407
let path_index = self.move_path(tcx, lp);
405408

406-
{
409+
if !is_also_move {
407410
let mut assignee_ids = self.assignee_ids.borrow_mut();
408411
assignee_ids.get().insert(assignee_id);
409412
}

0 commit comments

Comments
 (0)