blob: 1451d4170b076c2f958f54619f52ca767a06ec50 [file] [log] [blame]
Jooyung Han1f56b702021-02-11 13:16:15 +09001/*
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 Hanc5b1e082021-02-24 13:47:57 +090025using android::base::EndsWith;
Jooyung Han1f56b702021-02-11 13:16:15 +090026using android::base::Join;
27using android::base::Split;
28using std::string;
29using std::unique_ptr;
30
31namespace android {
32namespace aidl {
33
34void DumpVisitor::DumpType(const AidlDefinedType& dt, const string& type) {
Jooyung Hanae016752022-02-26 21:33:43 +090035 if (!dt.IsUserDefined()) {
36 return;
37 }
Jooyung Han1f56b702021-02-11 13:16:15 +090038 DumpComments(dt);
39 DumpAnnotations(dt);
Jooyung Han71dfec32021-05-23 07:18:58 +090040 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 Han1f56b702021-02-11 13:16:15 +090045 out.Indent();
46 DumpMembers(dt);
47 out.Dedent();
48 out << "}\n";
49}
50
51void DumpVisitor::DumpMembers(const AidlDefinedType& dt) {
52 for (const auto& method : dt.GetMethods()) {
Jooyung Han1f56b702021-02-11 13:16:15 +090053 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 Han2aedb112021-09-29 09:37:59 +090061 for (const auto& nested : dt.GetNestedTypes()) {
62 nested->DispatchVisit(*this);
63 }
Jooyung Han1f56b702021-02-11 13:16:15 +090064}
65
66// Dumps comment only if its has meaningful tags.
67void DumpVisitor::DumpComments(const AidlCommentable& c) {
68 const auto hidden = c.IsHidden();
69 const auto deprecated = FindDeprecated(c.GetComments());
Jooyung Han8db65942021-03-02 18:14:44 +090070 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 Han1f56b702021-02-11 13:16:15 +090075 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
86void DumpVisitor::DumpAnnotations(const AidlAnnotatable& a) {
87 auto annotations = a.ToString();
88 if (!annotations.empty()) {
89 out << annotations << "\n";
90 }
91}
92
93void DumpVisitor::DumpConstantValue(const AidlTypeSpecifier& type, const AidlConstantValue& c) {
94 out << c.ValueString(type, AidlConstantValueDecorator);
95}
96
97void DumpVisitor::Visit(const AidlInterface& t) {
98 DumpType(t, "interface");
99}
Jooyung Hanae016752022-02-26 21:33:43 +0900100
Jooyung Han1f56b702021-02-11 13:16:15 +0900101void DumpVisitor::Visit(const AidlParcelable& t) {
102 DumpType(t, "parcelable");
103}
Jooyung Hanae016752022-02-26 21:33:43 +0900104
Jooyung Han1f56b702021-02-11 13:16:15 +0900105void DumpVisitor::Visit(const AidlStructuredParcelable& t) {
106 DumpType(t, "parcelable");
107}
Jooyung Hanae016752022-02-26 21:33:43 +0900108
Jooyung Han1f56b702021-02-11 13:16:15 +0900109void DumpVisitor::Visit(const AidlUnionDecl& t) {
110 DumpType(t, "union");
111}
Jooyung Hanae016752022-02-26 21:33:43 +0900112
Jooyung Han1f56b702021-02-11 13:16:15 +0900113void DumpVisitor::Visit(const AidlEnumDeclaration& t) {
Jooyung Hanae016752022-02-26 21:33:43 +0900114 if (!t.IsUserDefined()) {
115 return;
116 }
Jooyung Han1f56b702021-02-11 13:16:15 +0900117 DumpComments(t);
118 DumpAnnotations(t);
119 out << "enum " << t.GetName() << " {\n";
120 out.Indent();
121 for (const auto& e : t.GetEnumerators()) {
Jooyung Han68d698f2022-03-18 15:38:27 +0900122 DumpComments(*e);
Jooyung Han1f56b702021-02-11 13:16:15 +0900123 out << e->GetName() << " = ";
124 DumpConstantValue(t.GetBackingType(), *e->GetValue());
125 out << ",\n";
126 }
127 out.Dedent();
128 out << "}\n";
129}
130
131void DumpVisitor::Visit(const AidlMethod& m) {
Jooyung Hanae016752022-02-26 21:33:43 +0900132 if (!m.IsUserDefined()) {
133 return;
134 }
Jooyung Han1f56b702021-02-11 13:16:15 +0900135 DumpComments(m);
136 out << m.ToString() << ";\n";
137}
Jooyung Hanae016752022-02-26 21:33:43 +0900138
Jooyung Han1f56b702021-02-11 13:16:15 +0900139void DumpVisitor::Visit(const AidlVariableDeclaration& v) {
Jooyung Hanae016752022-02-26 21:33:43 +0900140 if (!v.IsUserDefined()) {
141 return;
142 }
Jooyung Han1f56b702021-02-11 13:16:15 +0900143 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 Hanae016752022-02-26 21:33:43 +0900153
Jooyung Han1f56b702021-02-11 13:16:15 +0900154void DumpVisitor::Visit(const AidlConstantDeclaration& c) {
Jooyung Hanae016752022-02-26 21:33:43 +0900155 if (!c.IsUserDefined()) {
156 return;
157 }
Jooyung Han1f56b702021-02-11 13:16:15 +0900158 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 Han1f56b702021-02-11 13:16:15 +0900166void DumpVisitor::Visit(const AidlTypeSpecifier& t) {
167 out << t.ToString();
168}
169
170static 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 Hanc5b1e082021-02-24 13:47:57 +0900177static 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 Han1f56b702021-02-11 13:16:15 +0900188bool 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 Han252657e2021-02-27 02:51:39 +0900198 if (!options.DumpNoLicense()) {
199 // dump doc comments (license) as well for each type
200 DumpComments(*writer, doc.GetComments());
201 }
Jooyung Han1f56b702021-02-11 13:16:15 +0900202 (*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