2
* Copyright (C) 2019 Apple Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
7
* 1. Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer.
9
* 2. Redistributions in binary form must reproduce the above copyright
10
* notice, this list of conditions and the following disclaimer in the
11
* documentation and/or other materials provided with the distribution.
13
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23
* THE POSSIBILITY OF SUCH DAMAGE.
27
#include "WHLSLCheckReferenceTypes.h"
32
#include "WHLSLVisitor.h"
38
class ReferenceTypeChecker final : public Visitor {
40
ReferenceTypeChecker() = default;
41
virtual ~ReferenceTypeChecker() = default;
44
ALWAYS_INLINE void checkType(AST::UnnamedType& type, CodeLocation codeLocation, const char* error)
46
auto& unifiedType = type.unifyNode();
47
if (is<AST::ReferenceType>(unifiedType)) {
48
setError(Error(error, codeLocation));
52
if (is<AST::UnnamedType>(unifiedType))
53
checkErrorAndVisit(downcast<AST::UnnamedType>(unifiedType));
56
void visit(AST::ReferenceType& referenceType) override
58
checkType(referenceType.elementType(), referenceType.codeLocation(), "reference type cannot have another reference type as an inner type");
61
void visit(AST::ArrayType& arrayType) override
63
checkType(arrayType.type(), arrayType.codeLocation(), "array type cannot have a reference type as its inner type");
66
void visit(AST::StructureElement& structureElement) override
68
checkType(structureElement.type(), structureElement.codeLocation(), "cannot have a structure field which is a reference type");
71
void visit(AST::Expression& expression) override
73
Visitor::visit(expression);
74
checkErrorAndVisit(expression.resolvedType());
78
Expected<void, Error> checkReferenceTypes(Program& program)
80
// This pass does these things:
81
// 1. Disallow structs to have fields which are references. This prevents us from having to
82
// figure out how to default initialize such a struct. We could relax this in the future if we
83
// did an analysis on which structs contain reference fields, and ensure such struct variables
84
// always have initializers. This would also require us to create constructors for structs which
85
// initialize each field.
86
// 2. We also do the same for arrays.
87
// 3. References can only be one level deep. So no pointers to pointers. No references to
88
// references, etc. This is to support logical mode.
89
ReferenceTypeChecker referenceTypeChecker;
90
referenceTypeChecker.checkErrorAndVisit(program);
91
return referenceTypeChecker.result();
96
} // namespace WebCore
98
#endif // ENABLE(WEBGPU)