blob: 7d30e5f864ff81713b834cc92bdfbabb0e98cbc3 [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"
Ɓukasz Langae7c566c2017-09-06 17:27:58 -070011#include "pythonrun.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000012
13#include <assert.h>
14
Benjamin Peterson832bfe22011-08-09 16:15:04 -050015static int validate_stmts(asdl_seq *);
16static int validate_exprs(asdl_seq *, expr_context_ty, int);
17static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
18static int validate_stmt(stmt_ty);
19static int validate_expr(expr_ty, expr_context_ty);
20
21static int
22validate_comprehension(asdl_seq *gens)
23{
24 int i;
25 if (!asdl_seq_LEN(gens)) {
26 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
27 return 0;
28 }
29 for (i = 0; i < asdl_seq_LEN(gens); i++) {
30 comprehension_ty comp = asdl_seq_GET(gens, i);
31 if (!validate_expr(comp->target, Store) ||
32 !validate_expr(comp->iter, Load) ||
33 !validate_exprs(comp->ifs, Load, 0))
34 return 0;
35 }
36 return 1;
37}
38
39static int
40validate_slice(slice_ty slice)
41{
42 switch (slice->kind) {
43 case Slice_kind:
44 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
45 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
46 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
47 case ExtSlice_kind: {
48 int i;
49 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
50 return 0;
51 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
52 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
53 return 0;
54 return 1;
55 }
56 case Index_kind:
57 return validate_expr(slice->v.Index.value, Load);
58 default:
59 PyErr_SetString(PyExc_SystemError, "unknown slice node");
60 return 0;
61 }
62}
63
64static int
65validate_keywords(asdl_seq *keywords)
66{
67 int i;
68 for (i = 0; i < asdl_seq_LEN(keywords); i++)
69 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
70 return 0;
71 return 1;
72}
73
74static int
75validate_args(asdl_seq *args)
76{
77 int i;
78 for (i = 0; i < asdl_seq_LEN(args); i++) {
79 arg_ty arg = asdl_seq_GET(args, i);
80 if (arg->annotation && !validate_expr(arg->annotation, Load))
81 return 0;
82 }
83 return 1;
84}
85
86static const char *
87expr_context_name(expr_context_ty ctx)
88{
89 switch (ctx) {
90 case Load:
91 return "Load";
92 case Store:
93 return "Store";
94 case Del:
95 return "Del";
96 case AugLoad:
97 return "AugLoad";
98 case AugStore:
99 return "AugStore";
100 case Param:
101 return "Param";
102 default:
103 assert(0);
104 return "(unknown)";
105 }
106}
107
108static int
109validate_arguments(arguments_ty args)
110{
111 if (!validate_args(args->args))
112 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700113 if (args->vararg && args->vararg->annotation
114 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500115 return 0;
116 }
117 if (!validate_args(args->kwonlyargs))
118 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100119 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700120 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500121 return 0;
122 }
123 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
124 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
125 return 0;
126 }
127 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
128 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
129 "kw_defaults on arguments");
130 return 0;
131 }
132 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
133}
134
135static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100136validate_constant(PyObject *value)
137{
138 if (value == Py_None || value == Py_Ellipsis)
139 return 1;
140
141 if (PyLong_CheckExact(value)
142 || PyFloat_CheckExact(value)
143 || PyComplex_CheckExact(value)
144 || PyBool_Check(value)
145 || PyUnicode_CheckExact(value)
146 || PyBytes_CheckExact(value))
147 return 1;
148
149 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
150 PyObject *it;
151
152 it = PyObject_GetIter(value);
153 if (it == NULL)
154 return 0;
155
156 while (1) {
157 PyObject *item = PyIter_Next(it);
158 if (item == NULL) {
159 if (PyErr_Occurred()) {
160 Py_DECREF(it);
161 return 0;
162 }
163 break;
164 }
165
166 if (!validate_constant(item)) {
167 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100168 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100169 return 0;
170 }
Victor Stinner726f6902016-01-27 00:11:47 +0100171 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100172 }
173
174 Py_DECREF(it);
175 return 1;
176 }
177
178 return 0;
179}
180
181static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500182validate_expr(expr_ty exp, expr_context_ty ctx)
183{
184 int check_ctx = 1;
185 expr_context_ty actual_ctx;
186
187 /* First check expression context. */
188 switch (exp->kind) {
189 case Attribute_kind:
190 actual_ctx = exp->v.Attribute.ctx;
191 break;
192 case Subscript_kind:
193 actual_ctx = exp->v.Subscript.ctx;
194 break;
195 case Starred_kind:
196 actual_ctx = exp->v.Starred.ctx;
197 break;
198 case Name_kind:
199 actual_ctx = exp->v.Name.ctx;
200 break;
201 case List_kind:
202 actual_ctx = exp->v.List.ctx;
203 break;
204 case Tuple_kind:
205 actual_ctx = exp->v.Tuple.ctx;
206 break;
207 default:
208 if (ctx != Load) {
209 PyErr_Format(PyExc_ValueError, "expression which can't be "
210 "assigned to in %s context", expr_context_name(ctx));
211 return 0;
212 }
213 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100214 /* set actual_ctx to prevent gcc warning */
215 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500216 }
217 if (check_ctx && actual_ctx != ctx) {
218 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
219 expr_context_name(ctx), expr_context_name(actual_ctx));
220 return 0;
221 }
222
223 /* Now validate expression. */
224 switch (exp->kind) {
225 case BoolOp_kind:
226 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
227 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
228 return 0;
229 }
230 return validate_exprs(exp->v.BoolOp.values, Load, 0);
231 case BinOp_kind:
232 return validate_expr(exp->v.BinOp.left, Load) &&
233 validate_expr(exp->v.BinOp.right, Load);
234 case UnaryOp_kind:
235 return validate_expr(exp->v.UnaryOp.operand, Load);
236 case Lambda_kind:
237 return validate_arguments(exp->v.Lambda.args) &&
238 validate_expr(exp->v.Lambda.body, Load);
239 case IfExp_kind:
240 return validate_expr(exp->v.IfExp.test, Load) &&
241 validate_expr(exp->v.IfExp.body, Load) &&
242 validate_expr(exp->v.IfExp.orelse, Load);
243 case Dict_kind:
244 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
245 PyErr_SetString(PyExc_ValueError,
246 "Dict doesn't have the same number of keys as values");
247 return 0;
248 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400249 /* null_ok=1 for keys expressions to allow dict unpacking to work in
250 dict literals, i.e. ``{**{a:b}}`` */
251 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
252 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500253 case Set_kind:
254 return validate_exprs(exp->v.Set.elts, Load, 0);
255#define COMP(NAME) \
256 case NAME ## _kind: \
257 return validate_comprehension(exp->v.NAME.generators) && \
258 validate_expr(exp->v.NAME.elt, Load);
259 COMP(ListComp)
260 COMP(SetComp)
261 COMP(GeneratorExp)
262#undef COMP
263 case DictComp_kind:
264 return validate_comprehension(exp->v.DictComp.generators) &&
265 validate_expr(exp->v.DictComp.key, Load) &&
266 validate_expr(exp->v.DictComp.value, Load);
267 case Yield_kind:
268 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500269 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000270 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400271 case Await_kind:
272 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500273 case Compare_kind:
274 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
275 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
276 return 0;
277 }
278 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
279 asdl_seq_LEN(exp->v.Compare.ops)) {
280 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
281 "of comparators and operands");
282 return 0;
283 }
284 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
285 validate_expr(exp->v.Compare.left, Load);
286 case Call_kind:
287 return validate_expr(exp->v.Call.func, Load) &&
288 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400289 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100290 case Constant_kind:
291 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100292 PyErr_Format(PyExc_TypeError,
293 "got an invalid type in Constant: %s",
294 Py_TYPE(exp->v.Constant.value)->tp_name);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100295 return 0;
296 }
297 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500298 case Num_kind: {
299 PyObject *n = exp->v.Num.n;
300 if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) &&
301 !PyComplex_CheckExact(n)) {
302 PyErr_SetString(PyExc_TypeError, "non-numeric type in Num");
303 return 0;
304 }
305 return 1;
306 }
307 case Str_kind: {
308 PyObject *s = exp->v.Str.s;
309 if (!PyUnicode_CheckExact(s)) {
310 PyErr_SetString(PyExc_TypeError, "non-string type in Str");
311 return 0;
312 }
313 return 1;
314 }
Eric V. Smith235a6f02015-09-19 14:51:32 -0400315 case JoinedStr_kind:
316 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
317 case FormattedValue_kind:
318 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
319 return 0;
320 if (exp->v.FormattedValue.format_spec)
321 return validate_expr(exp->v.FormattedValue.format_spec, Load);
322 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500323 case Bytes_kind: {
324 PyObject *b = exp->v.Bytes.s;
325 if (!PyBytes_CheckExact(b)) {
326 PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes");
327 return 0;
328 }
329 return 1;
330 }
331 case Attribute_kind:
332 return validate_expr(exp->v.Attribute.value, Load);
333 case Subscript_kind:
334 return validate_slice(exp->v.Subscript.slice) &&
335 validate_expr(exp->v.Subscript.value, Load);
336 case Starred_kind:
337 return validate_expr(exp->v.Starred.value, ctx);
338 case List_kind:
339 return validate_exprs(exp->v.List.elts, ctx, 0);
340 case Tuple_kind:
341 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
342 /* These last cases don't have any checking. */
343 case Name_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -0500344 case NameConstant_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500345 case Ellipsis_kind:
346 return 1;
347 default:
348 PyErr_SetString(PyExc_SystemError, "unexpected expression");
349 return 0;
350 }
351}
352
353static int
354validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
355{
356 if (asdl_seq_LEN(seq))
357 return 1;
358 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
359 return 0;
360}
361
362static int
363validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
364{
365 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
366 validate_exprs(targets, ctx, 0);
367}
368
369static int
INADA Naokicb41b272017-02-23 00:31:59 +0900370validate_body(asdl_seq *body, const char *owner, int allowempty)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500371{
INADA Naokicb41b272017-02-23 00:31:59 +0900372 if (!allowempty && !validate_nonempty_seq(body, "body", owner)) {
373 return 0;
374 }
375 return validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500376}
377
378static int
379validate_stmt(stmt_ty stmt)
380{
381 int i;
382 switch (stmt->kind) {
383 case FunctionDef_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900384 return validate_body(stmt->v.FunctionDef.body, "FunctionDef",
385 stmt->v.FunctionDef.docstring != NULL) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500386 validate_arguments(stmt->v.FunctionDef.args) &&
387 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
388 (!stmt->v.FunctionDef.returns ||
389 validate_expr(stmt->v.FunctionDef.returns, Load));
390 case ClassDef_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900391 return validate_body(stmt->v.ClassDef.body, "ClassDef",
392 stmt->v.ClassDef.docstring != NULL) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500393 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
394 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400395 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500396 case Return_kind:
397 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
398 case Delete_kind:
399 return validate_assignlist(stmt->v.Delete.targets, Del);
400 case Assign_kind:
401 return validate_assignlist(stmt->v.Assign.targets, Store) &&
402 validate_expr(stmt->v.Assign.value, Load);
403 case AugAssign_kind:
404 return validate_expr(stmt->v.AugAssign.target, Store) &&
405 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700406 case AnnAssign_kind:
407 if (stmt->v.AnnAssign.target->kind != Name_kind &&
408 stmt->v.AnnAssign.simple) {
409 PyErr_SetString(PyExc_TypeError,
410 "AnnAssign with simple non-Name target");
411 return 0;
412 }
413 return validate_expr(stmt->v.AnnAssign.target, Store) &&
414 (!stmt->v.AnnAssign.value ||
415 validate_expr(stmt->v.AnnAssign.value, Load)) &&
416 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500417 case For_kind:
418 return validate_expr(stmt->v.For.target, Store) &&
419 validate_expr(stmt->v.For.iter, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900420 validate_body(stmt->v.For.body, "For", 0) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500421 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400422 case AsyncFor_kind:
423 return validate_expr(stmt->v.AsyncFor.target, Store) &&
424 validate_expr(stmt->v.AsyncFor.iter, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900425 validate_body(stmt->v.AsyncFor.body, "AsyncFor", 0) &&
Yury Selivanov75445082015-05-11 22:57:16 -0400426 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500427 case While_kind:
428 return validate_expr(stmt->v.While.test, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900429 validate_body(stmt->v.While.body, "While", 0) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500430 validate_stmts(stmt->v.While.orelse);
431 case If_kind:
432 return validate_expr(stmt->v.If.test, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900433 validate_body(stmt->v.If.body, "If", 0) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500434 validate_stmts(stmt->v.If.orelse);
435 case With_kind:
436 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
437 return 0;
438 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
439 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
440 if (!validate_expr(item->context_expr, Load) ||
441 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
442 return 0;
443 }
INADA Naokicb41b272017-02-23 00:31:59 +0900444 return validate_body(stmt->v.With.body, "With", 0);
Yury Selivanov75445082015-05-11 22:57:16 -0400445 case AsyncWith_kind:
446 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
447 return 0;
448 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
449 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
450 if (!validate_expr(item->context_expr, Load) ||
451 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
452 return 0;
453 }
INADA Naokicb41b272017-02-23 00:31:59 +0900454 return validate_body(stmt->v.AsyncWith.body, "AsyncWith", 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500455 case Raise_kind:
456 if (stmt->v.Raise.exc) {
457 return validate_expr(stmt->v.Raise.exc, Load) &&
458 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
459 }
460 if (stmt->v.Raise.cause) {
461 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
462 return 0;
463 }
464 return 1;
465 case Try_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900466 if (!validate_body(stmt->v.Try.body, "Try", 0))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500467 return 0;
468 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
469 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
470 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
471 return 0;
472 }
473 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
474 asdl_seq_LEN(stmt->v.Try.orelse)) {
475 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
476 return 0;
477 }
478 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
479 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
480 if ((handler->v.ExceptHandler.type &&
481 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
INADA Naokicb41b272017-02-23 00:31:59 +0900482 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler", 0))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500483 return 0;
484 }
485 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
486 validate_stmts(stmt->v.Try.finalbody)) &&
487 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
488 validate_stmts(stmt->v.Try.orelse));
489 case Assert_kind:
490 return validate_expr(stmt->v.Assert.test, Load) &&
491 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
492 case Import_kind:
493 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
494 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300495 if (stmt->v.ImportFrom.level < 0) {
496 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500497 return 0;
498 }
499 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
500 case Global_kind:
501 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
502 case Nonlocal_kind:
503 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
504 case Expr_kind:
505 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400506 case AsyncFunctionDef_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900507 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef",
508 stmt->v.AsyncFunctionDef.docstring != NULL) &&
Yury Selivanov75445082015-05-11 22:57:16 -0400509 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
510 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
511 (!stmt->v.AsyncFunctionDef.returns ||
512 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500513 case Pass_kind:
514 case Break_kind:
515 case Continue_kind:
516 return 1;
517 default:
518 PyErr_SetString(PyExc_SystemError, "unexpected statement");
519 return 0;
520 }
521}
522
523static int
524validate_stmts(asdl_seq *seq)
525{
526 int i;
527 for (i = 0; i < asdl_seq_LEN(seq); i++) {
528 stmt_ty stmt = asdl_seq_GET(seq, i);
529 if (stmt) {
530 if (!validate_stmt(stmt))
531 return 0;
532 }
533 else {
534 PyErr_SetString(PyExc_ValueError,
535 "None disallowed in statement list");
536 return 0;
537 }
538 }
539 return 1;
540}
541
542static int
543validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
544{
545 int i;
546 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
547 expr_ty expr = asdl_seq_GET(exprs, i);
548 if (expr) {
549 if (!validate_expr(expr, ctx))
550 return 0;
551 }
552 else if (!null_ok) {
553 PyErr_SetString(PyExc_ValueError,
554 "None disallowed in expression list");
555 return 0;
556 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100557
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500558 }
559 return 1;
560}
561
562int
563PyAST_Validate(mod_ty mod)
564{
565 int res = 0;
566
567 switch (mod->kind) {
568 case Module_kind:
569 res = validate_stmts(mod->v.Module.body);
570 break;
571 case Interactive_kind:
572 res = validate_stmts(mod->v.Interactive.body);
573 break;
574 case Expression_kind:
575 res = validate_expr(mod->v.Expression.body, Load);
576 break;
577 case Suite_kind:
578 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
579 break;
580 default:
581 PyErr_SetString(PyExc_SystemError, "impossible module node");
582 res = 0;
583 break;
584 }
585 return res;
586}
587
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500588/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500589#include "grammar.h"
590#include "parsetok.h"
591#include "graminit.h"
592
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593/* Data structure used internally */
594struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400595 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200596 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500597 PyObject *c_normalize; /* Normalization function from unicodedata. */
598 PyObject *c_normalize_args; /* Normalization argument tuple. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599};
600
601static asdl_seq *seq_for_testlist(struct compiling *, const node *);
602static expr_ty ast_for_expr(struct compiling *, const node *);
603static stmt_ty ast_for_stmt(struct compiling *, const node *);
INADA Naokicb41b272017-02-23 00:31:59 +0900604static asdl_seq *ast_for_body(struct compiling *c, const node *n,
605 string *docstring);
606static string docstring_from_stmts(asdl_seq *stmts);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000607static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
608 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000609static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000610static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000611
Yury Selivanov75445082015-05-11 22:57:16 -0400612static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
613static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
614
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615/* Note different signature for ast_for_call */
616static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
617
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000618static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400619static expr_ty parsestrplus(struct compiling *, const node *n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000620
Nick Coghlan650f0d02007-04-15 12:05:43 +0000621#define COMP_GENEXP 0
622#define COMP_LISTCOMP 1
623#define COMP_SETCOMP 2
624
Benjamin Peterson55e00432012-01-16 17:22:31 -0500625static int
626init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000627{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500628 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
629 if (!m)
630 return 0;
631 c->c_normalize = PyObject_GetAttrString(m, "normalize");
632 Py_DECREF(m);
633 if (!c->c_normalize)
634 return 0;
635 c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500636 if (!c->c_normalize_args) {
637 Py_CLEAR(c->c_normalize);
638 return 0;
639 }
Christian Heimes72f562f2013-07-24 21:02:17 +0200640 PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500641 return 1;
642}
643
644static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400645new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500646{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400647 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500648 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000649 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500650 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500651 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000652 /* Check whether there are non-ASCII characters in the
653 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500654 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200655 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500656 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500657 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200658 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500659 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500660 PyTuple_SET_ITEM(c->c_normalize_args, 1, id);
661 id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500662 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200663 if (!id2)
664 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200665 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000666 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000667 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200668 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
669 Py_DECREF(id);
670 return NULL;
671 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000672 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000673}
674
Benjamin Peterson55e00432012-01-16 17:22:31 -0500675#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400678ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000679{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400680 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000681
Victor Stinner14e461d2013-08-26 22:28:21 +0200682 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000683 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000684 Py_INCREF(Py_None);
685 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000686 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200687 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400688 if (!tmp)
689 return 0;
690 errstr = PyUnicode_FromString(errmsg);
691 if (!errstr) {
692 Py_DECREF(tmp);
693 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000694 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000695 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000696 Py_DECREF(errstr);
697 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400698 if (value) {
699 PyErr_SetObject(PyExc_SyntaxError, value);
700 Py_DECREF(value);
701 }
702 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703}
704
705/* num_stmts() returns number of contained statements.
706
707 Use this routine to determine how big a sequence is needed for
708 the statements in a parse tree. Its raison d'etre is this bit of
709 grammar:
710
711 stmt: simple_stmt | compound_stmt
712 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
713
714 A simple_stmt can contain multiple small_stmt elements joined
715 by semicolons. If the arg is a simple_stmt, the number of
716 small_stmt elements is returned.
717*/
718
719static int
720num_stmts(const node *n)
721{
722 int i, l;
723 node *ch;
724
725 switch (TYPE(n)) {
726 case single_input:
727 if (TYPE(CHILD(n, 0)) == NEWLINE)
728 return 0;
729 else
730 return num_stmts(CHILD(n, 0));
731 case file_input:
732 l = 0;
733 for (i = 0; i < NCH(n); i++) {
734 ch = CHILD(n, i);
735 if (TYPE(ch) == stmt)
736 l += num_stmts(ch);
737 }
738 return l;
739 case stmt:
740 return num_stmts(CHILD(n, 0));
741 case compound_stmt:
742 return 1;
743 case simple_stmt:
744 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
745 case suite:
746 if (NCH(n) == 1)
747 return num_stmts(CHILD(n, 0));
748 else {
749 l = 0;
750 for (i = 2; i < (NCH(n) - 1); i++)
751 l += num_stmts(CHILD(n, i));
752 return l;
753 }
754 default: {
755 char buf[128];
756
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000757 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758 TYPE(n), NCH(n));
759 Py_FatalError(buf);
760 }
761 }
762 assert(0);
763 return 0;
764}
765
766/* Transform the CST rooted at node * to the appropriate AST
767*/
768
769mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200770PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
771 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000773 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774 asdl_seq *stmts = NULL;
775 stmt_ty s;
776 node *ch;
777 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500778 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400780 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200781 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400782 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800783 c.c_normalize = NULL;
784 c.c_normalize_args = NULL;
785
786 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788
Jeremy Hyltona8293132006-02-28 17:58:27 +0000789 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790 switch (TYPE(n)) {
791 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200792 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500794 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795 for (i = 0; i < NCH(n) - 1; i++) {
796 ch = CHILD(n, i);
797 if (TYPE(ch) == NEWLINE)
798 continue;
799 REQ(ch, stmt);
800 num = num_stmts(ch);
801 if (num == 1) {
802 s = ast_for_stmt(&c, ch);
803 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500804 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000805 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806 }
807 else {
808 ch = CHILD(ch, 0);
809 REQ(ch, simple_stmt);
810 for (j = 0; j < num; j++) {
811 s = ast_for_stmt(&c, CHILD(ch, j * 2));
812 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500813 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000814 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815 }
816 }
817 }
INADA Naokicb41b272017-02-23 00:31:59 +0900818 res = Module(stmts, docstring_from_stmts(stmts), arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500819 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 case eval_input: {
821 expr_ty testlist_ast;
822
Nick Coghlan650f0d02007-04-15 12:05:43 +0000823 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000824 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500826 goto out;
827 res = Expression(testlist_ast, arena);
828 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829 }
830 case single_input:
831 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200832 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500834 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000835 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
836 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000837 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500838 goto out;
839 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840 }
841 else {
842 n = CHILD(n, 0);
843 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200844 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500846 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000847 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000848 s = ast_for_stmt(&c, n);
849 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500850 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000851 asdl_seq_SET(stmts, 0, s);
852 }
853 else {
854 /* Only a simple_stmt can contain multiple statements. */
855 REQ(n, simple_stmt);
856 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857 if (TYPE(CHILD(n, i)) == NEWLINE)
858 break;
859 s = ast_for_stmt(&c, CHILD(n, i));
860 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500861 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862 asdl_seq_SET(stmts, i / 2, s);
863 }
864 }
865
Benjamin Peterson55e00432012-01-16 17:22:31 -0500866 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500868 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000870 PyErr_Format(PyExc_SystemError,
871 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500872 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500874 out:
875 if (c.c_normalize) {
876 Py_DECREF(c.c_normalize);
877 PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL);
878 Py_DECREF(c.c_normalize_args);
879 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500880 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000881}
882
Victor Stinner14e461d2013-08-26 22:28:21 +0200883mod_ty
884PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
885 PyArena *arena)
886{
887 mod_ty mod;
888 PyObject *filename;
889 filename = PyUnicode_DecodeFSDefault(filename_str);
890 if (filename == NULL)
891 return NULL;
892 mod = PyAST_FromNodeObject(n, flags, filename, arena);
893 Py_DECREF(filename);
894 return mod;
895
896}
897
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000898/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
899*/
900
901static operator_ty
902get_operator(const node *n)
903{
904 switch (TYPE(n)) {
905 case VBAR:
906 return BitOr;
907 case CIRCUMFLEX:
908 return BitXor;
909 case AMPER:
910 return BitAnd;
911 case LEFTSHIFT:
912 return LShift;
913 case RIGHTSHIFT:
914 return RShift;
915 case PLUS:
916 return Add;
917 case MINUS:
918 return Sub;
919 case STAR:
920 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400921 case AT:
922 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000923 case SLASH:
924 return Div;
925 case DOUBLESLASH:
926 return FloorDiv;
927 case PERCENT:
928 return Mod;
929 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000930 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000931 }
932}
933
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200934static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000935 "None",
936 "True",
937 "False",
938 NULL,
939};
940
941static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400942forbidden_name(struct compiling *c, identifier name, const node *n,
943 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000944{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000945 assert(PyUnicode_Check(name));
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200946 if (_PyUnicode_EqualToASCIIString(name, "__debug__")) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400947 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000948 return 1;
949 }
Serhiy Storchaka3b73ea12016-11-16 10:19:20 +0200950 if (_PyUnicode_EqualToASCIIString(name, "async") ||
951 _PyUnicode_EqualToASCIIString(name, "await"))
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400952 {
953 PyObject *message = PyUnicode_FromString(
954 "'async' and 'await' will become reserved keywords"
955 " in Python 3.7");
Yury Selivanov1a9d6872016-11-08 16:54:18 -0500956 int ret;
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400957 if (message == NULL) {
958 return 1;
959 }
Yury Selivanov1a9d6872016-11-08 16:54:18 -0500960 ret = PyErr_WarnExplicitObject(
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400961 PyExc_DeprecationWarning,
962 message,
963 c->c_filename,
964 LINENO(n),
965 NULL,
Yury Selivanov1a9d6872016-11-08 16:54:18 -0500966 NULL);
967 Py_DECREF(message);
968 if (ret < 0) {
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400969 return 1;
970 }
971 }
Benjamin Peterson70f52762009-06-28 23:32:44 +0000972 if (full_checks) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200973 const char * const *p;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000974 for (p = FORBIDDEN; *p; p++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200975 if (_PyUnicode_EqualToASCIIString(name, *p)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400976 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000977 return 1;
978 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000979 }
980 }
981 return 0;
982}
983
Jeremy Hyltona8293132006-02-28 17:58:27 +0000984/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985
986 Only sets context for expr kinds that "can appear in assignment context"
987 (according to ../Parser/Python.asdl). For other expr kinds, it sets
988 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989*/
990
991static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000992set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993{
994 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000995 /* If a particular expression type can't be used for assign / delete,
996 set expr_name to its name and an error message will be generated.
997 */
998 const char* expr_name = NULL;
999
1000 /* The ast defines augmented store and load contexts, but the
1001 implementation here doesn't actually use them. The code may be
1002 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001003 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001004 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +00001005 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001006 */
1007 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008
1009 switch (e->kind) {
1010 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001011 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001012 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001013 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001014 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001016 e->v.Subscript.ctx = ctx;
1017 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001018 case Starred_kind:
1019 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001020 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001021 return 0;
1022 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001023 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +00001024 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001025 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001026 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001027 }
1028 e->v.Name.ctx = ctx;
1029 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001031 e->v.List.ctx = ctx;
1032 s = e->v.List.elts;
1033 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001035 e->v.Tuple.ctx = ctx;
1036 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001037 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001038 case Lambda_kind:
1039 expr_name = "lambda";
1040 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001042 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001043 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001044 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001045 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001046 case UnaryOp_kind:
1047 expr_name = "operator";
1048 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001049 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001050 expr_name = "generator expression";
1051 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001052 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001053 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001054 expr_name = "yield expression";
1055 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001056 case Await_kind:
1057 expr_name = "await expression";
1058 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001059 case ListComp_kind:
1060 expr_name = "list comprehension";
1061 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001062 case SetComp_kind:
1063 expr_name = "set comprehension";
1064 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001065 case DictComp_kind:
1066 expr_name = "dict comprehension";
1067 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001068 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +00001069 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001070 case Num_kind:
1071 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -05001072 case Bytes_kind:
Eric V. Smith235a6f02015-09-19 14:51:32 -04001073 case JoinedStr_kind:
1074 case FormattedValue_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001075 expr_name = "literal";
1076 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001077 case NameConstant_kind:
1078 expr_name = "keyword";
1079 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001080 case Ellipsis_kind:
1081 expr_name = "Ellipsis";
1082 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001083 case Compare_kind:
1084 expr_name = "comparison";
1085 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001086 case IfExp_kind:
1087 expr_name = "conditional expression";
1088 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001089 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 PyErr_Format(PyExc_SystemError,
1091 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001092 e->kind, e->lineno);
1093 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001094 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001095 /* Check for error string set by switch */
1096 if (expr_name) {
1097 char buf[300];
1098 PyOS_snprintf(buf, sizeof(buf),
1099 "can't %s %s",
1100 ctx == Store ? "assign to" : "delete",
1101 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001102 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001103 }
1104
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001105 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107 */
1108 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001109 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110
Thomas Wouters89f507f2006-12-13 04:49:30 +00001111 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001112 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001113 return 0;
1114 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001115 }
1116 return 1;
1117}
1118
1119static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001120ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121{
1122 REQ(n, augassign);
1123 n = CHILD(n, 0);
1124 switch (STR(n)[0]) {
1125 case '+':
1126 return Add;
1127 case '-':
1128 return Sub;
1129 case '/':
1130 if (STR(n)[1] == '/')
1131 return FloorDiv;
1132 else
1133 return Div;
1134 case '%':
1135 return Mod;
1136 case '<':
1137 return LShift;
1138 case '>':
1139 return RShift;
1140 case '&':
1141 return BitAnd;
1142 case '^':
1143 return BitXor;
1144 case '|':
1145 return BitOr;
1146 case '*':
1147 if (STR(n)[1] == '*')
1148 return Pow;
1149 else
1150 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001151 case '@':
1152 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001154 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001155 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156 }
1157}
1158
1159static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001160ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001162 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 |'is' 'not'
1164 */
1165 REQ(n, comp_op);
1166 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001167 n = CHILD(n, 0);
1168 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169 case LESS:
1170 return Lt;
1171 case GREATER:
1172 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001173 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174 return Eq;
1175 case LESSEQUAL:
1176 return LtE;
1177 case GREATEREQUAL:
1178 return GtE;
1179 case NOTEQUAL:
1180 return NotEq;
1181 case NAME:
1182 if (strcmp(STR(n), "in") == 0)
1183 return In;
1184 if (strcmp(STR(n), "is") == 0)
1185 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001186 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001188 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001190 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001191 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192 }
1193 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001194 /* handle "not in" and "is not" */
1195 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196 case NAME:
1197 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1198 return NotIn;
1199 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1200 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001201 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001203 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001205 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001206 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207 }
Neal Norwitz79792652005-11-14 04:25:03 +00001208 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001210 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211}
1212
1213static asdl_seq *
1214seq_for_testlist(struct compiling *c, const node *n)
1215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001217 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1218 */
Armin Rigo31441302005-10-21 12:57:31 +00001219 asdl_seq *seq;
1220 expr_ty expression;
1221 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001222 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001224 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225 if (!seq)
1226 return NULL;
1227
1228 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001230 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001231
Benjamin Peterson4905e802009-09-27 02:43:28 +00001232 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001233 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235
1236 assert(i / 2 < seq->size);
1237 asdl_seq_SET(seq, i / 2, expression);
1238 }
1239 return seq;
1240}
1241
Neal Norwitzc1505362006-12-28 06:47:50 +00001242static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001243ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001244{
1245 identifier name;
1246 expr_ty annotation = NULL;
1247 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001248 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001249
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001250 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001251 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001252 name = NEW_IDENTIFIER(ch);
1253 if (!name)
1254 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001255 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001256 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001257
1258 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1259 annotation = ast_for_expr(c, CHILD(n, 2));
1260 if (!annotation)
1261 return NULL;
1262 }
1263
Victor Stinnerc106c682015-11-06 17:01:48 +01001264 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001265 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001266 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001267 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268}
1269
Guido van Rossum4f72a782006-10-27 23:31:49 +00001270/* returns -1 if failed to handle keyword only arguments
1271 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001272 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001273 ^^^
1274 start pointing here
1275 */
1276static int
1277handle_keywordonly_args(struct compiling *c, const node *n, int start,
1278 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1279{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001280 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001281 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001282 expr_ty expression, annotation;
1283 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001284 int i = start;
1285 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001286
1287 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001288 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001289 return -1;
1290 }
1291 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001292 while (i < NCH(n)) {
1293 ch = CHILD(n, i);
1294 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001295 case vfpdef:
1296 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001297 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001298 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001299 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001300 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001301 asdl_seq_SET(kwdefaults, j, expression);
1302 i += 2; /* '=' and test */
1303 }
1304 else { /* setting NULL if no default value exists */
1305 asdl_seq_SET(kwdefaults, j, NULL);
1306 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001307 if (NCH(ch) == 3) {
1308 /* ch is NAME ':' test */
1309 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001310 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001311 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001312 }
1313 else {
1314 annotation = NULL;
1315 }
1316 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001317 argname = NEW_IDENTIFIER(ch);
1318 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001319 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001320 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001321 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001322 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1323 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001324 if (!arg)
1325 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001326 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001327 i += 2; /* the name and the comma */
1328 break;
1329 case DOUBLESTAR:
1330 return i;
1331 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001332 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001333 goto error;
1334 }
1335 }
1336 return i;
1337 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001339}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001340
Jeremy Hyltona8293132006-02-28 17:58:27 +00001341/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342
1343static arguments_ty
1344ast_for_arguments(struct compiling *c, const node *n)
1345{
Neal Norwitzc1505362006-12-28 06:47:50 +00001346 /* This function handles both typedargslist (function definition)
1347 and varargslist (lambda definition).
1348
1349 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001350 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1351 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1352 | '**' tfpdef [',']]]
1353 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1354 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001355 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001356 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1357 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1358 | '**' vfpdef [',']]]
1359 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1360 | '**' vfpdef [',']
1361 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001362 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001363
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001365 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1366 int nposdefaults = 0, found_default = 0;
1367 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001368 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001369 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370 node *ch;
1371
1372 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001373 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001374 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001375 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001376 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001377 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001378
Jeremy Hyltone921e022008-07-17 16:37:17 +00001379 /* First count the number of positional args & defaults. The
1380 variable i is the loop index for this for loop and the next.
1381 The next loop picks up where the first leaves off.
1382 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001383 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001384 ch = CHILD(n, i);
1385 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001386 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001387 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001388 if (i < NCH(n) && /* skip argument following star */
1389 (TYPE(CHILD(n, i)) == tfpdef ||
1390 TYPE(CHILD(n, i)) == vfpdef)) {
1391 i++;
1392 }
1393 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001394 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001395 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001396 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001397 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001400 defaults for keyword only args */
1401 for ( ; i < NCH(n); ++i) {
1402 ch = CHILD(n, i);
1403 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001404 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001405 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001406 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001407 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001408 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001409 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001410 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001411 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001412 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001414 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001415 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001416 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001418 since we set NULL as default for keyword only argument w/o default
1419 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001420 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001421 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001422 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001423 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001424
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001425 /* tfpdef: NAME [':' test]
1426 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427 */
1428 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001429 j = 0; /* index for defaults */
1430 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001432 ch = CHILD(n, i);
1433 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001434 case tfpdef:
1435 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1437 anything other than EQUAL or a comma? */
1438 /* XXX Should NCH(n) check be made a separate check? */
1439 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001440 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1441 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001442 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001443 assert(posdefaults != NULL);
1444 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001446 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001448 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001449 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001450 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001451 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001452 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001453 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001454 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001455 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001456 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457 i += 2; /* the name and the comma */
1458 break;
1459 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001460 if (i+1 >= NCH(n) ||
1461 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001462 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001463 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001464 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001465 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001466 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001467 if (TYPE(ch) == COMMA) {
1468 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001469 i += 2; /* now follows keyword only arguments */
1470 res = handle_keywordonly_args(c, n, i,
1471 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001472 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001473 i = res; /* res has new position to process */
1474 }
1475 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001476 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001477 if (!vararg)
1478 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001479
Guido van Rossum4f72a782006-10-27 23:31:49 +00001480 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001481 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1482 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001483 int res = 0;
1484 res = handle_keywordonly_args(c, n, i,
1485 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001486 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001487 i = res; /* res has new position to process */
1488 }
1489 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490 break;
1491 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001492 ch = CHILD(n, i+1); /* tfpdef */
1493 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001494 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001495 if (!kwarg)
1496 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497 i += 3;
1498 break;
1499 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001500 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501 "unexpected node in varargslist: %d @ %d",
1502 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001503 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001504 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001506 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507}
1508
1509static expr_ty
1510ast_for_dotted_name(struct compiling *c, const node *n)
1511{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001512 expr_ty e;
1513 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001514 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001515 int i;
1516
1517 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001518
1519 lineno = LINENO(n);
1520 col_offset = n->n_col_offset;
1521
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522 id = NEW_IDENTIFIER(CHILD(n, 0));
1523 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001524 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001525 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001526 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001527 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528
1529 for (i = 2; i < NCH(n); i+=2) {
1530 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001531 if (!id)
1532 return NULL;
1533 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1534 if (!e)
1535 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536 }
1537
1538 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539}
1540
1541static expr_ty
1542ast_for_decorator(struct compiling *c, const node *n)
1543{
1544 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1545 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001546 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001549 REQ(CHILD(n, 0), AT);
1550 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001552 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1553 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001554 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001556 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001557 d = name_expr;
1558 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559 }
1560 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001561 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001562 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001563 if (!d)
1564 return NULL;
1565 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566 }
1567 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001568 d = ast_for_call(c, CHILD(n, 3), name_expr);
1569 if (!d)
1570 return NULL;
1571 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001572 }
1573
1574 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575}
1576
1577static asdl_seq*
1578ast_for_decorators(struct compiling *c, const node *n)
1579{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001580 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001581 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001582 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001584 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001585 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586 if (!decorator_seq)
1587 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001590 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001591 if (!d)
1592 return NULL;
1593 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594 }
1595 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596}
1597
1598static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001599ast_for_funcdef_impl(struct compiling *c, const node *n,
1600 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001601{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001602 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001603 identifier name;
1604 arguments_ty args;
1605 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001606 expr_ty returns = NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09001607 string docstring;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001608 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001609
1610 REQ(n, funcdef);
1611
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001612 name = NEW_IDENTIFIER(CHILD(n, name_i));
1613 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001614 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001615 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001616 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1618 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001619 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001620 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1621 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1622 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001623 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001624 name_i += 2;
1625 }
INADA Naokicb41b272017-02-23 00:31:59 +09001626 body = ast_for_body(c, CHILD(n, name_i + 3), &docstring);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001627 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001628 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001629
Yury Selivanov75445082015-05-11 22:57:16 -04001630 if (is_async)
1631 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
INADA Naokicb41b272017-02-23 00:31:59 +09001632 docstring, LINENO(n),
Yury Selivanov75445082015-05-11 22:57:16 -04001633 n->n_col_offset, c->c_arena);
1634 else
1635 return FunctionDef(name, args, body, decorator_seq, returns,
INADA Naokicb41b272017-02-23 00:31:59 +09001636 docstring, LINENO(n),
Yury Selivanov75445082015-05-11 22:57:16 -04001637 n->n_col_offset, c->c_arena);
1638}
1639
1640static stmt_ty
1641ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1642{
1643 /* async_funcdef: ASYNC funcdef */
1644 REQ(n, async_funcdef);
1645 REQ(CHILD(n, 0), ASYNC);
1646 REQ(CHILD(n, 1), funcdef);
1647
1648 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1649 1 /* is_async */);
1650}
1651
1652static stmt_ty
1653ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1654{
1655 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1656 return ast_for_funcdef_impl(c, n, decorator_seq,
1657 0 /* is_async */);
1658}
1659
1660
1661static stmt_ty
1662ast_for_async_stmt(struct compiling *c, const node *n)
1663{
1664 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
1665 REQ(n, async_stmt);
1666 REQ(CHILD(n, 0), ASYNC);
1667
1668 switch (TYPE(CHILD(n, 1))) {
1669 case funcdef:
1670 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1671 1 /* is_async */);
1672 case with_stmt:
1673 return ast_for_with_stmt(c, CHILD(n, 1),
1674 1 /* is_async */);
1675
1676 case for_stmt:
1677 return ast_for_for_stmt(c, CHILD(n, 1),
1678 1 /* is_async */);
1679
1680 default:
1681 PyErr_Format(PyExc_SystemError,
1682 "invalid async stament: %s",
1683 STR(CHILD(n, 1)));
1684 return NULL;
1685 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686}
1687
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001688static stmt_ty
1689ast_for_decorated(struct compiling *c, const node *n)
1690{
Yury Selivanov75445082015-05-11 22:57:16 -04001691 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001692 stmt_ty thing = NULL;
1693 asdl_seq *decorator_seq = NULL;
1694
1695 REQ(n, decorated);
1696
1697 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1698 if (!decorator_seq)
1699 return NULL;
1700
1701 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001702 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001704
1705 if (TYPE(CHILD(n, 1)) == funcdef) {
1706 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1707 } else if (TYPE(CHILD(n, 1)) == classdef) {
1708 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001709 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1710 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001711 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001712 /* we count the decorators in when talking about the class' or
1713 * function's line number */
1714 if (thing) {
1715 thing->lineno = LINENO(n);
1716 thing->col_offset = n->n_col_offset;
1717 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001718 return thing;
1719}
1720
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001721static expr_ty
1722ast_for_lambdef(struct compiling *c, const node *n)
1723{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001724 /* lambdef: 'lambda' [varargslist] ':' test
1725 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726 arguments_ty args;
1727 expr_ty expression;
1728
1729 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001730 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001731 if (!args)
1732 return NULL;
1733 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001734 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001736 }
1737 else {
1738 args = ast_for_arguments(c, CHILD(n, 1));
1739 if (!args)
1740 return NULL;
1741 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001742 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744 }
1745
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001746 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001747}
1748
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001749static expr_ty
1750ast_for_ifexpr(struct compiling *c, const node *n)
1751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001753 expr_ty expression, body, orelse;
1754
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001755 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001756 body = ast_for_expr(c, CHILD(n, 0));
1757 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001758 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001759 expression = ast_for_expr(c, CHILD(n, 2));
1760 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001761 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001762 orelse = ast_for_expr(c, CHILD(n, 4));
1763 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001764 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001765 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1766 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001767}
1768
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001769/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001770 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771
Nick Coghlan650f0d02007-04-15 12:05:43 +00001772 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001773*/
1774
1775static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001776count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001778 int n_fors = 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001779 int is_async;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780
Guido van Rossumd8faa362007-04-27 19:54:29 +00001781 count_comp_for:
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001782 is_async = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001783 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001784 REQ(n, comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001785 if (TYPE(CHILD(n, 0)) == ASYNC) {
1786 is_async = 1;
1787 }
1788 if (NCH(n) == (5 + is_async)) {
1789 n = CHILD(n, 4 + is_async);
1790 }
1791 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001792 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001793 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001794 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001795 REQ(n, comp_iter);
1796 n = CHILD(n, 0);
1797 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001798 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001799 else if (TYPE(n) == comp_if) {
1800 if (NCH(n) == 3) {
1801 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001802 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001803 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001804 else
1805 return n_fors;
1806 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001807
Guido van Rossumd8faa362007-04-27 19:54:29 +00001808 /* Should never be reached */
1809 PyErr_SetString(PyExc_SystemError,
1810 "logic error in count_comp_fors");
1811 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812}
1813
Nick Coghlan650f0d02007-04-15 12:05:43 +00001814/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815
Nick Coghlan650f0d02007-04-15 12:05:43 +00001816 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817*/
1818
1819static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001820count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001821{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001822 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823
Guido van Rossumd8faa362007-04-27 19:54:29 +00001824 while (1) {
1825 REQ(n, comp_iter);
1826 if (TYPE(CHILD(n, 0)) == comp_for)
1827 return n_ifs;
1828 n = CHILD(n, 0);
1829 REQ(n, comp_if);
1830 n_ifs++;
1831 if (NCH(n) == 2)
1832 return n_ifs;
1833 n = CHILD(n, 2);
1834 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001835}
1836
Guido van Rossum992d4a32007-07-11 13:09:30 +00001837static asdl_seq *
1838ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001839{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001840 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001841 asdl_seq *comps;
1842
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001843 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844 if (n_fors == -1)
1845 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001846
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001847 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001848 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001849 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001850
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001851 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001852 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001853 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001854 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001855 node *for_ch;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001856 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857
Guido van Rossum992d4a32007-07-11 13:09:30 +00001858 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001860 if (TYPE(CHILD(n, 0)) == ASYNC) {
1861 is_async = 1;
1862 }
1863
1864 for_ch = CHILD(n, 1 + is_async);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001865 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001866 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001867 return NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001868 expression = ast_for_expr(c, CHILD(n, 3 + is_async));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001869 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001871
Thomas Wouters89f507f2006-12-13 04:49:30 +00001872 /* Check the # of children rather than the length of t, since
1873 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001874 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001875 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001876 comp = comprehension(first, expression, NULL,
1877 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001878 else
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001879 comp = comprehension(Tuple(t, Store, first->lineno,
1880 first->col_offset, c->c_arena),
1881 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001882 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001884
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001885 if (NCH(n) == (5 + is_async)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001886 int j, n_ifs;
1887 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001889 n = CHILD(n, 4 + is_async);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001890 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001891 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001892 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001893
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001894 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001895 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001896 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001897
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001898 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001899 REQ(n, comp_iter);
1900 n = CHILD(n, 0);
1901 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902
Guido van Rossum992d4a32007-07-11 13:09:30 +00001903 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001904 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001905 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001906 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001907 if (NCH(n) == 3)
1908 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001909 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001910 /* on exit, must guarantee that n is a comp_for */
1911 if (TYPE(n) == comp_iter)
1912 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001913 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001914 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001915 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001916 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001917 return comps;
1918}
1919
1920static expr_ty
1921ast_for_itercomp(struct compiling *c, const node *n, int type)
1922{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001923 /* testlist_comp: (test|star_expr)
1924 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001925 expr_ty elt;
1926 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001927 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928
Guido van Rossum992d4a32007-07-11 13:09:30 +00001929 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001931 ch = CHILD(n, 0);
1932 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001933 if (!elt)
1934 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001935 if (elt->kind == Starred_kind) {
1936 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1937 return NULL;
1938 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939
Guido van Rossum992d4a32007-07-11 13:09:30 +00001940 comps = ast_for_comprehension(c, CHILD(n, 1));
1941 if (!comps)
1942 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001943
1944 if (type == COMP_GENEXP)
1945 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1946 else if (type == COMP_LISTCOMP)
1947 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1948 else if (type == COMP_SETCOMP)
1949 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1950 else
1951 /* Should never happen */
1952 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001953}
1954
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001955/* Fills in the key, value pair corresponding to the dict element. In case
1956 * of an unpacking, key is NULL. *i is advanced by the number of ast
1957 * elements. Iff successful, nonzero is returned.
1958 */
1959static int
1960ast_for_dictelement(struct compiling *c, const node *n, int *i,
1961 expr_ty *key, expr_ty *value)
1962{
1963 expr_ty expression;
1964 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1965 assert(NCH(n) - *i >= 2);
1966
1967 expression = ast_for_expr(c, CHILD(n, *i + 1));
1968 if (!expression)
1969 return 0;
1970 *key = NULL;
1971 *value = expression;
1972
1973 *i += 2;
1974 }
1975 else {
1976 assert(NCH(n) - *i >= 3);
1977
1978 expression = ast_for_expr(c, CHILD(n, *i));
1979 if (!expression)
1980 return 0;
1981 *key = expression;
1982
1983 REQ(CHILD(n, *i + 1), COLON);
1984
1985 expression = ast_for_expr(c, CHILD(n, *i + 2));
1986 if (!expression)
1987 return 0;
1988 *value = expression;
1989
1990 *i += 3;
1991 }
1992 return 1;
1993}
1994
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001995static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001996ast_for_dictcomp(struct compiling *c, const node *n)
1997{
1998 expr_ty key, value;
1999 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002000 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002002 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002003 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002004 assert(key);
2005 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002007 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002008 if (!comps)
2009 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010
Guido van Rossum992d4a32007-07-11 13:09:30 +00002011 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
2012}
2013
2014static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002015ast_for_dictdisplay(struct compiling *c, const node *n)
2016{
2017 int i;
2018 int j;
2019 int size;
2020 asdl_seq *keys, *values;
2021
2022 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2023 keys = _Py_asdl_seq_new(size, c->c_arena);
2024 if (!keys)
2025 return NULL;
2026
2027 values = _Py_asdl_seq_new(size, c->c_arena);
2028 if (!values)
2029 return NULL;
2030
2031 j = 0;
2032 for (i = 0; i < NCH(n); i++) {
2033 expr_ty key, value;
2034
2035 if (!ast_for_dictelement(c, n, &i, &key, &value))
2036 return NULL;
2037 asdl_seq_SET(keys, j, key);
2038 asdl_seq_SET(values, j, value);
2039
2040 j++;
2041 }
2042 keys->size = j;
2043 values->size = j;
2044 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
2045}
2046
2047static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002048ast_for_genexp(struct compiling *c, const node *n)
2049{
2050 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002051 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002052}
2053
2054static expr_ty
2055ast_for_listcomp(struct compiling *c, const node *n)
2056{
2057 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002058 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002059}
2060
2061static expr_ty
2062ast_for_setcomp(struct compiling *c, const node *n)
2063{
2064 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002065 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002066}
2067
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002068static expr_ty
2069ast_for_setdisplay(struct compiling *c, const node *n)
2070{
2071 int i;
2072 int size;
2073 asdl_seq *elts;
2074
2075 assert(TYPE(n) == (dictorsetmaker));
2076 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2077 elts = _Py_asdl_seq_new(size, c->c_arena);
2078 if (!elts)
2079 return NULL;
2080 for (i = 0; i < NCH(n); i += 2) {
2081 expr_ty expression;
2082 expression = ast_for_expr(c, CHILD(n, i));
2083 if (!expression)
2084 return NULL;
2085 asdl_seq_SET(elts, i / 2, expression);
2086 }
2087 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2088}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002089
2090static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091ast_for_atom(struct compiling *c, const node *n)
2092{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002093 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2094 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002095 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002096 */
2097 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002099 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002100 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002101 PyObject *name;
2102 const char *s = STR(ch);
2103 size_t len = strlen(s);
2104 if (len >= 4 && len <= 5) {
2105 if (!strcmp(s, "None"))
2106 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2107 if (!strcmp(s, "True"))
2108 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2109 if (!strcmp(s, "False"))
2110 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2111 }
2112 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002113 if (!name)
2114 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002115 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002116 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2117 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002119 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002120 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002121 const char *errtype = NULL;
2122 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2123 errtype = "unicode error";
2124 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2125 errtype = "value error";
2126 if (errtype) {
2127 char buf[128];
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002128 const char *s = NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002129 PyObject *type, *value, *tback, *errstr;
2130 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002131 errstr = PyObject_Str(value);
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002132 if (errstr)
2133 s = PyUnicode_AsUTF8(errstr);
2134 if (s) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002135 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002136 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002137 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002138 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002139 }
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002140 Py_XDECREF(errstr);
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002141 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002142 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002143 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002144 Py_XDECREF(tback);
2145 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002146 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002147 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002148 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002149 }
2150 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002151 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002152 if (!pynum)
2153 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002154
Victor Stinner43d81952013-07-17 00:57:58 +02002155 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2156 Py_DECREF(pynum);
2157 return NULL;
2158 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002159 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002160 }
Georg Brandldde00282007-03-18 19:01:53 +00002161 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002162 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002163 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002164 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165
Thomas Wouters89f507f2006-12-13 04:49:30 +00002166 if (TYPE(ch) == RPAR)
2167 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168
Thomas Wouters89f507f2006-12-13 04:49:30 +00002169 if (TYPE(ch) == yield_expr)
2170 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002173 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002174 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002175
Nick Coghlan650f0d02007-04-15 12:05:43 +00002176 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002177 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002178 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179
Thomas Wouters89f507f2006-12-13 04:49:30 +00002180 if (TYPE(ch) == RSQB)
2181 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182
Nick Coghlan650f0d02007-04-15 12:05:43 +00002183 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002184 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2185 asdl_seq *elts = seq_for_testlist(c, ch);
2186 if (!elts)
2187 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002188
Thomas Wouters89f507f2006-12-13 04:49:30 +00002189 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2190 }
2191 else
2192 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002194 /* dictorsetmaker: ( ((test ':' test | '**' test)
2195 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2196 * ((test | '*' test)
2197 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002198 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002199 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002200 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002201 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002202 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002203 }
2204 else {
2205 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2206 if (NCH(ch) == 1 ||
2207 (NCH(ch) > 1 &&
2208 TYPE(CHILD(ch, 1)) == COMMA)) {
2209 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002210 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002211 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002212 else if (NCH(ch) > 1 &&
2213 TYPE(CHILD(ch, 1)) == comp_for) {
2214 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002215 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002216 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002217 else if (NCH(ch) > 3 - is_dict &&
2218 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2219 /* It's a dictionary comprehension. */
2220 if (is_dict) {
2221 ast_error(c, n, "dict unpacking cannot be used in "
2222 "dict comprehension");
2223 return NULL;
2224 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002225 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002226 }
2227 else {
2228 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002229 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002230 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002231 if (res) {
2232 res->lineno = LINENO(n);
2233 res->col_offset = n->n_col_offset;
2234 }
2235 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002236 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002237 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002239 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2240 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002241 }
2242}
2243
2244static slice_ty
2245ast_for_slice(struct compiling *c, const node *n)
2246{
2247 node *ch;
2248 expr_ty lower = NULL, upper = NULL, step = NULL;
2249
2250 REQ(n, subscript);
2251
2252 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002253 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002254 sliceop: ':' [test]
2255 */
2256 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257 if (NCH(n) == 1 && TYPE(ch) == test) {
2258 /* 'step' variable hold no significance in terms of being used over
2259 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002261 if (!step)
2262 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263
Thomas Wouters89f507f2006-12-13 04:49:30 +00002264 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002265 }
2266
2267 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002268 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002269 if (!lower)
2270 return NULL;
2271 }
2272
2273 /* If there's an upper bound it's in the second or third position. */
2274 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002275 if (NCH(n) > 1) {
2276 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002277
Thomas Wouters89f507f2006-12-13 04:49:30 +00002278 if (TYPE(n2) == test) {
2279 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280 if (!upper)
2281 return NULL;
2282 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002283 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002284 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002285 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286
Thomas Wouters89f507f2006-12-13 04:49:30 +00002287 if (TYPE(n2) == test) {
2288 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289 if (!upper)
2290 return NULL;
2291 }
2292 }
2293
2294 ch = CHILD(n, NCH(n) - 1);
2295 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002296 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002297 ch = CHILD(ch, 1);
2298 if (TYPE(ch) == test) {
2299 step = ast_for_expr(c, ch);
2300 if (!step)
2301 return NULL;
2302 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002303 }
2304 }
2305
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002306 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002307}
2308
2309static expr_ty
2310ast_for_binop(struct compiling *c, const node *n)
2311{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002312 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002314 BinOp(BinOp(A, op, B), op, C).
2315 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002316
Guido van Rossumd8faa362007-04-27 19:54:29 +00002317 int i, nops;
2318 expr_ty expr1, expr2, result;
2319 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002320
Guido van Rossumd8faa362007-04-27 19:54:29 +00002321 expr1 = ast_for_expr(c, CHILD(n, 0));
2322 if (!expr1)
2323 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002324
Guido van Rossumd8faa362007-04-27 19:54:29 +00002325 expr2 = ast_for_expr(c, CHILD(n, 2));
2326 if (!expr2)
2327 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002328
Guido van Rossumd8faa362007-04-27 19:54:29 +00002329 newoperator = get_operator(CHILD(n, 1));
2330 if (!newoperator)
2331 return NULL;
2332
2333 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2334 c->c_arena);
2335 if (!result)
2336 return NULL;
2337
2338 nops = (NCH(n) - 1) / 2;
2339 for (i = 1; i < nops; i++) {
2340 expr_ty tmp_result, tmp;
2341 const node* next_oper = CHILD(n, i * 2 + 1);
2342
2343 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002344 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002345 return NULL;
2346
Guido van Rossumd8faa362007-04-27 19:54:29 +00002347 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2348 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002349 return NULL;
2350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002352 LINENO(next_oper), next_oper->n_col_offset,
2353 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002355 return NULL;
2356 result = tmp_result;
2357 }
2358 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002359}
2360
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002361static expr_ty
2362ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002365 subscriptlist: subscript (',' subscript)* [',']
2366 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2367 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002368 REQ(n, trailer);
2369 if (TYPE(CHILD(n, 0)) == LPAR) {
2370 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002371 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002372 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002373 else
2374 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002375 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002376 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002377 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2378 if (!attr_id)
2379 return NULL;
2380 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002381 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002382 }
2383 else {
2384 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002385 REQ(CHILD(n, 2), RSQB);
2386 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002387 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002388 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2389 if (!slc)
2390 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002391 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2392 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002393 }
2394 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002396 by treating the sequence as a tuple literal if there are
2397 no slice features.
2398 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002399 int j;
2400 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002401 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002402 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002403 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002404 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002405 if (!slices)
2406 return NULL;
2407 for (j = 0; j < NCH(n); j += 2) {
2408 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002409 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002410 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002411 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002412 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002413 asdl_seq_SET(slices, j / 2, slc);
2414 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002415 if (!simple) {
2416 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002417 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002418 }
2419 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002420 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002421 if (!elts)
2422 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002423 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2424 slc = (slice_ty)asdl_seq_GET(slices, j);
2425 assert(slc->kind == Index_kind && slc->v.Index.value);
2426 asdl_seq_SET(elts, j, slc->v.Index.value);
2427 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002428 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002429 if (!e)
2430 return NULL;
2431 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002432 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002433 }
2434 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002435}
2436
2437static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002438ast_for_factor(struct compiling *c, const node *n)
2439{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002440 expr_ty expression;
2441
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002442 expression = ast_for_expr(c, CHILD(n, 1));
2443 if (!expression)
2444 return NULL;
2445
2446 switch (TYPE(CHILD(n, 0))) {
2447 case PLUS:
2448 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2449 c->c_arena);
2450 case MINUS:
2451 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2452 c->c_arena);
2453 case TILDE:
2454 return UnaryOp(Invert, expression, LINENO(n),
2455 n->n_col_offset, c->c_arena);
2456 }
2457 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2458 TYPE(CHILD(n, 0)));
2459 return NULL;
2460}
2461
2462static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002463ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002464{
Yury Selivanov75445082015-05-11 22:57:16 -04002465 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002466 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002467
2468 REQ(n, atom_expr);
2469 nch = NCH(n);
2470
2471 if (TYPE(CHILD(n, 0)) == AWAIT) {
2472 start = 1;
2473 assert(nch > 1);
2474 }
2475
2476 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002477 if (!e)
2478 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002479 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002480 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002481 if (start && nch == 2) {
2482 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2483 }
2484
2485 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002486 node *ch = CHILD(n, i);
2487 if (TYPE(ch) != trailer)
2488 break;
2489 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002490 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002491 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002492 tmp->lineno = e->lineno;
2493 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002494 e = tmp;
2495 }
Yury Selivanov75445082015-05-11 22:57:16 -04002496
2497 if (start) {
2498 /* there was an AWAIT */
2499 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2500 }
2501 else {
2502 return e;
2503 }
2504}
2505
2506static expr_ty
2507ast_for_power(struct compiling *c, const node *n)
2508{
2509 /* power: atom trailer* ('**' factor)*
2510 */
2511 expr_ty e;
2512 REQ(n, power);
2513 e = ast_for_atom_expr(c, CHILD(n, 0));
2514 if (!e)
2515 return NULL;
2516 if (NCH(n) == 1)
2517 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002518 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2519 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002520 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002521 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002522 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002523 }
2524 return e;
2525}
2526
Guido van Rossum0368b722007-05-11 16:50:42 +00002527static expr_ty
2528ast_for_starred(struct compiling *c, const node *n)
2529{
2530 expr_ty tmp;
2531 REQ(n, star_expr);
2532
2533 tmp = ast_for_expr(c, CHILD(n, 1));
2534 if (!tmp)
2535 return NULL;
2536
2537 /* The Load context is changed later. */
2538 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2539}
2540
2541
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002542/* Do not name a variable 'expr'! Will cause a compile error.
2543*/
2544
2545static expr_ty
2546ast_for_expr(struct compiling *c, const node *n)
2547{
2548 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002549 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002550 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552 and_test: not_test ('and' not_test)*
2553 not_test: 'not' not_test | comparison
2554 comparison: expr (comp_op expr)*
2555 expr: xor_expr ('|' xor_expr)*
2556 xor_expr: and_expr ('^' and_expr)*
2557 and_expr: shift_expr ('&' shift_expr)*
2558 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2559 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002560 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002562 power: atom_expr ['**' factor]
2563 atom_expr: [AWAIT] atom trailer*
2564 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002565 */
2566
2567 asdl_seq *seq;
2568 int i;
2569
2570 loop:
2571 switch (TYPE(n)) {
2572 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002573 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002574 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002575 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002576 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002577 else if (NCH(n) > 1)
2578 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002579 /* Fallthrough */
2580 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581 case and_test:
2582 if (NCH(n) == 1) {
2583 n = CHILD(n, 0);
2584 goto loop;
2585 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002586 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002587 if (!seq)
2588 return NULL;
2589 for (i = 0; i < NCH(n); i += 2) {
2590 expr_ty e = ast_for_expr(c, CHILD(n, i));
2591 if (!e)
2592 return NULL;
2593 asdl_seq_SET(seq, i / 2, e);
2594 }
2595 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002596 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2597 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002598 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002599 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002600 case not_test:
2601 if (NCH(n) == 1) {
2602 n = CHILD(n, 0);
2603 goto loop;
2604 }
2605 else {
2606 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2607 if (!expression)
2608 return NULL;
2609
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002610 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2611 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612 }
2613 case comparison:
2614 if (NCH(n) == 1) {
2615 n = CHILD(n, 0);
2616 goto loop;
2617 }
2618 else {
2619 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002620 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002621 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002622 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002623 if (!ops)
2624 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002625 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002626 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002627 return NULL;
2628 }
2629 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002630 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002631
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002632 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002633 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002634 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002635 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002636
2637 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002638 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002639 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002640 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002641
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002642 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643 asdl_seq_SET(cmps, i / 2, expression);
2644 }
2645 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002646 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002647 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002648 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002650 return Compare(expression, ops, cmps, LINENO(n),
2651 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 }
2653 break;
2654
Guido van Rossum0368b722007-05-11 16:50:42 +00002655 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002657 /* The next five cases all handle BinOps. The main body of code
2658 is the same in each case, but the switch turned inside out to
2659 reuse the code for each type of operator.
2660 */
2661 case expr:
2662 case xor_expr:
2663 case and_expr:
2664 case shift_expr:
2665 case arith_expr:
2666 case term:
2667 if (NCH(n) == 1) {
2668 n = CHILD(n, 0);
2669 goto loop;
2670 }
2671 return ast_for_binop(c, n);
2672 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002673 node *an = NULL;
2674 node *en = NULL;
2675 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002676 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002677 if (NCH(n) > 1)
2678 an = CHILD(n, 1); /* yield_arg */
2679 if (an) {
2680 en = CHILD(an, NCH(an) - 1);
2681 if (NCH(an) == 2) {
2682 is_from = 1;
2683 exp = ast_for_expr(c, en);
2684 }
2685 else
2686 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002687 if (!exp)
2688 return NULL;
2689 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002690 if (is_from)
2691 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2692 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002693 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002694 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695 if (NCH(n) == 1) {
2696 n = CHILD(n, 0);
2697 goto loop;
2698 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002699 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002700 case power:
2701 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002702 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002703 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002704 return NULL;
2705 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002706 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002707 return NULL;
2708}
2709
2710static expr_ty
2711ast_for_call(struct compiling *c, const node *n, expr_ty func)
2712{
2713 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002714 arglist: argument (',' argument)* [',']
2715 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002716 */
2717
2718 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002719 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002720 asdl_seq *args;
2721 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002722
2723 REQ(n, arglist);
2724
2725 nargs = 0;
2726 nkeywords = 0;
2727 ngens = 0;
2728 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002729 node *ch = CHILD(n, i);
2730 if (TYPE(ch) == argument) {
2731 if (NCH(ch) == 1)
2732 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002733 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002734 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002735 else if (TYPE(CHILD(ch, 0)) == STAR)
2736 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002737 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002738 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002739 nkeywords++;
2740 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002741 }
2742 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002743 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002744 "if not sole argument");
2745 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002746 }
2747
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002748 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002749 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002750 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002751 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002753 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002754
2755 nargs = 0; /* positional arguments + iterable argument unpackings */
2756 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2757 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002758 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002759 node *ch = CHILD(n, i);
2760 if (TYPE(ch) == argument) {
2761 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002762 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002763 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002764 /* a positional argument */
2765 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002766 if (ndoublestars) {
2767 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002768 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002769 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002770 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002771 else {
2772 ast_error(c, chch,
2773 "positional argument follows "
2774 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002775 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002776 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002777 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002778 e = ast_for_expr(c, chch);
2779 if (!e)
2780 return NULL;
2781 asdl_seq_SET(args, nargs++, e);
2782 }
2783 else if (TYPE(chch) == STAR) {
2784 /* an iterable argument unpacking */
2785 expr_ty starred;
2786 if (ndoublestars) {
2787 ast_error(c, chch,
2788 "iterable argument unpacking follows "
2789 "keyword argument unpacking");
2790 return NULL;
2791 }
2792 e = ast_for_expr(c, CHILD(ch, 1));
2793 if (!e)
2794 return NULL;
2795 starred = Starred(e, Load, LINENO(chch),
2796 chch->n_col_offset,
2797 c->c_arena);
2798 if (!starred)
2799 return NULL;
2800 asdl_seq_SET(args, nargs++, starred);
2801
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002802 }
2803 else if (TYPE(chch) == DOUBLESTAR) {
2804 /* a keyword argument unpacking */
2805 keyword_ty kw;
2806 i++;
2807 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002808 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002809 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002810 kw = keyword(NULL, e, c->c_arena);
2811 asdl_seq_SET(keywords, nkeywords++, kw);
2812 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002814 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002815 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002816 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002818 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002819 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002821 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002822 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002823 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002824 identifier key, tmp;
2825 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002827 /* chch is test, but must be an identifier? */
2828 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002830 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831 /* f(lambda x: x[0] = 3) ends up getting parsed with
2832 * LHS test = lambda x: x[0], and RHS test = 3.
2833 * SF bug 132313 points out that complaining about a keyword
2834 * then is very confusing.
2835 */
2836 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002837 ast_error(c, chch,
2838 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002839 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002840 }
2841 else if (e->kind != Name_kind) {
2842 ast_error(c, chch,
2843 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002844 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002845 }
2846 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002847 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002849 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002850 for (k = 0; k < nkeywords; k++) {
2851 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002852 if (tmp && !PyUnicode_Compare(tmp, key)) {
2853 ast_error(c, chch,
2854 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002855 return NULL;
2856 }
2857 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002858 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002859 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002860 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002861 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002862 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002863 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002864 asdl_seq_SET(keywords, nkeywords++, kw);
2865 }
2866 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867 }
2868
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002869 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002870}
2871
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002872static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002873ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002874{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002875 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002876 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002877 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002878 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002879 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002880 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002881 }
2882 else {
2883 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002884 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002885 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002886 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002887 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002888 else {
2889 asdl_seq *tmp = seq_for_testlist(c, n);
2890 if (!tmp)
2891 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002892 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002893 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002894}
2895
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002896static stmt_ty
2897ast_for_expr_stmt(struct compiling *c, const node *n)
2898{
2899 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002900 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
2901 ('=' (yield_expr|testlist_star_expr))*)
2902 annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +00002903 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002904 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002905 | '<<=' | '>>=' | '**=' | '//='
Martin Panter69332c12016-08-04 13:07:31 +00002906 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002907 */
2908
2909 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002910 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002911 if (!e)
2912 return NULL;
2913
Thomas Wouters89f507f2006-12-13 04:49:30 +00002914 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002915 }
2916 else if (TYPE(CHILD(n, 1)) == augassign) {
2917 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002918 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002919 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002920
Thomas Wouters89f507f2006-12-13 04:49:30 +00002921 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002922 if (!expr1)
2923 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002924 if(!set_context(c, expr1, Store, ch))
2925 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002926 /* set_context checks that most expressions are not the left side.
2927 Augmented assignments can only have a name, a subscript, or an
2928 attribute on the left, though, so we have to explicitly check for
2929 those. */
2930 switch (expr1->kind) {
2931 case Name_kind:
2932 case Attribute_kind:
2933 case Subscript_kind:
2934 break;
2935 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002936 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002937 return NULL;
2938 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002939
Thomas Wouters89f507f2006-12-13 04:49:30 +00002940 ch = CHILD(n, 2);
2941 if (TYPE(ch) == testlist)
2942 expr2 = ast_for_testlist(c, ch);
2943 else
2944 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002945 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002946 return NULL;
2947
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002948 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002949 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002950 return NULL;
2951
Thomas Wouters89f507f2006-12-13 04:49:30 +00002952 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002953 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002954 else if (TYPE(CHILD(n, 1)) == annassign) {
2955 expr_ty expr1, expr2, expr3;
2956 node *ch = CHILD(n, 0);
2957 node *deep, *ann = CHILD(n, 1);
2958 int simple = 1;
2959
2960 /* we keep track of parens to qualify (x) as expression not name */
2961 deep = ch;
2962 while (NCH(deep) == 1) {
2963 deep = CHILD(deep, 0);
2964 }
2965 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
2966 simple = 0;
2967 }
2968 expr1 = ast_for_testlist(c, ch);
2969 if (!expr1) {
2970 return NULL;
2971 }
2972 switch (expr1->kind) {
2973 case Name_kind:
2974 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
2975 return NULL;
2976 }
2977 expr1->v.Name.ctx = Store;
2978 break;
2979 case Attribute_kind:
2980 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
2981 return NULL;
2982 }
2983 expr1->v.Attribute.ctx = Store;
2984 break;
2985 case Subscript_kind:
2986 expr1->v.Subscript.ctx = Store;
2987 break;
2988 case List_kind:
2989 ast_error(c, ch,
2990 "only single target (not list) can be annotated");
2991 return NULL;
2992 case Tuple_kind:
2993 ast_error(c, ch,
2994 "only single target (not tuple) can be annotated");
2995 return NULL;
2996 default:
2997 ast_error(c, ch,
2998 "illegal target for annotation");
2999 return NULL;
3000 }
3001
3002 if (expr1->kind != Name_kind) {
3003 simple = 0;
3004 }
3005 ch = CHILD(ann, 1);
3006 expr2 = ast_for_expr(c, ch);
3007 if (!expr2) {
3008 return NULL;
3009 }
3010 if (NCH(ann) == 2) {
3011 return AnnAssign(expr1, expr2, NULL, simple,
3012 LINENO(n), n->n_col_offset, c->c_arena);
3013 }
3014 else {
3015 ch = CHILD(ann, 3);
3016 expr3 = ast_for_expr(c, ch);
3017 if (!expr3) {
3018 return NULL;
3019 }
3020 return AnnAssign(expr1, expr2, expr3, simple,
3021 LINENO(n), n->n_col_offset, c->c_arena);
3022 }
3023 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003024 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003025 int i;
3026 asdl_seq *targets;
3027 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003028 expr_ty expression;
3029
Thomas Wouters89f507f2006-12-13 04:49:30 +00003030 /* a normal assignment */
3031 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003032 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003033 if (!targets)
3034 return NULL;
3035 for (i = 0; i < NCH(n) - 2; i += 2) {
3036 expr_ty e;
3037 node *ch = CHILD(n, i);
3038 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003039 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003040 return NULL;
3041 }
3042 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003043 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003044 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003045
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003046 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003047 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003048 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003049
Thomas Wouters89f507f2006-12-13 04:49:30 +00003050 asdl_seq_SET(targets, i / 2, e);
3051 }
3052 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003053 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003054 expression = ast_for_testlist(c, value);
3055 else
3056 expression = ast_for_expr(c, value);
3057 if (!expression)
3058 return NULL;
3059 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003060 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003061}
3062
Benjamin Peterson78565b22009-06-28 19:19:51 +00003063
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003064static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003065ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003066{
3067 asdl_seq *seq;
3068 int i;
3069 expr_ty e;
3070
3071 REQ(n, exprlist);
3072
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003073 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003074 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003075 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003076 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003077 e = ast_for_expr(c, CHILD(n, i));
3078 if (!e)
3079 return NULL;
3080 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003081 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003082 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003083 }
3084 return seq;
3085}
3086
3087static stmt_ty
3088ast_for_del_stmt(struct compiling *c, const node *n)
3089{
3090 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003091
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003092 /* del_stmt: 'del' exprlist */
3093 REQ(n, del_stmt);
3094
3095 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3096 if (!expr_list)
3097 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003098 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003099}
3100
3101static stmt_ty
3102ast_for_flow_stmt(struct compiling *c, const node *n)
3103{
3104 /*
3105 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3106 | yield_stmt
3107 break_stmt: 'break'
3108 continue_stmt: 'continue'
3109 return_stmt: 'return' [testlist]
3110 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003111 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003112 raise_stmt: 'raise' [test [',' test [',' test]]]
3113 */
3114 node *ch;
3115
3116 REQ(n, flow_stmt);
3117 ch = CHILD(n, 0);
3118 switch (TYPE(ch)) {
3119 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003120 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003121 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003122 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003123 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003124 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3125 if (!exp)
3126 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003127 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003128 }
3129 case return_stmt:
3130 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003131 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003132 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003133 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003134 if (!expression)
3135 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003136 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003137 }
3138 case raise_stmt:
3139 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003140 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3141 else if (NCH(ch) >= 2) {
3142 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003143 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3144 if (!expression)
3145 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003146 if (NCH(ch) == 4) {
3147 cause = ast_for_expr(c, CHILD(ch, 3));
3148 if (!cause)
3149 return NULL;
3150 }
3151 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 }
Stefan Krahf432a322017-08-21 13:09:59 +02003153 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003154 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003155 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003156 "unexpected flow_stmt: %d", TYPE(ch));
3157 return NULL;
3158 }
3159}
3160
3161static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003162alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003163{
3164 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003165 import_as_name: NAME ['as' NAME]
3166 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003167 dotted_name: NAME ('.' NAME)*
3168 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003169 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003170
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003171 loop:
3172 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003173 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003174 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003175 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003176 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003177 if (!name)
3178 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003179 if (NCH(n) == 3) {
3180 node *str_node = CHILD(n, 2);
3181 str = NEW_IDENTIFIER(str_node);
3182 if (!str)
3183 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003184 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003185 return NULL;
3186 }
3187 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003188 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003189 return NULL;
3190 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003191 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003192 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003193 case dotted_as_name:
3194 if (NCH(n) == 1) {
3195 n = CHILD(n, 0);
3196 goto loop;
3197 }
3198 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003199 node *asname_node = CHILD(n, 2);
3200 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003201 if (!a)
3202 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003203 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003204 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003205 if (!a->asname)
3206 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003207 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003208 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003209 return a;
3210 }
3211 break;
3212 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003213 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003214 node *name_node = CHILD(n, 0);
3215 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003216 if (!name)
3217 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003218 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003219 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003220 return alias(name, NULL, c->c_arena);
3221 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003222 else {
3223 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003224 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003225 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003226 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003228
3229 len = 0;
3230 for (i = 0; i < NCH(n); i += 2)
3231 /* length of string plus one for the dot */
3232 len += strlen(STR(CHILD(n, i))) + 1;
3233 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003234 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003235 if (!str)
3236 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003237 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003238 if (!s)
3239 return NULL;
3240 for (i = 0; i < NCH(n); i += 2) {
3241 char *sch = STR(CHILD(n, i));
3242 strcpy(s, STR(CHILD(n, i)));
3243 s += strlen(sch);
3244 *s++ = '.';
3245 }
3246 --s;
3247 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003248 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3249 PyBytes_GET_SIZE(str),
3250 NULL);
3251 Py_DECREF(str);
3252 if (!uni)
3253 return NULL;
3254 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003255 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003256 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3257 Py_DECREF(str);
3258 return NULL;
3259 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003260 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003261 }
3262 break;
3263 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003264 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003265 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3266 Py_DECREF(str);
3267 return NULL;
3268 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003269 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003270 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003271 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003272 "unexpected import name: %d", TYPE(n));
3273 return NULL;
3274 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003275
3276 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003277 return NULL;
3278}
3279
3280static stmt_ty
3281ast_for_import_stmt(struct compiling *c, const node *n)
3282{
3283 /*
3284 import_stmt: import_name | import_from
3285 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003286 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3287 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003288 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003289 int lineno;
3290 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003291 int i;
3292 asdl_seq *aliases;
3293
3294 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003295 lineno = LINENO(n);
3296 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003298 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003300 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003301 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003302 if (!aliases)
3303 return NULL;
3304 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003305 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003306 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003307 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003308 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003310 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003311 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003312 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003313 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003314 int idx, ndots = 0;
3315 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003316 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003317
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003318 /* Count the number of dots (for relative imports) and check for the
3319 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003320 for (idx = 1; idx < NCH(n); idx++) {
3321 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003322 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3323 if (!mod)
3324 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003325 idx++;
3326 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003327 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003328 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003329 ndots += 3;
3330 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003331 } else if (TYPE(CHILD(n, idx)) != DOT) {
3332 break;
3333 }
3334 ndots++;
3335 }
3336 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003337 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003338 case STAR:
3339 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003340 n = CHILD(n, idx);
3341 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003342 break;
3343 case LPAR:
3344 /* from ... import (x, y, z) */
3345 n = CHILD(n, idx + 1);
3346 n_children = NCH(n);
3347 break;
3348 case import_as_names:
3349 /* from ... import x, y, z */
3350 n = CHILD(n, idx);
3351 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003352 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003353 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003354 " surrounding parentheses");
3355 return NULL;
3356 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003357 break;
3358 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003359 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003360 return NULL;
3361 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003363 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003364 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003365 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003366
3367 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003368 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003369 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003370 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003371 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003372 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003373 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003374 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003375 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003376 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003377 if (!import_alias)
3378 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003379 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003380 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003382 if (mod != NULL)
3383 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003384 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003385 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003386 }
Neal Norwitz79792652005-11-14 04:25:03 +00003387 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003388 "unknown import statement: starts with command '%s'",
3389 STR(CHILD(n, 0)));
3390 return NULL;
3391}
3392
3393static stmt_ty
3394ast_for_global_stmt(struct compiling *c, const node *n)
3395{
3396 /* global_stmt: 'global' NAME (',' NAME)* */
3397 identifier name;
3398 asdl_seq *s;
3399 int i;
3400
3401 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003402 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003403 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003404 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003405 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003406 name = NEW_IDENTIFIER(CHILD(n, i));
3407 if (!name)
3408 return NULL;
3409 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003410 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003411 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003412}
3413
3414static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003415ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3416{
3417 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3418 identifier name;
3419 asdl_seq *s;
3420 int i;
3421
3422 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003423 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003424 if (!s)
3425 return NULL;
3426 for (i = 1; i < NCH(n); i += 2) {
3427 name = NEW_IDENTIFIER(CHILD(n, i));
3428 if (!name)
3429 return NULL;
3430 asdl_seq_SET(s, i / 2, name);
3431 }
3432 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3433}
3434
3435static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003436ast_for_assert_stmt(struct compiling *c, const node *n)
3437{
3438 /* assert_stmt: 'assert' test [',' test] */
3439 REQ(n, assert_stmt);
3440 if (NCH(n) == 2) {
3441 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3442 if (!expression)
3443 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003444 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003445 }
3446 else if (NCH(n) == 4) {
3447 expr_ty expr1, expr2;
3448
3449 expr1 = ast_for_expr(c, CHILD(n, 1));
3450 if (!expr1)
3451 return NULL;
3452 expr2 = ast_for_expr(c, CHILD(n, 3));
3453 if (!expr2)
3454 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455
Thomas Wouters89f507f2006-12-13 04:49:30 +00003456 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003457 }
Neal Norwitz79792652005-11-14 04:25:03 +00003458 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003459 "improper number of parts to 'assert' statement: %d",
3460 NCH(n));
3461 return NULL;
3462}
3463
3464static asdl_seq *
3465ast_for_suite(struct compiling *c, const node *n)
3466{
3467 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003468 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003469 stmt_ty s;
3470 int i, total, num, end, pos = 0;
3471 node *ch;
3472
3473 REQ(n, suite);
3474
3475 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003476 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003477 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003478 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003479 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003480 n = CHILD(n, 0);
3481 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003483 */
3484 end = NCH(n) - 1;
3485 if (TYPE(CHILD(n, end - 1)) == SEMI)
3486 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003487 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003488 for (i = 0; i < end; i += 2) {
3489 ch = CHILD(n, i);
3490 s = ast_for_stmt(c, ch);
3491 if (!s)
3492 return NULL;
3493 asdl_seq_SET(seq, pos++, s);
3494 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003495 }
3496 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003497 for (i = 2; i < (NCH(n) - 1); i++) {
3498 ch = CHILD(n, i);
3499 REQ(ch, stmt);
3500 num = num_stmts(ch);
3501 if (num == 1) {
3502 /* small_stmt or compound_stmt with only one child */
3503 s = ast_for_stmt(c, ch);
3504 if (!s)
3505 return NULL;
3506 asdl_seq_SET(seq, pos++, s);
3507 }
3508 else {
3509 int j;
3510 ch = CHILD(ch, 0);
3511 REQ(ch, simple_stmt);
3512 for (j = 0; j < NCH(ch); j += 2) {
3513 /* statement terminates with a semi-colon ';' */
3514 if (NCH(CHILD(ch, j)) == 0) {
3515 assert((j + 1) == NCH(ch));
3516 break;
3517 }
3518 s = ast_for_stmt(c, CHILD(ch, j));
3519 if (!s)
3520 return NULL;
3521 asdl_seq_SET(seq, pos++, s);
3522 }
3523 }
3524 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525 }
3526 assert(pos == seq->size);
3527 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003528}
3529
INADA Naokicb41b272017-02-23 00:31:59 +09003530static string
3531docstring_from_stmts(asdl_seq *stmts)
3532{
3533 if (stmts && stmts->size) {
3534 stmt_ty s = (stmt_ty)asdl_seq_GET(stmts, 0);
3535 /* If first statement is a literal string, it's the doc string. */
3536 if (s->kind == Expr_kind && s->v.Expr.value->kind == Str_kind) {
3537 string doc = s->v.Expr.value->v.Str.s;
3538 /* not very efficient, but simple */
3539 memmove(&asdl_seq_GET(stmts, 0), &asdl_seq_GET(stmts, 1),
3540 (stmts->size - 1) * sizeof(void*));
3541 stmts->size--;
3542 return doc;
3543 }
3544 }
3545 return NULL;
3546}
3547
3548static asdl_seq *
3549ast_for_body(struct compiling *c, const node *n, string *docstring)
3550{
3551 asdl_seq *stmts = ast_for_suite(c, n);
3552 *docstring = docstring_from_stmts(stmts);
3553 return stmts;
3554}
3555
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003556static stmt_ty
3557ast_for_if_stmt(struct compiling *c, const node *n)
3558{
3559 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3560 ['else' ':' suite]
3561 */
3562 char *s;
3563
3564 REQ(n, if_stmt);
3565
3566 if (NCH(n) == 4) {
3567 expr_ty expression;
3568 asdl_seq *suite_seq;
3569
3570 expression = ast_for_expr(c, CHILD(n, 1));
3571 if (!expression)
3572 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003573 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003574 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003575 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003576
Guido van Rossumd8faa362007-04-27 19:54:29 +00003577 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3578 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003579 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003580
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003581 s = STR(CHILD(n, 4));
3582 /* s[2], the third character in the string, will be
3583 's' for el_s_e, or
3584 'i' for el_i_f
3585 */
3586 if (s[2] == 's') {
3587 expr_ty expression;
3588 asdl_seq *seq1, *seq2;
3589
3590 expression = ast_for_expr(c, CHILD(n, 1));
3591 if (!expression)
3592 return NULL;
3593 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003594 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003595 return NULL;
3596 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003597 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598 return NULL;
3599
Guido van Rossumd8faa362007-04-27 19:54:29 +00003600 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3601 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003602 }
3603 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003604 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003605 expr_ty expression;
3606 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003607 asdl_seq *orelse = NULL;
3608 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003609 /* must reference the child n_elif+1 since 'else' token is third,
3610 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003611 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3612 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3613 has_else = 1;
3614 n_elif -= 3;
3615 }
3616 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003617
Thomas Wouters89f507f2006-12-13 04:49:30 +00003618 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003619 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003621 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003622 if (!orelse)
3623 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003624 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003625 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003626 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003627 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3628 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003629 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003630 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3631 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003632 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003634 asdl_seq_SET(orelse, 0,
3635 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003636 LINENO(CHILD(n, NCH(n) - 6)),
3637 CHILD(n, NCH(n) - 6)->n_col_offset,
3638 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003639 /* the just-created orelse handled the last elif */
3640 n_elif--;
3641 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003642
Thomas Wouters89f507f2006-12-13 04:49:30 +00003643 for (i = 0; i < n_elif; i++) {
3644 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003645 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003646 if (!newobj)
3647 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003648 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003649 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003650 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003651 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003652 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003653 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003654
Thomas Wouters89f507f2006-12-13 04:49:30 +00003655 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003656 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003657 LINENO(CHILD(n, off)),
3658 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003659 orelse = newobj;
3660 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003661 expression = ast_for_expr(c, CHILD(n, 1));
3662 if (!expression)
3663 return NULL;
3664 suite_seq = ast_for_suite(c, CHILD(n, 3));
3665 if (!suite_seq)
3666 return NULL;
3667 return If(expression, suite_seq, orelse,
3668 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003669 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003670
3671 PyErr_Format(PyExc_SystemError,
3672 "unexpected token in 'if' statement: %s", s);
3673 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003674}
3675
3676static stmt_ty
3677ast_for_while_stmt(struct compiling *c, const node *n)
3678{
3679 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3680 REQ(n, while_stmt);
3681
3682 if (NCH(n) == 4) {
3683 expr_ty expression;
3684 asdl_seq *suite_seq;
3685
3686 expression = ast_for_expr(c, CHILD(n, 1));
3687 if (!expression)
3688 return NULL;
3689 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003690 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003691 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003692 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003693 }
3694 else if (NCH(n) == 7) {
3695 expr_ty expression;
3696 asdl_seq *seq1, *seq2;
3697
3698 expression = ast_for_expr(c, CHILD(n, 1));
3699 if (!expression)
3700 return NULL;
3701 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003702 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003703 return NULL;
3704 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003705 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003706 return NULL;
3707
Thomas Wouters89f507f2006-12-13 04:49:30 +00003708 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003709 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003710
3711 PyErr_Format(PyExc_SystemError,
3712 "wrong number of tokens for 'while' statement: %d",
3713 NCH(n));
3714 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003715}
3716
3717static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003718ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003719{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003720 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003721 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003722 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003723 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003724 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3725 REQ(n, for_stmt);
3726
3727 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003728 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003729 if (!seq)
3730 return NULL;
3731 }
3732
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003733 node_target = CHILD(n, 1);
3734 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003735 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003736 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003737 /* Check the # of children rather than the length of _target, since
3738 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003739 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003740 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003741 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003742 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003743 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003744
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003745 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003746 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003747 return NULL;
3748 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003749 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003750 return NULL;
3751
Yury Selivanov75445082015-05-11 22:57:16 -04003752 if (is_async)
3753 return AsyncFor(target, expression, suite_seq, seq,
3754 LINENO(n), n->n_col_offset,
3755 c->c_arena);
3756 else
3757 return For(target, expression, suite_seq, seq,
3758 LINENO(n), n->n_col_offset,
3759 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003760}
3761
3762static excepthandler_ty
3763ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3764{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003765 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003766 REQ(exc, except_clause);
3767 REQ(body, suite);
3768
3769 if (NCH(exc) == 1) {
3770 asdl_seq *suite_seq = ast_for_suite(c, body);
3771 if (!suite_seq)
3772 return NULL;
3773
Neal Norwitzad74aa82008-03-31 05:14:30 +00003774 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003775 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003776 }
3777 else if (NCH(exc) == 2) {
3778 expr_ty expression;
3779 asdl_seq *suite_seq;
3780
3781 expression = ast_for_expr(c, CHILD(exc, 1));
3782 if (!expression)
3783 return NULL;
3784 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003785 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003786 return NULL;
3787
Neal Norwitzad74aa82008-03-31 05:14:30 +00003788 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003789 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003790 }
3791 else if (NCH(exc) == 4) {
3792 asdl_seq *suite_seq;
3793 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003794 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003795 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003796 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003797 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003798 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003799 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003800 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003801 return NULL;
3802 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003803 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003804 return NULL;
3805
Neal Norwitzad74aa82008-03-31 05:14:30 +00003806 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003807 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003808 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003809
3810 PyErr_Format(PyExc_SystemError,
3811 "wrong number of children for 'except' clause: %d",
3812 NCH(exc));
3813 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003814}
3815
3816static stmt_ty
3817ast_for_try_stmt(struct compiling *c, const node *n)
3818{
Neal Norwitzf599f422005-12-17 21:33:47 +00003819 const int nch = NCH(n);
3820 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003821 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003822
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003823 REQ(n, try_stmt);
3824
Neal Norwitzf599f422005-12-17 21:33:47 +00003825 body = ast_for_suite(c, CHILD(n, 2));
3826 if (body == NULL)
3827 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003828
Neal Norwitzf599f422005-12-17 21:33:47 +00003829 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3830 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3831 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3832 /* we can assume it's an "else",
3833 because nch >= 9 for try-else-finally and
3834 it would otherwise have a type of except_clause */
3835 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3836 if (orelse == NULL)
3837 return NULL;
3838 n_except--;
3839 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003840
Neal Norwitzf599f422005-12-17 21:33:47 +00003841 finally = ast_for_suite(c, CHILD(n, nch - 1));
3842 if (finally == NULL)
3843 return NULL;
3844 n_except--;
3845 }
3846 else {
3847 /* we can assume it's an "else",
3848 otherwise it would have a type of except_clause */
3849 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3850 if (orelse == NULL)
3851 return NULL;
3852 n_except--;
3853 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003854 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003855 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003856 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003857 return NULL;
3858 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003859
Neal Norwitzf599f422005-12-17 21:33:47 +00003860 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003861 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003862 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003863 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003864 if (handlers == NULL)
3865 return NULL;
3866
3867 for (i = 0; i < n_except; i++) {
3868 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3869 CHILD(n, 5 + i * 3));
3870 if (!e)
3871 return NULL;
3872 asdl_seq_SET(handlers, i, e);
3873 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003874 }
3875
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003876 assert(finally != NULL || asdl_seq_LEN(handlers));
3877 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003878}
3879
Georg Brandl0c315622009-05-25 21:10:36 +00003880/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003881static withitem_ty
3882ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003883{
3884 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003885
Georg Brandl0c315622009-05-25 21:10:36 +00003886 REQ(n, with_item);
3887 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003888 if (!context_expr)
3889 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003890 if (NCH(n) == 3) {
3891 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003892
3893 if (!optional_vars) {
3894 return NULL;
3895 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003896 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003897 return NULL;
3898 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003899 }
3900
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003901 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003902}
3903
Georg Brandl0c315622009-05-25 21:10:36 +00003904/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3905static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003906ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003907{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003908 int i, n_items;
3909 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003910
3911 REQ(n, with_stmt);
3912
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003913 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003914 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003915 if (!items)
3916 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003917 for (i = 1; i < NCH(n) - 2; i += 2) {
3918 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3919 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003920 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003921 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003922 }
3923
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003924 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3925 if (!body)
3926 return NULL;
3927
Yury Selivanov75445082015-05-11 22:57:16 -04003928 if (is_async)
3929 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3930 else
3931 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003932}
3933
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003934static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003935ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003936{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003937 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003938 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003939 asdl_seq *s;
INADA Naokicb41b272017-02-23 00:31:59 +09003940 string docstring;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003941 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003942
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003943 REQ(n, classdef);
3944
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003945 if (NCH(n) == 4) { /* class NAME ':' suite */
INADA Naokicb41b272017-02-23 00:31:59 +09003946 s = ast_for_body(c, CHILD(n, 3), &docstring);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003947 if (!s)
3948 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003949 classname = NEW_IDENTIFIER(CHILD(n, 1));
3950 if (!classname)
3951 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003952 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003953 return NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09003954 return ClassDef(classname, NULL, NULL, s, decorator_seq, docstring,
3955 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003956 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003957
3958 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
INADA Naokicb41b272017-02-23 00:31:59 +09003959 s = ast_for_body(c, CHILD(n, 5), &docstring);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003960 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003961 return NULL;
3962 classname = NEW_IDENTIFIER(CHILD(n, 1));
3963 if (!classname)
3964 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003965 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003966 return NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09003967 return ClassDef(classname, NULL, NULL, s, decorator_seq, docstring,
3968 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003969 }
3970
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003971 /* class NAME '(' arglist ')' ':' suite */
3972 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003973 {
3974 PyObject *dummy_name;
3975 expr_ty dummy;
3976 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3977 if (!dummy_name)
3978 return NULL;
3979 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3980 call = ast_for_call(c, CHILD(n, 3), dummy);
3981 if (!call)
3982 return NULL;
3983 }
INADA Naokicb41b272017-02-23 00:31:59 +09003984 s = ast_for_body(c, CHILD(n, 6), &docstring);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003985 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003986 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003987 classname = NEW_IDENTIFIER(CHILD(n, 1));
3988 if (!classname)
3989 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003990 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003991 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003992
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003993 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
INADA Naokicb41b272017-02-23 00:31:59 +09003994 decorator_seq, docstring, LINENO(n), n->n_col_offset,
3995 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003996}
3997
3998static stmt_ty
3999ast_for_stmt(struct compiling *c, const node *n)
4000{
4001 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004002 assert(NCH(n) == 1);
4003 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004004 }
4005 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004006 assert(num_stmts(n) == 1);
4007 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004008 }
4009 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004010 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004011 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4012 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004013 */
4014 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004015 case expr_stmt:
4016 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004017 case del_stmt:
4018 return ast_for_del_stmt(c, n);
4019 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00004020 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004021 case flow_stmt:
4022 return ast_for_flow_stmt(c, n);
4023 case import_stmt:
4024 return ast_for_import_stmt(c, n);
4025 case global_stmt:
4026 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004027 case nonlocal_stmt:
4028 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004029 case assert_stmt:
4030 return ast_for_assert_stmt(c, n);
4031 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004032 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004033 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4034 TYPE(n), NCH(n));
4035 return NULL;
4036 }
4037 }
4038 else {
4039 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004040 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004041 */
4042 node *ch = CHILD(n, 0);
4043 REQ(n, compound_stmt);
4044 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004045 case if_stmt:
4046 return ast_for_if_stmt(c, ch);
4047 case while_stmt:
4048 return ast_for_while_stmt(c, ch);
4049 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004050 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004051 case try_stmt:
4052 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004053 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004054 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004055 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004056 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004057 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004058 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004059 case decorated:
4060 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004061 case async_stmt:
4062 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004063 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004064 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004065 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4066 TYPE(n), NCH(n));
4067 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004068 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004069 }
4070}
4071
4072static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004073parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004074{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004075 const char *end;
4076 long x;
4077 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004078 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004079 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004080
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004081 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004082 errno = 0;
4083 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004084 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004085 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004086 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004087 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004088 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004089 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004090 }
4091 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004092 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004093 if (*end == '\0') {
4094 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004095 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004096 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004097 }
4098 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004099 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004100 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004101 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4102 if (compl.imag == -1.0 && PyErr_Occurred())
4103 return NULL;
4104 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004105 }
4106 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004107 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004108 dx = PyOS_string_to_double(s, NULL, NULL);
4109 if (dx == -1.0 && PyErr_Occurred())
4110 return NULL;
4111 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004112 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004113}
4114
4115static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004116parsenumber(struct compiling *c, const char *s)
4117{
4118 char *dup, *end;
4119 PyObject *res = NULL;
4120
4121 assert(s != NULL);
4122
4123 if (strchr(s, '_') == NULL) {
4124 return parsenumber_raw(c, s);
4125 }
4126 /* Create a duplicate without underscores. */
4127 dup = PyMem_Malloc(strlen(s) + 1);
4128 end = dup;
4129 for (; *s; s++) {
4130 if (*s != '_') {
4131 *end++ = *s;
4132 }
4133 }
4134 *end = '\0';
4135 res = parsenumber_raw(c, dup);
4136 PyMem_Free(dup);
4137 return res;
4138}
4139
4140static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004141decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004142{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004143 const char *s, *t;
4144 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004145 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4146 while (s < end && (*s & 0x80)) s++;
4147 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004148 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004149}
4150
Eric V. Smith56466482016-10-31 14:46:26 -04004151static int
4152warn_invalid_escape_sequence(struct compiling *c, const node *n,
4153 char first_invalid_escape_char)
4154{
4155 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4156 first_invalid_escape_char);
4157 if (msg == NULL) {
4158 return -1;
4159 }
4160 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
4161 c->c_filename, LINENO(n),
4162 NULL, NULL) < 0 &&
4163 PyErr_ExceptionMatches(PyExc_DeprecationWarning))
4164 {
Victor Stinnerf9cca362016-11-15 09:12:10 +01004165 const char *s;
4166
4167 /* Replace the DeprecationWarning exception with a SyntaxError
4168 to get a more accurate error report */
4169 PyErr_Clear();
4170
4171 s = PyUnicode_AsUTF8(msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004172 if (s != NULL) {
4173 ast_error(c, n, s);
4174 }
4175 Py_DECREF(msg);
4176 return -1;
4177 }
4178 Py_DECREF(msg);
4179 return 0;
4180}
4181
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004182static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004183decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4184 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004185{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004186 PyObject *v, *u;
4187 char *buf;
4188 char *p;
4189 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004190
Benjamin Peterson202803a2016-02-25 22:34:45 -08004191 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004192 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004193 return NULL;
4194 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4195 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4196 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4197 if (u == NULL)
4198 return NULL;
4199 p = buf = PyBytes_AsString(u);
4200 end = s + len;
4201 while (s < end) {
4202 if (*s == '\\') {
4203 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004204 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004205 strcpy(p, "u005c");
4206 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004207 if (s >= end)
4208 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004209 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004210 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004211 if (*s & 0x80) { /* XXX inefficient */
4212 PyObject *w;
4213 int kind;
4214 void *data;
4215 Py_ssize_t len, i;
4216 w = decode_utf8(c, &s, end);
4217 if (w == NULL) {
4218 Py_DECREF(u);
4219 return NULL;
4220 }
4221 kind = PyUnicode_KIND(w);
4222 data = PyUnicode_DATA(w);
4223 len = PyUnicode_GET_LENGTH(w);
4224 for (i = 0; i < len; i++) {
4225 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4226 sprintf(p, "\\U%08x", chr);
4227 p += 10;
4228 }
4229 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004230 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004231 Py_DECREF(w);
4232 } else {
4233 *p++ = *s++;
4234 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004235 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004236 len = p - buf;
4237 s = buf;
4238
Eric V. Smith56466482016-10-31 14:46:26 -04004239 const char *first_invalid_escape;
4240 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4241
4242 if (v != NULL && first_invalid_escape != NULL) {
4243 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4244 /* We have not decref u before because first_invalid_escape points
4245 inside u. */
4246 Py_XDECREF(u);
4247 Py_DECREF(v);
4248 return NULL;
4249 }
4250 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004251 Py_XDECREF(u);
4252 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004253}
4254
Eric V. Smith56466482016-10-31 14:46:26 -04004255static PyObject *
4256decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4257 size_t len)
4258{
4259 const char *first_invalid_escape;
4260 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4261 &first_invalid_escape);
4262 if (result == NULL)
4263 return NULL;
4264
4265 if (first_invalid_escape != NULL) {
4266 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4267 Py_DECREF(result);
4268 return NULL;
4269 }
4270 }
4271 return result;
4272}
4273
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004274/* Shift locations for the given node and all its children by adding `lineno`
4275 and `col_offset` to existing locations. */
4276static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4277{
4278 n->n_col_offset = n->n_col_offset + col_offset;
4279 for (int i = 0; i < NCH(n); ++i) {
4280 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4281 /* Shifting column offsets unnecessary if there's been newlines. */
4282 col_offset = 0;
4283 }
4284 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4285 }
4286 n->n_lineno = n->n_lineno + lineno;
4287}
4288
4289/* Fix locations for the given node and its children.
4290
4291 `parent` is the enclosing node.
4292 `n` is the node which locations are going to be fixed relative to parent.
4293 `expr_str` is the child node's string representation, incuding braces.
4294*/
4295static void
4296fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4297{
4298 char *substr = NULL;
4299 char *start;
4300 int lines = LINENO(parent) - 1;
4301 int cols = parent->n_col_offset;
4302 /* Find the full fstring to fix location information in `n`. */
4303 while (parent && parent->n_type != STRING)
4304 parent = parent->n_child;
4305 if (parent && parent->n_str) {
4306 substr = strstr(parent->n_str, expr_str);
4307 if (substr) {
4308 start = substr;
4309 while (start > parent->n_str) {
4310 if (start[0] == '\n')
4311 break;
4312 start--;
4313 }
4314 cols += substr - start;
4315 /* Fix lineno in mulitline strings. */
4316 while ((substr = strchr(substr + 1, '\n')))
4317 lines--;
4318 }
4319 }
4320 fstring_shift_node_locations(n, lines, cols);
4321}
4322
Eric V. Smith451d0e32016-09-09 21:56:20 -04004323/* Compile this expression in to an expr_ty. Add parens around the
4324 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004325static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004326fstring_compile_expr(const char *expr_start, const char *expr_end,
4327 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004328
Eric V. Smith235a6f02015-09-19 14:51:32 -04004329{
4330 PyCompilerFlags cf;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004331 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004332 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004333 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004334 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004335 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004336
Eric V. Smith1d44c412015-09-23 07:49:00 -04004337 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004338 assert(*(expr_start-1) == '{');
4339 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004340
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004341 /* If the substring is all whitespace, it's an error. We need to catch this
4342 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4343 because turning the expression '' in to '()' would go from being invalid
4344 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004345 for (s = expr_start; s != expr_end; s++) {
4346 char c = *s;
4347 /* The Python parser ignores only the following whitespace
4348 characters (\r already is converted to \n). */
4349 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004350 break;
4351 }
4352 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004353 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004354 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004355 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004356 }
4357
Eric V. Smith451d0e32016-09-09 21:56:20 -04004358 len = expr_end - expr_start;
4359 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4360 str = PyMem_RawMalloc(len + 3);
4361 if (str == NULL)
4362 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004363
Eric V. Smith451d0e32016-09-09 21:56:20 -04004364 str[0] = '(';
4365 memcpy(str+1, expr_start, len);
4366 str[len+1] = ')';
4367 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004368
4369 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004370 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4371 Py_eval_input, 0);
4372 if (!mod_n) {
4373 PyMem_RawFree(str);
4374 return NULL;
4375 }
4376 /* Reuse str to find the correct column offset. */
4377 str[0] = '{';
4378 str[len+1] = '}';
4379 fstring_fix_node_location(n, mod_n, str);
4380 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004381 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004382 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004383 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004384 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004385 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004386}
4387
4388/* Return -1 on error.
4389
4390 Return 0 if we reached the end of the literal.
4391
4392 Return 1 if we haven't reached the end of the literal, but we want
4393 the caller to process the literal up to this point. Used for
4394 doubled braces.
4395*/
4396static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004397fstring_find_literal(const char **str, const char *end, int raw,
4398 PyObject **literal, int recurse_lvl,
4399 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004400{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004401 /* Get any literal string. It ends when we hit an un-doubled left
4402 brace (which isn't part of a unicode name escape such as
4403 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004404
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004405 const char *s = *str;
4406 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004407 int result = 0;
4408
Eric V. Smith235a6f02015-09-19 14:51:32 -04004409 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004410 while (s < end) {
4411 char ch = *s++;
4412 if (!raw && ch == '\\' && s < end) {
4413 ch = *s++;
4414 if (ch == 'N') {
4415 if (s < end && *s++ == '{') {
4416 while (s < end && *s++ != '}') {
4417 }
4418 continue;
4419 }
4420 break;
4421 }
4422 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4423 return -1;
4424 }
4425 }
4426 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004427 /* Check for doubled braces, but only at the top level. If
4428 we checked at every level, then f'{0:{3}}' would fail
4429 with the two closing braces. */
4430 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004431 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004432 /* We're going to tell the caller that the literal ends
4433 here, but that they should continue scanning. But also
4434 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004435 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004436 result = 1;
4437 goto done;
4438 }
4439
4440 /* Where a single '{' is the start of a new expression, a
4441 single '}' is not allowed. */
4442 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004443 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004444 ast_error(c, n, "f-string: single '}' is not allowed");
4445 return -1;
4446 }
4447 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004448 /* We're either at a '{', which means we're starting another
4449 expression; or a '}', which means we're at the end of this
4450 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004451 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004452 break;
4453 }
4454 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004455 *str = s;
4456 assert(s <= end);
4457 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004458done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004459 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004460 if (raw)
4461 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004462 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004463 NULL, NULL);
4464 else
Eric V. Smith56466482016-10-31 14:46:26 -04004465 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004466 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004467 if (!*literal)
4468 return -1;
4469 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004470 return result;
4471}
4472
4473/* Forward declaration because parsing is recursive. */
4474static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004475fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004476 struct compiling *c, const node *n);
4477
Eric V. Smith451d0e32016-09-09 21:56:20 -04004478/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004479 expression (so it must be a '{'). Returns the FormattedValue node,
4480 which includes the expression, conversion character, and
4481 format_spec expression.
4482
4483 Note that I don't do a perfect job here: I don't make sure that a
4484 closing brace doesn't match an opening paren, for example. It
4485 doesn't need to error on all invalid expressions, just correctly
4486 find the end of all valid ones. Any errors inside the expression
4487 will be caught when we parse it later. */
4488static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004489fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004490 expr_ty *expression, struct compiling *c, const node *n)
4491{
4492 /* Return -1 on error, else 0. */
4493
Eric V. Smith451d0e32016-09-09 21:56:20 -04004494 const char *expr_start;
4495 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004496 expr_ty simple_expression;
4497 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004498 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004499
4500 /* 0 if we're not in a string, else the quote char we're trying to
4501 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004502 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004503
4504 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4505 int string_type = 0;
4506
4507 /* Keep track of nesting level for braces/parens/brackets in
4508 expressions. */
4509 Py_ssize_t nested_depth = 0;
4510
4511 /* Can only nest one level deep. */
4512 if (recurse_lvl >= 2) {
4513 ast_error(c, n, "f-string: expressions nested too deeply");
4514 return -1;
4515 }
4516
4517 /* The first char must be a left brace, or we wouldn't have gotten
4518 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004519 assert(**str == '{');
4520 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004521
Eric V. Smith451d0e32016-09-09 21:56:20 -04004522 expr_start = *str;
4523 for (; *str < end; (*str)++) {
4524 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004525
4526 /* Loop invariants. */
4527 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004528 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004529 if (quote_char)
4530 assert(string_type == 1 || string_type == 3);
4531 else
4532 assert(string_type == 0);
4533
Eric V. Smith451d0e32016-09-09 21:56:20 -04004534 ch = **str;
4535 /* Nowhere inside an expression is a backslash allowed. */
4536 if (ch == '\\') {
4537 /* Error: can't include a backslash character, inside
4538 parens or strings or not. */
4539 ast_error(c, n, "f-string expression part "
4540 "cannot include a backslash");
4541 return -1;
4542 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004543 if (quote_char) {
4544 /* We're inside a string. See if we're at the end. */
4545 /* This code needs to implement the same non-error logic
4546 as tok_get from tokenizer.c, at the letter_quote
4547 label. To actually share that code would be a
4548 nightmare. But, it's unlikely to change and is small,
4549 so duplicate it here. Note we don't need to catch all
4550 of the errors, since they'll be caught when parsing the
4551 expression. We just need to match the non-error
4552 cases. Thus we can ignore \n in single-quoted strings,
4553 for example. Or non-terminated strings. */
4554 if (ch == quote_char) {
4555 /* Does this match the string_type (single or triple
4556 quoted)? */
4557 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004558 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004559 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004560 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004561 string_type = 0;
4562 quote_char = 0;
4563 continue;
4564 }
4565 } else {
4566 /* We're at the end of a normal string. */
4567 quote_char = 0;
4568 string_type = 0;
4569 continue;
4570 }
4571 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004572 } else if (ch == '\'' || ch == '"') {
4573 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004574 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004575 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004576 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004577 } else {
4578 /* Start of a normal string. */
4579 string_type = 1;
4580 }
4581 /* Start looking for the end of the string. */
4582 quote_char = ch;
4583 } else if (ch == '[' || ch == '{' || ch == '(') {
4584 nested_depth++;
4585 } else if (nested_depth != 0 &&
4586 (ch == ']' || ch == '}' || ch == ')')) {
4587 nested_depth--;
4588 } else if (ch == '#') {
4589 /* Error: can't include a comment character, inside parens
4590 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004591 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004592 return -1;
4593 } else if (nested_depth == 0 &&
4594 (ch == '!' || ch == ':' || ch == '}')) {
4595 /* First, test for the special case of "!=". Since '=' is
4596 not an allowed conversion character, nothing is lost in
4597 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004598 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004599 /* This isn't a conversion character, just continue. */
4600 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004601 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004602 /* Normal way out of this loop. */
4603 break;
4604 } else {
4605 /* Just consume this char and loop around. */
4606 }
4607 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004608 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004609 /* If we leave this loop in a string or with mismatched parens, we
4610 don't care. We'll get a syntax error when compiling the
4611 expression. But, we can produce a better error message, so
4612 let's just do that.*/
4613 if (quote_char) {
4614 ast_error(c, n, "f-string: unterminated string");
4615 return -1;
4616 }
4617 if (nested_depth) {
4618 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4619 return -1;
4620 }
4621
Eric V. Smith451d0e32016-09-09 21:56:20 -04004622 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004623 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004624
4625 /* Compile the expression as soon as possible, so we show errors
4626 related to the expression before errors related to the
4627 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004628 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004629 if (!simple_expression)
4630 return -1;
4631
4632 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004633 if (**str == '!') {
4634 *str += 1;
4635 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004636 goto unexpected_end_of_string;
4637
Eric V. Smith451d0e32016-09-09 21:56:20 -04004638 conversion = **str;
4639 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004640
4641 /* Validate the conversion. */
4642 if (!(conversion == 's' || conversion == 'r'
4643 || conversion == 'a')) {
4644 ast_error(c, n, "f-string: invalid conversion character: "
4645 "expected 's', 'r', or 'a'");
4646 return -1;
4647 }
4648 }
4649
4650 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004651 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004652 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004653 if (**str == ':') {
4654 *str += 1;
4655 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004656 goto unexpected_end_of_string;
4657
4658 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004659 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004660 if (!format_spec)
4661 return -1;
4662 }
4663
Eric V. Smith451d0e32016-09-09 21:56:20 -04004664 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004665 goto unexpected_end_of_string;
4666
4667 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004668 assert(*str < end);
4669 assert(**str == '}');
4670 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004671
Eric V. Smith451d0e32016-09-09 21:56:20 -04004672 /* And now create the FormattedValue node that represents this
4673 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004674 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004675 format_spec, LINENO(n), n->n_col_offset,
4676 c->c_arena);
4677 if (!*expression)
4678 return -1;
4679
4680 return 0;
4681
4682unexpected_end_of_string:
4683 ast_error(c, n, "f-string: expecting '}'");
4684 return -1;
4685}
4686
4687/* Return -1 on error.
4688
4689 Return 0 if we have a literal (possible zero length) and an
4690 expression (zero length if at the end of the string.
4691
4692 Return 1 if we have a literal, but no expression, and we want the
4693 caller to call us again. This is used to deal with doubled
4694 braces.
4695
4696 When called multiple times on the string 'a{{b{0}c', this function
4697 will return:
4698
4699 1. the literal 'a{' with no expression, and a return value
4700 of 1. Despite the fact that there's no expression, the return
4701 value of 1 means we're not finished yet.
4702
4703 2. the literal 'b' and the expression '0', with a return value of
4704 0. The fact that there's an expression means we're not finished.
4705
4706 3. literal 'c' with no expression and a return value of 0. The
4707 combination of the return value of 0 with no expression means
4708 we're finished.
4709*/
4710static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004711fstring_find_literal_and_expr(const char **str, const char *end, int raw,
4712 int recurse_lvl, PyObject **literal,
4713 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004714 struct compiling *c, const node *n)
4715{
4716 int result;
4717
4718 assert(*literal == NULL && *expression == NULL);
4719
4720 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004721 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004722 if (result < 0)
4723 goto error;
4724
4725 assert(result == 0 || result == 1);
4726
4727 if (result == 1)
4728 /* We have a literal, but don't look at the expression. */
4729 return 1;
4730
Eric V. Smith451d0e32016-09-09 21:56:20 -04004731 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004732 /* We're at the end of the string or the end of a nested
4733 f-string: no expression. The top-level error case where we
4734 expect to be at the end of the string but we're at a '}' is
4735 handled later. */
4736 return 0;
4737
4738 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004739 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004740
Eric V. Smith451d0e32016-09-09 21:56:20 -04004741 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004742 goto error;
4743
4744 return 0;
4745
4746error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004747 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004748 return -1;
4749}
4750
4751#define EXPRLIST_N_CACHED 64
4752
4753typedef struct {
4754 /* Incrementally build an array of expr_ty, so be used in an
4755 asdl_seq. Cache some small but reasonably sized number of
4756 expr_ty's, and then after that start dynamically allocating,
4757 doubling the number allocated each time. Note that the f-string
4758 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
4759 Str for the literal 'a'. So you add expr_ty's about twice as
4760 fast as you add exressions in an f-string. */
4761
4762 Py_ssize_t allocated; /* Number we've allocated. */
4763 Py_ssize_t size; /* Number we've used. */
4764 expr_ty *p; /* Pointer to the memory we're actually
4765 using. Will point to 'data' until we
4766 start dynamically allocating. */
4767 expr_ty data[EXPRLIST_N_CACHED];
4768} ExprList;
4769
4770#ifdef NDEBUG
4771#define ExprList_check_invariants(l)
4772#else
4773static void
4774ExprList_check_invariants(ExprList *l)
4775{
4776 /* Check our invariants. Make sure this object is "live", and
4777 hasn't been deallocated. */
4778 assert(l->size >= 0);
4779 assert(l->p != NULL);
4780 if (l->size <= EXPRLIST_N_CACHED)
4781 assert(l->data == l->p);
4782}
4783#endif
4784
4785static void
4786ExprList_Init(ExprList *l)
4787{
4788 l->allocated = EXPRLIST_N_CACHED;
4789 l->size = 0;
4790
4791 /* Until we start allocating dynamically, p points to data. */
4792 l->p = l->data;
4793
4794 ExprList_check_invariants(l);
4795}
4796
4797static int
4798ExprList_Append(ExprList *l, expr_ty exp)
4799{
4800 ExprList_check_invariants(l);
4801 if (l->size >= l->allocated) {
4802 /* We need to alloc (or realloc) the memory. */
4803 Py_ssize_t new_size = l->allocated * 2;
4804
4805 /* See if we've ever allocated anything dynamically. */
4806 if (l->p == l->data) {
4807 Py_ssize_t i;
4808 /* We're still using the cached data. Switch to
4809 alloc-ing. */
4810 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4811 if (!l->p)
4812 return -1;
4813 /* Copy the cached data into the new buffer. */
4814 for (i = 0; i < l->size; i++)
4815 l->p[i] = l->data[i];
4816 } else {
4817 /* Just realloc. */
4818 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4819 if (!tmp) {
4820 PyMem_RawFree(l->p);
4821 l->p = NULL;
4822 return -1;
4823 }
4824 l->p = tmp;
4825 }
4826
4827 l->allocated = new_size;
4828 assert(l->allocated == 2 * l->size);
4829 }
4830
4831 l->p[l->size++] = exp;
4832
4833 ExprList_check_invariants(l);
4834 return 0;
4835}
4836
4837static void
4838ExprList_Dealloc(ExprList *l)
4839{
4840 ExprList_check_invariants(l);
4841
4842 /* If there's been an error, or we've never dynamically allocated,
4843 do nothing. */
4844 if (!l->p || l->p == l->data) {
4845 /* Do nothing. */
4846 } else {
4847 /* We have dynamically allocated. Free the memory. */
4848 PyMem_RawFree(l->p);
4849 }
4850 l->p = NULL;
4851 l->size = -1;
4852}
4853
4854static asdl_seq *
4855ExprList_Finish(ExprList *l, PyArena *arena)
4856{
4857 asdl_seq *seq;
4858
4859 ExprList_check_invariants(l);
4860
4861 /* Allocate the asdl_seq and copy the expressions in to it. */
4862 seq = _Py_asdl_seq_new(l->size, arena);
4863 if (seq) {
4864 Py_ssize_t i;
4865 for (i = 0; i < l->size; i++)
4866 asdl_seq_SET(seq, i, l->p[i]);
4867 }
4868 ExprList_Dealloc(l);
4869 return seq;
4870}
4871
4872/* The FstringParser is designed to add a mix of strings and
4873 f-strings, and concat them together as needed. Ultimately, it
4874 generates an expr_ty. */
4875typedef struct {
4876 PyObject *last_str;
4877 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004878 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004879} FstringParser;
4880
4881#ifdef NDEBUG
4882#define FstringParser_check_invariants(state)
4883#else
4884static void
4885FstringParser_check_invariants(FstringParser *state)
4886{
4887 if (state->last_str)
4888 assert(PyUnicode_CheckExact(state->last_str));
4889 ExprList_check_invariants(&state->expr_list);
4890}
4891#endif
4892
4893static void
4894FstringParser_Init(FstringParser *state)
4895{
4896 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004897 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004898 ExprList_Init(&state->expr_list);
4899 FstringParser_check_invariants(state);
4900}
4901
4902static void
4903FstringParser_Dealloc(FstringParser *state)
4904{
4905 FstringParser_check_invariants(state);
4906
4907 Py_XDECREF(state->last_str);
4908 ExprList_Dealloc(&state->expr_list);
4909}
4910
4911/* Make a Str node, but decref the PyUnicode object being added. */
4912static expr_ty
4913make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4914{
4915 PyObject *s = *str;
4916 *str = NULL;
4917 assert(PyUnicode_CheckExact(s));
4918 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4919 Py_DECREF(s);
4920 return NULL;
4921 }
4922 return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
4923}
4924
4925/* Add a non-f-string (that is, a regular literal string). str is
4926 decref'd. */
4927static int
4928FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4929{
4930 FstringParser_check_invariants(state);
4931
4932 assert(PyUnicode_CheckExact(str));
4933
4934 if (PyUnicode_GET_LENGTH(str) == 0) {
4935 Py_DECREF(str);
4936 return 0;
4937 }
4938
4939 if (!state->last_str) {
4940 /* We didn't have a string before, so just remember this one. */
4941 state->last_str = str;
4942 } else {
4943 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004944 PyUnicode_AppendAndDel(&state->last_str, str);
4945 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004946 return -1;
4947 }
4948 FstringParser_check_invariants(state);
4949 return 0;
4950}
4951
Eric V. Smith451d0e32016-09-09 21:56:20 -04004952/* Parse an f-string. The f-string is in *str to end, with no
4953 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004954static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004955FstringParser_ConcatFstring(FstringParser *state, const char **str,
4956 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004957 struct compiling *c, const node *n)
4958{
4959 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004960 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004961
4962 /* Parse the f-string. */
4963 while (1) {
4964 PyObject *literal = NULL;
4965 expr_ty expression = NULL;
4966
4967 /* If there's a zero length literal in front of the
4968 expression, literal will be NULL. If we're at the end of
4969 the f-string, expression will be NULL (unless result == 1,
4970 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004971 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004972 &literal, &expression,
4973 c, n);
4974 if (result < 0)
4975 return -1;
4976
4977 /* Add the literal, if any. */
4978 if (!literal) {
4979 /* Do nothing. Just leave last_str alone (and possibly
4980 NULL). */
4981 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04004982 /* Note that the literal can be zero length, if the
4983 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004984 state->last_str = literal;
4985 literal = NULL;
4986 } else {
4987 /* We have a literal, concatenate it. */
4988 assert(PyUnicode_GET_LENGTH(literal) != 0);
4989 if (FstringParser_ConcatAndDel(state, literal) < 0)
4990 return -1;
4991 literal = NULL;
4992 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004993
4994 /* We've dealt with the literal now. It can't be leaked on further
4995 errors. */
4996 assert(literal == NULL);
4997
4998 /* See if we should just loop around to get the next literal
4999 and expression, while ignoring the expression this
5000 time. This is used for un-doubling braces, as an
5001 optimization. */
5002 if (result == 1)
5003 continue;
5004
5005 if (!expression)
5006 /* We're done with this f-string. */
5007 break;
5008
5009 /* We know we have an expression. Convert any existing string
5010 to a Str node. */
5011 if (!state->last_str) {
5012 /* Do nothing. No previous literal. */
5013 } else {
5014 /* Convert the existing last_str literal to a Str node. */
5015 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5016 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5017 return -1;
5018 }
5019
5020 if (ExprList_Append(&state->expr_list, expression) < 0)
5021 return -1;
5022 }
5023
Eric V. Smith235a6f02015-09-19 14:51:32 -04005024 /* If recurse_lvl is zero, then we must be at the end of the
5025 string. Otherwise, we must be at a right brace. */
5026
Eric V. Smith451d0e32016-09-09 21:56:20 -04005027 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005028 ast_error(c, n, "f-string: unexpected end of string");
5029 return -1;
5030 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005031 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005032 ast_error(c, n, "f-string: expecting '}'");
5033 return -1;
5034 }
5035
5036 FstringParser_check_invariants(state);
5037 return 0;
5038}
5039
5040/* Convert the partial state reflected in last_str and expr_list to an
5041 expr_ty. The expr_ty can be a Str, or a JoinedStr. */
5042static expr_ty
5043FstringParser_Finish(FstringParser *state, struct compiling *c,
5044 const node *n)
5045{
5046 asdl_seq *seq;
5047
5048 FstringParser_check_invariants(state);
5049
5050 /* If we're just a constant string with no expressions, return
5051 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005052 if (!state->fmode) {
5053 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005054 if (!state->last_str) {
5055 /* Create a zero length string. */
5056 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5057 if (!state->last_str)
5058 goto error;
5059 }
5060 return make_str_node_and_del(&state->last_str, c, n);
5061 }
5062
5063 /* Create a Str node out of last_str, if needed. It will be the
5064 last node in our expression list. */
5065 if (state->last_str) {
5066 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5067 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5068 goto error;
5069 }
5070 /* This has already been freed. */
5071 assert(state->last_str == NULL);
5072
5073 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5074 if (!seq)
5075 goto error;
5076
Eric V. Smith235a6f02015-09-19 14:51:32 -04005077 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
5078
5079error:
5080 FstringParser_Dealloc(state);
5081 return NULL;
5082}
5083
Eric V. Smith451d0e32016-09-09 21:56:20 -04005084/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5085 at end, parse it into an expr_ty. Return NULL on error. Adjust
5086 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005087static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005088fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005089 struct compiling *c, const node *n)
5090{
5091 FstringParser state;
5092
5093 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005094 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005095 c, n) < 0) {
5096 FstringParser_Dealloc(&state);
5097 return NULL;
5098 }
5099
5100 return FstringParser_Finish(&state, c, n);
5101}
5102
5103/* n is a Python string literal, including the bracketing quote
5104 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005105 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005106 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005107 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5108 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005109*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005110static int
5111parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5112 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005113{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005114 size_t len;
5115 const char *s = STR(n);
5116 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005117 int fmode = 0;
5118 *bytesmode = 0;
5119 *rawmode = 0;
5120 *result = NULL;
5121 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005122 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005123 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005124 if (quote == 'b' || quote == 'B') {
5125 quote = *++s;
5126 *bytesmode = 1;
5127 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005128 else if (quote == 'u' || quote == 'U') {
5129 quote = *++s;
5130 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005131 else if (quote == 'r' || quote == 'R') {
5132 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005133 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005134 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005135 else if (quote == 'f' || quote == 'F') {
5136 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005137 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005138 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005139 else {
5140 break;
5141 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005142 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005143 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005144 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005145 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005146 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005147 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005148 if (quote != '\'' && quote != '\"') {
5149 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005150 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005151 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005152 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005153 s++;
5154 len = strlen(s);
5155 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005156 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005157 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005158 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005159 }
5160 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005161 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005162 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005163 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005164 }
5165 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005166 /* A triple quoted string. We've already skipped one quote at
5167 the start and one at the end of the string. Now skip the
5168 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005169 s += 2;
5170 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005171 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005172 if (s[--len] != quote || s[--len] != quote) {
5173 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005174 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005175 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005176 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005177
Eric V. Smith451d0e32016-09-09 21:56:20 -04005178 if (fmode) {
5179 /* Just return the bytes. The caller will parse the resulting
5180 string. */
5181 *fstr = s;
5182 *fstrlen = len;
5183 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005184 }
5185
Eric V. Smith451d0e32016-09-09 21:56:20 -04005186 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005187 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005188 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005189 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005190 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005191 const char *ch;
5192 for (ch = s; *ch; ch++) {
5193 if (Py_CHARMASK(*ch) >= 0x80) {
5194 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005195 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005196 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005197 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005198 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005199 if (*rawmode)
5200 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005201 else
Eric V. Smith56466482016-10-31 14:46:26 -04005202 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005203 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005204 if (*rawmode)
5205 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005206 else
Eric V. Smith56466482016-10-31 14:46:26 -04005207 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005208 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005209 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005210}
5211
Eric V. Smith235a6f02015-09-19 14:51:32 -04005212/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5213 each STRING atom, and process it as needed. For bytes, just
5214 concatenate them together, and the result will be a Bytes node. For
5215 normal strings and f-strings, concatenate them together. The result
5216 will be a Str node if there were no f-strings; a FormattedValue
5217 node if there's just an f-string (with no leading or trailing
5218 literals), or a JoinedStr node if there are multiple f-strings or
5219 any literals involved. */
5220static expr_ty
5221parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005222{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005223 int bytesmode = 0;
5224 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005225 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005226
5227 FstringParser state;
5228 FstringParser_Init(&state);
5229
5230 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005231 int this_bytesmode;
5232 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005233 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005234 const char *fstr;
5235 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005236
5237 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005238 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5239 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005240 goto error;
5241
5242 /* Check that we're not mixing bytes with unicode. */
5243 if (i != 0 && bytesmode != this_bytesmode) {
5244 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005245 /* s is NULL if the current string part is an f-string. */
5246 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005247 goto error;
5248 }
5249 bytesmode = this_bytesmode;
5250
Eric V. Smith451d0e32016-09-09 21:56:20 -04005251 if (fstr != NULL) {
5252 int result;
5253 assert(s == NULL && !bytesmode);
5254 /* This is an f-string. Parse and concatenate it. */
5255 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5256 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005257 if (result < 0)
5258 goto error;
5259 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005260 /* A string or byte string. */
5261 assert(s != NULL && fstr == NULL);
5262
Eric V. Smith451d0e32016-09-09 21:56:20 -04005263 assert(bytesmode ? PyBytes_CheckExact(s) :
5264 PyUnicode_CheckExact(s));
5265
Eric V. Smith451d0e32016-09-09 21:56:20 -04005266 if (bytesmode) {
5267 /* For bytes, concat as we go. */
5268 if (i == 0) {
5269 /* First time, just remember this value. */
5270 bytes_str = s;
5271 } else {
5272 PyBytes_ConcatAndDel(&bytes_str, s);
5273 if (!bytes_str)
5274 goto error;
5275 }
5276 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005277 /* This is a regular string. Concatenate it. */
5278 if (FstringParser_ConcatAndDel(&state, s) < 0)
5279 goto error;
5280 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005281 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005282 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005283 if (bytesmode) {
5284 /* Just return the bytes object and we're done. */
5285 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5286 goto error;
5287 return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
5288 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005289
Eric V. Smith235a6f02015-09-19 14:51:32 -04005290 /* We're not a bytes string, bytes_str should never have been set. */
5291 assert(bytes_str == NULL);
5292
5293 return FstringParser_Finish(&state, c, n);
5294
5295error:
5296 Py_XDECREF(bytes_str);
5297 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005298 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005299}