11//! HIR version of the global scope in order to store descriptors and implementations
22
3- use compiler_typing:: TypedGlobalScope ;
3+ use compiler_global_scope:: key:: EntryKey ;
4+ use compiler_typing:: { TypedGlobalScope , TypedGlobalScopeEntry , raw:: RawType , tree:: Type } ;
5+ use diagnostics:: { DiagnosticResult , DiagnosticSpanOrigin } ;
46
57use crate :: { ctx:: HIRFunction , nodes:: HIRNode } ;
68
@@ -11,3 +13,102 @@ pub struct HIRGlobalScopeStorage {
1113 pub descriptors : Vec < HIRFunction > ,
1214 pub implementations : Vec < Box < HIRNode > > ,
1315}
16+
17+ impl HIRGlobalScopeStorage {
18+ pub fn new ( ) -> Self {
19+ HIRGlobalScopeStorage {
20+ scope : TypedGlobalScope :: new ( ) ,
21+ descriptors : vec ! [ ] ,
22+ implementations : vec ! [ ] ,
23+ }
24+ }
25+
26+ /// This doesn't automatically handle descriptors and implementations
27+ pub fn append < K : DiagnosticSpanOrigin > (
28+ & mut self ,
29+ name : EntryKey ,
30+ entry : TypedGlobalScopeEntry ,
31+ origin : & K ,
32+ ) -> DiagnosticResult < usize > {
33+ self . scope . append ( name, entry, origin)
34+ }
35+
36+ pub fn append_func < K : DiagnosticSpanOrigin > (
37+ & mut self ,
38+ name : EntryKey ,
39+ descriptor : HIRFunction ,
40+ implementation : Box < HIRNode > ,
41+ origin : & K ,
42+ ) -> DiagnosticResult < usize > {
43+ self . descriptors . push ( descriptor) ;
44+ self . implementations . push ( implementation) ;
45+
46+ self . scope . append (
47+ name,
48+ TypedGlobalScopeEntry :: Function {
49+ descriptor_ind : self . scope . descriptor_counter ,
50+ impl_ind : self . scope . impl_counter ,
51+ } ,
52+ origin,
53+ )
54+ }
55+
56+ pub fn append_implless_function < K : DiagnosticSpanOrigin > (
57+ & mut self ,
58+ name : EntryKey ,
59+ descriptor : HIRFunction ,
60+ origin : & K ,
61+ ) -> DiagnosticResult < usize > {
62+ self . descriptors . push ( descriptor) ;
63+
64+ self . scope . append (
65+ name,
66+ TypedGlobalScopeEntry :: ImplLessFunction ( self . scope . descriptor_counter ) ,
67+ origin,
68+ )
69+ }
70+
71+ pub fn append_struct_function < K : DiagnosticSpanOrigin > (
72+ & mut self ,
73+ name : EntryKey ,
74+ descriptor : HIRFunction ,
75+ implementation : Box < HIRNode > ,
76+ struct_type : RawType ,
77+ origin : & K ,
78+ ) -> DiagnosticResult < usize > {
79+ self . descriptors . push ( descriptor) ;
80+ self . implementations . push ( implementation) ;
81+
82+ let ind = self . scope . value_to_ind [ & TypedGlobalScopeEntry :: Type ( struct_type) ] ;
83+
84+ self . scope . append (
85+ name,
86+ TypedGlobalScopeEntry :: StructFunction {
87+ descriptor_ind : self . scope . descriptor_counter ,
88+ impl_ind : self . scope . impl_counter ,
89+ struct_type : ind,
90+ } ,
91+ origin,
92+ )
93+ }
94+
95+ pub fn append_type < K : DiagnosticSpanOrigin > (
96+ & mut self ,
97+ name : EntryKey ,
98+ t : RawType ,
99+ origin : & K ,
100+ ) -> DiagnosticResult < usize > {
101+ self . scope
102+ . append ( name, TypedGlobalScopeEntry :: Type ( t) , origin)
103+ }
104+
105+ pub fn append_type_binding < K : DiagnosticSpanOrigin > (
106+ & mut self ,
107+ name : EntryKey ,
108+ t : Type ,
109+ origin : & K ,
110+ ) -> DiagnosticResult < usize > {
111+ self . scope
112+ . append ( name, TypedGlobalScopeEntry :: TypeAlias ( t) , origin)
113+ }
114+ }
0 commit comments