blob: 205c711ac31c0f15b3a5ee57ea87253716391f0c [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
Benjamin Peterson832bfe22011-08-09 16:15:04 -05003 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#include "node.h"
9#include "ast.h"
10#include "token.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011
12#include <assert.h>
13
Benjamin Peterson832bfe22011-08-09 16:15:04 -050014static int validate_stmts(asdl_seq *);
15static int validate_exprs(asdl_seq *, expr_context_ty, int);
16static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
17static int validate_stmt(stmt_ty);
18static int validate_expr(expr_ty, expr_context_ty);
19
20static int
21validate_comprehension(asdl_seq *gens)
22{
23 int i;
24 if (!asdl_seq_LEN(gens)) {
25 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
26 return 0;
27 }
28 for (i = 0; i < asdl_seq_LEN(gens); i++) {
29 comprehension_ty comp = asdl_seq_GET(gens, i);
30 if (!validate_expr(comp->target, Store) ||
31 !validate_expr(comp->iter, Load) ||
32 !validate_exprs(comp->ifs, Load, 0))
33 return 0;
34 }
35 return 1;
36}
37
38static int
39validate_slice(slice_ty slice)
40{
41 switch (slice->kind) {
42 case Slice_kind:
43 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
44 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
45 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
46 case ExtSlice_kind: {
47 int i;
48 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
49 return 0;
50 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
51 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
52 return 0;
53 return 1;
54 }
55 case Index_kind:
56 return validate_expr(slice->v.Index.value, Load);
57 default:
58 PyErr_SetString(PyExc_SystemError, "unknown slice node");
59 return 0;
60 }
61}
62
63static int
64validate_keywords(asdl_seq *keywords)
65{
66 int i;
67 for (i = 0; i < asdl_seq_LEN(keywords); i++)
68 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
69 return 0;
70 return 1;
71}
72
73static int
74validate_args(asdl_seq *args)
75{
76 int i;
77 for (i = 0; i < asdl_seq_LEN(args); i++) {
78 arg_ty arg = asdl_seq_GET(args, i);
79 if (arg->annotation && !validate_expr(arg->annotation, Load))
80 return 0;
81 }
82 return 1;
83}
84
85static const char *
86expr_context_name(expr_context_ty ctx)
87{
88 switch (ctx) {
89 case Load:
90 return "Load";
91 case Store:
92 return "Store";
93 case Del:
94 return "Del";
95 case AugLoad:
96 return "AugLoad";
97 case AugStore:
98 return "AugStore";
99 case Param:
100 return "Param";
101 default:
102 assert(0);
103 return "(unknown)";
104 }
105}
106
107static int
108validate_arguments(arguments_ty args)
109{
110 if (!validate_args(args->args))
111 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700112 if (args->vararg && args->vararg->annotation
113 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500114 return 0;
115 }
116 if (!validate_args(args->kwonlyargs))
117 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100118 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700119 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500120 return 0;
121 }
122 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
123 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
124 return 0;
125 }
126 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
127 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
128 "kw_defaults on arguments");
129 return 0;
130 }
131 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
132}
133
134static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100135validate_constant(PyObject *value)
136{
137 if (value == Py_None || value == Py_Ellipsis)
138 return 1;
139
140 if (PyLong_CheckExact(value)
141 || PyFloat_CheckExact(value)
142 || PyComplex_CheckExact(value)
143 || PyBool_Check(value)
144 || PyUnicode_CheckExact(value)
145 || PyBytes_CheckExact(value))
146 return 1;
147
148 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
149 PyObject *it;
150
151 it = PyObject_GetIter(value);
152 if (it == NULL)
153 return 0;
154
155 while (1) {
156 PyObject *item = PyIter_Next(it);
157 if (item == NULL) {
158 if (PyErr_Occurred()) {
159 Py_DECREF(it);
160 return 0;
161 }
162 break;
163 }
164
165 if (!validate_constant(item)) {
166 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100167 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100168 return 0;
169 }
Victor Stinner726f6902016-01-27 00:11:47 +0100170 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100171 }
172
173 Py_DECREF(it);
174 return 1;
175 }
176
177 return 0;
178}
179
180static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500181validate_expr(expr_ty exp, expr_context_ty ctx)
182{
183 int check_ctx = 1;
184 expr_context_ty actual_ctx;
185
186 /* First check expression context. */
187 switch (exp->kind) {
188 case Attribute_kind:
189 actual_ctx = exp->v.Attribute.ctx;
190 break;
191 case Subscript_kind:
192 actual_ctx = exp->v.Subscript.ctx;
193 break;
194 case Starred_kind:
195 actual_ctx = exp->v.Starred.ctx;
196 break;
197 case Name_kind:
198 actual_ctx = exp->v.Name.ctx;
199 break;
200 case List_kind:
201 actual_ctx = exp->v.List.ctx;
202 break;
203 case Tuple_kind:
204 actual_ctx = exp->v.Tuple.ctx;
205 break;
206 default:
207 if (ctx != Load) {
208 PyErr_Format(PyExc_ValueError, "expression which can't be "
209 "assigned to in %s context", expr_context_name(ctx));
210 return 0;
211 }
212 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100213 /* set actual_ctx to prevent gcc warning */
214 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500215 }
216 if (check_ctx && actual_ctx != ctx) {
217 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
218 expr_context_name(ctx), expr_context_name(actual_ctx));
219 return 0;
220 }
221
222 /* Now validate expression. */
223 switch (exp->kind) {
224 case BoolOp_kind:
225 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
226 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
227 return 0;
228 }
229 return validate_exprs(exp->v.BoolOp.values, Load, 0);
230 case BinOp_kind:
231 return validate_expr(exp->v.BinOp.left, Load) &&
232 validate_expr(exp->v.BinOp.right, Load);
233 case UnaryOp_kind:
234 return validate_expr(exp->v.UnaryOp.operand, Load);
235 case Lambda_kind:
236 return validate_arguments(exp->v.Lambda.args) &&
237 validate_expr(exp->v.Lambda.body, Load);
238 case IfExp_kind:
239 return validate_expr(exp->v.IfExp.test, Load) &&
240 validate_expr(exp->v.IfExp.body, Load) &&
241 validate_expr(exp->v.IfExp.orelse, Load);
242 case Dict_kind:
243 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
244 PyErr_SetString(PyExc_ValueError,
245 "Dict doesn't have the same number of keys as values");
246 return 0;
247 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400248 /* null_ok=1 for keys expressions to allow dict unpacking to work in
249 dict literals, i.e. ``{**{a:b}}`` */
250 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
251 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500252 case Set_kind:
253 return validate_exprs(exp->v.Set.elts, Load, 0);
254#define COMP(NAME) \
255 case NAME ## _kind: \
256 return validate_comprehension(exp->v.NAME.generators) && \
257 validate_expr(exp->v.NAME.elt, Load);
258 COMP(ListComp)
259 COMP(SetComp)
260 COMP(GeneratorExp)
261#undef COMP
262 case DictComp_kind:
263 return validate_comprehension(exp->v.DictComp.generators) &&
264 validate_expr(exp->v.DictComp.key, Load) &&
265 validate_expr(exp->v.DictComp.value, Load);
266 case Yield_kind:
267 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500268 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000269 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400270 case Await_kind:
271 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500272 case Compare_kind:
273 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
274 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
275 return 0;
276 }
277 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
278 asdl_seq_LEN(exp->v.Compare.ops)) {
279 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
280 "of comparators and operands");
281 return 0;
282 }
283 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
284 validate_expr(exp->v.Compare.left, Load);
285 case Call_kind:
286 return validate_expr(exp->v.Call.func, Load) &&
287 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400288 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100289 case Constant_kind:
290 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100291 PyErr_Format(PyExc_TypeError,
292 "got an invalid type in Constant: %s",
293 Py_TYPE(exp->v.Constant.value)->tp_name);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100294 return 0;
295 }
296 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500297 case Num_kind: {
298 PyObject *n = exp->v.Num.n;
299 if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) &&
300 !PyComplex_CheckExact(n)) {
301 PyErr_SetString(PyExc_TypeError, "non-numeric type in Num");
302 return 0;
303 }
304 return 1;
305 }
306 case Str_kind: {
307 PyObject *s = exp->v.Str.s;
308 if (!PyUnicode_CheckExact(s)) {
309 PyErr_SetString(PyExc_TypeError, "non-string type in Str");
310 return 0;
311 }
312 return 1;
313 }
Eric V. Smith235a6f02015-09-19 14:51:32 -0400314 case JoinedStr_kind:
315 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
316 case FormattedValue_kind:
317 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
318 return 0;
319 if (exp->v.FormattedValue.format_spec)
320 return validate_expr(exp->v.FormattedValue.format_spec, Load);
321 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500322 case Bytes_kind: {
323 PyObject *b = exp->v.Bytes.s;
324 if (!PyBytes_CheckExact(b)) {
325 PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes");
326 return 0;
327 }
328 return 1;
329 }
330 case Attribute_kind:
331 return validate_expr(exp->v.Attribute.value, Load);
332 case Subscript_kind:
333 return validate_slice(exp->v.Subscript.slice) &&
334 validate_expr(exp->v.Subscript.value, Load);
335 case Starred_kind:
336 return validate_expr(exp->v.Starred.value, ctx);
337 case List_kind:
338 return validate_exprs(exp->v.List.elts, ctx, 0);
339 case Tuple_kind:
340 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
341 /* These last cases don't have any checking. */
342 case Name_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -0500343 case NameConstant_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500344 case Ellipsis_kind:
345 return 1;
346 default:
347 PyErr_SetString(PyExc_SystemError, "unexpected expression");
348 return 0;
349 }
350}
351
352static int
353validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
354{
355 if (asdl_seq_LEN(seq))
356 return 1;
357 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
358 return 0;
359}
360
361static int
362validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
363{
364 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
365 validate_exprs(targets, ctx, 0);
366}
367
368static int
INADA Naokicb41b272017-02-23 00:31:59 +0900369validate_body(asdl_seq *body, const char *owner, int allowempty)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500370{
INADA Naokicb41b272017-02-23 00:31:59 +0900371 if (!allowempty && !validate_nonempty_seq(body, "body", owner)) {
372 return 0;
373 }
374 return validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500375}
376
377static int
378validate_stmt(stmt_ty stmt)
379{
380 int i;
381 switch (stmt->kind) {
382 case FunctionDef_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900383 return validate_body(stmt->v.FunctionDef.body, "FunctionDef",
384 stmt->v.FunctionDef.docstring != NULL) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500385 validate_arguments(stmt->v.FunctionDef.args) &&
386 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
387 (!stmt->v.FunctionDef.returns ||
388 validate_expr(stmt->v.FunctionDef.returns, Load));
389 case ClassDef_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900390 return validate_body(stmt->v.ClassDef.body, "ClassDef",
391 stmt->v.ClassDef.docstring != NULL) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500392 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
393 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400394 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500395 case Return_kind:
396 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
397 case Delete_kind:
398 return validate_assignlist(stmt->v.Delete.targets, Del);
399 case Assign_kind:
400 return validate_assignlist(stmt->v.Assign.targets, Store) &&
401 validate_expr(stmt->v.Assign.value, Load);
402 case AugAssign_kind:
403 return validate_expr(stmt->v.AugAssign.target, Store) &&
404 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700405 case AnnAssign_kind:
406 if (stmt->v.AnnAssign.target->kind != Name_kind &&
407 stmt->v.AnnAssign.simple) {
408 PyErr_SetString(PyExc_TypeError,
409 "AnnAssign with simple non-Name target");
410 return 0;
411 }
412 return validate_expr(stmt->v.AnnAssign.target, Store) &&
413 (!stmt->v.AnnAssign.value ||
414 validate_expr(stmt->v.AnnAssign.value, Load)) &&
415 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500416 case For_kind:
417 return validate_expr(stmt->v.For.target, Store) &&
418 validate_expr(stmt->v.For.iter, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900419 validate_body(stmt->v.For.body, "For", 0) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500420 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400421 case AsyncFor_kind:
422 return validate_expr(stmt->v.AsyncFor.target, Store) &&
423 validate_expr(stmt->v.AsyncFor.iter, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900424 validate_body(stmt->v.AsyncFor.body, "AsyncFor", 0) &&
Yury Selivanov75445082015-05-11 22:57:16 -0400425 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500426 case While_kind:
427 return validate_expr(stmt->v.While.test, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900428 validate_body(stmt->v.While.body, "While", 0) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500429 validate_stmts(stmt->v.While.orelse);
430 case If_kind:
431 return validate_expr(stmt->v.If.test, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900432 validate_body(stmt->v.If.body, "If", 0) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500433 validate_stmts(stmt->v.If.orelse);
434 case With_kind:
435 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
436 return 0;
437 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
438 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
439 if (!validate_expr(item->context_expr, Load) ||
440 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
441 return 0;
442 }
INADA Naokicb41b272017-02-23 00:31:59 +0900443 return validate_body(stmt->v.With.body, "With", 0);
Yury Selivanov75445082015-05-11 22:57:16 -0400444 case AsyncWith_kind:
445 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
446 return 0;
447 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
448 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
449 if (!validate_expr(item->context_expr, Load) ||
450 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
451 return 0;
452 }
INADA Naokicb41b272017-02-23 00:31:59 +0900453 return validate_body(stmt->v.AsyncWith.body, "AsyncWith", 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500454 case Raise_kind:
455 if (stmt->v.Raise.exc) {
456 return validate_expr(stmt->v.Raise.exc, Load) &&
457 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
458 }
459 if (stmt->v.Raise.cause) {
460 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
461 return 0;
462 }
463 return 1;
464 case Try_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900465 if (!validate_body(stmt->v.Try.body, "Try", 0))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500466 return 0;
467 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
468 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
469 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
470 return 0;
471 }
472 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
473 asdl_seq_LEN(stmt->v.Try.orelse)) {
474 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
475 return 0;
476 }
477 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
478 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
479 if ((handler->v.ExceptHandler.type &&
480 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
INADA Naokicb41b272017-02-23 00:31:59 +0900481 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler", 0))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500482 return 0;
483 }
484 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
485 validate_stmts(stmt->v.Try.finalbody)) &&
486 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
487 validate_stmts(stmt->v.Try.orelse));
488 case Assert_kind:
489 return validate_expr(stmt->v.Assert.test, Load) &&
490 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
491 case Import_kind:
492 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
493 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300494 if (stmt->v.ImportFrom.level < 0) {
495 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500496 return 0;
497 }
498 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
499 case Global_kind:
500 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
501 case Nonlocal_kind:
502 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
503 case Expr_kind:
504 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400505 case AsyncFunctionDef_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900506 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef",
507 stmt->v.AsyncFunctionDef.docstring != NULL) &&
Yury Selivanov75445082015-05-11 22:57:16 -0400508 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
509 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
510 (!stmt->v.AsyncFunctionDef.returns ||
511 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500512 case Pass_kind:
513 case Break_kind:
514 case Continue_kind:
515 return 1;
516 default:
517 PyErr_SetString(PyExc_SystemError, "unexpected statement");
518 return 0;
519 }
520}
521
522static int
523validate_stmts(asdl_seq *seq)
524{
525 int i;
526 for (i = 0; i < asdl_seq_LEN(seq); i++) {
527 stmt_ty stmt = asdl_seq_GET(seq, i);
528 if (stmt) {
529 if (!validate_stmt(stmt))
530 return 0;
531 }
532 else {
533 PyErr_SetString(PyExc_ValueError,
534 "None disallowed in statement list");
535 return 0;
536 }
537 }
538 return 1;
539}
540
541static int
542validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
543{
544 int i;
545 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
546 expr_ty expr = asdl_seq_GET(exprs, i);
547 if (expr) {
548 if (!validate_expr(expr, ctx))
549 return 0;
550 }
551 else if (!null_ok) {
552 PyErr_SetString(PyExc_ValueError,
553 "None disallowed in expression list");
554 return 0;
555 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100556
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500557 }
558 return 1;
559}
560
561int
562PyAST_Validate(mod_ty mod)
563{
564 int res = 0;
565
566 switch (mod->kind) {
567 case Module_kind:
568 res = validate_stmts(mod->v.Module.body);
569 break;
570 case Interactive_kind:
571 res = validate_stmts(mod->v.Interactive.body);
572 break;
573 case Expression_kind:
574 res = validate_expr(mod->v.Expression.body, Load);
575 break;
576 case Suite_kind:
577 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
578 break;
579 default:
580 PyErr_SetString(PyExc_SystemError, "impossible module node");
581 res = 0;
582 break;
583 }
584 return res;
585}
586
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500587/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500588#include "grammar.h"
589#include "parsetok.h"
590#include "graminit.h"
591
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592/* Data structure used internally */
593struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400594 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200595 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500596 PyObject *c_normalize; /* Normalization function from unicodedata. */
597 PyObject *c_normalize_args; /* Normalization argument tuple. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598};
599
600static asdl_seq *seq_for_testlist(struct compiling *, const node *);
601static expr_ty ast_for_expr(struct compiling *, const node *);
602static stmt_ty ast_for_stmt(struct compiling *, const node *);
INADA Naokicb41b272017-02-23 00:31:59 +0900603static asdl_seq *ast_for_body(struct compiling *c, const node *n,
604 string *docstring);
605static string docstring_from_stmts(asdl_seq *stmts);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000606static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
607 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000608static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000609static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000610
Yury Selivanov75445082015-05-11 22:57:16 -0400611static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
612static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
613
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000614/* Note different signature for ast_for_call */
615static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
616
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000617static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400618static expr_ty parsestrplus(struct compiling *, const node *n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000619
Nick Coghlan650f0d02007-04-15 12:05:43 +0000620#define COMP_GENEXP 0
621#define COMP_LISTCOMP 1
622#define COMP_SETCOMP 2
623
Benjamin Peterson55e00432012-01-16 17:22:31 -0500624static int
625init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000626{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500627 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
628 if (!m)
629 return 0;
630 c->c_normalize = PyObject_GetAttrString(m, "normalize");
631 Py_DECREF(m);
632 if (!c->c_normalize)
633 return 0;
634 c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500635 if (!c->c_normalize_args) {
636 Py_CLEAR(c->c_normalize);
637 return 0;
638 }
Christian Heimes72f562f2013-07-24 21:02:17 +0200639 PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500640 return 1;
641}
642
643static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400644new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500645{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400646 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500647 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000648 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500649 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500650 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000651 /* Check whether there are non-ASCII characters in the
652 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500653 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200654 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500655 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500656 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200657 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500658 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500659 PyTuple_SET_ITEM(c->c_normalize_args, 1, id);
660 id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500661 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200662 if (!id2)
663 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200664 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000665 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000666 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200667 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
668 Py_DECREF(id);
669 return NULL;
670 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000671 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672}
673
Benjamin Peterson55e00432012-01-16 17:22:31 -0500674#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400677ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400679 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680
Victor Stinner14e461d2013-08-26 22:28:21 +0200681 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000683 Py_INCREF(Py_None);
684 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000685 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200686 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400687 if (!tmp)
688 return 0;
689 errstr = PyUnicode_FromString(errmsg);
690 if (!errstr) {
691 Py_DECREF(tmp);
692 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000693 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000694 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000695 Py_DECREF(errstr);
696 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400697 if (value) {
698 PyErr_SetObject(PyExc_SyntaxError, value);
699 Py_DECREF(value);
700 }
701 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702}
703
704/* num_stmts() returns number of contained statements.
705
706 Use this routine to determine how big a sequence is needed for
707 the statements in a parse tree. Its raison d'etre is this bit of
708 grammar:
709
710 stmt: simple_stmt | compound_stmt
711 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
712
713 A simple_stmt can contain multiple small_stmt elements joined
714 by semicolons. If the arg is a simple_stmt, the number of
715 small_stmt elements is returned.
716*/
717
718static int
719num_stmts(const node *n)
720{
721 int i, l;
722 node *ch;
723
724 switch (TYPE(n)) {
725 case single_input:
726 if (TYPE(CHILD(n, 0)) == NEWLINE)
727 return 0;
728 else
729 return num_stmts(CHILD(n, 0));
730 case file_input:
731 l = 0;
732 for (i = 0; i < NCH(n); i++) {
733 ch = CHILD(n, i);
734 if (TYPE(ch) == stmt)
735 l += num_stmts(ch);
736 }
737 return l;
738 case stmt:
739 return num_stmts(CHILD(n, 0));
740 case compound_stmt:
741 return 1;
742 case simple_stmt:
743 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
744 case suite:
745 if (NCH(n) == 1)
746 return num_stmts(CHILD(n, 0));
747 else {
748 l = 0;
749 for (i = 2; i < (NCH(n) - 1); i++)
750 l += num_stmts(CHILD(n, i));
751 return l;
752 }
753 default: {
754 char buf[128];
755
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000756 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000757 TYPE(n), NCH(n));
758 Py_FatalError(buf);
759 }
760 }
761 assert(0);
762 return 0;
763}
764
765/* Transform the CST rooted at node * to the appropriate AST
766*/
767
768mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200769PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
770 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000772 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773 asdl_seq *stmts = NULL;
774 stmt_ty s;
775 node *ch;
776 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500777 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400779 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200780 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400781 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800782 c.c_normalize = NULL;
783 c.c_normalize_args = NULL;
784
785 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787
Jeremy Hyltona8293132006-02-28 17:58:27 +0000788 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 switch (TYPE(n)) {
790 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200791 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500793 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794 for (i = 0; i < NCH(n) - 1; i++) {
795 ch = CHILD(n, i);
796 if (TYPE(ch) == NEWLINE)
797 continue;
798 REQ(ch, stmt);
799 num = num_stmts(ch);
800 if (num == 1) {
801 s = ast_for_stmt(&c, ch);
802 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500803 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000804 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000805 }
806 else {
807 ch = CHILD(ch, 0);
808 REQ(ch, simple_stmt);
809 for (j = 0; j < num; j++) {
810 s = ast_for_stmt(&c, CHILD(ch, j * 2));
811 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500812 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000813 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814 }
815 }
816 }
INADA Naokicb41b272017-02-23 00:31:59 +0900817 res = Module(stmts, docstring_from_stmts(stmts), arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500818 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819 case eval_input: {
820 expr_ty testlist_ast;
821
Nick Coghlan650f0d02007-04-15 12:05:43 +0000822 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000823 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500825 goto out;
826 res = Expression(testlist_ast, arena);
827 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828 }
829 case single_input:
830 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200831 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500833 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000834 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
835 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000836 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500837 goto out;
838 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 }
840 else {
841 n = CHILD(n, 0);
842 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200843 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500845 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000847 s = ast_for_stmt(&c, n);
848 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500849 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850 asdl_seq_SET(stmts, 0, s);
851 }
852 else {
853 /* Only a simple_stmt can contain multiple statements. */
854 REQ(n, simple_stmt);
855 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856 if (TYPE(CHILD(n, i)) == NEWLINE)
857 break;
858 s = ast_for_stmt(&c, CHILD(n, i));
859 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500860 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 asdl_seq_SET(stmts, i / 2, s);
862 }
863 }
864
Benjamin Peterson55e00432012-01-16 17:22:31 -0500865 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500867 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000869 PyErr_Format(PyExc_SystemError,
870 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500871 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500873 out:
874 if (c.c_normalize) {
875 Py_DECREF(c.c_normalize);
876 PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL);
877 Py_DECREF(c.c_normalize_args);
878 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500879 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000880}
881
Victor Stinner14e461d2013-08-26 22:28:21 +0200882mod_ty
883PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
884 PyArena *arena)
885{
886 mod_ty mod;
887 PyObject *filename;
888 filename = PyUnicode_DecodeFSDefault(filename_str);
889 if (filename == NULL)
890 return NULL;
891 mod = PyAST_FromNodeObject(n, flags, filename, arena);
892 Py_DECREF(filename);
893 return mod;
894
895}
896
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000897/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
898*/
899
900static operator_ty
901get_operator(const node *n)
902{
903 switch (TYPE(n)) {
904 case VBAR:
905 return BitOr;
906 case CIRCUMFLEX:
907 return BitXor;
908 case AMPER:
909 return BitAnd;
910 case LEFTSHIFT:
911 return LShift;
912 case RIGHTSHIFT:
913 return RShift;
914 case PLUS:
915 return Add;
916 case MINUS:
917 return Sub;
918 case STAR:
919 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400920 case AT:
921 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000922 case SLASH:
923 return Div;
924 case DOUBLESLASH:
925 return FloorDiv;
926 case PERCENT:
927 return Mod;
928 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000929 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000930 }
931}
932
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200933static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000934 "None",
935 "True",
936 "False",
937 NULL,
938};
939
940static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400941forbidden_name(struct compiling *c, identifier name, const node *n,
942 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000943{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000944 assert(PyUnicode_Check(name));
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200945 if (_PyUnicode_EqualToASCIIString(name, "__debug__")) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400946 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000947 return 1;
948 }
Serhiy Storchaka3b73ea12016-11-16 10:19:20 +0200949 if (_PyUnicode_EqualToASCIIString(name, "async") ||
950 _PyUnicode_EqualToASCIIString(name, "await"))
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400951 {
952 PyObject *message = PyUnicode_FromString(
953 "'async' and 'await' will become reserved keywords"
954 " in Python 3.7");
Yury Selivanov1a9d6872016-11-08 16:54:18 -0500955 int ret;
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400956 if (message == NULL) {
957 return 1;
958 }
Yury Selivanov1a9d6872016-11-08 16:54:18 -0500959 ret = PyErr_WarnExplicitObject(
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400960 PyExc_DeprecationWarning,
961 message,
962 c->c_filename,
963 LINENO(n),
964 NULL,
Yury Selivanov1a9d6872016-11-08 16:54:18 -0500965 NULL);
966 Py_DECREF(message);
967 if (ret < 0) {
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400968 return 1;
969 }
970 }
Benjamin Peterson70f52762009-06-28 23:32:44 +0000971 if (full_checks) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200972 const char * const *p;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000973 for (p = FORBIDDEN; *p; p++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200974 if (_PyUnicode_EqualToASCIIString(name, *p)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400975 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000976 return 1;
977 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000978 }
979 }
980 return 0;
981}
982
Jeremy Hyltona8293132006-02-28 17:58:27 +0000983/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984
985 Only sets context for expr kinds that "can appear in assignment context"
986 (according to ../Parser/Python.asdl). For other expr kinds, it sets
987 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988*/
989
990static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000991set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992{
993 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000994 /* If a particular expression type can't be used for assign / delete,
995 set expr_name to its name and an error message will be generated.
996 */
997 const char* expr_name = NULL;
998
999 /* The ast defines augmented store and load contexts, but the
1000 implementation here doesn't actually use them. The code may be
1001 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001002 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001003 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +00001004 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001005 */
1006 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007
1008 switch (e->kind) {
1009 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001010 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001011 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001012 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001013 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001015 e->v.Subscript.ctx = ctx;
1016 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001017 case Starred_kind:
1018 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001019 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001020 return 0;
1021 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +00001023 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001024 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001025 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001026 }
1027 e->v.Name.ctx = ctx;
1028 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001030 e->v.List.ctx = ctx;
1031 s = e->v.List.elts;
1032 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001034 e->v.Tuple.ctx = ctx;
1035 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001036 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001037 case Lambda_kind:
1038 expr_name = "lambda";
1039 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001040 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001041 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001042 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001043 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001044 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001045 case UnaryOp_kind:
1046 expr_name = "operator";
1047 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001048 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001049 expr_name = "generator expression";
1050 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001051 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001052 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001053 expr_name = "yield expression";
1054 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001055 case Await_kind:
1056 expr_name = "await expression";
1057 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001058 case ListComp_kind:
1059 expr_name = "list comprehension";
1060 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001061 case SetComp_kind:
1062 expr_name = "set comprehension";
1063 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001064 case DictComp_kind:
1065 expr_name = "dict comprehension";
1066 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001067 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +00001068 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001069 case Num_kind:
1070 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -05001071 case Bytes_kind:
Eric V. Smith235a6f02015-09-19 14:51:32 -04001072 case JoinedStr_kind:
1073 case FormattedValue_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001074 expr_name = "literal";
1075 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001076 case NameConstant_kind:
1077 expr_name = "keyword";
1078 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001079 case Ellipsis_kind:
1080 expr_name = "Ellipsis";
1081 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001082 case Compare_kind:
1083 expr_name = "comparison";
1084 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001085 case IfExp_kind:
1086 expr_name = "conditional expression";
1087 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001088 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 PyErr_Format(PyExc_SystemError,
1090 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001091 e->kind, e->lineno);
1092 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001094 /* Check for error string set by switch */
1095 if (expr_name) {
1096 char buf[300];
1097 PyOS_snprintf(buf, sizeof(buf),
1098 "can't %s %s",
1099 ctx == Store ? "assign to" : "delete",
1100 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001101 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001102 }
1103
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106 */
1107 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001108 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109
Thomas Wouters89f507f2006-12-13 04:49:30 +00001110 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001111 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001112 return 0;
1113 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114 }
1115 return 1;
1116}
1117
1118static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001119ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120{
1121 REQ(n, augassign);
1122 n = CHILD(n, 0);
1123 switch (STR(n)[0]) {
1124 case '+':
1125 return Add;
1126 case '-':
1127 return Sub;
1128 case '/':
1129 if (STR(n)[1] == '/')
1130 return FloorDiv;
1131 else
1132 return Div;
1133 case '%':
1134 return Mod;
1135 case '<':
1136 return LShift;
1137 case '>':
1138 return RShift;
1139 case '&':
1140 return BitAnd;
1141 case '^':
1142 return BitXor;
1143 case '|':
1144 return BitOr;
1145 case '*':
1146 if (STR(n)[1] == '*')
1147 return Pow;
1148 else
1149 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001150 case '@':
1151 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001153 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001154 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155 }
1156}
1157
1158static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001159ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001161 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001162 |'is' 'not'
1163 */
1164 REQ(n, comp_op);
1165 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001166 n = CHILD(n, 0);
1167 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168 case LESS:
1169 return Lt;
1170 case GREATER:
1171 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001172 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173 return Eq;
1174 case LESSEQUAL:
1175 return LtE;
1176 case GREATEREQUAL:
1177 return GtE;
1178 case NOTEQUAL:
1179 return NotEq;
1180 case NAME:
1181 if (strcmp(STR(n), "in") == 0)
1182 return In;
1183 if (strcmp(STR(n), "is") == 0)
1184 return Is;
1185 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001186 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001188 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001189 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190 }
1191 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001192 /* handle "not in" and "is not" */
1193 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194 case NAME:
1195 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1196 return NotIn;
1197 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1198 return IsNot;
1199 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001200 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001202 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001203 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204 }
Neal Norwitz79792652005-11-14 04:25:03 +00001205 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001207 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208}
1209
1210static asdl_seq *
1211seq_for_testlist(struct compiling *c, const node *n)
1212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001214 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1215 */
Armin Rigo31441302005-10-21 12:57:31 +00001216 asdl_seq *seq;
1217 expr_ty expression;
1218 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001219 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001220
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001221 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222 if (!seq)
1223 return NULL;
1224
1225 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001227 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001228
Benjamin Peterson4905e802009-09-27 02:43:28 +00001229 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001230 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001231 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001232
1233 assert(i / 2 < seq->size);
1234 asdl_seq_SET(seq, i / 2, expression);
1235 }
1236 return seq;
1237}
1238
Neal Norwitzc1505362006-12-28 06:47:50 +00001239static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001240ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001241{
1242 identifier name;
1243 expr_ty annotation = NULL;
1244 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001245 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001246
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001247 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001248 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001249 name = NEW_IDENTIFIER(ch);
1250 if (!name)
1251 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001252 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001253 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001254
1255 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1256 annotation = ast_for_expr(c, CHILD(n, 2));
1257 if (!annotation)
1258 return NULL;
1259 }
1260
Victor Stinnerc106c682015-11-06 17:01:48 +01001261 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001262 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001263 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001264 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265}
1266
Guido van Rossum4f72a782006-10-27 23:31:49 +00001267/* returns -1 if failed to handle keyword only arguments
1268 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001269 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001270 ^^^
1271 start pointing here
1272 */
1273static int
1274handle_keywordonly_args(struct compiling *c, const node *n, int start,
1275 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1276{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001277 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001278 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001279 expr_ty expression, annotation;
1280 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001281 int i = start;
1282 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001283
1284 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001285 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001286 return -1;
1287 }
1288 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001289 while (i < NCH(n)) {
1290 ch = CHILD(n, i);
1291 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001292 case vfpdef:
1293 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001294 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001295 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001296 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001297 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001298 asdl_seq_SET(kwdefaults, j, expression);
1299 i += 2; /* '=' and test */
1300 }
1301 else { /* setting NULL if no default value exists */
1302 asdl_seq_SET(kwdefaults, j, NULL);
1303 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001304 if (NCH(ch) == 3) {
1305 /* ch is NAME ':' test */
1306 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001307 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001308 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001309 }
1310 else {
1311 annotation = NULL;
1312 }
1313 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001314 argname = NEW_IDENTIFIER(ch);
1315 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001316 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001317 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001318 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001319 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1320 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001321 if (!arg)
1322 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001323 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001324 i += 2; /* the name and the comma */
1325 break;
1326 case DOUBLESTAR:
1327 return i;
1328 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001329 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001330 goto error;
1331 }
1332 }
1333 return i;
1334 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001336}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001337
Jeremy Hyltona8293132006-02-28 17:58:27 +00001338/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001339
1340static arguments_ty
1341ast_for_arguments(struct compiling *c, const node *n)
1342{
Neal Norwitzc1505362006-12-28 06:47:50 +00001343 /* This function handles both typedargslist (function definition)
1344 and varargslist (lambda definition).
1345
1346 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001347 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1348 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1349 | '**' tfpdef [',']]]
1350 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1351 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001352 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001353 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1354 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1355 | '**' vfpdef [',']]]
1356 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1357 | '**' vfpdef [',']
1358 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001359 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001360
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001361 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001362 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1363 int nposdefaults = 0, found_default = 0;
1364 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001365 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001366 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001367 node *ch;
1368
1369 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001370 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001371 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001372 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001374 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375
Jeremy Hyltone921e022008-07-17 16:37:17 +00001376 /* First count the number of positional args & defaults. The
1377 variable i is the loop index for this for loop and the next.
1378 The next loop picks up where the first leaves off.
1379 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001380 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001381 ch = CHILD(n, i);
1382 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001383 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001384 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001385 if (i < NCH(n) && /* skip argument following star */
1386 (TYPE(CHILD(n, i)) == tfpdef ||
1387 TYPE(CHILD(n, i)) == vfpdef)) {
1388 i++;
1389 }
1390 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001391 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001392 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001393 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001394 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001395 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001397 defaults for keyword only args */
1398 for ( ; i < NCH(n); ++i) {
1399 ch = CHILD(n, i);
1400 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001401 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001402 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001403 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001404 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001405 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001406 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001407 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001408 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001409 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001411 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001412 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001413 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001415 since we set NULL as default for keyword only argument w/o default
1416 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001417 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001418 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001419 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001420 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001421
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001422 /* tfpdef: NAME [':' test]
1423 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001424 */
1425 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001426 j = 0; /* index for defaults */
1427 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001429 ch = CHILD(n, i);
1430 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001431 case tfpdef:
1432 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1434 anything other than EQUAL or a comma? */
1435 /* XXX Should NCH(n) check be made a separate check? */
1436 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001437 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1438 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001439 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001440 assert(posdefaults != NULL);
1441 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001443 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001445 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001446 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001447 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001448 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001449 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001450 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001451 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001452 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001453 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454 i += 2; /* the name and the comma */
1455 break;
1456 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001457 if (i+1 >= NCH(n) ||
1458 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001459 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001460 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001461 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001462 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001463 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001464 if (TYPE(ch) == COMMA) {
1465 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001466 i += 2; /* now follows keyword only arguments */
1467 res = handle_keywordonly_args(c, n, i,
1468 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001469 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001470 i = res; /* res has new position to process */
1471 }
1472 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001473 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001474 if (!vararg)
1475 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001476
Guido van Rossum4f72a782006-10-27 23:31:49 +00001477 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001478 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1479 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001480 int res = 0;
1481 res = handle_keywordonly_args(c, n, i,
1482 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001483 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001484 i = res; /* res has new position to process */
1485 }
1486 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487 break;
1488 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001489 ch = CHILD(n, i+1); /* tfpdef */
1490 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001491 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001492 if (!kwarg)
1493 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494 i += 3;
1495 break;
1496 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001497 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498 "unexpected node in varargslist: %d @ %d",
1499 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001500 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001501 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001503 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504}
1505
1506static expr_ty
1507ast_for_dotted_name(struct compiling *c, const node *n)
1508{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001509 expr_ty e;
1510 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001511 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512 int i;
1513
1514 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001515
1516 lineno = LINENO(n);
1517 col_offset = n->n_col_offset;
1518
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519 id = NEW_IDENTIFIER(CHILD(n, 0));
1520 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001521 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001522 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001524 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525
1526 for (i = 2; i < NCH(n); i+=2) {
1527 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001528 if (!id)
1529 return NULL;
1530 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1531 if (!e)
1532 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533 }
1534
1535 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536}
1537
1538static expr_ty
1539ast_for_decorator(struct compiling *c, const node *n)
1540{
1541 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1542 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001543 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001546 REQ(CHILD(n, 0), AT);
1547 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1550 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001551 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001553 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001554 d = name_expr;
1555 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001556 }
1557 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001558 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001559 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001560 if (!d)
1561 return NULL;
1562 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001563 }
1564 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001565 d = ast_for_call(c, CHILD(n, 3), name_expr);
1566 if (!d)
1567 return NULL;
1568 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001569 }
1570
1571 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001572}
1573
1574static asdl_seq*
1575ast_for_decorators(struct compiling *c, const node *n)
1576{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001577 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001578 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001579 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001582 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583 if (!decorator_seq)
1584 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001587 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001588 if (!d)
1589 return NULL;
1590 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591 }
1592 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001593}
1594
1595static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001596ast_for_funcdef_impl(struct compiling *c, const node *n,
1597 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001599 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001600 identifier name;
1601 arguments_ty args;
1602 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001603 expr_ty returns = NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09001604 string docstring;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001605 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001606
1607 REQ(n, funcdef);
1608
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001609 name = NEW_IDENTIFIER(CHILD(n, name_i));
1610 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001611 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001612 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001613 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001614 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1615 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001616 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001617 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1618 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1619 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001620 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001621 name_i += 2;
1622 }
INADA Naokicb41b272017-02-23 00:31:59 +09001623 body = ast_for_body(c, CHILD(n, name_i + 3), &docstring);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001624 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001625 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626
Yury Selivanov75445082015-05-11 22:57:16 -04001627 if (is_async)
1628 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
INADA Naokicb41b272017-02-23 00:31:59 +09001629 docstring, LINENO(n),
Yury Selivanov75445082015-05-11 22:57:16 -04001630 n->n_col_offset, c->c_arena);
1631 else
1632 return FunctionDef(name, args, body, decorator_seq, returns,
INADA Naokicb41b272017-02-23 00:31:59 +09001633 docstring, LINENO(n),
Yury Selivanov75445082015-05-11 22:57:16 -04001634 n->n_col_offset, c->c_arena);
1635}
1636
1637static stmt_ty
1638ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1639{
1640 /* async_funcdef: ASYNC funcdef */
1641 REQ(n, async_funcdef);
1642 REQ(CHILD(n, 0), ASYNC);
1643 REQ(CHILD(n, 1), funcdef);
1644
1645 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1646 1 /* is_async */);
1647}
1648
1649static stmt_ty
1650ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1651{
1652 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1653 return ast_for_funcdef_impl(c, n, decorator_seq,
1654 0 /* is_async */);
1655}
1656
1657
1658static stmt_ty
1659ast_for_async_stmt(struct compiling *c, const node *n)
1660{
1661 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
1662 REQ(n, async_stmt);
1663 REQ(CHILD(n, 0), ASYNC);
1664
1665 switch (TYPE(CHILD(n, 1))) {
1666 case funcdef:
1667 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1668 1 /* is_async */);
1669 case with_stmt:
1670 return ast_for_with_stmt(c, CHILD(n, 1),
1671 1 /* is_async */);
1672
1673 case for_stmt:
1674 return ast_for_for_stmt(c, CHILD(n, 1),
1675 1 /* is_async */);
1676
1677 default:
1678 PyErr_Format(PyExc_SystemError,
1679 "invalid async stament: %s",
1680 STR(CHILD(n, 1)));
1681 return NULL;
1682 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683}
1684
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001685static stmt_ty
1686ast_for_decorated(struct compiling *c, const node *n)
1687{
Yury Selivanov75445082015-05-11 22:57:16 -04001688 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001689 stmt_ty thing = NULL;
1690 asdl_seq *decorator_seq = NULL;
1691
1692 REQ(n, decorated);
1693
1694 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1695 if (!decorator_seq)
1696 return NULL;
1697
1698 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001699 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001701
1702 if (TYPE(CHILD(n, 1)) == funcdef) {
1703 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1704 } else if (TYPE(CHILD(n, 1)) == classdef) {
1705 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001706 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1707 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001708 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001709 /* we count the decorators in when talking about the class' or
1710 * function's line number */
1711 if (thing) {
1712 thing->lineno = LINENO(n);
1713 thing->col_offset = n->n_col_offset;
1714 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001715 return thing;
1716}
1717
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718static expr_ty
1719ast_for_lambdef(struct compiling *c, const node *n)
1720{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001721 /* lambdef: 'lambda' [varargslist] ':' test
1722 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001723 arguments_ty args;
1724 expr_ty expression;
1725
1726 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001727 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728 if (!args)
1729 return NULL;
1730 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001731 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001733 }
1734 else {
1735 args = ast_for_arguments(c, CHILD(n, 1));
1736 if (!args)
1737 return NULL;
1738 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001739 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001741 }
1742
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001743 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744}
1745
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001746static expr_ty
1747ast_for_ifexpr(struct compiling *c, const node *n)
1748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001750 expr_ty expression, body, orelse;
1751
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001752 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001753 body = ast_for_expr(c, CHILD(n, 0));
1754 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001755 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001756 expression = ast_for_expr(c, CHILD(n, 2));
1757 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001758 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001759 orelse = ast_for_expr(c, CHILD(n, 4));
1760 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001761 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001762 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1763 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001764}
1765
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001766/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001767 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768
Nick Coghlan650f0d02007-04-15 12:05:43 +00001769 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770*/
1771
1772static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001773count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001774{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001775 int n_fors = 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001776 int is_async;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777
Guido van Rossumd8faa362007-04-27 19:54:29 +00001778 count_comp_for:
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001779 is_async = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001780 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001781 REQ(n, comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001782 if (TYPE(CHILD(n, 0)) == ASYNC) {
1783 is_async = 1;
1784 }
1785 if (NCH(n) == (5 + is_async)) {
1786 n = CHILD(n, 4 + is_async);
1787 }
1788 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001789 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001790 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001791 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001792 REQ(n, comp_iter);
1793 n = CHILD(n, 0);
1794 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001795 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001796 else if (TYPE(n) == comp_if) {
1797 if (NCH(n) == 3) {
1798 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001799 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001800 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001801 else
1802 return n_fors;
1803 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001804
Guido van Rossumd8faa362007-04-27 19:54:29 +00001805 /* Should never be reached */
1806 PyErr_SetString(PyExc_SystemError,
1807 "logic error in count_comp_fors");
1808 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809}
1810
Nick Coghlan650f0d02007-04-15 12:05:43 +00001811/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812
Nick Coghlan650f0d02007-04-15 12:05:43 +00001813 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814*/
1815
1816static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001817count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001819 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820
Guido van Rossumd8faa362007-04-27 19:54:29 +00001821 while (1) {
1822 REQ(n, comp_iter);
1823 if (TYPE(CHILD(n, 0)) == comp_for)
1824 return n_ifs;
1825 n = CHILD(n, 0);
1826 REQ(n, comp_if);
1827 n_ifs++;
1828 if (NCH(n) == 2)
1829 return n_ifs;
1830 n = CHILD(n, 2);
1831 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001832}
1833
Guido van Rossum992d4a32007-07-11 13:09:30 +00001834static asdl_seq *
1835ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001837 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001838 asdl_seq *comps;
1839
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001840 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841 if (n_fors == -1)
1842 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001843
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001844 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001845 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001847
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001848 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001849 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001850 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001851 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001852 node *for_ch;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001853 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854
Guido van Rossum992d4a32007-07-11 13:09:30 +00001855 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001857 if (TYPE(CHILD(n, 0)) == ASYNC) {
1858 is_async = 1;
1859 }
1860
1861 for_ch = CHILD(n, 1 + is_async);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001862 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001863 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001864 return NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001865 expression = ast_for_expr(c, CHILD(n, 3 + is_async));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001866 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001867 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001868
Thomas Wouters89f507f2006-12-13 04:49:30 +00001869 /* Check the # of children rather than the length of t, since
1870 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001871 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001872 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001873 comp = comprehension(first, expression, NULL,
1874 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875 else
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001876 comp = comprehension(Tuple(t, Store, first->lineno,
1877 first->col_offset, c->c_arena),
1878 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001879 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001881
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001882 if (NCH(n) == (5 + is_async)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883 int j, n_ifs;
1884 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001886 n = CHILD(n, 4 + is_async);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001887 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001888 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001889 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001890
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001891 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001892 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001894
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001895 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001896 REQ(n, comp_iter);
1897 n = CHILD(n, 0);
1898 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899
Guido van Rossum992d4a32007-07-11 13:09:30 +00001900 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001901 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001902 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001903 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001904 if (NCH(n) == 3)
1905 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001906 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001907 /* on exit, must guarantee that n is a comp_for */
1908 if (TYPE(n) == comp_iter)
1909 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001910 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001911 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001912 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001914 return comps;
1915}
1916
1917static expr_ty
1918ast_for_itercomp(struct compiling *c, const node *n, int type)
1919{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001920 /* testlist_comp: (test|star_expr)
1921 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001922 expr_ty elt;
1923 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001924 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925
Guido van Rossum992d4a32007-07-11 13:09:30 +00001926 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001928 ch = CHILD(n, 0);
1929 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001930 if (!elt)
1931 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001932 if (elt->kind == Starred_kind) {
1933 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1934 return NULL;
1935 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936
Guido van Rossum992d4a32007-07-11 13:09:30 +00001937 comps = ast_for_comprehension(c, CHILD(n, 1));
1938 if (!comps)
1939 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001940
1941 if (type == COMP_GENEXP)
1942 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1943 else if (type == COMP_LISTCOMP)
1944 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1945 else if (type == COMP_SETCOMP)
1946 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1947 else
1948 /* Should never happen */
1949 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950}
1951
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001952/* Fills in the key, value pair corresponding to the dict element. In case
1953 * of an unpacking, key is NULL. *i is advanced by the number of ast
1954 * elements. Iff successful, nonzero is returned.
1955 */
1956static int
1957ast_for_dictelement(struct compiling *c, const node *n, int *i,
1958 expr_ty *key, expr_ty *value)
1959{
1960 expr_ty expression;
1961 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1962 assert(NCH(n) - *i >= 2);
1963
1964 expression = ast_for_expr(c, CHILD(n, *i + 1));
1965 if (!expression)
1966 return 0;
1967 *key = NULL;
1968 *value = expression;
1969
1970 *i += 2;
1971 }
1972 else {
1973 assert(NCH(n) - *i >= 3);
1974
1975 expression = ast_for_expr(c, CHILD(n, *i));
1976 if (!expression)
1977 return 0;
1978 *key = expression;
1979
1980 REQ(CHILD(n, *i + 1), COLON);
1981
1982 expression = ast_for_expr(c, CHILD(n, *i + 2));
1983 if (!expression)
1984 return 0;
1985 *value = expression;
1986
1987 *i += 3;
1988 }
1989 return 1;
1990}
1991
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001993ast_for_dictcomp(struct compiling *c, const node *n)
1994{
1995 expr_ty key, value;
1996 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001997 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001999 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002000 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002001 assert(key);
2002 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002004 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002005 if (!comps)
2006 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007
Guido van Rossum992d4a32007-07-11 13:09:30 +00002008 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
2009}
2010
2011static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002012ast_for_dictdisplay(struct compiling *c, const node *n)
2013{
2014 int i;
2015 int j;
2016 int size;
2017 asdl_seq *keys, *values;
2018
2019 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2020 keys = _Py_asdl_seq_new(size, c->c_arena);
2021 if (!keys)
2022 return NULL;
2023
2024 values = _Py_asdl_seq_new(size, c->c_arena);
2025 if (!values)
2026 return NULL;
2027
2028 j = 0;
2029 for (i = 0; i < NCH(n); i++) {
2030 expr_ty key, value;
2031
2032 if (!ast_for_dictelement(c, n, &i, &key, &value))
2033 return NULL;
2034 asdl_seq_SET(keys, j, key);
2035 asdl_seq_SET(values, j, value);
2036
2037 j++;
2038 }
2039 keys->size = j;
2040 values->size = j;
2041 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
2042}
2043
2044static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002045ast_for_genexp(struct compiling *c, const node *n)
2046{
2047 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002048 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002049}
2050
2051static expr_ty
2052ast_for_listcomp(struct compiling *c, const node *n)
2053{
2054 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002055 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002056}
2057
2058static expr_ty
2059ast_for_setcomp(struct compiling *c, const node *n)
2060{
2061 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002062 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002063}
2064
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002065static expr_ty
2066ast_for_setdisplay(struct compiling *c, const node *n)
2067{
2068 int i;
2069 int size;
2070 asdl_seq *elts;
2071
2072 assert(TYPE(n) == (dictorsetmaker));
2073 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2074 elts = _Py_asdl_seq_new(size, c->c_arena);
2075 if (!elts)
2076 return NULL;
2077 for (i = 0; i < NCH(n); i += 2) {
2078 expr_ty expression;
2079 expression = ast_for_expr(c, CHILD(n, i));
2080 if (!expression)
2081 return NULL;
2082 asdl_seq_SET(elts, i / 2, expression);
2083 }
2084 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2085}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002086
2087static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002088ast_for_atom(struct compiling *c, const node *n)
2089{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002090 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2091 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002092 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093 */
2094 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002096 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002097 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002098 PyObject *name;
2099 const char *s = STR(ch);
2100 size_t len = strlen(s);
2101 if (len >= 4 && len <= 5) {
2102 if (!strcmp(s, "None"))
2103 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2104 if (!strcmp(s, "True"))
2105 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2106 if (!strcmp(s, "False"))
2107 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2108 }
2109 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002110 if (!name)
2111 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002112 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002113 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2114 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002115 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002116 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002117 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002118 const char *errtype = NULL;
2119 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2120 errtype = "unicode error";
2121 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2122 errtype = "value error";
2123 if (errtype) {
2124 char buf[128];
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002125 const char *s = NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002126 PyObject *type, *value, *tback, *errstr;
2127 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002128 errstr = PyObject_Str(value);
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002129 if (errstr)
2130 s = PyUnicode_AsUTF8(errstr);
2131 if (s) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002132 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002133 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002134 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002135 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002136 }
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002137 Py_XDECREF(errstr);
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002138 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002139 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002140 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002141 Py_XDECREF(tback);
2142 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002143 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002144 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002145 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146 }
2147 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002148 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002149 if (!pynum)
2150 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002151
Victor Stinner43d81952013-07-17 00:57:58 +02002152 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2153 Py_DECREF(pynum);
2154 return NULL;
2155 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002156 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002157 }
Georg Brandldde00282007-03-18 19:01:53 +00002158 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002159 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002160 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002161 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162
Thomas Wouters89f507f2006-12-13 04:49:30 +00002163 if (TYPE(ch) == RPAR)
2164 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165
Thomas Wouters89f507f2006-12-13 04:49:30 +00002166 if (TYPE(ch) == yield_expr)
2167 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002170 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002171 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002172
Nick Coghlan650f0d02007-04-15 12:05:43 +00002173 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002174 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002175 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176
Thomas Wouters89f507f2006-12-13 04:49:30 +00002177 if (TYPE(ch) == RSQB)
2178 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179
Nick Coghlan650f0d02007-04-15 12:05:43 +00002180 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002181 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2182 asdl_seq *elts = seq_for_testlist(c, ch);
2183 if (!elts)
2184 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002185
Thomas Wouters89f507f2006-12-13 04:49:30 +00002186 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2187 }
2188 else
2189 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002190 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002191 /* dictorsetmaker: ( ((test ':' test | '**' test)
2192 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2193 * ((test | '*' test)
2194 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002195 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002196 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002197 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002198 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002199 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002200 }
2201 else {
2202 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2203 if (NCH(ch) == 1 ||
2204 (NCH(ch) > 1 &&
2205 TYPE(CHILD(ch, 1)) == COMMA)) {
2206 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002207 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002208 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002209 else if (NCH(ch) > 1 &&
2210 TYPE(CHILD(ch, 1)) == comp_for) {
2211 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002212 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002213 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002214 else if (NCH(ch) > 3 - is_dict &&
2215 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2216 /* It's a dictionary comprehension. */
2217 if (is_dict) {
2218 ast_error(c, n, "dict unpacking cannot be used in "
2219 "dict comprehension");
2220 return NULL;
2221 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002222 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002223 }
2224 else {
2225 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002226 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002227 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002228 if (res) {
2229 res->lineno = LINENO(n);
2230 res->col_offset = n->n_col_offset;
2231 }
2232 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002233 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002234 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002235 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002236 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2237 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238 }
2239}
2240
2241static slice_ty
2242ast_for_slice(struct compiling *c, const node *n)
2243{
2244 node *ch;
2245 expr_ty lower = NULL, upper = NULL, step = NULL;
2246
2247 REQ(n, subscript);
2248
2249 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002250 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251 sliceop: ':' [test]
2252 */
2253 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002254 if (NCH(n) == 1 && TYPE(ch) == test) {
2255 /* 'step' variable hold no significance in terms of being used over
2256 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002258 if (!step)
2259 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260
Thomas Wouters89f507f2006-12-13 04:49:30 +00002261 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002262 }
2263
2264 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002265 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266 if (!lower)
2267 return NULL;
2268 }
2269
2270 /* If there's an upper bound it's in the second or third position. */
2271 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002272 if (NCH(n) > 1) {
2273 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002274
Thomas Wouters89f507f2006-12-13 04:49:30 +00002275 if (TYPE(n2) == test) {
2276 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002277 if (!upper)
2278 return NULL;
2279 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002280 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002281 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002282 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283
Thomas Wouters89f507f2006-12-13 04:49:30 +00002284 if (TYPE(n2) == test) {
2285 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286 if (!upper)
2287 return NULL;
2288 }
2289 }
2290
2291 ch = CHILD(n, NCH(n) - 1);
2292 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002293 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002294 ch = CHILD(ch, 1);
2295 if (TYPE(ch) == test) {
2296 step = ast_for_expr(c, ch);
2297 if (!step)
2298 return NULL;
2299 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300 }
2301 }
2302
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002303 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304}
2305
2306static expr_ty
2307ast_for_binop(struct compiling *c, const node *n)
2308{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002309 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002311 BinOp(BinOp(A, op, B), op, C).
2312 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002313
Guido van Rossumd8faa362007-04-27 19:54:29 +00002314 int i, nops;
2315 expr_ty expr1, expr2, result;
2316 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002317
Guido van Rossumd8faa362007-04-27 19:54:29 +00002318 expr1 = ast_for_expr(c, CHILD(n, 0));
2319 if (!expr1)
2320 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002321
Guido van Rossumd8faa362007-04-27 19:54:29 +00002322 expr2 = ast_for_expr(c, CHILD(n, 2));
2323 if (!expr2)
2324 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002325
Guido van Rossumd8faa362007-04-27 19:54:29 +00002326 newoperator = get_operator(CHILD(n, 1));
2327 if (!newoperator)
2328 return NULL;
2329
2330 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2331 c->c_arena);
2332 if (!result)
2333 return NULL;
2334
2335 nops = (NCH(n) - 1) / 2;
2336 for (i = 1; i < nops; i++) {
2337 expr_ty tmp_result, tmp;
2338 const node* next_oper = CHILD(n, i * 2 + 1);
2339
2340 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002341 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002342 return NULL;
2343
Guido van Rossumd8faa362007-04-27 19:54:29 +00002344 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2345 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002346 return NULL;
2347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002349 LINENO(next_oper), next_oper->n_col_offset,
2350 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002352 return NULL;
2353 result = tmp_result;
2354 }
2355 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002356}
2357
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002358static expr_ty
2359ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002362 subscriptlist: subscript (',' subscript)* [',']
2363 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2364 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002365 REQ(n, trailer);
2366 if (TYPE(CHILD(n, 0)) == LPAR) {
2367 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002368 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002369 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002370 else
2371 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002372 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002373 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002374 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2375 if (!attr_id)
2376 return NULL;
2377 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002378 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002379 }
2380 else {
2381 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002382 REQ(CHILD(n, 2), RSQB);
2383 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002384 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002385 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2386 if (!slc)
2387 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002388 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2389 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002390 }
2391 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002393 by treating the sequence as a tuple literal if there are
2394 no slice features.
2395 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002396 int j;
2397 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002398 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002399 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002400 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002401 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002402 if (!slices)
2403 return NULL;
2404 for (j = 0; j < NCH(n); j += 2) {
2405 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002406 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002407 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002408 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002409 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002410 asdl_seq_SET(slices, j / 2, slc);
2411 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002412 if (!simple) {
2413 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002414 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002415 }
2416 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002417 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002418 if (!elts)
2419 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002420 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2421 slc = (slice_ty)asdl_seq_GET(slices, j);
2422 assert(slc->kind == Index_kind && slc->v.Index.value);
2423 asdl_seq_SET(elts, j, slc->v.Index.value);
2424 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002425 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002426 if (!e)
2427 return NULL;
2428 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002429 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002430 }
2431 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002432}
2433
2434static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002435ast_for_factor(struct compiling *c, const node *n)
2436{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002437 expr_ty expression;
2438
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002439 expression = ast_for_expr(c, CHILD(n, 1));
2440 if (!expression)
2441 return NULL;
2442
2443 switch (TYPE(CHILD(n, 0))) {
2444 case PLUS:
2445 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2446 c->c_arena);
2447 case MINUS:
2448 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2449 c->c_arena);
2450 case TILDE:
2451 return UnaryOp(Invert, expression, LINENO(n),
2452 n->n_col_offset, c->c_arena);
2453 }
2454 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2455 TYPE(CHILD(n, 0)));
2456 return NULL;
2457}
2458
2459static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002460ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002461{
Yury Selivanov75445082015-05-11 22:57:16 -04002462 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002463 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002464
2465 REQ(n, atom_expr);
2466 nch = NCH(n);
2467
2468 if (TYPE(CHILD(n, 0)) == AWAIT) {
2469 start = 1;
2470 assert(nch > 1);
2471 }
2472
2473 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002474 if (!e)
2475 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002476 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002477 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002478 if (start && nch == 2) {
2479 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2480 }
2481
2482 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002483 node *ch = CHILD(n, i);
2484 if (TYPE(ch) != trailer)
2485 break;
2486 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002487 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002488 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002489 tmp->lineno = e->lineno;
2490 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002491 e = tmp;
2492 }
Yury Selivanov75445082015-05-11 22:57:16 -04002493
2494 if (start) {
2495 /* there was an AWAIT */
2496 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2497 }
2498 else {
2499 return e;
2500 }
2501}
2502
2503static expr_ty
2504ast_for_power(struct compiling *c, const node *n)
2505{
2506 /* power: atom trailer* ('**' factor)*
2507 */
2508 expr_ty e;
2509 REQ(n, power);
2510 e = ast_for_atom_expr(c, CHILD(n, 0));
2511 if (!e)
2512 return NULL;
2513 if (NCH(n) == 1)
2514 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002515 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2516 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002517 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002518 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002519 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002520 }
2521 return e;
2522}
2523
Guido van Rossum0368b722007-05-11 16:50:42 +00002524static expr_ty
2525ast_for_starred(struct compiling *c, const node *n)
2526{
2527 expr_ty tmp;
2528 REQ(n, star_expr);
2529
2530 tmp = ast_for_expr(c, CHILD(n, 1));
2531 if (!tmp)
2532 return NULL;
2533
2534 /* The Load context is changed later. */
2535 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2536}
2537
2538
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539/* Do not name a variable 'expr'! Will cause a compile error.
2540*/
2541
2542static expr_ty
2543ast_for_expr(struct compiling *c, const node *n)
2544{
2545 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002546 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002547 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002548 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 and_test: not_test ('and' not_test)*
2550 not_test: 'not' not_test | comparison
2551 comparison: expr (comp_op expr)*
2552 expr: xor_expr ('|' xor_expr)*
2553 xor_expr: and_expr ('^' and_expr)*
2554 and_expr: shift_expr ('&' shift_expr)*
2555 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2556 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002557 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002559 power: atom_expr ['**' factor]
2560 atom_expr: [AWAIT] atom trailer*
2561 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002562 */
2563
2564 asdl_seq *seq;
2565 int i;
2566
2567 loop:
2568 switch (TYPE(n)) {
2569 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002570 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002571 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002572 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002573 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002574 else if (NCH(n) > 1)
2575 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002576 /* Fallthrough */
2577 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578 case and_test:
2579 if (NCH(n) == 1) {
2580 n = CHILD(n, 0);
2581 goto loop;
2582 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002583 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002584 if (!seq)
2585 return NULL;
2586 for (i = 0; i < NCH(n); i += 2) {
2587 expr_ty e = ast_for_expr(c, CHILD(n, i));
2588 if (!e)
2589 return NULL;
2590 asdl_seq_SET(seq, i / 2, e);
2591 }
2592 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002593 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2594 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002595 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002596 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002597 case not_test:
2598 if (NCH(n) == 1) {
2599 n = CHILD(n, 0);
2600 goto loop;
2601 }
2602 else {
2603 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2604 if (!expression)
2605 return NULL;
2606
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002607 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2608 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002609 }
2610 case comparison:
2611 if (NCH(n) == 1) {
2612 n = CHILD(n, 0);
2613 goto loop;
2614 }
2615 else {
2616 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002617 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002618 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002619 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620 if (!ops)
2621 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002622 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002623 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002624 return NULL;
2625 }
2626 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002627 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002629 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002630 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002631 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002632 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002633
2634 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002635 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002636 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002637 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002639 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002640 asdl_seq_SET(cmps, i / 2, expression);
2641 }
2642 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002643 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002645 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002647 return Compare(expression, ops, cmps, LINENO(n),
2648 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649 }
2650 break;
2651
Guido van Rossum0368b722007-05-11 16:50:42 +00002652 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654 /* The next five cases all handle BinOps. The main body of code
2655 is the same in each case, but the switch turned inside out to
2656 reuse the code for each type of operator.
2657 */
2658 case expr:
2659 case xor_expr:
2660 case and_expr:
2661 case shift_expr:
2662 case arith_expr:
2663 case term:
2664 if (NCH(n) == 1) {
2665 n = CHILD(n, 0);
2666 goto loop;
2667 }
2668 return ast_for_binop(c, n);
2669 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002670 node *an = NULL;
2671 node *en = NULL;
2672 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002673 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002674 if (NCH(n) > 1)
2675 an = CHILD(n, 1); /* yield_arg */
2676 if (an) {
2677 en = CHILD(an, NCH(an) - 1);
2678 if (NCH(an) == 2) {
2679 is_from = 1;
2680 exp = ast_for_expr(c, en);
2681 }
2682 else
2683 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002684 if (!exp)
2685 return NULL;
2686 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002687 if (is_from)
2688 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2689 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002690 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002691 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692 if (NCH(n) == 1) {
2693 n = CHILD(n, 0);
2694 goto loop;
2695 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002696 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002697 case power:
2698 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002699 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002700 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701 return NULL;
2702 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002703 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002704 return NULL;
2705}
2706
2707static expr_ty
2708ast_for_call(struct compiling *c, const node *n, expr_ty func)
2709{
2710 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002711 arglist: argument (',' argument)* [',']
2712 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002713 */
2714
2715 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002716 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002717 asdl_seq *args;
2718 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002719
2720 REQ(n, arglist);
2721
2722 nargs = 0;
2723 nkeywords = 0;
2724 ngens = 0;
2725 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002726 node *ch = CHILD(n, i);
2727 if (TYPE(ch) == argument) {
2728 if (NCH(ch) == 1)
2729 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002730 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002731 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002732 else if (TYPE(CHILD(ch, 0)) == STAR)
2733 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002735 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002736 nkeywords++;
2737 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738 }
2739 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002740 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002741 "if not sole argument");
2742 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 }
2744
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002745 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002746 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002747 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002748 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002749 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002750 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002751
2752 nargs = 0; /* positional arguments + iterable argument unpackings */
2753 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2754 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002755 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002756 node *ch = CHILD(n, i);
2757 if (TYPE(ch) == argument) {
2758 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002759 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002760 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002761 /* a positional argument */
2762 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002763 if (ndoublestars) {
2764 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002765 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002766 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002767 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002768 else {
2769 ast_error(c, chch,
2770 "positional argument follows "
2771 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002772 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002773 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002774 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002775 e = ast_for_expr(c, chch);
2776 if (!e)
2777 return NULL;
2778 asdl_seq_SET(args, nargs++, e);
2779 }
2780 else if (TYPE(chch) == STAR) {
2781 /* an iterable argument unpacking */
2782 expr_ty starred;
2783 if (ndoublestars) {
2784 ast_error(c, chch,
2785 "iterable argument unpacking follows "
2786 "keyword argument unpacking");
2787 return NULL;
2788 }
2789 e = ast_for_expr(c, CHILD(ch, 1));
2790 if (!e)
2791 return NULL;
2792 starred = Starred(e, Load, LINENO(chch),
2793 chch->n_col_offset,
2794 c->c_arena);
2795 if (!starred)
2796 return NULL;
2797 asdl_seq_SET(args, nargs++, starred);
2798
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002799 }
2800 else if (TYPE(chch) == DOUBLESTAR) {
2801 /* a keyword argument unpacking */
2802 keyword_ty kw;
2803 i++;
2804 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002806 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002807 kw = keyword(NULL, e, c->c_arena);
2808 asdl_seq_SET(keywords, nkeywords++, kw);
2809 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002811 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002812 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002813 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002814 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002815 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002816 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002818 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002819 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002820 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002821 identifier key, tmp;
2822 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002824 /* chch is test, but must be an identifier? */
2825 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002827 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828 /* f(lambda x: x[0] = 3) ends up getting parsed with
2829 * LHS test = lambda x: x[0], and RHS test = 3.
2830 * SF bug 132313 points out that complaining about a keyword
2831 * then is very confusing.
2832 */
2833 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002834 ast_error(c, chch,
2835 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002836 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002837 }
2838 else if (e->kind != Name_kind) {
2839 ast_error(c, chch,
2840 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002841 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002842 }
2843 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002844 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002846 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002847 for (k = 0; k < nkeywords; k++) {
2848 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002849 if (tmp && !PyUnicode_Compare(tmp, key)) {
2850 ast_error(c, chch,
2851 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002852 return NULL;
2853 }
2854 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002855 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002857 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002858 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002859 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002860 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002861 asdl_seq_SET(keywords, nkeywords++, kw);
2862 }
2863 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002864 }
2865
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002866 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867}
2868
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002870ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002872 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002873 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002874 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002875 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002876 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002877 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002878 }
2879 else {
2880 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002881 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002882 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002883 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002884 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002885 else {
2886 asdl_seq *tmp = seq_for_testlist(c, n);
2887 if (!tmp)
2888 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002889 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002891}
2892
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002893static stmt_ty
2894ast_for_expr_stmt(struct compiling *c, const node *n)
2895{
2896 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002897 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
2898 ('=' (yield_expr|testlist_star_expr))*)
2899 annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +00002900 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002901 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002902 | '<<=' | '>>=' | '**=' | '//='
Martin Panter69332c12016-08-04 13:07:31 +00002903 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904 */
2905
2906 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002907 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002908 if (!e)
2909 return NULL;
2910
Thomas Wouters89f507f2006-12-13 04:49:30 +00002911 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002912 }
2913 else if (TYPE(CHILD(n, 1)) == augassign) {
2914 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002915 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002916 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002917
Thomas Wouters89f507f2006-12-13 04:49:30 +00002918 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002919 if (!expr1)
2920 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002921 if(!set_context(c, expr1, Store, ch))
2922 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002923 /* set_context checks that most expressions are not the left side.
2924 Augmented assignments can only have a name, a subscript, or an
2925 attribute on the left, though, so we have to explicitly check for
2926 those. */
2927 switch (expr1->kind) {
2928 case Name_kind:
2929 case Attribute_kind:
2930 case Subscript_kind:
2931 break;
2932 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002933 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002934 return NULL;
2935 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002936
Thomas Wouters89f507f2006-12-13 04:49:30 +00002937 ch = CHILD(n, 2);
2938 if (TYPE(ch) == testlist)
2939 expr2 = ast_for_testlist(c, ch);
2940 else
2941 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002942 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002943 return NULL;
2944
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002945 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002946 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947 return NULL;
2948
Thomas Wouters89f507f2006-12-13 04:49:30 +00002949 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002950 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002951 else if (TYPE(CHILD(n, 1)) == annassign) {
2952 expr_ty expr1, expr2, expr3;
2953 node *ch = CHILD(n, 0);
2954 node *deep, *ann = CHILD(n, 1);
2955 int simple = 1;
2956
2957 /* we keep track of parens to qualify (x) as expression not name */
2958 deep = ch;
2959 while (NCH(deep) == 1) {
2960 deep = CHILD(deep, 0);
2961 }
2962 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
2963 simple = 0;
2964 }
2965 expr1 = ast_for_testlist(c, ch);
2966 if (!expr1) {
2967 return NULL;
2968 }
2969 switch (expr1->kind) {
2970 case Name_kind:
2971 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
2972 return NULL;
2973 }
2974 expr1->v.Name.ctx = Store;
2975 break;
2976 case Attribute_kind:
2977 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
2978 return NULL;
2979 }
2980 expr1->v.Attribute.ctx = Store;
2981 break;
2982 case Subscript_kind:
2983 expr1->v.Subscript.ctx = Store;
2984 break;
2985 case List_kind:
2986 ast_error(c, ch,
2987 "only single target (not list) can be annotated");
2988 return NULL;
2989 case Tuple_kind:
2990 ast_error(c, ch,
2991 "only single target (not tuple) can be annotated");
2992 return NULL;
2993 default:
2994 ast_error(c, ch,
2995 "illegal target for annotation");
2996 return NULL;
2997 }
2998
2999 if (expr1->kind != Name_kind) {
3000 simple = 0;
3001 }
3002 ch = CHILD(ann, 1);
3003 expr2 = ast_for_expr(c, ch);
3004 if (!expr2) {
3005 return NULL;
3006 }
3007 if (NCH(ann) == 2) {
3008 return AnnAssign(expr1, expr2, NULL, simple,
3009 LINENO(n), n->n_col_offset, c->c_arena);
3010 }
3011 else {
3012 ch = CHILD(ann, 3);
3013 expr3 = ast_for_expr(c, ch);
3014 if (!expr3) {
3015 return NULL;
3016 }
3017 return AnnAssign(expr1, expr2, expr3, simple,
3018 LINENO(n), n->n_col_offset, c->c_arena);
3019 }
3020 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003021 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003022 int i;
3023 asdl_seq *targets;
3024 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003025 expr_ty expression;
3026
Thomas Wouters89f507f2006-12-13 04:49:30 +00003027 /* a normal assignment */
3028 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003029 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003030 if (!targets)
3031 return NULL;
3032 for (i = 0; i < NCH(n) - 2; i += 2) {
3033 expr_ty e;
3034 node *ch = CHILD(n, i);
3035 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003036 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003037 return NULL;
3038 }
3039 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003041 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003042
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003043 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003044 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003045 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003046
Thomas Wouters89f507f2006-12-13 04:49:30 +00003047 asdl_seq_SET(targets, i / 2, e);
3048 }
3049 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003050 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003051 expression = ast_for_testlist(c, value);
3052 else
3053 expression = ast_for_expr(c, value);
3054 if (!expression)
3055 return NULL;
3056 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003057 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003058}
3059
Benjamin Peterson78565b22009-06-28 19:19:51 +00003060
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003061static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003062ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003063{
3064 asdl_seq *seq;
3065 int i;
3066 expr_ty e;
3067
3068 REQ(n, exprlist);
3069
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003070 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003071 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003072 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003073 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003074 e = ast_for_expr(c, CHILD(n, i));
3075 if (!e)
3076 return NULL;
3077 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003078 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003079 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003080 }
3081 return seq;
3082}
3083
3084static stmt_ty
3085ast_for_del_stmt(struct compiling *c, const node *n)
3086{
3087 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003089 /* del_stmt: 'del' exprlist */
3090 REQ(n, del_stmt);
3091
3092 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3093 if (!expr_list)
3094 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003095 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003096}
3097
3098static stmt_ty
3099ast_for_flow_stmt(struct compiling *c, const node *n)
3100{
3101 /*
3102 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3103 | yield_stmt
3104 break_stmt: 'break'
3105 continue_stmt: 'continue'
3106 return_stmt: 'return' [testlist]
3107 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003108 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003109 raise_stmt: 'raise' [test [',' test [',' test]]]
3110 */
3111 node *ch;
3112
3113 REQ(n, flow_stmt);
3114 ch = CHILD(n, 0);
3115 switch (TYPE(ch)) {
3116 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003117 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003118 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003119 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003120 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003121 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3122 if (!exp)
3123 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003124 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003125 }
3126 case return_stmt:
3127 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003128 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003129 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003130 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003131 if (!expression)
3132 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003133 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003134 }
3135 case raise_stmt:
3136 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003137 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3138 else if (NCH(ch) >= 2) {
3139 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003140 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3141 if (!expression)
3142 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003143 if (NCH(ch) == 4) {
3144 cause = ast_for_expr(c, CHILD(ch, 3));
3145 if (!cause)
3146 return NULL;
3147 }
3148 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003149 }
3150 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003151 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 "unexpected flow_stmt: %d", TYPE(ch));
3153 return NULL;
3154 }
3155}
3156
3157static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003158alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159{
3160 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003161 import_as_name: NAME ['as' NAME]
3162 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003163 dotted_name: NAME ('.' NAME)*
3164 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003165 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003166
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003167 loop:
3168 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003169 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003170 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003171 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003172 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003173 if (!name)
3174 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003175 if (NCH(n) == 3) {
3176 node *str_node = CHILD(n, 2);
3177 str = NEW_IDENTIFIER(str_node);
3178 if (!str)
3179 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003180 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003181 return NULL;
3182 }
3183 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003184 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003185 return NULL;
3186 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003187 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003188 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003189 case dotted_as_name:
3190 if (NCH(n) == 1) {
3191 n = CHILD(n, 0);
3192 goto loop;
3193 }
3194 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003195 node *asname_node = CHILD(n, 2);
3196 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003197 if (!a)
3198 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003199 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003200 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003201 if (!a->asname)
3202 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003203 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003204 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205 return a;
3206 }
3207 break;
3208 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003209 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003210 node *name_node = CHILD(n, 0);
3211 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003212 if (!name)
3213 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003214 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003215 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003216 return alias(name, NULL, c->c_arena);
3217 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003218 else {
3219 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003220 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003221 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003222 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003224
3225 len = 0;
3226 for (i = 0; i < NCH(n); i += 2)
3227 /* length of string plus one for the dot */
3228 len += strlen(STR(CHILD(n, i))) + 1;
3229 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003230 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003231 if (!str)
3232 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003233 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003234 if (!s)
3235 return NULL;
3236 for (i = 0; i < NCH(n); i += 2) {
3237 char *sch = STR(CHILD(n, i));
3238 strcpy(s, STR(CHILD(n, i)));
3239 s += strlen(sch);
3240 *s++ = '.';
3241 }
3242 --s;
3243 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3245 PyBytes_GET_SIZE(str),
3246 NULL);
3247 Py_DECREF(str);
3248 if (!uni)
3249 return NULL;
3250 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003251 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003252 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3253 Py_DECREF(str);
3254 return NULL;
3255 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003256 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003257 }
3258 break;
3259 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003260 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003261 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3262 Py_DECREF(str);
3263 return NULL;
3264 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003265 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003266 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003267 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003268 "unexpected import name: %d", TYPE(n));
3269 return NULL;
3270 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003271
3272 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003273 return NULL;
3274}
3275
3276static stmt_ty
3277ast_for_import_stmt(struct compiling *c, const node *n)
3278{
3279 /*
3280 import_stmt: import_name | import_from
3281 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003282 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3283 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003284 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003285 int lineno;
3286 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003287 int i;
3288 asdl_seq *aliases;
3289
3290 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003291 lineno = LINENO(n);
3292 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003293 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003294 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003295 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003296 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003297 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003298 if (!aliases)
3299 return NULL;
3300 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003301 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003302 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003303 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003304 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003305 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003306 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003307 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003308 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003310 int idx, ndots = 0;
3311 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003312 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003313
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003314 /* Count the number of dots (for relative imports) and check for the
3315 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003316 for (idx = 1; idx < NCH(n); idx++) {
3317 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003318 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3319 if (!mod)
3320 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003321 idx++;
3322 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003323 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003325 ndots += 3;
3326 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003327 } else if (TYPE(CHILD(n, idx)) != DOT) {
3328 break;
3329 }
3330 ndots++;
3331 }
3332 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003333 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003334 case STAR:
3335 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003336 n = CHILD(n, idx);
3337 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003338 break;
3339 case LPAR:
3340 /* from ... import (x, y, z) */
3341 n = CHILD(n, idx + 1);
3342 n_children = NCH(n);
3343 break;
3344 case import_as_names:
3345 /* from ... import x, y, z */
3346 n = CHILD(n, idx);
3347 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003348 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003349 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003350 " surrounding parentheses");
3351 return NULL;
3352 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003353 break;
3354 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003355 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003356 return NULL;
3357 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003358
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003359 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003360 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003361 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362
3363 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003364 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003365 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003366 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003367 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003368 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003369 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003370 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003371 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003372 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003373 if (!import_alias)
3374 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003375 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003376 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003377 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003378 if (mod != NULL)
3379 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003380 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003381 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003382 }
Neal Norwitz79792652005-11-14 04:25:03 +00003383 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003384 "unknown import statement: starts with command '%s'",
3385 STR(CHILD(n, 0)));
3386 return NULL;
3387}
3388
3389static stmt_ty
3390ast_for_global_stmt(struct compiling *c, const node *n)
3391{
3392 /* global_stmt: 'global' NAME (',' NAME)* */
3393 identifier name;
3394 asdl_seq *s;
3395 int i;
3396
3397 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003398 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003399 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003400 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003401 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003402 name = NEW_IDENTIFIER(CHILD(n, i));
3403 if (!name)
3404 return NULL;
3405 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003406 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003407 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003408}
3409
3410static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003411ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3412{
3413 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3414 identifier name;
3415 asdl_seq *s;
3416 int i;
3417
3418 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003419 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003420 if (!s)
3421 return NULL;
3422 for (i = 1; i < NCH(n); i += 2) {
3423 name = NEW_IDENTIFIER(CHILD(n, i));
3424 if (!name)
3425 return NULL;
3426 asdl_seq_SET(s, i / 2, name);
3427 }
3428 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3429}
3430
3431static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003432ast_for_assert_stmt(struct compiling *c, const node *n)
3433{
3434 /* assert_stmt: 'assert' test [',' test] */
3435 REQ(n, assert_stmt);
3436 if (NCH(n) == 2) {
3437 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3438 if (!expression)
3439 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003440 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003441 }
3442 else if (NCH(n) == 4) {
3443 expr_ty expr1, expr2;
3444
3445 expr1 = ast_for_expr(c, CHILD(n, 1));
3446 if (!expr1)
3447 return NULL;
3448 expr2 = ast_for_expr(c, CHILD(n, 3));
3449 if (!expr2)
3450 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003451
Thomas Wouters89f507f2006-12-13 04:49:30 +00003452 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003453 }
Neal Norwitz79792652005-11-14 04:25:03 +00003454 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003455 "improper number of parts to 'assert' statement: %d",
3456 NCH(n));
3457 return NULL;
3458}
3459
3460static asdl_seq *
3461ast_for_suite(struct compiling *c, const node *n)
3462{
3463 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003464 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003465 stmt_ty s;
3466 int i, total, num, end, pos = 0;
3467 node *ch;
3468
3469 REQ(n, suite);
3470
3471 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003472 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003473 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003474 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003475 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003476 n = CHILD(n, 0);
3477 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003479 */
3480 end = NCH(n) - 1;
3481 if (TYPE(CHILD(n, end - 1)) == SEMI)
3482 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003483 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003484 for (i = 0; i < end; i += 2) {
3485 ch = CHILD(n, i);
3486 s = ast_for_stmt(c, ch);
3487 if (!s)
3488 return NULL;
3489 asdl_seq_SET(seq, pos++, s);
3490 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003491 }
3492 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003493 for (i = 2; i < (NCH(n) - 1); i++) {
3494 ch = CHILD(n, i);
3495 REQ(ch, stmt);
3496 num = num_stmts(ch);
3497 if (num == 1) {
3498 /* small_stmt or compound_stmt with only one child */
3499 s = ast_for_stmt(c, ch);
3500 if (!s)
3501 return NULL;
3502 asdl_seq_SET(seq, pos++, s);
3503 }
3504 else {
3505 int j;
3506 ch = CHILD(ch, 0);
3507 REQ(ch, simple_stmt);
3508 for (j = 0; j < NCH(ch); j += 2) {
3509 /* statement terminates with a semi-colon ';' */
3510 if (NCH(CHILD(ch, j)) == 0) {
3511 assert((j + 1) == NCH(ch));
3512 break;
3513 }
3514 s = ast_for_stmt(c, CHILD(ch, j));
3515 if (!s)
3516 return NULL;
3517 asdl_seq_SET(seq, pos++, s);
3518 }
3519 }
3520 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003521 }
3522 assert(pos == seq->size);
3523 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003524}
3525
INADA Naokicb41b272017-02-23 00:31:59 +09003526static string
3527docstring_from_stmts(asdl_seq *stmts)
3528{
3529 if (stmts && stmts->size) {
3530 stmt_ty s = (stmt_ty)asdl_seq_GET(stmts, 0);
3531 /* If first statement is a literal string, it's the doc string. */
3532 if (s->kind == Expr_kind && s->v.Expr.value->kind == Str_kind) {
3533 string doc = s->v.Expr.value->v.Str.s;
3534 /* not very efficient, but simple */
3535 memmove(&asdl_seq_GET(stmts, 0), &asdl_seq_GET(stmts, 1),
3536 (stmts->size - 1) * sizeof(void*));
3537 stmts->size--;
3538 return doc;
3539 }
3540 }
3541 return NULL;
3542}
3543
3544static asdl_seq *
3545ast_for_body(struct compiling *c, const node *n, string *docstring)
3546{
3547 asdl_seq *stmts = ast_for_suite(c, n);
3548 *docstring = docstring_from_stmts(stmts);
3549 return stmts;
3550}
3551
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003552static stmt_ty
3553ast_for_if_stmt(struct compiling *c, const node *n)
3554{
3555 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3556 ['else' ':' suite]
3557 */
3558 char *s;
3559
3560 REQ(n, if_stmt);
3561
3562 if (NCH(n) == 4) {
3563 expr_ty expression;
3564 asdl_seq *suite_seq;
3565
3566 expression = ast_for_expr(c, CHILD(n, 1));
3567 if (!expression)
3568 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003569 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003570 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003571 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003572
Guido van Rossumd8faa362007-04-27 19:54:29 +00003573 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3574 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003575 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003576
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003577 s = STR(CHILD(n, 4));
3578 /* s[2], the third character in the string, will be
3579 's' for el_s_e, or
3580 'i' for el_i_f
3581 */
3582 if (s[2] == 's') {
3583 expr_ty expression;
3584 asdl_seq *seq1, *seq2;
3585
3586 expression = ast_for_expr(c, CHILD(n, 1));
3587 if (!expression)
3588 return NULL;
3589 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003590 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003591 return NULL;
3592 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003593 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003594 return NULL;
3595
Guido van Rossumd8faa362007-04-27 19:54:29 +00003596 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3597 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598 }
3599 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003600 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003601 expr_ty expression;
3602 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003603 asdl_seq *orelse = NULL;
3604 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003605 /* must reference the child n_elif+1 since 'else' token is third,
3606 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003607 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3608 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3609 has_else = 1;
3610 n_elif -= 3;
3611 }
3612 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003613
Thomas Wouters89f507f2006-12-13 04:49:30 +00003614 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003615 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003616
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003617 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003618 if (!orelse)
3619 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003621 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003622 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003623 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3624 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003625 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003626 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3627 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003628 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003630 asdl_seq_SET(orelse, 0,
3631 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003632 LINENO(CHILD(n, NCH(n) - 6)),
3633 CHILD(n, NCH(n) - 6)->n_col_offset,
3634 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003635 /* the just-created orelse handled the last elif */
3636 n_elif--;
3637 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003638
Thomas Wouters89f507f2006-12-13 04:49:30 +00003639 for (i = 0; i < n_elif; i++) {
3640 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003641 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003642 if (!newobj)
3643 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003644 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003645 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003646 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003647 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003648 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003649 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003650
Thomas Wouters89f507f2006-12-13 04:49:30 +00003651 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003652 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003653 LINENO(CHILD(n, off)),
3654 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003655 orelse = newobj;
3656 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003657 expression = ast_for_expr(c, CHILD(n, 1));
3658 if (!expression)
3659 return NULL;
3660 suite_seq = ast_for_suite(c, CHILD(n, 3));
3661 if (!suite_seq)
3662 return NULL;
3663 return If(expression, suite_seq, orelse,
3664 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003665 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003666
3667 PyErr_Format(PyExc_SystemError,
3668 "unexpected token in 'if' statement: %s", s);
3669 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670}
3671
3672static stmt_ty
3673ast_for_while_stmt(struct compiling *c, const node *n)
3674{
3675 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3676 REQ(n, while_stmt);
3677
3678 if (NCH(n) == 4) {
3679 expr_ty expression;
3680 asdl_seq *suite_seq;
3681
3682 expression = ast_for_expr(c, CHILD(n, 1));
3683 if (!expression)
3684 return NULL;
3685 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003686 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003687 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003688 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003689 }
3690 else if (NCH(n) == 7) {
3691 expr_ty expression;
3692 asdl_seq *seq1, *seq2;
3693
3694 expression = ast_for_expr(c, CHILD(n, 1));
3695 if (!expression)
3696 return NULL;
3697 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003698 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003699 return NULL;
3700 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003701 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003702 return NULL;
3703
Thomas Wouters89f507f2006-12-13 04:49:30 +00003704 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003705 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003706
3707 PyErr_Format(PyExc_SystemError,
3708 "wrong number of tokens for 'while' statement: %d",
3709 NCH(n));
3710 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711}
3712
3713static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003714ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003715{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003716 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003717 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003718 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003719 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003720 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3721 REQ(n, for_stmt);
3722
3723 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003724 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003725 if (!seq)
3726 return NULL;
3727 }
3728
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003729 node_target = CHILD(n, 1);
3730 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003731 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003732 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003733 /* Check the # of children rather than the length of _target, since
3734 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003735 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003736 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003737 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003738 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003739 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003740
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003741 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003742 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003743 return NULL;
3744 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003745 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003746 return NULL;
3747
Yury Selivanov75445082015-05-11 22:57:16 -04003748 if (is_async)
3749 return AsyncFor(target, expression, suite_seq, seq,
3750 LINENO(n), n->n_col_offset,
3751 c->c_arena);
3752 else
3753 return For(target, expression, suite_seq, seq,
3754 LINENO(n), n->n_col_offset,
3755 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003756}
3757
3758static excepthandler_ty
3759ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3760{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003761 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003762 REQ(exc, except_clause);
3763 REQ(body, suite);
3764
3765 if (NCH(exc) == 1) {
3766 asdl_seq *suite_seq = ast_for_suite(c, body);
3767 if (!suite_seq)
3768 return NULL;
3769
Neal Norwitzad74aa82008-03-31 05:14:30 +00003770 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003771 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003772 }
3773 else if (NCH(exc) == 2) {
3774 expr_ty expression;
3775 asdl_seq *suite_seq;
3776
3777 expression = ast_for_expr(c, CHILD(exc, 1));
3778 if (!expression)
3779 return NULL;
3780 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003781 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003782 return NULL;
3783
Neal Norwitzad74aa82008-03-31 05:14:30 +00003784 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003785 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003786 }
3787 else if (NCH(exc) == 4) {
3788 asdl_seq *suite_seq;
3789 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003790 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003791 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003792 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003793 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003794 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003795 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003796 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003797 return NULL;
3798 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003799 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003800 return NULL;
3801
Neal Norwitzad74aa82008-03-31 05:14:30 +00003802 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003803 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003804 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003805
3806 PyErr_Format(PyExc_SystemError,
3807 "wrong number of children for 'except' clause: %d",
3808 NCH(exc));
3809 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003810}
3811
3812static stmt_ty
3813ast_for_try_stmt(struct compiling *c, const node *n)
3814{
Neal Norwitzf599f422005-12-17 21:33:47 +00003815 const int nch = NCH(n);
3816 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003817 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003818
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003819 REQ(n, try_stmt);
3820
Neal Norwitzf599f422005-12-17 21:33:47 +00003821 body = ast_for_suite(c, CHILD(n, 2));
3822 if (body == NULL)
3823 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003824
Neal Norwitzf599f422005-12-17 21:33:47 +00003825 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3826 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3827 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3828 /* we can assume it's an "else",
3829 because nch >= 9 for try-else-finally and
3830 it would otherwise have a type of except_clause */
3831 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3832 if (orelse == NULL)
3833 return NULL;
3834 n_except--;
3835 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003836
Neal Norwitzf599f422005-12-17 21:33:47 +00003837 finally = ast_for_suite(c, CHILD(n, nch - 1));
3838 if (finally == NULL)
3839 return NULL;
3840 n_except--;
3841 }
3842 else {
3843 /* we can assume it's an "else",
3844 otherwise it would have a type of except_clause */
3845 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3846 if (orelse == NULL)
3847 return NULL;
3848 n_except--;
3849 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003850 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003851 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003852 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003853 return NULL;
3854 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003855
Neal Norwitzf599f422005-12-17 21:33:47 +00003856 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003857 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003858 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003859 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003860 if (handlers == NULL)
3861 return NULL;
3862
3863 for (i = 0; i < n_except; i++) {
3864 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3865 CHILD(n, 5 + i * 3));
3866 if (!e)
3867 return NULL;
3868 asdl_seq_SET(handlers, i, e);
3869 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003870 }
3871
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003872 assert(finally != NULL || asdl_seq_LEN(handlers));
3873 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003874}
3875
Georg Brandl0c315622009-05-25 21:10:36 +00003876/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003877static withitem_ty
3878ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003879{
3880 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003881
Georg Brandl0c315622009-05-25 21:10:36 +00003882 REQ(n, with_item);
3883 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003884 if (!context_expr)
3885 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003886 if (NCH(n) == 3) {
3887 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003888
3889 if (!optional_vars) {
3890 return NULL;
3891 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003892 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003893 return NULL;
3894 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003895 }
3896
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003897 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003898}
3899
Georg Brandl0c315622009-05-25 21:10:36 +00003900/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3901static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003902ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003903{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003904 int i, n_items;
3905 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003906
3907 REQ(n, with_stmt);
3908
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003909 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003910 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003911 if (!items)
3912 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003913 for (i = 1; i < NCH(n) - 2; i += 2) {
3914 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3915 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003916 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003917 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003918 }
3919
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003920 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3921 if (!body)
3922 return NULL;
3923
Yury Selivanov75445082015-05-11 22:57:16 -04003924 if (is_async)
3925 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3926 else
3927 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003928}
3929
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003930static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003931ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003932{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003933 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003934 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003935 asdl_seq *s;
INADA Naokicb41b272017-02-23 00:31:59 +09003936 string docstring;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003937 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003938
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003939 REQ(n, classdef);
3940
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003941 if (NCH(n) == 4) { /* class NAME ':' suite */
INADA Naokicb41b272017-02-23 00:31:59 +09003942 s = ast_for_body(c, CHILD(n, 3), &docstring);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003943 if (!s)
3944 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003945 classname = NEW_IDENTIFIER(CHILD(n, 1));
3946 if (!classname)
3947 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003948 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003949 return NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09003950 return ClassDef(classname, NULL, NULL, s, decorator_seq, docstring,
3951 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003952 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003953
3954 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
INADA Naokicb41b272017-02-23 00:31:59 +09003955 s = ast_for_body(c, CHILD(n, 5), &docstring);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003956 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003957 return NULL;
3958 classname = NEW_IDENTIFIER(CHILD(n, 1));
3959 if (!classname)
3960 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003961 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003962 return NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09003963 return ClassDef(classname, NULL, NULL, s, decorator_seq, docstring,
3964 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003965 }
3966
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003967 /* class NAME '(' arglist ')' ':' suite */
3968 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003969 {
3970 PyObject *dummy_name;
3971 expr_ty dummy;
3972 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3973 if (!dummy_name)
3974 return NULL;
3975 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3976 call = ast_for_call(c, CHILD(n, 3), dummy);
3977 if (!call)
3978 return NULL;
3979 }
INADA Naokicb41b272017-02-23 00:31:59 +09003980 s = ast_for_body(c, CHILD(n, 6), &docstring);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003981 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003982 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003983 classname = NEW_IDENTIFIER(CHILD(n, 1));
3984 if (!classname)
3985 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003986 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003987 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003988
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003989 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
INADA Naokicb41b272017-02-23 00:31:59 +09003990 decorator_seq, docstring, LINENO(n), n->n_col_offset,
3991 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003992}
3993
3994static stmt_ty
3995ast_for_stmt(struct compiling *c, const node *n)
3996{
3997 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003998 assert(NCH(n) == 1);
3999 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004000 }
4001 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004002 assert(num_stmts(n) == 1);
4003 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004004 }
4005 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004006 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004007 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4008 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004009 */
4010 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004011 case expr_stmt:
4012 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004013 case del_stmt:
4014 return ast_for_del_stmt(c, n);
4015 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00004016 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004017 case flow_stmt:
4018 return ast_for_flow_stmt(c, n);
4019 case import_stmt:
4020 return ast_for_import_stmt(c, n);
4021 case global_stmt:
4022 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004023 case nonlocal_stmt:
4024 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004025 case assert_stmt:
4026 return ast_for_assert_stmt(c, n);
4027 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004028 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004029 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4030 TYPE(n), NCH(n));
4031 return NULL;
4032 }
4033 }
4034 else {
4035 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004036 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004037 */
4038 node *ch = CHILD(n, 0);
4039 REQ(n, compound_stmt);
4040 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004041 case if_stmt:
4042 return ast_for_if_stmt(c, ch);
4043 case while_stmt:
4044 return ast_for_while_stmt(c, ch);
4045 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004046 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004047 case try_stmt:
4048 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004049 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004050 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004051 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004052 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004053 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004054 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004055 case decorated:
4056 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004057 case async_stmt:
4058 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004059 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004060 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004061 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4062 TYPE(n), NCH(n));
4063 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004064 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004065 }
4066}
4067
4068static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004069parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004070{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004071 const char *end;
4072 long x;
4073 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004074 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004075 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004076
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004077 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004078 errno = 0;
4079 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004080 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004081 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004082 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004083 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004084 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004085 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004086 }
4087 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004088 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004089 if (*end == '\0') {
4090 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004091 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004092 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004093 }
4094 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004095 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004096 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004097 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4098 if (compl.imag == -1.0 && PyErr_Occurred())
4099 return NULL;
4100 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004101 }
4102 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004103 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004104 dx = PyOS_string_to_double(s, NULL, NULL);
4105 if (dx == -1.0 && PyErr_Occurred())
4106 return NULL;
4107 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004108 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004109}
4110
4111static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004112parsenumber(struct compiling *c, const char *s)
4113{
4114 char *dup, *end;
4115 PyObject *res = NULL;
4116
4117 assert(s != NULL);
4118
4119 if (strchr(s, '_') == NULL) {
4120 return parsenumber_raw(c, s);
4121 }
4122 /* Create a duplicate without underscores. */
4123 dup = PyMem_Malloc(strlen(s) + 1);
4124 end = dup;
4125 for (; *s; s++) {
4126 if (*s != '_') {
4127 *end++ = *s;
4128 }
4129 }
4130 *end = '\0';
4131 res = parsenumber_raw(c, dup);
4132 PyMem_Free(dup);
4133 return res;
4134}
4135
4136static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004137decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004138{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004139 const char *s, *t;
4140 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004141 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4142 while (s < end && (*s & 0x80)) s++;
4143 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004144 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004145}
4146
Eric V. Smith56466482016-10-31 14:46:26 -04004147static int
4148warn_invalid_escape_sequence(struct compiling *c, const node *n,
4149 char first_invalid_escape_char)
4150{
4151 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4152 first_invalid_escape_char);
4153 if (msg == NULL) {
4154 return -1;
4155 }
4156 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
4157 c->c_filename, LINENO(n),
4158 NULL, NULL) < 0 &&
4159 PyErr_ExceptionMatches(PyExc_DeprecationWarning))
4160 {
Victor Stinnerf9cca362016-11-15 09:12:10 +01004161 const char *s;
4162
4163 /* Replace the DeprecationWarning exception with a SyntaxError
4164 to get a more accurate error report */
4165 PyErr_Clear();
4166
4167 s = PyUnicode_AsUTF8(msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004168 if (s != NULL) {
4169 ast_error(c, n, s);
4170 }
4171 Py_DECREF(msg);
4172 return -1;
4173 }
4174 Py_DECREF(msg);
4175 return 0;
4176}
4177
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004178static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004179decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4180 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004181{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004182 PyObject *v, *u;
4183 char *buf;
4184 char *p;
4185 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004186
Benjamin Peterson202803a2016-02-25 22:34:45 -08004187 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004188 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004189 return NULL;
4190 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4191 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4192 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4193 if (u == NULL)
4194 return NULL;
4195 p = buf = PyBytes_AsString(u);
4196 end = s + len;
4197 while (s < end) {
4198 if (*s == '\\') {
4199 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004200 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004201 strcpy(p, "u005c");
4202 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004203 if (s >= end)
4204 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004205 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004206 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004207 if (*s & 0x80) { /* XXX inefficient */
4208 PyObject *w;
4209 int kind;
4210 void *data;
4211 Py_ssize_t len, i;
4212 w = decode_utf8(c, &s, end);
4213 if (w == NULL) {
4214 Py_DECREF(u);
4215 return NULL;
4216 }
4217 kind = PyUnicode_KIND(w);
4218 data = PyUnicode_DATA(w);
4219 len = PyUnicode_GET_LENGTH(w);
4220 for (i = 0; i < len; i++) {
4221 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4222 sprintf(p, "\\U%08x", chr);
4223 p += 10;
4224 }
4225 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004226 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004227 Py_DECREF(w);
4228 } else {
4229 *p++ = *s++;
4230 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004231 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004232 len = p - buf;
4233 s = buf;
4234
Eric V. Smith56466482016-10-31 14:46:26 -04004235 const char *first_invalid_escape;
4236 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4237
4238 if (v != NULL && first_invalid_escape != NULL) {
4239 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4240 /* We have not decref u before because first_invalid_escape points
4241 inside u. */
4242 Py_XDECREF(u);
4243 Py_DECREF(v);
4244 return NULL;
4245 }
4246 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004247 Py_XDECREF(u);
4248 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004249}
4250
Eric V. Smith56466482016-10-31 14:46:26 -04004251static PyObject *
4252decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4253 size_t len)
4254{
4255 const char *first_invalid_escape;
4256 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4257 &first_invalid_escape);
4258 if (result == NULL)
4259 return NULL;
4260
4261 if (first_invalid_escape != NULL) {
4262 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4263 Py_DECREF(result);
4264 return NULL;
4265 }
4266 }
4267 return result;
4268}
4269
Eric V. Smith451d0e32016-09-09 21:56:20 -04004270/* Compile this expression in to an expr_ty. Add parens around the
4271 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004272static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004273fstring_compile_expr(const char *expr_start, const char *expr_end,
4274 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004275
Eric V. Smith235a6f02015-09-19 14:51:32 -04004276{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004277 int all_whitespace = 1;
4278 int kind;
4279 void *data;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004280 PyCompilerFlags cf;
4281 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004282 char *str;
4283 PyObject *o;
4284 Py_ssize_t len;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004285 Py_ssize_t i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004286
Eric V. Smith1d44c412015-09-23 07:49:00 -04004287 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004288 assert(*(expr_start-1) == '{');
4289 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004290
Eric V. Smith451d0e32016-09-09 21:56:20 -04004291 /* We know there are no escapes here, because backslashes are not allowed,
4292 and we know it's utf-8 encoded (per PEP 263). But, in order to check
4293 that each char is not whitespace, we need to decode it to unicode.
4294 Which is unfortunate, but such is life. */
Eric V. Smith1d44c412015-09-23 07:49:00 -04004295
Eric V. Smith451d0e32016-09-09 21:56:20 -04004296 /* If the substring is all whitespace, it's an error. We need to catch
4297 this here, and not when we call PyParser_ASTFromString, because turning
4298 the expression '' in to '()' would go from being invalid to valid. */
4299 /* Note that this code says an empty string is all whitespace. That's
4300 important. There's a test for it: f'{}'. */
4301 o = PyUnicode_DecodeUTF8(expr_start, expr_end-expr_start, NULL);
4302 if (o == NULL)
4303 return NULL;
4304 len = PyUnicode_GET_LENGTH(o);
4305 kind = PyUnicode_KIND(o);
4306 data = PyUnicode_DATA(o);
4307 for (i = 0; i < len; i++) {
4308 if (!Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004309 all_whitespace = 0;
4310 break;
4311 }
4312 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004313 Py_DECREF(o);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004314 if (all_whitespace) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004315 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004316 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004317 }
4318
Eric V. Smith451d0e32016-09-09 21:56:20 -04004319 /* Reuse len to be the length of the utf-8 input string. */
4320 len = expr_end - expr_start;
4321 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4322 str = PyMem_RawMalloc(len + 3);
4323 if (str == NULL)
4324 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004325
Eric V. Smith451d0e32016-09-09 21:56:20 -04004326 str[0] = '(';
4327 memcpy(str+1, expr_start, len);
4328 str[len+1] = ')';
4329 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004330
4331 cf.cf_flags = PyCF_ONLY_AST;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004332 mod = PyParser_ASTFromString(str, "<fstring>",
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004333 Py_eval_input, &cf, c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004334 PyMem_RawFree(str);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004335 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004336 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004337 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004338}
4339
4340/* Return -1 on error.
4341
4342 Return 0 if we reached the end of the literal.
4343
4344 Return 1 if we haven't reached the end of the literal, but we want
4345 the caller to process the literal up to this point. Used for
4346 doubled braces.
4347*/
4348static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004349fstring_find_literal(const char **str, const char *end, int raw,
4350 PyObject **literal, int recurse_lvl,
4351 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004352{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004353 /* Get any literal string. It ends when we hit an un-doubled left
4354 brace (which isn't part of a unicode name escape such as
4355 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004356
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004357 const char *s = *str;
4358 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004359 int result = 0;
4360
Eric V. Smith235a6f02015-09-19 14:51:32 -04004361 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004362 while (s < end) {
4363 char ch = *s++;
4364 if (!raw && ch == '\\' && s < end) {
4365 ch = *s++;
4366 if (ch == 'N') {
4367 if (s < end && *s++ == '{') {
4368 while (s < end && *s++ != '}') {
4369 }
4370 continue;
4371 }
4372 break;
4373 }
4374 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4375 return -1;
4376 }
4377 }
4378 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004379 /* Check for doubled braces, but only at the top level. If
4380 we checked at every level, then f'{0:{3}}' would fail
4381 with the two closing braces. */
4382 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004383 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004384 /* We're going to tell the caller that the literal ends
4385 here, but that they should continue scanning. But also
4386 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004387 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004388 result = 1;
4389 goto done;
4390 }
4391
4392 /* Where a single '{' is the start of a new expression, a
4393 single '}' is not allowed. */
4394 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004395 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004396 ast_error(c, n, "f-string: single '}' is not allowed");
4397 return -1;
4398 }
4399 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004400 /* We're either at a '{', which means we're starting another
4401 expression; or a '}', which means we're at the end of this
4402 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004403 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004404 break;
4405 }
4406 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004407 *str = s;
4408 assert(s <= end);
4409 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004410done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004411 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004412 if (raw)
4413 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004414 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004415 NULL, NULL);
4416 else
Eric V. Smith56466482016-10-31 14:46:26 -04004417 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004418 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004419 if (!*literal)
4420 return -1;
4421 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004422 return result;
4423}
4424
4425/* Forward declaration because parsing is recursive. */
4426static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004427fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004428 struct compiling *c, const node *n);
4429
Eric V. Smith451d0e32016-09-09 21:56:20 -04004430/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004431 expression (so it must be a '{'). Returns the FormattedValue node,
4432 which includes the expression, conversion character, and
4433 format_spec expression.
4434
4435 Note that I don't do a perfect job here: I don't make sure that a
4436 closing brace doesn't match an opening paren, for example. It
4437 doesn't need to error on all invalid expressions, just correctly
4438 find the end of all valid ones. Any errors inside the expression
4439 will be caught when we parse it later. */
4440static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004441fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004442 expr_ty *expression, struct compiling *c, const node *n)
4443{
4444 /* Return -1 on error, else 0. */
4445
Eric V. Smith451d0e32016-09-09 21:56:20 -04004446 const char *expr_start;
4447 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004448 expr_ty simple_expression;
4449 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004450 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004451
4452 /* 0 if we're not in a string, else the quote char we're trying to
4453 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004454 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004455
4456 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4457 int string_type = 0;
4458
4459 /* Keep track of nesting level for braces/parens/brackets in
4460 expressions. */
4461 Py_ssize_t nested_depth = 0;
4462
4463 /* Can only nest one level deep. */
4464 if (recurse_lvl >= 2) {
4465 ast_error(c, n, "f-string: expressions nested too deeply");
4466 return -1;
4467 }
4468
4469 /* The first char must be a left brace, or we wouldn't have gotten
4470 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004471 assert(**str == '{');
4472 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004473
Eric V. Smith451d0e32016-09-09 21:56:20 -04004474 expr_start = *str;
4475 for (; *str < end; (*str)++) {
4476 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004477
4478 /* Loop invariants. */
4479 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004480 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004481 if (quote_char)
4482 assert(string_type == 1 || string_type == 3);
4483 else
4484 assert(string_type == 0);
4485
Eric V. Smith451d0e32016-09-09 21:56:20 -04004486 ch = **str;
4487 /* Nowhere inside an expression is a backslash allowed. */
4488 if (ch == '\\') {
4489 /* Error: can't include a backslash character, inside
4490 parens or strings or not. */
4491 ast_error(c, n, "f-string expression part "
4492 "cannot include a backslash");
4493 return -1;
4494 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004495 if (quote_char) {
4496 /* We're inside a string. See if we're at the end. */
4497 /* This code needs to implement the same non-error logic
4498 as tok_get from tokenizer.c, at the letter_quote
4499 label. To actually share that code would be a
4500 nightmare. But, it's unlikely to change and is small,
4501 so duplicate it here. Note we don't need to catch all
4502 of the errors, since they'll be caught when parsing the
4503 expression. We just need to match the non-error
4504 cases. Thus we can ignore \n in single-quoted strings,
4505 for example. Or non-terminated strings. */
4506 if (ch == quote_char) {
4507 /* Does this match the string_type (single or triple
4508 quoted)? */
4509 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004510 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004511 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004512 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004513 string_type = 0;
4514 quote_char = 0;
4515 continue;
4516 }
4517 } else {
4518 /* We're at the end of a normal string. */
4519 quote_char = 0;
4520 string_type = 0;
4521 continue;
4522 }
4523 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004524 } else if (ch == '\'' || ch == '"') {
4525 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004526 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004527 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004528 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004529 } else {
4530 /* Start of a normal string. */
4531 string_type = 1;
4532 }
4533 /* Start looking for the end of the string. */
4534 quote_char = ch;
4535 } else if (ch == '[' || ch == '{' || ch == '(') {
4536 nested_depth++;
4537 } else if (nested_depth != 0 &&
4538 (ch == ']' || ch == '}' || ch == ')')) {
4539 nested_depth--;
4540 } else if (ch == '#') {
4541 /* Error: can't include a comment character, inside parens
4542 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004543 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004544 return -1;
4545 } else if (nested_depth == 0 &&
4546 (ch == '!' || ch == ':' || ch == '}')) {
4547 /* First, test for the special case of "!=". Since '=' is
4548 not an allowed conversion character, nothing is lost in
4549 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004550 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004551 /* This isn't a conversion character, just continue. */
4552 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004553 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004554 /* Normal way out of this loop. */
4555 break;
4556 } else {
4557 /* Just consume this char and loop around. */
4558 }
4559 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004560 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004561 /* If we leave this loop in a string or with mismatched parens, we
4562 don't care. We'll get a syntax error when compiling the
4563 expression. But, we can produce a better error message, so
4564 let's just do that.*/
4565 if (quote_char) {
4566 ast_error(c, n, "f-string: unterminated string");
4567 return -1;
4568 }
4569 if (nested_depth) {
4570 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4571 return -1;
4572 }
4573
Eric V. Smith451d0e32016-09-09 21:56:20 -04004574 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004575 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004576
4577 /* Compile the expression as soon as possible, so we show errors
4578 related to the expression before errors related to the
4579 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004580 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004581 if (!simple_expression)
4582 return -1;
4583
4584 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004585 if (**str == '!') {
4586 *str += 1;
4587 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004588 goto unexpected_end_of_string;
4589
Eric V. Smith451d0e32016-09-09 21:56:20 -04004590 conversion = **str;
4591 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004592
4593 /* Validate the conversion. */
4594 if (!(conversion == 's' || conversion == 'r'
4595 || conversion == 'a')) {
4596 ast_error(c, n, "f-string: invalid conversion character: "
4597 "expected 's', 'r', or 'a'");
4598 return -1;
4599 }
4600 }
4601
4602 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004603 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004604 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004605 if (**str == ':') {
4606 *str += 1;
4607 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004608 goto unexpected_end_of_string;
4609
4610 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004611 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004612 if (!format_spec)
4613 return -1;
4614 }
4615
Eric V. Smith451d0e32016-09-09 21:56:20 -04004616 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004617 goto unexpected_end_of_string;
4618
4619 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004620 assert(*str < end);
4621 assert(**str == '}');
4622 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004623
Eric V. Smith451d0e32016-09-09 21:56:20 -04004624 /* And now create the FormattedValue node that represents this
4625 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004626 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004627 format_spec, LINENO(n), n->n_col_offset,
4628 c->c_arena);
4629 if (!*expression)
4630 return -1;
4631
4632 return 0;
4633
4634unexpected_end_of_string:
4635 ast_error(c, n, "f-string: expecting '}'");
4636 return -1;
4637}
4638
4639/* Return -1 on error.
4640
4641 Return 0 if we have a literal (possible zero length) and an
4642 expression (zero length if at the end of the string.
4643
4644 Return 1 if we have a literal, but no expression, and we want the
4645 caller to call us again. This is used to deal with doubled
4646 braces.
4647
4648 When called multiple times on the string 'a{{b{0}c', this function
4649 will return:
4650
4651 1. the literal 'a{' with no expression, and a return value
4652 of 1. Despite the fact that there's no expression, the return
4653 value of 1 means we're not finished yet.
4654
4655 2. the literal 'b' and the expression '0', with a return value of
4656 0. The fact that there's an expression means we're not finished.
4657
4658 3. literal 'c' with no expression and a return value of 0. The
4659 combination of the return value of 0 with no expression means
4660 we're finished.
4661*/
4662static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004663fstring_find_literal_and_expr(const char **str, const char *end, int raw,
4664 int recurse_lvl, PyObject **literal,
4665 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004666 struct compiling *c, const node *n)
4667{
4668 int result;
4669
4670 assert(*literal == NULL && *expression == NULL);
4671
4672 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004673 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004674 if (result < 0)
4675 goto error;
4676
4677 assert(result == 0 || result == 1);
4678
4679 if (result == 1)
4680 /* We have a literal, but don't look at the expression. */
4681 return 1;
4682
Eric V. Smith451d0e32016-09-09 21:56:20 -04004683 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004684 /* We're at the end of the string or the end of a nested
4685 f-string: no expression. The top-level error case where we
4686 expect to be at the end of the string but we're at a '}' is
4687 handled later. */
4688 return 0;
4689
4690 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004691 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004692
Eric V. Smith451d0e32016-09-09 21:56:20 -04004693 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004694 goto error;
4695
4696 return 0;
4697
4698error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004699 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004700 return -1;
4701}
4702
4703#define EXPRLIST_N_CACHED 64
4704
4705typedef struct {
4706 /* Incrementally build an array of expr_ty, so be used in an
4707 asdl_seq. Cache some small but reasonably sized number of
4708 expr_ty's, and then after that start dynamically allocating,
4709 doubling the number allocated each time. Note that the f-string
4710 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
4711 Str for the literal 'a'. So you add expr_ty's about twice as
4712 fast as you add exressions in an f-string. */
4713
4714 Py_ssize_t allocated; /* Number we've allocated. */
4715 Py_ssize_t size; /* Number we've used. */
4716 expr_ty *p; /* Pointer to the memory we're actually
4717 using. Will point to 'data' until we
4718 start dynamically allocating. */
4719 expr_ty data[EXPRLIST_N_CACHED];
4720} ExprList;
4721
4722#ifdef NDEBUG
4723#define ExprList_check_invariants(l)
4724#else
4725static void
4726ExprList_check_invariants(ExprList *l)
4727{
4728 /* Check our invariants. Make sure this object is "live", and
4729 hasn't been deallocated. */
4730 assert(l->size >= 0);
4731 assert(l->p != NULL);
4732 if (l->size <= EXPRLIST_N_CACHED)
4733 assert(l->data == l->p);
4734}
4735#endif
4736
4737static void
4738ExprList_Init(ExprList *l)
4739{
4740 l->allocated = EXPRLIST_N_CACHED;
4741 l->size = 0;
4742
4743 /* Until we start allocating dynamically, p points to data. */
4744 l->p = l->data;
4745
4746 ExprList_check_invariants(l);
4747}
4748
4749static int
4750ExprList_Append(ExprList *l, expr_ty exp)
4751{
4752 ExprList_check_invariants(l);
4753 if (l->size >= l->allocated) {
4754 /* We need to alloc (or realloc) the memory. */
4755 Py_ssize_t new_size = l->allocated * 2;
4756
4757 /* See if we've ever allocated anything dynamically. */
4758 if (l->p == l->data) {
4759 Py_ssize_t i;
4760 /* We're still using the cached data. Switch to
4761 alloc-ing. */
4762 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4763 if (!l->p)
4764 return -1;
4765 /* Copy the cached data into the new buffer. */
4766 for (i = 0; i < l->size; i++)
4767 l->p[i] = l->data[i];
4768 } else {
4769 /* Just realloc. */
4770 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4771 if (!tmp) {
4772 PyMem_RawFree(l->p);
4773 l->p = NULL;
4774 return -1;
4775 }
4776 l->p = tmp;
4777 }
4778
4779 l->allocated = new_size;
4780 assert(l->allocated == 2 * l->size);
4781 }
4782
4783 l->p[l->size++] = exp;
4784
4785 ExprList_check_invariants(l);
4786 return 0;
4787}
4788
4789static void
4790ExprList_Dealloc(ExprList *l)
4791{
4792 ExprList_check_invariants(l);
4793
4794 /* If there's been an error, or we've never dynamically allocated,
4795 do nothing. */
4796 if (!l->p || l->p == l->data) {
4797 /* Do nothing. */
4798 } else {
4799 /* We have dynamically allocated. Free the memory. */
4800 PyMem_RawFree(l->p);
4801 }
4802 l->p = NULL;
4803 l->size = -1;
4804}
4805
4806static asdl_seq *
4807ExprList_Finish(ExprList *l, PyArena *arena)
4808{
4809 asdl_seq *seq;
4810
4811 ExprList_check_invariants(l);
4812
4813 /* Allocate the asdl_seq and copy the expressions in to it. */
4814 seq = _Py_asdl_seq_new(l->size, arena);
4815 if (seq) {
4816 Py_ssize_t i;
4817 for (i = 0; i < l->size; i++)
4818 asdl_seq_SET(seq, i, l->p[i]);
4819 }
4820 ExprList_Dealloc(l);
4821 return seq;
4822}
4823
4824/* The FstringParser is designed to add a mix of strings and
4825 f-strings, and concat them together as needed. Ultimately, it
4826 generates an expr_ty. */
4827typedef struct {
4828 PyObject *last_str;
4829 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004830 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004831} FstringParser;
4832
4833#ifdef NDEBUG
4834#define FstringParser_check_invariants(state)
4835#else
4836static void
4837FstringParser_check_invariants(FstringParser *state)
4838{
4839 if (state->last_str)
4840 assert(PyUnicode_CheckExact(state->last_str));
4841 ExprList_check_invariants(&state->expr_list);
4842}
4843#endif
4844
4845static void
4846FstringParser_Init(FstringParser *state)
4847{
4848 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004849 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004850 ExprList_Init(&state->expr_list);
4851 FstringParser_check_invariants(state);
4852}
4853
4854static void
4855FstringParser_Dealloc(FstringParser *state)
4856{
4857 FstringParser_check_invariants(state);
4858
4859 Py_XDECREF(state->last_str);
4860 ExprList_Dealloc(&state->expr_list);
4861}
4862
4863/* Make a Str node, but decref the PyUnicode object being added. */
4864static expr_ty
4865make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4866{
4867 PyObject *s = *str;
4868 *str = NULL;
4869 assert(PyUnicode_CheckExact(s));
4870 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4871 Py_DECREF(s);
4872 return NULL;
4873 }
4874 return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
4875}
4876
4877/* Add a non-f-string (that is, a regular literal string). str is
4878 decref'd. */
4879static int
4880FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4881{
4882 FstringParser_check_invariants(state);
4883
4884 assert(PyUnicode_CheckExact(str));
4885
4886 if (PyUnicode_GET_LENGTH(str) == 0) {
4887 Py_DECREF(str);
4888 return 0;
4889 }
4890
4891 if (!state->last_str) {
4892 /* We didn't have a string before, so just remember this one. */
4893 state->last_str = str;
4894 } else {
4895 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004896 PyUnicode_AppendAndDel(&state->last_str, str);
4897 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004898 return -1;
4899 }
4900 FstringParser_check_invariants(state);
4901 return 0;
4902}
4903
Eric V. Smith451d0e32016-09-09 21:56:20 -04004904/* Parse an f-string. The f-string is in *str to end, with no
4905 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004906static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004907FstringParser_ConcatFstring(FstringParser *state, const char **str,
4908 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004909 struct compiling *c, const node *n)
4910{
4911 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004912 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004913
4914 /* Parse the f-string. */
4915 while (1) {
4916 PyObject *literal = NULL;
4917 expr_ty expression = NULL;
4918
4919 /* If there's a zero length literal in front of the
4920 expression, literal will be NULL. If we're at the end of
4921 the f-string, expression will be NULL (unless result == 1,
4922 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004923 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004924 &literal, &expression,
4925 c, n);
4926 if (result < 0)
4927 return -1;
4928
4929 /* Add the literal, if any. */
4930 if (!literal) {
4931 /* Do nothing. Just leave last_str alone (and possibly
4932 NULL). */
4933 } else if (!state->last_str) {
4934 state->last_str = literal;
4935 literal = NULL;
4936 } else {
4937 /* We have a literal, concatenate it. */
4938 assert(PyUnicode_GET_LENGTH(literal) != 0);
4939 if (FstringParser_ConcatAndDel(state, literal) < 0)
4940 return -1;
4941 literal = NULL;
4942 }
4943 assert(!state->last_str ||
4944 PyUnicode_GET_LENGTH(state->last_str) != 0);
4945
4946 /* We've dealt with the literal now. It can't be leaked on further
4947 errors. */
4948 assert(literal == NULL);
4949
4950 /* See if we should just loop around to get the next literal
4951 and expression, while ignoring the expression this
4952 time. This is used for un-doubling braces, as an
4953 optimization. */
4954 if (result == 1)
4955 continue;
4956
4957 if (!expression)
4958 /* We're done with this f-string. */
4959 break;
4960
4961 /* We know we have an expression. Convert any existing string
4962 to a Str node. */
4963 if (!state->last_str) {
4964 /* Do nothing. No previous literal. */
4965 } else {
4966 /* Convert the existing last_str literal to a Str node. */
4967 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4968 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4969 return -1;
4970 }
4971
4972 if (ExprList_Append(&state->expr_list, expression) < 0)
4973 return -1;
4974 }
4975
Eric V. Smith235a6f02015-09-19 14:51:32 -04004976 /* If recurse_lvl is zero, then we must be at the end of the
4977 string. Otherwise, we must be at a right brace. */
4978
Eric V. Smith451d0e32016-09-09 21:56:20 -04004979 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004980 ast_error(c, n, "f-string: unexpected end of string");
4981 return -1;
4982 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004983 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004984 ast_error(c, n, "f-string: expecting '}'");
4985 return -1;
4986 }
4987
4988 FstringParser_check_invariants(state);
4989 return 0;
4990}
4991
4992/* Convert the partial state reflected in last_str and expr_list to an
4993 expr_ty. The expr_ty can be a Str, or a JoinedStr. */
4994static expr_ty
4995FstringParser_Finish(FstringParser *state, struct compiling *c,
4996 const node *n)
4997{
4998 asdl_seq *seq;
4999
5000 FstringParser_check_invariants(state);
5001
5002 /* If we're just a constant string with no expressions, return
5003 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005004 if (!state->fmode) {
5005 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005006 if (!state->last_str) {
5007 /* Create a zero length string. */
5008 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5009 if (!state->last_str)
5010 goto error;
5011 }
5012 return make_str_node_and_del(&state->last_str, c, n);
5013 }
5014
5015 /* Create a Str node out of last_str, if needed. It will be the
5016 last node in our expression list. */
5017 if (state->last_str) {
5018 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5019 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5020 goto error;
5021 }
5022 /* This has already been freed. */
5023 assert(state->last_str == NULL);
5024
5025 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5026 if (!seq)
5027 goto error;
5028
Eric V. Smith235a6f02015-09-19 14:51:32 -04005029 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
5030
5031error:
5032 FstringParser_Dealloc(state);
5033 return NULL;
5034}
5035
Eric V. Smith451d0e32016-09-09 21:56:20 -04005036/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5037 at end, parse it into an expr_ty. Return NULL on error. Adjust
5038 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005039static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005040fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005041 struct compiling *c, const node *n)
5042{
5043 FstringParser state;
5044
5045 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005046 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005047 c, n) < 0) {
5048 FstringParser_Dealloc(&state);
5049 return NULL;
5050 }
5051
5052 return FstringParser_Finish(&state, c, n);
5053}
5054
5055/* n is a Python string literal, including the bracketing quote
5056 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005057 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005058 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005059 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5060 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005061*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005062static int
5063parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5064 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005065{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005066 size_t len;
5067 const char *s = STR(n);
5068 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005069 int fmode = 0;
5070 *bytesmode = 0;
5071 *rawmode = 0;
5072 *result = NULL;
5073 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005074 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005075 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005076 if (quote == 'b' || quote == 'B') {
5077 quote = *++s;
5078 *bytesmode = 1;
5079 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005080 else if (quote == 'u' || quote == 'U') {
5081 quote = *++s;
5082 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005083 else if (quote == 'r' || quote == 'R') {
5084 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005085 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005086 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005087 else if (quote == 'f' || quote == 'F') {
5088 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005089 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005090 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005091 else {
5092 break;
5093 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005094 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005095 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005096 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005097 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005098 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005099 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005100 if (quote != '\'' && quote != '\"') {
5101 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005102 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005103 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005104 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005105 s++;
5106 len = strlen(s);
5107 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005108 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005109 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005110 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005111 }
5112 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005113 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005114 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005115 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005116 }
5117 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005118 /* A triple quoted string. We've already skipped one quote at
5119 the start and one at the end of the string. Now skip the
5120 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005121 s += 2;
5122 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005123 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005124 if (s[--len] != quote || s[--len] != quote) {
5125 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005126 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005127 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005128 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005129
Eric V. Smith451d0e32016-09-09 21:56:20 -04005130 if (fmode) {
5131 /* Just return the bytes. The caller will parse the resulting
5132 string. */
5133 *fstr = s;
5134 *fstrlen = len;
5135 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005136 }
5137
Eric V. Smith451d0e32016-09-09 21:56:20 -04005138 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005139 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005140 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005141 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005142 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005143 const char *ch;
5144 for (ch = s; *ch; ch++) {
5145 if (Py_CHARMASK(*ch) >= 0x80) {
5146 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005147 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005148 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005149 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005150 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005151 if (*rawmode)
5152 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005153 else
Eric V. Smith56466482016-10-31 14:46:26 -04005154 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005155 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005156 if (*rawmode)
5157 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005158 else
Eric V. Smith56466482016-10-31 14:46:26 -04005159 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005160 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005161 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005162}
5163
Eric V. Smith235a6f02015-09-19 14:51:32 -04005164/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5165 each STRING atom, and process it as needed. For bytes, just
5166 concatenate them together, and the result will be a Bytes node. For
5167 normal strings and f-strings, concatenate them together. The result
5168 will be a Str node if there were no f-strings; a FormattedValue
5169 node if there's just an f-string (with no leading or trailing
5170 literals), or a JoinedStr node if there are multiple f-strings or
5171 any literals involved. */
5172static expr_ty
5173parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005174{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005175 int bytesmode = 0;
5176 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005177 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005178
5179 FstringParser state;
5180 FstringParser_Init(&state);
5181
5182 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005183 int this_bytesmode;
5184 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005185 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005186 const char *fstr;
5187 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005188
5189 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005190 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5191 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005192 goto error;
5193
5194 /* Check that we're not mixing bytes with unicode. */
5195 if (i != 0 && bytesmode != this_bytesmode) {
5196 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005197 /* s is NULL if the current string part is an f-string. */
5198 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005199 goto error;
5200 }
5201 bytesmode = this_bytesmode;
5202
Eric V. Smith451d0e32016-09-09 21:56:20 -04005203 if (fstr != NULL) {
5204 int result;
5205 assert(s == NULL && !bytesmode);
5206 /* This is an f-string. Parse and concatenate it. */
5207 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5208 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005209 if (result < 0)
5210 goto error;
5211 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005212 /* A string or byte string. */
5213 assert(s != NULL && fstr == NULL);
5214
Eric V. Smith451d0e32016-09-09 21:56:20 -04005215 assert(bytesmode ? PyBytes_CheckExact(s) :
5216 PyUnicode_CheckExact(s));
5217
Eric V. Smith451d0e32016-09-09 21:56:20 -04005218 if (bytesmode) {
5219 /* For bytes, concat as we go. */
5220 if (i == 0) {
5221 /* First time, just remember this value. */
5222 bytes_str = s;
5223 } else {
5224 PyBytes_ConcatAndDel(&bytes_str, s);
5225 if (!bytes_str)
5226 goto error;
5227 }
5228 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005229 /* This is a regular string. Concatenate it. */
5230 if (FstringParser_ConcatAndDel(&state, s) < 0)
5231 goto error;
5232 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005233 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005234 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005235 if (bytesmode) {
5236 /* Just return the bytes object and we're done. */
5237 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5238 goto error;
5239 return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
5240 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005241
Eric V. Smith235a6f02015-09-19 14:51:32 -04005242 /* We're not a bytes string, bytes_str should never have been set. */
5243 assert(bytes_str == NULL);
5244
5245 return FstringParser_Finish(&state, c, n);
5246
5247error:
5248 Py_XDECREF(bytes_str);
5249 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005250 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005251}