blob: 6364929278cd2c7ea6f68ac40a636e2f4b968ea7 [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());
61 if (hidden || deprecated) {
62 out << "/**\n";
63 if (hidden) {
64 out << " * @hide\n";
65 }
66 if (deprecated) {
67 out << " * @deprecated " << deprecated->note << "\n";
68 }
69 out << " */\n";
70 }
71}
72
73void DumpVisitor::DumpAnnotations(const AidlAnnotatable& a) {
74 auto annotations = a.ToString();
75 if (!annotations.empty()) {
76 out << annotations << "\n";
77 }
78}
79
80void DumpVisitor::DumpConstantValue(const AidlTypeSpecifier& type, const AidlConstantValue& c) {
81 out << c.ValueString(type, AidlConstantValueDecorator);
82}
83
84void DumpVisitor::Visit(const AidlInterface& t) {
85 DumpType(t, "interface");
86}
87void DumpVisitor::Visit(const AidlParcelable& t) {
88 DumpType(t, "parcelable");
89}
90void DumpVisitor::Visit(const AidlStructuredParcelable& t) {
91 DumpType(t, "parcelable");
92}
93void DumpVisitor::Visit(const AidlUnionDecl& t) {
94 DumpType(t, "union");
95}
96void DumpVisitor::Visit(const AidlEnumDeclaration& t) {
97 DumpComments(t);
98 DumpAnnotations(t);
99 out << "enum " << t.GetName() << " {\n";
100 out.Indent();
101 for (const auto& e : t.GetEnumerators()) {
102 out << e->GetName() << " = ";
103 DumpConstantValue(t.GetBackingType(), *e->GetValue());
104 out << ",\n";
105 }
106 out.Dedent();
107 out << "}\n";
108}
109
110void DumpVisitor::Visit(const AidlMethod& m) {
111 DumpComments(m);
112 out << m.ToString() << ";\n";
113}
114void DumpVisitor::Visit(const AidlVariableDeclaration& v) {
115 DumpComments(v);
116 Visit(v.GetType());
117 if (v.IsDefaultUserSpecified()) {
118 out << " " << v.GetName() << " = ";
119 DumpConstantValue(v.GetType(), *v.GetDefaultValue());
120 out << ";\n";
121 } else {
122 out << " " << v.GetName() << ";\n";
123 }
124}
125void DumpVisitor::Visit(const AidlConstantDeclaration& c) {
126 DumpComments(c);
127 out << "const ";
128 Visit(c.GetType());
129 out << " " << c.GetName() << " = ";
130 DumpConstantValue(c.GetType(), c.GetValue());
131 out << ";\n";
132}
133
134void DumpVisitor::Visit(const AidlEnumerator& e) {
135 out << e.GetName() << " = ";
136
137 e.GetValue()->DispatchVisit(*this);
138 out << ",\n";
139}
140
141void DumpVisitor::Visit(const AidlTypeSpecifier& t) {
142 out << t.ToString();
143}
144
145static string GetApiDumpPathFor(const AidlDefinedType& defined_type, const Options& options) {
146 string package_as_path = Join(Split(defined_type.GetPackage(), "."), OS_PATH_SEPARATOR);
147 AIDL_FATAL_IF(options.OutputDir().empty() || options.OutputDir().back() != '/', defined_type);
148 return options.OutputDir() + package_as_path + OS_PATH_SEPARATOR + defined_type.GetName() +
149 ".aidl";
150}
151
Jooyung Hanc5b1e082021-02-24 13:47:57 +0900152static void DumpComments(CodeWriter& out, const Comments& comments) {
153 bool needs_newline = false;
154 for (const auto& c : comments) {
155 out << c.body;
156 needs_newline = !EndsWith(c.body, "\n");
157 }
158 if (needs_newline) {
159 out << "\n";
160 }
161}
162
Jooyung Han1f56b702021-02-11 13:16:15 +0900163bool dump_api(const Options& options, const IoDelegate& io_delegate) {
164 for (const auto& file : options.InputFiles()) {
165 AidlTypenames typenames;
166 if (internals::load_and_validate_aidl(file, options, io_delegate, &typenames, nullptr) ==
167 AidlError::OK) {
168 const auto& doc = typenames.MainDocument();
169
170 for (const auto& type : doc.DefinedTypes()) {
171 unique_ptr<CodeWriter> writer =
172 io_delegate.GetCodeWriter(GetApiDumpPathFor(*type, options));
173 // dump doc comments (license) as well for each type
Jooyung Hanc5b1e082021-02-24 13:47:57 +0900174 DumpComments(*writer, doc.GetComments());
Jooyung Han1f56b702021-02-11 13:16:15 +0900175 (*writer) << kPreamble;
176 if (!type->GetPackage().empty()) {
177 (*writer) << "package " << type->GetPackage() << ";\n";
178 }
179 DumpVisitor visitor(*writer);
180 type->DispatchVisit(visitor);
181 }
182 } else {
183 return false;
184 }
185 }
186 return true;
187}
188
189} // namespace aidl
190} // namespace android