Skip to content
Merged
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
5 changes: 4 additions & 1 deletion internal/clang/emit.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,11 @@ func (g *Generator) emitImpl(w io.Writer) {
g.emitEmbeds(w, g.embeds.impl)
g.emitUnexportedTypes(w)
g.emitResultTypes(w, false)
g.emitPackageVars(w)
// Forward func decls before package vars: a package-var initializer may
// reference a function (e.g. a func-typed struct field), and a function
// prototype never references a package var, so this order satisfies both.
g.emitForwardFuncDecls(w)
g.emitPackageVars(w)

multiFile := len(g.pkg.Syntax) > 1
if !multiFile {
Expand Down
5 changes: 3 additions & 2 deletions internal/clang/symbols.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,9 @@ func (g *Generator) collectFuncDecl(d *ast.FuncDecl) {
}

// emitPackageVars writes all package-level variable and constant
// declarations at the top of the .c file, before forward declarations.
// This ensures they are defined before any function that references them.
// declarations at the top of the .c file, after forward func declarations
// (so initializers can reference functions) but before any function body
// (so functions can reference these vars).
func (g *Generator) emitPackageVars(w io.Writer) {
var symbols []symbol
for _, sym := range g.symbols {
Expand Down
6 changes: 3 additions & 3 deletions testdata/lang/arrays/dst/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ typedef struct arange {
uint8_t hi;
} arange;

// -- Variables and constants --
static arange aranges[16] = {[0] = (arange){0x10, 0x20}, [1] = (arange){0x30, 0x40}, [2] = (arange){0x50, 0x60}};

// -- Forward declarations --
static void change(so_int a[3]);
static box newBox(void);

// -- Variables and constants --
static arange aranges[16] = {[0] = (arange){0x10, 0x20}, [1] = (arange){0x30, 0x40}, [2] = (arange){0x50, 0x60}};

// -- Implementation --

static void change(so_int a[3]) {
Expand Down
6 changes: 3 additions & 3 deletions testdata/lang/attr/dst/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ typedef struct __attribute__((packed, aligned(16))) aligned {
//
typedef __attribute__((aligned(8))) so_int myInt;

// -- Forward declarations --
static __attribute__((noinline)) void helper(void);

// -- Variables and constants --

// Unexported volatile variable.
Expand All @@ -44,9 +47,6 @@ _Thread_local so_int main_PerThread = 0;
//
static _Thread_local volatile so_int flags = 0;

// -- Forward declarations --
static __attribute__((noinline)) void helper(void);

// -- Implementation --

// Exported function with so:attr.
Expand Down
6 changes: 3 additions & 3 deletions testdata/lang/comments/dst/main.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include "main.h"

// -- Forward declarations --
static main_Point offset(main_Point p, so_int dx, so_int dy);

// -- Variables and constants --

// MaxCoord is the maximum coordinate value.
const so_int main_MaxCoord = 1000;

// -- Forward declarations --
static main_Point offset(main_Point p, so_int dx, so_int dy);

// -- Implementation --

// NewPoint creates a new Point.
Expand Down
6 changes: 3 additions & 3 deletions testdata/lang/defer/dst/main.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#include "main.h"

// -- Variables and constants --
static so_int state = 0;

// -- Forward declarations --
static void xopen(so_int* x);
static void xclose(void* a);
static void funcScope(void);
static so_int funcWithReturn(void);
static void blockScope(void);

// -- Variables and constants --
static so_int state = 0;

// -- Implementation --

static void xopen(so_int* x) {
Expand Down
6 changes: 3 additions & 3 deletions testdata/lang/errors/dst/main.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "main.h"

// -- Variables and constants --
so_Error main_ErrOutOfTea = errors_New("no more tea available");

// -- Forward declarations --
static so_Error makeTea(so_int arg);
static so_R_int_err work(so_int n);

// -- Variables and constants --
so_Error main_ErrOutOfTea = errors_New("no more tea available");

// -- Implementation --

static so_Error makeTea(so_int arg) {
Expand Down
6 changes: 3 additions & 3 deletions testdata/lang/export/dst/main.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include "main.h"

// -- Forward declarations --
static bool someFunc(so_int x, so_int y);

// -- Variables and constants --
static const int64_t someConst = 7;
const int64_t main_SomeConst = 7;
static so_int someVar = 42;
so_int main_SomeVar = 42;

// -- Forward declarations --
static bool someFunc(so_int x, so_int y);

// -- Implementation --

static bool someFunc(so_int x, so_int y) {
Expand Down
6 changes: 3 additions & 3 deletions testdata/lang/init/dst/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ typedef struct value {
so_int x;
} value;

// -- Variables and constants --
static so_int state = 0;

// -- Forward declarations --
static void value_init(void* self, so_int x);

// -- Variables and constants --
static so_int state = 0;

// -- Implementation --

static void value_init(void* self, so_int x) {
Expand Down
6 changes: 3 additions & 3 deletions testdata/lang/multi-file/dst/main.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include "main.h"

// -- Forward declarations --
static so_int add(so_int a, so_int b);

// -- Variables and constants --
static so_int x = 11;
so_int main_Y = 22;
static const so_int z = 33;

// -- Forward declarations --
static so_int add(so_int a, so_int b);

// -- add.go --

static so_int add(so_int a, so_int b) {
Expand Down
6 changes: 3 additions & 3 deletions testdata/lang/multi-return-err/dst/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ typedef struct pointResult {
so_Error err;
} pointResult;

// -- Variables and constants --
static main_File file = {0};

// -- Forward declarations --
static main_FileResult makeFile(so_int size);
static so_R_ptr_err returnPtr(void);
Expand All @@ -30,6 +27,9 @@ static so_R_str_err returnString(void);
static so_R_slice_err returnSlice(void);
static so_R_int_err forwardCall(void);

// -- Variables and constants --
static main_File file = {0};

// -- Implementation --

static main_FileResult makeFile(so_int size) {
Expand Down
6 changes: 3 additions & 3 deletions testdata/lang/packages/dst/geom/geom.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "geom.h"

// -- Variables and constants --
const double geom_Pi = 3.14159;

// -- Forward declarations --
static double rectArea(double width, double height);

// -- Variables and constants --
const double geom_Pi = 3.14159;

// -- Implementation --

static double rectArea(double width, double height) {
Expand Down
6 changes: 3 additions & 3 deletions testdata/lang/panic/dst/main.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include "main.h"

// -- Variables and constants --
so_Error main_ErrNotFound = errors_New("not found");

// -- Forward declarations --
static void panicLiteral(void);
static void panicString(void);
static void panicError(void);

// -- Variables and constants --
so_Error main_ErrNotFound = errors_New("not found");

// -- Implementation --

static void panicLiteral(void) {
Expand Down
Loading