blob: 6c7ef78fd4787971d5e422cfa0fc683d29cc6311 [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);
37 out << type << " " << dt.GetName() << " {\n";
38 out.Indent();
39 DumpMembers(dt);
40 out.Dedent();
41 out << "}\n";
42}
43
44void DumpVisitor::DumpMembers(const AidlDefinedType& dt) {
45 for (const auto& method : dt.GetMethods()) {
46 if (!method->IsUserDefined()) continue;
47 method->DispatchVisit(*this);
48 }
49 for (const auto& field : dt.GetFields()) {
50 field->DispatchVisit(*this);
51 }
52 for (const auto& constdecl : dt.GetConstantDeclarations()) {
53 constdecl->DispatchVisit(*this);
54 }
55}
56
57// Dumps comment only if its has meaningful tags.
58void DumpVisitor::DumpComments(const AidlCommentable& c) {
59 const auto hidden = c.IsHidden();
60 const auto deprecated = FindDeprecated(c.GetComments());
Jooyung Han8db65942021-03-02 18:14:44 +090061 if (hidden && !deprecated) {
62 // to pass --checkapi between the current and the tot in the mainline-prod branch
63 // emit @hide in a legacy dump style
64 out << "/* @hide */\n";
65 } else if (hidden || deprecated) {
Jooyung Han1f56b702021-02-11 13:16:15 +090066 out << "/**\n";
67 if (hidden) {
68 out << " * @hide\n";
69 }
70 if (deprecated) {
71 out << " * @deprecated " << deprecated->note << "\n";
72 }
73 out << " */\n";
74 }
75}
76
77void DumpVisitor::DumpAnnotations(const AidlAnnotatable& a) {
78 auto annotations = a.ToString();
79 if (!annotations.empty()) {
80 out << annotations << "\n";
81 }
82}
83
84void DumpVisitor::DumpConstantValue(const AidlTypeSpecifier& type, const AidlConstantValue& c) {
85 out << c.ValueString(type, AidlConstantValueDecorator);
86}
87
88void DumpVisitor::Visit(const AidlInterface& t) {
89 DumpType(t, "interface");
90}
91void DumpVisitor::Visit(const AidlParcelable& t) {
92 DumpType(t, "parcelable");
93}
94void DumpVisitor::Visit(const AidlStructuredParcelable& t) {
95 DumpType(t, "parcelable");
96}
97void DumpVisitor::Visit(const AidlUnionDecl& t) {
98 DumpType(t, "union");
99}
100void DumpVisitor::Visit(const AidlEnumDeclaration& t) {
101 DumpComments(t);
102 DumpAnnotations(t);
103 out << "enum " << t.GetName() << " {\n";
104 out.Indent();
105 for (const auto& e : t.GetEnumerators()) {
106 out << e->GetName() << " = ";
107 DumpConstantValue(t.GetBackingType(), *e->GetValue());
108 out << ",\n";
109 }
110 out.Dedent();
111 out << "}\n";
112}
113
114void DumpVisitor::Visit(const AidlMethod& m) {
115 DumpComments(m);
116 out << m.ToString() << ";\n";
117}
118void DumpVisitor::Visit(const AidlVariableDeclaration& v) {
119 DumpComments(v);
120 Visit(v.GetType());
121 if (v.IsDefaultUserSpecified()) {
122 out << " " << v.GetName() << " = ";
123 DumpConstantValue(v.GetType(), *v.GetDefaultValue());
124 out << ";\n";
125 } else {
126 out << " " << v.GetName() << ";\n";
127 }
128}
129void DumpVisitor::Visit(const AidlConstantDeclaration& c) {
130 DumpComments(c);
131 out << "const ";
132 Visit(c.GetType());
133 out << " " << c.GetName() << " = ";
134 DumpConstantValue(c.GetType(), c.GetValue());
135 out << ";\n";
136}
137
138void DumpVisitor::Visit(const AidlEnumerator& e) {
139 out << e.GetName() << " = ";
140
141 e.GetValue()->DispatchVisit(*this);
142 out << ",\n";
143}
144
145void 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