]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgo/go/runtime/error.go
Add Go frontend, libgo library, and Go testsuite.
[thirdparty/gcc.git] / libgo / go / runtime / error.go
1 // Copyright 2010 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 package runtime
6
7 // The Error interface identifies a run time error.
8 type Error interface {
9 String() string
10
11 // RuntimeError is a no-op function but
12 // serves to distinguish types that are runtime
13 // errors from ordinary os.Errors: a type is a
14 // runtime error if it has a RuntimeError method.
15 RuntimeError()
16 }
17
18 // A TypeAssertionError explains a failed type assertion.
19 type TypeAssertionError struct {
20 interfaceType *Type // interface had this type
21 concreteType *Type // concrete value had this type
22 assertedType *Type // asserted type
23 interfaceString string
24 concreteString string
25 assertedString string
26 missingMethod string // one method needed by Interface, missing from Concrete
27 }
28
29 func (*TypeAssertionError) RuntimeError() {}
30
31 func (e *TypeAssertionError) String() string {
32 inter := e.interfaceString
33 if inter == "" {
34 inter = "interface"
35 }
36 if e.concreteType == nil {
37 return "interface conversion: " + inter + " is nil, not " + e.assertedString
38 }
39 if e.missingMethod == "" {
40 return "interface conversion: " + inter + " is " + e.concreteString +
41 ", not " + e.assertedString
42 }
43 return "interface conversion: " + e.concreteString + " is not " + e.assertedString +
44 ": missing method " + e.missingMethod
45 }
46
47 // Concrete returns the type of the concrete value in the failed type assertion.
48 // If the interface value was nil, Concrete returns nil.
49 func (e *TypeAssertionError) Concrete() *Type {
50 return e.concreteType
51 }
52
53 // Asserted returns the type incorrectly asserted by the type assertion.
54 func (e *TypeAssertionError) Asserted() *Type {
55 return e.assertedType
56 }
57
58 // If the type assertion is to an interface type, MissingMethod returns the
59 // name of a method needed to satisfy that interface type but not implemented
60 // by Concrete. If there are multiple such methods,
61 // MissingMethod returns one; which one is unspecified.
62 // If the type assertion is not to an interface type, MissingMethod returns an empty string.
63 func (e *TypeAssertionError) MissingMethod() string {
64 return e.missingMethod
65 }
66
67 // For calling from C.
68 func NewTypeAssertionError(pt1, pt2, pt3 *Type, ps1, ps2, ps3 *string, pmeth *string, ret *interface{}) {
69 var t1, t2, t3 *Type
70 var s1, s2, s3, meth string
71
72 if pt1 != nil {
73 t1 = pt1
74 }
75 if pt2 != nil {
76 t2 = pt2
77 }
78 if pt3 != nil {
79 t3 = pt3
80 }
81 if ps1 != nil {
82 s1 = *ps1
83 }
84 if ps2 != nil {
85 s2 = *ps2
86 }
87 if ps3 != nil {
88 s3 = *ps3
89 }
90 if pmeth != nil {
91 meth = *pmeth
92 }
93 *ret = &TypeAssertionError{t1, t2, t3, s1, s2, s3, meth}
94 }
95
96 // An errorString represents a runtime error described by a single string.
97 type errorString string
98
99 func (e errorString) RuntimeError() {}
100
101 func (e errorString) String() string {
102 return "runtime error: " + string(e)
103 }
104
105 // For calling from C.
106 func NewErrorString(s string, ret *interface{}) {
107 *ret = errorString(s)
108 }
109
110 type stringer interface {
111 String() string
112 }
113
114 func typestring(interface{}) string
115
116 // For calling from C.
117 // Prints an argument passed to panic.
118 // There's room for arbitrary complexity here, but we keep it
119 // simple and handle just a few important cases: int, string, and Stringer.
120 func Printany(i interface{}) {
121 switch v := i.(type) {
122 case nil:
123 print("nil")
124 case stringer:
125 print(v.String())
126 case int:
127 print(v)
128 case string:
129 print(v)
130 default:
131 print("(", typestring(i), ") ", i)
132 }
133 }