systemc-clang 2.0.0
Parsing SystemC constructs
Loading...
Searching...
No Matches
type_collector.py
Go to the documentation of this file.
1import re, warnings
2from lark import Lark, Transformer, Visitor
3
4from parselib.utils import p
5from .type_node import TypeNode
6
7
8class TypeCollector(Transformer):
9 """This pass collects all types within a hcode file."""
10 def __init__(self, skip=None, *args, **kwargs):
11 self.custom_types = dict()
12 self.module_types = dict()
14 pass
15
16 def is_module_type(self, name):
17 for key, _ in self.module_types.items():
18 if key.startswith(str(name)):
19 return key
20 return None
21
22 def get_port_bindings(self, name, parent=None):
23 # TODO: fix the portbinding hack
24 # list of port bindings
25 return self.module_types[name]
26
27 def is_custom_type(self, name):
28 return name in self.custom_types
29
30 def get_custom_type(self, name):
31 return self.custom_types[name]
32
33 def htype(self, args):
34 if len(args) == 1:
35 if re.match(r'(\+|\-)?0-9+', str(args[0])):
36 return int(args[0])
37 else:
38 return TypeNode(name=str(args[0]), params=[], fields=[])
39 else:
40 return TypeNode(name=str(args[0]), params=args[1:], fields=[])
41
42
43 def htypedef(self, args):
44 type_name = str(args[0])
45 # The check should be done together with type parameters
46 if type_name in self.custom_types:
47 raise Exception('repeated type definition')
48 self.custom_types[type_name] = TypeNode(name=type_name, params=args[1], fields=args[2])
49
50 def hmodule(self, args):
51 # TODO: fix this hack
52 mod_name = str(args[0])
53 self.current_mod = mod_name
54 if mod_name in self.module_types:
55 raise Exception('repeated module definition')
56 self.module_types[mod_name] = []
57 self.module_types[mod_name].extend(self.current_bindings)
58 self.current_bindings = []
59
60
61 def hvarref(self, args):
62 return args[0]
63
64
65 def htypeint(self, args):
66 return int(args[0])
67
68
69 def htypefields(self, args):
70 return args
71
72 def htypefield(self, args):
73 return (args[0], args[1])
74
75 def htypetemplateparams(self, args):
76 return args
77
78 def htypetemplateparam(self, args):
79 return str(args[0])
80
81
__init__(self, skip=None, *args, **kwargs)