]> Cypherpunks.ru repositories - gostls13.git/blob - src/go/types/errors.go
[dev.ssa] Merge remote-tracking branch 'origin/master' into dev.ssa
[gostls13.git] / src / go / types / errors.go
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
5 // This file implements various error reporters.
6
7 package types
8
9 import (
10         "fmt"
11         "go/ast"
12         "go/token"
13         "strings"
14 )
15
16 func assert(p bool) {
17         if !p {
18                 panic("assertion failed")
19         }
20 }
21
22 func unreachable() {
23         panic("unreachable")
24 }
25
26 func (check *Checker) sprintf(format string, args ...interface{}) string {
27         for i, arg := range args {
28                 switch a := arg.(type) {
29                 case nil:
30                         arg = "<nil>"
31                 case operand:
32                         panic("internal error: should always pass *operand")
33                 case *operand:
34                         arg = operandString(check.pkg, a)
35                 case token.Pos:
36                         arg = check.fset.Position(a).String()
37                 case ast.Expr:
38                         arg = ExprString(a)
39                 case Object:
40                         arg = ObjectString(check.pkg, a)
41                 case Type:
42                         arg = TypeString(check.pkg, a)
43                 }
44                 args[i] = arg
45         }
46         return fmt.Sprintf(format, args...)
47 }
48
49 func (check *Checker) trace(pos token.Pos, format string, args ...interface{}) {
50         fmt.Printf("%s:\t%s%s\n",
51                 check.fset.Position(pos),
52                 strings.Repeat(".  ", check.indent),
53                 check.sprintf(format, args...),
54         )
55 }
56
57 // dump is only needed for debugging
58 func (check *Checker) dump(format string, args ...interface{}) {
59         fmt.Println(check.sprintf(format, args...))
60 }
61
62 func (check *Checker) err(pos token.Pos, msg string, soft bool) {
63         err := Error{check.fset, pos, msg, soft}
64         if check.firstErr == nil {
65                 check.firstErr = err
66         }
67         f := check.conf.Error
68         if f == nil {
69                 panic(bailout{}) // report only first error
70         }
71         f(err)
72 }
73
74 func (check *Checker) error(pos token.Pos, msg string) {
75         check.err(pos, msg, false)
76 }
77
78 func (check *Checker) errorf(pos token.Pos, format string, args ...interface{}) {
79         check.err(pos, check.sprintf(format, args...), false)
80 }
81
82 func (check *Checker) softErrorf(pos token.Pos, format string, args ...interface{}) {
83         check.err(pos, check.sprintf(format, args...), true)
84 }
85
86 func (check *Checker) invalidAST(pos token.Pos, format string, args ...interface{}) {
87         check.errorf(pos, "invalid AST: "+format, args...)
88 }
89
90 func (check *Checker) invalidArg(pos token.Pos, format string, args ...interface{}) {
91         check.errorf(pos, "invalid argument: "+format, args...)
92 }
93
94 func (check *Checker) invalidOp(pos token.Pos, format string, args ...interface{}) {
95         check.errorf(pos, "invalid operation: "+format, args...)
96 }