Add ArrayLength expression

This commit is contained in:
Dzmitry Malyshau
2020-09-16 00:48:37 -04:00
committed by Dzmitry Malyshau
parent bcd3db2dad
commit 74c4a3ce18
4 changed files with 13 additions and 0 deletions

View File

@@ -1042,6 +1042,10 @@ fn write_expression<'a, 'b>(
.join(","),
))
}
Expression::ArrayLength(expr) => {
let base = write_expression(&builder.expressions[expr], module, builder)?;
Cow::Owned(format!("uint({}.length())", base))
}
})
}

View File

@@ -635,6 +635,8 @@ pub enum Expression {
origin: FunctionOrigin,
arguments: Vec<Handle<Expression>>,
},
/// Get dynamic array length.
ArrayLength(Handle<Expression>),
}
/// A code block is just a vector of statements.

View File

@@ -100,6 +100,9 @@ where
self.traverse_expr(argument);
}
}
E::ArrayLength(expr) => {
self.traverse_expr(expr);
}
}
}

View File

@@ -262,6 +262,10 @@ impl Typifier {
.ok_or(ResolveError::FunctionReturnsVoid)?;
Resolution::Handle(ty)
}
crate::Expression::ArrayLength(_) => Resolution::Value(crate::TypeInner::Scalar {
kind: crate::ScalarKind::Uint,
width: 4,
}),
})
}