blob: ae970ea11e2d16bb4e381a4c0995abb61d376dd5 [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
25using android::base::Join;
26using android::base::Split;
27using std::string;
28using std::unique_ptr;
29
30namespace android {
31namespace aidl {
32
33void DumpVisitor::DumpType(const AidlDefinedType& dt, const string& type) {
34 DumpComments(dt);
35 DumpAnnotations(dt);
36 out << type << " " << dt.GetName() << " {\n";
37 out.Indent();
38 DumpMembers(dt);
39 out.Dedent();
40 out << "}\n";
41}
42
43void DumpVisitor::DumpMembers(const AidlDefinedType& dt) {
44 for (const auto& method : dt.GetMethods()) {
45 if (!method->IsUserDefined()) continue;
46 method->DispatchVisit(*this);
47 }
48 for (const auto& field : dt.GetFields()) {
49 field->DispatchVisit(*this);
50 }
51 for (const auto& constdecl : dt.GetConstantDeclarations()) {
52 constdecl->DispatchVisit(*this);
53 }
54}
55
56// Dumps comment only if its has meaningful tags.
57void DumpVisitor::DumpComments(const AidlCommentable& c) {
58 const auto hidden = c.IsHidden();
59 const auto deprecated = FindDeprecated(c.GetComments());
60 if (hidden || deprecated) {
61 out << "/**\n";
62 if (hidden) {
63 out << " * @hide\n";
64 }
65 if (deprecated) {
66 out << " * @deprecated " << deprecated->note << "\n";
67 }
68 out << " */\n";
69 }
70}
71
72void DumpVisitor::DumpAnnotations(const AidlAnnotatable& a) {
73 auto annotations = a.ToString();
74 if (!annotations.empty()) {
75 out << annotations << "\n";
76 }
77}
78
79void DumpVisitor::DumpConstantValue(const AidlTypeSpecifier& type, const AidlConstantValue& c) {
80 out << c.ValueString(type, AidlConstantValueDecorator);
81}
82
83void DumpVisitor::Visit(const AidlInterface& t) {
84 DumpType(t, "interface");
85}
86void DumpVisitor::Visit(const AidlParcelable& t) {
87 DumpType(t, "parcelable");
88}
89void DumpVisitor::Visit(const AidlStructuredParcelable& t) {
90 DumpType(t, "parcelable");
91}
92void DumpVisitor::Visit(const AidlUnionDecl& t) {
93 DumpType(t, "union");
94}
95void DumpVisitor::Visit(const AidlEnumDeclaration& t) {
96 DumpComments(t);
97 DumpAnnotations(t);
98 out << "enum " << t.GetName() << " {\n";
99 out.Indent();
100 for (const auto& e : t.GetEnumerators()) {
101 out << e->GetName() << " = ";
102 DumpConstantValue(t.GetBackingType(), *e->GetValue());
103 out << ",\n";
104 }
105 out.Dedent();
106 out << "}\n";
107}
108
109void DumpVisitor::Visit(const AidlMethod& m) {
110 DumpComments(m);
111 out << m.ToString() << ";\n";
112}
113void DumpVisitor::Visit(const AidlVariableDeclaration& v) {
114 DumpComments(v);
115 Visit(v.GetType());
116 if (v.IsDefaultUserSpecified()) {
117 out << " " << v.GetName() << " = ";
118 DumpConstantValue(v.GetType(), *v.GetDefaultValue());
119 out << ";\n";
120 } else {
121 out << " " << v.GetName() << ";\n";
122 }
123}
124void DumpVisitor::Visit(const AidlConstantDeclaration& c) {
125 DumpComments(c);
126 out << "const ";
127 Visit(c.GetType());
128 out << " " << c.GetName() << " = ";
129 DumpConstantValue(c.GetType(), c.GetValue());
130 out << ";\n";
131}
132
133void DumpVisitor::Visit(const AidlEnumerator& e) {
134 out << e.GetName() << " = ";
135
136 e.GetValue()->DispatchVisit(*this);
137 out << ",\n";
138}
139
140void DumpVisitor::Visit(const AidlTypeSpecifier& t) {
141 out << t.ToString();
142}
143
144static string GetApiDumpPathFor(const AidlDefinedType& defined_type, const Options& options) {
145 string package_as_path = Join(Split(defined_type.GetPackage(), "."), OS_PATH_SEPARATOR);
146 AIDL_FATAL_IF(options.OutputDir().empty() || options.OutputDir().back() != '/', defined_type);
147 return options.OutputDir() + package_as_path + OS_PATH_SEPARATOR + defined_type.GetName() +
148 ".aidl";
149}
150
151bool dump_api(const Options& options, const IoDelegate& io_delegate) {
152 for (const auto& file : options.InputFiles()) {
153 AidlTypenames typenames;
154 if (internals::load_and_validate_aidl(file, options, io_delegate, &typenames, nullptr) ==
155 AidlError::OK) {
156 const auto& doc = typenames.MainDocument();
157
158 for (const auto& type : doc.DefinedTypes()) {
159 unique_ptr<CodeWriter> writer =
160 io_delegate.GetCodeWriter(GetApiDumpPathFor(*type, options));
161 // dump doc comments (license) as well for each type
162 for (const auto& c : doc.GetComments()) {
163 (*writer) << c.body;
164 }
165 (*writer) << kPreamble;
166 if (!type->GetPackage().empty()) {
167 (*writer) << "package " << type->GetPackage() << ";\n";
168 }
169 DumpVisitor visitor(*writer);
170 type->DispatchVisit(visitor);
171 }
172 } else {
173 return false;
174 }
175 }
176 return true;
177}
178
179} // namespace aidl
180} // namespace android