|
157 | 157 | import static com.oracle.graal.python.compiler.OpCodes.UNPACK_SEQUENCE;
|
158 | 158 | import static com.oracle.graal.python.compiler.OpCodes.UNWRAP_EXC;
|
159 | 159 | import static com.oracle.graal.python.compiler.OpCodes.YIELD_VALUE;
|
| 160 | +import static com.oracle.graal.python.compiler.SSTUtils.checkCaller; |
| 161 | +import static com.oracle.graal.python.compiler.SSTUtils.checkCompare; |
| 162 | +import static com.oracle.graal.python.compiler.SSTUtils.checkForbiddenArgs; |
| 163 | +import static com.oracle.graal.python.compiler.SSTUtils.checkIndex; |
| 164 | +import static com.oracle.graal.python.compiler.SSTUtils.checkSubscripter; |
160 | 165 | import static com.oracle.graal.python.nodes.SpecialAttributeNames.J___TYPE_PARAMS__;
|
161 | 166 | import static com.oracle.graal.python.nodes.StringLiterals.T_EMPTY_STRING;
|
162 | 167 | import static com.oracle.graal.python.util.PythonUtils.arrayCopyOf;
|
163 | 168 | import static com.oracle.graal.python.util.PythonUtils.codePointsToTruffleString;
|
164 | 169 | import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached;
|
165 |
| -import static com.oracle.truffle.api.CompilerDirectives.shouldNotReachHere; |
166 | 170 |
|
167 | 171 | import java.util.ArrayList;
|
168 | 172 | import java.util.Collections;
|
|
172 | 176 | import java.util.List;
|
173 | 177 |
|
174 | 178 | import com.oracle.graal.python.PythonLanguage;
|
175 |
| -import com.oracle.graal.python.builtins.PythonBuiltinClassType; |
176 | 179 | import com.oracle.graal.python.builtins.objects.PNone;
|
177 | 180 | import com.oracle.graal.python.compiler.OpCodes.CollectionBits;
|
178 | 181 | import com.oracle.graal.python.compiler.OpCodes.MakeTypeParamKind;
|
|
197 | 200 | import com.oracle.graal.python.pegparser.sst.ExceptHandlerTy;
|
198 | 201 | import com.oracle.graal.python.pegparser.sst.ExprContextTy;
|
199 | 202 | import com.oracle.graal.python.pegparser.sst.ExprTy;
|
200 |
| -import com.oracle.graal.python.pegparser.sst.ExprTy.Constant; |
201 | 203 | import com.oracle.graal.python.pegparser.sst.ExprTy.Tuple;
|
202 | 204 | import com.oracle.graal.python.pegparser.sst.KeywordTy;
|
203 | 205 | import com.oracle.graal.python.pegparser.sst.MatchCaseTy;
|
@@ -492,16 +494,7 @@ private void exitScope() {
|
492 | 494 | }
|
493 | 495 |
|
494 | 496 | protected final void checkForbiddenName(String id, ExprContextTy context) {
|
495 |
| - if (context == ExprContextTy.Store) { |
496 |
| - if (id.equals("__debug__")) { |
497 |
| - throw parserCallbacks.onError(ErrorType.Syntax, unit.currentLocation, "cannot assign to __debug__"); |
498 |
| - } |
499 |
| - } |
500 |
| - if (context == ExprContextTy.Del) { |
501 |
| - if (id.equals("__debug__")) { |
502 |
| - throw parserCallbacks.onError(ErrorType.Syntax, unit.currentLocation, "cannot delete __debug__"); |
503 |
| - } |
504 |
| - } |
| 497 | + SSTUtils.checkForbiddenName(parserCallbacks, unit.currentLocation, id, context); |
505 | 498 | }
|
506 | 499 |
|
507 | 500 | private boolean containsAnnotations(StmtTy[] stmts) {
|
@@ -1286,7 +1279,7 @@ private static boolean hasOnlyPlainArgs(ExprTy[] args, KeywordTy[] keywords) {
|
1286 | 1279 | @Override
|
1287 | 1280 | public Void visit(ExprTy.Call node) {
|
1288 | 1281 | SourceRange savedLocation = setLocation(node);
|
1289 |
| - checkCaller(node.func); |
| 1282 | + checkCaller(parserCallbacks, node.func); |
1290 | 1283 | try {
|
1291 | 1284 | // n.b.: we do things completely different from python for calls
|
1292 | 1285 | OpCodes op = CALL_FUNCTION_VARARGS;
|
@@ -1386,7 +1379,7 @@ private void addCompareOp(CmpOpTy op) {
|
1386 | 1379 | @Override
|
1387 | 1380 | public Void visit(ExprTy.Compare node) {
|
1388 | 1381 | SourceRange savedLocation = setLocation(node);
|
1389 |
| - checkCompare(node); |
| 1382 | + checkCompare(parserCallbacks, node); |
1390 | 1383 | try {
|
1391 | 1384 | node.left.accept(this);
|
1392 | 1385 | if (node.comparators.length == 1) {
|
@@ -1568,7 +1561,7 @@ public Void visit(ExprTy.JoinedStr node) {
|
1568 | 1561 | public Void visit(ExprTy.Lambda node) {
|
1569 | 1562 | SourceRange savedLocation = setLocation(node);
|
1570 | 1563 | try {
|
1571 |
| - checkForbiddenArgs(node.args); |
| 1564 | + checkForbiddenArgs(parserCallbacks, unit.currentLocation, node.args); |
1572 | 1565 | int makeFunctionFlags = collectDefaults(node.args);
|
1573 | 1566 | enterScope("<lambda>", CompilationScope.Lambda, node, node.args, node.getSourceRange());
|
1574 | 1567 | /* Make None the first constant, so the lambda can't have a docstring. */
|
@@ -1862,8 +1855,8 @@ public Void visit(ExprTy.Starred node) {
|
1862 | 1855 | public Void visit(ExprTy.Subscript node) {
|
1863 | 1856 | SourceRange savedLocation = setLocation(node);
|
1864 | 1857 | if (node.context == ExprContextTy.Load) {
|
1865 |
| - checkSubscripter(node.value); |
1866 |
| - checkIndex(node.value, node.slice); |
| 1858 | + checkSubscripter(parserCallbacks, node.value); |
| 1859 | + checkIndex(parserCallbacks, node.value, node.slice); |
1867 | 1860 | }
|
1868 | 1861 | try {
|
1869 | 1862 | node.value.accept(this);
|
@@ -2660,7 +2653,7 @@ public Void visit(StmtTy.FunctionDef node) {
|
2660 | 2653 |
|
2661 | 2654 | private Void visitFunctionDef(StmtTy node, String name, ArgumentsTy args, StmtTy[] body, ExprTy[] decoratorList, ExprTy returns, TypeParamTy[] typeParams, boolean isAsync) {
|
2662 | 2655 | setLocation(node);
|
2663 |
| - checkForbiddenArgs(args); |
| 2656 | + checkForbiddenArgs(parserCallbacks, unit.currentLocation, args); |
2664 | 2657 |
|
2665 | 2658 | // visit decorators
|
2666 | 2659 | visitSequence(decoratorList);
|
@@ -2831,32 +2824,6 @@ private int collectDefaults(ArgumentsTy args) {
|
2831 | 2824 | return makeFunctionFlags;
|
2832 | 2825 | }
|
2833 | 2826 |
|
2834 |
| - private void checkForbiddenArgs(ArgumentsTy args) { |
2835 |
| - if (args != null) { |
2836 |
| - if (args.posOnlyArgs != null) { |
2837 |
| - for (ArgTy arg : args.posOnlyArgs) { |
2838 |
| - checkForbiddenName(arg.arg, ExprContextTy.Store); |
2839 |
| - } |
2840 |
| - } |
2841 |
| - if (args.args != null) { |
2842 |
| - for (ArgTy arg : args.args) { |
2843 |
| - checkForbiddenName(arg.arg, ExprContextTy.Store); |
2844 |
| - } |
2845 |
| - } |
2846 |
| - if (args.kwOnlyArgs != null) { |
2847 |
| - for (ArgTy arg : args.kwOnlyArgs) { |
2848 |
| - checkForbiddenName(arg.arg, ExprContextTy.Store); |
2849 |
| - } |
2850 |
| - } |
2851 |
| - if (args.varArg != null) { |
2852 |
| - checkForbiddenName(args.varArg.arg, ExprContextTy.Store); |
2853 |
| - } |
2854 |
| - if (args.kwArg != null) { |
2855 |
| - checkForbiddenName(args.kwArg.arg, ExprContextTy.Store); |
2856 |
| - } |
2857 |
| - } |
2858 |
| - } |
2859 |
| - |
2860 | 2827 | @Override
|
2861 | 2828 | public Void visit(StmtTy.Global node) {
|
2862 | 2829 | setLocation(node);
|
@@ -2885,7 +2852,7 @@ private void jumpIf(ExprTy test, Block next, boolean jumpIfTrue) {
|
2885 | 2852 | // TODO Optimize for various test types, such as short-circuit operators
|
2886 | 2853 | // See compiler_jump_if in CPython
|
2887 | 2854 | if (test instanceof ExprTy.Compare) {
|
2888 |
| - checkCompare((ExprTy.Compare) test); |
| 2855 | + checkCompare(parserCallbacks, (ExprTy.Compare) test); |
2889 | 2856 | }
|
2890 | 2857 | test.accept(this);
|
2891 | 2858 | if (jumpIfTrue) {
|
@@ -4197,133 +4164,6 @@ private void warn(SSTNode node, String message, Object... arguments) {
|
4197 | 4164 | parserCallbacks.onWarning(WarningType.Syntax, node.getSourceRange(), message, arguments);
|
4198 | 4165 | }
|
4199 | 4166 |
|
4200 |
| - private void checkCompare(ExprTy.Compare node) { |
4201 |
| - ExprTy leftExpr = node.left; |
4202 |
| - boolean left = checkIsArg(leftExpr); |
4203 |
| - int n = node.ops == null ? 0 : node.ops.length; |
4204 |
| - for (int i = 0; i < n; ++i) { |
4205 |
| - CmpOpTy op = node.ops[i]; |
4206 |
| - ExprTy rightExpr = node.comparators[i]; |
4207 |
| - boolean right = checkIsArg(rightExpr); |
4208 |
| - if (op == CmpOpTy.Is || op == CmpOpTy.IsNot) { |
4209 |
| - if (!right || !left) { |
4210 |
| - ExprTy literal = !left ? leftExpr : rightExpr; |
4211 |
| - warn(node, op == CmpOpTy.Is ? "\"is\" with '%s' literal. Did you mean \"==\"?" : "\"is not\" with '%s' literal. Did you mean \"!=\"?", inferType(literal).getName()); |
4212 |
| - } |
4213 |
| - } |
4214 |
| - left = right; |
4215 |
| - leftExpr = rightExpr; |
4216 |
| - } |
4217 |
| - } |
4218 |
| - |
4219 |
| - private static boolean checkIsArg(ExprTy e) { |
4220 |
| - if (e instanceof ExprTy.Constant) { |
4221 |
| - ConstantValue.Kind kind = ((Constant) e).value.kind; |
4222 |
| - return kind == Kind.NONE || kind == Kind.BOOLEAN || kind == Kind.ELLIPSIS; |
4223 |
| - } |
4224 |
| - return true; |
4225 |
| - } |
4226 |
| - |
4227 |
| - private static PythonBuiltinClassType inferType(ExprTy e) { |
4228 |
| - if (e instanceof ExprTy.Tuple) { |
4229 |
| - return PythonBuiltinClassType.PTuple; |
4230 |
| - } |
4231 |
| - if (e instanceof ExprTy.List || e instanceof ExprTy.ListComp) { |
4232 |
| - return PythonBuiltinClassType.PList; |
4233 |
| - } |
4234 |
| - if (e instanceof ExprTy.Dict || e instanceof ExprTy.DictComp) { |
4235 |
| - return PythonBuiltinClassType.PDict; |
4236 |
| - } |
4237 |
| - if (e instanceof ExprTy.Set || e instanceof ExprTy.SetComp) { |
4238 |
| - return PythonBuiltinClassType.PSet; |
4239 |
| - } |
4240 |
| - if (e instanceof ExprTy.GeneratorExp) { |
4241 |
| - return PythonBuiltinClassType.PGenerator; |
4242 |
| - } |
4243 |
| - if (e instanceof ExprTy.Lambda) { |
4244 |
| - return PythonBuiltinClassType.PFunction; |
4245 |
| - } |
4246 |
| - if (e instanceof ExprTy.JoinedStr || e instanceof ExprTy.FormattedValue) { |
4247 |
| - return PythonBuiltinClassType.PString; |
4248 |
| - } |
4249 |
| - if (e instanceof ExprTy.Constant) { |
4250 |
| - switch (((ExprTy.Constant) e).value.kind) { |
4251 |
| - case NONE: |
4252 |
| - return PythonBuiltinClassType.PNone; |
4253 |
| - case ELLIPSIS: |
4254 |
| - return PythonBuiltinClassType.PEllipsis; |
4255 |
| - case BOOLEAN: |
4256 |
| - return PythonBuiltinClassType.Boolean; |
4257 |
| - case DOUBLE: |
4258 |
| - return PythonBuiltinClassType.PFloat; |
4259 |
| - case COMPLEX: |
4260 |
| - return PythonBuiltinClassType.PComplex; |
4261 |
| - case LONG: |
4262 |
| - case BIGINTEGER: |
4263 |
| - return PythonBuiltinClassType.PInt; |
4264 |
| - case CODEPOINTS: |
4265 |
| - return PythonBuiltinClassType.PString; |
4266 |
| - case BYTES: |
4267 |
| - return PythonBuiltinClassType.PBytes; |
4268 |
| - case TUPLE: |
4269 |
| - return PythonBuiltinClassType.PTuple; |
4270 |
| - case FROZENSET: |
4271 |
| - return PythonBuiltinClassType.PFrozenSet; |
4272 |
| - default: |
4273 |
| - throw shouldNotReachHere("Invalid ConstantValue kind: " + ((ExprTy.Constant) e).value.kind); |
4274 |
| - } |
4275 |
| - } |
4276 |
| - return null; |
4277 |
| - } |
4278 |
| - |
4279 |
| - private void checkCaller(ExprTy e) { |
4280 |
| - if (e instanceof ExprTy.Constant || e instanceof ExprTy.Tuple || e instanceof ExprTy.List || e instanceof ExprTy.ListComp || e instanceof ExprTy.Dict || e instanceof ExprTy.DictComp || |
4281 |
| - e instanceof ExprTy.Set || e instanceof ExprTy.SetComp || e instanceof ExprTy.GeneratorExp || e instanceof ExprTy.JoinedStr || e instanceof ExprTy.FormattedValue) { |
4282 |
| - warn(e, "'%s' object is not callable; perhaps you missed a comma?", inferType(e).getName()); |
4283 |
| - } |
4284 |
| - } |
4285 |
| - |
4286 |
| - private void checkSubscripter(ExprTy e) { |
4287 |
| - if (e instanceof ExprTy.Constant) { |
4288 |
| - switch (((ExprTy.Constant) e).value.kind) { |
4289 |
| - case NONE: |
4290 |
| - case ELLIPSIS: |
4291 |
| - case BOOLEAN: |
4292 |
| - case LONG: |
4293 |
| - case BIGINTEGER: |
4294 |
| - case DOUBLE: |
4295 |
| - case COMPLEX: |
4296 |
| - case FROZENSET: |
4297 |
| - break; |
4298 |
| - default: |
4299 |
| - return; |
4300 |
| - } |
4301 |
| - } else if (!(e instanceof ExprTy.Set || e instanceof ExprTy.SetComp || e instanceof ExprTy.GeneratorExp || e instanceof ExprTy.Lambda)) { |
4302 |
| - return; |
4303 |
| - } |
4304 |
| - warn(e, "'%s' object is not subscriptable; perhaps you missed a comma?", inferType(e).getName()); |
4305 |
| - } |
4306 |
| - |
4307 |
| - private void checkIndex(ExprTy e, ExprTy s) { |
4308 |
| - PythonBuiltinClassType indexType = inferType(s); |
4309 |
| - if (indexType == null || indexType == PythonBuiltinClassType.Boolean || indexType == PythonBuiltinClassType.PInt || indexType == PythonBuiltinClassType.PSlice) { |
4310 |
| - return; |
4311 |
| - } |
4312 |
| - if (e instanceof ExprTy.Constant) { |
4313 |
| - switch (((ExprTy.Constant) e).value.kind) { |
4314 |
| - case CODEPOINTS: |
4315 |
| - case BYTES: |
4316 |
| - case TUPLE: |
4317 |
| - break; |
4318 |
| - default: |
4319 |
| - return; |
4320 |
| - } |
4321 |
| - } else if (!(e instanceof ExprTy.Tuple || e instanceof ExprTy.List || e instanceof ExprTy.ListComp || e instanceof ExprTy.JoinedStr || e instanceof ExprTy.FormattedValue)) { |
4322 |
| - return; |
4323 |
| - } |
4324 |
| - warn(e, "%s indices must be integers or slices, not %s; perhaps you missed a comma?", inferType(e).getName(), indexType.getName()); |
4325 |
| - } |
4326 |
| - |
4327 | 4167 | public static Parser createParser(String src, ParserCallbacks errorCb, InputType inputType, boolean interactiveTerminal, boolean allowIncompleteInput) {
|
4328 | 4168 | EnumSet<AbstractParser.Flags> flags = EnumSet.noneOf(AbstractParser.Flags.class);
|
4329 | 4169 | if (interactiveTerminal) {
|
|
0 commit comments