Merge pull request #6421 from tadeokondrak/opaque-syntax
Add opaque syntax that allows declarations
This commit is contained in:
@@ -808,6 +808,7 @@ fn tokenizeAndPrintRaw(docgen_tokenizer: *Tokenizer, out: anytype, source_token:
|
||||
.Keyword_noalias,
|
||||
.Keyword_noinline,
|
||||
.Keyword_nosuspend,
|
||||
.Keyword_opaque,
|
||||
.Keyword_or,
|
||||
.Keyword_orelse,
|
||||
.Keyword_packed,
|
||||
|
||||
@@ -1988,7 +1988,7 @@ test "null terminated array" {
|
||||
<li>Supports slice syntax: {#syntax#}ptr[start..end]{#endsyntax#}</li>
|
||||
<li>Supports pointer arithmetic: {#syntax#}ptr + x{#endsyntax#}, {#syntax#}ptr - x{#endsyntax#}</li>
|
||||
<li>{#syntax#}T{#endsyntax#} must have a known size, which means that it cannot be
|
||||
{#syntax#}c_void{#endsyntax#} or any other {#link|opaque type|Opaque Types#}.</li>
|
||||
{#syntax#}c_void{#endsyntax#} or any other {#link|opaque type|opaque#}.</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
@@ -5545,7 +5545,7 @@ test "turn HashMap into a set with void" {
|
||||
</p>
|
||||
<p>
|
||||
{#syntax#}void{#endsyntax#} is distinct from {#syntax#}c_void{#endsyntax#}, which is defined like this:
|
||||
{#syntax#}pub const c_void = @Type(.Opaque);{#endsyntax#}.
|
||||
{#syntax#}pub const c_void = opaque {};{#endsyntax#}.
|
||||
{#syntax#}void{#endsyntax#} has a known size of 0 bytes, and {#syntax#}c_void{#endsyntax#} has an unknown, but non-zero, size.
|
||||
</p>
|
||||
<p>
|
||||
@@ -8471,7 +8471,7 @@ test "integer truncation" {
|
||||
<li>{#link|Error Set Type#}</li>
|
||||
<li>{#link|Error Union Type#}</li>
|
||||
<li>{#link|Vectors#}</li>
|
||||
<li>{#link|Opaque Types#}</li>
|
||||
<li>{#link|opaque#}</li>
|
||||
<li>{#link|@Frame#}</li>
|
||||
<li>{#syntax#}anyframe{#endsyntax#}</li>
|
||||
<li>{#link|struct#}</li>
|
||||
@@ -8547,17 +8547,18 @@ fn foo(comptime T: type, ptr: *T) T {
|
||||
{#header_close#}
|
||||
{#header_close#}
|
||||
|
||||
{#header_open|Opaque Types#}
|
||||
{#header_open|opaque#}
|
||||
<p>
|
||||
{#syntax#}@Type(.Opaque){#endsyntax#} creates a new type with an unknown (but non-zero) size and alignment.
|
||||
{#syntax#}opaque {}{#endsyntax#} declares a new type with an unknown (but non-zero) size and alignment.
|
||||
It can have declarations like structs, unions, or enums.
|
||||
</p>
|
||||
<p>
|
||||
This is typically used for type safety when interacting with C code that does not expose struct details.
|
||||
Example:
|
||||
</p>
|
||||
{#code_begin|test_err|expected type '*Derp', found '*Wat'#}
|
||||
const Derp = @Type(.Opaque);
|
||||
const Wat = @Type(.Opaque);
|
||||
const Derp = opaque {};
|
||||
const Wat = opaque {};
|
||||
|
||||
extern fn bar(d: *Derp) void;
|
||||
fn foo(w: *Wat) callconv(.C) void {
|
||||
@@ -11193,7 +11194,7 @@ PtrTypeStart
|
||||
ContainerDeclAuto <- ContainerDeclType LBRACE ContainerMembers RBRACE
|
||||
|
||||
ContainerDeclType
|
||||
<- (KEYWORD_struct / KEYWORD_enum) (LPAREN Expr RPAREN)?
|
||||
<- (KEYWORD_struct / KEYWORD_enum / KEYWORD_opaque) (LPAREN Expr RPAREN)?
|
||||
/ KEYWORD_union (LPAREN (KEYWORD_enum (LPAREN Expr RPAREN)? / Expr) RPAREN)?
|
||||
|
||||
# Alignment
|
||||
@@ -11340,6 +11341,7 @@ KEYWORD_inline <- 'inline' end_of_word
|
||||
KEYWORD_noalias <- 'noalias' end_of_word
|
||||
KEYWORD_nosuspend <- 'nosuspend' end_of_word
|
||||
KEYWORD_null <- 'null' end_of_word
|
||||
KEYWORD_opaque <- 'opaque' end_of_word
|
||||
KEYWORD_or <- 'or' end_of_word
|
||||
KEYWORD_orelse <- 'orelse' end_of_word
|
||||
KEYWORD_packed <- 'packed' end_of_word
|
||||
@@ -11368,7 +11370,7 @@ keyword <- KEYWORD_align / KEYWORD_and / KEYWORD_anyframe / KEYWORD_anytype
|
||||
/ KEYWORD_defer / KEYWORD_else / KEYWORD_enum / KEYWORD_errdefer
|
||||
/ KEYWORD_error / KEYWORD_export / KEYWORD_extern / KEYWORD_false
|
||||
/ KEYWORD_fn / KEYWORD_for / KEYWORD_if / KEYWORD_inline
|
||||
/ KEYWORD_noalias / KEYWORD_null / KEYWORD_or
|
||||
/ KEYWORD_noalias / KEYWORD_null / KEYWORD_opaque / KEYWORD_or
|
||||
/ KEYWORD_orelse / KEYWORD_packed / KEYWORD_pub
|
||||
/ KEYWORD_resume / KEYWORD_return / KEYWORD_linksection
|
||||
/ KEYWORD_struct / KEYWORD_suspend
|
||||
|
||||
@@ -198,7 +198,7 @@ pub const TypeInfo = union(enum) {
|
||||
Union: Union,
|
||||
Fn: Fn,
|
||||
BoundFn: Fn,
|
||||
Opaque: void,
|
||||
Opaque: Opaque,
|
||||
Frame: Frame,
|
||||
AnyFrame: AnyFrame,
|
||||
Vector: Vector,
|
||||
@@ -359,6 +359,12 @@ pub const TypeInfo = union(enum) {
|
||||
args: []const FnArg,
|
||||
};
|
||||
|
||||
/// This data structure is used by the Zig language code generation and
|
||||
/// therefore must be kept in sync with the compiler implementation.
|
||||
pub const Opaque = struct {
|
||||
decls: []const Declaration,
|
||||
};
|
||||
|
||||
/// This data structure is used by the Zig language code generation and
|
||||
/// therefore must be kept in sync with the compiler implementation.
|
||||
pub const Frame = struct {
|
||||
|
||||
@@ -329,8 +329,8 @@ pub extern "c" fn pthread_cond_signal(cond: *pthread_cond_t) c_int;
|
||||
pub extern "c" fn pthread_cond_broadcast(cond: *pthread_cond_t) c_int;
|
||||
pub extern "c" fn pthread_cond_destroy(cond: *pthread_cond_t) c_int;
|
||||
|
||||
pub const pthread_t = *@Type(.Opaque);
|
||||
pub const FILE = @Type(.Opaque);
|
||||
pub const pthread_t = *opaque {};
|
||||
pub const FILE = opaque {};
|
||||
|
||||
pub extern "c" fn dlopen(path: [*:0]const u8, mode: c_int) ?*c_void;
|
||||
pub extern "c" fn dlclose(handle: *c_void) c_int;
|
||||
|
||||
@@ -12,28 +12,28 @@ const in_bpf_program = switch (std.builtin.arch) {
|
||||
|
||||
pub const helpers = if (in_bpf_program) @import("helpers.zig") else struct {};
|
||||
|
||||
pub const BpfSock = @Type(.Opaque);
|
||||
pub const BpfSockAddr = @Type(.Opaque);
|
||||
pub const FibLookup = @Type(.Opaque);
|
||||
pub const MapDef = @Type(.Opaque);
|
||||
pub const PerfEventData = @Type(.Opaque);
|
||||
pub const PerfEventValue = @Type(.Opaque);
|
||||
pub const PidNsInfo = @Type(.Opaque);
|
||||
pub const SeqFile = @Type(.Opaque);
|
||||
pub const SkBuff = @Type(.Opaque);
|
||||
pub const SkMsgMd = @Type(.Opaque);
|
||||
pub const SkReusePortMd = @Type(.Opaque);
|
||||
pub const Sock = @Type(.Opaque);
|
||||
pub const SockAddr = @Type(.Opaque);
|
||||
pub const SockOps = @Type(.Opaque);
|
||||
pub const SockTuple = @Type(.Opaque);
|
||||
pub const SpinLock = @Type(.Opaque);
|
||||
pub const SysCtl = @Type(.Opaque);
|
||||
pub const Tcp6Sock = @Type(.Opaque);
|
||||
pub const TcpRequestSock = @Type(.Opaque);
|
||||
pub const TcpSock = @Type(.Opaque);
|
||||
pub const TcpTimewaitSock = @Type(.Opaque);
|
||||
pub const TunnelKey = @Type(.Opaque);
|
||||
pub const Udp6Sock = @Type(.Opaque);
|
||||
pub const XdpMd = @Type(.Opaque);
|
||||
pub const XfrmState = @Type(.Opaque);
|
||||
pub const BpfSock = opaque {};
|
||||
pub const BpfSockAddr = opaque {};
|
||||
pub const FibLookup = opaque {};
|
||||
pub const MapDef = opaque {};
|
||||
pub const PerfEventData = opaque {};
|
||||
pub const PerfEventValue = opaque {};
|
||||
pub const PidNsInfo = opaque {};
|
||||
pub const SeqFile = opaque {};
|
||||
pub const SkBuff = opaque {};
|
||||
pub const SkMsgMd = opaque {};
|
||||
pub const SkReusePortMd = opaque {};
|
||||
pub const Sock = opaque {};
|
||||
pub const SockAddr = opaque {};
|
||||
pub const SockOps = opaque {};
|
||||
pub const SockTuple = opaque {};
|
||||
pub const SpinLock = opaque {};
|
||||
pub const SysCtl = opaque {};
|
||||
pub const Tcp6Sock = opaque {};
|
||||
pub const TcpRequestSock = opaque {};
|
||||
pub const TcpSock = opaque {};
|
||||
pub const TcpTimewaitSock = opaque {};
|
||||
pub const TunnelKey = opaque {};
|
||||
pub const Udp6Sock = opaque {};
|
||||
pub const XdpMd = opaque {};
|
||||
pub const XfrmState = opaque {};
|
||||
|
||||
@@ -17,7 +17,7 @@ pub var handle: Handle = undefined;
|
||||
pub var system_table: *tables.SystemTable = undefined;
|
||||
|
||||
/// A handle to an event structure.
|
||||
pub const Event = *@Type(.Opaque);
|
||||
pub const Event = *opaque {};
|
||||
|
||||
/// GUIDs must be align(8)
|
||||
pub const Guid = extern struct {
|
||||
@@ -51,7 +51,7 @@ pub const Guid = extern struct {
|
||||
};
|
||||
|
||||
/// An EFI Handle represents a collection of related interfaces.
|
||||
pub const Handle = *@Type(.Opaque);
|
||||
pub const Handle = *opaque {};
|
||||
|
||||
/// This structure represents time information.
|
||||
pub const Time = extern struct {
|
||||
@@ -108,4 +108,4 @@ pub const TimeCapabilities = extern struct {
|
||||
};
|
||||
|
||||
/// File Handle as specified in the EFI Shell Spec
|
||||
pub const FileHandle = *@Type(.Opaque);
|
||||
pub const FileHandle = *opaque {};
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
const uefi = @import("std").os.uefi;
|
||||
const Guid = uefi.Guid;
|
||||
|
||||
pub const HIIHandle = *@Type(.Opaque);
|
||||
pub const HIIHandle = *opaque {};
|
||||
|
||||
/// The header found at the start of each package.
|
||||
pub const HIIPackageHeader = packed struct {
|
||||
|
||||
@@ -32,16 +32,16 @@ pub const UCHAR = u8;
|
||||
pub const FLOAT = f32;
|
||||
pub const HANDLE = *c_void;
|
||||
pub const HCRYPTPROV = ULONG_PTR;
|
||||
pub const HBRUSH = *@Type(.Opaque);
|
||||
pub const HCURSOR = *@Type(.Opaque);
|
||||
pub const HICON = *@Type(.Opaque);
|
||||
pub const HINSTANCE = *@Type(.Opaque);
|
||||
pub const HMENU = *@Type(.Opaque);
|
||||
pub const HMODULE = *@Type(.Opaque);
|
||||
pub const HWND = *@Type(.Opaque);
|
||||
pub const HDC = *@Type(.Opaque);
|
||||
pub const HGLRC = *@Type(.Opaque);
|
||||
pub const FARPROC = *@Type(.Opaque);
|
||||
pub const HBRUSH = *opaque {};
|
||||
pub const HCURSOR = *opaque {};
|
||||
pub const HICON = *opaque {};
|
||||
pub const HINSTANCE = *opaque {};
|
||||
pub const HMENU = *opaque {};
|
||||
pub const HMODULE = *opaque {};
|
||||
pub const HWND = *opaque {};
|
||||
pub const HDC = *opaque {};
|
||||
pub const HGLRC = *opaque {};
|
||||
pub const FARPROC = *opaque {};
|
||||
pub const INT = c_int;
|
||||
pub const LPBYTE = *BYTE;
|
||||
pub const LPCH = *CHAR;
|
||||
@@ -81,7 +81,7 @@ pub const WPARAM = usize;
|
||||
pub const LPARAM = ?*c_void;
|
||||
pub const LRESULT = ?*c_void;
|
||||
|
||||
pub const va_list = *@Type(.Opaque);
|
||||
pub const va_list = *opaque {};
|
||||
|
||||
pub const TRUE = 1;
|
||||
pub const FALSE = 0;
|
||||
@@ -1175,10 +1175,10 @@ pub const UNICODE_STRING = extern struct {
|
||||
Buffer: [*]WCHAR,
|
||||
};
|
||||
|
||||
const ACTIVATION_CONTEXT_DATA = @Type(.Opaque);
|
||||
const ASSEMBLY_STORAGE_MAP = @Type(.Opaque);
|
||||
const FLS_CALLBACK_INFO = @Type(.Opaque);
|
||||
const RTL_BITMAP = @Type(.Opaque);
|
||||
const ACTIVATION_CONTEXT_DATA = opaque {};
|
||||
const ASSEMBLY_STORAGE_MAP = opaque {};
|
||||
const FLS_CALLBACK_INFO = opaque {};
|
||||
const RTL_BITMAP = opaque {};
|
||||
pub const PRTL_BITMAP = *RTL_BITMAP;
|
||||
const KAFFINITY = usize;
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
// and substantial portions of the software.
|
||||
usingnamespace @import("bits.zig");
|
||||
|
||||
pub const SOCKET = *@Type(.Opaque);
|
||||
pub const SOCKET = *opaque {};
|
||||
pub const INVALID_SOCKET = @intToPtr(SOCKET, ~@as(usize, 0));
|
||||
pub const SOCKET_ERROR = -1;
|
||||
|
||||
|
||||
@@ -288,7 +288,7 @@ pub const Error = union(enum) {
|
||||
pub const ExpectedVarDecl = SingleTokenError("Expected variable declaration, found '{}'");
|
||||
pub const ExpectedFn = SingleTokenError("Expected function, found '{}'");
|
||||
pub const ExpectedReturnType = SingleTokenError("Expected 'var' or return type expression, found '{}'");
|
||||
pub const ExpectedAggregateKw = SingleTokenError("Expected '" ++ Token.Id.Keyword_struct.symbol() ++ "', '" ++ Token.Id.Keyword_union.symbol() ++ "', or '" ++ Token.Id.Keyword_enum.symbol() ++ "', found '{}'");
|
||||
pub const ExpectedAggregateKw = SingleTokenError("Expected '" ++ Token.Id.Keyword_struct.symbol() ++ "', '" ++ Token.Id.Keyword_union.symbol() ++ "', '" ++ Token.Id.Keyword_enum.symbol() ++ "', or '" ++ Token.Id.Keyword_opaque.symbol() ++ "', found '{}'");
|
||||
pub const ExpectedEqOrSemi = SingleTokenError("Expected '=' or ';', found '{}'");
|
||||
pub const ExpectedSemiOrLBrace = SingleTokenError("Expected ';' or '{{', found '{}'");
|
||||
pub const ExpectedSemiOrElse = SingleTokenError("Expected ';' or 'else', found '{}'");
|
||||
|
||||
@@ -2896,11 +2896,12 @@ const Parser = struct {
|
||||
/// <- KEYWORD_struct
|
||||
/// / KEYWORD_enum (LPAREN Expr RPAREN)?
|
||||
/// / KEYWORD_union (LPAREN (KEYWORD_enum (LPAREN Expr RPAREN)? / Expr) RPAREN)?
|
||||
/// / KEYWORD_opaque
|
||||
fn parseContainerDeclType(p: *Parser) !?ContainerDeclType {
|
||||
const kind_token = p.nextToken();
|
||||
|
||||
const init_arg_expr = switch (p.token_ids[kind_token]) {
|
||||
.Keyword_struct => Node.ContainerDecl.InitArg{ .None = {} },
|
||||
.Keyword_struct, .Keyword_opaque => Node.ContainerDecl.InitArg{ .None = {} },
|
||||
.Keyword_enum => blk: {
|
||||
if (p.eatToken(.LParen) != null) {
|
||||
const expr = try p.expectNode(parseExpr, .{
|
||||
|
||||
@@ -1492,7 +1492,19 @@ fn renderExpression(
|
||||
|
||||
// TODO remove after 0.7.0 release
|
||||
if (mem.eql(u8, tree.tokenSlice(builtin_call.builtin_token), "@OpaqueType"))
|
||||
return ais.writer().writeAll("@Type(.Opaque)");
|
||||
return ais.writer().writeAll("opaque {}");
|
||||
|
||||
// TODO remove after 0.7.0 release
|
||||
{
|
||||
const params = builtin_call.paramsConst();
|
||||
if (mem.eql(u8, tree.tokenSlice(builtin_call.builtin_token), "@Type") and
|
||||
params.len == 1)
|
||||
{
|
||||
if (params[0].castTag(.EnumLiteral)) |enum_literal|
|
||||
if (mem.eql(u8, tree.tokenSlice(enum_literal.name), "Opaque"))
|
||||
return ais.writer().writeAll("opaque {}");
|
||||
}
|
||||
}
|
||||
|
||||
try renderToken(tree, ais, builtin_call.builtin_token, Space.None); // @name
|
||||
|
||||
|
||||
@@ -47,6 +47,7 @@ pub const Token = struct {
|
||||
.{ "noinline", .Keyword_noinline },
|
||||
.{ "nosuspend", .Keyword_nosuspend },
|
||||
.{ "null", .Keyword_null },
|
||||
.{ "opaque", .Keyword_opaque },
|
||||
.{ "or", .Keyword_or },
|
||||
.{ "orelse", .Keyword_orelse },
|
||||
.{ "packed", .Keyword_packed },
|
||||
@@ -173,6 +174,7 @@ pub const Token = struct {
|
||||
Keyword_noinline,
|
||||
Keyword_nosuspend,
|
||||
Keyword_null,
|
||||
Keyword_opaque,
|
||||
Keyword_or,
|
||||
Keyword_orelse,
|
||||
Keyword_packed,
|
||||
@@ -296,6 +298,7 @@ pub const Token = struct {
|
||||
.Keyword_noinline => "noinline",
|
||||
.Keyword_nosuspend => "nosuspend",
|
||||
.Keyword_null => "null",
|
||||
.Keyword_opaque => "opaque",
|
||||
.Keyword_or => "or",
|
||||
.Keyword_orelse => "orelse",
|
||||
.Keyword_packed => "packed",
|
||||
|
||||
172
src/clang.zig
172
src/clang.zig
@@ -1,89 +1,89 @@
|
||||
const builtin = @import("builtin");
|
||||
|
||||
pub const struct_ZigClangConditionalOperator = @Type(.Opaque);
|
||||
pub const struct_ZigClangBinaryConditionalOperator = @Type(.Opaque);
|
||||
pub const struct_ZigClangAbstractConditionalOperator = @Type(.Opaque);
|
||||
pub const struct_ZigClangAPInt = @Type(.Opaque);
|
||||
pub const struct_ZigClangAPSInt = @Type(.Opaque);
|
||||
pub const struct_ZigClangAPFloat = @Type(.Opaque);
|
||||
pub const struct_ZigClangASTContext = @Type(.Opaque);
|
||||
pub const struct_ZigClangASTUnit = @Type(.Opaque);
|
||||
pub const struct_ZigClangArraySubscriptExpr = @Type(.Opaque);
|
||||
pub const struct_ZigClangArrayType = @Type(.Opaque);
|
||||
pub const struct_ZigClangAttributedType = @Type(.Opaque);
|
||||
pub const struct_ZigClangBinaryOperator = @Type(.Opaque);
|
||||
pub const struct_ZigClangBreakStmt = @Type(.Opaque);
|
||||
pub const struct_ZigClangBuiltinType = @Type(.Opaque);
|
||||
pub const struct_ZigClangCStyleCastExpr = @Type(.Opaque);
|
||||
pub const struct_ZigClangCallExpr = @Type(.Opaque);
|
||||
pub const struct_ZigClangCaseStmt = @Type(.Opaque);
|
||||
pub const struct_ZigClangCompoundAssignOperator = @Type(.Opaque);
|
||||
pub const struct_ZigClangCompoundStmt = @Type(.Opaque);
|
||||
pub const struct_ZigClangConstantArrayType = @Type(.Opaque);
|
||||
pub const struct_ZigClangContinueStmt = @Type(.Opaque);
|
||||
pub const struct_ZigClangDecayedType = @Type(.Opaque);
|
||||
pub const ZigClangDecl = @Type(.Opaque);
|
||||
pub const struct_ZigClangDeclRefExpr = @Type(.Opaque);
|
||||
pub const struct_ZigClangDeclStmt = @Type(.Opaque);
|
||||
pub const struct_ZigClangDefaultStmt = @Type(.Opaque);
|
||||
pub const struct_ZigClangDiagnosticOptions = @Type(.Opaque);
|
||||
pub const struct_ZigClangDiagnosticsEngine = @Type(.Opaque);
|
||||
pub const struct_ZigClangDoStmt = @Type(.Opaque);
|
||||
pub const struct_ZigClangElaboratedType = @Type(.Opaque);
|
||||
pub const struct_ZigClangEnumConstantDecl = @Type(.Opaque);
|
||||
pub const struct_ZigClangEnumDecl = @Type(.Opaque);
|
||||
pub const struct_ZigClangEnumType = @Type(.Opaque);
|
||||
pub const struct_ZigClangExpr = @Type(.Opaque);
|
||||
pub const struct_ZigClangFieldDecl = @Type(.Opaque);
|
||||
pub const struct_ZigClangFileID = @Type(.Opaque);
|
||||
pub const struct_ZigClangForStmt = @Type(.Opaque);
|
||||
pub const struct_ZigClangFullSourceLoc = @Type(.Opaque);
|
||||
pub const struct_ZigClangFunctionDecl = @Type(.Opaque);
|
||||
pub const struct_ZigClangFunctionProtoType = @Type(.Opaque);
|
||||
pub const struct_ZigClangIfStmt = @Type(.Opaque);
|
||||
pub const struct_ZigClangImplicitCastExpr = @Type(.Opaque);
|
||||
pub const struct_ZigClangIncompleteArrayType = @Type(.Opaque);
|
||||
pub const struct_ZigClangIntegerLiteral = @Type(.Opaque);
|
||||
pub const struct_ZigClangMacroDefinitionRecord = @Type(.Opaque);
|
||||
pub const struct_ZigClangMacroExpansion = @Type(.Opaque);
|
||||
pub const struct_ZigClangMacroQualifiedType = @Type(.Opaque);
|
||||
pub const struct_ZigClangMemberExpr = @Type(.Opaque);
|
||||
pub const struct_ZigClangNamedDecl = @Type(.Opaque);
|
||||
pub const struct_ZigClangNone = @Type(.Opaque);
|
||||
pub const struct_ZigClangOpaqueValueExpr = @Type(.Opaque);
|
||||
pub const struct_ZigClangPCHContainerOperations = @Type(.Opaque);
|
||||
pub const struct_ZigClangParenExpr = @Type(.Opaque);
|
||||
pub const struct_ZigClangParenType = @Type(.Opaque);
|
||||
pub const struct_ZigClangParmVarDecl = @Type(.Opaque);
|
||||
pub const struct_ZigClangPointerType = @Type(.Opaque);
|
||||
pub const struct_ZigClangPreprocessedEntity = @Type(.Opaque);
|
||||
pub const struct_ZigClangRecordDecl = @Type(.Opaque);
|
||||
pub const struct_ZigClangRecordType = @Type(.Opaque);
|
||||
pub const struct_ZigClangReturnStmt = @Type(.Opaque);
|
||||
pub const struct_ZigClangSkipFunctionBodiesScope = @Type(.Opaque);
|
||||
pub const struct_ZigClangSourceManager = @Type(.Opaque);
|
||||
pub const struct_ZigClangSourceRange = @Type(.Opaque);
|
||||
pub const ZigClangStmt = @Type(.Opaque);
|
||||
pub const struct_ZigClangStringLiteral = @Type(.Opaque);
|
||||
pub const struct_ZigClangStringRef = @Type(.Opaque);
|
||||
pub const struct_ZigClangSwitchStmt = @Type(.Opaque);
|
||||
pub const struct_ZigClangTagDecl = @Type(.Opaque);
|
||||
pub const struct_ZigClangType = @Type(.Opaque);
|
||||
pub const struct_ZigClangTypedefNameDecl = @Type(.Opaque);
|
||||
pub const struct_ZigClangTypedefType = @Type(.Opaque);
|
||||
pub const struct_ZigClangUnaryExprOrTypeTraitExpr = @Type(.Opaque);
|
||||
pub const struct_ZigClangUnaryOperator = @Type(.Opaque);
|
||||
pub const struct_ZigClangValueDecl = @Type(.Opaque);
|
||||
pub const struct_ZigClangVarDecl = @Type(.Opaque);
|
||||
pub const struct_ZigClangWhileStmt = @Type(.Opaque);
|
||||
pub const struct_ZigClangFunctionType = @Type(.Opaque);
|
||||
pub const struct_ZigClangPredefinedExpr = @Type(.Opaque);
|
||||
pub const struct_ZigClangInitListExpr = @Type(.Opaque);
|
||||
pub const ZigClangPreprocessingRecord = @Type(.Opaque);
|
||||
pub const ZigClangFloatingLiteral = @Type(.Opaque);
|
||||
pub const ZigClangConstantExpr = @Type(.Opaque);
|
||||
pub const ZigClangCharacterLiteral = @Type(.Opaque);
|
||||
pub const ZigClangStmtExpr = @Type(.Opaque);
|
||||
pub const struct_ZigClangConditionalOperator = opaque {};
|
||||
pub const struct_ZigClangBinaryConditionalOperator = opaque {};
|
||||
pub const struct_ZigClangAbstractConditionalOperator = opaque {};
|
||||
pub const struct_ZigClangAPInt = opaque {};
|
||||
pub const struct_ZigClangAPSInt = opaque {};
|
||||
pub const struct_ZigClangAPFloat = opaque {};
|
||||
pub const struct_ZigClangASTContext = opaque {};
|
||||
pub const struct_ZigClangASTUnit = opaque {};
|
||||
pub const struct_ZigClangArraySubscriptExpr = opaque {};
|
||||
pub const struct_ZigClangArrayType = opaque {};
|
||||
pub const struct_ZigClangAttributedType = opaque {};
|
||||
pub const struct_ZigClangBinaryOperator = opaque {};
|
||||
pub const struct_ZigClangBreakStmt = opaque {};
|
||||
pub const struct_ZigClangBuiltinType = opaque {};
|
||||
pub const struct_ZigClangCStyleCastExpr = opaque {};
|
||||
pub const struct_ZigClangCallExpr = opaque {};
|
||||
pub const struct_ZigClangCaseStmt = opaque {};
|
||||
pub const struct_ZigClangCompoundAssignOperator = opaque {};
|
||||
pub const struct_ZigClangCompoundStmt = opaque {};
|
||||
pub const struct_ZigClangConstantArrayType = opaque {};
|
||||
pub const struct_ZigClangContinueStmt = opaque {};
|
||||
pub const struct_ZigClangDecayedType = opaque {};
|
||||
pub const ZigClangDecl = opaque {};
|
||||
pub const struct_ZigClangDeclRefExpr = opaque {};
|
||||
pub const struct_ZigClangDeclStmt = opaque {};
|
||||
pub const struct_ZigClangDefaultStmt = opaque {};
|
||||
pub const struct_ZigClangDiagnosticOptions = opaque {};
|
||||
pub const struct_ZigClangDiagnosticsEngine = opaque {};
|
||||
pub const struct_ZigClangDoStmt = opaque {};
|
||||
pub const struct_ZigClangElaboratedType = opaque {};
|
||||
pub const struct_ZigClangEnumConstantDecl = opaque {};
|
||||
pub const struct_ZigClangEnumDecl = opaque {};
|
||||
pub const struct_ZigClangEnumType = opaque {};
|
||||
pub const struct_ZigClangExpr = opaque {};
|
||||
pub const struct_ZigClangFieldDecl = opaque {};
|
||||
pub const struct_ZigClangFileID = opaque {};
|
||||
pub const struct_ZigClangForStmt = opaque {};
|
||||
pub const struct_ZigClangFullSourceLoc = opaque {};
|
||||
pub const struct_ZigClangFunctionDecl = opaque {};
|
||||
pub const struct_ZigClangFunctionProtoType = opaque {};
|
||||
pub const struct_ZigClangIfStmt = opaque {};
|
||||
pub const struct_ZigClangImplicitCastExpr = opaque {};
|
||||
pub const struct_ZigClangIncompleteArrayType = opaque {};
|
||||
pub const struct_ZigClangIntegerLiteral = opaque {};
|
||||
pub const struct_ZigClangMacroDefinitionRecord = opaque {};
|
||||
pub const struct_ZigClangMacroExpansion = opaque {};
|
||||
pub const struct_ZigClangMacroQualifiedType = opaque {};
|
||||
pub const struct_ZigClangMemberExpr = opaque {};
|
||||
pub const struct_ZigClangNamedDecl = opaque {};
|
||||
pub const struct_ZigClangNone = opaque {};
|
||||
pub const struct_ZigClangOpaqueValueExpr = opaque {};
|
||||
pub const struct_ZigClangPCHContainerOperations = opaque {};
|
||||
pub const struct_ZigClangParenExpr = opaque {};
|
||||
pub const struct_ZigClangParenType = opaque {};
|
||||
pub const struct_ZigClangParmVarDecl = opaque {};
|
||||
pub const struct_ZigClangPointerType = opaque {};
|
||||
pub const struct_ZigClangPreprocessedEntity = opaque {};
|
||||
pub const struct_ZigClangRecordDecl = opaque {};
|
||||
pub const struct_ZigClangRecordType = opaque {};
|
||||
pub const struct_ZigClangReturnStmt = opaque {};
|
||||
pub const struct_ZigClangSkipFunctionBodiesScope = opaque {};
|
||||
pub const struct_ZigClangSourceManager = opaque {};
|
||||
pub const struct_ZigClangSourceRange = opaque {};
|
||||
pub const ZigClangStmt = opaque {};
|
||||
pub const struct_ZigClangStringLiteral = opaque {};
|
||||
pub const struct_ZigClangStringRef = opaque {};
|
||||
pub const struct_ZigClangSwitchStmt = opaque {};
|
||||
pub const struct_ZigClangTagDecl = opaque {};
|
||||
pub const struct_ZigClangType = opaque {};
|
||||
pub const struct_ZigClangTypedefNameDecl = opaque {};
|
||||
pub const struct_ZigClangTypedefType = opaque {};
|
||||
pub const struct_ZigClangUnaryExprOrTypeTraitExpr = opaque {};
|
||||
pub const struct_ZigClangUnaryOperator = opaque {};
|
||||
pub const struct_ZigClangValueDecl = opaque {};
|
||||
pub const struct_ZigClangVarDecl = opaque {};
|
||||
pub const struct_ZigClangWhileStmt = opaque {};
|
||||
pub const struct_ZigClangFunctionType = opaque {};
|
||||
pub const struct_ZigClangPredefinedExpr = opaque {};
|
||||
pub const struct_ZigClangInitListExpr = opaque {};
|
||||
pub const ZigClangPreprocessingRecord = opaque {};
|
||||
pub const ZigClangFloatingLiteral = opaque {};
|
||||
pub const ZigClangConstantExpr = opaque {};
|
||||
pub const ZigClangCharacterLiteral = opaque {};
|
||||
pub const ZigClangStmtExpr = opaque {};
|
||||
|
||||
pub const ZigClangBO = extern enum {
|
||||
PtrMemD,
|
||||
@@ -749,11 +749,11 @@ pub const ZigClangCharacterLiteral_CharacterKind = extern enum {
|
||||
};
|
||||
|
||||
pub const ZigClangRecordDecl_field_iterator = extern struct {
|
||||
opaque: *c_void,
|
||||
ptr: *c_void,
|
||||
};
|
||||
|
||||
pub const ZigClangEnumDecl_enumerator_iterator = extern struct {
|
||||
opaque: *c_void,
|
||||
ptr: *c_void,
|
||||
};
|
||||
|
||||
pub const ZigClangPreprocessingRecord_iterator = extern struct {
|
||||
|
||||
@@ -1053,6 +1053,7 @@ enum ContainerKind {
|
||||
ContainerKindStruct,
|
||||
ContainerKindEnum,
|
||||
ContainerKindUnion,
|
||||
ContainerKindOpaque,
|
||||
};
|
||||
|
||||
enum ContainerLayout {
|
||||
@@ -1570,7 +1571,10 @@ enum OnePossibleValue {
|
||||
};
|
||||
|
||||
struct ZigTypeOpaque {
|
||||
AstNode *decl_node;
|
||||
Buf *bare_name;
|
||||
|
||||
ScopeDecls *decls_scope;
|
||||
};
|
||||
|
||||
struct ZigTypeFnFrame {
|
||||
|
||||
@@ -86,14 +86,18 @@ ZigType *new_type_table_entry(ZigTypeId id) {
|
||||
}
|
||||
|
||||
static ScopeDecls **get_container_scope_ptr(ZigType *type_entry) {
|
||||
if (type_entry->id == ZigTypeIdStruct) {
|
||||
return &type_entry->data.structure.decls_scope;
|
||||
} else if (type_entry->id == ZigTypeIdEnum) {
|
||||
return &type_entry->data.enumeration.decls_scope;
|
||||
} else if (type_entry->id == ZigTypeIdUnion) {
|
||||
return &type_entry->data.unionation.decls_scope;
|
||||
switch (type_entry->id) {
|
||||
case ZigTypeIdStruct:
|
||||
return &type_entry->data.structure.decls_scope;
|
||||
case ZigTypeIdEnum:
|
||||
return &type_entry->data.enumeration.decls_scope;
|
||||
case ZigTypeIdUnion:
|
||||
return &type_entry->data.unionation.decls_scope;
|
||||
case ZigTypeIdOpaque:
|
||||
return &type_entry->data.opaque.decls_scope;
|
||||
default:
|
||||
zig_unreachable();
|
||||
}
|
||||
zig_unreachable();
|
||||
}
|
||||
|
||||
static ScopeExpr *find_expr_scope(Scope *scope) {
|
||||
@@ -912,13 +916,17 @@ ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const c
|
||||
ZigType *import = scope ? get_scope_import(scope) : nullptr;
|
||||
unsigned line = source_node ? (unsigned)(source_node->line + 1) : 0;
|
||||
|
||||
// Note: duplicated in get_partial_container_type
|
||||
entry->llvm_type = LLVMInt8Type();
|
||||
entry->llvm_di_type = ZigLLVMCreateDebugForwardDeclType(g->dbuilder,
|
||||
ZigLLVMTag_DW_structure_type(), full_name,
|
||||
import ? ZigLLVMFileToScope(import->data.structure.root_struct->di_file) : nullptr,
|
||||
import ? import->data.structure.root_struct->di_file : nullptr,
|
||||
line);
|
||||
entry->data.opaque.decl_node = source_node;
|
||||
entry->data.opaque.bare_name = bare_name;
|
||||
entry->data.opaque.decls_scope = create_decls_scope(
|
||||
g, source_node, scope, entry, import, &entry->name);
|
||||
|
||||
// The actual size is unknown, but the value must not be 0 because that
|
||||
// is how type_has_bits is determined.
|
||||
@@ -1078,6 +1086,8 @@ static ZigTypeId container_to_type(ContainerKind kind) {
|
||||
return ZigTypeIdEnum;
|
||||
case ContainerKindUnion:
|
||||
return ZigTypeIdUnion;
|
||||
case ContainerKindOpaque:
|
||||
return ZigTypeIdOpaque;
|
||||
}
|
||||
zig_unreachable();
|
||||
}
|
||||
@@ -1119,6 +1129,22 @@ ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind
|
||||
entry->data.unionation.decl_node = decl_node;
|
||||
entry->data.unionation.layout = layout;
|
||||
break;
|
||||
case ContainerKindOpaque: {
|
||||
ZigType *import = scope ? get_scope_import(scope) : nullptr;
|
||||
unsigned line = decl_node ? (unsigned)(decl_node->line + 1) : 0;
|
||||
// Note: duplicated in get_opaque_type
|
||||
entry->llvm_type = LLVMInt8Type();
|
||||
entry->llvm_di_type = ZigLLVMCreateDebugForwardDeclType(g->dbuilder,
|
||||
ZigLLVMTag_DW_structure_type(), full_name,
|
||||
import ? ZigLLVMFileToScope(import->data.structure.root_struct->di_file) : nullptr,
|
||||
import ? import->data.structure.root_struct->di_file : nullptr,
|
||||
line);
|
||||
entry->data.opaque.decl_node = decl_node;
|
||||
entry->abi_size = SIZE_MAX;
|
||||
entry->size_in_bits = SIZE_MAX;
|
||||
entry->abi_align = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
buf_init_from_str(&entry->name, full_name);
|
||||
@@ -3428,6 +3454,21 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
|
||||
return ErrorNone;
|
||||
}
|
||||
|
||||
static Error resolve_opaque_type(CodeGen *g, ZigType *opaque_type) {
|
||||
Error err = ErrorNone;
|
||||
AstNode *container_node = opaque_type->data.opaque.decl_node;
|
||||
if (container_node != nullptr) {
|
||||
assert(container_node->type == NodeTypeContainerDecl);
|
||||
AstNodeContainerDecl *container_decl = &container_node->data.container_decl;
|
||||
for (size_t i = 0; i < container_decl->fields.length; i++) {
|
||||
AstNode *field_node = container_decl->fields.items[i];
|
||||
add_node_error(g, field_node, buf_create_from_str("opaque types cannot have fields"));
|
||||
err = ErrorSemanticAnalyzeFail;
|
||||
}
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
void append_namespace_qualification(CodeGen *g, Buf *buf, ZigType *container_type) {
|
||||
if (g->root_import == container_type || buf_len(&container_type->name) == 0) return;
|
||||
buf_append_buf(buf, &container_type->name);
|
||||
@@ -3893,6 +3934,8 @@ static Error resolve_decl_container(CodeGen *g, TldContainer *tld_container) {
|
||||
return resolve_enum_zero_bits(g, tld_container->type_entry);
|
||||
case ZigTypeIdUnion:
|
||||
return resolve_union_type(g, tld_container->type_entry);
|
||||
case ZigTypeIdOpaque:
|
||||
return resolve_opaque_type(g, tld_container->type_entry);
|
||||
default:
|
||||
zig_unreachable();
|
||||
}
|
||||
@@ -4459,6 +4502,7 @@ bool is_container(ZigType *type_entry) {
|
||||
return type_entry->data.structure.special != StructSpecialSlice;
|
||||
case ZigTypeIdEnum:
|
||||
case ZigTypeIdUnion:
|
||||
case ZigTypeIdOpaque:
|
||||
return true;
|
||||
case ZigTypeIdPointer:
|
||||
case ZigTypeIdMetaType:
|
||||
@@ -4478,7 +4522,6 @@ bool is_container(ZigType *type_entry) {
|
||||
case ZigTypeIdErrorSet:
|
||||
case ZigTypeIdFn:
|
||||
case ZigTypeIdBoundFn:
|
||||
case ZigTypeIdOpaque:
|
||||
case ZigTypeIdVector:
|
||||
case ZigTypeIdFnFrame:
|
||||
case ZigTypeIdAnyFrame:
|
||||
@@ -8165,6 +8208,7 @@ const char *container_string(ContainerKind kind) {
|
||||
case ContainerKindEnum: return "enum";
|
||||
case ContainerKindStruct: return "struct";
|
||||
case ContainerKindUnion: return "union";
|
||||
case ContainerKindOpaque: return "opaque";
|
||||
}
|
||||
zig_unreachable();
|
||||
}
|
||||
@@ -8183,8 +8227,6 @@ Buf *type_bare_name(ZigType *type_entry) {
|
||||
return &type_entry->name;
|
||||
} else if (is_container(type_entry)) {
|
||||
return get_container_scope(type_entry)->bare_name;
|
||||
} else if (type_entry->id == ZigTypeIdOpaque) {
|
||||
return type_entry->data.opaque.bare_name;
|
||||
} else {
|
||||
return &type_entry->name;
|
||||
}
|
||||
|
||||
@@ -22386,6 +22386,8 @@ static IrInstGen *ir_analyze_container_member_access_inner(IrAnalyze *ira,
|
||||
prefix_name = "enum ";
|
||||
} else if (bare_struct_type->id == ZigTypeIdUnion) {
|
||||
prefix_name = "union ";
|
||||
} else if (bare_struct_type->id == ZigTypeIdOpaque) {
|
||||
prefix_name = "opaque type ";
|
||||
} else {
|
||||
prefix_name = "";
|
||||
}
|
||||
@@ -22586,7 +22588,7 @@ static IrInstGen *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name
|
||||
}
|
||||
}
|
||||
|
||||
if (bare_type->id == ZigTypeIdEnum) {
|
||||
if (bare_type->id == ZigTypeIdEnum || bare_type->id == ZigTypeIdOpaque) {
|
||||
return ir_analyze_container_member_access_inner(ira, bare_type, field_name,
|
||||
source_instr, container_ptr, container_ptr_src, container_type);
|
||||
}
|
||||
@@ -25182,7 +25184,6 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
|
||||
case ZigTypeIdEnumLiteral:
|
||||
case ZigTypeIdUndefined:
|
||||
case ZigTypeIdNull:
|
||||
case ZigTypeIdOpaque:
|
||||
result = ira->codegen->intern.for_void();
|
||||
break;
|
||||
case ZigTypeIdInt:
|
||||
@@ -25736,6 +25737,25 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
|
||||
if ((err = ir_make_type_info_value(ira, source_instr, fn_type, &result)))
|
||||
return err;
|
||||
|
||||
break;
|
||||
}
|
||||
case ZigTypeIdOpaque:
|
||||
{
|
||||
result = ira->codegen->pass1_arena->create<ZigValue>();
|
||||
result->special = ConstValSpecialStatic;
|
||||
result->type = ir_type_info_get_type(ira, "Opaque", nullptr);
|
||||
|
||||
ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 1);
|
||||
result->data.x_struct.fields = fields;
|
||||
|
||||
// decls: []TypeInfo.Declaration
|
||||
ensure_field_index(result->type, "decls", 0);
|
||||
if ((err = ir_make_type_info_decls(ira, source_instr, fields[0],
|
||||
type_entry->data.opaque.decls_scope, false)))
|
||||
{
|
||||
return err;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case ZigTypeIdFnFrame:
|
||||
@@ -26043,6 +26063,21 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
|
||||
return get_error_union_type(ira->codegen, err_set_type, payload_type);
|
||||
}
|
||||
case ZigTypeIdOpaque: {
|
||||
assert(payload->special == ConstValSpecialStatic);
|
||||
assert(payload->type == ir_type_info_get_type(ira, "Opaque", nullptr));
|
||||
|
||||
ZigValue *decls_value = get_const_field(ira, source_instr->source_node, payload, "decls", 0);
|
||||
if (decls_value == nullptr)
|
||||
return ira->codegen->invalid_inst_gen->value->type;
|
||||
assert(decls_value->special == ConstValSpecialStatic);
|
||||
assert(is_slice(decls_value->type));
|
||||
ZigValue *decls_len_value = decls_value->data.x_struct.fields[slice_len_index];
|
||||
size_t decls_len = bigint_as_usize(&decls_len_value->data.x_bigint);
|
||||
if (decls_len != 0) {
|
||||
ir_add_error(ira, source_instr, buf_create_from_str("TypeInfo.Struct.decls must be empty for @Type"));
|
||||
return ira->codegen->invalid_inst_gen->value->type;
|
||||
}
|
||||
|
||||
Buf *bare_name = buf_alloc();
|
||||
Buf *full_name = get_anon_type_name(ira->codegen,
|
||||
ira->old_irb.exec, "opaque", source_instr->scope, source_instr->source_node, bare_name);
|
||||
|
||||
@@ -2920,6 +2920,7 @@ static AstNode *ast_parse_container_decl_auto(ParseContext *pc) {
|
||||
// <- KEYWORD_struct
|
||||
// / KEYWORD_enum (LPAREN Expr RPAREN)?
|
||||
// / KEYWORD_union (LPAREN (KEYWORD_enum (LPAREN Expr RPAREN)? / Expr) RPAREN)?
|
||||
// / KEYWORD_opaque
|
||||
static AstNode *ast_parse_container_decl_type(ParseContext *pc) {
|
||||
Token *first = eat_token_if(pc, TokenIdKeywordStruct);
|
||||
if (first != nullptr) {
|
||||
@@ -2929,6 +2930,14 @@ static AstNode *ast_parse_container_decl_type(ParseContext *pc) {
|
||||
return res;
|
||||
}
|
||||
|
||||
first = eat_token_if(pc, TokenIdKeywordOpaque);
|
||||
if (first != nullptr) {
|
||||
AstNode *res = ast_create_node(pc, NodeTypeContainerDecl, first);
|
||||
res->data.container_decl.init_arg_expr = nullptr;
|
||||
res->data.container_decl.kind = ContainerKindOpaque;
|
||||
return res;
|
||||
}
|
||||
|
||||
first = eat_token_if(pc, TokenIdKeywordEnum);
|
||||
if (first != nullptr) {
|
||||
AstNode *init_arg_expr = nullptr;
|
||||
|
||||
@@ -133,6 +133,7 @@ static const struct ZigKeyword zig_keywords[] = {
|
||||
{"noinline", TokenIdKeywordNoInline},
|
||||
{"nosuspend", TokenIdKeywordNoSuspend},
|
||||
{"null", TokenIdKeywordNull},
|
||||
{"opaque", TokenIdKeywordOpaque},
|
||||
{"or", TokenIdKeywordOr},
|
||||
{"orelse", TokenIdKeywordOrElse},
|
||||
{"packed", TokenIdKeywordPacked},
|
||||
@@ -1595,6 +1596,7 @@ const char * token_name(TokenId id) {
|
||||
case TokenIdKeywordNoInline: return "noinline";
|
||||
case TokenIdKeywordNoSuspend: return "nosuspend";
|
||||
case TokenIdKeywordNull: return "null";
|
||||
case TokenIdKeywordOpaque: return "opaque";
|
||||
case TokenIdKeywordOr: return "or";
|
||||
case TokenIdKeywordOrElse: return "orelse";
|
||||
case TokenIdKeywordPacked: return "packed";
|
||||
|
||||
@@ -81,6 +81,7 @@ enum TokenId {
|
||||
TokenIdKeywordNoAlias,
|
||||
TokenIdKeywordNoSuspend,
|
||||
TokenIdKeywordNull,
|
||||
TokenIdKeywordOpaque,
|
||||
TokenIdKeywordOr,
|
||||
TokenIdKeywordOrElse,
|
||||
TokenIdKeywordPacked,
|
||||
|
||||
@@ -930,9 +930,9 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
|
||||
const init_node = blk: {
|
||||
const rp = makeRestorePoint(c);
|
||||
const record_def = ZigClangRecordDecl_getDefinition(record_decl) orelse {
|
||||
const opaque = try transCreateNodeOpaqueType(c);
|
||||
const opaque_type = try transCreateNodeOpaqueType(c);
|
||||
semicolon = try appendToken(c, .Semicolon, ";");
|
||||
break :blk opaque;
|
||||
break :blk opaque_type;
|
||||
};
|
||||
|
||||
const layout_tok = try if (ZigClangRecordDecl_getPackedAttribute(record_decl))
|
||||
@@ -954,17 +954,17 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
|
||||
const field_qt = ZigClangFieldDecl_getType(field_decl);
|
||||
|
||||
if (ZigClangFieldDecl_isBitField(field_decl)) {
|
||||
const opaque = try transCreateNodeOpaqueType(c);
|
||||
const opaque_type = try transCreateNodeOpaqueType(c);
|
||||
semicolon = try appendToken(c, .Semicolon, ";");
|
||||
try emitWarning(c, field_loc, "{} demoted to opaque type - has bitfield", .{container_kind_name});
|
||||
break :blk opaque;
|
||||
break :blk opaque_type;
|
||||
}
|
||||
|
||||
if (ZigClangType_isIncompleteOrZeroLengthArrayType(qualTypeCanon(field_qt), c.clang_context)) {
|
||||
const opaque = try transCreateNodeOpaqueType(c);
|
||||
const opaque_type = try transCreateNodeOpaqueType(c);
|
||||
semicolon = try appendToken(c, .Semicolon, ";");
|
||||
try emitWarning(c, field_loc, "{} demoted to opaque type - has variable length array", .{container_kind_name});
|
||||
break :blk opaque;
|
||||
break :blk opaque_type;
|
||||
}
|
||||
|
||||
var is_anon = false;
|
||||
@@ -979,10 +979,10 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
|
||||
_ = try appendToken(c, .Colon, ":");
|
||||
const field_type = transQualType(rp, field_qt, field_loc) catch |err| switch (err) {
|
||||
error.UnsupportedType => {
|
||||
const opaque = try transCreateNodeOpaqueType(c);
|
||||
const opaque_type = try transCreateNodeOpaqueType(c);
|
||||
semicolon = try appendToken(c, .Semicolon, ";");
|
||||
try emitWarning(c, record_loc, "{} demoted to opaque type - unable to translate type of field {}", .{ container_kind_name, raw_name });
|
||||
break :blk opaque;
|
||||
break :blk opaque_type;
|
||||
},
|
||||
else => |e| return e,
|
||||
};
|
||||
@@ -4438,10 +4438,18 @@ fn transCreateNodeFloat(c: *Context, int: anytype) !*ast.Node {
|
||||
}
|
||||
|
||||
fn transCreateNodeOpaqueType(c: *Context) !*ast.Node {
|
||||
const call_node = try c.createBuiltinCall("@Type", 1);
|
||||
call_node.params()[0] = try transCreateNodeEnumLiteral(c, "Opaque");
|
||||
call_node.rparen_token = try appendToken(c, .RParen, ")");
|
||||
return &call_node.base;
|
||||
const container_tok = try appendToken(c, .Keyword_opaque, "opaque");
|
||||
const lbrace_token = try appendToken(c, .LBrace, "{");
|
||||
const container_node = try ast.Node.ContainerDecl.alloc(c.arena, 0);
|
||||
container_node.* = .{
|
||||
.kind_token = container_tok,
|
||||
.layout_token = null,
|
||||
.lbrace_token = lbrace_token,
|
||||
.rbrace_token = try appendToken(c, .RBrace, "}"),
|
||||
.fields_and_decls_len = 0,
|
||||
.init_arg_expr = .None,
|
||||
};
|
||||
return &container_node.base;
|
||||
}
|
||||
|
||||
fn transCreateNodeMacroFn(c: *Context, name: []const u8, ref: *ast.Node, proto_alias: *ast.Node.FnProto) !*ast.Node {
|
||||
|
||||
@@ -125,6 +125,31 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
|
||||
"tmp.zig:15:23: error: enum field missing: 'arst'",
|
||||
"tmp.zig:27:24: note: referenced here",
|
||||
});
|
||||
|
||||
cases.add("field access of opaque type",
|
||||
\\const MyType = opaque {};
|
||||
\\
|
||||
\\export fn entry() bool {
|
||||
\\ var x: i32 = 1;
|
||||
\\ return bar(@ptrCast(*MyType, &x));
|
||||
\\}
|
||||
\\
|
||||
\\fn bar(x: *MyType) bool {
|
||||
\\ return x.blah;
|
||||
\\}
|
||||
, &[_][]const u8{
|
||||
"tmp.zig:9:13: error: no member named 'blah' in opaque type 'MyType'",
|
||||
});
|
||||
|
||||
cases.add("opaque type with field",
|
||||
\\const Opaque = opaque { foo: i32 };
|
||||
\\export fn entry() void {
|
||||
\\ const foo: ?*Opaque = null;
|
||||
\\}
|
||||
, &[_][]const u8{
|
||||
"tmp.zig:1:25: error: opaque types cannot have fields",
|
||||
});
|
||||
|
||||
cases.add("@Type(.Fn) with is_generic = true",
|
||||
\\const Foo = @Type(.{
|
||||
\\ .Fn = .{
|
||||
@@ -180,7 +205,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
|
||||
\\ .layout = .Auto,
|
||||
\\ .tag_type = null,
|
||||
\\ .fields = &[_]TypeInfo.UnionField{
|
||||
\\ .{ .name = "foo", .field_type = @Type(.Opaque), .alignment = 1 },
|
||||
\\ .{ .name = "foo", .field_type = opaque {}, .alignment = 1 },
|
||||
\\ },
|
||||
\\ .decls = &[_]TypeInfo.Declaration{},
|
||||
\\ },
|
||||
@@ -2613,7 +2638,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
|
||||
});
|
||||
|
||||
cases.add("directly embedding opaque type in struct and union",
|
||||
\\const O = @Type(.Opaque);
|
||||
\\const O = opaque {};
|
||||
\\const Foo = struct {
|
||||
\\ o: O,
|
||||
\\};
|
||||
@@ -2628,7 +2653,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
|
||||
\\ var bar: Bar = undefined;
|
||||
\\}
|
||||
\\export fn c() void {
|
||||
\\ var baz: *@Type(.Opaque) = undefined;
|
||||
\\ var baz: *opaque {} = undefined;
|
||||
\\ const qux = .{baz.*};
|
||||
\\}
|
||||
, &[_][]const u8{
|
||||
@@ -3592,7 +3617,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
|
||||
});
|
||||
|
||||
cases.add("unknown length pointer to opaque",
|
||||
\\export const T = [*]@Type(.Opaque);
|
||||
\\export const T = [*]opaque {};
|
||||
, &[_][]const u8{
|
||||
"tmp.zig:1:21: error: unknown-length pointer to opaque",
|
||||
});
|
||||
@@ -6827,8 +6852,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
|
||||
"tmp.zig:2:31: error: index 2 outside array of size 2",
|
||||
});
|
||||
|
||||
cases.add("wrong pointer coerced to pointer to @Type(.Opaque)",
|
||||
\\const Derp = @Type(.Opaque);
|
||||
cases.add("wrong pointer coerced to pointer to opaque {}",
|
||||
\\const Derp = opaque {};
|
||||
\\extern fn bar(d: *Derp) void;
|
||||
\\export fn foo() void {
|
||||
\\ var x = @as(u8, 1);
|
||||
@@ -6854,8 +6879,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
|
||||
\\export fn entry5() void {
|
||||
\\ var d = null;
|
||||
\\}
|
||||
\\export fn entry6(opaque: *Opaque) void {
|
||||
\\ var e = opaque.*;
|
||||
\\export fn entry6(opaque_: *Opaque) void {
|
||||
\\ var e = opaque_.*;
|
||||
\\}
|
||||
\\export fn entry7() void {
|
||||
\\ var f = i32;
|
||||
@@ -6866,7 +6891,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
|
||||
\\export fn entry9() void {
|
||||
\\ var z: noreturn = return;
|
||||
\\}
|
||||
\\const Opaque = @Type(.Opaque);
|
||||
\\const Opaque = opaque {};
|
||||
\\const Foo = struct {
|
||||
\\ fn bar(self: *const Foo) void {}
|
||||
\\};
|
||||
@@ -7019,21 +7044,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
|
||||
"tmp.zig:37:29: error: cannot store runtime value in compile time variable",
|
||||
});
|
||||
|
||||
cases.add("field access of opaque type",
|
||||
\\const MyType = @Type(.Opaque);
|
||||
\\
|
||||
\\export fn entry() bool {
|
||||
\\ var x: i32 = 1;
|
||||
\\ return bar(@ptrCast(*MyType, &x));
|
||||
\\}
|
||||
\\
|
||||
\\fn bar(x: *MyType) bool {
|
||||
\\ return x.blah;
|
||||
\\}
|
||||
, &[_][]const u8{
|
||||
"tmp.zig:9:13: error: type '*MyType' does not support field access",
|
||||
});
|
||||
|
||||
cases.add("invalid legacy unicode escape",
|
||||
\\export fn entry() void {
|
||||
\\ const a = '\U1234';
|
||||
@@ -7623,7 +7633,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
|
||||
});
|
||||
|
||||
cases.add("function returning opaque type",
|
||||
\\const FooType = @Type(.Opaque);
|
||||
\\const FooType = opaque {};
|
||||
\\export fn bar() !FooType {
|
||||
\\ return error.InvalidValue;
|
||||
\\}
|
||||
@@ -7641,7 +7651,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
|
||||
});
|
||||
|
||||
cases.add("generic function returning opaque type",
|
||||
\\const FooType = @Type(.Opaque);
|
||||
\\const FooType = opaque {};
|
||||
\\fn generic(comptime T: type) !T {
|
||||
\\ return undefined;
|
||||
\\}
|
||||
@@ -7665,7 +7675,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
|
||||
});
|
||||
|
||||
cases.add("function parameter is opaque",
|
||||
\\const FooType = @Type(.Opaque);
|
||||
\\const FooType = opaque {};
|
||||
\\export fn entry1() void {
|
||||
\\ const someFuncPtr: fn (FooType) void = undefined;
|
||||
\\}
|
||||
|
||||
@@ -74,7 +74,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
|
||||
});
|
||||
|
||||
cases.add("declare opaque type",
|
||||
\\const Foo = @Type(.Opaque);
|
||||
\\const Foo = opaque {};
|
||||
\\
|
||||
\\export fn entry(foo: ?*Foo) void { }
|
||||
, &[_][]const u8{
|
||||
|
||||
@@ -438,8 +438,8 @@ export fn writeToVRam() void {
|
||||
vram[0] = 'X';
|
||||
}
|
||||
|
||||
const OpaqueA = @Type(.Opaque);
|
||||
const OpaqueB = @Type(.Opaque);
|
||||
const OpaqueA = opaque {};
|
||||
const OpaqueB = opaque {};
|
||||
test "opaque types" {
|
||||
expect(*OpaqueA != *OpaqueB);
|
||||
expect(mem.eql(u8, @typeName(OpaqueA), "OpaqueA"));
|
||||
@@ -704,7 +704,7 @@ test "auto created variables have correct alignment" {
|
||||
comptime expect(S.foo("\x7a\x7a\x7a\x7a") == 0x7a7a7a7a);
|
||||
}
|
||||
|
||||
extern var opaque_extern_var: @Type(.Opaque);
|
||||
extern var opaque_extern_var: opaque {};
|
||||
var var_to_export: u32 = 42;
|
||||
test "extern variable with non-pointer opaque type" {
|
||||
@export(var_to_export, .{ .name = "opaque_extern_var" });
|
||||
|
||||
@@ -190,8 +190,17 @@ test "Type.ErrorUnion" {
|
||||
}
|
||||
|
||||
test "Type.Opaque" {
|
||||
testing.expect(@Type(.Opaque) != @Type(.Opaque));
|
||||
testing.expect(@typeInfo(@Type(.Opaque)) == .Opaque);
|
||||
const Opaque = @Type(.{
|
||||
.Opaque = .{
|
||||
.decls = &[_]TypeInfo.Declaration{},
|
||||
},
|
||||
});
|
||||
testing.expect(Opaque != opaque {});
|
||||
testing.expectEqualSlices(
|
||||
TypeInfo.Declaration,
|
||||
&[_]TypeInfo.Declaration{},
|
||||
@typeInfo(Opaque).Opaque.decls,
|
||||
);
|
||||
}
|
||||
|
||||
test "Type.Vector" {
|
||||
|
||||
@@ -199,7 +199,7 @@ fn testUnion() void {
|
||||
expect(typeinfo_info.Union.tag_type.? == TypeId);
|
||||
expect(typeinfo_info.Union.fields.len == 25);
|
||||
expect(typeinfo_info.Union.fields[4].field_type == @TypeOf(@typeInfo(u8).Int));
|
||||
expect(typeinfo_info.Union.decls.len == 21);
|
||||
expect(typeinfo_info.Union.decls.len == 22);
|
||||
|
||||
const TestNoTagUnion = union {
|
||||
Foo: void,
|
||||
@@ -265,6 +265,21 @@ const TestStruct = packed struct {
|
||||
const Self = @This();
|
||||
};
|
||||
|
||||
test "type info: opaque info" {
|
||||
testOpaque();
|
||||
comptime testOpaque();
|
||||
}
|
||||
|
||||
fn testOpaque() void {
|
||||
const Foo = opaque {
|
||||
const A = 1;
|
||||
fn b() void {}
|
||||
};
|
||||
|
||||
const foo_info = @typeInfo(Foo);
|
||||
expect(foo_info.Opaque.decls.len == 2);
|
||||
}
|
||||
|
||||
test "type info: function type info" {
|
||||
// wasm doesn't support align attributes on functions
|
||||
if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;
|
||||
|
||||
@@ -137,9 +137,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
\\struct foo { int x; int y[]; };
|
||||
\\struct bar { int x; int y[0]; };
|
||||
, &[_][]const u8{
|
||||
\\pub const struct_foo = @Type(.Opaque);
|
||||
\\pub const struct_foo = opaque {};
|
||||
,
|
||||
\\pub const struct_bar = @Type(.Opaque);
|
||||
\\pub const struct_bar = opaque {};
|
||||
});
|
||||
|
||||
cases.add("nested loops without blocks",
|
||||
@@ -207,7 +207,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
\\pub const struct_arcan_shmif_page = //
|
||||
,
|
||||
\\warning: unsupported type: 'Atomic'
|
||||
\\ @Type(.Opaque); //
|
||||
\\ opaque {}; //
|
||||
,
|
||||
\\ warning: struct demoted to opaque type - unable to translate type of field abufused
|
||||
, // TODO should be `addr: *struct_arcan_shmif_page`
|
||||
@@ -386,8 +386,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
\\ struct opaque_2 *cast = (struct opaque_2 *)opaque;
|
||||
\\}
|
||||
, &[_][]const u8{
|
||||
\\pub const struct_opaque = @Type(.Opaque);
|
||||
\\pub const struct_opaque_2 = @Type(.Opaque);
|
||||
\\pub const struct_opaque = opaque {};
|
||||
\\pub const struct_opaque_2 = opaque {};
|
||||
\\pub export fn function(arg_opaque_1: ?*struct_opaque) void {
|
||||
\\ var opaque_1 = arg_opaque_1;
|
||||
\\ var cast: ?*struct_opaque_2 = @ptrCast(?*struct_opaque_2, opaque_1);
|
||||
@@ -628,7 +628,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
\\ struct Foo *foo;
|
||||
\\};
|
||||
, &[_][]const u8{
|
||||
\\pub const struct_Foo = @Type(.Opaque);
|
||||
\\pub const struct_Foo = opaque {};
|
||||
,
|
||||
\\pub const struct_Bar = extern struct {
|
||||
\\ foo: ?*struct_Foo,
|
||||
@@ -705,7 +705,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
\\struct Foo;
|
||||
\\struct Foo *some_func(struct Foo *foo, int x);
|
||||
, &[_][]const u8{
|
||||
\\pub const struct_Foo = @Type(.Opaque);
|
||||
\\pub const struct_Foo = opaque {};
|
||||
,
|
||||
\\pub extern fn some_func(foo: ?*struct_Foo, x: c_int) ?*struct_Foo;
|
||||
,
|
||||
|
||||
Reference in New Issue
Block a user