1 /* This code is generated by conversiongenerator.py.
2  * I do not recommend editing it.
3  * To update, run: python2 conversionGenerator.py > generated.h
4  */
5 
6 #include <QStack>
7 #include "kdevpythonversion.h"
8 
9 class PythonAstTransformer {
10 public:
11     CodeAst* ast;
run(mod_ty syntaxtree,QString moduleName)12     void run(mod_ty syntaxtree, QString moduleName) {
13         ast = new CodeAst();
14         ast->name = new Identifier(moduleName);
15         nodeStack.push(ast);
16         ast->body = visitNodeList<_stmt, Ast>(syntaxtree->v.Module.body);
17         nodeStack.pop();
18         Q_ASSERT(nodeStack.isEmpty());
19     }
20     // Shift lines by some fixed amount
tline(int line)21     inline int tline(int line) {
22         if ( line == -99999 ) {
23             // don't touch the marker
24             return -99999;
25         }
26         return line;
27     };
28 private:
29     QStack<Ast*> nodeStack;
30 
parent()31     Ast* parent() {
32         return nodeStack.top();
33     }
34 
visitNodeList(asdl_seq * node)35     template<typename T, typename K> QList<K*> visitNodeList(asdl_seq* node) {
36         QList<K*> nodelist;
37         if ( ! node ) return nodelist;
38         for ( int i=0; i < node->size; i++ ) {
39             T* currentNode = static_cast<T*>(node->elements[i]);
40             Ast* result = visitNode(currentNode);
41             K* transformedNode = static_cast<K*>(result);
42             nodelist.append(transformedNode);
43         }
44         return nodelist;
45     }
46 
47 
48 
visitNode(_alias * node)49     Ast* visitNode(_alias* node) {
50         bool ranges_copied = false; Q_UNUSED(ranges_copied);
51         if ( ! node ) return nullptr;
52                 AliasAst* v = new  AliasAst(parent());
53             v->name = node->name ? new Python::Identifier(PyUnicodeObjectToQString(node->name)) : nullptr;
54             v->asName = node->asname ? new Python::Identifier(PyUnicodeObjectToQString(node->asname)) : nullptr;
55         return v;
56     }
57 
58 
visitNode(_arg * node)59     Ast* visitNode(_arg* node) {
60         bool ranges_copied = false; Q_UNUSED(ranges_copied);
61         if ( ! node ) return nullptr;
62                 ArgAst* v = new  ArgAst(parent());
63             v->argumentName = node->arg ? new Python::Identifier(PyUnicodeObjectToQString(node->arg)) : nullptr;
64                 if ( v->argumentName ) {
65                     v->argumentName->startCol = node->col_offset; v->startCol = v->argumentName->startCol;
66                     v->argumentName->startLine = tline(node->lineno - 1);  v->startLine = v->argumentName->startLine;
67                     v->argumentName->endCol = node->col_offset + v->argumentName->value.length() - 1;  v->endCol = v->argumentName->endCol;
68                     v->argumentName->endLine = tline(node->lineno - 1);  v->endLine = v->argumentName->endLine;
69                     ranges_copied = true;
70                 }
71             nodeStack.push(v); v->annotation = static_cast<ExpressionAst*>(visitNode(node->annotation)); nodeStack.pop();
72         return v;
73     }
74 
75 
visitNode(_arguments * node)76     Ast* visitNode(_arguments* node) {
77         bool ranges_copied = false; Q_UNUSED(ranges_copied);
78         if ( ! node ) return nullptr;
79 #if PYTHON_VERSION < QT_VERSION_CHECK(3, 8, 0)
80                 ArgumentsAst* v = new  ArgumentsAst(parent());
81             nodeStack.push(v); v->vararg = static_cast<ArgAst*>(visitNode(node->vararg)); nodeStack.pop();
82             nodeStack.push(v); v->kwarg = static_cast<ArgAst*>(visitNode(node->kwarg)); nodeStack.pop();
83             nodeStack.push(v); v->arguments = visitNodeList<_arg, ArgAst>(node->args); nodeStack.pop();
84             nodeStack.push(v); v->defaultValues = visitNodeList<_expr, ExpressionAst>(node->defaults); nodeStack.pop();
85             nodeStack.push(v); v->kwonlyargs = visitNodeList<_arg, ArgAst>(node->kwonlyargs); nodeStack.pop();
86 #endif
87 #if PYTHON_VERSION >= QT_VERSION_CHECK(3, 8, 0)
88                 ArgumentsAst* v = new  ArgumentsAst(parent());
89             nodeStack.push(v); v->vararg = static_cast<ArgAst*>(visitNode(node->vararg)); nodeStack.pop();
90             nodeStack.push(v); v->kwarg = static_cast<ArgAst*>(visitNode(node->kwarg)); nodeStack.pop();
91             nodeStack.push(v); v->arguments = visitNodeList<_arg, ArgAst>(node->args); nodeStack.pop();
92             nodeStack.push(v); v->defaultValues = visitNodeList<_expr, ExpressionAst>(node->defaults); nodeStack.pop();
93             nodeStack.push(v); v->kwonlyargs = visitNodeList<_arg, ArgAst>(node->kwonlyargs); nodeStack.pop();
94             nodeStack.push(v); v->posonlyargs = visitNodeList<_arg, ArgAst>(node->posonlyargs); nodeStack.pop();
95 #endif
96         return v;
97     }
98 
99 
visitNode(_comprehension * node)100     Ast* visitNode(_comprehension* node) {
101         bool ranges_copied = false; Q_UNUSED(ranges_copied);
102         if ( ! node ) return nullptr;
103                 ComprehensionAst* v = new  ComprehensionAst(parent());
104             nodeStack.push(v); v->target = static_cast<ExpressionAst*>(visitNode(node->target)); nodeStack.pop();
105             nodeStack.push(v); v->iterator = static_cast<ExpressionAst*>(visitNode(node->iter)); nodeStack.pop();
106             nodeStack.push(v); v->conditions = visitNodeList<_expr, ExpressionAst>(node->ifs); nodeStack.pop();
107         return v;
108     }
109 
110 
visitNode(_excepthandler * node)111     Ast* visitNode(_excepthandler* node) {
112         if ( ! node ) return nullptr;
113         bool ranges_copied = false; Q_UNUSED(ranges_copied);
114         Ast* result = nullptr;
115         switch ( node->kind ) {
116         case ExceptHandler_kind: {
117                 ExceptionHandlerAst* v = new  ExceptionHandlerAst(parent());
118                 nodeStack.push(v); v->type = static_cast<ExpressionAst*>(visitNode(node->v.ExceptHandler.type)); nodeStack.pop();
119                 v->name = node->v.ExceptHandler.name ? new Python::Identifier(PyUnicodeObjectToQString(node->v.ExceptHandler.name)) : nullptr;
120                 if ( v->name ) {
121                     v->name->startCol = node->col_offset; v->startCol = v->name->startCol;
122                     v->name->startLine = tline(node->lineno - 1);  v->startLine = v->name->startLine;
123                     v->name->endCol = node->col_offset + v->name->value.length() - 1;  v->endCol = v->name->endCol;
124                     v->name->endLine = tline(node->lineno - 1);  v->endLine = v->name->endLine;
125                     ranges_copied = true;
126                 }
127                 nodeStack.push(v); v->body = visitNodeList<_stmt, Ast>(node->v.ExceptHandler.body); nodeStack.pop();
128                 result = v;
129                 break;
130             }
131         default:
132             qWarning() << "Unsupported _excepthandler AST type: " << node->kind;
133             Q_ASSERT(false);
134         }
135 
136         // Walk through the tree and set proper end columns and lines, as the python parser sadly does not do this for us
137         if ( result->hasUsefulRangeInformation ) {
138             Ast* parent = result->parent;
139             while ( parent ) {
140                 if ( parent->endLine < result->endLine ) {
141                     parent->endLine = result->endLine;
142                     parent->endCol = result->endCol;
143                 }
144                 if ( ! parent->hasUsefulRangeInformation && parent->startLine == -99999 ) {
145                     parent->startLine = result->startLine;
146                     parent->startCol = result->startCol;
147                 }
148                 parent = parent->parent;
149             }
150         }
151 
152         if ( result && result->astType == Ast::NameAstType ) {
153             NameAst* r = static_cast<NameAst*>(result);
154             r->startCol = r->identifier->startCol;
155             r->endCol = r->identifier->endCol;
156             r->startLine = r->identifier->startLine;
157             r->endLine = r->identifier->endLine;
158         }
159         return result;
160     }
161 
162 
visitNode(_expr * node)163     Ast* visitNode(_expr* node) {
164         if ( ! node ) return nullptr;
165         bool ranges_copied = false; Q_UNUSED(ranges_copied);
166         Ast* result = nullptr;
167         switch ( node->kind ) {
168 #if PYTHON_VERSION >= QT_VERSION_CHECK(3, 5, 0)
169         case Await_kind: {
170                 AwaitAst* v = new  AwaitAst(parent());
171                 nodeStack.push(v); v->value = static_cast<ExpressionAst*>(visitNode(node->v.Await.value)); nodeStack.pop();
172                 result = v;
173                 break;
174             }
175 #endif
176         case BoolOp_kind: {
177                 BooleanOperationAst* v = new  BooleanOperationAst(parent());
178                 v->type = (ExpressionAst::BooleanOperationTypes) node->v.BoolOp.op;
179                 nodeStack.push(v); v->values = visitNodeList<_expr, ExpressionAst>(node->v.BoolOp.values); nodeStack.pop();
180                 result = v;
181                 break;
182             }
183         case BinOp_kind: {
184                 BinaryOperationAst* v = new  BinaryOperationAst(parent());
185                 v->type = (ExpressionAst::OperatorTypes) node->v.BinOp.op;
186                 nodeStack.push(v); v->lhs = static_cast<ExpressionAst*>(visitNode(node->v.BinOp.left)); nodeStack.pop();
187                 nodeStack.push(v); v->rhs = static_cast<ExpressionAst*>(visitNode(node->v.BinOp.right)); nodeStack.pop();
188                 result = v;
189                 break;
190             }
191         case UnaryOp_kind: {
192                 UnaryOperationAst* v = new  UnaryOperationAst(parent());
193                 v->type = (ExpressionAst::UnaryOperatorTypes) node->v.UnaryOp.op;
194                 nodeStack.push(v); v->operand = static_cast<ExpressionAst*>(visitNode(node->v.UnaryOp.operand)); nodeStack.pop();
195                 result = v;
196                 break;
197             }
198         case Lambda_kind: {
199                 LambdaAst* v = new  LambdaAst(parent());
200                 nodeStack.push(v); v->arguments = static_cast<ArgumentsAst*>(visitNode(node->v.Lambda.args)); nodeStack.pop();
201                 nodeStack.push(v); v->body = static_cast<ExpressionAst*>(visitNode(node->v.Lambda.body)); nodeStack.pop();
202                 result = v;
203                 break;
204             }
205         case IfExp_kind: {
206                 IfExpressionAst* v = new  IfExpressionAst(parent());
207                 nodeStack.push(v); v->condition = static_cast<ExpressionAst*>(visitNode(node->v.IfExp.test)); nodeStack.pop();
208                 nodeStack.push(v); v->body = static_cast<ExpressionAst*>(visitNode(node->v.IfExp.body)); nodeStack.pop();
209                 nodeStack.push(v); v->orelse = static_cast<ExpressionAst*>(visitNode(node->v.IfExp.orelse)); nodeStack.pop();
210                 result = v;
211                 break;
212             }
213         case Dict_kind: {
214                 DictAst* v = new  DictAst(parent());
215                 nodeStack.push(v); v->keys = visitNodeList<_expr, ExpressionAst>(node->v.Dict.keys); nodeStack.pop();
216                 nodeStack.push(v); v->values = visitNodeList<_expr, ExpressionAst>(node->v.Dict.values); nodeStack.pop();
217                 result = v;
218                 break;
219             }
220         case Set_kind: {
221                 SetAst* v = new  SetAst(parent());
222                 nodeStack.push(v); v->elements = visitNodeList<_expr, ExpressionAst>(node->v.Set.elts); nodeStack.pop();
223                 result = v;
224                 break;
225             }
226         case ListComp_kind: {
227                 ListComprehensionAst* v = new  ListComprehensionAst(parent());
228                 nodeStack.push(v); v->element = static_cast<ExpressionAst*>(visitNode(node->v.ListComp.elt)); nodeStack.pop();
229                 nodeStack.push(v); v->generators = visitNodeList<_comprehension, ComprehensionAst>(node->v.ListComp.generators); nodeStack.pop();
230                 result = v;
231                 break;
232             }
233         case SetComp_kind: {
234                 SetComprehensionAst* v = new  SetComprehensionAst(parent());
235                 nodeStack.push(v); v->element = static_cast<ExpressionAst*>(visitNode(node->v.SetComp.elt)); nodeStack.pop();
236                 nodeStack.push(v); v->generators = visitNodeList<_comprehension, ComprehensionAst>(node->v.SetComp.generators); nodeStack.pop();
237                 result = v;
238                 break;
239             }
240         case DictComp_kind: {
241                 DictionaryComprehensionAst* v = new  DictionaryComprehensionAst(parent());
242                 nodeStack.push(v); v->key = static_cast<ExpressionAst*>(visitNode(node->v.DictComp.key)); nodeStack.pop();
243                 nodeStack.push(v); v->value = static_cast<ExpressionAst*>(visitNode(node->v.DictComp.value)); nodeStack.pop();
244                 nodeStack.push(v); v->generators = visitNodeList<_comprehension, ComprehensionAst>(node->v.DictComp.generators); nodeStack.pop();
245                 result = v;
246                 break;
247             }
248         case GeneratorExp_kind: {
249                 GeneratorExpressionAst* v = new  GeneratorExpressionAst(parent());
250                 nodeStack.push(v); v->element = static_cast<ExpressionAst*>(visitNode(node->v.GeneratorExp.elt)); nodeStack.pop();
251                 nodeStack.push(v); v->generators = visitNodeList<_comprehension, ComprehensionAst>(node->v.GeneratorExp.generators); nodeStack.pop();
252                 result = v;
253                 break;
254             }
255         case Yield_kind: {
256                 YieldAst* v = new  YieldAst(parent());
257                 nodeStack.push(v); v->value = static_cast<ExpressionAst*>(visitNode(node->v.Yield.value)); nodeStack.pop();
258                 result = v;
259                 break;
260             }
261         case Compare_kind: {
262                 CompareAst* v = new  CompareAst(parent());
263                 nodeStack.push(v); v->leftmostElement = static_cast<ExpressionAst*>(visitNode(node->v.Compare.left)); nodeStack.pop();
264 
265                 for ( int _i = 0; _i < node->v.Compare.ops->size; _i++ ) {
266                     v->operators.append((ExpressionAst::ComparisonOperatorTypes) node->v.Compare.ops->elements[_i]);
267                 }
268 
269                 nodeStack.push(v); v->comparands = visitNodeList<_expr, ExpressionAst>(node->v.Compare.comparators); nodeStack.pop();
270                 result = v;
271                 break;
272             }
273 #if PYTHON_VERSION >= QT_VERSION_CHECK(3, 5, 0)
274         case Call_kind: {
275                 CallAst* v = new  CallAst(parent());
276                 nodeStack.push(v); v->function = static_cast<ExpressionAst*>(visitNode(node->v.Call.func)); nodeStack.pop();
277                 nodeStack.push(v); v->arguments = visitNodeList<_expr, ExpressionAst>(node->v.Call.args); nodeStack.pop();
278                 nodeStack.push(v); v->keywords = visitNodeList<_keyword, KeywordAst>(node->v.Call.keywords); nodeStack.pop();
279                 result = v;
280                 break;
281             }
282 #endif
283 #if PYTHON_VERSION < QT_VERSION_CHECK(3, 5, 0)
284         case Call_kind: {
285                 CallAst* v = new  CallAst(parent());
286                 nodeStack.push(v); v->function = static_cast<ExpressionAst*>(visitNode(node->v.Call.func)); nodeStack.pop();
287                 nodeStack.push(v); v->arguments = visitNodeList<_expr, ExpressionAst>(node->v.Call.args); nodeStack.pop();
288                 nodeStack.push(v); v->keywords = visitNodeList<_keyword, KeywordAst>(node->v.Call.keywords); nodeStack.pop();
289  /* Convert 3.4 unpacked-args AST to match the new format from 3.5+ */if (node->v.Call.starargs) {    nodeStack.push(v);    auto starred = new StarredAst(v);    starred->context = ExpressionAst::Context::Load;    nodeStack.push(starred);    starred->value = static_cast<ExpressionAst*>(visitNode(node->v.Call.starargs));    nodeStack.pop();    v->arguments.append(starred);    nodeStack.pop();};if (node->v.Call.kwargs) {    nodeStack.push(v);    auto kwargs = new KeywordAst(v);    nodeStack.push(kwargs);    kwargs->value = static_cast<ExpressionAst*>(visitNode(node->v.Call.kwargs));    nodeStack.pop();    v->keywords.append(kwargs);    nodeStack.pop();};
290                 result = v;
291                 break;
292             }
293 #endif
294 #if PYTHON_VERSION < QT_VERSION_CHECK(3, 8, 0)
295         case Num_kind: {
296                 NumberAst* v = new  NumberAst(parent());
297  v->isInt = PyLong_Check(node->v.Num.n); v->value = PyLong_AsLong(node->v.Num.n);
298                 result = v;
299                 break;
300             }
301 #endif
302 #if PYTHON_VERSION < QT_VERSION_CHECK(3, 8, 0)
303         case Str_kind: {
304                 StringAst* v = new  StringAst(parent());
305                 v->value = PyUnicodeObjectToQString(node->v.Str.s);
306                 result = v;
307                 break;
308             }
309 #endif
310 #if PYTHON_VERSION >= QT_VERSION_CHECK(3, 6, 0)
311         case JoinedStr_kind: {
312                 JoinedStringAst* v = new  JoinedStringAst(parent());
313                 nodeStack.push(v); v->values = visitNodeList<_expr, ExpressionAst>(node->v.JoinedStr.values); nodeStack.pop();
314                 result = v;
315                 break;
316             }
317 #endif
318 #if PYTHON_VERSION >= QT_VERSION_CHECK(3, 6, 0)
319         case FormattedValue_kind: {
320                 FormattedValueAst* v = new  FormattedValueAst(parent());
321                 nodeStack.push(v); v->value = static_cast<ExpressionAst*>(visitNode(node->v.FormattedValue.value)); nodeStack.pop();
322                 v->conversion = node->v.FormattedValue.conversion;
323                 nodeStack.push(v); v->formatSpec = static_cast<ExpressionAst*>(visitNode(node->v.FormattedValue.format_spec)); nodeStack.pop();
324                 result = v;
325                 break;
326             }
327 #endif
328 #if PYTHON_VERSION < QT_VERSION_CHECK(3, 8, 0)
329         case Bytes_kind: {
330                 BytesAst* v = new  BytesAst(parent());
331                 v->value = PyUnicodeObjectToQString(node->v.Bytes.s);
332                 result = v;
333                 break;
334             }
335 #endif
336         case Attribute_kind: {
337                 AttributeAst* v = new  AttributeAst(parent());
338                 v->attribute = node->v.Attribute.attr ? new Python::Identifier(PyUnicodeObjectToQString(node->v.Attribute.attr)) : nullptr;
339                 if ( v->attribute ) {
340                     v->attribute->startCol = node->col_offset; v->startCol = v->attribute->startCol;
341                     v->attribute->startLine = tline(node->lineno - 1);  v->startLine = v->attribute->startLine;
342                     v->attribute->endCol = node->col_offset + v->attribute->value.length() - 1;  v->endCol = v->attribute->endCol;
343                     v->attribute->endLine = tline(node->lineno - 1);  v->endLine = v->attribute->endLine;
344                     ranges_copied = true;
345                 }
346                 nodeStack.push(v); v->value = static_cast<ExpressionAst*>(visitNode(node->v.Attribute.value)); nodeStack.pop();
347                 v->context = (ExpressionAst::Context) node->v.Attribute.ctx;
348                 result = v;
349                 break;
350             }
351         case Subscript_kind: {
352                 SubscriptAst* v = new  SubscriptAst(parent());
353                 nodeStack.push(v); v->value = static_cast<ExpressionAst*>(visitNode(node->v.Subscript.value)); nodeStack.pop();
354                 nodeStack.push(v); v->slice = static_cast<SliceAst*>(visitNode(node->v.Subscript.slice)); nodeStack.pop();
355                 v->context = (ExpressionAst::Context) node->v.Subscript.ctx;
356                 result = v;
357                 break;
358             }
359         case Starred_kind: {
360                 StarredAst* v = new  StarredAst(parent());
361                 nodeStack.push(v); v->value = static_cast<ExpressionAst*>(visitNode(node->v.Starred.value)); nodeStack.pop();
362                 v->context = (ExpressionAst::Context) node->v.Starred.ctx;
363                 result = v;
364                 break;
365             }
366         case Name_kind: {
367                 NameAst* v = new  NameAst(parent());
368                 v->identifier = node->v.Name.id ? new Python::Identifier(PyUnicodeObjectToQString(node->v.Name.id)) : nullptr;
369                 if ( v->identifier ) {
370                     v->identifier->startCol = node->col_offset; v->startCol = v->identifier->startCol;
371                     v->identifier->startLine = tline(node->lineno - 1);  v->startLine = v->identifier->startLine;
372                     v->identifier->endCol = node->col_offset + v->identifier->value.length() - 1;  v->endCol = v->identifier->endCol;
373                     v->identifier->endLine = tline(node->lineno - 1);  v->endLine = v->identifier->endLine;
374                     ranges_copied = true;
375                 }
376                 v->context = (ExpressionAst::Context) node->v.Name.ctx;
377                 result = v;
378                 break;
379             }
380         case List_kind: {
381                 ListAst* v = new  ListAst(parent());
382                 nodeStack.push(v); v->elements = visitNodeList<_expr, ExpressionAst>(node->v.List.elts); nodeStack.pop();
383                 v->context = (ExpressionAst::Context) node->v.List.ctx;
384                 result = v;
385                 break;
386             }
387         case Tuple_kind: {
388                 TupleAst* v = new  TupleAst(parent());
389                 nodeStack.push(v); v->elements = visitNodeList<_expr, ExpressionAst>(node->v.Tuple.elts); nodeStack.pop();
390                 v->context = (ExpressionAst::Context) node->v.Tuple.ctx;
391                 result = v;
392                 break;
393             }
394 #if PYTHON_VERSION < QT_VERSION_CHECK(3, 8, 0)
395         case Ellipsis_kind: {
396                 EllipsisAst* v = new  EllipsisAst(parent());
397                 result = v;
398                 break;
399             }
400 #endif
401 #if PYTHON_VERSION < QT_VERSION_CHECK(3, 8, 0)
402         case NameConstant_kind: {
403                 NameConstantAst* v = new  NameConstantAst(parent());
404                 v->value = node->v.NameConstant.value == Py_None ? NameConstantAst::None : node->v.NameConstant.value == Py_False ? NameConstantAst::False : NameConstantAst::True;
405                 result = v;
406                 break;
407             }
408 #endif
409         case YieldFrom_kind: {
410                 YieldFromAst* v = new  YieldFromAst(parent());
411                 nodeStack.push(v); v->value = static_cast<ExpressionAst*>(visitNode(node->v.YieldFrom.value)); nodeStack.pop();
412                 result = v;
413                 break;
414             }
415 #if PYTHON_VERSION >= QT_VERSION_CHECK(3, 8, 0)
416         case Constant_kind: {
417 PyObject *value = node->v.Constant.value;if (value == Py_None) {    NameConstantAst* v = new NameConstantAst(parent());    v->value = NameConstantAst::None;    result = v;}else if (value == Py_True) {    NameConstantAst* v = new  NameConstantAst(parent());    v->value = NameConstantAst::True;    result = v;}else if (value == Py_False) {    NameConstantAst* v = new  NameConstantAst(parent());    v->value = NameConstantAst::False;    result = v;}else if (value->ob_type == &PyLong_Type) {    NumberAst* v = new NumberAst(parent());    v->isInt = true;    v->value = PyLong_AsLong(value);    result = v;}else if (value->ob_type == &PyFloat_Type || value->ob_type == &PyComplex_Type) {    result = new NumberAst(parent());}else if (value->ob_type == &PyUnicode_Type) {    StringAst* v = new StringAst(parent());    v->value = PyUnicodeObjectToQString(value);    result = v;}else if (value->ob_type == &PyBytes_Type) {    result = new BytesAst(parent());}else if (value->ob_type == &PyEllipsis_Type) {    result = new EllipsisAst(parent());}else {    qWarning() << "Unhandled constant type: " << value->ob_type->tp_name;    Q_ASSERT(false);};
418                 break;
419             }
420 #endif
421 #if PYTHON_VERSION >= QT_VERSION_CHECK(3, 8, 0)
422         case NamedExpr_kind: {
423                 AssignmentExpressionAst* v = new  AssignmentExpressionAst(parent());
424                 nodeStack.push(v); v->target = static_cast<ExpressionAst*>(visitNode(node->v.NamedExpr.target)); nodeStack.pop();
425                 nodeStack.push(v); v->value = static_cast<ExpressionAst*>(visitNode(node->v.NamedExpr.value)); nodeStack.pop();
426                 result = v;
427                 break;
428             }
429 #endif
430 #if PYTHON_VERSION >= QT_VERSION_CHECK(3, 9, 0)
431         case Slice_kind: {
432                 SliceAst* v = new  SliceAst(parent());
433                 nodeStack.push(v); v->lower = static_cast<ExpressionAst*>(visitNode(node->v.Slice.lower)); nodeStack.pop();
434                 nodeStack.push(v); v->upper = static_cast<ExpressionAst*>(visitNode(node->v.Slice.upper)); nodeStack.pop();
435                 nodeStack.push(v); v->step = static_cast<ExpressionAst*>(visitNode(node->v.Slice.step)); nodeStack.pop();
436                 result = v;
437                 break;
438             }
439 #endif
440         default:
441             qWarning() << "Unsupported _expr AST type: " << node->kind;
442             Q_ASSERT(false);
443         }
444 
445 	if ( ! result ) return nullptr;
446         if ( ! ranges_copied ) {
447             result->startCol = node->col_offset;
448             result->endCol = node->col_offset;
449             result->startLine = tline(node->lineno - 1);
450             result->endLine = tline(node->lineno - 1);
451             result->hasUsefulRangeInformation = true;
452         }
453         else {
454             result->hasUsefulRangeInformation = true;
455         }
456 
457         // Walk through the tree and set proper end columns and lines, as the python parser sadly does not do this for us
458         if ( result->hasUsefulRangeInformation ) {
459             Ast* parent = result->parent;
460             while ( parent ) {
461                 if ( parent->endLine < result->endLine ) {
462                     parent->endLine = result->endLine;
463                     parent->endCol = result->endCol;
464                 }
465                 if ( ! parent->hasUsefulRangeInformation && parent->startLine == -99999 ) {
466                     parent->startLine = result->startLine;
467                     parent->startCol = result->startCol;
468                 }
469                 parent = parent->parent;
470             }
471         }
472 
473         if ( result && result->astType == Ast::NameAstType ) {
474             NameAst* r = static_cast<NameAst*>(result);
475             r->startCol = r->identifier->startCol;
476             r->endCol = r->identifier->endCol;
477             r->startLine = r->identifier->startLine;
478             r->endLine = r->identifier->endLine;
479         }
480         return result;
481     }
482 
483 
visitNode(_keyword * node)484     Ast* visitNode(_keyword* node) {
485         bool ranges_copied = false; Q_UNUSED(ranges_copied);
486         if ( ! node ) return nullptr;
487                 KeywordAst* v = new  KeywordAst(parent());
488             v->argumentName = node->arg ? new Python::Identifier(PyUnicodeObjectToQString(node->arg)) : nullptr;
489             nodeStack.push(v); v->value = static_cast<ExpressionAst*>(visitNode(node->value)); nodeStack.pop();
490         return v;
491     }
492 
493 #if PYTHON_VERSION < QT_VERSION_CHECK(3, 9, 0)
494 
visitNode(_slice * node)495     Ast* visitNode(_slice* node) {
496         if ( ! node ) return nullptr;
497         bool ranges_copied = false; Q_UNUSED(ranges_copied);
498         Ast* result = nullptr;
499         switch ( node->kind ) {
500 #if PYTHON_VERSION < QT_VERSION_CHECK(3, 9, 0)
501         case Slice_kind: {
502                 SliceAst* v = new  SliceAst(parent());
503                 nodeStack.push(v); v->lower = static_cast<ExpressionAst*>(visitNode(node->v.Slice.lower)); nodeStack.pop();
504                 nodeStack.push(v); v->upper = static_cast<ExpressionAst*>(visitNode(node->v.Slice.upper)); nodeStack.pop();
505                 nodeStack.push(v); v->step = static_cast<ExpressionAst*>(visitNode(node->v.Slice.step)); nodeStack.pop();
506                 result = v;
507                 break;
508             }
509 #endif
510 #if PYTHON_VERSION < QT_VERSION_CHECK(3, 9, 0)
511         case ExtSlice_kind: {
512                 TupleAst* v = new  TupleAst(parent());
513                 nodeStack.push(v); v->elements = visitNodeList<_slice, ExpressionAst>(node->v.ExtSlice.dims); nodeStack.pop();
514                 result = v;
515                 break;
516             }
517 #endif
518 #if PYTHON_VERSION < QT_VERSION_CHECK(3, 9, 0)
519         case Index_kind: {
520  return visitNode(node->v.Index.value);
521                 break;
522             }
523 #endif
524         default:
525             qWarning() << "Unsupported _slice AST type: " << node->kind;
526             Q_ASSERT(false);
527         }
528 
529         // Walk through the tree and set proper end columns and lines, as the python parser sadly does not do this for us
530         if ( result->hasUsefulRangeInformation ) {
531             Ast* parent = result->parent;
532             while ( parent ) {
533                 if ( parent->endLine < result->endLine ) {
534                     parent->endLine = result->endLine;
535                     parent->endCol = result->endCol;
536                 }
537                 if ( ! parent->hasUsefulRangeInformation && parent->startLine == -99999 ) {
538                     parent->startLine = result->startLine;
539                     parent->startCol = result->startCol;
540                 }
541                 parent = parent->parent;
542             }
543         }
544 
545         if ( result && result->astType == Ast::NameAstType ) {
546             NameAst* r = static_cast<NameAst*>(result);
547             r->startCol = r->identifier->startCol;
548             r->endCol = r->identifier->endCol;
549             r->startLine = r->identifier->startLine;
550             r->endLine = r->identifier->endLine;
551         }
552         return result;
553     }
554 
555 #endif
556 
557 
visitNode(_stmt * node)558     Ast* visitNode(_stmt* node) {
559         if ( ! node ) return nullptr;
560         bool ranges_copied = false; Q_UNUSED(ranges_copied);
561         Ast* result = nullptr;
562         switch ( node->kind ) {
563         case Expr_kind: {
564                 ExpressionAst* v = new  ExpressionAst(parent());
565                 nodeStack.push(v); v->value = static_cast<ExpressionAst*>(visitNode(node->v.Expr.value)); nodeStack.pop();
566                 result = v;
567                 break;
568             }
569         case FunctionDef_kind: {
570                 FunctionDefinitionAst* v = new  FunctionDefinitionAst(parent());
571                 v->name = node->v.FunctionDef.name ? new Python::Identifier(PyUnicodeObjectToQString(node->v.FunctionDef.name)) : nullptr;
572                 if ( v->name ) {
573                     v->name->startCol = node->col_offset; v->startCol = v->name->startCol;
574                     v->name->startLine = tline(node->lineno - 1);  v->startLine = v->name->startLine;
575                     v->name->endCol = node->col_offset + v->name->value.length() - 1;  v->endCol = v->name->endCol;
576                     v->name->endLine = tline(node->lineno - 1);  v->endLine = v->name->endLine;
577                     ranges_copied = true;
578                 }
579                 nodeStack.push(v); v->arguments = static_cast<ArgumentsAst*>(visitNode(node->v.FunctionDef.args)); nodeStack.pop();
580                 nodeStack.push(v); v->body = visitNodeList<_stmt, Ast>(node->v.FunctionDef.body); nodeStack.pop();
581                 nodeStack.push(v); v->decorators = visitNodeList<_expr, ExpressionAst>(node->v.FunctionDef.decorator_list); nodeStack.pop();
582                 nodeStack.push(v); v->returns = static_cast<ExpressionAst*>(visitNode(node->v.FunctionDef.returns)); nodeStack.pop();
583                 result = v;
584                 break;
585             }
586 #if PYTHON_VERSION >= QT_VERSION_CHECK(3, 5, 0)
587         case AsyncFunctionDef_kind: {
588                 FunctionDefinitionAst* v = new  FunctionDefinitionAst(parent());
589                 v->name = node->v.AsyncFunctionDef.name ? new Python::Identifier(PyUnicodeObjectToQString(node->v.AsyncFunctionDef.name)) : nullptr;
590                 if ( v->name ) {
591                     v->name->startCol = node->col_offset; v->startCol = v->name->startCol;
592                     v->name->startLine = tline(node->lineno - 1);  v->startLine = v->name->startLine;
593                     v->name->endCol = node->col_offset + v->name->value.length() - 1;  v->endCol = v->name->endCol;
594                     v->name->endLine = tline(node->lineno - 1);  v->endLine = v->name->endLine;
595                     ranges_copied = true;
596                 }
597                 nodeStack.push(v); v->arguments = static_cast<ArgumentsAst*>(visitNode(node->v.AsyncFunctionDef.args)); nodeStack.pop();
598                 nodeStack.push(v); v->body = visitNodeList<_stmt, Ast>(node->v.AsyncFunctionDef.body); nodeStack.pop();
599                 nodeStack.push(v); v->decorators = visitNodeList<_expr, ExpressionAst>(node->v.AsyncFunctionDef.decorator_list); nodeStack.pop();
600                 nodeStack.push(v); v->returns = static_cast<ExpressionAst*>(visitNode(node->v.AsyncFunctionDef.returns)); nodeStack.pop();
601  v->async = true;
602                 result = v;
603                 break;
604             }
605 #endif
606         case ClassDef_kind: {
607                 ClassDefinitionAst* v = new  ClassDefinitionAst(parent());
608                 v->name = node->v.ClassDef.name ? new Python::Identifier(PyUnicodeObjectToQString(node->v.ClassDef.name)) : nullptr;
609                 if ( v->name ) {
610                     v->name->startCol = node->col_offset; v->startCol = v->name->startCol;
611                     v->name->startLine = tline(node->lineno - 1);  v->startLine = v->name->startLine;
612                     v->name->endCol = node->col_offset + v->name->value.length() - 1;  v->endCol = v->name->endCol;
613                     v->name->endLine = tline(node->lineno - 1);  v->endLine = v->name->endLine;
614                     ranges_copied = true;
615                 }
616                 nodeStack.push(v); v->baseClasses = visitNodeList<_expr, ExpressionAst>(node->v.ClassDef.bases); nodeStack.pop();
617                 nodeStack.push(v); v->body = visitNodeList<_stmt, Ast>(node->v.ClassDef.body); nodeStack.pop();
618                 nodeStack.push(v); v->decorators = visitNodeList<_expr, ExpressionAst>(node->v.ClassDef.decorator_list); nodeStack.pop();
619                 result = v;
620                 break;
621             }
622         case Return_kind: {
623                 ReturnAst* v = new  ReturnAst(parent());
624                 nodeStack.push(v); v->value = static_cast<ExpressionAst*>(visitNode(node->v.Return.value)); nodeStack.pop();
625                 result = v;
626                 break;
627             }
628         case Delete_kind: {
629                 DeleteAst* v = new  DeleteAst(parent());
630                 nodeStack.push(v); v->targets = visitNodeList<_expr, ExpressionAst>(node->v.Delete.targets); nodeStack.pop();
631                 result = v;
632                 break;
633             }
634         case Assign_kind: {
635                 AssignmentAst* v = new  AssignmentAst(parent());
636                 nodeStack.push(v); v->targets = visitNodeList<_expr, ExpressionAst>(node->v.Assign.targets); nodeStack.pop();
637                 nodeStack.push(v); v->value = static_cast<ExpressionAst*>(visitNode(node->v.Assign.value)); nodeStack.pop();
638                 result = v;
639                 break;
640             }
641         case AugAssign_kind: {
642                 AugmentedAssignmentAst* v = new  AugmentedAssignmentAst(parent());
643                 nodeStack.push(v); v->target = static_cast<ExpressionAst*>(visitNode(node->v.AugAssign.target)); nodeStack.pop();
644                 v->op = (ExpressionAst::OperatorTypes) node->v.AugAssign.op;
645                 nodeStack.push(v); v->value = static_cast<ExpressionAst*>(visitNode(node->v.AugAssign.value)); nodeStack.pop();
646                 result = v;
647                 break;
648             }
649 #if PYTHON_VERSION >= QT_VERSION_CHECK(3, 6, 0)
650         case AnnAssign_kind: {
651                 AnnotationAssignmentAst* v = new  AnnotationAssignmentAst(parent());
652                 nodeStack.push(v); v->target = static_cast<ExpressionAst*>(visitNode(node->v.AnnAssign.target)); nodeStack.pop();
653                 nodeStack.push(v); v->annotation = static_cast<ExpressionAst*>(visitNode(node->v.AnnAssign.annotation)); nodeStack.pop();
654                 nodeStack.push(v); v->value = static_cast<ExpressionAst*>(visitNode(node->v.AnnAssign.value)); nodeStack.pop();
655                 result = v;
656                 break;
657             }
658 #endif
659         case For_kind: {
660                 ForAst* v = new  ForAst(parent());
661                 nodeStack.push(v); v->target = static_cast<ExpressionAst*>(visitNode(node->v.For.target)); nodeStack.pop();
662                 nodeStack.push(v); v->iterator = static_cast<ExpressionAst*>(visitNode(node->v.For.iter)); nodeStack.pop();
663                 nodeStack.push(v); v->body = visitNodeList<_stmt, Ast>(node->v.For.body); nodeStack.pop();
664                 nodeStack.push(v); v->orelse = visitNodeList<_stmt, Ast>(node->v.For.orelse); nodeStack.pop();
665                 result = v;
666                 break;
667             }
668 #if PYTHON_VERSION >= QT_VERSION_CHECK(3, 5, 0)
669         case AsyncFor_kind: {
670                 ForAst* v = new  ForAst(parent());
671                 nodeStack.push(v); v->target = static_cast<ExpressionAst*>(visitNode(node->v.AsyncFor.target)); nodeStack.pop();
672                 nodeStack.push(v); v->iterator = static_cast<ExpressionAst*>(visitNode(node->v.AsyncFor.iter)); nodeStack.pop();
673                 nodeStack.push(v); v->body = visitNodeList<_stmt, Ast>(node->v.AsyncFor.body); nodeStack.pop();
674                 nodeStack.push(v); v->orelse = visitNodeList<_stmt, Ast>(node->v.AsyncFor.orelse); nodeStack.pop();
675                 result = v;
676                 break;
677             }
678 #endif
679         case While_kind: {
680                 WhileAst* v = new  WhileAst(parent());
681                 nodeStack.push(v); v->condition = static_cast<ExpressionAst*>(visitNode(node->v.While.test)); nodeStack.pop();
682                 nodeStack.push(v); v->body = visitNodeList<_stmt, Ast>(node->v.While.body); nodeStack.pop();
683                 nodeStack.push(v); v->orelse = visitNodeList<_stmt, Ast>(node->v.While.orelse); nodeStack.pop();
684                 result = v;
685                 break;
686             }
687         case If_kind: {
688                 IfAst* v = new  IfAst(parent());
689                 nodeStack.push(v); v->condition = static_cast<ExpressionAst*>(visitNode(node->v.If.test)); nodeStack.pop();
690                 nodeStack.push(v); v->body = visitNodeList<_stmt, Ast>(node->v.If.body); nodeStack.pop();
691                 nodeStack.push(v); v->orelse = visitNodeList<_stmt, Ast>(node->v.If.orelse); nodeStack.pop();
692                 result = v;
693                 break;
694             }
695         case With_kind: {
696                 WithAst* v = new  WithAst(parent());
697                 nodeStack.push(v); v->body = visitNodeList<_stmt, Ast>(node->v.With.body); nodeStack.pop();
698                 nodeStack.push(v); v->items = visitNodeList<_withitem, WithItemAst>(node->v.With.items); nodeStack.pop();
699                 result = v;
700                 break;
701             }
702 #if PYTHON_VERSION >= QT_VERSION_CHECK(3, 5, 0)
703         case AsyncWith_kind: {
704                 WithAst* v = new  WithAst(parent());
705                 nodeStack.push(v); v->body = visitNodeList<_stmt, Ast>(node->v.AsyncWith.body); nodeStack.pop();
706                 nodeStack.push(v); v->items = visitNodeList<_withitem, WithItemAst>(node->v.AsyncWith.items); nodeStack.pop();
707                 result = v;
708                 break;
709             }
710 #endif
711         case Raise_kind: {
712                 RaiseAst* v = new  RaiseAst(parent());
713                 nodeStack.push(v); v->type = static_cast<ExpressionAst*>(visitNode(node->v.Raise.exc)); nodeStack.pop();
714                 result = v;
715                 break;
716             }
717         case Try_kind: {
718                 TryAst* v = new  TryAst(parent());
719                 nodeStack.push(v); v->body = visitNodeList<_stmt, Ast>(node->v.Try.body); nodeStack.pop();
720                 nodeStack.push(v); v->handlers = visitNodeList<_excepthandler, ExceptionHandlerAst>(node->v.Try.handlers); nodeStack.pop();
721                 nodeStack.push(v); v->orelse = visitNodeList<_stmt, Ast>(node->v.Try.orelse); nodeStack.pop();
722                 nodeStack.push(v); v->finally = visitNodeList<_stmt, Ast>(node->v.Try.finalbody); nodeStack.pop();
723                 result = v;
724                 break;
725             }
726         case Assert_kind: {
727                 AssertionAst* v = new  AssertionAst(parent());
728                 nodeStack.push(v); v->condition = static_cast<ExpressionAst*>(visitNode(node->v.Assert.test)); nodeStack.pop();
729                 nodeStack.push(v); v->message = static_cast<ExpressionAst*>(visitNode(node->v.Assert.msg)); nodeStack.pop();
730                 result = v;
731                 break;
732             }
733         case Import_kind: {
734                 ImportAst* v = new  ImportAst(parent());
735                 nodeStack.push(v); v->names = visitNodeList<_alias, AliasAst>(node->v.Import.names); nodeStack.pop();
736                 result = v;
737                 break;
738             }
739         case ImportFrom_kind: {
740                 ImportFromAst* v = new  ImportFromAst(parent());
741                 v->module = node->v.ImportFrom.module ? new Python::Identifier(PyUnicodeObjectToQString(node->v.ImportFrom.module)) : nullptr;
742                 if ( v->module ) {
743                     v->module->startCol = node->col_offset; v->startCol = v->module->startCol;
744                     v->module->startLine = tline(node->lineno - 1);  v->startLine = v->module->startLine;
745                     v->module->endCol = node->col_offset + v->module->value.length() - 1;  v->endCol = v->module->endCol;
746                     v->module->endLine = tline(node->lineno - 1);  v->endLine = v->module->endLine;
747                     ranges_copied = true;
748                 }
749                 nodeStack.push(v); v->names = visitNodeList<_alias, AliasAst>(node->v.ImportFrom.names); nodeStack.pop();
750                 v->level = node->v.ImportFrom.level;
751                 result = v;
752                 break;
753             }
754         case Global_kind: {
755                 GlobalAst* v = new  GlobalAst(parent());
756 
757                 for ( int _i = 0; _i < node->v.Global.names->size; _i++ ) {
758                     Python::Identifier* id = new Python::Identifier(PyUnicodeObjectToQString(
759                                     static_cast<PyObject*>(node->v.Global.names->elements[_i])
760                             ));
761                     v->names.append(id);
762                 }
763 
764                 result = v;
765                 break;
766             }
767         case Break_kind: {
768                 BreakAst* v = new  BreakAst(parent());
769                 result = v;
770                 break;
771             }
772         case Continue_kind: {
773                 ContinueAst* v = new  ContinueAst(parent());
774                 result = v;
775                 break;
776             }
777         case Pass_kind: {
778                 PassAst* v = new  PassAst(parent());
779                 result = v;
780                 break;
781             }
782         case Nonlocal_kind: {
783                 NonlocalAst* v = new  NonlocalAst(parent());
784                 result = v;
785                 break;
786             }
787         default:
788             qWarning() << "Unsupported _stmt AST type: " << node->kind;
789             Q_ASSERT(false);
790         }
791 
792 	if ( ! result ) return nullptr;
793         if ( ! ranges_copied ) {
794             result->startCol = node->col_offset;
795             result->endCol = node->col_offset;
796             result->startLine = tline(node->lineno - 1);
797             result->endLine = tline(node->lineno - 1);
798             result->hasUsefulRangeInformation = true;
799         }
800         else {
801             result->hasUsefulRangeInformation = true;
802         }
803 
804         // Walk through the tree and set proper end columns and lines, as the python parser sadly does not do this for us
805         if ( result->hasUsefulRangeInformation ) {
806             Ast* parent = result->parent;
807             while ( parent ) {
808                 if ( parent->endLine < result->endLine ) {
809                     parent->endLine = result->endLine;
810                     parent->endCol = result->endCol;
811                 }
812                 if ( ! parent->hasUsefulRangeInformation && parent->startLine == -99999 ) {
813                     parent->startLine = result->startLine;
814                     parent->startCol = result->startCol;
815                 }
816                 parent = parent->parent;
817             }
818         }
819 
820         if ( result && result->astType == Ast::NameAstType ) {
821             NameAst* r = static_cast<NameAst*>(result);
822             r->startCol = r->identifier->startCol;
823             r->endCol = r->identifier->endCol;
824             r->startLine = r->identifier->startLine;
825             r->endLine = r->identifier->endLine;
826         }
827         return result;
828     }
829 
830 
visitNode(_withitem * node)831     Ast* visitNode(_withitem* node) {
832         bool ranges_copied = false; Q_UNUSED(ranges_copied);
833         if ( ! node ) return nullptr;
834                 WithItemAst* v = new  WithItemAst(parent());
835             nodeStack.push(v); v->contextExpression = static_cast<ExpressionAst*>(visitNode(node->context_expr)); nodeStack.pop();
836             nodeStack.push(v); v->optionalVars = static_cast<ExpressionAst*>(visitNode(node->optional_vars)); nodeStack.pop();
837         return v;
838     }
839 
840 };
841 
842 /*
843  * End generated code
844  */
845 
846