systemc-clang 2.0.0
Parsing SystemC constructs
Loading...
Searching...
No Matches
node.py
Go to the documentation of this file.
1from lark import Tree
2from copy import deepcopy
3from ..utils import dprint, is_tree_type, get_ids_in_tree, alternate_ids, set_ids_in_tree_dfs
4
5
6class Node:
7 def __init__(self):
8 pass
9
10
11class Statement(Tree):
12 def __init__(self):
13 pass
14
15
16class Expression(Tree):
17 def __init__(self):
18 pass
19
20
21class Always(Tree):
22 def __init__(self, tree):
23 super().__init__(tree.data, tree.children, tree.meta)
24
25
26class ArrayDeref(Tree):
27 def __init__(self, tree):
28 super().__init__(tree.data, tree.children, tree.meta)
29
30
32 def __init__(self, name, type_param_names, aliases, fields):
33 self.name = name
34 self.type_param_names = type_param_names
35 self.aliases = aliases
36 self.fields = fields
37
38 def _instantiate(self, field, params, types):
39 """
40 Instantiate a field in a type, given params map between the template parameter and argument,
41 and fields to instantiate
42 Note: nested type is not supported
43 """
44 if not isinstance(field, Tree):
45 return field
46 if isinstance(field, Tree):
47 if is_tree_type(field, 'htype'):
48 if field.children[0] in types:
49 raise NotImplementedError('Nested type support is WIP')
50 new_children = []
51 for x in field.children:
52 if x in params:
53 new_children.append(params[x])
54 else:
55 new_children.append(self._instantiate(x, params, types))
56 field.children = new_children
57 # if len(field.children) == 1 and not isinstance(field.children[0], Tree):
58 # return field.children[0]
59 return field
60 elif is_tree_type(field, 'hdeptype'):
61 # Check for local hTypeAlias
62 type_name = field.children[0]
63 if type_name in self.aliases:
64 original_type = self.aliases[type_name]
65 return self._instantiate(original_type, params, types)
66 else:
67 # query the global scope
68 # e.g. FP::expo_t
69 path_to_type = type_name.split('::')
70 assert len(path_to_type) == 2, "Only two levels of dependent type is supported (e.g. FP::expo_t is supported but FP::X::expo_t is not)"
71 for component in path_to_type[:-1]:
72 # maps parameter to argument (e.g. FP -> fp_t)
73 assert component in params, "{} not found when processing dependent type {}".format(component, type_name)
74 t = params[component] # get fp_t
75 assert t.data == 'htype', "Dependent type {} should be an htype (e.g. in FP::expo_t, FP must be htype)".format(component)
76 tname, tparams = t.children[0], t.children[1:]
77 aliases = types[t.children[0]].get_alias_type_with_instantiation(tparams, types)
78 last_path = path_to_type[-1]
79 res = aliases[last_path]
80 # returning some types
81 return self._instantiate(res, params, types)
82 else:
83 # dprint(field)
84 assert field.data == 'htypefield', "Generated field should be of type hTypeField, got {}".format(field.data)
85 field.children = [self._instantiate(x, params, types) if isinstance(x, Tree) else x for x in field.children]
86 return field
87
88 def get_fields_with_instantiation(self, params, types):
89 """
90 NOTE:
91 types is the global collected type
92 The type of hTypeField is searched in three scopes if including alias type
93 current type,
94 type parameter's scope,
95 global scope
96 """
97 param_maps = dict(zip(self.type_param_names, params))
98 instantiated_fields = deepcopy(self.fields)
99 # dprint(instantiated_fields)
100 instantiated_fields = list(map(lambda x: self._instantiate(x, param_maps, types), instantiated_fields))
101 # dprint(instantiated_fields)
102 res = [[f.children[0], f.children[1]] for f in instantiated_fields]
103 # dprint(res)
104 return res
105
106 def get_alias_type_with_instantiation(self, params, types):
107 param_maps = dict(zip(self.type_param_names, params))
108 aliases = dict(map(lambda x: (x[0], self._instantiate(x[1], param_maps, types)), deepcopy(self.aliases).items()))
109 return aliases
get_alias_type_with_instantiation(self, params, types)
Definition node.py:106
get_fields_with_instantiation(self, params, types)
Definition node.py:88
__init__(self, name, type_param_names, aliases, fields)
Definition node.py:32
_instantiate(self, field, params, types)
Definition node.py:38