| 1 |
// Copyright (c) 2009-2021, Google LLC |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// Redistribution and use in source and binary forms, with or without |
| 5 |
// modification, are permitted provided that the following conditions are met: |
| 6 |
// * Redistributions of source code must retain the above copyright |
| 7 |
// notice, this list of conditions and the following disclaimer. |
| 8 |
// * Redistributions in binary form must reproduce the above copyright |
| 9 |
// notice, this list of conditions and the following disclaimer in the |
| 10 |
// documentation and/or other materials provided with the distribution. |
| 11 |
// * Neither the name of Google LLC nor the |
| 12 |
// names of its contributors may be used to endorse or promote products |
| 13 |
// derived from this software without specific prior written permission. |
| 14 |
// |
| 15 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 |
// ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT, |
| 19 |
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 20 |
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 21 |
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 22 |
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 24 |
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
|
| 26 |
#include <memory> |
| 27 |
|
| 28 |
#include "absl/container/flat_hash_map.h" |
| 29 |
#include "absl/container/flat_hash_set.h" |
| 30 |
#include "absl/strings/ascii.h" |
| 31 |
#include "absl/strings/substitute.h" |
| 32 |
#include "google/protobuf/compiler/code_generator.h" |
| 33 |
#include "google/protobuf/compiler/plugin.h" |
| 34 |
#include "google/protobuf/descriptor.h" |
| 35 |
#include "google/protobuf/descriptor.pb.h" |
| 36 |
#include "google/protobuf/wire_format.h" |
| 37 |
#include "upb/mini_table.hpp" |
| 38 |
#include "upb/upb.hpp" |
| 39 |
#include "upbc/common.h" |
| 40 |
|
| 41 |
// Must be last. |
| 42 |
#include "upb/port_def.inc" |
| 43 |
|
| 44 |
namespace upbc { |
| 45 |
namespace { |
| 46 |
|
| 47 |
namespace protoc = ::google::protobuf::compiler; |
| 48 |
namespace protobuf = ::google::protobuf; |
| 49 |
|
| 50 |
// Returns fields in order of "hotness", eg. how frequently they appear in |
| 51 |
// serialized payloads. Ideally this will use a profile. When we don't have |
| 52 |
// that, we assume that fields with smaller numbers are used more frequently. |
| 53 |
inline std::vector<const google::protobuf::FieldDescriptor*> FieldHotnessOrder( |
| 54 |
const google::protobuf::Descriptor* message) { |
| 55 |
std::vector<const google::protobuf::FieldDescriptor*> fields; |
| 56 |
for (int i = 0; i < message->field_count(); i++) { |
| 57 |
fields.push_back(message->field(i)); |
| 58 |
} |
| 59 |
std::sort(fields.begin(), fields.end(), |
| 60 |
[](const google::protobuf::FieldDescriptor* a, |
| 61 |
const google::protobuf::FieldDescriptor* b) { |
| 62 |
return std::make_pair(!a->is_required(), a->number()) < |
| 63 |
std::make_pair(!b->is_required(), b->number()); |
| 64 |
}); |
| 65 |
return fields; |
| 66 |
} |
| 67 |
|
| 68 |
std::string SourceFilename(const google::protobuf::FileDescriptor* file) { |
| 69 |
return StripExtension(file->name()) + ".upb.c"; |
| 70 |
} |
| 71 |
|
| 72 |
std::string MessageInit(const protobuf::Descriptor* descriptor) { |
| 73 |
return MessageName(descriptor) + "_msginit"; |
| 74 |
} |
| 75 |
|
| 76 |
std::string EnumInit(const protobuf::EnumDescriptor* descriptor) { |
| 77 |
return ToCIdent(descriptor->full_name()) + "_enuminit"; |
| 78 |
} |
| 79 |
|
| 80 |
std::string ExtensionIdentBase(const protobuf::FieldDescriptor* ext) { |
| 81 |
assert(ext->is_extension()); |
| 82 |
std::string ext_scope; |
| 83 |
if (ext->extension_scope()) { |
| 84 |
return MessageName(ext->extension_scope()); |
| 85 |
} else { |
| 86 |
return ToCIdent(ext->file()->package()); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
std::string ExtensionLayout(const google::protobuf::FieldDescriptor* ext) { |
| 91 |
return absl::StrCat(ExtensionIdentBase(ext), "_", ext->name(), "_ext"); |
| 92 |
} |
| 93 |
|
| 94 |
const char* kEnumsInit = "enums_layout"; |
| 95 |
const char* kExtensionsInit = "extensions_layout"; |
| 96 |
const char* kMessagesInit = "messages_layout"; |
| 97 |
|
| 98 |
void AddEnums(const protobuf::Descriptor* message, |
| 99 |
std::vector<const protobuf::EnumDescriptor*>* enums) { |
| 100 |
enums->reserve(enums->size() + message->enum_type_count()); |
| 101 |
for (int i = 0; i < message->enum_type_count(); i++) { |
| 102 |
enums->push_back(message->enum_type(i)); |
| 103 |
} |
| 104 |
for (int i = 0; i < message->nested_type_count(); i++) { |
| 105 |
AddEnums(message->nested_type(i), enums); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
std::vector<const protobuf::EnumDescriptor*> SortedEnums( |
| 110 |
const protobuf::FileDescriptor* file) { |
| 111 |
std::vector<const protobuf::EnumDescriptor*> enums; |
| 112 |
enums.reserve(file->enum_type_count()); |
| 113 |
for (int i = 0; i < file->enum_type_count(); i++) { |
| 114 |
enums.push_back(file->enum_type(i)); |
| 115 |
} |
| 116 |
for (int i = 0; i < file->message_type_count(); i++) { |
| 117 |
AddEnums(file->message_type(i), &enums); |
| 118 |
} |
| 119 |
return enums; |
| 120 |
} |
| 121 |
|
| 122 |
std::vector<uint32_t> SortedUniqueEnumNumbers( |
| 123 |
const protobuf::EnumDescriptor* e) { |
| 124 |
std::vector<uint32_t> values; |
| 125 |
values.reserve(e->value_count()); |
| 126 |
for (int i = 0; i < e->value_count(); i++) { |
| 127 |
values.push_back(static_cast<uint32_t>(e->value(i)->number())); |
| 128 |
} |
| 129 |
std::sort(values.begin(), values.end()); |
| 130 |
auto last = std::unique(values.begin(), values.end()); |
| 131 |
values.erase(last, values.end()); |
| 132 |
return values; |
| 133 |
} |
| 134 |
|
| 135 |
void AddMessages(const protobuf::Descriptor* message, |
| 136 |
std::vector<const protobuf::Descriptor*>* messages) { |
| 137 |
messages->push_back(message); |
| 138 |
for (int i = 0; i < message->nested_type_count(); i++) { |
| 139 |
AddMessages(message->nested_type(i), messages); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
// Ordering must match upb/def.c! |
| 144 |
// |
| 145 |
// The ordering is significant because each upb_MessageDef* will point at the |
| 146 |
// corresponding upb_MiniTable and we just iterate through the list without |
| 147 |
// any search or lookup. |
| 148 |
std::vector<const protobuf::Descriptor*> SortedMessages( |
| 149 |
const protobuf::FileDescriptor* file) { |
| 150 |
std::vector<const protobuf::Descriptor*> messages; |
| 151 |
for (int i = 0; i < file->message_type_count(); i++) { |
| 152 |
AddMessages(file->message_type(i), &messages); |
| 153 |
} |
| 154 |
return messages; |
| 155 |
} |
| 156 |
|
| 157 |
void AddExtensionsFromMessage( |
| 158 |
const protobuf::Descriptor* message, |
| 159 |
std::vector<const protobuf::FieldDescriptor*>* exts) { |
| 160 |
for (int i = 0; i < message->extension_count(); i++) { |
| 161 |
exts->push_back(message->extension(i)); |
| 162 |
} |
| 163 |
for (int i = 0; i < message->nested_type_count(); i++) { |
| 164 |
AddExtensionsFromMessage(message->nested_type(i), exts); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
// Ordering must match upb/def.c! |
| 169 |
// |
| 170 |
// The ordering is significant because each upb_FieldDef* will point at the |
| 171 |
// corresponding upb_MiniTable_Extension and we just iterate through the list |
| 172 |
// without any search or lookup. |
| 173 |
std::vector<const protobuf::FieldDescriptor*> SortedExtensions( |
| 174 |
const protobuf::FileDescriptor* file) { |
| 175 |
std::vector<const protobuf::FieldDescriptor*> ret; |
| 176 |
for (int i = 0; i < file->extension_count(); i++) { |
| 177 |
ret.push_back(file->extension(i)); |
| 178 |
} |
| 179 |
for (int i = 0; i < file->message_type_count(); i++) { |
| 180 |
AddExtensionsFromMessage(file->message_type(i), &ret); |
| 181 |
} |
| 182 |
return ret; |
| 183 |
} |
| 184 |
|
| 185 |
std::vector<const protobuf::FieldDescriptor*> FieldNumberOrder( |
| 186 |
const protobuf::Descriptor* message) { |
| 187 |
std::vector<const protobuf::FieldDescriptor*> fields; |
| 188 |
for (int i = 0; i < message->field_count(); i++) { |
| 189 |
fields.push_back(message->field(i)); |
| 190 |
} |
| 191 |
std::sort(fields.begin(), fields.end(), |
| 192 |
[](const protobuf::FieldDescriptor* a, |
| 193 |
const protobuf::FieldDescriptor* b) { |
| 194 |
return a->number() < b->number(); |
| 195 |
}); |
| 196 |
return fields; |
| 197 |
} |
| 198 |
|
| 199 |
std::string EnumValueSymbol(const protobuf::EnumValueDescriptor* value) { |
| 200 |
return ToCIdent(value->full_name()); |
| 201 |
} |
| 202 |
|
| 203 |
std::string CTypeInternal(const protobuf::FieldDescriptor* field, |
| 204 |
bool is_const) { |
| 205 |
std::string maybe_const = is_const ? "const " : ""; |
| 206 |
switch (field->cpp_type()) { |
| 207 |
case protobuf::FieldDescriptor::CPPTYPE_MESSAGE: { |
| 208 |
std::string maybe_struct = |
| 209 |
field->file() != field->message_type()->file() ? "struct " : ""; |
| 210 |
return maybe_const + maybe_struct + MessageName(field->message_type()) + |
| 211 |
"*"; |
| 212 |
} |
| 213 |
case protobuf::FieldDescriptor::CPPTYPE_BOOL: |
| 214 |
return "bool"; |
| 215 |
case protobuf::FieldDescriptor::CPPTYPE_FLOAT: |
| 216 |
return "float"; |
| 217 |
case protobuf::FieldDescriptor::CPPTYPE_INT32: |
| 218 |
case protobuf::FieldDescriptor::CPPTYPE_ENUM: |
| 219 |
return "int32_t"; |
| 220 |
case protobuf::FieldDescriptor::CPPTYPE_UINT32: |
| 221 |
return "uint32_t"; |
| 222 |
case protobuf::FieldDescriptor::CPPTYPE_DOUBLE: |
| 223 |
return "double"; |
| 224 |
case protobuf::FieldDescriptor::CPPTYPE_INT64: |
| 225 |
return "int64_t"; |
| 226 |
case protobuf::FieldDescriptor::CPPTYPE_UINT64: |
| 227 |
return "uint64_t"; |
| 228 |
case protobuf::FieldDescriptor::CPPTYPE_STRING: |
| 229 |
return "upb_StringView"; |
| 230 |
default: |
| 231 |
fprintf(stderr, "Unexpected type"); |
| 232 |
abort(); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
std::string SizeLg2(const protobuf::FieldDescriptor* field) { |
| 237 |
switch (field->cpp_type()) { |
| 238 |
case protobuf::FieldDescriptor::CPPTYPE_MESSAGE: |
| 239 |
return "UPB_SIZE(2, 3)"; |
| 240 |
case protobuf::FieldDescriptor::CPPTYPE_ENUM: |
| 241 |
return std::to_string(2); |
| 242 |
case protobuf::FieldDescriptor::CPPTYPE_BOOL: |
| 243 |
return std::to_string(1); |
| 244 |
case protobuf::FieldDescriptor::CPPTYPE_FLOAT: |
| 245 |
return std::to_string(2); |
| 246 |
case protobuf::FieldDescriptor::CPPTYPE_INT32: |
| 247 |
return std::to_string(2); |
| 248 |
case protobuf::FieldDescriptor::CPPTYPE_UINT32: |
| 249 |
return std::to_string(2); |
| 250 |
case protobuf::FieldDescriptor::CPPTYPE_DOUBLE: |
| 251 |
return std::to_string(3); |
| 252 |
case protobuf::FieldDescriptor::CPPTYPE_INT64: |
| 253 |
return std::to_string(3); |
| 254 |
case protobuf::FieldDescriptor::CPPTYPE_UINT64: |
| 255 |
return std::to_string(3); |
| 256 |
case protobuf::FieldDescriptor::CPPTYPE_STRING: |
| 257 |
return "UPB_SIZE(3, 4)"; |
| 258 |
default: |
| 259 |
fprintf(stderr, "Unexpected type"); |
| 260 |
abort(); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
bool HasNonZeroDefault(const protobuf::FieldDescriptor* field) { |
| 265 |
switch (field->cpp_type()) { |
| 266 |
case protobuf::FieldDescriptor::CPPTYPE_MESSAGE: |
| 267 |
return false; |
| 268 |
case protobuf::FieldDescriptor::CPPTYPE_STRING: |
| 269 |
return !field->default_value_string().empty(); |
| 270 |
case protobuf::FieldDescriptor::CPPTYPE_INT32: |
| 271 |
return field->default_value_int32() != 0; |
| 272 |
case protobuf::FieldDescriptor::CPPTYPE_INT64: |
| 273 |
return field->default_value_int64() != 0; |
| 274 |
case protobuf::FieldDescriptor::CPPTYPE_UINT32: |
| 275 |
return field->default_value_uint32() != 0; |
| 276 |
case protobuf::FieldDescriptor::CPPTYPE_UINT64: |
| 277 |
return field->default_value_uint64() != 0; |
| 278 |
case protobuf::FieldDescriptor::CPPTYPE_FLOAT: |
| 279 |
return field->default_value_float() != 0; |
| 280 |
case protobuf::FieldDescriptor::CPPTYPE_DOUBLE: |
| 281 |
return field->default_value_double() != 0; |
| 282 |
case protobuf::FieldDescriptor::CPPTYPE_BOOL: |
| 283 |
return field->default_value_bool() != false; |
| 284 |
case protobuf::FieldDescriptor::CPPTYPE_ENUM: |
| 285 |
// Use a number instead of a symbolic name so that we don't require |
| 286 |
// this enum's header to be included. |
| 287 |
return field->default_value_enum()->number() != 0; |
| 288 |
} |
| 289 |
ABSL_ASSERT(false); |
| 290 |
return false; |
| 291 |
} |
| 292 |
|
| 293 |
std::string FieldDefault(const protobuf::FieldDescriptor* field) { |
| 294 |
switch (field->cpp_type()) { |
| 295 |
case protobuf::FieldDescriptor::CPPTYPE_MESSAGE: |
| 296 |
return "NULL"; |
| 297 |
case protobuf::FieldDescriptor::CPPTYPE_STRING: |
| 298 |
return absl::Substitute("upb_StringView_FromString(\"$0\")", |
| 299 |
absl::CEscape(field->default_value_string())); |
| 300 |
case protobuf::FieldDescriptor::CPPTYPE_INT32: |
| 301 |
return absl::Substitute("_upb_Int32_FromI($0)", |
| 302 |
field->default_value_int32()); |
| 303 |
case protobuf::FieldDescriptor::CPPTYPE_INT64: |
| 304 |
return absl::Substitute("_upb_Int64_FromLL($0ll)", |
| 305 |
field->default_value_int64()); |
| 306 |
case protobuf::FieldDescriptor::CPPTYPE_UINT32: |
| 307 |
return absl::Substitute("_upb_UInt32_FromU($0u)", |
| 308 |
field->default_value_uint32()); |
| 309 |
case protobuf::FieldDescriptor::CPPTYPE_UINT64: |
| 310 |
return absl::Substitute("_upb_UInt64_FromULL($0ull)", |
| 311 |
field->default_value_uint64()); |
| 312 |
case protobuf::FieldDescriptor::CPPTYPE_FLOAT: |
| 313 |
return absl::StrCat(field->default_value_float()); |
| 314 |
case protobuf::FieldDescriptor::CPPTYPE_DOUBLE: |
| 315 |
return absl::StrCat(field->default_value_double()); |
| 316 |
case protobuf::FieldDescriptor::CPPTYPE_BOOL: |
| 317 |
return field->default_value_bool() ? "true" : "false"; |
| 318 |
case protobuf::FieldDescriptor::CPPTYPE_ENUM: |
| 319 |
// Use a number instead of a symbolic name so that we don't require |
| 320 |
// this enum's header to be included. |
| 321 |
return absl::StrCat(field->default_value_enum()->number()); |
| 322 |
} |
| 323 |
ABSL_ASSERT(false); |
| 324 |
return "XXX"; |
| 325 |
} |
| 326 |
|
| 327 |
std::string CType(const protobuf::FieldDescriptor* field) { |
| 328 |
return CTypeInternal(field, false); |
| 329 |
} |
| 330 |
|
| 331 |
std::string CTypeConst(const protobuf::FieldDescriptor* field) { |
| 332 |
return CTypeInternal(field, true); |
| 333 |
} |
| 334 |
|
| 335 |
//////////////////////////////////////////////////////////////////////////////// |
| 336 |
// FilePlatformLayout |
| 337 |
//////////////////////////////////////////////////////////////////////////////// |
| 338 |
|
| 339 |
// FilePlatformLayout builds and vends upb MiniTables for a given platform (32 |
| 340 |
// or 64 bit). |
| 341 |
class FilePlatformLayout { |
| 342 |
public: |
| 343 |
FilePlatformLayout(const protobuf::FileDescriptor* fd, |
| 344 |
upb_MiniTablePlatform platform) |
| 345 |
: platform_(platform) { |
| 346 |
BuildMiniTables(fd); |
| 347 |
BuildExtensions(fd); |
| 348 |
} |
| 349 |
|
| 350 |
// Retrieves a upb MiniTable or Extension given a protobuf descriptor. The |
| 351 |
// descriptor must be from this layout's file. |
| 352 |
upb_MiniTable* GetMiniTable(const protobuf::Descriptor* m) const; |
| 353 |
upb_MiniTable_Enum* GetEnumTable(const protobuf::EnumDescriptor* d) const; |
| 354 |
const upb_MiniTable_Extension* GetExtension( |
| 355 |
const protobuf::FieldDescriptor* fd) const; |
| 356 |
|
| 357 |
// Get the initializer for the given sub-message/sub-enum link. |
| 358 |
static std::string GetSub(upb_MiniTable_Sub sub); |
| 359 |
|
| 360 |
private: |
| 361 |
// Functions to build mini-tables for this file's messages and extensions. |
| 362 |
void BuildMiniTables(const protobuf::FileDescriptor* fd); |
| 363 |
void BuildExtensions(const protobuf::FileDescriptor* fd); |
| 364 |
upb_MiniTable* MakeMiniTable(const protobuf::Descriptor* m); |
| 365 |
upb_MiniTable* MakeRegularMiniTable(const protobuf::Descriptor* m); |
| 366 |
upb_MiniTable_Enum* MakeMiniTableEnum(const protobuf::EnumDescriptor* d); |
| 367 |
uint64_t GetMessageModifiers(const protobuf::Descriptor* m); |
| 368 |
uint64_t GetFieldModifiers(const protobuf::FieldDescriptor* f); |
| 369 |
void ResolveIntraFileReferences(); |
| 370 |
|
| 371 |
// When we are generating code, tables are linked to sub-tables via name (ie. |
| 372 |
// a string) rather than by pointer. We need to emit an initializer like |
| 373 |
// `&foo_sub_table`. To do this, we store `const char*` strings in all the |
| 374 |
// links that would normally be pointers: |
| 375 |
// field -> sub-message |
| 376 |
// field -> enum table (proto2 only) |
| 377 |
// extension -> extendee |
| 378 |
// |
| 379 |
// This requires a bit of reinterpret_cast<>(), but it's confined to a few |
| 380 |
// functions. We tag the pointer so we know which member of the union to |
| 381 |
// initialize. |
| 382 |
enum SubTag { |
| 383 |
kNull = 0, |
| 384 |
kMessage = 1, |
| 385 |
kEnum = 2, |
| 386 |
kMask = 3, |
| 387 |
}; |
| 388 |
|
| 389 |
static upb_MiniTable_Sub PackSub(const char* data, SubTag tag); |
| 390 |
static bool IsNull(upb_MiniTable_Sub sub); |
| 391 |
void SetSubTableStrings(); |
| 392 |
upb_MiniTable_Sub PackSubForField(const protobuf::FieldDescriptor* f, |
| 393 |
const upb_MiniTable_Field* mt_f); |
| 394 |
const char* AllocStr(absl::string_view str); |
| 395 |
|
| 396 |
private: |
| 397 |
using TableMap = |
| 398 |
absl::flat_hash_map<const protobuf::Descriptor*, upb_MiniTable*>; |
| 399 |
using EnumMap = |
| 400 |
absl::flat_hash_map<const protobuf::EnumDescriptor*, upb_MiniTable_Enum*>; |
| 401 |
using ExtensionMap = absl::flat_hash_map<const protobuf::FieldDescriptor*, |
| 402 |
upb_MiniTable_Extension>; |
| 403 |
upb::Arena arena_; |
| 404 |
TableMap table_map_; |
| 405 |
EnumMap enum_map_; |
| 406 |
ExtensionMap extension_map_; |
| 407 |
upb_MiniTablePlatform platform_; |
| 408 |
}; |
| 409 |
|
| 410 |
upb_MiniTable* FilePlatformLayout::GetMiniTable( |
| 411 |
const protobuf::Descriptor* m) const { |
| 412 |
auto it = table_map_.find(m); |
| 413 |
assert(it != table_map_.end()); |
| 414 |
return it->second; |
| 415 |
} |
| 416 |
|
| 417 |
upb_MiniTable_Enum* FilePlatformLayout::GetEnumTable( |
| 418 |
const protobuf::EnumDescriptor* d) const { |
| 419 |
auto it = enum_map_.find(d); |
| 420 |
assert(it != enum_map_.end()); |
| 421 |
return it->second; |
| 422 |
} |
| 423 |
|
| 424 |
const upb_MiniTable_Extension* FilePlatformLayout::GetExtension( |
| 425 |
const protobuf::FieldDescriptor* fd) const { |
| 426 |
auto it = extension_map_.find(fd); |
| 427 |
assert(it != extension_map_.end()); |
| 428 |
return &it->second; |
| 429 |
} |
| 430 |
|
| 431 |
void FilePlatformLayout::ResolveIntraFileReferences() { |
| 432 |
// This properly resolves references within a file, in order to set any |
| 433 |
// necessary flags (eg. is a map). |
| 434 |
for (const auto& pair : table_map_) { |
| 435 |
upb_MiniTable* mt = pair.second; |
| 436 |
// First we properly resolve for defs within the file. |
| 437 |
for (const auto* f : FieldNumberOrder(pair.first)) { |
| 438 |
if (f->message_type() && f->message_type()->file() == f->file()) { |
| 439 |
// const_cast is safe because the mini-table is owned exclusively |
| 440 |
// by us, and was allocated from an arena (known-writable memory). |
| 441 |
upb_MiniTable_Field* mt_f = const_cast<upb_MiniTable_Field*>( |
| 442 |
upb_MiniTable_FindFieldByNumber(mt, f->number())); |
| 443 |
upb_MiniTable* sub_mt = GetMiniTable(f->message_type()); |
| 444 |
upb_MiniTable_SetSubMessage(mt, mt_f, sub_mt); |
| 445 |
} |
| 446 |
// We don't worry about enums here, because resolving an enum will |
| 447 |
// never alter the mini-table. |
| 448 |
} |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
upb_MiniTable_Sub FilePlatformLayout::PackSub(const char* data, SubTag tag) { |
| 453 |
uintptr_t val = reinterpret_cast<uintptr_t>(data); |
| 454 |
assert((val & kMask) == 0); |
| 455 |
upb_MiniTable_Sub sub; |
| 456 |
sub.submsg = reinterpret_cast<upb_MiniTable*>(val | tag); |
| 457 |
return sub; |
| 458 |
} |
| 459 |
|
| 460 |
bool FilePlatformLayout::IsNull(upb_MiniTable_Sub sub) { |
| 461 |
return reinterpret_cast<uintptr_t>(sub.subenum) == 0; |
| 462 |
} |
| 463 |
|
| 464 |
std::string FilePlatformLayout::GetSub(upb_MiniTable_Sub sub) { |
| 465 |
uintptr_t as_int = reinterpret_cast<uintptr_t>(sub.submsg); |
| 466 |
const char* str = reinterpret_cast<const char*>(as_int & ~SubTag::kMask); |
| 467 |
switch (as_int & SubTag::kMask) { |
| 468 |
case SubTag::kMessage: |
| 469 |
return absl::Substitute("{.submsg = &$0}", str); |
| 470 |
case SubTag::kEnum: |
| 471 |
return absl::Substitute("{.subenum = &$0}", str); |
| 472 |
default: |
| 473 |
return std::string("{.submsg = NULL}"); |
| 474 |
} |
| 475 |
return std::string("ERROR in GetSub"); |
| 476 |
} |
| 477 |
|
| 478 |
void FilePlatformLayout::SetSubTableStrings() { |
| 479 |
for (const auto& pair : table_map_) { |
| 480 |
upb_MiniTable* mt = pair.second; |
| 481 |
for (const auto* f : FieldNumberOrder(pair.first)) { |
| 482 |
upb_MiniTable_Field* mt_f = const_cast<upb_MiniTable_Field*>( |
| 483 |
upb_MiniTable_FindFieldByNumber(mt, f->number())); |
| 484 |
assert(mt_f); |
| 485 |
upb_MiniTable_Sub sub = PackSubForField(f, mt_f); |
| 486 |
if (IsNull(sub)) continue; |
| 487 |
// const_cast is safe because the mini-table is owned exclusively |
| 488 |
// by us, and was allocated from an arena (known-writable memory). |
| 489 |
*const_cast<upb_MiniTable_Sub*>(&mt->subs[mt_f->submsg_index]) = sub; |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
upb_MiniTable_Sub FilePlatformLayout::PackSubForField( |
| 495 |
const protobuf::FieldDescriptor* f, const upb_MiniTable_Field* mt_f) { |
| 496 |
if (mt_f->submsg_index == kUpb_NoSub) { |
| 497 |
return PackSub(nullptr, SubTag::kNull); |
| 498 |
} else if (f->message_type()) { |
| 499 |
return PackSub(AllocStr(MessageInit(f->message_type())), SubTag::kMessage); |
| 500 |
} else { |
| 501 |
ABSL_ASSERT(f->enum_type()); |
| 502 |
return PackSub(AllocStr(EnumInit(f->enum_type())), SubTag::kEnum); |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
const char* FilePlatformLayout::AllocStr(absl::string_view str) { |
| 507 |
char* ret = |
| 508 |
static_cast<char*>(upb_Arena_Malloc(arena_.ptr(), str.size() + 1)); |
| 509 |
memcpy(ret, str.data(), str.size()); |
| 510 |
ret[str.size()] = '\0'; |
| 511 |
return ret; |
| 512 |
} |
| 513 |
|
| 514 |
void FilePlatformLayout::BuildMiniTables(const protobuf::FileDescriptor* fd) { |
| 515 |
for (const auto& m : SortedMessages(fd)) { |
| 516 |
table_map_[m] = MakeMiniTable(m); |
| 517 |
} |
| 518 |
for (const auto& e : SortedEnums(fd)) { |
| 519 |
enum_map_[e] = MakeMiniTableEnum(e); |
| 520 |
} |
| 521 |
ResolveIntraFileReferences(); |
| 522 |
SetSubTableStrings(); |
| 523 |
} |
| 524 |
|
| 525 |
void FilePlatformLayout::BuildExtensions(const protobuf::FileDescriptor* fd) { |
| 526 |
std::vector<const protobuf::FieldDescriptor*> sorted = SortedExtensions(fd); |
| 527 |
upb::Status status; |
| 528 |
for (const auto* f : sorted) { |
| 529 |
upb::MtDataEncoder e; |
| 530 |
e.StartMessage(0); |
| 531 |
e.PutField(static_cast<upb_FieldType>(f->type()), f->number(), |
| 532 |
GetFieldModifiers(f)); |
| 533 |
upb_MiniTable_Extension& ext = extension_map_[f]; |
| 534 |
upb_MiniTable_Sub sub; |
| 535 |
bool ok = upb_MiniTable_BuildExtension(e.data().data(), e.data().size(), |
| 536 |
&ext, sub, status.ptr()); |
| 537 |
if (!ok) { |
| 538 |
// TODO(haberman): Use ABSL CHECK() when it is available. |
| 539 |
fprintf(stderr, "Error building mini-table: %s\n", |
| 540 |
status.error_message()); |
| 541 |
} |
| 542 |
ABSL_ASSERT(ok); |
| 543 |
ext.extendee = reinterpret_cast<const upb_MiniTable*>( |
| 544 |
AllocStr(MessageInit(f->containing_type()))); |
| 545 |
ext.sub = PackSubForField(f, &ext.field); |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
upb_MiniTable* FilePlatformLayout::MakeMiniTable( |
| 550 |
const protobuf::Descriptor* m) { |
| 551 |
if (m->options().message_set_wire_format()) { |
| 552 |
return upb_MiniTable_BuildMessageSet(platform_, arena_.ptr()); |
| 553 |
} else if (m->options().map_entry()) { |
| 554 |
return upb_MiniTable_BuildMapEntry( |
| 555 |
static_cast<upb_FieldType>(m->map_key()->type()), |
| 556 |
static_cast<upb_FieldType>(m->map_value()->type()), |
| 557 |
m->map_value()->enum_type() && |
| 558 |
m->map_value()->enum_type()->file()->syntax() == |
| 559 |
protobuf::FileDescriptor::SYNTAX_PROTO3, |
| 560 |
platform_, arena_.ptr()); |
| 561 |
} else { |
| 562 |
return MakeRegularMiniTable(m); |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
upb_MiniTable* FilePlatformLayout::MakeRegularMiniTable( |
| 567 |
const protobuf::Descriptor* m) { |
| 568 |
upb::MtDataEncoder e; |
| 569 |
e.StartMessage(GetMessageModifiers(m)); |
| 570 |
for (const auto* f : FieldNumberOrder(m)) { |
| 571 |
e.PutField(static_cast<upb_FieldType>(f->type()), f->number(), |
| 572 |
GetFieldModifiers(f)); |
| 573 |
} |
| 574 |
for (int i = 0; i < m->real_oneof_decl_count(); i++) { |
| 575 |
const protobuf::OneofDescriptor* oneof = m->oneof_decl(i); |
| 576 |
e.StartOneof(); |
| 577 |
for (int j = 0; j < oneof->field_count(); j++) { |
| 578 |
const protobuf::FieldDescriptor* f = oneof->field(j); |
| 579 |
e.PutOneofField(f->number()); |
| 580 |
} |
| 581 |
} |
| 582 |
absl::string_view str = e.data(); |
| 583 |
upb::Status status; |
| 584 |
upb_MiniTable* ret = upb_MiniTable_Build(str.data(), str.size(), platform_, |
| 585 |
arena_.ptr(), status.ptr()); |
| 586 |
if (!ret) { |
| 587 |
fprintf(stderr, "Error building mini-table: %s\n", status.error_message()); |
| 588 |
} |
| 589 |
assert(ret); |
| 590 |
return ret; |
| 591 |
} |
| 592 |
|
| 593 |
upb_MiniTable_Enum* FilePlatformLayout::MakeMiniTableEnum( |
| 594 |
const protobuf::EnumDescriptor* d) { |
| 595 |
upb::Arena arena; |
| 596 |
upb::MtDataEncoder e; |
| 597 |
|
| 598 |
e.StartEnum(); |
| 599 |
for (uint32_t i : SortedUniqueEnumNumbers(d)) { |
| 600 |
e.PutEnumValue(i); |
| 601 |
} |
| 602 |
e.EndEnum(); |
| 603 |
|
| 604 |
absl::string_view str = e.data(); |
| 605 |
upb::Status status; |
| 606 |
upb_MiniTable_Enum* ret = upb_MiniTable_BuildEnum(str.data(), str.size(), |
| 607 |
arena_.ptr(), status.ptr()); |
| 608 |
if (!ret) { |
| 609 |
fprintf(stderr, "Error building mini-table: %s\n", status.error_message()); |
| 610 |
} |
| 611 |
assert(ret); |
| 612 |
return ret; |
| 613 |
} |
| 614 |
|
| 615 |
uint64_t FilePlatformLayout::GetMessageModifiers( |
| 616 |
const protobuf::Descriptor* m) { |
| 617 |
uint64_t ret = 0; |
| 618 |
|
| 619 |
if (m->file()->syntax() == protobuf::FileDescriptor::SYNTAX_PROTO3) { |
| 620 |
ret |= kUpb_MessageModifier_ValidateUtf8; |
| 621 |
ret |= kUpb_MessageModifier_DefaultIsPacked; |
| 622 |
} |
| 623 |
|
| 624 |
if (m->extension_range_count() > 0) { |
| 625 |
ret |= kUpb_MessageModifier_IsExtendable; |
| 626 |
} |
| 627 |
|
| 628 |
assert(!m->options().map_entry()); |
| 629 |
return ret; |
| 630 |
} |
| 631 |
|
| 632 |
uint64_t FilePlatformLayout::GetFieldModifiers( |
| 633 |
const protobuf::FieldDescriptor* f) { |
| 634 |
uint64_t ret = 0; |
| 635 |
|
| 636 |
if (f->is_repeated()) ret |= kUpb_FieldModifier_IsRepeated; |
| 637 |
if (f->is_required()) ret |= kUpb_FieldModifier_IsRequired; |
| 638 |
if (f->is_packed()) ret |= kUpb_FieldModifier_IsPacked; |
| 639 |
if (f->enum_type() && f->enum_type()->file()->syntax() == |
| 640 |
protobuf::FileDescriptor::SYNTAX_PROTO2) { |
| 641 |
ret |= kUpb_FieldModifier_IsClosedEnum; |
| 642 |
} |
| 643 |
if (f->is_optional() && !f->has_presence()) { |
| 644 |
ret |= kUpb_FieldModifier_IsProto3Singular; |
| 645 |
} |
| 646 |
|
| 647 |
return ret; |
| 648 |
} |
| 649 |
|
| 650 |
//////////////////////////////////////////////////////////////////////////////// |
| 651 |
// FileLayout |
| 652 |
//////////////////////////////////////////////////////////////////////////////// |
| 653 |
|
| 654 |
// FileLayout is a pair of platform layouts: one for 32-bit and one for 64-bit. |
| 655 |
class FileLayout { |
| 656 |
public: |
| 657 |
FileLayout(const protobuf::FileDescriptor* fd) |
| 658 |
: descriptor_(fd), |
| 659 |
layout32_(fd, kUpb_MiniTablePlatform_32Bit), |
| 660 |
layout64_(fd, kUpb_MiniTablePlatform_64Bit) {} |
| 661 |
|
| 662 |
const protobuf::FileDescriptor* descriptor() const { return descriptor_; } |
| 663 |
|
| 664 |
const upb_MiniTable* GetMiniTable32(const protobuf::Descriptor* m) const { |
| 665 |
return layout32_.GetMiniTable(m); |
| 666 |
} |
| 667 |
|
| 668 |
const upb_MiniTable* GetMiniTable64(const protobuf::Descriptor* m) const { |
| 669 |
return layout64_.GetMiniTable(m); |
| 670 |
} |
| 671 |
|
| 672 |
const upb_MiniTable_Enum* GetEnumTable( |
| 673 |
const protobuf::EnumDescriptor* d) const { |
| 674 |
return layout64_.GetEnumTable(d); |
| 675 |
} |
| 676 |
|
| 677 |
std::string GetFieldOffset(const protobuf::FieldDescriptor* f) const { |
| 678 |
const upb_MiniTable_Field* f_32 = upb_MiniTable_FindFieldByNumber( |
| 679 |
GetMiniTable32(f->containing_type()), f->number()); |
| 680 |
const upb_MiniTable_Field* f_64 = upb_MiniTable_FindFieldByNumber( |
| 681 |
GetMiniTable64(f->containing_type()), f->number()); |
| 682 |
return absl::Substitute("UPB_SIZE($0, $1)", f_32->offset, f_64->offset); |
| 683 |
} |
| 684 |
|
| 685 |
std::string GetOneofCaseOffset(const protobuf::OneofDescriptor* o) const { |
| 686 |
const protobuf::FieldDescriptor* f = o->field(0); |
| 687 |
const upb_MiniTable_Field* f_32 = upb_MiniTable_FindFieldByNumber( |
| 688 |
GetMiniTable32(f->containing_type()), f->number()); |
| 689 |
const upb_MiniTable_Field* f_64 = upb_MiniTable_FindFieldByNumber( |
| 690 |
GetMiniTable64(f->containing_type()), f->number()); |
| 691 |
return absl::Substitute("UPB_SIZE($0, $1)", ~f_32->presence, |
| 692 |
~f_64->presence); |
| 693 |
} |
| 694 |
|
| 695 |
std::string GetMessageSize(const protobuf::Descriptor* d) const { |
| 696 |
return absl::Substitute("UPB_SIZE($0, $1)", GetMiniTable32(d)->size, |
| 697 |
GetMiniTable64(d)->size); |
| 698 |
} |
| 699 |
|
| 700 |
int GetHasbitIndex(const protobuf::FieldDescriptor* f) const { |
| 701 |
const upb_MiniTable_Field* f_64 = upb_MiniTable_FindFieldByNumber( |
| 702 |
GetMiniTable64(f->containing_type()), f->number()); |
| 703 |
return f_64->presence; |
| 704 |
} |
| 705 |
|
| 706 |
bool HasHasbit(const protobuf::FieldDescriptor* f) const { |
| 707 |
return GetHasbitIndex(f) > 0; |
| 708 |
} |
| 709 |
|
| 710 |
const upb_MiniTable_Extension* GetExtension( |
| 711 |
const protobuf::FieldDescriptor* f) const { |
| 712 |
return layout64_.GetExtension(f); |
| 713 |
} |
| 714 |
|
| 715 |
private: |
| 716 |
const protobuf::FileDescriptor* descriptor_; |
| 717 |
FilePlatformLayout layout32_; |
| 718 |
FilePlatformLayout layout64_; |
| 719 |
}; |
| 720 |
|
| 721 |
void DumpEnumValues(const protobuf::EnumDescriptor* desc, Output& output) { |
| 722 |
std::vector<const protobuf::EnumValueDescriptor*> values; |
| 723 |
for (int i = 0; i < desc->value_count(); i++) { |
| 724 |
values.push_back(desc->value(i)); |
| 725 |
} |
| 726 |
std::sort(values.begin(), values.end(), |
| 727 |
[](const protobuf::EnumValueDescriptor* a, |
| 728 |
const protobuf::EnumValueDescriptor* b) { |
| 729 |
return a->number() < b->number(); |
| 730 |
}); |
| 731 |
|
| 732 |
for (size_t i = 0; i < values.size(); i++) { |
| 733 |
auto value = values[i]; |
| 734 |
output(" $0 = $1", EnumValueSymbol(value), value->number()); |
| 735 |
if (i != values.size() - 1) { |
| 736 |
output(","); |
| 737 |
} |
| 738 |
output("\n"); |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
void GenerateExtensionInHeader(const protobuf::FieldDescriptor* ext, |
| 743 |
Output& output) { |
| 744 |
output( |
| 745 |
R"cc( |
| 746 |
UPB_INLINE bool $0_has_$1(const struct $2* msg) { |
| 747 |
return _upb_Message_Getext(msg, &$3) != NULL; |
| 748 |
} |
| 749 |
)cc", |
| 750 |
ExtensionIdentBase(ext), ext->name(), MessageName(ext->containing_type()), |
| 751 |
ExtensionLayout(ext)); |
| 752 |
|
| 753 |
output( |
| 754 |
R"cc( |
| 755 |
UPB_INLINE void $0_clear_$1(struct $2* msg) { |
| 756 |
_upb_Message_Clearext(msg, &$3); |
| 757 |
} |
| 758 |
)cc", |
| 759 |
ExtensionIdentBase(ext), ext->name(), MessageName(ext->containing_type()), |
| 760 |
ExtensionLayout(ext)); |
| 761 |
|
| 762 |
if (ext->is_repeated()) { |
| 763 |
} else if (ext->message_type()) { |
| 764 |
output( |
| 765 |
R"cc( |
| 766 |
UPB_INLINE $0 $1_$2(const struct $3* msg) { |
| 767 |
const upb_Message_Extension* ext = _upb_Message_Getext(msg, &$4); |
| 768 |
UPB_ASSERT(ext); |
| 769 |
return *UPB_PTR_AT(&ext->data, 0, $0); |
| 770 |
} |
| 771 |
)cc", |
| 772 |
CTypeConst(ext), ExtensionIdentBase(ext), ext->name(), |
| 773 |
MessageName(ext->containing_type()), ExtensionLayout(ext), |
| 774 |
FieldDefault(ext)); |
| 775 |
output( |
| 776 |
R"cc( |
| 777 |
UPB_INLINE void $1_set_$2(struct $3* msg, $0 ext, upb_Arena* arena) { |
| 778 |
const upb_Message_Extension* msg_ext = |
| 779 |
_upb_Message_GetOrCreateExtension(msg, &$4, arena); |
| 780 |
UPB_ASSERT(msg_ext); |
| 781 |
*UPB_PTR_AT(&msg_ext->data, 0, $0) = ext; |
| 782 |
} |
| 783 |
)cc", |
| 784 |
CTypeConst(ext), ExtensionIdentBase(ext), ext->name(), |
| 785 |
MessageName(ext->containing_type()), ExtensionLayout(ext), |
| 786 |
FieldDefault(ext)); |
| 787 |
} else { |
| 788 |
// Returns default if extension field is not a message. |
| 789 |
output( |
| 790 |
R"cc( |
| 791 |
UPB_INLINE $0 $1_$2(const struct $3* msg) { |
| 792 |
const upb_Message_Extension* ext = _upb_Message_Getext(msg, &$4); |
| 793 |
return ext ? *UPB_PTR_AT(&ext->data, 0, $0) : $5; |
| 794 |
} |
| 795 |
)cc", |
| 796 |
CTypeConst(ext), ExtensionIdentBase(ext), ext->name(), |
| 797 |
MessageName(ext->containing_type()), ExtensionLayout(ext), |
| 798 |
FieldDefault(ext)); |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
void GenerateMessageFunctionsInHeader(const protobuf::Descriptor* message, |
| 803 |
Output& output) { |
| 804 |
// TODO(b/235839510): The generated code here does not check the return values |
| 805 |
// from upb_Encode(). How can we even fix this without breaking other things? |
| 806 |
output( |
| 807 |
R"cc( |
| 808 |
UPB_INLINE $0* $0_new(upb_Arena* arena) { |
| 809 |
return ($0*)_upb_Message_New(&$1, arena); |
| 810 |
} |
| 811 |
UPB_INLINE $0* $0_parse(const char* buf, size_t size, upb_Arena* arena) { |
| 812 |
$0* ret = $0_new(arena); |
| 813 |
if (!ret) return NULL; |
| 814 |
if (upb_Decode(buf, size, ret, &$1, NULL, 0, arena) != kUpb_DecodeStatus_Ok) { |
| 815 |
return NULL; |
| 816 |
} |
| 817 |
return ret; |
| 818 |
} |
| 819 |
UPB_INLINE $0* $0_parse_ex(const char* buf, size_t size, |
| 820 |
const upb_ExtensionRegistry* extreg, |
| 821 |
int options, upb_Arena* arena) { |
| 822 |
$0* ret = $0_new(arena); |
| 823 |
if (!ret) return NULL; |
| 824 |
if (upb_Decode(buf, size, ret, &$1, extreg, options, arena) != |
| 825 |
kUpb_DecodeStatus_Ok) { |
| 826 |
return NULL; |
| 827 |
} |
| 828 |
return ret; |
| 829 |
} |
| 830 |
UPB_INLINE char* $0_serialize(const $0* msg, upb_Arena* arena, size_t* len) { |
| 831 |
char* ptr; |
| 832 |
(void)upb_Encode(msg, &$1, 0, arena, &ptr, len); |
| 833 |
return ptr; |
| 834 |
} |
| 835 |
UPB_INLINE char* $0_serialize_ex(const $0* msg, int options, |
| 836 |
upb_Arena* arena, size_t* len) { |
| 837 |
char* ptr; |
| 838 |
(void)upb_Encode(msg, &$1, options, arena, &ptr, len); |
| 839 |
return ptr; |
| 840 |
} |
| 841 |
)cc", |
| 842 |
MessageName(message), MessageInit(message)); |
| 843 |
} |
| 844 |
|
| 845 |
void GenerateOneofInHeader(const protobuf::OneofDescriptor* oneof, |
| 846 |
const FileLayout& layout, absl::string_view msg_name, |
| 847 |
Output& output) { |
| 848 |
std::string fullname = ToCIdent(oneof->full_name()); |
| 849 |
output("typedef enum {\n"); |
| 850 |
for (int j = 0; j < oneof->field_count(); j++) { |
| 851 |
const protobuf::FieldDescriptor* field = oneof->field(j); |
| 852 |
output(" $0_$1 = $2,\n", fullname, field->name(), field->number()); |
| 853 |
} |
| 854 |
output( |
| 855 |
" $0_NOT_SET = 0\n" |
| 856 |
"} $0_oneofcases;\n", |
| 857 |
fullname); |
| 858 |
output( |
| 859 |
R"cc( |
| 860 |
UPB_INLINE $0_oneofcases $1_$2_case(const $1* msg) { |
| 861 |
return ($0_oneofcases)*UPB_PTR_AT(msg, $3, int32_t); |
| 862 |
} |
| 863 |
)cc", |
| 864 |
fullname, msg_name, oneof->name(), layout.GetOneofCaseOffset(oneof)); |
| 865 |
} |
| 866 |
|
| 867 |
void GenerateHazzer(const protobuf::FieldDescriptor* field, |
| 868 |
const FileLayout& layout, absl::string_view msg_name, |
| 869 |
Output& output) { |
| 870 |
if (layout.HasHasbit(field)) { |
| 871 |
output( |
| 872 |
R"cc( |
| 873 |
UPB_INLINE bool $0_has_$1(const $0* msg) { |
| 874 |
return _upb_hasbit(msg, $2); |
| 875 |
} |
| 876 |
)cc", |
| 877 |
msg_name, field->name(), layout.GetHasbitIndex(field)); |
| 878 |
} else if (field->real_containing_oneof()) { |
| 879 |
output( |
| 880 |
R"cc( |
| 881 |
UPB_INLINE bool $0_has_$1(const $0* msg) { |
| 882 |
return _upb_getoneofcase(msg, $2) == $3; |
| 883 |
} |
| 884 |
)cc", |
| 885 |
msg_name, field->name(), |
| 886 |
layout.GetOneofCaseOffset(field->real_containing_oneof()), |
| 887 |
field->number()); |
| 888 |
} else if (field->message_type()) { |
| 889 |
output( |
| 890 |
R"cc( |
| 891 |
UPB_INLINE bool $0_has_$1(const $0* msg) { |
| 892 |
return _upb_has_submsg_nohasbit(msg, $2); |
| 893 |
} |
| 894 |
)cc", |
| 895 |
msg_name, field->name(), layout.GetFieldOffset(field)); |
| 896 |
} |
| 897 |
} |
| 898 |
|
| 899 |
void GenerateClear(const protobuf::FieldDescriptor* field, |
| 900 |
const FileLayout& layout, absl::string_view msg_name, |
| 901 |
Output& output) { |
| 902 |
if (field == field->containing_type()->map_key() || |
| 903 |
field == field->containing_type()->map_value()) { |
| 904 |
// Cannot be cleared. |
| 905 |
return; |
| 906 |
} |
| 907 |
|
| 908 |
if (field->real_containing_oneof()) { |
| 909 |
const protobuf::OneofDescriptor* oneof = field->real_containing_oneof(); |
| 910 |
std::string oneof_fullname = ToCIdent(oneof->full_name()); |
| 911 |
std::string default_value = |
| 912 |
field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_STRING |
| 913 |
? "upb_StringView_FromDataAndSize(NULL, 0)" |
| 914 |
: "0"; |
| 915 |
output( |
| 916 |
R"cc( |
| 917 |
UPB_INLINE void $0_clear_$1(const $0* msg) { |
| 918 |
UPB_WRITE_ONEOF(msg, $2, $3, $7, $4, $6_NOT_SET); |
| 919 |
} |
| 920 |
)cc", |
| 921 |
msg_name, field->name(), CType(field), layout.GetFieldOffset(field), |
| 922 |
layout.GetOneofCaseOffset(field->real_containing_oneof()), |
| 923 |
field->number(), oneof_fullname, default_value); |
| 924 |
} else { |
| 925 |
if (field->message_type()) { |
| 926 |
output( |
| 927 |
R"cc( |
| 928 |
UPB_INLINE void $0_clear_$1(const $0* msg) { |
| 929 |
*UPB_PTR_AT(msg, $2, const upb_Message*) = NULL; |
| 930 |
} |
| 931 |
)cc", |
| 932 |
msg_name, field->name(), layout.GetFieldOffset(field)); |
| 933 |
} else if (layout.HasHasbit(field)) { |
| 934 |
if (field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_STRING) { |
| 935 |
output( |
| 936 |
R"cc( |
| 937 |
UPB_INLINE void $0_clear_$1(const $0* msg) { |
| 938 |
*UPB_PTR_AT(msg, $3, $2) = upb_StringView_FromDataAndSize(NULL, 0); |
| 939 |
_upb_clearhas(msg, $4); |
| 940 |
} |
| 941 |
)cc", |
| 942 |
msg_name, field->name(), CType(field), layout.GetFieldOffset(field), |
| 943 |
layout.GetHasbitIndex(field)); |
| 944 |
} else { |
| 945 |
output( |
| 946 |
R"cc( |
| 947 |
UPB_INLINE void $0_clear_$1(const $0* msg) { |
| 948 |
*UPB_PTR_AT(msg, $3, $2) = 0; |
| 949 |
_upb_clearhas(msg, $4); |
| 950 |
} |
| 951 |
)cc", |
| 952 |
msg_name, field->name(), CType(field), layout.GetFieldOffset(field), |
| 953 |
layout.GetHasbitIndex(field)); |
| 954 |
} |
| 955 |
} else { |
| 956 |
if (field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_STRING) { |
| 957 |
output( |
| 958 |
R"cc( |
| 959 |
UPB_INLINE void $0_clear_$1(const $0* msg) { |
| 960 |
*UPB_PTR_AT(msg, $3, $2) = upb_StringView_FromDataAndSize(NULL, 0); |
| 961 |
} |
| 962 |
)cc", |
| 963 |
msg_name, field->name(), CType(field), |
| 964 |
layout.GetFieldOffset(field)); |
| 965 |
} else { |
| 966 |
output( |
| 967 |
R"cc( |
| 968 |
UPB_INLINE void $0_clear_$1(const $0* msg) { |
| 969 |
*UPB_PTR_AT(msg, $3, $2) = 0; |
| 970 |
} |
| 971 |
)cc", |
| 972 |
msg_name, field->name(), CType(field), layout.GetFieldOffset(field), |
| 973 |
layout.GetHasbitIndex(field)); |
| 974 |
} |
| 975 |
} |
| 976 |
} |
| 977 |
} |
| 978 |
|
| 979 |
void GenerateRepeatedClear(const protobuf::FieldDescriptor* field, |
| 980 |
const FileLayout& layout, absl::string_view msg_name, |
| 981 |
Output& output) { |
| 982 |
output( |
| 983 |
R"cc( |
| 984 |
UPB_INLINE void $0_clear_$1(const $0* msg) { |
| 985 |
_upb_array_detach(msg, $2); |
| 986 |
} |
| 987 |
)cc", |
| 988 |
msg_name, field->name(), layout.GetFieldOffset(field)); |
| 989 |
} |
| 990 |
|
| 991 |
void GenerateMapGetters(const protobuf::FieldDescriptor* field, |
| 992 |
const FileLayout& layout, absl::string_view msg_name, |
| 993 |
Output& output) { |
| 994 |
const protobuf::Descriptor* entry = field->message_type(); |
| 995 |
const protobuf::FieldDescriptor* key = entry->FindFieldByNumber(1); |
| 996 |
const protobuf::FieldDescriptor* val = entry->FindFieldByNumber(2); |
| 997 |
output( |
| 998 |
R"cc( |
| 999 |
UPB_INLINE size_t $0_$1_size(const $0* msg) { |
| 1000 |
return _upb_msg_map_size(msg, $2); |
| 1001 |
} |
| 1002 |
)cc", |
| 1003 |
msg_name, field->name(), layout.GetFieldOffset(field)); |
| 1004 |
output( |
| 1005 |
R"cc( |
| 1006 |
UPB_INLINE bool $0_$1_get(const $0* msg, $2 key, $3* val) { |
| 1007 |
return _upb_msg_map_get(msg, $4, &key, $5, val, $6); |
| 1008 |
} |
| 1009 |
)cc", |
| 1010 |
msg_name, field->name(), CType(key), CType(val), |
| 1011 |
layout.GetFieldOffset(field), |
| 1012 |
key->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_STRING |
| 1013 |
? "0" |
| 1014 |
: "sizeof(key)", |
| 1015 |
val->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_STRING |
| 1016 |
? "0" |
| 1017 |
: "sizeof(*val)"); |
| 1018 |
output( |
| 1019 |
R"cc( |
| 1020 |
UPB_INLINE $0 $1_$2_next(const $1* msg, size_t* iter) { |
| 1021 |
return ($0)_upb_msg_map_next(msg, $3, iter); |
| 1022 |
} |
| 1023 |
)cc", |
| 1024 |
CTypeConst(field), msg_name, field->name(), layout.GetFieldOffset(field)); |
| 1025 |
} |
| 1026 |
|
| 1027 |
void GenerateMapEntryGetters(const protobuf::FieldDescriptor* field, |
| 1028 |
absl::string_view msg_name, Output& output) { |
| 1029 |
output( |
| 1030 |
R"cc( |
| 1031 |
UPB_INLINE $0 $1_$2(const $1* msg) { |
| 1032 |
$3 ret; |
| 1033 |
_upb_msg_map_$2(msg, &ret, $4); |
| 1034 |
return ret; |
| 1035 |
} |
| 1036 |
)cc", |
| 1037 |
CTypeConst(field), msg_name, field->name(), CType(field), |
| 1038 |
field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_STRING |
| 1039 |
? "0" |
| 1040 |
: "sizeof(ret)"); |
| 1041 |
} |
| 1042 |
|
| 1043 |
void GenerateRepeatedGetters(const protobuf::FieldDescriptor* field, |
| 1044 |
const FileLayout& layout, |
| 1045 |
absl::string_view msg_name, Output& output) { |
| 1046 |
output( |
| 1047 |
R"cc( |
| 1048 |
UPB_INLINE $0 const* $1_$2(const $1* msg, size_t* len) { |
| 1049 |
return ($0 const*)_upb_array_accessor(msg, $3, len); |
| 1050 |
} |
| 1051 |
)cc", |
| 1052 |
CTypeConst(field), msg_name, field->name(), layout.GetFieldOffset(field)); |
| 1053 |
} |
| 1054 |
|
| 1055 |
void GenerateOneofGetters(const protobuf::FieldDescriptor* field, |
| 1056 |
const FileLayout& layout, absl::string_view msg_name, |
| 1057 |
Output& output) { |
| 1058 |
output( |
| 1059 |
R"cc( |
| 1060 |
UPB_INLINE $0 $1_$2(const $1* msg) { |
| 1061 |
return UPB_READ_ONEOF(msg, $0, $3, $4, $5, $6); |
| 1062 |
} |
| 1063 |
)cc", |
| 1064 |
CTypeConst(field), msg_name, field->name(), layout.GetFieldOffset(field), |
| 1065 |
layout.GetOneofCaseOffset(field->real_containing_oneof()), |
| 1066 |
field->number(), FieldDefault(field)); |
| 1067 |
} |
| 1068 |
|
| 1069 |
void GenerateScalarGetters(const protobuf::FieldDescriptor* field, |
| 1070 |
const FileLayout& layout, absl::string_view msg_name, |
| 1071 |
Output& output) { |
| 1072 |
if (HasNonZeroDefault(field)) { |
| 1073 |
output( |
| 1074 |
R"cc( |
| 1075 |
UPB_INLINE $0 $1_$2(const $1* msg) { |
| 1076 |
return $1_has_$2(msg) ? *UPB_PTR_AT(msg, $3, $0) : $4; |
| 1077 |
} |
| 1078 |
)cc", |
| 1079 |
CTypeConst(field), msg_name, field->name(), |
| 1080 |
layout.GetFieldOffset(field), FieldDefault(field)); |
| 1081 |
} else { |
| 1082 |
output( |
| 1083 |
R"cc( |
| 1084 |
UPB_INLINE $0 $1_$2(const $1* msg) { |
| 1085 |
return *UPB_PTR_AT(msg, $3, $0); |
| 1086 |
} |
| 1087 |
)cc", |
| 1088 |
CTypeConst(field), msg_name, field->name(), |
| 1089 |
layout.GetFieldOffset(field)); |
| 1090 |
} |
| 1091 |
} |
| 1092 |
|
| 1093 |
void GenerateGetters(const protobuf::FieldDescriptor* field, |
| 1094 |
const FileLayout& layout, absl::string_view msg_name, |
| 1095 |
Output& output) { |
| 1096 |
if (field->is_map()) { |
| 1097 |
GenerateMapGetters(field, layout, msg_name, output); |
| 1098 |
} else if (field->containing_type()->options().map_entry()) { |
| 1099 |
GenerateMapEntryGetters(field, msg_name, output); |
| 1100 |
} else if (field->is_repeated()) { |
| 1101 |
GenerateRepeatedGetters(field, layout, msg_name, output); |
| 1102 |
} else if (field->real_containing_oneof()) { |
| 1103 |
GenerateOneofGetters(field, layout, msg_name, output); |
| 1104 |
} else { |
| 1105 |
GenerateScalarGetters(field, layout, msg_name, output); |
| 1106 |
} |
| 1107 |
} |
| 1108 |
|
| 1109 |
void GenerateMapSetters(const protobuf::FieldDescriptor* field, |
| 1110 |
const FileLayout& layout, absl::string_view msg_name, |
| 1111 |
Output& output) { |
| 1112 |
const protobuf::Descriptor* entry = field->message_type(); |
| 1113 |
const protobuf::FieldDescriptor* key = entry->FindFieldByNumber(1); |
| 1114 |
const protobuf::FieldDescriptor* val = entry->FindFieldByNumber(2); |
| 1115 |
output( |
| 1116 |
R"cc( |
| 1117 |
UPB_INLINE void $0_$1_clear($0* msg) { _upb_msg_map_clear(msg, $2); } |
| 1118 |
)cc", |
| 1119 |
msg_name, field->name(), layout.GetFieldOffset(field)); |
| 1120 |
output( |
| 1121 |
R"cc( |
| 1122 |
UPB_INLINE bool $0_$1_set($0* msg, $2 key, $3 val, upb_Arena* a) { |
| 1123 |
return _upb_msg_map_set(msg, $4, &key, $5, &val, $6, a); |
| 1124 |
} |
| 1125 |
)cc", |
| 1126 |
msg_name, field->name(), CType(key), CType(val), |
| 1127 |
layout.GetFieldOffset(field), |
| 1128 |
key->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_STRING |
| 1129 |
? "0" |
| 1130 |
: "sizeof(key)", |
| 1131 |
val->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_STRING |
| 1132 |
? "0" |
| 1133 |
: "sizeof(val)"); |
| 1134 |
output( |
| 1135 |
R"cc( |
| 1136 |
UPB_INLINE bool $0_$1_delete($0* msg, $2 key) { |
| 1137 |
return _upb_msg_map_delete(msg, $3, &key, $4); |
| 1138 |
} |
| 1139 |
)cc", |
| 1140 |
msg_name, field->name(), CType(key), layout.GetFieldOffset(field), |
| 1141 |
key->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_STRING |
| 1142 |
? "0" |
| 1143 |
: "sizeof(key)"); |
| 1144 |
output( |
| 1145 |
R"cc( |
| 1146 |
UPB_INLINE $0 $1_$2_nextmutable($1* msg, size_t* iter) { |
| 1147 |
return ($0)_upb_msg_map_next(msg, $3, iter); |
| 1148 |
} |
| 1149 |
)cc", |
| 1150 |
CType(field), msg_name, field->name(), layout.GetFieldOffset(field)); |
| 1151 |
} |
| 1152 |
|
| 1153 |
void GenerateRepeatedSetters(const protobuf::FieldDescriptor* field, |
| 1154 |
const FileLayout& layout, |
| 1155 |
absl::string_view msg_name, Output& output) { |
| 1156 |
output( |
| 1157 |
R"cc( |
| 1158 |
UPB_INLINE $0* $1_mutable_$2($1* msg, size_t* len) { |
| 1159 |
return ($0*)_upb_array_mutable_accessor(msg, $3, len); |
| 1160 |
} |
| 1161 |
)cc", |
| 1162 |
CType(field), msg_name, field->name(), layout.GetFieldOffset(field)); |
| 1163 |
output( |
| 1164 |
R"cc( |
| 1165 |
UPB_INLINE $0* $1_resize_$2($1* msg, size_t len, upb_Arena* arena) { |
| 1166 |
return ($0*)_upb_Array_Resize_accessor2(msg, $3, len, $4, arena); |
| 1167 |
} |
| 1168 |
)cc", |
| 1169 |
CType(field), msg_name, field->name(), layout.GetFieldOffset(field), |
| 1170 |
SizeLg2(field)); |
| 1171 |
if (field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE) { |
| 1172 |
output( |
| 1173 |
R"cc( |
| 1174 |
UPB_INLINE struct $0* $1_add_$2($1* msg, upb_Arena* arena) { |
| 1175 |
struct $0* sub = (struct $0*)_upb_Message_New(&$3, arena); |
| 1176 |
bool ok = _upb_Array_Append_accessor2(msg, $4, $5, &sub, arena); |
| 1177 |
if (!ok) return NULL; |
| 1178 |
return sub; |
| 1179 |
} |
| 1180 |
)cc", |
| 1181 |
MessageName(field->message_type()), msg_name, field->name(), |
| 1182 |
MessageInit(field->message_type()), layout.GetFieldOffset(field), |
| 1183 |
SizeLg2(field)); |
| 1184 |
} else { |
| 1185 |
output( |
| 1186 |
R"cc( |
| 1187 |
UPB_INLINE bool $1_add_$2($1* msg, $0 val, upb_Arena* arena) { |
| 1188 |
return _upb_Array_Append_accessor2(msg, $3, $4, &val, arena); |
| 1189 |
} |
| 1190 |
)cc", |
| 1191 |
CType(field), msg_name, field->name(), layout.GetFieldOffset(field), |
| 1192 |
SizeLg2(field)); |
| 1193 |
} |
| 1194 |
} |
| 1195 |
|
| 1196 |
void GenerateNonRepeatedSetters(const protobuf::FieldDescriptor* field, |
| 1197 |
const FileLayout& layout, |
| 1198 |
absl::string_view msg_name, Output& output) { |
| 1199 |
if (field == field->containing_type()->map_key()) { |
| 1200 |
// Key cannot be mutated. |
| 1201 |
return; |
| 1202 |
} |
| 1203 |
|
| 1204 |
// The common function signature for all setters. Varying |
| 1205 |
// implementations follow. |
| 1206 |
output("UPB_INLINE void $0_set_$1($0 *msg, $2 value) {\n", msg_name, |
| 1207 |
field->name(), CType(field)); |
| 1208 |
|
| 1209 |
if (field == field->containing_type()->map_value()) { |
| 1210 |
output( |
| 1211 |
" _upb_msg_map_set_value(msg, &value, $0);\n" |
| 1212 |
"}\n", |
| 1213 |
field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_STRING |
| 1214 |
? "0" |
| 1215 |
: "sizeof(" + CType(field) + ")"); |
| 1216 |
} else if (field->real_containing_oneof()) { |
| 1217 |
output( |
| 1218 |
" UPB_WRITE_ONEOF(msg, $0, $1, value, $2, $3);\n" |
| 1219 |
"}\n", |
| 1220 |
CType(field), layout.GetFieldOffset(field), |
| 1221 |
layout.GetOneofCaseOffset(field->real_containing_oneof()), |
| 1222 |
field->number()); |
| 1223 |
} else { |
| 1224 |
if (layout.HasHasbit(field)) { |
| 1225 |
output(" _upb_sethas(msg, $0);\n", layout.GetHasbitIndex(field)); |
| 1226 |
} |
| 1227 |
output( |
| 1228 |
" *UPB_PTR_AT(msg, $1, $0) = value;\n" |
| 1229 |
"}\n", |
| 1230 |
CType(field), layout.GetFieldOffset(field)); |
| 1231 |
} |
| 1232 |
|
| 1233 |
// Message fields also have a Msg_mutable_foo() accessor that will create |
| 1234 |
// the sub-message if it doesn't already exist. |
| 1235 |
if (field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE && |
| 1236 |
!field->containing_type()->options().map_entry()) { |
| 1237 |
output( |
| 1238 |
R"cc( |
| 1239 |
UPB_INLINE struct $0* $1_mutable_$2($1* msg, upb_Arena* arena) { |
| 1240 |
struct $0* sub = (struct $0*)$1_$2(msg); |
| 1241 |
if (sub == NULL) { |
| 1242 |
sub = (struct $0*)_upb_Message_New(&$3, arena); |
| 1243 |
if (!sub) return NULL; |
| 1244 |
$1_set_$2(msg, sub); |
| 1245 |
} |
| 1246 |
return sub; |
| 1247 |
} |
| 1248 |
)cc", |
| 1249 |
MessageName(field->message_type()), msg_name, field->name(), |
| 1250 |
MessageInit(field->message_type())); |
| 1251 |
} |
| 1252 |
} |
| 1253 |
|
| 1254 |
void GenerateSetters(const protobuf::FieldDescriptor* field, |
| 1255 |
const FileLayout& layout, absl::string_view msg_name, |
| 1256 |
Output& output) { |
| 1257 |
if (field->is_map()) { |
| 1258 |
GenerateMapSetters(field, layout, msg_name, output); |
| 1259 |
} else if (field->is_repeated()) { |
| 1260 |
GenerateRepeatedSetters(field, layout, msg_name, output); |
| 1261 |
} else { |
| 1262 |
GenerateNonRepeatedSetters(field, layout, msg_name, output); |
| 1263 |
} |
| 1264 |
} |
| 1265 |
|
| 1266 |
void GenerateMessageInHeader(const protobuf::Descriptor* message, |
| 1267 |
const FileLayout& layout, Output& output) { |
| 1268 |
output("/* $0 */\n\n", message->full_name()); |
| 1269 |
std::string msg_name = ToCIdent(message->full_name()); |
| 1270 |
|
| 1271 |
if (!message->options().map_entry()) { |
| 1272 |
GenerateMessageFunctionsInHeader(message, output); |
| 1273 |
} |
| 1274 |
|
| 1275 |
for (int i = 0; i < message->real_oneof_decl_count(); i++) { |
| 1276 |
GenerateOneofInHeader(message->oneof_decl(i), layout, msg_name, output); |
| 1277 |
} |
| 1278 |
|
| 1279 |
for (auto field : FieldNumberOrder(message)) { |
| 1280 |
GenerateHazzer(field, layout, msg_name, output); |
| 1281 |
if (field->is_repeated()) { |
| 1282 |
GenerateRepeatedClear(field, layout, msg_name, output); |
| 1283 |
} else { |
| 1284 |
GenerateClear(field, layout, msg_name, output); |
| 1285 |
} |
| 1286 |
GenerateGetters(field, layout, msg_name, output); |
| 1287 |
} |
| 1288 |
|
| 1289 |
output("\n"); |
| 1290 |
|
| 1291 |
for (auto field : FieldNumberOrder(message)) { |
| 1292 |
GenerateSetters(field, layout, msg_name, output); |
| 1293 |
} |
| 1294 |
|
| 1295 |
output("\n"); |
| 1296 |
} |
| 1297 |
|
| 1298 |
void WriteHeader(const FileLayout& layout, Output& output) { |
| 1299 |
const protobuf::FileDescriptor* file = layout.descriptor(); |
| 1300 |
EmitFileWarning(file, output); |
| 1301 |
output( |
| 1302 |
"#ifndef $0_UPB_H_\n" |
| 1303 |
"#define $0_UPB_H_\n\n" |
| 1304 |
"#include \"upb/msg_internal.h\"\n" |
| 1305 |
"#include \"upb/decode.h\"\n" |
| 1306 |
"#include \"upb/decode_fast.h\"\n" |
| 1307 |
"#include \"upb/encode.h\"\n\n", |
| 1308 |
ToPreproc(file->name())); |
| 1309 |
|
| 1310 |
for (int i = 0; i < file->public_dependency_count(); i++) { |
| 1311 |
if (i == 0) { |
| 1312 |
output("/* Public Imports. */\n"); |
| 1313 |
} |
| 1314 |
output("#include \"$0\"\n", HeaderFilename(file)); |
| 1315 |
if (i == file->public_dependency_count() - 1) { |
| 1316 |
output("\n"); |
| 1317 |
} |
| 1318 |
} |
| 1319 |
|
| 1320 |
output( |
| 1321 |
"#include \"upb/port_def.inc\"\n" |
| 1322 |
"\n" |
| 1323 |
"#ifdef __cplusplus\n" |
| 1324 |
"extern \"C\" {\n" |
| 1325 |
"#endif\n" |
| 1326 |
"\n"); |
| 1327 |
|
| 1328 |
const std::vector<const protobuf::Descriptor*> this_file_messages = |
| 1329 |
SortedMessages(file); |
| 1330 |
const std::vector<const protobuf::FieldDescriptor*> this_file_exts = |
| 1331 |
SortedExtensions(file); |
| 1332 |
|
| 1333 |
// Forward-declare types defined in this file. |
| 1334 |
for (auto message : this_file_messages) { |
| 1335 |
output("struct $0;\n", ToCIdent(message->full_name())); |
| 1336 |
} |
| 1337 |
for (auto message : this_file_messages) { |
| 1338 |
output("typedef struct $0 $0;\n", ToCIdent(message->full_name())); |
| 1339 |
} |
| 1340 |
for (auto message : this_file_messages) { |
| 1341 |
output("extern const upb_MiniTable $0;\n", MessageInit(message)); |
| 1342 |
} |
| 1343 |
for (auto ext : this_file_exts) { |
| 1344 |
output("extern const upb_MiniTable_Extension $0;\n", ExtensionLayout(ext)); |
| 1345 |
} |
| 1346 |
|
| 1347 |
// Forward-declare types not in this file, but used as submessages. |
| 1348 |
// Order by full name for consistent ordering. |
| 1349 |
std::map<std::string, const protobuf::Descriptor*> forward_messages; |
| 1350 |
|
| 1351 |
for (auto* message : this_file_messages) { |
| 1352 |
for (int i = 0; i < message->field_count(); i++) { |
| 1353 |
const protobuf::FieldDescriptor* field = message->field(i); |
| 1354 |
if (field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE && |
| 1355 |
field->file() != field->message_type()->file()) { |
| 1356 |
forward_messages[field->message_type()->full_name()] = |
| 1357 |
field->message_type(); |
| 1358 |
} |
| 1359 |
} |
| 1360 |
} |
| 1361 |
for (auto ext : this_file_exts) { |
| 1362 |
if (ext->file() != ext->containing_type()->file()) { |
| 1363 |
forward_messages[ext->containing_type()->full_name()] = |
| 1364 |
ext->containing_type(); |
| 1365 |
} |
| 1366 |
} |
| 1367 |
for (const auto& pair : forward_messages) { |
| 1368 |
output("struct $0;\n", MessageName(pair.second)); |
| 1369 |
} |
| 1370 |
for (const auto& pair : forward_messages) { |
| 1371 |
output("extern const upb_MiniTable $0;\n", MessageInit(pair.second)); |
| 1372 |
} |
| 1373 |
|
| 1374 |
if (!this_file_messages.empty()) { |
| 1375 |
output("\n"); |
| 1376 |
} |
| 1377 |
|
| 1378 |
std::vector<const protobuf::EnumDescriptor*> this_file_enums = |
| 1379 |
SortedEnums(file); |
| 1380 |
std::sort( |
| 1381 |
this_file_enums.begin(), this_file_enums.end(), |
| 1382 |
[](const protobuf::EnumDescriptor* a, const protobuf::EnumDescriptor* b) { |
| 1383 |
return a->full_name() < b->full_name(); |
| 1384 |
}); |
| 1385 |
|
| 1386 |
for (auto enumdesc : this_file_enums) { |
| 1387 |
output("typedef enum {\n"); |
| 1388 |
DumpEnumValues(enumdesc, output); |
| 1389 |
output("} $0;\n\n", ToCIdent(enumdesc->full_name())); |
| 1390 |
} |
| 1391 |
|
| 1392 |
output("\n"); |
| 1393 |
|
| 1394 |
if (file->syntax() == protobuf::FileDescriptor::SYNTAX_PROTO2) { |
| 1395 |
for (const auto* enumdesc : this_file_enums) { |
| 1396 |
output("extern const upb_MiniTable_Enum $0;\n", EnumInit(enumdesc)); |
| 1397 |
} |
| 1398 |
} |
| 1399 |
|
| 1400 |
output("\n"); |
| 1401 |
|
| 1402 |
for (auto message : this_file_messages) { |
| 1403 |
GenerateMessageInHeader(message, layout, output); |
| 1404 |
} |
| 1405 |
|
| 1406 |
for (auto ext : this_file_exts) { |
| 1407 |
GenerateExtensionInHeader(ext, output); |
| 1408 |
} |
| 1409 |
|
| 1410 |
output("extern const upb_MiniTable_File $0;\n\n", FileLayoutName(file)); |
| 1411 |
|
| 1412 |
if (file->name() == |
| 1413 |
protobuf::FileDescriptorProto::descriptor()->file()->name()) { |
| 1414 |
// This is gratuitously inefficient with how many times it rebuilds |
| 1415 |
// MessageLayout objects for the same message. But we only do this for one |
| 1416 |
// proto (descriptor.proto) so we don't worry about it. |
| 1417 |
const protobuf::Descriptor* max32_message = nullptr; |
| 1418 |
const protobuf::Descriptor* max64_message = nullptr; |
| 1419 |
size_t max32 = 0; |
| 1420 |
size_t max64 = 0; |
| 1421 |
for (const auto* message : this_file_messages) { |
| 1422 |
if (absl::EndsWith(message->name(), "Options")) { |
| 1423 |
size_t size32 = layout.GetMiniTable32(message)->size; |
| 1424 |
size_t size64 = layout.GetMiniTable64(message)->size; |
| 1425 |
if (size32 > max32) { |
| 1426 |
max32 = size32; |
| 1427 |
max32_message = message; |
| 1428 |
} |
| 1429 |
if (size64 > max64) { |
| 1430 |
max64 = size64; |
| 1431 |
max64_message = message; |
| 1432 |
} |
| 1433 |
} |
| 1434 |
} |
| 1435 |
|
| 1436 |
output("/* Max size 32 is $0 */\n", max32_message->full_name()); |
| 1437 |
output("/* Max size 64 is $0 */\n", max64_message->full_name()); |
| 1438 |
output("#define _UPB_MAXOPT_SIZE UPB_SIZE($0, $1)\n\n", max32, max64); |
| 1439 |
} |
| 1440 |
|
| 1441 |
output( |
| 1442 |
"#ifdef __cplusplus\n" |
| 1443 |
"} /* extern \"C\" */\n" |
| 1444 |
"#endif\n" |
| 1445 |
"\n" |
| 1446 |
"#include \"upb/port_undef.inc\"\n" |
| 1447 |
"\n" |
| 1448 |
"#endif /* $0_UPB_H_ */\n", |
| 1449 |
ToPreproc(file->name())); |
| 1450 |
} |
| 1451 |
|
| 1452 |
typedef std::pair<std::string, uint64_t> TableEntry; |
| 1453 |
|
| 1454 |
uint64_t GetEncodedTag(const protobuf::FieldDescriptor* field) { |
| 1455 |
protobuf::internal::WireFormatLite::WireType wire_type = |
| 1456 |
protobuf::internal::WireFormat::WireTypeForField(field); |
| 1457 |
uint32_t unencoded_tag = |
| 1458 |
protobuf::internal::WireFormatLite::MakeTag(field->number(), wire_type); |
| 1459 |
uint8_t tag_bytes[10] = {0}; |
| 1460 |
protobuf::io::CodedOutputStream::WriteVarint32ToArray(unencoded_tag, |
| 1461 |
tag_bytes); |
| 1462 |
uint64_t encoded_tag = 0; |
| 1463 |
memcpy(&encoded_tag, tag_bytes, sizeof(encoded_tag)); |
| 1464 |
// TODO: byte-swap for big endian. |
| 1465 |
return encoded_tag; |
| 1466 |
} |
| 1467 |
|
| 1468 |
int GetTableSlot(const protobuf::FieldDescriptor* field) { |
| 1469 |
uint64_t tag = GetEncodedTag(field); |
| 1470 |
if (tag > 0x7fff) { |
| 1471 |
// Tag must fit within a two-byte varint. |
| 1472 |
return -1; |
| 1473 |
} |
| 1474 |
return (tag & 0xf8) >> 3; |
| 1475 |
} |
| 1476 |
|
| 1477 |
bool TryFillTableEntry(const FileLayout& layout, |
| 1478 |
const protobuf::FieldDescriptor* field, |
| 1479 |
TableEntry& ent) { |
| 1480 |
const upb_MiniTable* mt = layout.GetMiniTable64(field->containing_type()); |
| 1481 |
const upb_MiniTable_Field* mt_f = |
| 1482 |
upb_MiniTable_FindFieldByNumber(mt, field->number()); |
| 1483 |
std::string type = ""; |
| 1484 |
std::string cardinality = ""; |
| 1485 |
switch (mt_f->descriptortype) { |
| 1486 |
case kUpb_FieldType_Bool: |
| 1487 |
type = "b1"; |
| 1488 |
break; |
| 1489 |
case kUpb_FieldType_Enum: |
| 1490 |
// We don't have the means to test proto2 enum fields for valid values. |
| 1491 |
return false; |
| 1492 |
case kUpb_FieldType_Int32: |
| 1493 |
case kUpb_FieldType_UInt32: |
| 1494 |
type = "v4"; |
| 1495 |
break; |
| 1496 |
case kUpb_FieldType_Int64: |
| 1497 |
case kUpb_FieldType_UInt64: |
| 1498 |
type = "v8"; |
| 1499 |
break; |
| 1500 |
case kUpb_FieldType_Fixed32: |
| 1501 |
case kUpb_FieldType_SFixed32: |
| 1502 |
case kUpb_FieldType_Float: |
| 1503 |
type = "f4"; |
| 1504 |
break; |
| 1505 |
case kUpb_FieldType_Fixed64: |
| 1506 |
case kUpb_FieldType_SFixed64: |
| 1507 |
case kUpb_FieldType_Double: |
| 1508 |
type = "f8"; |
| 1509 |
break; |
| 1510 |
case kUpb_FieldType_SInt32: |
| 1511 |
type = "z4"; |
| 1512 |
break; |
| 1513 |
case kUpb_FieldType_SInt64: |
| 1514 |
type = "z8"; |
| 1515 |
break; |
| 1516 |
case kUpb_FieldType_String: |
| 1517 |
type = "s"; |
| 1518 |
break; |
| 1519 |
case kUpb_FieldType_Bytes: |
| 1520 |
type = "b"; |
| 1521 |
break; |
| 1522 |
case kUpb_FieldType_Message: |
| 1523 |
type = "m"; |
| 1524 |
break; |
| 1525 |
default: |
| 1526 |
return false; // Not supported yet. |
| 1527 |
} |
| 1528 |
|
| 1529 |
switch (upb_FieldMode_Get(mt_f)) { |
| 1530 |
case kUpb_FieldMode_Map: |
| 1531 |
return false; // Not supported yet (ever?). |
| 1532 |
case kUpb_FieldMode_Array: |
| 1533 |
if (mt_f->mode & kUpb_LabelFlags_IsPacked) { |
| 1534 |
cardinality = "p"; |
| 1535 |
} else { |
| 1536 |
cardinality = "r"; |
| 1537 |
} |
| 1538 |
break; |
| 1539 |
case kUpb_FieldMode_Scalar: |
| 1540 |
if (mt_f->presence < 0) { |
| 1541 |
cardinality = "o"; |
| 1542 |
} else { |
| 1543 |
cardinality = "s"; |
| 1544 |
} |
| 1545 |
break; |
| 1546 |
} |
| 1547 |
|
| 1548 |
uint64_t expected_tag = GetEncodedTag(field); |
| 1549 |
|
| 1550 |
// Data is: |
| 1551 |
// |
| 1552 |
// 48 32 16 0 |
| 1553 |
// |--------|--------|--------|--------|--------|--------|--------|--------| |
| 1554 |
// | offset (16) |case offset (16) |presence| submsg | exp. tag (16) | |
| 1555 |
// |--------|--------|--------|--------|--------|--------|--------|--------| |
| 1556 |
// |
| 1557 |
// - |presence| is either hasbit index or field number for oneofs. |
| 1558 |
|
| 1559 |
uint64_t data = static_cast<uint64_t>(mt_f->offset) << 48 | expected_tag; |
| 1560 |
|
| 1561 |
if (field->is_repeated()) { |
| 1562 |
// No hasbit/oneof-related fields. |
| 1563 |
} |
| 1564 |
if (field->real_containing_oneof()) { |
| 1565 |
size_t case_offset = ~mt_f->presence; |
| 1566 |
if (case_offset > 0xffff) return false; |
| 1567 |
assert(field->number() < 256); |
| 1568 |
data |= field->number() << 24; |
| 1569 |
data |= case_offset << 32; |
| 1570 |
} else { |
| 1571 |
uint64_t hasbit_index = 63; // No hasbit (set a high, unused bit). |
| 1572 |
if (mt_f->presence) { |
| 1573 |
hasbit_index = mt_f->presence; |
| 1574 |
if (hasbit_index > 31) return false; |
| 1575 |
} |
| 1576 |
data |= hasbit_index << 24; |
| 1577 |
} |
| 1578 |
|
| 1579 |
if (field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE) { |
| 1580 |
uint64_t idx = mt_f->submsg_index; |
| 1581 |
if (idx > 255) return false; |
| 1582 |
data |= idx << 16; |
| 1583 |
|
| 1584 |
std::string size_ceil = "max"; |
| 1585 |
size_t size = SIZE_MAX; |
| 1586 |
if (field->message_type()->file() == field->file()) { |
| 1587 |
// We can only be guaranteed the size of the sub-message if it is in the |
| 1588 |
// same file as us. We could relax this to increase the speed of |
| 1589 |
// cross-file sub-message parsing if we are comfortable requiring that |
| 1590 |
// users compile all messages at the same time. |
| 1591 |
const upb_MiniTable* sub_mt = |
| 1592 |
layout.GetMiniTable64(field->message_type()); |
| 1593 |
size = sub_mt->size + 8; |
| 1594 |
} |
| 1595 |
std::vector<size_t> breaks = {64, 128, 192, 256}; |
| 1596 |
for (auto brk : breaks) { |
| 1597 |
if (size <= brk) { |
| 1598 |
size_ceil = std::to_string(brk); |
| 1599 |
break; |
| 1600 |
} |
| 1601 |
} |
| 1602 |
ent.first = absl::Substitute("upb_p$0$1_$2bt_max$3b", cardinality, type, |
| 1603 |
expected_tag > 0xff ? "2" : "1", size_ceil); |
| 1604 |
|
| 1605 |
} else { |
| 1606 |
ent.first = absl::Substitute("upb_p$0$1_$2bt", cardinality, type, |
| 1607 |
expected_tag > 0xff ? "2" : "1"); |
| 1608 |
} |
| 1609 |
ent.second = data; |
| 1610 |
return true; |
| 1611 |
} |
| 1612 |
|
| 1613 |
std::vector<TableEntry> FastDecodeTable(const protobuf::Descriptor* message, |
| 1614 |
const FileLayout& layout) { |
| 1615 |
std::vector<TableEntry> table; |
| 1616 |
for (const auto field : FieldHotnessOrder(message)) { |
| 1617 |
TableEntry ent; |
| 1618 |
int slot = GetTableSlot(field); |
| 1619 |
// std::cerr << "table slot: " << field->number() << ": " << slot << "\n"; |
| 1620 |
if (slot < 0) { |
| 1621 |
// Tag can't fit in the table. |
| 1622 |
continue; |
| 1623 |
} |
| 1624 |
if (!TryFillTableEntry(layout, field, ent)) { |
| 1625 |
// Unsupported field type or offset, hasbit index, etc. doesn't fit. |
| 1626 |
continue; |
| 1627 |
} |
| 1628 |
while ((size_t)slot >= table.size()) { |
| 1629 |
size_t size = std::max(static_cast<size_t>(1), table.size() * 2); |
| 1630 |
table.resize(size, TableEntry{"fastdecode_generic", 0}); |
| 1631 |
} |
| 1632 |
if (table[slot].first != "fastdecode_generic") { |
| 1633 |
// A hotter field already filled this slot. |
| 1634 |
continue; |
| 1635 |
} |
| 1636 |
table[slot] = ent; |
| 1637 |
} |
| 1638 |
return table; |
| 1639 |
} |
| 1640 |
|
| 1641 |
// Returns the field mode as a string initializer. |
| 1642 |
// |
| 1643 |
// We could just emit this as a number (and we may yet go in that direction) but |
| 1644 |
// for now emitting symbolic constants gives this better readability and |
| 1645 |
// debuggability. |
| 1646 |
std::string GetModeInit(uint8_t mode) { |
| 1647 |
std::string ret; |
| 1648 |
switch (mode & kUpb_FieldMode_Mask) { |
| 1649 |
case kUpb_FieldMode_Map: |
| 1650 |
ret = "kUpb_FieldMode_Map"; |
| 1651 |
break; |
| 1652 |
case kUpb_FieldMode_Array: |
| 1653 |
ret = "kUpb_FieldMode_Array"; |
| 1654 |
break; |
| 1655 |
case kUpb_FieldMode_Scalar: |
| 1656 |
ret = "kUpb_FieldMode_Scalar"; |
| 1657 |
break; |
| 1658 |
default: |
| 1659 |
break; |
| 1660 |
} |
| 1661 |
|
| 1662 |
if (mode & kUpb_LabelFlags_IsPacked) { |
| 1663 |
absl::StrAppend(&ret, " | kUpb_LabelFlags_IsPacked"); |
| 1664 |
} |
| 1665 |
|
| 1666 |
if (mode & kUpb_LabelFlags_IsExtension) { |
| 1667 |
absl::StrAppend(&ret, " | kUpb_LabelFlags_IsExtension"); |
| 1668 |
} |
| 1669 |
|
| 1670 |
std::string rep; |
| 1671 |
switch (mode >> kUpb_FieldRep_Shift) { |
| 1672 |
case kUpb_FieldRep_1Byte: |
| 1673 |
rep = "kUpb_FieldRep_1Byte"; |
| 1674 |
break; |
| 1675 |
case kUpb_FieldRep_4Byte: |
| 1676 |
rep = "kUpb_FieldRep_4Byte"; |
| 1677 |
break; |
| 1678 |
case kUpb_FieldRep_Pointer: |
| 1679 |
rep = "kUpb_FieldRep_Pointer"; |
| 1680 |
break; |
| 1681 |
case kUpb_FieldRep_StringView: |
| 1682 |
rep = "kUpb_FieldRep_StringView"; |
| 1683 |
break; |
| 1684 |
case kUpb_FieldRep_8Byte: |
| 1685 |
rep = "kUpb_FieldRep_8Byte"; |
| 1686 |
break; |
| 1687 |
} |
| 1688 |
|
| 1689 |
absl::StrAppend(&ret, " | (", rep, " << kUpb_FieldRep_Shift)"); |
| 1690 |
return ret; |
| 1691 |
} |
| 1692 |
|
| 1693 |
void WriteField(const upb_MiniTable_Field* field64, |
| 1694 |
const upb_MiniTable_Field* field32, Output& output) { |
| 1695 |
output("{$0, UPB_SIZE($1, $2), UPB_SIZE($3, $4), $5, $6, $7}", |
| 1696 |
field64->number, field32->offset, field64->offset, field32->presence, |
| 1697 |
field64->presence, |
| 1698 |
field64->submsg_index == kUpb_NoSub |
| 1699 |
? "kUpb_NoSub" |
| 1700 |
: absl::StrCat(field64->submsg_index).c_str(), |
| 1701 |
field64->descriptortype, GetModeInit(field64->mode)); |
| 1702 |
} |
| 1703 |
|
| 1704 |
// Writes a single field into a .upb.c source file. |
| 1705 |
void WriteMessageField(const upb_MiniTable_Field* field64, |
| 1706 |
const upb_MiniTable_Field* field32, Output& output) { |
| 1707 |
output(" "); |
| 1708 |
WriteField(field64, field32, output); |
| 1709 |
output(",\n"); |
| 1710 |
} |
| 1711 |
|
| 1712 |
// Writes a single message into a .upb.c source file. |
| 1713 |
void WriteMessage(const protobuf::Descriptor* message, const FileLayout& layout, |
| 1714 |
Output& output, bool fasttable_enabled) { |
| 1715 |
std::string msg_name = ToCIdent(message->full_name()); |
| 1716 |
std::string fields_array_ref = "NULL"; |
| 1717 |
std::string submsgs_array_ref = "NULL"; |
| 1718 |
std::string subenums_array_ref = "NULL"; |
| 1719 |
const upb_MiniTable* mt_32 = layout.GetMiniTable32(message); |
| 1720 |
const upb_MiniTable* mt_64 = layout.GetMiniTable64(message); |
| 1721 |
std::vector<std::string> subs; |
| 1722 |
|
| 1723 |
for (int i = 0; i < mt_64->field_count; i++) { |
| 1724 |
const upb_MiniTable_Field* f = &mt_64->fields[i]; |
| 1725 |
if (f->submsg_index != kUpb_NoSub) { |
| 1726 |
subs.push_back(FilePlatformLayout::GetSub(mt_64->subs[f->submsg_index])); |
| 1727 |
} |
| 1728 |
} |
| 1729 |
|
| 1730 |
if (!subs.empty()) { |
| 1731 |
std::string submsgs_array_name = msg_name + "_submsgs"; |
| 1732 |
submsgs_array_ref = "&" + submsgs_array_name + "[0]"; |
| 1733 |
output("static const upb_MiniTable_Sub $0[$1] = {\n", submsgs_array_name, |
| 1734 |
subs.size()); |
| 1735 |
|
| 1736 |
for (const auto& sub : subs) { |
| 1737 |
output(" $0,\n", sub); |
| 1738 |
} |
| 1739 |
|
| 1740 |
output("};\n\n"); |
| 1741 |
} |
| 1742 |
|
| 1743 |
if (mt_64->field_count > 0) { |
| 1744 |
std::string fields_array_name = msg_name + "__fields"; |
| 1745 |
fields_array_ref = "&" + fields_array_name + "[0]"; |
| 1746 |
output("static const upb_MiniTable_Field $0[$1] = {\n", fields_array_name, |
| 1747 |
mt_64->field_count); |
| 1748 |
for (int i = 0; i < mt_64->field_count; i++) { |
| 1749 |
WriteMessageField(&mt_64->fields[i], &mt_32->fields[i], output); |
| 1750 |
} |
| 1751 |
output("};\n\n"); |
| 1752 |
} |
| 1753 |
|
| 1754 |
std::vector<TableEntry> table; |
| 1755 |
uint8_t table_mask = -1; |
| 1756 |
|
| 1757 |
if (fasttable_enabled) { |
| 1758 |
table = FastDecodeTable(message, layout); |
| 1759 |
} |
| 1760 |
|
| 1761 |
if (table.size() > 1) { |
| 1762 |
assert((table.size() & (table.size() - 1)) == 0); |
| 1763 |
table_mask = (table.size() - 1) << 3; |
| 1764 |
} |
| 1765 |
|
| 1766 |
std::string msgext = "kUpb_ExtMode_NonExtendable"; |
| 1767 |
|
| 1768 |
if (message->extension_range_count()) { |
| 1769 |
if (message->options().message_set_wire_format()) { |
| 1770 |
msgext = "kUpb_ExtMode_IsMessageSet"; |
| 1771 |
} else { |
| 1772 |
msgext = "kUpb_ExtMode_Extendable"; |
| 1773 |
} |
| 1774 |
} |
| 1775 |
|
| 1776 |
output("const upb_MiniTable $0 = {\n", MessageInit(message)); |
| 1777 |
output(" $0,\n", submsgs_array_ref); |
| 1778 |
output(" $0,\n", fields_array_ref); |
| 1779 |
output(" $0, $1, $2, $3, $4, $5,\n", layout.GetMessageSize(message), |
| 1780 |
mt_64->field_count, msgext, mt_64->dense_below, table_mask, |
| 1781 |
mt_64->required_count); |
| 1782 |
if (!table.empty()) { |
| 1783 |
output(" UPB_FASTTABLE_INIT({\n"); |
| 1784 |
for (const auto& ent : table) { |
| 1785 |
output(" {0x$1, &$0},\n", ent.first, |
| 1786 |
absl::StrCat(absl::Hex(ent.second, absl::kZeroPad16))); |
| 1787 |
} |
| 1788 |
output(" }),\n"); |
| 1789 |
} |
| 1790 |
output("};\n\n"); |
| 1791 |
} |
| 1792 |
|
| 1793 |
void WriteEnum(const upb_MiniTable_Enum* mt, const protobuf::EnumDescriptor* e, |
| 1794 |
Output& output) { |
| 1795 |
std::string values_init = "NULL"; |
| 1796 |
|
| 1797 |
if (mt->value_count) { |
| 1798 |
values_init = EnumInit(e) + "_values"; |
| 1799 |
output("static const int32_t $0[$1] = {\n", values_init, mt->value_count); |
| 1800 |
for (int i = 0; i < mt->value_count; i++) { |
| 1801 |
output(" $0,\n", mt->values[i]); |
| 1802 |
} |
| 1803 |
output("};\n\n"); |
| 1804 |
} |
| 1805 |
|
| 1806 |
output( |
| 1807 |
R"cc( |
| 1808 |
const upb_MiniTable_Enum $0 = { |
| 1809 |
$1, |
| 1810 |
$2, |
| 1811 |
$3, |
| 1812 |
}; |
| 1813 |
)cc", |
| 1814 |
EnumInit(e), values_init, absl::StrCat("0x", absl::Hex(mt->mask), "ULL"), |
| 1815 |
mt->value_count); |
| 1816 |
output("\n"); |
| 1817 |
} |
| 1818 |
|
| 1819 |
int WriteEnums(const FileLayout& layout, Output& output) { |
| 1820 |
const protobuf::FileDescriptor* file = layout.descriptor(); |
| 1821 |
|
| 1822 |
if (file->syntax() != protobuf::FileDescriptor::SYNTAX_PROTO2) { |
| 1823 |
return 0; |
| 1824 |
} |
| 1825 |
|
| 1826 |
std::vector<const protobuf::EnumDescriptor*> this_file_enums = |
| 1827 |
SortedEnums(file); |
| 1828 |
|
| 1829 |
for (const auto* e : this_file_enums) { |
| 1830 |
WriteEnum(layout.GetEnumTable(e), e, output); |
| 1831 |
} |
| 1832 |
|
| 1833 |
if (!this_file_enums.empty()) { |
| 1834 |
output("static const upb_MiniTable_Enum *$0[$1] = {\n", kEnumsInit, |
| 1835 |
this_file_enums.size()); |
| 1836 |
for (const auto* e : this_file_enums) { |
| 1837 |
output(" &$0,\n", EnumInit(e)); |
| 1838 |
} |
| 1839 |
output("};\n"); |
| 1840 |
output("\n"); |
| 1841 |
} |
| 1842 |
|
| 1843 |
return this_file_enums.size(); |
| 1844 |
} |
| 1845 |
|
| 1846 |
int WriteMessages(const FileLayout& layout, Output& output, |
| 1847 |
bool fasttable_enabled) { |
| 1848 |
const protobuf::FileDescriptor* file = layout.descriptor(); |
| 1849 |
std::vector<const protobuf::Descriptor*> file_messages = SortedMessages(file); |
| 1850 |
|
| 1851 |
if (file_messages.empty()) return 0; |
| 1852 |
|
| 1853 |
for (auto message : file_messages) { |
| 1854 |
WriteMessage(message, layout, output, fasttable_enabled); |
| 1855 |
} |
| 1856 |
|
| 1857 |
output("static const upb_MiniTable *$0[$1] = {\n", kMessagesInit, |
| 1858 |
file_messages.size()); |
| 1859 |
for (auto message : file_messages) { |
| 1860 |
output(" &$0,\n", MessageInit(message)); |
| 1861 |
} |
| 1862 |
output("};\n"); |
| 1863 |
output("\n"); |
| 1864 |
return file_messages.size(); |
| 1865 |
} |
| 1866 |
|
| 1867 |
void WriteExtension(const upb_MiniTable_Extension* ext, Output& output) { |
| 1868 |
WriteField(&ext->field, &ext->field, output); |
| 1869 |
output(",\n"); |
| 1870 |
output(" &$0,\n", reinterpret_cast<const char*>(ext->extendee)); |
| 1871 |
output(" $0,\n", FilePlatformLayout::GetSub(ext->sub)); |
| 1872 |
} |
| 1873 |
|
| 1874 |
int WriteExtensions(const FileLayout& layout, Output& output) { |
| 1875 |
auto exts = SortedExtensions(layout.descriptor()); |
| 1876 |
absl::flat_hash_set<const protobuf::Descriptor*> forward_decls; |
| 1877 |
|
| 1878 |
if (exts.empty()) return 0; |
| 1879 |
|
| 1880 |
// Order by full name for consistent ordering. |
| 1881 |
std::map<std::string, const protobuf::Descriptor*> forward_messages; |
| 1882 |
|
| 1883 |
for (auto ext : exts) { |
| 1884 |
forward_messages[ext->containing_type()->full_name()] = |
| 1885 |
ext->containing_type(); |
| 1886 |
if (ext->message_type()) { |
| 1887 |
forward_messages[ext->message_type()->full_name()] = ext->message_type(); |
| 1888 |
} |
| 1889 |
} |
| 1890 |
|
| 1891 |
for (const auto& decl : forward_messages) { |
| 1892 |
output("extern const upb_MiniTable $0;\n", MessageInit(decl.second)); |
| 1893 |
} |
| 1894 |
|
| 1895 |
for (auto ext : exts) { |
| 1896 |
output("const upb_MiniTable_Extension $0 = {\n ", ExtensionLayout(ext)); |
| 1897 |
WriteExtension(layout.GetExtension(ext), output); |
| 1898 |
output("\n};\n"); |
| 1899 |
} |
| 1900 |
|
| 1901 |
output( |
| 1902 |
"\n" |
| 1903 |
"static const upb_MiniTable_Extension *$0[$1] = {\n", |
| 1904 |
kExtensionsInit, exts.size()); |
| 1905 |
|
| 1906 |
for (auto ext : exts) { |
| 1907 |
output(" &$0,\n", ExtensionLayout(ext)); |
| 1908 |
} |
| 1909 |
|
| 1910 |
output( |
| 1911 |
"};\n" |
| 1912 |
"\n"); |
| 1913 |
return exts.size(); |
| 1914 |
} |
| 1915 |
|
| 1916 |
// Writes a .upb.c source file. |
| 1917 |
void WriteSource(const FileLayout& layout, Output& output, |
| 1918 |
bool fasttable_enabled) { |
| 1919 |
const protobuf::FileDescriptor* file = layout.descriptor(); |
| 1920 |
EmitFileWarning(file, output); |
| 1921 |
|
| 1922 |
output( |
| 1923 |
"#include <stddef.h>\n" |
| 1924 |
"#include \"upb/msg_internal.h\"\n" |
| 1925 |
"#include \"$0\"\n", |
| 1926 |
HeaderFilename(file)); |
| 1927 |
|
| 1928 |
for (int i = 0; i < file->dependency_count(); i++) { |
| 1929 |
output("#include \"$0\"\n", HeaderFilename(file->dependency(i))); |
| 1930 |
} |
| 1931 |
|
| 1932 |
output( |
| 1933 |
"\n" |
| 1934 |
"#include \"upb/port_def.inc\"\n" |
| 1935 |
"\n"); |
| 1936 |
|
| 1937 |
int msg_count = WriteMessages(layout, output, fasttable_enabled); |
| 1938 |
int ext_count = WriteExtensions(layout, output); |
| 1939 |
int enum_count = WriteEnums(layout, output); |
| 1940 |
|
| 1941 |
output("const upb_MiniTable_File $0 = {\n", FileLayoutName(file)); |
| 1942 |
output(" $0,\n", msg_count ? kMessagesInit : "NULL"); |
| 1943 |
output(" $0,\n", enum_count ? kEnumsInit : "NULL"); |
| 1944 |
output(" $0,\n", ext_count ? kExtensionsInit : "NULL"); |
| 1945 |
output(" $0,\n", msg_count); |
| 1946 |
output(" $0,\n", enum_count); |
| 1947 |
output(" $0,\n", ext_count); |
| 1948 |
output("};\n\n"); |
| 1949 |
|
| 1950 |
output("#include \"upb/port_undef.inc\"\n"); |
| 1951 |
output("\n"); |
| 1952 |
} |
| 1953 |
|
| 1954 |
class Generator : public protoc::CodeGenerator { |
| 1955 |
~Generator() override {} |
| 1956 |
bool Generate(const protobuf::FileDescriptor* file, |
| 1957 |
const std::string& parameter, protoc::GeneratorContext* context, |
| 1958 |
std::string* error) const override; |
| 1959 |
uint64_t GetSupportedFeatures() const override { |
| 1960 |
return FEATURE_PROTO3_OPTIONAL; |
| 1961 |
} |
| 1962 |
}; |
| 1963 |
|
| 1964 |
bool Generator::Generate(const protobuf::FileDescriptor* file, |
| 1965 |
const std::string& parameter, |
| 1966 |
protoc::GeneratorContext* context, |
| 1967 |
std::string* error) const { |
| 1968 |
bool fasttable_enabled = false; |
| 1969 |
std::vector<std::pair<std::string, std::string>> params; |
| 1970 |
google::protobuf::compiler::ParseGeneratorParameter(parameter, ¶ms); |
| 1971 |
|
| 1972 |
for (const auto& pair : params) { |
| 1973 |
if (pair.first == "fasttable") { |
| 1974 |
fasttable_enabled = true; |
| 1975 |
} else { |
| 1976 |
*error = "Unknown parameter: " + pair.first; |
| 1977 |
return false; |
| 1978 |
} |
| 1979 |
} |
| 1980 |
|
| 1981 |
FileLayout layout(file); |
| 1982 |
|
| 1983 |
std::unique_ptr<protobuf::io::ZeroCopyOutputStream> h_output_stream( |
| 1984 |
context->Open(HeaderFilename(file))); |
| 1985 |
Output h_output(h_output_stream.get()); |
| 1986 |
WriteHeader(layout, h_output); |
| 1987 |
|
| 1988 |
std::unique_ptr<protobuf::io::ZeroCopyOutputStream> c_output_stream( |
| 1989 |
context->Open(SourceFilename(file))); |
| 1990 |
Output c_output(c_output_stream.get()); |
| 1991 |
WriteSource(layout, c_output, fasttable_enabled); |
| 1992 |
|
| 1993 |
return true; |
| 1994 |
} |
| 1995 |
|
| 1996 |
} // namespace |
| 1997 |
} // namespace upbc |
| 1998 |
|
| 1999 |
int main(int argc, char** argv) { |
| 2000 |
std::unique_ptr<google::protobuf::compiler::CodeGenerator> generator( |
| 2001 |
new upbc::Generator()); |
| 2002 |
return google::protobuf::compiler::PluginMain(argc, argv, generator.get()); |
| 2003 |
} |