Skip to content

Commit

Permalink
[WGSL] Use UniqueRefVector to simplify working with AST
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=246560
rdar://problem/101198284

Reviewed by Dean Jackson.

Use UniqueRefVector for lists of AST nodes to eliminate the need to unwrap
UniqueRef<> with .get().

Tested by TestWebKitAPI/Tests/WGSL/ParserTests.cpp

* Source/WebGPU/WGSL/AST/Attribute.h:
* Source/WebGPU/WGSL/AST/Decl.h:
(WGSL::AST::Decl::~Decl):
* Source/WebGPU/WGSL/AST/Expression.h:
* Source/WebGPU/WGSL/AST/Expressions/ArrayAccess.h:
* Source/WebGPU/WGSL/AST/Expressions/CallableExpression.h:
* Source/WebGPU/WGSL/AST/FunctionDecl.h:
* Source/WebGPU/WGSL/AST/GlobalDirective.h:
* Source/WebGPU/WGSL/AST/ShaderModule.h:
* Source/WebGPU/WGSL/AST/Statement.h:
* Source/WebGPU/WGSL/AST/Statements/CompoundStatement.h:
* Source/WebGPU/WGSL/AST/StructureDecl.h:
* Source/WebGPU/WGSL/AST/VariableDecl.h:
* Source/WebGPU/WGSL/Parser.cpp:
(WGSL::Parser<Lexer>::parseShader):
(WGSL::Parser<Lexer>::parseAttributes):
(WGSL::Parser<Lexer>::parseStructDecl):
(WGSL::Parser<Lexer>::parseVariableDecl):
(WGSL::Parser<Lexer>::parseVariableDeclWithAttributes):
(WGSL::Parser<Lexer>::parseFunctionDecl):
(WGSL::Parser<Lexer>::parseStatement):
(WGSL::Parser<Lexer>::parseCompoundStatement):
(WGSL::Parser<Lexer>::parseArgumentExpressionList):
* Source/WebGPU/WGSL/ParserPrivate.h:
* Tools/TestWebKitAPI/Tests/WGSL/ParserTests.cpp:
(TestWGSLAPI::TEST):

Canonical link: https://commits.webkit.org/255597@main
  • Loading branch information
djg committed Oct 16, 2022
1 parent 6adb4a8 commit 8b4319b
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 145 deletions.
17 changes: 12 additions & 5 deletions Source/WebGPU/WGSL/AST/Attribute.h
Expand Up @@ -26,14 +26,17 @@
#pragma once

#include "ASTNode.h"
#include <wtf/text/StringView.h>

#include <wtf/UniqueRef.h>
#include <wtf/UniqueRefVector.h>
#include <wtf/Vector.h>
#include <wtf/text/StringView.h>

namespace WGSL::AST {

class Attribute : public ASTNode {
WTF_MAKE_FAST_ALLOCATED;

public:
enum class Kind {
Group,
Expand All @@ -43,14 +46,16 @@ class Attribute : public ASTNode {
Builtin,
};

using List = UniqueRefVector<Attribute, 2>;

Attribute(SourceSpan span)
: ASTNode(span)
{
}

virtual ~Attribute() {};

virtual Kind kind() const = 0 ;
virtual Kind kind() const = 0;
bool isGroup() const { return kind() == Kind::Group; }
bool isBinding() const { return kind() == Kind::Binding; }
bool isStage() const { return kind() == Kind::Stage; }
Expand All @@ -60,6 +65,7 @@ class Attribute : public ASTNode {

class GroupAttribute final : public Attribute {
WTF_MAKE_FAST_ALLOCATED;

public:
GroupAttribute(SourceSpan span, unsigned group)
: Attribute(span)
Expand All @@ -76,6 +82,7 @@ class GroupAttribute final : public Attribute {

class BindingAttribute final : public Attribute {
WTF_MAKE_FAST_ALLOCATED;

public:
BindingAttribute(SourceSpan span, unsigned binding)
: Attribute(span)
Expand All @@ -92,6 +99,7 @@ class BindingAttribute final : public Attribute {

class StageAttribute final : public Attribute {
WTF_MAKE_FAST_ALLOCATED;

public:
enum class Stage : uint8_t {
Compute,
Expand All @@ -114,6 +122,7 @@ class StageAttribute final : public Attribute {

class BuiltinAttribute final : public Attribute {
WTF_MAKE_FAST_ALLOCATED;

public:
BuiltinAttribute(SourceSpan span, StringView name)
: Attribute(span)
Expand All @@ -130,6 +139,7 @@ class BuiltinAttribute final : public Attribute {

class LocationAttribute final : public Attribute {
WTF_MAKE_FAST_ALLOCATED;

public:
LocationAttribute(SourceSpan span, unsigned value)
: Attribute(span)
Expand All @@ -144,8 +154,6 @@ class LocationAttribute final : public Attribute {
unsigned m_value;
};

using Attributes = Vector<UniqueRef<Attribute>, 2>;

} // namespace WGSL::AST

#define SPECIALIZE_TYPE_TRAITS_WGSL_ATTRIBUTE(ToValueTypeName, predicate) \
Expand All @@ -158,4 +166,3 @@ SPECIALIZE_TYPE_TRAITS_WGSL_ATTRIBUTE(BindingAttribute, isBinding())
SPECIALIZE_TYPE_TRAITS_WGSL_ATTRIBUTE(StageAttribute, isStage())
SPECIALIZE_TYPE_TRAITS_WGSL_ATTRIBUTE(LocationAttribute, isLocation())
SPECIALIZE_TYPE_TRAITS_WGSL_ATTRIBUTE(BuiltinAttribute, isBuiltin())

5 changes: 5 additions & 0 deletions Source/WebGPU/WGSL/AST/Decl.h
Expand Up @@ -26,19 +26,24 @@
#pragma once

#include "ASTNode.h"

#include <wtf/TypeCasts.h>
#include <wtf/UniqueRefVector.h>

namespace WGSL::AST {

class Decl : public ASTNode {
WTF_MAKE_FAST_ALLOCATED;

public:
enum class Kind {
Variable,
Struct,
Function,
};

using List = UniqueRefVector<Decl>;

Decl(SourceSpan span)
: ASTNode(span)
{
Expand Down
7 changes: 6 additions & 1 deletion Source/WebGPU/WGSL/AST/Expression.h
Expand Up @@ -26,12 +26,15 @@
#pragma once

#include "ASTNode.h"

#include <wtf/TypeCasts.h>
#include <wtf/UniqueRefVector.h>

namespace WGSL::AST {

class Expression : public ASTNode {
WTF_MAKE_FAST_ALLOCATED;

public:
enum class Kind {
BoolLiteral,
Expand All @@ -47,14 +50,16 @@ class Expression : public ASTNode {
UnaryExpression,
};

using List = UniqueRefVector<Expression>;

Expression(SourceSpan span)
: ASTNode(span)
{
}

virtual ~Expression() {}

virtual Kind kind() const = 0;
virtual Kind kind() const = 0;
bool isBoolLiteral() const { return kind() == Kind::BoolLiteral; }
bool isInt32Literal() const { return kind() == Kind::Int32Literal; }
bool isUInt32Literal() const { return kind() == Kind::Uint32Literal; }
Expand Down
4 changes: 2 additions & 2 deletions Source/WebGPU/WGSL/AST/Expressions/ArrayAccess.h
Expand Up @@ -41,8 +41,8 @@ class ArrayAccess final : public Expression {
}

Kind kind() const override { return Kind::ArrayAccess; }
UniqueRef<Expression>& base() { return m_base; }
UniqueRef<Expression>& index() { return m_index; }
Expression& base() { return m_base.get(); }
Expression& index() { return m_index.get(); }

private:
UniqueRef<Expression> m_base;
Expand Down
15 changes: 4 additions & 11 deletions Source/WebGPU/WGSL/AST/Expressions/CallableExpression.h
Expand Up @@ -38,8 +38,9 @@ namespace WGSL::AST {
// kind of expression can only be resolved during semantic analysis.
class CallableExpression final : public Expression {
WTF_MAKE_FAST_ALLOCATED;

public:
CallableExpression(SourceSpan span, UniqueRef<TypeDecl>&& target, Vector<UniqueRef<Expression>>&& arguments)
CallableExpression(SourceSpan span, UniqueRef<TypeDecl>&& target, Expression::List&& arguments)
: Expression(span)
, m_target(WTFMove(target))
, m_arguments(WTFMove(arguments))
Expand All @@ -48,23 +49,15 @@ class CallableExpression final : public Expression {

Kind kind() const override { return Kind::CallableExpression; }
const TypeDecl& target() const { return m_target; }
Vector<std::reference_wrapper<const Expression>> arguments() const
{
Vector<std::reference_wrapper<const Expression>> arguments;

for (const auto& argument : m_arguments)
arguments.append(std::cref(argument.get()));

return arguments;
}
const Expression::List& arguments() const { return m_arguments; }

private:
// If m_target is a NamedType, it could either be a:
// * Type that does not accept parameters (bool, i32, u32, ...)
// * Identifier that refers to a type alias.
// * Identifier that refers to a function.
UniqueRef<TypeDecl> m_target;
Vector<UniqueRef<Expression>> m_arguments;
Expression::List m_arguments;
};

} // namespace WGSL::AST
Expand Down
28 changes: 18 additions & 10 deletions Source/WebGPU/WGSL/AST/FunctionDecl.h
Expand Up @@ -32,12 +32,17 @@
#include "Statements/CompoundStatement.h"
#include "TypeDecl.h"

#include <wtf/UniqueRefVector.h>

namespace WGSL::AST {

class Parameter final : public ASTNode {
WTF_MAKE_FAST_ALLOCATED;

public:
Parameter(SourceSpan span, StringView name, UniqueRef<TypeDecl>&& type, Attributes&& attributes)
using List = UniqueRefVector<Parameter>;

Parameter(SourceSpan span, StringView name, UniqueRef<TypeDecl>&& type, Attribute::List&& attributes)
: ASTNode(span)
, m_name(WTFMove(name))
, m_type(WTFMove(type))
Expand All @@ -47,18 +52,21 @@ class Parameter final : public ASTNode {

const StringView& name() const { return m_name; }
TypeDecl& type() { return m_type; }
Attributes& attributes() { return m_attributes; }
Attribute::List& attributes() { return m_attributes; }

private:
StringView m_name;
UniqueRef<TypeDecl> m_type;
Attributes m_attributes;
Attribute::List m_attributes;
};

class FunctionDecl final : public Decl {
WTF_MAKE_FAST_ALLOCATED;

public:
FunctionDecl(SourceSpan sourceSpan, StringView name, Vector<UniqueRef<Parameter>>&& parameters, std::unique_ptr<TypeDecl>&& returnType, CompoundStatement&& body, Attributes&& attributes, Attributes&& returnAttributes)
using List = UniqueRefVector<FunctionDecl>;

FunctionDecl(SourceSpan sourceSpan, StringView name, Parameter::List&& parameters, std::unique_ptr<TypeDecl>&& returnType, CompoundStatement&& body, Attribute::List&& attributes, Attribute::List&& returnAttributes)
: Decl(sourceSpan)
, m_name(name)
, m_parameters(WTFMove(parameters))
Expand All @@ -71,17 +79,17 @@ class FunctionDecl final : public Decl {

Kind kind() const override { return Kind::Function; }
const StringView& name() const { return m_name; }
Vector<UniqueRef<Parameter>>& parameters() { return m_parameters; }
Attributes& attributes() { return m_attributes; }
Attributes& returnAttributes() { return m_returnAttributes; }
Parameter::List& parameters() { return m_parameters; }
Attribute::List& attributes() { return m_attributes; }
Attribute::List& returnAttributes() { return m_returnAttributes; }
TypeDecl* maybeReturnType() { return m_returnType.get(); }
CompoundStatement& body() { return m_body; }

private:
StringView m_name;
Vector<UniqueRef<Parameter>> m_parameters;
Attributes m_attributes;
Attributes m_returnAttributes;
Parameter::List m_parameters;
Attribute::List m_attributes;
Attribute::List m_returnAttributes;
std::unique_ptr<TypeDecl> m_returnType;
CompoundStatement m_body;
};
Expand Down
3 changes: 3 additions & 0 deletions Source/WebGPU/WGSL/AST/GlobalDirective.h
Expand Up @@ -32,7 +32,10 @@ namespace WGSL::AST {

class GlobalDirective : public ASTNode {
WTF_MAKE_FAST_ALLOCATED;

public:
using List = UniqueRefVector<GlobalDirective>;

GlobalDirective(SourceSpan span, StringView name)
: ASTNode(span)
, m_name(name)
Expand Down
30 changes: 16 additions & 14 deletions Source/WebGPU/WGSL/AST/ShaderModule.h
Expand Up @@ -39,41 +39,43 @@ namespace WGSL::AST {

class ShaderModule final : ASTNode {
WTF_MAKE_FAST_ALLOCATED;

public:
ShaderModule(SourceSpan span, Vector<UniqueRef<GlobalDirective>>&& directives, Vector<UniqueRef<Decl>>&& decls)
ShaderModule(SourceSpan span, GlobalDirective::List&& directives, Decl::List&& decls)
: ASTNode(span)
, m_directives(WTFMove(directives))
{
for (auto& decl : decls) {
if (is<StructDecl>(decl.get())) {
for (size_t i = decls.size(); i > 0; --i) {
UniqueRef<Decl> decl = decls.takeLast();
if (is<StructDecl>(decl)) {
// We want to go from UniqueRef<BaseClass> to UniqueRef<DerivedClass>, but that is not supported by downcast.
// So instead we do UniqueRef -> unique_ptr -> raw_pointer -(downcast)-> raw_pointer -> unique_ptr -> UniqueRef...
Decl* rawPtr = decl.moveToUniquePtr().release();
StructDecl* downcastedRawPtr = downcast<StructDecl>(rawPtr);
m_structs.append(makeUniqueRefFromNonNullUniquePtr(std::unique_ptr<StructDecl>(downcastedRawPtr)));
m_structs.insert(0, makeUniqueRefFromNonNullUniquePtr(std::unique_ptr<StructDecl>(downcastedRawPtr)));
} else if (is<VariableDecl>(decl.get())) {
Decl* rawPtr = decl.moveToUniquePtr().release();
VariableDecl* downcastedRawPtr = downcast<VariableDecl>(rawPtr);
m_globalVars.append(makeUniqueRefFromNonNullUniquePtr(std::unique_ptr<VariableDecl>(downcastedRawPtr)));
m_globalVars.insert(0, makeUniqueRefFromNonNullUniquePtr(std::unique_ptr<VariableDecl>(downcastedRawPtr)));
} else {
Decl* rawPtr = decl.moveToUniquePtr().release();
FunctionDecl* func = downcast<FunctionDecl>(rawPtr);
m_functions.append(makeUniqueRefFromNonNullUniquePtr(std::unique_ptr<FunctionDecl>(func)));
m_functions.insert(0, makeUniqueRefFromNonNullUniquePtr(std::unique_ptr<FunctionDecl>(func)));
}
}
}

Vector<UniqueRef<GlobalDirective>>& directives() { return m_directives; }
Vector<UniqueRef<StructDecl>>& structs() { return m_structs; }
Vector<UniqueRef<VariableDecl>>& globalVars() { return m_globalVars; }
Vector<UniqueRef<FunctionDecl>>& functions() { return m_functions; }
GlobalDirective::List& directives() { return m_directives; }
StructDecl::List& structs() { return m_structs; }
VariableDecl::List& globalVars() { return m_globalVars; }
FunctionDecl::List& functions() { return m_functions; }

private:
Vector<UniqueRef<GlobalDirective>> m_directives;
GlobalDirective::List m_directives;

Vector<UniqueRef<StructDecl>> m_structs;
Vector<UniqueRef<VariableDecl>> m_globalVars;
Vector<UniqueRef<FunctionDecl>> m_functions;
StructDecl::List m_structs;
VariableDecl::List m_globalVars;
FunctionDecl::List m_functions;
};

} // namespace WGSL::AST
2 changes: 2 additions & 0 deletions Source/WebGPU/WGSL/AST/Statement.h
Expand Up @@ -41,6 +41,8 @@ class Statement : public ASTNode {
Variable,
};

using List = UniqueRefVector<Statement>;

Statement(SourceSpan span)
: ASTNode(span)
{
Expand Down
7 changes: 4 additions & 3 deletions Source/WebGPU/WGSL/AST/Statements/CompoundStatement.h
Expand Up @@ -31,18 +31,19 @@ namespace WGSL::AST {

class CompoundStatement final : public Statement {
WTF_MAKE_FAST_ALLOCATED;

public:
CompoundStatement(SourceSpan span, Vector<UniqueRef<Statement>>&& statements)
CompoundStatement(SourceSpan span, Statement::List&& statements)
: Statement(span)
, m_statements(WTFMove(statements))
{
}

Kind kind() const override { return Kind::Compound; }
Vector<UniqueRef<Statement>>& statements() { return m_statements; }
Statement::List& statements() { return m_statements; }

private:
Vector<UniqueRef<Statement>> m_statements;
Statement::List m_statements;
};

} // namespace WGSL::AST
Expand Down

0 comments on commit 8b4319b

Please sign in to comment.