Skip to content

Commit

Permalink
expose comments in
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz committed Oct 4, 2024
1 parent 2da85d3 commit edf20a1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
6 changes: 5 additions & 1 deletion naga/src/front/wgsl/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions naga/src/front/wgsl/parse/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Span>,
) -> usize {
Expand All @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions naga/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2263,4 +2263,18 @@ pub struct Module {
pub functions: Arena<Function>,
/// Entry points.
pub entry_points: Vec<EntryPoint>,
/// 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<Handle<Type>, Vec<String>>,
}
2 changes: 2 additions & 0 deletions naga/src/valid/handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit edf20a1

Please sign in to comment.