Skip to content
← Back

src/semantic/lookups/resource_sources.ghul

1
@@IF.dotnet()
2
3
namespace Semantic.Lookups is
4
use TYPE = System.Type
5
use System.Exception
6
7
use Types.Type
8
9
// The .NET calls whose result is a sequence that holds a resource
10
// until something disposes it, and so should not be read by a loop,
11
// a pipe chain or a terminal without a `let use` holding it. Every
12
// such call the compiler knows about is listed here: the file and
13
// directory enumerations by member, and anything returning a data
14
// reader or a PLINQ query by its result type.
15
class RESOURCE_SOURCES(
16
_type_mapper: DotNet.TYPE_MAPPER,
17
_type_source: DotNet.TypeSource
18
) is
19
_member_cache: Collections.SET[Symbols.Symbol]?
20
_result_type_cache: Collections.List[Type]?
21
22
holds_resource(function: Symbols.Function) -> bool is
23
if _members.contains(function.root_specialized_from) then
24
return true
25
fi
26
27
if let return_type = function.return_type then
28
for t in _result_types do
29
if t.is_assignable_from(return_type) then
30
return true
31
fi
32
od
33
fi
34
35
return false
36
si
37
38
_members: Collections.SET[Symbols.Symbol] is
39
if let members = _member_cache then
40
return members
41
fi
42
43
let result = Collections.SET[Symbols.Symbol]()
44
45
_add_members(result, "System.IO.File", ["read_lines"])
46
_add_members(result, "System.IO.Directory", [
47
"enumerate_files",
48
"enumerate_directories",
49
"enumerate_file_system_entries"
50
])
51
52
_member_cache = result
53
54
return result
55
si
56
57
_result_types: Collections.List[Type] is
58
if let types = _result_type_cache then
59
return types
60
fi
61
62
let result = Collections.LIST[Type]()
63
64
for (assembly, name) in [
65
("System.Data.Common", "System.Data.IDataReader"),
66
("System.Linq.Parallel", "System.Linq.ParallelQuery")
67
] do
68
if let type = _try_get_type(assembly, name) then
69
result.add(type)
70
fi
71
od
72
73
_result_type_cache = result
74
75
return result
76
si
77
78
_add_members(result: Collections.SET[Symbols.Symbol], type_name: string, names: string[]) is
79
let type = _try_get_type("System.Runtime", type_name)
80
81
if !type? then
82
return
83
fi
84
85
for name in names do
86
if let group: Symbols.FUNCTION_GROUP = type.symbol.find_member(name) then
87
for f in group.functions do
88
result.add(f.root_specialized_from)
89
od
90
fi
91
od
92
si
93
94
// A type in an assembly the compilation may not reference, which
95
// then has nothing in it to report.
96
_try_get_type(assembly: string, name: string) -> Type? is
97
try
98
return _type_mapper.get_type(_type_source.get_type(assembly, name))
99
catch e: Exception
100
return null
101
yrt
102
si
103
si
104
si