Skip to content

Commit 999330d

Browse files
committed
expose comments in Module
1 parent 2da85d3 commit 999330d

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

naga/src/front/wgsl/lower/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::front::Typifier;
88
use crate::proc::{
99
ensure_block_returns, Alignment, ConstantEvaluator, Emitter, Layouter, ResolveContext,
1010
};
11-
use crate::{Arena, FastHashMap, FastIndexMap, Handle, Span};
11+
use crate::{Arena, Comments, FastHashMap, FastIndexMap, Handle, Span};
1212

1313
mod construction;
1414
mod conversion;
@@ -1090,6 +1090,10 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
10901090
let handle = self.r#struct(s, span, &mut ctx)?;
10911091
ctx.globals
10921092
.insert(s.name.name, LoweredGlobalDecl::Type(handle));
1093+
ctx.module
1094+
.comments
1095+
.types
1096+
.insert(handle, s.comments.iter().map(|s| s.to_string()).collect());
10931097
}
10941098
ast::GlobalDeclKind::Type(ref alias) => {
10951099
let ty = self.resolve_named_ast_type(

naga/src/front/wgsl/parse/lexer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ impl<'a> Lexer<'a> {
259259
(token, rest)
260260
}
261261

262-
pub(in crate::front::wgsl) fn start_byte_offset_and_aggregate_comment(
262+
pub(in crate::front::wgsl) fn start_byte_offset_and_aggregate_comment<'b>(
263263
&'a mut self,
264264
comments: &mut Vec<Span>,
265265
) -> usize {
@@ -268,9 +268,9 @@ impl<'a> Lexer<'a> {
268268
// Eat all trivia because `next` doesn't eat trailing trivia.
269269
let (token, rest) = consume_token(self.input, false);
270270
if let Token::Comment(_) = token {
271+
self.input = rest;
271272
let next = self.current_byte_offset();
272273
comments.push(Span::new(start as u32, next as u32));
273-
self.input = rest;
274274
} else if let Token::Trivia = token {
275275
self.input = rest;
276276
} else {

naga/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2263,4 +2263,18 @@ pub struct Module {
22632263
pub functions: Arena<Function>,
22642264
/// Entry points.
22652265
pub entry_points: Vec<EntryPoint>,
2266+
/// Comments, usually serving as documentation
2267+
pub comments: Comments,
2268+
}
2269+
2270+
/// Comments preceding items.
2271+
///
2272+
/// These can be used to generate automated documentation,
2273+
/// IDE hover information or translate shaders with their context comments.
2274+
#[derive(Debug, Default, Clone)]
2275+
#[cfg_attr(feature = "serialize", derive(Serialize))]
2276+
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
2277+
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
2278+
pub struct Comments {
2279+
pub types: FastIndexMap<Handle<Type>, Vec<String>>,
22662280
}

naga/src/valid/handles.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ impl super::Validator {
3939
ref types,
4040
ref special_types,
4141
ref global_expressions,
42+
// TODO: validate comments (shouldn't have invalid handle or spans ?)
43+
..
4244
} = module;
4345

4446
// NOTE: Types being first is important. All other forms of validation depend on this.

0 commit comments

Comments
 (0)