Skip to content

cpp: include exported resource class headers after interface types - #1762

Open
Snowiiii wants to merge 1 commit into
bytecodealliance:mainfrom
Snowiiii:fix/cpp-issue-1658
Open

Snowiiii wants to merge 1 commit into
bytecodealliance:mainfrom
Snowiiii:fix/cpp-issue-1658

Conversation

@Snowiiii

Copy link
Copy Markdown
Contributor

Previously, user class header files (exports-*.h) were included at the top of <world>_cpp.h. When an exported resource method or constructor accepted or returned records or variants defined in the interface, the user class header failed to compile because the WIT types had not been declared yet.

This commit resolves the issue by:

  1. Moving user class file includes (exports-*.h) to the bottom of the header, ensuring all records and types are fully defined beforehand.
  2. Forward-declaring exported resource classes in their respective namespaces so types and functions in the world header can reference them.
  3. Referring to owned handles of exported resources as wit::ResourceExportBase<Resource>::Owned instead of Resource::Owned in the header, which is valid for incomplete forward-declared types.
  4. Merging dependencies from user class files so helper headers like wit.h are included and emitted.

Closes #1658.

…ytecodealliance#1658)

Previously, user class header files (`exports-*.h`) were included at the
top of `<world>_cpp.h`. When an exported resource method or constructor
accepted or returned records or variants defined in the interface, the user
class header failed to compile because the WIT types had not been declared yet.

This commit resolves the issue by:
1. Moving user class file includes (`exports-*.h`) to the bottom of the
   header, ensuring all records and types are fully defined beforehand.
2. Forward-declaring exported resource classes in their respective namespaces
   so types and functions in the world header can reference them.
3. Referring to owned handles of exported resources as
   `wit::ResourceExportBase<Resource>::Owned` instead of `Resource::Owned`
   in the header, which is valid for incomplete forward-declared types.
4. Merging dependencies from user class files so helper headers like
   `wit.h` are included and emitted.

Closes bytecodealliance#1658.
@alexcrichton alexcrichton added the gen-cpp Related to the C++ code generator label Oct 30, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gen-cpp Related to the C++ code generator

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C++: generated _cpp.h includes exports-*.h before the records they use

2 participants