| 1 |
// Protocol Buffers - Google's data interchange format |
| 2 |
// Copyright 2008 Google Inc. All rights reserved. |
| 3 |
// https://developers.google.com/protocol-buffers/ |
| 4 |
// |
| 5 |
// Redistribution and use in source and binary forms, with or without |
| 6 |
// modification, are permitted provided that the following conditions are |
| 7 |
// met: |
| 8 |
// |
| 9 |
// * Redistributions of source code must retain the above copyright |
| 10 |
// notice, this list of conditions and the following disclaimer. |
| 11 |
// * Redistributions in binary form must reproduce the above |
| 12 |
// copyright notice, this list of conditions and the following disclaimer |
| 13 |
// in the documentation and/or other materials provided with the |
| 14 |
// distribution. |
| 15 |
// * Neither the name of Google Inc. nor the names of its |
| 16 |
// contributors may be used to endorse or promote products derived from |
| 17 |
// this software without specific prior written permission. |
| 18 |
// |
| 19 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
|
| 31 |
#include "google/protobuf/compiler/objectivec/file.h" |
| 32 |
|
| 33 |
#include <algorithm> |
| 34 |
#include <functional> |
| 35 |
#include <iterator> |
| 36 |
#include <memory> |
| 37 |
#include <string> |
| 38 |
#include <vector> |
| 39 |
|
| 40 |
#include "absl/container/btree_set.h" |
| 41 |
#include "absl/container/flat_hash_map.h" |
| 42 |
#include "absl/container/flat_hash_set.h" |
| 43 |
#include "absl/strings/str_cat.h" |
| 44 |
#include "absl/strings/str_join.h" |
| 45 |
#include "google/protobuf/compiler/objectivec/enum.h" |
| 46 |
#include "google/protobuf/compiler/objectivec/extension.h" |
| 47 |
#include "google/protobuf/compiler/objectivec/import_writer.h" |
| 48 |
#include "google/protobuf/compiler/objectivec/message.h" |
| 49 |
#include "google/protobuf/compiler/objectivec/names.h" |
| 50 |
#include "google/protobuf/descriptor.h" |
| 51 |
#include "google/protobuf/descriptor.pb.h" |
| 52 |
#include "google/protobuf/descriptor_legacy.h" |
| 53 |
#include "google/protobuf/io/printer.h" |
| 54 |
|
| 55 |
namespace google { |
| 56 |
namespace protobuf { |
| 57 |
namespace compiler { |
| 58 |
namespace objectivec { |
| 59 |
|
| 60 |
namespace { |
| 61 |
|
| 62 |
// This is also found in GPBBootstrap.h, and needs to be kept in sync. |
| 63 |
const int32_t GOOGLE_PROTOBUF_OBJC_VERSION = 30007; |
| 64 |
|
| 65 |
const char* kHeaderExtension = ".pbobjc.h"; |
| 66 |
|
| 67 |
// Checks if a message contains any extension definitions (on the message or |
| 68 |
// a nested message under it). |
| 69 |
bool MessageContainsExtensions(const Descriptor* message) { |
| 70 |
if (message->extension_count() > 0) { |
| 71 |
return true; |
| 72 |
} |
| 73 |
for (int i = 0; i < message->nested_type_count(); i++) { |
| 74 |
if (MessageContainsExtensions(message->nested_type(i))) { |
| 75 |
return true; |
| 76 |
} |
| 77 |
} |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
// Checks if the file contains any extensions definitions (at the root or |
| 82 |
// nested under a message). |
| 83 |
bool FileContainsExtensions(const FileDescriptor* file) { |
| 84 |
if (file->extension_count() > 0) { |
| 85 |
return true; |
| 86 |
} |
| 87 |
for (int i = 0; i < file->message_type_count(); i++) { |
| 88 |
if (MessageContainsExtensions(file->message_type(i))) { |
| 89 |
return true; |
| 90 |
} |
| 91 |
} |
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
bool IsDirectDependency(const FileDescriptor* dep, const FileDescriptor* file) { |
| 96 |
for (int i = 0; i < file->dependency_count(); i++) { |
| 97 |
if (dep == file->dependency(i)) { |
| 98 |
return true; |
| 99 |
} |
| 100 |
} |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
struct FileDescriptorsOrderedByName { |
| 105 |
inline bool operator()(const FileDescriptor* a, |
| 106 |
const FileDescriptor* b) const { |
| 107 |
return a->name() < b->name(); |
| 108 |
} |
| 109 |
}; |
| 110 |
|
| 111 |
void MakeDescriptors( |
| 112 |
const Descriptor* descriptor, const std::string& file_description_name, |
| 113 |
std::vector<std::unique_ptr<EnumGenerator>>* enum_generators, |
| 114 |
std::vector<std::unique_ptr<ExtensionGenerator>>* extension_generators, |
| 115 |
std::vector<std::unique_ptr<MessageGenerator>>* message_generators) { |
| 116 |
for (int i = 0; i < descriptor->enum_type_count(); i++) { |
| 117 |
enum_generators->emplace_back( |
| 118 |
std::make_unique<EnumGenerator>(descriptor->enum_type(i))); |
| 119 |
} |
| 120 |
for (int i = 0; i < descriptor->nested_type_count(); i++) { |
| 121 |
message_generators->emplace_back(std::make_unique<MessageGenerator>( |
| 122 |
file_description_name, descriptor->nested_type(i))); |
| 123 |
message_generators->back()->AddExtensionGenerators(extension_generators); |
| 124 |
MakeDescriptors(descriptor->nested_type(i), file_description_name, |
| 125 |
enum_generators, extension_generators, message_generators); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
void EmitSourceFwdDecls(const absl::btree_set<std::string>& fwd_decls, |
| 130 |
io::Printer* p) { |
| 131 |
if (fwd_decls.empty()) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
p->Emit({{"fwd_decls", absl::StrJoin(fwd_decls, "\n")}}, |
| 136 |
R"objc( |
| 137 |
#pragma mark - Objective-C Class declarations |
| 138 |
// Forward declarations of Objective-C classes that we can use as |
| 139 |
// static values in struct initializers. |
| 140 |
// We don't use [Foo class] because it is not a static value. |
| 141 |
$fwd_decls$ |
| 142 |
)objc"); |
| 143 |
p->Emit("\n"); |
| 144 |
} |
| 145 |
|
| 146 |
} // namespace |
| 147 |
|
| 148 |
const FileGenerator::CommonState::MinDepsEntry& |
| 149 |
FileGenerator::CommonState::CollectMinimalFileDepsContainingExtensionsInternal( |
| 150 |
const FileDescriptor* file) { |
| 151 |
auto it = deps_info_cache.find(file); |
| 152 |
if (it != deps_info_cache.end()) { |
| 153 |
return it->second; |
| 154 |
} |
| 155 |
|
| 156 |
absl::flat_hash_set<const FileDescriptor*> min_deps_collector; |
| 157 |
absl::flat_hash_set<const FileDescriptor*> transitive_deps_collector; |
| 158 |
absl::flat_hash_set<const FileDescriptor*> to_prune; |
| 159 |
for (int i = 0; i < file->dependency_count(); i++) { |
| 160 |
const FileDescriptor* dep = file->dependency(i); |
| 161 |
MinDepsEntry dep_info = |
| 162 |
CollectMinimalFileDepsContainingExtensionsInternal(dep); |
| 163 |
|
| 164 |
// Everything the dep covered, this file will also cover. |
| 165 |
transitive_deps_collector.insert(dep_info.transitive_deps.begin(), |
| 166 |
dep_info.transitive_deps.end()); |
| 167 |
// Prune everything from the dep's covered list in case another dep lists it |
| 168 |
// as a min dep. |
| 169 |
to_prune.insert(dep_info.transitive_deps.begin(), |
| 170 |
dep_info.transitive_deps.end()); |
| 171 |
|
| 172 |
// Does the dep have any extensions... |
| 173 |
if (dep_info.has_extensions) { |
| 174 |
// Yes -> Add this file, prune its min_deps and add them to the covered |
| 175 |
// deps. |
| 176 |
min_deps_collector.insert(dep); |
| 177 |
to_prune.insert(dep_info.min_deps.begin(), dep_info.min_deps.end()); |
| 178 |
transitive_deps_collector.insert(dep_info.min_deps.begin(), |
| 179 |
dep_info.min_deps.end()); |
| 180 |
} else { |
| 181 |
// No -> Just use its min_deps. |
| 182 |
min_deps_collector.insert(dep_info.min_deps.begin(), |
| 183 |
dep_info.min_deps.end()); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
const bool file_has_exts = FileContainsExtensions(file); |
| 188 |
|
| 189 |
// Fast path: if nothing to prune or there was only one dep, the prune work is |
| 190 |
// a waste, skip it. |
| 191 |
if (to_prune.empty() || file->dependency_count() == 1) { |
| 192 |
return deps_info_cache |
| 193 |
.insert( |
| 194 |
{file, |
| 195 |
{file_has_exts, min_deps_collector, transitive_deps_collector}}) |
| 196 |
.first->second; |
| 197 |
} |
| 198 |
|
| 199 |
absl::flat_hash_set<const FileDescriptor*> min_deps; |
| 200 |
std::copy_if(min_deps_collector.begin(), min_deps_collector.end(), |
| 201 |
std::inserter(min_deps, min_deps.begin()), |
| 202 |
[&](const FileDescriptor* value) { |
| 203 |
return to_prune.find(value) == to_prune.end(); |
| 204 |
}); |
| 205 |
return deps_info_cache |
| 206 |
.insert({file, {file_has_exts, min_deps, transitive_deps_collector}}) |
| 207 |
.first->second; |
| 208 |
} |
| 209 |
|
| 210 |
// Collect the deps of the given file that contain extensions. This can be used |
| 211 |
// to create the chain of roots that need to be wired together. |
| 212 |
// |
| 213 |
// NOTE: If any changes are made to this and the supporting functions, you will |
| 214 |
// need to manually validate what the generated code is for the test files: |
| 215 |
// objectivec/Tests/unittest_extension_chain_*.proto |
| 216 |
// There are comments about what the expected code should be line and limited |
| 217 |
// testing objectivec/Tests/GPBUnittestProtos2.m around compilation (#imports |
| 218 |
// specifically). |
| 219 |
std::vector<const FileDescriptor*> |
| 220 |
FileGenerator::CommonState::CollectMinimalFileDepsContainingExtensions( |
| 221 |
const FileDescriptor* file) { |
| 222 |
absl::flat_hash_set<const FileDescriptor*> min_deps = |
| 223 |
CollectMinimalFileDepsContainingExtensionsInternal(file).min_deps; |
| 224 |
// Sort the list since pointer order isn't stable across runs. |
| 225 |
std::vector<const FileDescriptor*> result(min_deps.begin(), min_deps.end()); |
| 226 |
std::sort(result.begin(), result.end(), FileDescriptorsOrderedByName()); |
| 227 |
return result; |
| 228 |
} |
| 229 |
|
| 230 |
FileGenerator::FileGenerator(const FileDescriptor* file, |
| 231 |
const GenerationOptions& generation_options, |
| 232 |
CommonState& common_state) |
| 233 |
: file_(file), |
| 234 |
generation_options_(generation_options), |
| 235 |
common_state_(&common_state), |
| 236 |
root_class_name_(FileClassName(file)), |
| 237 |
file_description_name_(FileClassName(file) + "_FileDescription"), |
| 238 |
is_bundled_proto_(IsProtobufLibraryBundledProtoFile(file)) { |
| 239 |
for (int i = 0; i < file_->enum_type_count(); i++) { |
| 240 |
enum_generators_.emplace_back( |
| 241 |
std::make_unique<EnumGenerator>(file_->enum_type(i))); |
| 242 |
} |
| 243 |
for (int i = 0; i < file_->extension_count(); i++) { |
| 244 |
extension_generators_.push_back(std::make_unique<ExtensionGenerator>( |
| 245 |
root_class_name_, file_->extension(i))); |
| 246 |
} |
| 247 |
for (int i = 0; i < file_->message_type_count(); i++) { |
| 248 |
message_generators_.emplace_back(std::make_unique<MessageGenerator>( |
| 249 |
file_description_name_, file_->message_type(i))); |
| 250 |
message_generators_.back()->AddExtensionGenerators(&extension_generators_); |
| 251 |
MakeDescriptors(file_->message_type(i), file_description_name_, |
| 252 |
&enum_generators_, &extension_generators_, |
| 253 |
&message_generators_); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
void FileGenerator::GenerateHeader(io::Printer* p) const { |
| 258 |
GenerateFile(p, GeneratedFileType::kHeader, [&] { |
| 259 |
absl::btree_set<std::string> fwd_decls; |
| 260 |
for (const auto& generator : message_generators_) { |
| 261 |
generator->DetermineForwardDeclarations(&fwd_decls, |
| 262 |
/* include_external_types = */ |
| 263 |
HeadersUseForwardDeclarations()); |
| 264 |
} |
| 265 |
|
| 266 |
p->Emit("CF_EXTERN_C_BEGIN\n\n"); |
| 267 |
|
| 268 |
if (!fwd_decls.empty()) { |
| 269 |
p->Emit({{"fwd_decls", absl::StrJoin(fwd_decls, "\n")}}, |
| 270 |
"$fwd_decls$\n\n"); |
| 271 |
} |
| 272 |
|
| 273 |
p->Emit("NS_ASSUME_NONNULL_BEGIN\n\n"); |
| 274 |
|
| 275 |
for (const auto& generator : enum_generators_) { |
| 276 |
generator->GenerateHeader(p); |
| 277 |
} |
| 278 |
|
| 279 |
// For extensions to chain together, the Root gets created even if there |
| 280 |
// are no extensions. |
| 281 |
p->Emit(R"objc( |
| 282 |
#pragma mark - $root_class_name$ |
| 283 |
|
| 284 |
/** |
| 285 |
* Exposes the extension registry for this file. |
| 286 |
* |
| 287 |
* The base class provides: |
| 288 |
* @code |
| 289 |
* + (GPBExtensionRegistry *)extensionRegistry; |
| 290 |
* @endcode |
| 291 |
* which is a @c GPBExtensionRegistry that includes all the extensions defined by |
| 292 |
* this file and all files that it depends on. |
| 293 |
**/ |
| 294 |
GPB_FINAL @interface $root_class_name$ : GPBRootObject |
| 295 |
@end |
| 296 |
)objc"); |
| 297 |
p->Emit("\n"); |
| 298 |
|
| 299 |
// The dynamic methods block is only needed if there are extensions that are |
| 300 |
// file level scoped (not message scoped). The first |
| 301 |
// file_->extension_count() of extension_generators_ are the file scoped |
| 302 |
// ones. |
| 303 |
if (file_->extension_count()) { |
| 304 |
p->Emit("@interface $root_class_name$ (DynamicMethods)\n"); |
| 305 |
|
| 306 |
for (int i = 0; i < file_->extension_count(); i++) { |
| 307 |
extension_generators_[i]->GenerateMembersHeader(p); |
| 308 |
} |
| 309 |
|
| 310 |
p->Emit("@end\n\n"); |
| 311 |
} // file_->extension_count() |
| 312 |
|
| 313 |
for (const auto& generator : message_generators_) { |
| 314 |
generator->GenerateMessageHeader(p); |
| 315 |
} |
| 316 |
|
| 317 |
p->Emit(R"objc( |
| 318 |
NS_ASSUME_NONNULL_END |
| 319 |
|
| 320 |
CF_EXTERN_C_END |
| 321 |
)objc"); |
| 322 |
}); |
| 323 |
} |
| 324 |
|
| 325 |
void FileGenerator::GenerateSource(io::Printer* p) const { |
| 326 |
std::vector<const FileDescriptor*> deps_with_extensions = |
| 327 |
common_state_->CollectMinimalFileDepsContainingExtensions(file_); |
| 328 |
GeneratedFileOptions file_options; |
| 329 |
|
| 330 |
// If any indirect dependency provided extensions, it needs to be directly |
| 331 |
// imported so it can get merged into the root's extensions registry. |
| 332 |
// See the Note by CollectMinimalFileDepsContainingExtensions before |
| 333 |
// changing this. |
| 334 |
for (auto& dep : deps_with_extensions) { |
| 335 |
if (!IsDirectDependency(dep, file_)) { |
| 336 |
file_options.extra_files_to_import.push_back(dep); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
absl::btree_set<std::string> fwd_decls; |
| 341 |
for (const auto& generator : message_generators_) { |
| 342 |
generator->DetermineObjectiveCClassDefinitions(&fwd_decls); |
| 343 |
} |
| 344 |
for (const auto& generator : extension_generators_) { |
| 345 |
generator->DetermineObjectiveCClassDefinitions(&fwd_decls); |
| 346 |
} |
| 347 |
|
| 348 |
// The generated code for oneof's uses direct ivar access, suppress the |
| 349 |
// warning in case developer turn that on in the context they compile the |
| 350 |
// generated code. |
| 351 |
for (const auto& generator : message_generators_) { |
| 352 |
if (generator->IncludesOneOfDefinition()) { |
| 353 |
file_options.ignored_warnings.push_back("direct-ivar-access"); |
| 354 |
break; |
| 355 |
} |
| 356 |
} |
| 357 |
if (!fwd_decls.empty()) { |
| 358 |
file_options.ignored_warnings.push_back("dollar-in-identifier-extension"); |
| 359 |
} |
| 360 |
|
| 361 |
// Enum implementation uses atomic in the generated code, so add |
| 362 |
// the system import as needed. |
| 363 |
if (!enum_generators_.empty()) { |
| 364 |
file_options.extra_system_headers.push_back("stdatomic.h"); |
| 365 |
} |
| 366 |
|
| 367 |
GenerateFile(p, GeneratedFileType::kSource, file_options, [&] { |
| 368 |
EmitSourceFwdDecls(fwd_decls, p); |
| 369 |
EmitRootImplementation(p, deps_with_extensions); |
| 370 |
EmitFileDescription(p); |
| 371 |
|
| 372 |
for (const auto& generator : enum_generators_) { |
| 373 |
generator->GenerateSource(p); |
| 374 |
} |
| 375 |
for (const auto& generator : message_generators_) { |
| 376 |
generator->GenerateSource(p); |
| 377 |
} |
| 378 |
}); |
| 379 |
} |
| 380 |
|
| 381 |
void FileGenerator::GenerateGlobalSource(io::Printer* p) const { |
| 382 |
std::vector<const FileDescriptor*> deps_with_extensions = |
| 383 |
common_state_->CollectMinimalFileDepsContainingExtensions(file_); |
| 384 |
GeneratedFileOptions file_options; |
| 385 |
|
| 386 |
// If any indirect dependency provided extensions, it needs to be directly |
| 387 |
// imported so it can get merged into the root's extensions registry. |
| 388 |
// See the Note by CollectMinimalFileDepsContainingExtensions before |
| 389 |
// changing this. |
| 390 |
for (auto& dep : deps_with_extensions) { |
| 391 |
if (!IsDirectDependency(dep, file_)) { |
| 392 |
file_options.extra_files_to_import.push_back(dep); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
absl::btree_set<std::string> fwd_decls; |
| 397 |
for (const auto& generator : extension_generators_) { |
| 398 |
generator->DetermineObjectiveCClassDefinitions(&fwd_decls); |
| 399 |
} |
| 400 |
|
| 401 |
if (!fwd_decls.empty()) { |
| 402 |
file_options.ignored_warnings.push_back("dollar-in-identifier-extension"); |
| 403 |
} |
| 404 |
|
| 405 |
GenerateFile(p, GeneratedFileType::kSource, file_options, [&] { |
| 406 |
EmitSourceFwdDecls(fwd_decls, p); |
| 407 |
EmitRootImplementation(p, deps_with_extensions); |
| 408 |
}); |
| 409 |
} |
| 410 |
|
| 411 |
void FileGenerator::GenerateSourceForEnums(io::Printer* p) const { |
| 412 |
// Enum implementation uses atomic in the generated code. |
| 413 |
GeneratedFileOptions file_options; |
| 414 |
file_options.extra_system_headers.push_back("stdatomic.h"); |
| 415 |
|
| 416 |
GenerateFile(p, GeneratedFileType::kSource, file_options, [&] { |
| 417 |
for (const auto& generator : enum_generators_) { |
| 418 |
generator->GenerateSource(p); |
| 419 |
} |
| 420 |
}); |
| 421 |
} |
| 422 |
|
| 423 |
void FileGenerator::GenerateSourceForMessage(int idx, io::Printer* p) const { |
| 424 |
const auto& generator = message_generators_[idx]; |
| 425 |
|
| 426 |
absl::btree_set<std::string> fwd_decls; |
| 427 |
generator->DetermineObjectiveCClassDefinitions(&fwd_decls); |
| 428 |
|
| 429 |
GeneratedFileOptions file_options; |
| 430 |
// The generated code for oneof's uses direct ivar access, suppress the |
| 431 |
// warning in case developer turn that on in the context they compile the |
| 432 |
// generated code. |
| 433 |
if (generator->IncludesOneOfDefinition()) { |
| 434 |
file_options.ignored_warnings.push_back("direct-ivar-access"); |
| 435 |
} |
| 436 |
|
| 437 |
GenerateFile(p, GeneratedFileType::kSource, file_options, [&] { |
| 438 |
EmitSourceFwdDecls(fwd_decls, p); |
| 439 |
EmitFileDescription(p); |
| 440 |
generator->GenerateSource(p); |
| 441 |
}); |
| 442 |
} |
| 443 |
|
| 444 |
void FileGenerator::GenerateFile(io::Printer* p, GeneratedFileType file_type, |
| 445 |
const GeneratedFileOptions& file_options, |
| 446 |
std::function<void()> body) const { |
| 447 |
ImportWriter import_writer( |
| 448 |
generation_options_.generate_for_named_framework, |
| 449 |
generation_options_.named_framework_to_proto_path_mappings_path, |
| 450 |
generation_options_.runtime_import_prefix, |
| 451 |
/* for_bundled_proto = */ is_bundled_proto_); |
| 452 |
const std::string header_extension(kHeaderExtension); |
| 453 |
|
| 454 |
switch (file_type) { |
| 455 |
case GeneratedFileType::kHeader: |
| 456 |
// Generated files bundled with the library get minimal imports, |
| 457 |
// everything else gets the wrapper so everything is usable. |
| 458 |
if (is_bundled_proto_) { |
| 459 |
import_writer.AddRuntimeImport("GPBDescriptor.h"); |
| 460 |
import_writer.AddRuntimeImport("GPBMessage.h"); |
| 461 |
import_writer.AddRuntimeImport("GPBRootObject.h"); |
| 462 |
} else { |
| 463 |
import_writer.AddRuntimeImport("GPBProtocolBuffers.h"); |
| 464 |
} |
| 465 |
if (HeadersUseForwardDeclarations()) { |
| 466 |
// #import any headers for "public imports" in the proto file. |
| 467 |
for (int i = 0; i < file_->public_dependency_count(); i++) { |
| 468 |
import_writer.AddFile(file_->public_dependency(i), header_extension); |
| 469 |
} |
| 470 |
} else { |
| 471 |
for (int i = 0; i < file_->dependency_count(); i++) { |
| 472 |
import_writer.AddFile(file_->dependency(i), header_extension); |
| 473 |
} |
| 474 |
} |
| 475 |
break; |
| 476 |
case GeneratedFileType::kSource: |
| 477 |
import_writer.AddRuntimeImport("GPBProtocolBuffers_RuntimeSupport.h"); |
| 478 |
import_writer.AddFile(file_, header_extension); |
| 479 |
if (HeadersUseForwardDeclarations()) { |
| 480 |
// #import the headers for anything that a plain dependency of this |
| 481 |
// proto file (that means they were just an include, not a "public" |
| 482 |
// include). |
| 483 |
absl::flat_hash_set<std::string> public_import_names; |
| 484 |
for (int i = 0; i < file_->public_dependency_count(); i++) { |
| 485 |
public_import_names.insert(file_->public_dependency(i)->name()); |
| 486 |
} |
| 487 |
for (int i = 0; i < file_->dependency_count(); i++) { |
| 488 |
const FileDescriptor* dep = file_->dependency(i); |
| 489 |
if (!public_import_names.contains(dep->name())) { |
| 490 |
import_writer.AddFile(dep, header_extension); |
| 491 |
} |
| 492 |
} |
| 493 |
} |
| 494 |
break; |
| 495 |
} |
| 496 |
|
| 497 |
for (const auto& dep : file_options.extra_files_to_import) { |
| 498 |
import_writer.AddFile(dep, header_extension); |
| 499 |
} |
| 500 |
|
| 501 |
// Some things for all Emit() calls to have access too. |
| 502 |
auto vars = p->WithVars({ |
| 503 |
{// Avoid the directive within the template strings as the tool would |
| 504 |
// then honor the directives within the generators sources. |
| 505 |
"clangfmt", "clang-format"}, |
| 506 |
{"root_class_name", root_class_name_}, |
| 507 |
}); |
| 508 |
|
| 509 |
p->Emit( |
| 510 |
{ |
| 511 |
{"filename", file_->name()}, |
| 512 |
{"google_protobuf_objc_version", GOOGLE_PROTOBUF_OBJC_VERSION}, |
| 513 |
{"runtime_imports", |
| 514 |
[&] { |
| 515 |
import_writer.PrintRuntimeImports( |
| 516 |
p, /* default_cpp_symbol = */ !is_bundled_proto_); |
| 517 |
}}, |
| 518 |
{"extra_system_imports", |
| 519 |
[&] { |
| 520 |
if (file_options.extra_system_headers.empty()) { |
| 521 |
return; |
| 522 |
} |
| 523 |
for (const auto& system_header : |
| 524 |
file_options.extra_system_headers) { |
| 525 |
p->Emit({{"header", system_header}}, |
| 526 |
R"objc( |
| 527 |
#import <$header$> |
| 528 |
)objc"); |
| 529 |
} |
| 530 |
p->Emit("\n"); |
| 531 |
}}, |
| 532 |
{"file_imports", [&] { import_writer.PrintFileImports(p); }}, |
| 533 |
{"extra_warnings", |
| 534 |
[&] { |
| 535 |
for (const auto& warning : file_options.ignored_warnings) { |
| 536 |
p->Emit({{"warning", warning}}, |
| 537 |
R"objc( |
| 538 |
#pragma clang diagnostic ignored "-W$warning$" |
| 539 |
)objc"); |
| 540 |
} |
| 541 |
}}, |
| 542 |
}, |
| 543 |
R"objc( |
| 544 |
// Generated by the protocol buffer compiler. DO NOT EDIT! |
| 545 |
// $clangfmt$ off |
| 546 |
// source: $filename$ |
| 547 |
|
| 548 |
$runtime_imports$ |
| 549 |
|
| 550 |
#if GOOGLE_PROTOBUF_OBJC_VERSION < $google_protobuf_objc_version$ |
| 551 |
#error This file was generated by a newer version of protoc which is incompatible with your Protocol Buffer library sources. |
| 552 |
#endif |
| 553 |
#if $google_protobuf_objc_version$ < GOOGLE_PROTOBUF_OBJC_MIN_SUPPORTED_VERSION |
| 554 |
#error This file was generated by an older version of protoc which is incompatible with your Protocol Buffer library sources. |
| 555 |
#endif |
| 556 |
|
| 557 |
$extra_system_imports$ |
| 558 |
$file_imports$ |
| 559 |
// @@protoc_insertion_point(imports) |
| 560 |
|
| 561 |
#pragma clang diagnostic push |
| 562 |
#pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 563 |
$extra_warnings$ |
| 564 |
)objc"); |
| 565 |
|
| 566 |
p->Emit("\n"); |
| 567 |
|
| 568 |
body(); |
| 569 |
|
| 570 |
p->Emit("\n"); |
| 571 |
|
| 572 |
p->Emit(R"objc( |
| 573 |
#pragma clang diagnostic pop |
| 574 |
|
| 575 |
// @@protoc_insertion_point(global_scope) |
| 576 |
|
| 577 |
// $clangfmt$ on |
| 578 |
)objc"); |
| 579 |
} |
| 580 |
|
| 581 |
void FileGenerator::EmitRootImplementation( |
| 582 |
io::Printer* p, |
| 583 |
const std::vector<const FileDescriptor*>& deps_with_extensions) const { |
| 584 |
p->Emit( |
| 585 |
R"objc( |
| 586 |
#pragma mark - $root_class_name$ |
| 587 |
|
| 588 |
@implementation $root_class_name$ |
| 589 |
)objc"); |
| 590 |
|
| 591 |
p->Emit("\n"); |
| 592 |
|
| 593 |
// If there were any extensions or this file has any dependencies, |
| 594 |
// output a registry to override to create the file specific |
| 595 |
// registry. |
| 596 |
if (extension_generators_.empty() && deps_with_extensions.empty()) { |
| 597 |
if (file_->dependency_count() == 0) { |
| 598 |
p->Emit(R"objc( |
| 599 |
// No extensions in the file and no imports, so no need to generate |
| 600 |
// +extensionRegistry. |
| 601 |
)objc"); |
| 602 |
} else { |
| 603 |
p->Emit(R"objc( |
| 604 |
// No extensions in the file and none of the imports (direct or indirect) |
| 605 |
// defined extensions, so no need to generate +extensionRegistry. |
| 606 |
)objc"); |
| 607 |
} |
| 608 |
} else { |
| 609 |
EmitRootExtensionRegistryImplementation(p, deps_with_extensions); |
| 610 |
} |
| 611 |
|
| 612 |
p->Emit("\n"); |
| 613 |
p->Emit("@end\n\n"); |
| 614 |
} |
| 615 |
|
| 616 |
void FileGenerator::EmitRootExtensionRegistryImplementation( |
| 617 |
io::Printer* p, |
| 618 |
const std::vector<const FileDescriptor*>& deps_with_extensions) const { |
| 619 |
p->Emit( |
| 620 |
{ |
| 621 |
{"register_local_extensions", |
| 622 |
[&] { |
| 623 |
if (extension_generators_.empty()) { |
| 624 |
return; |
| 625 |
} |
| 626 |
p->Emit( |
| 627 |
{ |
| 628 |
{"register_local_extensions_variable_blocks", |
| 629 |
[&] { |
| 630 |
for (const auto& generator : extension_generators_) { |
| 631 |
generator->GenerateStaticVariablesInitialization(p); |
| 632 |
} |
| 633 |
}}, |
| 634 |
}, |
| 635 |
R"objc( |
| 636 |
static GPBExtensionDescription descriptions[] = { |
| 637 |
$register_local_extensions_variable_blocks$ |
| 638 |
}; |
| 639 |
for (size_t i = 0; i < sizeof(descriptions) / sizeof(descriptions[0]); ++i) { |
| 640 |
GPBExtensionDescriptor *extension = |
| 641 |
[[GPBExtensionDescriptor alloc] initWithExtensionDescription:&descriptions[i] |
| 642 |
usesClassRefs:YES]; |
| 643 |
[registry addExtension:extension]; |
| 644 |
[self globallyRegisterExtension:extension]; |
| 645 |
[extension release]; |
| 646 |
} |
| 647 |
)objc"); |
| 648 |
}}, |
| 649 |
{"register_imports", |
| 650 |
[&] { |
| 651 |
if (deps_with_extensions.empty()) { |
| 652 |
p->Emit(R"objc( |
| 653 |
// None of the imports (direct or indirect) defined extensions, so no need to add |
| 654 |
// them to this registry. |
| 655 |
)objc"); |
| 656 |
} else { |
| 657 |
p->Emit(R"objc( |
| 658 |
// Merge in the imports (direct or indirect) that defined extensions. |
| 659 |
)objc"); |
| 660 |
for (const auto& dep : deps_with_extensions) { |
| 661 |
p->Emit({{"dependency", FileClassName(dep)}}, |
| 662 |
R"objc( |
| 663 |
[registry addExtensions:[$dependency$ extensionRegistry]]; |
| 664 |
)objc"); |
| 665 |
} |
| 666 |
} |
| 667 |
}}, |
| 668 |
}, |
| 669 |
R"objc( |
| 670 |
+ (GPBExtensionRegistry*)extensionRegistry { |
| 671 |
// This is called by +initialize so there is no need to worry |
| 672 |
// about thread safety and initialization of registry. |
| 673 |
static GPBExtensionRegistry* registry = nil; |
| 674 |
if (!registry) { |
| 675 |
GPB_DEBUG_CHECK_RUNTIME_VERSIONS(); |
| 676 |
registry = [[GPBExtensionRegistry alloc] init]; |
| 677 |
$register_local_extensions$; |
| 678 |
$register_imports$ |
| 679 |
} |
| 680 |
return registry; |
| 681 |
} |
| 682 |
)objc"); |
| 683 |
} |
| 684 |
|
| 685 |
void FileGenerator::EmitFileDescription(io::Printer* p) const { |
| 686 |
// File descriptor only needed if there are messages to use it. |
| 687 |
if (message_generators_.empty()) { |
| 688 |
return; |
| 689 |
} |
| 690 |
|
| 691 |
const std::string objc_prefix(FileClassPrefix(file_)); |
| 692 |
std::string syntax; |
| 693 |
switch (FileDescriptorLegacy(file_).syntax()) { |
| 694 |
case FileDescriptorLegacy::Syntax::SYNTAX_UNKNOWN: |
| 695 |
syntax = "GPBFileSyntaxUnknown"; |
| 696 |
break; |
| 697 |
case FileDescriptorLegacy::Syntax::SYNTAX_PROTO2: |
| 698 |
syntax = "GPBFileSyntaxProto2"; |
| 699 |
break; |
| 700 |
case FileDescriptorLegacy::Syntax::SYNTAX_PROTO3: |
| 701 |
syntax = "GPBFileSyntaxProto3"; |
| 702 |
break; |
| 703 |
#ifdef PROTOBUF_FUTURE_EDITIONS |
| 704 |
case FileDescriptorLegacy::Syntax::SYNTAX_EDITIONS: |
| 705 |
syntax = "GPBFileSyntaxProtoEditions"; |
| 706 |
break; |
| 707 |
#endif // PROTOBUF_FUTURE_EDITIONS |
| 708 |
} |
| 709 |
|
| 710 |
p->Emit({{"file_description_name", file_description_name_}, |
| 711 |
{"package_value", file_->package().empty() |
| 712 |
? "NULL" |
| 713 |
: absl::StrCat("\"", file_->package(), "\"")}, |
| 714 |
{"prefix_value", |
| 715 |
objc_prefix.empty() && !file_->options().has_objc_class_prefix() |
| 716 |
? "NULL" |
| 717 |
: absl::StrCat("\"", objc_prefix, "\"")}, |
| 718 |
{"syntax", syntax}}, |
| 719 |
R"objc( |
| 720 |
static GPBFileDescription $file_description_name$ = { |
| 721 |
.package = $package_value$, |
| 722 |
.prefix = $prefix_value$, |
| 723 |
.syntax = $syntax$ |
| 724 |
}; |
| 725 |
)objc"); |
| 726 |
p->Emit("\n"); |
| 727 |
} |
| 728 |
|
| 729 |
} // namespace objectivec |
| 730 |
} // namespace compiler |
| 731 |
} // namespace protobuf |
| 732 |
} // namespace google |