* README.Portability: Remove note on an Irix compatibility issue.
[official-gcc.git] / libgo / go / runtime / error.go
blob9cf2230ab3f935dbd475029f33852fbd37247de4
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 import "unsafe"
9 // The Error interface identifies a run time error.
10 type Error interface {
11 error
13 // RuntimeError is a no-op function but
14 // serves to distinguish types that are run time
15 // errors from ordinary errors: a type is a
16 // run time error if it has a RuntimeError method.
17 RuntimeError()
20 // A TypeAssertionError explains a failed type assertion.
21 type TypeAssertionError struct {
22 interfaceString string
23 concreteString string
24 assertedString string
25 missingMethod string // one method needed by Interface, missing from Concrete
28 func (*TypeAssertionError) RuntimeError() {}
30 func (e *TypeAssertionError) Error() string {
31 inter := e.interfaceString
32 if inter == "" {
33 inter = "interface"
35 if e.concreteString == "" {
36 return "interface conversion: " + inter + " is nil, not " + e.assertedString
38 if e.missingMethod == "" {
39 return "interface conversion: " + inter + " is " + e.concreteString +
40 ", not " + e.assertedString
42 return "interface conversion: " + e.concreteString + " is not " + e.assertedString +
43 ": missing method " + e.missingMethod
46 // For calling from C.
47 func NewTypeAssertionError(ps1, ps2, ps3 *string, pmeth *string, ret *interface{}) {
48 var s1, s2, s3, meth string
50 if ps1 != nil {
51 s1 = *ps1
53 if ps2 != nil {
54 s2 = *ps2
56 if ps3 != nil {
57 s3 = *ps3
59 if pmeth != nil {
60 meth = *pmeth
63 // For gccgo, strip out quoted strings.
64 s1 = unquote(s1)
65 s2 = unquote(s2)
66 s3 = unquote(s3)
68 *ret = &TypeAssertionError{s1, s2, s3, meth}
71 // Remove quoted strings from gccgo reflection strings.
72 func unquote(s string) string {
73 ls := len(s)
74 var i int
75 for i = 0; i < ls; i++ {
76 if s[i] == '\t' {
77 break
80 if i == ls {
81 return s
83 var q bool
84 r := make([]byte, len(s))
85 j := 0
86 for i = 0; i < ls; i++ {
87 if s[i] == '\t' {
88 q = !q
89 } else if !q {
90 r[j] = s[i]
91 j++
94 return string(r[:j])
97 // An errorString represents a runtime error described by a single string.
98 type errorString string
100 func (e errorString) RuntimeError() {}
102 func (e errorString) Error() string {
103 return "runtime error: " + string(e)
106 // An errorCString represents a runtime error described by a single C string.
107 // Not "type errorCString uintptr" because of http://golang.org/issue/7084.
108 type errorCString struct{ cstr uintptr }
110 func (e errorCString) RuntimeError() {}
112 func (e errorCString) Error() string {
113 return "runtime error: " + gostringnocopy((*byte)(unsafe.Pointer(e.cstr)))
116 // For calling from C.
117 func NewErrorCString(s uintptr, ret *interface{}) {
118 *ret = errorCString{s}
121 // plainError represents a runtime error described a string without
122 // the prefix "runtime error: " after invoking errorString.Error().
123 // See Issue #14965.
124 type plainError string
126 func (e plainError) RuntimeError() {}
128 func (e plainError) Error() string {
129 return string(e)
132 type stringer interface {
133 String() string
136 func typestring(x interface{}) string {
137 e := efaceOf(&x)
138 return *e._type.string
141 // For calling from C.
142 // Prints an argument passed to panic.
143 // There's room for arbitrary complexity here, but we keep it
144 // simple and handle just a few important cases: int, string, and Stringer.
145 func printany(i interface{}) {
146 switch v := i.(type) {
147 case nil:
148 print("nil")
149 case stringer:
150 print(v.String())
151 case error:
152 print(v.Error())
153 case int:
154 print(v)
155 case string:
156 print(v)
157 default:
158 print("(", typestring(i), ") ", i)
162 // called from generated code
163 func panicwrap(pkg, typ, meth string) {
164 panic(plainError("value method " + pkg + "." + typ + "." + meth + " called using nil *" + typ + " pointer"))