nac3core: Add field "constructor_lookup" to the struct TopLevelComposer #308

Closed
z78078 wants to merge 9 commits from issue-139 into master

The code below failed because the InnerResolver's 'pyid_to_type' table didn't store the type information for the classes that don't have their own constructor.

@nac3
class A
    @kernel
    def __init__(self):
        pass


@nac3
class B(A):
    pass


@nac3
class C(B):
    pass


@nac3
class Demo:
    core: KernelInvariant[Core]

    def __init__(self):
        self.core = Core()

    @kernel
    def run(self):
    	c = C()

>>>
nac3artiq.CompileError: compilation failed
----------
type error at identifier `C` (<class 'type'> is not registered with NAC3 (@nac3 decorator missing?))

In the register step, TopLevelComposer::reigster_top_level won't return the constructor's type of the class that doesn't have a constructor and therefore the 'pyid_to_type' will not be abled to store the type information for the class (the ty is None here).

This change add a field 'constructor_lookup' (HashMap) to TopLevelComposer which maintain a mapping between class name to constructor function definition.

With the 'constructor_lookup' table, the TopLevelComposer::reigster_top_level will be able to recognize and return the constructor's type for classes that inherit constructor function from its base class and 'pyid_to_type' will store the type information for the class as a result.

The code below failed because the InnerResolver's 'pyid_to_type' table didn't store the type information for the classes that don't have their own constructor. ``` @nac3 class A @kernel def __init__(self): pass @nac3 class B(A): pass @nac3 class C(B): pass @nac3 class Demo: core: KernelInvariant[Core] def __init__(self): self.core = Core() @kernel def run(self): c = C() >>> nac3artiq.CompileError: compilation failed ---------- type error at identifier `C` (<class 'type'> is not registered with NAC3 (@nac3 decorator missing?)) ``` In the register step, TopLevelComposer::reigster_top_level won't return the constructor's type of the class that doesn't have a constructor and therefore the 'pyid_to_type' will not be abled to store the type information for the class (the ty is None [here](https://git.m-labs.hk/M-Labs/nac3/src/branch/master/nac3artiq/src/lib.rs#L416-L418)). This change add a field 'constructor_lookup' (HashMap) to TopLevelComposer which maintain a mapping between class name to constructor function definition. With the 'constructor_lookup' table, the TopLevelComposer::reigster_top_level will be able to recognize and return the constructor's type for classes that inherit constructor function from its base class and 'pyid_to_type' will store the type information for the class as a result.
z78078 added 6 commits 2022-07-20 17:38:05 +08:00
sb10q reviewed 2022-07-20 17:38:40 +08:00
@ -37,6 +37,8 @@ pub struct TopLevelComposer {
// number of built-in function and classes in the definition list, later skip
pub builtin_num: usize,
pub core_config: ComposerConfig,
// the class name and it's constructor function

its

its

It is not clear from your description whether this fixes issue #139 or not.

Why do constructors need such special treatment compared to other inherited methods?

It is not clear from your description whether this fixes issue #139 or not. Why do constructors need such special treatment compared to other inherited methods?
z78078 added 2 commits 2022-07-21 09:57:01 +08:00
z78078 added 1 commit 2022-07-21 15:39:05 +08:00
z78078 closed this pull request 2022-07-25 11:47:27 +08:00

Pull request closed

Sign in to join this conversation.
No reviewers
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: M-Labs/nac3#308
There is no content yet.