|
|
@ -180,24 +180,61 @@ void ExtensionGenerator::GenerateDefinition(io::Printer* p) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool ExtensionGenerator::WillGenerateRegistration(InitPriority priority) { |
|
|
|
bool ExtensionGenerator::WillGenerateRegistration(InitPriority priority) { |
|
|
|
// We only use priority 101 for weak descriptors.
|
|
|
|
// When not using weak descriptors we initialize everything on priority 102.
|
|
|
|
return priority != kInitPriority101 || |
|
|
|
if (!UsingImplicitWeakDescriptor(descriptor_->file(), options_)) { |
|
|
|
(descriptor_->cpp_type() == descriptor_->CPPTYPE_MESSAGE && |
|
|
|
return priority == kInitPriority102; |
|
|
|
UsingImplicitWeakDescriptor(descriptor_->file(), options_)); |
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ExtensionGenerator::GenerateRegistration(io::Printer* p, |
|
|
|
void ExtensionGenerator::GenerateRegistration(io::Printer* p, |
|
|
|
InitPriority priority) { |
|
|
|
InitPriority priority) { |
|
|
|
ABSL_CHECK(WillGenerateRegistration(priority)); |
|
|
|
ABSL_CHECK(WillGenerateRegistration(priority)); |
|
|
|
|
|
|
|
const bool using_implicit_weak_descriptors = |
|
|
|
|
|
|
|
UsingImplicitWeakDescriptor(descriptor_->file(), options_); |
|
|
|
|
|
|
|
const auto find_index = [](auto* desc) { |
|
|
|
|
|
|
|
const std::vector<const Descriptor*> msgs = |
|
|
|
|
|
|
|
FlattenMessagesInFile(desc->file()); |
|
|
|
|
|
|
|
return absl::c_find(msgs, desc) - msgs.begin(); |
|
|
|
|
|
|
|
}; |
|
|
|
auto vars = p->WithVars(variables_); |
|
|
|
auto vars = p->WithVars(variables_); |
|
|
|
|
|
|
|
auto vars2 = p->WithVars({{ |
|
|
|
|
|
|
|
{"extendee_table", |
|
|
|
|
|
|
|
DescriptorTableName(descriptor_->containing_type()->file(), options_)}, |
|
|
|
|
|
|
|
{"extendee_index", find_index(descriptor_->containing_type())}, |
|
|
|
|
|
|
|
{"preregister", priority == kInitPriority101}, |
|
|
|
|
|
|
|
}}); |
|
|
|
switch (descriptor_->cpp_type()) { |
|
|
|
switch (descriptor_->cpp_type()) { |
|
|
|
case FieldDescriptor::CPPTYPE_ENUM: |
|
|
|
case FieldDescriptor::CPPTYPE_ENUM: |
|
|
|
p->Emit({{"enum_name", ClassName(descriptor_->enum_type(), true)}}, |
|
|
|
if (using_implicit_weak_descriptors) { |
|
|
|
R"cc( |
|
|
|
p->Emit({{"enum_name", ClassName(descriptor_->enum_type(), true)}}, |
|
|
|
::_pbi::ExtensionSet::RegisterEnumExtension( |
|
|
|
R"cc( |
|
|
|
&$extendee$::default_instance(), $number$, $field_type$, |
|
|
|
#if defined(PROTOBUF_INTERNAL_TEMPORARY_WEAK_EXTENSION_OPT_IN) |
|
|
|
$repeated$, $packed$, $enum_name$_IsValid), |
|
|
|
(::_pbi::ExtensionSet::ShouldRegisterAtThisTime( |
|
|
|
)cc"); |
|
|
|
{{&$extendee_table$, $extendee_index$}}, $preregister$) |
|
|
|
|
|
|
|
? ::_pbi::ExtensionSet::RegisterEnumExtension( |
|
|
|
|
|
|
|
::_pbi::GetPrototypeForWeakDescriptor( |
|
|
|
|
|
|
|
&$extendee_table$, $extendee_index$, true), |
|
|
|
|
|
|
|
$number$, $field_type$, $repeated$, $packed$, |
|
|
|
|
|
|
|
$enum_name$_IsValid) |
|
|
|
|
|
|
|
: (void)0), |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
)cc"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (priority == kInitPriority102) { |
|
|
|
|
|
|
|
p->Emit({{"enum_name", ClassName(descriptor_->enum_type(), true)}}, |
|
|
|
|
|
|
|
R"cc( |
|
|
|
|
|
|
|
::_pbi::ExtensionSet::RegisterEnumExtension( |
|
|
|
|
|
|
|
&$extendee$::default_instance(), $number$, $field_type$, |
|
|
|
|
|
|
|
$repeated$, $packed$, $enum_name$_IsValid), |
|
|
|
|
|
|
|
)cc"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (using_implicit_weak_descriptors) { |
|
|
|
|
|
|
|
p->Emit(R"cc( |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
)cc"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
break; |
|
|
|
break; |
|
|
|
case FieldDescriptor::CPPTYPE_MESSAGE: { |
|
|
|
case FieldDescriptor::CPPTYPE_MESSAGE: { |
|
|
|
const bool should_verify = |
|
|
|
const bool should_verify = |
|
|
@ -215,64 +252,76 @@ void ExtensionGenerator::GenerateRegistration(io::Printer* p, |
|
|
|
{"lazy", descriptor_->options().has_lazy() |
|
|
|
{"lazy", descriptor_->options().has_lazy() |
|
|
|
? descriptor_->options().lazy() ? "kLazy" : "kEager" |
|
|
|
? descriptor_->options().lazy() ? "kLazy" : "kEager" |
|
|
|
: "kUndefined"}}); |
|
|
|
: "kUndefined"}}); |
|
|
|
const auto register_message = [&] { |
|
|
|
if (using_implicit_weak_descriptors) { |
|
|
|
p->Emit(R"cc( |
|
|
|
|
|
|
|
::_pbi::ExtensionSet::RegisterMessageExtension( |
|
|
|
|
|
|
|
&$extendee$::default_instance(), $number$, $field_type$, |
|
|
|
|
|
|
|
$repeated$, $packed$, &$message_type$::default_instance(), |
|
|
|
|
|
|
|
$verify$, ::_pbi::LazyAnnotation::$lazy$), |
|
|
|
|
|
|
|
)cc"); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
if (UsingImplicitWeakDescriptor(descriptor_->file(), options_)) { |
|
|
|
|
|
|
|
const auto find_index = [](auto* desc) { |
|
|
|
|
|
|
|
const std::vector<const Descriptor*> msgs = |
|
|
|
|
|
|
|
FlattenMessagesInFile(desc->file()); |
|
|
|
|
|
|
|
return absl::c_find(msgs, desc) - msgs.begin(); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
p->Emit( |
|
|
|
p->Emit( |
|
|
|
{ |
|
|
|
{ |
|
|
|
{"extendee_table", |
|
|
|
|
|
|
|
DescriptorTableName(descriptor_->containing_type()->file(), |
|
|
|
|
|
|
|
options_)}, |
|
|
|
|
|
|
|
{"extendee_index", find_index(descriptor_->containing_type())}, |
|
|
|
|
|
|
|
{"extension_table", |
|
|
|
{"extension_table", |
|
|
|
DescriptorTableName(descriptor_->message_type()->file(), |
|
|
|
DescriptorTableName(descriptor_->message_type()->file(), |
|
|
|
options_)}, |
|
|
|
options_)}, |
|
|
|
{"extension_index", find_index(descriptor_->message_type())}, |
|
|
|
{"extension_index", find_index(descriptor_->message_type())}, |
|
|
|
{"preregister", priority == kInitPriority101}, |
|
|
|
|
|
|
|
{"fallback_for_opt_out", |
|
|
|
|
|
|
|
// For now we have a fallback to use normal registration,
|
|
|
|
|
|
|
|
// which should only happen at priority 102.
|
|
|
|
|
|
|
|
// Once we turn this on by default we can remove the opt-in
|
|
|
|
|
|
|
|
// and the fallback.
|
|
|
|
|
|
|
|
[&] { |
|
|
|
|
|
|
|
if (priority != kInitPriority102) return; |
|
|
|
|
|
|
|
register_message(); |
|
|
|
|
|
|
|
}}, |
|
|
|
|
|
|
|
}, |
|
|
|
}, |
|
|
|
R"cc( |
|
|
|
R"cc( |
|
|
|
//
|
|
|
|
|
|
|
|
#if defined(PROTOBUF_INTERNAL_TEMPORARY_WEAK_EXTENSION_OPT_IN) |
|
|
|
#if defined(PROTOBUF_INTERNAL_TEMPORARY_WEAK_EXTENSION_OPT_IN) |
|
|
|
::_pbi::ExtensionSet::RegisterWeakMessageExtension( |
|
|
|
(::_pbi::ExtensionSet::ShouldRegisterAtThisTime( |
|
|
|
{&$extendee_table$, $extendee_index$}, $number$, $field_type$, |
|
|
|
{{&$extendee_table$, $extendee_index$}, |
|
|
|
$repeated$, {&$extension_table$, $extension_index$}, $verify$, |
|
|
|
{&$extension_table$, $extension_index$}}, |
|
|
|
::_pbi::LazyAnnotation::$lazy$, $preregister$), |
|
|
|
$preregister$) |
|
|
|
|
|
|
|
? ::_pbi::ExtensionSet::RegisterMessageExtension( |
|
|
|
|
|
|
|
::_pbi::GetPrototypeForWeakDescriptor( |
|
|
|
|
|
|
|
&$extendee_table$, $extendee_index$, true), |
|
|
|
|
|
|
|
$number$, $field_type$, $repeated$, $packed$, |
|
|
|
|
|
|
|
::_pbi::GetPrototypeForWeakDescriptor( |
|
|
|
|
|
|
|
&$extension_table$, $extension_index$, true), |
|
|
|
|
|
|
|
$verify$, ::_pbi::LazyAnnotation::$lazy$) |
|
|
|
|
|
|
|
: (void)0), |
|
|
|
#else |
|
|
|
#else |
|
|
|
$fallback_for_opt_out$, |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
)cc"); |
|
|
|
)cc"); |
|
|
|
} else { |
|
|
|
} |
|
|
|
register_message(); |
|
|
|
if (priority == kInitPriority102) { |
|
|
|
|
|
|
|
p->Emit(R"cc( |
|
|
|
|
|
|
|
::_pbi::ExtensionSet::RegisterMessageExtension( |
|
|
|
|
|
|
|
&$extendee$::default_instance(), $number$, $field_type$, |
|
|
|
|
|
|
|
$repeated$, $packed$, &$message_type$::default_instance(), |
|
|
|
|
|
|
|
$verify$, ::_pbi::LazyAnnotation::$lazy$), |
|
|
|
|
|
|
|
)cc"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (using_implicit_weak_descriptors) { |
|
|
|
|
|
|
|
p->Emit(R"cc( |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
)cc"); |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
default: |
|
|
|
default: |
|
|
|
p->Emit( |
|
|
|
if (using_implicit_weak_descriptors) { |
|
|
|
R"cc( |
|
|
|
p->Emit(R"cc( |
|
|
|
::_pbi::ExtensionSet::RegisterExtension( |
|
|
|
#if defined(PROTOBUF_INTERNAL_TEMPORARY_WEAK_EXTENSION_OPT_IN) |
|
|
|
&$extendee$::default_instance(), $number$, $field_type$, |
|
|
|
(::_pbi::ExtensionSet::ShouldRegisterAtThisTime( |
|
|
|
$repeated$, $packed$), |
|
|
|
{{&$extendee_table$, $extendee_index$}}, $preregister$) |
|
|
|
)cc"); |
|
|
|
? ::_pbi::ExtensionSet::RegisterExtension( |
|
|
|
|
|
|
|
::_pbi::GetPrototypeForWeakDescriptor(&$extendee_table$, |
|
|
|
|
|
|
|
$extendee_index$, |
|
|
|
|
|
|
|
true), |
|
|
|
|
|
|
|
$number$, $field_type$, $repeated$, $packed$) |
|
|
|
|
|
|
|
: (void)0), |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
)cc"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (priority == kInitPriority102) { |
|
|
|
|
|
|
|
p->Emit( |
|
|
|
|
|
|
|
R"cc( |
|
|
|
|
|
|
|
::_pbi::ExtensionSet::RegisterExtension( |
|
|
|
|
|
|
|
&$extendee$::default_instance(), $number$, $field_type$, |
|
|
|
|
|
|
|
$repeated$, $packed$), |
|
|
|
|
|
|
|
)cc"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (using_implicit_weak_descriptors) { |
|
|
|
|
|
|
|
p->Emit(R"cc( |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
)cc"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|