blob: 89206c3464f92d2b2087d6611df829f6c33cb001 [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;
Stefan Krahf432a322017-08-21 13:09:59 +02001185 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001186 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001187 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001189 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001190 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191 }
1192 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001193 /* handle "not in" and "is not" */
1194 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 case NAME:
1196 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1197 return NotIn;
1198 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1199 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001200 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001202 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001204 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001205 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206 }
Neal Norwitz79792652005-11-14 04:25:03 +00001207 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001209 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210}
1211
1212static asdl_seq *
1213seq_for_testlist(struct compiling *c, const node *n)
1214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001216 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1217 */
Armin Rigo31441302005-10-21 12:57:31 +00001218 asdl_seq *seq;
1219 expr_ty expression;
1220 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001221 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001223 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224 if (!seq)
1225 return NULL;
1226
1227 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001229 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230
Benjamin Peterson4905e802009-09-27 02:43:28 +00001231 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001232 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234
1235 assert(i / 2 < seq->size);
1236 asdl_seq_SET(seq, i / 2, expression);
1237 }
1238 return seq;
1239}
1240
Neal Norwitzc1505362006-12-28 06:47:50 +00001241static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001242ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001243{
1244 identifier name;
1245 expr_ty annotation = NULL;
1246 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001247 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001248
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001249 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001250 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001251 name = NEW_IDENTIFIER(ch);
1252 if (!name)
1253 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001254 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001255 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001256
1257 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1258 annotation = ast_for_expr(c, CHILD(n, 2));
1259 if (!annotation)
1260 return NULL;
1261 }
1262
Victor Stinnerc106c682015-11-06 17:01:48 +01001263 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001264 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001265 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001266 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267}
1268
Guido van Rossum4f72a782006-10-27 23:31:49 +00001269/* returns -1 if failed to handle keyword only arguments
1270 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001271 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001272 ^^^
1273 start pointing here
1274 */
1275static int
1276handle_keywordonly_args(struct compiling *c, const node *n, int start,
1277 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1278{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001279 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001280 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001281 expr_ty expression, annotation;
1282 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001283 int i = start;
1284 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001285
1286 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001287 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001288 return -1;
1289 }
1290 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001291 while (i < NCH(n)) {
1292 ch = CHILD(n, i);
1293 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001294 case vfpdef:
1295 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001296 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001297 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001298 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001299 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001300 asdl_seq_SET(kwdefaults, j, expression);
1301 i += 2; /* '=' and test */
1302 }
1303 else { /* setting NULL if no default value exists */
1304 asdl_seq_SET(kwdefaults, j, NULL);
1305 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001306 if (NCH(ch) == 3) {
1307 /* ch is NAME ':' test */
1308 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001309 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001310 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001311 }
1312 else {
1313 annotation = NULL;
1314 }
1315 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001316 argname = NEW_IDENTIFIER(ch);
1317 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001318 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001319 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001320 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001321 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1322 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001323 if (!arg)
1324 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001325 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001326 i += 2; /* the name and the comma */
1327 break;
1328 case DOUBLESTAR:
1329 return i;
1330 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001331 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001332 goto error;
1333 }
1334 }
1335 return i;
1336 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001338}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001339
Jeremy Hyltona8293132006-02-28 17:58:27 +00001340/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341
1342static arguments_ty
1343ast_for_arguments(struct compiling *c, const node *n)
1344{
Neal Norwitzc1505362006-12-28 06:47:50 +00001345 /* This function handles both typedargslist (function definition)
1346 and varargslist (lambda definition).
1347
1348 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001349 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1350 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1351 | '**' tfpdef [',']]]
1352 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1353 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001354 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001355 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1356 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1357 | '**' vfpdef [',']]]
1358 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1359 | '**' vfpdef [',']
1360 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001361 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001362
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001364 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1365 int nposdefaults = 0, found_default = 0;
1366 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001367 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001368 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001369 node *ch;
1370
1371 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001372 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001373 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001374 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001376 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377
Jeremy Hyltone921e022008-07-17 16:37:17 +00001378 /* First count the number of positional args & defaults. The
1379 variable i is the loop index for this for loop and the next.
1380 The next loop picks up where the first leaves off.
1381 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001382 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001383 ch = CHILD(n, i);
1384 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001385 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001386 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001387 if (i < NCH(n) && /* skip argument following star */
1388 (TYPE(CHILD(n, i)) == tfpdef ||
1389 TYPE(CHILD(n, i)) == vfpdef)) {
1390 i++;
1391 }
1392 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001393 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001394 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001395 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001396 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001397 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001399 defaults for keyword only args */
1400 for ( ; i < NCH(n); ++i) {
1401 ch = CHILD(n, i);
1402 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001403 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001404 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001405 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001406 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001407 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001408 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001409 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001410 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001411 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001413 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001414 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001415 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001417 since we set NULL as default for keyword only argument w/o default
1418 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001419 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001420 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001421 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001422 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001423
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001424 /* tfpdef: NAME [':' test]
1425 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426 */
1427 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001428 j = 0; /* index for defaults */
1429 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001431 ch = CHILD(n, i);
1432 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001433 case tfpdef:
1434 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1436 anything other than EQUAL or a comma? */
1437 /* XXX Should NCH(n) check be made a separate check? */
1438 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001439 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1440 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001441 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001442 assert(posdefaults != NULL);
1443 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001445 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001447 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001448 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001449 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001450 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001451 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001452 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001453 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001454 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001455 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456 i += 2; /* the name and the comma */
1457 break;
1458 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001459 if (i+1 >= NCH(n) ||
1460 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001461 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001462 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001463 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001464 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001465 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001466 if (TYPE(ch) == COMMA) {
1467 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001468 i += 2; /* now follows keyword only arguments */
1469 res = handle_keywordonly_args(c, n, i,
1470 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001471 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001472 i = res; /* res has new position to process */
1473 }
1474 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001475 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001476 if (!vararg)
1477 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001478
Guido van Rossum4f72a782006-10-27 23:31:49 +00001479 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001480 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1481 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001482 int res = 0;
1483 res = handle_keywordonly_args(c, n, i,
1484 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001485 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001486 i = res; /* res has new position to process */
1487 }
1488 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489 break;
1490 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001491 ch = CHILD(n, i+1); /* tfpdef */
1492 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001493 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001494 if (!kwarg)
1495 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496 i += 3;
1497 break;
1498 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001499 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500 "unexpected node in varargslist: %d @ %d",
1501 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001502 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001503 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001505 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506}
1507
1508static expr_ty
1509ast_for_dotted_name(struct compiling *c, const node *n)
1510{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001511 expr_ty e;
1512 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001513 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514 int i;
1515
1516 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001517
1518 lineno = LINENO(n);
1519 col_offset = n->n_col_offset;
1520
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521 id = NEW_IDENTIFIER(CHILD(n, 0));
1522 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001523 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001524 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001526 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001527
1528 for (i = 2; i < NCH(n); i+=2) {
1529 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001530 if (!id)
1531 return NULL;
1532 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1533 if (!e)
1534 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535 }
1536
1537 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538}
1539
1540static expr_ty
1541ast_for_decorator(struct compiling *c, const node *n)
1542{
1543 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1544 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001545 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001547 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001548 REQ(CHILD(n, 0), AT);
1549 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1552 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001553 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001555 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001556 d = name_expr;
1557 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001558 }
1559 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001560 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001561 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001562 if (!d)
1563 return NULL;
1564 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565 }
1566 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001567 d = ast_for_call(c, CHILD(n, 3), name_expr);
1568 if (!d)
1569 return NULL;
1570 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001571 }
1572
1573 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574}
1575
1576static asdl_seq*
1577ast_for_decorators(struct compiling *c, const node *n)
1578{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001579 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001580 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001584 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001585 if (!decorator_seq)
1586 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001589 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001590 if (!d)
1591 return NULL;
1592 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001593 }
1594 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001595}
1596
1597static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001598ast_for_funcdef_impl(struct compiling *c, const node *n,
1599 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001601 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001602 identifier name;
1603 arguments_ty args;
1604 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001605 expr_ty returns = NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09001606 string docstring;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001607 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001608
1609 REQ(n, funcdef);
1610
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001611 name = NEW_IDENTIFIER(CHILD(n, name_i));
1612 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001613 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001614 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001615 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001616 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1617 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001618 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001619 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1620 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1621 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001622 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001623 name_i += 2;
1624 }
INADA Naokicb41b272017-02-23 00:31:59 +09001625 body = ast_for_body(c, CHILD(n, name_i + 3), &docstring);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001627 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001628
Yury Selivanov75445082015-05-11 22:57:16 -04001629 if (is_async)
1630 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
INADA Naokicb41b272017-02-23 00:31:59 +09001631 docstring, LINENO(n),
Yury Selivanov75445082015-05-11 22:57:16 -04001632 n->n_col_offset, c->c_arena);
1633 else
1634 return FunctionDef(name, args, body, decorator_seq, returns,
INADA Naokicb41b272017-02-23 00:31:59 +09001635 docstring, LINENO(n),
Yury Selivanov75445082015-05-11 22:57:16 -04001636 n->n_col_offset, c->c_arena);
1637}
1638
1639static stmt_ty
1640ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1641{
1642 /* async_funcdef: ASYNC funcdef */
1643 REQ(n, async_funcdef);
1644 REQ(CHILD(n, 0), ASYNC);
1645 REQ(CHILD(n, 1), funcdef);
1646
1647 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1648 1 /* is_async */);
1649}
1650
1651static stmt_ty
1652ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1653{
1654 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1655 return ast_for_funcdef_impl(c, n, decorator_seq,
1656 0 /* is_async */);
1657}
1658
1659
1660static stmt_ty
1661ast_for_async_stmt(struct compiling *c, const node *n)
1662{
1663 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
1664 REQ(n, async_stmt);
1665 REQ(CHILD(n, 0), ASYNC);
1666
1667 switch (TYPE(CHILD(n, 1))) {
1668 case funcdef:
1669 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1670 1 /* is_async */);
1671 case with_stmt:
1672 return ast_for_with_stmt(c, CHILD(n, 1),
1673 1 /* is_async */);
1674
1675 case for_stmt:
1676 return ast_for_for_stmt(c, CHILD(n, 1),
1677 1 /* is_async */);
1678
1679 default:
1680 PyErr_Format(PyExc_SystemError,
1681 "invalid async stament: %s",
1682 STR(CHILD(n, 1)));
1683 return NULL;
1684 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001685}
1686
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001687static stmt_ty
1688ast_for_decorated(struct compiling *c, const node *n)
1689{
Yury Selivanov75445082015-05-11 22:57:16 -04001690 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001691 stmt_ty thing = NULL;
1692 asdl_seq *decorator_seq = NULL;
1693
1694 REQ(n, decorated);
1695
1696 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1697 if (!decorator_seq)
1698 return NULL;
1699
1700 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001701 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001703
1704 if (TYPE(CHILD(n, 1)) == funcdef) {
1705 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1706 } else if (TYPE(CHILD(n, 1)) == classdef) {
1707 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001708 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1709 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001710 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001711 /* we count the decorators in when talking about the class' or
1712 * function's line number */
1713 if (thing) {
1714 thing->lineno = LINENO(n);
1715 thing->col_offset = n->n_col_offset;
1716 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001717 return thing;
1718}
1719
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720static expr_ty
1721ast_for_lambdef(struct compiling *c, const node *n)
1722{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001723 /* lambdef: 'lambda' [varargslist] ':' test
1724 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725 arguments_ty args;
1726 expr_ty expression;
1727
1728 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001729 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730 if (!args)
1731 return NULL;
1732 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001733 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735 }
1736 else {
1737 args = ast_for_arguments(c, CHILD(n, 1));
1738 if (!args)
1739 return NULL;
1740 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001741 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743 }
1744
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001745 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001746}
1747
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001748static expr_ty
1749ast_for_ifexpr(struct compiling *c, const node *n)
1750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001752 expr_ty expression, body, orelse;
1753
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001754 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001755 body = ast_for_expr(c, CHILD(n, 0));
1756 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001757 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001758 expression = ast_for_expr(c, CHILD(n, 2));
1759 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001760 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001761 orelse = ast_for_expr(c, CHILD(n, 4));
1762 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001763 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001764 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1765 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001766}
1767
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001769 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770
Nick Coghlan650f0d02007-04-15 12:05:43 +00001771 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001772*/
1773
1774static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001775count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001776{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001777 int n_fors = 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001778 int is_async;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001779
Guido van Rossumd8faa362007-04-27 19:54:29 +00001780 count_comp_for:
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001781 is_async = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001782 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001783 REQ(n, comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001784 if (TYPE(CHILD(n, 0)) == ASYNC) {
1785 is_async = 1;
1786 }
1787 if (NCH(n) == (5 + is_async)) {
1788 n = CHILD(n, 4 + is_async);
1789 }
1790 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001791 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001792 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001793 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001794 REQ(n, comp_iter);
1795 n = CHILD(n, 0);
1796 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001797 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001798 else if (TYPE(n) == comp_if) {
1799 if (NCH(n) == 3) {
1800 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001801 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001802 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001803 else
1804 return n_fors;
1805 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001806
Guido van Rossumd8faa362007-04-27 19:54:29 +00001807 /* Should never be reached */
1808 PyErr_SetString(PyExc_SystemError,
1809 "logic error in count_comp_fors");
1810 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811}
1812
Nick Coghlan650f0d02007-04-15 12:05:43 +00001813/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814
Nick Coghlan650f0d02007-04-15 12:05:43 +00001815 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001816*/
1817
1818static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001819count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001821 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001822
Guido van Rossumd8faa362007-04-27 19:54:29 +00001823 while (1) {
1824 REQ(n, comp_iter);
1825 if (TYPE(CHILD(n, 0)) == comp_for)
1826 return n_ifs;
1827 n = CHILD(n, 0);
1828 REQ(n, comp_if);
1829 n_ifs++;
1830 if (NCH(n) == 2)
1831 return n_ifs;
1832 n = CHILD(n, 2);
1833 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001834}
1835
Guido van Rossum992d4a32007-07-11 13:09:30 +00001836static asdl_seq *
1837ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001838{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001839 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001840 asdl_seq *comps;
1841
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001842 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001843 if (n_fors == -1)
1844 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001845
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001846 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001847 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001848 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001849
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001850 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001851 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001852 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001853 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001854 node *for_ch;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001855 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856
Guido van Rossum992d4a32007-07-11 13:09:30 +00001857 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001859 if (TYPE(CHILD(n, 0)) == ASYNC) {
1860 is_async = 1;
1861 }
1862
1863 for_ch = CHILD(n, 1 + is_async);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001864 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001865 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866 return NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001867 expression = ast_for_expr(c, CHILD(n, 3 + is_async));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001868 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001869 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001870
Thomas Wouters89f507f2006-12-13 04:49:30 +00001871 /* Check the # of children rather than the length of t, since
1872 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001873 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001874 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001875 comp = comprehension(first, expression, NULL,
1876 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001877 else
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001878 comp = comprehension(Tuple(t, Store, first->lineno,
1879 first->col_offset, c->c_arena),
1880 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001881 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001882 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001883
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001884 if (NCH(n) == (5 + is_async)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885 int j, n_ifs;
1886 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001888 n = CHILD(n, 4 + is_async);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001889 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001890 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001891 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001892
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001893 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001894 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001895 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001896
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001898 REQ(n, comp_iter);
1899 n = CHILD(n, 0);
1900 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901
Guido van Rossum992d4a32007-07-11 13:09:30 +00001902 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001903 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001904 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001905 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001906 if (NCH(n) == 3)
1907 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001909 /* on exit, must guarantee that n is a comp_for */
1910 if (TYPE(n) == comp_iter)
1911 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001912 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001914 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001915 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001916 return comps;
1917}
1918
1919static expr_ty
1920ast_for_itercomp(struct compiling *c, const node *n, int type)
1921{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001922 /* testlist_comp: (test|star_expr)
1923 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001924 expr_ty elt;
1925 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001926 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927
Guido van Rossum992d4a32007-07-11 13:09:30 +00001928 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001930 ch = CHILD(n, 0);
1931 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001932 if (!elt)
1933 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001934 if (elt->kind == Starred_kind) {
1935 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1936 return NULL;
1937 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938
Guido van Rossum992d4a32007-07-11 13:09:30 +00001939 comps = ast_for_comprehension(c, CHILD(n, 1));
1940 if (!comps)
1941 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001942
1943 if (type == COMP_GENEXP)
1944 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1945 else if (type == COMP_LISTCOMP)
1946 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1947 else if (type == COMP_SETCOMP)
1948 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1949 else
1950 /* Should never happen */
1951 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952}
1953
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001954/* Fills in the key, value pair corresponding to the dict element. In case
1955 * of an unpacking, key is NULL. *i is advanced by the number of ast
1956 * elements. Iff successful, nonzero is returned.
1957 */
1958static int
1959ast_for_dictelement(struct compiling *c, const node *n, int *i,
1960 expr_ty *key, expr_ty *value)
1961{
1962 expr_ty expression;
1963 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1964 assert(NCH(n) - *i >= 2);
1965
1966 expression = ast_for_expr(c, CHILD(n, *i + 1));
1967 if (!expression)
1968 return 0;
1969 *key = NULL;
1970 *value = expression;
1971
1972 *i += 2;
1973 }
1974 else {
1975 assert(NCH(n) - *i >= 3);
1976
1977 expression = ast_for_expr(c, CHILD(n, *i));
1978 if (!expression)
1979 return 0;
1980 *key = expression;
1981
1982 REQ(CHILD(n, *i + 1), COLON);
1983
1984 expression = ast_for_expr(c, CHILD(n, *i + 2));
1985 if (!expression)
1986 return 0;
1987 *value = expression;
1988
1989 *i += 3;
1990 }
1991 return 1;
1992}
1993
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001994static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001995ast_for_dictcomp(struct compiling *c, const node *n)
1996{
1997 expr_ty key, value;
1998 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001999 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002001 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002002 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002003 assert(key);
2004 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002006 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002007 if (!comps)
2008 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009
Guido van Rossum992d4a32007-07-11 13:09:30 +00002010 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
2011}
2012
2013static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002014ast_for_dictdisplay(struct compiling *c, const node *n)
2015{
2016 int i;
2017 int j;
2018 int size;
2019 asdl_seq *keys, *values;
2020
2021 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2022 keys = _Py_asdl_seq_new(size, c->c_arena);
2023 if (!keys)
2024 return NULL;
2025
2026 values = _Py_asdl_seq_new(size, c->c_arena);
2027 if (!values)
2028 return NULL;
2029
2030 j = 0;
2031 for (i = 0; i < NCH(n); i++) {
2032 expr_ty key, value;
2033
2034 if (!ast_for_dictelement(c, n, &i, &key, &value))
2035 return NULL;
2036 asdl_seq_SET(keys, j, key);
2037 asdl_seq_SET(values, j, value);
2038
2039 j++;
2040 }
2041 keys->size = j;
2042 values->size = j;
2043 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
2044}
2045
2046static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002047ast_for_genexp(struct compiling *c, const node *n)
2048{
2049 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002050 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002051}
2052
2053static expr_ty
2054ast_for_listcomp(struct compiling *c, const node *n)
2055{
2056 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002057 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002058}
2059
2060static expr_ty
2061ast_for_setcomp(struct compiling *c, const node *n)
2062{
2063 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002064 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002065}
2066
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002067static expr_ty
2068ast_for_setdisplay(struct compiling *c, const node *n)
2069{
2070 int i;
2071 int size;
2072 asdl_seq *elts;
2073
2074 assert(TYPE(n) == (dictorsetmaker));
2075 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2076 elts = _Py_asdl_seq_new(size, c->c_arena);
2077 if (!elts)
2078 return NULL;
2079 for (i = 0; i < NCH(n); i += 2) {
2080 expr_ty expression;
2081 expression = ast_for_expr(c, CHILD(n, i));
2082 if (!expression)
2083 return NULL;
2084 asdl_seq_SET(elts, i / 2, expression);
2085 }
2086 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2087}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002088
2089static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002090ast_for_atom(struct compiling *c, const node *n)
2091{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002092 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2093 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002094 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002095 */
2096 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002099 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002100 PyObject *name;
2101 const char *s = STR(ch);
2102 size_t len = strlen(s);
2103 if (len >= 4 && len <= 5) {
2104 if (!strcmp(s, "None"))
2105 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2106 if (!strcmp(s, "True"))
2107 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2108 if (!strcmp(s, "False"))
2109 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2110 }
2111 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002112 if (!name)
2113 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002114 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002115 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2116 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002117 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002118 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002119 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002120 const char *errtype = NULL;
2121 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2122 errtype = "unicode error";
2123 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2124 errtype = "value error";
2125 if (errtype) {
2126 char buf[128];
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002127 const char *s = NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002128 PyObject *type, *value, *tback, *errstr;
2129 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002130 errstr = PyObject_Str(value);
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002131 if (errstr)
2132 s = PyUnicode_AsUTF8(errstr);
2133 if (s) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002134 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002135 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002136 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002137 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002138 }
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002139 Py_XDECREF(errstr);
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002140 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002141 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002142 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002143 Py_XDECREF(tback);
2144 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002145 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002146 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002147 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002148 }
2149 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002150 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002151 if (!pynum)
2152 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002153
Victor Stinner43d81952013-07-17 00:57:58 +02002154 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2155 Py_DECREF(pynum);
2156 return NULL;
2157 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002158 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002159 }
Georg Brandldde00282007-03-18 19:01:53 +00002160 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002161 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002162 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002163 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164
Thomas Wouters89f507f2006-12-13 04:49:30 +00002165 if (TYPE(ch) == RPAR)
2166 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167
Thomas Wouters89f507f2006-12-13 04:49:30 +00002168 if (TYPE(ch) == yield_expr)
2169 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002172 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002173 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002174
Nick Coghlan650f0d02007-04-15 12:05:43 +00002175 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002176 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002177 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178
Thomas Wouters89f507f2006-12-13 04:49:30 +00002179 if (TYPE(ch) == RSQB)
2180 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181
Nick Coghlan650f0d02007-04-15 12:05:43 +00002182 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002183 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2184 asdl_seq *elts = seq_for_testlist(c, ch);
2185 if (!elts)
2186 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002187
Thomas Wouters89f507f2006-12-13 04:49:30 +00002188 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2189 }
2190 else
2191 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002192 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002193 /* dictorsetmaker: ( ((test ':' test | '**' test)
2194 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2195 * ((test | '*' test)
2196 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002197 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002198 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002199 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002200 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002201 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002202 }
2203 else {
2204 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2205 if (NCH(ch) == 1 ||
2206 (NCH(ch) > 1 &&
2207 TYPE(CHILD(ch, 1)) == COMMA)) {
2208 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002209 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002210 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002211 else if (NCH(ch) > 1 &&
2212 TYPE(CHILD(ch, 1)) == comp_for) {
2213 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002214 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002215 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002216 else if (NCH(ch) > 3 - is_dict &&
2217 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2218 /* It's a dictionary comprehension. */
2219 if (is_dict) {
2220 ast_error(c, n, "dict unpacking cannot be used in "
2221 "dict comprehension");
2222 return NULL;
2223 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002224 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002225 }
2226 else {
2227 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002228 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002229 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002230 if (res) {
2231 res->lineno = LINENO(n);
2232 res->col_offset = n->n_col_offset;
2233 }
2234 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002235 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002236 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002237 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002238 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2239 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002240 }
2241}
2242
2243static slice_ty
2244ast_for_slice(struct compiling *c, const node *n)
2245{
2246 node *ch;
2247 expr_ty lower = NULL, upper = NULL, step = NULL;
2248
2249 REQ(n, subscript);
2250
2251 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002252 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002253 sliceop: ':' [test]
2254 */
2255 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256 if (NCH(n) == 1 && TYPE(ch) == test) {
2257 /* 'step' variable hold no significance in terms of being used over
2258 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002260 if (!step)
2261 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262
Thomas Wouters89f507f2006-12-13 04:49:30 +00002263 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002264 }
2265
2266 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002267 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268 if (!lower)
2269 return NULL;
2270 }
2271
2272 /* If there's an upper bound it's in the second or third position. */
2273 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002274 if (NCH(n) > 1) {
2275 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276
Thomas Wouters89f507f2006-12-13 04:49:30 +00002277 if (TYPE(n2) == test) {
2278 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002279 if (!upper)
2280 return NULL;
2281 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002282 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002284 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002285
Thomas Wouters89f507f2006-12-13 04:49:30 +00002286 if (TYPE(n2) == test) {
2287 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002288 if (!upper)
2289 return NULL;
2290 }
2291 }
2292
2293 ch = CHILD(n, NCH(n) - 1);
2294 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002295 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002296 ch = CHILD(ch, 1);
2297 if (TYPE(ch) == test) {
2298 step = ast_for_expr(c, ch);
2299 if (!step)
2300 return NULL;
2301 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002302 }
2303 }
2304
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002305 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002306}
2307
2308static expr_ty
2309ast_for_binop(struct compiling *c, const node *n)
2310{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002311 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002313 BinOp(BinOp(A, op, B), op, C).
2314 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002315
Guido van Rossumd8faa362007-04-27 19:54:29 +00002316 int i, nops;
2317 expr_ty expr1, expr2, result;
2318 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002319
Guido van Rossumd8faa362007-04-27 19:54:29 +00002320 expr1 = ast_for_expr(c, CHILD(n, 0));
2321 if (!expr1)
2322 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002323
Guido van Rossumd8faa362007-04-27 19:54:29 +00002324 expr2 = ast_for_expr(c, CHILD(n, 2));
2325 if (!expr2)
2326 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002327
Guido van Rossumd8faa362007-04-27 19:54:29 +00002328 newoperator = get_operator(CHILD(n, 1));
2329 if (!newoperator)
2330 return NULL;
2331
2332 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2333 c->c_arena);
2334 if (!result)
2335 return NULL;
2336
2337 nops = (NCH(n) - 1) / 2;
2338 for (i = 1; i < nops; i++) {
2339 expr_ty tmp_result, tmp;
2340 const node* next_oper = CHILD(n, i * 2 + 1);
2341
2342 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002343 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002344 return NULL;
2345
Guido van Rossumd8faa362007-04-27 19:54:29 +00002346 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2347 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002348 return NULL;
2349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002351 LINENO(next_oper), next_oper->n_col_offset,
2352 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002354 return NULL;
2355 result = tmp_result;
2356 }
2357 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002358}
2359
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002360static expr_ty
2361ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002364 subscriptlist: subscript (',' subscript)* [',']
2365 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2366 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002367 REQ(n, trailer);
2368 if (TYPE(CHILD(n, 0)) == LPAR) {
2369 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002370 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002371 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002372 else
2373 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002374 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002375 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002376 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2377 if (!attr_id)
2378 return NULL;
2379 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002380 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002381 }
2382 else {
2383 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002384 REQ(CHILD(n, 2), RSQB);
2385 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002386 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002387 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2388 if (!slc)
2389 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002390 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2391 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002392 }
2393 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002395 by treating the sequence as a tuple literal if there are
2396 no slice features.
2397 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002398 int j;
2399 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002400 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002401 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002402 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002403 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002404 if (!slices)
2405 return NULL;
2406 for (j = 0; j < NCH(n); j += 2) {
2407 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002408 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002409 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002410 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002411 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002412 asdl_seq_SET(slices, j / 2, slc);
2413 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002414 if (!simple) {
2415 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002416 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002417 }
2418 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002419 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002420 if (!elts)
2421 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002422 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2423 slc = (slice_ty)asdl_seq_GET(slices, j);
2424 assert(slc->kind == Index_kind && slc->v.Index.value);
2425 asdl_seq_SET(elts, j, slc->v.Index.value);
2426 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002427 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002428 if (!e)
2429 return NULL;
2430 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002431 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002432 }
2433 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002434}
2435
2436static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002437ast_for_factor(struct compiling *c, const node *n)
2438{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002439 expr_ty expression;
2440
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002441 expression = ast_for_expr(c, CHILD(n, 1));
2442 if (!expression)
2443 return NULL;
2444
2445 switch (TYPE(CHILD(n, 0))) {
2446 case PLUS:
2447 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2448 c->c_arena);
2449 case MINUS:
2450 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2451 c->c_arena);
2452 case TILDE:
2453 return UnaryOp(Invert, expression, LINENO(n),
2454 n->n_col_offset, c->c_arena);
2455 }
2456 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2457 TYPE(CHILD(n, 0)));
2458 return NULL;
2459}
2460
2461static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002462ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002463{
Yury Selivanov75445082015-05-11 22:57:16 -04002464 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002465 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002466
2467 REQ(n, atom_expr);
2468 nch = NCH(n);
2469
2470 if (TYPE(CHILD(n, 0)) == AWAIT) {
2471 start = 1;
2472 assert(nch > 1);
2473 }
2474
2475 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002476 if (!e)
2477 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002478 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002479 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002480 if (start && nch == 2) {
2481 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2482 }
2483
2484 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002485 node *ch = CHILD(n, i);
2486 if (TYPE(ch) != trailer)
2487 break;
2488 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002489 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002490 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002491 tmp->lineno = e->lineno;
2492 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002493 e = tmp;
2494 }
Yury Selivanov75445082015-05-11 22:57:16 -04002495
2496 if (start) {
2497 /* there was an AWAIT */
2498 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2499 }
2500 else {
2501 return e;
2502 }
2503}
2504
2505static expr_ty
2506ast_for_power(struct compiling *c, const node *n)
2507{
2508 /* power: atom trailer* ('**' factor)*
2509 */
2510 expr_ty e;
2511 REQ(n, power);
2512 e = ast_for_atom_expr(c, CHILD(n, 0));
2513 if (!e)
2514 return NULL;
2515 if (NCH(n) == 1)
2516 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002517 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2518 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002519 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002520 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002521 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002522 }
2523 return e;
2524}
2525
Guido van Rossum0368b722007-05-11 16:50:42 +00002526static expr_ty
2527ast_for_starred(struct compiling *c, const node *n)
2528{
2529 expr_ty tmp;
2530 REQ(n, star_expr);
2531
2532 tmp = ast_for_expr(c, CHILD(n, 1));
2533 if (!tmp)
2534 return NULL;
2535
2536 /* The Load context is changed later. */
2537 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2538}
2539
2540
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002541/* Do not name a variable 'expr'! Will cause a compile error.
2542*/
2543
2544static expr_ty
2545ast_for_expr(struct compiling *c, const node *n)
2546{
2547 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002548 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002549 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002551 and_test: not_test ('and' not_test)*
2552 not_test: 'not' not_test | comparison
2553 comparison: expr (comp_op expr)*
2554 expr: xor_expr ('|' xor_expr)*
2555 xor_expr: and_expr ('^' and_expr)*
2556 and_expr: shift_expr ('&' shift_expr)*
2557 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2558 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002559 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002560 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002561 power: atom_expr ['**' factor]
2562 atom_expr: [AWAIT] atom trailer*
2563 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002564 */
2565
2566 asdl_seq *seq;
2567 int i;
2568
2569 loop:
2570 switch (TYPE(n)) {
2571 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002572 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002573 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002574 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002575 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002576 else if (NCH(n) > 1)
2577 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002578 /* Fallthrough */
2579 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002580 case and_test:
2581 if (NCH(n) == 1) {
2582 n = CHILD(n, 0);
2583 goto loop;
2584 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002585 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002586 if (!seq)
2587 return NULL;
2588 for (i = 0; i < NCH(n); i += 2) {
2589 expr_ty e = ast_for_expr(c, CHILD(n, i));
2590 if (!e)
2591 return NULL;
2592 asdl_seq_SET(seq, i / 2, e);
2593 }
2594 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002595 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2596 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002597 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002598 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002599 case not_test:
2600 if (NCH(n) == 1) {
2601 n = CHILD(n, 0);
2602 goto loop;
2603 }
2604 else {
2605 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2606 if (!expression)
2607 return NULL;
2608
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002609 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2610 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002611 }
2612 case comparison:
2613 if (NCH(n) == 1) {
2614 n = CHILD(n, 0);
2615 goto loop;
2616 }
2617 else {
2618 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002619 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002620 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002621 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622 if (!ops)
2623 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002624 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002625 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002626 return NULL;
2627 }
2628 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002629 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002630
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002631 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002632 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002633 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002634 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002635
2636 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002637 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002638 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002639 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002640
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002641 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642 asdl_seq_SET(cmps, i / 2, expression);
2643 }
2644 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002645 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002646 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002647 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002648
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002649 return Compare(expression, ops, cmps, LINENO(n),
2650 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002651 }
2652 break;
2653
Guido van Rossum0368b722007-05-11 16:50:42 +00002654 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656 /* The next five cases all handle BinOps. The main body of code
2657 is the same in each case, but the switch turned inside out to
2658 reuse the code for each type of operator.
2659 */
2660 case expr:
2661 case xor_expr:
2662 case and_expr:
2663 case shift_expr:
2664 case arith_expr:
2665 case term:
2666 if (NCH(n) == 1) {
2667 n = CHILD(n, 0);
2668 goto loop;
2669 }
2670 return ast_for_binop(c, n);
2671 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002672 node *an = NULL;
2673 node *en = NULL;
2674 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002675 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002676 if (NCH(n) > 1)
2677 an = CHILD(n, 1); /* yield_arg */
2678 if (an) {
2679 en = CHILD(an, NCH(an) - 1);
2680 if (NCH(an) == 2) {
2681 is_from = 1;
2682 exp = ast_for_expr(c, en);
2683 }
2684 else
2685 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002686 if (!exp)
2687 return NULL;
2688 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002689 if (is_from)
2690 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2691 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002692 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002693 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002694 if (NCH(n) == 1) {
2695 n = CHILD(n, 0);
2696 goto loop;
2697 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002698 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002699 case power:
2700 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002702 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002703 return NULL;
2704 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002705 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002706 return NULL;
2707}
2708
2709static expr_ty
2710ast_for_call(struct compiling *c, const node *n, expr_ty func)
2711{
2712 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002713 arglist: argument (',' argument)* [',']
2714 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002715 */
2716
2717 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002718 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002719 asdl_seq *args;
2720 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002721
2722 REQ(n, arglist);
2723
2724 nargs = 0;
2725 nkeywords = 0;
2726 ngens = 0;
2727 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002728 node *ch = CHILD(n, i);
2729 if (TYPE(ch) == argument) {
2730 if (NCH(ch) == 1)
2731 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002732 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002733 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002734 else if (TYPE(CHILD(ch, 0)) == STAR)
2735 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002736 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002737 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002738 nkeywords++;
2739 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740 }
2741 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002742 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002743 "if not sole argument");
2744 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002745 }
2746
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002747 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002749 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002750 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002751 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002752 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002753
2754 nargs = 0; /* positional arguments + iterable argument unpackings */
2755 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2756 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002758 node *ch = CHILD(n, i);
2759 if (TYPE(ch) == argument) {
2760 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002761 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002762 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002763 /* a positional argument */
2764 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002765 if (ndoublestars) {
2766 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002767 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002768 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002769 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002770 else {
2771 ast_error(c, chch,
2772 "positional argument follows "
2773 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002774 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002775 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002776 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002777 e = ast_for_expr(c, chch);
2778 if (!e)
2779 return NULL;
2780 asdl_seq_SET(args, nargs++, e);
2781 }
2782 else if (TYPE(chch) == STAR) {
2783 /* an iterable argument unpacking */
2784 expr_ty starred;
2785 if (ndoublestars) {
2786 ast_error(c, chch,
2787 "iterable argument unpacking follows "
2788 "keyword argument unpacking");
2789 return NULL;
2790 }
2791 e = ast_for_expr(c, CHILD(ch, 1));
2792 if (!e)
2793 return NULL;
2794 starred = Starred(e, Load, LINENO(chch),
2795 chch->n_col_offset,
2796 c->c_arena);
2797 if (!starred)
2798 return NULL;
2799 asdl_seq_SET(args, nargs++, starred);
2800
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002801 }
2802 else if (TYPE(chch) == DOUBLESTAR) {
2803 /* a keyword argument unpacking */
2804 keyword_ty kw;
2805 i++;
2806 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002807 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002808 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002809 kw = keyword(NULL, e, c->c_arena);
2810 asdl_seq_SET(keywords, nkeywords++, kw);
2811 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002813 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002814 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002815 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002817 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002818 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002820 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002821 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002822 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002823 identifier key, tmp;
2824 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002826 /* chch is test, but must be an identifier? */
2827 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002829 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002830 /* f(lambda x: x[0] = 3) ends up getting parsed with
2831 * LHS test = lambda x: x[0], and RHS test = 3.
2832 * SF bug 132313 points out that complaining about a keyword
2833 * then is very confusing.
2834 */
2835 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002836 ast_error(c, chch,
2837 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002838 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002839 }
2840 else if (e->kind != Name_kind) {
2841 ast_error(c, chch,
2842 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002843 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002844 }
2845 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002846 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002848 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002849 for (k = 0; k < nkeywords; k++) {
2850 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002851 if (tmp && !PyUnicode_Compare(tmp, key)) {
2852 ast_error(c, chch,
2853 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002854 return NULL;
2855 }
2856 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002857 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002858 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002859 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002860 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002861 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002862 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002863 asdl_seq_SET(keywords, nkeywords++, kw);
2864 }
2865 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866 }
2867
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002868 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869}
2870
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002872ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002873{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002874 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002875 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002876 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002877 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002878 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002879 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002880 }
2881 else {
2882 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002883 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002884 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002885 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002886 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002887 else {
2888 asdl_seq *tmp = seq_for_testlist(c, n);
2889 if (!tmp)
2890 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002891 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002893}
2894
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895static stmt_ty
2896ast_for_expr_stmt(struct compiling *c, const node *n)
2897{
2898 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002899 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
2900 ('=' (yield_expr|testlist_star_expr))*)
2901 annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +00002902 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002903 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002904 | '<<=' | '>>=' | '**=' | '//='
Martin Panter69332c12016-08-04 13:07:31 +00002905 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002906 */
2907
2908 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002909 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002910 if (!e)
2911 return NULL;
2912
Thomas Wouters89f507f2006-12-13 04:49:30 +00002913 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002914 }
2915 else if (TYPE(CHILD(n, 1)) == augassign) {
2916 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002917 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002918 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002919
Thomas Wouters89f507f2006-12-13 04:49:30 +00002920 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002921 if (!expr1)
2922 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002923 if(!set_context(c, expr1, Store, ch))
2924 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002925 /* set_context checks that most expressions are not the left side.
2926 Augmented assignments can only have a name, a subscript, or an
2927 attribute on the left, though, so we have to explicitly check for
2928 those. */
2929 switch (expr1->kind) {
2930 case Name_kind:
2931 case Attribute_kind:
2932 case Subscript_kind:
2933 break;
2934 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002935 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002936 return NULL;
2937 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002938
Thomas Wouters89f507f2006-12-13 04:49:30 +00002939 ch = CHILD(n, 2);
2940 if (TYPE(ch) == testlist)
2941 expr2 = ast_for_testlist(c, ch);
2942 else
2943 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002944 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002945 return NULL;
2946
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002947 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002948 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002949 return NULL;
2950
Thomas Wouters89f507f2006-12-13 04:49:30 +00002951 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002952 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002953 else if (TYPE(CHILD(n, 1)) == annassign) {
2954 expr_ty expr1, expr2, expr3;
2955 node *ch = CHILD(n, 0);
2956 node *deep, *ann = CHILD(n, 1);
2957 int simple = 1;
2958
2959 /* we keep track of parens to qualify (x) as expression not name */
2960 deep = ch;
2961 while (NCH(deep) == 1) {
2962 deep = CHILD(deep, 0);
2963 }
2964 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
2965 simple = 0;
2966 }
2967 expr1 = ast_for_testlist(c, ch);
2968 if (!expr1) {
2969 return NULL;
2970 }
2971 switch (expr1->kind) {
2972 case Name_kind:
2973 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
2974 return NULL;
2975 }
2976 expr1->v.Name.ctx = Store;
2977 break;
2978 case Attribute_kind:
2979 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
2980 return NULL;
2981 }
2982 expr1->v.Attribute.ctx = Store;
2983 break;
2984 case Subscript_kind:
2985 expr1->v.Subscript.ctx = Store;
2986 break;
2987 case List_kind:
2988 ast_error(c, ch,
2989 "only single target (not list) can be annotated");
2990 return NULL;
2991 case Tuple_kind:
2992 ast_error(c, ch,
2993 "only single target (not tuple) can be annotated");
2994 return NULL;
2995 default:
2996 ast_error(c, ch,
2997 "illegal target for annotation");
2998 return NULL;
2999 }
3000
3001 if (expr1->kind != Name_kind) {
3002 simple = 0;
3003 }
3004 ch = CHILD(ann, 1);
3005 expr2 = ast_for_expr(c, ch);
3006 if (!expr2) {
3007 return NULL;
3008 }
3009 if (NCH(ann) == 2) {
3010 return AnnAssign(expr1, expr2, NULL, simple,
3011 LINENO(n), n->n_col_offset, c->c_arena);
3012 }
3013 else {
3014 ch = CHILD(ann, 3);
3015 expr3 = ast_for_expr(c, ch);
3016 if (!expr3) {
3017 return NULL;
3018 }
3019 return AnnAssign(expr1, expr2, expr3, simple,
3020 LINENO(n), n->n_col_offset, c->c_arena);
3021 }
3022 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003024 int i;
3025 asdl_seq *targets;
3026 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003027 expr_ty expression;
3028
Thomas Wouters89f507f2006-12-13 04:49:30 +00003029 /* a normal assignment */
3030 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003031 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003032 if (!targets)
3033 return NULL;
3034 for (i = 0; i < NCH(n) - 2; i += 2) {
3035 expr_ty e;
3036 node *ch = CHILD(n, i);
3037 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003038 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003039 return NULL;
3040 }
3041 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003042 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003043 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003044
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003045 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003046 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003047 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003048
Thomas Wouters89f507f2006-12-13 04:49:30 +00003049 asdl_seq_SET(targets, i / 2, e);
3050 }
3051 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003052 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003053 expression = ast_for_testlist(c, value);
3054 else
3055 expression = ast_for_expr(c, value);
3056 if (!expression)
3057 return NULL;
3058 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003059 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003060}
3061
Benjamin Peterson78565b22009-06-28 19:19:51 +00003062
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003063static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003064ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003065{
3066 asdl_seq *seq;
3067 int i;
3068 expr_ty e;
3069
3070 REQ(n, exprlist);
3071
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003072 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003073 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003074 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003075 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003076 e = ast_for_expr(c, CHILD(n, i));
3077 if (!e)
3078 return NULL;
3079 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003080 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003081 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003082 }
3083 return seq;
3084}
3085
3086static stmt_ty
3087ast_for_del_stmt(struct compiling *c, const node *n)
3088{
3089 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003091 /* del_stmt: 'del' exprlist */
3092 REQ(n, del_stmt);
3093
3094 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3095 if (!expr_list)
3096 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003097 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003098}
3099
3100static stmt_ty
3101ast_for_flow_stmt(struct compiling *c, const node *n)
3102{
3103 /*
3104 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3105 | yield_stmt
3106 break_stmt: 'break'
3107 continue_stmt: 'continue'
3108 return_stmt: 'return' [testlist]
3109 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003110 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003111 raise_stmt: 'raise' [test [',' test [',' test]]]
3112 */
3113 node *ch;
3114
3115 REQ(n, flow_stmt);
3116 ch = CHILD(n, 0);
3117 switch (TYPE(ch)) {
3118 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003119 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003120 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003121 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003122 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003123 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3124 if (!exp)
3125 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003126 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003127 }
3128 case return_stmt:
3129 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003130 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003131 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003132 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133 if (!expression)
3134 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003135 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003136 }
3137 case raise_stmt:
3138 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003139 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3140 else if (NCH(ch) >= 2) {
3141 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003142 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3143 if (!expression)
3144 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003145 if (NCH(ch) == 4) {
3146 cause = ast_for_expr(c, CHILD(ch, 3));
3147 if (!cause)
3148 return NULL;
3149 }
3150 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003151 }
Stefan Krahf432a322017-08-21 13:09:59 +02003152 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003153 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003154 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003155 "unexpected flow_stmt: %d", TYPE(ch));
3156 return NULL;
3157 }
3158}
3159
3160static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003161alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003162{
3163 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003164 import_as_name: NAME ['as' NAME]
3165 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003166 dotted_name: NAME ('.' NAME)*
3167 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003168 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003169
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003170 loop:
3171 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003172 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003173 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003174 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003175 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003176 if (!name)
3177 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003178 if (NCH(n) == 3) {
3179 node *str_node = CHILD(n, 2);
3180 str = NEW_IDENTIFIER(str_node);
3181 if (!str)
3182 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003183 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003184 return NULL;
3185 }
3186 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003187 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003188 return NULL;
3189 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003190 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003191 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003192 case dotted_as_name:
3193 if (NCH(n) == 1) {
3194 n = CHILD(n, 0);
3195 goto loop;
3196 }
3197 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003198 node *asname_node = CHILD(n, 2);
3199 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003200 if (!a)
3201 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003203 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003204 if (!a->asname)
3205 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003206 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003207 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003208 return a;
3209 }
3210 break;
3211 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003212 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003213 node *name_node = CHILD(n, 0);
3214 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003215 if (!name)
3216 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003217 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003218 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003219 return alias(name, NULL, c->c_arena);
3220 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003221 else {
3222 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003223 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003224 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003225 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003227
3228 len = 0;
3229 for (i = 0; i < NCH(n); i += 2)
3230 /* length of string plus one for the dot */
3231 len += strlen(STR(CHILD(n, i))) + 1;
3232 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003233 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003234 if (!str)
3235 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003236 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003237 if (!s)
3238 return NULL;
3239 for (i = 0; i < NCH(n); i += 2) {
3240 char *sch = STR(CHILD(n, i));
3241 strcpy(s, STR(CHILD(n, i)));
3242 s += strlen(sch);
3243 *s++ = '.';
3244 }
3245 --s;
3246 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003247 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3248 PyBytes_GET_SIZE(str),
3249 NULL);
3250 Py_DECREF(str);
3251 if (!uni)
3252 return NULL;
3253 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003254 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003255 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3256 Py_DECREF(str);
3257 return NULL;
3258 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003259 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003260 }
3261 break;
3262 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003263 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003264 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3265 Py_DECREF(str);
3266 return NULL;
3267 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003268 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003269 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003270 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003271 "unexpected import name: %d", TYPE(n));
3272 return NULL;
3273 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003274
3275 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003276 return NULL;
3277}
3278
3279static stmt_ty
3280ast_for_import_stmt(struct compiling *c, const node *n)
3281{
3282 /*
3283 import_stmt: import_name | import_from
3284 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003285 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3286 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003287 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003288 int lineno;
3289 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003290 int i;
3291 asdl_seq *aliases;
3292
3293 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003294 lineno = LINENO(n);
3295 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003296 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003297 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003298 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003299 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003300 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003301 if (!aliases)
3302 return NULL;
3303 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003304 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003305 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003306 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003307 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003308 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003309 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003310 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003311 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003312 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003313 int idx, ndots = 0;
3314 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003315 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003317 /* Count the number of dots (for relative imports) and check for the
3318 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003319 for (idx = 1; idx < NCH(n); idx++) {
3320 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003321 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3322 if (!mod)
3323 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003324 idx++;
3325 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003326 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003328 ndots += 3;
3329 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003330 } else if (TYPE(CHILD(n, idx)) != DOT) {
3331 break;
3332 }
3333 ndots++;
3334 }
3335 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003336 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003337 case STAR:
3338 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003339 n = CHILD(n, idx);
3340 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003341 break;
3342 case LPAR:
3343 /* from ... import (x, y, z) */
3344 n = CHILD(n, idx + 1);
3345 n_children = NCH(n);
3346 break;
3347 case import_as_names:
3348 /* from ... import x, y, z */
3349 n = CHILD(n, idx);
3350 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003351 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003352 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003353 " surrounding parentheses");
3354 return NULL;
3355 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003356 break;
3357 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003358 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003359 return NULL;
3360 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003361
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003362 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003363 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003364 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003365
3366 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003367 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003368 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003369 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003370 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003371 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003372 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003373 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003374 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003375 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003376 if (!import_alias)
3377 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003378 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003379 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003380 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003381 if (mod != NULL)
3382 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003383 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003384 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003385 }
Neal Norwitz79792652005-11-14 04:25:03 +00003386 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003387 "unknown import statement: starts with command '%s'",
3388 STR(CHILD(n, 0)));
3389 return NULL;
3390}
3391
3392static stmt_ty
3393ast_for_global_stmt(struct compiling *c, const node *n)
3394{
3395 /* global_stmt: 'global' NAME (',' NAME)* */
3396 identifier name;
3397 asdl_seq *s;
3398 int i;
3399
3400 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003401 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003402 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003403 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003404 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003405 name = NEW_IDENTIFIER(CHILD(n, i));
3406 if (!name)
3407 return NULL;
3408 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003409 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003410 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003411}
3412
3413static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003414ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3415{
3416 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3417 identifier name;
3418 asdl_seq *s;
3419 int i;
3420
3421 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003422 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003423 if (!s)
3424 return NULL;
3425 for (i = 1; i < NCH(n); i += 2) {
3426 name = NEW_IDENTIFIER(CHILD(n, i));
3427 if (!name)
3428 return NULL;
3429 asdl_seq_SET(s, i / 2, name);
3430 }
3431 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3432}
3433
3434static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003435ast_for_assert_stmt(struct compiling *c, const node *n)
3436{
3437 /* assert_stmt: 'assert' test [',' test] */
3438 REQ(n, assert_stmt);
3439 if (NCH(n) == 2) {
3440 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3441 if (!expression)
3442 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003443 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003444 }
3445 else if (NCH(n) == 4) {
3446 expr_ty expr1, expr2;
3447
3448 expr1 = ast_for_expr(c, CHILD(n, 1));
3449 if (!expr1)
3450 return NULL;
3451 expr2 = ast_for_expr(c, CHILD(n, 3));
3452 if (!expr2)
3453 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003454
Thomas Wouters89f507f2006-12-13 04:49:30 +00003455 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003456 }
Neal Norwitz79792652005-11-14 04:25:03 +00003457 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003458 "improper number of parts to 'assert' statement: %d",
3459 NCH(n));
3460 return NULL;
3461}
3462
3463static asdl_seq *
3464ast_for_suite(struct compiling *c, const node *n)
3465{
3466 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003467 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003468 stmt_ty s;
3469 int i, total, num, end, pos = 0;
3470 node *ch;
3471
3472 REQ(n, suite);
3473
3474 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003475 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003476 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003477 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003478 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003479 n = CHILD(n, 0);
3480 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003482 */
3483 end = NCH(n) - 1;
3484 if (TYPE(CHILD(n, end - 1)) == SEMI)
3485 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003486 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003487 for (i = 0; i < end; i += 2) {
3488 ch = CHILD(n, i);
3489 s = ast_for_stmt(c, ch);
3490 if (!s)
3491 return NULL;
3492 asdl_seq_SET(seq, pos++, s);
3493 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003494 }
3495 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003496 for (i = 2; i < (NCH(n) - 1); i++) {
3497 ch = CHILD(n, i);
3498 REQ(ch, stmt);
3499 num = num_stmts(ch);
3500 if (num == 1) {
3501 /* small_stmt or compound_stmt with only one child */
3502 s = ast_for_stmt(c, ch);
3503 if (!s)
3504 return NULL;
3505 asdl_seq_SET(seq, pos++, s);
3506 }
3507 else {
3508 int j;
3509 ch = CHILD(ch, 0);
3510 REQ(ch, simple_stmt);
3511 for (j = 0; j < NCH(ch); j += 2) {
3512 /* statement terminates with a semi-colon ';' */
3513 if (NCH(CHILD(ch, j)) == 0) {
3514 assert((j + 1) == NCH(ch));
3515 break;
3516 }
3517 s = ast_for_stmt(c, CHILD(ch, j));
3518 if (!s)
3519 return NULL;
3520 asdl_seq_SET(seq, pos++, s);
3521 }
3522 }
3523 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003524 }
3525 assert(pos == seq->size);
3526 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003527}
3528
INADA Naokicb41b272017-02-23 00:31:59 +09003529static string
3530docstring_from_stmts(asdl_seq *stmts)
3531{
3532 if (stmts && stmts->size) {
3533 stmt_ty s = (stmt_ty)asdl_seq_GET(stmts, 0);
3534 /* If first statement is a literal string, it's the doc string. */
3535 if (s->kind == Expr_kind && s->v.Expr.value->kind == Str_kind) {
3536 string doc = s->v.Expr.value->v.Str.s;
3537 /* not very efficient, but simple */
3538 memmove(&asdl_seq_GET(stmts, 0), &asdl_seq_GET(stmts, 1),
3539 (stmts->size - 1) * sizeof(void*));
3540 stmts->size--;
3541 return doc;
3542 }
3543 }
3544 return NULL;
3545}
3546
3547static asdl_seq *
3548ast_for_body(struct compiling *c, const node *n, string *docstring)
3549{
3550 asdl_seq *stmts = ast_for_suite(c, n);
3551 *docstring = docstring_from_stmts(stmts);
3552 return stmts;
3553}
3554
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003555static stmt_ty
3556ast_for_if_stmt(struct compiling *c, const node *n)
3557{
3558 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3559 ['else' ':' suite]
3560 */
3561 char *s;
3562
3563 REQ(n, if_stmt);
3564
3565 if (NCH(n) == 4) {
3566 expr_ty expression;
3567 asdl_seq *suite_seq;
3568
3569 expression = ast_for_expr(c, CHILD(n, 1));
3570 if (!expression)
3571 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003572 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003573 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003574 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575
Guido van Rossumd8faa362007-04-27 19:54:29 +00003576 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3577 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003578 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003579
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003580 s = STR(CHILD(n, 4));
3581 /* s[2], the third character in the string, will be
3582 's' for el_s_e, or
3583 'i' for el_i_f
3584 */
3585 if (s[2] == 's') {
3586 expr_ty expression;
3587 asdl_seq *seq1, *seq2;
3588
3589 expression = ast_for_expr(c, CHILD(n, 1));
3590 if (!expression)
3591 return NULL;
3592 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003593 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003594 return NULL;
3595 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003596 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003597 return NULL;
3598
Guido van Rossumd8faa362007-04-27 19:54:29 +00003599 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3600 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003601 }
3602 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003603 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003604 expr_ty expression;
3605 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003606 asdl_seq *orelse = NULL;
3607 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003608 /* must reference the child n_elif+1 since 'else' token is third,
3609 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003610 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3611 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3612 has_else = 1;
3613 n_elif -= 3;
3614 }
3615 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003616
Thomas Wouters89f507f2006-12-13 04:49:30 +00003617 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003618 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003619
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003620 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003621 if (!orelse)
3622 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003623 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003624 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003625 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003626 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3627 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003628 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003629 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3630 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003631 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003633 asdl_seq_SET(orelse, 0,
3634 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003635 LINENO(CHILD(n, NCH(n) - 6)),
3636 CHILD(n, NCH(n) - 6)->n_col_offset,
3637 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003638 /* the just-created orelse handled the last elif */
3639 n_elif--;
3640 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003641
Thomas Wouters89f507f2006-12-13 04:49:30 +00003642 for (i = 0; i < n_elif; i++) {
3643 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003644 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003645 if (!newobj)
3646 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003647 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003648 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003649 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003650 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003651 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003652 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003653
Thomas Wouters89f507f2006-12-13 04:49:30 +00003654 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003656 LINENO(CHILD(n, off)),
3657 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003658 orelse = newobj;
3659 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003660 expression = ast_for_expr(c, CHILD(n, 1));
3661 if (!expression)
3662 return NULL;
3663 suite_seq = ast_for_suite(c, CHILD(n, 3));
3664 if (!suite_seq)
3665 return NULL;
3666 return If(expression, suite_seq, orelse,
3667 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003668 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003669
3670 PyErr_Format(PyExc_SystemError,
3671 "unexpected token in 'if' statement: %s", s);
3672 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003673}
3674
3675static stmt_ty
3676ast_for_while_stmt(struct compiling *c, const node *n)
3677{
3678 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3679 REQ(n, while_stmt);
3680
3681 if (NCH(n) == 4) {
3682 expr_ty expression;
3683 asdl_seq *suite_seq;
3684
3685 expression = ast_for_expr(c, CHILD(n, 1));
3686 if (!expression)
3687 return NULL;
3688 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003689 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003690 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003691 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003692 }
3693 else if (NCH(n) == 7) {
3694 expr_ty expression;
3695 asdl_seq *seq1, *seq2;
3696
3697 expression = ast_for_expr(c, CHILD(n, 1));
3698 if (!expression)
3699 return NULL;
3700 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003701 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003702 return NULL;
3703 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003704 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003705 return NULL;
3706
Thomas Wouters89f507f2006-12-13 04:49:30 +00003707 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003708 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003709
3710 PyErr_Format(PyExc_SystemError,
3711 "wrong number of tokens for 'while' statement: %d",
3712 NCH(n));
3713 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003714}
3715
3716static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003717ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003718{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003719 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003720 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003721 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003722 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003723 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3724 REQ(n, for_stmt);
3725
3726 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003727 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003728 if (!seq)
3729 return NULL;
3730 }
3731
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003732 node_target = CHILD(n, 1);
3733 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003734 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003735 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003736 /* Check the # of children rather than the length of _target, since
3737 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003738 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003739 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003740 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003741 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003742 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003743
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003744 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003745 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003746 return NULL;
3747 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003748 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003749 return NULL;
3750
Yury Selivanov75445082015-05-11 22:57:16 -04003751 if (is_async)
3752 return AsyncFor(target, expression, suite_seq, seq,
3753 LINENO(n), n->n_col_offset,
3754 c->c_arena);
3755 else
3756 return For(target, expression, suite_seq, seq,
3757 LINENO(n), n->n_col_offset,
3758 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003759}
3760
3761static excepthandler_ty
3762ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3763{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003764 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003765 REQ(exc, except_clause);
3766 REQ(body, suite);
3767
3768 if (NCH(exc) == 1) {
3769 asdl_seq *suite_seq = ast_for_suite(c, body);
3770 if (!suite_seq)
3771 return NULL;
3772
Neal Norwitzad74aa82008-03-31 05:14:30 +00003773 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003774 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003775 }
3776 else if (NCH(exc) == 2) {
3777 expr_ty expression;
3778 asdl_seq *suite_seq;
3779
3780 expression = ast_for_expr(c, CHILD(exc, 1));
3781 if (!expression)
3782 return NULL;
3783 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003784 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003785 return NULL;
3786
Neal Norwitzad74aa82008-03-31 05:14:30 +00003787 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003788 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003789 }
3790 else if (NCH(exc) == 4) {
3791 asdl_seq *suite_seq;
3792 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003793 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003794 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003795 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003796 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003797 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003798 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003799 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003800 return NULL;
3801 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003802 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003803 return NULL;
3804
Neal Norwitzad74aa82008-03-31 05:14:30 +00003805 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003806 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003807 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003808
3809 PyErr_Format(PyExc_SystemError,
3810 "wrong number of children for 'except' clause: %d",
3811 NCH(exc));
3812 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003813}
3814
3815static stmt_ty
3816ast_for_try_stmt(struct compiling *c, const node *n)
3817{
Neal Norwitzf599f422005-12-17 21:33:47 +00003818 const int nch = NCH(n);
3819 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003820 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003821
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003822 REQ(n, try_stmt);
3823
Neal Norwitzf599f422005-12-17 21:33:47 +00003824 body = ast_for_suite(c, CHILD(n, 2));
3825 if (body == NULL)
3826 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003827
Neal Norwitzf599f422005-12-17 21:33:47 +00003828 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3829 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3830 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3831 /* we can assume it's an "else",
3832 because nch >= 9 for try-else-finally and
3833 it would otherwise have a type of except_clause */
3834 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3835 if (orelse == NULL)
3836 return NULL;
3837 n_except--;
3838 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003839
Neal Norwitzf599f422005-12-17 21:33:47 +00003840 finally = ast_for_suite(c, CHILD(n, nch - 1));
3841 if (finally == NULL)
3842 return NULL;
3843 n_except--;
3844 }
3845 else {
3846 /* we can assume it's an "else",
3847 otherwise it would have a type of except_clause */
3848 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3849 if (orelse == NULL)
3850 return NULL;
3851 n_except--;
3852 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003853 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003854 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003855 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003856 return NULL;
3857 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003858
Neal Norwitzf599f422005-12-17 21:33:47 +00003859 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003860 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003861 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003862 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003863 if (handlers == NULL)
3864 return NULL;
3865
3866 for (i = 0; i < n_except; i++) {
3867 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3868 CHILD(n, 5 + i * 3));
3869 if (!e)
3870 return NULL;
3871 asdl_seq_SET(handlers, i, e);
3872 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003873 }
3874
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003875 assert(finally != NULL || asdl_seq_LEN(handlers));
3876 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003877}
3878
Georg Brandl0c315622009-05-25 21:10:36 +00003879/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003880static withitem_ty
3881ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003882{
3883 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003884
Georg Brandl0c315622009-05-25 21:10:36 +00003885 REQ(n, with_item);
3886 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003887 if (!context_expr)
3888 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003889 if (NCH(n) == 3) {
3890 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003891
3892 if (!optional_vars) {
3893 return NULL;
3894 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003895 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003896 return NULL;
3897 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003898 }
3899
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003900 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003901}
3902
Georg Brandl0c315622009-05-25 21:10:36 +00003903/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3904static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003905ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003906{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003907 int i, n_items;
3908 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003909
3910 REQ(n, with_stmt);
3911
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003912 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003913 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003914 if (!items)
3915 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003916 for (i = 1; i < NCH(n) - 2; i += 2) {
3917 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3918 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003919 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003920 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003921 }
3922
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003923 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3924 if (!body)
3925 return NULL;
3926
Yury Selivanov75445082015-05-11 22:57:16 -04003927 if (is_async)
3928 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3929 else
3930 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003931}
3932
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003933static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003934ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003935{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003936 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003937 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003938 asdl_seq *s;
INADA Naokicb41b272017-02-23 00:31:59 +09003939 string docstring;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003940 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003941
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003942 REQ(n, classdef);
3943
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003944 if (NCH(n) == 4) { /* class NAME ':' suite */
INADA Naokicb41b272017-02-23 00:31:59 +09003945 s = ast_for_body(c, CHILD(n, 3), &docstring);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003946 if (!s)
3947 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003948 classname = NEW_IDENTIFIER(CHILD(n, 1));
3949 if (!classname)
3950 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003951 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003952 return NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09003953 return ClassDef(classname, NULL, NULL, s, decorator_seq, docstring,
3954 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003955 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003956
3957 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
INADA Naokicb41b272017-02-23 00:31:59 +09003958 s = ast_for_body(c, CHILD(n, 5), &docstring);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003959 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003960 return NULL;
3961 classname = NEW_IDENTIFIER(CHILD(n, 1));
3962 if (!classname)
3963 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003964 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003965 return NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09003966 return ClassDef(classname, NULL, NULL, s, decorator_seq, docstring,
3967 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003968 }
3969
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003970 /* class NAME '(' arglist ')' ':' suite */
3971 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003972 {
3973 PyObject *dummy_name;
3974 expr_ty dummy;
3975 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3976 if (!dummy_name)
3977 return NULL;
3978 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3979 call = ast_for_call(c, CHILD(n, 3), dummy);
3980 if (!call)
3981 return NULL;
3982 }
INADA Naokicb41b272017-02-23 00:31:59 +09003983 s = ast_for_body(c, CHILD(n, 6), &docstring);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003984 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003985 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003986 classname = NEW_IDENTIFIER(CHILD(n, 1));
3987 if (!classname)
3988 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003989 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003990 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003991
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003992 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
INADA Naokicb41b272017-02-23 00:31:59 +09003993 decorator_seq, docstring, LINENO(n), n->n_col_offset,
3994 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003995}
3996
3997static stmt_ty
3998ast_for_stmt(struct compiling *c, const node *n)
3999{
4000 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004001 assert(NCH(n) == 1);
4002 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004003 }
4004 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004005 assert(num_stmts(n) == 1);
4006 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004007 }
4008 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004009 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004010 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4011 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004012 */
4013 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004014 case expr_stmt:
4015 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004016 case del_stmt:
4017 return ast_for_del_stmt(c, n);
4018 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00004019 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004020 case flow_stmt:
4021 return ast_for_flow_stmt(c, n);
4022 case import_stmt:
4023 return ast_for_import_stmt(c, n);
4024 case global_stmt:
4025 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004026 case nonlocal_stmt:
4027 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004028 case assert_stmt:
4029 return ast_for_assert_stmt(c, n);
4030 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004031 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004032 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4033 TYPE(n), NCH(n));
4034 return NULL;
4035 }
4036 }
4037 else {
4038 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004039 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004040 */
4041 node *ch = CHILD(n, 0);
4042 REQ(n, compound_stmt);
4043 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004044 case if_stmt:
4045 return ast_for_if_stmt(c, ch);
4046 case while_stmt:
4047 return ast_for_while_stmt(c, ch);
4048 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004049 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004050 case try_stmt:
4051 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004052 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004053 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004054 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004055 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004056 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004057 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004058 case decorated:
4059 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004060 case async_stmt:
4061 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004062 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004063 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004064 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4065 TYPE(n), NCH(n));
4066 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004067 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004068 }
4069}
4070
4071static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004072parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004073{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004074 const char *end;
4075 long x;
4076 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004077 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004078 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004079
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004080 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004081 errno = 0;
4082 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004083 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004084 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004085 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004086 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004087 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004088 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004089 }
4090 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004091 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004092 if (*end == '\0') {
4093 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004094 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004095 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004096 }
4097 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004098 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004099 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004100 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4101 if (compl.imag == -1.0 && PyErr_Occurred())
4102 return NULL;
4103 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004104 }
4105 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004106 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004107 dx = PyOS_string_to_double(s, NULL, NULL);
4108 if (dx == -1.0 && PyErr_Occurred())
4109 return NULL;
4110 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004111 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004112}
4113
4114static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004115parsenumber(struct compiling *c, const char *s)
4116{
4117 char *dup, *end;
4118 PyObject *res = NULL;
4119
4120 assert(s != NULL);
4121
4122 if (strchr(s, '_') == NULL) {
4123 return parsenumber_raw(c, s);
4124 }
4125 /* Create a duplicate without underscores. */
4126 dup = PyMem_Malloc(strlen(s) + 1);
4127 end = dup;
4128 for (; *s; s++) {
4129 if (*s != '_') {
4130 *end++ = *s;
4131 }
4132 }
4133 *end = '\0';
4134 res = parsenumber_raw(c, dup);
4135 PyMem_Free(dup);
4136 return res;
4137}
4138
4139static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004140decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004141{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004142 const char *s, *t;
4143 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004144 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4145 while (s < end && (*s & 0x80)) s++;
4146 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004147 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004148}
4149
Eric V. Smith56466482016-10-31 14:46:26 -04004150static int
4151warn_invalid_escape_sequence(struct compiling *c, const node *n,
4152 char first_invalid_escape_char)
4153{
4154 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4155 first_invalid_escape_char);
4156 if (msg == NULL) {
4157 return -1;
4158 }
4159 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
4160 c->c_filename, LINENO(n),
4161 NULL, NULL) < 0 &&
4162 PyErr_ExceptionMatches(PyExc_DeprecationWarning))
4163 {
Victor Stinnerf9cca362016-11-15 09:12:10 +01004164 const char *s;
4165
4166 /* Replace the DeprecationWarning exception with a SyntaxError
4167 to get a more accurate error report */
4168 PyErr_Clear();
4169
4170 s = PyUnicode_AsUTF8(msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004171 if (s != NULL) {
4172 ast_error(c, n, s);
4173 }
4174 Py_DECREF(msg);
4175 return -1;
4176 }
4177 Py_DECREF(msg);
4178 return 0;
4179}
4180
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004181static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004182decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4183 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004184{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004185 PyObject *v, *u;
4186 char *buf;
4187 char *p;
4188 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004189
Benjamin Peterson202803a2016-02-25 22:34:45 -08004190 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004191 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004192 return NULL;
4193 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4194 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4195 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4196 if (u == NULL)
4197 return NULL;
4198 p = buf = PyBytes_AsString(u);
4199 end = s + len;
4200 while (s < end) {
4201 if (*s == '\\') {
4202 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004203 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004204 strcpy(p, "u005c");
4205 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004206 if (s >= end)
4207 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004208 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004209 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004210 if (*s & 0x80) { /* XXX inefficient */
4211 PyObject *w;
4212 int kind;
4213 void *data;
4214 Py_ssize_t len, i;
4215 w = decode_utf8(c, &s, end);
4216 if (w == NULL) {
4217 Py_DECREF(u);
4218 return NULL;
4219 }
4220 kind = PyUnicode_KIND(w);
4221 data = PyUnicode_DATA(w);
4222 len = PyUnicode_GET_LENGTH(w);
4223 for (i = 0; i < len; i++) {
4224 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4225 sprintf(p, "\\U%08x", chr);
4226 p += 10;
4227 }
4228 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004229 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004230 Py_DECREF(w);
4231 } else {
4232 *p++ = *s++;
4233 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004234 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004235 len = p - buf;
4236 s = buf;
4237
Eric V. Smith56466482016-10-31 14:46:26 -04004238 const char *first_invalid_escape;
4239 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4240
4241 if (v != NULL && first_invalid_escape != NULL) {
4242 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4243 /* We have not decref u before because first_invalid_escape points
4244 inside u. */
4245 Py_XDECREF(u);
4246 Py_DECREF(v);
4247 return NULL;
4248 }
4249 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004250 Py_XDECREF(u);
4251 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004252}
4253
Eric V. Smith56466482016-10-31 14:46:26 -04004254static PyObject *
4255decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4256 size_t len)
4257{
4258 const char *first_invalid_escape;
4259 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4260 &first_invalid_escape);
4261 if (result == NULL)
4262 return NULL;
4263
4264 if (first_invalid_escape != NULL) {
4265 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4266 Py_DECREF(result);
4267 return NULL;
4268 }
4269 }
4270 return result;
4271}
4272
Eric V. Smith451d0e32016-09-09 21:56:20 -04004273/* Compile this expression in to an expr_ty. Add parens around the
4274 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004275static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004276fstring_compile_expr(const char *expr_start, const char *expr_end,
4277 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004278
Eric V. Smith235a6f02015-09-19 14:51:32 -04004279{
4280 PyCompilerFlags cf;
4281 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004282 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004283 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004284 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004285
Eric V. Smith1d44c412015-09-23 07:49:00 -04004286 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004287 assert(*(expr_start-1) == '{');
4288 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004289
Eric V. Smith451d0e32016-09-09 21:56:20 -04004290 /* If the substring is all whitespace, it's an error. We need to catch
4291 this here, and not when we call PyParser_ASTFromString, because turning
4292 the expression '' in to '()' would go from being invalid to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004293 for (s = expr_start; s != expr_end; s++) {
4294 char c = *s;
4295 /* The Python parser ignores only the following whitespace
4296 characters (\r already is converted to \n). */
4297 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004298 break;
4299 }
4300 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004301 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004302 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004303 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004304 }
4305
Eric V. Smith451d0e32016-09-09 21:56:20 -04004306 len = expr_end - expr_start;
4307 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4308 str = PyMem_RawMalloc(len + 3);
4309 if (str == NULL)
4310 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004311
Eric V. Smith451d0e32016-09-09 21:56:20 -04004312 str[0] = '(';
4313 memcpy(str+1, expr_start, len);
4314 str[len+1] = ')';
4315 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004316
4317 cf.cf_flags = PyCF_ONLY_AST;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004318 mod = PyParser_ASTFromString(str, "<fstring>",
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004319 Py_eval_input, &cf, c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004320 PyMem_RawFree(str);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004321 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004322 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004323 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004324}
4325
4326/* Return -1 on error.
4327
4328 Return 0 if we reached the end of the literal.
4329
4330 Return 1 if we haven't reached the end of the literal, but we want
4331 the caller to process the literal up to this point. Used for
4332 doubled braces.
4333*/
4334static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004335fstring_find_literal(const char **str, const char *end, int raw,
4336 PyObject **literal, int recurse_lvl,
4337 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004338{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004339 /* Get any literal string. It ends when we hit an un-doubled left
4340 brace (which isn't part of a unicode name escape such as
4341 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004342
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004343 const char *s = *str;
4344 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004345 int result = 0;
4346
Eric V. Smith235a6f02015-09-19 14:51:32 -04004347 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004348 while (s < end) {
4349 char ch = *s++;
4350 if (!raw && ch == '\\' && s < end) {
4351 ch = *s++;
4352 if (ch == 'N') {
4353 if (s < end && *s++ == '{') {
4354 while (s < end && *s++ != '}') {
4355 }
4356 continue;
4357 }
4358 break;
4359 }
4360 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4361 return -1;
4362 }
4363 }
4364 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004365 /* Check for doubled braces, but only at the top level. If
4366 we checked at every level, then f'{0:{3}}' would fail
4367 with the two closing braces. */
4368 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004369 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004370 /* We're going to tell the caller that the literal ends
4371 here, but that they should continue scanning. But also
4372 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004373 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004374 result = 1;
4375 goto done;
4376 }
4377
4378 /* Where a single '{' is the start of a new expression, a
4379 single '}' is not allowed. */
4380 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004381 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004382 ast_error(c, n, "f-string: single '}' is not allowed");
4383 return -1;
4384 }
4385 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004386 /* We're either at a '{', which means we're starting another
4387 expression; or a '}', which means we're at the end of this
4388 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004389 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004390 break;
4391 }
4392 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004393 *str = s;
4394 assert(s <= end);
4395 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004396done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004397 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004398 if (raw)
4399 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004400 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004401 NULL, NULL);
4402 else
Eric V. Smith56466482016-10-31 14:46:26 -04004403 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004404 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004405 if (!*literal)
4406 return -1;
4407 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004408 return result;
4409}
4410
4411/* Forward declaration because parsing is recursive. */
4412static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004413fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004414 struct compiling *c, const node *n);
4415
Eric V. Smith451d0e32016-09-09 21:56:20 -04004416/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004417 expression (so it must be a '{'). Returns the FormattedValue node,
4418 which includes the expression, conversion character, and
4419 format_spec expression.
4420
4421 Note that I don't do a perfect job here: I don't make sure that a
4422 closing brace doesn't match an opening paren, for example. It
4423 doesn't need to error on all invalid expressions, just correctly
4424 find the end of all valid ones. Any errors inside the expression
4425 will be caught when we parse it later. */
4426static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004427fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004428 expr_ty *expression, struct compiling *c, const node *n)
4429{
4430 /* Return -1 on error, else 0. */
4431
Eric V. Smith451d0e32016-09-09 21:56:20 -04004432 const char *expr_start;
4433 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004434 expr_ty simple_expression;
4435 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004436 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004437
4438 /* 0 if we're not in a string, else the quote char we're trying to
4439 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004440 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004441
4442 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4443 int string_type = 0;
4444
4445 /* Keep track of nesting level for braces/parens/brackets in
4446 expressions. */
4447 Py_ssize_t nested_depth = 0;
4448
4449 /* Can only nest one level deep. */
4450 if (recurse_lvl >= 2) {
4451 ast_error(c, n, "f-string: expressions nested too deeply");
4452 return -1;
4453 }
4454
4455 /* The first char must be a left brace, or we wouldn't have gotten
4456 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004457 assert(**str == '{');
4458 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004459
Eric V. Smith451d0e32016-09-09 21:56:20 -04004460 expr_start = *str;
4461 for (; *str < end; (*str)++) {
4462 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004463
4464 /* Loop invariants. */
4465 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004466 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004467 if (quote_char)
4468 assert(string_type == 1 || string_type == 3);
4469 else
4470 assert(string_type == 0);
4471
Eric V. Smith451d0e32016-09-09 21:56:20 -04004472 ch = **str;
4473 /* Nowhere inside an expression is a backslash allowed. */
4474 if (ch == '\\') {
4475 /* Error: can't include a backslash character, inside
4476 parens or strings or not. */
4477 ast_error(c, n, "f-string expression part "
4478 "cannot include a backslash");
4479 return -1;
4480 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004481 if (quote_char) {
4482 /* We're inside a string. See if we're at the end. */
4483 /* This code needs to implement the same non-error logic
4484 as tok_get from tokenizer.c, at the letter_quote
4485 label. To actually share that code would be a
4486 nightmare. But, it's unlikely to change and is small,
4487 so duplicate it here. Note we don't need to catch all
4488 of the errors, since they'll be caught when parsing the
4489 expression. We just need to match the non-error
4490 cases. Thus we can ignore \n in single-quoted strings,
4491 for example. Or non-terminated strings. */
4492 if (ch == quote_char) {
4493 /* Does this match the string_type (single or triple
4494 quoted)? */
4495 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004496 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004497 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004498 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004499 string_type = 0;
4500 quote_char = 0;
4501 continue;
4502 }
4503 } else {
4504 /* We're at the end of a normal string. */
4505 quote_char = 0;
4506 string_type = 0;
4507 continue;
4508 }
4509 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004510 } else if (ch == '\'' || ch == '"') {
4511 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004512 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004513 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004514 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004515 } else {
4516 /* Start of a normal string. */
4517 string_type = 1;
4518 }
4519 /* Start looking for the end of the string. */
4520 quote_char = ch;
4521 } else if (ch == '[' || ch == '{' || ch == '(') {
4522 nested_depth++;
4523 } else if (nested_depth != 0 &&
4524 (ch == ']' || ch == '}' || ch == ')')) {
4525 nested_depth--;
4526 } else if (ch == '#') {
4527 /* Error: can't include a comment character, inside parens
4528 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004529 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004530 return -1;
4531 } else if (nested_depth == 0 &&
4532 (ch == '!' || ch == ':' || ch == '}')) {
4533 /* First, test for the special case of "!=". Since '=' is
4534 not an allowed conversion character, nothing is lost in
4535 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004536 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004537 /* This isn't a conversion character, just continue. */
4538 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004539 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004540 /* Normal way out of this loop. */
4541 break;
4542 } else {
4543 /* Just consume this char and loop around. */
4544 }
4545 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004546 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004547 /* If we leave this loop in a string or with mismatched parens, we
4548 don't care. We'll get a syntax error when compiling the
4549 expression. But, we can produce a better error message, so
4550 let's just do that.*/
4551 if (quote_char) {
4552 ast_error(c, n, "f-string: unterminated string");
4553 return -1;
4554 }
4555 if (nested_depth) {
4556 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4557 return -1;
4558 }
4559
Eric V. Smith451d0e32016-09-09 21:56:20 -04004560 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004561 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004562
4563 /* Compile the expression as soon as possible, so we show errors
4564 related to the expression before errors related to the
4565 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004566 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004567 if (!simple_expression)
4568 return -1;
4569
4570 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004571 if (**str == '!') {
4572 *str += 1;
4573 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004574 goto unexpected_end_of_string;
4575
Eric V. Smith451d0e32016-09-09 21:56:20 -04004576 conversion = **str;
4577 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004578
4579 /* Validate the conversion. */
4580 if (!(conversion == 's' || conversion == 'r'
4581 || conversion == 'a')) {
4582 ast_error(c, n, "f-string: invalid conversion character: "
4583 "expected 's', 'r', or 'a'");
4584 return -1;
4585 }
4586 }
4587
4588 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004589 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004590 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004591 if (**str == ':') {
4592 *str += 1;
4593 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004594 goto unexpected_end_of_string;
4595
4596 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004597 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004598 if (!format_spec)
4599 return -1;
4600 }
4601
Eric V. Smith451d0e32016-09-09 21:56:20 -04004602 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004603 goto unexpected_end_of_string;
4604
4605 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004606 assert(*str < end);
4607 assert(**str == '}');
4608 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004609
Eric V. Smith451d0e32016-09-09 21:56:20 -04004610 /* And now create the FormattedValue node that represents this
4611 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004612 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004613 format_spec, LINENO(n), n->n_col_offset,
4614 c->c_arena);
4615 if (!*expression)
4616 return -1;
4617
4618 return 0;
4619
4620unexpected_end_of_string:
4621 ast_error(c, n, "f-string: expecting '}'");
4622 return -1;
4623}
4624
4625/* Return -1 on error.
4626
4627 Return 0 if we have a literal (possible zero length) and an
4628 expression (zero length if at the end of the string.
4629
4630 Return 1 if we have a literal, but no expression, and we want the
4631 caller to call us again. This is used to deal with doubled
4632 braces.
4633
4634 When called multiple times on the string 'a{{b{0}c', this function
4635 will return:
4636
4637 1. the literal 'a{' with no expression, and a return value
4638 of 1. Despite the fact that there's no expression, the return
4639 value of 1 means we're not finished yet.
4640
4641 2. the literal 'b' and the expression '0', with a return value of
4642 0. The fact that there's an expression means we're not finished.
4643
4644 3. literal 'c' with no expression and a return value of 0. The
4645 combination of the return value of 0 with no expression means
4646 we're finished.
4647*/
4648static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004649fstring_find_literal_and_expr(const char **str, const char *end, int raw,
4650 int recurse_lvl, PyObject **literal,
4651 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004652 struct compiling *c, const node *n)
4653{
4654 int result;
4655
4656 assert(*literal == NULL && *expression == NULL);
4657
4658 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004659 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004660 if (result < 0)
4661 goto error;
4662
4663 assert(result == 0 || result == 1);
4664
4665 if (result == 1)
4666 /* We have a literal, but don't look at the expression. */
4667 return 1;
4668
Eric V. Smith451d0e32016-09-09 21:56:20 -04004669 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004670 /* We're at the end of the string or the end of a nested
4671 f-string: no expression. The top-level error case where we
4672 expect to be at the end of the string but we're at a '}' is
4673 handled later. */
4674 return 0;
4675
4676 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004677 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004678
Eric V. Smith451d0e32016-09-09 21:56:20 -04004679 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004680 goto error;
4681
4682 return 0;
4683
4684error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004685 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004686 return -1;
4687}
4688
4689#define EXPRLIST_N_CACHED 64
4690
4691typedef struct {
4692 /* Incrementally build an array of expr_ty, so be used in an
4693 asdl_seq. Cache some small but reasonably sized number of
4694 expr_ty's, and then after that start dynamically allocating,
4695 doubling the number allocated each time. Note that the f-string
4696 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
4697 Str for the literal 'a'. So you add expr_ty's about twice as
4698 fast as you add exressions in an f-string. */
4699
4700 Py_ssize_t allocated; /* Number we've allocated. */
4701 Py_ssize_t size; /* Number we've used. */
4702 expr_ty *p; /* Pointer to the memory we're actually
4703 using. Will point to 'data' until we
4704 start dynamically allocating. */
4705 expr_ty data[EXPRLIST_N_CACHED];
4706} ExprList;
4707
4708#ifdef NDEBUG
4709#define ExprList_check_invariants(l)
4710#else
4711static void
4712ExprList_check_invariants(ExprList *l)
4713{
4714 /* Check our invariants. Make sure this object is "live", and
4715 hasn't been deallocated. */
4716 assert(l->size >= 0);
4717 assert(l->p != NULL);
4718 if (l->size <= EXPRLIST_N_CACHED)
4719 assert(l->data == l->p);
4720}
4721#endif
4722
4723static void
4724ExprList_Init(ExprList *l)
4725{
4726 l->allocated = EXPRLIST_N_CACHED;
4727 l->size = 0;
4728
4729 /* Until we start allocating dynamically, p points to data. */
4730 l->p = l->data;
4731
4732 ExprList_check_invariants(l);
4733}
4734
4735static int
4736ExprList_Append(ExprList *l, expr_ty exp)
4737{
4738 ExprList_check_invariants(l);
4739 if (l->size >= l->allocated) {
4740 /* We need to alloc (or realloc) the memory. */
4741 Py_ssize_t new_size = l->allocated * 2;
4742
4743 /* See if we've ever allocated anything dynamically. */
4744 if (l->p == l->data) {
4745 Py_ssize_t i;
4746 /* We're still using the cached data. Switch to
4747 alloc-ing. */
4748 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4749 if (!l->p)
4750 return -1;
4751 /* Copy the cached data into the new buffer. */
4752 for (i = 0; i < l->size; i++)
4753 l->p[i] = l->data[i];
4754 } else {
4755 /* Just realloc. */
4756 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4757 if (!tmp) {
4758 PyMem_RawFree(l->p);
4759 l->p = NULL;
4760 return -1;
4761 }
4762 l->p = tmp;
4763 }
4764
4765 l->allocated = new_size;
4766 assert(l->allocated == 2 * l->size);
4767 }
4768
4769 l->p[l->size++] = exp;
4770
4771 ExprList_check_invariants(l);
4772 return 0;
4773}
4774
4775static void
4776ExprList_Dealloc(ExprList *l)
4777{
4778 ExprList_check_invariants(l);
4779
4780 /* If there's been an error, or we've never dynamically allocated,
4781 do nothing. */
4782 if (!l->p || l->p == l->data) {
4783 /* Do nothing. */
4784 } else {
4785 /* We have dynamically allocated. Free the memory. */
4786 PyMem_RawFree(l->p);
4787 }
4788 l->p = NULL;
4789 l->size = -1;
4790}
4791
4792static asdl_seq *
4793ExprList_Finish(ExprList *l, PyArena *arena)
4794{
4795 asdl_seq *seq;
4796
4797 ExprList_check_invariants(l);
4798
4799 /* Allocate the asdl_seq and copy the expressions in to it. */
4800 seq = _Py_asdl_seq_new(l->size, arena);
4801 if (seq) {
4802 Py_ssize_t i;
4803 for (i = 0; i < l->size; i++)
4804 asdl_seq_SET(seq, i, l->p[i]);
4805 }
4806 ExprList_Dealloc(l);
4807 return seq;
4808}
4809
4810/* The FstringParser is designed to add a mix of strings and
4811 f-strings, and concat them together as needed. Ultimately, it
4812 generates an expr_ty. */
4813typedef struct {
4814 PyObject *last_str;
4815 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004816 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004817} FstringParser;
4818
4819#ifdef NDEBUG
4820#define FstringParser_check_invariants(state)
4821#else
4822static void
4823FstringParser_check_invariants(FstringParser *state)
4824{
4825 if (state->last_str)
4826 assert(PyUnicode_CheckExact(state->last_str));
4827 ExprList_check_invariants(&state->expr_list);
4828}
4829#endif
4830
4831static void
4832FstringParser_Init(FstringParser *state)
4833{
4834 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004835 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004836 ExprList_Init(&state->expr_list);
4837 FstringParser_check_invariants(state);
4838}
4839
4840static void
4841FstringParser_Dealloc(FstringParser *state)
4842{
4843 FstringParser_check_invariants(state);
4844
4845 Py_XDECREF(state->last_str);
4846 ExprList_Dealloc(&state->expr_list);
4847}
4848
4849/* Make a Str node, but decref the PyUnicode object being added. */
4850static expr_ty
4851make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4852{
4853 PyObject *s = *str;
4854 *str = NULL;
4855 assert(PyUnicode_CheckExact(s));
4856 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4857 Py_DECREF(s);
4858 return NULL;
4859 }
4860 return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
4861}
4862
4863/* Add a non-f-string (that is, a regular literal string). str is
4864 decref'd. */
4865static int
4866FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4867{
4868 FstringParser_check_invariants(state);
4869
4870 assert(PyUnicode_CheckExact(str));
4871
4872 if (PyUnicode_GET_LENGTH(str) == 0) {
4873 Py_DECREF(str);
4874 return 0;
4875 }
4876
4877 if (!state->last_str) {
4878 /* We didn't have a string before, so just remember this one. */
4879 state->last_str = str;
4880 } else {
4881 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004882 PyUnicode_AppendAndDel(&state->last_str, str);
4883 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004884 return -1;
4885 }
4886 FstringParser_check_invariants(state);
4887 return 0;
4888}
4889
Eric V. Smith451d0e32016-09-09 21:56:20 -04004890/* Parse an f-string. The f-string is in *str to end, with no
4891 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004892static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004893FstringParser_ConcatFstring(FstringParser *state, const char **str,
4894 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004895 struct compiling *c, const node *n)
4896{
4897 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004898 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004899
4900 /* Parse the f-string. */
4901 while (1) {
4902 PyObject *literal = NULL;
4903 expr_ty expression = NULL;
4904
4905 /* If there's a zero length literal in front of the
4906 expression, literal will be NULL. If we're at the end of
4907 the f-string, expression will be NULL (unless result == 1,
4908 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004909 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004910 &literal, &expression,
4911 c, n);
4912 if (result < 0)
4913 return -1;
4914
4915 /* Add the literal, if any. */
4916 if (!literal) {
4917 /* Do nothing. Just leave last_str alone (and possibly
4918 NULL). */
4919 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04004920 /* Note that the literal can be zero length, if the
4921 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004922 state->last_str = literal;
4923 literal = NULL;
4924 } else {
4925 /* We have a literal, concatenate it. */
4926 assert(PyUnicode_GET_LENGTH(literal) != 0);
4927 if (FstringParser_ConcatAndDel(state, literal) < 0)
4928 return -1;
4929 literal = NULL;
4930 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004931
4932 /* We've dealt with the literal now. It can't be leaked on further
4933 errors. */
4934 assert(literal == NULL);
4935
4936 /* See if we should just loop around to get the next literal
4937 and expression, while ignoring the expression this
4938 time. This is used for un-doubling braces, as an
4939 optimization. */
4940 if (result == 1)
4941 continue;
4942
4943 if (!expression)
4944 /* We're done with this f-string. */
4945 break;
4946
4947 /* We know we have an expression. Convert any existing string
4948 to a Str node. */
4949 if (!state->last_str) {
4950 /* Do nothing. No previous literal. */
4951 } else {
4952 /* Convert the existing last_str literal to a Str node. */
4953 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4954 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4955 return -1;
4956 }
4957
4958 if (ExprList_Append(&state->expr_list, expression) < 0)
4959 return -1;
4960 }
4961
Eric V. Smith235a6f02015-09-19 14:51:32 -04004962 /* If recurse_lvl is zero, then we must be at the end of the
4963 string. Otherwise, we must be at a right brace. */
4964
Eric V. Smith451d0e32016-09-09 21:56:20 -04004965 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004966 ast_error(c, n, "f-string: unexpected end of string");
4967 return -1;
4968 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004969 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004970 ast_error(c, n, "f-string: expecting '}'");
4971 return -1;
4972 }
4973
4974 FstringParser_check_invariants(state);
4975 return 0;
4976}
4977
4978/* Convert the partial state reflected in last_str and expr_list to an
4979 expr_ty. The expr_ty can be a Str, or a JoinedStr. */
4980static expr_ty
4981FstringParser_Finish(FstringParser *state, struct compiling *c,
4982 const node *n)
4983{
4984 asdl_seq *seq;
4985
4986 FstringParser_check_invariants(state);
4987
4988 /* If we're just a constant string with no expressions, return
4989 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004990 if (!state->fmode) {
4991 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004992 if (!state->last_str) {
4993 /* Create a zero length string. */
4994 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
4995 if (!state->last_str)
4996 goto error;
4997 }
4998 return make_str_node_and_del(&state->last_str, c, n);
4999 }
5000
5001 /* Create a Str node out of last_str, if needed. It will be the
5002 last node in our expression list. */
5003 if (state->last_str) {
5004 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5005 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5006 goto error;
5007 }
5008 /* This has already been freed. */
5009 assert(state->last_str == NULL);
5010
5011 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5012 if (!seq)
5013 goto error;
5014
Eric V. Smith235a6f02015-09-19 14:51:32 -04005015 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
5016
5017error:
5018 FstringParser_Dealloc(state);
5019 return NULL;
5020}
5021
Eric V. Smith451d0e32016-09-09 21:56:20 -04005022/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5023 at end, parse it into an expr_ty. Return NULL on error. Adjust
5024 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005025static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005026fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005027 struct compiling *c, const node *n)
5028{
5029 FstringParser state;
5030
5031 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005032 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005033 c, n) < 0) {
5034 FstringParser_Dealloc(&state);
5035 return NULL;
5036 }
5037
5038 return FstringParser_Finish(&state, c, n);
5039}
5040
5041/* n is a Python string literal, including the bracketing quote
5042 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005043 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005044 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005045 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5046 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005047*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005048static int
5049parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5050 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005051{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005052 size_t len;
5053 const char *s = STR(n);
5054 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005055 int fmode = 0;
5056 *bytesmode = 0;
5057 *rawmode = 0;
5058 *result = NULL;
5059 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005060 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005061 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005062 if (quote == 'b' || quote == 'B') {
5063 quote = *++s;
5064 *bytesmode = 1;
5065 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005066 else if (quote == 'u' || quote == 'U') {
5067 quote = *++s;
5068 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005069 else if (quote == 'r' || quote == 'R') {
5070 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005071 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005072 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005073 else if (quote == 'f' || quote == 'F') {
5074 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005075 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005076 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005077 else {
5078 break;
5079 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005080 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005081 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005082 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005083 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005084 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005085 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005086 if (quote != '\'' && quote != '\"') {
5087 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005088 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005089 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005090 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005091 s++;
5092 len = strlen(s);
5093 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005094 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005095 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005096 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005097 }
5098 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005099 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005100 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005101 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005102 }
5103 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005104 /* A triple quoted string. We've already skipped one quote at
5105 the start and one at the end of the string. Now skip the
5106 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005107 s += 2;
5108 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005109 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005110 if (s[--len] != quote || s[--len] != quote) {
5111 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005112 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005113 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005114 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005115
Eric V. Smith451d0e32016-09-09 21:56:20 -04005116 if (fmode) {
5117 /* Just return the bytes. The caller will parse the resulting
5118 string. */
5119 *fstr = s;
5120 *fstrlen = len;
5121 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005122 }
5123
Eric V. Smith451d0e32016-09-09 21:56:20 -04005124 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005125 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005126 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005127 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005128 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005129 const char *ch;
5130 for (ch = s; *ch; ch++) {
5131 if (Py_CHARMASK(*ch) >= 0x80) {
5132 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005133 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005134 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005135 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005136 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005137 if (*rawmode)
5138 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005139 else
Eric V. Smith56466482016-10-31 14:46:26 -04005140 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005141 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005142 if (*rawmode)
5143 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005144 else
Eric V. Smith56466482016-10-31 14:46:26 -04005145 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005146 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005147 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005148}
5149
Eric V. Smith235a6f02015-09-19 14:51:32 -04005150/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5151 each STRING atom, and process it as needed. For bytes, just
5152 concatenate them together, and the result will be a Bytes node. For
5153 normal strings and f-strings, concatenate them together. The result
5154 will be a Str node if there were no f-strings; a FormattedValue
5155 node if there's just an f-string (with no leading or trailing
5156 literals), or a JoinedStr node if there are multiple f-strings or
5157 any literals involved. */
5158static expr_ty
5159parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005160{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005161 int bytesmode = 0;
5162 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005163 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005164
5165 FstringParser state;
5166 FstringParser_Init(&state);
5167
5168 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005169 int this_bytesmode;
5170 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005171 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005172 const char *fstr;
5173 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005174
5175 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005176 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5177 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005178 goto error;
5179
5180 /* Check that we're not mixing bytes with unicode. */
5181 if (i != 0 && bytesmode != this_bytesmode) {
5182 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005183 /* s is NULL if the current string part is an f-string. */
5184 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005185 goto error;
5186 }
5187 bytesmode = this_bytesmode;
5188
Eric V. Smith451d0e32016-09-09 21:56:20 -04005189 if (fstr != NULL) {
5190 int result;
5191 assert(s == NULL && !bytesmode);
5192 /* This is an f-string. Parse and concatenate it. */
5193 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5194 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005195 if (result < 0)
5196 goto error;
5197 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005198 /* A string or byte string. */
5199 assert(s != NULL && fstr == NULL);
5200
Eric V. Smith451d0e32016-09-09 21:56:20 -04005201 assert(bytesmode ? PyBytes_CheckExact(s) :
5202 PyUnicode_CheckExact(s));
5203
Eric V. Smith451d0e32016-09-09 21:56:20 -04005204 if (bytesmode) {
5205 /* For bytes, concat as we go. */
5206 if (i == 0) {
5207 /* First time, just remember this value. */
5208 bytes_str = s;
5209 } else {
5210 PyBytes_ConcatAndDel(&bytes_str, s);
5211 if (!bytes_str)
5212 goto error;
5213 }
5214 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005215 /* This is a regular string. Concatenate it. */
5216 if (FstringParser_ConcatAndDel(&state, s) < 0)
5217 goto error;
5218 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005219 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005220 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005221 if (bytesmode) {
5222 /* Just return the bytes object and we're done. */
5223 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5224 goto error;
5225 return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
5226 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005227
Eric V. Smith235a6f02015-09-19 14:51:32 -04005228 /* We're not a bytes string, bytes_str should never have been set. */
5229 assert(bytes_str == NULL);
5230
5231 return FstringParser_Finish(&state, c, n);
5232
5233error:
5234 Py_XDECREF(bytes_str);
5235 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005236 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005237}