Skip to content
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

Type: add a name field to track typedef names #767

Merged
merged 1 commit into from
Sep 8, 2024
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
7 changes: 6 additions & 1 deletion src/aro/SymbolStack.zig
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,12 @@ pub fn defineTypedef(
.kind = .typedef,
.name = name,
.tok = tok,
.ty = ty,
.ty = .{
.name = name,
.specifier = ty.specifier,
.qual = ty.qual,
.data = ty.data,
},
.node = node,
.val = .{},
});
Expand Down
4 changes: 4 additions & 0 deletions src/aro/Tree.zig
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,10 @@ fn dumpNode(
}
try config.setColor(w, TYPE);
try w.writeByte('\'');
const name = ty.getName();
if (name != .empty) {
try w.print("{s}': '", .{mapper.lookup(name)});
}
try ty.dump(mapper, tree.comp.langopts, w);
try w.writeByte('\'');

Expand Down
11 changes: 11 additions & 0 deletions src/aro/Type.zig
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ data: union {
specifier: Specifier,
qual: Qualifiers = .{},
decayed: bool = false,
/// typedef name, if any
name: StringId = .empty,

pub const int = Type{ .specifier = .int };
pub const invalid = Type{ .specifier = .invalid };
Expand Down Expand Up @@ -1175,6 +1177,15 @@ pub fn enumIsPacked(ty: Type, comp: *const Compilation) bool {
return comp.langopts.short_enums or target_util.packAllEnums(comp.target) or ty.hasAttribute(.@"packed");
}

pub fn getName(ty: Type) StringId {
return switch (ty.specifier) {
.typeof_type => if (ty.name == .empty) ty.data.sub_type.getName() else ty.name,
.typeof_expr => if (ty.name == .empty) ty.data.expr.ty.getName() else ty.name,
.attributed => if (ty.name == .empty) ty.data.attributed.base.getName() else ty.name,
else => ty.name,
};
}

pub fn annotationAlignment(comp: *const Compilation, attrs: Attribute.Iterator) ?u29 {
var it = attrs;
var max_requested: ?u29 = null;
Expand Down
14 changes: 7 additions & 7 deletions test/cases/ast/_Float16.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
typedef: '[1]struct __va_list_tag'
typedef: '__builtin_va_list': '[1]struct __va_list_tag'
name: va_list

typedef: '[1]struct __va_list_tag'
typedef: '__builtin_va_list': '[1]struct __va_list_tag'
name: __gnuc_va_list

fn_def: 'fn (x: _Float16, y: _Float16) _Float16'
Expand All @@ -24,23 +24,23 @@ fn_def: 'fn (x: int, ...) void'
name: bar
body:
compound_stmt: 'void'
var: '[1]struct __va_list_tag'
var: 'va_list': '[1]struct __va_list_tag'
name: va

builtin_call_expr: 'void'
name: __builtin_va_start
args:
implicit_cast: (array_to_pointer) '*d[1]struct __va_list_tag'
decl_ref_expr: '[1]struct __va_list_tag' lvalue
implicit_cast: (array_to_pointer) 'va_list': '*d[1]struct __va_list_tag'
decl_ref_expr: 'va_list': '[1]struct __va_list_tag' lvalue
name: va
decl_ref_expr: 'int' lvalue
name: x

builtin_call_expr_one: 'void'
name: __builtin_va_end
arg:
implicit_cast: (array_to_pointer) '*d[1]struct __va_list_tag'
decl_ref_expr: '[1]struct __va_list_tag' lvalue
implicit_cast: (array_to_pointer) 'va_list': '*d[1]struct __va_list_tag'
decl_ref_expr: 'va_list': '[1]struct __va_list_tag' lvalue
name: va

implicit_return: 'void'
Expand Down
6 changes: 3 additions & 3 deletions test/cases/ast/__float80.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ fn_def: 'fn () void'
name: foo
body:
compound_stmt: 'void'
var: 'long double'
var: '__float80': 'long double'
name: x
init:
float_literal: 'long double' (value: 1)

assign_expr: 'long double'
assign_expr: '__float80': 'long double'
lhs:
decl_ref_expr: 'long double' lvalue
decl_ref_expr: '__float80': 'long double' lvalue
name: x
rhs:
float_literal: 'long double' (value: 1)
Expand Down
18 changes: 9 additions & 9 deletions test/cases/ast/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ fn_proto: 'attributed(fn () void)'
typedef: 'int'
name: A

typedef: 'int'
typedef: 'A': 'int'
name: B

typedef: 'int'
typedef: 'A': 'int'
name: C

typedef: 'int'
typedef: 'C': 'int'
name: B

typedef: '[2]int'
Expand All @@ -45,20 +45,20 @@ fn_def: 'fn (a: *d[2]const int, b: *d[2]const int) void'
name: qux
body:
compound_stmt: 'void'
add_assign_expr: '*d[2]const int'
add_assign_expr: 'I': '*d[2]const int'
lhs:
decl_ref_expr: '*d[2]const int' lvalue
decl_ref_expr: 'I': '*d[2]const int' lvalue
name: b
rhs:
implicit_cast: (int_to_pointer) '*d[2]const int'
implicit_cast: (int_to_pointer) 'I': '*d[2]const int'
int_literal: 'int' (value: 1)

add_assign_expr: '*d[2]const int'
add_assign_expr: 'I': '*d[2]const int'
lhs:
decl_ref_expr: '*d[2]const int' lvalue
decl_ref_expr: 'I': '*d[2]const int' lvalue
name: a
rhs:
implicit_cast: (int_to_pointer) '*d[2]const int'
implicit_cast: (int_to_pointer) 'I': '*d[2]const int'
int_literal: 'int' (value: 1)

implicit_return: 'void'
Expand Down
16 changes: 8 additions & 8 deletions test/cases/ast/vectors.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ fn_def: 'fn () void'
name: foo
body:
compound_stmt: 'void'
var: 'vector(2, float)'
var: 'f2v': 'vector(2, float)'
name: a

var: 'vector(2, float)'
var: 'f2v': 'vector(2, float)'
name: b

assign_expr: 'vector(2, float)'
assign_expr: 'f2v': 'vector(2, float)'
lhs:
decl_ref_expr: 'vector(2, float)' lvalue
decl_ref_expr: 'f2v': 'vector(2, float)' lvalue
name: a
rhs:
implicit_cast: (lval_to_rval) 'vector(2, float)'
decl_ref_expr: 'vector(2, float)' lvalue
implicit_cast: (lval_to_rval) 'f2v': 'vector(2, float)'
decl_ref_expr: 'f2v': 'vector(2, float)' lvalue
name: b

mul_assign_expr: 'vector(2, float)'
mul_assign_expr: 'f2v': 'vector(2, float)'
lhs:
decl_ref_expr: 'vector(2, float)' lvalue
decl_ref_expr: 'f2v': 'vector(2, float)' lvalue
name: a
rhs:
implicit_cast: (vector_splat) 'float'
Expand Down
Loading