@@ -15,8 +15,12 @@ import
1515
1616type
1717 TPreferedDesc * = enum
18- preferName, preferDesc, preferExported, preferModuleInfo, preferGenericArg,
19- preferTypeName
18+ preferName, # default
19+ preferDesc, preferExported,
20+ preferModuleInfo, # fully qualified
21+ preferGenericArg,
22+ preferTypeName,
23+ preferResolved # fully resolved symbols
2024
2125proc typeToString * (typ: PType ; prefer: TPreferedDesc = preferName): string
2226template `$` * (typ: PType ): string = typeToString (typ)
@@ -98,6 +102,7 @@ proc isFloatLit*(t: PType): bool {.inline.} =
98102
99103proc getProcHeader * (conf: ConfigRef ; sym: PSym ; prefer: TPreferedDesc = preferName): string =
100104 assert sym != nil
105+ # consider using `skipGenericOwner` to avoid fun2.fun2 when fun2 is generic
101106 result = sym.owner.name.s & '.' & sym.name.s
102107 if sym.kind in routineKinds:
103108 result .add '('
@@ -391,12 +396,12 @@ proc rangeToStr(n: PNode): string =
391396 result = valueToString (n.sons[0 ]) & " .." & valueToString (n.sons[1 ])
392397
393398const
394- typeToStr: array [TTypeKind , string ] = [" None" , " bool" , " Char " , " empty" ,
399+ typeToStr: array [TTypeKind , string ] = [" None" , " bool" , " char " , " empty" ,
395400 " Alias" , " nil" , " untyped" , " typed" , " typeDesc" ,
396401 " GenericInvocation" , " GenericBody" , " GenericInst" , " GenericParam" ,
397402 " distinct $1" , " enum" , " ordinal[$1]" , " array[$1, $2]" , " object" , " tuple" ,
398403 " set[$1]" , " range[$1]" , " ptr " , " ref " , " var " , " seq[$1]" , " proc" ,
399- " pointer" , " OpenArray[$1]" , " string" , " CString " , " Forward" ,
404+ " pointer" , " OpenArray[$1]" , " string" , " cstring " , " Forward" ,
400405 " int" , " int8" , " int16" , " int32" , " int64" ,
401406 " float" , " float32" , " float64" , " float128" ,
402407 " uint" , " uint8" , " uint16" , " uint32" , " uint64" ,
@@ -407,7 +412,7 @@ const
407412 " and" , " or" , " not" , " any" , " static" , " TypeFromExpr" , " FieldAccessor" ,
408413 " void" ]
409414
410- const preferToResolveSymbols = {preferName, preferTypeName, preferModuleInfo, preferGenericArg}
415+ const preferToResolveSymbols = {preferName, preferTypeName, preferModuleInfo, preferGenericArg, preferResolved }
411416
412417template bindConcreteTypeToUserTypeClass * (tc, concrete: PType ) =
413418 tc.sons.add concrete
@@ -426,8 +431,13 @@ proc addTypeFlags(name: var string, typ: PType) {.inline.} =
426431 if tfNotNil in typ.flags: name.add (" not nil" )
427432
428433proc typeToString (typ: PType , prefer: TPreferedDesc = preferName): string =
434+ proc updatePrefer (prefer2: TPreferedDesc ): TPreferedDesc =
435+ if prefer == preferResolved: prefer
436+ elif prefer == preferModuleInfo: prefer
437+ else : prefer2
438+
429439 proc typeToString (typ: PType ): string =
430- var t = typ
440+ let t = typ
431441 result = " "
432442 if t == nil : return
433443 if prefer in preferToResolveSymbols and t.sym != nil and
@@ -436,6 +446,17 @@ proc typeToString(typ: PType, prefer: TPreferedDesc = preferName): string =
436446 result = t.sym.name.s & " literal(" & $ t.n.intVal & " )"
437447 elif t.kind == tyAlias and t.sons[0 ].kind != tyAlias:
438448 result = typeToString (t.sons[0 ])
449+ elif prefer == preferResolved:
450+ case t.kind
451+ of IntegralTypes + {tyFloat.. tyFloat128} + {tyString, tyCString}:
452+ result = typeToStr[t.kind]
453+ of tyGenericBody:
454+ result = typeToString (t.lastSon)
455+ of tyCompositeTypeClass:
456+ # avoids showing `A[any]` in `proc fun(a: A)` with `A = object[T]`
457+ result = typeToString (t.lastSon.lastSon)
458+ else :
459+ result = t.sym.name.s
439460 elif prefer in {preferName, preferTypeName} or t.sym.owner.isNil:
440461 result = t.sym.name.s
441462 if t.kind == tyGenericParam and t.sonsLen > 0 :
@@ -462,13 +483,13 @@ proc typeToString(typ: PType, prefer: TPreferedDesc = preferName): string =
462483 result = typeToString (t.sons[0 ]) & '['
463484 for i in 1 ..< sonsLen (t)- ord (t.kind != tyGenericInvocation):
464485 if i > 1 : add (result , " , " )
465- add (result , typeToString (t.sons[i], preferGenericArg))
486+ add (result , typeToString (t.sons[i], preferGenericArg.updatePrefer ))
466487 add (result , ']' )
467488 of tyGenericBody:
468489 result = typeToString (t.lastSon) & '['
469490 for i in 0 .. sonsLen (t)- 2 :
470491 if i > 0 : add (result , " , " )
471- add (result , typeToString (t.sons[i], preferTypeName))
492+ add (result , typeToString (t.sons[i], preferTypeName.updatePrefer ))
472493 add (result , ']' )
473494 of tyTypeDesc:
474495 if t.sons[0 ].kind == tyNone: result = " typedesc"
@@ -551,8 +572,7 @@ proc typeToString(typ: PType, prefer: TPreferedDesc = preferName): string =
551572 of tyOpenArray:
552573 result = " openarray[" & typeToString (t.sons[0 ]) & ']'
553574 of tyDistinct:
554- result = " distinct " & typeToString (t.sons[0 ],
555- if prefer == preferModuleInfo: preferModuleInfo else : preferTypeName)
575+ result = " distinct " & typeToString (t.sons[0 ], preferTypeName.updatePrefer)
556576 of tyTuple:
557577 # we iterate over t.sons here, because t.n may be nil
558578 if t.n != nil :
0 commit comments