-
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| 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"]), | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| ) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| @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]: | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||||||||||
| 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) | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| 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]: | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||||||||||
| return { | ||||||||||
| "kind": self.KIND, | ||||||||||
| "column": str(self.column), | ||||||||||
| "line_no": str(self.line_no), | ||||||||||
|
Comment on lines
+139
to
+140
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| } | ||||||||||
|
|
||||||||||
| @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"])) | ||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This docstring is hard for me to parse, can you reword?