-
Notifications
You must be signed in to change notification settings - Fork 547
/
types.go
91 lines (79 loc) · 3.1 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Copyright (c) 2023 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package typesutil
import (
"go/token"
"go/types"
"unsafe"
)
// -----------------------------------------------------------------------------
// A Scope maintains a set of objects and links to its containing
// (parent) and contained (children) scopes. Objects may be inserted
// and looked up by name. The zero value for Scope is a ready-to-use
// empty scope.
type Scope struct {
parent *Scope
children []*Scope
number int // parent.children[number-1] is this scope; 0 if there is no parent
elems map[string]types.Object // lazily allocated
pos, end token.Pos // scope extent; may be invalid
comment string // for debugging only
isFunc bool // set if this is a function scope (internal use only)
}
// ScopeDelete deletes an object from specified scope by its name.
func ScopeDelete(s *types.Scope, name string) types.Object {
elems := (*Scope)(unsafe.Pointer(s)).elems
if o, ok := elems[name]; ok {
delete(elems, name)
return o
}
return nil
}
// -----------------------------------------------------------------------------
// An Error describes a type-checking error; it implements the error interface.
// A "soft" error is an error that still permits a valid interpretation of a
// package (such as "unused variable"); "hard" errors may lead to unpredictable
// behavior if ignored.
type Error struct {
Fset *token.FileSet // file set for interpretation of Pos
Pos token.Pos // error position
Msg string // error message
Soft bool // if set, error is "soft"
// go116code is a future API, unexported as the set of error codes is large
// and likely to change significantly during experimentation. Tools wishing
// to preview this feature may read go116code using reflection (see
// errorcodes_test.go), but beware that there is no guarantee of future
// compatibility.
go116code int
go116start token.Pos
go116end token.Pos
}
func SetErrorGo116(ret *types.Error, code int, start, end token.Pos) {
e := (*Error)(unsafe.Pointer(ret))
e.go116code = code
e.go116start = start
e.go116end = end
}
// -----------------------------------------------------------------------------
func init() {
if unsafe.Sizeof(Scope{}) != unsafe.Sizeof(types.Scope{}) {
panic("unexpected sizeof types.Scope")
}
if unsafe.Sizeof(Error{}) != unsafe.Sizeof(types.Error{}) {
panic("unexpected sizeof types.Error")
}
}
// -----------------------------------------------------------------------------