blob: e9629d740954ecde0e1f7957cd92b56b11f3a35a [file] [log] [blame]
Lalit Maganti16117cc2022-12-21 15:33:21 +00001# Copyright (C) 2022 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from typing import List
16from typing import Optional
17
18from python.generators.trace_processor_table.public import Alias
Lalit Maganti16117cc2022-12-21 15:33:21 +000019from python.generators.trace_processor_table.public import ColumnFlag
Lalit Maganti746fc2b2023-03-24 13:21:24 +000020from python.generators.trace_processor_table.util import ParsedTable
21from python.generators.trace_processor_table.util import ParsedColumn
Lalit Maganti3df8a7e2023-04-25 14:18:17 +010022from python.generators.trace_processor_table.util import parse_type
23from python.generators.trace_processor_table.util import typed_column_type
Lalit Maganti16117cc2022-12-21 15:33:21 +000024
25
26class ColumnSerializer:
27 """Functions for serializing a single Column in a table into C++."""
28
Lalit Maganti746fc2b2023-03-24 13:21:24 +000029 def __init__(self, table: ParsedTable, column: ParsedColumn, col_index: int):
Lalit Maganti16117cc2022-12-21 15:33:21 +000030 self.col_index = col_index
Lalit Maganti746fc2b2023-03-24 13:21:24 +000031 self.parsed_col = column
32 self.col = self.parsed_col.column
Lalit Maganti16117cc2022-12-21 15:33:21 +000033 self.name = self.col.name
34 self.flags = self.col.flags
Lalit Maganti3df8a7e2023-04-25 14:18:17 +010035 self.typed_column_type = typed_column_type(table.table, self.parsed_col)
36 self.cpp_type = parse_type(table.table,
37 self.col.type).cpp_type_with_optionality()
Lalit Maganti746fc2b2023-03-24 13:21:24 +000038
39 self.is_implicit_id = self.parsed_col.is_implicit_id
40 self.is_implicit_type = self.parsed_col.is_implicit_type
41 self.is_ancestor = self.parsed_col.is_ancestor
Lalit Maganti16117cc2022-12-21 15:33:21 +000042
43 def colindex(self) -> str:
44 return f' static constexpr uint32_t {self.name} = {self.col_index};'
45
46 def coltype_enum(self) -> str:
47 return f' using {self.name} = {self.typed_column_type};'
48
49 def row_field(self) -> Optional[str]:
Lalit Maganti746fc2b2023-03-24 13:21:24 +000050 if self.is_implicit_id or self.is_implicit_type:
Lalit Maganti16117cc2022-12-21 15:33:21 +000051 return None
Lalit Maganti746fc2b2023-03-24 13:21:24 +000052 if self.is_ancestor:
Lalit Maganti157d7102023-03-17 20:07:41 +000053 return None
Lalit Maganti16117cc2022-12-21 15:33:21 +000054 return f' {self.cpp_type} {self.name};'
55
56 def row_param(self) -> Optional[str]:
Lalit Maganti746fc2b2023-03-24 13:21:24 +000057 if self.is_implicit_id or self.is_implicit_type:
Lalit Maganti16117cc2022-12-21 15:33:21 +000058 return None
59 return f'{self.cpp_type} in_{self.name} = {{}}'
60
Lalit Maganti157d7102023-03-17 20:07:41 +000061 def parent_row_initializer(self) -> Optional[str]:
Lalit Maganti746fc2b2023-03-24 13:21:24 +000062 if self.is_implicit_id or self.is_implicit_type:
Lalit Maganti157d7102023-03-17 20:07:41 +000063 return None
Lalit Maganti746fc2b2023-03-24 13:21:24 +000064 if not self.is_ancestor:
Lalit Maganti157d7102023-03-17 20:07:41 +000065 return None
66 return f'std::move(in_{self.name})'
67
Lalit Maganti16117cc2022-12-21 15:33:21 +000068 def row_initializer(self) -> Optional[str]:
Lalit Maganti746fc2b2023-03-24 13:21:24 +000069 if self.is_implicit_id or self.is_implicit_type:
Lalit Maganti16117cc2022-12-21 15:33:21 +000070 return None
Lalit Maganti746fc2b2023-03-24 13:21:24 +000071 if self.is_ancestor:
Lalit Maganti157d7102023-03-17 20:07:41 +000072 return None
Lalit Maganti16117cc2022-12-21 15:33:21 +000073 return f'{self.name}(std::move(in_{self.name}))'
74
Lalit Magantie2a74562023-03-16 18:07:25 +000075 def const_row_ref_getter(self) -> Optional[str]:
76 return f'''ColumnType::{self.name}::type {self.name}() const {{
77 return table_->{self.name}()[row_number_];
78 }}'''
79
80 def row_ref_getter(self) -> Optional[str]:
Lalit Maganti746fc2b2023-03-24 13:21:24 +000081 if self.is_implicit_id or self.is_implicit_type:
Lalit Magantie2a74562023-03-16 18:07:25 +000082 return None
83 return f'''void set_{self.name}(
84 ColumnType::{self.name}::non_optional_type v) {{
85 return mutable_table()->mutable_{self.name}()->Set(row_number_, v);
86 }}'''
87
Lalit Maganti16117cc2022-12-21 15:33:21 +000088 def flag(self) -> Optional[str]:
Lalit Maganti746fc2b2023-03-24 13:21:24 +000089 if self.is_implicit_id or self.is_implicit_type:
Lalit Maganti16117cc2022-12-21 15:33:21 +000090 return None
Lalit Maganti746fc2b2023-03-24 13:21:24 +000091 if self.is_ancestor:
Lalit Maganti157d7102023-03-17 20:07:41 +000092 return None
Lalit Maganti16117cc2022-12-21 15:33:21 +000093 default = f'ColumnType::{self.name}::default_flags()'
94 if self.flags == ColumnFlag.NONE:
95 flags = default
96 else:
97 flags = f'static_cast<uint32_t>({to_cpp_flags(self.flags)}) | {default}'
98 return f'''
Lalit Maganti157d7102023-03-17 20:07:41 +000099 static constexpr uint32_t {self.name} = {flags};
Lalit Maganti16117cc2022-12-21 15:33:21 +0000100 '''
101
102 def storage_init(self) -> Optional[str]:
Lalit Maganti746fc2b2023-03-24 13:21:24 +0000103 if self.is_implicit_id or self.is_implicit_type:
Lalit Maganti16117cc2022-12-21 15:33:21 +0000104 return None
Lalit Maganti746fc2b2023-03-24 13:21:24 +0000105 if self.is_ancestor:
Lalit Maganti157d7102023-03-17 20:07:41 +0000106 return None
Lalit Maganti16117cc2022-12-21 15:33:21 +0000107
108 storage = f'ColumnStorage<ColumnType::{self.name}::stored_type>'
Lalit Magantie1d4d442023-03-23 21:43:33 +0000109 dense = str(ColumnFlag.DENSE in self.flags).lower()
110 return f'''{self.name}_({storage}::Create<{dense}>())'''
Lalit Maganti16117cc2022-12-21 15:33:21 +0000111
112 def column_init(self) -> Optional[str]:
Lalit Maganti746fc2b2023-03-24 13:21:24 +0000113 if self.is_implicit_id or self.is_implicit_type:
Lalit Maganti16117cc2022-12-21 15:33:21 +0000114 return None
Lalit Maganti746fc2b2023-03-24 13:21:24 +0000115 if self.is_ancestor:
Lalit Maganti157d7102023-03-17 20:07:41 +0000116 return None
Lalit Maganti16117cc2022-12-21 15:33:21 +0000117 return f'''
118 columns_.emplace_back("{self.name}", &{self.name}_, ColumnFlag::{self.name},
119 this, static_cast<uint32_t>(columns_.size()),
Lalit Maganti0c998102023-04-24 12:30:49 +0100120 olay_idx);
Lalit Maganti16117cc2022-12-21 15:33:21 +0000121 '''
122
123 def shrink_to_fit(self) -> Optional[str]:
Lalit Maganti746fc2b2023-03-24 13:21:24 +0000124 if self.is_implicit_id:
Lalit Maganti16117cc2022-12-21 15:33:21 +0000125 return None
Lalit Maganti746fc2b2023-03-24 13:21:24 +0000126 if self.is_ancestor:
Lalit Maganti157d7102023-03-17 20:07:41 +0000127 return None
Lalit Maganti16117cc2022-12-21 15:33:21 +0000128 return f' {self.name}_.ShrinkToFit();'
129
130 def append(self) -> Optional[str]:
Lalit Maganti746fc2b2023-03-24 13:21:24 +0000131 if self.is_implicit_id or self.is_implicit_type:
Lalit Maganti16117cc2022-12-21 15:33:21 +0000132 return None
Lalit Maganti746fc2b2023-03-24 13:21:24 +0000133 if self.is_ancestor:
Lalit Maganti157d7102023-03-17 20:07:41 +0000134 return None
Lalit Maganti16117cc2022-12-21 15:33:21 +0000135 return f' mutable_{self.name}()->Append(std::move(row.{self.name}));'
136
137 def accessor(self) -> Optional[str]:
138 inner = f'columns_[ColumnIndex::{self.name}]'
139 return f'''
140 const {self.typed_column_type}& {self.name}() const {{
141 return static_cast<const ColumnType::{self.name}&>({inner});
142 }}
143 '''
144
145 def mutable_accessor(self) -> Optional[str]:
Lalit Maganti746fc2b2023-03-24 13:21:24 +0000146 if self.is_implicit_id or self.is_implicit_type:
Lalit Maganti16117cc2022-12-21 15:33:21 +0000147 return None
148 return f'''
149 {self.typed_column_type}* mutable_{self.name}() {{
150 return static_cast<ColumnType::{self.name}*>(
151 &columns_[ColumnIndex::{self.name}]);
152 }}
153 '''
154
155 def storage(self) -> Optional[str]:
Lalit Maganti746fc2b2023-03-24 13:21:24 +0000156 if self.is_implicit_id or self.is_implicit_type:
Lalit Maganti16117cc2022-12-21 15:33:21 +0000157 return None
Lalit Maganti746fc2b2023-03-24 13:21:24 +0000158 if self.is_ancestor:
Lalit Maganti157d7102023-03-17 20:07:41 +0000159 return None
Lalit Maganti16117cc2022-12-21 15:33:21 +0000160 name = self.name
161 return f' ColumnStorage<ColumnType::{name}::stored_type> {name}_;'
162
Lalit Magantie1d4d442023-03-23 21:43:33 +0000163 def iterator_getter(self) -> Optional[str]:
164 name = self.name
165 return f'''
166 ColumnType::{self.name}::type {name}() const {{
167 const auto& col = table_->{name}();
168 return col.GetAtIdx(its_[col.overlay_index()].index());
169 }}
170 '''
171
172 def iterator_setter(self) -> Optional[str]:
173 if self.is_implicit_id or self.is_implicit_type:
174 return None
175 return f'''
176 void set_{self.name}(ColumnType::{self.name}::non_optional_type v) {{
177 auto* col = mutable_table_->mutable_{self.name}();
178 col->SetAtIdx(its_[col->overlay_index()].index(), v);
179 }}
180 '''
181
182 def static_schema(self) -> Optional[str]:
183 if self.is_implicit_id or self.is_implicit_type:
184 return None
185 return f'''
Lalit Maganti3df8a7e2023-04-25 14:18:17 +0100186 schema.columns.emplace_back(Table::Schema::Column{{
187 "{self.name}", ColumnType::{self.name}::SqlValueType(), false,
188 {str(ColumnFlag.SORTED in self.flags).lower()},
189 {str(ColumnFlag.HIDDEN in self.flags).lower()},
190 {str(ColumnFlag.SET_ID in self.flags).lower()}}});
Lalit Magantie1d4d442023-03-23 21:43:33 +0000191 '''
192
193 def row_eq(self) -> Optional[str]:
194 if self.is_implicit_id or self.is_implicit_type:
195 return None
196 return f'ColumnType::{self.name}::Equals({self.name}, other.{self.name})'
197
Lalit Maganti3df8a7e2023-04-25 14:18:17 +0100198 def extend_parent_param(self) -> Optional[str]:
199 if self.is_implicit_id or self.is_implicit_type:
200 return None
201 if self.is_ancestor:
202 return None
203 return f'ColumnStorage<ColumnType::{self.name}::stored_type> {self.name}'
204
205 def extend_parent_param_arg(self) -> Optional[str]:
206 if self.is_implicit_id or self.is_implicit_type:
207 return None
208 if self.is_ancestor:
209 return None
210 return f'std::move({self.name})'
211
212 def static_assert_flags(self) -> Optional[str]:
213 if self.is_implicit_id or self.is_implicit_type:
214 return None
215 if self.is_ancestor:
216 return None
217 return f'''
218 static_assert(
219 Column::IsFlagsAndTypeValid<ColumnType::{self.name}::stored_type>(
220 ColumnFlag::{self.name}),
221 "Column type and flag combination is not valid");
222 '''
223
224 def extend_nullable_vector(self) -> Optional[str]:
225 if self.is_implicit_id or self.is_implicit_type:
226 return None
227 if self.is_ancestor:
228 return None
229 return f'''
230 PERFETTO_DCHECK({self.name}.size() == parent_overlay.size());
231 {self.name}_ = std::move({self.name});
232 '''
233
Lalit Maganti16117cc2022-12-21 15:33:21 +0000234
235class TableSerializer(object):
236 """Functions for seralizing a single Table into C++."""
237
Lalit Maganti746fc2b2023-03-24 13:21:24 +0000238 def __init__(self, parsed: ParsedTable):
239 self.table = parsed.table
240 self.table_name = parsed.table.class_name
Lalit Maganti157d7102023-03-17 20:07:41 +0000241 self.column_serializers = []
242
Lalit Maganti746fc2b2023-03-24 13:21:24 +0000243 if parsed.table.parent:
244 self.parent_class_name = parsed.table.parent.class_name
Lalit Maganti157d7102023-03-17 20:07:41 +0000245 else:
246 self.parent_class_name = 'macros_internal::RootParentTable'
247
Lalit Maganti746fc2b2023-03-24 13:21:24 +0000248 self.column_serializers = []
249 for c in parsed.columns:
250 # Aliases should be ignored as they are handled in SQL currently.
251 if isinstance(c.column.type, Alias):
252 continue
253 self.column_serializers.append(
254 ColumnSerializer(parsed, c, len(self.column_serializers)))
Lalit Maganti16117cc2022-12-21 15:33:21 +0000255
256 def foreach_col(self, serialize_fn, delimiter='\n') -> str:
257 lines = []
258 for c in self.column_serializers:
259 serialized = serialize_fn(c)
260 if serialized:
261 lines.append(serialized.lstrip('\n').rstrip())
262 return delimiter.join(lines).strip()
263
264 def id_defn(self) -> str:
Lalit Maganti157d7102023-03-17 20:07:41 +0000265 if self.table.parent:
266 return f'''
267 using Id = {self.table.parent.class_name}::Id;
268 '''
Lalit Maganti16117cc2022-12-21 15:33:21 +0000269 return '''
270 struct Id : public BaseId {
271 Id() = default;
272 explicit constexpr Id(uint32_t v) : BaseId(v) {}
273 };
274 static_assert(std::is_trivially_destructible<Id>::value,
275 "Inheritance used without trivial destruction");
276 '''
277
278 def row_struct(self) -> str:
279 param = self.foreach_col(
280 ColumnSerializer.row_param, delimiter=',\n ')
Lalit Maganti157d7102023-03-17 20:07:41 +0000281 parent_row_init = self.foreach_col(
282 ColumnSerializer.parent_row_initializer, delimiter=', ')
Lalit Maganti16117cc2022-12-21 15:33:21 +0000283 row_init = self.foreach_col(
284 ColumnSerializer.row_initializer, delimiter=',\n ')
Lalit Maganti0c998102023-04-24 12:30:49 +0100285 parent_separator = ',' if row_init else ''
Lalit Magantie1d4d442023-03-23 21:43:33 +0000286 row_eq = self.foreach_col(ColumnSerializer.row_eq, delimiter=' &&\n ')
Lalit Maganti16117cc2022-12-21 15:33:21 +0000287 return f'''
Lalit Maganti157d7102023-03-17 20:07:41 +0000288 struct Row : public {self.parent_class_name}::Row {{
Lalit Magantie2a74562023-03-16 18:07:25 +0000289 Row({param},
290 std::nullptr_t = nullptr)
Lalit Maganti0c998102023-04-24 12:30:49 +0100291 : {self.parent_class_name}::Row({parent_row_init}){parent_separator}
Lalit Maganti16117cc2022-12-21 15:33:21 +0000292 {row_init} {{
293 type_ = "{self.table.sql_name}";
294 }}
295 {self.foreach_col(ColumnSerializer.row_field)}
Lalit Magantie1d4d442023-03-23 21:43:33 +0000296
297 bool operator==(const {self.table_name}::Row& other) const {{
298 return type() == other.type() && {row_eq};
299 }}
Lalit Maganti16117cc2022-12-21 15:33:21 +0000300 }};
301 '''
302
Lalit Magantie2a74562023-03-16 18:07:25 +0000303 def const_row_reference_struct(self) -> str:
304 row_ref_getters = self.foreach_col(
305 ColumnSerializer.const_row_ref_getter, delimiter='\n ')
306 return f'''
307 class ConstRowReference : public macros_internal::AbstractConstRowReference<
308 {self.table_name}, RowNumber> {{
309 public:
310 ConstRowReference(const {self.table_name}* table, uint32_t row_number)
311 : AbstractConstRowReference(table, row_number) {{}}
312
313 {row_ref_getters}
314 }};
315 static_assert(std::is_trivially_destructible<ConstRowReference>::value,
316 "Inheritance used without trivial destruction");
317 '''
318
319 def row_reference_struct(self) -> str:
320 row_ref_getters = self.foreach_col(
321 ColumnSerializer.row_ref_getter, delimiter='\n ')
322 return f'''
323 class RowReference : public ConstRowReference {{
324 public:
325 RowReference(const {self.table_name}* table, uint32_t row_number)
326 : ConstRowReference(table, row_number) {{}}
327
328 {row_ref_getters}
329
330 private:
331 {self.table_name}* mutable_table() const {{
332 return const_cast<{self.table_name}*>(table_);
333 }}
334 }};
335 static_assert(std::is_trivially_destructible<RowReference>::value,
336 "Inheritance used without trivial destruction");
337 '''
338
Lalit Maganti16117cc2022-12-21 15:33:21 +0000339 def constructor(self) -> str:
Lalit Maganti0c998102023-04-24 12:30:49 +0100340 storage_init = self.foreach_col(
Lalit Maganti16117cc2022-12-21 15:33:21 +0000341 ColumnSerializer.storage_init, delimiter=',\n ')
Lalit Maganti157d7102023-03-17 20:07:41 +0000342 if self.table.parent:
343 parent_param = f', {self.parent_class_name}* parent'
344 parent_arg = 'parent'
Lalit Maganti0c998102023-04-24 12:30:49 +0100345 parent_init = 'parent_(parent)' + (', ' if storage_init else '')
Lalit Maganti157d7102023-03-17 20:07:41 +0000346 else:
347 parent_param = ''
348 parent_arg = 'nullptr'
349 parent_init = ''
Lalit Maganti0c998102023-04-24 12:30:49 +0100350 col_init = self.foreach_col(ColumnSerializer.column_init)
351 if col_init:
352 olay = 'uint32_t olay_idx = static_cast<uint32_t>(overlays_.size()) - 1;'
353 else:
354 olay = ''
Lalit Maganti16117cc2022-12-21 15:33:21 +0000355 return f'''
Lalit Maganti157d7102023-03-17 20:07:41 +0000356 explicit {self.table_name}(StringPool* pool{parent_param})
357 : macros_internal::MacroTable(pool, {parent_arg}),
Lalit Maganti0c998102023-04-24 12:30:49 +0100358 {parent_init}{storage_init} {{
Lalit Maganti3df8a7e2023-04-25 14:18:17 +0100359 {self.foreach_col(ColumnSerializer.static_assert_flags)}
Lalit Maganti0c998102023-04-24 12:30:49 +0100360 {olay}
361 {col_init}
Lalit Maganti16117cc2022-12-21 15:33:21 +0000362 }}
363 '''
364
Lalit Maganti157d7102023-03-17 20:07:41 +0000365 def parent_field(self) -> str:
366 if self.table.parent:
367 return f'''
368 {self.parent_class_name}* parent_ = nullptr;
369 '''
370 return ''
371
372 def insert_common(self) -> str:
373 if self.table.parent:
374 return '''
375 Id id = Id{parent_->Insert(row).id};
376 UpdateOverlaysAfterParentInsert();
377 '''
378 return '''
379 Id id = Id{row_number};
380 type_.Append(string_pool_->InternString(row.type()));
381 '''
382
Lalit Magantie1d4d442023-03-23 21:43:33 +0000383 def const_iterator(self) -> str:
384 iterator_getters = self.foreach_col(
385 ColumnSerializer.iterator_getter, delimiter='\n')
386 return f'''
387 class ConstIterator;
388 class ConstIterator : public macros_internal::AbstractConstIterator<
389 ConstIterator, {self.table_name}, RowNumber, ConstRowReference> {{
390 public:
391 {iterator_getters}
392
393 protected:
394 explicit ConstIterator(const {self.table_name}* table,
395 std::vector<ColumnStorageOverlay> overlays)
396 : AbstractConstIterator(table, std::move(overlays)) {{}}
397
398 uint32_t CurrentRowNumber() const {{
399 return its_.back().index();
400 }}
401
402 private:
403 friend class {self.table_name};
Daniele Di Proiettoe53bcd32023-08-03 17:24:26 +0000404 friend class macros_internal::AbstractConstIterator<
405 ConstIterator, {self.table_name}, RowNumber, ConstRowReference>;
Lalit Magantie1d4d442023-03-23 21:43:33 +0000406 }};
407 '''
408
409 def iterator(self) -> str:
410 iterator_setters = self.foreach_col(
411 ColumnSerializer.iterator_setter, delimiter='\n')
412 return f'''
413 class Iterator : public ConstIterator {{
414 public:
415 {iterator_setters}
416
417 RowReference row_reference() const {{
418 return RowReference(mutable_table_, CurrentRowNumber());
419 }}
420
421 private:
422 friend class {self.table_name};
423
424 explicit Iterator({self.table_name}* table,
425 std::vector<ColumnStorageOverlay> overlays)
426 : ConstIterator(table, std::move(overlays)),
427 mutable_table_(table) {{}}
428
429 {self.table_name}* mutable_table_ = nullptr;
430 }};
431 '''
432
Lalit Maganti3df8a7e2023-04-25 14:18:17 +0100433 def extend(self) -> str:
434 if not self.table.parent:
435 return ''
436 params = self.foreach_col(
437 ColumnSerializer.extend_parent_param, delimiter='\n, ')
438 args = self.foreach_col(
439 ColumnSerializer.extend_parent_param_arg, delimiter=', ')
440 delim = ',' if params else ''
441 return f'''
442 static std::unique_ptr<Table> ExtendParent(
443 const {self.parent_class_name}& parent{delim}
444 {params}) {{
445 return std::unique_ptr<Table>(new {self.table_name}(
446 parent.string_pool(), parent, RowMap(0, parent.row_count()){delim}
447 {args}));
448 }}
449
450 static std::unique_ptr<Table> SelectAndExtendParent(
451 const {self.parent_class_name}& parent,
452 std::vector<{self.parent_class_name}::RowNumber> parent_overlay{delim}
453 {params}) {{
454 std::vector<uint32_t> prs_untyped(parent_overlay.size());
455 for (uint32_t i = 0; i < parent_overlay.size(); ++i) {{
456 prs_untyped[i] = parent_overlay[i].row_number();
457 }}
458 return std::unique_ptr<Table>(new {self.table_name}(
459 parent.string_pool(), parent, RowMap(std::move(prs_untyped)){delim}
460 {args}));
461 }}
462 '''
463
464 def extend_constructor(self) -> str:
465 if not self.table.parent:
466 return ''
467 params = self.foreach_col(
468 ColumnSerializer.extend_parent_param, delimiter='\n, ')
469 if params:
470 olay = 'uint32_t olay_idx = static_cast<uint32_t>(overlays_.size()) - 1;'
471 else:
472 olay = ''
473 return f'''
474 {self.table_name}(StringPool* pool,
475 const {self.parent_class_name}& parent,
476 const RowMap& parent_overlay{',' if params else ''}
477 {params})
478 : macros_internal::MacroTable(pool, parent, parent_overlay) {{
479 {self.foreach_col(ColumnSerializer.static_assert_flags)}
480 {self.foreach_col(ColumnSerializer.extend_nullable_vector)}
481
482 {olay}
483 {self.foreach_col(ColumnSerializer.column_init)}
484 }}
485 '''
486
Lalit Maganti16117cc2022-12-21 15:33:21 +0000487 def serialize(self) -> str:
488 return f'''
489class {self.table_name} : public macros_internal::MacroTable {{
490 public:
491 {self.id_defn().lstrip()}
492 struct ColumnIndex {{
493 {self.foreach_col(ColumnSerializer.colindex)}
494 }};
495 struct ColumnType {{
496 {self.foreach_col(ColumnSerializer.coltype_enum)}
497 }};
498 {self.row_struct().strip()}
Lalit Maganti16117cc2022-12-21 15:33:21 +0000499 struct ColumnFlag {{
500 {self.foreach_col(ColumnSerializer.flag)}
501 }};
502
Lalit Magantie2a74562023-03-16 18:07:25 +0000503 class RowNumber;
504 class ConstRowReference;
505 class RowReference;
506
507 class RowNumber : public macros_internal::AbstractRowNumber<
508 {self.table_name}, ConstRowReference, RowReference> {{
509 public:
510 explicit RowNumber(uint32_t row_number)
511 : AbstractRowNumber(row_number) {{}}
512 }};
513 static_assert(std::is_trivially_destructible<RowNumber>::value,
514 "Inheritance used without trivial destruction");
515
516 {self.const_row_reference_struct().strip()}
517 {self.row_reference_struct().strip()}
518
Lalit Magantie1d4d442023-03-23 21:43:33 +0000519 {self.const_iterator().strip()}
520 {self.iterator().strip()}
521
522 struct IdAndRow {{
523 Id id;
524 uint32_t row;
525 RowReference row_reference;
526 RowNumber row_number;
527 }};
528
Lalit Maganti16117cc2022-12-21 15:33:21 +0000529 {self.constructor().strip()}
530 ~{self.table_name}() override;
531
532 static const char* Name() {{ return "{self.table.sql_name}"; }}
533
Lalit Magantie1d4d442023-03-23 21:43:33 +0000534 static Table::Schema ComputeStaticSchema() {{
535 Table::Schema schema;
536 schema.columns.emplace_back(Table::Schema::Column{{
537 "id", SqlValue::Type::kLong, true, true, false, false}});
538 schema.columns.emplace_back(Table::Schema::Column{{
539 "type", SqlValue::Type::kString, false, false, false, false}});
540 {self.foreach_col(ColumnSerializer.static_schema)}
541 return schema;
542 }}
543
544 ConstIterator IterateRows() const {{
545 return ConstIterator(this, CopyOverlays());
546 }}
547
548 Iterator IterateRows() {{ return Iterator(this, CopyOverlays()); }}
549
550 ConstIterator FilterToIterator(
551 const std::vector<Constraint>& cs,
552 RowMap::OptimizeFor opt = RowMap::OptimizeFor::kMemory) const {{
553 return ConstIterator(this, FilterAndApplyToOverlays(cs, opt));
554 }}
555
556 Iterator FilterToIterator(
557 const std::vector<Constraint>& cs,
558 RowMap::OptimizeFor opt = RowMap::OptimizeFor::kMemory) {{
559 return Iterator(this, FilterAndApplyToOverlays(cs, opt));
560 }}
561
Lalit Maganti16117cc2022-12-21 15:33:21 +0000562 void ShrinkToFit() {{
563 {self.foreach_col(ColumnSerializer.shrink_to_fit)}
564 }}
565
Lalit Maganti4e2303c2023-03-29 15:28:36 +0100566 std::optional<ConstRowReference> FindById(Id find_id) const {{
567 std::optional<uint32_t> row = id().IndexOf(find_id);
568 return row ? std::make_optional(ConstRowReference(this, *row))
569 : std::nullopt;
Lalit Magantie2a74562023-03-16 18:07:25 +0000570 }}
571
Lalit Maganti4e2303c2023-03-29 15:28:36 +0100572 std::optional<RowReference> FindById(Id find_id) {{
573 std::optional<uint32_t> row = id().IndexOf(find_id);
574 return row ? std::make_optional(RowReference(this, *row)) : std::nullopt;
Lalit Magantie2a74562023-03-16 18:07:25 +0000575 }}
576
Lalit Maganti16117cc2022-12-21 15:33:21 +0000577 IdAndRow Insert(const Row& row) {{
578 uint32_t row_number = row_count();
Lalit Maganti157d7102023-03-17 20:07:41 +0000579 {self.insert_common().strip()}
Lalit Maganti16117cc2022-12-21 15:33:21 +0000580 {self.foreach_col(ColumnSerializer.append)}
581 UpdateSelfOverlayAfterInsert();
Lalit Magantie1d4d442023-03-23 21:43:33 +0000582 return IdAndRow{{std::move(id), row_number, RowReference(this, row_number),
583 RowNumber(row_number)}};
Lalit Maganti16117cc2022-12-21 15:33:21 +0000584 }}
585
Lalit Maganti3df8a7e2023-04-25 14:18:17 +0100586 {self.extend().strip()}
587
Lalit Maganti16117cc2022-12-21 15:33:21 +0000588 {self.foreach_col(ColumnSerializer.accessor)}
589
590 {self.foreach_col(ColumnSerializer.mutable_accessor)}
591
592 private:
Lalit Maganti3df8a7e2023-04-25 14:18:17 +0100593 {self.extend_constructor().strip()}
Lalit Maganti157d7102023-03-17 20:07:41 +0000594 {self.parent_field().strip()}
Lalit Maganti16117cc2022-12-21 15:33:21 +0000595 {self.foreach_col(ColumnSerializer.storage)}
596}};
597 '''.strip('\n')
598
599
Lalit Maganti746fc2b2023-03-24 13:21:24 +0000600def serialize_header(ifdef_guard: str, tables: List[ParsedTable],
Lalit Maganti16117cc2022-12-21 15:33:21 +0000601 include_paths: List[str]) -> str:
602 """Serializes a table header file containing the given set of tables."""
603 include_paths_str = '\n'.join([f'#include "{i}"' for i in include_paths])
604 tables_str = '\n\n'.join([TableSerializer(t).serialize() for t in tables])
605 return f'''
606#ifndef {ifdef_guard}
607#define {ifdef_guard}
608
Lalit Maganti9de99492023-04-25 15:31:05 +0100609#include "src/trace_processor/tables/macros_internal.h"
Lalit Maganti16117cc2022-12-21 15:33:21 +0000610
611{include_paths_str}
612
613namespace perfetto {{
614namespace trace_processor {{
615namespace tables {{
616
617{tables_str.strip()}
618
619}} // namespace tables
620}} // namespace trace_processor
621}} // namespace perfetto
622
623#endif // {ifdef_guard}
624 '''.strip()
Lalit Magantie1d4d442023-03-23 21:43:33 +0000625
626
627def to_cpp_flags(raw_flag: ColumnFlag) -> str:
628 """Converts a ColumnFlag to the C++ flags which it represents
629
630 It is not valid to call this function with ColumnFlag.NONE as in this case
631 defaults for that column should be implicitly used."""
632
633 assert raw_flag != ColumnFlag.NONE
634 flags = []
635 if ColumnFlag.SORTED in raw_flag:
636 flags.append('Column::Flag::kSorted')
637 if ColumnFlag.HIDDEN in raw_flag:
638 flags.append('Column::Flag::kHidden')
639 if ColumnFlag.DENSE in raw_flag:
640 flags.append('Column::Flag::kDense')
641 if ColumnFlag.SET_ID in raw_flag:
642 flags.append('Column::Flag::kSetId')
643 return ' | '.join(flags)