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

Nic30 / hdlConvertor / #200

10 Jun 2025 02:20PM UTC coverage: 60.002% (+6.0%) from 54.006%
#200

push

travis-ci

Nic30
build: fix propagation of '-Wno-overloaded-virtual' for antrl4

41577 of 69293 relevant lines covered (60.0%)

1047823.65 hits per line

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

65.31
/hdlConvertor/toPy.h
1
#pragma once
2

3
#define PY_SSIZE_T_CLEAN
4
#include <Python.h>
5
#include <vector>
6

7
#include <hdlConvertor/hdlAst/hdlCompInst.h>
8
#include <hdlConvertor/hdlAst/hdlContext.h>
9
#include <hdlConvertor/hdlAst/hdlLibrary.h>
10
#include <hdlConvertor/hdlAst/iHdlExpr.h>
11
#include <hdlConvertor/hdlAst/named.h>
12
#include <hdlConvertor/hdlAst/hdlOp.h>
13
#include <hdlConvertor/hdlAst/hdlOpType.h>
14
#include <hdlConvertor/hdlAst/hdlNamespace.h>
15
#include <hdlConvertor/hdlAst/hdlModuleDec.h>
16
#include <hdlConvertor/hdlAst/hdlModuleDef.h>
17
#include <hdlConvertor/hdlAst/hdlIdDef.h>
18
#include <hdlConvertor/hdlAst/hdlFunctionDef.h>
19
#include <hdlConvertor/hdlAst/iHdlStatement.h>
20
#include <hdlConvertor/hdlAst/hdlStm_others.h>
21
#include <hdlConvertor/hdlAst/hdlStmAssign.h>
22
#include <hdlConvertor/hdlAst/hdlStmBlock.h>
23
#include <hdlConvertor/hdlAst/hdlStmCase.h>
24
#include <hdlConvertor/hdlAst/hdlStmExpr.h>
25
#include <hdlConvertor/hdlAst/hdlStmFor.h>
26
#include <hdlConvertor/hdlAst/hdlStmIf.h>
27
#include <hdlConvertor/hdlAst/hdlStmProcess.h>
28
#include <hdlConvertor/hdlAst/hdlStmWhile.h>
29
#include <hdlConvertor/hdlAst/hdlTypes.h>
30
#include <hdlConvertor/hdlAst/hdlValue.h>
31

32
namespace hdlConvertor {
33

34
class ToPy {
35
        PyObject *hdlAst_module;
36
        PyObject *ContextCls;
37
        PyObject *CodePositionCls;
38
        PyObject *HdlModuleDefCls;
39
        PyObject *HdlModuleDecCls;
40
        PyObject *HdlIdDefCls;
41
        PyObject *HdlOpCls;
42
        PyObject *HdlOpTypeEnum;
43
        PyObject *HdlValueIntCls;
44
        PyObject *HdlExprNotImplementedCls;
45
        PyObject *HdlValueIdCls;
46
        PyObject *HdlDirectionEnum;
47
        PyObject *HdlAllCls;
48
        PyObject *HdlOthersCls;
49
        PyObject *HdlTypeAutoCls;
50
        PyObject *HdlTypeTypeCls;
51
        PyObject *HdlTypeSubtypeCls;
52
        PyObject *HdlClassTypeEnum;
53
        PyObject *HdlClassDefCls;
54
        PyObject *HdlPhysicalDefCls;
55
        PyObject *HdlEnumDefCls;
56
        PyObject *HdlStmIfCls;
57
        PyObject *HdlStmAssignCls;
58
        PyObject *HdlStmProcessTriggerConstrainEnum;
59
        PyObject *HdlStmProcessCls;
60
        PyObject *HdlStmCaseUniqConstrainEnum;
61
        PyObject *HdlStmCaseTypeEnum;
62
        PyObject *HdlStmCaseCls;
63
        PyObject *HdlStmForCls;
64
        PyObject *HdlStmForInCls;
65
        PyObject *HdlStmWhileCls;
66
        PyObject *HdlStmNopCls;
67
        PyObject *HdlStmRepeatCls;
68
        PyObject *HdlStmReturnCls;
69
        PyObject *HdlStmBreakCls;
70
        PyObject *HdlStmContinueCls;
71
        PyObject *HdlStmWaitCls;
72
        PyObject *HdlStmBlockCls;
73
        PyObject *HdlStmBlockJoinTypeCls;
74
        PyObject *HdlImportCls;
75
        PyObject *HdlLibraryCls;
76
        PyObject *HdlCompInstCls;
77
        PyObject *HdlFunctionDefCls;
78
        PyObject *HdlValueIdspaceCls;
79

80
        std::string PyObject_repr(PyObject *o);
81

82
        template<typename OBJ_T>
83
        int toPy_dict(PyObject *parent, const std::string &prop_name,
84
                        const std::vector<std::pair<std::string, OBJ_T>> &objs) {
85
                PyObject *parent_dict = PyObject_GetAttrString(parent,
86
                                prop_name.c_str());
87
                if (parent_dict == nullptr) {
88
                        std::string err_msg = (std::string(
89
                                        "ToPy::toPy_arr object does not have specified property:")
90
                                        + prop_name + std::string(" : ") + PyObject_repr(parent));
91
                        Py_DECREF(parent);
92
                        PyErr_SetString(PyExc_ValueError, err_msg.c_str());
93
                        return -1;
94
                }
95
                for (const auto &o : objs) {
96
                        const auto &key = o.first;
97
                        const auto &py_val = toPy(o.second);
98
                        if (py_val == nullptr) {
99
                                Py_DECREF(parent_dict);
100
                                return -1;
101
                        }
102
                        auto e = PyDict_SetItemString(parent_dict, key.c_str(), py_val);
103
                        Py_DECREF(py_val); // needed? docs say SetItem doesn't steal a reference
104
                        if (e) {
105
                                Py_DECREF(parent_dict);
106
                                return e;
107
                        }
108
                }
109
                Py_DECREF(parent_dict);
110
                return 0;
111
        }
112

113
        template<typename OBJ_T>
114
        int toPy_arr(PyObject *parent, const std::string &prop_name,
549,193✔
115
                        const std::vector<OBJ_T> &objs) {
116
                PyObject *parent_list = PyObject_GetAttrString(parent,
549,193✔
117
                                prop_name.c_str());
118
                if (parent_list == nullptr) {
549,193✔
119
                        std::string err_msg = (std::string(
×
120
                                        "ToPy::toPy_arr object does not have specified property: ")
121
                                        + prop_name + std::string(" : ") + PyObject_repr(parent));
×
122
                        Py_DECREF(parent);
123
                        PyErr_SetString(PyExc_ValueError, err_msg.c_str());
×
124
                        return -1;
×
125
                } else if (Py_IsNone(parent_list)) {
549,193✔
126
                        parent_list = PyList_New(0);
717✔
127
                        if (!parent_list)
717✔
128
                                return -1;
×
129
                        if (PyObject_SetAttrString(parent, prop_name.c_str(), parent_list)
717✔
130
                                        < 0) {
717✔
131
                                std::string err_msg =
×
132
                                                (std::string(
×
133
                                                                "ToPy::toPy_arr parent object has currently None stored in specified attribute and setting it to a new list have failed:")
134
                                                                + prop_name + std::string(" : ")
×
135
                                                                + PyObject_repr(parent));
×
136
                                Py_DECREF(parent);
137
                                PyErr_SetString(PyExc_ValueError, err_msg.c_str());
×
138
                                return -1;
×
139
                        }
×
140
                }
141
                auto res = toPy_arr<OBJ_T>(parent_list, objs);
549,193✔
142
                if (res)
549,193✔
143
                        Py_DECREF(parent_list); // decr because we do not longer use porowed property
144
                return res;
549,193✔
145
        }
146

147
        template<typename OBJ_T>
148
        int toPy_arr(PyObject *py_list, const std::vector<OBJ_T> &objs) {
1,264,139✔
149
                for (auto &o : objs) {
3,367,733✔
150
                        auto py_obj = toPy(o);
2,103,594✔
151
                        if (py_obj == nullptr) {
2,103,594✔
152
                                Py_DECREF(py_list);
153
                                return -1;
×
154
                        }
155
                        auto e = PyList_Append(py_list, py_obj);
2,103,594✔
156
                        Py_DECREF(py_obj);
157
                        if (e) {
2,103,594✔
158
                                Py_DECREF(py_list);
159
                                return e;
×
160
                        }
161
                }
162
                return 0;
1,264,139✔
163
        }
164

165
        int toPy_property(PyObject *py_inst, const char *prop_name,
4,172,455✔
166
                        PyObject *py_o) {
167
                int e = PyObject_SetAttrString(py_inst, prop_name, py_o);
4,172,455✔
168
                Py_DECREF(py_o);
169
                if (e < 0) {
4,172,455✔
170
                        Py_DECREF(py_inst);
171
                        return -1;
×
172
                }
173
                return 0;
4,172,455✔
174
        }
175
        template<typename OBJ_T>
176
        int toPy_property(PyObject *py_inst, const char *prop_name,
411,422✔
177
                        const std::unique_ptr<OBJ_T> &o) {
178
                auto py_o = toPy(o.get());
411,422✔
179
                if (!py_o) {
411,422✔
180
                        Py_DECREF(py_inst);
181
                        return -1;
×
182
                }
183
                return toPy_property(py_inst, prop_name, py_o);
411,422✔
184
        }
185
        template<typename OBJ_T>
186
        int toPy_property(PyObject *py_inst, const char *prop_name,
3,761,033✔
187
                        const OBJ_T &o) {
188
                auto py_o = toPy(o);
3,761,033✔
189
                if (!py_o) {
3,761,033✔
190
                        Py_DECREF(py_inst);
191
                        return -1;
×
192
                }
193
                return toPy_property(py_inst, prop_name, py_o);
3,761,033✔
194
        }
195

196
        int toPy_hdlAttributes(PyObject *parent, const hdlAst::iHdlObj *obj);
197
        PyObject * toPy_fillHdlAttributes(PyObject *parent, const hdlAst::iHdlObj *obj);
198
public:
199
        ToPy();
200

201
        // automatic conversion from std::unique_ptr<T> to const T * for any type
202
        template<typename T>
203
        PyObject* toPy(const std::unique_ptr<T> &o) {
2,394,324✔
204
                return toPy(o.get());
2,394,324✔
205
        }
206

207
        int toPy(const hdlAst::WithNameAndDoc *o, PyObject *py_inst);
208
        int toPy(const hdlAst::WithDoc *o, PyObject *py_inst);
209
        int toPy(const hdlAst::WithPos *o, PyObject *py_inst);
210

211
        PyObject* toPy(const hdlAst::HdlExprAndiHdlObj &o);
212
        PyObject* toPy(const hdlAst::iHdlStatement *o);
213
        PyObject* toPy(const hdlAst::HdlModuleDef *o);
214
        PyObject* toPy(const hdlAst::HdlCompInst *o);
215
        PyObject* toPy(const hdlAst::HdlContext *o);
216
        PyObject* toPy(const hdlAst::HdlLibrary *o);
217
        PyObject* toPy(const hdlAst::HdlDirection o);
218
        PyObject* toPy(const hdlAst::HdlModuleDec *o);
219
        PyObject* toPy(const hdlAst::iHdlExprItem *o);
220
        PyObject* toPy(const hdlAst::HdlOp *o);
221
        PyObject* toPy(const hdlAst::HdlValueArr *o);
222
        PyObject* toPy(const hdlAst::HdlValueFloat *o);
223
        PyObject* toPy(const hdlAst::HdlValueId *o);
224
        PyObject* toPy(const hdlAst::HdlValueInt *o);
225
        PyObject* toPy(const hdlAst::HdlValueStr *o);
226
        PyObject* toPy(const hdlAst::HdlValueSymbol *o);
227
        PyObject* toPy(const hdlAst::HdlClassType o);
228
        PyObject* toPy(const hdlAst::HdlClassDef *o);
229
        PyObject* toPy(const hdlAst::HdlPhysicalDef *o);
230
        PyObject* toPy(const hdlAst::HdlEnumDef *o);
231
        PyObject* toPy(const hdlAst::HdlExprNotImplemented *o);
232
        PyObject* toPy(const hdlAst::HdlFunctionDef *o);
233
        PyObject* toPy(const hdlAst::iHdlObj *o);
234
        PyObject* toPy(const hdlAst::HdlOpType o);
235
        PyObject* toPy(const hdlAst::HdlValueIdspace *o);
236
        PyObject* toPy(const hdlAst::HdlIdDef *o);
237
        PyObject* toPy(const hdlAst::HdlStmExpr *o);
238
        PyObject* toPy(const hdlAst::HdlStmIf *o);
239
        PyObject* toPy(const hdlAst::HdlStmBlockJoinType o);
240
        PyObject* toPy(const hdlAst::HdlStmBlock *o);
241
        PyObject* toPy(const hdlAst::HdlStmCaseType o);
242
        PyObject* toPy(const hdlAst::HdlStmCaseUniqConstrain o);
243
        PyObject* toPy(const hdlAst::HdlStmCase *o);
244
        PyObject* toPy(const hdlAst::HdlStmFor *o);
245
        PyObject* toPy(const hdlAst::HdlStmForIn *o);
246
        PyObject* toPy(const hdlAst::HdlStmNop *o);
247
        PyObject* toPy(const hdlAst::HdlStmRepeat *o);
248
        PyObject* toPy(const hdlAst::HdlStmReturn *o);
249
        PyObject* toPy(const hdlAst::HdlStmAssign *o);
250
        PyObject* toPy(const hdlAst::HdlStmWhile *o);
251
        PyObject* toPy(const hdlAst::HdlStmProcessTriggerConstrain o);
252
        PyObject* toPy(const hdlAst::HdlStmProcess *o);
253
        PyObject* toPy(const hdlAst::HdlStmWait *o);
254
        PyObject* toPy(const hdlAst::HdlStmImport *o);
255
        PyObject* toPy(const hdlAst::CodePosition o);
256
        PyObject* toPy(const std::string &o);
257
        PyObject* toPy(size_t o);
258
        PyObject* toPy(bool o);
259
        PyObject* toPy(
260
                        const std::pair<const std::string*, const hdlAst::iHdlExprItem*> &o);
261
        PyObject* toPy(
262
                        const std::pair<std::unique_ptr<std::string>,
263
                                        std::unique_ptr<hdlAst::iHdlExprItem>> &o);
264
        PyObject* toPy(
265
                        const std::pair<std::string, std::unique_ptr<hdlAst::iHdlExprItem>> &o);
266
        PyObject* toPy(const hdlAst::iHdlObj::HdlAttribute &o);
267

268
        ~ToPy();
269
};
270

271
extern const char* get_cpp_py_error_message();
272

273
}
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

© 2025 Coveralls, Inc