Return list of (tag, data_type) pairs. The first pair is the root and always has tag None. The last pair represents the argument. The tag is only present if the data_type in that pair has an ancestor and is a member of that ancestore's enumerated subtypes. Suppose we have the
(data_type)
| 198 | |
| 199 | |
| 200 | def get_ancestors(data_type): |
| 201 | """Return list of (tag, data_type) pairs. |
| 202 | |
| 203 | The first pair is the root and always has tag None. |
| 204 | The last pair represents the argument. |
| 205 | |
| 206 | The tag is only present if the data_type in that pair has an |
| 207 | ancestor and is a member of that ancestore's enumerated subtypes. |
| 208 | |
| 209 | Suppose we have the following tree: |
| 210 | |
| 211 | struct A |
| 212 | struct B extends A |
| 213 | struct C extends B |
| 214 | |
| 215 | Without enumerated subtypes: |
| 216 | - get_ancestors(C) returns [(None, A), (None, B), (None, C)] |
| 217 | - get_ancestors(B) returns [(None, A), (None, B)] |
| 218 | - get_ancestors(A) returns [(None, A)] |
| 219 | |
| 220 | Now add enumerated subtypes, so the tree becomes: |
| 221 | |
| 222 | struct A |
| 223 | union |
| 224 | b B |
| 225 | struct B extends A |
| 226 | union |
| 227 | c C |
| 228 | struct C extends B |
| 229 | |
| 230 | Now the return values are: |
| 231 | - get_ancestors(C) returns [(None, A), ('b', B), ('c', C)] |
| 232 | - get_ancestors(B) returns [(None, A), ('b', B)] |
| 233 | - get_ancestors(A) returns [(None, A)] |
| 234 | """ |
| 235 | assert isinstance(data_type, DataType), repr(data_type) |
| 236 | ancestors = [] |
| 237 | while data_type is not None: |
| 238 | parent_type = data_type.parent_type |
| 239 | tag = None |
| 240 | if parent_type is not None and parent_type.has_enumerated_subtypes(): |
| 241 | for field in parent_type.get_enumerated_subtypes(): |
| 242 | if field.data_type is data_type: |
| 243 | tag = field.name |
| 244 | break |
| 245 | else: |
| 246 | assert False, "Type {} not found in subtypes of ancestor {}".format(data_type.name, |
| 247 | parent_type.name) |
| 248 | ancestors.append((tag, data_type)) |
| 249 | data_type = parent_type |
| 250 | ancestors.reverse() |
| 251 | return ancestors |
| 252 | |
| 253 | |
| 254 | def get_enumerated_subtypes_recursively(data_type): |
no test coverage detected