Brian Silverman | 4e662aa | 2022-05-11 23:10:19 -0700 | [diff] [blame^] | 1 | // 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 | |
| 9 | use itertools::Itertools; |
| 10 | |
| 11 | use crate::{ |
| 12 | conversion::api::{Api, TypeKind}, |
| 13 | types::QualifiedName, |
| 14 | }; |
| 15 | |
| 16 | use super::{ |
| 17 | fun::{FnPhase, FnPrePhase1, PodAndDepAnalysis}, |
| 18 | pod::PodAnalysis, |
| 19 | tdef::TypedefAnalysis, |
| 20 | }; |
| 21 | |
| 22 | pub(crate) trait HasDependencies { |
| 23 | fn name(&self) -> &QualifiedName; |
| 24 | fn deps(&self) -> Box<dyn Iterator<Item = &QualifiedName> + '_>; |
| 25 | |
| 26 | fn format_deps(&self) -> String { |
| 27 | self.deps().join(",") |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | impl HasDependencies for Api<FnPrePhase1> { |
| 32 | fn deps(&self) -> Box<dyn Iterator<Item = &QualifiedName> + '_> { |
| 33 | match self { |
| 34 | Api::Typedef { |
| 35 | old_tyname, |
| 36 | analysis: TypedefAnalysis { deps, .. }, |
| 37 | .. |
| 38 | } => Box::new(old_tyname.iter().chain(deps.iter())), |
| 39 | Api::Struct { |
| 40 | analysis: |
| 41 | PodAnalysis { |
| 42 | kind: TypeKind::Pod, |
| 43 | bases, |
| 44 | field_deps, |
| 45 | .. |
| 46 | }, |
| 47 | .. |
| 48 | } => Box::new(field_deps.iter().chain(bases.iter())), |
| 49 | Api::Function { analysis, .. } => Box::new(analysis.deps.iter()), |
| 50 | Api::Subclass { |
| 51 | name: _, |
| 52 | superclass, |
| 53 | } => Box::new(std::iter::once(superclass)), |
| 54 | Api::RustSubclassFn { details, .. } => Box::new(details.dependencies.iter()), |
| 55 | Api::RustFn { receiver, .. } => Box::new(receiver.iter()), |
| 56 | _ => Box::new(std::iter::empty()), |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | fn name(&self) -> &QualifiedName { |
| 61 | self.name() |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | impl HasDependencies for Api<FnPhase> { |
| 66 | /// Any dependencies on other APIs which this API has. |
| 67 | fn deps(&self) -> Box<dyn Iterator<Item = &QualifiedName> + '_> { |
| 68 | match self { |
| 69 | Api::Typedef { |
| 70 | old_tyname, |
| 71 | analysis: TypedefAnalysis { deps, .. }, |
| 72 | .. |
| 73 | } => Box::new(old_tyname.iter().chain(deps.iter())), |
| 74 | Api::Struct { |
| 75 | analysis: |
| 76 | PodAndDepAnalysis { |
| 77 | pod: |
| 78 | PodAnalysis { |
| 79 | kind: TypeKind::Pod, |
| 80 | bases, |
| 81 | field_deps, |
| 82 | .. |
| 83 | }, |
| 84 | constructor_and_allocator_deps, |
| 85 | .. |
| 86 | }, |
| 87 | .. |
| 88 | } => Box::new( |
| 89 | field_deps |
| 90 | .iter() |
| 91 | .chain(bases.iter()) |
| 92 | .chain(constructor_and_allocator_deps.iter()), |
| 93 | ), |
| 94 | Api::Struct { |
| 95 | analysis: |
| 96 | PodAndDepAnalysis { |
| 97 | constructor_and_allocator_deps, |
| 98 | .. |
| 99 | }, |
| 100 | .. |
| 101 | } => Box::new(constructor_and_allocator_deps.iter()), |
| 102 | Api::Function { analysis, .. } => Box::new(analysis.deps.iter()), |
| 103 | Api::Subclass { |
| 104 | name: _, |
| 105 | superclass, |
| 106 | } => Box::new(std::iter::once(superclass)), |
| 107 | Api::RustSubclassFn { details, .. } => Box::new(details.dependencies.iter()), |
| 108 | Api::RustFn { receiver, .. } => Box::new(receiver.iter()), |
| 109 | _ => Box::new(std::iter::empty()), |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | fn name(&self) -> &QualifiedName { |
| 114 | self.name() |
| 115 | } |
| 116 | } |