Matthew Dempsky | 51e3d70 | 2020-06-10 13:05:41 -0700 | [diff] [blame] | 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 | |
Rob Findley | 6d2eea5 | 2020-10-20 10:17:44 -0400 | [diff] [blame] | 5 | // Package typesinternal provides access to internal go/types APIs that are not |
| 6 | // yet exported. |
Matthew Dempsky | 51e3d70 | 2020-06-10 13:05:41 -0700 | [diff] [blame] | 7 | package typesinternal |
| 8 | |
| 9 | import ( |
Rob Findley | 6d2eea5 | 2020-10-20 10:17:44 -0400 | [diff] [blame] | 10 | "go/token" |
Matthew Dempsky | 51e3d70 | 2020-06-10 13:05:41 -0700 | [diff] [blame] | 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 | } |
Rob Findley | 6d2eea5 | 2020-10-20 10:17:44 -0400 | [diff] [blame] | 32 | |
Robert Findley | fe076c8 | 2021-09-20 15:14:52 -0400 | [diff] [blame] | 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) { |
Rob Findley | 6d2eea5 | 2020-10-20 10:17:44 -0400 | [diff] [blame] | 39 | var data [3]int |
| 40 | // By coincidence all of these fields are ints, which simplifies things. |
Robert Findley | fe076c8 | 2021-09-20 15:14:52 -0400 | [diff] [blame] | 41 | v := reflect.ValueOf(err) |
Rob Findley | 6d2eea5 | 2020-10-20 10:17:44 -0400 | [diff] [blame] | 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 | } |
Heschi Kreinick | 164402d | 2022-02-04 16:50:42 -0500 | [diff] [blame] | 51 | |
| 52 | var SetGoVersion = func(conf *types.Config, version string) bool { return false } |