MCPcopy Create free account
hub / github.com/bytecodealliance/wit-bindgen / lift_variant

Method lift_variant

crates/csharp/src/function.rs:160–238  ·  view source on GitHub ↗
(
        &mut self,
        ty: &Type,
        cases: &[(&str, Option<Type>)],
        op: &str,
        results: &mut Vec<String>,
    )

Source from the content-addressed store, hash-verified

158 }
159
160 fn lift_variant(
161 &mut self,
162 ty: &Type,
163 cases: &[(&str, Option<Type>)],
164 op: &str,
165 results: &mut Vec<String>,
166 ) {
167 let blocks = self
168 .blocks
169 .drain(self.blocks.len() - cases.len()..)
170 .collect::<Vec<_>>();
171 let variant_name = self.interface_gen.type_name_with_qualifier(ty, false);
172 let ty = self.interface_gen.type_name_with_qualifier(ty, true);
173 let generics_position = ty.find('<');
174 let lifted = self.locals.tmp("lifted");
175
176 let cases = cases
177 .iter()
178 .zip(blocks)
179 .enumerate()
180 .map(|(i, ((case_name, case_ty), Block { body, results, .. }))| {
181 let payload = if self
182 .interface_gen
183 .non_empty_type(case_ty.as_ref())
184 .is_some()
185 {
186 results.into_iter().next().unwrap()
187 } else if generics_position.is_some() {
188 if let Some(ty) = case_ty.as_ref() {
189 format!(
190 "{}.INSTANCE",
191 self.interface_gen.type_name_with_qualifier(ty, true)
192 )
193 } else {
194 format!(
195 "new global::{}None()",
196 self.interface_gen.csharp_gen.qualifier()
197 )
198 }
199 } else {
200 String::new()
201 };
202
203 let method =
204 variant_new_func_name(&variant_name, &case_name.to_csharp_ident_upper());
205
206 let call = if let Some(position) = generics_position {
207 let (ty, generics) = ty.split_at(position);
208 format!("{ty}{generics}.{method}")
209 } else {
210 format!("{ty}.{method}")
211 };
212
213 format!(
214 "case {i}: {{
215 {body}
216 {lifted} = {call}({payload});
217 break;

Callers 1

emitMethod · 0.45

Calls 12

newFunction · 0.85
variant_new_func_nameFunction · 0.85
findMethod · 0.80
joinMethod · 0.80
mapMethod · 0.80
to_csharp_ident_upperMethod · 0.80
tmpMethod · 0.45
iterMethod · 0.45
non_empty_typeMethod · 0.45
nextMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected