-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.go
More file actions
294 lines (256 loc) · 8.04 KB
/
Copy pathobject.go
File metadata and controls
294 lines (256 loc) · 8.04 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package analysis
import (
"cmp"
"fmt"
"github.com/ProCode-Software/klar/internal/klarerrs"
"github.com/ProCode-Software/klar/internal/ranges"
)
// Object represents a declared Klar object.
type Object struct {
Name string // Name of the object as declared in its module
Context *Context // Context in which the object was declared
Range ranges.Range // Position span of the object in the source code
File FileID // File ID the object was declared in
Public bool // Whether the object is exported
Module *Module // Module where the object was declared
Type ObjectKind // Type of the object
Order uint32 // Order in which the object was declared in the module/block
Flags Flag // Flags applied to the object
attrs *Attributes
info *DeclarationInfo
}
// NewObject returns a new [Object] without context information.
func NewObject(
name string, fid FileID, rang ranges.Range, mod *Module, typ ObjectKind,
) *Object {
return &Object{Name: name, Module: mod, Range: rang, File: fid, Type: typ}
}
// FileContext returns the context for the file in which the object was declared.
// The return value is not equal to [Object.Context], but is the context where
// imported objects (that the object could depend on) are declared.
func (obj *Object) FileContext() *Context { return obj.Module.fileContext[obj.File] }
// LookupContext returns the context in which imported objects (that the object
// could depend on) are declared. This is the object's file context, unless
// the object is declared in a nested scope (such as a function).
func (obj *Object) LookupContext() *Context {
fctx := obj.FileContext()
if obj.Context.File <= 0 {
return fctx
}
return obj.Context
}
// Underlying is equivalent to obj.Type()
func (obj *Object) Underlying() Type { return obj.Type }
// Kind returns the kind of the object. Kind is equivalent to obj.Type().Kind().
func (obj *Object) Kind() Kind { return obj.Type.Kind() }
// String returns a human-readable representation of the object's type.
func (obj *Object) String() string {
if obj.IsTypeName() {
return obj.Name
}
return obj.Type.String()
}
// ObjectString returns a human-readable representation of the object.
func (obj *Object) ObjectString() string {
declKind := "object"
filePath := fmt.Sprintf(" (%s:%s)", obj.FilePath(), obj.Range)
switch typ := obj.Type.(type) {
case *Function:
return typ.StringWithName(obj.Name) + filePath
case *FunctionAlias:
case *Variable:
declKind = "var"
case *Constant:
declKind = "const"
case *TypeName:
switch inner := typ.Type.(type) {
case *Struct:
declKind = "struct"
case *StructField:
declKind = "field"
case *EnumItem:
declKind = "enum item"
case *Enum:
declKind = "enum"
case *Interface:
declKind = "interface"
case *Tag:
return "type #" + obj.Name + filePath
default: // Including type alias
return fmt.Sprintf("type %s = %s%s", obj.Name, inner.String(), filePath)
}
return declKind + obj.Name + filePath
}
return fmt.Sprintf("%s %s: %s%s", declKind, obj.Name, obj.Type.String(), filePath)
}
// Path returns the name of the object with the full import path.
func (obj *Object) Path() string {
return obj.Module.ImportPathString() + "/" + obj.Name
}
// TODO: If the object is top-level, these don't return a file. Find a solution
// FileName returns the base name of the file o was declared in.
func (o *Object) FileName() string { return o.Module.ResolveFile(o.File) }
// FileName returns the full path of the file o was declared in.
func (o *Object) FilePath() string { return o.Module.ResolveFilePath(o.File) }
// FileRange returns a [ranges.FileRange] representing the range of o's declaration
// and the base name of the containing file.
func (o *Object) FileRange() ranges.FileRange {
return ranges.FileRange{o.Range, o.FileName()}
}
// FilePathRange returns a [ranges.FilePathRange] representing the range of o's
// declaration and the full path of the containing file.
func (o *Object) FilePathRange() ranges.FileRange {
return ranges.FileRange{o.Range, o.FilePath()}
}
// IsTypeName reports whether o represents a type declaration.
func (o *Object) IsTypeName() bool {
if o == nil {
return false
}
_, ok := o.Type.(*TypeName)
return ok
}
// TypeName returns o's Type() as a [*TypeName], or panics if
// o is not a type name.
func (o *Object) TypeName() *TypeName { return o.Type.(*TypeName) }
func (o *Object) Clone(mod *Module, fid FileID, rang ranges.Range) *Object {
cloned := new(*o)
cloned.Range = rang
cloned.File = fid
if mod != nil {
cloned.Module = mod
}
return cloned
}
type ObjectKind interface {
Type
objKind()
Underlying() Type
}
type InvalidObject struct{}
func (o *InvalidObject) Kind() Kind { return InvalidType }
func (o *InvalidObject) String() string { return o.Kind().String() }
func (o *InvalidObject) Underlying() Type { return o.Kind() }
func (o *InvalidObject) objKind() {}
// Type Kinds
// ============
// Kind represents the kind of an object.
type Kind int
const (
// Kinds that can be used as standalone [Type]s.
InvalidType Kind = iota
IntType
StringType
BoolType
FloatType
AnyType
ErrorType
NothingType
RegExType
KindList
KindMap
KindResult
KindFunction
KindUnion
KindOptional
KindTuple
KindTask
KindEnum
KindStruct
KindInterface
KindTag
KindNamespace
KindGeneric
)
// Kind returns the receiver. It panics if the receiver isn't a primitive.
func (k Kind) Kind() Kind {
if !k.IsPrimitive() {
panic(fmt.Sprintf("kind %d is not a primitive", k))
}
return k
}
func (k Kind) IsPrimitive() bool {
switch k {
case InvalidType, IntType, StringType, BoolType, FloatType,
AnyType, ErrorType, NothingType, RegExType:
return true
default:
return false
}
}
// String returns the kind of the type as a human-readable string. If k is a
// primitive, the name of the Klar type is returned.
func (k Kind) String() string {
return [...]string{
// Primitives
IntType: "Int",
StringType: "String",
BoolType: "Bool",
FloatType: "Float",
AnyType: "Any",
ErrorType: "Error",
NothingType: "Nothing",
RegExType: "RegEx",
InvalidType: "invalid type",
KindList: "list",
KindMap: "map",
KindResult: "Result",
KindFunction: "function",
KindUnion: "union",
KindOptional: "optional",
KindTuple: "tuple",
KindTask: "Task",
KindEnum: "enum",
KindStruct: "struct",
KindInterface: "interface",
KindTag: "tag",
KindNamespace: "module",
KindGeneric: "generic",
}[k]
}
func (k Kind) Index(i string, t *Expr) *klarerrs.Error {
if !k.IsPrimitive() {
panic("cannot Index non-primitive type")
}
return indexBuiltin(k.String(), i, t)
}
func (k Kind) IndexComputed(i Type, t *Expr) *klarerrs.Error {
switch {
case !k.IsPrimitive():
panic("cannot Index non-primitive type")
case k != StringType:
// String is the only primitive that allows computed indexing
return indexError(klarerrs.ErrInvalidComputedIndex, i, "")
case i.Kind() != IntType:
return indexTypeMismatchError(
klarerrs.ErrNonNumericIndex,
StringType, i, "Can't index String using type "+i.String(),
)
default:
// TODO: constant analysis (negative index, out of range index)
t.Type = &Optional{StringType}
return nil
}
}
// Types can be indexed via `obj[index]`.
// ComputedIndexer is implemented by the following types:
// - [Map] when index is type [Map.Key]
// - [List] when index is [IntType]
// - [StringType] when index is [IntType]
// - [Tuple] when index is a constant [IntType]
type ComputedIndexer interface {
IndexComputed(index Type, t *Expr) *klarerrs.Error
}
// Per the spec
var (
_ = [...]ComputedIndexer{&Map{}, &List{}, StringType, &Tuple{}}
_ = [...]Indexer{
&Map{}, &List{}, &Struct{}, &Enum{}, &Interface{}, &Task{},
StringType, IntType, FloatType, ErrorType,
}
)
// The result of a function call that doesn't return. Statements
// following this are unreachable.
type NoReturn struct{ Type }
func (nr *NoReturn) IsTODO() bool { return nr.Type == nil }
func (u *NoReturn) Underlying() Type { return cmp.Or[Type](u.Type, u /* is a TODO */) }