PageRenderTime 64ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/go/gotour/src/code.google.com/p/go.tools/go/types/errors.go

https://github.com/zatkin/programming
Go | 96 lines | 76 code | 15 blank | 5 comment | 7 complexity | 7d47272f7c4f5bda2c349e4904193484 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, Apache-2.0, MIT
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // This file implements various error reporters.
  5. package types
  6. import (
  7. "fmt"
  8. "go/ast"
  9. "go/token"
  10. "strings"
  11. )
  12. func assert(p bool) {
  13. if !p {
  14. panic("assertion failed")
  15. }
  16. }
  17. func unreachable() {
  18. panic("unreachable")
  19. }
  20. func (check *checker) sprintf(format string, args ...interface{}) string {
  21. for i, arg := range args {
  22. switch a := arg.(type) {
  23. case nil:
  24. arg = "<nil>"
  25. case operand:
  26. panic("internal error: should always pass *operand")
  27. case *operand:
  28. arg = operandString(check.pkg, a)
  29. case token.Pos:
  30. arg = check.fset.Position(a).String()
  31. case ast.Expr:
  32. arg = ExprString(a)
  33. case Object:
  34. arg = ObjectString(check.pkg, a)
  35. case Type:
  36. arg = TypeString(check.pkg, a)
  37. }
  38. args[i] = arg
  39. }
  40. return fmt.Sprintf(format, args...)
  41. }
  42. func (check *checker) trace(pos token.Pos, format string, args ...interface{}) {
  43. fmt.Printf("%s:\t%s%s\n",
  44. check.fset.Position(pos),
  45. strings.Repeat(". ", check.indent),
  46. check.sprintf(format, args...),
  47. )
  48. }
  49. // dump is only needed for debugging
  50. func (check *checker) dump(format string, args ...interface{}) {
  51. fmt.Println(check.sprintf(format, args...))
  52. }
  53. func (check *checker) err(pos token.Pos, msg string, soft bool) {
  54. err := Error{check.fset, pos, msg, soft}
  55. if check.firstErr == nil {
  56. check.firstErr = err
  57. }
  58. f := check.conf.Error
  59. if f == nil {
  60. panic(bailout{}) // report only first error
  61. }
  62. f(err)
  63. }
  64. func (check *checker) error(pos token.Pos, msg string) {
  65. check.err(pos, msg, false)
  66. }
  67. func (check *checker) errorf(pos token.Pos, format string, args ...interface{}) {
  68. check.err(pos, check.sprintf(format, args...), false)
  69. }
  70. func (check *checker) softErrorf(pos token.Pos, format string, args ...interface{}) {
  71. check.err(pos, check.sprintf(format, args...), true)
  72. }
  73. func (check *checker) invalidAST(pos token.Pos, format string, args ...interface{}) {
  74. check.errorf(pos, "invalid AST: "+format, args...)
  75. }
  76. func (check *checker) invalidArg(pos token.Pos, format string, args ...interface{}) {
  77. check.errorf(pos, "invalid argument: "+format, args...)
  78. }
  79. func (check *checker) invalidOp(pos token.Pos, format string, args ...interface{}) {
  80. check.errorf(pos, "invalid operation: "+format, args...)
  81. }