Merge branch 'devel' of https://github.com/Araq/Nim into devel

This commit is contained in:
Araq
2014-12-31 16:08:07 +01:00
6 changed files with 65 additions and 8 deletions

View File

@@ -926,7 +926,8 @@ const
proc readTypeParameter(c: PContext, typ: PType,
paramName: PIdent, info: TLineInfo): PNode =
let ty = if typ.kind == tyGenericInst: typ.skipGenericAlias
else: (internalAssert(typ.kind == tyCompositeTypeClass); typ.sons[1])
else: (internalAssert(typ.kind == tyCompositeTypeClass);
typ.sons[1].skipGenericAlias)
#debug ty
let tbody = ty.sons[0]
for s in countup(0, tbody.len-2):
@@ -965,6 +966,7 @@ proc builtinFieldAccess(c: PContext, n: PNode, flags: TExprFlags): PNode =
var ty = n.sons[0].typ
var f: PSym = nil
result = nil
if isTypeExpr(n.sons[0]) or (ty.kind == tyTypeDesc and ty.base.kind != tyNone):
if ty.kind == tyTypeDesc: ty = ty.base
ty = ty.skipTypes(tyDotOpTransparent)

View File

@@ -72,7 +72,8 @@ proc semGenericStmtSymbol(c: PContext, n: PNode, s: PSym,
result = n
styleCheckUse(n.info, s)
of skType:
if (s.typ != nil) and (s.typ.kind != tyGenericParam):
if (s.typ != nil) and
(s.typ.flags * {tfGenericTypeParam, tfImplicitTypeParam} == {}):
result = newSymNodeTypeDesc(s, n.info)
else:
result = n

View File

@@ -979,6 +979,9 @@ proc typeRel(c: var TCandidate, f, aOrig: PType, doBind = true): TTypeRelation =
of tyStatic:
if aOrig.kind == tyStatic:
result = typeRel(c, f.lastSon, a)
if result != isNone and f.n != nil:
if not exprStructuralEquivalent(f.n, a.n):
result = isNone
if result != isNone: put(c.bindings, f, aOrig)
else:
result = isNone

View File

@@ -17,7 +17,7 @@ proc lastOrd*(t: PType): BiggestInt
proc lengthOrd*(t: PType): BiggestInt
type
TPreferedDesc* = enum
preferName, preferDesc, preferExported, preferModuleInfo
preferName, preferDesc, preferExported, preferModuleInfo, preferGenericArg
proc typeToString*(typ: PType; prefer: TPreferedDesc = preferName): string
proc base*(t: PType): PType
@@ -411,11 +411,13 @@ const
"UserTypeClassInst", "CompositeTypeClass",
"and", "or", "not", "any", "static", "TypeFromExpr", "FieldAccessor"]
const preferToResolveSymbols = {preferName, preferModuleInfo, preferGenericArg}
proc typeToString(typ: PType, prefer: TPreferedDesc = preferName): string =
var t = typ
result = ""
if t == nil: return
if prefer in {preferName, preferModuleInfo} and t.sym != nil and
if prefer in preferToResolveSymbols and t.sym != nil and
sfAnon notin t.sym.flags:
if t.kind == tyInt and isIntLit(t):
return t.sym.name.s & " literal(" & $t.n.intVal & ")"
@@ -428,20 +430,26 @@ proc typeToString(typ: PType, prefer: TPreferedDesc = preferName): string =
if not isIntLit(t) or prefer == preferExported:
result = typeToStr[t.kind]
else:
result = "int literal(" & $t.n.intVal & ")"
if prefer == preferGenericArg:
result = $t.n.intVal
else:
result = "int literal(" & $t.n.intVal & ")"
of tyGenericBody, tyGenericInst, tyGenericInvokation:
result = typeToString(t.sons[0]) & '['
for i in countup(1, sonsLen(t) -1 -ord(t.kind != tyGenericInvokation)):
if i > 1: add(result, ", ")
add(result, typeToString(t.sons[i]))
add(result, typeToString(t.sons[i], preferGenericArg))
add(result, ']')
of tyTypeDesc:
if t.base.kind == tyNone: result = "typedesc"
else: result = "typedesc[" & typeToString(t.base) & "]"
of tyStatic:
internalAssert t.len > 0
result = "static[" & typeToString(t.sons[0]) & "]"
if t.n != nil: result.add "(" & renderTree(t.n) & ")"
if prefer == preferGenericArg and t.n != nil:
result = t.n.renderTree
else:
result = "static[" & typeToString(t.sons[0]) & "]"
if t.n != nil: result.add "(" & renderTree(t.n) & ")"
of tyUserTypeClass:
internalAssert t.sym != nil and t.sym.owner != nil
return t.sym.owner.name.s

26
tests/generics/t1056.nim Normal file
View File

@@ -0,0 +1,26 @@
discard """
output: '''TMatrix[3, 3, system.int]
3
3'''
"""
import typetraits
type
TMatrix*[N,M: static[int], T] = object
data*: array[0..N*M-1, T]
TMat2[T] = TMatrix[2,2,T]
proc echoMatrix(a: TMatrix) =
echo a.type.name
echo TMatrix.N
proc echoMat2(a: TMat2) =
echo TMat2.M
var m = TMatrix[3,3,int](data: [1,2,3,4,5,6,7,8,9])
echoMatrix m
echoMat2 m

View File

@@ -34,3 +34,20 @@ type
yes s.items is Iter[TNumber]
no s.items is Iter[float]
type
Foo[N: static[int], T] = object
field: array[1..N, T]
Bar[T] = Foo[4, T]
Baz[N: static[int]] = Foo[N, float]
no Foo[2, float] is Foo[3, float]
no Foo[2, float] is Foo[2, int]
yes Foo[4, string] is Foo[4, string]
yes Bar[int] is Foo[4, int]
yes Foo[4, int] is Bar[int]
no Foo[4, int] is Baz[4]
yes Foo[4, float] is Baz[4]