golang.org/x/tools@v0.21.0/internal/typesinternal/types.go (about)

     1  // Copyright 2020 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 typesinternal provides access to internal go/types APIs that are not
     6  // yet exported.
     7  package typesinternal
     8  
     9  import (
    10  	"go/token"
    11  	"go/types"
    12  	"reflect"
    13  	"unsafe"
    14  )
    15  
    16  func SetUsesCgo(conf *types.Config) bool {
    17  	v := reflect.ValueOf(conf).Elem()
    18  
    19  	f := v.FieldByName("go115UsesCgo")
    20  	if !f.IsValid() {
    21  		f = v.FieldByName("UsesCgo")
    22  		if !f.IsValid() {
    23  			return false
    24  		}
    25  	}
    26  
    27  	addr := unsafe.Pointer(f.UnsafeAddr())
    28  	*(*bool)(addr) = true
    29  
    30  	return true
    31  }
    32  
    33  // ReadGo116ErrorData extracts additional information from types.Error values
    34  // generated by Go version 1.16 and later: the error code, start position, and
    35  // end position. If all positions are valid, start <= err.Pos <= end.
    36  //
    37  // If the data could not be read, the final result parameter will be false.
    38  func ReadGo116ErrorData(err types.Error) (code ErrorCode, start, end token.Pos, ok bool) {
    39  	var data [3]int
    40  	// By coincidence all of these fields are ints, which simplifies things.
    41  	v := reflect.ValueOf(err)
    42  	for i, name := range []string{"go116code", "go116start", "go116end"} {
    43  		f := v.FieldByName(name)
    44  		if !f.IsValid() {
    45  			return 0, 0, 0, false
    46  		}
    47  		data[i] = int(f.Int())
    48  	}
    49  	return ErrorCode(data[0]), token.Pos(data[1]), token.Pos(data[2]), true
    50  }