|
| 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 | + */ |
1 | 20 | package org.sonar.python.semantic.v2;
|
2 | 21 |
|
| 22 | +import java.util.ArrayDeque; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Collection; |
| 25 | +import java.util.Deque; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Optional; |
| 29 | +import org.sonar.plugins.python.api.tree.AssignmentStatement; |
3 | 30 | import org.sonar.plugins.python.api.tree.BaseTreeVisitor;
|
| 31 | +import org.sonar.plugins.python.api.tree.CallExpression; |
| 32 | +import org.sonar.plugins.python.api.tree.ClassDef; |
| 33 | +import org.sonar.plugins.python.api.tree.Expression; |
| 34 | +import org.sonar.plugins.python.api.tree.ExpressionList; |
| 35 | +import org.sonar.plugins.python.api.tree.FileInput; |
| 36 | +import org.sonar.plugins.python.api.tree.FunctionDef; |
| 37 | +import org.sonar.plugins.python.api.tree.ImportName; |
| 38 | +import org.sonar.plugins.python.api.tree.ListLiteral; |
| 39 | +import org.sonar.plugins.python.api.tree.Name; |
| 40 | +import org.sonar.plugins.python.api.tree.NumericLiteral; |
| 41 | +import org.sonar.plugins.python.api.tree.QualifiedExpression; |
| 42 | +import org.sonar.plugins.python.api.tree.StringLiteral; |
| 43 | +import org.sonar.plugins.python.api.types.InferredType; |
| 44 | +import org.sonar.python.tree.ListLiteralImpl; |
| 45 | +import org.sonar.python.tree.NameImpl; |
| 46 | +import org.sonar.python.tree.NumericLiteralImpl; |
| 47 | +import org.sonar.python.tree.StringLiteralImpl; |
| 48 | +import org.sonar.python.types.RuntimeType; |
| 49 | +import org.sonar.python.types.v2.ClassType; |
| 50 | +import org.sonar.python.types.v2.FunctionType; |
| 51 | +import org.sonar.python.types.v2.Member; |
| 52 | +import org.sonar.python.types.v2.ModuleType; |
| 53 | +import org.sonar.python.types.v2.ObjectType; |
| 54 | +import org.sonar.python.types.v2.ObjectTypeBuilder; |
| 55 | +import org.sonar.python.types.v2.PythonType; |
4 | 56 |
|
5 | 57 | public class TypeInferenceV2 extends BaseTreeVisitor {
|
6 | 58 |
|
| 59 | + private final ProjectLevelTypeTable projectLevelTypeTable; |
| 60 | + |
| 61 | + private final Deque<PythonType> typeStack = new ArrayDeque<>(); |
| 62 | + |
| 63 | + public TypeInferenceV2(ProjectLevelTypeTable projectLevelTypeTable) { |
| 64 | + this.projectLevelTypeTable = projectLevelTypeTable; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void visitFileInput(FileInput fileInput) { |
| 69 | + var type = new ModuleType("somehow get its name", new HashMap<>()); |
| 70 | + inTypeScope(type, () -> super.visitFileInput(fileInput)); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void visitStringLiteral(StringLiteral stringLiteral) { |
| 75 | + ModuleType builtins = this.projectLevelTypeTable.getModule("builtins"); |
| 76 | + // TODO: multiple object types to represent str instance? |
| 77 | + ((StringLiteralImpl) stringLiteral).typeV2(new ObjectType(builtins.resolveMember("str"), List.of(), List.of())); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void visitNumericLiteral(NumericLiteral numericLiteral) { |
| 82 | + ModuleType builtins = this.projectLevelTypeTable.getModule("builtins"); |
| 83 | + InferredType type = numericLiteral.type(); |
| 84 | + String memberName = ((RuntimeType) type).getTypeClass().fullyQualifiedName(); |
| 85 | + if (memberName != null) { |
| 86 | + ((NumericLiteralImpl) numericLiteral).typeV2(new ObjectType(builtins.resolveMember(memberName), List.of(), List.of())); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void visitListLiteral(ListLiteral listLiteral) { |
| 92 | + ModuleType builtins = this.projectLevelTypeTable.getModule("builtins"); |
| 93 | + scan(listLiteral.elements()); |
| 94 | + List<PythonType> pythonTypes = listLiteral.elements().expressions().stream().map(Expression::typeV2).distinct().toList(); |
| 95 | + // TODO: cleanly reduce attributes |
| 96 | + ((ListLiteralImpl) listLiteral).typeV2(new ObjectType(builtins.resolveMember("list"), pythonTypes, List.of())); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void visitClassDef(ClassDef classDef) { |
| 101 | + scan(classDef.args()); |
| 102 | + Name name = classDef.name(); |
| 103 | + ClassType type = new ClassType(name.name()); |
| 104 | + ((NameImpl) name).typeV2(type); |
| 105 | + |
| 106 | + inTypeScope(type, () -> scan(classDef.body())); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void visitFunctionDef(FunctionDef functionDef) { |
| 111 | + scan(functionDef.decorators()); |
| 112 | + FunctionType functionType = new FunctionType(functionDef.name().name(), new ArrayList<>(), new ArrayList<>(), PythonType.UNKNOWN); |
| 113 | + if (currentType() instanceof ClassType classType) { |
| 114 | + if (functionDef.name().symbolV2().hasSingleBindingUsage()) { |
| 115 | + classType.members().add(new Member(functionType.name(), functionType)); |
| 116 | + } else { |
| 117 | + // TODO: properly infer type in case of multiple assignments |
| 118 | + classType.members().add(new Member(functionType.name(), PythonType.UNKNOWN)); |
| 119 | + } |
| 120 | + } |
| 121 | + ((NameImpl) functionDef.name()).typeV2(functionType); |
| 122 | + inTypeScope(functionType, () -> { |
| 123 | + // TODO: check scope accuracy |
| 124 | + scan(functionDef.typeParams()); |
| 125 | + scan(functionDef.parameters()); |
| 126 | + scan(functionDef.returnTypeAnnotation()); |
| 127 | + scan(functionDef.body()); |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void visitImportName(ImportName importName) { |
| 133 | + //createImportedNames(importName.modules(), null, Collections.emptyList()); |
| 134 | + super.visitImportName(importName); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public void visitAssignmentStatement(AssignmentStatement assignmentStatement) { |
| 139 | + scan(assignmentStatement.assignedValue()); |
| 140 | + Optional.of(assignmentStatement) |
| 141 | + .map(AssignmentStatement::lhsExpressions) |
| 142 | + .filter(lhs -> lhs.size() == 1) |
| 143 | + .map(lhs -> lhs.get(0)) |
| 144 | + .map(ExpressionList::expressions) |
| 145 | + .filter(lhs -> lhs.size() == 1) |
| 146 | + .map(lhs -> lhs.get(0)) |
| 147 | + .filter(NameImpl.class::isInstance) |
| 148 | + .map(NameImpl.class::cast) |
| 149 | + .ifPresent(lhsName -> { |
| 150 | + var assignedValueType = assignmentStatement.assignedValue().typeV2(); |
| 151 | + lhsName.typeV2(assignedValueType); |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public void visitQualifiedExpression(QualifiedExpression qualifiedExpression) { |
| 157 | + scan(qualifiedExpression.qualifier()); |
| 158 | + if (qualifiedExpression.name() instanceof NameImpl name) { |
| 159 | + var nameType = qualifiedExpression.qualifier().typeV2().resolveMember(qualifiedExpression.name().name()); |
| 160 | + name.typeV2(nameType); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public void visitName(Name name) { |
| 166 | + var type = Optional.of(name) |
| 167 | + .map(Name::symbolV2) |
| 168 | + .map(SymbolV2::usages) |
| 169 | + .stream() |
| 170 | + .flatMap(Collection::stream) |
| 171 | + .filter(UsageV2::isBindingUsage) |
| 172 | + .map(UsageV2::tree) |
| 173 | + .filter(Expression.class::isInstance) |
| 174 | + .map(Expression.class::cast) |
| 175 | + .map(Expression::typeV2) |
| 176 | + .toList(); |
| 177 | + |
| 178 | + if (type.size() == 1 && name instanceof NameImpl nameImpl) { |
| 179 | + nameImpl.typeV2(type.get(0)); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private PythonType currentType() { |
| 184 | + return typeStack.peek(); |
| 185 | + } |
| 186 | + |
| 187 | + private void inTypeScope(PythonType pythonType, Runnable runnable) { |
| 188 | + this.typeStack.push(pythonType); |
| 189 | + runnable.run(); |
| 190 | + this.typeStack.poll(); |
| 191 | + } |
7 | 192 |
|
8 | 193 | }
|
0 commit comments