• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 // Helper functions for generating ObjectiveC code.
9 
10 #ifndef GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_NAMES_H__
11 #define GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_NAMES_H__
12 
13 #include <string>
14 #include <vector>
15 
16 #include "absl/strings/string_view.h"
17 #include "google/protobuf/descriptor.h"
18 
19 // Must be included last
20 #include "google/protobuf/port_def.inc"
21 
22 namespace google {
23 namespace protobuf {
24 namespace compiler {
25 namespace objectivec {
26 
27 // Get/Set the path to a file to load for objc class prefix lookups.
28 PROTOC_EXPORT absl::string_view GetPackageToPrefixMappingsPath();
29 PROTOC_EXPORT void SetPackageToPrefixMappingsPath(absl::string_view file_path);
30 // Get/Set if the proto package should be used to make the default prefix for
31 // symbols. This will then impact most of the type naming apis below. It is done
32 // as a global to not break any other generator reusing the methods since they
33 // are exported.
34 PROTOC_EXPORT bool UseProtoPackageAsDefaultPrefix();
35 PROTOC_EXPORT void SetUseProtoPackageAsDefaultPrefix(bool on_or_off);
36 // Get/Set the path to a file to load as exceptions when
37 // `UseProtoPackageAsDefaultPrefix()` is `true`. An empty string means there
38 // should be no exceptions.
39 PROTOC_EXPORT absl::string_view GetProtoPackagePrefixExceptionList();
40 PROTOC_EXPORT void SetProtoPackagePrefixExceptionList(
41     absl::string_view file_path);
42 // Get/Set a prefix to add before the prefix generated from the package name.
43 // This is only used when UseProtoPackageAsDefaultPrefix() is True.
44 PROTOC_EXPORT absl::string_view GetForcedPackagePrefix();
45 PROTOC_EXPORT void SetForcedPackagePrefix(absl::string_view prefix);
46 
47 // Returns true if the name requires a ns_returns_not_retained attribute applied
48 // to it.
49 PROTOC_EXPORT bool IsRetainedName(absl::string_view name);
50 
51 // Returns true if the name starts with "init" and will need to have special
52 // handling under ARC.
53 PROTOC_EXPORT bool IsInitName(absl::string_view name);
54 
55 // Returns true if the name requires a cf_returns_not_retained attribute applied
56 // to it.
57 PROTOC_EXPORT bool IsCreateName(absl::string_view name);
58 
59 // Gets the objc_class_prefix or the prefix made from the proto package.
60 PROTOC_EXPORT std::string FileClassPrefix(const FileDescriptor* file);
61 
62 // Gets the path of the file we're going to generate (sans the .pb.h
63 // extension).  The path will be dependent on the objectivec package
64 // declared in the proto package.
65 PROTOC_EXPORT std::string FilePath(const FileDescriptor* file);
66 
67 // Just like FilePath(), but without the directory part.
68 PROTOC_EXPORT std::string FilePathBasename(const FileDescriptor* file);
69 
70 // Gets the name of the root class we'll generate in the file.  This class
71 // is not meant for external consumption, but instead contains helpers that
72 // the rest of the classes need
73 PROTOC_EXPORT std::string FileClassName(const FileDescriptor* file);
74 
75 // These return the fully-qualified class name corresponding to the given
76 // descriptor.
77 PROTOC_EXPORT std::string ClassName(const Descriptor* descriptor);
78 PROTOC_EXPORT std::string ClassName(const Descriptor* descriptor,
79                                     std::string* out_suffix_added);
80 PROTOC_EXPORT std::string EnumName(const EnumDescriptor* descriptor);
81 
82 // Returns the fully-qualified name of the enum value corresponding to the
83 // the descriptor.
84 PROTOC_EXPORT std::string EnumValueName(const EnumValueDescriptor* descriptor);
85 
86 // Returns the name of the enum value corresponding to the descriptor.
87 PROTOC_EXPORT std::string EnumValueShortName(
88     const EnumValueDescriptor* descriptor);
89 
90 // Reverse what an enum does.
91 PROTOC_EXPORT std::string UnCamelCaseEnumShortName(absl::string_view name);
92 
93 // Returns the name to use for the extension (used as the method off the file's
94 // Root class).
95 PROTOC_EXPORT std::string ExtensionMethodName(
96     const FieldDescriptor* descriptor);
97 
98 // Returns the transformed field name.
99 PROTOC_EXPORT std::string FieldName(const FieldDescriptor* field);
100 PROTOC_EXPORT std::string FieldNameCapitalized(const FieldDescriptor* field);
101 
102 // The formatting options for FieldObjCType
103 enum FieldObjCTypeOptions : unsigned int {
104   // No options.
105   kFieldObjCTypeOptions_None = 0,
106   // Leave off the lightweight generics from any collection classes.
107   kFieldObjCTypeOptions_OmitLightweightGenerics = 1 << 0,
108   // For things that are pointers, include a space before the `*`.
109   kFieldObjCTypeOptions_IncludeSpaceBeforeStar = 1 << 1,
110   // For things that are basic types (int, float, etc.), include a space after
111   // the type, needed for some usage cases. This is mainly needed when using the
112   // type to declare variables. Pointers don't need the space to generate valid
113   // code.
114   kFieldObjCTypeOptions_IncludeSpaceAfterBasicTypes = 1 << 2,
115 };
116 
117 // This returns the ObjC type for the field. `options` allows some controls on
118 // how the string is created for different usages.
119 PROTOC_EXPORT std::string FieldObjCType(
120     const FieldDescriptor* field,
121     FieldObjCTypeOptions options = kFieldObjCTypeOptions_None);
122 
123 // Returns the transformed oneof name.
124 PROTOC_EXPORT std::string OneofEnumName(const OneofDescriptor* descriptor);
125 PROTOC_EXPORT std::string OneofName(const OneofDescriptor* descriptor);
126 PROTOC_EXPORT std::string OneofNameCapitalized(
127     const OneofDescriptor* descriptor);
128 
129 // Reverse of the above.
130 PROTOC_EXPORT std::string UnCamelCaseFieldName(absl::string_view name,
131                                                const FieldDescriptor* field);
132 
133 // The name the commonly used by the library when built as a framework.
134 // This lines up to the name used in the CocoaPod.
135 extern PROTOC_EXPORT const char* const ProtobufLibraryFrameworkName;
136 // Returns the CPP symbol name to use as the gate for framework style imports
137 // for the given framework name to use.
138 PROTOC_EXPORT std::string ProtobufFrameworkImportSymbol(
139     absl::string_view framework_name);
140 
141 // ---------------------------------------------------------------------------
142 
143 // These aren't really "naming" related, but can be useful for something
144 // building on top of ObjC Protos to be able to share the knowledge/enforcement.
145 
146 // Checks if the file is one of the proto's bundled with the library.
147 PROTOC_EXPORT bool IsProtobufLibraryBundledProtoFile(
148     const FileDescriptor* file);
149 
150 // Generator Prefix Validation Options (see generator.cc for a
151 // description of each):
152 struct Options {
153   Options();
154   std::string expected_prefixes_path;
155   std::vector<std::string> expected_prefixes_suppressions;
156   bool prefixes_must_be_registered;
157   bool require_prefixes;
158 };
159 
160 // Checks the prefix for the given files and outputs any warnings as needed. If
161 // there are flat out errors, then out_error is filled in with the first error
162 // and the result is false.
163 PROTOC_EXPORT bool ValidateObjCClassPrefixes(
164     const std::vector<const FileDescriptor*>& files,
165     const Options& validation_options, std::string* out_error);
166 // Same was the other ValidateObjCClassPrefixes() calls, but the options all
167 // come from the environment variables.
168 PROTOC_EXPORT bool ValidateObjCClassPrefixes(
169     const std::vector<const FileDescriptor*>& files, std::string* out_error);
170 
171 }  // namespace objectivec
172 }  // namespace compiler
173 }  // namespace protobuf
174 }  // namespace google
175 
176 #include "google/protobuf/port_undef.inc"
177 
178 #endif  // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_NAMES_H__
179