Skip to content

translate_c: handle forward declared variables #24432

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/clang.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,12 @@ pub const VarDecl = opaque {

pub const isStaticLocal = ZigClangVarDecl_isStaticLocal;
extern fn ZigClangVarDecl_isStaticLocal(*const VarDecl) bool;

pub const isThisDeclarationADefinition = ZigClangVarDecl_isThisDeclarationADefinition;
extern fn ZigClangVarDecl_isThisDeclarationADefinition(*const VarDecl) bool;

pub const getDefinition = ZigClangVarDecl_getDefinition;
extern fn ZigClangVarDecl_getDefinition(*const VarDecl) ?*const VarDecl;
};

pub const VectorType = opaque {
Expand Down
6 changes: 6 additions & 0 deletions src/translate_c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,12 @@ fn visitVarDecl(c: *Context, var_decl: *const clang.VarDecl, mangled_name: ?[]co
if (c.global_scope.sym_table.contains(var_name))
return; // Avoid processing this decl twice

// Skip this declaration if a proper definition exists
if (!var_decl.isThisDeclarationADefinition()) {
if (var_decl.getDefinition()) |def|
return visitVarDecl(c, def, mangled_name);
}

const is_pub = mangled_name == null;
const is_threadlocal = var_decl.getTLSKind() != .None;
const scope = &c.global_scope.base;
Expand Down
10 changes: 10 additions & 0 deletions src/zig_clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3310,6 +3310,16 @@ bool ZigClangVarDecl_isStaticLocal(const struct ZigClangVarDecl *self) {
return casted->isStaticLocal();
}

bool ZigClangVarDecl_isThisDeclarationADefinition(const struct ZigClangVarDecl *self) {
auto casted = reinterpret_cast<const clang::VarDecl *>(self);
return casted->isThisDeclarationADefinition();
}

const ZigClangVarDecl *ZigClangVarDecl_getDefinition(const struct ZigClangVarDecl *self) {
auto casted = reinterpret_cast<const clang::VarDecl *>(self);
return reinterpret_cast<const ZigClangVarDecl *>(casted->getDefinition());
}

enum ZigClangBuiltinTypeKind ZigClangBuiltinType_getKind(const struct ZigClangBuiltinType *self) {
auto casted = reinterpret_cast<const clang::BuiltinType *>(self);
return (ZigClangBuiltinTypeKind)casted->getKind();
Expand Down
2 changes: 2 additions & 0 deletions src/zig_clang.h
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,8 @@ ZIG_EXTERN_C const struct ZigClangAPValue *ZigClangVarDecl_evaluateValue(const s
ZIG_EXTERN_C struct ZigClangQualType ZigClangVarDecl_getTypeSourceInfo_getType(const struct ZigClangVarDecl *);
ZIG_EXTERN_C enum ZigClangStorageClass ZigClangVarDecl_getStorageClass(const struct ZigClangVarDecl *self);
ZIG_EXTERN_C bool ZigClangVarDecl_isStaticLocal(const struct ZigClangVarDecl *self);
ZIG_EXTERN_C bool ZigClangVarDecl_isThisDeclarationADefinition(const struct ZigClangVarDecl *);
ZIG_EXTERN_C const ZigClangVarDecl *ZigClangVarDecl_getDefinition(const struct ZigClangVarDecl *);

ZIG_EXTERN_C bool ZigClangSourceLocation_eq(struct ZigClangSourceLocation a, struct ZigClangSourceLocation b);

Expand Down
Loading