-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathchapel.cpp
More file actions
646 lines (559 loc) · 21.3 KB
/
Copy pathchapel.cpp
File metadata and controls
646 lines (559 loc) · 21.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/*
* Copyright 2021-2023 Hewlett Packard Enterprise Development LP
* Other additional copyright holders may be indicated within.
*
* The entirety of this work is licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "chpl/framework/Context.h"
#include "chpl/parsing/parsing-queries.h"
#include <utility>
struct IterAdapterBase {
virtual ~IterAdapterBase() = default;
virtual const chpl::uast::AstNode* next() = 0;
};
template <typename IterPair>
struct IterAdapter : IterAdapterBase {
private:
using IterType = decltype(std::declval<IterPair>().begin());
IterType current;
IterType end;
public:
IterAdapter(IterPair pair) : current(pair.begin()), end(pair.end()) {}
const chpl::uast::AstNode* next() override {
if (current == end) return nullptr;
return *(current++);
}
};
static PyTypeObject* parentTypeFor(chpl::uast::asttags::AstTag tag);
typedef struct {
PyObject_HEAD
IterAdapterBase* iterAdapter;
PyObject* contextObject;
} AstIterObject;
extern PyTypeObject AstIterType;
typedef struct {
PyObject_HEAD
int current;
int num;
const void* container;
chpl::UniqueString (*nameGetter)(const void*, int);
const chpl::uast::AstNode* (*childGetter)(const void*, int);
PyObject* contextObject;
} AstCallIterObject;
extern PyTypeObject AstCallIterType;
typedef struct {
PyObject_HEAD
chpl::Context context;
/* Type-specific fields go here. */
} ContextObject;
extern PyTypeObject ContextType;
typedef struct {
PyObject_HEAD
chpl::Location location;
} LocationObject;
extern PyTypeObject LocationType;
typedef struct {
PyObject_HEAD
PyObject* contextObject;
const chpl::uast::AstNode* astNode;
} AstNodeObject;
extern PyTypeObject AstNodeType;
/**
Declare a Python PyTypeObject that corresponds to an AST node with the given
name and tag. The tag is not the same as the name because abstract base
classes like NamedDecl have corresponding tags called START_NamedDecl
and END_NamedDecl, but not NamedDecl.
*/
#define DEFINE_PY_OBJECT_FOR(NAME, TAG)\
typedef struct { \
AstNodeObject parent; \
} NAME##Object; \
\
extern PyTypeObject NAME##Type; \
static int NAME##Object_init(NAME##Object* self, PyObject* args, PyObject* kwargs) { \
return parentTypeFor(chpl::uast::asttags::TAG)->tp_init((PyObject*) self, args, kwargs); \
} \
/* Generate a Python object for reach AST node type. */
#define AST_NODE(NAME) DEFINE_PY_OBJECT_FOR(NAME, NAME)
#define AST_LEAF(NAME) DEFINE_PY_OBJECT_FOR(NAME, NAME)
#define AST_BEGIN_SUBCLASSES(NAME) DEFINE_PY_OBJECT_FOR(NAME, START_##NAME)
#define AST_END_SUBCLASSES(NAME)
#include "chpl/uast/uast-classes-list.h"
#undef AST_NODE
#undef AST_LEAF
#undef AST_BEGIN_SUBCLASSES
#undef AST_END_SUBCLASSES
/**
Create a Python object of the class corresponding to the given AST node's
type. For example, an Identifier node will be wrapped in a a chapel.Identifier.
*/
static PyObject* wrapAstNode(ContextObject* context, const chpl::uast::AstNode* node) {
PyObject* toReturn = nullptr;
if (node == nullptr) {
Py_RETURN_NONE;
}
PyObject* args = Py_BuildValue("(O)", (PyObject*) context);
switch (node->tag()) {
#define CAST_TO(NAME) \
case chpl::uast::asttags::NAME: \
toReturn = PyObject_CallObject((PyObject*) &NAME##Type, args); \
((NAME##Object*) toReturn)->parent.astNode = node->to##NAME(); \
break;
#define AST_NODE(NAME) CAST_TO(NAME)
#define AST_LEAF(NAME) CAST_TO(NAME)
#define AST_BEGIN_SUBCLASSES(NAME) /* No need to handle abstract parent classes. */
#define AST_END_SUBCLASSES(NAME)
#include "chpl/uast/uast-classes-list.h"
#undef AST_NODE
#undef AST_LEAF
#undef AST_BEGIN_SUBCLASSES
#undef AST_END_SUBCLASSES
default: break;
}
Py_XDECREF(args);
return toReturn;
}
template <typename IterPair>
static PyObject* wrapIterPair(ContextObject* context, const IterPair& pair) {
auto argList = Py_BuildValue("(O)", (PyObject*) context);
auto astIterObjectPy = PyObject_CallObject((PyObject *) &AstIterType, argList);
auto astIterObject = (AstIterObject*) astIterObjectPy;
astIterObject->iterAdapter = new IterAdapter<decltype(pair)>(pair);
Py_XDECREF(argList);
return astIterObjectPy;
}
static PyTypeObject* parentTypeFor(chpl::uast::asttags::AstTag tag) {
#define AST_NODE(NAME)
#define AST_LEAF(NAME)
#define AST_BEGIN_SUBCLASSES(NAME)
#define AST_END_SUBCLASSES(NAME) \
if (tag > chpl::uast::asttags::START_##NAME && tag < chpl::uast::asttags::END_##NAME) { \
return &NAME##Type; \
}
#include "chpl/uast/uast-classes-list.h"
#include "chpl/uast/uast-classes-list.h"
#undef AST_NODE
#undef AST_LEAF
#undef AST_BEGIN_SUBCLASSES
#undef AST_END_SUBCLASSES
return &AstNodeType;
}
static int AstIterObject_init(AstIterObject* self, PyObject* args, PyObject* kwargs) {
PyObject* astObjectPy;
if (!PyArg_ParseTuple(args, "O", &astObjectPy))
return -1;
auto contextObject = (ContextObject*) astObjectPy;
Py_INCREF(contextObject);
self->contextObject = (PyObject*) contextObject;
self->iterAdapter = nullptr;
return 0;
}
static void AstIterObject_dealloc(AstIterObject* self) {
delete self->iterAdapter;
Py_XDECREF(self->contextObject);
Py_TYPE(self)->tp_free((PyObject *) self);
}
static PyObject* AstIterObject_iter(AstIterObject *self) {
Py_INCREF(self);
return (PyObject*) self;
}
static PyObject* AstIterObject_next(AstIterObject *self) {
if (self->iterAdapter) {
if (auto nextNode = self->iterAdapter->next()) {
return wrapAstNode((ContextObject*) self->contextObject, nextNode);
}
}
PyErr_SetNone(PyExc_StopIteration);
return nullptr;
}
PyTypeObject AstIterType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "AstIter",
.tp_basicsize = sizeof(AstIterObject),
.tp_itemsize = 0,
.tp_dealloc = (destructor) AstIterObject_dealloc,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = PyDoc_STR("An iterator over Chapel AST nodes"),
.tp_iter = (getiterfunc) AstIterObject_iter,
.tp_iternext = (iternextfunc) AstIterObject_next,
.tp_init = (initproc) AstIterObject_init,
.tp_new = PyType_GenericNew,
};
static int AstCallIterObject_init(AstCallIterObject* self, PyObject* args, PyObject* kwargs) {
PyObject* astObjectPy;
if (!PyArg_ParseTuple(args, "O", &astObjectPy))
return -1;
auto astObject = (AstNodeObject*) astObjectPy;
Py_INCREF(astObject->contextObject);
self->contextObject = astObject->contextObject;
return 0;
}
static void AstCallIterObject_dealloc(AstCallIterObject* self) {
Py_XDECREF(self->contextObject);
Py_TYPE(self)->tp_free((PyObject *) self);
}
static PyObject* AstCallIterObject_iter(AstCallIterObject *self) {
Py_INCREF(self);
return (PyObject*) self;
}
static PyObject* AstCallIterObject_next(AstCallIterObject *self) {
if (self->current == self->num) {
PyErr_SetNone(PyExc_StopIteration);
return nullptr;
}
auto argName = self->nameGetter(self->container, self->current);
auto child = wrapAstNode((ContextObject*) self->contextObject,
self->childGetter(self->container, self->current));
PyObject* toReturn = nullptr;
if (!argName.isEmpty()) {
toReturn = Py_BuildValue("sO", argName.c_str(), child);
Py_XDECREF(child);
} else {
toReturn = child;
}
self->current++;
return toReturn;
}
PyTypeObject AstCallIterType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "AstCallIter",
.tp_basicsize = sizeof(AstCallIterObject),
.tp_itemsize = 0,
.tp_dealloc = (destructor) AstCallIterObject_dealloc,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = PyDoc_STR("An iterator over Chapel function call actuals"),
.tp_iter = (getiterfunc) AstCallIterObject_iter,
.tp_iternext = (iternextfunc) AstCallIterObject_next,
.tp_init = (initproc) AstCallIterObject_init,
.tp_new = PyType_GenericNew,
};
static int LocationObject_init(LocationObject* self, PyObject* args, PyObject* kwargs) {
new (&self->location) chpl::Location();
return 0;
}
static void LocationObject_dealloc(LocationObject* self) {
self->location.~Location();
Py_TYPE(self)->tp_free((PyObject *) self);
}
static PyObject* LocationObject_start(LocationObject *self, PyObject* Py_UNUSED(args)) {
auto& location = self->location;
return Py_BuildValue("ii", location.firstLine(), location.firstColumn());
}
static PyObject* LocationObject_end(LocationObject *self, PyObject* Py_UNUSED(args)) {
auto& location = self->location;
return Py_BuildValue("ii", location.lastLine(), location.lastColumn());
}
static PyObject* LocationObject_path(LocationObject *self, PyObject* Py_UNUSED(args)) {
return Py_BuildValue("s", self->location.path().c_str());
}
static PyMethodDef LocationObject_methods[] = {
{ "start", (PyCFunction) LocationObject_start, METH_VARARGS, "Get the start of a Location object" },
{ "end", (PyCFunction) LocationObject_end, METH_VARARGS, "Get the end of a Location object" },
{ "path", (PyCFunction) LocationObject_path, METH_VARARGS, "Get the path of a Location object" },
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyTypeObject LocationType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "Location",
.tp_basicsize = sizeof(LocationObject),
.tp_itemsize = 0,
.tp_dealloc = (destructor) LocationObject_dealloc,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = PyDoc_STR("The Chapel context object that tracks various frontend state"),
.tp_methods = LocationObject_methods,
.tp_init = (initproc) LocationObject_init,
.tp_new = PyType_GenericNew,
};
static int ContextObject_init(ContextObject* self, PyObject* args, PyObject* kwargs) {
chpl::Context::Configuration config;
config.chplHome = getenv("CHPL_HOME");
new (&self->context) chpl::Context(std::move(config));
chpl::parsing::setupModuleSearchPaths(&self->context, false, false, {}, {});
return 0;
}
static void ContextObject_dealloc(ContextObject* self) {
self->context.~Context();
Py_TYPE(self)->tp_free((PyObject *) self);
}
static PyObject* ContextObject_parse(ContextObject *self, PyObject* args) {
auto context = &self->context;
const char* fileName;
if (!PyArg_ParseTuple(args, "s", &fileName)) {
PyErr_BadArgument();
return nullptr;
}
auto fileNameUS = chpl::UniqueString::get(context, fileName);
auto parentPathUS = chpl::UniqueString();
auto& builderResult = chpl::parsing::parseFileToBuilderResult(context, fileNameUS, parentPathUS);
int listSize = builderResult.numTopLevelExpressions();
PyObject* topExprs = PyList_New(listSize);
for (auto i = 0; i < listSize; i++) {
PyObject* node = wrapAstNode(self, builderResult.topLevelExpression(i));
PyList_SetItem(topExprs, i, node);
}
return topExprs;
}
static PyObject* ContextObject_is_bundled_path(ContextObject *self, PyObject* args) {
auto context = &self->context;
const char* fileName;
if (!PyArg_ParseTuple(args, "s", &fileName)) {
PyErr_BadArgument();
return nullptr;
}
auto pathUS = chpl::UniqueString::get(context, fileName);
bool isInternalPath =
chpl::parsing::filePathIsInInternalModule(context, pathUS) ||
chpl::parsing::filePathIsInStandardModule(context, pathUS) ||
chpl::parsing::filePathIsInBundledModule(context, pathUS);
return PyBool_FromLong(isInternalPath);
}
static PyMethodDef ContextObject_methods[] = {
{ "parse", (PyCFunction) ContextObject_parse, METH_VARARGS, "Parse a top-level AST node from the given file" },
{ "is_bundled_path", (PyCFunction) ContextObject_is_bundled_path, METH_VARARGS, "Check if the given file path is within the bundled (built-in) Chapel files" },
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyTypeObject ContextType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "Context",
.tp_basicsize = sizeof(ContextObject),
.tp_itemsize = 0,
.tp_dealloc = (destructor) ContextObject_dealloc,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = PyDoc_STR("The Chapel context object that tracks various frontend state"),
.tp_methods = ContextObject_methods,
.tp_init = (initproc) ContextObject_init,
.tp_new = PyType_GenericNew,
};
static int AstNodeObject_init(AstNodeObject* self, PyObject* args, PyObject* kwargs) { \
PyObject* contextObjectPy;
if (!PyArg_ParseTuple(args, "O", &contextObjectPy))
return -1;
Py_INCREF(contextObjectPy);
self->astNode = nullptr;
self->contextObject = contextObjectPy;
return 0;
}
static void AstNodeObject_dealloc(AstNodeObject* self) {
Py_XDECREF(self->contextObject);
Py_TYPE(self)->tp_free((PyObject *) self);
}
static PyObject* AstNodeObject_dump(AstNodeObject *self, PyObject *Py_UNUSED(ignored)) {
self->astNode->dump();
Py_RETURN_NONE;
}
static PyObject* AstNodeObject_tag(AstNodeObject *self, PyObject *Py_UNUSED(ignored)) {
const char* nodeType = chpl::uast::asttags::tagToString(self->astNode->tag());
return Py_BuildValue("s", nodeType);
}
static PyObject* AstNodeObject_unique_id(AstNodeObject *self, PyObject *Py_UNUSED(ignored)) {
auto uniqueID = (intptr_t)self->astNode;
return Py_BuildValue("K", uniqueID);
}
static PyObject* AstNodeObject_attribute_group(AstNodeObject *self, PyObject *Py_UNUSED(ignored)) {
return wrapAstNode((ContextObject*) self->contextObject,
self->astNode->attributeGroup());
}
static PyObject* AstNodeObject_parent(AstNodeObject* self, PyObject *Py_UNUSED(ignored)) {
auto contextObject = (ContextObject*) self->contextObject;
auto context = &contextObject->context;
return wrapAstNode(contextObject, chpl::parsing::parentAst(context, self->astNode));
}
static PyObject* AstNodeObject_iter(AstNodeObject *self) {
return wrapIterPair((ContextObject*) self->contextObject, self->astNode->children());
}
static PyObject* AstNodeObject_location(AstNodeObject *self) {
auto locationObjectPy = PyObject_CallObject((PyObject *) &LocationType, nullptr);
auto& location = ((LocationObject*) locationObjectPy)->location;
auto context = &((ContextObject*) self->contextObject)->context;
location = chpl::parsing::locateAst(context, self->astNode);
return locationObjectPy;
}
static PyMethodDef AstNodeObject_methods[] = {
{"dump", (PyCFunction) AstNodeObject_dump, METH_NOARGS, "Dump the internal representation of the given AST node"},
{"tag", (PyCFunction) AstNodeObject_tag, METH_NOARGS, "Get a string representation of the AST node's type"},
{"attribute_group", (PyCFunction) AstNodeObject_attribute_group, METH_NOARGS, "Get the attribute group, if any, associated with this node"},
{"location", (PyCFunction) AstNodeObject_location, METH_NOARGS, "Get the location of this AST node in its file"},
{"parent", (PyCFunction) AstNodeObject_parent, METH_NOARGS, "Get the parent node of this AST node"},
{"unique_id", (PyCFunction) AstNodeObject_unique_id, METH_NOARGS, "Get a unique identifer for this AST node"},
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyTypeObject AstNodeType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "AstNode",
.tp_basicsize = sizeof(AstNodeObject),
.tp_itemsize = 0,
.tp_dealloc = (destructor) AstNodeObject_dealloc,
.tp_flags = Py_TPFLAGS_BASETYPE,
.tp_doc = PyDoc_STR("The base type of Chapel AST nodes"),
.tp_iter = (getiterfunc) AstNodeObject_iter,
.tp_methods = AstNodeObject_methods,
.tp_init = (initproc) AstNodeObject_init,
.tp_new = PyType_GenericNew,
};
static const char* blockStyleToString(chpl::uast::BlockStyle blockStyle) {
switch (blockStyle) {
case chpl::uast::BlockStyle::EXPLICIT: return "explicit";
case chpl::uast::BlockStyle::IMPLICIT: return "implicit";
case chpl::uast::BlockStyle::UNNECESSARY_KEYWORD_AND_BLOCK: return "unnecessary";
}
}
static const char* opKindToString(chpl::uast::Range::OpKind kind) {
switch (kind) {
case chpl::uast::Range::DEFAULT: return "..";
case chpl::uast::Range::OPEN_HIGH: return "..<";
default: return "";
}
}
template<typename IntentType>
static const char* intentToString(IntentType intent) {
return chpl::uast::qualifierToString(chpl::uast::Qualifier(int(intent)));
}
template <typename T>
const char* toCString(T& t) { throw std::invalid_argument("Invalid conversion into C string"); }
template <>
const char* toCString<const char*>(const char*& t) { return t; }
template <>
const char* toCString<chpl::UniqueString>(chpl::UniqueString& us) { return us.c_str(); }
#define PLAIN_GETTER(NODE, NAME, DOCSTR, TYPESTR, BODY)\
static PyObject* NODE##Object_##NAME(PyObject *self, PyObject *Py_UNUSED(ignored)) {\
auto cast = ((NODE##Object*) self)->parent.astNode->to##NODE(); \
auto result = [](const chpl::uast::NODE* node, ContextObject* contextObject) { \
BODY; \
}(cast, (ContextObject*) ((NODE##Object*) self)->parent.contextObject); \
if constexpr (std::string_view(TYPESTR) == std::string_view("s")) { \
return Py_BuildValue(TYPESTR, toCString(result)); \
} else { \
return Py_BuildValue(TYPESTR, result); \
} \
}
#include "method-tables.h"
#define ACTUAL_ITERATOR(NAME)\
static PyObject* NAME##Object_actuals(PyObject *self, PyObject *Py_UNUSED(ignored)) { \
auto node = ((NAME##Object*) self)->parent.astNode->to##NAME(); \
\
auto argList = Py_BuildValue("(O)", (PyObject*) self); \
auto astCallIterObjectPy = PyObject_CallObject((PyObject *) &AstCallIterType, argList); \
Py_XDECREF(argList); \
\
auto astCalliterObject = (AstCallIterObject*) astCallIterObjectPy; \
astCalliterObject->current = 0; \
astCalliterObject->num = node->numActuals(); \
astCalliterObject->container = node; \
astCalliterObject->childGetter = [](const void* n, int child) { \
return ((chpl::uast::NAME*) n)->actual(child); \
}; \
astCalliterObject->nameGetter = [](const void* n, int child) { \
return ((chpl::uast::NAME*) n)->actualName(child); \
}; \
\
return astCallIterObjectPy; \
}
ACTUAL_ITERATOR(Attribute);
ACTUAL_ITERATOR(FnCall);
template <chpl::uast::asttags::AstTag tag>
struct PerNodeInfo {
static constexpr PyMethodDef methods[] = {
{NULL, NULL, 0, NULL} /* Sentinel */
};
};
#define CLASS_BEGIN(TAG) \
template <> \
struct PerNodeInfo<chpl::uast::asttags::TAG> { \
static constexpr PyMethodDef methods[] = {
#define CLASS_END(TAG) \
{NULL, NULL, 0, NULL} /* Sentinel */ \
}; \
};
#define PLAIN_GETTER(NODE, NAME, DOCSTR, TYPESTR, BODY) \
{#NAME, NODE##Object_##NAME, METH_NOARGS, DOCSTR},
#define METHOD_PROTOTYPE(NODE, NAME, DOCSTR) \
{#NAME, NODE##Object_##NAME, METH_NOARGS, DOCSTR},
#include "method-tables.h"
#define DEFINE_PY_TYPE_FOR(NAME, TAG, FLAGS)\
PyTypeObject NAME##Type = { \
PyVarObject_HEAD_INIT(NULL, 0) \
.tp_name = #NAME, \
.tp_basicsize = sizeof(NAME##Object), \
.tp_itemsize = 0, \
.tp_flags = FLAGS, \
.tp_doc = PyDoc_STR("A Chapel " #NAME " AST node"), \
.tp_methods = (PyMethodDef*) PerNodeInfo<TAG>::methods, \
.tp_base = parentTypeFor(TAG), \
.tp_init = (initproc) NAME##Object_init, \
.tp_new = PyType_GenericNew, \
}; \
#define AST_NODE(NAME) DEFINE_PY_TYPE_FOR(NAME, chpl::uast::asttags::NAME, Py_TPFLAGS_DEFAULT)
#define AST_LEAF(NAME) DEFINE_PY_TYPE_FOR(NAME, chpl::uast::asttags::NAME, Py_TPFLAGS_DEFAULT)
#define AST_BEGIN_SUBCLASSES(NAME) DEFINE_PY_TYPE_FOR(NAME, chpl::uast::asttags::START_##NAME, Py_TPFLAGS_BASETYPE)
#define AST_END_SUBCLASSES(NAME)
#include "chpl/uast/uast-classes-list.h"
#undef AST_NODE
#undef AST_LEAF
#undef AST_BEGIN_SUBCLASSES
#undef AST_END_SUBCLASSES
static PyMethodDef ChapelMethods[] = {
{ NULL, NULL, 0, NULL } /* Sentinel */
};
static PyModuleDef ChapelModule {
PyModuleDef_HEAD_INIT,
.m_name="core",
.m_doc="A Python bridge for the Chapel frontend library",
.m_size=-1 /* Per-interpreter memory (not used currently) */,
.m_methods=ChapelMethods,
};
extern "C" {
PyMODINIT_FUNC PyInit_core() {
PyObject* chapelModule = nullptr;
if (PyType_Ready(&ContextType) < 0) return nullptr;
if (PyType_Ready(&LocationType) < 0) return nullptr;
if (PyType_Ready(&AstIterType) < 0) return nullptr;
if (PyType_Ready(&AstCallIterType) < 0) return nullptr;
if (PyType_Ready(&AstNodeType) < 0) return nullptr;
#define READY_TYPE(NAME) if (PyType_Ready(&NAME##Type) < 0) return nullptr;
#define AST_NODE(NAME) READY_TYPE(NAME)
#define AST_LEAF(NAME) READY_TYPE(NAME)
#define AST_BEGIN_SUBCLASSES(NAME) READY_TYPE(NAME)
#define AST_END_SUBCLASSES(NAME)
#include "chpl/uast/uast-classes-list.h"
#undef AST_NODE
#undef AST_LEAF
#undef AST_BEGIN_SUBCLASSES
#undef AST_END_SUBCLASSES
chapelModule = PyModule_Create(&ChapelModule);
if (!chapelModule) return nullptr;
#define ADD_TYPE(NAME) if (PyModule_AddObject(chapelModule, #NAME, (PyObject*) &NAME##Type) < 0) return nullptr;
#define AST_NODE(NAME) ADD_TYPE(NAME)
#define AST_LEAF(NAME) ADD_TYPE(NAME)
#define AST_BEGIN_SUBCLASSES(NAME) ADD_TYPE(NAME)
#define AST_END_SUBCLASSES(NAME)
#include "chpl/uast/uast-classes-list.h"
#undef AST_NODE
#undef AST_LEAF
#undef AST_BEGIN_SUBCLASSES
#undef AST_END_SUBCLASSES
ADD_TYPE(AstNode);
if (PyModule_AddObject(chapelModule, "Context", (PyObject *) &ContextType) < 0) {
Py_DECREF(&ContextType);
Py_DECREF(chapelModule);
return NULL;
}
return chapelModule;
}
}
int main() {
chpl::Context myContext;
}