blob: 7d3f19dc22e0a740bda2efd63773b41ec103d0a3 [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) {
35 DumpComments(dt);
36 DumpAnnotations(dt);
Jooyung Han71dfec32021-05-23 07:18:58 +090037 out << type << " " << dt.GetName();
38 if (auto generic_type = dt.AsParameterizable(); generic_type && generic_type->IsGeneric()) {
39 out << "<" << Join(generic_type->GetTypeParameters(), ", ") << ">";
40 }
41 out << " {\n";
Jooyung Han1f56b702021-02-11 13:16:15 +090042 out.Indent();
43 DumpMembers(dt);
44 out.Dedent();
45 out << "}\n";
46}
47
48void DumpVisitor::DumpMembers(const AidlDefinedType& dt) {
49 for (const auto& method : dt.GetMethods()) {
50 if (!method->IsUserDefined()) continue;
51 method->DispatchVisit(*this);
52 }
53 for (const auto& field : dt.GetFields()) {
54 field->DispatchVisit(*this);
55 }
56 for (const auto& constdecl : dt.GetConstantDeclarations()) {
57 constdecl->DispatchVisit(*this);
58 }
Jooyung Han2aedb112021-09-29 09:37:59 +090059 for (const auto& nested : dt.GetNestedTypes()) {
60 nested->DispatchVisit(*this);
61 }
Jooyung Han1f56b702021-02-11 13:16:15 +090062}
63
64// Dumps comment only if its has meaningful tags.
65void DumpVisitor::DumpComments(const AidlCommentable& c) {
66 const auto hidden = c.IsHidden();
67 const auto deprecated = FindDeprecated(c.GetComments());
Jooyung Han8db65942021-03-02 18:14:44 +090068 if (hidden && !deprecated) {
69 // to pass --checkapi between the current and the tot in the mainline-prod branch
70 // emit @hide in a legacy dump style
71 out << "/* @hide */\n";
72 } else if (hidden || deprecated) {
Jooyung Han1f56b702021-02-11 13:16:15 +090073 out << "/**\n";
74 if (hidden) {
75 out << " * @hide\n";
76 }
77 if (deprecated) {
78 out << " * @deprecated " << deprecated->note << "\n";
79 }
80 out << " */\n";
81 }
82}
83
84void DumpVisitor::DumpAnnotations(const AidlAnnotatable& a) {
85 auto annotations = a.ToString();
86 if (!annotations.empty()) {
87 out << annotations << "\n";
88 }
89}
90
91void DumpVisitor::DumpConstantValue(const AidlTypeSpecifier& type, const AidlConstantValue& c) {
92 out << c.ValueString(type, AidlConstantValueDecorator);
93}
94
95void DumpVisitor::Visit(const AidlInterface& t) {
96 DumpType(t, "interface");
97}
98void DumpVisitor::Visit(const AidlParcelable& t) {
99 DumpType(t, "parcelable");
100}
101void DumpVisitor::Visit(const AidlStructuredParcelable& t) {
102 DumpType(t, "parcelable");
103}
104void DumpVisitor::Visit(const AidlUnionDecl& t) {
105 DumpType(t, "union");
106}
107void DumpVisitor::Visit(const AidlEnumDeclaration& t) {
108 DumpComments(t);
109 DumpAnnotations(t);
110 out << "enum " << t.GetName() << " {\n";
111 out.Indent();
112 for (const auto& e : t.GetEnumerators()) {
113 out << e->GetName() << " = ";
114 DumpConstantValue(t.GetBackingType(), *e->GetValue());
115 out << ",\n";
116 }
117 out.Dedent();
118 out << "}\n";
119}
120
121void DumpVisitor::Visit(const AidlMethod& m) {
122 DumpComments(m);
123 out << m.ToString() << ";\n";
124}
125void DumpVisitor::Visit(const AidlVariableDeclaration& v) {
126 DumpComments(v);
127 Visit(v.GetType());
128 if (v.IsDefaultUserSpecified()) {
129 out << " " << v.GetName() << " = ";
130 DumpConstantValue(v.GetType(), *v.GetDefaultValue());
131 out << ";\n";
132 } else {
133 out << " " << v.GetName() << ";\n";
134 }
135}
136void DumpVisitor::Visit(const AidlConstantDeclaration& c) {
137 DumpComments(c);
138 out << "const ";
139 Visit(c.GetType());
140 out << " " << c.GetName() << " = ";
141 DumpConstantValue(c.GetType(), c.GetValue());
142 out << ";\n";
143}
144
Jooyung Han1f56b702021-02-11 13:16:15 +0900145void DumpVisitor::Visit(const AidlTypeSpecifier& t) {
146 out << t.ToString();
147}
148
149static string GetApiDumpPathFor(const AidlDefinedType& defined_type, const Options& options) {
150 string package_as_path = Join(Split(defined_type.GetPackage(), "."), OS_PATH_SEPARATOR);
151 AIDL_FATAL_IF(options.OutputDir().empty() || options.OutputDir().back() != '/', defined_type);
152 return options.OutputDir() + package_as_path + OS_PATH_SEPARATOR + defined_type.GetName() +
153 ".aidl";
154}
155
Jooyung Hanc5b1e082021-02-24 13:47:57 +0900156static void DumpComments(CodeWriter& out, const Comments& comments) {
157 bool needs_newline = false;
158 for (const auto& c : comments) {
159 out << c.body;
160 needs_newline = !EndsWith(c.body, "\n");
161 }
162 if (needs_newline) {
163 out << "\n";
164 }
165}
166
Jooyung Han1f56b702021-02-11 13:16:15 +0900167bool dump_api(const Options& options, const IoDelegate& io_delegate) {
168 for (const auto& file : options.InputFiles()) {
169 AidlTypenames typenames;
170 if (internals::load_and_validate_aidl(file, options, io_delegate, &typenames, nullptr) ==
171 AidlError::OK) {
172 const auto& doc = typenames.MainDocument();
173
174 for (const auto& type : doc.DefinedTypes()) {
175 unique_ptr<CodeWriter> writer =
176 io_delegate.GetCodeWriter(GetApiDumpPathFor(*type, options));
Jooyung Han252657e2021-02-27 02:51:39 +0900177 if (!options.DumpNoLicense()) {
178 // dump doc comments (license) as well for each type
179 DumpComments(*writer, doc.GetComments());
180 }
Jooyung Han1f56b702021-02-11 13:16:15 +0900181 (*writer) << kPreamble;
182 if (!type->GetPackage().empty()) {
183 (*writer) << "package " << type->GetPackage() << ";\n";
184 }
185 DumpVisitor visitor(*writer);
186 type->DispatchVisit(visitor);
187 }
188 } else {
189 return false;
190 }
191 }
192 return true;
193}
194
195} // namespace aidl
196} // namespace android