blob: 1ea5e11909c61eac88c5003a2dc18c2d8118554b [file] [log] [blame]
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -07001/*
2 * Copyright (C) 2015 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 <iostream>
18#include <sstream>
19
20#include "Generator.h"
21#include "Specification.h"
22#include "Utilities.h"
23
24using namespace std;
25
26// Convert a file name into a string that can be used to guard the include file with #ifdef...
27static string makeGuardString(const string& filename) {
28 string s;
29 s.resize(15 + filename.size());
30 s = "RENDERSCRIPT_";
31 for (char c : filename) {
32 if (c == '.') {
33 s += '_';
34 } else {
35 s += toupper(c);
36 }
37 }
38 return s;
39}
40
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -070041/* Write #ifdef's that ensure that the specified version is present. If we're at the final version,
42 * add a check on a flag that can be set for internal builds. This enables us to keep supporting
43 * old APIs in the runtime code.
44 */
Yang Ni12398d82015-09-18 14:57:07 -070045static void writeVersionGuardStart(GeneratedFile* file, VersionInfo info, unsigned int finalVersion) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070046 if (info.intSize == 32) {
47 *file << "#ifndef __LP64__\n";
48 } else if (info.intSize == 64) {
49 *file << "#ifdef __LP64__\n";
50 }
51
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -070052 ostringstream checkMaxVersion;
53 if (info.maxVersion > 0) {
54 checkMaxVersion << "(";
55 if (info.maxVersion == finalVersion) {
56 checkMaxVersion << "defined(RS_DECLARE_EXPIRED_APIS) || ";
57 }
58 checkMaxVersion << "RS_VERSION <= " << info.maxVersion << ")";
59 }
60
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070061 if (info.minVersion <= 1) {
62 // No minimum
63 if (info.maxVersion > 0) {
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -070064 *file << "#if !defined(RS_VERSION) || " << checkMaxVersion.str() << "\n";
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070065 }
66 } else {
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -070067 *file << "#if (defined(RS_VERSION) && (RS_VERSION >= " << info.minVersion << ")";
68 if (info.maxVersion > 0) {
69 *file << " && " << checkMaxVersion.str();
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070070 }
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -070071 *file << ")\n";
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070072 }
73}
74
75static void writeVersionGuardEnd(GeneratedFile* file, VersionInfo info) {
76 if (info.minVersion > 1 || info.maxVersion != 0) {
77 *file << "#endif\n";
78 }
79 if (info.intSize != 0) {
80 *file << "#endif\n";
81 }
82}
83
84static void writeComment(GeneratedFile* file, const string& name, const string& briefComment,
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -070085 const vector<string>& comment, bool addDeprecatedWarning,
86 bool closeBlock) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070087 if (briefComment.empty() && comment.size() == 0) {
88 return;
89 }
90 *file << "/*\n";
91 if (!briefComment.empty()) {
92 *file << " * " << name << ": " << briefComment << "\n";
93 *file << " *\n";
94 }
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -070095 if (addDeprecatedWarning) {
96 *file << " * DEPRECATED. Do not use.\n";
97 *file << " *\n";
98 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070099 for (size_t ct = 0; ct < comment.size(); ct++) {
100 string s = stripHtml(comment[ct]);
101 s = stringReplace(s, "@", "");
102 if (!s.empty()) {
103 *file << " * " << s << "\n";
104 } else {
105 *file << " *\n";
106 }
107 }
108 if (closeBlock) {
109 *file << " */\n";
110 }
111}
112
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700113static void writeConstantComment(GeneratedFile* file, const Constant& constant) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700114 const string name = constant.getName();
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700115 writeComment(file, name, constant.getSummary(), constant.getDescription(),
116 constant.deprecated(), true);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700117}
118
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700119static void writeConstantSpecification(GeneratedFile* file, const ConstantSpecification& spec) {
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700120 const Constant* constant = spec.getConstant();
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700121 VersionInfo info = spec.getVersionInfo();
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700122 writeVersionGuardStart(file, info, constant->getFinalVersion());
David Grosscb25a812017-02-10 15:10:52 -0800123 *file << "static const " << spec.getType() << " " << constant->getName()
124 << " = " << spec.getValue() << ";\n\n";
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700125 writeVersionGuardEnd(file, info);
126}
127
128static void writeTypeSpecification(GeneratedFile* file, const TypeSpecification& spec) {
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700129 const Type* type = spec.getType();
130 const string& typeName = type->getName();
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700131 const VersionInfo info = spec.getVersionInfo();
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700132 writeVersionGuardStart(file, info, type->getFinalVersion());
Jean-Luc Brouillet36e2be52015-04-30 14:41:24 -0700133
134 const string attribute =
135 makeAttributeTag(spec.getAttribute(), "", type->getDeprecatedApiLevel(),
136 type->getDeprecatedMessage());
137 *file << "typedef ";
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700138 switch (spec.getKind()) {
139 case SIMPLE:
Jean-Luc Brouillet36e2be52015-04-30 14:41:24 -0700140 *file << spec.getSimpleType() << attribute;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700141 break;
Stephen Hinesca51c782015-08-25 23:43:34 -0700142 case RS_OBJECT:
143 *file << "struct " << typeName << " _RS_OBJECT_DECL" << attribute;
144 break;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700145 case ENUM: {
Jean-Luc Brouillet36e2be52015-04-30 14:41:24 -0700146 *file << "enum" << attribute << " ";
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700147 const string name = spec.getEnumName();
148 if (!name.empty()) {
149 *file << name << " ";
150 }
151 *file << "{\n";
152
153 const vector<string>& values = spec.getValues();
154 const vector<string>& valueComments = spec.getValueComments();
155 const size_t last = values.size() - 1;
156 for (size_t i = 0; i <= last; i++) {
157 *file << " " << values[i];
158 if (i != last) {
159 *file << ",";
160 }
161 if (valueComments.size() > i && !valueComments[i].empty()) {
162 *file << " // " << valueComments[i];
163 }
164 *file << "\n";
165 }
Jean-Luc Brouillet36e2be52015-04-30 14:41:24 -0700166 *file << "}";
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700167 break;
168 }
169 case STRUCT: {
Jean-Luc Brouillet36e2be52015-04-30 14:41:24 -0700170 *file << "struct" << attribute << " ";
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700171 const string name = spec.getStructName();
172 if (!name.empty()) {
173 *file << name << " ";
174 }
175 *file << "{\n";
176
177 const vector<string>& fields = spec.getFields();
178 const vector<string>& fieldComments = spec.getFieldComments();
179 for (size_t i = 0; i < fields.size(); i++) {
180 *file << " " << fields[i] << ";";
181 if (fieldComments.size() > i && !fieldComments[i].empty()) {
182 *file << " // " << fieldComments[i];
183 }
184 *file << "\n";
185 }
Jean-Luc Brouillet36e2be52015-04-30 14:41:24 -0700186 *file << "}";
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700187 break;
188 }
189 }
Jean-Luc Brouillet36e2be52015-04-30 14:41:24 -0700190 *file << " " << typeName << ";\n";
191
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700192 writeVersionGuardEnd(file, info);
193 *file << "\n";
194}
195
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700196static void writeTypeComment(GeneratedFile* file, const Type& type) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700197 const string name = type.getName();
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700198 writeComment(file, name, type.getSummary(), type.getDescription(), type.deprecated(), true);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700199}
200
201static void writeFunctionPermutation(GeneratedFile* file, const FunctionSpecification& spec,
202 const FunctionPermutation& permutation) {
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700203 Function* function = spec.getFunction();
204 writeVersionGuardStart(file, spec.getVersionInfo(), function->getFinalVersion());
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700205
206 // Write linkage info.
207 const auto inlineCodeLines = permutation.getInline();
208 if (inlineCodeLines.size() > 0) {
209 *file << "static inline ";
210 } else {
211 *file << "extern ";
212 }
213
214 // Write the return type.
215 auto ret = permutation.getReturn();
216 if (ret) {
217 *file << ret->rsType;
218 } else {
219 *file << "void";
220 }
221
Yang Ni12398d82015-09-18 14:57:07 -0700222 *file << makeAttributeTag(spec.getAttribute(), spec.isOverloadable() ? "overloadable" : "",
Jean-Luc Brouillet36e2be52015-04-30 14:41:24 -0700223 function->getDeprecatedApiLevel(), function->getDeprecatedMessage());
224 *file << "\n";
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700225
226 // Write the function name.
227 *file << " " << permutation.getName() << "(";
228 const int offset = 4 + permutation.getName().size() + 1; // Size of above
229
230 // Write the arguments. We wrap on mulitple lines if a line gets too long.
231 int charsOnLine = offset;
232 bool hasGenerated = false;
233 for (auto p : permutation.getParams()) {
234 if (hasGenerated) {
235 *file << ",";
236 charsOnLine++;
237 }
238 ostringstream ps;
239 ps << p->rsType;
240 if (p->isOutParameter) {
241 ps << "*";
242 }
Yang Nifab69472015-11-10 15:41:03 -0800243 if (!p->specName.empty() && p->rsType != "...") {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700244 ps << " " << p->specName;
245 }
246 const string s = ps.str();
247 if (charsOnLine + s.size() >= 100) {
248 *file << "\n" << string(offset, ' ');
249 charsOnLine = offset;
250 } else if (hasGenerated) {
251 *file << " ";
252 charsOnLine++;
253 }
254 *file << s;
255 charsOnLine += s.size();
256 hasGenerated = true;
257 }
258 // In C, if no parameters, we need to output void, e.g. fn(void).
259 if (!hasGenerated) {
260 *file << "void";
261 }
262 *file << ")";
263
264 // Write the inline code, if any.
265 if (inlineCodeLines.size() > 0) {
266 *file << " {\n";
267 for (size_t ct = 0; ct < inlineCodeLines.size(); ct++) {
268 if (inlineCodeLines[ct].empty()) {
269 *file << "\n";
270 } else {
271 *file << " " << inlineCodeLines[ct] << "\n";
272 }
273 }
274 *file << "}\n";
275 } else {
276 *file << ";\n";
277 }
278
279 writeVersionGuardEnd(file, spec.getVersionInfo());
280 *file << "\n";
281}
282
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700283static void writeFunctionComment(GeneratedFile* file, const Function& function) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700284 // Write the generic documentation.
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700285 writeComment(file, function.getName(), function.getSummary(), function.getDescription(),
286 function.deprecated(), false);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700287
288 // Comment the parameters.
289 if (function.someParametersAreDocumented()) {
290 *file << " *\n";
291 *file << " * Parameters:\n";
292 for (auto p : function.getParameters()) {
293 if (!p->documentation.empty()) {
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700294 *file << " * " << p->name << ": " << p->documentation << "\n";
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700295 }
296 }
297 }
298
299 // Comment the return type.
300 const string returnDoc = function.getReturnDocumentation();
301 if (!returnDoc.empty()) {
302 *file << " *\n";
303 *file << " * Returns: " << returnDoc << "\n";
304 }
305
306 *file << " */\n";
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700307}
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700308
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700309static void writeFunctionSpecification(GeneratedFile* file, const FunctionSpecification& spec) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700310 // Write all the variants.
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700311 for (auto permutation : spec.getPermutations()) {
312 writeFunctionPermutation(file, spec, *permutation);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700313 }
314}
315
Jean-Luc Brouillet62e09932015-03-22 11:14:07 -0700316static bool writeHeaderFile(const string& directory, const SpecFile& specFile) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700317 const string headerFileName = specFile.getHeaderFileName();
318
319 // We generate one header file for each spec file.
320 GeneratedFile file;
Jean-Luc Brouillet62e09932015-03-22 11:14:07 -0700321 if (!file.start(directory, headerFileName)) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700322 return false;
323 }
324
325 // Write the comments that start the file.
326 file.writeNotices();
327 writeComment(&file, headerFileName, specFile.getBriefDescription(),
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700328 specFile.getFullDescription(), false, true);
329 file << "\n";
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700330
331 // Write the ifndef that prevents the file from being included twice.
332 const string guard = makeGuardString(headerFileName);
333 file << "#ifndef " << guard << "\n";
334 file << "#define " << guard << "\n\n";
335
336 // Add lines that need to be put in "as is".
337 if (specFile.getVerbatimInclude().size() > 0) {
338 for (auto s : specFile.getVerbatimInclude()) {
339 file << s << "\n";
340 }
341 file << "\n";
342 }
343
344 /* Write the constants, types, and functions in the same order as
345 * encountered in the spec file.
346 */
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700347 set<Constant*> documentedConstants;
348 for (auto spec : specFile.getConstantSpecifications()) {
349 Constant* constant = spec->getConstant();
350 if (documentedConstants.find(constant) == documentedConstants.end()) {
351 documentedConstants.insert(constant);
352 writeConstantComment(&file, *constant);
353 }
354 writeConstantSpecification(&file, *spec);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700355 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700356 set<Type*> documentedTypes;
357 for (auto spec : specFile.getTypeSpecifications()) {
358 Type* type = spec->getType();
359 if (documentedTypes.find(type) == documentedTypes.end()) {
360 documentedTypes.insert(type);
361 writeTypeComment(&file, *type);
362 }
363 writeTypeSpecification(&file, *spec);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700364 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700365
366 set<Function*> documentedFunctions;
367 for (auto spec : specFile.getFunctionSpecifications()) {
Yang Ni12398d82015-09-18 14:57:07 -0700368 // Do not include internal APIs in the header files.
369 if (spec->isInternal()) {
370 continue;
371 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700372 Function* function = spec->getFunction();
373 if (documentedFunctions.find(function) == documentedFunctions.end()) {
374 documentedFunctions.insert(function);
375 writeFunctionComment(&file, *function);
376 }
377 writeFunctionSpecification(&file, *spec);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700378 }
379
380 file << "#endif // " << guard << "\n";
381 file.close();
382 return true;
383}
384
Jean-Luc Brouillet66fea242015-04-09 16:47:59 -0700385bool generateHeaderFiles(const string& directory) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700386 bool success = true;
387 for (auto specFile : systemSpecification.getSpecFiles()) {
Jean-Luc Brouillet62e09932015-03-22 11:14:07 -0700388 if (!writeHeaderFile(directory, *specFile)) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700389 success = false;
390 }
391 }
392 return success;
393}