astmsgs.nim 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # this module avoids ast depending on msgs or vice versa
  2. import std/strutils
  3. import options, ast, msgs
  4. proc typSym*(t: PType): PSym =
  5. result = t.sym
  6. if result == nil and t.kind == tyGenericInst: # this might need to be refined
  7. result = t.genericHead.sym
  8. proc addDeclaredLoc*(result: var string, conf: ConfigRef; sym: PSym) =
  9. result.add " [$1 declared in $2]" % [sym.kind.toHumanStr, toFileLineCol(conf, sym.info)]
  10. proc addDeclaredLocMaybe*(result: var string, conf: ConfigRef; sym: PSym) =
  11. if optDeclaredLocs in conf.globalOptions and sym != nil:
  12. addDeclaredLoc(result, conf, sym)
  13. proc addDeclaredLoc*(result: var string, conf: ConfigRef; typ: PType) =
  14. # xxx figure out how to resolve `tyGenericParam`, e.g. for
  15. # proc fn[T](a: T, b: T) = discard
  16. # fn(1.1, "a")
  17. let typ = typ.skipTypes(abstractInst + {tyStatic, tySequence, tyArray, tySet, tyUserTypeClassInst, tyVar, tyRef, tyPtr} - {tyRange})
  18. result.add " [$1" % typ.kind.toHumanStr
  19. if typ.sym != nil:
  20. result.add " declared in " & toFileLineCol(conf, typ.sym.info)
  21. result.add "]"
  22. proc addTypeNodeDeclaredLoc*(result: var string, conf: ConfigRef; typ: PType) =
  23. result.add " [$1" % typ.kind.toHumanStr
  24. if typ.sym != nil:
  25. result.add " declared in " & toFileLineCol(conf, typ.sym.info)
  26. result.add "]"
  27. proc addDeclaredLocMaybe*(result: var string, conf: ConfigRef; typ: PType) =
  28. if optDeclaredLocs in conf.globalOptions: addDeclaredLoc(result, conf, typ)
  29. template quoteExpr*(a: string): untyped =
  30. ## can be used for quoting expressions in error msgs.
  31. "'" & a & "'"
  32. proc genFieldDefect*(conf: ConfigRef, field: string, disc: PSym): string =
  33. let obj = disc.owner.name.s # `types.typeToString` might be better, eg for generics
  34. result = "field '$#' is not accessible for type '$#'" % [field, obj]
  35. if optDeclaredLocs in conf.globalOptions:
  36. result.add " [discriminant declared in $#]" % toFileLineCol(conf, disc.info)
  37. result.add " using '$# = " % disc.name.s