|
| 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; |
| 21 | + |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Objects; |
| 25 | +import java.util.Optional; |
| 26 | +import javax.annotation.Nullable; |
| 27 | +import org.sonar.plugins.python.api.tree.AnyParameter; |
| 28 | +import org.sonar.plugins.python.api.tree.FunctionDef; |
| 29 | +import org.sonar.plugins.python.api.tree.Name; |
| 30 | +import org.sonar.plugins.python.api.tree.ParameterList; |
| 31 | +import org.sonar.plugins.python.api.tree.Token; |
| 32 | +import org.sonar.plugins.python.api.tree.Tree; |
| 33 | +import org.sonar.python.tree.TreeUtils; |
| 34 | +import org.sonar.python.types.v2.FunctionType; |
| 35 | +import org.sonar.python.types.v2.ParameterV2; |
| 36 | +import org.sonar.python.types.v2.PythonType; |
| 37 | + |
| 38 | +import static org.sonar.python.tree.TreeUtils.locationInFile; |
| 39 | + |
| 40 | +public class FunctionTypeBuilder { |
| 41 | + |
| 42 | + private boolean hasVariadicParameter; |
| 43 | + private String name; |
| 44 | + private List<PythonType> attributes; |
| 45 | + private List<ParameterV2> parameters; |
| 46 | + private boolean isAsynchronous; |
| 47 | + private boolean hasDecorators; |
| 48 | + private boolean isInstanceMethod; |
| 49 | + private PythonType owner; |
| 50 | + private PythonType returnType = PythonType.UNKNOWN; |
| 51 | + |
| 52 | + private static final String CLASS_METHOD_DECORATOR = "classmethod"; |
| 53 | + private static final String STATIC_METHOD_DECORATOR = "staticmethod"; |
| 54 | + |
| 55 | + public FunctionTypeBuilder fromFunctionDef(FunctionDef functionDef) { |
| 56 | + this.name = functionDef.name().name(); |
| 57 | + this.attributes = new ArrayList<>(); |
| 58 | + this.parameters = new ArrayList<>(); |
| 59 | + isAsynchronous = functionDef.asyncKeyword() != null; |
| 60 | + hasDecorators = !functionDef.decorators().isEmpty(); |
| 61 | + isInstanceMethod = isInstanceMethod(functionDef); |
| 62 | + ParameterList parameterList = functionDef.parameters(); |
| 63 | + if (parameterList != null) { |
| 64 | + createParameterNames(parameterList.all(), null); |
| 65 | + } |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | + public FunctionType build() { |
| 70 | + return new FunctionType(name, attributes, parameters, returnType, isAsynchronous, hasDecorators, isInstanceMethod, hasVariadicParameter, owner); |
| 71 | + } |
| 72 | + |
| 73 | + private static boolean isInstanceMethod(FunctionDef functionDef) { |
| 74 | + return !"__new__".equals(functionDef.name().name()) && functionDef.isMethodDefinition() && functionDef.decorators().stream() |
| 75 | + .map(decorator -> TreeUtils.decoratorNameFromExpression(decorator.expression())) |
| 76 | + .filter(Objects::nonNull) |
| 77 | + .noneMatch(decorator -> decorator.equals(STATIC_METHOD_DECORATOR) || decorator.equals(CLASS_METHOD_DECORATOR)); |
| 78 | + } |
| 79 | + |
| 80 | + public void setOwner(PythonType owner) { |
| 81 | + this.owner = owner; |
| 82 | + } |
| 83 | + |
| 84 | + private void createParameterNames(List<AnyParameter> parameterTrees, @Nullable String fileId) { |
| 85 | + ParameterState parameterState = new ParameterState(); |
| 86 | + parameterState.positionalOnly = parameterTrees.stream().anyMatch(param -> Optional.of(param) |
| 87 | + .filter(p -> p.is(Tree.Kind.PARAMETER)) |
| 88 | + .map(p -> ((org.sonar.plugins.python.api.tree.Parameter) p).starToken()) |
| 89 | + .map(Token::value) |
| 90 | + .filter("/"::equals) |
| 91 | + .isPresent() |
| 92 | + ); |
| 93 | + for (AnyParameter anyParameter : parameterTrees) { |
| 94 | + if (anyParameter.is(Tree.Kind.PARAMETER)) { |
| 95 | + addParameter((org.sonar.plugins.python.api.tree.Parameter) anyParameter, fileId, parameterState); |
| 96 | + } else { |
| 97 | + parameters.add(new ParameterV2(null, PythonType.UNKNOWN, false, |
| 98 | + parameterState.keywordOnly, parameterState.positionalOnly, false, false, locationInFile(anyParameter, fileId))); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private void addParameter(org.sonar.plugins.python.api.tree.Parameter parameter, @Nullable String fileId, ParameterState parameterState) { |
| 104 | + Name parameterName = parameter.name(); |
| 105 | + Token starToken = parameter.starToken(); |
| 106 | + if (parameterName != null) { |
| 107 | + ParameterType parameterType = getParameterType(parameter); |
| 108 | + this.parameters.add(new ParameterV2(parameterName.name(), parameterType.pythonType(), parameter.defaultValue() != null, |
| 109 | + parameterState.keywordOnly, parameterState.positionalOnly, parameterType.isKeywordVariadic(), parameterType.isPositionalVariadic(), locationInFile(parameter, fileId))); |
| 110 | + if (starToken != null) { |
| 111 | + hasVariadicParameter = true; |
| 112 | + parameterState.keywordOnly = true; |
| 113 | + parameterState.positionalOnly = false; |
| 114 | + } |
| 115 | + } else if (starToken != null) { |
| 116 | + if ("*".equals(starToken.value())) { |
| 117 | + parameterState.keywordOnly = true; |
| 118 | + parameterState.positionalOnly = false; |
| 119 | + } |
| 120 | + if ("/".equals(starToken.value())) { |
| 121 | + parameterState.positionalOnly = false; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private ParameterType getParameterType(org.sonar.plugins.python.api.tree.Parameter parameter) { |
| 127 | + boolean isPositionalVariadic = false; |
| 128 | + boolean isKeywordVariadic = false; |
| 129 | + Token starToken = parameter.starToken(); |
| 130 | + if (starToken != null) { |
| 131 | + // https://docs.python.org/3/reference/compound_stmts.html#function-definitions |
| 132 | + hasVariadicParameter = true; |
| 133 | + if ("*".equals(starToken.value())) { |
| 134 | + // if the form “*identifier” is present, it is initialized to a tuple receiving any excess positional parameters |
| 135 | + isPositionalVariadic = true; |
| 136 | + // Should set PythonType to TUPLE |
| 137 | + } |
| 138 | + if ("**".equals(starToken.value())) { |
| 139 | + // If the form “**identifier” is present, it is initialized to a new ordered mapping receiving any excess keyword arguments |
| 140 | + isKeywordVariadic = true; |
| 141 | + // Should set PythonType to DICT |
| 142 | + } |
| 143 | + } |
| 144 | + // TODO: SONARPY-1773 handle parameter declared types |
| 145 | + return new ParameterType(PythonType.UNKNOWN, isKeywordVariadic, isPositionalVariadic); |
| 146 | + } |
| 147 | + |
| 148 | + public static class ParameterState { |
| 149 | + boolean keywordOnly = false; |
| 150 | + boolean positionalOnly = false; |
| 151 | + } |
| 152 | + |
| 153 | + record ParameterType(PythonType pythonType, boolean isKeywordVariadic, boolean isPositionalVariadic) { } |
| 154 | +} |
0 commit comments