blob: 686fb0fd6a4f4f943e74977b77e3445df4520f47 [file] [log] [blame]
Andreas Wundsam542a13c2013-11-15 13:28:55 -08001import c_gen.of_g_legacy as of_g
2
3def class_signature(members):
4 """
5 Generate a signature string for a class in canonical form
6
7 @param cls The class whose signature is to be generated
8 """
9 return ";".join([",".join([x["m_type"], x["name"], str(x["offset"])])
10 for x in members])
11
12def type_dec_to_count_base(m_type):
13 """
14 Resolve a type declaration like uint8_t[4] to a count (4) and base_type
15 (uint8_t)
16
17 @param m_type The string type declaration to process
18 """
19 count = 1
20 chk_ar = m_type.split('[')
21 if len(chk_ar) > 1:
22 count_str = chk_ar[1].split(']')[0]
23 if count_str in of_g.ofp_constants:
24 count = of_g.ofp_constants[count_str]
25 else:
26 count = int(count_str)
27 base_type = chk_ar[0]
28 else:
29 base_type = m_type
30 return count, base_type
31
32def list_to_entry_type(cls):
33 """
34 Return the entry type for a list
35 """
36 slen = len("of_list_")
37 return "of_" + cls[slen:]
38
39def type_to_short_name(m_type):
40 if m_type in of_g.of_base_types:
41 tname = of_g.of_base_types[m_type]["short_name"]
42 elif m_type in of_g.of_mixed_types:
43 tname = of_g.of_mixed_types[m_type]["short_name"]
44 else:
45 tname = "unknown"
46 return tname
47
48def type_to_name_type(cls, member_name):
49 """
50 Generate the root name of a member for accessor functions, etc
51 @param cls The class name
52 @param member_name The member name
53 """
54 members = of_g.unified[cls]["union"]
55 if not member_name in members:
56 debug("Error: %s is not in class %s for acc_name defn" %
57 (member_name, cls))
58 os.exit()
59
60 mem = members[member_name]
61 m_type = mem["m_type"]
62 id = mem["memid"]
63 tname = type_to_short_name(m_type)
64
65 return "o%d_m%d_%s" % (of_g.unified[cls]["object_id"], id, tname)
66
67
68def member_to_index(m_name, members):
69 """
70 Given a member name, return the index in the members dict
71 @param m_name The name of the data member to search for
72 @param members The dict of members
73 @return Index if found, -1 not found
74
75 Note we could generate an index when processing the original input
76 """
77 count = 0
78 for d in members:
79 if d["name"] == m_name:
80 return count
81 count += 1
82 return -1
83
84def member_base_type(cls, m_name):
85 """
86 Map a member to its of_ type
87 @param cls The class name
88 @param m_name The name of the member being gotten
89 @return The of_ type of the member
90 """
91 rv = of_g.unified[cls]["union"][m_name]["m_type"]
92 if rv[-2:] == "_t":
93 return rv
94 return rv + "_t"
95
96def member_type_is_octets(cls, m_name):
97 return member_base_type(cls, m_name) == "of_octets_t"
98
Andreas Wundsam542a13c2013-11-15 13:28:55 -080099def h_file_to_define(name):
100 """
101 Convert a .h file name to the define used for the header
102 """
103 h_name = name[:-2].upper()
104 h_name = "_" + h_name + "_H_"
105 return h_name
106
107def type_to_cof_type(m_type):
108 if m_type in of_g.of_base_types:
109 if "cof_type" in of_g.of_base_types[m_type]:
110 return of_g.of_base_types[m_type]["cof_type"]
111 return m_type
112
113
114def member_is_scalar(cls, m_name):
115 return of_g.unified[cls]["union"][m_name]["m_type"] in of_g.of_scalar_types
116
117def type_is_scalar(m_type):
118 return m_type in of_g.of_scalar_types
119
120def skip_member_name(name):
121 return name.find("pad") == 0 or name in of_g.skip_members
122
123def enum_name(cls):
124 """
125 Return the name used for an enum identifier for the given class
126 @param cls The class name
127 """
128 return cls.upper()
129
130def class_in_version(cls, ver):
131 """
132 Return boolean indicating if cls is defined for wire version ver
133 """
134
135 return (cls, ver) in of_g.base_length
136
137def instance_to_class(instance, parent):
138 """
139 Return the name of the class for an instance of inheritance type parent
140 """
141 return parent + "_" + instance
142
143def sub_class_to_var_name(cls):
144 """
145 Given a subclass name like of_action_output, generate the
146 name of a variable like 'output'
147 @param cls The class name
148 """
149 pass
150
151def class_is_var_len(cls, version):
152 # Match is special case. Only version 1.2 (wire version 3) is var
153 if cls == "of_match":
154 return version == 3
155
156 return not (cls, version) in of_g.is_fixed_length
157
158def base_type_to_length(base_type, version):
159 if base_type + "_t" in of_g.of_base_types:
160 inst_len = of_g.of_base_types[base_type + "_t"]["bytes"]
161 else:
162 inst_len = of_g.base_length[(base_type, version)]
163
164def version_to_name(version):
165 """
166 Convert an integer version to the C macro name
167 """
168 return "OF_" + of_g.version_names[version]
169
170##
171# Is class a flow modify of some sort?
172
173def cls_is_flow_mod(cls):
174 return cls in ["of_flow_mod", "of_flow_modify", "of_flow_add", "of_flow_delete",
175 "of_flow_modify_strict", "of_flow_delete_strict"]
176
177def all_member_types_get(cls, version):
178 """
179 Get the members and list of types for members of a given class
180 @param cls The class name to process
181 @param version The version for the class
182 """
183 member_types = []
184
185 if not version in of_g.unified[cls]:
186 return ([], [])
187
188 if "use_version" in of_g.unified[cls][version]:
189 v = of_g.unified[cls][version]["use_version"]
190 members = of_g.unified[cls][v]["members"]
191 else:
192 members = of_g.unified[cls][version]["members"]
193 # Accumulate variables that are supported
194 for member in members:
195 m_type = member["m_type"]
196 m_name = member["name"]
197 if skip_member_name(m_name):
198 continue
199 if not m_type in member_types:
200 member_types.append(m_type)
201
202 return (members, member_types)
203
204def list_name_extract(list_type):
205 """
206 Return the base name for a list object of the given type
207 @param list_type The type of the list as appears in the input,
208 for example list(of_port_desc_t).
209 @return A pair, (list-name, base-type) where list-name is the
210 base name for the list, for example of_list_port_desc, and base-type
211 is the type of list elements like of_port_desc_t
212 """
213 base_type = list_type[5:-1]
214 list_name = base_type
215 if list_name.find("of_") == 0:
216 list_name = list_name[3:]
217 if list_name[-2:] == "_t":
218 list_name = list_name[:-2]
219 list_name = "of_list_" + list_name
220 return (list_name, base_type)
221
222def version_to_name(version):
223 """
224 Convert an integer version to the C macro name
225 """
226 return "OF_" + of_g.version_names[version]
227
228def gen_c_copy_license(out):
229 """
230 Generate the top comments for copyright and license
231 """
232 import c_gen.util
233 c_gen.util.render_template(out, '_copyright.c')
234
235def accessor_returns_error(a_type, m_type):
236 is_var_len = (not type_is_scalar(m_type)) and \
237 [x for x in of_g.of_version_range if class_is_var_len(m_type[:-2], x)] != []
238 if a_type == "set" and is_var_len:
239 return True
240 elif m_type == "of_match_t":
241 return True
242 else:
243 return False