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

fixes #24604; importc fails to generate stub type #24606

Open
wants to merge 8 commits into
base: devel
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ errors.

- With `-d:nimPreviewAsmSemSymbol`, backticked symbols are type checked in the `asm/emit` statements.

- `importc` no longer implies `nodecl` for imported types. Use `header` or `nodecl` pragmas so that it doesn't generate a declaration for the type symbol.

## Standard library additions and changes

[//]: # "Additions:"
Expand Down
9 changes: 6 additions & 3 deletions compiler/ccgtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ proc mapReturnType(conf: ConfigRef; typ: PType): TCTypeKind =
proc isImportedType(t: PType): bool =
result = t.sym != nil and sfImportc in t.sym.flags

proc isNoDeclType(t: PType): bool =
result = t.sym != nil and {lfNoDecl, lfHeader} * t.sym.loc.flags != {}

proc isImportedCppType(t: PType): bool =
let x = t.skipTypes(irrelevantForBackend)
result = (t.sym != nil and sfInfixCall in t.sym.flags) or
Expand Down Expand Up @@ -390,7 +393,7 @@ proc getTypeForward(m: BModule; typ: PType; sig: SigHash): Rope =
of tySequence, tyTuple, tyObject:
result = getTypeName(m, typ, sig)
m.forwTypeCache[sig] = result
if not isImportedType(concrete):
if not isNoDeclType(concrete):
addForwardStructFormat(m, structOrUnion(typ), result)
else:
pushType(m, concrete)
Expand Down Expand Up @@ -1043,14 +1046,14 @@ proc getTypeDescAux(m: BModule; origTyp: PType, check: var IntSet; kind: TypeDes
if result == "":
result = getTypeName(m, origTyp, sig)
m.forwTypeCache[sig] = result
if not isImportedType(t):
if not isNoDeclType(t):
addForwardStructFormat(m, structOrUnion(t), result)
assert m.forwTypeCache[sig] == result
m.typeCache[sig] = result # always call for sideeffects:
if not incompleteType(t):
let recdesc = if t.kind != tyTuple: getRecordDesc(m, t, result, check)
else: getTupleDesc(m, t, result, check)
if not isImportedType(t):
if not isImportedType(t) and not isNoDeclType(t):
m.s[cfsTypes].add(recdesc)
elif tfIncompleteStruct notin t.flags:
discard # addAbiCheck(m, t, result) # already handled elsewhere
Expand Down
2 changes: 1 addition & 1 deletion lib/std/sysrand.nim
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ elif defined(ios) or defined(macosx):
const errSecSuccess = 0 ## No error.

type
SecRandom {.importc: "struct __SecRandom".} = object
SecRandom {.importc: "struct __SecRandom", header: "<Security/SecRandom.h>".} = object

SecRandomRef = ptr SecRandom
## An abstract Core Foundation-type object containing information about a random number generator.
Expand Down
4 changes: 2 additions & 2 deletions lib/system/dyncalls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ elif defined(windows) or defined(dos):
#
when defined(cpp):
type
THINSTANCE {.importc: "HINSTANCE".} = object
THINSTANCE {.importc: "HINSTANCE", nodecl.} = object
x: pointer
proc getProcAddress(lib: THINSTANCE, name: cstring): ProcAddr {.
importcpp: "(void*)GetProcAddress(@)", header: "<windows.h>", stdcall.}
else:
type
THINSTANCE {.importc: "HINSTANCE".} = pointer
THINSTANCE {.importc: "HINSTANCE", nodecl.} = pointer
proc getProcAddress(lib: THINSTANCE, name: cstring): ProcAddr {.
importc: "GetProcAddress", header: "<windows.h>", stdcall.}

Expand Down
4 changes: 2 additions & 2 deletions tests/arc/t14472.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ var s = bork()
import tables

type
cdbl {.importc: "double".} = object
cdbl {.importc: "double", nodecl.} = object

MyObject = ref object of RootObj
y: Table[string, cdbl]


proc test =
var x = new(MyObject)
Expand Down
2 changes: 1 addition & 1 deletion tests/ccgbugs/tcgbug.nim
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ block: # bug #9940
typedef struct { int base; } S;
""".}

type S {.importc: "S", completeStruct.} = object
type S {.importc: "S", nodecl, completeStruct.} = object
base: cint
proc init(x:ptr S) =
x.base = 1
Expand Down
7 changes: 6 additions & 1 deletion tests/ccgbugs/tctypes.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
discard """
targets: "c cpp"
matrix: "--gc:refc; --gc:arc"
matrix: "--mm:refc; --mm:arc"
"""

# bug #7308
Expand Down Expand Up @@ -41,3 +41,8 @@ block: # bug #11797
proc foo3(): int32 = 2
foo(proc(): cint = foo1())
foo(proc(): int32 = foo3())


block: # bug #24604
type MyType {.importc, incompleteStruct.} = object
var v {.exportc.}: ptr MyType
2 changes: 1 addition & 1 deletion tests/cpp/tget_subsystem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct SystemManager {

""".}

type Input {.importcpp: "System::Input".} = object
type Input {.importcpp: "System::Input", nodecl.} = object
proc getSubsystem*[T](): ptr T {.
importcpp: "SystemManager::getSubsystem<'*0>()", nodecl.}

Expand Down
2 changes: 1 addition & 1 deletion tests/misc/msizeof5.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ template ensureCgen(T: typedesc) =
var a {.volatile.}: T

block:
type Foo1Alias{.importc: "struct Foo1", size: sizeof(cint).} = object
type Foo1Alias{.importc: "struct Foo1", nodecl, size: sizeof(cint).} = object
a: cint
ensureCgen Foo1Alias

Expand Down
4 changes: 2 additions & 2 deletions tests/misc/tsizeof.nim
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ typedef struct{
""".}

type
Foo {.importc.} = object
Foo {.importc, nodecl.} = object

Bar = object
b: byte
Expand Down Expand Up @@ -549,7 +549,7 @@ doAssert alignof(MyCustomAlignObject) == 32
##########################################

type
imported_double {.importc: "double".} = object
imported_double {.importc: "double", nodecl.} = object

Pod = object
v* : imported_double
Expand Down
8 changes: 4 additions & 4 deletions tests/niminaction/Chapter8/sfml/sfml.nim
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{.passL: "-lsfml-graphics -lsfml-system -lsfml-window".}

type
VideoMode* {.importcpp: "sf::VideoMode".} = object
RenderWindowObj {.importcpp: "sf::RenderWindow".} = object
VideoMode* {.importcpp: "sf::VideoMode", nodecl.} = object
RenderWindowObj {.importcpp: "sf::RenderWindow", nodecl.} = object
RenderWindow* = ptr RenderWindowObj
Color* {.importcpp: "sf::Color".} = object
Event* {.importcpp: "sf::Event".} = object
Color* {.importcpp: "sf::Color", nodecl.} = object
Event* {.importcpp: "sf::Event", nodecl.} = object

{.push cdecl, header: "<SFML/Graphics.hpp>".}

Expand Down
2 changes: 1 addition & 1 deletion tests/overload/tstatic_with_converter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ cimported set1_imported(double x) {

"""}

type vfloat{.importc: "cimported".} = object
type vfloat{.importc: "cimported", nodecl.} = object

proc set1(a: float): vfloat {.importc: "set1_imported".}

Expand Down
Loading