blob: 6989965efabbad2b4976b51f49e47a3472238f98 [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:
Barry Warsawb2e57942017-09-14 18:13:16 -0700103 Py_UNREACHABLE();
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500104 }
105}
106
107static int
108validate_arguments(arguments_ty args)
109{
110 if (!validate_args(args->args))
111 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700112 if (args->vararg && args->vararg->annotation
113 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500114 return 0;
115 }
116 if (!validate_args(args->kwonlyargs))
117 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100118 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700119 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500120 return 0;
121 }
122 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
123 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
124 return 0;
125 }
126 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
127 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
128 "kw_defaults on arguments");
129 return 0;
130 }
131 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
132}
133
134static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100135validate_constant(PyObject *value)
136{
137 if (value == Py_None || value == Py_Ellipsis)
138 return 1;
139
140 if (PyLong_CheckExact(value)
141 || PyFloat_CheckExact(value)
142 || PyComplex_CheckExact(value)
143 || PyBool_Check(value)
144 || PyUnicode_CheckExact(value)
145 || PyBytes_CheckExact(value))
146 return 1;
147
148 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
149 PyObject *it;
150
151 it = PyObject_GetIter(value);
152 if (it == NULL)
153 return 0;
154
155 while (1) {
156 PyObject *item = PyIter_Next(it);
157 if (item == NULL) {
158 if (PyErr_Occurred()) {
159 Py_DECREF(it);
160 return 0;
161 }
162 break;
163 }
164
165 if (!validate_constant(item)) {
166 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100167 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100168 return 0;
169 }
Victor Stinner726f6902016-01-27 00:11:47 +0100170 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100171 }
172
173 Py_DECREF(it);
174 return 1;
175 }
176
177 return 0;
178}
179
180static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500181validate_expr(expr_ty exp, expr_context_ty ctx)
182{
183 int check_ctx = 1;
184 expr_context_ty actual_ctx;
185
186 /* First check expression context. */
187 switch (exp->kind) {
188 case Attribute_kind:
189 actual_ctx = exp->v.Attribute.ctx;
190 break;
191 case Subscript_kind:
192 actual_ctx = exp->v.Subscript.ctx;
193 break;
194 case Starred_kind:
195 actual_ctx = exp->v.Starred.ctx;
196 break;
197 case Name_kind:
198 actual_ctx = exp->v.Name.ctx;
199 break;
200 case List_kind:
201 actual_ctx = exp->v.List.ctx;
202 break;
203 case Tuple_kind:
204 actual_ctx = exp->v.Tuple.ctx;
205 break;
206 default:
207 if (ctx != Load) {
208 PyErr_Format(PyExc_ValueError, "expression which can't be "
209 "assigned to in %s context", expr_context_name(ctx));
210 return 0;
211 }
212 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100213 /* set actual_ctx to prevent gcc warning */
214 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500215 }
216 if (check_ctx && actual_ctx != ctx) {
217 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
218 expr_context_name(ctx), expr_context_name(actual_ctx));
219 return 0;
220 }
221
222 /* Now validate expression. */
223 switch (exp->kind) {
224 case BoolOp_kind:
225 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
226 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
227 return 0;
228 }
229 return validate_exprs(exp->v.BoolOp.values, Load, 0);
230 case BinOp_kind:
231 return validate_expr(exp->v.BinOp.left, Load) &&
232 validate_expr(exp->v.BinOp.right, Load);
233 case UnaryOp_kind:
234 return validate_expr(exp->v.UnaryOp.operand, Load);
235 case Lambda_kind:
236 return validate_arguments(exp->v.Lambda.args) &&
237 validate_expr(exp->v.Lambda.body, Load);
238 case IfExp_kind:
239 return validate_expr(exp->v.IfExp.test, Load) &&
240 validate_expr(exp->v.IfExp.body, Load) &&
241 validate_expr(exp->v.IfExp.orelse, Load);
242 case Dict_kind:
243 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
244 PyErr_SetString(PyExc_ValueError,
245 "Dict doesn't have the same number of keys as values");
246 return 0;
247 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400248 /* null_ok=1 for keys expressions to allow dict unpacking to work in
249 dict literals, i.e. ``{**{a:b}}`` */
250 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
251 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500252 case Set_kind:
253 return validate_exprs(exp->v.Set.elts, Load, 0);
254#define COMP(NAME) \
255 case NAME ## _kind: \
256 return validate_comprehension(exp->v.NAME.generators) && \
257 validate_expr(exp->v.NAME.elt, Load);
258 COMP(ListComp)
259 COMP(SetComp)
260 COMP(GeneratorExp)
261#undef COMP
262 case DictComp_kind:
263 return validate_comprehension(exp->v.DictComp.generators) &&
264 validate_expr(exp->v.DictComp.key, Load) &&
265 validate_expr(exp->v.DictComp.value, Load);
266 case Yield_kind:
267 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500268 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000269 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400270 case Await_kind:
271 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500272 case Compare_kind:
273 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
274 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
275 return 0;
276 }
277 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
278 asdl_seq_LEN(exp->v.Compare.ops)) {
279 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
280 "of comparators and operands");
281 return 0;
282 }
283 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
284 validate_expr(exp->v.Compare.left, Load);
285 case Call_kind:
286 return validate_expr(exp->v.Call.func, Load) &&
287 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400288 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100289 case Constant_kind:
290 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100291 PyErr_Format(PyExc_TypeError,
292 "got an invalid type in Constant: %s",
293 Py_TYPE(exp->v.Constant.value)->tp_name);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100294 return 0;
295 }
296 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500297 case Num_kind: {
298 PyObject *n = exp->v.Num.n;
299 if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) &&
300 !PyComplex_CheckExact(n)) {
301 PyErr_SetString(PyExc_TypeError, "non-numeric type in Num");
302 return 0;
303 }
304 return 1;
305 }
306 case Str_kind: {
307 PyObject *s = exp->v.Str.s;
308 if (!PyUnicode_CheckExact(s)) {
309 PyErr_SetString(PyExc_TypeError, "non-string type in Str");
310 return 0;
311 }
312 return 1;
313 }
Eric V. Smith235a6f02015-09-19 14:51:32 -0400314 case JoinedStr_kind:
315 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
316 case FormattedValue_kind:
317 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
318 return 0;
319 if (exp->v.FormattedValue.format_spec)
320 return validate_expr(exp->v.FormattedValue.format_spec, Load);
321 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500322 case Bytes_kind: {
323 PyObject *b = exp->v.Bytes.s;
324 if (!PyBytes_CheckExact(b)) {
325 PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes");
326 return 0;
327 }
328 return 1;
329 }
330 case Attribute_kind:
331 return validate_expr(exp->v.Attribute.value, Load);
332 case Subscript_kind:
333 return validate_slice(exp->v.Subscript.slice) &&
334 validate_expr(exp->v.Subscript.value, Load);
335 case Starred_kind:
336 return validate_expr(exp->v.Starred.value, ctx);
337 case List_kind:
338 return validate_exprs(exp->v.List.elts, ctx, 0);
339 case Tuple_kind:
340 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
341 /* These last cases don't have any checking. */
342 case Name_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -0500343 case NameConstant_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500344 case Ellipsis_kind:
345 return 1;
346 default:
347 PyErr_SetString(PyExc_SystemError, "unexpected expression");
348 return 0;
349 }
350}
351
352static int
353validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
354{
355 if (asdl_seq_LEN(seq))
356 return 1;
357 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
358 return 0;
359}
360
361static int
362validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
363{
364 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
365 validate_exprs(targets, ctx, 0);
366}
367
368static int
INADA Naokicb41b272017-02-23 00:31:59 +0900369validate_body(asdl_seq *body, const char *owner, int allowempty)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500370{
INADA Naokicb41b272017-02-23 00:31:59 +0900371 if (!allowempty && !validate_nonempty_seq(body, "body", owner)) {
372 return 0;
373 }
374 return validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500375}
376
377static int
378validate_stmt(stmt_ty stmt)
379{
380 int i;
381 switch (stmt->kind) {
382 case FunctionDef_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900383 return validate_body(stmt->v.FunctionDef.body, "FunctionDef",
384 stmt->v.FunctionDef.docstring != NULL) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500385 validate_arguments(stmt->v.FunctionDef.args) &&
386 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
387 (!stmt->v.FunctionDef.returns ||
388 validate_expr(stmt->v.FunctionDef.returns, Load));
389 case ClassDef_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900390 return validate_body(stmt->v.ClassDef.body, "ClassDef",
391 stmt->v.ClassDef.docstring != NULL) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500392 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
393 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400394 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500395 case Return_kind:
396 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
397 case Delete_kind:
398 return validate_assignlist(stmt->v.Delete.targets, Del);
399 case Assign_kind:
400 return validate_assignlist(stmt->v.Assign.targets, Store) &&
401 validate_expr(stmt->v.Assign.value, Load);
402 case AugAssign_kind:
403 return validate_expr(stmt->v.AugAssign.target, Store) &&
404 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700405 case AnnAssign_kind:
406 if (stmt->v.AnnAssign.target->kind != Name_kind &&
407 stmt->v.AnnAssign.simple) {
408 PyErr_SetString(PyExc_TypeError,
409 "AnnAssign with simple non-Name target");
410 return 0;
411 }
412 return validate_expr(stmt->v.AnnAssign.target, Store) &&
413 (!stmt->v.AnnAssign.value ||
414 validate_expr(stmt->v.AnnAssign.value, Load)) &&
415 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500416 case For_kind:
417 return validate_expr(stmt->v.For.target, Store) &&
418 validate_expr(stmt->v.For.iter, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900419 validate_body(stmt->v.For.body, "For", 0) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500420 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400421 case AsyncFor_kind:
422 return validate_expr(stmt->v.AsyncFor.target, Store) &&
423 validate_expr(stmt->v.AsyncFor.iter, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900424 validate_body(stmt->v.AsyncFor.body, "AsyncFor", 0) &&
Yury Selivanov75445082015-05-11 22:57:16 -0400425 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500426 case While_kind:
427 return validate_expr(stmt->v.While.test, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900428 validate_body(stmt->v.While.body, "While", 0) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500429 validate_stmts(stmt->v.While.orelse);
430 case If_kind:
431 return validate_expr(stmt->v.If.test, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900432 validate_body(stmt->v.If.body, "If", 0) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500433 validate_stmts(stmt->v.If.orelse);
434 case With_kind:
435 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
436 return 0;
437 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
438 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
439 if (!validate_expr(item->context_expr, Load) ||
440 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
441 return 0;
442 }
INADA Naokicb41b272017-02-23 00:31:59 +0900443 return validate_body(stmt->v.With.body, "With", 0);
Yury Selivanov75445082015-05-11 22:57:16 -0400444 case AsyncWith_kind:
445 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
446 return 0;
447 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
448 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
449 if (!validate_expr(item->context_expr, Load) ||
450 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
451 return 0;
452 }
INADA Naokicb41b272017-02-23 00:31:59 +0900453 return validate_body(stmt->v.AsyncWith.body, "AsyncWith", 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500454 case Raise_kind:
455 if (stmt->v.Raise.exc) {
456 return validate_expr(stmt->v.Raise.exc, Load) &&
457 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
458 }
459 if (stmt->v.Raise.cause) {
460 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
461 return 0;
462 }
463 return 1;
464 case Try_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900465 if (!validate_body(stmt->v.Try.body, "Try", 0))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500466 return 0;
467 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
468 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
469 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
470 return 0;
471 }
472 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
473 asdl_seq_LEN(stmt->v.Try.orelse)) {
474 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
475 return 0;
476 }
477 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
478 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
479 if ((handler->v.ExceptHandler.type &&
480 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
INADA Naokicb41b272017-02-23 00:31:59 +0900481 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler", 0))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500482 return 0;
483 }
484 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
485 validate_stmts(stmt->v.Try.finalbody)) &&
486 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
487 validate_stmts(stmt->v.Try.orelse));
488 case Assert_kind:
489 return validate_expr(stmt->v.Assert.test, Load) &&
490 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
491 case Import_kind:
492 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
493 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300494 if (stmt->v.ImportFrom.level < 0) {
495 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500496 return 0;
497 }
498 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
499 case Global_kind:
500 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
501 case Nonlocal_kind:
502 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
503 case Expr_kind:
504 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400505 case AsyncFunctionDef_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900506 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef",
507 stmt->v.AsyncFunctionDef.docstring != NULL) &&
Yury Selivanov75445082015-05-11 22:57:16 -0400508 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
509 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
510 (!stmt->v.AsyncFunctionDef.returns ||
511 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500512 case Pass_kind:
513 case Break_kind:
514 case Continue_kind:
515 return 1;
516 default:
517 PyErr_SetString(PyExc_SystemError, "unexpected statement");
518 return 0;
519 }
520}
521
522static int
523validate_stmts(asdl_seq *seq)
524{
525 int i;
526 for (i = 0; i < asdl_seq_LEN(seq); i++) {
527 stmt_ty stmt = asdl_seq_GET(seq, i);
528 if (stmt) {
529 if (!validate_stmt(stmt))
530 return 0;
531 }
532 else {
533 PyErr_SetString(PyExc_ValueError,
534 "None disallowed in statement list");
535 return 0;
536 }
537 }
538 return 1;
539}
540
541static int
542validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
543{
544 int i;
545 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
546 expr_ty expr = asdl_seq_GET(exprs, i);
547 if (expr) {
548 if (!validate_expr(expr, ctx))
549 return 0;
550 }
551 else if (!null_ok) {
552 PyErr_SetString(PyExc_ValueError,
553 "None disallowed in expression list");
554 return 0;
555 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100556
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500557 }
558 return 1;
559}
560
561int
562PyAST_Validate(mod_ty mod)
563{
564 int res = 0;
565
566 switch (mod->kind) {
567 case Module_kind:
568 res = validate_stmts(mod->v.Module.body);
569 break;
570 case Interactive_kind:
571 res = validate_stmts(mod->v.Interactive.body);
572 break;
573 case Expression_kind:
574 res = validate_expr(mod->v.Expression.body, Load);
575 break;
576 case Suite_kind:
577 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
578 break;
579 default:
580 PyErr_SetString(PyExc_SystemError, "impossible module node");
581 res = 0;
582 break;
583 }
584 return res;
585}
586
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500587/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500588#include "grammar.h"
589#include "parsetok.h"
590#include "graminit.h"
591
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592/* Data structure used internally */
593struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400594 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200595 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500596 PyObject *c_normalize; /* Normalization function from unicodedata. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000597};
598
599static asdl_seq *seq_for_testlist(struct compiling *, const node *);
600static expr_ty ast_for_expr(struct compiling *, const node *);
601static stmt_ty ast_for_stmt(struct compiling *, const node *);
INADA Naokicb41b272017-02-23 00:31:59 +0900602static asdl_seq *ast_for_body(struct compiling *c, const node *n,
603 string *docstring);
604static string docstring_from_stmts(asdl_seq *stmts);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000605static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
606 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000607static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000608static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000609
Yury Selivanov75445082015-05-11 22:57:16 -0400610static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
611static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
612
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613/* Note different signature for ast_for_call */
614static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
615
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000616static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400617static expr_ty parsestrplus(struct compiling *, const node *n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000618
Nick Coghlan650f0d02007-04-15 12:05:43 +0000619#define COMP_GENEXP 0
620#define COMP_LISTCOMP 1
621#define COMP_SETCOMP 2
622
Benjamin Peterson55e00432012-01-16 17:22:31 -0500623static int
624init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000625{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500626 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
627 if (!m)
628 return 0;
629 c->c_normalize = PyObject_GetAttrString(m, "normalize");
630 Py_DECREF(m);
631 if (!c->c_normalize)
632 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500633 return 1;
634}
635
636static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400637new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500638{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400639 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500640 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000641 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500642 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500643 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000644 /* Check whether there are non-ASCII characters in the
645 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500646 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200647 PyObject *id2;
Oren Milman7dc46d82017-09-30 20:16:24 +0300648 _Py_IDENTIFIER(NFKC);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500649 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500650 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200651 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500652 }
Oren Milman7dc46d82017-09-30 20:16:24 +0300653 PyObject *form = _PyUnicode_FromId(&PyId_NFKC);
654 if (form == NULL) {
655 Py_DECREF(id);
656 return NULL;
657 }
658 PyObject *args[2] = {form, id};
659 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500660 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200661 if (!id2)
662 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300663 if (!PyUnicode_Check(id2)) {
664 PyErr_Format(PyExc_TypeError,
665 "unicodedata.normalize() must return a string, not "
666 "%.200s",
667 Py_TYPE(id2)->tp_name);
668 Py_DECREF(id2);
669 return NULL;
670 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200671 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000672 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000673 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200674 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
675 Py_DECREF(id);
676 return NULL;
677 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000678 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000679}
680
Benjamin Peterson55e00432012-01-16 17:22:31 -0500681#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000683static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400684ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000685{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400686 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000687
Victor Stinner14e461d2013-08-26 22:28:21 +0200688 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000689 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000690 Py_INCREF(Py_None);
691 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000692 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200693 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400694 if (!tmp)
695 return 0;
696 errstr = PyUnicode_FromString(errmsg);
697 if (!errstr) {
698 Py_DECREF(tmp);
699 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000700 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000701 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702 Py_DECREF(errstr);
703 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400704 if (value) {
705 PyErr_SetObject(PyExc_SyntaxError, value);
706 Py_DECREF(value);
707 }
708 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000709}
710
711/* num_stmts() returns number of contained statements.
712
713 Use this routine to determine how big a sequence is needed for
714 the statements in a parse tree. Its raison d'etre is this bit of
715 grammar:
716
717 stmt: simple_stmt | compound_stmt
718 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
719
720 A simple_stmt can contain multiple small_stmt elements joined
721 by semicolons. If the arg is a simple_stmt, the number of
722 small_stmt elements is returned.
723*/
724
725static int
726num_stmts(const node *n)
727{
728 int i, l;
729 node *ch;
730
731 switch (TYPE(n)) {
732 case single_input:
733 if (TYPE(CHILD(n, 0)) == NEWLINE)
734 return 0;
735 else
736 return num_stmts(CHILD(n, 0));
737 case file_input:
738 l = 0;
739 for (i = 0; i < NCH(n); i++) {
740 ch = CHILD(n, i);
741 if (TYPE(ch) == stmt)
742 l += num_stmts(ch);
743 }
744 return l;
745 case stmt:
746 return num_stmts(CHILD(n, 0));
747 case compound_stmt:
748 return 1;
749 case simple_stmt:
750 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
751 case suite:
752 if (NCH(n) == 1)
753 return num_stmts(CHILD(n, 0));
754 else {
755 l = 0;
756 for (i = 2; i < (NCH(n) - 1); i++)
757 l += num_stmts(CHILD(n, i));
758 return l;
759 }
760 default: {
761 char buf[128];
762
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000763 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764 TYPE(n), NCH(n));
765 Py_FatalError(buf);
766 }
767 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700768 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769}
770
771/* Transform the CST rooted at node * to the appropriate AST
772*/
773
774mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200775PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
776 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000778 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779 asdl_seq *stmts = NULL;
780 stmt_ty s;
781 node *ch;
782 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500783 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400785 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200786 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400787 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800788 c.c_normalize = NULL;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800789
790 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792
Jeremy Hyltona8293132006-02-28 17:58:27 +0000793 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794 switch (TYPE(n)) {
795 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200796 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500798 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799 for (i = 0; i < NCH(n) - 1; i++) {
800 ch = CHILD(n, i);
801 if (TYPE(ch) == NEWLINE)
802 continue;
803 REQ(ch, stmt);
804 num = num_stmts(ch);
805 if (num == 1) {
806 s = ast_for_stmt(&c, ch);
807 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500808 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000809 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810 }
811 else {
812 ch = CHILD(ch, 0);
813 REQ(ch, simple_stmt);
814 for (j = 0; j < num; j++) {
815 s = ast_for_stmt(&c, CHILD(ch, j * 2));
816 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500817 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000818 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819 }
820 }
821 }
INADA Naokicb41b272017-02-23 00:31:59 +0900822 res = Module(stmts, docstring_from_stmts(stmts), arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500823 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 case eval_input: {
825 expr_ty testlist_ast;
826
Nick Coghlan650f0d02007-04-15 12:05:43 +0000827 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000828 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500830 goto out;
831 res = Expression(testlist_ast, arena);
832 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833 }
834 case single_input:
835 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200836 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500838 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000839 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
840 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000841 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500842 goto out;
843 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 }
845 else {
846 n = CHILD(n, 0);
847 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200848 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500850 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000851 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000852 s = ast_for_stmt(&c, n);
853 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500854 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855 asdl_seq_SET(stmts, 0, s);
856 }
857 else {
858 /* Only a simple_stmt can contain multiple statements. */
859 REQ(n, simple_stmt);
860 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 if (TYPE(CHILD(n, i)) == NEWLINE)
862 break;
863 s = ast_for_stmt(&c, CHILD(n, i));
864 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500865 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866 asdl_seq_SET(stmts, i / 2, s);
867 }
868 }
869
Benjamin Peterson55e00432012-01-16 17:22:31 -0500870 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500872 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000874 PyErr_Format(PyExc_SystemError,
875 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500876 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500878 out:
879 if (c.c_normalize) {
880 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500881 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500882 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000883}
884
Victor Stinner14e461d2013-08-26 22:28:21 +0200885mod_ty
886PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
887 PyArena *arena)
888{
889 mod_ty mod;
890 PyObject *filename;
891 filename = PyUnicode_DecodeFSDefault(filename_str);
892 if (filename == NULL)
893 return NULL;
894 mod = PyAST_FromNodeObject(n, flags, filename, arena);
895 Py_DECREF(filename);
896 return mod;
897
898}
899
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000900/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
901*/
902
903static operator_ty
904get_operator(const node *n)
905{
906 switch (TYPE(n)) {
907 case VBAR:
908 return BitOr;
909 case CIRCUMFLEX:
910 return BitXor;
911 case AMPER:
912 return BitAnd;
913 case LEFTSHIFT:
914 return LShift;
915 case RIGHTSHIFT:
916 return RShift;
917 case PLUS:
918 return Add;
919 case MINUS:
920 return Sub;
921 case STAR:
922 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400923 case AT:
924 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000925 case SLASH:
926 return Div;
927 case DOUBLESLASH:
928 return FloorDiv;
929 case PERCENT:
930 return Mod;
931 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000932 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000933 }
934}
935
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200936static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000937 "None",
938 "True",
939 "False",
940 NULL,
941};
942
943static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400944forbidden_name(struct compiling *c, identifier name, const node *n,
945 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000946{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000947 assert(PyUnicode_Check(name));
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200948 if (_PyUnicode_EqualToASCIIString(name, "__debug__")) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400949 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000950 return 1;
951 }
952 if (full_checks) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200953 const char * const *p;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000954 for (p = FORBIDDEN; *p; p++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200955 if (_PyUnicode_EqualToASCIIString(name, *p)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400956 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000957 return 1;
958 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000959 }
960 }
961 return 0;
962}
963
Jeremy Hyltona8293132006-02-28 17:58:27 +0000964/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965
966 Only sets context for expr kinds that "can appear in assignment context"
967 (according to ../Parser/Python.asdl). For other expr kinds, it sets
968 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969*/
970
971static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000972set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000973{
974 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000975 /* If a particular expression type can't be used for assign / delete,
976 set expr_name to its name and an error message will be generated.
977 */
978 const char* expr_name = NULL;
979
980 /* The ast defines augmented store and load contexts, but the
981 implementation here doesn't actually use them. The code may be
982 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000983 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000984 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000985 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000986 */
987 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988
989 switch (e->kind) {
990 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000991 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400992 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000993 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000994 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000996 e->v.Subscript.ctx = ctx;
997 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000998 case Starred_kind:
999 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001000 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001001 return 0;
1002 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +00001004 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001005 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001006 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001007 }
1008 e->v.Name.ctx = ctx;
1009 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001011 e->v.List.ctx = ctx;
1012 s = e->v.List.elts;
1013 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001015 e->v.Tuple.ctx = ctx;
1016 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001017 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001018 case Lambda_kind:
1019 expr_name = "lambda";
1020 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001022 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001023 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001024 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001026 case UnaryOp_kind:
1027 expr_name = "operator";
1028 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001030 expr_name = "generator expression";
1031 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001032 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001033 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001034 expr_name = "yield expression";
1035 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001036 case Await_kind:
1037 expr_name = "await expression";
1038 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001039 case ListComp_kind:
1040 expr_name = "list comprehension";
1041 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001042 case SetComp_kind:
1043 expr_name = "set comprehension";
1044 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001045 case DictComp_kind:
1046 expr_name = "dict comprehension";
1047 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001048 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +00001049 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050 case Num_kind:
1051 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -05001052 case Bytes_kind:
Eric V. Smith235a6f02015-09-19 14:51:32 -04001053 case JoinedStr_kind:
1054 case FormattedValue_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001055 expr_name = "literal";
1056 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001057 case NameConstant_kind:
1058 expr_name = "keyword";
1059 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001060 case Ellipsis_kind:
1061 expr_name = "Ellipsis";
1062 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001063 case Compare_kind:
1064 expr_name = "comparison";
1065 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001066 case IfExp_kind:
1067 expr_name = "conditional expression";
1068 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001069 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 PyErr_Format(PyExc_SystemError,
1071 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001072 e->kind, e->lineno);
1073 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001075 /* Check for error string set by switch */
1076 if (expr_name) {
1077 char buf[300];
1078 PyOS_snprintf(buf, sizeof(buf),
1079 "can't %s %s",
1080 ctx == Store ? "assign to" : "delete",
1081 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001082 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001083 }
1084
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087 */
1088 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001089 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001090
Thomas Wouters89f507f2006-12-13 04:49:30 +00001091 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001092 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001093 return 0;
1094 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001095 }
1096 return 1;
1097}
1098
1099static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001100ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101{
1102 REQ(n, augassign);
1103 n = CHILD(n, 0);
1104 switch (STR(n)[0]) {
1105 case '+':
1106 return Add;
1107 case '-':
1108 return Sub;
1109 case '/':
1110 if (STR(n)[1] == '/')
1111 return FloorDiv;
1112 else
1113 return Div;
1114 case '%':
1115 return Mod;
1116 case '<':
1117 return LShift;
1118 case '>':
1119 return RShift;
1120 case '&':
1121 return BitAnd;
1122 case '^':
1123 return BitXor;
1124 case '|':
1125 return BitOr;
1126 case '*':
1127 if (STR(n)[1] == '*')
1128 return Pow;
1129 else
1130 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001131 case '@':
1132 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001134 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001135 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136 }
1137}
1138
1139static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001140ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001142 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143 |'is' 'not'
1144 */
1145 REQ(n, comp_op);
1146 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001147 n = CHILD(n, 0);
1148 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001149 case LESS:
1150 return Lt;
1151 case GREATER:
1152 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001153 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 return Eq;
1155 case LESSEQUAL:
1156 return LtE;
1157 case GREATEREQUAL:
1158 return GtE;
1159 case NOTEQUAL:
1160 return NotEq;
1161 case NAME:
1162 if (strcmp(STR(n), "in") == 0)
1163 return In;
1164 if (strcmp(STR(n), "is") == 0)
1165 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001166 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001168 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001170 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001171 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172 }
1173 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001174 /* handle "not in" and "is not" */
1175 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176 case NAME:
1177 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1178 return NotIn;
1179 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1180 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001181 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001183 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001185 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001186 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187 }
Neal Norwitz79792652005-11-14 04:25:03 +00001188 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001190 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191}
1192
1193static asdl_seq *
1194seq_for_testlist(struct compiling *c, const node *n)
1195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001197 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1198 */
Armin Rigo31441302005-10-21 12:57:31 +00001199 asdl_seq *seq;
1200 expr_ty expression;
1201 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001202 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001204 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 if (!seq)
1206 return NULL;
1207
1208 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001210 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211
Benjamin Peterson4905e802009-09-27 02:43:28 +00001212 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001213 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215
1216 assert(i / 2 < seq->size);
1217 asdl_seq_SET(seq, i / 2, expression);
1218 }
1219 return seq;
1220}
1221
Neal Norwitzc1505362006-12-28 06:47:50 +00001222static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001223ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001224{
1225 identifier name;
1226 expr_ty annotation = NULL;
1227 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001228 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001229
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001230 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001231 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001232 name = NEW_IDENTIFIER(ch);
1233 if (!name)
1234 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001235 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001236 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001237
1238 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1239 annotation = ast_for_expr(c, CHILD(n, 2));
1240 if (!annotation)
1241 return NULL;
1242 }
1243
Victor Stinnerc106c682015-11-06 17:01:48 +01001244 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001245 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001246 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001247 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248}
1249
Guido van Rossum4f72a782006-10-27 23:31:49 +00001250/* returns -1 if failed to handle keyword only arguments
1251 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001252 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001253 ^^^
1254 start pointing here
1255 */
1256static int
1257handle_keywordonly_args(struct compiling *c, const node *n, int start,
1258 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1259{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001260 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001261 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001262 expr_ty expression, annotation;
1263 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001264 int i = start;
1265 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001266
1267 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001268 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001269 return -1;
1270 }
1271 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001272 while (i < NCH(n)) {
1273 ch = CHILD(n, i);
1274 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001275 case vfpdef:
1276 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001277 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001278 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001279 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001280 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001281 asdl_seq_SET(kwdefaults, j, expression);
1282 i += 2; /* '=' and test */
1283 }
1284 else { /* setting NULL if no default value exists */
1285 asdl_seq_SET(kwdefaults, j, NULL);
1286 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001287 if (NCH(ch) == 3) {
1288 /* ch is NAME ':' test */
1289 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001290 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001291 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001292 }
1293 else {
1294 annotation = NULL;
1295 }
1296 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001297 argname = NEW_IDENTIFIER(ch);
1298 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001299 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001300 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001301 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001302 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1303 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001304 if (!arg)
1305 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001306 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001307 i += 2; /* the name and the comma */
1308 break;
1309 case DOUBLESTAR:
1310 return i;
1311 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001312 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001313 goto error;
1314 }
1315 }
1316 return i;
1317 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001319}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320
Jeremy Hyltona8293132006-02-28 17:58:27 +00001321/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001322
1323static arguments_ty
1324ast_for_arguments(struct compiling *c, const node *n)
1325{
Neal Norwitzc1505362006-12-28 06:47:50 +00001326 /* This function handles both typedargslist (function definition)
1327 and varargslist (lambda definition).
1328
1329 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001330 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1331 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1332 | '**' tfpdef [',']]]
1333 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1334 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001335 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001336 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1337 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1338 | '**' vfpdef [',']]]
1339 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1340 | '**' vfpdef [',']
1341 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001342 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001343
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001345 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1346 int nposdefaults = 0, found_default = 0;
1347 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001348 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001349 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350 node *ch;
1351
1352 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001353 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001354 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001355 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001357 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001358
Jeremy Hyltone921e022008-07-17 16:37:17 +00001359 /* First count the number of positional args & defaults. The
1360 variable i is the loop index for this for loop and the next.
1361 The next loop picks up where the first leaves off.
1362 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001364 ch = CHILD(n, i);
1365 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001366 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001367 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001368 if (i < NCH(n) && /* skip argument following star */
1369 (TYPE(CHILD(n, i)) == tfpdef ||
1370 TYPE(CHILD(n, i)) == vfpdef)) {
1371 i++;
1372 }
1373 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001374 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001375 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001376 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001377 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001378 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001380 defaults for keyword only args */
1381 for ( ; i < NCH(n); ++i) {
1382 ch = CHILD(n, i);
1383 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001384 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001385 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001386 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001387 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001388 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001389 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001390 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001391 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001392 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001394 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001395 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001396 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001398 since we set NULL as default for keyword only argument w/o default
1399 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001400 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001401 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001402 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001403 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001404
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001405 /* tfpdef: NAME [':' test]
1406 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407 */
1408 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001409 j = 0; /* index for defaults */
1410 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001411 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001412 ch = CHILD(n, i);
1413 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001414 case tfpdef:
1415 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1417 anything other than EQUAL or a comma? */
1418 /* XXX Should NCH(n) check be made a separate check? */
1419 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001420 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1421 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001422 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001423 assert(posdefaults != NULL);
1424 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001426 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001428 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001429 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001430 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001431 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001432 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001433 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001434 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001435 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001436 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437 i += 2; /* the name and the comma */
1438 break;
1439 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001440 if (i+1 >= NCH(n) ||
1441 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001442 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001443 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001444 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001445 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001446 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001447 if (TYPE(ch) == COMMA) {
1448 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001449 i += 2; /* now follows keyword only arguments */
1450 res = handle_keywordonly_args(c, n, i,
1451 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001452 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001453 i = res; /* res has new position to process */
1454 }
1455 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001456 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001457 if (!vararg)
1458 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001459
Guido van Rossum4f72a782006-10-27 23:31:49 +00001460 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001461 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1462 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001463 int res = 0;
1464 res = handle_keywordonly_args(c, n, i,
1465 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001466 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001467 i = res; /* res has new position to process */
1468 }
1469 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470 break;
1471 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001472 ch = CHILD(n, i+1); /* tfpdef */
1473 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001474 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001475 if (!kwarg)
1476 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001477 i += 3;
1478 break;
1479 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001480 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481 "unexpected node in varargslist: %d @ %d",
1482 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001483 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001484 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001486 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487}
1488
1489static expr_ty
1490ast_for_dotted_name(struct compiling *c, const node *n)
1491{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001492 expr_ty e;
1493 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001494 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001495 int i;
1496
1497 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001498
1499 lineno = LINENO(n);
1500 col_offset = n->n_col_offset;
1501
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502 id = NEW_IDENTIFIER(CHILD(n, 0));
1503 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001504 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001505 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001507 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508
1509 for (i = 2; i < NCH(n); i+=2) {
1510 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001511 if (!id)
1512 return NULL;
1513 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1514 if (!e)
1515 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516 }
1517
1518 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519}
1520
1521static expr_ty
1522ast_for_decorator(struct compiling *c, const node *n)
1523{
1524 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1525 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001526 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001529 REQ(CHILD(n, 0), AT);
1530 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1533 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001534 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001537 d = name_expr;
1538 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539 }
1540 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001541 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001542 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001543 if (!d)
1544 return NULL;
1545 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001546 }
1547 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001548 d = ast_for_call(c, CHILD(n, 3), name_expr);
1549 if (!d)
1550 return NULL;
1551 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001552 }
1553
1554 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001555}
1556
1557static asdl_seq*
1558ast_for_decorators(struct compiling *c, const node *n)
1559{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001560 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001561 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001562 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001565 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566 if (!decorator_seq)
1567 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001569 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001570 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001571 if (!d)
1572 return NULL;
1573 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574 }
1575 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001576}
1577
1578static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001579ast_for_funcdef_impl(struct compiling *c, const node *n,
1580 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001582 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001583 identifier name;
1584 arguments_ty args;
1585 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001586 expr_ty returns = NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09001587 string docstring;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001588 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589
1590 REQ(n, funcdef);
1591
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001592 name = NEW_IDENTIFIER(CHILD(n, name_i));
1593 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001594 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001595 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001596 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001597 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1598 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001599 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001600 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1601 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1602 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001603 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001604 name_i += 2;
1605 }
INADA Naokicb41b272017-02-23 00:31:59 +09001606 body = ast_for_body(c, CHILD(n, name_i + 3), &docstring);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001607 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001608 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001609
Yury Selivanov75445082015-05-11 22:57:16 -04001610 if (is_async)
1611 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
INADA Naokicb41b272017-02-23 00:31:59 +09001612 docstring, LINENO(n),
Yury Selivanov75445082015-05-11 22:57:16 -04001613 n->n_col_offset, c->c_arena);
1614 else
1615 return FunctionDef(name, args, body, decorator_seq, returns,
INADA Naokicb41b272017-02-23 00:31:59 +09001616 docstring, LINENO(n),
Yury Selivanov75445082015-05-11 22:57:16 -04001617 n->n_col_offset, c->c_arena);
1618}
1619
1620static stmt_ty
1621ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1622{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001623 /* async_funcdef: 'async' funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001624 REQ(n, async_funcdef);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001625 REQ(CHILD(n, 0), NAME);
1626 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001627 REQ(CHILD(n, 1), funcdef);
1628
1629 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1630 1 /* is_async */);
1631}
1632
1633static stmt_ty
1634ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1635{
1636 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1637 return ast_for_funcdef_impl(c, n, decorator_seq,
1638 0 /* is_async */);
1639}
1640
1641
1642static stmt_ty
1643ast_for_async_stmt(struct compiling *c, const node *n)
1644{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001645 /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001646 REQ(n, async_stmt);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001647 REQ(CHILD(n, 0), NAME);
1648 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001649
1650 switch (TYPE(CHILD(n, 1))) {
1651 case funcdef:
1652 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1653 1 /* is_async */);
1654 case with_stmt:
1655 return ast_for_with_stmt(c, CHILD(n, 1),
1656 1 /* is_async */);
1657
1658 case for_stmt:
1659 return ast_for_for_stmt(c, CHILD(n, 1),
1660 1 /* is_async */);
1661
1662 default:
1663 PyErr_Format(PyExc_SystemError,
1664 "invalid async stament: %s",
1665 STR(CHILD(n, 1)));
1666 return NULL;
1667 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668}
1669
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001670static stmt_ty
1671ast_for_decorated(struct compiling *c, const node *n)
1672{
Yury Selivanov75445082015-05-11 22:57:16 -04001673 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001674 stmt_ty thing = NULL;
1675 asdl_seq *decorator_seq = NULL;
1676
1677 REQ(n, decorated);
1678
1679 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1680 if (!decorator_seq)
1681 return NULL;
1682
1683 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001684 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001686
1687 if (TYPE(CHILD(n, 1)) == funcdef) {
1688 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1689 } else if (TYPE(CHILD(n, 1)) == classdef) {
1690 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001691 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1692 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001693 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001694 /* we count the decorators in when talking about the class' or
1695 * function's line number */
1696 if (thing) {
1697 thing->lineno = LINENO(n);
1698 thing->col_offset = n->n_col_offset;
1699 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001700 return thing;
1701}
1702
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001703static expr_ty
1704ast_for_lambdef(struct compiling *c, const node *n)
1705{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001706 /* lambdef: 'lambda' [varargslist] ':' test
1707 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708 arguments_ty args;
1709 expr_ty expression;
1710
1711 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001712 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001713 if (!args)
1714 return NULL;
1715 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001716 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718 }
1719 else {
1720 args = ast_for_arguments(c, CHILD(n, 1));
1721 if (!args)
1722 return NULL;
1723 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001724 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726 }
1727
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001728 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729}
1730
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001731static expr_ty
1732ast_for_ifexpr(struct compiling *c, const node *n)
1733{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001735 expr_ty expression, body, orelse;
1736
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001737 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001738 body = ast_for_expr(c, CHILD(n, 0));
1739 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001740 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001741 expression = ast_for_expr(c, CHILD(n, 2));
1742 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001743 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001744 orelse = ast_for_expr(c, CHILD(n, 4));
1745 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001746 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001747 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1748 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001749}
1750
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001752 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001753
Nick Coghlan650f0d02007-04-15 12:05:43 +00001754 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001755*/
1756
1757static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001758count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001759{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001760 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761
Guido van Rossumd8faa362007-04-27 19:54:29 +00001762 count_comp_for:
1763 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001764 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001765 if (NCH(n) == 2) {
1766 REQ(CHILD(n, 0), NAME);
1767 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1768 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001769 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001770 else if (NCH(n) == 1) {
1771 n = CHILD(n, 0);
1772 }
1773 else {
1774 goto error;
1775 }
1776 if (NCH(n) == (5)) {
1777 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001778 }
1779 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001780 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001781 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001782 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001783 REQ(n, comp_iter);
1784 n = CHILD(n, 0);
1785 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001786 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001787 else if (TYPE(n) == comp_if) {
1788 if (NCH(n) == 3) {
1789 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001790 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001791 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001792 else
1793 return n_fors;
1794 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001795
Jelle Zijlstraac317702017-10-05 20:24:46 -07001796 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001797 /* Should never be reached */
1798 PyErr_SetString(PyExc_SystemError,
1799 "logic error in count_comp_fors");
1800 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001801}
1802
Nick Coghlan650f0d02007-04-15 12:05:43 +00001803/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001804
Nick Coghlan650f0d02007-04-15 12:05:43 +00001805 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806*/
1807
1808static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001809count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001811 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812
Guido van Rossumd8faa362007-04-27 19:54:29 +00001813 while (1) {
1814 REQ(n, comp_iter);
1815 if (TYPE(CHILD(n, 0)) == comp_for)
1816 return n_ifs;
1817 n = CHILD(n, 0);
1818 REQ(n, comp_if);
1819 n_ifs++;
1820 if (NCH(n) == 2)
1821 return n_ifs;
1822 n = CHILD(n, 2);
1823 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001824}
1825
Guido van Rossum992d4a32007-07-11 13:09:30 +00001826static asdl_seq *
1827ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001829 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001830 asdl_seq *comps;
1831
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001832 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001833 if (n_fors == -1)
1834 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001835
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001836 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001837 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001838 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001839
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001840 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001841 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001843 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001844 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001845 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001846 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847
Guido van Rossum992d4a32007-07-11 13:09:30 +00001848 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849
Jelle Zijlstraac317702017-10-05 20:24:46 -07001850 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001851 is_async = 1;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001852 REQ(CHILD(n, 0), NAME);
1853 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1854 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001855 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001856 else {
1857 sync_n = CHILD(n, 0);
1858 }
1859 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001860
Jelle Zijlstraac317702017-10-05 20:24:46 -07001861 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001862 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001863 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001864 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001865 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001866 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001867 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001868
Thomas Wouters89f507f2006-12-13 04:49:30 +00001869 /* Check the # of children rather than the length of t, since
1870 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001871 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001872 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001873 comp = comprehension(first, expression, NULL,
1874 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875 else
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001876 comp = comprehension(Tuple(t, Store, first->lineno,
1877 first->col_offset, c->c_arena),
1878 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001879 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001881
Jelle Zijlstraac317702017-10-05 20:24:46 -07001882 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883 int j, n_ifs;
1884 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885
Jelle Zijlstraac317702017-10-05 20:24:46 -07001886 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001887 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001888 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001889 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001890
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001891 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001892 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001894
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001895 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001896 REQ(n, comp_iter);
1897 n = CHILD(n, 0);
1898 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899
Guido van Rossum992d4a32007-07-11 13:09:30 +00001900 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001901 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001902 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001903 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001904 if (NCH(n) == 3)
1905 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001906 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001907 /* on exit, must guarantee that n is a comp_for */
1908 if (TYPE(n) == comp_iter)
1909 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001910 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001911 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001912 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001914 return comps;
1915}
1916
1917static expr_ty
1918ast_for_itercomp(struct compiling *c, const node *n, int type)
1919{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001920 /* testlist_comp: (test|star_expr)
1921 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001922 expr_ty elt;
1923 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001924 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925
Guido van Rossum992d4a32007-07-11 13:09:30 +00001926 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001928 ch = CHILD(n, 0);
1929 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001930 if (!elt)
1931 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001932 if (elt->kind == Starred_kind) {
1933 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1934 return NULL;
1935 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936
Guido van Rossum992d4a32007-07-11 13:09:30 +00001937 comps = ast_for_comprehension(c, CHILD(n, 1));
1938 if (!comps)
1939 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001940
1941 if (type == COMP_GENEXP)
1942 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1943 else if (type == COMP_LISTCOMP)
1944 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1945 else if (type == COMP_SETCOMP)
1946 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1947 else
1948 /* Should never happen */
1949 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950}
1951
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001952/* Fills in the key, value pair corresponding to the dict element. In case
1953 * of an unpacking, key is NULL. *i is advanced by the number of ast
1954 * elements. Iff successful, nonzero is returned.
1955 */
1956static int
1957ast_for_dictelement(struct compiling *c, const node *n, int *i,
1958 expr_ty *key, expr_ty *value)
1959{
1960 expr_ty expression;
1961 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1962 assert(NCH(n) - *i >= 2);
1963
1964 expression = ast_for_expr(c, CHILD(n, *i + 1));
1965 if (!expression)
1966 return 0;
1967 *key = NULL;
1968 *value = expression;
1969
1970 *i += 2;
1971 }
1972 else {
1973 assert(NCH(n) - *i >= 3);
1974
1975 expression = ast_for_expr(c, CHILD(n, *i));
1976 if (!expression)
1977 return 0;
1978 *key = expression;
1979
1980 REQ(CHILD(n, *i + 1), COLON);
1981
1982 expression = ast_for_expr(c, CHILD(n, *i + 2));
1983 if (!expression)
1984 return 0;
1985 *value = expression;
1986
1987 *i += 3;
1988 }
1989 return 1;
1990}
1991
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001993ast_for_dictcomp(struct compiling *c, const node *n)
1994{
1995 expr_ty key, value;
1996 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001997 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001999 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002000 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002001 assert(key);
2002 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002004 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002005 if (!comps)
2006 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007
Guido van Rossum992d4a32007-07-11 13:09:30 +00002008 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
2009}
2010
2011static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002012ast_for_dictdisplay(struct compiling *c, const node *n)
2013{
2014 int i;
2015 int j;
2016 int size;
2017 asdl_seq *keys, *values;
2018
2019 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2020 keys = _Py_asdl_seq_new(size, c->c_arena);
2021 if (!keys)
2022 return NULL;
2023
2024 values = _Py_asdl_seq_new(size, c->c_arena);
2025 if (!values)
2026 return NULL;
2027
2028 j = 0;
2029 for (i = 0; i < NCH(n); i++) {
2030 expr_ty key, value;
2031
2032 if (!ast_for_dictelement(c, n, &i, &key, &value))
2033 return NULL;
2034 asdl_seq_SET(keys, j, key);
2035 asdl_seq_SET(values, j, value);
2036
2037 j++;
2038 }
2039 keys->size = j;
2040 values->size = j;
2041 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
2042}
2043
2044static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002045ast_for_genexp(struct compiling *c, const node *n)
2046{
2047 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002048 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002049}
2050
2051static expr_ty
2052ast_for_listcomp(struct compiling *c, const node *n)
2053{
2054 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002055 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002056}
2057
2058static expr_ty
2059ast_for_setcomp(struct compiling *c, const node *n)
2060{
2061 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002062 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002063}
2064
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002065static expr_ty
2066ast_for_setdisplay(struct compiling *c, const node *n)
2067{
2068 int i;
2069 int size;
2070 asdl_seq *elts;
2071
2072 assert(TYPE(n) == (dictorsetmaker));
2073 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2074 elts = _Py_asdl_seq_new(size, c->c_arena);
2075 if (!elts)
2076 return NULL;
2077 for (i = 0; i < NCH(n); i += 2) {
2078 expr_ty expression;
2079 expression = ast_for_expr(c, CHILD(n, i));
2080 if (!expression)
2081 return NULL;
2082 asdl_seq_SET(elts, i / 2, expression);
2083 }
2084 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2085}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002086
2087static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002088ast_for_atom(struct compiling *c, const node *n)
2089{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002090 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2091 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002092 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093 */
2094 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002096 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002097 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002098 PyObject *name;
2099 const char *s = STR(ch);
2100 size_t len = strlen(s);
2101 if (len >= 4 && len <= 5) {
2102 if (!strcmp(s, "None"))
2103 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2104 if (!strcmp(s, "True"))
2105 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2106 if (!strcmp(s, "False"))
2107 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2108 }
2109 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002110 if (!name)
2111 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002112 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002113 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2114 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002115 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002116 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002117 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002118 const char *errtype = NULL;
2119 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2120 errtype = "unicode error";
2121 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2122 errtype = "value error";
2123 if (errtype) {
2124 char buf[128];
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002125 const char *s = NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002126 PyObject *type, *value, *tback, *errstr;
2127 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002128 errstr = PyObject_Str(value);
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002129 if (errstr)
2130 s = PyUnicode_AsUTF8(errstr);
2131 if (s) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002132 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002133 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002134 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002135 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002136 }
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002137 Py_XDECREF(errstr);
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002138 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002139 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002140 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002141 Py_XDECREF(tback);
2142 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002143 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002144 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002145 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146 }
2147 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002148 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002149 if (!pynum)
2150 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002151
Victor Stinner43d81952013-07-17 00:57:58 +02002152 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2153 Py_DECREF(pynum);
2154 return NULL;
2155 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002156 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002157 }
Georg Brandldde00282007-03-18 19:01:53 +00002158 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002159 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002160 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002161 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162
Thomas Wouters89f507f2006-12-13 04:49:30 +00002163 if (TYPE(ch) == RPAR)
2164 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165
Thomas Wouters89f507f2006-12-13 04:49:30 +00002166 if (TYPE(ch) == yield_expr)
2167 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002170 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002171 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002172
Nick Coghlan650f0d02007-04-15 12:05:43 +00002173 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002174 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002175 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176
Thomas Wouters89f507f2006-12-13 04:49:30 +00002177 if (TYPE(ch) == RSQB)
2178 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179
Nick Coghlan650f0d02007-04-15 12:05:43 +00002180 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002181 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2182 asdl_seq *elts = seq_for_testlist(c, ch);
2183 if (!elts)
2184 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002185
Thomas Wouters89f507f2006-12-13 04:49:30 +00002186 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2187 }
2188 else
2189 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002190 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002191 /* dictorsetmaker: ( ((test ':' test | '**' test)
2192 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2193 * ((test | '*' test)
2194 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002195 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002196 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002197 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002198 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002199 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002200 }
2201 else {
2202 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2203 if (NCH(ch) == 1 ||
2204 (NCH(ch) > 1 &&
2205 TYPE(CHILD(ch, 1)) == COMMA)) {
2206 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002207 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002208 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002209 else if (NCH(ch) > 1 &&
2210 TYPE(CHILD(ch, 1)) == comp_for) {
2211 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002212 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002213 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002214 else if (NCH(ch) > 3 - is_dict &&
2215 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2216 /* It's a dictionary comprehension. */
2217 if (is_dict) {
2218 ast_error(c, n, "dict unpacking cannot be used in "
2219 "dict comprehension");
2220 return NULL;
2221 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002222 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002223 }
2224 else {
2225 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002226 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002227 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002228 if (res) {
2229 res->lineno = LINENO(n);
2230 res->col_offset = n->n_col_offset;
2231 }
2232 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002233 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002234 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002235 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002236 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2237 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238 }
2239}
2240
2241static slice_ty
2242ast_for_slice(struct compiling *c, const node *n)
2243{
2244 node *ch;
2245 expr_ty lower = NULL, upper = NULL, step = NULL;
2246
2247 REQ(n, subscript);
2248
2249 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002250 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251 sliceop: ':' [test]
2252 */
2253 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002254 if (NCH(n) == 1 && TYPE(ch) == test) {
2255 /* 'step' variable hold no significance in terms of being used over
2256 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002258 if (!step)
2259 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260
Thomas Wouters89f507f2006-12-13 04:49:30 +00002261 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002262 }
2263
2264 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002265 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266 if (!lower)
2267 return NULL;
2268 }
2269
2270 /* If there's an upper bound it's in the second or third position. */
2271 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002272 if (NCH(n) > 1) {
2273 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002274
Thomas Wouters89f507f2006-12-13 04:49:30 +00002275 if (TYPE(n2) == test) {
2276 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002277 if (!upper)
2278 return NULL;
2279 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002280 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002281 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002282 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283
Thomas Wouters89f507f2006-12-13 04:49:30 +00002284 if (TYPE(n2) == test) {
2285 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286 if (!upper)
2287 return NULL;
2288 }
2289 }
2290
2291 ch = CHILD(n, NCH(n) - 1);
2292 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002293 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002294 ch = CHILD(ch, 1);
2295 if (TYPE(ch) == test) {
2296 step = ast_for_expr(c, ch);
2297 if (!step)
2298 return NULL;
2299 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300 }
2301 }
2302
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002303 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304}
2305
2306static expr_ty
2307ast_for_binop(struct compiling *c, const node *n)
2308{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002309 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002311 BinOp(BinOp(A, op, B), op, C).
2312 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002313
Guido van Rossumd8faa362007-04-27 19:54:29 +00002314 int i, nops;
2315 expr_ty expr1, expr2, result;
2316 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002317
Guido van Rossumd8faa362007-04-27 19:54:29 +00002318 expr1 = ast_for_expr(c, CHILD(n, 0));
2319 if (!expr1)
2320 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002321
Guido van Rossumd8faa362007-04-27 19:54:29 +00002322 expr2 = ast_for_expr(c, CHILD(n, 2));
2323 if (!expr2)
2324 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002325
Guido van Rossumd8faa362007-04-27 19:54:29 +00002326 newoperator = get_operator(CHILD(n, 1));
2327 if (!newoperator)
2328 return NULL;
2329
2330 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2331 c->c_arena);
2332 if (!result)
2333 return NULL;
2334
2335 nops = (NCH(n) - 1) / 2;
2336 for (i = 1; i < nops; i++) {
2337 expr_ty tmp_result, tmp;
2338 const node* next_oper = CHILD(n, i * 2 + 1);
2339
2340 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002341 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002342 return NULL;
2343
Guido van Rossumd8faa362007-04-27 19:54:29 +00002344 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2345 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002346 return NULL;
2347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002349 LINENO(next_oper), next_oper->n_col_offset,
2350 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002352 return NULL;
2353 result = tmp_result;
2354 }
2355 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002356}
2357
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002358static expr_ty
2359ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002362 subscriptlist: subscript (',' subscript)* [',']
2363 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2364 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002365 REQ(n, trailer);
2366 if (TYPE(CHILD(n, 0)) == LPAR) {
2367 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002368 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002369 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002370 else
2371 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002372 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002373 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002374 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2375 if (!attr_id)
2376 return NULL;
2377 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002378 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002379 }
2380 else {
2381 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002382 REQ(CHILD(n, 2), RSQB);
2383 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002384 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002385 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2386 if (!slc)
2387 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002388 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2389 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002390 }
2391 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002393 by treating the sequence as a tuple literal if there are
2394 no slice features.
2395 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002396 int j;
2397 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002398 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002399 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002400 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002401 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002402 if (!slices)
2403 return NULL;
2404 for (j = 0; j < NCH(n); j += 2) {
2405 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002406 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002407 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002408 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002409 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002410 asdl_seq_SET(slices, j / 2, slc);
2411 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002412 if (!simple) {
2413 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002414 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002415 }
2416 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002417 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002418 if (!elts)
2419 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002420 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2421 slc = (slice_ty)asdl_seq_GET(slices, j);
2422 assert(slc->kind == Index_kind && slc->v.Index.value);
2423 asdl_seq_SET(elts, j, slc->v.Index.value);
2424 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002425 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002426 if (!e)
2427 return NULL;
2428 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002429 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002430 }
2431 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002432}
2433
2434static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002435ast_for_factor(struct compiling *c, const node *n)
2436{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002437 expr_ty expression;
2438
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002439 expression = ast_for_expr(c, CHILD(n, 1));
2440 if (!expression)
2441 return NULL;
2442
2443 switch (TYPE(CHILD(n, 0))) {
2444 case PLUS:
2445 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2446 c->c_arena);
2447 case MINUS:
2448 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2449 c->c_arena);
2450 case TILDE:
2451 return UnaryOp(Invert, expression, LINENO(n),
2452 n->n_col_offset, c->c_arena);
2453 }
2454 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2455 TYPE(CHILD(n, 0)));
2456 return NULL;
2457}
2458
2459static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002460ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002461{
Yury Selivanov75445082015-05-11 22:57:16 -04002462 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002463 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002464
2465 REQ(n, atom_expr);
2466 nch = NCH(n);
2467
Jelle Zijlstraac317702017-10-05 20:24:46 -07002468 if (TYPE(CHILD(n, 0)) == NAME && strcmp(STR(CHILD(n, 0)), "await") == 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04002469 start = 1;
2470 assert(nch > 1);
2471 }
2472
2473 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002474 if (!e)
2475 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002476 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002477 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002478 if (start && nch == 2) {
2479 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2480 }
2481
2482 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002483 node *ch = CHILD(n, i);
2484 if (TYPE(ch) != trailer)
2485 break;
2486 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002487 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002488 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002489 tmp->lineno = e->lineno;
2490 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002491 e = tmp;
2492 }
Yury Selivanov75445082015-05-11 22:57:16 -04002493
2494 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002495 /* there was an 'await' */
Yury Selivanov75445082015-05-11 22:57:16 -04002496 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2497 }
2498 else {
2499 return e;
2500 }
2501}
2502
2503static expr_ty
2504ast_for_power(struct compiling *c, const node *n)
2505{
2506 /* power: atom trailer* ('**' factor)*
2507 */
2508 expr_ty e;
2509 REQ(n, power);
2510 e = ast_for_atom_expr(c, CHILD(n, 0));
2511 if (!e)
2512 return NULL;
2513 if (NCH(n) == 1)
2514 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002515 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2516 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002517 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002518 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002519 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002520 }
2521 return e;
2522}
2523
Guido van Rossum0368b722007-05-11 16:50:42 +00002524static expr_ty
2525ast_for_starred(struct compiling *c, const node *n)
2526{
2527 expr_ty tmp;
2528 REQ(n, star_expr);
2529
2530 tmp = ast_for_expr(c, CHILD(n, 1));
2531 if (!tmp)
2532 return NULL;
2533
2534 /* The Load context is changed later. */
2535 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2536}
2537
2538
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539/* Do not name a variable 'expr'! Will cause a compile error.
2540*/
2541
2542static expr_ty
2543ast_for_expr(struct compiling *c, const node *n)
2544{
2545 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002546 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002547 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002548 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 and_test: not_test ('and' not_test)*
2550 not_test: 'not' not_test | comparison
2551 comparison: expr (comp_op expr)*
2552 expr: xor_expr ('|' xor_expr)*
2553 xor_expr: and_expr ('^' and_expr)*
2554 and_expr: shift_expr ('&' shift_expr)*
2555 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2556 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002557 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002559 power: atom_expr ['**' factor]
Jelle Zijlstraac317702017-10-05 20:24:46 -07002560 atom_expr: ['await'] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002561 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002562 */
2563
2564 asdl_seq *seq;
2565 int i;
2566
2567 loop:
2568 switch (TYPE(n)) {
2569 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002570 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002571 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002572 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002573 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002574 else if (NCH(n) > 1)
2575 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002576 /* Fallthrough */
2577 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578 case and_test:
2579 if (NCH(n) == 1) {
2580 n = CHILD(n, 0);
2581 goto loop;
2582 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002583 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002584 if (!seq)
2585 return NULL;
2586 for (i = 0; i < NCH(n); i += 2) {
2587 expr_ty e = ast_for_expr(c, CHILD(n, i));
2588 if (!e)
2589 return NULL;
2590 asdl_seq_SET(seq, i / 2, e);
2591 }
2592 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002593 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2594 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002595 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002596 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002597 case not_test:
2598 if (NCH(n) == 1) {
2599 n = CHILD(n, 0);
2600 goto loop;
2601 }
2602 else {
2603 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2604 if (!expression)
2605 return NULL;
2606
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002607 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2608 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002609 }
2610 case comparison:
2611 if (NCH(n) == 1) {
2612 n = CHILD(n, 0);
2613 goto loop;
2614 }
2615 else {
2616 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002617 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002618 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002619 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620 if (!ops)
2621 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002622 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002623 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002624 return NULL;
2625 }
2626 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002627 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002629 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002630 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002631 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002632 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002633
2634 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002635 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002636 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002637 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002639 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002640 asdl_seq_SET(cmps, i / 2, expression);
2641 }
2642 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002643 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002645 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002647 return Compare(expression, ops, cmps, LINENO(n),
2648 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649 }
2650 break;
2651
Guido van Rossum0368b722007-05-11 16:50:42 +00002652 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654 /* The next five cases all handle BinOps. The main body of code
2655 is the same in each case, but the switch turned inside out to
2656 reuse the code for each type of operator.
2657 */
2658 case expr:
2659 case xor_expr:
2660 case and_expr:
2661 case shift_expr:
2662 case arith_expr:
2663 case term:
2664 if (NCH(n) == 1) {
2665 n = CHILD(n, 0);
2666 goto loop;
2667 }
2668 return ast_for_binop(c, n);
2669 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002670 node *an = NULL;
2671 node *en = NULL;
2672 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002673 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002674 if (NCH(n) > 1)
2675 an = CHILD(n, 1); /* yield_arg */
2676 if (an) {
2677 en = CHILD(an, NCH(an) - 1);
2678 if (NCH(an) == 2) {
2679 is_from = 1;
2680 exp = ast_for_expr(c, en);
2681 }
2682 else
2683 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002684 if (!exp)
2685 return NULL;
2686 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002687 if (is_from)
2688 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2689 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002690 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002691 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692 if (NCH(n) == 1) {
2693 n = CHILD(n, 0);
2694 goto loop;
2695 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002696 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002697 case power:
2698 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002699 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002700 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701 return NULL;
2702 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002703 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002704 return NULL;
2705}
2706
2707static expr_ty
2708ast_for_call(struct compiling *c, const node *n, expr_ty func)
2709{
2710 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002711 arglist: argument (',' argument)* [',']
2712 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002713 */
2714
2715 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002716 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002717 asdl_seq *args;
2718 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002719
2720 REQ(n, arglist);
2721
2722 nargs = 0;
2723 nkeywords = 0;
2724 ngens = 0;
2725 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002726 node *ch = CHILD(n, i);
2727 if (TYPE(ch) == argument) {
2728 if (NCH(ch) == 1)
2729 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002730 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002731 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002732 else if (TYPE(CHILD(ch, 0)) == STAR)
2733 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002735 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002736 nkeywords++;
2737 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738 }
2739 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002740 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002741 "if not sole argument");
2742 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 }
2744
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002745 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002746 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002747 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002748 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002749 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002750 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002751
2752 nargs = 0; /* positional arguments + iterable argument unpackings */
2753 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2754 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002755 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002756 node *ch = CHILD(n, i);
2757 if (TYPE(ch) == argument) {
2758 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002759 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002760 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002761 /* a positional argument */
2762 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002763 if (ndoublestars) {
2764 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002765 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002766 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002767 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002768 else {
2769 ast_error(c, chch,
2770 "positional argument follows "
2771 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002772 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002773 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002774 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002775 e = ast_for_expr(c, chch);
2776 if (!e)
2777 return NULL;
2778 asdl_seq_SET(args, nargs++, e);
2779 }
2780 else if (TYPE(chch) == STAR) {
2781 /* an iterable argument unpacking */
2782 expr_ty starred;
2783 if (ndoublestars) {
2784 ast_error(c, chch,
2785 "iterable argument unpacking follows "
2786 "keyword argument unpacking");
2787 return NULL;
2788 }
2789 e = ast_for_expr(c, CHILD(ch, 1));
2790 if (!e)
2791 return NULL;
2792 starred = Starred(e, Load, LINENO(chch),
2793 chch->n_col_offset,
2794 c->c_arena);
2795 if (!starred)
2796 return NULL;
2797 asdl_seq_SET(args, nargs++, starred);
2798
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002799 }
2800 else if (TYPE(chch) == DOUBLESTAR) {
2801 /* a keyword argument unpacking */
2802 keyword_ty kw;
2803 i++;
2804 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002806 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002807 kw = keyword(NULL, e, c->c_arena);
2808 asdl_seq_SET(keywords, nkeywords++, kw);
2809 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002811 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002812 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002813 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002814 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002815 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002816 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002818 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002819 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002820 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002821 identifier key, tmp;
2822 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002824 /* chch is test, but must be an identifier? */
2825 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002827 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828 /* f(lambda x: x[0] = 3) ends up getting parsed with
2829 * LHS test = lambda x: x[0], and RHS test = 3.
2830 * SF bug 132313 points out that complaining about a keyword
2831 * then is very confusing.
2832 */
2833 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002834 ast_error(c, chch,
2835 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002836 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002837 }
2838 else if (e->kind != Name_kind) {
2839 ast_error(c, chch,
2840 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002841 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002842 }
2843 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002844 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002846 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002847 for (k = 0; k < nkeywords; k++) {
2848 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002849 if (tmp && !PyUnicode_Compare(tmp, key)) {
2850 ast_error(c, chch,
2851 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002852 return NULL;
2853 }
2854 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002855 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002857 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002858 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002859 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002860 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002861 asdl_seq_SET(keywords, nkeywords++, kw);
2862 }
2863 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002864 }
2865
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002866 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867}
2868
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002870ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002872 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002873 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002874 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002875 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002876 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002877 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002878 }
2879 else {
2880 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002881 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002882 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002883 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002884 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002885 else {
2886 asdl_seq *tmp = seq_for_testlist(c, n);
2887 if (!tmp)
2888 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002889 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002891}
2892
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002893static stmt_ty
2894ast_for_expr_stmt(struct compiling *c, const node *n)
2895{
2896 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002897 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
2898 ('=' (yield_expr|testlist_star_expr))*)
2899 annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +00002900 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002901 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002902 | '<<=' | '>>=' | '**=' | '//='
Martin Panter69332c12016-08-04 13:07:31 +00002903 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904 */
2905
2906 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002907 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002908 if (!e)
2909 return NULL;
2910
Thomas Wouters89f507f2006-12-13 04:49:30 +00002911 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002912 }
2913 else if (TYPE(CHILD(n, 1)) == augassign) {
2914 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002915 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002916 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002917
Thomas Wouters89f507f2006-12-13 04:49:30 +00002918 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002919 if (!expr1)
2920 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002921 if(!set_context(c, expr1, Store, ch))
2922 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002923 /* set_context checks that most expressions are not the left side.
2924 Augmented assignments can only have a name, a subscript, or an
2925 attribute on the left, though, so we have to explicitly check for
2926 those. */
2927 switch (expr1->kind) {
2928 case Name_kind:
2929 case Attribute_kind:
2930 case Subscript_kind:
2931 break;
2932 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002933 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002934 return NULL;
2935 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002936
Thomas Wouters89f507f2006-12-13 04:49:30 +00002937 ch = CHILD(n, 2);
2938 if (TYPE(ch) == testlist)
2939 expr2 = ast_for_testlist(c, ch);
2940 else
2941 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002942 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002943 return NULL;
2944
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002945 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002946 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947 return NULL;
2948
Thomas Wouters89f507f2006-12-13 04:49:30 +00002949 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002950 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002951 else if (TYPE(CHILD(n, 1)) == annassign) {
2952 expr_ty expr1, expr2, expr3;
2953 node *ch = CHILD(n, 0);
2954 node *deep, *ann = CHILD(n, 1);
2955 int simple = 1;
2956
2957 /* we keep track of parens to qualify (x) as expression not name */
2958 deep = ch;
2959 while (NCH(deep) == 1) {
2960 deep = CHILD(deep, 0);
2961 }
2962 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
2963 simple = 0;
2964 }
2965 expr1 = ast_for_testlist(c, ch);
2966 if (!expr1) {
2967 return NULL;
2968 }
2969 switch (expr1->kind) {
2970 case Name_kind:
2971 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
2972 return NULL;
2973 }
2974 expr1->v.Name.ctx = Store;
2975 break;
2976 case Attribute_kind:
2977 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
2978 return NULL;
2979 }
2980 expr1->v.Attribute.ctx = Store;
2981 break;
2982 case Subscript_kind:
2983 expr1->v.Subscript.ctx = Store;
2984 break;
2985 case List_kind:
2986 ast_error(c, ch,
2987 "only single target (not list) can be annotated");
2988 return NULL;
2989 case Tuple_kind:
2990 ast_error(c, ch,
2991 "only single target (not tuple) can be annotated");
2992 return NULL;
2993 default:
2994 ast_error(c, ch,
2995 "illegal target for annotation");
2996 return NULL;
2997 }
2998
2999 if (expr1->kind != Name_kind) {
3000 simple = 0;
3001 }
3002 ch = CHILD(ann, 1);
3003 expr2 = ast_for_expr(c, ch);
3004 if (!expr2) {
3005 return NULL;
3006 }
3007 if (NCH(ann) == 2) {
3008 return AnnAssign(expr1, expr2, NULL, simple,
3009 LINENO(n), n->n_col_offset, c->c_arena);
3010 }
3011 else {
3012 ch = CHILD(ann, 3);
3013 expr3 = ast_for_expr(c, ch);
3014 if (!expr3) {
3015 return NULL;
3016 }
3017 return AnnAssign(expr1, expr2, expr3, simple,
3018 LINENO(n), n->n_col_offset, c->c_arena);
3019 }
3020 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003021 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003022 int i;
3023 asdl_seq *targets;
3024 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003025 expr_ty expression;
3026
Thomas Wouters89f507f2006-12-13 04:49:30 +00003027 /* a normal assignment */
3028 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003029 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003030 if (!targets)
3031 return NULL;
3032 for (i = 0; i < NCH(n) - 2; i += 2) {
3033 expr_ty e;
3034 node *ch = CHILD(n, i);
3035 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003036 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003037 return NULL;
3038 }
3039 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003041 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003042
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003043 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003044 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003045 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003046
Thomas Wouters89f507f2006-12-13 04:49:30 +00003047 asdl_seq_SET(targets, i / 2, e);
3048 }
3049 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003050 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003051 expression = ast_for_testlist(c, value);
3052 else
3053 expression = ast_for_expr(c, value);
3054 if (!expression)
3055 return NULL;
3056 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003057 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003058}
3059
Benjamin Peterson78565b22009-06-28 19:19:51 +00003060
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003061static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003062ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003063{
3064 asdl_seq *seq;
3065 int i;
3066 expr_ty e;
3067
3068 REQ(n, exprlist);
3069
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003070 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003071 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003072 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003073 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003074 e = ast_for_expr(c, CHILD(n, i));
3075 if (!e)
3076 return NULL;
3077 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003078 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003079 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003080 }
3081 return seq;
3082}
3083
3084static stmt_ty
3085ast_for_del_stmt(struct compiling *c, const node *n)
3086{
3087 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003089 /* del_stmt: 'del' exprlist */
3090 REQ(n, del_stmt);
3091
3092 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3093 if (!expr_list)
3094 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003095 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003096}
3097
3098static stmt_ty
3099ast_for_flow_stmt(struct compiling *c, const node *n)
3100{
3101 /*
3102 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3103 | yield_stmt
3104 break_stmt: 'break'
3105 continue_stmt: 'continue'
3106 return_stmt: 'return' [testlist]
3107 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003108 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003109 raise_stmt: 'raise' [test [',' test [',' test]]]
3110 */
3111 node *ch;
3112
3113 REQ(n, flow_stmt);
3114 ch = CHILD(n, 0);
3115 switch (TYPE(ch)) {
3116 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003117 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003118 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003119 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003120 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003121 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3122 if (!exp)
3123 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003124 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003125 }
3126 case return_stmt:
3127 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003128 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003129 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003130 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003131 if (!expression)
3132 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003133 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003134 }
3135 case raise_stmt:
3136 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003137 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3138 else if (NCH(ch) >= 2) {
3139 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003140 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3141 if (!expression)
3142 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003143 if (NCH(ch) == 4) {
3144 cause = ast_for_expr(c, CHILD(ch, 3));
3145 if (!cause)
3146 return NULL;
3147 }
3148 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003149 }
Stefan Krahf432a322017-08-21 13:09:59 +02003150 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003151 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003152 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003153 "unexpected flow_stmt: %d", TYPE(ch));
3154 return NULL;
3155 }
3156}
3157
3158static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003159alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003160{
3161 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003162 import_as_name: NAME ['as' NAME]
3163 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003164 dotted_name: NAME ('.' NAME)*
3165 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003166 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003167
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003168 loop:
3169 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003170 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003171 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003172 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003173 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003174 if (!name)
3175 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003176 if (NCH(n) == 3) {
3177 node *str_node = CHILD(n, 2);
3178 str = NEW_IDENTIFIER(str_node);
3179 if (!str)
3180 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003181 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003182 return NULL;
3183 }
3184 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003185 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003186 return NULL;
3187 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003188 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003189 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003190 case dotted_as_name:
3191 if (NCH(n) == 1) {
3192 n = CHILD(n, 0);
3193 goto loop;
3194 }
3195 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003196 node *asname_node = CHILD(n, 2);
3197 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003198 if (!a)
3199 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003200 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003201 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003202 if (!a->asname)
3203 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003204 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003205 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003206 return a;
3207 }
3208 break;
3209 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003210 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003211 node *name_node = CHILD(n, 0);
3212 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003213 if (!name)
3214 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003215 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003216 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003217 return alias(name, NULL, c->c_arena);
3218 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003219 else {
3220 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003221 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003222 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003223 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003225
3226 len = 0;
3227 for (i = 0; i < NCH(n); i += 2)
3228 /* length of string plus one for the dot */
3229 len += strlen(STR(CHILD(n, i))) + 1;
3230 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003231 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003232 if (!str)
3233 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003234 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003235 if (!s)
3236 return NULL;
3237 for (i = 0; i < NCH(n); i += 2) {
3238 char *sch = STR(CHILD(n, i));
3239 strcpy(s, STR(CHILD(n, i)));
3240 s += strlen(sch);
3241 *s++ = '.';
3242 }
3243 --s;
3244 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003245 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3246 PyBytes_GET_SIZE(str),
3247 NULL);
3248 Py_DECREF(str);
3249 if (!uni)
3250 return NULL;
3251 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003252 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003253 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3254 Py_DECREF(str);
3255 return NULL;
3256 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003257 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003258 }
3259 break;
3260 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003261 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003262 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3263 Py_DECREF(str);
3264 return NULL;
3265 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003266 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003267 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003268 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003269 "unexpected import name: %d", TYPE(n));
3270 return NULL;
3271 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003272
3273 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003274 return NULL;
3275}
3276
3277static stmt_ty
3278ast_for_import_stmt(struct compiling *c, const node *n)
3279{
3280 /*
3281 import_stmt: import_name | import_from
3282 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003283 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3284 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003285 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003286 int lineno;
3287 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003288 int i;
3289 asdl_seq *aliases;
3290
3291 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003292 lineno = LINENO(n);
3293 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003294 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003295 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003296 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003297 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003298 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003299 if (!aliases)
3300 return NULL;
3301 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003302 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003303 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003304 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003305 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003306 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003307 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003308 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003309 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003310 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003311 int idx, ndots = 0;
3312 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003313 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003315 /* Count the number of dots (for relative imports) and check for the
3316 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003317 for (idx = 1; idx < NCH(n); idx++) {
3318 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003319 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3320 if (!mod)
3321 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003322 idx++;
3323 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003324 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003325 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003326 ndots += 3;
3327 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003328 } else if (TYPE(CHILD(n, idx)) != DOT) {
3329 break;
3330 }
3331 ndots++;
3332 }
3333 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003334 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003335 case STAR:
3336 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003337 n = CHILD(n, idx);
3338 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003339 break;
3340 case LPAR:
3341 /* from ... import (x, y, z) */
3342 n = CHILD(n, idx + 1);
3343 n_children = NCH(n);
3344 break;
3345 case import_as_names:
3346 /* from ... import x, y, z */
3347 n = CHILD(n, idx);
3348 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003349 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003350 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003351 " surrounding parentheses");
3352 return NULL;
3353 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003354 break;
3355 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003356 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003357 return NULL;
3358 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003359
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003360 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003361 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003363
3364 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003365 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003366 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003367 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003368 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003369 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003370 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003371 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003372 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003373 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003374 if (!import_alias)
3375 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003376 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003377 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003378 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003379 if (mod != NULL)
3380 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003381 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003382 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003383 }
Neal Norwitz79792652005-11-14 04:25:03 +00003384 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003385 "unknown import statement: starts with command '%s'",
3386 STR(CHILD(n, 0)));
3387 return NULL;
3388}
3389
3390static stmt_ty
3391ast_for_global_stmt(struct compiling *c, const node *n)
3392{
3393 /* global_stmt: 'global' NAME (',' NAME)* */
3394 identifier name;
3395 asdl_seq *s;
3396 int i;
3397
3398 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003399 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003400 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003401 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003402 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003403 name = NEW_IDENTIFIER(CHILD(n, i));
3404 if (!name)
3405 return NULL;
3406 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003407 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003408 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003409}
3410
3411static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003412ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3413{
3414 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3415 identifier name;
3416 asdl_seq *s;
3417 int i;
3418
3419 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003420 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003421 if (!s)
3422 return NULL;
3423 for (i = 1; i < NCH(n); i += 2) {
3424 name = NEW_IDENTIFIER(CHILD(n, i));
3425 if (!name)
3426 return NULL;
3427 asdl_seq_SET(s, i / 2, name);
3428 }
3429 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3430}
3431
3432static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003433ast_for_assert_stmt(struct compiling *c, const node *n)
3434{
3435 /* assert_stmt: 'assert' test [',' test] */
3436 REQ(n, assert_stmt);
3437 if (NCH(n) == 2) {
3438 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3439 if (!expression)
3440 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003441 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003442 }
3443 else if (NCH(n) == 4) {
3444 expr_ty expr1, expr2;
3445
3446 expr1 = ast_for_expr(c, CHILD(n, 1));
3447 if (!expr1)
3448 return NULL;
3449 expr2 = ast_for_expr(c, CHILD(n, 3));
3450 if (!expr2)
3451 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452
Thomas Wouters89f507f2006-12-13 04:49:30 +00003453 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003454 }
Neal Norwitz79792652005-11-14 04:25:03 +00003455 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003456 "improper number of parts to 'assert' statement: %d",
3457 NCH(n));
3458 return NULL;
3459}
3460
3461static asdl_seq *
3462ast_for_suite(struct compiling *c, const node *n)
3463{
3464 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003465 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003466 stmt_ty s;
3467 int i, total, num, end, pos = 0;
3468 node *ch;
3469
3470 REQ(n, suite);
3471
3472 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003473 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003474 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003475 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003476 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003477 n = CHILD(n, 0);
3478 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003479 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003480 */
3481 end = NCH(n) - 1;
3482 if (TYPE(CHILD(n, end - 1)) == SEMI)
3483 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003484 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003485 for (i = 0; i < end; i += 2) {
3486 ch = CHILD(n, i);
3487 s = ast_for_stmt(c, ch);
3488 if (!s)
3489 return NULL;
3490 asdl_seq_SET(seq, pos++, s);
3491 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003492 }
3493 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003494 for (i = 2; i < (NCH(n) - 1); i++) {
3495 ch = CHILD(n, i);
3496 REQ(ch, stmt);
3497 num = num_stmts(ch);
3498 if (num == 1) {
3499 /* small_stmt or compound_stmt with only one child */
3500 s = ast_for_stmt(c, ch);
3501 if (!s)
3502 return NULL;
3503 asdl_seq_SET(seq, pos++, s);
3504 }
3505 else {
3506 int j;
3507 ch = CHILD(ch, 0);
3508 REQ(ch, simple_stmt);
3509 for (j = 0; j < NCH(ch); j += 2) {
3510 /* statement terminates with a semi-colon ';' */
3511 if (NCH(CHILD(ch, j)) == 0) {
3512 assert((j + 1) == NCH(ch));
3513 break;
3514 }
3515 s = ast_for_stmt(c, CHILD(ch, j));
3516 if (!s)
3517 return NULL;
3518 asdl_seq_SET(seq, pos++, s);
3519 }
3520 }
3521 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003522 }
3523 assert(pos == seq->size);
3524 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525}
3526
INADA Naokicb41b272017-02-23 00:31:59 +09003527static string
3528docstring_from_stmts(asdl_seq *stmts)
3529{
3530 if (stmts && stmts->size) {
3531 stmt_ty s = (stmt_ty)asdl_seq_GET(stmts, 0);
3532 /* If first statement is a literal string, it's the doc string. */
3533 if (s->kind == Expr_kind && s->v.Expr.value->kind == Str_kind) {
3534 string doc = s->v.Expr.value->v.Str.s;
3535 /* not very efficient, but simple */
3536 memmove(&asdl_seq_GET(stmts, 0), &asdl_seq_GET(stmts, 1),
3537 (stmts->size - 1) * sizeof(void*));
3538 stmts->size--;
3539 return doc;
3540 }
3541 }
3542 return NULL;
3543}
3544
3545static asdl_seq *
3546ast_for_body(struct compiling *c, const node *n, string *docstring)
3547{
3548 asdl_seq *stmts = ast_for_suite(c, n);
3549 *docstring = docstring_from_stmts(stmts);
3550 return stmts;
3551}
3552
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003553static stmt_ty
3554ast_for_if_stmt(struct compiling *c, const node *n)
3555{
3556 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3557 ['else' ':' suite]
3558 */
3559 char *s;
3560
3561 REQ(n, if_stmt);
3562
3563 if (NCH(n) == 4) {
3564 expr_ty expression;
3565 asdl_seq *suite_seq;
3566
3567 expression = ast_for_expr(c, CHILD(n, 1));
3568 if (!expression)
3569 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003570 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003571 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003572 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003573
Guido van Rossumd8faa362007-04-27 19:54:29 +00003574 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3575 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003577
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003578 s = STR(CHILD(n, 4));
3579 /* s[2], the third character in the string, will be
3580 's' for el_s_e, or
3581 'i' for el_i_f
3582 */
3583 if (s[2] == 's') {
3584 expr_ty expression;
3585 asdl_seq *seq1, *seq2;
3586
3587 expression = ast_for_expr(c, CHILD(n, 1));
3588 if (!expression)
3589 return NULL;
3590 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003591 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003592 return NULL;
3593 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003594 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003595 return NULL;
3596
Guido van Rossumd8faa362007-04-27 19:54:29 +00003597 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3598 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003599 }
3600 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003601 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003602 expr_ty expression;
3603 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003604 asdl_seq *orelse = NULL;
3605 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003606 /* must reference the child n_elif+1 since 'else' token is third,
3607 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003608 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3609 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3610 has_else = 1;
3611 n_elif -= 3;
3612 }
3613 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003614
Thomas Wouters89f507f2006-12-13 04:49:30 +00003615 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003616 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003617
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003618 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003619 if (!orelse)
3620 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003621 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003622 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003623 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003624 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3625 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003626 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003627 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3628 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003629 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003631 asdl_seq_SET(orelse, 0,
3632 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003633 LINENO(CHILD(n, NCH(n) - 6)),
3634 CHILD(n, NCH(n) - 6)->n_col_offset,
3635 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003636 /* the just-created orelse handled the last elif */
3637 n_elif--;
3638 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003639
Thomas Wouters89f507f2006-12-13 04:49:30 +00003640 for (i = 0; i < n_elif; i++) {
3641 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003642 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003643 if (!newobj)
3644 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003645 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003646 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003647 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003648 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003649 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003650 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003651
Thomas Wouters89f507f2006-12-13 04:49:30 +00003652 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003654 LINENO(CHILD(n, off)),
3655 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003656 orelse = newobj;
3657 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003658 expression = ast_for_expr(c, CHILD(n, 1));
3659 if (!expression)
3660 return NULL;
3661 suite_seq = ast_for_suite(c, CHILD(n, 3));
3662 if (!suite_seq)
3663 return NULL;
3664 return If(expression, suite_seq, orelse,
3665 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003666 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003667
3668 PyErr_Format(PyExc_SystemError,
3669 "unexpected token in 'if' statement: %s", s);
3670 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003671}
3672
3673static stmt_ty
3674ast_for_while_stmt(struct compiling *c, const node *n)
3675{
3676 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3677 REQ(n, while_stmt);
3678
3679 if (NCH(n) == 4) {
3680 expr_ty expression;
3681 asdl_seq *suite_seq;
3682
3683 expression = ast_for_expr(c, CHILD(n, 1));
3684 if (!expression)
3685 return NULL;
3686 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003687 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003688 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003689 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003690 }
3691 else if (NCH(n) == 7) {
3692 expr_ty expression;
3693 asdl_seq *seq1, *seq2;
3694
3695 expression = ast_for_expr(c, CHILD(n, 1));
3696 if (!expression)
3697 return NULL;
3698 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003699 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003700 return NULL;
3701 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003702 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003703 return NULL;
3704
Thomas Wouters89f507f2006-12-13 04:49:30 +00003705 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003706 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003707
3708 PyErr_Format(PyExc_SystemError,
3709 "wrong number of tokens for 'while' statement: %d",
3710 NCH(n));
3711 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003712}
3713
3714static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003715ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003716{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003717 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003718 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003719 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003720 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003721 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3722 REQ(n, for_stmt);
3723
3724 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003725 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003726 if (!seq)
3727 return NULL;
3728 }
3729
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003730 node_target = CHILD(n, 1);
3731 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003732 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003733 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003734 /* Check the # of children rather than the length of _target, since
3735 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003736 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003737 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003738 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003739 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003740 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003741
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003742 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003743 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003744 return NULL;
3745 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003746 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003747 return NULL;
3748
Yury Selivanov75445082015-05-11 22:57:16 -04003749 if (is_async)
3750 return AsyncFor(target, expression, suite_seq, seq,
3751 LINENO(n), n->n_col_offset,
3752 c->c_arena);
3753 else
3754 return For(target, expression, suite_seq, seq,
3755 LINENO(n), n->n_col_offset,
3756 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003757}
3758
3759static excepthandler_ty
3760ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3761{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003762 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003763 REQ(exc, except_clause);
3764 REQ(body, suite);
3765
3766 if (NCH(exc) == 1) {
3767 asdl_seq *suite_seq = ast_for_suite(c, body);
3768 if (!suite_seq)
3769 return NULL;
3770
Neal Norwitzad74aa82008-03-31 05:14:30 +00003771 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003772 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003773 }
3774 else if (NCH(exc) == 2) {
3775 expr_ty expression;
3776 asdl_seq *suite_seq;
3777
3778 expression = ast_for_expr(c, CHILD(exc, 1));
3779 if (!expression)
3780 return NULL;
3781 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003782 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003783 return NULL;
3784
Neal Norwitzad74aa82008-03-31 05:14:30 +00003785 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003786 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003787 }
3788 else if (NCH(exc) == 4) {
3789 asdl_seq *suite_seq;
3790 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003791 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003792 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003793 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003794 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003795 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003796 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003797 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003798 return NULL;
3799 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003800 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003801 return NULL;
3802
Neal Norwitzad74aa82008-03-31 05:14:30 +00003803 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003804 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003805 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003806
3807 PyErr_Format(PyExc_SystemError,
3808 "wrong number of children for 'except' clause: %d",
3809 NCH(exc));
3810 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003811}
3812
3813static stmt_ty
3814ast_for_try_stmt(struct compiling *c, const node *n)
3815{
Neal Norwitzf599f422005-12-17 21:33:47 +00003816 const int nch = NCH(n);
3817 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003818 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003819
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003820 REQ(n, try_stmt);
3821
Neal Norwitzf599f422005-12-17 21:33:47 +00003822 body = ast_for_suite(c, CHILD(n, 2));
3823 if (body == NULL)
3824 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003825
Neal Norwitzf599f422005-12-17 21:33:47 +00003826 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3827 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3828 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3829 /* we can assume it's an "else",
3830 because nch >= 9 for try-else-finally and
3831 it would otherwise have a type of except_clause */
3832 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3833 if (orelse == NULL)
3834 return NULL;
3835 n_except--;
3836 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003837
Neal Norwitzf599f422005-12-17 21:33:47 +00003838 finally = ast_for_suite(c, CHILD(n, nch - 1));
3839 if (finally == NULL)
3840 return NULL;
3841 n_except--;
3842 }
3843 else {
3844 /* we can assume it's an "else",
3845 otherwise it would have a type of except_clause */
3846 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3847 if (orelse == NULL)
3848 return NULL;
3849 n_except--;
3850 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003851 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003852 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003853 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003854 return NULL;
3855 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003856
Neal Norwitzf599f422005-12-17 21:33:47 +00003857 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003858 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003859 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003860 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003861 if (handlers == NULL)
3862 return NULL;
3863
3864 for (i = 0; i < n_except; i++) {
3865 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3866 CHILD(n, 5 + i * 3));
3867 if (!e)
3868 return NULL;
3869 asdl_seq_SET(handlers, i, e);
3870 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003871 }
3872
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003873 assert(finally != NULL || asdl_seq_LEN(handlers));
3874 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003875}
3876
Georg Brandl0c315622009-05-25 21:10:36 +00003877/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003878static withitem_ty
3879ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003880{
3881 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003882
Georg Brandl0c315622009-05-25 21:10:36 +00003883 REQ(n, with_item);
3884 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003885 if (!context_expr)
3886 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003887 if (NCH(n) == 3) {
3888 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003889
3890 if (!optional_vars) {
3891 return NULL;
3892 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003893 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003894 return NULL;
3895 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003896 }
3897
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003898 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003899}
3900
Georg Brandl0c315622009-05-25 21:10:36 +00003901/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3902static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003903ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003904{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003905 int i, n_items;
3906 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003907
3908 REQ(n, with_stmt);
3909
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003910 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003911 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003912 if (!items)
3913 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003914 for (i = 1; i < NCH(n) - 2; i += 2) {
3915 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3916 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003917 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003918 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003919 }
3920
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003921 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3922 if (!body)
3923 return NULL;
3924
Yury Selivanov75445082015-05-11 22:57:16 -04003925 if (is_async)
3926 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3927 else
3928 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003929}
3930
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003931static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003932ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003933{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003934 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003935 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003936 asdl_seq *s;
INADA Naokicb41b272017-02-23 00:31:59 +09003937 string docstring;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003938 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003939
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003940 REQ(n, classdef);
3941
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003942 if (NCH(n) == 4) { /* class NAME ':' suite */
INADA Naokicb41b272017-02-23 00:31:59 +09003943 s = ast_for_body(c, CHILD(n, 3), &docstring);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003944 if (!s)
3945 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003946 classname = NEW_IDENTIFIER(CHILD(n, 1));
3947 if (!classname)
3948 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003949 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003950 return NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09003951 return ClassDef(classname, NULL, NULL, s, decorator_seq, docstring,
3952 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003953 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003954
3955 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
INADA Naokicb41b272017-02-23 00:31:59 +09003956 s = ast_for_body(c, CHILD(n, 5), &docstring);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003957 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003958 return NULL;
3959 classname = NEW_IDENTIFIER(CHILD(n, 1));
3960 if (!classname)
3961 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003962 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003963 return NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09003964 return ClassDef(classname, NULL, NULL, s, decorator_seq, docstring,
3965 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003966 }
3967
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003968 /* class NAME '(' arglist ')' ':' suite */
3969 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003970 {
3971 PyObject *dummy_name;
3972 expr_ty dummy;
3973 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3974 if (!dummy_name)
3975 return NULL;
3976 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3977 call = ast_for_call(c, CHILD(n, 3), dummy);
3978 if (!call)
3979 return NULL;
3980 }
INADA Naokicb41b272017-02-23 00:31:59 +09003981 s = ast_for_body(c, CHILD(n, 6), &docstring);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003982 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003983 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003984 classname = NEW_IDENTIFIER(CHILD(n, 1));
3985 if (!classname)
3986 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003987 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003988 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003989
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003990 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
INADA Naokicb41b272017-02-23 00:31:59 +09003991 decorator_seq, docstring, LINENO(n), n->n_col_offset,
3992 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003993}
3994
3995static stmt_ty
3996ast_for_stmt(struct compiling *c, const node *n)
3997{
3998 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003999 assert(NCH(n) == 1);
4000 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004001 }
4002 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004003 assert(num_stmts(n) == 1);
4004 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004005 }
4006 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004007 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004008 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4009 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004010 */
4011 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004012 case expr_stmt:
4013 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004014 case del_stmt:
4015 return ast_for_del_stmt(c, n);
4016 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00004017 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004018 case flow_stmt:
4019 return ast_for_flow_stmt(c, n);
4020 case import_stmt:
4021 return ast_for_import_stmt(c, n);
4022 case global_stmt:
4023 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004024 case nonlocal_stmt:
4025 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004026 case assert_stmt:
4027 return ast_for_assert_stmt(c, n);
4028 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004029 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004030 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4031 TYPE(n), NCH(n));
4032 return NULL;
4033 }
4034 }
4035 else {
4036 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004037 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004038 */
4039 node *ch = CHILD(n, 0);
4040 REQ(n, compound_stmt);
4041 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004042 case if_stmt:
4043 return ast_for_if_stmt(c, ch);
4044 case while_stmt:
4045 return ast_for_while_stmt(c, ch);
4046 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004047 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004048 case try_stmt:
4049 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004050 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004051 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004052 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004053 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004054 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004055 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 case decorated:
4057 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004058 case async_stmt:
4059 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004060 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004061 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004062 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4063 TYPE(n), NCH(n));
4064 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004065 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004066 }
4067}
4068
4069static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004070parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004071{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004072 const char *end;
4073 long x;
4074 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004075 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004076 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004077
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004078 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004079 errno = 0;
4080 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004081 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004082 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004083 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004084 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004085 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004086 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004087 }
4088 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004089 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004090 if (*end == '\0') {
4091 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004092 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004093 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004094 }
4095 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004096 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004097 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004098 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4099 if (compl.imag == -1.0 && PyErr_Occurred())
4100 return NULL;
4101 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004102 }
4103 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004104 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004105 dx = PyOS_string_to_double(s, NULL, NULL);
4106 if (dx == -1.0 && PyErr_Occurred())
4107 return NULL;
4108 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004109 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004110}
4111
4112static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004113parsenumber(struct compiling *c, const char *s)
4114{
4115 char *dup, *end;
4116 PyObject *res = NULL;
4117
4118 assert(s != NULL);
4119
4120 if (strchr(s, '_') == NULL) {
4121 return parsenumber_raw(c, s);
4122 }
4123 /* Create a duplicate without underscores. */
4124 dup = PyMem_Malloc(strlen(s) + 1);
4125 end = dup;
4126 for (; *s; s++) {
4127 if (*s != '_') {
4128 *end++ = *s;
4129 }
4130 }
4131 *end = '\0';
4132 res = parsenumber_raw(c, dup);
4133 PyMem_Free(dup);
4134 return res;
4135}
4136
4137static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004138decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004139{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004140 const char *s, *t;
4141 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004142 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4143 while (s < end && (*s & 0x80)) s++;
4144 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004145 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004146}
4147
Eric V. Smith56466482016-10-31 14:46:26 -04004148static int
4149warn_invalid_escape_sequence(struct compiling *c, const node *n,
4150 char first_invalid_escape_char)
4151{
4152 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4153 first_invalid_escape_char);
4154 if (msg == NULL) {
4155 return -1;
4156 }
4157 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
4158 c->c_filename, LINENO(n),
4159 NULL, NULL) < 0 &&
4160 PyErr_ExceptionMatches(PyExc_DeprecationWarning))
4161 {
Victor Stinnerf9cca362016-11-15 09:12:10 +01004162 const char *s;
4163
4164 /* Replace the DeprecationWarning exception with a SyntaxError
4165 to get a more accurate error report */
4166 PyErr_Clear();
4167
4168 s = PyUnicode_AsUTF8(msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004169 if (s != NULL) {
4170 ast_error(c, n, s);
4171 }
4172 Py_DECREF(msg);
4173 return -1;
4174 }
4175 Py_DECREF(msg);
4176 return 0;
4177}
4178
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004179static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004180decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4181 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004182{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004183 PyObject *v, *u;
4184 char *buf;
4185 char *p;
4186 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004187
Benjamin Peterson202803a2016-02-25 22:34:45 -08004188 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004189 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004190 return NULL;
4191 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4192 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4193 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4194 if (u == NULL)
4195 return NULL;
4196 p = buf = PyBytes_AsString(u);
4197 end = s + len;
4198 while (s < end) {
4199 if (*s == '\\') {
4200 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004201 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004202 strcpy(p, "u005c");
4203 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004204 if (s >= end)
4205 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004206 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004207 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004208 if (*s & 0x80) { /* XXX inefficient */
4209 PyObject *w;
4210 int kind;
4211 void *data;
4212 Py_ssize_t len, i;
4213 w = decode_utf8(c, &s, end);
4214 if (w == NULL) {
4215 Py_DECREF(u);
4216 return NULL;
4217 }
4218 kind = PyUnicode_KIND(w);
4219 data = PyUnicode_DATA(w);
4220 len = PyUnicode_GET_LENGTH(w);
4221 for (i = 0; i < len; i++) {
4222 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4223 sprintf(p, "\\U%08x", chr);
4224 p += 10;
4225 }
4226 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004227 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004228 Py_DECREF(w);
4229 } else {
4230 *p++ = *s++;
4231 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004232 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004233 len = p - buf;
4234 s = buf;
4235
Eric V. Smith56466482016-10-31 14:46:26 -04004236 const char *first_invalid_escape;
4237 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4238
4239 if (v != NULL && first_invalid_escape != NULL) {
4240 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4241 /* We have not decref u before because first_invalid_escape points
4242 inside u. */
4243 Py_XDECREF(u);
4244 Py_DECREF(v);
4245 return NULL;
4246 }
4247 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004248 Py_XDECREF(u);
4249 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004250}
4251
Eric V. Smith56466482016-10-31 14:46:26 -04004252static PyObject *
4253decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4254 size_t len)
4255{
4256 const char *first_invalid_escape;
4257 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4258 &first_invalid_escape);
4259 if (result == NULL)
4260 return NULL;
4261
4262 if (first_invalid_escape != NULL) {
4263 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4264 Py_DECREF(result);
4265 return NULL;
4266 }
4267 }
4268 return result;
4269}
4270
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004271/* Shift locations for the given node and all its children by adding `lineno`
4272 and `col_offset` to existing locations. */
4273static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4274{
4275 n->n_col_offset = n->n_col_offset + col_offset;
4276 for (int i = 0; i < NCH(n); ++i) {
4277 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4278 /* Shifting column offsets unnecessary if there's been newlines. */
4279 col_offset = 0;
4280 }
4281 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4282 }
4283 n->n_lineno = n->n_lineno + lineno;
4284}
4285
4286/* Fix locations for the given node and its children.
4287
4288 `parent` is the enclosing node.
4289 `n` is the node which locations are going to be fixed relative to parent.
4290 `expr_str` is the child node's string representation, incuding braces.
4291*/
4292static void
4293fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4294{
4295 char *substr = NULL;
4296 char *start;
4297 int lines = LINENO(parent) - 1;
4298 int cols = parent->n_col_offset;
4299 /* Find the full fstring to fix location information in `n`. */
4300 while (parent && parent->n_type != STRING)
4301 parent = parent->n_child;
4302 if (parent && parent->n_str) {
4303 substr = strstr(parent->n_str, expr_str);
4304 if (substr) {
4305 start = substr;
4306 while (start > parent->n_str) {
4307 if (start[0] == '\n')
4308 break;
4309 start--;
4310 }
4311 cols += substr - start;
4312 /* Fix lineno in mulitline strings. */
4313 while ((substr = strchr(substr + 1, '\n')))
4314 lines--;
4315 }
4316 }
4317 fstring_shift_node_locations(n, lines, cols);
4318}
4319
Eric V. Smith451d0e32016-09-09 21:56:20 -04004320/* Compile this expression in to an expr_ty. Add parens around the
4321 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004322static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004323fstring_compile_expr(const char *expr_start, const char *expr_end,
4324 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004325
Eric V. Smith235a6f02015-09-19 14:51:32 -04004326{
4327 PyCompilerFlags cf;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004328 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004329 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004330 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004331 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004332 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004333
Eric V. Smith1d44c412015-09-23 07:49:00 -04004334 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004335 assert(*(expr_start-1) == '{');
4336 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004337
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004338 /* If the substring is all whitespace, it's an error. We need to catch this
4339 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4340 because turning the expression '' in to '()' would go from being invalid
4341 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004342 for (s = expr_start; s != expr_end; s++) {
4343 char c = *s;
4344 /* The Python parser ignores only the following whitespace
4345 characters (\r already is converted to \n). */
4346 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004347 break;
4348 }
4349 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004350 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004351 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004352 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004353 }
4354
Eric V. Smith451d0e32016-09-09 21:56:20 -04004355 len = expr_end - expr_start;
4356 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4357 str = PyMem_RawMalloc(len + 3);
4358 if (str == NULL)
4359 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004360
Eric V. Smith451d0e32016-09-09 21:56:20 -04004361 str[0] = '(';
4362 memcpy(str+1, expr_start, len);
4363 str[len+1] = ')';
4364 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004365
4366 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004367 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4368 Py_eval_input, 0);
4369 if (!mod_n) {
4370 PyMem_RawFree(str);
4371 return NULL;
4372 }
4373 /* Reuse str to find the correct column offset. */
4374 str[0] = '{';
4375 str[len+1] = '}';
4376 fstring_fix_node_location(n, mod_n, str);
4377 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004378 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004379 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004380 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004381 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004382 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004383}
4384
4385/* Return -1 on error.
4386
4387 Return 0 if we reached the end of the literal.
4388
4389 Return 1 if we haven't reached the end of the literal, but we want
4390 the caller to process the literal up to this point. Used for
4391 doubled braces.
4392*/
4393static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004394fstring_find_literal(const char **str, const char *end, int raw,
4395 PyObject **literal, int recurse_lvl,
4396 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004397{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004398 /* Get any literal string. It ends when we hit an un-doubled left
4399 brace (which isn't part of a unicode name escape such as
4400 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004401
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004402 const char *s = *str;
4403 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004404 int result = 0;
4405
Eric V. Smith235a6f02015-09-19 14:51:32 -04004406 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004407 while (s < end) {
4408 char ch = *s++;
4409 if (!raw && ch == '\\' && s < end) {
4410 ch = *s++;
4411 if (ch == 'N') {
4412 if (s < end && *s++ == '{') {
4413 while (s < end && *s++ != '}') {
4414 }
4415 continue;
4416 }
4417 break;
4418 }
4419 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4420 return -1;
4421 }
4422 }
4423 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004424 /* Check for doubled braces, but only at the top level. If
4425 we checked at every level, then f'{0:{3}}' would fail
4426 with the two closing braces. */
4427 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004428 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004429 /* We're going to tell the caller that the literal ends
4430 here, but that they should continue scanning. But also
4431 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004432 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004433 result = 1;
4434 goto done;
4435 }
4436
4437 /* Where a single '{' is the start of a new expression, a
4438 single '}' is not allowed. */
4439 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004440 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004441 ast_error(c, n, "f-string: single '}' is not allowed");
4442 return -1;
4443 }
4444 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004445 /* We're either at a '{', which means we're starting another
4446 expression; or a '}', which means we're at the end of this
4447 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004448 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004449 break;
4450 }
4451 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004452 *str = s;
4453 assert(s <= end);
4454 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004455done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004456 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004457 if (raw)
4458 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004459 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004460 NULL, NULL);
4461 else
Eric V. Smith56466482016-10-31 14:46:26 -04004462 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004463 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004464 if (!*literal)
4465 return -1;
4466 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004467 return result;
4468}
4469
4470/* Forward declaration because parsing is recursive. */
4471static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004472fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004473 struct compiling *c, const node *n);
4474
Eric V. Smith451d0e32016-09-09 21:56:20 -04004475/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004476 expression (so it must be a '{'). Returns the FormattedValue node,
4477 which includes the expression, conversion character, and
4478 format_spec expression.
4479
4480 Note that I don't do a perfect job here: I don't make sure that a
4481 closing brace doesn't match an opening paren, for example. It
4482 doesn't need to error on all invalid expressions, just correctly
4483 find the end of all valid ones. Any errors inside the expression
4484 will be caught when we parse it later. */
4485static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004486fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004487 expr_ty *expression, struct compiling *c, const node *n)
4488{
4489 /* Return -1 on error, else 0. */
4490
Eric V. Smith451d0e32016-09-09 21:56:20 -04004491 const char *expr_start;
4492 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004493 expr_ty simple_expression;
4494 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004495 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004496
4497 /* 0 if we're not in a string, else the quote char we're trying to
4498 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004499 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004500
4501 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4502 int string_type = 0;
4503
4504 /* Keep track of nesting level for braces/parens/brackets in
4505 expressions. */
4506 Py_ssize_t nested_depth = 0;
4507
4508 /* Can only nest one level deep. */
4509 if (recurse_lvl >= 2) {
4510 ast_error(c, n, "f-string: expressions nested too deeply");
4511 return -1;
4512 }
4513
4514 /* The first char must be a left brace, or we wouldn't have gotten
4515 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004516 assert(**str == '{');
4517 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004518
Eric V. Smith451d0e32016-09-09 21:56:20 -04004519 expr_start = *str;
4520 for (; *str < end; (*str)++) {
4521 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004522
4523 /* Loop invariants. */
4524 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004525 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004526 if (quote_char)
4527 assert(string_type == 1 || string_type == 3);
4528 else
4529 assert(string_type == 0);
4530
Eric V. Smith451d0e32016-09-09 21:56:20 -04004531 ch = **str;
4532 /* Nowhere inside an expression is a backslash allowed. */
4533 if (ch == '\\') {
4534 /* Error: can't include a backslash character, inside
4535 parens or strings or not. */
4536 ast_error(c, n, "f-string expression part "
4537 "cannot include a backslash");
4538 return -1;
4539 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004540 if (quote_char) {
4541 /* We're inside a string. See if we're at the end. */
4542 /* This code needs to implement the same non-error logic
4543 as tok_get from tokenizer.c, at the letter_quote
4544 label. To actually share that code would be a
4545 nightmare. But, it's unlikely to change and is small,
4546 so duplicate it here. Note we don't need to catch all
4547 of the errors, since they'll be caught when parsing the
4548 expression. We just need to match the non-error
4549 cases. Thus we can ignore \n in single-quoted strings,
4550 for example. Or non-terminated strings. */
4551 if (ch == quote_char) {
4552 /* Does this match the string_type (single or triple
4553 quoted)? */
4554 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004555 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004556 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004557 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004558 string_type = 0;
4559 quote_char = 0;
4560 continue;
4561 }
4562 } else {
4563 /* We're at the end of a normal string. */
4564 quote_char = 0;
4565 string_type = 0;
4566 continue;
4567 }
4568 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004569 } else if (ch == '\'' || ch == '"') {
4570 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004571 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004572 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004573 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004574 } else {
4575 /* Start of a normal string. */
4576 string_type = 1;
4577 }
4578 /* Start looking for the end of the string. */
4579 quote_char = ch;
4580 } else if (ch == '[' || ch == '{' || ch == '(') {
4581 nested_depth++;
4582 } else if (nested_depth != 0 &&
4583 (ch == ']' || ch == '}' || ch == ')')) {
4584 nested_depth--;
4585 } else if (ch == '#') {
4586 /* Error: can't include a comment character, inside parens
4587 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004588 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004589 return -1;
4590 } else if (nested_depth == 0 &&
4591 (ch == '!' || ch == ':' || ch == '}')) {
4592 /* First, test for the special case of "!=". Since '=' is
4593 not an allowed conversion character, nothing is lost in
4594 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004595 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004596 /* This isn't a conversion character, just continue. */
4597 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004598 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004599 /* Normal way out of this loop. */
4600 break;
4601 } else {
4602 /* Just consume this char and loop around. */
4603 }
4604 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004605 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004606 /* If we leave this loop in a string or with mismatched parens, we
4607 don't care. We'll get a syntax error when compiling the
4608 expression. But, we can produce a better error message, so
4609 let's just do that.*/
4610 if (quote_char) {
4611 ast_error(c, n, "f-string: unterminated string");
4612 return -1;
4613 }
4614 if (nested_depth) {
4615 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4616 return -1;
4617 }
4618
Eric V. Smith451d0e32016-09-09 21:56:20 -04004619 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004620 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004621
4622 /* Compile the expression as soon as possible, so we show errors
4623 related to the expression before errors related to the
4624 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004625 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004626 if (!simple_expression)
4627 return -1;
4628
4629 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004630 if (**str == '!') {
4631 *str += 1;
4632 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004633 goto unexpected_end_of_string;
4634
Eric V. Smith451d0e32016-09-09 21:56:20 -04004635 conversion = **str;
4636 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004637
4638 /* Validate the conversion. */
4639 if (!(conversion == 's' || conversion == 'r'
4640 || conversion == 'a')) {
4641 ast_error(c, n, "f-string: invalid conversion character: "
4642 "expected 's', 'r', or 'a'");
4643 return -1;
4644 }
4645 }
4646
4647 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004648 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004649 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004650 if (**str == ':') {
4651 *str += 1;
4652 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004653 goto unexpected_end_of_string;
4654
4655 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004656 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004657 if (!format_spec)
4658 return -1;
4659 }
4660
Eric V. Smith451d0e32016-09-09 21:56:20 -04004661 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004662 goto unexpected_end_of_string;
4663
4664 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004665 assert(*str < end);
4666 assert(**str == '}');
4667 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004668
Eric V. Smith451d0e32016-09-09 21:56:20 -04004669 /* And now create the FormattedValue node that represents this
4670 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004671 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004672 format_spec, LINENO(n), n->n_col_offset,
4673 c->c_arena);
4674 if (!*expression)
4675 return -1;
4676
4677 return 0;
4678
4679unexpected_end_of_string:
4680 ast_error(c, n, "f-string: expecting '}'");
4681 return -1;
4682}
4683
4684/* Return -1 on error.
4685
4686 Return 0 if we have a literal (possible zero length) and an
4687 expression (zero length if at the end of the string.
4688
4689 Return 1 if we have a literal, but no expression, and we want the
4690 caller to call us again. This is used to deal with doubled
4691 braces.
4692
4693 When called multiple times on the string 'a{{b{0}c', this function
4694 will return:
4695
4696 1. the literal 'a{' with no expression, and a return value
4697 of 1. Despite the fact that there's no expression, the return
4698 value of 1 means we're not finished yet.
4699
4700 2. the literal 'b' and the expression '0', with a return value of
4701 0. The fact that there's an expression means we're not finished.
4702
4703 3. literal 'c' with no expression and a return value of 0. The
4704 combination of the return value of 0 with no expression means
4705 we're finished.
4706*/
4707static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004708fstring_find_literal_and_expr(const char **str, const char *end, int raw,
4709 int recurse_lvl, PyObject **literal,
4710 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004711 struct compiling *c, const node *n)
4712{
4713 int result;
4714
4715 assert(*literal == NULL && *expression == NULL);
4716
4717 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004718 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004719 if (result < 0)
4720 goto error;
4721
4722 assert(result == 0 || result == 1);
4723
4724 if (result == 1)
4725 /* We have a literal, but don't look at the expression. */
4726 return 1;
4727
Eric V. Smith451d0e32016-09-09 21:56:20 -04004728 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004729 /* We're at the end of the string or the end of a nested
4730 f-string: no expression. The top-level error case where we
4731 expect to be at the end of the string but we're at a '}' is
4732 handled later. */
4733 return 0;
4734
4735 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004736 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004737
Eric V. Smith451d0e32016-09-09 21:56:20 -04004738 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004739 goto error;
4740
4741 return 0;
4742
4743error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004744 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004745 return -1;
4746}
4747
4748#define EXPRLIST_N_CACHED 64
4749
4750typedef struct {
4751 /* Incrementally build an array of expr_ty, so be used in an
4752 asdl_seq. Cache some small but reasonably sized number of
4753 expr_ty's, and then after that start dynamically allocating,
4754 doubling the number allocated each time. Note that the f-string
4755 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
4756 Str for the literal 'a'. So you add expr_ty's about twice as
4757 fast as you add exressions in an f-string. */
4758
4759 Py_ssize_t allocated; /* Number we've allocated. */
4760 Py_ssize_t size; /* Number we've used. */
4761 expr_ty *p; /* Pointer to the memory we're actually
4762 using. Will point to 'data' until we
4763 start dynamically allocating. */
4764 expr_ty data[EXPRLIST_N_CACHED];
4765} ExprList;
4766
4767#ifdef NDEBUG
4768#define ExprList_check_invariants(l)
4769#else
4770static void
4771ExprList_check_invariants(ExprList *l)
4772{
4773 /* Check our invariants. Make sure this object is "live", and
4774 hasn't been deallocated. */
4775 assert(l->size >= 0);
4776 assert(l->p != NULL);
4777 if (l->size <= EXPRLIST_N_CACHED)
4778 assert(l->data == l->p);
4779}
4780#endif
4781
4782static void
4783ExprList_Init(ExprList *l)
4784{
4785 l->allocated = EXPRLIST_N_CACHED;
4786 l->size = 0;
4787
4788 /* Until we start allocating dynamically, p points to data. */
4789 l->p = l->data;
4790
4791 ExprList_check_invariants(l);
4792}
4793
4794static int
4795ExprList_Append(ExprList *l, expr_ty exp)
4796{
4797 ExprList_check_invariants(l);
4798 if (l->size >= l->allocated) {
4799 /* We need to alloc (or realloc) the memory. */
4800 Py_ssize_t new_size = l->allocated * 2;
4801
4802 /* See if we've ever allocated anything dynamically. */
4803 if (l->p == l->data) {
4804 Py_ssize_t i;
4805 /* We're still using the cached data. Switch to
4806 alloc-ing. */
4807 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4808 if (!l->p)
4809 return -1;
4810 /* Copy the cached data into the new buffer. */
4811 for (i = 0; i < l->size; i++)
4812 l->p[i] = l->data[i];
4813 } else {
4814 /* Just realloc. */
4815 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4816 if (!tmp) {
4817 PyMem_RawFree(l->p);
4818 l->p = NULL;
4819 return -1;
4820 }
4821 l->p = tmp;
4822 }
4823
4824 l->allocated = new_size;
4825 assert(l->allocated == 2 * l->size);
4826 }
4827
4828 l->p[l->size++] = exp;
4829
4830 ExprList_check_invariants(l);
4831 return 0;
4832}
4833
4834static void
4835ExprList_Dealloc(ExprList *l)
4836{
4837 ExprList_check_invariants(l);
4838
4839 /* If there's been an error, or we've never dynamically allocated,
4840 do nothing. */
4841 if (!l->p || l->p == l->data) {
4842 /* Do nothing. */
4843 } else {
4844 /* We have dynamically allocated. Free the memory. */
4845 PyMem_RawFree(l->p);
4846 }
4847 l->p = NULL;
4848 l->size = -1;
4849}
4850
4851static asdl_seq *
4852ExprList_Finish(ExprList *l, PyArena *arena)
4853{
4854 asdl_seq *seq;
4855
4856 ExprList_check_invariants(l);
4857
4858 /* Allocate the asdl_seq and copy the expressions in to it. */
4859 seq = _Py_asdl_seq_new(l->size, arena);
4860 if (seq) {
4861 Py_ssize_t i;
4862 for (i = 0; i < l->size; i++)
4863 asdl_seq_SET(seq, i, l->p[i]);
4864 }
4865 ExprList_Dealloc(l);
4866 return seq;
4867}
4868
4869/* The FstringParser is designed to add a mix of strings and
4870 f-strings, and concat them together as needed. Ultimately, it
4871 generates an expr_ty. */
4872typedef struct {
4873 PyObject *last_str;
4874 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004875 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004876} FstringParser;
4877
4878#ifdef NDEBUG
4879#define FstringParser_check_invariants(state)
4880#else
4881static void
4882FstringParser_check_invariants(FstringParser *state)
4883{
4884 if (state->last_str)
4885 assert(PyUnicode_CheckExact(state->last_str));
4886 ExprList_check_invariants(&state->expr_list);
4887}
4888#endif
4889
4890static void
4891FstringParser_Init(FstringParser *state)
4892{
4893 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004894 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004895 ExprList_Init(&state->expr_list);
4896 FstringParser_check_invariants(state);
4897}
4898
4899static void
4900FstringParser_Dealloc(FstringParser *state)
4901{
4902 FstringParser_check_invariants(state);
4903
4904 Py_XDECREF(state->last_str);
4905 ExprList_Dealloc(&state->expr_list);
4906}
4907
4908/* Make a Str node, but decref the PyUnicode object being added. */
4909static expr_ty
4910make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4911{
4912 PyObject *s = *str;
4913 *str = NULL;
4914 assert(PyUnicode_CheckExact(s));
4915 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4916 Py_DECREF(s);
4917 return NULL;
4918 }
4919 return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
4920}
4921
4922/* Add a non-f-string (that is, a regular literal string). str is
4923 decref'd. */
4924static int
4925FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4926{
4927 FstringParser_check_invariants(state);
4928
4929 assert(PyUnicode_CheckExact(str));
4930
4931 if (PyUnicode_GET_LENGTH(str) == 0) {
4932 Py_DECREF(str);
4933 return 0;
4934 }
4935
4936 if (!state->last_str) {
4937 /* We didn't have a string before, so just remember this one. */
4938 state->last_str = str;
4939 } else {
4940 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004941 PyUnicode_AppendAndDel(&state->last_str, str);
4942 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004943 return -1;
4944 }
4945 FstringParser_check_invariants(state);
4946 return 0;
4947}
4948
Eric V. Smith451d0e32016-09-09 21:56:20 -04004949/* Parse an f-string. The f-string is in *str to end, with no
4950 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004951static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004952FstringParser_ConcatFstring(FstringParser *state, const char **str,
4953 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004954 struct compiling *c, const node *n)
4955{
4956 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004957 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004958
4959 /* Parse the f-string. */
4960 while (1) {
4961 PyObject *literal = NULL;
4962 expr_ty expression = NULL;
4963
4964 /* If there's a zero length literal in front of the
4965 expression, literal will be NULL. If we're at the end of
4966 the f-string, expression will be NULL (unless result == 1,
4967 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004968 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004969 &literal, &expression,
4970 c, n);
4971 if (result < 0)
4972 return -1;
4973
4974 /* Add the literal, if any. */
4975 if (!literal) {
4976 /* Do nothing. Just leave last_str alone (and possibly
4977 NULL). */
4978 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04004979 /* Note that the literal can be zero length, if the
4980 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004981 state->last_str = literal;
4982 literal = NULL;
4983 } else {
4984 /* We have a literal, concatenate it. */
4985 assert(PyUnicode_GET_LENGTH(literal) != 0);
4986 if (FstringParser_ConcatAndDel(state, literal) < 0)
4987 return -1;
4988 literal = NULL;
4989 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004990
4991 /* We've dealt with the literal now. It can't be leaked on further
4992 errors. */
4993 assert(literal == NULL);
4994
4995 /* See if we should just loop around to get the next literal
4996 and expression, while ignoring the expression this
4997 time. This is used for un-doubling braces, as an
4998 optimization. */
4999 if (result == 1)
5000 continue;
5001
5002 if (!expression)
5003 /* We're done with this f-string. */
5004 break;
5005
5006 /* We know we have an expression. Convert any existing string
5007 to a Str node. */
5008 if (!state->last_str) {
5009 /* Do nothing. No previous literal. */
5010 } else {
5011 /* Convert the existing last_str literal to a Str node. */
5012 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5013 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5014 return -1;
5015 }
5016
5017 if (ExprList_Append(&state->expr_list, expression) < 0)
5018 return -1;
5019 }
5020
Eric V. Smith235a6f02015-09-19 14:51:32 -04005021 /* If recurse_lvl is zero, then we must be at the end of the
5022 string. Otherwise, we must be at a right brace. */
5023
Eric V. Smith451d0e32016-09-09 21:56:20 -04005024 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005025 ast_error(c, n, "f-string: unexpected end of string");
5026 return -1;
5027 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005028 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005029 ast_error(c, n, "f-string: expecting '}'");
5030 return -1;
5031 }
5032
5033 FstringParser_check_invariants(state);
5034 return 0;
5035}
5036
5037/* Convert the partial state reflected in last_str and expr_list to an
5038 expr_ty. The expr_ty can be a Str, or a JoinedStr. */
5039static expr_ty
5040FstringParser_Finish(FstringParser *state, struct compiling *c,
5041 const node *n)
5042{
5043 asdl_seq *seq;
5044
5045 FstringParser_check_invariants(state);
5046
5047 /* If we're just a constant string with no expressions, return
5048 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005049 if (!state->fmode) {
5050 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005051 if (!state->last_str) {
5052 /* Create a zero length string. */
5053 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5054 if (!state->last_str)
5055 goto error;
5056 }
5057 return make_str_node_and_del(&state->last_str, c, n);
5058 }
5059
5060 /* Create a Str node out of last_str, if needed. It will be the
5061 last node in our expression list. */
5062 if (state->last_str) {
5063 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5064 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5065 goto error;
5066 }
5067 /* This has already been freed. */
5068 assert(state->last_str == NULL);
5069
5070 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5071 if (!seq)
5072 goto error;
5073
Eric V. Smith235a6f02015-09-19 14:51:32 -04005074 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
5075
5076error:
5077 FstringParser_Dealloc(state);
5078 return NULL;
5079}
5080
Eric V. Smith451d0e32016-09-09 21:56:20 -04005081/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5082 at end, parse it into an expr_ty. Return NULL on error. Adjust
5083 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005084static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005085fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005086 struct compiling *c, const node *n)
5087{
5088 FstringParser state;
5089
5090 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005091 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005092 c, n) < 0) {
5093 FstringParser_Dealloc(&state);
5094 return NULL;
5095 }
5096
5097 return FstringParser_Finish(&state, c, n);
5098}
5099
5100/* n is a Python string literal, including the bracketing quote
5101 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005102 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005103 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005104 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5105 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005106*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005107static int
5108parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5109 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005110{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005111 size_t len;
5112 const char *s = STR(n);
5113 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005114 int fmode = 0;
5115 *bytesmode = 0;
5116 *rawmode = 0;
5117 *result = NULL;
5118 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005119 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005120 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005121 if (quote == 'b' || quote == 'B') {
5122 quote = *++s;
5123 *bytesmode = 1;
5124 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005125 else if (quote == 'u' || quote == 'U') {
5126 quote = *++s;
5127 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005128 else if (quote == 'r' || quote == 'R') {
5129 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005130 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005131 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005132 else if (quote == 'f' || quote == 'F') {
5133 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005134 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005135 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005136 else {
5137 break;
5138 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005139 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005140 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005141 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005142 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005143 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005144 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005145 if (quote != '\'' && quote != '\"') {
5146 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005147 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005148 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005149 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005150 s++;
5151 len = strlen(s);
5152 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005153 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005154 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005155 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005156 }
5157 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005158 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005159 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005160 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005161 }
5162 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005163 /* A triple quoted string. We've already skipped one quote at
5164 the start and one at the end of the string. Now skip the
5165 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005166 s += 2;
5167 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005168 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005169 if (s[--len] != quote || s[--len] != quote) {
5170 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005171 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005172 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005173 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005174
Eric V. Smith451d0e32016-09-09 21:56:20 -04005175 if (fmode) {
5176 /* Just return the bytes. The caller will parse the resulting
5177 string. */
5178 *fstr = s;
5179 *fstrlen = len;
5180 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005181 }
5182
Eric V. Smith451d0e32016-09-09 21:56:20 -04005183 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005184 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005185 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005186 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005187 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005188 const char *ch;
5189 for (ch = s; *ch; ch++) {
5190 if (Py_CHARMASK(*ch) >= 0x80) {
5191 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005192 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005193 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005194 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005195 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005196 if (*rawmode)
5197 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005198 else
Eric V. Smith56466482016-10-31 14:46:26 -04005199 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005200 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005201 if (*rawmode)
5202 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005203 else
Eric V. Smith56466482016-10-31 14:46:26 -04005204 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005205 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005206 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005207}
5208
Eric V. Smith235a6f02015-09-19 14:51:32 -04005209/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5210 each STRING atom, and process it as needed. For bytes, just
5211 concatenate them together, and the result will be a Bytes node. For
5212 normal strings and f-strings, concatenate them together. The result
5213 will be a Str node if there were no f-strings; a FormattedValue
5214 node if there's just an f-string (with no leading or trailing
5215 literals), or a JoinedStr node if there are multiple f-strings or
5216 any literals involved. */
5217static expr_ty
5218parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005219{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005220 int bytesmode = 0;
5221 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005222 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005223
5224 FstringParser state;
5225 FstringParser_Init(&state);
5226
5227 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005228 int this_bytesmode;
5229 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005230 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005231 const char *fstr;
5232 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005233
5234 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005235 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5236 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005237 goto error;
5238
5239 /* Check that we're not mixing bytes with unicode. */
5240 if (i != 0 && bytesmode != this_bytesmode) {
5241 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005242 /* s is NULL if the current string part is an f-string. */
5243 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005244 goto error;
5245 }
5246 bytesmode = this_bytesmode;
5247
Eric V. Smith451d0e32016-09-09 21:56:20 -04005248 if (fstr != NULL) {
5249 int result;
5250 assert(s == NULL && !bytesmode);
5251 /* This is an f-string. Parse and concatenate it. */
5252 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5253 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005254 if (result < 0)
5255 goto error;
5256 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005257 /* A string or byte string. */
5258 assert(s != NULL && fstr == NULL);
5259
Eric V. Smith451d0e32016-09-09 21:56:20 -04005260 assert(bytesmode ? PyBytes_CheckExact(s) :
5261 PyUnicode_CheckExact(s));
5262
Eric V. Smith451d0e32016-09-09 21:56:20 -04005263 if (bytesmode) {
5264 /* For bytes, concat as we go. */
5265 if (i == 0) {
5266 /* First time, just remember this value. */
5267 bytes_str = s;
5268 } else {
5269 PyBytes_ConcatAndDel(&bytes_str, s);
5270 if (!bytes_str)
5271 goto error;
5272 }
5273 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005274 /* This is a regular string. Concatenate it. */
5275 if (FstringParser_ConcatAndDel(&state, s) < 0)
5276 goto error;
5277 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005278 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005279 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005280 if (bytesmode) {
5281 /* Just return the bytes object and we're done. */
5282 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5283 goto error;
5284 return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
5285 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005286
Eric V. Smith235a6f02015-09-19 14:51:32 -04005287 /* We're not a bytes string, bytes_str should never have been set. */
5288 assert(bytes_str == NULL);
5289
5290 return FstringParser_Finish(&state, c, n);
5291
5292error:
5293 Py_XDECREF(bytes_str);
5294 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005295 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005296}