runtime: scan register backing store on ia64
[official-gcc.git] / libgo / go / runtime / error.go
blob34b7d37c3f88c9b6895566d09610d5126f25a388
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 // printany prints an argument passed to panic.
142 func printany(i interface{}) {
143 switch v := i.(type) {
144 case nil:
145 print("nil")
146 case stringer:
147 print(v.String())
148 case error:
149 print(v.Error())
150 case bool:
151 print(v)
152 case int:
153 print(v)
154 case int8:
155 print(v)
156 case int16:
157 print(v)
158 case int32:
159 print(v)
160 case int64:
161 print(v)
162 case uint:
163 print(v)
164 case uint8:
165 print(v)
166 case uint16:
167 print(v)
168 case uint32:
169 print(v)
170 case uint64:
171 print(v)
172 case uintptr:
173 print(v)
174 case float32:
175 print(v)
176 case float64:
177 print(v)
178 case complex64:
179 print(v)
180 case complex128:
181 print(v)
182 case string:
183 print(v)
184 default:
185 print("(", typestring(i), ") ", i)