~mmach/netext73/webkit2gtk

« back to all changes in this revision

Viewing changes to Source/WebCore/Modules/webgpu/WHLSL/WHLSLCheckReferenceTypes.cpp

  • Committer: mmach
  • Date: 2023-06-16 17:21:37 UTC
  • Revision ID: netbit73@gmail.com-20230616172137-2rqx6yr96ga9g3kp
1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2019 Apple Inc. All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions
 
6
 * are met:
 
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.
 
12
 *
 
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.
 
24
 */
 
25
 
 
26
#include "config.h"
 
27
#include "WHLSLCheckReferenceTypes.h"
 
28
 
 
29
#if ENABLE(WEBGPU)
 
30
 
 
31
#include "WHLSLAST.h"
 
32
#include "WHLSLVisitor.h"
 
33
 
 
34
namespace WebCore {
 
35
 
 
36
namespace WHLSL {
 
37
 
 
38
class ReferenceTypeChecker final : public Visitor {
 
39
public:
 
40
    ReferenceTypeChecker() = default;
 
41
    virtual ~ReferenceTypeChecker() = default;
 
42
 
 
43
private:
 
44
    ALWAYS_INLINE void checkType(AST::UnnamedType& type, CodeLocation codeLocation, const char* error)
 
45
    {
 
46
        auto& unifiedType = type.unifyNode();
 
47
        if (is<AST::ReferenceType>(unifiedType)) {
 
48
            setError(Error(error, codeLocation));
 
49
            return;
 
50
        }
 
51
 
 
52
        if (is<AST::UnnamedType>(unifiedType))
 
53
            checkErrorAndVisit(downcast<AST::UnnamedType>(unifiedType));
 
54
    }
 
55
 
 
56
    void visit(AST::ReferenceType& referenceType) override
 
57
    {
 
58
        checkType(referenceType.elementType(), referenceType.codeLocation(), "reference type cannot have another reference type as an inner type");
 
59
    }
 
60
 
 
61
    void visit(AST::ArrayType& arrayType) override
 
62
    {
 
63
        checkType(arrayType.type(), arrayType.codeLocation(), "array type cannot have a reference type as its inner type");
 
64
    }
 
65
 
 
66
    void visit(AST::StructureElement& structureElement) override
 
67
    {
 
68
        checkType(structureElement.type(), structureElement.codeLocation(), "cannot have a structure field which is a reference type");
 
69
    }
 
70
 
 
71
    void visit(AST::Expression& expression) override
 
72
    {
 
73
        Visitor::visit(expression);
 
74
        checkErrorAndVisit(expression.resolvedType());
 
75
    }
 
76
};
 
77
 
 
78
Expected<void, Error> checkReferenceTypes(Program& program)
 
79
{
 
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();
 
92
}
 
93
 
 
94
} // namespace WHLSL
 
95
 
 
96
} // namespace WebCore
 
97
 
 
98
#endif // ENABLE(WEBGPU)