Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "aidl_dumpapi.h" |
| 18 | |
| 19 | #include <android-base/strings.h> |
| 20 | |
| 21 | #include "aidl.h" |
| 22 | #include "logging.h" |
| 23 | #include "os.h" |
| 24 | |
Jooyung Han | c5b1e08 | 2021-02-24 13:47:57 +0900 | [diff] [blame] | 25 | using android::base::EndsWith; |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 26 | using android::base::Join; |
| 27 | using android::base::Split; |
| 28 | using std::string; |
| 29 | using std::unique_ptr; |
| 30 | |
| 31 | namespace android { |
| 32 | namespace aidl { |
| 33 | |
| 34 | void DumpVisitor::DumpType(const AidlDefinedType& dt, const string& type) { |
Jooyung Han | ae01675 | 2022-02-26 21:33:43 +0900 | [diff] [blame] | 35 | if (!dt.IsUserDefined()) { |
| 36 | return; |
| 37 | } |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 38 | DumpComments(dt); |
| 39 | DumpAnnotations(dt); |
Jooyung Han | 71dfec3 | 2021-05-23 07:18:58 +0900 | [diff] [blame] | 40 | out << type << " " << dt.GetName(); |
| 41 | if (auto generic_type = dt.AsParameterizable(); generic_type && generic_type->IsGeneric()) { |
| 42 | out << "<" << Join(generic_type->GetTypeParameters(), ", ") << ">"; |
| 43 | } |
| 44 | out << " {\n"; |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 45 | out.Indent(); |
| 46 | DumpMembers(dt); |
| 47 | out.Dedent(); |
| 48 | out << "}\n"; |
| 49 | } |
| 50 | |
| 51 | void DumpVisitor::DumpMembers(const AidlDefinedType& dt) { |
| 52 | for (const auto& method : dt.GetMethods()) { |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 53 | method->DispatchVisit(*this); |
| 54 | } |
| 55 | for (const auto& field : dt.GetFields()) { |
| 56 | field->DispatchVisit(*this); |
| 57 | } |
| 58 | for (const auto& constdecl : dt.GetConstantDeclarations()) { |
| 59 | constdecl->DispatchVisit(*this); |
| 60 | } |
Jooyung Han | 2aedb11 | 2021-09-29 09:37:59 +0900 | [diff] [blame] | 61 | for (const auto& nested : dt.GetNestedTypes()) { |
| 62 | nested->DispatchVisit(*this); |
| 63 | } |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // Dumps comment only if its has meaningful tags. |
| 67 | void DumpVisitor::DumpComments(const AidlCommentable& c) { |
| 68 | const auto hidden = c.IsHidden(); |
| 69 | const auto deprecated = FindDeprecated(c.GetComments()); |
Jooyung Han | 8db6594 | 2021-03-02 18:14:44 +0900 | [diff] [blame] | 70 | if (hidden && !deprecated) { |
| 71 | // to pass --checkapi between the current and the tot in the mainline-prod branch |
| 72 | // emit @hide in a legacy dump style |
| 73 | out << "/* @hide */\n"; |
| 74 | } else if (hidden || deprecated) { |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 75 | out << "/**\n"; |
| 76 | if (hidden) { |
| 77 | out << " * @hide\n"; |
| 78 | } |
| 79 | if (deprecated) { |
| 80 | out << " * @deprecated " << deprecated->note << "\n"; |
| 81 | } |
| 82 | out << " */\n"; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void DumpVisitor::DumpAnnotations(const AidlAnnotatable& a) { |
| 87 | auto annotations = a.ToString(); |
| 88 | if (!annotations.empty()) { |
| 89 | out << annotations << "\n"; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void DumpVisitor::DumpConstantValue(const AidlTypeSpecifier& type, const AidlConstantValue& c) { |
| 94 | out << c.ValueString(type, AidlConstantValueDecorator); |
| 95 | } |
| 96 | |
| 97 | void DumpVisitor::Visit(const AidlInterface& t) { |
| 98 | DumpType(t, "interface"); |
| 99 | } |
Jooyung Han | ae01675 | 2022-02-26 21:33:43 +0900 | [diff] [blame] | 100 | |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 101 | void DumpVisitor::Visit(const AidlParcelable& t) { |
| 102 | DumpType(t, "parcelable"); |
| 103 | } |
Jooyung Han | ae01675 | 2022-02-26 21:33:43 +0900 | [diff] [blame] | 104 | |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 105 | void DumpVisitor::Visit(const AidlStructuredParcelable& t) { |
| 106 | DumpType(t, "parcelable"); |
| 107 | } |
Jooyung Han | ae01675 | 2022-02-26 21:33:43 +0900 | [diff] [blame] | 108 | |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 109 | void DumpVisitor::Visit(const AidlUnionDecl& t) { |
| 110 | DumpType(t, "union"); |
| 111 | } |
Jooyung Han | ae01675 | 2022-02-26 21:33:43 +0900 | [diff] [blame] | 112 | |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 113 | void DumpVisitor::Visit(const AidlEnumDeclaration& t) { |
Jooyung Han | ae01675 | 2022-02-26 21:33:43 +0900 | [diff] [blame] | 114 | if (!t.IsUserDefined()) { |
| 115 | return; |
| 116 | } |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 117 | DumpComments(t); |
| 118 | DumpAnnotations(t); |
| 119 | out << "enum " << t.GetName() << " {\n"; |
| 120 | out.Indent(); |
| 121 | for (const auto& e : t.GetEnumerators()) { |
Jooyung Han | 68d698f | 2022-03-18 15:38:27 +0900 | [diff] [blame] | 122 | DumpComments(*e); |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 123 | out << e->GetName() << " = "; |
| 124 | DumpConstantValue(t.GetBackingType(), *e->GetValue()); |
| 125 | out << ",\n"; |
| 126 | } |
| 127 | out.Dedent(); |
| 128 | out << "}\n"; |
| 129 | } |
| 130 | |
| 131 | void DumpVisitor::Visit(const AidlMethod& m) { |
Jooyung Han | ae01675 | 2022-02-26 21:33:43 +0900 | [diff] [blame] | 132 | if (!m.IsUserDefined()) { |
| 133 | return; |
| 134 | } |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 135 | DumpComments(m); |
| 136 | out << m.ToString() << ";\n"; |
| 137 | } |
Jooyung Han | ae01675 | 2022-02-26 21:33:43 +0900 | [diff] [blame] | 138 | |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 139 | void DumpVisitor::Visit(const AidlVariableDeclaration& v) { |
Jooyung Han | ae01675 | 2022-02-26 21:33:43 +0900 | [diff] [blame] | 140 | if (!v.IsUserDefined()) { |
| 141 | return; |
| 142 | } |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 143 | DumpComments(v); |
| 144 | Visit(v.GetType()); |
| 145 | if (v.IsDefaultUserSpecified()) { |
| 146 | out << " " << v.GetName() << " = "; |
| 147 | DumpConstantValue(v.GetType(), *v.GetDefaultValue()); |
| 148 | out << ";\n"; |
| 149 | } else { |
| 150 | out << " " << v.GetName() << ";\n"; |
| 151 | } |
| 152 | } |
Jooyung Han | ae01675 | 2022-02-26 21:33:43 +0900 | [diff] [blame] | 153 | |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 154 | void DumpVisitor::Visit(const AidlConstantDeclaration& c) { |
Jooyung Han | ae01675 | 2022-02-26 21:33:43 +0900 | [diff] [blame] | 155 | if (!c.IsUserDefined()) { |
| 156 | return; |
| 157 | } |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 158 | DumpComments(c); |
| 159 | out << "const "; |
| 160 | Visit(c.GetType()); |
| 161 | out << " " << c.GetName() << " = "; |
| 162 | DumpConstantValue(c.GetType(), c.GetValue()); |
| 163 | out << ";\n"; |
| 164 | } |
| 165 | |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 166 | void DumpVisitor::Visit(const AidlTypeSpecifier& t) { |
| 167 | out << t.ToString(); |
| 168 | } |
| 169 | |
| 170 | static string GetApiDumpPathFor(const AidlDefinedType& defined_type, const Options& options) { |
| 171 | string package_as_path = Join(Split(defined_type.GetPackage(), "."), OS_PATH_SEPARATOR); |
| 172 | AIDL_FATAL_IF(options.OutputDir().empty() || options.OutputDir().back() != '/', defined_type); |
| 173 | return options.OutputDir() + package_as_path + OS_PATH_SEPARATOR + defined_type.GetName() + |
| 174 | ".aidl"; |
| 175 | } |
| 176 | |
Jooyung Han | c5b1e08 | 2021-02-24 13:47:57 +0900 | [diff] [blame] | 177 | static void DumpComments(CodeWriter& out, const Comments& comments) { |
| 178 | bool needs_newline = false; |
| 179 | for (const auto& c : comments) { |
| 180 | out << c.body; |
| 181 | needs_newline = !EndsWith(c.body, "\n"); |
| 182 | } |
| 183 | if (needs_newline) { |
| 184 | out << "\n"; |
| 185 | } |
| 186 | } |
| 187 | |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 188 | bool dump_api(const Options& options, const IoDelegate& io_delegate) { |
| 189 | for (const auto& file : options.InputFiles()) { |
| 190 | AidlTypenames typenames; |
| 191 | if (internals::load_and_validate_aidl(file, options, io_delegate, &typenames, nullptr) == |
| 192 | AidlError::OK) { |
| 193 | const auto& doc = typenames.MainDocument(); |
| 194 | |
| 195 | for (const auto& type : doc.DefinedTypes()) { |
| 196 | unique_ptr<CodeWriter> writer = |
| 197 | io_delegate.GetCodeWriter(GetApiDumpPathFor(*type, options)); |
Jooyung Han | 252657e | 2021-02-27 02:51:39 +0900 | [diff] [blame] | 198 | if (!options.DumpNoLicense()) { |
| 199 | // dump doc comments (license) as well for each type |
| 200 | DumpComments(*writer, doc.GetComments()); |
| 201 | } |
Jooyung Han | 1f56b70 | 2021-02-11 13:16:15 +0900 | [diff] [blame] | 202 | (*writer) << kPreamble; |
| 203 | if (!type->GetPackage().empty()) { |
| 204 | (*writer) << "package " << type->GetPackage() << ";\n"; |
| 205 | } |
| 206 | DumpVisitor visitor(*writer); |
| 207 | type->DispatchVisit(visitor); |
| 208 | } |
| 209 | } else { |
| 210 | return false; |
| 211 | } |
| 212 | } |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | } // namespace aidl |
| 217 | } // namespace android |