|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) 2011-2024 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonar.python.semantic.v2.types; |
| 21 | + |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.Iterator; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Set; |
| 26 | +import java.util.stream.Collectors; |
| 27 | +import org.sonar.python.semantic.v2.SymbolV2; |
| 28 | +import org.sonar.python.semantic.v2.TypeTable; |
| 29 | +import org.sonar.python.types.v2.PythonType; |
| 30 | + |
| 31 | +public class AstBasedPropagation { |
| 32 | + private final Map<SymbolV2, Set<Propagation>> propagationsByLhs; |
| 33 | + private final TypeTable typeTable; |
| 34 | + |
| 35 | + public AstBasedPropagation(Map<SymbolV2, Set<Propagation>> propagationsByLhs, TypeTable typeTable) { |
| 36 | + this.propagationsByLhs = propagationsByLhs; |
| 37 | + this.typeTable = typeTable; |
| 38 | + } |
| 39 | + |
| 40 | + public Map<SymbolV2, Set<PythonType>> processPropagations(Set<SymbolV2> trackedVars) { |
| 41 | + Set<Propagation> propagations = new HashSet<>(); |
| 42 | + Set<SymbolV2> initializedVars = new HashSet<>(); |
| 43 | + |
| 44 | + propagationsByLhs.forEach((lhs, props) -> { |
| 45 | + if (trackedVars.contains(lhs)) { |
| 46 | + props.stream() |
| 47 | + .filter(Assignment.class::isInstance) |
| 48 | + .map(Assignment.class::cast) |
| 49 | + .forEach(a -> a.computeDependencies(trackedVars)); |
| 50 | + propagations.addAll(props); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + applyPropagations(propagations, initializedVars, true); |
| 55 | + applyPropagations(propagations, initializedVars, false); |
| 56 | + return propagations.stream().collect(Collectors.groupingBy(Propagation::lhsSymbol, Collectors.mapping(Propagation::rhsType, Collectors.toSet()))); |
| 57 | + } |
| 58 | + |
| 59 | + private static void applyPropagations(Set<Propagation> propagations, Set<SymbolV2> initializedVars, boolean checkDependenciesReadiness) { |
| 60 | + Set<Propagation> workSet = new HashSet<>(propagations); |
| 61 | + while (!workSet.isEmpty()) { |
| 62 | + Iterator<Propagation> iterator = workSet.iterator(); |
| 63 | + Propagation propagation = iterator.next(); |
| 64 | + iterator.remove(); |
| 65 | + if (!checkDependenciesReadiness || propagation.areDependenciesReady(initializedVars)) { |
| 66 | + boolean learnt = propagation.propagate(initializedVars); |
| 67 | + if (learnt) { |
| 68 | + workSet.addAll(propagation.dependents()); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments