OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 case FieldDescriptor::CPPTYPE_INT32: | 364 case FieldDescriptor::CPPTYPE_INT32: |
365 return Int32ToString(field->default_value_int32()); | 365 return Int32ToString(field->default_value_int32()); |
366 case FieldDescriptor::CPPTYPE_UINT32: | 366 case FieldDescriptor::CPPTYPE_UINT32: |
367 return SimpleItoa(field->default_value_uint32()) + "u"; | 367 return SimpleItoa(field->default_value_uint32()) + "u"; |
368 case FieldDescriptor::CPPTYPE_INT64: | 368 case FieldDescriptor::CPPTYPE_INT64: |
369 return Int64ToString(field->default_value_int64()); | 369 return Int64ToString(field->default_value_int64()); |
370 case FieldDescriptor::CPPTYPE_UINT64: | 370 case FieldDescriptor::CPPTYPE_UINT64: |
371 return "GOOGLE_ULONGLONG(" + SimpleItoa(field->default_value_uint64())+ ")
"; | 371 return "GOOGLE_ULONGLONG(" + SimpleItoa(field->default_value_uint64())+ ")
"; |
372 case FieldDescriptor::CPPTYPE_DOUBLE: { | 372 case FieldDescriptor::CPPTYPE_DOUBLE: { |
373 double value = field->default_value_double(); | 373 double value = field->default_value_double(); |
374 if (value == numeric_limits<double>::infinity()) { | 374 if (value == std::numeric_limits<double>::infinity()) { |
375 return "::google::protobuf::internal::Infinity()"; | 375 return "::google::protobuf::internal::Infinity()"; |
376 } else if (value == -numeric_limits<double>::infinity()) { | 376 } else if (value == -std::numeric_limits<double>::infinity()) { |
377 return "-::google::protobuf::internal::Infinity()"; | 377 return "-::google::protobuf::internal::Infinity()"; |
378 } else if (value != value) { | 378 } else if (value != value) { |
379 return "::google::protobuf::internal::NaN()"; | 379 return "::google::protobuf::internal::NaN()"; |
380 } else { | 380 } else { |
381 return SimpleDtoa(value); | 381 return SimpleDtoa(value); |
382 } | 382 } |
383 } | 383 } |
384 case FieldDescriptor::CPPTYPE_FLOAT: | 384 case FieldDescriptor::CPPTYPE_FLOAT: |
385 { | 385 { |
386 float value = field->default_value_float(); | 386 float value = field->default_value_float(); |
387 if (value == numeric_limits<float>::infinity()) { | 387 if (value == std::numeric_limits<float>::infinity()) { |
388 return "static_cast<float>(::google::protobuf::internal::Infinity())"; | 388 return "static_cast<float>(::google::protobuf::internal::Infinity())"; |
389 } else if (value == -numeric_limits<float>::infinity()) { | 389 } else if (value == -std::numeric_limits<float>::infinity()) { |
390 return "static_cast<float>(-::google::protobuf::internal::Infinity())"
; | 390 return "static_cast<float>(-::google::protobuf::internal::Infinity())"
; |
391 } else if (value != value) { | 391 } else if (value != value) { |
392 return "static_cast<float>(::google::protobuf::internal::NaN())"; | 392 return "static_cast<float>(::google::protobuf::internal::NaN())"; |
393 } else { | 393 } else { |
394 string float_value = SimpleFtoa(value); | 394 string float_value = SimpleFtoa(value); |
395 // If floating point value contains a period (.) or an exponent | 395 // If floating point value contains a period (.) or an exponent |
396 // (either E or e), then append suffix 'f' to make it a float | 396 // (either E or e), then append suffix 'f' to make it a float |
397 // literal. | 397 // literal. |
398 if (float_value.find_first_of(".eE") != string::npos) { | 398 if (float_value.find_first_of(".eE") != string::npos) { |
399 float_value.push_back('f'); | 399 float_value.push_back('f'); |
400 } | 400 } |
401 return float_value; | 401 return float_value; |
402 } | 402 } |
403 } | 403 } |
404 case FieldDescriptor::CPPTYPE_BOOL: | 404 case FieldDescriptor::CPPTYPE_BOOL: |
405 return field->default_value_bool() ? "true" : "false"; | 405 return field->default_value_bool() ? "true" : "false"; |
406 case FieldDescriptor::CPPTYPE_ENUM: | 406 case FieldDescriptor::CPPTYPE_ENUM: |
407 // Lazy: Generate a static_cast because we don't have a helper function | 407 // Lazy: Generate a static_cast because we don't have a helper function |
408 // that constructs the full name of an enum value. | 408 // that constructs the full name of an enum value. |
409 return strings::Substitute( | 409 return strings::Substitute( |
410 "static_cast< $0 >($1)", | 410 "static_cast< $0 >($1)", |
411 ClassName(field->enum_type(), true), | 411 ClassName(field->enum_type(), true), |
412 Int32ToString(field->default_value_enum()->number())); | 412 Int32ToString(field->default_value_enum()->number())); |
413 case FieldDescriptor::CPPTYPE_STRING: | 413 case FieldDescriptor::CPPTYPE_STRING: |
414 return "\"" + EscapeTrigraphs( | 414 return "\"" + EscapeTrigraphs( |
415 CEscape(field->default_value_string())) + | 415 CEscape(field->default_value_string())) + |
416 "\""; | 416 "\""; |
417 case FieldDescriptor::CPPTYPE_MESSAGE: | 417 case FieldDescriptor::CPPTYPE_MESSAGE: |
418 return FieldMessageTypeName(field) + "::default_instance()"; | 418 return "*" + FieldMessageTypeName(field) + |
| 419 "::internal_default_instance()"; |
419 } | 420 } |
420 // Can't actually get here; make compiler happy. (We could add a default | 421 // Can't actually get here; make compiler happy. (We could add a default |
421 // case above but then we wouldn't get the nice compiler warning when a | 422 // case above but then we wouldn't get the nice compiler warning when a |
422 // new type is added.) | 423 // new type is added.) |
423 GOOGLE_LOG(FATAL) << "Can't get here."; | 424 GOOGLE_LOG(FATAL) << "Can't get here."; |
424 return ""; | 425 return ""; |
425 } | 426 } |
426 | 427 |
427 // Convert a file name into a valid identifier. | 428 // Convert a file name into a valid identifier. |
428 string FilenameIdentifier(const string& filename) { | 429 string FilenameIdentifier(const string& filename) { |
429 string result; | 430 string result; |
430 for (int i = 0; i < filename.size(); i++) { | 431 for (int i = 0; i < filename.size(); i++) { |
431 if (ascii_isalnum(filename[i])) { | 432 if (ascii_isalnum(filename[i])) { |
432 result.push_back(filename[i]); | 433 result.push_back(filename[i]); |
433 } else { | 434 } else { |
434 // Not alphanumeric. To avoid any possibility of name conflicts we | 435 // Not alphanumeric. To avoid any possibility of name conflicts we |
435 // use the hex code for the character. | 436 // use the hex code for the character. |
436 StrAppend(&result, "_", strings::Hex(static_cast<uint8>(filename[i]))); | 437 StrAppend(&result, "_", strings::Hex(static_cast<uint8>(filename[i]))); |
437 } | 438 } |
438 } | 439 } |
439 return result; | 440 return result; |
440 } | 441 } |
441 | 442 |
442 // Return the name of the AddDescriptors() function for a given file. | 443 // Return the name of the AddDescriptors() function for a given file. |
443 string GlobalAddDescriptorsName(const string& filename) { | 444 string GlobalAddDescriptorsName(const string& filename) { |
444 return "protobuf_AddDesc_" + FilenameIdentifier(filename); | 445 return "protobuf_AddDesc_" + FilenameIdentifier(filename); |
445 } | 446 } |
446 | 447 |
| 448 string GlobalInitDefaultsName(const string& filename) { |
| 449 return "protobuf_InitDefaults_" + FilenameIdentifier(filename); |
| 450 } |
| 451 |
447 // Return the name of the AssignDescriptors() function for a given file. | 452 // Return the name of the AssignDescriptors() function for a given file. |
448 string GlobalAssignDescriptorsName(const string& filename) { | 453 string GlobalOffsetTableName(const string& filename) { |
449 return "protobuf_AssignDesc_" + FilenameIdentifier(filename); | 454 return "protobuf_Offsets_" + FilenameIdentifier(filename); |
450 } | 455 } |
451 | 456 |
452 // Return the name of the ShutdownFile() function for a given file. | 457 // Return the name of the ShutdownFile() function for a given file. |
453 string GlobalShutdownFileName(const string& filename) { | 458 string GlobalShutdownFileName(const string& filename) { |
454 return "protobuf_ShutdownFile_" + FilenameIdentifier(filename); | 459 return "protobuf_ShutdownFile_" + FilenameIdentifier(filename); |
455 } | 460 } |
456 | 461 |
457 // Return the qualified C++ name for a file level symbol. | 462 // Return the qualified C++ name for a file level symbol. |
458 string QualifiedFileLevelSymbol(const string& package, const string& name) { | 463 string QualifiedFileLevelSymbol(const string& package, const string& name) { |
459 if (package.empty()) { | 464 if (package.empty()) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 return true; | 498 return true; |
494 } | 499 } |
495 for (int i = 0; i < file->message_type_count(); ++i) { | 500 for (int i = 0; i < file->message_type_count(); ++i) { |
496 if (HasExtension(file->message_type(i))) { | 501 if (HasExtension(file->message_type(i))) { |
497 return true; | 502 return true; |
498 } | 503 } |
499 } | 504 } |
500 return false; | 505 return false; |
501 } | 506 } |
502 | 507 |
503 void PrintHandlingOptionalStaticInitializers( | |
504 const FileDescriptor* file, const Options& options, io::Printer* printer, | |
505 const char* with_static_init, const char* without_static_init, | |
506 const char* var1, const string& val1, const char* var2, | |
507 const string& val2) { | |
508 map<string, string> vars; | |
509 if (var1) { | |
510 vars[var1] = val1; | |
511 } | |
512 if (var2) { | |
513 vars[var2] = val2; | |
514 } | |
515 PrintHandlingOptionalStaticInitializers( | |
516 vars, file, options, printer, with_static_init, without_static_init); | |
517 } | |
518 | |
519 void PrintHandlingOptionalStaticInitializers(const map<string, string>& vars, | |
520 const FileDescriptor* file, | |
521 const Options& options, | |
522 io::Printer* printer, | |
523 const char* with_static_init, | |
524 const char* without_static_init) { | |
525 if (StaticInitializersForced(file, options)) { | |
526 printer->Print(vars, with_static_init); | |
527 } else { | |
528 printer->Print(vars, (string( | |
529 "#ifdef GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER\n") + | |
530 without_static_init + | |
531 "#else\n" + | |
532 with_static_init + | |
533 "#endif\n").c_str()); | |
534 } | |
535 } | |
536 | |
537 | 508 |
538 static bool HasMapFields(const Descriptor* descriptor) { | 509 static bool HasMapFields(const Descriptor* descriptor) { |
539 for (int i = 0; i < descriptor->field_count(); ++i) { | 510 for (int i = 0; i < descriptor->field_count(); ++i) { |
540 if (descriptor->field(i)->is_map()) { | 511 if (descriptor->field(i)->is_map()) { |
541 return true; | 512 return true; |
542 } | 513 } |
543 } | 514 } |
544 for (int i = 0; i < descriptor->nested_type_count(); ++i) { | 515 for (int i = 0; i < descriptor->nested_type_count(); ++i) { |
545 if (HasMapFields(descriptor->nested_type(i))) return true; | 516 if (HasMapFields(descriptor->nested_type(i))) return true; |
546 } | 517 } |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
624 } else if (GetOptimizeFor(field->file(), options) != | 595 } else if (GetOptimizeFor(field->file(), options) != |
625 FileOptions::LITE_RUNTIME) { | 596 FileOptions::LITE_RUNTIME) { |
626 return VERIFY; | 597 return VERIFY; |
627 } else { | 598 } else { |
628 return NONE; | 599 return NONE; |
629 } | 600 } |
630 } | 601 } |
631 | 602 |
632 static void GenerateUtf8CheckCode(const FieldDescriptor* field, | 603 static void GenerateUtf8CheckCode(const FieldDescriptor* field, |
633 const Options& options, bool for_parse, | 604 const Options& options, bool for_parse, |
634 const map<string, string>& variables, | 605 const std::map<string, string>& variables, |
635 const char* parameters, | 606 const char* parameters, |
636 const char* strict_function, | 607 const char* strict_function, |
637 const char* verify_function, | 608 const char* verify_function, |
638 io::Printer* printer) { | 609 io::Printer* printer) { |
639 switch (GetUtf8CheckMode(field, options)) { | 610 switch (GetUtf8CheckMode(field, options)) { |
640 case STRICT: { | 611 case STRICT: { |
641 if (for_parse) { | 612 if (for_parse) { |
642 printer->Print("DO_("); | 613 printer->Print("DO_("); |
643 } | 614 } |
644 printer->Print( | 615 printer->Print( |
(...skipping 29 matching lines...) Expand all Loading... |
674 printer->Outdent(); | 645 printer->Outdent(); |
675 break; | 646 break; |
676 } | 647 } |
677 case NONE: | 648 case NONE: |
678 break; | 649 break; |
679 } | 650 } |
680 } | 651 } |
681 | 652 |
682 void GenerateUtf8CheckCodeForString(const FieldDescriptor* field, | 653 void GenerateUtf8CheckCodeForString(const FieldDescriptor* field, |
683 const Options& options, bool for_parse, | 654 const Options& options, bool for_parse, |
684 const map<string, string>& variables, | 655 const std::map<string, string>& variables, |
685 const char* parameters, | 656 const char* parameters, |
686 io::Printer* printer) { | 657 io::Printer* printer) { |
687 GenerateUtf8CheckCode(field, options, for_parse, variables, parameters, | 658 GenerateUtf8CheckCode(field, options, for_parse, variables, parameters, |
688 "VerifyUtf8String", "VerifyUTF8StringNamedField", | 659 "VerifyUtf8String", "VerifyUTF8StringNamedField", |
689 printer); | 660 printer); |
690 } | 661 } |
691 | 662 |
692 void GenerateUtf8CheckCodeForCord(const FieldDescriptor* field, | 663 void GenerateUtf8CheckCodeForCord(const FieldDescriptor* field, |
693 const Options& options, bool for_parse, | 664 const Options& options, bool for_parse, |
694 const map<string, string>& variables, | 665 const std::map<string, string>& variables, |
695 const char* parameters, | 666 const char* parameters, |
696 io::Printer* printer) { | 667 io::Printer* printer) { |
697 GenerateUtf8CheckCode(field, options, for_parse, variables, parameters, | 668 GenerateUtf8CheckCode(field, options, for_parse, variables, parameters, |
698 "VerifyUtf8Cord", "VerifyUTF8CordNamedField", printer); | 669 "VerifyUtf8Cord", "VerifyUTF8CordNamedField", printer); |
699 } | 670 } |
700 | 671 |
701 } // namespace cpp | 672 } // namespace cpp |
702 } // namespace compiler | 673 } // namespace compiler |
703 } // namespace protobuf | 674 } // namespace protobuf |
704 } // namespace google | 675 } // namespace google |
OLD | NEW |