| 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 "WHLSLCheckDuplicateFunctions.h" |
| 28 | |
| 29 | #if ENABLE(WEBGPU) |
| 30 | |
| 31 | #include "WHLSLArrayReferenceType.h" |
| 32 | #include "WHLSLArrayType.h" |
| 33 | #include "WHLSLInferTypes.h" |
| 34 | #include "WHLSLTypeReference.h" |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | namespace WHLSL { |
| 39 | |
| 40 | bool checkDuplicateFunctions(const Program& program) |
| 41 | { |
| 42 | Vector<std::reference_wrapper<const AST::FunctionDeclaration>> functions; |
| 43 | for (auto& functionDefinition : program.functionDefinitions()) |
| 44 | functions.append(functionDefinition); |
| 45 | for (auto& nativeFunctionDeclaration : program.nativeFunctionDeclarations()) |
| 46 | functions.append(nativeFunctionDeclaration); |
| 47 | |
| 48 | std::sort(functions.begin(), functions.end(), [](const AST::FunctionDeclaration& a, const AST::FunctionDeclaration& b) -> bool { |
| 49 | if (a.name().length() < b.name().length()) |
| 50 | return true; |
| 51 | if (a.name().length() > b.name().length()) |
| 52 | return false; |
| 53 | for (unsigned i = 0; i < a.name().length(); ++i) { |
| 54 | if (a.name()[i] < b.name()[i]) |
| 55 | return true; |
| 56 | if (a.name()[i] > b.name()[i]) |
| 57 | return false; |
| 58 | } |
| 59 | return false; |
| 60 | }); |
| 61 | for (size_t i = 0; i < functions.size(); ++i) { |
| 62 | for (size_t j = i + 1; j < functions.size(); ++j) { |
| 63 | if (functions[i].get().name() != functions[j].get().name()) |
| 64 | break; |
| 65 | if (is<AST::NativeFunctionDeclaration>(functions[i].get()) && is<AST::NativeFunctionDeclaration>(functions[j].get())) |
| 66 | continue; |
| 67 | if (functions[i].get().parameters().size() != functions[j].get().parameters().size()) |
| 68 | continue; |
| 69 | if (functions[i].get().isCast() && !matches(functions[i].get().type(), functions[j].get().type())) |
| 70 | continue; |
| 71 | bool same = true; |
| 72 | for (size_t k = 0; k < functions[i].get().parameters().size(); ++k) { |
| 73 | if (!matches(*functions[i].get().parameters()[k].type(), *functions[j].get().parameters()[k].type())) { |
| 74 | same = false; |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | if (same) |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | if (functions[i].get().name() == "operator&[]" && functions[i].get().parameters().size() == 2 |
| 83 | && is<AST::ArrayReferenceType>(static_cast<const AST::UnnamedType&>(*functions[i].get().parameters()[0].type()))) { |
| 84 | auto& type = static_cast<const AST::UnnamedType&>(*functions[i].get().parameters()[1].type()); |
| 85 | if (is<AST::TypeReference>(type)) { |
| 86 | if (auto* resolvedType = downcast<AST::TypeReference>(type).resolvedType()) { |
| 87 | if (is<AST::NativeTypeDeclaration>(*resolvedType)) { |
| 88 | auto& nativeTypeDeclaration = downcast<AST::NativeTypeDeclaration>(*resolvedType); |
| 89 | if (nativeTypeDeclaration.name() == "uint" ) |
| 90 | return false; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } else if (functions[i].get().name() == "operator.length" && functions[i].get().parameters().size() == 1 |
| 95 | && (is<AST::ArrayReferenceType>(static_cast<const AST::UnnamedType&>(*functions[i].get().parameters()[0].type())) |
| 96 | || is<AST::ArrayType>(static_cast<const AST::UnnamedType&>(*functions[i].get().parameters()[0].type())))) |
| 97 | return false; |
| 98 | else if (functions[i].get().name() == "operator==" |
| 99 | && functions[i].get().parameters().size() == 2 |
| 100 | && is<AST::ReferenceType>(static_cast<const AST::UnnamedType&>(*functions[i].get().parameters()[0].type())) |
| 101 | && is<AST::ReferenceType>(static_cast<const AST::UnnamedType&>(*functions[i].get().parameters()[1].type())) |
| 102 | && matches(*functions[i].get().parameters()[0].type(), *functions[i].get().parameters()[1].type())) |
| 103 | return false; |
| 104 | } |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | } // namespace WHLSL |
| 109 | |
| 110 | } // namespace WebCore |
| 111 | |
| 112 | #endif // ENABLE(WEBGPU) |
| 113 | |