Skip to content

Commit ad1e6b8

Browse files
committed
refactored Arity state storage class, track whether the function takes kwArgs or not
- in addition track if the function takes a fixed number of args
1 parent 2644922 commit ad1e6b8

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1347,7 +1347,7 @@ synchronized public Object doIt(PFunction func) {
13471347
String[] parameterIds = new String[arity.getParameterIds().length + 1];
13481348
parameterIds[0] = "self";
13491349
System.arraycopy(arity.getParameterIds(), 0, parameterIds, 1, parameterIds.length - 1);
1350-
Arity arityWithSelf = new Arity(name, arity.getMinNumOfArgs() + 1, arity.getMaxNumOfArgs() + 1, arity.takesKeywordArg(), arity.takesVarArgs(), parameterIds,
1350+
Arity arityWithSelf = new Arity(name, arity.getMinNumOfArgs() + 1, arity.getMaxNumOfArgs() + 1, arity.takesKeywordArgs(), arity.takesVarArgs(), parameterIds,
13511351
arity.getKeywordNames());
13521352
FunctionRootNode functionRootNode = (FunctionRootNode) func.getFunctionRootNode();
13531353
if (!functionRootNode.isRewritten()) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/Arity.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class Arity {
3636
private final int minNumOfArgs;
3737
private final int maxNumOfArgs;
3838

39-
private final boolean takesKeywordArg;
39+
private final boolean takesKeywordArgs;
4040
private final boolean takesVarArgs;
4141

4242
@CompilationFinal(dimensions = 1) private final String[] parameterIds;
@@ -46,42 +46,50 @@ public Arity(String functionName, int minNumOfArgs, int maxNumOfArgs, List<Strin
4646
this.functionName = functionName;
4747
this.minNumOfArgs = minNumOfArgs;
4848
this.maxNumOfArgs = maxNumOfArgs;
49-
this.takesKeywordArg = true;
49+
this.takesKeywordArgs = true;
5050
this.takesVarArgs = false;
5151
this.parameterIds = parameterIds.toArray(new String[0]);
5252
this.keywordNames = keywordNames.toArray(new String[0]);
5353
}
5454

55-
public Arity(String functionName, int minNumOfArgs, int maxNumOfArgs, boolean takesKeywordArg, boolean takesVarArgs, List<String> parameterIds, List<String> keywordNames) {
55+
public Arity(String functionName, int minNumOfArgs, int maxNumOfArgs, boolean takesKeywordArgs, boolean takesVarArgs, List<String> parameterIds, List<String> keywordNames) {
5656
this.functionName = functionName;
5757
this.minNumOfArgs = minNumOfArgs;
5858
this.maxNumOfArgs = maxNumOfArgs;
59-
this.takesKeywordArg = takesKeywordArg;
59+
this.takesKeywordArgs = takesKeywordArgs;
6060
this.takesVarArgs = takesVarArgs;
6161
this.parameterIds = parameterIds.toArray(new String[0]);
6262
this.keywordNames = keywordNames.toArray(new String[0]);
6363
}
6464

65-
public Arity(String functionName, int minNumOfArgs, int maxNumOfArgs, boolean takesKeywordArg, boolean takesVarArgs, String[] parameterIds, String[] keywordNames) {
65+
public Arity(String functionName, int minNumOfArgs, int maxNumOfArgs, boolean takesKeywordArgs, boolean takesVarArgs, String[] parameterIds, String[] keywordNames) {
6666
this.functionName = functionName;
6767
this.minNumOfArgs = minNumOfArgs;
6868
this.maxNumOfArgs = maxNumOfArgs;
69-
this.takesKeywordArg = takesKeywordArg;
69+
this.takesKeywordArgs = takesKeywordArgs;
7070
this.takesVarArgs = takesVarArgs;
7171
this.parameterIds = parameterIds;
7272
this.keywordNames = keywordNames;
7373
}
7474

75+
public static Arity createOneArgument(String functionName) {
76+
return new Arity(functionName, 1, 1, new ArrayList<String>(), new ArrayList<String>());
77+
}
78+
7579
public boolean takesVarArgs() {
7680
return takesVarArgs;
7781
}
7882

79-
public boolean takesKeywordArg() {
80-
return takesKeywordArg;
83+
public boolean takesKeywordArgs() {
84+
return takesKeywordArgs;
8185
}
8286

83-
public static Arity createOneArgument(String functionName) {
84-
return new Arity(functionName, 1, 1, new ArrayList<String>(), new ArrayList<String>());
87+
public boolean takesFixedNumberOfArguments() {
88+
if (takesVarArgs || takesKeywordArgs) {
89+
return false;
90+
} else {
91+
return minNumOfArgs == maxNumOfArgs;
92+
}
8593
}
8694

8795
public int getMinNumOfArgs() {
@@ -104,7 +112,7 @@ public String[] getParameterIds() {
104112
return parameterIds;
105113
}
106114

107-
public int parametersSize() {
115+
public int getNumberOfParameters() {
108116
return parameterIds.length;
109117
}
110118

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/PythonTreeTranslator.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,7 +1631,11 @@ public Object visitLambdef_nocond(Python3Parser.Lambdef_nocondContext ctx) {
16311631
}
16321632

16331633
private static Arity createArity(String functionName, PNode argBlock) {
1634-
boolean takesFixedNumOfArgs = true;
1634+
boolean takesKeywordArgs = false;
1635+
int maxNumOfArgs = 0;
1636+
int minNumOfArgs = 0;
1637+
List<String> parameterIds = new ArrayList<>();
1638+
List<String> keywordNames = new ArrayList<>();
16351639

16361640
PNode[] statements;
16371641
if (argBlock instanceof BlockNode) {
@@ -1641,24 +1645,23 @@ private static Arity createArity(String functionName, PNode argBlock) {
16411645
} else {
16421646
statements = new PNode[]{argBlock};
16431647
}
1644-
int maxNumOfArgs = 0;
1645-
int minNumOfArgs = 0;
1646-
List<String> parameterIds = new ArrayList<>();
1647-
List<String> keywordNames = new ArrayList<>();
1648+
16481649
for (PNode writeLocal : statements) {
16491650
WriteIdentifierNode writeNode = (WriteIdentifierNode) writeLocal;
16501651
PNode rhs = writeNode.getRhs();
16511652
if (rhs instanceof ReadVarArgsNode) {
16521653
maxNumOfArgs = -1;
16531654
} else if (rhs instanceof ReadVarKeywordsNode) {
16541655
maxNumOfArgs = -1;
1656+
takesKeywordArgs = true;
16551657
} else if (rhs instanceof ReadKeywordNode) {
16561658
if (((ReadKeywordNode) rhs).canBePositional()) {
16571659
// this default can be passed positionally
16581660
maxNumOfArgs++;
1661+
} else {
1662+
takesKeywordArgs = true;
16591663
}
16601664
keywordNames.add((String) writeNode.getIdentifier());
1661-
takesFixedNumOfArgs = false;
16621665
} else if (rhs instanceof ReadIndexedArgumentNode) {
16631666
minNumOfArgs++;
16641667
maxNumOfArgs++;
@@ -1668,11 +1671,9 @@ private static Arity createArity(String functionName, PNode argBlock) {
16681671
}
16691672
}
16701673

1671-
takesFixedNumOfArgs = takesFixedNumOfArgs && maxNumOfArgs == minNumOfArgs;
16721674
boolean takesVarArgs = maxNumOfArgs == -1;
1673-
boolean takesKeywordArg = true;
16741675

1675-
return new Arity(functionName, minNumOfArgs, maxNumOfArgs, takesKeywordArg, takesVarArgs, parameterIds, keywordNames);
1676+
return new Arity(functionName, minNumOfArgs, maxNumOfArgs, takesKeywordArgs, takesVarArgs, parameterIds, keywordNames);
16761677
}
16771678

16781679
@Override

0 commit comments

Comments
 (0)