aboutsummaryrefslogtreecommitdiff
path: root/src/server/symbol.odin
blob: 57d3946cdc9ddb048f8182f4fca43cae7f9b110d (plain)
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package server

import "core:fmt"
import "core:hash"
import "core:log"
import "core:mem"
import "core:odin/ast"
import "core:odin/tokenizer"
import "core:path/filepath"
import path "core:path/slashpath"
import "core:slice"
import "core:strings"

import "shared:common"

SymbolAndNode :: struct {
	symbol:      Symbol,
	node:        ^ast.Node,
	is_resolved: bool,
}

SymbolStructValue :: struct {
	names:  []string,
	ranges: []common.Range,
	types:  []^ast.Expr,
	usings: map[int]bool,
	poly:   ^ast.Field_List,
	args:   []^ast.Expr, //The arguments in the call expression for poly
}

SymbolPackageValue :: struct {}

SymbolProcedureValue :: struct {
	return_types: []^ast.Field,
	arg_types:    []^ast.Field,
	generic:      bool,
}

SymbolProcedureGroupValue :: struct {
	group: ^ast.Expr,
}

//runtime temp symbol value
SymbolAggregateValue :: struct {
	symbols: []Symbol,
}

SymbolEnumValue :: struct {
	names:  []string,
	ranges: []common.Range,
}

SymbolUnionValue :: struct {
	types: []^ast.Expr,
	poly:  ^ast.Field_List,
}

SymbolDynamicArrayValue :: struct {
	expr: ^ast.Expr,
}

SymbolMultiPointer :: struct {
	expr: ^ast.Expr,
}

SymbolFixedArrayValue :: struct {
	len:  ^ast.Expr,
	expr: ^ast.Expr,
}

SymbolSliceValue :: struct {
	expr: ^ast.Expr,
}

SymbolBasicValue :: struct {
	ident: ^ast.Ident,
}

SymbolBitSetValue :: struct {
	expr: ^ast.Expr,
}

SymbolUntypedValue :: struct {
	type: enum {
		Integer,
		Float,
		String,
		Bool,
	},
	tok:  tokenizer.Token,
}

SymbolMapValue :: struct {
	key:   ^ast.Expr,
	value: ^ast.Expr,
}

SymbolMatrixValue :: struct {
	x:    ^ast.Expr,
	y:    ^ast.Expr,
	expr: ^ast.Expr,
}

/*
	Generic symbol that is used by the indexer for any variable type(constants, defined global variables, etc),
*/
SymbolGenericValue :: struct {
	expr: ^ast.Expr,
}

SymbolValue :: union {
	SymbolStructValue,
	SymbolPackageValue,
	SymbolProcedureValue,
	SymbolGenericValue,
	SymbolProcedureGroupValue,
	SymbolUnionValue,
	SymbolEnumValue,
	SymbolBitSetValue,
	SymbolAggregateValue,
	SymbolDynamicArrayValue,
	SymbolFixedArrayValue,
	SymbolMultiPointer,
	SymbolMapValue,
	SymbolSliceValue,
	SymbolBasicValue,
	SymbolUntypedValue,
	SymbolMatrixValue,
}

SymbolFlag :: enum {
	Distinct,
	Deprecated,
	PrivateFile,
	PrivatePackage,
	Anonymous, //Usually applied to structs that are defined inline inside another struct
	Variable, //Symbols that are variable, this means their value decl was mutable
	Local,
	ObjC,
	ObjCIsClassMethod, // should be set true only when ObjC is enabled
}

SymbolFlags :: bit_set[SymbolFlag]

Symbol :: struct {
	range:     common.Range, //the range of the symbol in the file
	uri:       string, //uri of the file the symbol resides
	pkg:       string, //absolute directory path where the symbol resides
	name:      string, //name of the symbol
	doc:       string,
	signature: string, //type signature
	type:      SymbolType,
	value:     SymbolValue,
	pointers:  int, //how many `^` are applied to the symbol
	flags:     SymbolFlags,
}

SymbolType :: enum {
	Function      = 3,
	Field         = 5,
	Variable      = 6,
	Package       = 9,
	Enum          = 13,
	Keyword       = 14,
	EnumMember    = 20,
	Constant      = 21,
	Struct        = 22,
	Type_Function = 23,
	Union         = 7,
	Unresolved    = 1, //Use text if not being able to resolve it.
}

new_clone_symbol :: proc(
	data: Symbol,
	allocator := context.allocator,
) -> ^Symbol {
	new_symbol := new(Symbol, allocator)
	new_symbol^ = data
	new_symbol.value = data.value
	return new_symbol
}

free_symbol :: proc(symbol: Symbol, allocator: mem.Allocator) {
	if symbol.signature != "" &&
	   symbol.signature != "struct" &&
	   symbol.signature != "union" &&
	   symbol.signature != "enum" &&
	   symbol.signature != "bitset" {
		delete(symbol.signature, allocator)
	}

	if symbol.doc != "" {
		delete(symbol.doc, allocator)
	}

	switch v in symbol.value {
	case SymbolMatrixValue:
		common.free_ast(v.expr, allocator)
		common.free_ast(v.x, allocator)
		common.free_ast(v.y, allocator)
	case SymbolMultiPointer:
		common.free_ast(v.expr, allocator)
	case SymbolProcedureValue:
		common.free_ast(v.return_types, allocator)
		common.free_ast(v.arg_types, allocator)
	case SymbolStructValue:
		delete(v.names, allocator)
		common.free_ast(v.types, allocator)
	case SymbolGenericValue:
		common.free_ast(v.expr, allocator)
	case SymbolProcedureGroupValue:
		common.free_ast(v.group, allocator)
	case SymbolEnumValue:
		delete(v.names, allocator)
	case SymbolUnionValue:
		common.free_ast(v.types, allocator)
	case SymbolBitSetValue:
		common.free_ast(v.expr, allocator)
	case SymbolDynamicArrayValue:
		common.free_ast(v.expr, allocator)
	case SymbolFixedArrayValue:
		common.free_ast(v.expr, allocator)
		common.free_ast(v.len, allocator)
	case SymbolSliceValue:
		common.free_ast(v.expr, allocator)
	case SymbolBasicValue:
		common.free_ast(v.ident, allocator)
	case SymbolAggregateValue:
		for symbol in v.symbols {
			free_symbol(symbol, allocator)
		}
	case SymbolMapValue:
		common.free_ast(v.key, allocator)
		common.free_ast(v.value, allocator)
	case SymbolUntypedValue:
		delete(v.tok.text)
	case SymbolPackageValue:
	}
}

symbol_type_to_completion_kind :: proc(
	type: SymbolType,
) -> CompletionItemKind {
	switch type {
	case .Function:
		return .Function
	case .Field:
		return .Field
	case .Variable:
		return .Variable
	case .Package:
		return .Module
	case .Enum:
		return .Enum
	case .Keyword:
		return .Keyword
	case .EnumMember:
		return .EnumMember
	case .Constant:
		return .Constant
	case .Struct:
		return .Struct
	case .Type_Function:
		return .Function
	case .Union:
		return .Enum
	case .Unresolved:
		return .Text
	case:
		return .Text
	}
}

symbol_kind_to_type :: proc(type: SymbolType) -> SymbolKind {
	#partial switch type {
	case .Function:
		return .Function
	case .Constant:
		return .Constant
	case .Variable:
		return .Variable
	case .Union:
		return .Enum
	case .Struct:
		return .Struct
	case .Enum:
		return .Enum
	case .Keyword:
		return .Key
	case:
		return .Null
	}
}

symbol_to_expr :: proc(
	symbol: Symbol,
	file: string,
	allocator := context.temp_allocator,
) -> ^ast.Expr {

	pos := tokenizer.Pos {
		file = file,
	}

	end := tokenizer.Pos {
		file = file,
	}

	#partial switch v in symbol.value {
	case SymbolDynamicArrayValue:
		type := new_type(ast.Dynamic_Array_Type, pos, end, allocator)
		type.elem = v.expr
		return type
	case SymbolFixedArrayValue:
		type := new_type(ast.Array_Type, pos, end, allocator)
		type.elem = v.expr
		type.len = v.len
		return type
	case SymbolMapValue:
		type := new_type(ast.Map_Type, pos, end, allocator)
		type.key = v.key
		type.value = v.value
		return type
	case SymbolBasicValue:
		return v.ident
	case SymbolSliceValue:
		type := new_type(ast.Array_Type, pos, end, allocator)
		type.elem = v.expr
		return type
	case SymbolStructValue:
		type := new_type(ast.Struct_Type, pos, end, allocator)
		return type
	case SymbolUntypedValue:
		type := new_type(ast.Basic_Lit, pos, end, allocator)
		return type
	case SymbolMatrixValue:
		type := new_type(ast.Matrix_Type, pos, end, allocator)
		type.row_count = v.x
		type.column_count = v.y
		type.elem = v.expr
		return type
	case SymbolProcedureValue:
		type := new_type(ast.Proc_Type, pos, end, allocator)
		type.results = new_type(ast.Field_List, pos, end, allocator)
		type.results.list = v.return_types
		type.params = new_type(ast.Field_List, pos, end, allocator)
		type.params.list = v.arg_types
		return type
	case:
		return nil
	}

	return nil
}