mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-26 17:31:05 +01:00
Typifier lookups into constants, variables, and structs
This commit is contained in:
1 parent
6ba942330a
commit
3c5a12412c
4 files changed
+84
-21
No files matched your search
@@ -1135,6 +1135,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
|
||||
let type_id = self.next()?;
|
||||
let id = self.next()?;
|
||||
let type_lookup = self.lookup_type.lookup(type_id)?;
|
||||
let ty = type_lookup.handle;
|
||||
let inner = match module.types[type_lookup.handle].inner {
|
||||
crate::TypeInner::Scalar { kind: crate::ScalarKind::Uint, width } => {
|
||||
let low = self.next()?;
|
||||
@@ -1186,6 +1187,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
|
||||
.and_then(|dec| dec.name),
|
||||
specialization: None, //TODO
|
||||
inner,
|
||||
ty,
|
||||
}),
|
||||
type_id,
|
||||
});
|
||||
|
||||
+51
-14
@@ -260,6 +260,7 @@ struct StatementContext<'input, 'temp, 'out> {
|
||||
expressions: &'out mut Arena<crate::Expression>,
|
||||
types: &'out mut Arena<crate::Type>,
|
||||
constants: &'out mut Arena<crate::Constant>,
|
||||
global_vars: &'out Arena<crate::GlobalVariable>,
|
||||
}
|
||||
|
||||
impl<'a> StatementContext<'a, '_, '_> {
|
||||
@@ -271,6 +272,7 @@ impl<'a> StatementContext<'a, '_, '_> {
|
||||
expressions: self.expressions,
|
||||
types: self.types,
|
||||
constants: self.constants,
|
||||
global_vars: self.global_vars,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -281,6 +283,8 @@ impl<'a> StatementContext<'a, '_, '_> {
|
||||
expressions: self.expressions,
|
||||
types: self.types,
|
||||
constants: self.constants,
|
||||
global_vars: self.global_vars,
|
||||
local_vars: self.variables,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -291,6 +295,8 @@ struct ExpressionContext<'input, 'temp, 'out> {
|
||||
expressions: &'out mut Arena<crate::Expression>,
|
||||
types: &'out mut Arena<crate::Type>,
|
||||
constants: &'out mut Arena<crate::Constant>,
|
||||
global_vars: &'out Arena<crate::GlobalVariable>,
|
||||
local_vars: &'out Arena<crate::LocalVariable>,
|
||||
}
|
||||
|
||||
impl<'a> ExpressionContext<'a, '_, '_> {
|
||||
@@ -301,9 +307,15 @@ impl<'a> ExpressionContext<'a, '_, '_> {
|
||||
expressions: self.expressions,
|
||||
types: self.types,
|
||||
constants: self.constants,
|
||||
global_vars: self.global_vars,
|
||||
local_vars: self.local_vars,
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_type(&mut self, handle: Handle<crate::Expression>) -> Handle<crate::Type> {
|
||||
self.typifier.resolve(handle, self.expressions, self.types, self.constants, self.global_vars, self.local_vars)
|
||||
}
|
||||
|
||||
fn parse_binary_op(
|
||||
&mut self,
|
||||
lexer: &mut Lexer<'a>,
|
||||
@@ -456,6 +468,13 @@ impl Parser {
|
||||
name: None,
|
||||
specialization: None,
|
||||
inner: crate::ConstantInner::Bool(true),
|
||||
ty: Self::deduce_type_handle(
|
||||
crate::TypeInner::Scalar {
|
||||
kind: crate::ScalarKind::Bool,
|
||||
width: 1,
|
||||
},
|
||||
ctx.types,
|
||||
)
|
||||
});
|
||||
crate::Expression::Constant(handle)
|
||||
}
|
||||
@@ -464,14 +483,27 @@ impl Parser {
|
||||
name: None,
|
||||
specialization: None,
|
||||
inner: crate::ConstantInner::Bool(false),
|
||||
ty: Self::deduce_type_handle(
|
||||
crate::TypeInner::Scalar {
|
||||
kind: crate::ScalarKind::Bool,
|
||||
width: 1,
|
||||
},
|
||||
ctx.types,
|
||||
)
|
||||
});
|
||||
crate::Expression::Constant(handle)
|
||||
}
|
||||
Token::Number(word) => {
|
||||
let inner = Self::get_constant_inner(word)?;
|
||||
let kind = inner.scalar_kind();
|
||||
let handle = ctx.constants.append(crate::Constant {
|
||||
name: None,
|
||||
specialization: None,
|
||||
inner: Self::get_constant_inner(word)?,
|
||||
inner,
|
||||
ty: Self::deduce_type_handle(
|
||||
crate::TypeInner::Scalar { kind, width: 32 },
|
||||
ctx.types,
|
||||
)
|
||||
});
|
||||
crate::Expression::Constant(handle)
|
||||
}
|
||||
@@ -528,7 +560,7 @@ impl Parser {
|
||||
Token::Separator('.') => {
|
||||
let _ = lexer.next();
|
||||
let name = lexer.next_ident()?;
|
||||
let type_handle = ctx.typifier.resolve(handle, ctx.expressions);
|
||||
let type_handle = ctx.resolve_type(handle);
|
||||
let index = match ctx.types[type_handle].inner {
|
||||
crate::TypeInner::Struct { ref members } => {
|
||||
members
|
||||
@@ -844,6 +876,19 @@ impl Parser {
|
||||
}
|
||||
}
|
||||
|
||||
fn deduce_type_handle<'a>(inner: crate::TypeInner, arena: &mut Arena<crate::Type>) -> Handle<crate::Type> {
|
||||
if let Some((token, _)) = arena
|
||||
.iter()
|
||||
.find(|(_, ty)| ty.inner == inner)
|
||||
{
|
||||
return token;
|
||||
}
|
||||
arena.append(crate::Type {
|
||||
name: None,
|
||||
inner,
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_type_decl<'a>(
|
||||
&mut self,
|
||||
lexer: &mut Lexer<'a>,
|
||||
@@ -956,17 +1001,7 @@ impl Parser {
|
||||
other => return Err(Error::Unexpected(other)),
|
||||
};
|
||||
self.scopes.pop();
|
||||
|
||||
if let Some((token, _)) = type_arena
|
||||
.iter()
|
||||
.find(|(_, ty)| ty.inner == inner)
|
||||
{
|
||||
return Ok(token);
|
||||
}
|
||||
Ok(type_arena.append(crate::Type {
|
||||
name: None,
|
||||
inner,
|
||||
}))
|
||||
Ok(Self::deduce_type_handle(inner, type_arena))
|
||||
}
|
||||
|
||||
fn parse_statement<'a>(
|
||||
@@ -1127,6 +1162,7 @@ impl Parser {
|
||||
expressions: &mut expressions,
|
||||
types: &mut module.types,
|
||||
constants: &mut module.constants,
|
||||
global_vars: &module.global_variables,
|
||||
})?;
|
||||
// done
|
||||
self.scopes.pop();
|
||||
@@ -1217,7 +1253,7 @@ impl Parser {
|
||||
lexer.expect(Token::Separator(';'))?;
|
||||
}
|
||||
Token::Word("const") => {
|
||||
let (name, _ty) = self.parse_variable_ident_decl(lexer, &mut module.types)?;
|
||||
let (name, ty) = self.parse_variable_ident_decl(lexer, &mut module.types)?;
|
||||
lexer.expect(Token::Operation('='))?;
|
||||
let inner = self.parse_const_expression(lexer, &mut module.types, &mut module.constants)?;
|
||||
lexer.expect(Token::Separator(';'))?;
|
||||
@@ -1225,6 +1261,7 @@ impl Parser {
|
||||
name: Some(name.to_owned()),
|
||||
specialization: None,
|
||||
inner,
|
||||
ty,
|
||||
});
|
||||
lookup_global_expression.insert(name, crate::Expression::Constant(const_handle));
|
||||
}
|
||||
|
||||
+12
@@ -86,6 +86,7 @@ pub struct Constant {
|
||||
pub name: Option<String>,
|
||||
pub specialization: Option<spirv::Word>,
|
||||
pub inner: ConstantInner,
|
||||
pub ty: Handle<Type>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -96,6 +97,17 @@ pub enum ConstantInner {
|
||||
Bool(bool),
|
||||
}
|
||||
|
||||
impl ConstantInner {
|
||||
pub fn scalar_kind(&self) -> ScalarKind {
|
||||
match *self {
|
||||
ConstantInner::Sint(_) => ScalarKind::Sint,
|
||||
ConstantInner::Uint(_) => ScalarKind::Uint,
|
||||
ConstantInner::Float(_) => ScalarKind::Float,
|
||||
ConstantInner::Bool(_) => ScalarKind::Bool,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Binding {
|
||||
BuiltIn(spirv::BuiltIn),
|
||||
|
||||
+19
-7
@@ -15,20 +15,32 @@ impl Typifier {
|
||||
&mut self,
|
||||
expr_handle: Handle<crate::Expression>,
|
||||
expressions: &Arena<crate::Expression>,
|
||||
types: &Arena<crate::Type>,
|
||||
constants: &Arena<crate::Constant>,
|
||||
global_vars: &Arena<crate::GlobalVariable>,
|
||||
local_vars: &Arena<crate::LocalVariable>,
|
||||
) -> Handle<crate::Type> {
|
||||
if self.types.len() <= expr_handle.index() {
|
||||
for (_, expr) in expressions.iter().skip(self.types.len()) {
|
||||
self.types.push(match *expr {
|
||||
crate::Expression::Access { base, .. } |
|
||||
crate::Expression::AccessIndex { base, .. } => {
|
||||
let _ = base;
|
||||
unimplemented!()
|
||||
crate::Expression::Access { base, .. } => {
|
||||
match types[self.types[base.index()]].inner {
|
||||
crate::TypeInner::Array { base, .. } => self.types[base.index()],
|
||||
ref other => panic!("Can't access into {:?}", other),
|
||||
}
|
||||
}
|
||||
crate::Expression::Constant(_) => unimplemented!(),
|
||||
crate::Expression::AccessIndex { base, index } => {
|
||||
match types[self.types[base.index()]].inner {
|
||||
crate::TypeInner::Array { base, .. } => self.types[base.index()],
|
||||
crate::TypeInner::Struct { ref members } => members[index as usize].ty,
|
||||
ref other => panic!("Can't access into {:?}", other),
|
||||
}
|
||||
}
|
||||
crate::Expression::Constant(h) => constants[h].ty,
|
||||
crate::Expression::Compose { ty, .. } => ty,
|
||||
crate::Expression::FunctionParameter(_) => unimplemented!(),
|
||||
crate::Expression::GlobalVariable(_) => unimplemented!(),
|
||||
crate::Expression::LocalVariable(_) => unimplemented!(),
|
||||
crate::Expression::GlobalVariable(h) => global_vars[h].ty,
|
||||
crate::Expression::LocalVariable(h) => local_vars[h].ty,
|
||||
crate::Expression::Load { .. } => unimplemented!(),
|
||||
crate::Expression::Mul(_, _) => unimplemented!(),
|
||||
crate::Expression::ImageSample { .. } => unimplemented!(),
|
||||
|
||||
Reference in new issue
Block a user