2015-05-29 François Dumont fdumont@gcc.gnu.org>
[official-gcc.git] / libgo / go / runtime / error.go
blobd759a54a51958f2969cb6f020dcf36bee4036913
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 // Not "type errorCString uintptr" because of http://golang.org/issue/7084.
111 type errorCString struct{ cstr uintptr }
113 func (e errorCString) RuntimeError() {}
115 func cstringToGo(uintptr) string
117 func (e errorCString) Error() string {
118 return "runtime error: " + cstringToGo(e.cstr)
121 // For calling from C.
122 func NewErrorCString(s uintptr, ret *interface{}) {
123 *ret = errorCString{s}
126 type stringer interface {
127 String() string
130 func typestring(interface{}) string
132 // For calling from C.
133 // Prints an argument passed to panic.
134 // There's room for arbitrary complexity here, but we keep it
135 // simple and handle just a few important cases: int, string, and Stringer.
136 func Printany(i interface{}) {
137 switch v := i.(type) {
138 case nil:
139 print("nil")
140 case stringer:
141 print(v.String())
142 case error:
143 print(v.Error())
144 case int:
145 print(v)
146 case string:
147 print(v)
148 default:
149 print("(", typestring(i), ") ", i)
153 // called from generated code
154 func panicwrap(pkg, typ, meth string) {
155 panic("value method " + pkg + "." + typ + "." + meth + " called using nil *" + typ + " pointer")