Skip to content

Commit 70d38c1

Browse files
committed
fix: use not importing and generating
Signed-off-by: Gordon Smith <GordonJSmith@gmail.com>
1 parent 3934d24 commit 70d38c1

1 file changed

Lines changed: 52 additions & 26 deletions

File tree

tools/wit-codegen/code_generator.cpp

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ void CodeGenerator::generateHeader(const std::vector<InterfaceInfo> &interfaces,
253253
out << "// Interface: " << iface->name << "\n";
254254
out << "namespace " << sanitize_identifier(iface->name) << " {\n\n";
255255

256-
// Generate using declarations for same-namespace imports
256+
// Generate using declarations for use statements
257257
if (!iface->use_statements.empty())
258258
{
259259
bool hasUsings = false;
@@ -265,12 +265,6 @@ void CodeGenerator::generateHeader(const std::vector<InterfaceInfo> &interfaces,
265265
continue;
266266
}
267267

268-
// Skip cross-interface use statements (types defined in other files)
269-
if (useStmt.source_interface != iface->name)
270-
{
271-
continue;
272-
}
273-
274268
// Find the source interface to determine its namespace
275269
const InterfaceInfo *sourceIface = nullptr;
276270
for (const auto &other : interfaces)
@@ -282,15 +276,34 @@ void CodeGenerator::generateHeader(const std::vector<InterfaceInfo> &interfaces,
282276
}
283277
}
284278

285-
// Only generate using declarations for same-namespace imports
286-
// Cross-namespace references will use fully qualified names
287-
if (!sourceIface || sourceIface->kind == iface->kind)
279+
if (sourceIface)
288280
{
281+
// Generate using declarations for types from the source interface
289282
for (const auto &typeName : useStmt.imported_types)
290283
{
291-
out << "using " << sanitize_identifier(typeName) << " = "
292-
<< sanitize_identifier(useStmt.source_interface) << "::"
293-
<< sanitize_identifier(typeName) << ";\n";
284+
// Check if type was renamed
285+
std::string localName = typeName;
286+
auto renameIt = useStmt.type_renames.find(typeName);
287+
if (renameIt != useStmt.type_renames.end())
288+
{
289+
localName = renameIt->second;
290+
}
291+
292+
// Determine the fully qualified name based on namespace
293+
std::string qualifiedName;
294+
if (sourceIface->kind == iface->kind)
295+
{
296+
// Same namespace (both guest or both host) - use relative path
297+
qualifiedName = sanitize_identifier(useStmt.source_interface) + "::" + sanitize_identifier(typeName);
298+
}
299+
else
300+
{
301+
// Cross-namespace - use fully qualified path
302+
std::string prefix = (sourceIface->kind == InterfaceKind::Import) ? "::host::" : "::guest::";
303+
qualifiedName = prefix + sanitize_identifier(useStmt.source_interface) + "::" + sanitize_identifier(typeName);
304+
}
305+
306+
out << "using " << sanitize_identifier(localName) << " = " << qualifiedName << ";\n";
294307
hasUsings = true;
295308
}
296309
}
@@ -342,7 +355,7 @@ void CodeGenerator::generateHeader(const std::vector<InterfaceInfo> &interfaces,
342355
out << "// Interface: " << iface->name << "\n";
343356
out << "namespace " << sanitize_identifier(iface->name) << " {\n\n";
344357

345-
// Generate using declarations for same-namespace imports
358+
// Generate using declarations for use statements
346359
if (!iface->use_statements.empty())
347360
{
348361
bool hasUsings = false;
@@ -354,12 +367,6 @@ void CodeGenerator::generateHeader(const std::vector<InterfaceInfo> &interfaces,
354367
continue;
355368
}
356369

357-
// Skip cross-interface use statements (types defined in other files)
358-
if (useStmt.source_interface != iface->name)
359-
{
360-
continue;
361-
}
362-
363370
// Find the source interface to determine its namespace
364371
const InterfaceInfo *sourceIface = nullptr;
365372
for (const auto &other : interfaces)
@@ -371,15 +378,34 @@ void CodeGenerator::generateHeader(const std::vector<InterfaceInfo> &interfaces,
371378
}
372379
}
373380

374-
// Only generate using declarations for same-namespace imports
375-
// Cross-namespace references will use fully qualified names
376-
if (!sourceIface || sourceIface->kind == iface->kind)
381+
if (sourceIface)
377382
{
383+
// Generate using declarations for types from the source interface
378384
for (const auto &typeName : useStmt.imported_types)
379385
{
380-
out << "using " << sanitize_identifier(typeName) << " = "
381-
<< sanitize_identifier(useStmt.source_interface) << "::"
382-
<< sanitize_identifier(typeName) << ";\n";
386+
// Check if type was renamed
387+
std::string localName = typeName;
388+
auto renameIt = useStmt.type_renames.find(typeName);
389+
if (renameIt != useStmt.type_renames.end())
390+
{
391+
localName = renameIt->second;
392+
}
393+
394+
// Determine the fully qualified name based on namespace
395+
std::string qualifiedName;
396+
if (sourceIface->kind == iface->kind)
397+
{
398+
// Same namespace (both guest or both host) - use relative path
399+
qualifiedName = sanitize_identifier(useStmt.source_interface) + "::" + sanitize_identifier(typeName);
400+
}
401+
else
402+
{
403+
// Cross-namespace - use fully qualified path
404+
std::string prefix = (sourceIface->kind == InterfaceKind::Import) ? "::host::" : "::guest::";
405+
qualifiedName = prefix + sanitize_identifier(useStmt.source_interface) + "::" + sanitize_identifier(typeName);
406+
}
407+
408+
out << "using " << sanitize_identifier(localName) << " = " << qualifiedName << ";\n";
383409
hasUsings = true;
384410
}
385411
}

0 commit comments

Comments
 (0)