This commit is contained in:
Zahary Karadjov 2018-06-10 16:44:49 +03:00 • committed by zah
commit 5f2cdcd4fa
4 changed files with 34 additions and 13 deletions

View file

@ -791,8 +791,6 @@ proc getTypeDescAux(m: BModule, origTyp: PType, check: var IntSet): Rope =
else: addAbiCheck(m, t, result) else: addAbiCheck(m, t, result)
of tyObject, tyTuple: of tyObject, tyTuple:
if isImportedCppType(t) and origTyp.kind == tyGenericInst: if isImportedCppType(t) and origTyp.kind == tyGenericInst:
# for instantiated templates we do not go through the type cache as the
# the type cache is not aware of 'tyGenericInst'.
let cppName = getTypeName(m, t, sig) let cppName = getTypeName(m, t, sig)
var i = 0 var i = 0
var chunkStart = 0 var chunkStart = 0

View file

@ -12,7 +12,7 @@
import ast, md5, tables, ropes import ast, md5, tables, ropes
from hashes import Hash from hashes import Hash
from astalgo import debug from astalgo import debug
from types import typeToString, preferDesc import types
from strutils import startsWith, contains from strutils import startsWith, contains
when false: when false:
@ -148,19 +148,23 @@ proc hashType(c: var MD5Context, t: PType; flags: set[ConsiderFlag]) =
of tyGenericInvocation: of tyGenericInvocation:
for i in countup(0, sonsLen(t) - 1): for i in countup(0, sonsLen(t) - 1):
c.hashType t.sons[i], flags c.hashType t.sons[i], flags
return
of tyDistinct: of tyDistinct:
if CoType in flags: if CoType in flags:
c.hashType t.lastSon, flags c.hashType t.lastSon, flags
else: else:
c.hashSym(t.sym) c.hashSym(t.sym)
return of tyGenericInst:
of tyAlias, tySink, tyGenericInst, tyUserTypeClasses: if sfInfixCall in t.base.sym.flags:
# This is an imported C++ generic type.
# We cannot trust the `lastSon` to hold a properly populated and unique
# value for each instantiation, so we hash the generic parameters here:
let normalizedType = t.skipGenericAlias
for i in 0 .. normalizedType.len - 2:
c.hashType t.sons[i], flags
else:
c.hashType t.lastSon, flags
of tyAlias, tySink, tyUserTypeClasses:
c.hashType t.lastSon, flags c.hashType t.lastSon, flags
return
else:
discard
case t.kind
of tyBool, tyChar, tyInt..tyUInt64: of tyBool, tyChar, tyInt..tyUInt64:
# no canonicalization for integral types, so that e.g. ``pid_t`` is # no canonicalization for integral types, so that e.g. ``pid_t`` is
# produced instead of ``NI``: # produced instead of ``NI``:
@ -168,11 +172,12 @@ proc hashType(c: var MD5Context, t: PType; flags: set[ConsiderFlag]) =
if t.sym != nil and {sfImportc, sfExportc} * t.sym.flags != {}: if t.sym != nil and {sfImportc, sfExportc} * t.sym.flags != {}:
c.hashSym(t.sym) c.hashSym(t.sym)
of tyObject, tyEnum: of tyObject, tyEnum:
c &= char(t.kind)
if t.typeInst != nil: if t.typeInst != nil:
assert t.typeInst.kind == tyGenericInst assert t.typeInst.kind == tyGenericInst
for i in countup(1, sonsLen(t.typeInst) - 2): for i in countup(0, sonsLen(t.typeInst) - 2):
c.hashType t.typeInst.sons[i], flags c.hashType t.typeInst.sons[i], flags
return
c &= char(t.kind)
# Every cyclic type in Nim need to be constructed via some 't.sym', so this # Every cyclic type in Nim need to be constructed via some 't.sym', so this
# is actually safe without an infinite recursion check: # is actually safe without an infinite recursion check:
if t.sym != nil: if t.sym != nil:

View file

@ -20,3 +20,19 @@ v.doSomething()
var vf = initVector[float]() var vf = initVector[float]()
vf.doSomething() # Nim uses doSomething[int] here in C++ vf.doSomething() # Nim uses doSomething[int] here in C++
# Alternative definition:
# https://github.com/nim-lang/Nim/issues/7653
type VectorAlt* {.importcpp: "std::vector", header: "<vector>", nodecl.} [T] = object
proc mkVector*[T]: VectorAlt[T] {.importcpp: "std::vector<'*0>()", header: "<vector>", constructor, nodecl.}
proc foo(): VectorAlt[cint] =
mkVector[cint]()
proc bar(): VectorAlt[cstring] =
mkVector[cstring]()
var x = foo()
var y = bar()

View file

@ -1,12 +1,14 @@
discard """ discard """
output: '''42''' output: "42\n42"
""" """
type type
Foo[N: static[int]] = object Foo[N: static[int]] = object
proc foo[N](x: Foo[N]) = proc foo[N](x: Foo[N]) =
let n = N
echo N echo N
echo n
var f1: Foo[42] var f1: Foo[42]
f1.foo f1.foo