|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | +package org.openjdk.jextract.impl; |
| 26 | + |
| 27 | +import org.openjdk.jextract.Declaration; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.HashSet; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Set; |
| 32 | + |
| 33 | +/* |
| 34 | + * This visitor filters duplicate top-level variables, constants and functions. |
| 35 | + */ |
| 36 | +final class DuplicateFilter implements TreeTransformer, Declaration.Visitor<Void, Void> { |
| 37 | + // To detect duplicate Variable and Function declarations. |
| 38 | + private final Set<String> constants = new HashSet<>(); |
| 39 | + private final Set<String> variables = new HashSet<>(); |
| 40 | + private final Set<Declaration.Typedef> typedefs = new HashSet<>(); |
| 41 | + private final Set<Declaration.Function> functions = new HashSet<>(); |
| 42 | + private final List<Declaration> decls = new ArrayList<>(); |
| 43 | + |
| 44 | + // have we seen this Constant earlier? |
| 45 | + private boolean constantSeen(Declaration.Constant tree) { |
| 46 | + return !constants.add(tree.name()); |
| 47 | + } |
| 48 | + |
| 49 | + // have we seen this Variable earlier? |
| 50 | + private boolean variableSeen(Declaration.Variable tree) { |
| 51 | + return !variables.add(tree.name()); |
| 52 | + } |
| 53 | + |
| 54 | + // have we seen this Function earlier? |
| 55 | + private boolean functionSeen(Declaration.Function tree) { |
| 56 | + return !functions.add(tree); |
| 57 | + } |
| 58 | + |
| 59 | + // have we seen this Function earlier? |
| 60 | + private boolean typedefSeen(Declaration.Typedef tree) { |
| 61 | + return !typedefs.add(tree); |
| 62 | + } |
| 63 | + |
| 64 | + DuplicateFilter() { |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Declaration.Scoped transform(Declaration.Scoped header) { |
| 69 | + // Process all header declarations are collect potential |
| 70 | + // declarations that will go into transformed HeaderTree |
| 71 | + // into the this.decls field. |
| 72 | + header.members().forEach(fieldTree -> fieldTree.accept(this, null)); |
| 73 | + return createHeader(header, decls); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Void visitConstant(Declaration.Constant constant, Void ignored) { |
| 78 | + if (constantSeen(constant)) { |
| 79 | + //skip |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + decls.add(constant); |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public Void visitFunction(Declaration.Function funcTree, Void ignored) { |
| 89 | + if (functionSeen(funcTree)) { |
| 90 | + //skip |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + decls.add(funcTree); |
| 95 | + return null; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public Void visitTypedef(Declaration.Typedef tree, Void ignored) { |
| 100 | + if (typedefSeen(tree)) { |
| 101 | + //skip |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + decls.add(tree); |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public Void visitVariable(Declaration.Variable tree, Void ignored) { |
| 111 | + if (variableSeen(tree)) { |
| 112 | + //skip |
| 113 | + return null; |
| 114 | + } |
| 115 | + |
| 116 | + decls.add(tree); |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public Void visitDeclaration(Declaration decl, Void ignored) { |
| 122 | + decls.add(decl); |
| 123 | + return null; |
| 124 | + } |
| 125 | +} |
0 commit comments