github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/third_party/gofrontend/libgo/go/runtime/error.go (about)

     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  	error
    10  
    11  	// RuntimeError is a no-op function but
    12  	// serves to distinguish types that are run time
    13  	// errors from ordinary errors: a type is a
    14  	// run time error if it has a RuntimeError method.
    15  	RuntimeError()
    16  }
    17  
    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
    24  }
    25  
    26  func (*TypeAssertionError) RuntimeError() {}
    27  
    28  func (e *TypeAssertionError) Error() string {
    29  	inter := e.interfaceString
    30  	if inter == "" {
    31  		inter = "interface"
    32  	}
    33  	if e.concreteString == "" {
    34  		return "interface conversion: " + inter + " is nil, not " + e.assertedString
    35  	}
    36  	if e.missingMethod == "" {
    37  		return "interface conversion: " + inter + " is " + e.concreteString +
    38  			", not " + e.assertedString
    39  	}
    40  	return "interface conversion: " + e.concreteString + " is not " + e.assertedString +
    41  		": missing method " + e.missingMethod
    42  }
    43  
    44  // For calling from C.
    45  func NewTypeAssertionError(ps1, ps2, ps3 *string, pmeth *string, ret *interface{}) {
    46  	var s1, s2, s3, meth string
    47  
    48  	if ps1 != nil {
    49  		s1 = *ps1
    50  	}
    51  	if ps2 != nil {
    52  		s2 = *ps2
    53  	}
    54  	if ps3 != nil {
    55  		s3 = *ps3
    56  	}
    57  	if pmeth != nil {
    58  		meth = *pmeth
    59  	}
    60  
    61  	// For gccgo, strip out quoted strings.
    62  	s1 = unquote(s1)
    63  	s2 = unquote(s2)
    64  	s3 = unquote(s3)
    65  
    66  	*ret = &TypeAssertionError{s1, s2, s3, meth}
    67  }
    68  
    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
    76  		}
    77  	}
    78  	if i == ls {
    79  		return s
    80  	}
    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++
    90  		}
    91  	}
    92  	return string(r[:j])
    93  }
    94  
    95  // An errorString represents a runtime error described by a single string.
    96  type errorString string
    97  
    98  func (e errorString) RuntimeError() {}
    99  
   100  func (e errorString) Error() string {
   101  	return "runtime error: " + string(e)
   102  }
   103  
   104  // For calling from C.
   105  func NewErrorString(s string, ret *interface{}) {
   106  	*ret = errorString(s)
   107  }
   108  
   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 }
   112  
   113  func (e errorCString) RuntimeError() {}
   114  
   115  func cstringToGo(uintptr) string
   116  
   117  func (e errorCString) Error() string {
   118  	return "runtime error: " + cstringToGo(e.cstr)
   119  }
   120  
   121  // For calling from C.
   122  func NewErrorCString(s uintptr, ret *interface{}) {
   123  	*ret = errorCString{s}
   124  }
   125  
   126  type stringer interface {
   127  	String() string
   128  }
   129  
   130  func typestring(interface{}) string
   131  
   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)
   150  	}
   151  }
   152  
   153  // called from generated code
   154  func panicwrap(pkg, typ, meth string) {
   155  	panic("value method " + pkg + "." + typ + "." + meth + " called using nil *" + typ + " pointer")
   156  }