Skip to content

Language: import accepts optional list of imported symbols - #551

Open
chqrlie wants to merge 1 commit into
c2lang:masterfrom
chqrlie:import-list
Open

Language: import accepts optional list of imported symbols#551
chqrlie wants to merge 1 commit into
c2lang:masterfrom
chqrlie:import-list

Conversation

@chqrlie

@chqrlie chqrlie commented Jul 25, 2026

Copy link
Copy Markdown
Contributor
  • syntax is import stdio { fopen, fclose, printf }
  • this helps readability and reduces symbol table lookups

@chqrlie
chqrlie force-pushed the import-list branch 2 times, most recently from e4083b0 to 70c78b4 Compare July 28, 2026 16:09
@bvdberg

bvdberg commented Jul 29, 2026

Copy link
Copy Markdown
Member

I'm playing around with this. Some observations:

module foo;
public type Public struct { i32 x; }
type Private struct { i32 x; }
  • import foo { Private } will give an error: module 'foo' has no symbol 'Private' (it should say: symbol 'foo.Private' is not public
  • Detection of ambiguous symbols still works with:
import foo { Public }
import bar local;

// Public a; // correct error

I'm not sure if I like the {} notation. I'm playing around with symbols:

        import stdio { printf }
        import stdio { * }      // old import local
        import stdio;
    OR
        import stdio [ printf, sprintf ];
        import stdio [ * ];
        import stdio;
    OR
        import stdio ( printf, sprintf );
        import stdio ( * );
        import stdio;

I like the [] and () also, except for the traling semi-colon....

For implementation, I expected that you used the InitListExpr to store the import-list. I think your version is a bit more compact in memory (as it is tail-allocated). We will need to update the refs plugin for this, so can a ref for 'printf' is generated.

@bvdberg

bvdberg commented Jul 29, 2026

Copy link
Copy Markdown
Member

Wow this is getting fancy... The u32 ImportDecl.symlist_count causes the ImportDecl to grow by 8 bytes. In Decl there are still 17 bits in the flags left. That would easily fit any list a user could add. Then the size would remain 40 bytes :)

@chqrlie
chqrlie force-pushed the import-list branch 2 times, most recently from d137064 to 7d8064a Compare July 29, 2026 18:53
@chqrlie

chqrlie commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

Wow this is getting fancy... The u32 ImportDecl.symlist_count causes the ImportDecl to grow by 8 bytes. In Decl there are still 17 bits in the flags left. That would easily fit any list a user could add. Then the size would remain 40 bytes :)

Good point, actually only 16 bits left in the ImportDeclBits, a nicely aligned 16-bit word.... Done!

* this helps readability and reduces symbol table lookups
* syntax is `import stdio { fopen, fclose, printf }`
* `import module local;` is now `import module { * }`
* explicitly imported symbols can be aliased:
  `import stdio { malloc as alloc }`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants