]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/types2/errors.go
cmd/compile/internal/types2: review of errors.go
[gostls13.git] / src / cmd / compile / internal / types2 / 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 types2
8
9 import (
10         "bytes"
11         "cmd/compile/internal/syntax"
12         "fmt"
13         "strconv"
14         "strings"
15 )
16
17 func unimplemented() {
18         panic("unimplemented")
19 }
20
21 func assert(p bool) {
22         if !p {
23                 panic("assertion failed")
24         }
25 }
26
27 func unreachable() {
28         panic("unreachable")
29 }
30
31 // An error_ represents a type-checking error.
32 // To report an error_, call Checker.report.
33 type error_ struct {
34         desc []errorDesc
35         soft bool // TODO(gri) eventually determine this from an error code
36 }
37
38 // An errorDesc describes part of a type-checking error.
39 type errorDesc struct {
40         pos    syntax.Pos
41         format string
42         args   []interface{}
43 }
44
45 func (err *error_) empty() bool {
46         return err.desc == nil
47 }
48
49 func (err *error_) pos() syntax.Pos {
50         if err.empty() {
51                 return nopos
52         }
53         return err.desc[0].pos
54 }
55
56 func (err *error_) msg(qf Qualifier) string {
57         if err.empty() {
58                 return "no error"
59         }
60         var buf bytes.Buffer
61         for i := range err.desc {
62                 p := &err.desc[i]
63                 if i > 0 {
64                         fmt.Fprintf(&buf, "\n\t%s: ", p.pos)
65                 }
66                 buf.WriteString(sprintf(qf, p.format, p.args...))
67         }
68         return buf.String()
69 }
70
71 // String is for testing.
72 func (err *error_) String() string {
73         if err.empty() {
74                 return "no error"
75         }
76         return fmt.Sprintf("%s: %s", err.pos(), err.msg(nil))
77 }
78
79 // errorf adds formatted error information to err.
80 // It may be called multiple times to provide additional information.
81 func (err *error_) errorf(at poser, format string, args ...interface{}) {
82         err.desc = append(err.desc, errorDesc{posFor(at), format, args})
83 }
84
85 func sprintf(qf Qualifier, format string, args ...interface{}) string {
86         for i, arg := range args {
87                 switch a := arg.(type) {
88                 case nil:
89                         arg = "<nil>"
90                 case operand:
91                         panic("internal error: should always pass *operand")
92                 case *operand:
93                         arg = operandString(a, qf)
94                 case syntax.Pos:
95                         arg = a.String()
96                 case syntax.Expr:
97                         arg = syntax.String(a)
98                 case Object:
99                         arg = ObjectString(a, qf)
100                 case Type:
101                         arg = TypeString(a, qf)
102                 }
103                 args[i] = arg
104         }
105         return fmt.Sprintf(format, args...)
106 }
107
108 func (check *Checker) qualifier(pkg *Package) string {
109         // Qualify the package unless it's the package being type-checked.
110         if pkg != check.pkg {
111                 // If the same package name was used by multiple packages, display the full path.
112                 if check.pkgCnt[pkg.name] > 1 {
113                         return strconv.Quote(pkg.path)
114                 }
115                 return pkg.name
116         }
117         return ""
118 }
119
120 func (check *Checker) sprintf(format string, args ...interface{}) string {
121         return sprintf(check.qualifier, format, args...)
122 }
123
124 func (check *Checker) report(err *error_) {
125         if err.empty() {
126                 panic("internal error: reporting no error")
127         }
128         check.err(err.pos(), err.msg(check.qualifier), err.soft)
129 }
130
131 func (check *Checker) trace(pos syntax.Pos, format string, args ...interface{}) {
132         fmt.Printf("%s:\t%s%s\n",
133                 pos,
134                 strings.Repeat(".  ", check.indent),
135                 check.sprintf(format, args...),
136         )
137 }
138
139 // dump is only needed for debugging
140 func (check *Checker) dump(format string, args ...interface{}) {
141         fmt.Println(check.sprintf(format, args...))
142 }
143
144 func (check *Checker) err(at poser, msg string, soft bool) {
145         // Cheap trick: Don't report errors with messages containing
146         // "invalid operand" or "invalid type" as those tend to be
147         // follow-on errors which don't add useful information. Only
148         // exclude them if these strings are not at the beginning,
149         // and only if we have at least one error already reported.
150         if check.firstErr != nil && (strings.Index(msg, "invalid operand") > 0 || strings.Index(msg, "invalid type") > 0) {
151                 return
152         }
153
154         pos := posFor(at)
155
156         // If we are encountering an error while evaluating an inherited
157         // constant initialization expression, pos is the position of in
158         // the original expression, and not of the currently declared
159         // constant identifier. Use the provided errpos instead.
160         // TODO(gri) We may also want to augment the error message and
161         // refer to the position (pos) in the original expression.
162         if check.errpos.IsKnown() {
163                 assert(check.iota != nil)
164                 pos = check.errpos
165         }
166
167         err := Error{pos, stripAnnotations(msg), msg, soft}
168         if check.firstErr == nil {
169                 check.firstErr = err
170         }
171
172         if check.conf.Trace {
173                 check.trace(pos, "ERROR: %s", msg)
174         }
175
176         f := check.conf.Error
177         if f == nil {
178                 panic(bailout{}) // report only first error
179         }
180         f(err)
181 }
182
183 const (
184         invalidAST = "invalid AST: "
185         invalidArg = "invalid argument: "
186         invalidOp  = "invalid operation: "
187 )
188
189 type poser interface {
190         Pos() syntax.Pos
191 }
192
193 func (check *Checker) error(at poser, msg string) {
194         check.err(at, msg, false)
195 }
196
197 func (check *Checker) errorf(at poser, format string, args ...interface{}) {
198         check.err(at, check.sprintf(format, args...), false)
199 }
200
201 func (check *Checker) softErrorf(at poser, format string, args ...interface{}) {
202         check.err(at, check.sprintf(format, args...), true)
203 }
204
205 // posFor reports the left (= start) position of at.
206 func posFor(at poser) syntax.Pos {
207         switch x := at.(type) {
208         case *operand:
209                 if x.expr != nil {
210                         return startPos(x.expr)
211                 }
212         case syntax.Node:
213                 return startPos(x)
214         }
215         return at.Pos()
216 }
217
218 // stripAnnotations removes internal (type) annotations from s.
219 func stripAnnotations(s string) string {
220         // Would like to use strings.Builder but it's not available in Go 1.4.
221         var b bytes.Buffer
222         for _, r := range s {
223                 // strip #'s and subscript digits
224                 if r != instanceMarker && !('₀' <= r && r < '₀'+10) { // '₀' == U+2080
225                         b.WriteRune(r)
226                 }
227         }
228         if b.Len() < len(s) {
229                 return b.String()
230         }
231         return s
232 }