blob: e721ac1c0a4a15de9216e59c9118f7cc2d52a1ad [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>
Serhiy Storchakaddbce132017-11-15 17:39:37 +020014#include <stdbool.h>
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015
Serhiy Storchaka58159ef2019-01-12 09:46:50 +020016#define MAXLEVEL 200 /* Max parentheses level */
17
Benjamin Peterson832bfe22011-08-09 16:15:04 -050018static int validate_stmts(asdl_seq *);
19static int validate_exprs(asdl_seq *, expr_context_ty, int);
20static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
21static int validate_stmt(stmt_ty);
22static int validate_expr(expr_ty, expr_context_ty);
23
24static int
25validate_comprehension(asdl_seq *gens)
26{
Victor Stinner4d73ae72018-11-22 14:45:16 +010027 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050028 if (!asdl_seq_LEN(gens)) {
29 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
30 return 0;
31 }
32 for (i = 0; i < asdl_seq_LEN(gens); i++) {
33 comprehension_ty comp = asdl_seq_GET(gens, i);
34 if (!validate_expr(comp->target, Store) ||
35 !validate_expr(comp->iter, Load) ||
36 !validate_exprs(comp->ifs, Load, 0))
37 return 0;
38 }
39 return 1;
40}
41
42static int
43validate_slice(slice_ty slice)
44{
45 switch (slice->kind) {
46 case Slice_kind:
47 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
48 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
49 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
50 case ExtSlice_kind: {
Victor Stinner4d73ae72018-11-22 14:45:16 +010051 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050052 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
53 return 0;
54 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
55 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
56 return 0;
57 return 1;
58 }
59 case Index_kind:
60 return validate_expr(slice->v.Index.value, Load);
61 default:
62 PyErr_SetString(PyExc_SystemError, "unknown slice node");
63 return 0;
64 }
65}
66
67static int
68validate_keywords(asdl_seq *keywords)
69{
Victor Stinner4d73ae72018-11-22 14:45:16 +010070 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050071 for (i = 0; i < asdl_seq_LEN(keywords); i++)
72 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
73 return 0;
74 return 1;
75}
76
77static int
78validate_args(asdl_seq *args)
79{
Victor Stinner4d73ae72018-11-22 14:45:16 +010080 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050081 for (i = 0; i < asdl_seq_LEN(args); i++) {
82 arg_ty arg = asdl_seq_GET(args, i);
83 if (arg->annotation && !validate_expr(arg->annotation, Load))
84 return 0;
85 }
86 return 1;
87}
88
89static const char *
90expr_context_name(expr_context_ty ctx)
91{
92 switch (ctx) {
93 case Load:
94 return "Load";
95 case Store:
96 return "Store";
Emily Morehouse8f59ee02019-01-24 16:49:56 -070097 case NamedStore:
98 return "NamedStore";
Benjamin Peterson832bfe22011-08-09 16:15:04 -050099 case Del:
100 return "Del";
101 case AugLoad:
102 return "AugLoad";
103 case AugStore:
104 return "AugStore";
105 case Param:
106 return "Param";
107 default:
Barry Warsawb2e57942017-09-14 18:13:16 -0700108 Py_UNREACHABLE();
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500109 }
110}
111
112static int
113validate_arguments(arguments_ty args)
114{
115 if (!validate_args(args->args))
116 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700117 if (args->vararg && args->vararg->annotation
118 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500119 return 0;
120 }
121 if (!validate_args(args->kwonlyargs))
122 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100123 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700124 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500125 return 0;
126 }
127 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
128 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
129 return 0;
130 }
131 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
132 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
133 "kw_defaults on arguments");
134 return 0;
135 }
136 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
137}
138
139static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100140validate_constant(PyObject *value)
141{
142 if (value == Py_None || value == Py_Ellipsis)
143 return 1;
144
145 if (PyLong_CheckExact(value)
146 || PyFloat_CheckExact(value)
147 || PyComplex_CheckExact(value)
148 || PyBool_Check(value)
149 || PyUnicode_CheckExact(value)
150 || PyBytes_CheckExact(value))
151 return 1;
152
153 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
154 PyObject *it;
155
156 it = PyObject_GetIter(value);
157 if (it == NULL)
158 return 0;
159
160 while (1) {
161 PyObject *item = PyIter_Next(it);
162 if (item == NULL) {
163 if (PyErr_Occurred()) {
164 Py_DECREF(it);
165 return 0;
166 }
167 break;
168 }
169
170 if (!validate_constant(item)) {
171 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100172 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100173 return 0;
174 }
Victor Stinner726f6902016-01-27 00:11:47 +0100175 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100176 }
177
178 Py_DECREF(it);
179 return 1;
180 }
181
182 return 0;
183}
184
185static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500186validate_expr(expr_ty exp, expr_context_ty ctx)
187{
188 int check_ctx = 1;
189 expr_context_ty actual_ctx;
190
191 /* First check expression context. */
192 switch (exp->kind) {
193 case Attribute_kind:
194 actual_ctx = exp->v.Attribute.ctx;
195 break;
196 case Subscript_kind:
197 actual_ctx = exp->v.Subscript.ctx;
198 break;
199 case Starred_kind:
200 actual_ctx = exp->v.Starred.ctx;
201 break;
202 case Name_kind:
203 actual_ctx = exp->v.Name.ctx;
204 break;
205 case List_kind:
206 actual_ctx = exp->v.List.ctx;
207 break;
208 case Tuple_kind:
209 actual_ctx = exp->v.Tuple.ctx;
210 break;
211 default:
212 if (ctx != Load) {
213 PyErr_Format(PyExc_ValueError, "expression which can't be "
214 "assigned to in %s context", expr_context_name(ctx));
215 return 0;
216 }
217 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100218 /* set actual_ctx to prevent gcc warning */
219 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500220 }
221 if (check_ctx && actual_ctx != ctx) {
222 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
223 expr_context_name(ctx), expr_context_name(actual_ctx));
224 return 0;
225 }
226
227 /* Now validate expression. */
228 switch (exp->kind) {
229 case BoolOp_kind:
230 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
231 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
232 return 0;
233 }
234 return validate_exprs(exp->v.BoolOp.values, Load, 0);
235 case BinOp_kind:
236 return validate_expr(exp->v.BinOp.left, Load) &&
237 validate_expr(exp->v.BinOp.right, Load);
238 case UnaryOp_kind:
239 return validate_expr(exp->v.UnaryOp.operand, Load);
240 case Lambda_kind:
241 return validate_arguments(exp->v.Lambda.args) &&
242 validate_expr(exp->v.Lambda.body, Load);
243 case IfExp_kind:
244 return validate_expr(exp->v.IfExp.test, Load) &&
245 validate_expr(exp->v.IfExp.body, Load) &&
246 validate_expr(exp->v.IfExp.orelse, Load);
247 case Dict_kind:
248 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
249 PyErr_SetString(PyExc_ValueError,
250 "Dict doesn't have the same number of keys as values");
251 return 0;
252 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400253 /* null_ok=1 for keys expressions to allow dict unpacking to work in
254 dict literals, i.e. ``{**{a:b}}`` */
255 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
256 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500257 case Set_kind:
258 return validate_exprs(exp->v.Set.elts, Load, 0);
259#define COMP(NAME) \
260 case NAME ## _kind: \
261 return validate_comprehension(exp->v.NAME.generators) && \
262 validate_expr(exp->v.NAME.elt, Load);
263 COMP(ListComp)
264 COMP(SetComp)
265 COMP(GeneratorExp)
266#undef COMP
267 case DictComp_kind:
268 return validate_comprehension(exp->v.DictComp.generators) &&
269 validate_expr(exp->v.DictComp.key, Load) &&
270 validate_expr(exp->v.DictComp.value, Load);
271 case Yield_kind:
272 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500273 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000274 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400275 case Await_kind:
276 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500277 case Compare_kind:
278 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
279 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
280 return 0;
281 }
282 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
283 asdl_seq_LEN(exp->v.Compare.ops)) {
284 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
285 "of comparators and operands");
286 return 0;
287 }
288 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
289 validate_expr(exp->v.Compare.left, Load);
290 case Call_kind:
291 return validate_expr(exp->v.Call.func, Load) &&
292 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400293 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100294 case Constant_kind:
295 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100296 PyErr_Format(PyExc_TypeError,
297 "got an invalid type in Constant: %s",
298 Py_TYPE(exp->v.Constant.value)->tp_name);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100299 return 0;
300 }
301 return 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400302 case JoinedStr_kind:
303 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
304 case FormattedValue_kind:
305 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
306 return 0;
307 if (exp->v.FormattedValue.format_spec)
308 return validate_expr(exp->v.FormattedValue.format_spec, Load);
309 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500310 case Attribute_kind:
311 return validate_expr(exp->v.Attribute.value, Load);
312 case Subscript_kind:
313 return validate_slice(exp->v.Subscript.slice) &&
314 validate_expr(exp->v.Subscript.value, Load);
315 case Starred_kind:
316 return validate_expr(exp->v.Starred.value, ctx);
317 case List_kind:
318 return validate_exprs(exp->v.List.elts, ctx, 0);
319 case Tuple_kind:
320 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300321 /* This last case doesn't have any checking. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500322 case Name_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500323 return 1;
324 default:
325 PyErr_SetString(PyExc_SystemError, "unexpected expression");
326 return 0;
327 }
328}
329
330static int
331validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
332{
333 if (asdl_seq_LEN(seq))
334 return 1;
335 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
336 return 0;
337}
338
339static int
340validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
341{
342 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
343 validate_exprs(targets, ctx, 0);
344}
345
346static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300347validate_body(asdl_seq *body, const char *owner)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500348{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300349 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500350}
351
352static int
353validate_stmt(stmt_ty stmt)
354{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100355 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500356 switch (stmt->kind) {
357 case FunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300358 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500359 validate_arguments(stmt->v.FunctionDef.args) &&
360 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
361 (!stmt->v.FunctionDef.returns ||
362 validate_expr(stmt->v.FunctionDef.returns, Load));
363 case ClassDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300364 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500365 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
366 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400367 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500368 case Return_kind:
369 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
370 case Delete_kind:
371 return validate_assignlist(stmt->v.Delete.targets, Del);
372 case Assign_kind:
373 return validate_assignlist(stmt->v.Assign.targets, Store) &&
374 validate_expr(stmt->v.Assign.value, Load);
375 case AugAssign_kind:
376 return validate_expr(stmt->v.AugAssign.target, Store) &&
377 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700378 case AnnAssign_kind:
379 if (stmt->v.AnnAssign.target->kind != Name_kind &&
380 stmt->v.AnnAssign.simple) {
381 PyErr_SetString(PyExc_TypeError,
382 "AnnAssign with simple non-Name target");
383 return 0;
384 }
385 return validate_expr(stmt->v.AnnAssign.target, Store) &&
386 (!stmt->v.AnnAssign.value ||
387 validate_expr(stmt->v.AnnAssign.value, Load)) &&
388 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500389 case For_kind:
390 return validate_expr(stmt->v.For.target, Store) &&
391 validate_expr(stmt->v.For.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300392 validate_body(stmt->v.For.body, "For") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500393 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400394 case AsyncFor_kind:
395 return validate_expr(stmt->v.AsyncFor.target, Store) &&
396 validate_expr(stmt->v.AsyncFor.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300397 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400398 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500399 case While_kind:
400 return validate_expr(stmt->v.While.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300401 validate_body(stmt->v.While.body, "While") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500402 validate_stmts(stmt->v.While.orelse);
403 case If_kind:
404 return validate_expr(stmt->v.If.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300405 validate_body(stmt->v.If.body, "If") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500406 validate_stmts(stmt->v.If.orelse);
407 case With_kind:
408 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
409 return 0;
410 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
411 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
412 if (!validate_expr(item->context_expr, Load) ||
413 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
414 return 0;
415 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300416 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400417 case AsyncWith_kind:
418 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
419 return 0;
420 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
421 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
422 if (!validate_expr(item->context_expr, Load) ||
423 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
424 return 0;
425 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300426 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500427 case Raise_kind:
428 if (stmt->v.Raise.exc) {
429 return validate_expr(stmt->v.Raise.exc, Load) &&
430 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
431 }
432 if (stmt->v.Raise.cause) {
433 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
434 return 0;
435 }
436 return 1;
437 case Try_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300438 if (!validate_body(stmt->v.Try.body, "Try"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500439 return 0;
440 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
441 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
442 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
443 return 0;
444 }
445 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
446 asdl_seq_LEN(stmt->v.Try.orelse)) {
447 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
448 return 0;
449 }
450 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
451 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
452 if ((handler->v.ExceptHandler.type &&
453 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300454 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500455 return 0;
456 }
457 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
458 validate_stmts(stmt->v.Try.finalbody)) &&
459 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
460 validate_stmts(stmt->v.Try.orelse));
461 case Assert_kind:
462 return validate_expr(stmt->v.Assert.test, Load) &&
463 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
464 case Import_kind:
465 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
466 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300467 if (stmt->v.ImportFrom.level < 0) {
468 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500469 return 0;
470 }
471 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
472 case Global_kind:
473 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
474 case Nonlocal_kind:
475 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
476 case Expr_kind:
477 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400478 case AsyncFunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300479 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400480 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
481 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
482 (!stmt->v.AsyncFunctionDef.returns ||
483 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500484 case Pass_kind:
485 case Break_kind:
486 case Continue_kind:
487 return 1;
488 default:
489 PyErr_SetString(PyExc_SystemError, "unexpected statement");
490 return 0;
491 }
492}
493
494static int
495validate_stmts(asdl_seq *seq)
496{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100497 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500498 for (i = 0; i < asdl_seq_LEN(seq); i++) {
499 stmt_ty stmt = asdl_seq_GET(seq, i);
500 if (stmt) {
501 if (!validate_stmt(stmt))
502 return 0;
503 }
504 else {
505 PyErr_SetString(PyExc_ValueError,
506 "None disallowed in statement list");
507 return 0;
508 }
509 }
510 return 1;
511}
512
513static int
514validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
515{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100516 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500517 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
518 expr_ty expr = asdl_seq_GET(exprs, i);
519 if (expr) {
520 if (!validate_expr(expr, ctx))
521 return 0;
522 }
523 else if (!null_ok) {
524 PyErr_SetString(PyExc_ValueError,
525 "None disallowed in expression list");
526 return 0;
527 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100528
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500529 }
530 return 1;
531}
532
533int
534PyAST_Validate(mod_ty mod)
535{
536 int res = 0;
537
538 switch (mod->kind) {
539 case Module_kind:
540 res = validate_stmts(mod->v.Module.body);
541 break;
542 case Interactive_kind:
543 res = validate_stmts(mod->v.Interactive.body);
544 break;
545 case Expression_kind:
546 res = validate_expr(mod->v.Expression.body, Load);
547 break;
548 case Suite_kind:
549 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
550 break;
551 default:
552 PyErr_SetString(PyExc_SystemError, "impossible module node");
553 res = 0;
554 break;
555 }
556 return res;
557}
558
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500559/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500560#include "grammar.h"
561#include "parsetok.h"
562#include "graminit.h"
563
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564/* Data structure used internally */
565struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400566 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200567 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500568 PyObject *c_normalize; /* Normalization function from unicodedata. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000569};
570
571static asdl_seq *seq_for_testlist(struct compiling *, const node *);
572static expr_ty ast_for_expr(struct compiling *, const node *);
573static stmt_ty ast_for_stmt(struct compiling *, const node *);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300574static asdl_seq *ast_for_suite(struct compiling *c, const node *n);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000575static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
576 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000577static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000578static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579
guoci90fc8982018-09-11 17:45:45 -0400580static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool);
581static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool);
Yury Selivanov75445082015-05-11 22:57:16 -0400582
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583/* Note different signature for ast_for_call */
Serhiy Storchakab619b092018-11-27 09:40:29 +0200584static expr_ty ast_for_call(struct compiling *, const node *, expr_ty,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000585 const node *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000586
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000587static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400588static expr_ty parsestrplus(struct compiling *, const node *n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000589static void get_last_end_pos(asdl_seq *, int *, int *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590
Nick Coghlan650f0d02007-04-15 12:05:43 +0000591#define COMP_GENEXP 0
592#define COMP_LISTCOMP 1
593#define COMP_SETCOMP 2
594
Benjamin Peterson55e00432012-01-16 17:22:31 -0500595static int
596init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000597{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500598 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
599 if (!m)
600 return 0;
601 c->c_normalize = PyObject_GetAttrString(m, "normalize");
602 Py_DECREF(m);
603 if (!c->c_normalize)
604 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500605 return 1;
606}
607
608static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400609new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500610{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400611 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500612 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000613 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500614 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500615 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000616 /* Check whether there are non-ASCII characters in the
617 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500618 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200619 PyObject *id2;
Oren Milman7dc46d82017-09-30 20:16:24 +0300620 _Py_IDENTIFIER(NFKC);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500621 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500622 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200623 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500624 }
Oren Milman7dc46d82017-09-30 20:16:24 +0300625 PyObject *form = _PyUnicode_FromId(&PyId_NFKC);
626 if (form == NULL) {
627 Py_DECREF(id);
628 return NULL;
629 }
630 PyObject *args[2] = {form, id};
631 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500632 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200633 if (!id2)
634 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300635 if (!PyUnicode_Check(id2)) {
636 PyErr_Format(PyExc_TypeError,
637 "unicodedata.normalize() must return a string, not "
638 "%.200s",
639 Py_TYPE(id2)->tp_name);
640 Py_DECREF(id2);
641 return NULL;
642 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200643 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000644 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000645 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200646 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
647 Py_DECREF(id);
648 return NULL;
649 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000650 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651}
652
Benjamin Peterson55e00432012-01-16 17:22:31 -0500653#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655static int
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200656ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000657{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400658 PyObject *value, *errstr, *loc, *tmp;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200659 va_list va;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200661 va_start(va, errmsg);
662 errstr = PyUnicode_FromFormatV(errmsg, va);
663 va_end(va);
664 if (!errstr) {
665 return 0;
666 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200667 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000669 Py_INCREF(Py_None);
670 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000671 }
Ammar Askar025eb982018-09-24 17:12:49 -0400672 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200673 if (!tmp) {
674 Py_DECREF(errstr);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400675 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000676 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000677 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678 Py_DECREF(errstr);
679 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400680 if (value) {
681 PyErr_SetObject(PyExc_SyntaxError, value);
682 Py_DECREF(value);
683 }
684 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000685}
686
687/* num_stmts() returns number of contained statements.
688
689 Use this routine to determine how big a sequence is needed for
690 the statements in a parse tree. Its raison d'etre is this bit of
691 grammar:
692
693 stmt: simple_stmt | compound_stmt
694 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
695
696 A simple_stmt can contain multiple small_stmt elements joined
697 by semicolons. If the arg is a simple_stmt, the number of
698 small_stmt elements is returned.
699*/
700
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800701static string
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800702new_type_comment(const char *s, struct compiling *c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800703{
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800704 PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
Guido van Rossum4b250fc2019-02-11 08:10:42 -0800705 if (res == NULL)
706 return NULL;
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800707 if (PyArena_AddPyObject(c->c_arena, res) < 0) {
708 Py_DECREF(res);
709 return NULL;
710 }
711 return res;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800712}
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800713#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800714
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000715static int
716num_stmts(const node *n)
717{
718 int i, l;
719 node *ch;
720
721 switch (TYPE(n)) {
722 case single_input:
723 if (TYPE(CHILD(n, 0)) == NEWLINE)
724 return 0;
725 else
726 return num_stmts(CHILD(n, 0));
727 case file_input:
728 l = 0;
729 for (i = 0; i < NCH(n); i++) {
730 ch = CHILD(n, i);
731 if (TYPE(ch) == stmt)
732 l += num_stmts(ch);
733 }
734 return l;
735 case stmt:
736 return num_stmts(CHILD(n, 0));
737 case compound_stmt:
738 return 1;
739 case simple_stmt:
740 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
741 case suite:
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800742 case func_body_suite:
743 /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
744 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000745 if (NCH(n) == 1)
746 return num_stmts(CHILD(n, 0));
747 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800748 i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749 l = 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800750 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT)
751 i += 2;
752 for (; i < (NCH(n) - 1); i++)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000753 l += num_stmts(CHILD(n, i));
754 return l;
755 }
756 default: {
757 char buf[128];
758
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000759 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760 TYPE(n), NCH(n));
761 Py_FatalError(buf);
762 }
763 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700764 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765}
766
767/* Transform the CST rooted at node * to the appropriate AST
768*/
769
770mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200771PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
772 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000774 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000775 asdl_seq *stmts = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800776 asdl_seq *type_ignores = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777 stmt_ty s;
778 node *ch;
779 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500780 mod_ty res = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800781 asdl_seq *argtypes = NULL;
782 expr_ty ret, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400784 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200785 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400786 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800787 c.c_normalize = NULL;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800788
789 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791
Jeremy Hyltona8293132006-02-28 17:58:27 +0000792 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793 switch (TYPE(n)) {
794 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200795 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500797 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 for (i = 0; i < NCH(n) - 1; i++) {
799 ch = CHILD(n, i);
800 if (TYPE(ch) == NEWLINE)
801 continue;
802 REQ(ch, stmt);
803 num = num_stmts(ch);
804 if (num == 1) {
805 s = ast_for_stmt(&c, ch);
806 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500807 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000808 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000809 }
810 else {
811 ch = CHILD(ch, 0);
812 REQ(ch, simple_stmt);
813 for (j = 0; j < num; j++) {
814 s = ast_for_stmt(&c, CHILD(ch, j * 2));
815 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500816 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000817 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818 }
819 }
820 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800821
822 /* Type ignores are stored under the ENDMARKER in file_input. */
823 ch = CHILD(n, NCH(n) - 1);
824 REQ(ch, ENDMARKER);
825 num = NCH(ch);
826 type_ignores = _Py_asdl_seq_new(num, arena);
827 if (!type_ignores)
828 goto out;
829
830 for (i = 0; i < num; i++) {
831 type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), arena);
832 if (!ti)
833 goto out;
834 asdl_seq_SET(type_ignores, i, ti);
835 }
836
837 res = Module(stmts, type_ignores, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500838 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 case eval_input: {
840 expr_ty testlist_ast;
841
Nick Coghlan650f0d02007-04-15 12:05:43 +0000842 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000843 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500845 goto out;
846 res = Expression(testlist_ast, arena);
847 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 }
849 case single_input:
850 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200851 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500853 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000854 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000855 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000856 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000857 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500858 goto out;
859 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 }
861 else {
862 n = CHILD(n, 0);
863 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200864 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500866 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000868 s = ast_for_stmt(&c, n);
869 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500870 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871 asdl_seq_SET(stmts, 0, s);
872 }
873 else {
874 /* Only a simple_stmt can contain multiple statements. */
875 REQ(n, simple_stmt);
876 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877 if (TYPE(CHILD(n, i)) == NEWLINE)
878 break;
879 s = ast_for_stmt(&c, CHILD(n, i));
880 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500881 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882 asdl_seq_SET(stmts, i / 2, s);
883 }
884 }
885
Benjamin Peterson55e00432012-01-16 17:22:31 -0500886 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000887 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500888 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800889 case func_type_input:
890 n = CHILD(n, 0);
891 REQ(n, func_type);
892
893 if (TYPE(CHILD(n, 1)) == typelist) {
894 ch = CHILD(n, 1);
895 /* this is overly permissive -- we don't pay any attention to
896 * stars on the args -- just parse them into an ordered list */
897 num = 0;
898 for (i = 0; i < NCH(ch); i++) {
899 if (TYPE(CHILD(ch, i)) == test) {
900 num++;
901 }
902 }
903
904 argtypes = _Py_asdl_seq_new(num, arena);
905 if (!argtypes)
906 goto out;
907
908 j = 0;
909 for (i = 0; i < NCH(ch); i++) {
910 if (TYPE(CHILD(ch, i)) == test) {
911 arg = ast_for_expr(&c, CHILD(ch, i));
912 if (!arg)
913 goto out;
914 asdl_seq_SET(argtypes, j++, arg);
915 }
916 }
917 }
918 else {
919 argtypes = _Py_asdl_seq_new(0, arena);
920 if (!argtypes)
921 goto out;
922 }
923
924 ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1));
925 if (!ret)
926 goto out;
927 res = FunctionType(argtypes, ret, arena);
928 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000930 PyErr_Format(PyExc_SystemError,
931 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500932 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000933 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500934 out:
935 if (c.c_normalize) {
936 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500937 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500938 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939}
940
Victor Stinner14e461d2013-08-26 22:28:21 +0200941mod_ty
942PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
943 PyArena *arena)
944{
945 mod_ty mod;
946 PyObject *filename;
947 filename = PyUnicode_DecodeFSDefault(filename_str);
948 if (filename == NULL)
949 return NULL;
950 mod = PyAST_FromNodeObject(n, flags, filename, arena);
951 Py_DECREF(filename);
952 return mod;
953
954}
955
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
957*/
958
959static operator_ty
960get_operator(const node *n)
961{
962 switch (TYPE(n)) {
963 case VBAR:
964 return BitOr;
965 case CIRCUMFLEX:
966 return BitXor;
967 case AMPER:
968 return BitAnd;
969 case LEFTSHIFT:
970 return LShift;
971 case RIGHTSHIFT:
972 return RShift;
973 case PLUS:
974 return Add;
975 case MINUS:
976 return Sub;
977 case STAR:
978 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400979 case AT:
980 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981 case SLASH:
982 return Div;
983 case DOUBLESLASH:
984 return FloorDiv;
985 case PERCENT:
986 return Mod;
987 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000988 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989 }
990}
991
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200992static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000993 "None",
994 "True",
995 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200996 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +0000997 NULL,
998};
999
1000static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001001forbidden_name(struct compiling *c, identifier name, const node *n,
1002 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001003{
Benjamin Peterson78565b22009-06-28 19:19:51 +00001004 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001005 const char * const *p = FORBIDDEN;
1006 if (!full_checks) {
1007 /* In most cases, the parser will protect True, False, and None
1008 from being assign to. */
1009 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +00001010 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001011 for (; *p; p++) {
1012 if (_PyUnicode_EqualToASCIIString(name, *p)) {
1013 ast_error(c, n, "cannot assign to %U", name);
1014 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +00001015 }
1016 }
1017 return 0;
1018}
1019
Serhiy Storchakab619b092018-11-27 09:40:29 +02001020static expr_ty
1021copy_location(expr_ty e, const node *n)
1022{
1023 if (e) {
1024 e->lineno = LINENO(n);
1025 e->col_offset = n->n_col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001026 e->end_lineno = n->n_end_lineno;
1027 e->end_col_offset = n->n_end_col_offset;
Serhiy Storchakab619b092018-11-27 09:40:29 +02001028 }
1029 return e;
1030}
1031
Jeremy Hyltona8293132006-02-28 17:58:27 +00001032/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033
1034 Only sets context for expr kinds that "can appear in assignment context"
1035 (according to ../Parser/Python.asdl). For other expr kinds, it sets
1036 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037*/
1038
1039static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001040set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041{
1042 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001043 /* If a particular expression type can't be used for assign / delete,
1044 set expr_name to its name and an error message will be generated.
1045 */
1046 const char* expr_name = NULL;
1047
1048 /* The ast defines augmented store and load contexts, but the
1049 implementation here doesn't actually use them. The code may be
1050 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001051 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001052 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +00001053 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001054 */
1055 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001056
1057 switch (e->kind) {
1058 case Attribute_kind:
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001059 if (ctx == NamedStore) {
1060 expr_name = "attribute";
1061 break;
1062 }
1063
Thomas Wouters89f507f2006-12-13 04:49:30 +00001064 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001065 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001066 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001067 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068 case Subscript_kind:
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001069 if (ctx == NamedStore) {
1070 expr_name = "subscript";
1071 break;
1072 }
1073
Thomas Wouters89f507f2006-12-13 04:49:30 +00001074 e->v.Subscript.ctx = ctx;
1075 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001076 case Starred_kind:
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001077 if (ctx == NamedStore) {
1078 expr_name = "starred";
1079 break;
1080 }
1081
Guido van Rossum0368b722007-05-11 16:50:42 +00001082 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001083 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001084 return 0;
1085 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +00001087 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001088 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001089 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001090 }
1091 e->v.Name.ctx = ctx;
1092 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093 case List_kind:
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001094 if (ctx == NamedStore) {
1095 expr_name = "list";
1096 break;
1097 }
1098
Thomas Wouters89f507f2006-12-13 04:49:30 +00001099 e->v.List.ctx = ctx;
1100 s = e->v.List.elts;
1101 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102 case Tuple_kind:
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001103 if (ctx == NamedStore) {
1104 expr_name = "tuple";
1105 break;
1106 }
1107
Berker Peksag094c9c92016-05-18 08:44:29 +03001108 e->v.Tuple.ctx = ctx;
1109 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001110 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001111 case Lambda_kind:
1112 expr_name = "lambda";
1113 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001115 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001116 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001117 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001119 case UnaryOp_kind:
1120 expr_name = "operator";
1121 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001123 expr_name = "generator expression";
1124 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001125 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001126 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001127 expr_name = "yield expression";
1128 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001129 case Await_kind:
1130 expr_name = "await expression";
1131 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001132 case ListComp_kind:
1133 expr_name = "list comprehension";
1134 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001135 case SetComp_kind:
1136 expr_name = "set comprehension";
1137 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001138 case DictComp_kind:
1139 expr_name = "dict comprehension";
1140 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001141 case Dict_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001142 expr_name = "dict display";
1143 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001144 case Set_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001145 expr_name = "set display";
1146 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001147 case JoinedStr_kind:
1148 case FormattedValue_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001149 expr_name = "f-string expression";
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001150 break;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001151 case Constant_kind: {
1152 PyObject *value = e->v.Constant.value;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001153 if (value == Py_None || value == Py_False || value == Py_True
1154 || value == Py_Ellipsis)
1155 {
1156 return ast_error(c, n, "cannot %s %R",
1157 ctx == Store ? "assign to" : "delete",
1158 value);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001159 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001160 expr_name = "literal";
Benjamin Peterson442f2092012-12-06 17:41:04 -05001161 break;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001162 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001163 case Compare_kind:
1164 expr_name = "comparison";
1165 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001166 case IfExp_kind:
1167 expr_name = "conditional expression";
1168 break;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001169 case NamedExpr_kind:
1170 expr_name = "named expression";
1171 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001172 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 PyErr_Format(PyExc_SystemError,
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001174 "unexpected expression in %sassignment %d (line %d)",
1175 ctx == NamedStore ? "named ": "",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001176 e->kind, e->lineno);
1177 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001179 /* Check for error string set by switch */
1180 if (expr_name) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001181 if (ctx == NamedStore) {
1182 return ast_error(c, n, "cannot use named assignment with %s",
1183 expr_name);
1184 }
1185 else {
1186 return ast_error(c, n, "cannot %s %s",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001187 ctx == Store ? "assign to" : "delete",
1188 expr_name);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001189 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001190 }
1191
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194 */
1195 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001196 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197
Thomas Wouters89f507f2006-12-13 04:49:30 +00001198 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001199 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001200 return 0;
1201 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202 }
1203 return 1;
1204}
1205
1206static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001207ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208{
1209 REQ(n, augassign);
1210 n = CHILD(n, 0);
1211 switch (STR(n)[0]) {
1212 case '+':
1213 return Add;
1214 case '-':
1215 return Sub;
1216 case '/':
1217 if (STR(n)[1] == '/')
1218 return FloorDiv;
1219 else
1220 return Div;
1221 case '%':
1222 return Mod;
1223 case '<':
1224 return LShift;
1225 case '>':
1226 return RShift;
1227 case '&':
1228 return BitAnd;
1229 case '^':
1230 return BitXor;
1231 case '|':
1232 return BitOr;
1233 case '*':
1234 if (STR(n)[1] == '*')
1235 return Pow;
1236 else
1237 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001238 case '@':
1239 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001241 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001242 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243 }
1244}
1245
1246static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001247ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001249 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001250 |'is' 'not'
1251 */
1252 REQ(n, comp_op);
1253 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001254 n = CHILD(n, 0);
1255 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001256 case LESS:
1257 return Lt;
1258 case GREATER:
1259 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001260 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001261 return Eq;
1262 case LESSEQUAL:
1263 return LtE;
1264 case GREATEREQUAL:
1265 return GtE;
1266 case NOTEQUAL:
1267 return NotEq;
1268 case NAME:
1269 if (strcmp(STR(n), "in") == 0)
1270 return In;
1271 if (strcmp(STR(n), "is") == 0)
1272 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001273 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001275 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001277 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001278 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001279 }
1280 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001281 /* handle "not in" and "is not" */
1282 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283 case NAME:
1284 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1285 return NotIn;
1286 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1287 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001288 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001289 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001290 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001292 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001293 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001294 }
Neal Norwitz79792652005-11-14 04:25:03 +00001295 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001296 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001297 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001298}
1299
1300static asdl_seq *
1301seq_for_testlist(struct compiling *c, const node *n)
1302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001304 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1305 */
Armin Rigo31441302005-10-21 12:57:31 +00001306 asdl_seq *seq;
1307 expr_ty expression;
1308 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001309 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001311 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312 if (!seq)
1313 return NULL;
1314
1315 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 const node *ch = CHILD(n, i);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001317 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318
Benjamin Peterson4905e802009-09-27 02:43:28 +00001319 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001320 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001321 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001322
1323 assert(i / 2 < seq->size);
1324 asdl_seq_SET(seq, i / 2, expression);
1325 }
1326 return seq;
1327}
1328
Neal Norwitzc1505362006-12-28 06:47:50 +00001329static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001330ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001331{
1332 identifier name;
1333 expr_ty annotation = NULL;
1334 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001335 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001336
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001337 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001338 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001339 name = NEW_IDENTIFIER(ch);
1340 if (!name)
1341 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001342 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001343 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001344
1345 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1346 annotation = ast_for_expr(c, CHILD(n, 2));
1347 if (!annotation)
1348 return NULL;
1349 }
1350
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001351 ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001352 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001353 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001354 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001355 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356}
1357
Guido van Rossum4f72a782006-10-27 23:31:49 +00001358/* returns -1 if failed to handle keyword only arguments
1359 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001360 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001361 ^^^
1362 start pointing here
1363 */
1364static int
1365handle_keywordonly_args(struct compiling *c, const node *n, int start,
1366 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1367{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001368 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001369 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001370 expr_ty expression, annotation;
Pablo Galindo164686f2019-02-10 20:37:07 +00001371 arg_ty arg = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001372 int i = start;
1373 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001374
1375 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001376 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001377 return -1;
1378 }
1379 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001380 while (i < NCH(n)) {
1381 ch = CHILD(n, i);
1382 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001383 case vfpdef:
1384 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001385 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001386 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001387 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001388 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001389 asdl_seq_SET(kwdefaults, j, expression);
1390 i += 2; /* '=' and test */
1391 }
1392 else { /* setting NULL if no default value exists */
1393 asdl_seq_SET(kwdefaults, j, NULL);
1394 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001395 if (NCH(ch) == 3) {
1396 /* ch is NAME ':' test */
1397 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001398 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001399 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001400 }
1401 else {
1402 annotation = NULL;
1403 }
1404 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001405 argname = NEW_IDENTIFIER(ch);
1406 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001407 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001408 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001409 goto error;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001410 arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001411 ch->n_end_lineno, ch->n_end_col_offset,
Victor Stinnerc106c682015-11-06 17:01:48 +01001412 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001413 if (!arg)
1414 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001415 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001416 i += 1; /* the name */
1417 if (TYPE(CHILD(n, i)) == COMMA)
1418 i += 1; /* the comma, if present */
1419 break;
1420 case TYPE_COMMENT:
1421 /* arg will be equal to the last argument processed */
1422 arg->type_comment = NEW_TYPE_COMMENT(ch);
1423 if (!arg->type_comment)
1424 goto error;
1425 i += 1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001426 break;
1427 case DOUBLESTAR:
1428 return i;
1429 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001430 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001431 goto error;
1432 }
1433 }
1434 return i;
1435 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001437}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438
Jeremy Hyltona8293132006-02-28 17:58:27 +00001439/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440
1441static arguments_ty
1442ast_for_arguments(struct compiling *c, const node *n)
1443{
Neal Norwitzc1505362006-12-28 06:47:50 +00001444 /* This function handles both typedargslist (function definition)
1445 and varargslist (lambda definition).
1446
1447 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001448 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1449 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1450 | '**' tfpdef [',']]]
1451 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1452 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001453 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001454 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1455 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1456 | '**' vfpdef [',']]]
1457 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1458 | '**' vfpdef [',']
1459 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001460 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001461
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001463 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1464 int nposdefaults = 0, found_default = 0;
1465 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001466 arg_ty vararg = NULL, kwarg = NULL;
Pablo Galindo164686f2019-02-10 20:37:07 +00001467 arg_ty arg = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001468 node *ch;
1469
1470 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001471 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001472 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001473 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001475 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476
Jeremy Hyltone921e022008-07-17 16:37:17 +00001477 /* First count the number of positional args & defaults. The
1478 variable i is the loop index for this for loop and the next.
1479 The next loop picks up where the first leaves off.
1480 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001482 ch = CHILD(n, i);
1483 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001484 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001485 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001486 if (i < NCH(n) && /* skip argument following star */
1487 (TYPE(CHILD(n, i)) == tfpdef ||
1488 TYPE(CHILD(n, i)) == vfpdef)) {
1489 i++;
1490 }
1491 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001492 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001493 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001494 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001495 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001498 defaults for keyword only args */
1499 for ( ; i < NCH(n); ++i) {
1500 ch = CHILD(n, i);
1501 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001502 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001503 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001504 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001505 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001506 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001507 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001508 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001509 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001510 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001512 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001513 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001514 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001516 since we set NULL as default for keyword only argument w/o default
1517 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001518 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001519 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001520 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001521 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001522
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001523 /* tfpdef: NAME [':' test]
1524 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525 */
1526 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001527 j = 0; /* index for defaults */
1528 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001530 ch = CHILD(n, i);
1531 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001532 case tfpdef:
1533 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1535 anything other than EQUAL or a comma? */
1536 /* XXX Should NCH(n) check be made a separate check? */
1537 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001538 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1539 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001540 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001541 assert(posdefaults != NULL);
1542 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001544 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001546 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001547 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001548 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001549 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001550 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001551 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001552 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001553 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001554 asdl_seq_SET(posargs, k++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001555 i += 1; /* the name */
1556 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1557 i += 1; /* the comma, if present */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001558 break;
1559 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001560 if (i+1 >= NCH(n) ||
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001561 (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA
1562 || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001563 ast_error(c, CHILD(n, i),
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001564 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001565 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001566 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001567 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001568 if (TYPE(ch) == COMMA) {
1569 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001570 i += 2; /* now follows keyword only arguments */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001571
1572 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1573 ast_error(c, CHILD(n, i),
1574 "bare * has associated type comment");
1575 return NULL;
1576 }
1577
Guido van Rossum4f72a782006-10-27 23:31:49 +00001578 res = handle_keywordonly_args(c, n, i,
1579 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001580 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001581 i = res; /* res has new position to process */
1582 }
1583 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001584 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001585 if (!vararg)
1586 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001587
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001588 i += 2; /* the star and the name */
1589 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1590 i += 1; /* the comma, if present */
1591
1592 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1593 vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i));
1594 if (!vararg->type_comment)
1595 return NULL;
1596 i += 1;
1597 }
1598
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001599 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1600 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001601 int res = 0;
1602 res = handle_keywordonly_args(c, n, i,
1603 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001604 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001605 i = res; /* res has new position to process */
1606 }
1607 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001608 break;
1609 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001610 ch = CHILD(n, i+1); /* tfpdef */
1611 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001612 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001613 if (!kwarg)
1614 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001615 i += 2; /* the double star and the name */
1616 if (TYPE(CHILD(n, i)) == COMMA)
1617 i += 1; /* the comma, if present */
1618 break;
1619 case TYPE_COMMENT:
1620 assert(i);
1621
1622 if (kwarg)
1623 arg = kwarg;
1624
1625 /* arg will be equal to the last argument processed */
1626 arg->type_comment = NEW_TYPE_COMMENT(ch);
1627 if (!arg->type_comment)
1628 return NULL;
1629 i += 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001630 break;
1631 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001632 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001633 "unexpected node in varargslist: %d @ %d",
1634 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001635 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001636 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001637 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001638 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001639}
1640
1641static expr_ty
1642ast_for_dotted_name(struct compiling *c, const node *n)
1643{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001644 expr_ty e;
1645 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001646 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001647 int i;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001648 node *ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001649
1650 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001651
1652 lineno = LINENO(n);
1653 col_offset = n->n_col_offset;
1654
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001655 ch = CHILD(n, 0);
1656 id = NEW_IDENTIFIER(ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001657 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001658 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001659 e = Name(id, Load, lineno, col_offset,
1660 ch->n_end_lineno, ch->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001662 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001663
1664 for (i = 2; i < NCH(n); i+=2) {
1665 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001666 if (!id)
1667 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001668 e = Attribute(e, id, Load, lineno, col_offset,
1669 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001670 if (!e)
1671 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001672 }
1673
1674 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001675}
1676
1677static expr_ty
1678ast_for_decorator(struct compiling *c, const node *n)
1679{
1680 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1681 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001682 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001685 REQ(CHILD(n, 0), AT);
1686 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1689 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001690 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001693 d = name_expr;
1694 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695 }
1696 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001697 d = Call(name_expr, NULL, NULL, LINENO(n),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001698 n->n_col_offset, n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001699 if (!d)
1700 return NULL;
1701 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702 }
1703 else {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001704 d = ast_for_call(c, CHILD(n, 3), name_expr, CHILD(n, 2), CHILD(n, 4));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001705 if (!d)
1706 return NULL;
1707 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708 }
1709
1710 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001711}
1712
1713static asdl_seq*
1714ast_for_decorators(struct compiling *c, const node *n)
1715{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001716 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001717 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001721 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722 if (!decorator_seq)
1723 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001726 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001727 if (!d)
1728 return NULL;
1729 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730 }
1731 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732}
1733
1734static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001735ast_for_funcdef_impl(struct compiling *c, const node *n0,
1736 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001737{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001738 /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */
guoci90fc8982018-09-11 17:45:45 -04001739 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001740 identifier name;
1741 arguments_ty args;
1742 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001743 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001744 int name_i = 1;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001745 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001746 node *tc;
1747 string type_comment = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748
1749 REQ(n, funcdef);
1750
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751 name = NEW_IDENTIFIER(CHILD(n, name_i));
1752 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001753 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001754 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001755 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001756 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1757 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001758 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001759 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1760 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1761 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001762 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001763 name_i += 2;
1764 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001765 if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) {
1766 type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3));
1767 if (!type_comment)
1768 return NULL;
1769 name_i += 1;
1770 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001771 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001772 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001773 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001774 get_last_end_pos(body, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001776 if (NCH(CHILD(n, name_i + 3)) > 1) {
1777 /* Check if the suite has a type comment in it. */
1778 tc = CHILD(CHILD(n, name_i + 3), 1);
1779
1780 if (TYPE(tc) == TYPE_COMMENT) {
1781 if (type_comment != NULL) {
1782 ast_error(c, n, "Cannot have two type comments on def");
1783 return NULL;
1784 }
1785 type_comment = NEW_TYPE_COMMENT(tc);
1786 if (!type_comment)
1787 return NULL;
1788 }
1789 }
1790
Yury Selivanov75445082015-05-11 22:57:16 -04001791 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001792 return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001793 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001794 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001795 return FunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001796 LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001797}
1798
1799static stmt_ty
1800ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1801{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001802 /* async_funcdef: 'async' funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001803 REQ(n, async_funcdef);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001804 REQ(CHILD(n, 0), NAME);
1805 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001806 REQ(CHILD(n, 1), funcdef);
1807
guoci90fc8982018-09-11 17:45:45 -04001808 return ast_for_funcdef_impl(c, n, decorator_seq,
1809 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001810}
1811
1812static stmt_ty
1813ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1814{
1815 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1816 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001817 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001818}
1819
1820
1821static stmt_ty
1822ast_for_async_stmt(struct compiling *c, const node *n)
1823{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001824 /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001825 REQ(n, async_stmt);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001826 REQ(CHILD(n, 0), NAME);
1827 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001828
1829 switch (TYPE(CHILD(n, 1))) {
1830 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001831 return ast_for_funcdef_impl(c, n, NULL,
1832 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001833 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001834 return ast_for_with_stmt(c, n,
1835 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001836
1837 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001838 return ast_for_for_stmt(c, n,
1839 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001840
1841 default:
1842 PyErr_Format(PyExc_SystemError,
1843 "invalid async stament: %s",
1844 STR(CHILD(n, 1)));
1845 return NULL;
1846 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847}
1848
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001849static stmt_ty
1850ast_for_decorated(struct compiling *c, const node *n)
1851{
Yury Selivanov75445082015-05-11 22:57:16 -04001852 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001853 stmt_ty thing = NULL;
1854 asdl_seq *decorator_seq = NULL;
1855
1856 REQ(n, decorated);
1857
1858 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1859 if (!decorator_seq)
1860 return NULL;
1861
1862 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001863 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001865
1866 if (TYPE(CHILD(n, 1)) == funcdef) {
1867 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1868 } else if (TYPE(CHILD(n, 1)) == classdef) {
1869 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001870 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1871 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001872 }
1873 return thing;
1874}
1875
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001876static expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001877ast_for_namedexpr(struct compiling *c, const node *n)
1878{
1879 /* if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)*
1880 ['else' ':' suite]
1881 namedexpr_test: test [':=' test]
1882 argument: ( test [comp_for] |
1883 test ':=' test |
1884 test '=' test |
1885 '**' test |
1886 '*' test )
1887 */
1888 expr_ty target, value;
1889
1890 target = ast_for_expr(c, CHILD(n, 0));
1891 if (!target)
1892 return NULL;
1893
1894 value = ast_for_expr(c, CHILD(n, 2));
1895 if (!value)
1896 return NULL;
1897
1898 if (!set_context(c, target, NamedStore, n))
1899 return NULL;
1900
1901 return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno,
1902 n->n_end_col_offset, c->c_arena);
1903}
1904
1905static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001906ast_for_lambdef(struct compiling *c, const node *n)
1907{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001908 /* lambdef: 'lambda' [varargslist] ':' test
1909 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001910 arguments_ty args;
1911 expr_ty expression;
1912
1913 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001914 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001915 if (!args)
1916 return NULL;
1917 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001918 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001919 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001920 }
1921 else {
1922 args = ast_for_arguments(c, CHILD(n, 1));
1923 if (!args)
1924 return NULL;
1925 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001926 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001927 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001928 }
1929
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001930 return Lambda(args, expression, LINENO(n), n->n_col_offset,
1931 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001932}
1933
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001934static expr_ty
1935ast_for_ifexpr(struct compiling *c, const node *n)
1936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001938 expr_ty expression, body, orelse;
1939
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001940 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001941 body = ast_for_expr(c, CHILD(n, 0));
1942 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001943 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001944 expression = ast_for_expr(c, CHILD(n, 2));
1945 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001946 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001947 orelse = ast_for_expr(c, CHILD(n, 4));
1948 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001949 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001950 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001951 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001952 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001953}
1954
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001955/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001956 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001957
Nick Coghlan650f0d02007-04-15 12:05:43 +00001958 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001959*/
1960
1961static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001962count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001963{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001964 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001965
Guido van Rossumd8faa362007-04-27 19:54:29 +00001966 count_comp_for:
1967 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001968 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001969 if (NCH(n) == 2) {
1970 REQ(CHILD(n, 0), NAME);
1971 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1972 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001973 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001974 else if (NCH(n) == 1) {
1975 n = CHILD(n, 0);
1976 }
1977 else {
1978 goto error;
1979 }
1980 if (NCH(n) == (5)) {
1981 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001982 }
1983 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001984 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001985 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001986 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001987 REQ(n, comp_iter);
1988 n = CHILD(n, 0);
1989 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001990 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001991 else if (TYPE(n) == comp_if) {
1992 if (NCH(n) == 3) {
1993 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001994 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001995 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001996 else
1997 return n_fors;
1998 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001999
Jelle Zijlstraac317702017-10-05 20:24:46 -07002000 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00002001 /* Should never be reached */
2002 PyErr_SetString(PyExc_SystemError,
2003 "logic error in count_comp_fors");
2004 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005}
2006
Nick Coghlan650f0d02007-04-15 12:05:43 +00002007/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008
Nick Coghlan650f0d02007-04-15 12:05:43 +00002009 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010*/
2011
2012static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002013count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002015 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002016
Guido van Rossumd8faa362007-04-27 19:54:29 +00002017 while (1) {
2018 REQ(n, comp_iter);
2019 if (TYPE(CHILD(n, 0)) == comp_for)
2020 return n_ifs;
2021 n = CHILD(n, 0);
2022 REQ(n, comp_if);
2023 n_ifs++;
2024 if (NCH(n) == 2)
2025 return n_ifs;
2026 n = CHILD(n, 2);
2027 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002028}
2029
Guido van Rossum992d4a32007-07-11 13:09:30 +00002030static asdl_seq *
2031ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002032{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002033 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002034 asdl_seq *comps;
2035
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002036 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002037 if (n_fors == -1)
2038 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002039
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002040 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002041 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002042 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002043
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002044 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002045 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002046 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002047 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002048 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002049 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002050 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051
Guido van Rossum992d4a32007-07-11 13:09:30 +00002052 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053
Jelle Zijlstraac317702017-10-05 20:24:46 -07002054 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002055 is_async = 1;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002056 REQ(CHILD(n, 0), NAME);
2057 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
2058 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002059 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002060 else {
2061 sync_n = CHILD(n, 0);
2062 }
2063 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002064
Jelle Zijlstraac317702017-10-05 20:24:46 -07002065 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002066 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002067 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002069 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002070 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002071 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002072
Thomas Wouters89f507f2006-12-13 04:49:30 +00002073 /* Check the # of children rather than the length of t, since
2074 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002075 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002076 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002077 comp = comprehension(first, expression, NULL,
2078 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002079 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002080 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
2081 for_ch->n_end_lineno, for_ch->n_end_col_offset,
2082 c->c_arena),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002083 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002084 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002086
Jelle Zijlstraac317702017-10-05 20:24:46 -07002087 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002088 int j, n_ifs;
2089 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090
Jelle Zijlstraac317702017-10-05 20:24:46 -07002091 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002092 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002093 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002094 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002095
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002096 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002097 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002099
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002100 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002101 REQ(n, comp_iter);
2102 n = CHILD(n, 0);
2103 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104
Guido van Rossum992d4a32007-07-11 13:09:30 +00002105 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002106 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002107 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002108 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002109 if (NCH(n) == 3)
2110 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002111 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002112 /* on exit, must guarantee that n is a comp_for */
2113 if (TYPE(n) == comp_iter)
2114 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002115 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002116 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002117 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002119 return comps;
2120}
2121
2122static expr_ty
2123ast_for_itercomp(struct compiling *c, const node *n, int type)
2124{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002125 /* testlist_comp: (test|star_expr)
2126 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002127 expr_ty elt;
2128 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002129 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130
Guido van Rossum992d4a32007-07-11 13:09:30 +00002131 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002133 ch = CHILD(n, 0);
2134 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002135 if (!elt)
2136 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002137 if (elt->kind == Starred_kind) {
2138 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
2139 return NULL;
2140 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141
Guido van Rossum992d4a32007-07-11 13:09:30 +00002142 comps = ast_for_comprehension(c, CHILD(n, 1));
2143 if (!comps)
2144 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002145
2146 if (type == COMP_GENEXP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002147 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset,
2148 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002149 else if (type == COMP_LISTCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002150 return ListComp(elt, comps, LINENO(n), n->n_col_offset,
2151 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002152 else if (type == COMP_SETCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002153 return SetComp(elt, comps, LINENO(n), n->n_col_offset,
2154 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002155 else
2156 /* Should never happen */
2157 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158}
2159
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002160/* Fills in the key, value pair corresponding to the dict element. In case
2161 * of an unpacking, key is NULL. *i is advanced by the number of ast
2162 * elements. Iff successful, nonzero is returned.
2163 */
2164static int
2165ast_for_dictelement(struct compiling *c, const node *n, int *i,
2166 expr_ty *key, expr_ty *value)
2167{
2168 expr_ty expression;
2169 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
2170 assert(NCH(n) - *i >= 2);
2171
2172 expression = ast_for_expr(c, CHILD(n, *i + 1));
2173 if (!expression)
2174 return 0;
2175 *key = NULL;
2176 *value = expression;
2177
2178 *i += 2;
2179 }
2180 else {
2181 assert(NCH(n) - *i >= 3);
2182
2183 expression = ast_for_expr(c, CHILD(n, *i));
2184 if (!expression)
2185 return 0;
2186 *key = expression;
2187
2188 REQ(CHILD(n, *i + 1), COLON);
2189
2190 expression = ast_for_expr(c, CHILD(n, *i + 2));
2191 if (!expression)
2192 return 0;
2193 *value = expression;
2194
2195 *i += 3;
2196 }
2197 return 1;
2198}
2199
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002200static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002201ast_for_dictcomp(struct compiling *c, const node *n)
2202{
2203 expr_ty key, value;
2204 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002205 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002207 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002208 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002209 assert(key);
2210 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002212 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002213 if (!comps)
2214 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002216 return DictComp(key, value, comps, LINENO(n), n->n_col_offset,
2217 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002218}
2219
2220static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002221ast_for_dictdisplay(struct compiling *c, const node *n)
2222{
2223 int i;
2224 int j;
2225 int size;
2226 asdl_seq *keys, *values;
2227
2228 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2229 keys = _Py_asdl_seq_new(size, c->c_arena);
2230 if (!keys)
2231 return NULL;
2232
2233 values = _Py_asdl_seq_new(size, c->c_arena);
2234 if (!values)
2235 return NULL;
2236
2237 j = 0;
2238 for (i = 0; i < NCH(n); i++) {
2239 expr_ty key, value;
2240
2241 if (!ast_for_dictelement(c, n, &i, &key, &value))
2242 return NULL;
2243 asdl_seq_SET(keys, j, key);
2244 asdl_seq_SET(values, j, value);
2245
2246 j++;
2247 }
2248 keys->size = j;
2249 values->size = j;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002250 return Dict(keys, values, LINENO(n), n->n_col_offset,
2251 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002252}
2253
2254static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002255ast_for_genexp(struct compiling *c, const node *n)
2256{
2257 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002258 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002259}
2260
2261static expr_ty
2262ast_for_listcomp(struct compiling *c, const node *n)
2263{
2264 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002265 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002266}
2267
2268static expr_ty
2269ast_for_setcomp(struct compiling *c, const node *n)
2270{
2271 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002272 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002273}
2274
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002275static expr_ty
2276ast_for_setdisplay(struct compiling *c, const node *n)
2277{
2278 int i;
2279 int size;
2280 asdl_seq *elts;
2281
2282 assert(TYPE(n) == (dictorsetmaker));
2283 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2284 elts = _Py_asdl_seq_new(size, c->c_arena);
2285 if (!elts)
2286 return NULL;
2287 for (i = 0; i < NCH(n); i += 2) {
2288 expr_ty expression;
2289 expression = ast_for_expr(c, CHILD(n, i));
2290 if (!expression)
2291 return NULL;
2292 asdl_seq_SET(elts, i / 2, expression);
2293 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002294 return Set(elts, LINENO(n), n->n_col_offset,
2295 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002296}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002297
2298static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002299ast_for_atom(struct compiling *c, const node *n)
2300{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002301 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2302 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002303 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304 */
2305 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002307 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002308 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002309 PyObject *name;
2310 const char *s = STR(ch);
2311 size_t len = strlen(s);
2312 if (len >= 4 && len <= 5) {
2313 if (!strcmp(s, "None"))
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002314 return Constant(Py_None, LINENO(n), n->n_col_offset,
2315 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002316 if (!strcmp(s, "True"))
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002317 return Constant(Py_True, LINENO(n), n->n_col_offset,
2318 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002319 if (!strcmp(s, "False"))
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002320 return Constant(Py_False, LINENO(n), n->n_col_offset,
2321 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002322 }
2323 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002324 if (!name)
2325 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002326 /* All names start in Load context, but may later be changed. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002327 return Name(name, Load, LINENO(n), n->n_col_offset,
2328 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson30760062008-11-25 04:02:28 +00002329 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002330 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002331 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002332 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002333 const char *errtype = NULL;
2334 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2335 errtype = "unicode error";
2336 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2337 errtype = "value error";
2338 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002339 PyObject *type, *value, *tback, *errstr;
2340 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002341 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002342 if (errstr) {
2343 ast_error(c, n, "(%s) %U", errtype, errstr);
2344 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002345 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002346 else {
2347 PyErr_Clear();
2348 ast_error(c, n, "(%s) unknown error", errtype);
2349 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002350 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002351 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002352 Py_XDECREF(tback);
2353 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002354 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002355 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002356 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002357 }
2358 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002359 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002360 if (!pynum)
2361 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002362
Victor Stinner43d81952013-07-17 00:57:58 +02002363 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2364 Py_DECREF(pynum);
2365 return NULL;
2366 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002367 return Constant(pynum, LINENO(n), n->n_col_offset,
2368 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002369 }
Georg Brandldde00282007-03-18 19:01:53 +00002370 case ELLIPSIS: /* Ellipsis */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002371 return Constant(Py_Ellipsis, LINENO(n), n->n_col_offset,
2372 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002373 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002374 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375
Thomas Wouters89f507f2006-12-13 04:49:30 +00002376 if (TYPE(ch) == RPAR)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002377 return Tuple(NULL, Load, LINENO(n), n->n_col_offset,
2378 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379
Thomas Wouters89f507f2006-12-13 04:49:30 +00002380 if (TYPE(ch) == yield_expr)
2381 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002384 if (NCH(ch) == 1) {
2385 return ast_for_testlist(c, ch);
2386 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002387
Serhiy Storchakab619b092018-11-27 09:40:29 +02002388 if (TYPE(CHILD(ch, 1)) == comp_for) {
2389 return copy_location(ast_for_genexp(c, ch), n);
2390 }
2391 else {
2392 return copy_location(ast_for_testlist(c, ch), n);
2393 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002394 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002395 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396
Thomas Wouters89f507f2006-12-13 04:49:30 +00002397 if (TYPE(ch) == RSQB)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002398 return List(NULL, Load, LINENO(n), n->n_col_offset,
2399 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400
Nick Coghlan650f0d02007-04-15 12:05:43 +00002401 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002402 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2403 asdl_seq *elts = seq_for_testlist(c, ch);
2404 if (!elts)
2405 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002406
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002407 return List(elts, Load, LINENO(n), n->n_col_offset,
2408 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002409 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002410 else {
2411 return copy_location(ast_for_listcomp(c, ch), n);
2412 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002413 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002414 /* dictorsetmaker: ( ((test ':' test | '**' test)
2415 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2416 * ((test | '*' test)
2417 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002418 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002419 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002420 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002421 /* It's an empty dict. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002422 return Dict(NULL, NULL, LINENO(n), n->n_col_offset,
2423 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002424 }
2425 else {
2426 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2427 if (NCH(ch) == 1 ||
2428 (NCH(ch) > 1 &&
2429 TYPE(CHILD(ch, 1)) == COMMA)) {
2430 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002431 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002432 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002433 else if (NCH(ch) > 1 &&
2434 TYPE(CHILD(ch, 1)) == comp_for) {
2435 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002436 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002437 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002438 else if (NCH(ch) > 3 - is_dict &&
2439 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2440 /* It's a dictionary comprehension. */
2441 if (is_dict) {
2442 ast_error(c, n, "dict unpacking cannot be used in "
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002443 "dict comprehension");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002444 return NULL;
2445 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002446 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002447 }
2448 else {
2449 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002450 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002451 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002452 return copy_location(res, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002453 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002454 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002455 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002456 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2457 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002458 }
2459}
2460
2461static slice_ty
2462ast_for_slice(struct compiling *c, const node *n)
2463{
2464 node *ch;
2465 expr_ty lower = NULL, upper = NULL, step = NULL;
2466
2467 REQ(n, subscript);
2468
2469 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002470 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471 sliceop: ':' [test]
2472 */
2473 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002474 if (NCH(n) == 1 && TYPE(ch) == test) {
2475 /* 'step' variable hold no significance in terms of being used over
2476 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002478 if (!step)
2479 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480
Thomas Wouters89f507f2006-12-13 04:49:30 +00002481 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002482 }
2483
2484 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002485 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002486 if (!lower)
2487 return NULL;
2488 }
2489
2490 /* If there's an upper bound it's in the second or third position. */
2491 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002492 if (NCH(n) > 1) {
2493 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002494
Thomas Wouters89f507f2006-12-13 04:49:30 +00002495 if (TYPE(n2) == test) {
2496 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002497 if (!upper)
2498 return NULL;
2499 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002500 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002501 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002502 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002503
Thomas Wouters89f507f2006-12-13 04:49:30 +00002504 if (TYPE(n2) == test) {
2505 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002506 if (!upper)
2507 return NULL;
2508 }
2509 }
2510
2511 ch = CHILD(n, NCH(n) - 1);
2512 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002513 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002514 ch = CHILD(ch, 1);
2515 if (TYPE(ch) == test) {
2516 step = ast_for_expr(c, ch);
2517 if (!step)
2518 return NULL;
2519 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002520 }
2521 }
2522
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002523 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002524}
2525
2526static expr_ty
2527ast_for_binop(struct compiling *c, const node *n)
2528{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002529 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002530 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002531 BinOp(BinOp(A, op, B), op, C).
2532 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533
Guido van Rossumd8faa362007-04-27 19:54:29 +00002534 int i, nops;
2535 expr_ty expr1, expr2, result;
2536 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537
Guido van Rossumd8faa362007-04-27 19:54:29 +00002538 expr1 = ast_for_expr(c, CHILD(n, 0));
2539 if (!expr1)
2540 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002541
Guido van Rossumd8faa362007-04-27 19:54:29 +00002542 expr2 = ast_for_expr(c, CHILD(n, 2));
2543 if (!expr2)
2544 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002545
Guido van Rossumd8faa362007-04-27 19:54:29 +00002546 newoperator = get_operator(CHILD(n, 1));
2547 if (!newoperator)
2548 return NULL;
2549
2550 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002551 CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002552 c->c_arena);
2553 if (!result)
2554 return NULL;
2555
2556 nops = (NCH(n) - 1) / 2;
2557 for (i = 1; i < nops; i++) {
2558 expr_ty tmp_result, tmp;
2559 const node* next_oper = CHILD(n, i * 2 + 1);
2560
2561 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002562 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002563 return NULL;
2564
Guido van Rossumd8faa362007-04-27 19:54:29 +00002565 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2566 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002567 return NULL;
2568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002570 LINENO(next_oper), next_oper->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002571 CHILD(n, i * 2 + 2)->n_end_lineno,
2572 CHILD(n, i * 2 + 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002573 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002575 return NULL;
2576 result = tmp_result;
2577 }
2578 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002579}
2580
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002581static expr_ty
2582ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002585 subscriptlist: subscript (',' subscript)* [',']
2586 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2587 */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002588 const node *n_copy = n;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002589 REQ(n, trailer);
2590 if (TYPE(CHILD(n, 0)) == LPAR) {
2591 if (NCH(n) == 2)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002592 return Call(left_expr, NULL, NULL, LINENO(n), n->n_col_offset,
2593 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002594 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002595 return ast_for_call(c, CHILD(n, 1), left_expr, CHILD(n, 0), CHILD(n, 2));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002596 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002597 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002598 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2599 if (!attr_id)
2600 return NULL;
2601 return Attribute(left_expr, attr_id, Load,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002602 LINENO(n), n->n_col_offset,
2603 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002604 }
2605 else {
2606 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002607 REQ(CHILD(n, 2), RSQB);
2608 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002609 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002610 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2611 if (!slc)
2612 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002613 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002614 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002615 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002616 }
2617 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002618 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002619 by treating the sequence as a tuple literal if there are
2620 no slice features.
2621 */
Victor Stinner4d73ae72018-11-22 14:45:16 +01002622 Py_ssize_t j;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002623 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002624 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002625 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002626 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002627 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002628 if (!slices)
2629 return NULL;
2630 for (j = 0; j < NCH(n); j += 2) {
2631 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002632 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002633 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002634 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002635 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002636 asdl_seq_SET(slices, j / 2, slc);
2637 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002638 if (!simple) {
2639 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002640 Load, LINENO(n), n->n_col_offset,
2641 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002642 }
2643 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002644 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002645 if (!elts)
2646 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002647 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2648 slc = (slice_ty)asdl_seq_GET(slices, j);
2649 assert(slc->kind == Index_kind && slc->v.Index.value);
2650 asdl_seq_SET(elts, j, slc->v.Index.value);
2651 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002652 e = Tuple(elts, Load, LINENO(n), n->n_col_offset,
2653 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002654 if (!e)
2655 return NULL;
2656 return Subscript(left_expr, Index(e, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002657 Load, LINENO(n), n->n_col_offset,
2658 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002659 }
2660 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002661}
2662
2663static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002664ast_for_factor(struct compiling *c, const node *n)
2665{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002666 expr_ty expression;
2667
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002668 expression = ast_for_expr(c, CHILD(n, 1));
2669 if (!expression)
2670 return NULL;
2671
2672 switch (TYPE(CHILD(n, 0))) {
2673 case PLUS:
2674 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002675 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002676 c->c_arena);
2677 case MINUS:
2678 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002679 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002680 c->c_arena);
2681 case TILDE:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002682 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset,
2683 n->n_end_lineno, n->n_end_col_offset,
2684 c->c_arena);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002685 }
2686 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2687 TYPE(CHILD(n, 0)));
2688 return NULL;
2689}
2690
2691static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002692ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002693{
Yury Selivanov75445082015-05-11 22:57:16 -04002694 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002695 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002696
2697 REQ(n, atom_expr);
2698 nch = NCH(n);
2699
Jelle Zijlstraac317702017-10-05 20:24:46 -07002700 if (TYPE(CHILD(n, 0)) == NAME && strcmp(STR(CHILD(n, 0)), "await") == 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04002701 start = 1;
2702 assert(nch > 1);
2703 }
2704
2705 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002706 if (!e)
2707 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002708 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002709 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002710 if (start && nch == 2) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002711 return Await(e, LINENO(n), n->n_col_offset,
2712 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002713 }
2714
2715 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002716 node *ch = CHILD(n, i);
2717 if (TYPE(ch) != trailer)
2718 break;
2719 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002720 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002721 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002722 tmp->lineno = e->lineno;
2723 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002724 e = tmp;
2725 }
Yury Selivanov75445082015-05-11 22:57:16 -04002726
2727 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002728 /* there was an 'await' */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002729 return Await(e, LINENO(n), n->n_col_offset,
2730 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002731 }
2732 else {
2733 return e;
2734 }
2735}
2736
2737static expr_ty
2738ast_for_power(struct compiling *c, const node *n)
2739{
2740 /* power: atom trailer* ('**' factor)*
2741 */
2742 expr_ty e;
2743 REQ(n, power);
2744 e = ast_for_atom_expr(c, CHILD(n, 0));
2745 if (!e)
2746 return NULL;
2747 if (NCH(n) == 1)
2748 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002749 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2750 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002751 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002752 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002753 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset,
2754 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002755 }
2756 return e;
2757}
2758
Guido van Rossum0368b722007-05-11 16:50:42 +00002759static expr_ty
2760ast_for_starred(struct compiling *c, const node *n)
2761{
2762 expr_ty tmp;
2763 REQ(n, star_expr);
2764
2765 tmp = ast_for_expr(c, CHILD(n, 1));
2766 if (!tmp)
2767 return NULL;
2768
2769 /* The Load context is changed later. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002770 return Starred(tmp, Load, LINENO(n), n->n_col_offset,
2771 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum0368b722007-05-11 16:50:42 +00002772}
2773
2774
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002775/* Do not name a variable 'expr'! Will cause a compile error.
2776*/
2777
2778static expr_ty
2779ast_for_expr(struct compiling *c, const node *n)
2780{
2781 /* handle the full range of simple expressions
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002782 namedexpr_test: test [':=' test]
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002783 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002784 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002786 and_test: not_test ('and' not_test)*
2787 not_test: 'not' not_test | comparison
2788 comparison: expr (comp_op expr)*
2789 expr: xor_expr ('|' xor_expr)*
2790 xor_expr: and_expr ('^' and_expr)*
2791 and_expr: shift_expr ('&' shift_expr)*
2792 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2793 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002794 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002795 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002796 power: atom_expr ['**' factor]
Jelle Zijlstraac317702017-10-05 20:24:46 -07002797 atom_expr: ['await'] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002798 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002799 */
2800
2801 asdl_seq *seq;
2802 int i;
2803
2804 loop:
2805 switch (TYPE(n)) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002806 case namedexpr_test:
2807 if (NCH(n) == 3)
2808 return ast_for_namedexpr(c, n);
2809 /* Fallthrough */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002810 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002811 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002812 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002813 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002814 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002815 else if (NCH(n) > 1)
2816 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002817 /* Fallthrough */
2818 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819 case and_test:
2820 if (NCH(n) == 1) {
2821 n = CHILD(n, 0);
2822 goto loop;
2823 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002824 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825 if (!seq)
2826 return NULL;
2827 for (i = 0; i < NCH(n); i += 2) {
2828 expr_ty e = ast_for_expr(c, CHILD(n, i));
2829 if (!e)
2830 return NULL;
2831 asdl_seq_SET(seq, i / 2, e);
2832 }
2833 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002834 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002835 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002836 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002837 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002838 return BoolOp(Or, seq, LINENO(n), n->n_col_offset,
2839 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002840 case not_test:
2841 if (NCH(n) == 1) {
2842 n = CHILD(n, 0);
2843 goto loop;
2844 }
2845 else {
2846 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2847 if (!expression)
2848 return NULL;
2849
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002850 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002851 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002852 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853 }
2854 case comparison:
2855 if (NCH(n) == 1) {
2856 n = CHILD(n, 0);
2857 goto loop;
2858 }
2859 else {
2860 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002861 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002862 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002863 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002864 if (!ops)
2865 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002866 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868 return NULL;
2869 }
2870 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002871 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002872
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002873 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002874 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002875 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002876 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002877
2878 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002879 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002881 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002883 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002884 asdl_seq_SET(cmps, i / 2, expression);
2885 }
2886 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002887 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002888 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002889 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002891 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset,
2892 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002893 }
2894 break;
2895
Guido van Rossum0368b722007-05-11 16:50:42 +00002896 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002898 /* The next five cases all handle BinOps. The main body of code
2899 is the same in each case, but the switch turned inside out to
2900 reuse the code for each type of operator.
2901 */
2902 case expr:
2903 case xor_expr:
2904 case and_expr:
2905 case shift_expr:
2906 case arith_expr:
2907 case term:
2908 if (NCH(n) == 1) {
2909 n = CHILD(n, 0);
2910 goto loop;
2911 }
2912 return ast_for_binop(c, n);
2913 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002914 node *an = NULL;
2915 node *en = NULL;
2916 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002917 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002918 if (NCH(n) > 1)
2919 an = CHILD(n, 1); /* yield_arg */
2920 if (an) {
2921 en = CHILD(an, NCH(an) - 1);
2922 if (NCH(an) == 2) {
2923 is_from = 1;
2924 exp = ast_for_expr(c, en);
2925 }
2926 else
2927 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002928 if (!exp)
2929 return NULL;
2930 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002931 if (is_from)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002932 return YieldFrom(exp, LINENO(n), n->n_col_offset,
2933 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
2934 return Yield(exp, LINENO(n), n->n_col_offset,
2935 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002936 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002937 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002938 if (NCH(n) == 1) {
2939 n = CHILD(n, 0);
2940 goto loop;
2941 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002942 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002943 case power:
2944 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002945 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002946 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947 return NULL;
2948 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002949 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002950 return NULL;
2951}
2952
2953static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02002954ast_for_call(struct compiling *c, const node *n, expr_ty func,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002955 const node *maybegenbeg, const node *closepar)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002956{
2957 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002958 arglist: argument (',' argument)* [',']
2959 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002960 */
2961
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002962 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002963 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002964 asdl_seq *args;
2965 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002966
2967 REQ(n, arglist);
2968
2969 nargs = 0;
2970 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002971 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002972 node *ch = CHILD(n, i);
2973 if (TYPE(ch) == argument) {
2974 if (NCH(ch) == 1)
2975 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002976 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2977 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02002978 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002979 ast_error(c, ch, "invalid syntax");
2980 return NULL;
2981 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002982 if (NCH(n) > 1) {
2983 ast_error(c, ch, "Generator expression must be parenthesized");
2984 return NULL;
2985 }
2986 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002987 else if (TYPE(CHILD(ch, 0)) == STAR)
2988 nargs++;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002989 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
2990 nargs++;
2991 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002992 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002993 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002994 nkeywords++;
2995 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002996 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002997
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002998 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002999 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003000 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003001 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003002 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003003 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003004
3005 nargs = 0; /* positional arguments + iterable argument unpackings */
3006 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
3007 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003008 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003009 node *ch = CHILD(n, i);
3010 if (TYPE(ch) == argument) {
3011 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003012 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003013 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003014 /* a positional argument */
3015 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003016 if (ndoublestars) {
3017 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003018 "positional argument follows "
3019 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003020 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003021 else {
3022 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003023 "positional argument follows "
3024 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003025 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003026 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00003027 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003028 e = ast_for_expr(c, chch);
3029 if (!e)
3030 return NULL;
3031 asdl_seq_SET(args, nargs++, e);
3032 }
3033 else if (TYPE(chch) == STAR) {
3034 /* an iterable argument unpacking */
3035 expr_ty starred;
3036 if (ndoublestars) {
3037 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003038 "iterable argument unpacking follows "
3039 "keyword argument unpacking");
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003040 return NULL;
3041 }
3042 e = ast_for_expr(c, CHILD(ch, 1));
3043 if (!e)
3044 return NULL;
3045 starred = Starred(e, Load, LINENO(chch),
3046 chch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003047 chch->n_end_lineno, chch->n_end_col_offset,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003048 c->c_arena);
3049 if (!starred)
3050 return NULL;
3051 asdl_seq_SET(args, nargs++, starred);
3052
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003053 }
3054 else if (TYPE(chch) == DOUBLESTAR) {
3055 /* a keyword argument unpacking */
3056 keyword_ty kw;
3057 i++;
3058 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003059 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003060 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003061 kw = keyword(NULL, e, c->c_arena);
3062 asdl_seq_SET(keywords, nkeywords++, kw);
3063 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003064 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003065 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003066 /* the lone generator expression */
Serhiy Storchakab619b092018-11-27 09:40:29 +02003067 e = copy_location(ast_for_genexp(c, ch), maybegenbeg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003068 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003069 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003070 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003071 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003072 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3073 /* treat colon equal as positional argument */
3074 if (nkeywords) {
3075 if (ndoublestars) {
3076 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003077 "positional argument follows "
3078 "keyword argument unpacking");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003079 }
3080 else {
3081 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003082 "positional argument follows "
3083 "keyword argument");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003084 }
3085 return NULL;
3086 }
3087 e = ast_for_namedexpr(c, ch);
3088 if (!e)
3089 return NULL;
3090 asdl_seq_SET(args, nargs++, e);
3091 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003092 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003093 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003094 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003095 identifier key, tmp;
3096 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003097
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003098 // To remain LL(1), the grammar accepts any test (basically, any
3099 // expression) in the keyword slot of a call site. So, we need
3100 // to manually enforce that the keyword is a NAME here.
3101 static const int name_tree[] = {
3102 test,
3103 or_test,
3104 and_test,
3105 not_test,
3106 comparison,
3107 expr,
3108 xor_expr,
3109 and_expr,
3110 shift_expr,
3111 arith_expr,
3112 term,
3113 factor,
3114 power,
3115 atom_expr,
3116 atom,
3117 0,
3118 };
3119 node *expr_node = chch;
3120 for (int i = 0; name_tree[i]; i++) {
3121 if (TYPE(expr_node) != name_tree[i])
3122 break;
3123 if (NCH(expr_node) != 1)
3124 break;
3125 expr_node = CHILD(expr_node, 0);
3126 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003127 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003128 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003129 "expression cannot contain assignment, "
3130 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003131 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003132 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003133 key = new_identifier(STR(expr_node), c);
3134 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003135 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003137 if (forbidden_name(c, key, chch, 1)) {
3138 return NULL;
3139 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003140 for (k = 0; k < nkeywords; k++) {
3141 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003142 if (tmp && !PyUnicode_Compare(tmp, key)) {
3143 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003144 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003145 return NULL;
3146 }
3147 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003148 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003149 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003150 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003151 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003153 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003154 asdl_seq_SET(keywords, nkeywords++, kw);
3155 }
3156 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003157 }
3158
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003159 return Call(func, args, keywords, func->lineno, func->col_offset,
3160 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003161}
3162
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003163static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003164ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003165{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003166 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003167 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003168 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003169 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003170 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003171 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003172 }
3173 else {
3174 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003175 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003176 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003177 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003178 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003179 else {
3180 asdl_seq *tmp = seq_for_testlist(c, n);
3181 if (!tmp)
3182 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003183 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3184 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003185 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003186}
3187
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003188static stmt_ty
3189ast_for_expr_stmt(struct compiling *c, const node *n)
3190{
3191 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003192 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003193 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
3194 annassign: ':' test ['=' (yield_expr|testlist)]
3195 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
3196 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
3197 '<<=' | '>>=' | '**=' | '//=')
Martin Panter69332c12016-08-04 13:07:31 +00003198 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003199 */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003200 int num = NCH(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003201
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003202 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003203 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003204 if (!e)
3205 return NULL;
3206
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003207 return Expr(e, LINENO(n), n->n_col_offset,
3208 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003209 }
3210 else if (TYPE(CHILD(n, 1)) == augassign) {
3211 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003212 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003213 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003214
Thomas Wouters89f507f2006-12-13 04:49:30 +00003215 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003216 if (!expr1)
3217 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003218 if(!set_context(c, expr1, Store, ch))
3219 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003220 /* set_context checks that most expressions are not the left side.
3221 Augmented assignments can only have a name, a subscript, or an
3222 attribute on the left, though, so we have to explicitly check for
3223 those. */
3224 switch (expr1->kind) {
3225 case Name_kind:
3226 case Attribute_kind:
3227 case Subscript_kind:
3228 break;
3229 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003230 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003231 return NULL;
3232 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233
Thomas Wouters89f507f2006-12-13 04:49:30 +00003234 ch = CHILD(n, 2);
3235 if (TYPE(ch) == testlist)
3236 expr2 = ast_for_testlist(c, ch);
3237 else
3238 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003239 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003240 return NULL;
3241
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003242 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003243 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003244 return NULL;
3245
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003246 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3247 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003248 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003249 else if (TYPE(CHILD(n, 1)) == annassign) {
3250 expr_ty expr1, expr2, expr3;
3251 node *ch = CHILD(n, 0);
3252 node *deep, *ann = CHILD(n, 1);
3253 int simple = 1;
3254
3255 /* we keep track of parens to qualify (x) as expression not name */
3256 deep = ch;
3257 while (NCH(deep) == 1) {
3258 deep = CHILD(deep, 0);
3259 }
3260 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3261 simple = 0;
3262 }
3263 expr1 = ast_for_testlist(c, ch);
3264 if (!expr1) {
3265 return NULL;
3266 }
3267 switch (expr1->kind) {
3268 case Name_kind:
3269 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3270 return NULL;
3271 }
3272 expr1->v.Name.ctx = Store;
3273 break;
3274 case Attribute_kind:
3275 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3276 return NULL;
3277 }
3278 expr1->v.Attribute.ctx = Store;
3279 break;
3280 case Subscript_kind:
3281 expr1->v.Subscript.ctx = Store;
3282 break;
3283 case List_kind:
3284 ast_error(c, ch,
3285 "only single target (not list) can be annotated");
3286 return NULL;
3287 case Tuple_kind:
3288 ast_error(c, ch,
3289 "only single target (not tuple) can be annotated");
3290 return NULL;
3291 default:
3292 ast_error(c, ch,
3293 "illegal target for annotation");
3294 return NULL;
3295 }
3296
3297 if (expr1->kind != Name_kind) {
3298 simple = 0;
3299 }
3300 ch = CHILD(ann, 1);
3301 expr2 = ast_for_expr(c, ch);
3302 if (!expr2) {
3303 return NULL;
3304 }
3305 if (NCH(ann) == 2) {
3306 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003307 LINENO(n), n->n_col_offset,
3308 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003309 }
3310 else {
3311 ch = CHILD(ann, 3);
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003312 if (TYPE(ch) == testlist) {
3313 expr3 = ast_for_testlist(c, ch);
3314 }
3315 else {
3316 expr3 = ast_for_expr(c, ch);
3317 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003318 if (!expr3) {
3319 return NULL;
3320 }
3321 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003322 LINENO(n), n->n_col_offset,
3323 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003324 }
3325 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003326 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003327 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003328 asdl_seq *targets;
3329 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003330 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003331 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003332
Thomas Wouters89f507f2006-12-13 04:49:30 +00003333 /* a normal assignment */
3334 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003335
3336 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3337 nch_minus_type = num - has_type_comment;
3338
3339 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003340 if (!targets)
3341 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003342 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003343 expr_ty e;
3344 node *ch = CHILD(n, i);
3345 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003346 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003347 return NULL;
3348 }
3349 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003351 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003353 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003354 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003355 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003356
Thomas Wouters89f507f2006-12-13 04:49:30 +00003357 asdl_seq_SET(targets, i / 2, e);
3358 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003359 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003360 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003361 expression = ast_for_testlist(c, value);
3362 else
3363 expression = ast_for_expr(c, value);
3364 if (!expression)
3365 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003366 if (has_type_comment) {
3367 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3368 if (!type_comment)
3369 return NULL;
3370 }
3371 else
3372 type_comment = NULL;
3373 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003374 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003375 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003376}
3377
Benjamin Peterson78565b22009-06-28 19:19:51 +00003378
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003379static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003380ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381{
3382 asdl_seq *seq;
3383 int i;
3384 expr_ty e;
3385
3386 REQ(n, exprlist);
3387
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003388 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003389 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003390 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003391 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003392 e = ast_for_expr(c, CHILD(n, i));
3393 if (!e)
3394 return NULL;
3395 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003396 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003397 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003398 }
3399 return seq;
3400}
3401
3402static stmt_ty
3403ast_for_del_stmt(struct compiling *c, const node *n)
3404{
3405 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003406
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003407 /* del_stmt: 'del' exprlist */
3408 REQ(n, del_stmt);
3409
3410 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3411 if (!expr_list)
3412 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003413 return Delete(expr_list, LINENO(n), n->n_col_offset,
3414 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003415}
3416
3417static stmt_ty
3418ast_for_flow_stmt(struct compiling *c, const node *n)
3419{
3420 /*
3421 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3422 | yield_stmt
3423 break_stmt: 'break'
3424 continue_stmt: 'continue'
3425 return_stmt: 'return' [testlist]
3426 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003427 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003428 raise_stmt: 'raise' [test [',' test [',' test]]]
3429 */
3430 node *ch;
3431
3432 REQ(n, flow_stmt);
3433 ch = CHILD(n, 0);
3434 switch (TYPE(ch)) {
3435 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003436 return Break(LINENO(n), n->n_col_offset,
3437 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003438 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003439 return Continue(LINENO(n), n->n_col_offset,
3440 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003441 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003442 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3443 if (!exp)
3444 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003445 return Expr(exp, LINENO(n), n->n_col_offset,
3446 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003447 }
3448 case return_stmt:
3449 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003450 return Return(NULL, LINENO(n), n->n_col_offset,
3451 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003452 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003453 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003454 if (!expression)
3455 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003456 return Return(expression, LINENO(n), n->n_col_offset,
3457 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003458 }
3459 case raise_stmt:
3460 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003461 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3462 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003463 else if (NCH(ch) >= 2) {
3464 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003465 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3466 if (!expression)
3467 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003468 if (NCH(ch) == 4) {
3469 cause = ast_for_expr(c, CHILD(ch, 3));
3470 if (!cause)
3471 return NULL;
3472 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003473 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3474 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003475 }
Stefan Krahf432a322017-08-21 13:09:59 +02003476 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003477 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003478 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003479 "unexpected flow_stmt: %d", TYPE(ch));
3480 return NULL;
3481 }
3482}
3483
3484static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003485alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003486{
3487 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003488 import_as_name: NAME ['as' NAME]
3489 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003490 dotted_name: NAME ('.' NAME)*
3491 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003492 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003493
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003494 loop:
3495 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003496 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003497 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003498 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003499 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003500 if (!name)
3501 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003502 if (NCH(n) == 3) {
3503 node *str_node = CHILD(n, 2);
3504 str = NEW_IDENTIFIER(str_node);
3505 if (!str)
3506 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003507 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003508 return NULL;
3509 }
3510 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003511 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003512 return NULL;
3513 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003514 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003515 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003516 case dotted_as_name:
3517 if (NCH(n) == 1) {
3518 n = CHILD(n, 0);
3519 goto loop;
3520 }
3521 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003522 node *asname_node = CHILD(n, 2);
3523 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003524 if (!a)
3525 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003526 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003527 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003528 if (!a->asname)
3529 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003530 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003531 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003532 return a;
3533 }
3534 break;
3535 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003536 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003537 node *name_node = CHILD(n, 0);
3538 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003539 if (!name)
3540 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003541 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003542 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003543 return alias(name, NULL, c->c_arena);
3544 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003545 else {
3546 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003547 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003548 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003549 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003551
3552 len = 0;
3553 for (i = 0; i < NCH(n); i += 2)
3554 /* length of string plus one for the dot */
3555 len += strlen(STR(CHILD(n, i))) + 1;
3556 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003557 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003558 if (!str)
3559 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003560 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003561 if (!s)
3562 return NULL;
3563 for (i = 0; i < NCH(n); i += 2) {
3564 char *sch = STR(CHILD(n, i));
3565 strcpy(s, STR(CHILD(n, i)));
3566 s += strlen(sch);
3567 *s++ = '.';
3568 }
3569 --s;
3570 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003571 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3572 PyBytes_GET_SIZE(str),
3573 NULL);
3574 Py_DECREF(str);
3575 if (!uni)
3576 return NULL;
3577 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003578 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003579 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3580 Py_DECREF(str);
3581 return NULL;
3582 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003583 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003584 }
3585 break;
3586 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003587 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003588 if (!str)
3589 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003590 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3591 Py_DECREF(str);
3592 return NULL;
3593 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003594 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003595 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003596 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003597 "unexpected import name: %d", TYPE(n));
3598 return NULL;
3599 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003600
3601 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003602 return NULL;
3603}
3604
3605static stmt_ty
3606ast_for_import_stmt(struct compiling *c, const node *n)
3607{
3608 /*
3609 import_stmt: import_name | import_from
3610 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003611 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3612 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003613 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003614 int lineno;
3615 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003616 int i;
3617 asdl_seq *aliases;
3618
3619 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003620 lineno = LINENO(n);
3621 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003622 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003623 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003624 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003625 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003626 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003627 if (!aliases)
3628 return NULL;
3629 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003630 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003631 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003632 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003633 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003634 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003635 // Even though n is modified above, the end position is not changed
3636 return Import(aliases, lineno, col_offset,
3637 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003638 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003639 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003640 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003641 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003642 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003643 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003644 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003645
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003646 /* Count the number of dots (for relative imports) and check for the
3647 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003648 for (idx = 1; idx < NCH(n); idx++) {
3649 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003650 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3651 if (!mod)
3652 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003653 idx++;
3654 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003655 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003656 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003657 ndots += 3;
3658 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003659 } else if (TYPE(CHILD(n, idx)) != DOT) {
3660 break;
3661 }
3662 ndots++;
3663 }
3664 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003665 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003666 case STAR:
3667 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003668 n = CHILD(n, idx);
3669 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003670 break;
3671 case LPAR:
3672 /* from ... import (x, y, z) */
3673 n = CHILD(n, idx + 1);
3674 n_children = NCH(n);
3675 break;
3676 case import_as_names:
3677 /* from ... import x, y, z */
3678 n = CHILD(n, idx);
3679 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003680 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003681 ast_error(c, n,
3682 "trailing comma not allowed without"
3683 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003684 return NULL;
3685 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003686 break;
3687 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003688 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003689 return NULL;
3690 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003691
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003692 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003693 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003694 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003695
3696 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003697 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003698 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003699 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003700 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003701 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003702 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003703 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003704 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003705 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003706 if (!import_alias)
3707 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003708 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003709 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003710 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003711 if (mod != NULL)
3712 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003713 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003714 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003715 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003716 }
Neal Norwitz79792652005-11-14 04:25:03 +00003717 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003718 "unknown import statement: starts with command '%s'",
3719 STR(CHILD(n, 0)));
3720 return NULL;
3721}
3722
3723static stmt_ty
3724ast_for_global_stmt(struct compiling *c, const node *n)
3725{
3726 /* global_stmt: 'global' NAME (',' NAME)* */
3727 identifier name;
3728 asdl_seq *s;
3729 int i;
3730
3731 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003732 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003733 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003734 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003735 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003736 name = NEW_IDENTIFIER(CHILD(n, i));
3737 if (!name)
3738 return NULL;
3739 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003740 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003741 return Global(s, LINENO(n), n->n_col_offset,
3742 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003743}
3744
3745static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003746ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3747{
3748 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3749 identifier name;
3750 asdl_seq *s;
3751 int i;
3752
3753 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003754 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003755 if (!s)
3756 return NULL;
3757 for (i = 1; i < NCH(n); i += 2) {
3758 name = NEW_IDENTIFIER(CHILD(n, i));
3759 if (!name)
3760 return NULL;
3761 asdl_seq_SET(s, i / 2, name);
3762 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003763 return Nonlocal(s, LINENO(n), n->n_col_offset,
3764 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003765}
3766
3767static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003768ast_for_assert_stmt(struct compiling *c, const node *n)
3769{
3770 /* assert_stmt: 'assert' test [',' test] */
3771 REQ(n, assert_stmt);
3772 if (NCH(n) == 2) {
3773 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3774 if (!expression)
3775 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003776 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3777 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003778 }
3779 else if (NCH(n) == 4) {
3780 expr_ty expr1, expr2;
3781
3782 expr1 = ast_for_expr(c, CHILD(n, 1));
3783 if (!expr1)
3784 return NULL;
3785 expr2 = ast_for_expr(c, CHILD(n, 3));
3786 if (!expr2)
3787 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003788
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003789 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3790 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003791 }
Neal Norwitz79792652005-11-14 04:25:03 +00003792 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003793 "improper number of parts to 'assert' statement: %d",
3794 NCH(n));
3795 return NULL;
3796}
3797
3798static asdl_seq *
3799ast_for_suite(struct compiling *c, const node *n)
3800{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003801 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003802 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003803 stmt_ty s;
3804 int i, total, num, end, pos = 0;
3805 node *ch;
3806
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003807 if (TYPE(n) != func_body_suite) {
3808 REQ(n, suite);
3809 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003810
3811 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003812 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003813 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003814 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003815 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003816 n = CHILD(n, 0);
3817 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003818 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003819 */
3820 end = NCH(n) - 1;
3821 if (TYPE(CHILD(n, end - 1)) == SEMI)
3822 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003823 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003824 for (i = 0; i < end; i += 2) {
3825 ch = CHILD(n, i);
3826 s = ast_for_stmt(c, ch);
3827 if (!s)
3828 return NULL;
3829 asdl_seq_SET(seq, pos++, s);
3830 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003831 }
3832 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003833 i = 2;
3834 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3835 i += 2;
3836 REQ(CHILD(n, 2), NEWLINE);
3837 }
3838
3839 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003840 ch = CHILD(n, i);
3841 REQ(ch, stmt);
3842 num = num_stmts(ch);
3843 if (num == 1) {
3844 /* small_stmt or compound_stmt with only one child */
3845 s = ast_for_stmt(c, ch);
3846 if (!s)
3847 return NULL;
3848 asdl_seq_SET(seq, pos++, s);
3849 }
3850 else {
3851 int j;
3852 ch = CHILD(ch, 0);
3853 REQ(ch, simple_stmt);
3854 for (j = 0; j < NCH(ch); j += 2) {
3855 /* statement terminates with a semi-colon ';' */
3856 if (NCH(CHILD(ch, j)) == 0) {
3857 assert((j + 1) == NCH(ch));
3858 break;
3859 }
3860 s = ast_for_stmt(c, CHILD(ch, j));
3861 if (!s)
3862 return NULL;
3863 asdl_seq_SET(seq, pos++, s);
3864 }
3865 }
3866 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003867 }
3868 assert(pos == seq->size);
3869 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003870}
3871
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003872static void
3873get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3874{
Pablo Galindo46a97922019-02-19 22:51:53 +00003875 Py_ssize_t tot = asdl_seq_LEN(s);
Ivan Levkivskyi181835d2019-02-10 15:39:49 +00003876 // There must be no empty suites.
3877 assert(tot > 0);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003878 stmt_ty last = asdl_seq_GET(s, tot - 1);
3879 *end_lineno = last->end_lineno;
3880 *end_col_offset = last->end_col_offset;
3881}
3882
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003883static stmt_ty
3884ast_for_if_stmt(struct compiling *c, const node *n)
3885{
3886 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3887 ['else' ':' suite]
3888 */
3889 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003890 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003891
3892 REQ(n, if_stmt);
3893
3894 if (NCH(n) == 4) {
3895 expr_ty expression;
3896 asdl_seq *suite_seq;
3897
3898 expression = ast_for_expr(c, CHILD(n, 1));
3899 if (!expression)
3900 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003901 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003902 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003903 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003904 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003905
Guido van Rossumd8faa362007-04-27 19:54:29 +00003906 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003907 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003908 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003909
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003910 s = STR(CHILD(n, 4));
3911 /* s[2], the third character in the string, will be
3912 's' for el_s_e, or
3913 'i' for el_i_f
3914 */
3915 if (s[2] == 's') {
3916 expr_ty expression;
3917 asdl_seq *seq1, *seq2;
3918
3919 expression = ast_for_expr(c, CHILD(n, 1));
3920 if (!expression)
3921 return NULL;
3922 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003923 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003924 return NULL;
3925 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003926 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003927 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003928 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003929
Guido van Rossumd8faa362007-04-27 19:54:29 +00003930 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003931 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003932 }
3933 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003934 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003935 expr_ty expression;
3936 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003937 asdl_seq *orelse = NULL;
3938 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003939 /* must reference the child n_elif+1 since 'else' token is third,
3940 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003941 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3942 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3943 has_else = 1;
3944 n_elif -= 3;
3945 }
3946 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003947
Thomas Wouters89f507f2006-12-13 04:49:30 +00003948 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003949 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003950
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003951 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003952 if (!orelse)
3953 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003954 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003955 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003956 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003957 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3958 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003959 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003960 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3961 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003962 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003963 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003965 asdl_seq_SET(orelse, 0,
3966 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003967 LINENO(CHILD(n, NCH(n) - 6)),
3968 CHILD(n, NCH(n) - 6)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003969 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003970 /* the just-created orelse handled the last elif */
3971 n_elif--;
3972 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003973
Thomas Wouters89f507f2006-12-13 04:49:30 +00003974 for (i = 0; i < n_elif; i++) {
3975 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003976 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003977 if (!newobj)
3978 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003979 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003980 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003981 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003982 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003983 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003984 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003985
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003986 if (orelse != NULL) {
3987 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
3988 } else {
3989 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3990 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003991 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003992 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003993 LINENO(CHILD(n, off)),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003994 CHILD(n, off)->n_col_offset,
3995 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003996 orelse = newobj;
3997 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003998 expression = ast_for_expr(c, CHILD(n, 1));
3999 if (!expression)
4000 return NULL;
4001 suite_seq = ast_for_suite(c, CHILD(n, 3));
4002 if (!suite_seq)
4003 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004004 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004005 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004006 LINENO(n), n->n_col_offset,
4007 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004008 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004009
4010 PyErr_Format(PyExc_SystemError,
4011 "unexpected token in 'if' statement: %s", s);
4012 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004013}
4014
4015static stmt_ty
4016ast_for_while_stmt(struct compiling *c, const node *n)
4017{
4018 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
4019 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004020 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004021
4022 if (NCH(n) == 4) {
4023 expr_ty expression;
4024 asdl_seq *suite_seq;
4025
4026 expression = ast_for_expr(c, CHILD(n, 1));
4027 if (!expression)
4028 return NULL;
4029 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004030 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004031 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004032 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4033 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
4034 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004035 }
4036 else if (NCH(n) == 7) {
4037 expr_ty expression;
4038 asdl_seq *seq1, *seq2;
4039
4040 expression = ast_for_expr(c, CHILD(n, 1));
4041 if (!expression)
4042 return NULL;
4043 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004044 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004045 return NULL;
4046 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004047 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004048 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004049 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004050
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004051 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4052 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004053 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004054
4055 PyErr_Format(PyExc_SystemError,
4056 "wrong number of tokens for 'while' statement: %d",
4057 NCH(n));
4058 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004059}
4060
4061static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004062ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004063{
guoci90fc8982018-09-11 17:45:45 -04004064 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004065 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004066 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004067 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004068 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004069 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004070 int has_type_comment;
4071 string type_comment;
4072 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004073 REQ(n, for_stmt);
4074
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004075 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4076
4077 if (NCH(n) == 9 + has_type_comment) {
4078 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004079 if (!seq)
4080 return NULL;
4081 }
4082
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004083 node_target = CHILD(n, 1);
4084 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004085 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004086 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004087 /* Check the # of children rather than the length of _target, since
4088 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004089 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004090 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004091 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004092 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004093 target = Tuple(_target, Store, first->lineno, first->col_offset,
4094 node_target->n_end_lineno, node_target->n_end_col_offset,
4095 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004096
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004097 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004098 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004099 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004100 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004101 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004102 return NULL;
4103
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004104 if (seq != NULL) {
4105 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4106 } else {
4107 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4108 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004109
4110 if (has_type_comment) {
4111 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4112 if (!type_comment)
4113 return NULL;
4114 }
4115 else
4116 type_comment = NULL;
4117
Yury Selivanov75445082015-05-11 22:57:16 -04004118 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004119 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004120 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004121 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004122 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004123 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004124 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004125 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004126}
4127
4128static excepthandler_ty
4129ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4130{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004131 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004132 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004133 REQ(exc, except_clause);
4134 REQ(body, suite);
4135
4136 if (NCH(exc) == 1) {
4137 asdl_seq *suite_seq = ast_for_suite(c, body);
4138 if (!suite_seq)
4139 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004140 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004141
Neal Norwitzad74aa82008-03-31 05:14:30 +00004142 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004143 exc->n_col_offset,
4144 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004145 }
4146 else if (NCH(exc) == 2) {
4147 expr_ty expression;
4148 asdl_seq *suite_seq;
4149
4150 expression = ast_for_expr(c, CHILD(exc, 1));
4151 if (!expression)
4152 return NULL;
4153 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004154 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004155 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004156 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004157
Neal Norwitzad74aa82008-03-31 05:14:30 +00004158 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004159 exc->n_col_offset,
4160 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004161 }
4162 else if (NCH(exc) == 4) {
4163 asdl_seq *suite_seq;
4164 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004165 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004166 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004167 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004168 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004169 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004170 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004171 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004172 return NULL;
4173 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004174 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004175 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004176 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004177
Neal Norwitzad74aa82008-03-31 05:14:30 +00004178 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004179 exc->n_col_offset,
4180 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004181 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004182
4183 PyErr_Format(PyExc_SystemError,
4184 "wrong number of children for 'except' clause: %d",
4185 NCH(exc));
4186 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004187}
4188
4189static stmt_ty
4190ast_for_try_stmt(struct compiling *c, const node *n)
4191{
Neal Norwitzf599f422005-12-17 21:33:47 +00004192 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004193 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004194 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004195 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004196
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004197 REQ(n, try_stmt);
4198
Neal Norwitzf599f422005-12-17 21:33:47 +00004199 body = ast_for_suite(c, CHILD(n, 2));
4200 if (body == NULL)
4201 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004202
Neal Norwitzf599f422005-12-17 21:33:47 +00004203 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4204 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4205 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4206 /* we can assume it's an "else",
4207 because nch >= 9 for try-else-finally and
4208 it would otherwise have a type of except_clause */
4209 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4210 if (orelse == NULL)
4211 return NULL;
4212 n_except--;
4213 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004214
Neal Norwitzf599f422005-12-17 21:33:47 +00004215 finally = ast_for_suite(c, CHILD(n, nch - 1));
4216 if (finally == NULL)
4217 return NULL;
4218 n_except--;
4219 }
4220 else {
4221 /* we can assume it's an "else",
4222 otherwise it would have a type of except_clause */
4223 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4224 if (orelse == NULL)
4225 return NULL;
4226 n_except--;
4227 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004228 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004229 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004230 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004231 return NULL;
4232 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004233
Neal Norwitzf599f422005-12-17 21:33:47 +00004234 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004235 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004236 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004237 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004238 if (handlers == NULL)
4239 return NULL;
4240
4241 for (i = 0; i < n_except; i++) {
4242 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4243 CHILD(n, 5 + i * 3));
4244 if (!e)
4245 return NULL;
4246 asdl_seq_SET(handlers, i, e);
4247 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004248 }
4249
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004250 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004251 if (finally != NULL) {
4252 // finally is always last
4253 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4254 } else if (orelse != NULL) {
4255 // otherwise else is last
4256 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4257 } else {
4258 // inline the get_last_end_pos logic due to layout mismatch
4259 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4260 end_lineno = last_handler->end_lineno;
4261 end_col_offset = last_handler->end_col_offset;
4262 }
4263 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4264 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004265}
4266
Georg Brandl0c315622009-05-25 21:10:36 +00004267/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004268static withitem_ty
4269ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004270{
4271 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004272
Georg Brandl0c315622009-05-25 21:10:36 +00004273 REQ(n, with_item);
4274 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004275 if (!context_expr)
4276 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004277 if (NCH(n) == 3) {
4278 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004279
4280 if (!optional_vars) {
4281 return NULL;
4282 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004283 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004284 return NULL;
4285 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004286 }
4287
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004288 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004289}
4290
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004291/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004292static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004293ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004294{
guoci90fc8982018-09-11 17:45:45 -04004295 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004296 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004297 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004298 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004299
4300 REQ(n, with_stmt);
4301
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004302 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4303 nch_minus_type = NCH(n) - has_type_comment;
4304
4305 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004306 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004307 if (!items)
4308 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004309 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004310 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4311 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004312 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004313 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004314 }
4315
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004316 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4317 if (!body)
4318 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004319 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004320
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004321 if (has_type_comment) {
4322 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4323 if (!type_comment)
4324 return NULL;
4325 }
4326 else
4327 type_comment = NULL;
4328
Yury Selivanov75445082015-05-11 22:57:16 -04004329 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004330 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004331 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004332 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004333 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004334 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004335}
4336
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004337static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004338ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004339{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004340 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004341 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004342 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004343 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004344 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004345
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004346 REQ(n, classdef);
4347
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004348 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004349 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004350 if (!s)
4351 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004352 get_last_end_pos(s, &end_lineno, &end_col_offset);
4353
Benjamin Peterson30760062008-11-25 04:02:28 +00004354 classname = NEW_IDENTIFIER(CHILD(n, 1));
4355 if (!classname)
4356 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004357 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004358 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004359 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004360 LINENO(n), n->n_col_offset,
4361 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004362 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004363
4364 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004365 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004366 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004367 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004368 get_last_end_pos(s, &end_lineno, &end_col_offset);
4369
Benjamin Peterson30760062008-11-25 04:02:28 +00004370 classname = NEW_IDENTIFIER(CHILD(n, 1));
4371 if (!classname)
4372 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004373 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004374 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004375 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004376 LINENO(n), n->n_col_offset,
4377 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004378 }
4379
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004380 /* class NAME '(' arglist ')' ':' suite */
4381 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004382 {
4383 PyObject *dummy_name;
4384 expr_ty dummy;
4385 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4386 if (!dummy_name)
4387 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004388 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4389 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4390 c->c_arena);
4391 call = ast_for_call(c, CHILD(n, 3), dummy, NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004392 if (!call)
4393 return NULL;
4394 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004395 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004396 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004397 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004398 get_last_end_pos(s, &end_lineno, &end_col_offset);
4399
Benjamin Peterson30760062008-11-25 04:02:28 +00004400 classname = NEW_IDENTIFIER(CHILD(n, 1));
4401 if (!classname)
4402 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004403 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004404 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004405
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004406 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004407 decorator_seq, LINENO(n), n->n_col_offset,
4408 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004409}
4410
4411static stmt_ty
4412ast_for_stmt(struct compiling *c, const node *n)
4413{
4414 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004415 assert(NCH(n) == 1);
4416 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004417 }
4418 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004419 assert(num_stmts(n) == 1);
4420 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004421 }
4422 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004423 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004424 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4425 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004426 */
4427 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004428 case expr_stmt:
4429 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004430 case del_stmt:
4431 return ast_for_del_stmt(c, n);
4432 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004433 return Pass(LINENO(n), n->n_col_offset,
4434 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004435 case flow_stmt:
4436 return ast_for_flow_stmt(c, n);
4437 case import_stmt:
4438 return ast_for_import_stmt(c, n);
4439 case global_stmt:
4440 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004441 case nonlocal_stmt:
4442 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004443 case assert_stmt:
4444 return ast_for_assert_stmt(c, n);
4445 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004446 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004447 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4448 TYPE(n), NCH(n));
4449 return NULL;
4450 }
4451 }
4452 else {
4453 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004454 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004455 */
4456 node *ch = CHILD(n, 0);
4457 REQ(n, compound_stmt);
4458 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004459 case if_stmt:
4460 return ast_for_if_stmt(c, ch);
4461 case while_stmt:
4462 return ast_for_while_stmt(c, ch);
4463 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004464 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004465 case try_stmt:
4466 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004467 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004468 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004469 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004470 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004471 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004472 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004473 case decorated:
4474 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004475 case async_stmt:
4476 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004477 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004478 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004479 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004480 TYPE(n), NCH(n));
4481 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004482 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004483 }
4484}
4485
4486static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004487parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004488{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004489 const char *end;
4490 long x;
4491 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004492 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004493 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004494
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004495 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004496 errno = 0;
4497 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004498 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004499 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004500 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004501 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004502 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004503 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004504 }
4505 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004506 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004507 if (*end == '\0') {
4508 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004509 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004510 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004511 }
4512 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004513 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004514 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004515 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4516 if (compl.imag == -1.0 && PyErr_Occurred())
4517 return NULL;
4518 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004519 }
4520 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004521 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004522 dx = PyOS_string_to_double(s, NULL, NULL);
4523 if (dx == -1.0 && PyErr_Occurred())
4524 return NULL;
4525 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004526 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004527}
4528
4529static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004530parsenumber(struct compiling *c, const char *s)
4531{
4532 char *dup, *end;
4533 PyObject *res = NULL;
4534
4535 assert(s != NULL);
4536
4537 if (strchr(s, '_') == NULL) {
4538 return parsenumber_raw(c, s);
4539 }
4540 /* Create a duplicate without underscores. */
4541 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004542 if (dup == NULL) {
4543 return PyErr_NoMemory();
4544 }
Brett Cannona721aba2016-09-09 14:57:09 -07004545 end = dup;
4546 for (; *s; s++) {
4547 if (*s != '_') {
4548 *end++ = *s;
4549 }
4550 }
4551 *end = '\0';
4552 res = parsenumber_raw(c, dup);
4553 PyMem_Free(dup);
4554 return res;
4555}
4556
4557static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004558decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004559{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004560 const char *s, *t;
4561 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004562 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4563 while (s < end && (*s & 0x80)) s++;
4564 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004565 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004566}
4567
Eric V. Smith56466482016-10-31 14:46:26 -04004568static int
4569warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004570 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004571{
4572 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4573 first_invalid_escape_char);
4574 if (msg == NULL) {
4575 return -1;
4576 }
Serhiy Storchaka65439122018-10-19 17:42:06 +03004577 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004578 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004579 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004580 {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004581 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004582 /* Replace the SyntaxWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004583 to get a more accurate error report */
4584 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004585 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004586 }
4587 Py_DECREF(msg);
4588 return -1;
4589 }
4590 Py_DECREF(msg);
4591 return 0;
4592}
4593
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004594static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004595decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4596 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004597{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004598 PyObject *v, *u;
4599 char *buf;
4600 char *p;
4601 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004602
Benjamin Peterson202803a2016-02-25 22:34:45 -08004603 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004604 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004605 return NULL;
4606 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4607 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4608 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4609 if (u == NULL)
4610 return NULL;
4611 p = buf = PyBytes_AsString(u);
4612 end = s + len;
4613 while (s < end) {
4614 if (*s == '\\') {
4615 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004616 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004617 strcpy(p, "u005c");
4618 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004619 if (s >= end)
4620 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004621 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004622 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004623 if (*s & 0x80) { /* XXX inefficient */
4624 PyObject *w;
4625 int kind;
4626 void *data;
4627 Py_ssize_t len, i;
4628 w = decode_utf8(c, &s, end);
4629 if (w == NULL) {
4630 Py_DECREF(u);
4631 return NULL;
4632 }
4633 kind = PyUnicode_KIND(w);
4634 data = PyUnicode_DATA(w);
4635 len = PyUnicode_GET_LENGTH(w);
4636 for (i = 0; i < len; i++) {
4637 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4638 sprintf(p, "\\U%08x", chr);
4639 p += 10;
4640 }
4641 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004642 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004643 Py_DECREF(w);
4644 } else {
4645 *p++ = *s++;
4646 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004647 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004648 len = p - buf;
4649 s = buf;
4650
Eric V. Smith56466482016-10-31 14:46:26 -04004651 const char *first_invalid_escape;
4652 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4653
4654 if (v != NULL && first_invalid_escape != NULL) {
4655 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4656 /* We have not decref u before because first_invalid_escape points
4657 inside u. */
4658 Py_XDECREF(u);
4659 Py_DECREF(v);
4660 return NULL;
4661 }
4662 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004663 Py_XDECREF(u);
4664 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004665}
4666
Eric V. Smith56466482016-10-31 14:46:26 -04004667static PyObject *
4668decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4669 size_t len)
4670{
4671 const char *first_invalid_escape;
4672 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4673 &first_invalid_escape);
4674 if (result == NULL)
4675 return NULL;
4676
4677 if (first_invalid_escape != NULL) {
4678 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4679 Py_DECREF(result);
4680 return NULL;
4681 }
4682 }
4683 return result;
4684}
4685
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004686/* Shift locations for the given node and all its children by adding `lineno`
4687 and `col_offset` to existing locations. */
4688static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4689{
4690 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004691 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004692 for (int i = 0; i < NCH(n); ++i) {
4693 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4694 /* Shifting column offsets unnecessary if there's been newlines. */
4695 col_offset = 0;
4696 }
4697 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4698 }
4699 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004700 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004701}
4702
4703/* Fix locations for the given node and its children.
4704
4705 `parent` is the enclosing node.
4706 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004707 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004708*/
4709static void
4710fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4711{
4712 char *substr = NULL;
4713 char *start;
4714 int lines = LINENO(parent) - 1;
4715 int cols = parent->n_col_offset;
4716 /* Find the full fstring to fix location information in `n`. */
4717 while (parent && parent->n_type != STRING)
4718 parent = parent->n_child;
4719 if (parent && parent->n_str) {
4720 substr = strstr(parent->n_str, expr_str);
4721 if (substr) {
4722 start = substr;
4723 while (start > parent->n_str) {
4724 if (start[0] == '\n')
4725 break;
4726 start--;
4727 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004728 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004729 /* adjust the start based on the number of newlines encountered
4730 before the f-string expression */
4731 for (char* p = parent->n_str; p < substr; p++) {
4732 if (*p == '\n') {
4733 lines++;
4734 }
4735 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004736 }
4737 }
4738 fstring_shift_node_locations(n, lines, cols);
4739}
4740
Eric V. Smith451d0e32016-09-09 21:56:20 -04004741/* Compile this expression in to an expr_ty. Add parens around the
4742 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004743static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004744fstring_compile_expr(const char *expr_start, const char *expr_end,
4745 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004746
Eric V. Smith235a6f02015-09-19 14:51:32 -04004747{
4748 PyCompilerFlags cf;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004749 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004750 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004751 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004752 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004753 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004754
Eric V. Smith1d44c412015-09-23 07:49:00 -04004755 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004756 assert(*(expr_start-1) == '{');
4757 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004758
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004759 /* If the substring is all whitespace, it's an error. We need to catch this
4760 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4761 because turning the expression '' in to '()' would go from being invalid
4762 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004763 for (s = expr_start; s != expr_end; s++) {
4764 char c = *s;
4765 /* The Python parser ignores only the following whitespace
4766 characters (\r already is converted to \n). */
4767 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004768 break;
4769 }
4770 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004771 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004772 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004773 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004774 }
4775
Eric V. Smith451d0e32016-09-09 21:56:20 -04004776 len = expr_end - expr_start;
4777 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4778 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004779 if (str == NULL) {
4780 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004781 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004782 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004783
Eric V. Smith451d0e32016-09-09 21:56:20 -04004784 str[0] = '(';
4785 memcpy(str+1, expr_start, len);
4786 str[len+1] = ')';
4787 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004788
4789 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004790 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4791 Py_eval_input, 0);
4792 if (!mod_n) {
4793 PyMem_RawFree(str);
4794 return NULL;
4795 }
4796 /* Reuse str to find the correct column offset. */
4797 str[0] = '{';
4798 str[len+1] = '}';
4799 fstring_fix_node_location(n, mod_n, str);
4800 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004801 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004802 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004803 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004804 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004805 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004806}
4807
4808/* Return -1 on error.
4809
4810 Return 0 if we reached the end of the literal.
4811
4812 Return 1 if we haven't reached the end of the literal, but we want
4813 the caller to process the literal up to this point. Used for
4814 doubled braces.
4815*/
4816static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004817fstring_find_literal(const char **str, const char *end, int raw,
4818 PyObject **literal, int recurse_lvl,
4819 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004820{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004821 /* Get any literal string. It ends when we hit an un-doubled left
4822 brace (which isn't part of a unicode name escape such as
4823 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004824
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004825 const char *s = *str;
4826 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004827 int result = 0;
4828
Eric V. Smith235a6f02015-09-19 14:51:32 -04004829 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004830 while (s < end) {
4831 char ch = *s++;
4832 if (!raw && ch == '\\' && s < end) {
4833 ch = *s++;
4834 if (ch == 'N') {
4835 if (s < end && *s++ == '{') {
4836 while (s < end && *s++ != '}') {
4837 }
4838 continue;
4839 }
4840 break;
4841 }
4842 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4843 return -1;
4844 }
4845 }
4846 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004847 /* Check for doubled braces, but only at the top level. If
4848 we checked at every level, then f'{0:{3}}' would fail
4849 with the two closing braces. */
4850 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004851 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004852 /* We're going to tell the caller that the literal ends
4853 here, but that they should continue scanning. But also
4854 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004855 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004856 result = 1;
4857 goto done;
4858 }
4859
4860 /* Where a single '{' is the start of a new expression, a
4861 single '}' is not allowed. */
4862 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004863 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004864 ast_error(c, n, "f-string: single '}' is not allowed");
4865 return -1;
4866 }
4867 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004868 /* We're either at a '{', which means we're starting another
4869 expression; or a '}', which means we're at the end of this
4870 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004871 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004872 break;
4873 }
4874 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004875 *str = s;
4876 assert(s <= end);
4877 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004878done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004879 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004880 if (raw)
4881 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004882 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004883 NULL, NULL);
4884 else
Eric V. Smith56466482016-10-31 14:46:26 -04004885 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004886 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004887 if (!*literal)
4888 return -1;
4889 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004890 return result;
4891}
4892
4893/* Forward declaration because parsing is recursive. */
4894static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004895fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004896 struct compiling *c, const node *n);
4897
Eric V. Smith451d0e32016-09-09 21:56:20 -04004898/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004899 expression (so it must be a '{'). Returns the FormattedValue node,
4900 which includes the expression, conversion character, and
4901 format_spec expression.
4902
4903 Note that I don't do a perfect job here: I don't make sure that a
4904 closing brace doesn't match an opening paren, for example. It
4905 doesn't need to error on all invalid expressions, just correctly
4906 find the end of all valid ones. Any errors inside the expression
4907 will be caught when we parse it later. */
4908static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004909fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004910 expr_ty *expression, struct compiling *c, const node *n)
4911{
4912 /* Return -1 on error, else 0. */
4913
Eric V. Smith451d0e32016-09-09 21:56:20 -04004914 const char *expr_start;
4915 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004916 expr_ty simple_expression;
4917 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004918 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004919
4920 /* 0 if we're not in a string, else the quote char we're trying to
4921 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004922 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004923
4924 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4925 int string_type = 0;
4926
4927 /* Keep track of nesting level for braces/parens/brackets in
4928 expressions. */
4929 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004930 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04004931
4932 /* Can only nest one level deep. */
4933 if (recurse_lvl >= 2) {
4934 ast_error(c, n, "f-string: expressions nested too deeply");
4935 return -1;
4936 }
4937
4938 /* The first char must be a left brace, or we wouldn't have gotten
4939 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004940 assert(**str == '{');
4941 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004942
Eric V. Smith451d0e32016-09-09 21:56:20 -04004943 expr_start = *str;
4944 for (; *str < end; (*str)++) {
4945 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004946
4947 /* Loop invariants. */
4948 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004949 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004950 if (quote_char)
4951 assert(string_type == 1 || string_type == 3);
4952 else
4953 assert(string_type == 0);
4954
Eric V. Smith451d0e32016-09-09 21:56:20 -04004955 ch = **str;
4956 /* Nowhere inside an expression is a backslash allowed. */
4957 if (ch == '\\') {
4958 /* Error: can't include a backslash character, inside
4959 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004960 ast_error(c, n,
4961 "f-string expression part "
4962 "cannot include a backslash");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004963 return -1;
4964 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004965 if (quote_char) {
4966 /* We're inside a string. See if we're at the end. */
4967 /* This code needs to implement the same non-error logic
4968 as tok_get from tokenizer.c, at the letter_quote
4969 label. To actually share that code would be a
4970 nightmare. But, it's unlikely to change and is small,
4971 so duplicate it here. Note we don't need to catch all
4972 of the errors, since they'll be caught when parsing the
4973 expression. We just need to match the non-error
4974 cases. Thus we can ignore \n in single-quoted strings,
4975 for example. Or non-terminated strings. */
4976 if (ch == quote_char) {
4977 /* Does this match the string_type (single or triple
4978 quoted)? */
4979 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004980 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004981 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004982 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004983 string_type = 0;
4984 quote_char = 0;
4985 continue;
4986 }
4987 } else {
4988 /* We're at the end of a normal string. */
4989 quote_char = 0;
4990 string_type = 0;
4991 continue;
4992 }
4993 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004994 } else if (ch == '\'' || ch == '"') {
4995 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004996 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004997 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004998 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004999 } else {
5000 /* Start of a normal string. */
5001 string_type = 1;
5002 }
5003 /* Start looking for the end of the string. */
5004 quote_char = ch;
5005 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005006 if (nested_depth >= MAXLEVEL) {
5007 ast_error(c, n, "f-string: too many nested parenthesis");
5008 return -1;
5009 }
5010 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005011 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005012 } else if (ch == '#') {
5013 /* Error: can't include a comment character, inside parens
5014 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04005015 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04005016 return -1;
5017 } else if (nested_depth == 0 &&
5018 (ch == '!' || ch == ':' || ch == '}')) {
5019 /* First, test for the special case of "!=". Since '=' is
5020 not an allowed conversion character, nothing is lost in
5021 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005022 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005023 /* This isn't a conversion character, just continue. */
5024 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005025 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005026 /* Normal way out of this loop. */
5027 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005028 } else if (ch == ']' || ch == '}' || ch == ')') {
5029 if (!nested_depth) {
5030 ast_error(c, n, "f-string: unmatched '%c'", ch);
5031 return -1;
5032 }
5033 nested_depth--;
5034 int opening = parenstack[nested_depth];
5035 if (!((opening == '(' && ch == ')') ||
5036 (opening == '[' && ch == ']') ||
5037 (opening == '{' && ch == '}')))
5038 {
5039 ast_error(c, n,
5040 "f-string: closing parenthesis '%c' "
5041 "does not match opening parenthesis '%c'",
5042 ch, opening);
5043 return -1;
5044 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005045 } else {
5046 /* Just consume this char and loop around. */
5047 }
5048 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005049 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005050 /* If we leave this loop in a string or with mismatched parens, we
5051 don't care. We'll get a syntax error when compiling the
5052 expression. But, we can produce a better error message, so
5053 let's just do that.*/
5054 if (quote_char) {
5055 ast_error(c, n, "f-string: unterminated string");
5056 return -1;
5057 }
5058 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005059 int opening = parenstack[nested_depth - 1];
5060 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005061 return -1;
5062 }
5063
Eric V. Smith451d0e32016-09-09 21:56:20 -04005064 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005065 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005066
5067 /* Compile the expression as soon as possible, so we show errors
5068 related to the expression before errors related to the
5069 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005070 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005071 if (!simple_expression)
5072 return -1;
5073
5074 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005075 if (**str == '!') {
5076 *str += 1;
5077 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005078 goto unexpected_end_of_string;
5079
Eric V. Smith451d0e32016-09-09 21:56:20 -04005080 conversion = **str;
5081 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005082
5083 /* Validate the conversion. */
5084 if (!(conversion == 's' || conversion == 'r'
5085 || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005086 ast_error(c, n,
5087 "f-string: invalid conversion character: "
5088 "expected 's', 'r', or 'a'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04005089 return -1;
5090 }
5091 }
5092
5093 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005094 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005095 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005096 if (**str == ':') {
5097 *str += 1;
5098 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005099 goto unexpected_end_of_string;
5100
5101 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005102 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005103 if (!format_spec)
5104 return -1;
5105 }
5106
Eric V. Smith451d0e32016-09-09 21:56:20 -04005107 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005108 goto unexpected_end_of_string;
5109
5110 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005111 assert(*str < end);
5112 assert(**str == '}');
5113 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005114
Eric V. Smith451d0e32016-09-09 21:56:20 -04005115 /* And now create the FormattedValue node that represents this
5116 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005117 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005118 format_spec, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005119 n->n_end_lineno, n->n_end_col_offset,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005120 c->c_arena);
5121 if (!*expression)
5122 return -1;
5123
5124 return 0;
5125
5126unexpected_end_of_string:
5127 ast_error(c, n, "f-string: expecting '}'");
5128 return -1;
5129}
5130
5131/* Return -1 on error.
5132
5133 Return 0 if we have a literal (possible zero length) and an
5134 expression (zero length if at the end of the string.
5135
5136 Return 1 if we have a literal, but no expression, and we want the
5137 caller to call us again. This is used to deal with doubled
5138 braces.
5139
5140 When called multiple times on the string 'a{{b{0}c', this function
5141 will return:
5142
5143 1. the literal 'a{' with no expression, and a return value
5144 of 1. Despite the fact that there's no expression, the return
5145 value of 1 means we're not finished yet.
5146
5147 2. the literal 'b' and the expression '0', with a return value of
5148 0. The fact that there's an expression means we're not finished.
5149
5150 3. literal 'c' with no expression and a return value of 0. The
5151 combination of the return value of 0 with no expression means
5152 we're finished.
5153*/
5154static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005155fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5156 int recurse_lvl, PyObject **literal,
5157 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005158 struct compiling *c, const node *n)
5159{
5160 int result;
5161
5162 assert(*literal == NULL && *expression == NULL);
5163
5164 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005165 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005166 if (result < 0)
5167 goto error;
5168
5169 assert(result == 0 || result == 1);
5170
5171 if (result == 1)
5172 /* We have a literal, but don't look at the expression. */
5173 return 1;
5174
Eric V. Smith451d0e32016-09-09 21:56:20 -04005175 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005176 /* We're at the end of the string or the end of a nested
5177 f-string: no expression. The top-level error case where we
5178 expect to be at the end of the string but we're at a '}' is
5179 handled later. */
5180 return 0;
5181
5182 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005183 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005184
Eric V. Smith451d0e32016-09-09 21:56:20 -04005185 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005186 goto error;
5187
5188 return 0;
5189
5190error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005191 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005192 return -1;
5193}
5194
5195#define EXPRLIST_N_CACHED 64
5196
5197typedef struct {
5198 /* Incrementally build an array of expr_ty, so be used in an
5199 asdl_seq. Cache some small but reasonably sized number of
5200 expr_ty's, and then after that start dynamically allocating,
5201 doubling the number allocated each time. Note that the f-string
5202 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005203 Constant for the literal 'a'. So you add expr_ty's about twice as
Eric V. Smith235a6f02015-09-19 14:51:32 -04005204 fast as you add exressions in an f-string. */
5205
5206 Py_ssize_t allocated; /* Number we've allocated. */
5207 Py_ssize_t size; /* Number we've used. */
5208 expr_ty *p; /* Pointer to the memory we're actually
5209 using. Will point to 'data' until we
5210 start dynamically allocating. */
5211 expr_ty data[EXPRLIST_N_CACHED];
5212} ExprList;
5213
5214#ifdef NDEBUG
5215#define ExprList_check_invariants(l)
5216#else
5217static void
5218ExprList_check_invariants(ExprList *l)
5219{
5220 /* Check our invariants. Make sure this object is "live", and
5221 hasn't been deallocated. */
5222 assert(l->size >= 0);
5223 assert(l->p != NULL);
5224 if (l->size <= EXPRLIST_N_CACHED)
5225 assert(l->data == l->p);
5226}
5227#endif
5228
5229static void
5230ExprList_Init(ExprList *l)
5231{
5232 l->allocated = EXPRLIST_N_CACHED;
5233 l->size = 0;
5234
5235 /* Until we start allocating dynamically, p points to data. */
5236 l->p = l->data;
5237
5238 ExprList_check_invariants(l);
5239}
5240
5241static int
5242ExprList_Append(ExprList *l, expr_ty exp)
5243{
5244 ExprList_check_invariants(l);
5245 if (l->size >= l->allocated) {
5246 /* We need to alloc (or realloc) the memory. */
5247 Py_ssize_t new_size = l->allocated * 2;
5248
5249 /* See if we've ever allocated anything dynamically. */
5250 if (l->p == l->data) {
5251 Py_ssize_t i;
5252 /* We're still using the cached data. Switch to
5253 alloc-ing. */
5254 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5255 if (!l->p)
5256 return -1;
5257 /* Copy the cached data into the new buffer. */
5258 for (i = 0; i < l->size; i++)
5259 l->p[i] = l->data[i];
5260 } else {
5261 /* Just realloc. */
5262 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5263 if (!tmp) {
5264 PyMem_RawFree(l->p);
5265 l->p = NULL;
5266 return -1;
5267 }
5268 l->p = tmp;
5269 }
5270
5271 l->allocated = new_size;
5272 assert(l->allocated == 2 * l->size);
5273 }
5274
5275 l->p[l->size++] = exp;
5276
5277 ExprList_check_invariants(l);
5278 return 0;
5279}
5280
5281static void
5282ExprList_Dealloc(ExprList *l)
5283{
5284 ExprList_check_invariants(l);
5285
5286 /* If there's been an error, or we've never dynamically allocated,
5287 do nothing. */
5288 if (!l->p || l->p == l->data) {
5289 /* Do nothing. */
5290 } else {
5291 /* We have dynamically allocated. Free the memory. */
5292 PyMem_RawFree(l->p);
5293 }
5294 l->p = NULL;
5295 l->size = -1;
5296}
5297
5298static asdl_seq *
5299ExprList_Finish(ExprList *l, PyArena *arena)
5300{
5301 asdl_seq *seq;
5302
5303 ExprList_check_invariants(l);
5304
5305 /* Allocate the asdl_seq and copy the expressions in to it. */
5306 seq = _Py_asdl_seq_new(l->size, arena);
5307 if (seq) {
5308 Py_ssize_t i;
5309 for (i = 0; i < l->size; i++)
5310 asdl_seq_SET(seq, i, l->p[i]);
5311 }
5312 ExprList_Dealloc(l);
5313 return seq;
5314}
5315
5316/* The FstringParser is designed to add a mix of strings and
5317 f-strings, and concat them together as needed. Ultimately, it
5318 generates an expr_ty. */
5319typedef struct {
5320 PyObject *last_str;
5321 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005322 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005323} FstringParser;
5324
5325#ifdef NDEBUG
5326#define FstringParser_check_invariants(state)
5327#else
5328static void
5329FstringParser_check_invariants(FstringParser *state)
5330{
5331 if (state->last_str)
5332 assert(PyUnicode_CheckExact(state->last_str));
5333 ExprList_check_invariants(&state->expr_list);
5334}
5335#endif
5336
5337static void
5338FstringParser_Init(FstringParser *state)
5339{
5340 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005341 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005342 ExprList_Init(&state->expr_list);
5343 FstringParser_check_invariants(state);
5344}
5345
5346static void
5347FstringParser_Dealloc(FstringParser *state)
5348{
5349 FstringParser_check_invariants(state);
5350
5351 Py_XDECREF(state->last_str);
5352 ExprList_Dealloc(&state->expr_list);
5353}
5354
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005355/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005356static expr_ty
5357make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5358{
5359 PyObject *s = *str;
5360 *str = NULL;
5361 assert(PyUnicode_CheckExact(s));
5362 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5363 Py_DECREF(s);
5364 return NULL;
5365 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005366 return Constant(s, LINENO(n), n->n_col_offset,
5367 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005368}
5369
5370/* Add a non-f-string (that is, a regular literal string). str is
5371 decref'd. */
5372static int
5373FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5374{
5375 FstringParser_check_invariants(state);
5376
5377 assert(PyUnicode_CheckExact(str));
5378
5379 if (PyUnicode_GET_LENGTH(str) == 0) {
5380 Py_DECREF(str);
5381 return 0;
5382 }
5383
5384 if (!state->last_str) {
5385 /* We didn't have a string before, so just remember this one. */
5386 state->last_str = str;
5387 } else {
5388 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005389 PyUnicode_AppendAndDel(&state->last_str, str);
5390 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005391 return -1;
5392 }
5393 FstringParser_check_invariants(state);
5394 return 0;
5395}
5396
Eric V. Smith451d0e32016-09-09 21:56:20 -04005397/* Parse an f-string. The f-string is in *str to end, with no
5398 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005399static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005400FstringParser_ConcatFstring(FstringParser *state, const char **str,
5401 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005402 struct compiling *c, const node *n)
5403{
5404 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005405 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005406
5407 /* Parse the f-string. */
5408 while (1) {
5409 PyObject *literal = NULL;
5410 expr_ty expression = NULL;
5411
5412 /* If there's a zero length literal in front of the
5413 expression, literal will be NULL. If we're at the end of
5414 the f-string, expression will be NULL (unless result == 1,
5415 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005416 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005417 &literal, &expression,
5418 c, n);
5419 if (result < 0)
5420 return -1;
5421
5422 /* Add the literal, if any. */
5423 if (!literal) {
5424 /* Do nothing. Just leave last_str alone (and possibly
5425 NULL). */
5426 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04005427 /* Note that the literal can be zero length, if the
5428 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005429 state->last_str = literal;
5430 literal = NULL;
5431 } else {
5432 /* We have a literal, concatenate it. */
5433 assert(PyUnicode_GET_LENGTH(literal) != 0);
5434 if (FstringParser_ConcatAndDel(state, literal) < 0)
5435 return -1;
5436 literal = NULL;
5437 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005438
5439 /* We've dealt with the literal now. It can't be leaked on further
5440 errors. */
5441 assert(literal == NULL);
5442
5443 /* See if we should just loop around to get the next literal
5444 and expression, while ignoring the expression this
5445 time. This is used for un-doubling braces, as an
5446 optimization. */
5447 if (result == 1)
5448 continue;
5449
5450 if (!expression)
5451 /* We're done with this f-string. */
5452 break;
5453
5454 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005455 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005456 if (!state->last_str) {
5457 /* Do nothing. No previous literal. */
5458 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005459 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005460 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5461 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5462 return -1;
5463 }
5464
5465 if (ExprList_Append(&state->expr_list, expression) < 0)
5466 return -1;
5467 }
5468
Eric V. Smith235a6f02015-09-19 14:51:32 -04005469 /* If recurse_lvl is zero, then we must be at the end of the
5470 string. Otherwise, we must be at a right brace. */
5471
Eric V. Smith451d0e32016-09-09 21:56:20 -04005472 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005473 ast_error(c, n, "f-string: unexpected end of string");
5474 return -1;
5475 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005476 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005477 ast_error(c, n, "f-string: expecting '}'");
5478 return -1;
5479 }
5480
5481 FstringParser_check_invariants(state);
5482 return 0;
5483}
5484
5485/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005486 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005487static expr_ty
5488FstringParser_Finish(FstringParser *state, struct compiling *c,
5489 const node *n)
5490{
5491 asdl_seq *seq;
5492
5493 FstringParser_check_invariants(state);
5494
5495 /* If we're just a constant string with no expressions, return
5496 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005497 if (!state->fmode) {
5498 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005499 if (!state->last_str) {
5500 /* Create a zero length string. */
5501 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5502 if (!state->last_str)
5503 goto error;
5504 }
5505 return make_str_node_and_del(&state->last_str, c, n);
5506 }
5507
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005508 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005509 last node in our expression list. */
5510 if (state->last_str) {
5511 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5512 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5513 goto error;
5514 }
5515 /* This has already been freed. */
5516 assert(state->last_str == NULL);
5517
5518 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5519 if (!seq)
5520 goto error;
5521
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005522 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5523 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005524
5525error:
5526 FstringParser_Dealloc(state);
5527 return NULL;
5528}
5529
Eric V. Smith451d0e32016-09-09 21:56:20 -04005530/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5531 at end, parse it into an expr_ty. Return NULL on error. Adjust
5532 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005533static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005534fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005535 struct compiling *c, const node *n)
5536{
5537 FstringParser state;
5538
5539 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005540 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005541 c, n) < 0) {
5542 FstringParser_Dealloc(&state);
5543 return NULL;
5544 }
5545
5546 return FstringParser_Finish(&state, c, n);
5547}
5548
5549/* n is a Python string literal, including the bracketing quote
5550 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005551 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005552 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005553 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5554 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005555*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005556static int
5557parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5558 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005559{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005560 size_t len;
5561 const char *s = STR(n);
5562 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005563 int fmode = 0;
5564 *bytesmode = 0;
5565 *rawmode = 0;
5566 *result = NULL;
5567 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005568 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005569 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005570 if (quote == 'b' || quote == 'B') {
5571 quote = *++s;
5572 *bytesmode = 1;
5573 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005574 else if (quote == 'u' || quote == 'U') {
5575 quote = *++s;
5576 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005577 else if (quote == 'r' || quote == 'R') {
5578 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005579 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005580 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005581 else if (quote == 'f' || quote == 'F') {
5582 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005583 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005584 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005585 else {
5586 break;
5587 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005588 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005589 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005590 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005591 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005592 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005593 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005594 if (quote != '\'' && quote != '\"') {
5595 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005596 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005597 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005598 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005599 s++;
5600 len = strlen(s);
5601 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005602 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005603 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005604 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005605 }
5606 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005607 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005608 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005609 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005610 }
5611 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005612 /* A triple quoted string. We've already skipped one quote at
5613 the start and one at the end of the string. Now skip the
5614 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005615 s += 2;
5616 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005617 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005618 if (s[--len] != quote || s[--len] != quote) {
5619 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005620 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005621 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005622 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005623
Eric V. Smith451d0e32016-09-09 21:56:20 -04005624 if (fmode) {
5625 /* Just return the bytes. The caller will parse the resulting
5626 string. */
5627 *fstr = s;
5628 *fstrlen = len;
5629 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005630 }
5631
Eric V. Smith451d0e32016-09-09 21:56:20 -04005632 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005633 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005634 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005635 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005636 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005637 const char *ch;
5638 for (ch = s; *ch; ch++) {
5639 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005640 ast_error(c, n,
5641 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005642 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005643 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005644 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005645 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005646 if (*rawmode)
5647 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005648 else
Eric V. Smith56466482016-10-31 14:46:26 -04005649 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005650 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005651 if (*rawmode)
5652 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005653 else
Eric V. Smith56466482016-10-31 14:46:26 -04005654 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005655 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005656 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005657}
5658
Eric V. Smith235a6f02015-09-19 14:51:32 -04005659/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5660 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005661 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005662 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005663 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005664 node if there's just an f-string (with no leading or trailing
5665 literals), or a JoinedStr node if there are multiple f-strings or
5666 any literals involved. */
5667static expr_ty
5668parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005669{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005670 int bytesmode = 0;
5671 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005672 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005673
5674 FstringParser state;
5675 FstringParser_Init(&state);
5676
5677 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005678 int this_bytesmode;
5679 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005680 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005681 const char *fstr;
5682 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005683
5684 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005685 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5686 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005687 goto error;
5688
5689 /* Check that we're not mixing bytes with unicode. */
5690 if (i != 0 && bytesmode != this_bytesmode) {
5691 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005692 /* s is NULL if the current string part is an f-string. */
5693 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005694 goto error;
5695 }
5696 bytesmode = this_bytesmode;
5697
Eric V. Smith451d0e32016-09-09 21:56:20 -04005698 if (fstr != NULL) {
5699 int result;
5700 assert(s == NULL && !bytesmode);
5701 /* This is an f-string. Parse and concatenate it. */
5702 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5703 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005704 if (result < 0)
5705 goto error;
5706 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005707 /* A string or byte string. */
5708 assert(s != NULL && fstr == NULL);
5709
Eric V. Smith451d0e32016-09-09 21:56:20 -04005710 assert(bytesmode ? PyBytes_CheckExact(s) :
5711 PyUnicode_CheckExact(s));
5712
Eric V. Smith451d0e32016-09-09 21:56:20 -04005713 if (bytesmode) {
5714 /* For bytes, concat as we go. */
5715 if (i == 0) {
5716 /* First time, just remember this value. */
5717 bytes_str = s;
5718 } else {
5719 PyBytes_ConcatAndDel(&bytes_str, s);
5720 if (!bytes_str)
5721 goto error;
5722 }
5723 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005724 /* This is a regular string. Concatenate it. */
5725 if (FstringParser_ConcatAndDel(&state, s) < 0)
5726 goto error;
5727 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005728 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005729 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005730 if (bytesmode) {
5731 /* Just return the bytes object and we're done. */
5732 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5733 goto error;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005734 return Constant(bytes_str, LINENO(n), n->n_col_offset,
5735 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005736 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005737
Eric V. Smith235a6f02015-09-19 14:51:32 -04005738 /* We're not a bytes string, bytes_str should never have been set. */
5739 assert(bytes_str == NULL);
5740
5741 return FstringParser_Finish(&state, c, n);
5742
5743error:
5744 Py_XDECREF(bytes_str);
5745 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005746 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005747}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005748
5749PyObject *
5750_PyAST_GetDocString(asdl_seq *body)
5751{
5752 if (!asdl_seq_LEN(body)) {
5753 return NULL;
5754 }
5755 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5756 if (st->kind != Expr_kind) {
5757 return NULL;
5758 }
5759 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005760 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5761 return e->v.Constant.value;
5762 }
5763 return NULL;
5764}