-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathpykokkos_visitor.py
More file actions
749 lines (572 loc) · 26.3 KB
/
pykokkos_visitor.py
File metadata and controls
749 lines (572 loc) · 26.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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
import ast
from ast import FunctionDef, AST
import os
import re
import sys
from typing import List, Dict, Optional, Set, Union
from pykokkos.core import cppast
from pykokkos.core.optimizations import (
adjust_kokkos_function_call,
get_restrict_ptr_name,
index_restrict_view,
)
from pykokkos.interface import View
from . import visitors_util
class PyKokkosVisitor(ast.NodeVisitor):
def __init__(
self,
env,
src,
views: Dict[cppast.DeclRefExpr, cppast.Type],
work_units: Dict[str, FunctionDef],
fields: Dict[cppast.DeclRefExpr, cppast.PrimitiveType],
kokkos_functions: Dict[str, FunctionDef],
dependency_methods: Dict[str, List[str]],
pk_import: str,
restrict_views: Set[str],
debug=False,
):
self.env = env
self.src = src
self.views = views
self.work_units = work_units
self.fields = fields
self.kokkos_functions = kokkos_functions
self.dependency_methods = dependency_methods
self.pk_import = pk_import
self.restrict_views = restrict_views
self.debug = debug
# Map from subview to parent view
self.subviews: Dict[str, str] = {}
self.pkmain_views: Dict[str, View] = {}
self.lists: List[str] = []
# Maps from nested work unit name to definition
self.nested_work_units: Dict[str, cppast.LambdaExpr] = {}
def visit_arguments(self, node: ast.arguments) -> List[cppast.ParmVarDecl]:
args: List[cppast.ParmVarDecl] = [
self.visit(a) for a in node.args if a.arg != "self"
]
return args
def visit_arg(self, node: ast.arg) -> Union[cppast.ParmVarDecl, str]:
if node.arg == "self":
return ""
if node.annotation is None:
self.error(node, "Missing type annotation")
decltype: cppast.Type = visitors_util.get_type(node.annotation, self.pk_import)
if decltype is None:
self.error(node, "Type not supported")
if self.is_dependency(decltype):
decltype.is_reference = True
declname = cppast.DeclRefExpr(node.arg)
if isinstance(decltype, cppast.ClassType) and decltype.typename.startswith(
"View"
):
decltype = self.views[declname]
decltype = visitors_util.cpp_view_type(decltype)
arg = cppast.ParmVarDecl(decltype, declname)
return arg
def visit_Assign(
self, node: ast.Assign
) -> Union[cppast.AssignOperator, cppast.DeclStmt]:
for target in node.targets:
if (
# TODO: check if target.value.id is in scope
(isinstance(target, ast.Attribute) and target.value.id == "self")
and type(target) not in {ast.Name, ast.Subscript}
):
self.error(
target,
"Only local variables and views supported for assignment",
)
# handle subview
if isinstance(node.value, ast.Subscript):
if (
sys.version_info.minor <= 8
and not isinstance(node.value.slice, ast.Index)
) or (
sys.version_info.minor > 8 and isinstance(node.value.slice, ast.Tuple)
):
view = node.value.value
if isinstance(view, ast.Attribute) and view.value.id == "self":
# reference view through self
attr = node.value.value
view_name = view.attr
elif isinstance(view, ast.Name):
# reference views through params (standalone)
view_name = view.id
else:
self.error(view, "View not recognized")
if cppast.DeclRefExpr(view_name) in self.views:
return self.generate_subview(node, view_name)
else:
self.error(node, "Can only take subview of views")
targets: List[cppast.DeclRefExpr] = [self.visit(t) for t in node.targets]
value: cppast.Expr = self.visit(node.value)
op: cppast.BinaryOperatorKind = cppast.BinaryOperatorKind.Assign
assign = cppast.AssignOperator(targets, value, op)
return assign
def generate_subview(self, node: ast.Assign, view_name: str) -> cppast.DeclStmt:
subview_args: List[cppast.Expr] = [cppast.DeclRefExpr(view_name)]
slice_node = node.value
for dim in slice_node.slice.elts:
# In Python >= 3.9, ast.Index is deprecated
# (see # https://docs.python.org/3/whatsnew/3.9.html)
# Instead of ast.Index, value will be used directly
check_type: type
if sys.version_info.minor <= 8:
check_type: type = ast.Index
else:
check_type: type = ast.Name
if isinstance(dim, check_type):
subview_args.append(self.visit(dim))
else:
if dim.lower is None and dim.upper is None:
subview_args.append(cppast.DeclRefExpr("Kokkos::ALL"))
elif dim.lower is not None and dim.upper is not None:
make_pair = cppast.CallExpr(
"std::make_pair", [self.visit(dim.lower), self.visit(dim.upper)]
)
subview_args.append(make_pair)
else:
self.error(
slice_node, "Partial slice not supported, use [n:m] or [:]"
)
if len(node.targets) > 1:
self.error(node, "Multiple declarations of subview not supported")
auto = cppast.ClassType("auto")
target = node.targets[0]
target_ref = cppast.DeclRefExpr(target.id)
if target_ref in self.views:
self.error(node, "Redeclaration of existing subview")
else:
self.views[target_ref] = None
self.subviews[target_ref.declname] = view_name
call = cppast.CallExpr("Kokkos::subview", subview_args)
decl = cppast.DeclStmt(cppast.VarDecl(auto, self.visit(target), call))
return decl
def visit_AugAssign(self, node: ast.AugAssign) -> cppast.CompoundAssignOperator:
variable: cppast.DeclRefExpr = self.visit(node.target)
value: cppast.Expr = self.visit(node.value)
op: cppast.BinaryOperatorKind = self.visit(node.op)
augassign = cppast.CompoundAssignOperator(variable, value, op)
return augassign
def visit_Attribute(self, node: ast.Attribute) -> cppast.DeclRefExpr:
if (
not isinstance(node.value, ast.Name)
# TODO: implement proper scope checking
# or node.value.id not in ("self", "math")
):
self.error(node, "Unrecognized attribute")
# Math constant
if node.value.id == "math":
try:
constant: str = visitors_util.get_math_constant_str(node.attr)
except NotImplementedError:
self.error(node, "Unrecognized math constant")
return cppast.DeclRefExpr(constant)
if node.value.id == "self":
name: str = node.attr
if cppast.DeclRefExpr(name) in self.views:
return name
# if name not in self.env:
# self.error(node, "Couldn't find variable")
field = cppast.MemberExpr(cppast.DeclRefExpr("this"), name)
field.is_pointer = True
return field
return cppast.DeclRefExpr(f"{node.value.id}.{node.attr}")
def visit_Constant(self, node: ast.Constant) -> Union[
cppast.BoolLiteral,
cppast.FloatingLiteral,
cppast.IntegerLiteral,
cppast.StringLiteral,
]:
if isinstance(node.value, bool):
return cppast.BoolLiteral(node.value)
if isinstance(node.value, float):
return cppast.FloatingLiteral(node.value)
if isinstance(node.value, int):
return cppast.IntegerLiteral(node.value)
if isinstance(node.value, str):
return cppast.StringLiteral(node.value)
self.error(node, "Unsupported Constant")
def visit_Subscript(
self, node: ast.Subscript
) -> Union[cppast.ArraySubscriptExpr, cppast.CallExpr]:
current_node: ast.Subscript = node
slices: List = []
dim: int = 0
while isinstance(current_node, ast.Subscript):
index = current_node.slice
if sys.version_info.minor <= 8:
# In Python >= 3.9, ast.Index is deprecated
# (see # https://docs.python.org/3/whatsnew/3.9.html)
# Instead of ast.Index, value will be used directly
if not isinstance(index, ast.Index):
self.error(current_node, "Slices not supported, use simple indices")
slices.insert(0, index)
current_node = current_node.value
dim += 1
name: str = visitors_util.get_node_name(current_node)
ref = cppast.DeclRefExpr(name)
if ref not in self.views and name not in self.lists:
self.error(current_node, "Unknown view or list")
dim_map: List = [
cppast.ClassType("View1D"),
cppast.ClassType("View2D"),
cppast.ClassType("View3D"),
cppast.ClassType("View4D"),
cppast.ClassType("View5D"),
cppast.ClassType("View6D"),
cppast.ClassType("View7D"),
cppast.ClassType("View8D"),
]
if name in self.lists:
indices: List[cppast.Expr] = [self.visit(s) for s in slices]
subscript = cppast.ArraySubscriptExpr(ref, indices)
return subscript
if ref in self.views and (
self.views[ref] is None # For views added in @pk.main
or self.views[ref].typename == dim_map[dim - 1].typename
):
args: List[cppast.Expr] = [self.visit(s) for s in slices]
# Account for fused views
r = re.search("fused_(.*)_[0-9]*", ref.declname)
unfused_name: str = r.group(1) if r else ref.declname
if (
"PK_RESTRICT" in os.environ
and unfused_name in self.restrict_views
or name in self.restrict_views
):
if unfused_name in self.restrict_views:
v = self.restrict_views[unfused_name]
else:
v = self.restrict_views[name]
subscript = index_restrict_view(ref, args, v)
else:
subscript = cppast.CallExpr(ref, args)
return subscript
self.error(node, f"'{name}' is not a View{dim}D")
def visit_BinOp(
self, node: ast.BinOp
) -> Union[cppast.BinaryOperator, cppast.CallExpr, cppast.CastExpr]:
lhs = cppast.ParenExpr(self.visit(node.left))
rhs = cppast.ParenExpr(self.visit(node.right))
if isinstance(node.op, ast.Pow):
return cppast.CallExpr(cppast.DeclRefExpr("pow"), [lhs, rhs])
op: cppast.BinaryOperatorKind = self.visit(node.op)
if isinstance(node.op, ast.Div):
# Cast one of the operands to a double
lhs = cppast.CastExpr(cppast.PrimitiveType(cppast.BuiltinType.DOUBLE), lhs)
binop = cppast.BinaryOperator(lhs, rhs, op)
if isinstance(node.op, ast.FloorDiv):
# Cast the result to an int
cast = cppast.CastExpr(cppast.PrimitiveType(cppast.BuiltinType.INT), binop)
return cast
return binop
def visit_UnaryOp(self, node: ast.UnaryOp) -> cppast.UnaryOperator:
op: cppast.BinaryOperatorKind = self.visit(node.op)
operand: cppast.Expr = self.visit(node.operand)
unop = cppast.UnaryOperator(operand, op)
return unop
def visit_Compare(self, node: ast.Compare) -> cppast.BinaryOperator:
if len(node.comparators) > 1:
# TODO: possibly break out into multiple comparisons
self.error(
node.comparators[1],
"Chaining comparisons not supported for translation",
)
left: cppast.Expr = self.visit(node.left)
op: cppast.BinaryOperatorKind = self.visit(node.ops[0])
comparators: cppast.Expr = self.visit(node.comparators[0])
compare = cppast.BinaryOperator(left, comparators, op)
return compare
def visit_BoolOp(self, node: ast.BoolOp) -> cppast.BoolOperator:
exprs: List[cppast.Expr] = [self.visit(v) for v in node.values]
op: cppast.BinaryOperatorKind = self.visit(node.op)
boolop = cppast.BoolOperator(exprs, op)
return boolop
def visit_Name(self, node: ast.Name) -> cppast.DeclRefExpr:
return cppast.DeclRefExpr(node.id)
def visit_Index(self, node: ast.Index) -> cppast.Expr:
return self.visit(node.value)
def visit_For(self, node: ast.For) -> cppast.ForStmt:
if not isinstance(node.target, ast.Name):
self.error(node.target, "Must use single loop variable")
if node.orelse:
self.error(node.orelse, "Else clause not supported for translation")
if not isinstance(node.iter, ast.Call) or node.iter.func.id != "range":
# TODO: support other iterators?
self.error(node.iter, "Only range() iterator is supported for translation")
index: cppast.DeclRefExpr = self.visit(node.target)
start: cppast.Expr
end: cppast.Expr
step: cppast.Expr = cppast.IntegerLiteral(1)
op = cppast.BinaryOperatorKind.LT
args = node.iter.args
if len(args) == 1:
start = cppast.IntegerLiteral(0)
end = self.visit(args[0])
else:
start = self.visit(args[0])
end = self.visit(args[1])
if len(args) == 3:
step = self.visit(args[2])
# Negative step sizes are only handled correctly if they're
# written with a preceeding minus sign
if isinstance(args[2], ast.UnaryOp) and isinstance(
args[2].op, ast.USub
):
op = cppast.BinaryOperatorKind.GT
body = cppast.CompoundStmt([self.visit(b) for b in node.body])
init = cppast.DeclStmt(
cppast.VarDecl(cppast.PrimitiveType(cppast.BuiltinType.INT), index, start)
)
condition = cppast.BinaryOperator(index, end, op)
increment = cppast.BinaryOperator(
index, step, cppast.BinaryOperatorKind.AddAssign
)
forstmt = cppast.ForStmt(init, condition, increment, body)
return forstmt
def visit_If(self, node: ast.If) -> cppast.IfStmt:
condition: cppast.Expr = self.visit(node.test)
then_body = cppast.CompoundStmt([self.visit(b) for b in node.body])
else_body = (
cppast.CompoundStmt([self.visit(b) for b in node.orelse])
if node.orelse
else None
)
ifstmt = cppast.IfStmt(condition, then_body, else_body)
return ifstmt
def visit_While(self, node: ast.While) -> cppast.WhileStmt:
if node.orelse:
self.error(node.orelse, "Else clause not supported for translation")
condition: cppast.Expr = self.visit(node.test)
body = cppast.CompoundStmt([self.visit(b) for b in node.body])
whilestmt = cppast.WhileStmt(condition, body)
return whilestmt
def visit_Call(self, node: ast.Call) -> cppast.CallExpr:
name: str = visitors_util.get_node_name(node.func)
if name == "print":
self.error(
node.func, "Function not supported, did you mean pykokkos.printf()?"
)
elif name in ["PerTeam", "PerThread", "fence"]:
name = "Kokkos::" + name
elif name in {"complex32", "complex64"}:
name = "Kokkos::complex"
if "32" in name:
name += "<float>"
else:
name += "<double>"
function = cppast.DeclRefExpr(name)
args: List[cppast.Expr] = [self.visit(a) for a in node.args]
if visitors_util.is_math_function(name) or name in [
"printf",
"abs",
"Kokkos::PerTeam",
"Kokkos::PerThread",
"Kokkos::fence",
"Kokkos::complex<float>",
"Kokkos::complex<double>",
]:
return cppast.CallExpr(function, args)
if name in visitors_util.math_special_functions:
function = cppast.DeclRefExpr("Kokkos::Experimental::" + name)
return cppast.CallExpr(function, args)
if function in self.kokkos_functions:
if "PK_RESTRICT" in os.environ:
return adjust_kokkos_function_call(
function, args, self.restrict_views, self.views
)
else:
return cppast.CallExpr(function, args)
# Call to a dependency's constructor
if function.declname in visitors_util.allowed_types:
name = visitors_util.allowed_types[name]
function = cppast.DeclRefExpr(name)
return cppast.CallExpr(function, args)
# Call to a dependency's method
for key, value in self.dependency_methods.items():
if function in value:
object_name: cppast.DeclRefExpr = self.visit(node.func.value)
return cppast.MemberCallExpr(object_name, function, args)
self.error(node.func, f"Function {name} not supported for translation")
def visit_Return(self, node: ast.Return) -> cppast.ReturnStmt:
parent_function: FunctionDef = self.get_parent_function(node)
if parent_function is None:
self.error(node, "Cannot return outside of function")
if node.value:
if cppast.DeclRefExpr(parent_function.name) in self.kokkos_functions:
return cppast.ReturnStmt(self.visit(node.value))
else:
self.error(node.value, "Cannot return value from translated function")
return cppast.ReturnStmt()
def visit_Break(self, node: ast.Break) -> cppast.BreakStmt:
return cppast.BreakStmt()
def visit_Continue(self, node: ast.Continue) -> cppast.ContinueStmt:
return cppast.ContinueStmt()
def visit_Pass(self, node: ast.Pass) -> cppast.EmptyStmt:
return cppast.EmptyStmt()
def visit_AnnAssign(self, node: ast.AnnAssign) -> cppast.DeclStmt:
if not isinstance(node.target, ast.Name):
self.generic_error(node)
decltype: cppast.Type = visitors_util.get_type(node.annotation, self.pk_import)
if decltype is None:
self.error(node, "Type not supported")
target: cppast.DeclRefExpr = self.visit(node.target)
value: cppast.Expr = self.visit(node.value)
# Add the name of the list to self.lists
# and add the length of the list to its name
if isinstance(node.annotation, ast.Subscript):
self.lists.append(target.declname)
target.add_length(len(node.value.elts))
annassign = cppast.DeclStmt(cppast.VarDecl(decltype, target, value))
return annassign
def visit_NamedExpr(self, node: ast.NamedExpr) -> str:
self.generic_error(node) # TODO: check if needed
def visit_Assert(self, node: ast.Assert) -> str:
self.generic_error(node) # TODO: test if possible
def visit_IfExp(self, node: ast.IfExp) -> str:
self.generic_error(node) # TODO: test if possible (maybe ternary)
def visit_Tuple(self, node: ast.Tuple) -> str:
self.generic_error(node) # TODO: test if possible
def visit_Expr(self, node: ast.Expr) -> cppast.CallStmt:
# This is needed for docstrings
if isinstance(node.value, ast.Constant):
return cppast.EmptyStmt()
if not isinstance(node.value, ast.Call):
self.error(node, "Only function calls are allowed as standalone statements")
call: cppast.CallExpr = self.visit(node.value)
return cppast.CallStmt(call)
def visit_Delete(self, node: ast.Delete) -> str:
self.generic_error(node)
def visit_Global(self, node: ast.Global) -> str:
self.generic_error(node)
# ignore nonlocal
def visit_Nonlocal(self, node: ast.Nonlocal) -> str:
return ""
def visit_Yield(self, node: ast.Yield) -> str:
self.generic_error(node)
def visit_With(self, node: ast.With) -> str:
self.generic_error(node)
def visit_List(self, node: ast.List) -> cppast.InitListExpr:
exprs: List[cppast.Expr] = [self.visit(e) for e in node.elts]
initlist = cppast.InitListExpr(exprs)
return initlist
def visit_ListComp(self, node: ast.ListComp) -> str:
self.generic_error(node)
def visit_GeneratorExp(self, node: ast.GeneratorExp) -> str:
self.generic_error(node)
def visit_Slice(self, node: ast.slice) -> str:
self.generic_error(node)
def visit_UAdd(self, node: ast.UAdd) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.Plus
def visit_USub(self, node: ast.USub) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.Minus
def visit_Not(self, node: ast.Not) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.LNot
def visit_Invert(self, node: ast.Invert) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.Not
def visit_Add(self, node: ast.Add) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.Add
def visit_Sub(self, node: ast.Sub) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.Sub
def visit_Mult(self, node: ast.Mult) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.Mul
def visit_Div(self, node: ast.Div) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.Div
def visit_FloorDiv(self, node: ast.FloorDiv) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.Div
def visit_Mod(self, node: ast.Mod) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.Rem
def visit_Pow(self, node: ast.Pow) -> cppast.BinaryOperatorKind:
self.generic_error(node)
def visit_LShift(self, node: ast.LShift) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.Shl
def visit_RShift(self, node: ast.RShift) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.Shr
def visit_BitOr(self, node: ast.BitOr) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.Or
def visit_BitXor(self, node: ast.BitXor) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.Xor
def visit_BitAnd(self, node: ast.BitAnd) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.And
def visit_MatMult(self, node: ast.MatMult) -> cppast.BinaryOperatorKind:
self.generic_error(node)
def visit_And(self, node: ast.And) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.LAnd
def visit_Or(self, node: ast.Or) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.LOr
def visit_Eq(self, node: ast.Eq) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.EQ
def visit_NotEq(self, node: ast.NotEq) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.NE
def visit_Lt(self, node: ast.Lt) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.LT
def visit_LtE(self, node: ast.LtE) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.LE
def visit_Gt(self, node: ast.Gt) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.GT
def visit_GtE(self, node: ast.GtE) -> cppast.BinaryOperatorKind:
return cppast.BinaryOperatorKind.GE
def visit_Is(self, node: ast.Is) -> cppast.BinaryOperatorKind:
self.generic_error(node)
def visit_IsNot(self, node: ast.IsNot) -> cppast.BinaryOperatorKind:
self.generic_error(node)
def visit_In(self, node: ast.In) -> cppast.BinaryOperatorKind:
self.generic_error(node)
def visit_NotIn(self, node: ast.NotIn) -> cppast.BinaryOperatorKind:
self.generic_error(node)
# Returns the parent function of a node
def get_parent_function(self, node: AST) -> FunctionDef:
while hasattr(node, "parent"):
if isinstance(node.parent, FunctionDef):
return node.parent
node = node.parent
return None
def is_in_loop(self, node: AST) -> bool:
while hasattr(node, "parent"):
if isinstance(node.parent, ast.For) or isinstance(node.parent, ast.While):
return True
node = node.parent
return False
def is_void_function(self, node: ast.FunctionDef) -> bool:
if node.returns is None or (
isinstance(node.returns, ast.Constant) and node.returns.value is None
):
return True
return False
def is_dependency(self, input_type: cppast.Type) -> bool:
if isinstance(input_type, cppast.PrimitiveType):
return False
classref = cppast.DeclRefExpr(input_type.typename)
if classref in self.dependency_methods:
return True
return False
def get_scratch_view_type(self, view_type: ast.Subscript) -> Optional[str]:
"""
Get the cppast representation of a scratch view
:param view_type: the subscripted type of the view
:returns: the string representation of the view if it is valid
"""
is_valid: bool = False
if isinstance(view_type, ast.Subscript) and isinstance(
view_type.value, ast.Attribute
):
attr: ast.Attribute = view_type.value
if attr.value.id == self.pk_import and attr.attr.startswith("ScratchView"):
view_dtype: cppast.PrimitiveType = visitors_util.get_type(
view_type.slice, self.pk_import
)
view_type: str = attr.attr
is_valid = True
if not is_valid:
return None
scratch_view = cppast.ClassType(view_type)
scratch_view.add_template_param(view_dtype)
cpp_view_type: str = visitors_util.cpp_view_type(scratch_view)
return cpp_view_type
def error(self, node, message):
visitors_util.error(self.src, self.debug, node, message)
def generic_error(self, node):
self.error(node, "Not supported for translation")