blob: ce7d4351b2203af122e6fc05506203736807736a [file] [log] [blame]
Matthew Dempsky51e3d702020-06-10 13:05:41 -07001// 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 Findley6d2eea52020-10-20 10:17:44 -04005// Package typesinternal provides access to internal go/types APIs that are not
6// yet exported.
Matthew Dempsky51e3d702020-06-10 13:05:41 -07007package typesinternal
8
9import (
Rob Findley6d2eea52020-10-20 10:17:44 -040010 "go/token"
Matthew Dempsky51e3d702020-06-10 13:05:41 -070011 "go/types"
12 "reflect"
13 "unsafe"
14)
15
16func 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 Findley6d2eea52020-10-20 10:17:44 -040032
Robert Findleyfe076c82021-09-20 15:14:52 -040033// 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.
38func ReadGo116ErrorData(err types.Error) (code ErrorCode, start, end token.Pos, ok bool) {
Rob Findley6d2eea52020-10-20 10:17:44 -040039 var data [3]int
40 // By coincidence all of these fields are ints, which simplifies things.
Robert Findleyfe076c82021-09-20 15:14:52 -040041 v := reflect.ValueOf(err)
Rob Findley6d2eea52020-10-20 10:17:44 -040042 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 Kreinick164402d2022-02-04 16:50:42 -050051
52var SetGoVersion = func(conf *types.Config, version string) bool { return false }