summaryrefslogtreecommitdiff
path: root/stats/stats_log_api_gen/native_writer_vendor.cpp
blob: f0fe0d33630e15241f7b048462dd4d7722490d89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
 * Copyright (C) 2023, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "native_writer_vendor.h"

#include <stdio.h>

#include <set>
#include <string>
#include <vector>

#include "Collation.h"
#include "utils.h"

namespace android {
namespace stats_log_api_gen {

using std::string;

static void write_native_vendor_annotation_header(FILE* out, const string& annotationName,
                                                  const char* indent) {
    fprintf(out, "%s{\n", indent);
    fprintf(out, "%s    Annotation annotation;\n", indent);
    fprintf(out, "%s    annotation.annotationId = %s;\n", indent, annotationName.c_str());
}

static void write_native_vendor_annotation_footer(FILE* out, const char* indent) {
    fprintf(out, "%s    annotations.push_back(std::move(annotation));\n", indent);
    fprintf(out, "%s}\n", indent);
}

static void write_native_vendor_annotation_int(FILE* out, const string& annotationName, int value,
                                               const char* indent) {
    write_native_vendor_annotation_header(out, annotationName, indent);
    fprintf(out, "%sannotation.value.set<AnnotationValue::intValue>(%d);\n", indent, value);
    write_native_vendor_annotation_footer(out, indent);
}

static void write_native_vendor_annotation_int_constant(FILE* out, const string& annotationName,
                                                        const string& constantName,
                                                        const char* indent) {
    write_native_vendor_annotation_header(out, annotationName, indent);
    fprintf(out, "%sannotation.value.set<AnnotationValue::intValue>(%s);\n", indent,
            constantName.c_str());
    write_native_vendor_annotation_footer(out, indent);
}

static void write_native_vendor_annotation_bool(FILE* out, const string& annotationName, bool value,
                                                const char* indent) {
    write_native_vendor_annotation_header(out, annotationName, indent);
    fprintf(out, "%sannotation.value.set<AnnotationValue::boolValue>(%s);\n", indent,
            value ? "true" : "false");
    write_native_vendor_annotation_footer(out, indent);
}

static void write_native_annotations_vendor_for_field(FILE* out, int argIndex,
                                                      const AtomDeclSet& atomDeclSet) {
    if (atomDeclSet.empty()) {
        return;
    }

    const char* indent = "    ";
    const char* indent2 = "        ";
    const char* indent3 = "            ";

    const int valueIndex = argIndex - 2;

    const map<AnnotationId, AnnotationStruct>& ANNOTATION_ID_CONSTANTS =
            get_annotation_id_constants(ANNOTATION_CONSTANT_NAME_VENDOR_NATIVE_PREFIX);

    for (const shared_ptr<AtomDecl>& atomDecl : atomDeclSet) {
        const string atomConstant = make_constant_name(atomDecl->name);
        fprintf(out, "%sif (%s == code) {\n", indent, atomConstant.c_str());

        if (argIndex == ATOM_ID_FIELD_NUMBER) {
            fprintf(out, "%sstd::vector<std::optional<Annotation>> annotations;\n", indent2);
        } else {
            fprintf(out, "%sstd::vector<Annotation> annotations;\n", indent2);
        }

        const AnnotationSet& annotations = atomDecl->fieldNumberToAnnotations.at(argIndex);
        int resetState = -1;
        int defaultState = -1;
        for (const shared_ptr<Annotation>& annotation : annotations) {
            const AnnotationStruct& annotationConstant =
                    ANNOTATION_ID_CONSTANTS.at(annotation->annotationId);
            switch (annotation->type) {
                case ANNOTATION_TYPE_INT:
                    if (ANNOTATION_ID_TRIGGER_STATE_RESET == annotation->annotationId) {
                        resetState = annotation->value.intValue;
                    } else if (ANNOTATION_ID_DEFAULT_STATE == annotation->annotationId) {
                        defaultState = annotation->value.intValue;
                    } else if (ANNOTATION_ID_RESTRICTION_CATEGORY == annotation->annotationId) {
                        fprintf(out, "%s{\n", indent2);
                        write_native_vendor_annotation_int_constant(
                                out, annotationConstant.name,
                                get_restriction_category_str(annotation->value.intValue), indent3);
                        fprintf(out, "%s}\n", indent2);
                    } else {
                        fprintf(out, "%s{\n", indent2);
                        write_native_vendor_annotation_int(out, annotationConstant.name,
                                                           annotation->value.intValue, indent3);
                        fprintf(out, "%s}\n", indent2);
                    }
                    break;
                case ANNOTATION_TYPE_BOOL:
                    fprintf(out, "%s{\n", indent2);
                    write_native_vendor_annotation_bool(out, annotationConstant.name,
                                                        annotation->value.boolValue, indent3);
                    fprintf(out, "%s}\n", indent2);
                    break;
                default:
                    break;
            }
        }
        if (defaultState != -1 && resetState != -1) {
            const AnnotationStruct& annotationConstant =
                    ANNOTATION_ID_CONSTANTS.at(ANNOTATION_ID_TRIGGER_STATE_RESET);
            fprintf(out, "%sif (arg%d == %d) {\n", indent2, argIndex, resetState);
            write_native_vendor_annotation_int(out, annotationConstant.name, defaultState, indent3);
            fprintf(out, "%s}\n", indent2);
        }

        if (argIndex == ATOM_ID_FIELD_NUMBER) {
            fprintf(out, "%satomAnnotations = std::move(annotations);\n", indent2);
        } else {
            fprintf(out, "%sif (annotations.size() > 0) {\n", indent2);
            fprintf(out, "%sAnnotationSet field%dAnnotations;\n", indent3, valueIndex);
            fprintf(out, "%sfield%dAnnotations.valueIndex = %d;\n", indent3, valueIndex,
                    valueIndex);
            fprintf(out, "%sfield%dAnnotations.annotations = std::move(annotations);\n", indent3,
                    valueIndex);
            fprintf(out, "%sfieldsAnnotations.push_back(std::move(field%dAnnotations));\n", indent3,
                    valueIndex);
            fprintf(out, "%s}\n", indent2);
        }
        fprintf(out, "%s}\n", indent);
    }
}

static int write_native_create_vendor_atom_methods(FILE* out,
                                                   const SignatureInfoMap& signatureInfoMap,
                                                   const AtomDecl& attributionDecl) {
    fprintf(out, "\n");
    for (const auto& [signature, fieldNumberToAtomDeclSet] : signatureInfoMap) {
        // TODO (b/264922532): provide vendor implementation to skip arg1 for reverseDomainName
        write_native_method_signature(out, "VendorAtom createVendorAtom(", signature,
                                      attributionDecl, " {", /*isVendorAtomLogging=*/true);

        fprintf(out, "    VendorAtom atom;\n");

        // Write method body.
        fprintf(out, "    atom.atomId = code;\n");
        fprintf(out, "    atom.reverseDomainName = arg1;\n");

        // Exclude first field - which is reverseDomainName
        const int vendorAtomValuesCount = signature.size() - 1;
        fprintf(out, "    vector<VendorAtomValue> values(%d);\n", vendorAtomValuesCount);

        // Use 1-based index to access signature arguments
        for (int argIndex = 2; argIndex <= signature.size(); argIndex++) {
            const java_type_t& argType = signature[argIndex - 1];

            const int atomValueIndex = argIndex - 2;

            switch (argType) {
                case JAVA_TYPE_ATTRIBUTION_CHAIN: {
                    fprintf(stderr, "Found attribution chain - not supported.\n");
                    return 1;
                }
                case JAVA_TYPE_BYTE_ARRAY:
                    fprintf(out,
                            "    "
                            "values[%d].set<VendorAtomValue::byteArrayValue>(arg%d);\n",
                            atomValueIndex, argIndex);
                    break;
                case JAVA_TYPE_BOOLEAN:
                    fprintf(out, "    values[%d].set<VendorAtomValue::boolValue>(arg%d);\n",
                            atomValueIndex, argIndex);
                    break;
                case JAVA_TYPE_INT:
                    [[fallthrough]];
                case JAVA_TYPE_ENUM:
                    fprintf(out, "    values[%d].set<VendorAtomValue::intValue>(arg%d);\n",
                            atomValueIndex, argIndex);
                    break;
                case JAVA_TYPE_FLOAT:
                    fprintf(out, "    values[%d].set<VendorAtomValue::floatValue>(arg%d);\n",
                            atomValueIndex, argIndex);
                    break;
                case JAVA_TYPE_LONG:
                    fprintf(out, "    values[%d].set<VendorAtomValue::longValue>(arg%d);\n",
                            atomValueIndex, argIndex);
                    break;
                case JAVA_TYPE_STRING:
                    fprintf(out, "    values[%d].set<VendorAtomValue::stringValue>(arg%d);\n",
                            atomValueIndex, argIndex);
                    break;
                case JAVA_TYPE_BOOLEAN_ARRAY:
                    fprintf(out, "    values[%d].set<VendorAtomValue::repeatedBoolValue>(arg%d);\n",
                            atomValueIndex, argIndex);
                    break;
                case JAVA_TYPE_INT_ARRAY:
                    [[fallthrough]];
                case JAVA_TYPE_ENUM_ARRAY:
                    fprintf(out, "    values[%d].set<VendorAtomValue::repeatedIntValue>(arg%d);\n",
                            atomValueIndex, argIndex);
                    break;
                case JAVA_TYPE_FLOAT_ARRAY:
                    fprintf(out,
                            "    values[%d].set<VendorAtomValue::repeatedFloatValue>(arg%d);\n",
                            atomValueIndex, argIndex);
                    break;
                case JAVA_TYPE_LONG_ARRAY:
                    fprintf(out, "    values[%d].set<VendorAtomValue::repeatedLongValue>(arg%d);\n",
                            atomValueIndex, argIndex);
                    break;
                case JAVA_TYPE_STRING_ARRAY:
                    fprintf(out, "    {\n");
                    fprintf(out, "    vector<optional<string>> arrayValue(\n");
                    fprintf(out, "        arg%d.begin(), arg%d.end());\n", argIndex, argIndex);
                    fprintf(out,
                            "    "
                            "values[%d].set<VendorAtomValue::repeatedStringValue>(std::move("
                            "arrayValue));\n",
                            atomValueIndex);
                    fprintf(out, "    }\n");
                    break;
                default:
                    // Unsupported types: OBJECT, DOUBLE
                    fprintf(stderr, "Encountered unsupported type.\n");
                    return 1;
            }
        }
        fprintf(out, "    atom.values = std::move(values);\n");  // end method body.

        // check will be there an atom for this signature with atom level annotations
        const AtomDeclSet atomAnnotations =
                get_annotations(ATOM_ID_FIELD_NUMBER, fieldNumberToAtomDeclSet);
        if (atomAnnotations.size()) {
            fprintf(out, "    std::vector<std::optional<Annotation>> atomAnnotations;\n");
            write_native_annotations_vendor_for_field(out, ATOM_ID_FIELD_NUMBER, atomAnnotations);
            fprintf(out, "    if (atomAnnotations.size() > 0) {\n");
            fprintf(out, "        atom.atomAnnotations = std::move(atomAnnotations);\n");
            fprintf(out, "    }\n\n");
        }

        // Create fieldsAnnotations instance only in case if there is an atom fields with annotation
        // for this signature
        bool atomWithFieldsAnnotation = false;
        for (int argIndex = 2; argIndex <= signature.size(); argIndex++) {
            if (get_annotations(argIndex, fieldNumberToAtomDeclSet).size() > 0) {
                atomWithFieldsAnnotation = true;
                break;
            }
        }

        if (atomWithFieldsAnnotation) {
            fprintf(out, "    std::vector<std::optional<AnnotationSet>> fieldsAnnotations;\n");
            for (int argIndex = 2; argIndex <= signature.size(); argIndex++) {
                const AtomDeclSet fieldAnnotations =
                        get_annotations(argIndex, fieldNumberToAtomDeclSet);
                write_native_annotations_vendor_for_field(out, argIndex, fieldAnnotations);
            }
            fprintf(out, "    if (fieldsAnnotations.size() > 0) {\n");
            fprintf(out, "        atom.valuesAnnotations = std::move(fieldsAnnotations);\n");
            fprintf(out, "    }\n\n");
        }

        fprintf(out, "    // elision of copy operations is permitted on return\n");
        fprintf(out, "    return atom;\n");
        fprintf(out, "}\n\n");  // end method.
    }
    return 0;
}

int write_stats_log_cpp_vendor(FILE* out, const Atoms& atoms, const AtomDecl& attributionDecl,
                               const string& cppNamespace, const string& importHeader) {
    // Print prelude
    fprintf(out, "// This file is autogenerated\n");
    fprintf(out, "\n");

    fprintf(out, "#include <%s>\n", importHeader.c_str());
    fprintf(out, "#include <aidl/android/frameworks/stats/VendorAtom.h>\n");

    fprintf(out, "\n");
    write_namespace(out, cppNamespace);
    fprintf(out, "\n");
    fprintf(out, "using namespace aidl::android::frameworks::stats;\n");
    fprintf(out, "using std::make_optional;\n");
    fprintf(out, "using std::optional;\n");
    fprintf(out, "using std::vector;\n");
    fprintf(out, "using std::string;\n");

    const int ret =
            write_native_create_vendor_atom_methods(out, atoms.signatureInfoMap, attributionDecl);
    if (ret != 0) {
        return ret;
    }
    // Print footer
    fprintf(out, "\n");
    write_closing_namespace(out, cppNamespace);

    return 0;
}

int write_stats_log_header_vendor(FILE* out, const Atoms& atoms, const AtomDecl& attributionDecl,
                                  const string& cppNamespace) {
    write_native_header_preamble(out, cppNamespace, false, /*isVendorAtomLogging=*/true);
    write_native_atom_constants(out, atoms, attributionDecl, "createVendorAtom(",
                                /*isVendorAtomLogging=*/true);

    for (AtomDeclSet::const_iterator atomIt = atoms.decls.begin(); atomIt != atoms.decls.end();
         atomIt++) {
        set<string> processedEnums;

        for (vector<AtomField>::const_iterator field = (*atomIt)->fields.begin();
             field != (*atomIt)->fields.end(); field++) {
            if (field->javaType == JAVA_TYPE_ENUM || field->javaType == JAVA_TYPE_ENUM_ARRAY) {
                // There might be N fields with the same enum type
                // avoid duplication definitions
                if (processedEnums.find(field->enumTypeName) != processedEnums.end()) {
                    continue;
                }

                if (processedEnums.empty()) {
                    fprintf(out, "class %s final {\n", (*atomIt)->message.c_str());
                    fprintf(out, "public:\n\n");
                }

                processedEnums.insert(field->enumTypeName);

                fprintf(out, "enum %s {\n", field->enumTypeName.c_str());
                size_t i = 0;
                for (map<int, string>::const_iterator value = field->enumValues.begin();
                     value != field->enumValues.end(); value++) {
                    fprintf(out, "    %s = %d", make_constant_name(value->second).c_str(),
                            value->first);
                    char const* const comma = (i == field->enumValues.size() - 1) ? "" : ",";
                    fprintf(out, "%s\n", comma);
                    i++;
                }

                fprintf(out, "};\n");
            }
        }
        if (!processedEnums.empty()) {
            fprintf(out, "};\n\n");
        }
    }

    fprintf(out, "using ::aidl::android::frameworks::stats::VendorAtom;\n");

    // Print write methods
    fprintf(out, "//\n");
    fprintf(out, "// Write methods\n");
    fprintf(out, "//\n");
    write_native_method_header(out, "VendorAtom createVendorAtom(", atoms.signatureInfoMap,
                               attributionDecl,
                               /*isVendorAtomLogging=*/true);
    fprintf(out, "\n");

    write_native_header_epilogue(out, cppNamespace);

    return 0;
}

}  // namespace stats_log_api_gen
}  // namespace android