-
Notifications
You must be signed in to change notification settings - Fork 15
feat: Add debug info metadata specification in hugr-py
#2971
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tatiana-s
wants to merge
4
commits into
main
Choose a base branch
from
ts/py-debug-info
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+234
−5
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| HUGR_GENERATOR: str | ||
| HUGR_USED_EXTENSIONS: str | ||
| HUGR_DEBUG_INFO: str |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| """Typed generator source debug information metadata for HUGR nodes.""" | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from dataclasses import dataclass | ||
| from typing import ClassVar, cast | ||
|
|
||
| from hugr.utils import JsonType | ||
|
|
||
|
|
||
| @dataclass | ||
| class DebugRecord(ABC): | ||
| """Abstract base class for debug records.""" | ||
|
|
||
| @abstractmethod | ||
| def to_json(self) -> JsonType: | ||
| """Encodes the record as a dictionary of native types that can be serialized by | ||
| `json.dump`. | ||
| """ | ||
|
|
||
| @classmethod | ||
| def from_json(cls, value: JsonType) -> "DebugRecord": | ||
| """Decode a debug record from json. This is not an abstract method because when | ||
| decoding from json by calling `DebugRecord.from_json` we do not have concrete | ||
| subtype information, so we decode from the explicit variant tag stored in `kind` | ||
| instead. | ||
| """ | ||
| if not isinstance(value, dict): | ||
| msg = f"Expected a dictionary for DebugRecord, but got {type(value)}" | ||
| raise TypeError(msg) | ||
|
|
||
| kind = value.get("kind") | ||
| if isinstance(kind, str): | ||
| if kind == DICompileUnit.KIND: | ||
| return DICompileUnit.from_json(value) | ||
| if kind == DISubprogram.KIND: | ||
| return DISubprogram.from_json(value) | ||
| if kind == DILocation.KIND: | ||
| return DILocation.from_json(value) | ||
| msg = f"Unknown DebugRecord kind: {kind}" | ||
| raise TypeError(msg) | ||
|
|
||
| msg = "Expected DebugRecord to contain string field 'kind'." | ||
| raise TypeError(msg) | ||
|
|
||
|
|
||
| @dataclass | ||
| class DICompileUnit(DebugRecord): | ||
| """Debug information for a compilation unit, corresponds to a HUGR module node.""" | ||
|
|
||
| KIND: ClassVar[str] = "compile_unit" | ||
|
|
||
| directory: str # Working directory of the compiler that generated the HUGR. | ||
| filename: int # File that contains the HUGR entrypoint. | ||
| file_table: list[str] # Global table of all files referenced in the module. | ||
|
|
||
| def to_json(self) -> dict[str, JsonType]: | ||
| return { | ||
| "kind": self.KIND, | ||
| "directory": self.directory, | ||
| "filename": self.filename, | ||
| "file_table": cast("list[JsonType]", self.file_table), | ||
| } | ||
|
|
||
| @classmethod | ||
| def from_json(cls, value: JsonType) -> "DICompileUnit": | ||
| if not isinstance(value, dict): | ||
| msg = f"Expected a dictionary for DICompileUnit, but got {type(value)}" | ||
| raise TypeError(msg) | ||
| for key in ("kind", "directory", "filename", "file_table"): | ||
| if key not in value: | ||
| msg = f"Expected DICompileUnit to have a '{key}' key but got {value}" | ||
| raise TypeError(msg) | ||
| files = value["file_table"] | ||
| if not isinstance(files, list): | ||
| msg = f"Expected 'file_table' to be a list but got {type(files)}" | ||
| raise TypeError(msg) | ||
| return DICompileUnit( | ||
| directory=str(value["directory"]), | ||
| filename=int(value["filename"]), | ||
| file_table=list[str](value["file_table"]), | ||
cqc-alec marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
|
|
||
| @dataclass | ||
| class DISubprogram(DebugRecord): | ||
| """Debug information for a subprogram, corresponds to a function definition or | ||
| declaration node. | ||
| """ | ||
|
|
||
| KIND: ClassVar[str] = "subprogram" | ||
|
|
||
| file: int # Index into the string table for filenames. | ||
| line_no: int # First line of the function definition. | ||
| scope_line: int | None = None # First line of the function body. | ||
|
|
||
| def to_json(self) -> dict[str, str]: | ||
cqc-alec marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| data = { | ||
| "kind": self.KIND, | ||
| "file": str(self.file), | ||
| "line_no": str(self.line_no), | ||
| } | ||
| # Declarations have no function body so could have no scope_line. | ||
| if self.scope_line is not None: | ||
| data["scope_line"] = str(self.scope_line) | ||
cqc-alec marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return data | ||
|
|
||
| @classmethod | ||
| def from_json(cls, value: JsonType) -> "DISubprogram": | ||
| if not isinstance(value, dict): | ||
| msg = f"Expected a dictionary for DISubprogram, but got {type(value)}" | ||
| raise TypeError(msg) | ||
| for key in ("kind", "file", "line_no"): | ||
| if key not in value: | ||
| msg = f"Expected DISubprogram to have a '{key}' key but got {value}" | ||
| raise TypeError(msg) | ||
| # Declarations have no function body so could have no scope_line. | ||
| scope_line = int(value["scope_line"]) if "scope_line" in value else None | ||
| return DISubprogram( | ||
| file=int(value["file"]), | ||
| line_no=int(value["line_no"]), | ||
| scope_line=scope_line, | ||
| ) | ||
|
|
||
|
|
||
| @dataclass | ||
| class DILocation(DebugRecord): | ||
| """Debug information for a location, corresponds to call or extension operation | ||
| node. | ||
| """ | ||
|
|
||
| KIND: ClassVar[str] = "location" | ||
|
|
||
| column: int | ||
| line_no: int | ||
|
|
||
| def to_json(self) -> dict[str, str]: | ||
cqc-alec marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return { | ||
| "kind": self.KIND, | ||
| "column": str(self.column), | ||
| "line_no": str(self.line_no), | ||
cqc-alec marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @classmethod | ||
| def from_json(cls, value: JsonType) -> "DILocation": | ||
| if not isinstance(value, dict): | ||
| msg = f"Expected a dictionary for DILocation, but got {type(value)}" | ||
| raise TypeError(msg) | ||
| for key in ("kind", "column", "line_no"): | ||
| if key not in value: | ||
| msg = f"Expected DILocation to have a '{key}' key but got {value}" | ||
| raise TypeError(msg) | ||
| return DILocation(column=int(value["column"]), line_no=int(value["line_no"])) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.