83
83
hash_set<string> kKeywords = MakeKeywordsMap();
85
// Returns whether the provided descriptor has an extension. This includes its
87
bool HasExtension(const Descriptor* descriptor) {
88
if (descriptor->extension_count() > 0) {
91
for (int i = 0; i < descriptor->nested_type_count(); ++i) {
92
if (HasExtension(descriptor->nested_type(i))) {
85
101
string UnderscoresToCamelCase(const string& input, bool cap_next_letter) {
87
103
// Note: I distrust ctype.h due to locales.
110
// Returns whether the provided descriptor has an extension. This includes its
112
bool HasExtension(const Descriptor* descriptor) {
113
if (descriptor->extension_count() > 0) {
116
for (int i = 0; i < descriptor->nested_type_count(); ++i) {
117
if (HasExtension(descriptor->nested_type(i))) {
126
126
const char kThickSeparator[] =
127
127
"// ===================================================================\n";
128
128
const char kThinSeparator[] =
259
string Int32ToString(int number) {
260
// gcc rejects the decimal form of kint32min.
261
if (number == kint32min) {
262
GOOGLE_COMPILE_ASSERT(kint32min == (~0x7fffffff), kint32min_value_error);
263
return "(~0x7fffffff)";
265
return SimpleItoa(number);
269
string Int64ToString(int64 number) {
270
// gcc rejects the decimal form of kint64min
271
if (number == kint64min) {
272
// Make sure we are in a 2's complement system.
273
GOOGLE_COMPILE_ASSERT(kint64min == GOOGLE_LONGLONG(~0x7fffffffffffffff),
274
kint64min_value_error);
275
return "GOOGLE_LONGLONG(~0x7fffffffffffffff)";
277
return "GOOGLE_LONGLONG(" + SimpleItoa(number) + ")";
259
280
string DefaultValue(const FieldDescriptor* field) {
260
281
switch (field->cpp_type()) {
261
282
case FieldDescriptor::CPPTYPE_INT32:
262
// gcc rejects the decimal form of kint32min and kint64min.
263
if (field->default_value_int32() == kint32min) {
264
// Make sure we are in a 2's complement system.
265
GOOGLE_COMPILE_ASSERT(kint32min == -0x80000000, kint32min_value_error);
266
return "-0x80000000";
268
return SimpleItoa(field->default_value_int32());
283
return Int32ToString(field->default_value_int32());
269
284
case FieldDescriptor::CPPTYPE_UINT32:
270
285
return SimpleItoa(field->default_value_uint32()) + "u";
271
286
case FieldDescriptor::CPPTYPE_INT64:
272
// See the comments for CPPTYPE_INT32.
273
if (field->default_value_int64() == kint64min) {
274
// Make sure we are in a 2's complement system.
275
GOOGLE_COMPILE_ASSERT(kint64min == GOOGLE_LONGLONG(-0x8000000000000000),
276
kint64min_value_error);
277
return "GOOGLE_LONGLONG(-0x8000000000000000)";
279
return "GOOGLE_LONGLONG(" + SimpleItoa(field->default_value_int64()) + ")";
287
return Int64ToString(field->default_value_int64());
280
288
case FieldDescriptor::CPPTYPE_UINT64:
281
289
return "GOOGLE_ULONGLONG(" + SimpleItoa(field->default_value_uint64())+ ")";
282
290
case FieldDescriptor::CPPTYPE_DOUBLE: {
319
327
return strings::Substitute(
320
328
"static_cast< $0 >($1)",
321
329
ClassName(field->enum_type(), true),
322
field->default_value_enum()->number());
330
Int32ToString(field->default_value_enum()->number()));
323
331
case FieldDescriptor::CPPTYPE_STRING:
324
332
return "\"" + EscapeTrigraphs(
325
333
CEscape(field->default_value_string())) +
366
374
return "protobuf_ShutdownFile_" + FilenameIdentifier(filename);
377
// Return the qualified C++ name for a file level symbol.
378
string QualifiedFileLevelSymbol(const string& package, const string& name) {
379
if (package.empty()) {
380
return StrCat("::", name);
382
return StrCat("::", DotsToColons(package), "::", name);
369
385
// Escape C++ trigraphs by escaping question marks to \?
370
386
string EscapeTrigraphs(const string& to_escape) {
371
387
return StringReplace(to_escape, "?", "\\?", true);
390
// Escaped function name to eliminate naming conflict.
391
string SafeFunctionName(const Descriptor* descriptor,
392
const FieldDescriptor* field,
393
const string& prefix) {
394
// Do not use FieldName() since it will escape keywords.
395
string name = field->name();
397
string function_name = prefix + name;
398
if (descriptor->FindFieldByName(function_name)) {
399
// Single underscore will also make it conflicting with the private data
400
// member. We use double underscore to escape function names.
401
function_name.append("__");
402
} else if (kKeywords.count(name) > 0) {
403
// If the field name is a keyword, we append the underscore back to keep it
404
// consistent with other function names.
405
function_name.append("_");
407
return function_name;
374
410
bool StaticInitializersForced(const FileDescriptor* file) {
375
411
if (HasDescriptorMethods(file) || file->extension_count() > 0) {
471
bool IsStringOrMessage(const FieldDescriptor* field) {
472
switch (field->cpp_type()) {
473
case FieldDescriptor::CPPTYPE_INT32:
474
case FieldDescriptor::CPPTYPE_INT64:
475
case FieldDescriptor::CPPTYPE_UINT32:
476
case FieldDescriptor::CPPTYPE_UINT64:
477
case FieldDescriptor::CPPTYPE_DOUBLE:
478
case FieldDescriptor::CPPTYPE_FLOAT:
479
case FieldDescriptor::CPPTYPE_BOOL:
480
case FieldDescriptor::CPPTYPE_ENUM:
482
case FieldDescriptor::CPPTYPE_STRING:
483
case FieldDescriptor::CPPTYPE_MESSAGE:
487
GOOGLE_LOG(FATAL) << "Can't get here.";
435
491
} // namespace cpp
436
492
} // namespace compiler
437
493
} // namespace protobuf