Daily bump.
[official-gcc.git] / libgo / go / runtime / error.go
blob88d5df5e415f5adf93d12cbdabaaabc2429e52e6
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.
5 package runtime
7 // The Error interface identifies a run time error.
8 type Error interface {
9 error
11 // RuntimeError is a no-op function but
12 // serves to distinguish types that are runtime
13 // errors from ordinary errors: a type is a
14 // runtime error if it has a RuntimeError method.
15 RuntimeError()
18 // A TypeAssertionError explains a failed type assertion.
19 type TypeAssertionError struct {
20 interfaceString string
21 concreteString string
22 assertedString string
23 missingMethod string // one method needed by Interface, missing from Concrete
26 func (*TypeAssertionError) RuntimeError() {}
28 func (e *TypeAssertionError) Error() string {
29 inter := e.interfaceString
30 if inter == "" {
31 inter = "interface"
33 if e.concreteString == "" {
34 return "interface conversion: " + inter + " is nil, not " + e.assertedString
36 if e.missingMethod == "" {
37 return "interface conversion: " + inter + " is " + e.concreteString +
38 ", not " + e.assertedString
40 return "interface conversion: " + e.concreteString + " is not " + e.assertedString +
41 ": missing method " + e.missingMethod
44 // For calling from C.
45 func NewTypeAssertionError(ps1, ps2, ps3 *string, pmeth *string, ret *interface{}) {
46 var s1, s2, s3, meth string
48 if ps1 != nil {
49 s1 = *ps1
51 if ps2 != nil {
52 s2 = *ps2
54 if ps3 != nil {
55 s3 = *ps3
57 if pmeth != nil {
58 meth = *pmeth
61 // For gccgo, strip out quoted strings.
62 s1 = unquote(s1)
63 s2 = unquote(s2)
64 s3 = unquote(s3)
66 *ret = &TypeAssertionError{s1, s2, s3, meth}
69 // Remove quoted strings from gccgo reflection strings.
70 func unquote(s string) string {
71 ls := len(s)
72 var i int
73 for i = 0; i < ls; i++ {
74 if s[i] == '\t' {
75 break
78 if i == ls {
79 return s
81 var q bool
82 r := make([]byte, len(s))
83 j := 0
84 for i = 0; i < ls; i++ {
85 if s[i] == '\t' {
86 q = !q
87 } else if !q {
88 r[j] = s[i]
89 j++
92 return string(r[:j])
95 // An errorString represents a runtime error described by a single string.
96 type errorString string
98 func (e errorString) RuntimeError() {}
100 func (e errorString) Error() string {
101 return "runtime error: " + string(e)
104 // For calling from C.
105 func NewErrorString(s string, ret *interface{}) {
106 *ret = errorString(s)
109 // An errorCString represents a runtime error described by a single C string.
110 type errorCString uintptr
112 func (e errorCString) RuntimeError() {}
114 func cstringToGo(uintptr) string
116 func (e errorCString) Error() string {
117 return "runtime error: " + cstringToGo(uintptr(e))
120 // For calling from C.
121 func NewErrorCString(s uintptr, ret *interface{}) {
122 *ret = errorCString(s)
125 type stringer interface {
126 String() string
129 func typestring(interface{}) string
131 // For calling from C.
132 // Prints an argument passed to panic.
133 // There's room for arbitrary complexity here, but we keep it
134 // simple and handle just a few important cases: int, string, and Stringer.
135 func Printany(i interface{}) {
136 switch v := i.(type) {
137 case nil:
138 print("nil")
139 case stringer:
140 print(v.String())
141 case error:
142 print(v.Error())
143 case int:
144 print(v)
145 case string:
146 print(v)
147 default:
148 print("(", typestring(i), ") ", i)
152 // called from generated code
153 func panicwrap(pkg, typ, meth string) {
154 panic("value method " + pkg + "." + typ + "." + meth + " called using nil *" + typ + " pointer")