Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:11:59

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 // Helper functions for generating ObjectiveC code.
0009 
0010 #ifndef GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_HELPERS_H__
0011 #define GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_HELPERS_H__
0012 
0013 #include <string>
0014 #include <vector>
0015 
0016 #include "absl/strings/str_cat.h"
0017 #include "absl/strings/string_view.h"
0018 #include "google/protobuf/compiler/objectivec/options.h"
0019 #include "google/protobuf/descriptor.h"
0020 #include "google/protobuf/descriptor.pb.h"
0021 #include "google/protobuf/io/printer.h"
0022 
0023 namespace google {
0024 namespace protobuf {
0025 namespace compiler {
0026 namespace objectivec {
0027 
0028 // Escape C++ trigraphs by escaping question marks to "\?".
0029 std::string EscapeTrigraphs(absl::string_view to_escape);
0030 
0031 // Returns true if the extension field is a custom option.
0032 // https://protobuf.dev/programming-guides/proto2/#customoptions
0033 bool ExtensionIsCustomOption(const FieldDescriptor* extension_field);
0034 
0035 enum ObjectiveCType {
0036   OBJECTIVECTYPE_INT32,
0037   OBJECTIVECTYPE_UINT32,
0038   OBJECTIVECTYPE_INT64,
0039   OBJECTIVECTYPE_UINT64,
0040   OBJECTIVECTYPE_FLOAT,
0041   OBJECTIVECTYPE_DOUBLE,
0042   OBJECTIVECTYPE_BOOLEAN,
0043   OBJECTIVECTYPE_STRING,
0044   OBJECTIVECTYPE_DATA,
0045   OBJECTIVECTYPE_ENUM,
0046   OBJECTIVECTYPE_MESSAGE
0047 };
0048 
0049 enum FlagType {
0050   FLAGTYPE_DESCRIPTOR_INITIALIZATION,
0051   FLAGTYPE_EXTENSION,
0052   FLAGTYPE_FIELD
0053 };
0054 
0055 std::string GetCapitalizedType(const FieldDescriptor* field);
0056 
0057 ObjectiveCType GetObjectiveCType(FieldDescriptor::Type field_type);
0058 
0059 inline ObjectiveCType GetObjectiveCType(const FieldDescriptor* field) {
0060   return GetObjectiveCType(field->type());
0061 }
0062 
0063 inline bool IsPrimitiveType(const FieldDescriptor* field) {
0064   ObjectiveCType type = GetObjectiveCType(field);
0065   switch (type) {
0066     case OBJECTIVECTYPE_INT32:
0067     case OBJECTIVECTYPE_UINT32:
0068     case OBJECTIVECTYPE_INT64:
0069     case OBJECTIVECTYPE_UINT64:
0070     case OBJECTIVECTYPE_FLOAT:
0071     case OBJECTIVECTYPE_DOUBLE:
0072     case OBJECTIVECTYPE_BOOLEAN:
0073     case OBJECTIVECTYPE_ENUM:
0074       return true;
0075       break;
0076     default:
0077       return false;
0078   }
0079 }
0080 
0081 inline bool IsReferenceType(const FieldDescriptor* field) {
0082   return !IsPrimitiveType(field);
0083 }
0084 
0085 std::string GPBGenericValueFieldName(const FieldDescriptor* field);
0086 std::string DefaultValue(const FieldDescriptor* field);
0087 
0088 std::string BuildFlagsString(FlagType type,
0089                              const std::vector<std::string>& strings);
0090 
0091 // Returns a symbol that can be used in C code to refer to an Objective-C
0092 // class without initializing the class.
0093 std::string ObjCClass(absl::string_view class_name);
0094 
0095 // Declares an Objective-C class without initializing the class so that it can
0096 // be referred to by ObjCClass.
0097 std::string ObjCClassDeclaration(absl::string_view class_name);
0098 
0099 // Flag to control the behavior of `EmitCommentsString`.
0100 enum CommentStringFlags : unsigned int {
0101   kCommentStringFlags_None = 0,
0102   // Add a newline before the comment.
0103   kCommentStringFlags_AddLeadingNewline = 1 << 1,
0104   // Force a multiline comment even if only 1 line.
0105   kCommentStringFlags_ForceMultiline = 1 << 2,
0106 };
0107 
0108 // Emits HeaderDoc/appledoc style comments out of the comments in the .proto
0109 // file.
0110 void EmitCommentsString(io::Printer* printer, const GenerationOptions& opts,
0111                         const SourceLocation& location,
0112                         CommentStringFlags flags = kCommentStringFlags_None);
0113 
0114 // Emits HeaderDoc/appledoc style comments out of the comments in the .proto
0115 // file.
0116 template <class TDescriptor>
0117 void EmitCommentsString(io::Printer* printer, const GenerationOptions& opts,
0118                         const TDescriptor* descriptor,
0119                         CommentStringFlags flags = kCommentStringFlags_None) {
0120   SourceLocation location;
0121   if (descriptor->GetSourceLocation(&location)) {
0122     EmitCommentsString(printer, opts, location, flags);
0123   }
0124 }
0125 
0126 template <class TDescriptor>
0127 std::string GetOptionalDeprecatedAttribute(
0128     const TDescriptor* descriptor, const FileDescriptor* file = nullptr) {
0129   bool isDeprecated = descriptor->options().deprecated();
0130   // The file is only passed when checking Messages & Enums, so those types
0131   // get tagged. At the moment, it doesn't seem to make sense to tag every
0132   // field or enum value with when the file is deprecated.
0133   bool isFileLevelDeprecation = false;
0134   if (!isDeprecated && file) {
0135     isFileLevelDeprecation = file->options().deprecated();
0136     isDeprecated = isFileLevelDeprecation;
0137   }
0138   if (isDeprecated) {
0139     std::string message;
0140     const FileDescriptor* sourceFile = descriptor->file();
0141     if (isFileLevelDeprecation) {
0142       message = absl::StrCat(sourceFile->name(), " is deprecated.");
0143     } else {
0144       message = absl::StrCat(descriptor->full_name(), " is deprecated (see ",
0145                              sourceFile->name(), ").");
0146     }
0147 
0148     return absl::StrCat("GPB_DEPRECATED_MSG(\"", message, "\")");
0149   } else {
0150     return "";
0151   }
0152 }
0153 
0154 // Helpers to identify the WellKnownType files/messages that get an Objective-C
0155 // category within the runtime to add helpers.
0156 bool HasWKTWithObjCCategory(const FileDescriptor* file);
0157 bool IsWKTWithObjCCategory(const Descriptor* descriptor);
0158 
0159 }  // namespace objectivec
0160 }  // namespace compiler
0161 }  // namespace protobuf
0162 }  // namespace google
0163 
0164 #endif  // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_HELPERS_H__