Make span copyable (#1314)

* Make span copyable

* Use fixed size integers for span
This commit is contained in:
João Capucho 2021-09-10 20:52:45 +01:00 committed by GitHub
parent 439148b1a7
commit 130f802c89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 241 additions and 237 deletions

View File

@ -260,15 +260,18 @@ impl<T> Arena<T> {
self.data.clear()
}
pub fn get_span(&self, handle: Handle<T>) -> &Span {
pub fn get_span(&self, handle: Handle<T>) -> Span {
#[cfg(feature = "span")]
{
return self.span_info.get(handle.index()).unwrap_or(&Span::Unknown);
*self
.span_info
.get(handle.index())
.unwrap_or(&Span::default())
}
#[cfg(not(feature = "span"))]
{
let _ = handle;
&Span::Unknown
Span::default()
}
}
}
@ -284,7 +287,9 @@ where
{
let data = Vec::deserialize(deserializer)?;
#[cfg(feature = "span")]
let span_info = std::iter::repeat(Span::Unknown).take(data.len()).collect();
let span_info = std::iter::repeat(Span::default())
.take(data.len())
.collect();
Ok(Self {
data,

View File

@ -23,7 +23,9 @@ impl Block {
pub fn from_vec(body: Vec<Statement>) -> Self {
#[cfg(feature = "span")]
let span_info = std::iter::repeat(Span::Unknown).take(body.len()).collect();
let span_info = std::iter::repeat(Span::default())
.take(body.len())
.collect();
Self {
body,
#[cfg(feature = "span")]

View File

@ -21,7 +21,7 @@ impl Module {
name: None,
inner: arg,
},
Span::Unknown,
Span::default(),
));
parameters_info.push(ParameterInfo {
qualifier: ParameterQualifier::In,
@ -1190,7 +1190,7 @@ fn inject_common_builtin(
},
},
},
Span::Unknown,
Span::default(),
);
let parameters = vec![ty, ty];
@ -1340,7 +1340,6 @@ fn inject_common_builtin(
}
}
"smoothstep" => {
// bits layout
// bit 0 - splatted
// bit 1 trough 2 - dims
for bits in 0..(0b1000) {
@ -1624,7 +1623,7 @@ impl MacroCall {
value: ScalarValue::Float(value),
},
},
Span::Unknown,
Span::default(),
);
let right = ctx.add_expression(
Expression::Constant(constant),
@ -1832,7 +1831,7 @@ pub fn sampled_to_depth(
class: ImageClass::Depth { multi },
},
},
Span::Unknown,
Span::default(),
)
}
ImageClass::Depth { .. } => {}

View File

@ -64,7 +64,7 @@ impl<'a> ConstantSolver<'a> {
&mut self,
expr: Handle<Expression>,
) -> Result<Handle<Constant>, ConstantSolvingError> {
let span = self.expressions.get_span(expr).clone();
let span = self.expressions.get_span(expr);
match self.expressions[expr] {
Expression::Constant(constant) => Ok(constant),
Expression::AccessIndex { base, index } => self.access(base, index as usize),
@ -338,7 +338,7 @@ impl<'a> ConstantSolver<'a> {
}
for component in components {
*component = self.cast(*component, kind, target_width, span.clone())?;
*component = self.cast(*component, kind, target_width, span)?;
}
}
}
@ -385,7 +385,7 @@ impl<'a> ConstantSolver<'a> {
}
for component in components {
*component = self.unary_op(op, *component, span.clone())?
*component = self.unary_op(op, *component, span)?
}
}
}
@ -486,14 +486,14 @@ impl<'a> ConstantSolver<'a> {
(&ConstantInner::Composite { ref components, ty }, &ConstantInner::Scalar { .. }) => {
let mut components = components.clone();
for comp in components.iter_mut() {
*comp = self.binary_op(op, *comp, right, span.clone())?;
*comp = self.binary_op(op, *comp, right, span)?;
}
ConstantInner::Composite { ty, components }
}
(&ConstantInner::Scalar { .. }, &ConstantInner::Composite { ref components, ty }) => {
let mut components = components.clone();
for comp in components.iter_mut() {
*comp = self.binary_op(op, left, *comp, span.clone())?;
*comp = self.binary_op(op, left, *comp, span)?;
}
ConstantInner::Composite { ty, components }
}

View File

@ -101,7 +101,7 @@ impl Context {
self.emit_flush(body);
let (expr, load) = match kind {
GlobalLookupKind::Variable(v) => {
let span = parser.module.global_variables.get_span(v).clone();
let span = parser.module.global_variables.get_span(v);
let res = (
self.expressions.append(Expression::GlobalVariable(v), span),
parser.module.global_variables[v].class != StorageClass::Handle,
@ -111,10 +111,10 @@ impl Context {
res
}
GlobalLookupKind::BlockSelect(handle, index) => {
let span = parser.module.global_variables.get_span(handle).clone();
let span = parser.module.global_variables.get_span(handle);
let base = self
.expressions
.append(Expression::GlobalVariable(handle), span.clone());
.append(Expression::GlobalVariable(handle), span);
self.emit_start();
let expr = self
.expressions
@ -140,7 +140,7 @@ impl Context {
})
}
GlobalLookupKind::Constant(v) => {
let span = parser.module.constants.get_span(v).clone();
let span = parser.module.constants.get_span(v);
let res = (
self.expressions.append(Expression::Constant(v), span),
false,
@ -245,7 +245,7 @@ impl Context {
};
if qualifier.is_lhs() {
let span = parser.module.types.get_span(arg.ty).clone();
let span = parser.module.types.get_span(arg.ty);
arg.ty = parser.module.types.fetch_or_append(
Type {
name: None,

View File

@ -578,7 +578,7 @@ impl Parser {
name: None,
inner: TypeInner::Vector { size, kind, width },
},
Span::Unknown,
Span::default(),
);
let temp_var = ctx.locals.append(
LocalVariable {
@ -586,7 +586,7 @@ impl Parser {
ty,
init: None,
},
Span::Unknown,
Span::default(),
);
let temp_expr = ctx.add_expression(
Expression::LocalVariable(temp_var),
@ -599,7 +599,7 @@ impl Parser {
pointer: temp_expr,
value: handle,
},
Span::Unknown,
Span::default(),
);
arguments.push(temp_expr);

View File

@ -84,7 +84,7 @@ pub fn calculate_offset(
crate::ArraySize::Dynamic => stride,
};
let ty_span = types.get_span(ty).clone();
let ty_span = types.get_span(ty);
ty = types.fetch_or_append(
Type {
name,
@ -144,7 +144,7 @@ pub fn calculate_offset(
span = align_up(span, align);
let ty_span = types.get_span(ty).clone();
let ty_span = types.get_span(ty);
ty = types.fetch_or_append(
Type {
name,

View File

@ -55,7 +55,7 @@ impl<'source> ParsingContext<'source> {
let new_break = || {
let mut block = Block::new();
block.push(Statement::Break, crate::Span::Unknown);
block.push(Statement::Break, crate::Span::default());
block
};
@ -332,7 +332,7 @@ impl<'source> ParsingContext<'source> {
accept: new_break(),
reject: Block::new(),
},
crate::Span::Unknown,
crate::Span::default(),
);
let mut meta = meta.union(&expr_meta);
@ -389,7 +389,7 @@ impl<'source> ParsingContext<'source> {
accept: new_break(),
reject: Block::new(),
},
crate::Span::Unknown,
crate::Span::default(),
);
body.push(
@ -474,7 +474,7 @@ impl<'source> ParsingContext<'source> {
accept: new_break(),
reject: Block::new(),
},
crate::Span::Unknown,
crate::Span::default(),
);
self.expect(parser, TokenValue::Semicolon)?;

View File

@ -57,7 +57,7 @@ impl SourceMetadata {
}
pub fn as_span(&self) -> crate::Span {
crate::Span::ByteRange(self.start..self.end)
crate::Span::new(self.start as u32, self.end as u32)
}
pub(crate) fn none() -> Self {

View File

@ -36,7 +36,7 @@ impl Emitter {
let start_len = self.start_len.take().unwrap();
if start_len != arena.len() {
#[allow(unused_mut)]
let mut span = crate::span::Span::Unknown;
let mut span = crate::span::Span::default();
let range = arena.range_from(start_len);
#[cfg(feature = "span")]
for handle in range.clone() {

View File

@ -176,7 +176,7 @@ impl<I: Iterator<Item = u32>> super::Parser<I> {
// Get a pointer to the local variable for the phi's value.
let phi_pointer = fun.expressions.append(
crate::Expression::LocalVariable(phi.local),
crate::Span::Unknown,
crate::Span::default(),
);
// At the end of each of `phi`'s predecessor blocks, store the corresponding
@ -219,26 +219,26 @@ impl<I: Iterator<Item = u32>> super::Parser<I> {
ty,
init: None,
},
crate::Span::Unknown,
crate::Span::default(),
);
let pointer = fun.expressions.append(
crate::Expression::LocalVariable(local),
crate::Span::Unknown,
crate::Span::default(),
);
// Get the spilled value of the source expression.
let start = fun.expressions.len();
let expr = fun
.expressions
.append(crate::Expression::Load { pointer }, crate::Span::Unknown);
.append(crate::Expression::Load { pointer }, crate::Span::default());
let range = fun.expressions.range_from(start);
block_ctx
.blocks
.get_mut(&predecessor)
.unwrap()
.push(crate::Statement::Emit(range), crate::Span::Unknown);
.push(crate::Statement::Emit(range), crate::Span::default());
// At the end of the block that defines it, spill the source
// expression's value.
@ -251,7 +251,7 @@ impl<I: Iterator<Item = u32>> super::Parser<I> {
pointer,
value: source_lexp.handle,
},
crate::Span::Unknown,
crate::Span::default(),
);
expr
@ -264,7 +264,7 @@ impl<I: Iterator<Item = u32>> super::Parser<I> {
pointer: phi_pointer,
value,
},
crate::Span::Unknown,
crate::Span::default(),
)
}
}
@ -297,10 +297,10 @@ impl<I: Iterator<Item = u32>> super::Parser<I> {
for &v_id in ep.variable_ids.iter() {
let lvar = self.lookup_variable.lookup(v_id)?;
if let super::Variable::Input(ref arg) = lvar.inner {
let span = module.global_variables.get_span(lvar.handle).clone();
let span = module.global_variables.get_span(lvar.handle);
let arg_expr = function.expressions.append(
crate::Expression::FunctionArgument(function.arguments.len() as u32),
span.clone(),
span,
);
let load_expr = if arg.ty == module.global_variables[lvar.handle].ty {
arg_expr
@ -315,17 +315,16 @@ impl<I: Iterator<Item = u32>> super::Parser<I> {
kind: crate::ScalarKind::Sint,
convert: Some(4),
},
span.clone(),
span,
);
function.body.extend(emitter.finish(&function.expressions));
handle
};
function.body.push(
crate::Statement::Store {
pointer: function.expressions.append(
crate::Expression::GlobalVariable(lvar.handle),
span.clone(),
),
pointer: function
.expressions
.append(crate::Expression::GlobalVariable(lvar.handle), span),
value: load_expr,
},
span,
@ -354,7 +353,7 @@ impl<I: Iterator<Item = u32>> super::Parser<I> {
arguments: Vec::new(),
result: None,
},
crate::Span::Unknown,
crate::Span::default(),
);
// 3. copy the outputs from privates to the result
@ -363,10 +362,10 @@ impl<I: Iterator<Item = u32>> super::Parser<I> {
for &v_id in ep.variable_ids.iter() {
let lvar = self.lookup_variable.lookup(v_id)?;
if let super::Variable::Output(ref result) = lvar.inner {
let span = module.global_variables.get_span(lvar.handle).clone();
let span = module.global_variables.get_span(lvar.handle);
let expr_handle = function
.expressions
.append(crate::Expression::GlobalVariable(lvar.handle), span.clone());
.append(crate::Expression::GlobalVariable(lvar.handle), span);
match module.types[result.ty].inner {
crate::TypeInner::Struct {
members: ref sub_members,
@ -383,7 +382,7 @@ impl<I: Iterator<Item = u32>> super::Parser<I> {
base: expr_handle,
index: index as u32,
},
span.clone(),
span,
));
}
}
@ -410,26 +409,26 @@ impl<I: Iterator<Item = u32>> super::Parser<I> {
let mut emitter = Emitter::default();
emitter.start(&function.expressions);
let global_expr = components[member_index];
let span = function.expressions.get_span(global_expr).clone();
let span = function.expressions.get_span(global_expr);
let access_expr = function.expressions.append(
crate::Expression::AccessIndex {
base: global_expr,
index: 1,
},
span.clone(),
span,
);
let load_expr = function.expressions.append(
crate::Expression::Load {
pointer: access_expr,
},
span.clone(),
span,
);
let neg_expr = function.expressions.append(
crate::Expression::Unary {
op: crate::UnaryOperator::Negate,
expr: load_expr,
},
span.clone(),
span,
);
function.body.extend(emitter.finish(&function.expressions));
function.body.push(
@ -450,7 +449,7 @@ impl<I: Iterator<Item = u32>> super::Parser<I> {
let load_expr = crate::Expression::Load {
pointer: *component,
};
let span = function.expressions.get_span(*component).clone();
let span = function.expressions.get_span(*component);
*component = function.expressions.append(load_expr, span);
}
@ -458,7 +457,7 @@ impl<I: Iterator<Item = u32>> super::Parser<I> {
[] => {}
[member] => {
function.body.extend(emitter.finish(&function.expressions));
let span = function.expressions.get_span(components[0]).clone();
let span = function.expressions.get_span(components[0]);
function.body.push(
crate::Statement::Return {
value: components.first().cloned(),
@ -483,11 +482,11 @@ impl<I: Iterator<Item = u32>> super::Parser<I> {
span: 0xFFFF, // shouldn't matter
},
},
span.clone(),
span,
);
let result_expr = function
.expressions
.append(crate::Expression::Compose { ty, components }, span.clone());
.append(crate::Expression::Compose { ty, components }, span);
function.body.extend(emitter.finish(&function.expressions));
function.body.push(
crate::Statement::Return {
@ -541,7 +540,7 @@ impl BlockContext {
accept,
reject,
},
crate::Span::Unknown,
crate::Span::default(),
)
}
super::BodyFragment::Loop { body, continuing } => {
@ -550,7 +549,7 @@ impl BlockContext {
block.push(
crate::Statement::Loop { body, continuing },
crate::Span::Unknown,
crate::Span::default(),
)
}
super::BodyFragment::Switch {
@ -578,14 +577,14 @@ impl BlockContext {
.collect(),
default,
},
crate::Span::Unknown,
crate::Span::default(),
)
}
super::BodyFragment::Break => {
block.push(crate::Statement::Break, crate::Span::Unknown)
block.push(crate::Statement::Break, crate::Span::default())
}
super::BodyFragment::Continue => {
block.push(crate::Statement::Continue, crate::Span::Unknown)
block.push(crate::Statement::Continue, crate::Span::default())
}
}
}

View File

@ -89,22 +89,19 @@ fn extract_image_coordinates(
index: required_size.map_or(1, |size| size as u32),
};
let base_span = expressions.get_span(base).clone();
let base_span = expressions.get_span(base);
match extra_coordinate {
ExtraCoordinate::ArrayLayer => {
let extracted = match required_size {
None => expressions.append(
crate::Expression::AccessIndex { base, index: 0 },
base_span.clone(),
),
None => {
expressions.append(crate::Expression::AccessIndex { base, index: 0 }, base_span)
}
Some(size) => {
let mut components = Vec::with_capacity(size as usize);
for index in 0..size as u32 {
let comp = expressions.append(
crate::Expression::AccessIndex { base, index },
base_span.clone(),
);
let comp = expressions
.append(crate::Expression::AccessIndex { base, index }, base_span);
components.push(comp);
}
expressions.append(
@ -112,11 +109,11 @@ fn extract_image_coordinates(
ty: required_ty.unwrap(),
components,
},
base_span.clone(),
base_span,
)
}
};
let array_index_f32 = expressions.append(extra_expr, base_span.clone());
let array_index_f32 = expressions.append(extra_expr, base_span);
let array_index = expressions.append(
crate::Expression::As {
kind: crate::ScalarKind::Sint,
@ -128,13 +125,11 @@ fn extract_image_coordinates(
(extracted, Some(array_index))
}
ExtraCoordinate::Projection => {
let projection = expressions.append(extra_expr, base_span.clone());
let projection = expressions.append(extra_expr, base_span);
let divided = match required_size {
None => {
let temp = expressions.append(
crate::Expression::AccessIndex { base, index: 0 },
base_span.clone(),
);
let temp = expressions
.append(crate::Expression::AccessIndex { base, index: 0 }, base_span);
expressions.append(
crate::Expression::Binary {
op: crate::BinaryOperator::Divide,
@ -147,17 +142,15 @@ fn extract_image_coordinates(
Some(size) => {
let mut components = Vec::with_capacity(size as usize);
for index in 0..size as u32 {
let temp = expressions.append(
crate::Expression::AccessIndex { base, index },
base_span.clone(),
);
let temp = expressions
.append(crate::Expression::AccessIndex { base, index }, base_span);
let comp = expressions.append(
crate::Expression::Binary {
op: crate::BinaryOperator::Divide,
left: temp,
right: projection,
},
base_span.clone(),
base_span,
);
components.push(comp);
}
@ -202,7 +195,7 @@ pub(super) fn patch_comparison_type(
log::debug!("Flipping comparison for {:?}", var);
let original_ty = &arena[var.ty];
let original_ty_span = arena.get_span(var.ty).clone();
let original_ty_span = arena.get_span(var.ty);
let ty_inner = match original_ty.inner {
crate::TypeInner::Image {
class: crate::ImageClass::Sampled { multi, .. },
@ -637,7 +630,7 @@ impl<I: Iterator<Item = u32>> super::Parser<I> {
base: coord_handle,
index: required_size.map_or(1, |size| size as u32),
},
crate::Span::Unknown,
crate::Span::default(),
);
expr = expressions.append(
crate::Expression::Binary {
@ -645,7 +638,7 @@ impl<I: Iterator<Item = u32>> super::Parser<I> {
left: expr,
right,
},
crate::Span::Unknown,
crate::Span::default(),
)
};
Some(expr)

View File

@ -564,11 +564,11 @@ impl<I: Iterator<Item = u32>> Parser<I> {
}
fn span_from(&self, from: usize) -> crate::Span {
crate::Span::ByteRange(from..self.data_offset)
crate::Span::from(from..self.data_offset)
}
fn span_from_with_op(&self, from: usize) -> crate::Span {
crate::Span::ByteRange((from - 4)..self.data_offset)
crate::Span::from((from - 4)..self.data_offset)
}
fn next(&mut self) -> Result<u32, Error> {
@ -742,17 +742,17 @@ impl<I: Iterator<Item = u32>> Parser<I> {
ty,
init: None,
},
crate::Span::Unknown,
crate::Span::default(),
);
block.extend(emitter.finish(expressions));
let pointer = expressions.append(
crate::Expression::LocalVariable(local),
crate::Span::Unknown,
crate::Span::default(),
);
emitter.start(expressions);
let expr =
expressions.append(crate::Expression::Load { pointer }, crate::Span::Unknown);
expressions.append(crate::Expression::Load { pointer }, crate::Span::default());
// Add a slightly odd entry to the phi table, so that while `id`'s
// `Expression` is still in scope, the usual phi processing will
@ -924,7 +924,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
kind,
convert: None,
},
span.clone(),
span,
)
},
right: if p2_lexp.type_id == result_type_id {
@ -936,7 +936,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
kind,
convert: None,
},
span.clone(),
span,
)
},
};
@ -1001,7 +1001,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
kind: crate::ScalarKind::Uint,
convert: None,
},
span.clone(),
span,
);
let expr = crate::Expression::Binary { op, left, right };
@ -1075,7 +1075,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
Some(&index) => index,
None => return Ok(object_expr),
};
let root_span = expressions.get_span(root_expr).clone();
let root_span = expressions.get_span(root_expr);
let root_lookup = self.lookup_type.lookup(root_type_id)?;
let (count, child_type_id) = match type_arena[root_lookup.handle].inner {
crate::TypeInner::Struct { ref members, .. } => {
@ -1103,11 +1103,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
base: root_expr,
index,
},
if index == selection {
span.clone()
} else {
root_span.clone()
},
if index == selection { span } else { root_span },
);
components.push(expr);
}
@ -1118,7 +1114,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
&selections[1..],
type_arena,
expressions,
span.clone(),
span,
)?;
Ok(expressions.append(
@ -1260,7 +1256,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
use spirv::Op;
let start = self.data_offset;
let inst = self.next_inst()?;
let span = crate::Span::ByteRange(start..(start + 4 * (inst.wc as usize)));
let span = crate::Span::from(start..(start + 4 * (inst.wc as usize)));
log::debug!("\t\t{:?} [{}]", inst.op, inst.wc);
match inst.op {
@ -1317,7 +1313,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
},
init,
},
span.clone(),
span,
);
self.lookup_expression.insert(
@ -1347,8 +1343,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
},
self.span_from(start),
);
let pointer =
expressions.append(crate::Expression::LocalVariable(local), span.clone());
let pointer = expressions.append(crate::Expression::LocalVariable(local), span);
let in_count = (inst.wc - 3) / 2;
let mut phi = PhiExpression {
@ -1430,7 +1425,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
base: acex.base_handle,
index,
},
span.clone(),
span,
);
AccessExpression {
base_handle,
@ -1446,7 +1441,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
crate::Expression::Load {
pointer: base_handle,
},
span.clone(),
span,
);
let transposed = expressions.append(
crate::Expression::Math {
@ -1455,7 +1450,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
arg1: None,
arg2: None,
},
span.clone(),
span,
);
LookupLoadOverride::Loaded(transposed)
}
@ -1478,7 +1473,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
base: load_expr,
index,
},
span.clone(),
span,
);
Some(LookupLoadOverride::Loaded(sub_handle))
}
@ -1495,7 +1490,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
},
};
AccessExpression {
base_handle: expressions.append(sub_expr, span.clone()),
base_handle: expressions.append(sub_expr, span),
type_id: type_lookup
.base_id
.ok_or(Error::InvalidAccessType(acex.type_id))?,
@ -1509,7 +1504,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
base: acex.base_handle,
index: index_expr_handle,
},
span.clone(),
span,
);
let load_override = match acex.load_override {
// If there is a load override in place, then we always end up
@ -1523,7 +1518,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
crate::Expression::Load {
pointer: base_handle,
},
span.clone(),
span,
);
expressions.append(
crate::Expression::Math {
@ -1532,7 +1527,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
arg1: None,
arg2: None,
},
span.clone(),
span,
)
}
// We are indexing inside a row-major matrix.
@ -1542,7 +1537,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
base: load_expr,
index: index_expr_handle,
},
span.clone(),
span,
),
};
Some(LookupLoadOverride::Loaded(sub_expr))
@ -1594,7 +1589,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
base: root_handle,
index: self.index_constant_expressions[0],
},
span.clone(),
span,
);
for &index_expr in self.index_constant_expressions[1..num_components].iter() {
let access_expr = expressions.append(
@ -1602,7 +1597,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
base: root_handle,
index: index_expr,
},
span.clone(),
span,
);
let cond = expressions.append(
crate::Expression::Binary {
@ -1610,7 +1605,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
left: index_expr,
right: index_handle,
},
span.clone(),
span,
);
handle = expressions.append(
crate::Expression::Select {
@ -1618,7 +1613,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
accept: access_expr,
reject: handle,
},
span.clone(),
span,
);
}
@ -1659,7 +1654,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
base: root_handle,
index: index_expr,
},
span.clone(),
span,
);
let cond = expressions.append(
crate::Expression::Binary {
@ -1667,7 +1662,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
left: index_expr,
right: index_handle,
},
span.clone(),
span,
);
let handle = expressions.append(
crate::Expression::Select {
@ -1675,7 +1670,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
accept: object_handle,
reject: access_expr,
},
span.clone(),
span,
);
components.push(handle);
}
@ -1684,7 +1679,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
ty: root_type_lookup.handle,
components,
},
span.clone(),
span,
);
self.lookup_expression.insert(
@ -1732,7 +1727,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
base: lexp.handle,
index,
},
span.clone(),
span,
),
type_id,
block_id,
@ -2298,7 +2293,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
} else {
return Err(Error::InvalidAccessIndex(index));
};
components.push(expressions.append(expr, span.clone()));
components.push(expressions.append(expr, span));
}
crate::Expression::Compose {
ty: self.lookup_type.lookup(result_type_id)?.handle,
@ -2376,8 +2371,8 @@ impl<I: Iterator<Item = u32>> Parser<I> {
let result = if self.lookup_void_type == Some(result_type_id) {
None
} else {
let expr_handle = expressions
.append(crate::Expression::CallResult(function), span.clone());
let expr_handle =
expressions.append(crate::Expression::CallResult(function), span);
self.lookup_expression.insert(
result_id,
LookupExpression {
@ -2898,7 +2893,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
base: structure_handle,
index: member_index,
},
span.clone(),
span,
);
let length =
@ -2936,7 +2931,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
crate::Expression::Load {
pointer: source_handle,
},
span.clone(),
span,
);
block.extend(emitter.finish(expressions));
@ -2996,7 +2991,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
block.extend(emitter.finish(expressions));
if let Some(stmt) = terminator {
block.push(stmt, crate::Span::Unknown);
block.push(stmt, crate::Span::default());
}
// Save this block fragment in `block_ctx.blocks`, and mark it to be
@ -3019,7 +3014,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
}
// register global variables
for (&id, var) in self.lookup_variable.iter() {
let span = globals.get_span(var.handle).clone();
let span = globals.get_span(var.handle);
let handle = expressions.append(crate::Expression::GlobalVariable(var.handle), span);
self.lookup_expression.insert(
id,
@ -3036,13 +3031,13 @@ impl<I: Iterator<Item = u32>> Parser<I> {
// register special constants
self.index_constant_expressions.clear();
for &con_handle in self.index_constants.iter() {
let span = constants.get_span(con_handle).clone();
let span = constants.get_span(con_handle);
let handle = expressions.append(crate::Expression::Constant(con_handle), span);
self.index_constant_expressions.push(handle);
}
// register constants
for (&id, con) in self.lookup_constant.iter() {
let span = constants.get_span(con.handle).clone();
let span = constants.get_span(con.handle);
let handle = expressions.append(crate::Expression::Constant(con.handle), span);
self.lookup_expression.insert(
id,
@ -3298,7 +3293,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
// add it to the newly formed arena, and adjust the lookup
lookup.handle = module
.functions
.append(fun, functions.get_span(lookup.handle).clone());
.append(fun, functions.get_span(lookup.handle));
}
}
// patch all the functions
@ -4247,7 +4242,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
let type_lookup = self.lookup_type.lookup(type_id)?;
let ty = type_lookup.handle;
let inner = null::generate_null_constant(ty, types, constants, span.clone())?;
let inner = null::generate_null_constant(ty, types, constants, span)?;
let handle = constants.append(
crate::Constant {
name: self.future_decor.remove(&id).and_then(|dec| dec.name),
@ -4427,7 +4422,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
effective_ty,
&module.types,
&mut module.constants,
span.clone(),
span,
) {
Ok(handle) => Some(handle),
Err(e) => {
@ -4458,7 +4453,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
member_ty,
&module.types,
&mut module.constants,
span.clone(),
span,
)?;
components.push(handle);
}
@ -4471,7 +4466,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
components,
},
},
span.clone(),
span,
))
}
_ => None,

View File

@ -30,7 +30,7 @@ pub fn generate_null_constant(
specialization: None,
inner: make_scalar_inner(kind, width),
},
span.clone(),
span,
));
}
crate::ConstantInner::Composite { ty, components }
@ -51,8 +51,7 @@ pub fn generate_null_constant(
}
})
.unwrap();
let vector_inner =
generate_null_constant(vector_ty, type_arena, constant_arena, span.clone())?;
let vector_inner = generate_null_constant(vector_ty, type_arena, constant_arena, span)?;
let vector_handle = constant_arena.fetch_or_append(
crate::Constant {
name: None,
@ -71,15 +70,14 @@ pub fn generate_null_constant(
// copy out the types to avoid borrowing `members`
let member_tys = members.iter().map(|member| member.ty).collect::<Vec<_>>();
for member_ty in member_tys {
let inner =
generate_null_constant(member_ty, type_arena, constant_arena, span.clone())?;
let inner = generate_null_constant(member_ty, type_arena, constant_arena, span)?;
components.push(constant_arena.fetch_or_append(
crate::Constant {
name: None,
specialization: None,
inner,
},
span.clone(),
span,
));
}
crate::ConstantInner::Composite { ty, components }
@ -92,7 +90,7 @@ pub fn generate_null_constant(
let size = constant_arena[handle]
.to_array_length()
.ok_or(Error::InvalidArraySize(handle))?;
let inner = generate_null_constant(base, type_arena, constant_arena, span.clone())?;
let inner = generate_null_constant(base, type_arena, constant_arena, span)?;
let value = constant_arena.fetch_or_append(
crate::Constant {
name: None,
@ -133,7 +131,7 @@ pub fn generate_default_built_in(
width: 4,
},
},
span.clone(),
span,
);
let one = constant_arena.fetch_or_append(
crate::Constant {
@ -144,7 +142,7 @@ pub fn generate_default_built_in(
width: 4,
},
},
span.clone(),
span,
);
crate::ConstantInner::Composite {
ty,
@ -164,7 +162,7 @@ pub fn generate_default_built_in(
width: 4,
},
//Note: `crate::BuiltIn::ClipDistance` is intentionally left for the default path
_ => generate_null_constant(ty, type_arena, constant_arena, span.clone())?,
_ => generate_null_constant(ty, type_arena, constant_arena, span)?,
};
Ok(constant_arena.fetch_or_append(
crate::Constant {

View File

@ -829,15 +829,15 @@ impl<'a> ExpressionContext<'a, '_, '_> {
ExpressionContext<'a, '_, '_>,
) -> Result<Handle<crate::Expression>, Error<'a>>,
) -> Result<Handle<crate::Expression>, Error<'a>> {
let start = lexer.current_byte_offset();
let start = lexer.current_byte_offset() as u32;
let mut left = parser(lexer, self.reborrow())?;
while let Some(op) = classifier(lexer.peek().0) {
let _ = lexer.next();
let right = parser(lexer, self.reborrow())?;
let end = lexer.current_byte_offset();
let end = lexer.current_byte_offset() as u32;
left = self.expressions.append(
crate::Expression::Binary { op, left, right },
NagaSpan::ByteRange(start..end),
NagaSpan::new(start, end),
);
}
Ok(left)
@ -852,12 +852,12 @@ impl<'a> ExpressionContext<'a, '_, '_> {
ExpressionContext<'a, '_, '_>,
) -> Result<Handle<crate::Expression>, Error<'a>>,
) -> Result<Handle<crate::Expression>, Error<'a>> {
let start = lexer.current_byte_offset();
let start = lexer.current_byte_offset() as u32;
let mut left = parser(lexer, self.reborrow())?;
while let Some(op) = classifier(lexer.peek().0) {
let _ = lexer.next();
let mut right = parser(lexer, self.reborrow())?;
let end = lexer.current_byte_offset();
let end = lexer.current_byte_offset() as u32;
// insert splats, if needed by the non-'*' operations
if op != crate::BinaryOperator::Multiply {
let left_size = match *self.resolve_type(left)? {
@ -868,13 +868,13 @@ impl<'a> ExpressionContext<'a, '_, '_> {
(Some(size), &crate::TypeInner::Scalar { .. }) => {
right = self.expressions.append(
crate::Expression::Splat { size, value: right },
self.expressions.get_span(right).clone(),
self.expressions.get_span(right),
);
}
(None, &crate::TypeInner::Vector { size, .. }) => {
left = self.expressions.append(
crate::Expression::Splat { size, value: left },
self.expressions.get_span(left).clone(),
self.expressions.get_span(left),
);
}
_ => {}
@ -882,7 +882,7 @@ impl<'a> ExpressionContext<'a, '_, '_> {
}
left = self.expressions.append(
crate::Expression::Binary { op, left, right },
NagaSpan::ByteRange(start..end),
NagaSpan::new(start, end),
);
}
Ok(left)
@ -1274,7 +1274,8 @@ impl Parser {
_ => return Err(Error::InvalidAtomicOperandType(value_span)),
};
let result = ctx.interrupt_emitter(expression, NagaSpan::ByteRange(value_span.clone()));
let span = NagaSpan::from(value_span);
let result = ctx.interrupt_emitter(expression, span);
ctx.block.push(
crate::Statement::Atomic {
pointer,
@ -1282,7 +1283,7 @@ impl Parser {
value,
result,
},
NagaSpan::ByteRange(value_span),
span,
);
Ok(result)
}
@ -1483,9 +1484,8 @@ impl Parser {
_ => return Err(Error::InvalidAtomicOperandType(value_span)),
};
let span = self.peek_scope(lexer);
let result =
ctx.interrupt_emitter(expression, NagaSpan::ByteRange(span.clone()));
let span = NagaSpan::from(self.peek_scope(lexer));
let result = ctx.interrupt_emitter(expression, span);
ctx.block.push(
crate::Statement::Atomic {
pointer,
@ -1493,7 +1493,7 @@ impl Parser {
value,
result,
},
NagaSpan::ByteRange(span),
span,
);
return Ok(Some(result));
}
@ -1784,10 +1784,10 @@ impl Parser {
let handle =
match self.parse_local_function_call(lexer, name, ctx.reborrow())? {
Some((function, arguments)) => {
let span = self.peek_scope(lexer);
let span = NagaSpan::from(self.peek_scope(lexer));
let result = Some(ctx.interrupt_emitter(
crate::Expression::CallResult(function),
NagaSpan::ByteRange(span.clone()),
span,
));
ctx.block.push(
crate::Statement::Call {
@ -1795,7 +1795,7 @@ impl Parser {
arguments,
result,
},
NagaSpan::ByteRange(span),
span,
);
result
}
@ -1805,10 +1805,8 @@ impl Parser {
}
}
};
let span = self.peek_scope(lexer);
Ok(Some(
ctx.expressions.append(expr, NagaSpan::ByteRange(span)),
))
let span = NagaSpan::from(self.peek_scope(lexer));
Ok(Some(ctx.expressions.append(expr, span)))
}
/// Expects [`Scope::PrimaryExpr`] scope on top; if returning Some(_), pops it.
@ -1913,10 +1911,8 @@ impl Parser {
crate::Expression::Compose { ty, components }
};
let span = self.pop_scope(lexer);
Ok(Some(
ctx.expressions.append(expr, NagaSpan::ByteRange(span)),
))
let span = NagaSpan::from(self.pop_scope(lexer));
Ok(Some(ctx.expressions.append(expr, span)))
}
fn parse_const_expression_impl<'a>(
@ -1972,7 +1968,7 @@ impl Parser {
// Only set span if it's a named constant. Otherwise, the enclosing Expression should have
// the span.
let span = self.pop_scope(lexer);
let span = NagaSpan::from(self.pop_scope(lexer));
let handle = if let Some(name) = register_name {
const_arena.append(
crate::Constant {
@ -1980,7 +1976,7 @@ impl Parser {
specialization: None,
inner,
},
NagaSpan::ByteRange(span),
span,
)
} else {
const_arena.fetch_or_append(
@ -2026,11 +2022,8 @@ impl Parser {
let _ = lexer.next();
let const_handle =
self.parse_const_expression_impl(token, lexer, None, ctx.types, ctx.constants)?;
let span = self.pop_scope(lexer);
ctx.interrupt_emitter(
crate::Expression::Constant(const_handle),
NagaSpan::ByteRange(span),
)
let span = NagaSpan::from(self.pop_scope(lexer));
ctx.interrupt_emitter(crate::Expression::Constant(const_handle), span)
}
(Token::Word(word), span) => {
if let Some(&expr) = ctx.lookup_ident.get(word) {
@ -2087,7 +2080,7 @@ impl Parser {
let expression = crate::Expression::Load { pointer: handle };
handle = ctx
.expressions
.append(expression, NagaSpan::ByteRange(lexer.span_from(span_start)));
.append(expression, NagaSpan::from(lexer.span_from(span_start)));
needs_deref = false;
}
}
@ -2180,7 +2173,7 @@ impl Parser {
return Ok(if needs_deref {
let expression = crate::Expression::Load { pointer: handle };
ctx.expressions
.append(expression, ctx.expressions.get_span(handle).clone())
.append(expression, ctx.expressions.get_span(handle))
} else {
handle
});
@ -2189,7 +2182,7 @@ impl Parser {
handle = ctx
.expressions
.append(expression, NagaSpan::ByteRange(lexer.span_from(span_start)));
.append(expression, NagaSpan::from(lexer.span_from(span_start)));
}
}
@ -2209,10 +2202,7 @@ impl Parser {
expr: self.parse_singular_expression(lexer, ctx.reborrow())?,
};
let span = self.peek_scope(lexer);
(
true,
ctx.expressions.append(expr, NagaSpan::ByteRange(span)),
)
(true, ctx.expressions.append(expr, NagaSpan::from(span)))
}
Token::Operation('!') | Token::Operation('~') => {
let _ = lexer.next();
@ -2221,10 +2211,7 @@ impl Parser {
expr: self.parse_singular_expression(lexer, ctx.reborrow())?,
};
let span = self.peek_scope(lexer);
(
true,
ctx.expressions.append(expr, NagaSpan::ByteRange(span)),
)
(true, ctx.expressions.append(expr, NagaSpan::from(span)))
}
Token::Operation('*') => {
let _ = lexer.next();
@ -2233,10 +2220,9 @@ impl Parser {
(
false,
match ctx.rhs {
true => ctx.expressions.append(
crate::Expression::Load { pointer },
NagaSpan::ByteRange(span),
),
true => ctx
.expressions
.append(crate::Expression::Load { pointer }, NagaSpan::from(span)),
false => pointer,
},
)
@ -2887,7 +2873,7 @@ impl Parser {
name: debug_name.map(|s| s.to_string()),
inner,
},
NagaSpan::ByteRange(span),
NagaSpan::from(span),
)
}
None => return Err(Error::UnknownType(name_span)),
@ -2960,7 +2946,7 @@ impl Parser {
.extend(context.emitter.finish(context.expressions));
context.block.push(
crate::Statement::Store { pointer, value },
NagaSpan::ByteRange(span_start..span_end),
NagaSpan::from(span_start..span_end),
);
Ok(())
}
@ -2988,7 +2974,7 @@ impl Parser {
arguments,
result: None,
},
NagaSpan::ByteRange(span_start..span_end),
NagaSpan::from(span_start..span_end),
);
Ok(())
}
@ -3020,11 +3006,8 @@ impl Parser {
)?;
}
self.pop_scope(lexer);
let span = self.pop_scope(lexer);
block.push(
crate::Statement::Block(statements),
NagaSpan::ByteRange(span),
);
let span = NagaSpan::from(self.pop_scope(lexer));
block.push(crate::Statement::Block(statements), span);
return Ok(());
}
(Token::Word(word), word_span) => {
@ -3169,7 +3152,7 @@ impl Parser {
_ => None,
},
},
NagaSpan::ByteRange(name_span),
NagaSpan::from(name_span),
);
// Doesn't make sense to assign a span to cached lookup
@ -3255,7 +3238,7 @@ impl Parser {
};
reject = crate::Block::new();
reject.extend(other_emit);
reject.push(sub_stmt, NagaSpan::ByteRange(other_span_start..span_end))
reject.push(sub_stmt, NagaSpan::from(other_span_start..span_end))
}
Some(crate::Statement::If {
@ -3400,14 +3383,14 @@ impl Parser {
Ok(condition)
})?;
let mut reject = crate::Block::new();
reject.push(crate::Statement::Break, NagaSpan::Unknown);
reject.push(crate::Statement::Break, NagaSpan::default());
body.push(
crate::Statement::If {
condition,
accept: crate::Block::new(),
reject,
},
NagaSpan::ByteRange(span),
NagaSpan::from(span),
);
};
@ -3531,9 +3514,9 @@ impl Parser {
None
}
};
let span = self.pop_scope(lexer);
let span = NagaSpan::from(self.pop_scope(lexer));
if let Some(statement) = statement {
block.push(statement, NagaSpan::ByteRange(span));
block.push(statement, span);
}
}
_ => {
@ -3609,9 +3592,9 @@ impl Parser {
for (&name, expression) in lookup_global_expression.iter() {
let span = match *expression {
crate::Expression::GlobalVariable(handle) => {
module.global_variables.get_span(handle).clone()
module.global_variables.get_span(handle)
}
crate::Expression::Constant(handle) => module.constants.get_span(handle).clone(),
crate::Expression::Constant(handle) => module.constants.get_span(handle),
_ => unreachable!(),
};
let expr_handle = expressions.append(expression.clone(), span);
@ -3634,7 +3617,7 @@ impl Parser {
let param_index = arguments.len() as u32;
let expression_token = expressions.append(
crate::Expression::FunctionArgument(param_index),
NagaSpan::ByteRange(param_name_span),
NagaSpan::from(param_name_span),
);
lookup_ident.insert(param_name, expression_token);
arguments.push(crate::FunctionArgument {
@ -3793,7 +3776,7 @@ impl Parser {
let name = lexer.next_ident()?;
let (members, span) =
self.parse_struct_body(lexer, &mut module.types, &mut module.constants)?;
let type_span = lexer.span_from(start);
let type_span = NagaSpan::from(lexer.span_from(start));
let ty = module.types.fetch_or_append(
crate::Type {
name: Some(name.to_string()),
@ -3803,7 +3786,7 @@ impl Parser {
span,
},
},
NagaSpan::ByteRange(type_span),
type_span,
);
self.lookup_type.insert(name.to_owned(), ty);
lexer.expect(Token::Separator(';'))?;
@ -3892,7 +3875,7 @@ impl Parser {
ty: pvar.ty,
init: pvar.init,
},
NagaSpan::ByteRange(pvar.name_span),
NagaSpan::from(pvar.name_span),
);
lookup_global_expression
.insert(pvar.name, crate::Expression::GlobalVariable(var_handle));
@ -3911,7 +3894,7 @@ impl Parser {
None => {
module
.functions
.append(function, NagaSpan::ByteRange(lexer.span_from(start)));
.append(function, NagaSpan::from(lexer.span_from(start)));
}
}
}

View File

@ -1,38 +1,68 @@
use std::ops::Range;
// A source code span, used for error reporting.
#[derive(Clone, Debug, PartialEq)]
pub enum Span {
// Span is unknown - no source information.
Unknown,
// Byte range.
ByteRange(Range<usize>),
}
impl Default for Span {
fn default() -> Self {
Self::Unknown
}
/// A source code span, used for error reporting.
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub struct Span {
start: u32,
end: u32,
}
impl Span {
pub fn subsume(&mut self, other: &Self) {
match *self {
Self::Unknown => self.clone_from(other),
Self::ByteRange(ref mut self_range) => {
if let Self::ByteRange(ref other_range) = *other {
self_range.start = self_range.start.min(other_range.start);
self_range.end = self_range.end.max(other_range.end);
}
/// Creates a new `Span` from a range of byte indices
///
/// Note: end is exclusive, it doesn't belong to the `Span`
pub fn new(start: u32, end: u32) -> Self {
Span { start, end }
}
/// Modifies `self` to contain the smallest `Span` possible that
/// contains both `self` and `other`
pub fn subsume(&mut self, other: Self) {
*self = if !self.is_defined() {
// self isn't defined so use other
other
} else if !other.is_defined() {
// other isn't defined so don't try to subsume
*self
} else {
// Both self and other are defined so calculate the span that contains them both
Span {
start: self.start.min(other.start),
end: self.end.max(other.end),
}
}
}
pub fn total_span<'a, T: Iterator<Item = &'a Self>>(from: T) -> Self {
/// Returns the smallest `Span` possible that contains all the `Span`s
/// defined in the `from` iterator
pub fn total_span<T: Iterator<Item = Self>>(from: T) -> Self {
let mut span: Self = Default::default();
for other in from {
span.subsume(other);
}
span
}
/// Converts `self` to a range if the span is not unknown
pub fn to_range(self) -> Option<Range<usize>> {
if self.is_defined() {
Some(self.start as usize..self.end as usize)
} else {
None
}
}
/// Check wether `self` was defined or is a default/unknown span
fn is_defined(&self) -> bool {
*self == Self::default()
}
}
impl From<Range<usize>> for Span {
fn from(range: Range<usize>) -> Self {
Span {
start: range.start as u32,
end: range.end as u32,
}
}
}