WHLSLGatherEntryPointItems.cpp [plain text]
#include "config.h"
#include "WHLSLGatherEntryPointItems.h"
#if ENABLE(WEBGPU)
#include "WHLSLArrayReferenceType.h"
#include "WHLSLArrayType.h"
#include "WHLSLFunctionDefinition.h"
#include "WHLSLInferTypes.h"
#include "WHLSLIntrinsics.h"
#include "WHLSLPointerType.h"
#include "WHLSLStructureDefinition.h"
#include "WHLSLTypeDefinition.h"
#include "WHLSLTypeReference.h"
#include "WHLSLVariableDeclaration.h"
#include "WHLSLVisitor.h"
#include <wtf/Optional.h>
namespace WebCore {
namespace WHLSL {
class Gatherer : public Visitor {
public:
Gatherer(const Intrinsics& intrinsics, AST::Semantic* semantic = nullptr)
: m_intrinsics(intrinsics)
, m_currentSemantic(semantic)
{
}
virtual ~Gatherer() = default;
void reset()
{
m_currentSemantic = nullptr;
m_currentVariableLocation = { };
}
Vector<EntryPointItem> takeEntryPointItems()
{
return WTFMove(m_entryPointItems);
}
void visit(AST::EnumerationDefinition&)
{
if (!m_currentSemantic) {
setError(Error("Expected semantic for entrypoint argument.", m_currentVariableLocation));
return;
}
m_entryPointItems.append(EntryPointItem(m_typeReferences.last().get(), *m_currentSemantic, m_path));
}
void visit(AST::NativeTypeDeclaration& nativeTypeDeclaration)
{
if (!m_currentSemantic) {
setError(Error("Expected semantic for entrypoint argument.", m_currentVariableLocation));
return;
}
if (matches(nativeTypeDeclaration, m_intrinsics.voidType())) {
setError(Error("Unexpected void type for entrypoint argument.", m_currentVariableLocation));
return;
}
m_entryPointItems.append(EntryPointItem(m_typeReferences.last().get(), *m_currentSemantic, m_path));
}
void visit(AST::StructureDefinition& structureDefinition)
{
if (m_currentSemantic) {
setError(Error("Unexpected semantic for struct entrypoint argument.", m_currentVariableLocation));
return;
}
for (auto& structureElement : structureDefinition.structureElements()) {
if (structureElement.semantic())
m_currentSemantic = structureElement.semantic();
m_path.append(structureElement.name());
checkErrorAndVisit(structureElement);
m_path.takeLast();
}
}
void visit(AST::TypeDefinition& typeDefinition)
{
checkErrorAndVisit(typeDefinition.type());
}
void visit(AST::TypeReference& typeReference)
{
m_typeReferences.append(typeReference);
auto depth = m_typeReferences.size();
checkErrorAndVisit(typeReference.resolvedType());
ASSERT_UNUSED(depth, m_typeReferences.size() == depth);
m_typeReferences.removeLast();
}
void visit(AST::PointerType& pointerType)
{
if (!m_currentSemantic) {
setError(Error("Expected semantic for entrypoint argument.", m_currentVariableLocation));
return;
}
m_entryPointItems.append(EntryPointItem(pointerType, *m_currentSemantic, m_path));
}
void visit(AST::ArrayReferenceType& arrayReferenceType)
{
if (!m_currentSemantic) {
setError(Error("Expected semantic for entrypoint argument.", m_currentVariableLocation));
return;
}
m_entryPointItems.append(EntryPointItem(arrayReferenceType, *m_currentSemantic, m_path));
}
void visit(AST::ArrayType& arrayType)
{
if (!m_currentSemantic) {
setError(Error("Expected semantic for entrypoint argument.", m_currentVariableLocation));
return;
}
m_entryPointItems.append(EntryPointItem(arrayType, *m_currentSemantic, m_path));
}
void visit(AST::VariableDeclaration& variableDeclaration)
{
ASSERT(!m_currentSemantic);
m_currentVariableLocation = variableDeclaration.codeLocation();
if (variableDeclaration.semantic())
m_currentSemantic = variableDeclaration.semantic();
ASSERT(variableDeclaration.type());
m_path.append(variableDeclaration.name());
checkErrorAndVisit(*variableDeclaration.type());
m_path.takeLast();
}
private:
Vector<String> m_path;
const Intrinsics& m_intrinsics;
AST::Semantic* m_currentSemantic { nullptr };
CodeLocation m_currentVariableLocation;
Vector<std::reference_wrapper<AST::TypeReference>> m_typeReferences;
Vector<EntryPointItem> m_entryPointItems;
};
Expected<EntryPointItems, Error> gatherEntryPointItems(const Intrinsics& intrinsics, AST::FunctionDefinition& functionDefinition)
{
ASSERT(functionDefinition.entryPointType());
Gatherer inputGatherer(intrinsics);
for (auto& parameter : functionDefinition.parameters()) {
inputGatherer.reset();
inputGatherer.checkErrorAndVisit(parameter);
if (inputGatherer.hasError())
return makeUnexpected(inputGatherer.result().error());
}
Gatherer outputGatherer(intrinsics, functionDefinition.semantic());
if (*functionDefinition.entryPointType() != AST::EntryPointType::Compute)
outputGatherer.checkErrorAndVisit(functionDefinition.type());
if (outputGatherer.hasError())
return makeUnexpected(outputGatherer.result().error());
return {{ inputGatherer.takeEntryPointItems(), outputGatherer.takeEntryPointItems() }};
}
}
}
#endif // ENABLE(WEBGPU)