[wgsl-reader] Parsing sampler type

Change-Id: I58a3218a5d0b7ccbe6422340c94cdf3dc1bdf17a
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/28000
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Tomek Ponitka 2020-09-02 13:26:15 +00:00 committed by Commit Bot service account
parent 464928ed91
commit c354200889
9 changed files with 160 additions and 66 deletions

View File

@ -908,6 +908,7 @@ source_set("tint_unittests_wgsl_reader_src") {
"src/reader/wgsl/parser_impl_postfix_expression_test.cc", "src/reader/wgsl/parser_impl_postfix_expression_test.cc",
"src/reader/wgsl/parser_impl_primary_expression_test.cc", "src/reader/wgsl/parser_impl_primary_expression_test.cc",
"src/reader/wgsl/parser_impl_relational_expression_test.cc", "src/reader/wgsl/parser_impl_relational_expression_test.cc",
"src/reader/wgsl/parser_impl_sampler_type_test.cc",
"src/reader/wgsl/parser_impl_shift_expression_test.cc", "src/reader/wgsl/parser_impl_shift_expression_test.cc",
"src/reader/wgsl/parser_impl_statement_test.cc", "src/reader/wgsl/parser_impl_statement_test.cc",
"src/reader/wgsl/parser_impl_statements_test.cc", "src/reader/wgsl/parser_impl_statements_test.cc",

View File

@ -439,6 +439,7 @@ if(${TINT_BUILD_WGSL_READER})
reader/wgsl/parser_impl_postfix_expression_test.cc reader/wgsl/parser_impl_postfix_expression_test.cc
reader/wgsl/parser_impl_primary_expression_test.cc reader/wgsl/parser_impl_primary_expression_test.cc
reader/wgsl/parser_impl_relational_expression_test.cc reader/wgsl/parser_impl_relational_expression_test.cc
reader/wgsl/parser_impl_sampler_type_test.cc
reader/wgsl/parser_impl_shift_expression_test.cc reader/wgsl/parser_impl_shift_expression_test.cc
reader/wgsl/parser_impl_statement_test.cc reader/wgsl/parser_impl_statement_test.cc
reader/wgsl/parser_impl_statements_test.cc reader/wgsl/parser_impl_statements_test.cc

View File

@ -561,6 +561,10 @@ Token Lexer::check_keyword(const Source& source, const std::string& str) {
return {Token::Type::kPtr, source, "ptr"}; return {Token::Type::kPtr, source, "ptr"};
if (str == "return") if (str == "return")
return {Token::Type::kReturn, source, "return"}; return {Token::Type::kReturn, source, "return"};
if (str == "sampler")
return {Token::Type::kSampler, source, "sampler"};
if (str == "sampler_comparison")
return {Token::Type::kComparisonSampler, source, "sampler_comparison"};
if (str == "set") if (str == "set")
return {Token::Type::kSet, source, "set"}; return {Token::Type::kSet, source, "set"};
if (str == "storage_buffer") if (str == "storage_buffer")

View File

@ -411,7 +411,8 @@ TEST_P(KeywordTest, Parses) {
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(
LexerTest, LexerTest,
KeywordTest, KeywordTest,
testing::Values(TokenData{"array", Token::Type::kArray}, testing::Values(
TokenData{"array", Token::Type::kArray},
TokenData{"as", Token::Type::kAs}, TokenData{"as", Token::Type::kAs},
TokenData{"binding", Token::Type::kBinding}, TokenData{"binding", Token::Type::kBinding},
TokenData{"block", Token::Type::kBlock}, TokenData{"block", Token::Type::kBlock},
@ -457,6 +458,8 @@ INSTANTIATE_TEST_SUITE_P(
TokenData{"private", Token::Type::kPrivate}, TokenData{"private", Token::Type::kPrivate},
TokenData{"ptr", Token::Type::kPtr}, TokenData{"ptr", Token::Type::kPtr},
TokenData{"return", Token::Type::kReturn}, TokenData{"return", Token::Type::kReturn},
TokenData{"sampler", Token::Type::kSampler},
TokenData{"sampler_comparison", Token::Type::kComparisonSampler},
TokenData{"set", Token::Type::kSet}, TokenData{"set", Token::Type::kSet},
TokenData{"storage_buffer", Token::Type::kStorageBuffer}, TokenData{"storage_buffer", Token::Type::kStorageBuffer},
TokenData{"stride", Token::Type::kStride}, TokenData{"stride", Token::Type::kStride},
@ -466,8 +469,7 @@ INSTANTIATE_TEST_SUITE_P(
TokenData{"type", Token::Type::kType}, TokenData{"type", Token::Type::kType},
TokenData{"u32", Token::Type::kU32}, TokenData{"u32", Token::Type::kU32},
TokenData{"uniform", Token::Type::kUniform}, TokenData{"uniform", Token::Type::kUniform},
TokenData{"uniform_constant", TokenData{"uniform_constant", Token::Type::kUniformConstant},
Token::Type::kUniformConstant},
TokenData{"var", Token::Type::kVar}, TokenData{"var", Token::Type::kVar},
TokenData{"vec2", Token::Type::kVec2}, TokenData{"vec2", Token::Type::kVec2},
TokenData{"vec3", Token::Type::kVec3}, TokenData{"vec3", Token::Type::kVec3},

View File

@ -50,6 +50,7 @@
#include "src/ast/type/i32_type.h" #include "src/ast/type/i32_type.h"
#include "src/ast/type/matrix_type.h" #include "src/ast/type/matrix_type.h"
#include "src/ast/type/pointer_type.h" #include "src/ast/type/pointer_type.h"
#include "src/ast/type/sampler_type.h"
#include "src/ast/type/struct_type.h" #include "src/ast/type/struct_type.h"
#include "src/ast/type/u32_type.h" #include "src/ast/type/u32_type.h"
#include "src/ast/type/vector_type.h" #include "src/ast/type/vector_type.h"
@ -575,6 +576,24 @@ std::unique_ptr<ast::Variable> ParserImpl::variable_decl() {
return std::make_unique<ast::Variable>(source, name, sc, type); return std::make_unique<ast::Variable>(source, name, sc, type);
} }
// sampler_type
// : SAMPLER
// | SAMPLER_COMPARISON
ast::type::Type* ParserImpl::sampler_type() {
auto t = peek();
if (t.IsSampler()) {
next(); // Consume the peek
return ctx_.type_mgr().Get(std::make_unique<ast::type::SamplerType>(
ast::type::SamplerKind::kSampler));
}
if (t.IsComparisonSampler()) {
next(); // Consume the peek
return ctx_.type_mgr().Get(std::make_unique<ast::type::SamplerType>(
ast::type::SamplerKind::kComparisonSampler));
}
return nullptr;
}
// variable_ident_decl // variable_ident_decl
// : IDENT COLON type_decl // : IDENT COLON type_decl
std::pair<std::string, ast::type::Type*> ParserImpl::variable_ident_decl() { std::pair<std::string, ast::type::Type*> ParserImpl::variable_ident_decl() {

View File

@ -150,8 +150,7 @@ class ParserImpl {
/// @returns the type alias or nullptr on error /// @returns the type alias or nullptr on error
ast::type::AliasType* type_alias(); ast::type::AliasType* type_alias();
/// Parses a `type_decl` grammar element /// Parses a `type_decl` grammar element
/// @returns the parsed Type or nullptr if none matched. The returned type /// @returns the parsed Type or nullptr if none matched.
// is owned by the TypeManager.
ast::type::Type* type_decl(); ast::type::Type* type_decl();
/// Parses a `storage_class` grammar element /// Parses a `storage_class` grammar element
/// @returns the storage class or StorageClass::kNone if none matched /// @returns the storage class or StorageClass::kNone if none matched
@ -181,6 +180,9 @@ class ParserImpl {
/// Parses a `function_decl` grammar element /// Parses a `function_decl` grammar element
/// @returns the parsed function, nullptr otherwise /// @returns the parsed function, nullptr otherwise
std::unique_ptr<ast::Function> function_decl(); std::unique_ptr<ast::Function> function_decl();
/// Parses a `sampler_type` grammar element
/// @returns the parsed Type or nullptr if none matched.
ast::type::Type* sampler_type();
/// Parses a `function_type_decl` grammar element /// Parses a `function_type_decl` grammar element
/// @returns the parsed type or nullptr otherwise /// @returns the parsed type or nullptr otherwise
ast::type::Type* function_type_decl(); ast::type::Type* function_type_decl();

View File

@ -0,0 +1,53 @@
// Copyright 2020 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "gtest/gtest.h"
#include "src/ast/type/sampler_type.h"
#include "src/reader/wgsl/parser_impl.h"
#include "src/reader/wgsl/parser_impl_test_helper.h"
namespace tint {
namespace reader {
namespace wgsl {
namespace {
TEST_F(ParserImplTest, SamplerType_Invalid) {
auto* p = parser("1234");
auto* t = p->sampler_type();
EXPECT_EQ(t, nullptr);
EXPECT_FALSE(p->has_error());
}
TEST_F(ParserImplTest, SamplerType_Sampler) {
auto* p = parser("sampler");
auto* t = p->sampler_type();
ASSERT_NE(t, nullptr);
ASSERT_TRUE(t->IsSampler());
EXPECT_FALSE(t->AsSampler()->IsComparison());
EXPECT_FALSE(p->has_error());
}
TEST_F(ParserImplTest, SamplerType_ComparisonSampler) {
auto* p = parser("sampler_comparison");
auto* t = p->sampler_type();
ASSERT_NE(t, nullptr);
ASSERT_TRUE(t->IsSampler());
EXPECT_TRUE(t->AsSampler()->IsComparison());
EXPECT_FALSE(p->has_error());
}
} // namespace
} // namespace wgsl
} // namespace reader
} // namespace tint

View File

@ -197,6 +197,10 @@ std::string Token::TypeToName(Type type) {
return "ptr"; return "ptr";
case Token::Type::kReturn: case Token::Type::kReturn:
return "return"; return "return";
case Token::Type::kSampler:
return "sampler";
case Token::Type::kComparisonSampler:
return "sampler_comparison";
case Token::Type::kSet: case Token::Type::kSet:
return "set"; return "set";
case Token::Type::kStorageBuffer: case Token::Type::kStorageBuffer:

View File

@ -208,6 +208,10 @@ class Token {
kPtr, kPtr,
/// A 'return' /// A 'return'
kReturn, kReturn,
/// A 'sampler'
kSampler,
/// A 'sampler_comparison'
kComparisonSampler,
/// A 'set' /// A 'set'
kSet, kSet,
/// A 'storage_buffer' /// A 'storage_buffer'
@ -465,6 +469,10 @@ class Token {
bool IsPtr() const { return type_ == Type::kPtr; } bool IsPtr() const { return type_ == Type::kPtr; }
/// @returns true if token is a 'return' /// @returns true if token is a 'return'
bool IsReturn() const { return type_ == Type::kReturn; } bool IsReturn() const { return type_ == Type::kReturn; }
/// @returns true if token is a 'sampler'
bool IsSampler() const { return type_ == Type::kSampler; }
/// @returns true if token is a 'sampler_comparison'
bool IsComparisonSampler() const { return type_ == Type::kComparisonSampler; }
/// @returns true if token is a 'set' /// @returns true if token is a 'set'
bool IsSet() const { return type_ == Type::kSet; } bool IsSet() const { return type_ == Type::kSet; }
/// @returns true if token is a 'storage_buffer' /// @returns true if token is a 'storage_buffer'