Skip to content

Commit

Permalink
fix typedef before struct (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
phcreery authored Nov 23, 2024
1 parent c464ccc commit 134090f
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/node.v
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ mut:
kind NodeKind @[skip]
current_child_id int @[skip]
redeclarations_count int @[skip] // increased when some *other* Node had previous_decl == this Node.id
owned_tag_decl OwnedTagDecl @[json: 'ownedTagDecl'] // for TagDecl nodes, to store the TagDecl node that is owned by this node
}
// vfmt on

Expand Down Expand Up @@ -82,6 +83,12 @@ mut:
kind NodeKind @[skip]
}

struct OwnedTagDecl {
id string
kind_str string @[json: 'kind']
name string
}

const bad_node = Node{
kind: .bad
}
Expand Down
8 changes: 7 additions & 1 deletion src/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,20 @@ fn (mut c C2V) record_decl(node &Node) {
c.genln('// struct decl name="${c_name}"')
}
if c_name in c.types {
return
if node.previous_declaration == '' {
return
}
}
// Anonymous struct, most likely the next node is a vardecl with this anon struct type, so remember it
if c_name == '' {
c_name = 'AnonStruct_${node.location.line}'
c.last_declared_type_name = c_name
}
if c_name !in ['struct', 'union'] {
// in case typedef was already generated
if c_name in c.types {
c.types.delete(c_name)
}
v_name := c.add_struct_name(mut c.types, c_name)
if node.tags.contains('union') {
c.genln('union ${v_name} { ')
Expand Down
48 changes: 48 additions & 0 deletions tests/24.typedef_struct.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
typedef struct
{
float x, y;
} ImVec2;

typedef struct ImVec3 ImVec3;
struct ImVec3
{
float x, y, z;
};

struct ImVec4
{
float x, y, z, w;
};
typedef struct ImVec4 ImVec4;

struct ImGuiTextRange
{
const char *b;
const char *e;
};
typedef struct ImGuiTextRange ImGuiTextRange;

typedef struct ImVector_ImGuiTextRange
{
int Size;
int Capacity;
ImGuiTextRange *Data;
} ImVector_ImGuiTextRange;

struct ImGuiTextFilter
{
char InputBuf[256];
ImVector_ImGuiTextRange Filters;
int CountGrep;
};
typedef struct ImGuiTextRange ImGuiTextRange;

typedef unsigned short ImWchar16;
typedef ImWchar16 ImWchar;

typedef struct ImGuiContext ImGuiContext;
struct ImGuiContext;
struct ImGuiContext
{
int Initialized;
};
31 changes: 31 additions & 0 deletions tests/24.typedef_struct.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@[translated]
module tests

struct ImVec2 {
x f32
y f32
}
struct ImVec3 {
x f32
y f32
z f32
}
struct ImVec4 {
x f32
y f32
z f32
w f32
}
struct ImGuiTextRange {
b &i8
e &i8
}
struct ImVector_ImGuiTextRange {
size int
capacity int
data &ImGuiTextRange
}
type ImWchar16 = u16
struct ImGuiContext {
initialized int
}
31 changes: 31 additions & 0 deletions tests/24.typedef_struct.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@[translated]
module C:\Users\phcre\Documents\v\c2v\tests

struct ImVec2 {
x f32
y f32
}
struct ImVec3 {
x f32
y f32
z f32
}
struct ImVec4 {
x f32
y f32
z f32
w f32
}
struct ImGuiTextRange {
b &i8
e &i8
}
struct ImVector_ImGuiTextRange {
size int
capacity int
data &ImGuiTextRange
}
type ImWchar16 = u16
struct ImGuiContext {
initialized int
}

0 comments on commit 134090f

Please sign in to comment.