Topologically sort the declaration dict by dependencies. Declarations can require other declarations to come prior in C (such as declaring structs). In order to guarantee that the C output will compile the declarations will thus need to be properly ordered. This simple DFS g
(self)
| 1462 | break |
| 1463 | |
| 1464 | def toposort_declarations(self) -> list[HeaderDeclaration]: |
| 1465 | """Topologically sort the declaration dict by dependencies. |
| 1466 | |
| 1467 | Declarations can require other declarations to come prior in C (such as declaring structs). |
| 1468 | In order to guarantee that the C output will compile the declarations will thus need to |
| 1469 | be properly ordered. This simple DFS guarantees that we have a proper ordering. |
| 1470 | |
| 1471 | This runs in O(V + E). |
| 1472 | """ |
| 1473 | result = [] |
| 1474 | marked_declarations: dict[str, MarkedDeclaration] = {} |
| 1475 | for k, v in self.context.declarations.items(): |
| 1476 | marked_declarations[k] = MarkedDeclaration(v, False) |
| 1477 | |
| 1478 | def _toposort_visit(name: str) -> None: |
| 1479 | decl = marked_declarations[name] |
| 1480 | if decl.mark: |
| 1481 | return |
| 1482 | |
| 1483 | for child in sorted(decl.declaration.dependencies): |
| 1484 | _toposort_visit(child) |
| 1485 | |
| 1486 | result.append(decl.declaration) |
| 1487 | decl.mark = True |
| 1488 | |
| 1489 | for name in marked_declarations: |
| 1490 | _toposort_visit(name) |
| 1491 | |
| 1492 | return result |
| 1493 | |
| 1494 | def declare_global( |
| 1495 | self, type_spaced: str, name: str, *, initializer: str | None = None |
no test coverage detected