From 86a617f1108cbfa49c8c1faebc7b4af8acd36588 Mon Sep 17 00:00:00 2001 From: Austin Eng Date: Thu, 19 May 2022 20:08:19 +0000 Subject: [PATCH] Add InsertBraces: true to .clang-format Bug: none Change-Id: I4b4f2a4abfea7adcea406f458bc4e4a13b0e8c43 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/91000 Reviewed-by: Corentin Wallez Kokoro: Kokoro Commit-Queue: Austin Eng Reviewed-by: dan sinclair Reviewed-by: Ben Clayton --- .clang-format | 2 +- src/dawn/common/StackContainer.h | 5 +- src/dawn/node/binding/GPUAdapter.cpp | 12 +- src/dawn/platform/tracing/TraceEvent.h | 3 +- src/dawn/tests/DawnTest.cpp | 3 +- .../tests/end2end/VideoViewsTests_gbm.cpp | 3 +- .../tests/unittests/StackContainerTests.cpp | 6 +- .../VulkanImageWrappingTests_DmaBuf.cpp | 6 +- src/tint/cmd/main.cc | 18 +- src/tint/reader/spirv/function.cc | 9 +- src/tint/reader/wgsl/lexer.cc | 180 +++-- src/tint/reader/wgsl/parser_impl.cc | 718 ++++++++++++------ src/tint/resolver/ctor_conv_intrinsic.cc | 1 - src/tint/symbol_table.cc | 3 +- src/tint/writer/glsl/generator_impl.cc | 9 +- src/tint/writer/hlsl/generator_impl.cc | 6 +- src/tint/writer/msl/generator_impl.cc | 3 +- src/tint/writer/spirv/builder.cc | 3 +- 18 files changed, 654 insertions(+), 336 deletions(-) diff --git a/.clang-format b/.clang-format index 30216bc10f..d9e1846d21 100644 --- a/.clang-format +++ b/.clang-format @@ -7,4 +7,4 @@ ColumnLimit: 100 IndentWidth: 4 ObjCBlockIndentWidth: 4 AccessModifierOffset: -2 - +InsertBraces: true \ No newline at end of file diff --git a/src/dawn/common/StackContainer.h b/src/dawn/common/StackContainer.h index 9788510c73..a69c3f1469 100644 --- a/src/dawn/common/StackContainer.h +++ b/src/dawn/common/StackContainer.h @@ -103,10 +103,11 @@ class StackAllocator : public std::allocator { // Free: when trying to free the stack buffer, just mark it as free. For // non-stack-buffer pointers, just fall though to the standard allocator. void deallocate(pointer p, size_type n) { - if (source_ && p == source_->stack_buffer()) + if (source_ && p == source_->stack_buffer()) { source_->used_stack_buffer_ = false; - else + } else { std::allocator::deallocate(p, n); + } } private: diff --git a/src/dawn/node/binding/GPUAdapter.cpp b/src/dawn/node/binding/GPUAdapter.cpp index aef968a003..be3864c2a8 100644 --- a/src/dawn/node/binding/GPUAdapter.cpp +++ b/src/dawn/node/binding/GPUAdapter.cpp @@ -25,8 +25,9 @@ namespace { // TODO(amaiorano): Move to utility header std::vector Split(const std::string& s, char delim) { - if (s.empty()) + if (s.empty()) { return {}; + } std::vector result; const size_t lastIndex = s.length() - 1; @@ -36,13 +37,15 @@ std::vector Split(const std::string& s, char delim) { while (i <= lastIndex) { if (s[i] == delim) { auto token = s.substr(startIndex, i - startIndex); - if (!token.empty()) // Discard empty tokens + if (!token.empty()) { // Discard empty tokens result.push_back(token); + } startIndex = i + 1; } else if (i == lastIndex) { auto token = s.substr(startIndex, i - startIndex + 1); - if (!token.empty()) // Discard empty tokens + if (!token.empty()) { // Discard empty tokens result.push_back(token); + } } ++i; } @@ -151,7 +154,8 @@ GPUAdapter::GPUAdapter(dawn::native::Adapter a, const Flags& flags) : adapter_(a name_ = props.name; } -// TODO(dawn:1133): Avoid the extra copy by making the generator make a virtual method with const std::string& +// TODO(dawn:1133): Avoid the extra copy by making the generator make a virtual method with const +// std::string& std::string GPUAdapter::getName(Napi::Env) { return name_; } diff --git a/src/dawn/platform/tracing/TraceEvent.h b/src/dawn/platform/tracing/TraceEvent.h index d50305d8c4..76cbc56037 100644 --- a/src/dawn/platform/tracing/TraceEvent.h +++ b/src/dawn/platform/tracing/TraceEvent.h @@ -921,8 +921,9 @@ class TraceEndOnScopeClose { // Note: members of m_data intentionally left uninitialized. See initialize. TraceEndOnScopeClose() : m_pdata(0) {} ~TraceEndOnScopeClose() { - if (m_pdata) + if (m_pdata) { addEventIfEnabled(); + } } void initialize(dawn::platform::Platform* platform, diff --git a/src/dawn/tests/DawnTest.cpp b/src/dawn/tests/DawnTest.cpp index a5dffad9f8..25058d45dd 100644 --- a/src/dawn/tests/DawnTest.cpp +++ b/src/dawn/tests/DawnTest.cpp @@ -978,8 +978,7 @@ std::pair DawnTestBase::CreateDeviceImpl(std::string i mDeviceErrorCallback.MakeUserdata(device.Get())); device.SetDeviceLostCallback(mDeviceLostCallback.Callback(), mDeviceLostCallback.MakeUserdata(device.Get())); - EXPECT_CALL(mDeviceLostCallback, - Call(WGPUDeviceLostReason_Destroyed, testing::_, device.Get())) + EXPECT_CALL(mDeviceLostCallback, Call(WGPUDeviceLostReason_Destroyed, testing::_, device.Get())) .Times(testing::AtMost(1)); device.SetLoggingCallback( diff --git a/src/dawn/tests/end2end/VideoViewsTests_gbm.cpp b/src/dawn/tests/end2end/VideoViewsTests_gbm.cpp index 9a45b6cfa7..4a7b81fb82 100644 --- a/src/dawn/tests/end2end/VideoViewsTests_gbm.cpp +++ b/src/dawn/tests/end2end/VideoViewsTests_gbm.cpp @@ -84,8 +84,9 @@ class VideoViewsTestBackendGbm : public VideoViewsTestBackend { for (uint32_t i = kRenderNodeStart; i < kRenderNodeEnd; i++) { std::string renderNode = kRenderNodeTemplate + std::to_string(i); renderNodeFd = open(renderNode.c_str(), O_RDWR); - if (renderNodeFd >= 0) + if (renderNodeFd >= 0) { break; + } } ASSERT(renderNodeFd > 0); diff --git a/src/dawn/tests/unittests/StackContainerTests.cpp b/src/dawn/tests/unittests/StackContainerTests.cpp index b206187c5a..50cfb907de 100644 --- a/src/dawn/tests/unittests/StackContainerTests.cpp +++ b/src/dawn/tests/unittests/StackContainerTests.cpp @@ -47,8 +47,9 @@ TEST(StackContainer, Vector) { } // The array should still be in order. - for (int i = 0; i < stack_size * 2; i++) + for (int i = 0; i < stack_size * 2; i++) { EXPECT_EQ(i, vect.container()[i]); + } // Resize to smaller. Our STL implementation won't reallocate in this case, // otherwise it might use our stack buffer. We reserve right after the resize @@ -64,8 +65,9 @@ TEST(StackContainer, Vector) { std::vector> other(vect.container()); EXPECT_EQ(stack_buffer, &other.front()); EXPECT_TRUE(vect.stack_data().used_stack_buffer_); - for (int i = 0; i < stack_size; i++) + for (int i = 0; i < stack_size; i++) { EXPECT_EQ(i, other[i]); + } } TEST(StackContainer, VectorDoubleDelete) { diff --git a/src/dawn/tests/white_box/VulkanImageWrappingTests_DmaBuf.cpp b/src/dawn/tests/white_box/VulkanImageWrappingTests_DmaBuf.cpp index bf3735917f..7e62168a43 100644 --- a/src/dawn/tests/white_box/VulkanImageWrappingTests_DmaBuf.cpp +++ b/src/dawn/tests/white_box/VulkanImageWrappingTests_DmaBuf.cpp @@ -154,8 +154,9 @@ class VulkanImageWrappingTestBackendDmaBuf : public VulkanImageWrappingTestBacke for (uint32_t i = kRenderNodeStart; i < kRenderNodeEnd; i++) { std::string renderNode = kRenderNodeTemplate + std::to_string(i); renderNodeFd = open(renderNode.c_str(), O_RDWR); - if (renderNodeFd >= 0) + if (renderNodeFd >= 0) { break; + } } EXPECT_GE(renderNodeFd, 0) << "Failed to get file descriptor for render node"; @@ -167,8 +168,9 @@ class VulkanImageWrappingTestBackendDmaBuf : public VulkanImageWrappingTestBacke private: gbm_bo* CreateGbmBo(uint32_t width, uint32_t height, bool linear) { uint32_t flags = GBM_BO_USE_RENDERING; - if (linear) + if (linear) { flags |= GBM_BO_USE_LINEAR; + } gbm_bo* gbmBo = gbm_bo_create(mGbmDevice, width, height, GBM_FORMAT_XBGR8888, flags); EXPECT_NE(gbmBo, nullptr) << "Failed to create GBM buffer object"; return gbmBo; diff --git a/src/tint/cmd/main.cc b/src/tint/cmd/main.cc index 52dfd705e7..b9c6d042e5 100644 --- a/src/tint/cmd/main.cc +++ b/src/tint/cmd/main.cc @@ -125,30 +125,36 @@ Format parse_format(const std::string& fmt) { (void)fmt; #if TINT_BUILD_SPV_WRITER - if (fmt == "spirv") + if (fmt == "spirv") { return Format::kSpirv; - if (fmt == "spvasm") + } + if (fmt == "spvasm") { return Format::kSpvAsm; + } #endif // TINT_BUILD_SPV_WRITER #if TINT_BUILD_WGSL_WRITER - if (fmt == "wgsl") + if (fmt == "wgsl") { return Format::kWgsl; + } #endif // TINT_BUILD_WGSL_WRITER #if TINT_BUILD_MSL_WRITER - if (fmt == "msl") + if (fmt == "msl") { return Format::kMsl; + } #endif // TINT_BUILD_MSL_WRITER #if TINT_BUILD_HLSL_WRITER - if (fmt == "hlsl") + if (fmt == "hlsl") { return Format::kHlsl; + } #endif // TINT_BUILD_HLSL_WRITER #if TINT_BUILD_GLSL_WRITER - if (fmt == "glsl") + if (fmt == "glsl") { return Format::kGlsl; + } #endif // TINT_BUILD_GLSL_WRITER return Format::kNone; diff --git a/src/tint/reader/spirv/function.cc b/src/tint/reader/spirv/function.cc index 82f6224064..d88773e2ed 100644 --- a/src/tint/reader/spirv/function.cc +++ b/src/tint/reader/spirv/function.cc @@ -602,10 +602,12 @@ class StructuredTraverser { // header, we will visit its merge block, then its continue target (if any). // Also records the post order ordering. void VisitBackward(uint32_t id) { - if (id == 0) + if (id == 0) { return; - if (visited_.count(id)) + } + if (visited_.count(id)) { return; + } visited_.insert(id); const spvtools::opt::BasicBlock* bb = id_to_block_[id]; // non-null for valid modules @@ -1600,8 +1602,9 @@ bool FunctionEmitter::RegisterMerges() { bool is_single_block_loop = false; block_info->basic_block->ForEachSuccessorLabel( [&is_single_block_loop, block_id](const uint32_t succ) { - if (block_id == succ) + if (block_id == succ) { is_single_block_loop = true; + } }); const auto ct = block_info->continue_for_header; block_info->is_continue_entire_loop = ct == block_id; diff --git a/src/tint/reader/wgsl/lexer.cc b/src/tint/reader/wgsl/lexer.cc index 4b5f955dbc..20b33640bd 100644 --- a/src/tint/reader/wgsl/lexer.cc +++ b/src/tint/reader/wgsl/lexer.cc @@ -232,8 +232,9 @@ bool Lexer::is_hex(char ch) const { } bool Lexer::matches(size_t pos, std::string_view sub_string) { - if (pos >= length()) + if (pos >= length()) { return false; + } return substr(pos, sub_string.size()) == sub_string; } @@ -265,8 +266,9 @@ Token Lexer::skip_blankspace_and_comments() { // If the cursor didn't advance we didn't remove any blankspace // so we're done. - if (loc == location_) + if (loc == location_) { break; + } } if (is_eof()) { return {Token::Type::kEOF, begin_source()}; @@ -1043,110 +1045,159 @@ Token Lexer::try_punctuation() { } Token Lexer::check_keyword(const Source& source, std::string_view str) { - if (str == "array") + if (str == "array") { return {Token::Type::kArray, source, "array"}; - if (str == "atomic") + } + if (str == "atomic") { return {Token::Type::kAtomic, source, "atomic"}; - if (str == "bitcast") + } + if (str == "bitcast") { return {Token::Type::kBitcast, source, "bitcast"}; - if (str == "bool") + } + if (str == "bool") { return {Token::Type::kBool, source, "bool"}; - if (str == "break") + } + if (str == "break") { return {Token::Type::kBreak, source, "break"}; - if (str == "case") + } + if (str == "case") { return {Token::Type::kCase, source, "case"}; - if (str == "continue") + } + if (str == "continue") { return {Token::Type::kContinue, source, "continue"}; - if (str == "continuing") + } + if (str == "continuing") { return {Token::Type::kContinuing, source, "continuing"}; - if (str == "discard") + } + if (str == "discard") { return {Token::Type::kDiscard, source, "discard"}; - if (str == "default") + } + if (str == "default") { return {Token::Type::kDefault, source, "default"}; - if (str == "else") + } + if (str == "else") { return {Token::Type::kElse, source, "else"}; - if (str == "enable") + } + if (str == "enable") { return {Token::Type::kEnable, source, "enable"}; - if (str == "f16") + } + if (str == "f16") { return {Token::Type::kF16, source, "f16"}; - if (str == "f32") + } + if (str == "f32") { return {Token::Type::kF32, source, "f32"}; - if (str == "fallthrough") + } + if (str == "fallthrough") { return {Token::Type::kFallthrough, source, "fallthrough"}; - if (str == "false") + } + if (str == "false") { return {Token::Type::kFalse, source, "false"}; - if (str == "fn") + } + if (str == "fn") { return {Token::Type::kFn, source, "fn"}; - if (str == "for") + } + if (str == "for") { return {Token::Type::kFor, source, "for"}; - if (str == "function") + } + if (str == "function") { return {Token::Type::kFunction, source, "function"}; - if (str == "i32") + } + if (str == "i32") { return {Token::Type::kI32, source, "i32"}; - if (str == "if") + } + if (str == "if") { return {Token::Type::kIf, source, "if"}; - if (str == "import") + } + if (str == "import") { return {Token::Type::kImport, source, "import"}; - if (str == "let") + } + if (str == "let") { return {Token::Type::kLet, source, "let"}; - if (str == "loop") + } + if (str == "loop") { return {Token::Type::kLoop, source, "loop"}; - if (str == "mat2x2") + } + if (str == "mat2x2") { return {Token::Type::kMat2x2, source, "mat2x2"}; - if (str == "mat2x3") + } + if (str == "mat2x3") { return {Token::Type::kMat2x3, source, "mat2x3"}; - if (str == "mat2x4") + } + if (str == "mat2x4") { return {Token::Type::kMat2x4, source, "mat2x4"}; - if (str == "mat3x2") + } + if (str == "mat3x2") { return {Token::Type::kMat3x2, source, "mat3x2"}; - if (str == "mat3x3") + } + if (str == "mat3x3") { return {Token::Type::kMat3x3, source, "mat3x3"}; - if (str == "mat3x4") + } + if (str == "mat3x4") { return {Token::Type::kMat3x4, source, "mat3x4"}; - if (str == "mat4x2") + } + if (str == "mat4x2") { return {Token::Type::kMat4x2, source, "mat4x2"}; - if (str == "mat4x3") + } + if (str == "mat4x3") { return {Token::Type::kMat4x3, source, "mat4x3"}; - if (str == "mat4x4") + } + if (str == "mat4x4") { return {Token::Type::kMat4x4, source, "mat4x4"}; - if (str == "override") + } + if (str == "override") { return {Token::Type::kOverride, source, "override"}; - if (str == "private") + } + if (str == "private") { return {Token::Type::kPrivate, source, "private"}; - if (str == "ptr") + } + if (str == "ptr") { return {Token::Type::kPtr, source, "ptr"}; - if (str == "return") + } + if (str == "return") { return {Token::Type::kReturn, source, "return"}; - if (str == "sampler") + } + if (str == "sampler") { return {Token::Type::kSampler, source, "sampler"}; - if (str == "sampler_comparison") + } + if (str == "sampler_comparison") { return {Token::Type::kComparisonSampler, source, "sampler_comparison"}; - if (str == "storage_buffer" || str == "storage") + } + if (str == "storage_buffer" || str == "storage") { return {Token::Type::kStorage, source, "storage"}; - if (str == "struct") + } + if (str == "struct") { return {Token::Type::kStruct, source, "struct"}; - if (str == "switch") + } + if (str == "switch") { return {Token::Type::kSwitch, source, "switch"}; - if (str == "texture_1d") + } + if (str == "texture_1d") { return {Token::Type::kTextureSampled1d, source, "texture_1d"}; - if (str == "texture_2d") + } + if (str == "texture_2d") { return {Token::Type::kTextureSampled2d, source, "texture_2d"}; - if (str == "texture_2d_array") + } + if (str == "texture_2d_array") { return {Token::Type::kTextureSampled2dArray, source, "texture_2d_array"}; - if (str == "texture_3d") + } + if (str == "texture_3d") { return {Token::Type::kTextureSampled3d, source, "texture_3d"}; - if (str == "texture_cube") + } + if (str == "texture_cube") { return {Token::Type::kTextureSampledCube, source, "texture_cube"}; + } if (str == "texture_cube_array") { return {Token::Type::kTextureSampledCubeArray, source, "texture_cube_array"}; } - if (str == "texture_depth_2d") + if (str == "texture_depth_2d") { return {Token::Type::kTextureDepth2d, source, "texture_depth_2d"}; + } if (str == "texture_depth_2d_array") { return {Token::Type::kTextureDepth2dArray, source, "texture_depth_2d_array"}; } - if (str == "texture_depth_cube") + if (str == "texture_depth_cube") { return {Token::Type::kTextureDepthCube, source, "texture_depth_cube"}; + } if (str == "texture_depth_cube_array") { return {Token::Type::kTextureDepthCubeArray, source, "texture_depth_cube_array"}; } @@ -1171,24 +1222,33 @@ Token Lexer::check_keyword(const Source& source, std::string_view str) { if (str == "texture_storage_3d") { return {Token::Type::kTextureStorage3d, source, "texture_storage_3d"}; } - if (str == "true") + if (str == "true") { return {Token::Type::kTrue, source, "true"}; - if (str == "type") + } + if (str == "type") { return {Token::Type::kType, source, "type"}; - if (str == "u32") + } + if (str == "u32") { return {Token::Type::kU32, source, "u32"}; - if (str == "uniform") + } + if (str == "uniform") { return {Token::Type::kUniform, source, "uniform"}; - if (str == "var") + } + if (str == "var") { return {Token::Type::kVar, source, "var"}; - if (str == "vec2") + } + if (str == "vec2") { return {Token::Type::kVec2, source, "vec2"}; - if (str == "vec3") + } + if (str == "vec3") { return {Token::Type::kVec3, source, "vec3"}; - if (str == "vec4") + } + if (str == "vec4") { return {Token::Type::kVec4, source, "vec4"}; - if (str == "workgroup") + } + if (str == "workgroup") { return {Token::Type::kWorkgroup, source, "workgroup"}; + } return {}; } diff --git a/src/tint/reader/wgsl/parser_impl.cc b/src/tint/reader/wgsl/parser_impl.cc index 4f0b152934..4ad0f76e40 100644 --- a/src/tint/reader/wgsl/parser_impl.cc +++ b/src/tint/reader/wgsl/parser_impl.cc @@ -141,18 +141,24 @@ struct BlockCounters { /// @return the current enter-exit depth for the given block token type. If /// `t` is not a block token type, then 0 is always returned. int consume(const Token& t) { - if (t.Is(Token::Type::kBraceLeft)) + if (t.Is(Token::Type::kBraceLeft)) { return brace++; - if (t.Is(Token::Type::kBraceRight)) + } + if (t.Is(Token::Type::kBraceRight)) { return brace--; - if (t.Is(Token::Type::kBracketLeft)) + } + if (t.Is(Token::Type::kBracketLeft)) { return bracket++; - if (t.Is(Token::Type::kBracketRight)) + } + if (t.Is(Token::Type::kBracketRight)) { return bracket--; - if (t.Is(Token::Type::kParenLeft)) + } + if (t.Is(Token::Type::kParenLeft)) { return paren++; - if (t.Is(Token::Type::kParenRight)) + } + if (t.Is(Token::Type::kParenRight)) { return paren--; + } return 0; } }; @@ -393,56 +399,66 @@ Maybe ParserImpl::enable_directive() { // | struct_decl // | function_decl Maybe ParserImpl::global_decl() { - if (match(Token::Type::kSemicolon) || match(Token::Type::kEOF)) + if (match(Token::Type::kSemicolon) || match(Token::Type::kEOF)) { return true; + } bool errored = false; auto attrs = attribute_list(); - if (attrs.errored) + if (attrs.errored) { errored = true; - if (!continue_parsing()) + } + if (!continue_parsing()) { return Failure::kErrored; + } auto decl = sync(Token::Type::kSemicolon, [&]() -> Maybe { auto gv = global_variable_decl(attrs.value); - if (gv.errored) + if (gv.errored) { return Failure::kErrored; + } if (gv.matched) { - if (!expect("variable declaration", Token::Type::kSemicolon)) + if (!expect("variable declaration", Token::Type::kSemicolon)) { return Failure::kErrored; + } builder_.AST().AddGlobalVariable(gv.value); return true; } auto gc = global_constant_decl(attrs.value); - if (gc.errored) + if (gc.errored) { return Failure::kErrored; + } if (gc.matched) { - if (!expect("let declaration", Token::Type::kSemicolon)) + if (!expect("let declaration", Token::Type::kSemicolon)) { return Failure::kErrored; + } builder_.AST().AddGlobalVariable(gc.value); return true; } auto ta = type_alias(); - if (ta.errored) + if (ta.errored) { return Failure::kErrored; + } if (ta.matched) { - if (!expect("type alias", Token::Type::kSemicolon)) + if (!expect("type alias", Token::Type::kSemicolon)) { return Failure::kErrored; + } builder_.AST().AddTypeDecl(ta.value); return true; } auto str = struct_decl(); - if (str.errored) + if (str.errored) { return Failure::kErrored; + } if (str.matched) { builder_.AST().AddTypeDecl(str.value); @@ -510,16 +526,19 @@ Maybe ParserImpl::global_decl() { // | variable_attribute_list* variable_decl EQUAL const_expr Maybe ParserImpl::global_variable_decl(ast::AttributeList& attrs) { auto decl = variable_decl(); - if (decl.errored) + if (decl.errored) { return Failure::kErrored; - if (!decl.matched) + } + if (!decl.matched) { return Failure::kNoMatch; + } const ast::Expression* constructor = nullptr; if (match(Token::Type::kEqual)) { auto expr = expect_const_expr(); - if (expr.errored) + if (expr.errored) { return Failure::kErrored; + } constructor = expr.value; } @@ -552,8 +571,9 @@ Maybe ParserImpl::global_constant_decl(ast::AttributeList& } auto decl = expect_variable_ident_decl(use, /* allow_inferred = */ true); - if (decl.errored) + if (decl.errored) { return Failure::kErrored; + } const ast::Expression* initializer = nullptr; if (match(Token::Type::kEqual)) { @@ -579,20 +599,23 @@ Maybe ParserImpl::global_constant_decl(ast::AttributeList& // : VAR variable_qualifier? variable_ident_decl Maybe ParserImpl::variable_decl(bool allow_inferred) { Source source; - if (!match(Token::Type::kVar, &source)) + if (!match(Token::Type::kVar, &source)) { return Failure::kNoMatch; + } VariableQualifier vq; auto explicit_vq = variable_qualifier(); - if (explicit_vq.errored) + if (explicit_vq.errored) { return Failure::kErrored; + } if (explicit_vq.matched) { vq = explicit_vq.value; } auto decl = expect_variable_ident_decl("variable declaration", allow_inferred); - if (decl.errored) + if (decl.errored) { return Failure::kErrored; + } return VarDeclInfo{decl->source, decl->name, vq.storage_class, vq.access, decl->type}; } @@ -606,16 +629,19 @@ Maybe ParserImpl::variable_decl(bool allow_inferred) { // COMMA access GREATER_THAN Maybe ParserImpl::texture_samplers() { auto type = sampler(); - if (type.matched) + if (type.matched) { return type; + } type = depth_texture(); - if (type.matched) + if (type.matched) { return type; + } type = external_texture(); - if (type.matched) + if (type.matched) { return type.value; + } auto source_range = make_source_range(); @@ -624,8 +650,9 @@ Maybe ParserImpl::texture_samplers() { const char* use = "sampled texture type"; auto subtype = expect_lt_gt_block(use, [&] { return expect_type(use); }); - if (subtype.errored) + if (subtype.errored) { return Failure::kErrored; + } return builder_.ty.sampled_texture(source_range, dim.value, subtype.value); } @@ -635,8 +662,9 @@ Maybe ParserImpl::texture_samplers() { const char* use = "multisampled texture type"; auto subtype = expect_lt_gt_block(use, [&] { return expect_type(use); }); - if (subtype.errored) + if (subtype.errored) { return Failure::kErrored; + } return builder_.ty.multisampled_texture(source_range, ms_dim.value, subtype.value); } @@ -679,11 +707,13 @@ Maybe ParserImpl::texture_samplers() { // | SAMPLER_COMPARISON Maybe ParserImpl::sampler() { Source source; - if (match(Token::Type::kSampler, &source)) + if (match(Token::Type::kSampler, &source)) { return builder_.ty.sampler(source, ast::SamplerKind::kSampler); + } - if (match(Token::Type::kComparisonSampler, &source)) + if (match(Token::Type::kComparisonSampler, &source)) { return builder_.ty.sampler(source, ast::SamplerKind::kComparisonSampler); + } return Failure::kNoMatch; } @@ -696,23 +726,29 @@ Maybe ParserImpl::sampler() { // | TEXTURE_SAMPLED_CUBE // | TEXTURE_SAMPLED_CUBE_ARRAY Maybe ParserImpl::sampled_texture() { - if (match(Token::Type::kTextureSampled1d)) + if (match(Token::Type::kTextureSampled1d)) { return ast::TextureDimension::k1d; + } - if (match(Token::Type::kTextureSampled2d)) + if (match(Token::Type::kTextureSampled2d)) { return ast::TextureDimension::k2d; + } - if (match(Token::Type::kTextureSampled2dArray)) + if (match(Token::Type::kTextureSampled2dArray)) { return ast::TextureDimension::k2dArray; + } - if (match(Token::Type::kTextureSampled3d)) + if (match(Token::Type::kTextureSampled3d)) { return ast::TextureDimension::k3d; + } - if (match(Token::Type::kTextureSampledCube)) + if (match(Token::Type::kTextureSampledCube)) { return ast::TextureDimension::kCube; + } - if (match(Token::Type::kTextureSampledCubeArray)) + if (match(Token::Type::kTextureSampledCubeArray)) { return ast::TextureDimension::kCubeArray; + } return Failure::kNoMatch; } @@ -731,8 +767,9 @@ Maybe ParserImpl::external_texture() { // multisampled_texture // : TEXTURE_MULTISAMPLED_2D Maybe ParserImpl::multisampled_texture() { - if (match(Token::Type::kTextureMultisampled2d)) + if (match(Token::Type::kTextureMultisampled2d)) { return ast::TextureDimension::k2d; + } return Failure::kNoMatch; } @@ -743,14 +780,18 @@ Maybe ParserImpl::multisampled_texture() { // | TEXTURE_STORAGE_2D_ARRAY // | TEXTURE_STORAGE_3D Maybe ParserImpl::storage_texture() { - if (match(Token::Type::kTextureStorage1d)) + if (match(Token::Type::kTextureStorage1d)) { return ast::TextureDimension::k1d; - if (match(Token::Type::kTextureStorage2d)) + } + if (match(Token::Type::kTextureStorage2d)) { return ast::TextureDimension::k2d; - if (match(Token::Type::kTextureStorage2dArray)) + } + if (match(Token::Type::kTextureStorage2dArray)) { return ast::TextureDimension::k2dArray; - if (match(Token::Type::kTextureStorage3d)) + } + if (match(Token::Type::kTextureStorage3d)) { return ast::TextureDimension::k3d; + } return Failure::kNoMatch; } @@ -856,37 +897,45 @@ Expect ParserImpl::expect_texel_format(std::string_view use) { Expect ParserImpl::expect_variable_ident_decl(std::string_view use, bool allow_inferred) { auto ident = expect_ident(use); - if (ident.errored) + if (ident.errored) { return Failure::kErrored; + } if (allow_inferred && !peek_is(Token::Type::kColon)) { return TypedIdentifier{nullptr, ident.value, ident.source}; } - if (!expect(use, Token::Type::kColon)) + if (!expect(use, Token::Type::kColon)) { return Failure::kErrored; + } auto t = peek(); auto type = type_decl(); - if (type.errored) + if (type.errored) { return Failure::kErrored; - if (!type.matched) + } + if (!type.matched) { return add_error(t.source(), "invalid type", use); + } return TypedIdentifier{type.value, ident.value, ident.source}; } Expect ParserImpl::expect_access(std::string_view use) { auto ident = expect_ident(use); - if (ident.errored) + if (ident.errored) { return Failure::kErrored; + } - if (ident.value == kReadAccess) + if (ident.value == kReadAccess) { return {ast::Access::kRead, ident.source}; - if (ident.value == kWriteAccess) + } + if (ident.value == kWriteAccess) { return {ast::Access::kWrite, ident.source}; - if (ident.value == kReadWriteAccess) + } + if (ident.value == kReadWriteAccess) { return {ast::Access::kReadWrite, ident.source}; + } return add_error(ident.source, "invalid value for access control"); } @@ -926,24 +975,29 @@ Maybe ParserImpl::variable_qualifier() { // type_alias // : TYPE IDENT EQUAL type_decl Maybe ParserImpl::type_alias() { - if (!peek_is(Token::Type::kType)) + if (!peek_is(Token::Type::kType)) { return Failure::kNoMatch; + } auto t = next(); const char* use = "type alias"; auto name = expect_ident(use); - if (name.errored) + if (name.errored) { return Failure::kErrored; + } - if (!expect(use, Token::Type::kEqual)) + if (!expect(use, Token::Type::kEqual)) { return Failure::kErrored; + } auto type = type_decl(); - if (type.errored) + if (type.errored) { return Failure::kErrored; - if (!type.matched) + } + if (!type.matched) { return add_error(peek(), "invalid type alias"); + } return builder_.ty.alias(make_source_range_from(t.source()), name.value, type.value); } @@ -979,20 +1033,25 @@ Maybe ParserImpl::type_decl() { return builder_.create(source, builder_.Symbols().Register(t.to_str())); } - if (match(Token::Type::kBool, &source)) + if (match(Token::Type::kBool, &source)) { return builder_.ty.bool_(source); + } - if (match(Token::Type::kF16, &source)) + if (match(Token::Type::kF16, &source)) { return builder_.ty.f16(source); + } - if (match(Token::Type::kF32, &source)) + if (match(Token::Type::kF32, &source)) { return builder_.ty.f32(source); + } - if (match(Token::Type::kI32, &source)) + if (match(Token::Type::kI32, &source)) { return builder_.ty.i32(source); + } - if (match(Token::Type::kU32, &source)) + if (match(Token::Type::kU32, &source)) { return builder_.ty.u32(source); + } if (t.IsVector()) { next(); // Consume the peek @@ -1017,20 +1076,24 @@ Maybe ParserImpl::type_decl() { } auto texture_or_sampler = texture_samplers(); - if (texture_or_sampler.errored) + if (texture_or_sampler.errored) { return Failure::kErrored; - if (texture_or_sampler.matched) + } + if (texture_or_sampler.matched) { return texture_or_sampler; + } return Failure::kNoMatch; } Expect ParserImpl::expect_type(std::string_view use) { auto type = type_decl(); - if (type.errored) + if (type.errored) { return Failure::kErrored; - if (!type.matched) + } + if (!type.matched) { return add_error(peek().source(), "invalid type", use); + } return type.value; } @@ -1114,8 +1177,9 @@ Expect ParserImpl::expect_type_decl_array(Token t) { auto subtype = expect_lt_gt_block(use, [&]() -> Expect { auto type = expect_type(use); - if (type.errored) + if (type.errored) { return Failure::kErrored; + } if (match(Token::Type::kComma)) { auto expr = primary_expression(); @@ -1176,20 +1240,25 @@ Expect ParserImpl::expect_type_decl_matrix(Token t) { Expect ParserImpl::expect_storage_class(std::string_view use) { auto source = peek().source(); - if (match(Token::Type::kUniform)) + if (match(Token::Type::kUniform)) { return {ast::StorageClass::kUniform, source}; + } - if (match(Token::Type::kWorkgroup)) + if (match(Token::Type::kWorkgroup)) { return {ast::StorageClass::kWorkgroup, source}; + } - if (match(Token::Type::kStorage)) + if (match(Token::Type::kStorage)) { return {ast::StorageClass::kStorage, source}; + } - if (match(Token::Type::kPrivate)) + if (match(Token::Type::kPrivate)) { return {ast::StorageClass::kPrivate, source}; + } - if (match(Token::Type::kFunction)) + if (match(Token::Type::kFunction)) { return {ast::StorageClass::kFunction, source}; + } return add_error(source, "invalid storage class", use); } @@ -1200,16 +1269,19 @@ Maybe ParserImpl::struct_decl() { auto t = peek(); auto source = t.source(); - if (!match(Token::Type::kStruct)) + if (!match(Token::Type::kStruct)) { return Failure::kNoMatch; + } auto name = expect_ident("struct declaration"); - if (name.errored) + if (name.errored) { return Failure::kErrored; + } auto body = expect_struct_body_decl(); - if (body.errored) + if (body.errored) { return Failure::kErrored; + } auto sym = builder_.Symbols().Register(name.value); return create(source, sym, std::move(body.value), ast::AttributeList{}); @@ -1244,8 +1316,9 @@ Expect ParserImpl::expect_struct_body_decl() { next(); continue; } - if (!match(Token::Type::kComma)) + if (!match(Token::Type::kComma)) { break; + } } if (errored) { return Failure::kErrored; @@ -1263,8 +1336,9 @@ Expect ParserImpl::expect_struct_member() { } auto decl = expect_variable_ident_decl("struct member"); - if (decl.errored) + if (decl.errored) { return Failure::kErrored; + } return create(decl->source, builder_.Symbols().Register(decl->name), decl->type, std::move(attrs.value)); @@ -1286,17 +1360,20 @@ Maybe ParserImpl::function_decl(ast::AttributeList& attrs) } return Failure::kErrored; } - if (!header.matched) + if (!header.matched) { return Failure::kNoMatch; + } bool errored = false; auto body = expect_body_stmt(); - if (body.errored) + if (body.errored) { errored = true; + } - if (errored) + if (errored) { return Failure::kErrored; + } return create(header->source, builder_.Symbols().Register(header->name), header->params, header->return_type, body.value, attrs, @@ -1376,12 +1453,14 @@ Expect ParserImpl::expect_param_list() { } auto param = expect_param(); - if (param.errored) + if (param.errored) { return Failure::kErrored; + } ret.push_back(param.value); - if (!match(Token::Type::kComma)) + if (!match(Token::Type::kComma)) { break; + } } return ret; @@ -1393,8 +1472,9 @@ Expect ParserImpl::expect_param() { auto attrs = attribute_list(); auto decl = expect_variable_ident_decl("parameter"); - if (decl.errored) + if (decl.errored) { return Failure::kErrored; + } auto* var = create(decl->source, // source builder_.Symbols().Register(decl->name), // symbol @@ -1436,12 +1516,14 @@ Expect ParserImpl::expect_pipeline_stage() { Expect ParserImpl::expect_builtin() { auto ident = expect_ident("builtin"); - if (ident.errored) + if (ident.errored) { return Failure::kErrored; + } ast::Builtin builtin = ident_to_builtin(ident.value); - if (builtin == ast::Builtin::kNone) + if (builtin == ast::Builtin::kNone) { return add_error(ident.source, "invalid value for builtin attribute"); + } return {builtin, ident.source}; } @@ -1451,8 +1533,9 @@ Expect ParserImpl::expect_builtin() { Expect ParserImpl::expect_body_stmt() { return expect_brace_block("", [&]() -> Expect { auto stmts = expect_statements(); - if (stmts.errored) + if (stmts.errored) { return Failure::kErrored; + } return create(Source{}, stmts.value); }); } @@ -1462,10 +1545,12 @@ Expect ParserImpl::expect_body_stmt() { Expect ParserImpl::expect_paren_rhs_stmt() { return expect_paren_block("", [&]() -> Expect { auto expr = logical_or_expression(); - if (expr.errored) + if (expr.errored) { return Failure::kErrored; - if (!expr.matched) + } + if (!expr.matched) { return add_error(peek(), "unable to parse expression"); + } return expr.value; }); @@ -1488,8 +1573,9 @@ Expect ParserImpl::expect_statements() { } } - if (errored) + if (errored) { return Failure::kErrored; + } return stmts; } @@ -1519,39 +1605,50 @@ Maybe ParserImpl::statement() { // Non-block statments that error can resynchronize on semicolon. auto stmt = sync(Token::Type::kSemicolon, [&] { return non_block_statement(); }); - if (stmt.errored) + if (stmt.errored) { return Failure::kErrored; - if (stmt.matched) + } + if (stmt.matched) { return stmt; + } auto stmt_if = if_stmt(); - if (stmt_if.errored) + if (stmt_if.errored) { return Failure::kErrored; - if (stmt_if.matched) + } + if (stmt_if.matched) { return stmt_if.value; + } auto sw = switch_stmt(); - if (sw.errored) + if (sw.errored) { return Failure::kErrored; - if (sw.matched) + } + if (sw.matched) { return sw.value; + } auto loop = loop_stmt(); - if (loop.errored) + if (loop.errored) { return Failure::kErrored; - if (loop.matched) + } + if (loop.matched) { return loop.value; + } auto stmt_for = for_stmt(); - if (stmt_for.errored) + if (stmt_for.errored) { return Failure::kErrored; - if (stmt_for.matched) + } + if (stmt_for.matched) { return stmt_for.value; + } if (peek_is(Token::Type::kBraceLeft)) { auto body = expect_body_stmt(); - if (body.errored) + if (body.errored) { return Failure::kErrored; + } return body.value; } @@ -1571,50 +1668,64 @@ Maybe ParserImpl::statement() { Maybe ParserImpl::non_block_statement() { auto stmt = [&]() -> Maybe { auto ret_stmt = return_stmt(); - if (ret_stmt.errored) + if (ret_stmt.errored) { return Failure::kErrored; - if (ret_stmt.matched) + } + if (ret_stmt.matched) { return ret_stmt.value; + } auto func = func_call_stmt(); - if (func.errored) + if (func.errored) { return Failure::kErrored; - if (func.matched) + } + if (func.matched) { return func.value; + } auto var = variable_stmt(); - if (var.errored) + if (var.errored) { return Failure::kErrored; - if (var.matched) + } + if (var.matched) { return var.value; + } auto b = break_stmt(); - if (b.errored) + if (b.errored) { return Failure::kErrored; - if (b.matched) + } + if (b.matched) { return b.value; + } auto cont = continue_stmt(); - if (cont.errored) + if (cont.errored) { return Failure::kErrored; - if (cont.matched) + } + if (cont.matched) { return cont.value; + } auto assign = assignment_stmt(); - if (assign.errored) + if (assign.errored) { return Failure::kErrored; - if (assign.matched) + } + if (assign.matched) { return assign.value; + } Source source; - if (match(Token::Type::kDiscard, &source)) + if (match(Token::Type::kDiscard, &source)) { return create(source); + } return Failure::kNoMatch; }(); - if (stmt.matched && !expect(stmt->Name(), Token::Type::kSemicolon)) + if (stmt.matched && !expect(stmt->Name(), Token::Type::kSemicolon)) { return Failure::kErrored; + } return stmt; } @@ -1623,15 +1734,18 @@ Maybe ParserImpl::non_block_statement() { // : RETURN logical_or_expression? Maybe ParserImpl::return_stmt() { Source source; - if (!match(Token::Type::kReturn, &source)) + if (!match(Token::Type::kReturn, &source)) { return Failure::kNoMatch; + } - if (peek_is(Token::Type::kSemicolon)) + if (peek_is(Token::Type::kSemicolon)) { return create(source, nullptr); + } auto expr = logical_or_expression(); - if (expr.errored) + if (expr.errored) { return Failure::kErrored; + } // TODO(bclayton): Check matched? return create(source, expr.value); @@ -1645,17 +1759,21 @@ Maybe ParserImpl::variable_stmt() { if (match(Token::Type::kLet)) { auto decl = expect_variable_ident_decl("let declaration", /*allow_inferred = */ true); - if (decl.errored) + if (decl.errored) { return Failure::kErrored; + } - if (!expect("let declaration", Token::Type::kEqual)) + if (!expect("let declaration", Token::Type::kEqual)) { return Failure::kErrored; + } auto constructor = logical_or_expression(); - if (constructor.errored) + if (constructor.errored) { return Failure::kErrored; - if (!constructor.matched) + } + if (!constructor.matched) { return add_error(peek(), "missing constructor for let declaration"); + } auto* var = create(decl->source, // source builder_.Symbols().Register(decl->name), // symbol @@ -1671,18 +1789,22 @@ Maybe ParserImpl::variable_stmt() { } auto decl = variable_decl(/*allow_inferred = */ true); - if (decl.errored) + if (decl.errored) { return Failure::kErrored; - if (!decl.matched) + } + if (!decl.matched) { return Failure::kNoMatch; + } const ast::Expression* constructor = nullptr; if (match(Token::Type::kEqual)) { auto constructor_expr = logical_or_expression(); - if (constructor_expr.errored) + if (constructor_expr.errored) { return Failure::kErrored; - if (!constructor_expr.matched) + } + if (!constructor_expr.matched) { return add_error(peek(), "missing constructor for variable declaration"); + } constructor = constructor_expr.value; } @@ -1786,12 +1908,14 @@ Maybe ParserImpl::if_stmt() { // : SWITCH paren_rhs_stmt BRACKET_LEFT switch_body+ BRACKET_RIGHT Maybe ParserImpl::switch_stmt() { Source source; - if (!match(Token::Type::kSwitch, &source)) + if (!match(Token::Type::kSwitch, &source)) { return Failure::kNoMatch; + } auto condition = logical_or_expression(); - if (condition.errored) + if (condition.errored) { return Failure::kErrored; + } if (!condition.matched) { return add_error(peek(), "unable to parse selector expression"); } @@ -1805,17 +1929,20 @@ Maybe ParserImpl::switch_stmt() { errored = true; continue; } - if (!stmt.matched) + if (!stmt.matched) { break; + } list.push_back(stmt.value); } - if (errored) + if (errored) { return Failure::kErrored; + } return list; }); - if (body.errored) + if (body.errored) { return Failure::kErrored; + } return create(source, condition.value, body.value); } @@ -1824,8 +1951,9 @@ Maybe ParserImpl::switch_stmt() { // : CASE case_selectors COLON? BRACKET_LEFT case_body BRACKET_RIGHT // | DEFAULT COLON? BRACKET_LEFT case_body BRACKET_RIGHT Maybe ParserImpl::switch_body() { - if (!peek_is(Token::Type::kCase) && !peek_is(Token::Type::kDefault)) + if (!peek_is(Token::Type::kCase) && !peek_is(Token::Type::kDefault)) { return Failure::kNoMatch; + } auto t = next(); auto source = t.source(); @@ -1833,8 +1961,9 @@ Maybe ParserImpl::switch_body() { ast::CaseSelectorList selector_list; if (t.Is(Token::Type::kCase)) { auto selectors = expect_case_selectors(); - if (selectors.errored) + if (selectors.errored) { return Failure::kErrored; + } selector_list = std::move(selectors.value); } @@ -1845,10 +1974,12 @@ Maybe ParserImpl::switch_body() { const char* use = "case statement"; auto body = expect_brace_block(use, [&] { return case_body(); }); - if (body.errored) + if (body.errored) { return Failure::kErrored; - if (!body.matched) + } + if (!body.matched) { return add_error(body.source, "expected case body"); + } return create(source, selector_list, body.value); } @@ -1875,8 +2006,9 @@ Expect ParserImpl::expect_case_selectors() { } } - if (selectors.empty()) + if (selectors.empty()) { return add_error(peek(), "unable to parse case selectors"); + } return selectors; } @@ -1890,18 +2022,21 @@ Maybe ParserImpl::case_body() { while (continue_parsing()) { Source source; if (match(Token::Type::kFallthrough, &source)) { - if (!expect("fallthrough statement", Token::Type::kSemicolon)) + if (!expect("fallthrough statement", Token::Type::kSemicolon)) { return Failure::kErrored; + } stmts.emplace_back(create(source)); break; } auto stmt = statement(); - if (stmt.errored) + if (stmt.errored) { return Failure::kErrored; - if (!stmt.matched) + } + if (!stmt.matched) { break; + } stmts.emplace_back(stmt.value); } @@ -1913,17 +2048,20 @@ Maybe ParserImpl::case_body() { // : LOOP BRACKET_LEFT statements continuing_stmt? BRACKET_RIGHT Maybe ParserImpl::loop_stmt() { Source source; - if (!match(Token::Type::kLoop, &source)) + if (!match(Token::Type::kLoop, &source)) { return Failure::kNoMatch; + } return expect_brace_block("loop", [&]() -> Maybe { auto stmts = expect_statements(); - if (stmts.errored) + if (stmts.errored) { return Failure::kErrored; + } auto continuing = continuing_stmt(); - if (continuing.errored) + if (continuing.errored) { return Failure::kErrored; + } auto* body = create(source, stmts.value); return create(source, body, continuing.value); @@ -1941,22 +2079,28 @@ ForHeader::~ForHeader() = default; // func_call_stmt)? Maybe ParserImpl::for_header_initializer() { auto call = func_call_stmt(); - if (call.errored) + if (call.errored) { return Failure::kErrored; - if (call.matched) + } + if (call.matched) { return call.value; + } auto var = variable_stmt(); - if (var.errored) + if (var.errored) { return Failure::kErrored; - if (var.matched) + } + if (var.matched) { return var.value; + } auto assign = assignment_stmt(); - if (assign.errored) + if (assign.errored) { return Failure::kErrored; - if (assign.matched) + } + if (assign.matched) { return assign.value; + } return Failure::kNoMatch; } @@ -1964,16 +2108,20 @@ Maybe ParserImpl::for_header_initializer() { // (increment_stmt | decrement_stmt | assignment_stmt | func_call_stmt)? Maybe ParserImpl::for_header_continuing() { auto call_stmt = func_call_stmt(); - if (call_stmt.errored) + if (call_stmt.errored) { return Failure::kErrored; - if (call_stmt.matched) + } + if (call_stmt.matched) { return call_stmt.value; + } auto assign = assignment_stmt(); - if (assign.errored) + if (assign.errored) { return Failure::kErrored; - if (assign.matched) + } + if (assign.matched) { return assign.value; + } return Failure::kNoMatch; } @@ -1985,22 +2133,27 @@ Maybe ParserImpl::for_header_continuing() { // (assignment_stmt | func_call_stmt)? Expect> ParserImpl::expect_for_header() { auto initializer = for_header_initializer(); - if (initializer.errored) + if (initializer.errored) { return Failure::kErrored; + } - if (!expect("initializer in for loop", Token::Type::kSemicolon)) + if (!expect("initializer in for loop", Token::Type::kSemicolon)) { return Failure::kErrored; + } auto condition = logical_or_expression(); - if (condition.errored) + if (condition.errored) { return Failure::kErrored; + } - if (!expect("condition in for loop", Token::Type::kSemicolon)) + if (!expect("condition in for loop", Token::Type::kSemicolon)) { return Failure::kErrored; + } auto continuing = for_header_continuing(); - if (continuing.errored) + if (continuing.errored) { return Failure::kErrored; + } return std::make_unique(initializer.value, condition.value, continuing.value); } @@ -2009,16 +2162,19 @@ Expect> ParserImpl::expect_for_header() { // : FOR PAREN_LEFT for_header PAREN_RIGHT BRACE_LEFT statements BRACE_RIGHT Maybe ParserImpl::for_stmt() { Source source; - if (!match(Token::Type::kFor, &source)) + if (!match(Token::Type::kFor, &source)) { return Failure::kNoMatch; + } auto header = expect_paren_block("for loop", [&] { return expect_for_header(); }); - if (header.errored) + if (header.errored) { return Failure::kErrored; + } auto stmts = expect_brace_block("for loop", [&] { return expect_statements(); }); - if (stmts.errored) + if (stmts.errored) { return Failure::kErrored; + } return create(source, header->initializer, header->condition, header->continuing, @@ -2030,8 +2186,9 @@ Maybe ParserImpl::for_stmt() { Maybe ParserImpl::func_call_stmt() { auto t = peek(); auto t2 = peek(1); - if (!t.IsIdentifier() || !t2.Is(Token::Type::kParenLeft)) + if (!t.IsIdentifier() || !t2.Is(Token::Type::kParenLeft)) { return Failure::kNoMatch; + } next(); // Consume the first peek @@ -2039,8 +2196,9 @@ Maybe ParserImpl::func_call_stmt() { auto name = t.to_str(); auto params = expect_argument_expression_list("function call"); - if (params.errored) + if (params.errored) { return Failure::kErrored; + } return create( source, @@ -2053,8 +2211,9 @@ Maybe ParserImpl::func_call_stmt() { // : BREAK Maybe ParserImpl::break_stmt() { Source source; - if (!match(Token::Type::kBreak, &source)) + if (!match(Token::Type::kBreak, &source)) { return Failure::kNoMatch; + } return create(source); } @@ -2063,8 +2222,9 @@ Maybe ParserImpl::break_stmt() { // : CONTINUE Maybe ParserImpl::continue_stmt() { Source source; - if (!match(Token::Type::kContinue, &source)) + if (!match(Token::Type::kContinue, &source)) { return Failure::kNoMatch; + } return create(source); } @@ -2072,8 +2232,9 @@ Maybe ParserImpl::continue_stmt() { // continuing_stmt // : CONTINUING body_stmt Maybe ParserImpl::continuing_stmt() { - if (!match(Token::Type::kContinuing)) + if (!match(Token::Type::kContinuing)) { return create(Source{}, ast::StatementList{}); + } return expect_body_stmt(); } @@ -2109,12 +2270,14 @@ Maybe ParserImpl::primary_expression() { const char* use = "bitcast expression"; auto type = expect_lt_gt_block(use, [&] { return expect_type(use); }); - if (type.errored) + if (type.errored) { return Failure::kErrored; + } auto params = expect_paren_rhs_stmt(); - if (params.errored) + if (params.errored) { return Failure::kErrored; + } return create(source, type.value, params.value); } @@ -2127,8 +2290,9 @@ Maybe ParserImpl::primary_expression() { if (peek_is(Token::Type::kParenLeft)) { auto params = expect_argument_expression_list("function call"); - if (params.errored) + if (params.errored) { return Failure::kErrored; + } return create(source, ident, std::move(params.value)); } @@ -2137,12 +2301,14 @@ Maybe ParserImpl::primary_expression() { } auto type = type_decl(); - if (type.errored) + if (type.errored) { return Failure::kErrored; + } if (type.matched) { auto params = expect_argument_expression_list("type constructor"); - if (params.errored) + if (params.errored) { return Failure::kErrored; + } return builder_.Construct(source, type.value, std::move(params.value)); } @@ -2161,8 +2327,9 @@ Maybe ParserImpl::postfix_expression(const ast::Expressi if (match(Token::Type::kBracketLeft, &source)) { auto res = sync(Token::Type::kBracketRight, [&]() -> Maybe { auto param = logical_or_expression(); - if (param.errored) + if (param.errored) { return Failure::kErrored; + } if (!param.matched) { return add_error(peek(), "unable to parse expression inside []"); } @@ -2204,10 +2371,12 @@ Maybe ParserImpl::postfix_expression(const ast::Expressi // : primary_expression postfix_expr Maybe ParserImpl::singular_expression() { auto prefix = primary_expression(); - if (prefix.errored) + if (prefix.errored) { return Failure::kErrored; - if (!prefix.matched) + } + if (!prefix.matched) { return Failure::kNoMatch; + } return postfix_expression(prefix.value); } @@ -2298,22 +2467,24 @@ Maybe ParserImpl::unary_expression() { Expect ParserImpl::expect_multiplicative_expr(const ast::Expression* lhs) { while (continue_parsing()) { ast::BinaryOp op = ast::BinaryOp::kNone; - if (peek_is(Token::Type::kStar)) + if (peek_is(Token::Type::kStar)) { op = ast::BinaryOp::kMultiply; - else if (peek_is(Token::Type::kForwardSlash)) + } else if (peek_is(Token::Type::kForwardSlash)) { op = ast::BinaryOp::kDivide; - else if (peek_is(Token::Type::kMod)) + } else if (peek_is(Token::Type::kMod)) { op = ast::BinaryOp::kModulo; - else + } else { return lhs; + } auto t = next(); auto source = t.source(); auto name = t.to_name(); auto rhs = unary_expression(); - if (rhs.errored) + if (rhs.errored) { return Failure::kErrored; + } if (!rhs.matched) { return add_error(peek(), "unable to parse right side of " + std::string(name) + " expression"); @@ -2328,10 +2499,12 @@ Expect ParserImpl::expect_multiplicative_expr(const ast: // : unary_expression multiplicative_expr Maybe ParserImpl::multiplicative_expression() { auto lhs = unary_expression(); - if (lhs.errored) + if (lhs.errored) { return Failure::kErrored; - if (!lhs.matched) + } + if (!lhs.matched) { return Failure::kNoMatch; + } return expect_multiplicative_expr(lhs.value); } @@ -2343,21 +2516,24 @@ Maybe ParserImpl::multiplicative_expression() { Expect ParserImpl::expect_additive_expr(const ast::Expression* lhs) { while (continue_parsing()) { ast::BinaryOp op = ast::BinaryOp::kNone; - if (peek_is(Token::Type::kPlus)) + if (peek_is(Token::Type::kPlus)) { op = ast::BinaryOp::kAdd; - else if (peek_is(Token::Type::kMinus)) + } else if (peek_is(Token::Type::kMinus)) { op = ast::BinaryOp::kSubtract; - else + } else { return lhs; + } auto t = next(); auto source = t.source(); auto rhs = multiplicative_expression(); - if (rhs.errored) + if (rhs.errored) { return Failure::kErrored; - if (!rhs.matched) + } + if (!rhs.matched) { return add_error(peek(), "unable to parse right side of + expression"); + } lhs = create(source, op, lhs, rhs.value); } @@ -2368,10 +2544,12 @@ Expect ParserImpl::expect_additive_expr(const ast::Expre // : multiplicative_expression additive_expr Maybe ParserImpl::additive_expression() { auto lhs = multiplicative_expression(); - if (lhs.errored) + if (lhs.errored) { return Failure::kErrored; - if (!lhs.matched) + } + if (!lhs.matched) { return Failure::kNoMatch; + } return expect_additive_expr(lhs.value); } @@ -2397,8 +2575,9 @@ Expect ParserImpl::expect_shift_expr(const ast::Expressi auto t = next(); auto source = t.source(); auto rhs = additive_expression(); - if (rhs.errored) + if (rhs.errored) { return Failure::kErrored; + } if (!rhs.matched) { return add_error(peek(), std::string("unable to parse right side of ") + name + " expression"); @@ -2413,10 +2592,12 @@ Expect ParserImpl::expect_shift_expr(const ast::Expressi // : additive_expression shift_expr Maybe ParserImpl::shift_expression() { auto lhs = additive_expression(); - if (lhs.errored) + if (lhs.errored) { return Failure::kErrored; - if (!lhs.matched) + } + if (!lhs.matched) { return Failure::kNoMatch; + } return expect_shift_expr(lhs.value); } @@ -2430,24 +2611,26 @@ Maybe ParserImpl::shift_expression() { Expect ParserImpl::expect_relational_expr(const ast::Expression* lhs) { while (continue_parsing()) { ast::BinaryOp op = ast::BinaryOp::kNone; - if (peek_is(Token::Type::kLessThan)) + if (peek_is(Token::Type::kLessThan)) { op = ast::BinaryOp::kLessThan; - else if (peek_is(Token::Type::kGreaterThan)) + } else if (peek_is(Token::Type::kGreaterThan)) { op = ast::BinaryOp::kGreaterThan; - else if (peek_is(Token::Type::kLessThanEqual)) + } else if (peek_is(Token::Type::kLessThanEqual)) { op = ast::BinaryOp::kLessThanEqual; - else if (peek_is(Token::Type::kGreaterThanEqual)) + } else if (peek_is(Token::Type::kGreaterThanEqual)) { op = ast::BinaryOp::kGreaterThanEqual; - else + } else { return lhs; + } auto t = next(); auto source = t.source(); auto name = t.to_name(); auto rhs = shift_expression(); - if (rhs.errored) + if (rhs.errored) { return Failure::kErrored; + } if (!rhs.matched) { return add_error(peek(), "unable to parse right side of " + std::string(name) + " expression"); @@ -2462,10 +2645,12 @@ Expect ParserImpl::expect_relational_expr(const ast::Exp // : shift_expression relational_expr Maybe ParserImpl::relational_expression() { auto lhs = shift_expression(); - if (lhs.errored) + if (lhs.errored) { return Failure::kErrored; - if (!lhs.matched) + } + if (!lhs.matched) { return Failure::kNoMatch; + } return expect_relational_expr(lhs.value); } @@ -2477,20 +2662,22 @@ Maybe ParserImpl::relational_expression() { Expect ParserImpl::expect_equality_expr(const ast::Expression* lhs) { while (continue_parsing()) { ast::BinaryOp op = ast::BinaryOp::kNone; - if (peek_is(Token::Type::kEqualEqual)) + if (peek_is(Token::Type::kEqualEqual)) { op = ast::BinaryOp::kEqual; - else if (peek_is(Token::Type::kNotEqual)) + } else if (peek_is(Token::Type::kNotEqual)) { op = ast::BinaryOp::kNotEqual; - else + } else { return lhs; + } auto t = next(); auto source = t.source(); auto name = t.to_name(); auto rhs = relational_expression(); - if (rhs.errored) + if (rhs.errored) { return Failure::kErrored; + } if (!rhs.matched) { return add_error(peek(), "unable to parse right side of " + std::string(name) + " expression"); @@ -2505,10 +2692,12 @@ Expect ParserImpl::expect_equality_expr(const ast::Expre // : relational_expression equality_expr Maybe ParserImpl::equality_expression() { auto lhs = relational_expression(); - if (lhs.errored) + if (lhs.errored) { return Failure::kErrored; - if (!lhs.matched) + } + if (!lhs.matched) { return Failure::kNoMatch; + } return expect_equality_expr(lhs.value); } @@ -2526,10 +2715,12 @@ Expect ParserImpl::expect_and_expr(const ast::Expression auto source = t.source(); auto rhs = equality_expression(); - if (rhs.errored) + if (rhs.errored) { return Failure::kErrored; - if (!rhs.matched) + } + if (!rhs.matched) { return add_error(peek(), "unable to parse right side of & expression"); + } lhs = create(source, ast::BinaryOp::kAnd, lhs, rhs.value); } @@ -2540,10 +2731,12 @@ Expect ParserImpl::expect_and_expr(const ast::Expression // : equality_expression and_expr Maybe ParserImpl::and_expression() { auto lhs = equality_expression(); - if (lhs.errored) + if (lhs.errored) { return Failure::kErrored; - if (!lhs.matched) + } + if (!lhs.matched) { return Failure::kNoMatch; + } return expect_and_expr(lhs.value); } @@ -2554,14 +2747,17 @@ Maybe ParserImpl::and_expression() { Expect ParserImpl::expect_exclusive_or_expr(const ast::Expression* lhs) { while (continue_parsing()) { Source source; - if (!match(Token::Type::kXor, &source)) + if (!match(Token::Type::kXor, &source)) { return lhs; + } auto rhs = and_expression(); - if (rhs.errored) + if (rhs.errored) { return Failure::kErrored; - if (!rhs.matched) + } + if (!rhs.matched) { return add_error(peek(), "unable to parse right side of ^ expression"); + } lhs = create(source, ast::BinaryOp::kXor, lhs, rhs.value); } @@ -2572,10 +2768,12 @@ Expect ParserImpl::expect_exclusive_or_expr(const ast::E // : and_expression exclusive_or_expr Maybe ParserImpl::exclusive_or_expression() { auto lhs = and_expression(); - if (lhs.errored) + if (lhs.errored) { return Failure::kErrored; - if (!lhs.matched) + } + if (!lhs.matched) { return Failure::kNoMatch; + } return expect_exclusive_or_expr(lhs.value); } @@ -2586,14 +2784,17 @@ Maybe ParserImpl::exclusive_or_expression() { Expect ParserImpl::expect_inclusive_or_expr(const ast::Expression* lhs) { while (continue_parsing()) { Source source; - if (!match(Token::Type::kOr, &source)) + if (!match(Token::Type::kOr, &source)) { return lhs; + } auto rhs = exclusive_or_expression(); - if (rhs.errored) + if (rhs.errored) { return Failure::kErrored; - if (!rhs.matched) + } + if (!rhs.matched) { return add_error(peek(), "unable to parse right side of | expression"); + } lhs = create(source, ast::BinaryOp::kOr, lhs, rhs.value); } @@ -2604,10 +2805,12 @@ Expect ParserImpl::expect_inclusive_or_expr(const ast::E // : exclusive_or_expression inclusive_or_expr Maybe ParserImpl::inclusive_or_expression() { auto lhs = exclusive_or_expression(); - if (lhs.errored) + if (lhs.errored) { return Failure::kErrored; - if (!lhs.matched) + } + if (!lhs.matched) { return Failure::kNoMatch; + } return expect_inclusive_or_expr(lhs.value); } @@ -2625,10 +2828,12 @@ Expect ParserImpl::expect_logical_and_expr(const ast::Ex auto source = t.source(); auto rhs = inclusive_or_expression(); - if (rhs.errored) + if (rhs.errored) { return Failure::kErrored; - if (!rhs.matched) + } + if (!rhs.matched) { return add_error(peek(), "unable to parse right side of && expression"); + } lhs = create(source, ast::BinaryOp::kLogicalAnd, lhs, rhs.value); } @@ -2639,10 +2844,12 @@ Expect ParserImpl::expect_logical_and_expr(const ast::Ex // : inclusive_or_expression logical_and_expr Maybe ParserImpl::logical_and_expression() { auto lhs = inclusive_or_expression(); - if (lhs.errored) + if (lhs.errored) { return Failure::kErrored; - if (!lhs.matched) + } + if (!lhs.matched) { return Failure::kNoMatch; + } return expect_logical_and_expr(lhs.value); } @@ -2653,14 +2860,17 @@ Maybe ParserImpl::logical_and_expression() { Expect ParserImpl::expect_logical_or_expr(const ast::Expression* lhs) { while (continue_parsing()) { Source source; - if (!match(Token::Type::kOrOr, &source)) + if (!match(Token::Type::kOrOr, &source)) { return lhs; + } auto rhs = logical_and_expression(); - if (rhs.errored) + if (rhs.errored) { return Failure::kErrored; - if (!rhs.matched) + } + if (!rhs.matched) { return add_error(peek(), "unable to parse right side of || expression"); + } lhs = create(source, ast::BinaryOp::kLogicalOr, lhs, rhs.value); } @@ -2671,10 +2881,12 @@ Expect ParserImpl::expect_logical_or_expr(const ast::Exp // : logical_and_expression logical_or_expr Maybe ParserImpl::logical_or_expression() { auto lhs = logical_and_expression(); - if (lhs.errored) + if (lhs.errored) { return Failure::kErrored; - if (!lhs.matched) + } + if (!lhs.matched) { return Failure::kNoMatch; + } return expect_logical_or_expr(lhs.value); } @@ -2861,8 +3073,9 @@ Expect ParserImpl::expect_const_expr() { return list; }); - if (params.errored) + if (params.errored) { return Failure::kErrored; + } return builder_.Construct(source, type.value, params.value); } @@ -2885,11 +3098,13 @@ Maybe ParserImpl::attribute_list() { } } - if (errored) + if (errored) { return Failure::kErrored; + } - if (attrs.empty()) + if (attrs.empty()) { return Failure::kNoMatch; + } return attrs; } @@ -2897,10 +3112,12 @@ Maybe ParserImpl::attribute_list() { Expect ParserImpl::expect_attribute() { auto t = peek(); auto attr = attribute(); - if (attr.errored) + if (attr.errored) { return Failure::kErrored; - if (attr.matched) + } + if (attr.matched) { return attr.value; + } return add_error(t, "expected attribute"); } @@ -2916,8 +3133,9 @@ Maybe ParserImpl::attribute() { const char* use = "location attribute"; return expect_paren_block(use, [&]() -> Result { auto val = expect_positive_sint(use); - if (val.errored) + if (val.errored) { return Failure::kErrored; + } return create(t.source(), val.value); }); @@ -2927,8 +3145,9 @@ Maybe ParserImpl::attribute() { const char* use = "binding attribute"; return expect_paren_block(use, [&]() -> Result { auto val = expect_positive_sint(use); - if (val.errored) + if (val.errored) { return Failure::kErrored; + } return create(t.source(), val.value); }); @@ -2938,8 +3157,9 @@ Maybe ParserImpl::attribute() { const char* use = "group attribute"; return expect_paren_block(use, [&]() -> Result { auto val = expect_positive_sint(use); - if (val.errored) + if (val.errored) { return Failure::kErrored; + } return create(t.source(), val.value); }); @@ -2985,8 +3205,9 @@ Maybe ParserImpl::attribute() { if (t == kBuiltinAttribute) { return expect_paren_block("builtin attribute", [&]() -> Result { auto builtin = expect_builtin(); - if (builtin.errored) + if (builtin.errored) { return Failure::kErrored; + } return create(t.source(), builtin.value); }); @@ -3033,8 +3254,9 @@ Maybe ParserImpl::attribute() { if (t == kStageAttribute) { return expect_paren_block("stage attribute", [&]() -> Result { auto stage = expect_pipeline_stage(); - if (stage.errored) + if (stage.errored) { return Failure::kErrored; + } return create(t.source(), stage.value); }); @@ -3044,8 +3266,9 @@ Maybe ParserImpl::attribute() { const char* use = "size attribute"; return expect_paren_block(use, [&]() -> Result { auto val = expect_positive_sint(use); - if (val.errored) + if (val.errored) { return Failure::kErrored; + } return create(t.source(), val.value); }); @@ -3055,8 +3278,9 @@ Maybe ParserImpl::attribute() { const char* use = "align attribute"; return expect_paren_block(use, [&]() -> Result { auto val = expect_positive_sint(use); - if (val.errored) + if (val.errored) { return Failure::kErrored; + } return create(t.source(), val.value); }); @@ -3066,8 +3290,9 @@ Maybe ParserImpl::attribute() { const char* use = "id attribute"; return expect_paren_block(use, [&]() -> Result { auto val = expect_positive_sint(use); - if (val.errored) + if (val.errored) { return Failure::kErrored; + } return create(t.source(), val.value); }); @@ -3087,8 +3312,9 @@ bool ParserImpl::expect_attributes_consumed(ast::AttributeList& in) { bool ParserImpl::match(Token::Type tok, Source* source /*= nullptr*/) { auto t = peek(); - if (source != nullptr) + if (source != nullptr) { *source = t.source(); + } if (t.Is(tok)) { next(); @@ -3157,22 +3383,26 @@ Expect ParserImpl::expect_sint(std::string_view use) { Expect ParserImpl::expect_positive_sint(std::string_view use) { auto sint = expect_sint(use); - if (sint.errored) + if (sint.errored) { return Failure::kErrored; + } - if (sint.value < 0) + if (sint.value < 0) { return add_error(sint.source, std::string(use) + " must be positive"); + } return {static_cast(sint.value), sint.source}; } Expect ParserImpl::expect_nonzero_positive_sint(std::string_view use) { auto sint = expect_sint(use); - if (sint.errored) + if (sint.errored) { return Failure::kErrored; + } - if (sint.value <= 0) + if (sint.value <= 0) { return add_error(sint.source, std::string(use) + " must be greater than 0"); + } return {static_cast(sint.value), sint.source}; } @@ -3205,11 +3435,13 @@ T ParserImpl::expect_block(Token::Type start, Token::Type end, std::string_view return sync(end, [&]() -> T { auto res = body(); - if (res.errored) + if (res.errored) { return Failure::kErrored; + } - if (!expect(use, end)) + if (!expect(use, end)) { return Failure::kErrored; + } return res; }); diff --git a/src/tint/resolver/ctor_conv_intrinsic.cc b/src/tint/resolver/ctor_conv_intrinsic.cc index 5618fbab49..f33bf564e4 100644 --- a/src/tint/resolver/ctor_conv_intrinsic.cc +++ b/src/tint/resolver/ctor_conv_intrinsic.cc @@ -67,4 +67,3 @@ const char* str(CtorConvIntrinsic i) { } } // namespace tint::resolver - diff --git a/src/tint/symbol_table.cc b/src/tint/symbol_table.cc index 0a7fa5c2fd..2b20fed9a0 100644 --- a/src/tint/symbol_table.cc +++ b/src/tint/symbol_table.cc @@ -34,8 +34,9 @@ Symbol SymbolTable::Register(const std::string& name) { TINT_ASSERT(Symbol, !name.empty()); auto it = name_to_symbol_.find(name); - if (it != name_to_symbol_.end()) + if (it != name_to_symbol_.end()) { return it->second; + } #if TINT_SYMBOL_STORE_DEBUG_NAME Symbol sym(next_symbol_, program_id_, name); diff --git a/src/tint/writer/glsl/generator_impl.cc b/src/tint/writer/glsl/generator_impl.cc index 97450796aa..40b43da863 100644 --- a/src/tint/writer/glsl/generator_impl.cc +++ b/src/tint/writer/glsl/generator_impl.cc @@ -1464,8 +1464,9 @@ bool GeneratorImpl::EmitTextureCall(std::ostream& out, out << "("; - if (!EmitExpression(out, texture)) + if (!EmitExpression(out, texture)) { return false; + } out << ", "; @@ -2601,8 +2602,10 @@ bool GeneratorImpl::EmitType(std::ostream& out, if (storage && storage->access() != ast::Access::kRead) { out << "writeonly "; } - auto* subtype = - sampled ? sampled->type() : storage ? storage->type() : ms ? ms->type() : nullptr; + auto* subtype = sampled ? sampled->type() + : storage ? storage->type() + : ms ? ms->type() + : nullptr; if (!subtype || subtype->Is()) { } else if (subtype->Is()) { out << "i"; diff --git a/src/tint/writer/hlsl/generator_impl.cc b/src/tint/writer/hlsl/generator_impl.cc index 45d57a29b3..bf67ff1530 100644 --- a/src/tint/writer/hlsl/generator_impl.cc +++ b/src/tint/writer/hlsl/generator_impl.cc @@ -2324,8 +2324,9 @@ bool GeneratorImpl::EmitTextureCall(std::ostream& out, break; } - if (!EmitExpression(out, texture)) + if (!EmitExpression(out, texture)) { return false; + } // If pack_level_in_coords is true, then the mip level will be appended as the // last value of the coordinates argument. If the WGSL builtin overload does @@ -2397,8 +2398,9 @@ bool GeneratorImpl::EmitTextureCall(std::ostream& out, } if (auto* sampler = arg(Usage::kSampler)) { - if (!EmitExpression(out, sampler)) + if (!EmitExpression(out, sampler)) { return false; + } out << ", "; } diff --git a/src/tint/writer/msl/generator_impl.cc b/src/tint/writer/msl/generator_impl.cc index ce764fb865..3cbd963b7d 100644 --- a/src/tint/writer/msl/generator_impl.cc +++ b/src/tint/writer/msl/generator_impl.cc @@ -1043,8 +1043,9 @@ bool GeneratorImpl::EmitTextureCall(std::ostream& out, } } - if (!EmitExpression(out, e->Declaration())) + if (!EmitExpression(out, e->Declaration())) { return false; + } if (casted) { out << ")"; diff --git a/src/tint/writer/spirv/builder.cc b/src/tint/writer/spirv/builder.cc index 0b3118a9c1..41eb6594bc 100644 --- a/src/tint/writer/spirv/builder.cc +++ b/src/tint/writer/spirv/builder.cc @@ -55,8 +55,9 @@ const char kGLSLstd450[] = "GLSL.std.450"; uint32_t size_of(const InstructionList& instructions) { uint32_t size = 0; - for (const auto& inst : instructions) + for (const auto& inst : instructions) { size += inst.word_length(); + } return size; }