blob: 30f65dca6d446f269813094bf580a434f23495a1 [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()) {
122 out << e->GetName() << " = ";
123 DumpConstantValue(t.GetBackingType(), *e->GetValue());
124 out << ",\n";
125 }
126 out.Dedent();
127 out << "}\n";
128}
129
130void DumpVisitor::Visit(const AidlMethod& m) {
Jooyung Hanae016752022-02-26 21:33:43 +0900131 if (!m.IsUserDefined()) {
132 return;
133 }
Jooyung Han1f56b702021-02-11 13:16:15 +0900134 DumpComments(m);
135 out << m.ToString() << ";\n";
136}
Jooyung Hanae016752022-02-26 21:33:43 +0900137
Jooyung Han1f56b702021-02-11 13:16:15 +0900138void DumpVisitor::Visit(const AidlVariableDeclaration& v) {
Jooyung Hanae016752022-02-26 21:33:43 +0900139 if (!v.IsUserDefined()) {
140 return;
141 }
Jooyung Han1f56b702021-02-11 13:16:15 +0900142 DumpComments(v);
143 Visit(v.GetType());
144 if (v.IsDefaultUserSpecified()) {
145 out << " " << v.GetName() << " = ";
146 DumpConstantValue(v.GetType(), *v.GetDefaultValue());
147 out << ";\n";
148 } else {
149 out << " " << v.GetName() << ";\n";
150 }
151}
Jooyung Hanae016752022-02-26 21:33:43 +0900152
Jooyung Han1f56b702021-02-11 13:16:15 +0900153void DumpVisitor::Visit(const AidlConstantDeclaration& c) {
Jooyung Hanae016752022-02-26 21:33:43 +0900154 if (!c.IsUserDefined()) {
155 return;
156 }
Jooyung Han1f56b702021-02-11 13:16:15 +0900157 DumpComments(c);
158 out << "const ";
159 Visit(c.GetType());
160 out << " " << c.GetName() << " = ";
161 DumpConstantValue(c.GetType(), c.GetValue());
162 out << ";\n";
163}
164
Jooyung Han1f56b702021-02-11 13:16:15 +0900165void DumpVisitor::Visit(const AidlTypeSpecifier& t) {
166 out << t.ToString();
167}
168
169static string GetApiDumpPathFor(const AidlDefinedType& defined_type, const Options& options) {
170 string package_as_path = Join(Split(defined_type.GetPackage(), "."), OS_PATH_SEPARATOR);
171 AIDL_FATAL_IF(options.OutputDir().empty() || options.OutputDir().back() != '/', defined_type);
172 return options.OutputDir() + package_as_path + OS_PATH_SEPARATOR + defined_type.GetName() +
173 ".aidl";
174}
175
Jooyung Hanc5b1e082021-02-24 13:47:57 +0900176static void DumpComments(CodeWriter& out, const Comments& comments) {
177 bool needs_newline = false;
178 for (const auto& c : comments) {
179 out << c.body;
180 needs_newline = !EndsWith(c.body, "\n");
181 }
182 if (needs_newline) {
183 out << "\n";
184 }
185}
186
Jooyung Han1f56b702021-02-11 13:16:15 +0900187bool dump_api(const Options& options, const IoDelegate& io_delegate) {
188 for (const auto& file : options.InputFiles()) {
189 AidlTypenames typenames;
190 if (internals::load_and_validate_aidl(file, options, io_delegate, &typenames, nullptr) ==
191 AidlError::OK) {
192 const auto& doc = typenames.MainDocument();
193
194 for (const auto& type : doc.DefinedTypes()) {
195 unique_ptr<CodeWriter> writer =
196 io_delegate.GetCodeWriter(GetApiDumpPathFor(*type, options));
Jooyung Han252657e2021-02-27 02:51:39 +0900197 if (!options.DumpNoLicense()) {
198 // dump doc comments (license) as well for each type
199 DumpComments(*writer, doc.GetComments());
200 }
Jooyung Han1f56b702021-02-11 13:16:15 +0900201 (*writer) << kPreamble;
202 if (!type->GetPackage().empty()) {
203 (*writer) << "package " << type->GetPackage() << ";\n";
204 }
205 DumpVisitor visitor(*writer);
206 type->DispatchVisit(visitor);
207 }
208 } else {
209 return false;
210 }
211 }
212 return true;
213}
214
215} // namespace aidl
216} // namespace android