diff --git a/naga/src/front/wgsl/lower/mod.rs b/naga/src/front/wgsl/lower/mod.rs index 34f8daf506..46ea7c7dbf 100644 --- a/naga/src/front/wgsl/lower/mod.rs +++ b/naga/src/front/wgsl/lower/mod.rs @@ -8,7 +8,7 @@ use crate::front::Typifier; use crate::proc::{ ensure_block_returns, Alignment, ConstantEvaluator, Emitter, Layouter, ResolveContext, }; -use crate::{Arena, FastHashMap, FastIndexMap, Handle, Span}; +use crate::{Arena, Comments, FastHashMap, FastIndexMap, Handle, Span}; mod construction; mod conversion; @@ -1090,6 +1090,10 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { let handle = self.r#struct(s, span, &mut ctx)?; ctx.globals .insert(s.name.name, LoweredGlobalDecl::Type(handle)); + ctx.module + .comments + .types + .insert(handle, s.comments.iter().map(|s| s.to_string()).collect()); } ast::GlobalDeclKind::Type(ref alias) => { let ty = self.resolve_named_ast_type( diff --git a/naga/src/front/wgsl/parse/lexer.rs b/naga/src/front/wgsl/parse/lexer.rs index 243de6080f..c581c00f0f 100644 --- a/naga/src/front/wgsl/parse/lexer.rs +++ b/naga/src/front/wgsl/parse/lexer.rs @@ -259,7 +259,7 @@ impl<'a> Lexer<'a> { (token, rest) } - pub(in crate::front::wgsl) fn start_byte_offset_and_aggregate_comment( + pub(in crate::front::wgsl) fn start_byte_offset_and_aggregate_comment<'b>( &'a mut self, comments: &mut Vec, ) -> usize { @@ -268,9 +268,9 @@ impl<'a> Lexer<'a> { // Eat all trivia because `next` doesn't eat trailing trivia. let (token, rest) = consume_token(self.input, false); if let Token::Comment(_) = token { + self.input = rest; let next = self.current_byte_offset(); comments.push(Span::new(start as u32, next as u32)); - self.input = rest; } else if let Token::Trivia = token { self.input = rest; } else { diff --git a/naga/src/lib.rs b/naga/src/lib.rs index ab470b4310..f10a62907b 100644 --- a/naga/src/lib.rs +++ b/naga/src/lib.rs @@ -2263,4 +2263,18 @@ pub struct Module { pub functions: Arena, /// Entry points. pub entry_points: Vec, + /// Comments, usually serving as documentation + pub comments: Comments, +} + +/// Comments preceding items. +/// +/// These can be used to generate automated documentation, +/// IDE hover information or translate shaders with their context comments. +#[derive(Debug, Default, Clone)] +#[cfg_attr(feature = "serialize", derive(Serialize))] +#[cfg_attr(feature = "deserialize", derive(Deserialize))] +#[cfg_attr(feature = "arbitrary", derive(Arbitrary))] +pub struct Comments { + pub types: FastIndexMap, Vec>, } diff --git a/naga/src/valid/handles.rs b/naga/src/valid/handles.rs index f8be76d026..ef0fef4a82 100644 --- a/naga/src/valid/handles.rs +++ b/naga/src/valid/handles.rs @@ -39,6 +39,8 @@ impl super::Validator { ref types, ref special_types, ref global_expressions, + // TODO: validate comments (shouldn't have invalid handle or spans ?) + .. } = module; // NOTE: Types being first is important. All other forms of validation depend on this.