-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathconstructor_deps.rs
106 lines (100 loc) · 3.48 KB
/
constructor_deps.rs
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
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use indexmap::map::IndexMap as HashMap;
use crate::{
conversion::{
api::{Api, ApiName, StructDetails, TypeKind},
apivec::ApiVec,
convert_error::ConvertErrorWithContext,
error_reporter::convert_apis,
},
types::QualifiedName,
};
use super::fun::{
FnAnalysis, FnKind, FnPhase, FnPrePhase3, PodAndConstructorAnalysis, PodAndDepAnalysis,
TraitMethodKind,
};
/// We've now analyzed all functions (including both implicit and explicit
/// constructors). Decorate each struct with a note of its constructors,
/// which will later be used as edges in the garbage collection, because
/// typically any use of a type will require us to call its copy or move
/// constructor. The same applies to its alloc/free functions.
pub(crate) fn decorate_types_with_constructor_deps(apis: ApiVec<FnPrePhase3>) -> ApiVec<FnPhase> {
let mut constructors_and_allocators_by_type = find_important_constructors(&apis);
let mut results = ApiVec::new();
convert_apis(
apis,
&mut results,
Api::fun_unchanged,
|name, details, pod| {
decorate_struct(name, details, pod, &mut constructors_and_allocators_by_type)
},
Api::enum_unchanged,
Api::typedef_unchanged,
Api::subclass_unchanged,
);
results
}
fn decorate_struct(
name: ApiName,
details: Box<StructDetails>,
fn_struct: PodAndConstructorAnalysis,
constructors_and_allocators_by_type: &mut HashMap<QualifiedName, Vec<QualifiedName>>,
) -> Result<Box<dyn Iterator<Item = Api<FnPhase>>>, ConvertErrorWithContext> {
let pod = fn_struct.pod;
let is_abstract = matches!(pod.kind, TypeKind::Abstract);
let constructor_and_allocator_deps = if is_abstract || pod.num_generics > 0 {
Vec::new()
} else {
constructors_and_allocators_by_type
.remove(&name.name)
.unwrap_or_default()
};
Ok(Box::new(std::iter::once(Api::Struct {
name,
details,
analysis: PodAndDepAnalysis {
pod,
constructor_and_allocator_deps,
constructors: fn_struct.constructors,
},
})))
}
fn find_important_constructors(
apis: &ApiVec<FnPrePhase3>,
) -> HashMap<QualifiedName, Vec<QualifiedName>> {
let mut results: HashMap<QualifiedName, Vec<QualifiedName>> = HashMap::new();
for api in apis.iter() {
if let Api::Function {
name,
analysis:
FnAnalysis {
kind:
FnKind::TraitMethod {
kind:
TraitMethodKind::Alloc
| TraitMethodKind::Dealloc
| TraitMethodKind::CopyConstructor
| TraitMethodKind::MoveConstructor,
impl_for,
..
},
ignore_reason: Ok(_),
..
},
..
} = api
{
results
.entry(impl_for.clone())
.or_default()
.push(name.name.clone())
}
}
results
}