• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Nic30 / hdlConvertor / #201

10 Jun 2025 02:39PM UTC coverage: 59.995% (-2.5%) from 62.506%
#201

push

travis-ci

Nic30
Merge remote-tracking branch 'origin/mesonbuild'

109 of 127 new or added lines in 14 files covered. (85.83%)

11 existing lines in 6 files now uncovered.

41572 of 69293 relevant lines covered (59.99%)

1047819.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.43
/hdlConvertor/toPy.cpp
1
#include "toPy.h"
2

3
#include <typeinfo>
4
#include <iterator>
5
#include <stdexcept>
6
#include <string>
7
#include <tuple>
8
#include <utility>
9

10
#include <hdlConvertor/hdlAst/bigInteger.h>
11
#include <hdlConvertor/hdlAst/hdlDirection.h>
12
#include <hdlConvertor/hdlAst/hdlValue.h>
13
#include <hdlConvertor/conversion_exception.h>
14

15
namespace hdlConvertor {
16

17
using namespace hdlAst;
18

19
ToPy::ToPy() {
18,368✔
20
        hdlAst_module = PyImport_ImportModule("hdlConvertorAst.hdlAst");
18,368✔
21
        if (hdlAst_module == nullptr) {
18,368✔
22
                // this could happen only if there are missing files in library
23
                PyErr_Print();
×
24
                throw std::runtime_error("can not import hdlConvertorAst.hdlAst");
×
25
        }
26
        auto import =
27
                        [this](PyObject *&obj, const std::string &name) {
789,824✔
28
                                obj = PyObject_GetAttrString(hdlAst_module, name.c_str());
789,824✔
29
                                assert(
789,824✔
30
                                                obj != NULL && "hdlConvertorAst.hdlAst not as expected from hdlConvertor");
31
                        };
18,368✔
32
        import(ContextCls, "HdlContext");
36,736✔
33
        import(CodePositionCls, "CodePosition");
36,736✔
34
        import(HdlModuleDefCls, "HdlModuleDef");
36,736✔
35
        import(HdlModuleDecCls, "HdlModuleDec");
36,736✔
36
        import(HdlIdDefCls, "HdlIdDef");
36,736✔
37
        import(HdlOpCls, "HdlOp");
36,736✔
38
        import(HdlOpTypeEnum, "HdlOpType");
36,736✔
39
        import(HdlValueIntCls, "HdlValueInt");
36,736✔
40
        import(HdlExprNotImplementedCls, "HdlExprNotImplemented");
36,736✔
41
        import(HdlValueIdCls, "HdlValueId");
36,736✔
42
        import(HdlDirectionEnum, "HdlDirection");
36,736✔
43
        import(HdlAllCls, "HdlAll");
36,736✔
44
        import(HdlOthersCls, "HdlOthers");
36,736✔
45
        import(HdlTypeAutoCls, "HdlTypeAuto");
36,736✔
46
        import(HdlTypeTypeCls, "HdlTypeType");
36,736✔
47
        import(HdlTypeSubtypeCls, "HdlTypeSubtype");
36,736✔
48
        import(HdlClassTypeEnum, "HdlClassType");
36,736✔
49
        import(HdlClassDefCls, "HdlClassDef");
36,736✔
50
        import(HdlPhysicalDefCls, "HdlPhysicalDef");
36,736✔
51
        import(HdlEnumDefCls, "HdlEnumDef");
36,736✔
52
        import(HdlStmIfCls, "HdlStmIf");
36,736✔
53
        import(HdlStmAssignCls, "HdlStmAssign");
36,736✔
54
        import(HdlStmProcessTriggerConstrainEnum, "HdlStmProcessTriggerConstrain");
36,736✔
55
        import(HdlStmProcessCls, "HdlStmProcess");
36,736✔
56
        import(HdlStmCaseUniqConstrainEnum, "HdlStmCaseUniqConstrain");
36,736✔
57
        import(HdlStmCaseTypeEnum, "HdlStmCaseType");
36,736✔
58
        import(HdlStmCaseCls, "HdlStmCase");
36,736✔
59
        import(HdlStmForCls, "HdlStmFor");
36,736✔
60
        import(HdlStmForInCls, "HdlStmForIn");
36,736✔
61
        import(HdlStmWhileCls, "HdlStmWhile");
36,736✔
62
        import(HdlStmNopCls, "HdlStmNop");
36,736✔
63
        import(HdlStmRepeatCls, "HdlStmRepeat");
36,736✔
64
        import(HdlStmReturnCls, "HdlStmReturn");
36,736✔
65
        import(HdlStmBreakCls, "HdlStmBreak");
36,736✔
66
        import(HdlStmContinueCls, "HdlStmContinue");
36,736✔
67
        import(HdlStmWaitCls, "HdlStmWait");
36,736✔
68
        import(HdlStmBlockJoinTypeCls, "HdlStmBlockJoinType");
36,736✔
69
        import(HdlStmBlockCls, "HdlStmBlock");
36,736✔
70
        import(HdlLibraryCls, "HdlLibrary");
36,736✔
71
        import(HdlImportCls, "HdlImport");
36,736✔
72
        import(HdlCompInstCls, "HdlCompInst");
36,736✔
73
        import(HdlFunctionDefCls, "HdlFunctionDef");
36,736✔
74
        import(HdlValueIdspaceCls, "HdlValueIdspace");
18,368✔
75
}
18,368✔
76

77
std::string ToPy::PyObject_repr(PyObject *o) {
×
78
        PyObject *args = Py_BuildValue("(O)", PyObject_Repr(o));
×
79
        if (!args)
×
80
                return "";
×
81
        const char *s = nullptr;
×
82
        if (!PyArg_ParseTuple(args, "s", &s)) {
×
83
                Py_DECREF(args);
84
                return "";
×
85
        }
86
        std::string ret = s;
×
87
        Py_DECREF(args);
88
        return ret;
×
89
}
×
90

91
PyObject* ToPy::toPy(const HdlContext *o) {
8,881✔
92
        PyObject *py_inst = PyObject_CallObject(ContextCls, NULL);
8,881✔
93
        if (!py_inst)
8,881✔
94
                return nullptr;
×
95
        if (toPy_arr(py_inst, "objs", o->objs)) {
26,643✔
96
                return nullptr;
×
97
        }
98
        return py_inst;
8,881✔
99
}
100

101
PyObject* ToPy::toPy(const HdlLibrary *o) {
2,475✔
102
        Py_INCREF(Py_None);
103
        PyObject *py_inst = PyObject_CallFunctionObjArgs(HdlLibraryCls, Py_None,
2,475✔
104
        NULL);
105
        if (!py_inst)
2,475✔
106
                return nullptr;
×
107
        if (toPy(static_cast<const WithNameAndDoc*>(o), py_inst))
2,475✔
108
                return nullptr;
×
109
        return toPy_fillHdlAttributes(py_inst, o);
2,475✔
110
}
111

112
PyObject* ToPy::toPy(const iHdlObj *o) {
499,087✔
113
        auto ex = dynamic_cast<const iHdlExprItem*>(o);
499,087✔
114
        if (ex)
499,087✔
115
                return toPy(ex);
×
116
        auto fn = dynamic_cast<const HdlFunctionDef*>(o);
499,087✔
117
        if (fn)
499,087✔
118
                return toPy(fn);
19,047✔
119
        auto v = dynamic_cast<const HdlIdDef*>(o);
480,040✔
120
        if (v)
480,040✔
121
                return toPy(v);
66,548✔
122
        auto s = dynamic_cast<const iHdlStatement*>(o);
413,492✔
123
        if (s)
413,492✔
124
                return toPy(s);
388,127✔
125
        auto md = dynamic_cast<const HdlModuleDec*>(o);
25,365✔
126
        if (md)
25,365✔
127
                return toPy(md);
2,554✔
128
        auto mdef = dynamic_cast<const HdlModuleDef*>(o);
22,811✔
129
        if (mdef)
22,811✔
130
                return toPy(mdef);
13,096✔
131
        auto ci = dynamic_cast<const HdlCompInst*>(o);
9,715✔
132
        if (ci)
9,715✔
133
                return toPy(ci);
6,279✔
134
        auto ns = dynamic_cast<const HdlValueIdspace*>(o);
3,436✔
135
        if (ns)
3,436✔
136
                return toPy(ns);
961✔
137
        auto lib = dynamic_cast<const HdlLibrary*>(o);
2,475✔
138
        if (lib)
2,475✔
139
                return toPy(lib);
2,475✔
140

141
        std::string err_msg;
×
142
        if (o)
×
143
                err_msg = std::string("ToPy::toPy unknown type of iHdlObj:")
×
144
                                + std::string(typeid(*o).name());
×
145
        else
146
                err_msg = "ToPy::toPy called for nullptr";
×
147
        PyErr_SetString(PyExc_ValueError, err_msg.c_str());
×
148
        return nullptr;
×
149
}
×
150

151
int ToPy::toPy(const WithNameAndDoc *o, PyObject *py_inst) {
203,714✔
152
        if (!o) {
203,714✔
153
                PyErr_SetString(PyExc_ValueError, "ToPy::toPy invalid WithNameAndDoc*");
×
154
                Py_DECREF(py_inst);
155
                return -1;
×
156
        }
157
        int e;
158
        if (o->name.size()) {
203,714✔
159
                if (toPy_property(py_inst, "name", o->name))
192,539✔
160
                        return -1;
×
161
        }
162
        e = toPy(static_cast<const WithDoc*>(o), py_inst);
203,714✔
163
        if (e < 0)
203,714✔
164
                return e;
×
165
        return toPy(static_cast<const WithPos*>(o), py_inst);
203,714✔
166
}
167

168
int ToPy::toPy(const WithDoc *o, PyObject *py_inst) {
478,173✔
169
        if (o->__doc__.size())
478,173✔
170
                if (toPy_property(py_inst, "doc", o->__doc__))
26,999✔
171
                        return -1;
×
172
        return 0;
478,173✔
173
}
174

175
int ToPy::toPy(const WithPos *o, PyObject *py_inst) {
479,048✔
176
        if (toPy_property(py_inst, "position", o->position))
479,048✔
177
                return -1;
×
178
        return 0;
479,048✔
179
}
180

181
PyObject* ToPy::toPy(const CodePosition o) {
479,048✔
182
        PyObject *py_inst = PyObject_CallObject(CodePositionCls, NULL);
479,048✔
183
        if (!py_inst)
479,048✔
184
                return nullptr;
×
185
        if (toPy_property(py_inst, "start_line", o.start_line))
479,048✔
186
                return nullptr;
×
187
        if (toPy_property(py_inst, "start_column", o.start_column))
479,048✔
188
                return nullptr;
×
189
        if (toPy_property(py_inst, "stop_line", o.stop_line))
479,048✔
190
                return nullptr;
×
191
        if (toPy_property(py_inst, "stop_column", o.stop_column))
479,048✔
192
                return nullptr;
×
193
        return py_inst;
479,048✔
194
}
195

196
PyObject* ToPy::toPy(const HdlModuleDef *o) {
13,096✔
197
        PyObject *py_inst = PyObject_CallObject(HdlModuleDefCls, NULL);
13,096✔
198
        if (py_inst == nullptr)
13,096✔
199
                return nullptr;
×
200
        if (toPy(static_cast<const WithNameAndDoc*>(o), py_inst))
13,096✔
201
                return nullptr;
×
202
        if (o->module_name) {
13,096✔
203
                if (toPy_property(py_inst, "module_name", o->module_name))
13,096✔
204
                        return nullptr;
×
205
        }
206
        if (o->dec) {
13,096✔
207
                if (toPy_property(py_inst, "dec", o->dec))
10,758✔
208
                        return nullptr;
×
209
        }
210
        if (toPy_arr(py_inst, "objs", o->objs))
39,288✔
211
                return nullptr;
×
212

213
        return toPy_fillHdlAttributes(py_inst, o);
13,096✔
214
}
215

216
PyObject* ToPy::toPy(const hdlAst::HdlCompInst *o) {
6,279✔
217
        PyObject *py_inst = PyObject_CallObject(HdlCompInstCls, NULL);
6,279✔
218
        if (py_inst == nullptr)
6,279✔
219
                return nullptr;
×
220
        if (toPy_property(py_inst, "name", o->name))
6,279✔
221
                return nullptr;
×
222
        if (toPy_property(py_inst, "module_name", o->module_name))
6,279✔
223
                return nullptr;
×
224
        if (toPy_arr(py_inst, "param_map", o->genericMap))
18,837✔
225
                return nullptr;
×
226
        if (toPy_arr(py_inst, "port_map", o->portMap))
18,837✔
227
                return nullptr;
×
228

229
        return toPy_fillHdlAttributes(py_inst, o);
6,279✔
230
}
231

232
PyObject* ToPy::toPy(const HdlModuleDec *o) {
13,312✔
233
        PyObject *py_inst = PyObject_CallObject(HdlModuleDecCls, NULL);
13,312✔
234
        if (py_inst == nullptr)
13,312✔
235
                return nullptr;
×
236
        int e = toPy(static_cast<const WithNameAndDoc*>(o), py_inst);
13,312✔
237
        if (e)
13,312✔
238
                return nullptr;
×
239

240
        if (toPy_arr(py_inst, "params", o->generics))
39,936✔
241
                return nullptr;
×
242

243
        if (toPy_arr(py_inst, "ports", o->ports))
39,936✔
244
                return nullptr;
×
245

246
        if (toPy_arr(py_inst, "objs", o->objs))
39,936✔
247
                return nullptr;
×
248

249
        return toPy_fillHdlAttributes(py_inst, o);
13,312✔
250
}
251

252
PyObject* ToPy::toPy(const HdlFunctionDef *o) {
19,047✔
253
        PyObject *py_inst = PyObject_CallObject(HdlFunctionDefCls, NULL);
19,047✔
254
        if (py_inst == nullptr)
19,047✔
255
                return nullptr;
×
256
        if (toPy(static_cast<const WithNameAndDoc*>(o), py_inst))
19,047✔
257
                return nullptr;
×
258
        if (toPy_property(py_inst, "is_declaration_only", o->is_declaration_only))
19,047✔
259
                return nullptr;
×
260
        if (toPy_property(py_inst, "is_operator", o->is_operator))
19,047✔
261
                return nullptr;
×
262
        if (toPy_property(py_inst, "is_static", o->is_static))
19,047✔
263
                return nullptr;
×
264
        if (toPy_property(py_inst, "is_task", o->is_task))
19,047✔
265
                return nullptr;
×
266
        if (toPy_property(py_inst, "is_virtual", o->is_virtual))
19,047✔
267
                return nullptr;
×
268
        if (o->params) {
19,047✔
269
                if (toPy_arr(py_inst, "params", *o->params))
55,887✔
270
                        return nullptr;
×
271
        }
272
        if (o->returnT) {
19,047✔
273
                if (toPy_property(py_inst, "return_t", o->returnT))
14,666✔
274
                        return nullptr;
×
275
        }
276
        if (toPy_arr(py_inst, "body", o->body))
57,141✔
277
                return nullptr;
×
278

279
        return toPy_fillHdlAttributes(py_inst, o);
19,047✔
280
}
281

282
PyObject* ToPy::toPy(const HdlDirection o) {
154,823✔
283
        const char *name;
284
        try {
285
                name = HdlDirection_toString(o);
154,823✔
286
        } catch (const std::runtime_error &e) {
×
287
                PyErr_SetString(PyExc_ValueError, e.what());
×
288
                return nullptr;
×
289
        }
×
290
        return PyObject_GetAttrString(HdlDirectionEnum, name);
154,823✔
291
}
292

293
PyObject* ToPy::toPy(const HdlOp *o) {
703,944✔
294
        auto fn = toPy(o->op);
703,944✔
295
        if (!fn) {
703,944✔
296
                return nullptr;
×
297
        }
298
        auto ops = PyList_New(0);
703,944✔
299
        if (!ops) {
703,944✔
300
                Py_DECREF(fn);
301
                return nullptr;
×
302
        }
303
        if (toPy_arr(ops, o->operands)) {
703,944✔
304
                Py_DECREF(fn);
305
                return nullptr;
×
306
        }
307

308
        auto res = PyObject_CallFunctionObjArgs(HdlOpCls, fn, ops, NULL);
703,944✔
309
        Py_DECREF(fn);
310
        Py_DECREF(ops);
311
        return res;
703,944✔
312
}
313

314
// [TODO] too similar with the code for HdlModuleDef
315
PyObject* ToPy::toPy(const HdlValueIdspace *o) {
961✔
316
        PyObject *py_inst = PyObject_CallObject(HdlValueIdspaceCls, NULL);
961✔
317
        if (py_inst == nullptr)
961✔
318
                return nullptr;
×
319
        if (toPy(static_cast<const WithNameAndDoc*>(o), py_inst))
961✔
320
                return nullptr;
×
321
        if (toPy_arr(py_inst, "objs", o->objs))
2,883✔
322
                return nullptr;
×
323

324
        auto dec_only = PyBool_FromLong(o->defs_only);
961✔
325
        if (!dec_only) {
961✔
326
                Py_DECREF(py_inst);
327
                return nullptr;
×
328
        }
329

330
        if (PyObject_SetAttrString(py_inst, "declaration_only", dec_only)) {
961✔
331
                Py_DECREF(py_inst);
332
                return nullptr;
×
333
        }
334
        Py_DECREF(dec_only);
335

336
        return toPy_fillHdlAttributes(py_inst, o);
961✔
337
}
338

339
PyObject* ToPy::toPy(const HdlIdDef *o) {
154,823✔
340
        PyObject *py_inst = PyObject_CallObject(HdlIdDefCls, NULL);
154,823✔
341
        if (!py_inst)
154,823✔
342
                return nullptr;
×
343
        if (toPy(static_cast<const WithNameAndDoc*>(o), py_inst))
154,823✔
344
                return nullptr;
×
345

346
        if (toPy_property(py_inst, "type", o->type))
154,823✔
347
                return nullptr;
×
348
        if (o->value) {
154,823✔
349
                if (toPy_property(py_inst, "value", o->value))
41,583✔
350
                        return nullptr;
×
351
        } // else let it to be None
352
        if (toPy_property(py_inst, "is_latched", o->is_latched))
154,823✔
353
                return nullptr;
×
354
        if (toPy_property(py_inst, "is_const", o->is_const))
154,823✔
355
                return nullptr;
×
356
        if (toPy_property(py_inst, "is_static", o->is_static))
154,823✔
357
                return nullptr;
×
358
        if (toPy_property(py_inst, "is_shared", o->is_shared))
154,823✔
359
                return nullptr;
×
360
        if (toPy_property(py_inst, "direction", o->direction))
154,823✔
361
                return nullptr;
×
362
        return toPy_fillHdlAttributes(py_inst, o);
154,823✔
363
}
364

365
PyObject* ToPy::toPy(size_t o) {
1,916,192✔
366
        if (o == std::numeric_limits<size_t>::max()) {
1,916,192✔
367
                Py_RETURN_NONE;
7,192✔
368
        } else {
369
                return PyLong_FromLong((long) o);
1,909,000✔
370
        }
371
}
372

373
PyObject* ToPy::toPy(bool o) {
988,986✔
374
        return PyBool_FromLong((long) o);
988,986✔
375
}
376

377
PyObject* ToPy::toPy(const std::string &o) {
224,492✔
378
        return PyUnicode_FromString(o.c_str());
224,492✔
379
}
380

381
int ToPy::toPy_hdlAttributes(PyObject *parent, const iHdlObj *obj) {
484,452✔
382
        if (!obj->hdlAttributes)
484,452✔
383
                return 0;
483,735✔
384
        return toPy_arr(parent, "hdlAttributes", *obj->hdlAttributes);
2,151✔
385
}
386

387
PyObject* ToPy::toPy_fillHdlAttributes(PyObject *parent,
484,452✔
388
                const hdlAst::iHdlObj *obj) {
389
        if (toPy_hdlAttributes(parent, obj) < 0) {
484,452✔
NEW
390
                return nullptr;
×
391
        }
392
        return parent;
484,452✔
393
}
394

395
PyObject* ToPy::toPy(const hdlAst::iHdlObj::HdlAttribute &o) {
828✔
396
        auto id = toPy(o.id);
828✔
397
        if (!id)
828✔
NEW
398
                return nullptr;
×
399
        PyObject *val;
400
        if (o.value) {
828✔
401
                val = toPy(o.value);
230✔
402
                if (!val) {
230✔
403
                        Py_DECREF(id);
NEW
404
                        return nullptr;
×
405
                }
406
        } else {
407
                val = Py_None;
598✔
408
                Py_INCREF(val);
409
        }
410
        return PyTuple_Pack(2, id, val);
828✔
411
}
412

413
ToPy::~ToPy() {
18,368✔
414
        Py_XDECREF(HdlValueIdspaceCls);
18,368✔
415
        Py_XDECREF(HdlFunctionDefCls);
18,368✔
416
        Py_XDECREF(HdlCompInstCls);
18,368✔
417
        Py_XDECREF(HdlImportCls);
18,368✔
418
        Py_XDECREF(HdlLibraryCls);
18,368✔
419
        Py_XDECREF(HdlStmBlockCls);
18,368✔
420
        Py_XDECREF(HdlStmBlockJoinTypeCls);
18,368✔
421
        Py_XDECREF(HdlStmWaitCls);
18,368✔
422
        Py_XDECREF(HdlStmContinueCls);
18,368✔
423
        Py_XDECREF(HdlStmBreakCls);
18,368✔
424
        Py_XDECREF(HdlStmReturnCls);
18,368✔
425
        Py_XDECREF(HdlStmRepeatCls);
18,368✔
426
        Py_XDECREF(HdlStmNopCls);
18,368✔
427
        Py_XDECREF(HdlStmWhileCls);
18,368✔
428
        Py_XDECREF(HdlStmForCls);
18,368✔
429
        Py_XDECREF(HdlStmForInCls);
18,368✔
430
        Py_XDECREF(HdlStmCaseCls);
18,368✔
431
        Py_XDECREF(HdlStmCaseTypeEnum);
18,368✔
432
        Py_XDECREF(HdlStmCaseUniqConstrainEnum);
18,368✔
433
        Py_XDECREF(HdlStmProcessCls);
18,368✔
434
        Py_XDECREF(HdlStmProcessTriggerConstrainEnum);
18,368✔
435
        Py_XDECREF(HdlStmAssignCls);
18,368✔
436
        Py_XDECREF(HdlStmIfCls);
18,368✔
437
        Py_XDECREF(HdlOthersCls);
18,368✔
438
        Py_XDECREF(HdlAllCls);
18,368✔
439
        Py_XDECREF(HdlPhysicalDefCls);
18,368✔
440
        Py_XDECREF(HdlEnumDefCls);
18,368✔
441
        Py_XDECREF(HdlClassDefCls);
18,368✔
442
        Py_XDECREF(HdlClassTypeEnum);
18,368✔
443
        Py_XDECREF(HdlTypeSubtypeCls);
18,368✔
444
        Py_XDECREF(HdlTypeTypeCls);
18,368✔
445
        Py_XDECREF(HdlTypeAutoCls);
18,368✔
446
        Py_XDECREF(HdlDirectionEnum);
18,368✔
447
        Py_XDECREF(HdlValueIdCls);
18,368✔
448
        Py_XDECREF(HdlExprNotImplementedCls);
18,368✔
449
        Py_XDECREF(HdlValueIntCls);
18,368✔
450
        Py_XDECREF(HdlOpTypeEnum);
18,368✔
451
        Py_XDECREF(HdlOpCls);
18,368✔
452
        Py_XDECREF(HdlIdDefCls);
18,368✔
453
        Py_XDECREF(HdlModuleDecCls);
18,368✔
454
        Py_XDECREF(HdlModuleDefCls);
18,368✔
455
        Py_XDECREF(CodePositionCls);
18,368✔
456
        Py_XDECREF(ContextCls);
18,368✔
457
        Py_XDECREF(hdlAst_module);
18,368✔
458
}
18,368✔
459

460
const char* get_cpp_py_error_message() {
621✔
461
        try {
462
                throw;
621✔
463
        } catch (const ParseException &e) {
621✔
464
                return e.what();
618✔
465
        } catch (const std::exception &e) {
621✔
466
                return e.what();
3✔
467
        }
3✔
468
        return "<unknown C++ exception>";
469
}
470

471
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc