astmsgs.nim 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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[0].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 addDeclaredLocMaybe*(result: var string, conf: ConfigRef; typ: PType) =
  23. if optDeclaredLocs in conf.globalOptions: addDeclaredLoc(result, conf, typ)
  24. template quoteExpr*(a: string): untyped =
  25. ## can be used for quoting expressions in error msgs.
  26. "'" & a & "'"
  27. proc genFieldDefect*(conf: ConfigRef, field: string, disc: PSym): string =
  28. let obj = disc.owner.name.s # `types.typeToString` might be better, eg for generics
  29. result = "field '$#' is not accessible for type '$#'" % [field, obj]
  30. if optDeclaredLocs in conf.globalOptions:
  31. result.add " [discriminant declared in $#]" % toFileLineCol(conf, disc.info)
  32. result.add " using '$# = " % disc.name.s