blob: a301090291d0474b923749bfceb309c35346e3af [file] [log] [blame]
Brian Silverman4e662aa2022-05-11 23:10:19 -07001// Copyright 2022 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
Brian Silverman4e662aa2022-05-11 23:10:19 -07009use crate::{
10 conversion::api::{Api, TypeKind},
11 types::QualifiedName,
12};
13
14use super::{
15 fun::{FnPhase, FnPrePhase1, PodAndDepAnalysis},
16 pod::PodAnalysis,
17 tdef::TypedefAnalysis,
18};
19
20pub(crate) trait HasDependencies {
21 fn name(&self) -> &QualifiedName;
22 fn deps(&self) -> Box<dyn Iterator<Item = &QualifiedName> + '_>;
Brian Silverman4e662aa2022-05-11 23:10:19 -070023}
24
25impl HasDependencies for Api<FnPrePhase1> {
26 fn deps(&self) -> Box<dyn Iterator<Item = &QualifiedName> + '_> {
27 match self {
28 Api::Typedef {
29 old_tyname,
30 analysis: TypedefAnalysis { deps, .. },
31 ..
32 } => Box::new(old_tyname.iter().chain(deps.iter())),
33 Api::Struct {
Austin Schuh6ea9bfa2023-08-06 19:05:10 -070034 analysis: PodAnalysis {
35 bases, field_deps, ..
36 },
Brian Silverman4e662aa2022-05-11 23:10:19 -070037 ..
38 } => Box::new(field_deps.iter().chain(bases.iter())),
39 Api::Function { analysis, .. } => Box::new(analysis.deps.iter()),
40 Api::Subclass {
41 name: _,
42 superclass,
43 } => Box::new(std::iter::once(superclass)),
44 Api::RustSubclassFn { details, .. } => Box::new(details.dependencies.iter()),
Austin Schuh6ea9bfa2023-08-06 19:05:10 -070045 Api::RustFn { deps, .. } => Box::new(deps.iter()),
Brian Silverman4e662aa2022-05-11 23:10:19 -070046 _ => Box::new(std::iter::empty()),
47 }
48 }
49
50 fn name(&self) -> &QualifiedName {
51 self.name()
52 }
53}
54
55impl HasDependencies for Api<FnPhase> {
56 /// Any dependencies on other APIs which this API has.
57 fn deps(&self) -> Box<dyn Iterator<Item = &QualifiedName> + '_> {
58 match self {
59 Api::Typedef {
60 old_tyname,
61 analysis: TypedefAnalysis { deps, .. },
62 ..
63 } => Box::new(old_tyname.iter().chain(deps.iter())),
64 Api::Struct {
65 analysis:
66 PodAndDepAnalysis {
67 pod:
68 PodAnalysis {
69 kind: TypeKind::Pod,
70 bases,
71 field_deps,
72 ..
73 },
74 constructor_and_allocator_deps,
75 ..
76 },
77 ..
78 } => Box::new(
79 field_deps
80 .iter()
81 .chain(bases.iter())
82 .chain(constructor_and_allocator_deps.iter()),
83 ),
84 Api::Struct {
85 analysis:
86 PodAndDepAnalysis {
87 constructor_and_allocator_deps,
88 ..
89 },
90 ..
91 } => Box::new(constructor_and_allocator_deps.iter()),
92 Api::Function { analysis, .. } => Box::new(analysis.deps.iter()),
93 Api::Subclass {
94 name: _,
95 superclass,
96 } => Box::new(std::iter::once(superclass)),
97 Api::RustSubclassFn { details, .. } => Box::new(details.dependencies.iter()),
Austin Schuh6ea9bfa2023-08-06 19:05:10 -070098 Api::RustFn { deps, .. } => Box::new(deps.iter()),
Brian Silverman4e662aa2022-05-11 23:10:19 -070099 _ => Box::new(std::iter::empty()),
100 }
101 }
102
103 fn name(&self) -> &QualifiedName {
104 self.name()
105 }
106}