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 == std::numeric_limits<double>::infinity()) { | 374 if (value == numeric_limits<double>::infinity()) { |
375 return "::google::protobuf::internal::Infinity()"; | 375 return "::google::protobuf::internal::Infinity()"; |
376 } else if (value == -std::numeric_limits<double>::infinity()) { | 376 } else if (value == -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 == std::numeric_limits<float>::infinity()) { | 387 if (value == 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 == -std::numeric_limits<float>::infinity()) { | 389 } else if (value == -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) + | 418 return FieldMessageTypeName(field) + "::default_instance()"; |
419 "::internal_default_instance()"; | |
420 } | 419 } |
421 // Can't actually get here; make compiler happy. (We could add a default | 420 // Can't actually get here; make compiler happy. (We could add a default |
422 // case above but then we wouldn't get the nice compiler warning when a | 421 // case above but then we wouldn't get the nice compiler warning when a |
423 // new type is added.) | 422 // new type is added.) |
424 GOOGLE_LOG(FATAL) << "Can't get here."; | 423 GOOGLE_LOG(FATAL) << "Can't get here."; |
425 return ""; | 424 return ""; |
426 } | 425 } |
427 | 426 |
428 // Convert a file name into a valid identifier. | 427 // Convert a file name into a valid identifier. |
429 string FilenameIdentifier(const string& filename) { | 428 string FilenameIdentifier(const string& filename) { |
430 string result; | 429 string result; |
431 for (int i = 0; i < filename.size(); i++) { | 430 for (int i = 0; i < filename.size(); i++) { |
432 if (ascii_isalnum(filename[i])) { | 431 if (ascii_isalnum(filename[i])) { |
433 result.push_back(filename[i]); | 432 result.push_back(filename[i]); |
434 } else { | 433 } else { |
435 // Not alphanumeric. To avoid any possibility of name conflicts we | 434 // Not alphanumeric. To avoid any possibility of name conflicts we |
436 // use the hex code for the character. | 435 // use the hex code for the character. |
437 StrAppend(&result, "_", strings::Hex(static_cast<uint8>(filename[i]))); | 436 StrAppend(&result, "_", strings::Hex(static_cast<uint8>(filename[i]))); |
438 } | 437 } |
439 } | 438 } |
440 return result; | 439 return result; |
441 } | 440 } |
442 | 441 |
443 // Return the name of the AddDescriptors() function for a given file. | 442 // Return the name of the AddDescriptors() function for a given file. |
444 string GlobalAddDescriptorsName(const string& filename) { | 443 string GlobalAddDescriptorsName(const string& filename) { |
445 return "protobuf_AddDesc_" + FilenameIdentifier(filename); | 444 return "protobuf_AddDesc_" + FilenameIdentifier(filename); |
446 } | 445 } |
447 | 446 |
448 string GlobalInitDefaultsName(const string& filename) { | |
449 return "protobuf_InitDefaults_" + FilenameIdentifier(filename); | |
450 } | |
451 | |
452 // Return the name of the AssignDescriptors() function for a given file. | 447 // Return the name of the AssignDescriptors() function for a given file. |
453 string GlobalOffsetTableName(const string& filename) { | 448 string GlobalAssignDescriptorsName(const string& filename) { |
454 return "protobuf_Offsets_" + FilenameIdentifier(filename); | 449 return "protobuf_AssignDesc_" + FilenameIdentifier(filename); |
455 } | 450 } |
456 | 451 |
457 // Return the name of the ShutdownFile() function for a given file. | 452 // Return the name of the ShutdownFile() function for a given file. |
458 string GlobalShutdownFileName(const string& filename) { | 453 string GlobalShutdownFileName(const string& filename) { |
459 return "protobuf_ShutdownFile_" + FilenameIdentifier(filename); | 454 return "protobuf_ShutdownFile_" + FilenameIdentifier(filename); |
460 } | 455 } |
461 | 456 |
462 // Return the qualified C++ name for a file level symbol. | 457 // Return the qualified C++ name for a file level symbol. |
463 string QualifiedFileLevelSymbol(const string& package, const string& name) { | 458 string QualifiedFileLevelSymbol(const string& package, const string& name) { |
464 if (package.empty()) { | 459 if (package.empty()) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 return true; | 493 return true; |
499 } | 494 } |
500 for (int i = 0; i < file->message_type_count(); ++i) { | 495 for (int i = 0; i < file->message_type_count(); ++i) { |
501 if (HasExtension(file->message_type(i))) { | 496 if (HasExtension(file->message_type(i))) { |
502 return true; | 497 return true; |
503 } | 498 } |
504 } | 499 } |
505 return false; | 500 return false; |
506 } | 501 } |
507 | 502 |
| 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 |
508 | 537 |
509 static bool HasMapFields(const Descriptor* descriptor) { | 538 static bool HasMapFields(const Descriptor* descriptor) { |
510 for (int i = 0; i < descriptor->field_count(); ++i) { | 539 for (int i = 0; i < descriptor->field_count(); ++i) { |
511 if (descriptor->field(i)->is_map()) { | 540 if (descriptor->field(i)->is_map()) { |
512 return true; | 541 return true; |
513 } | 542 } |
514 } | 543 } |
515 for (int i = 0; i < descriptor->nested_type_count(); ++i) { | 544 for (int i = 0; i < descriptor->nested_type_count(); ++i) { |
516 if (HasMapFields(descriptor->nested_type(i))) return true; | 545 if (HasMapFields(descriptor->nested_type(i))) return true; |
517 } | 546 } |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
595 } else if (GetOptimizeFor(field->file(), options) != | 624 } else if (GetOptimizeFor(field->file(), options) != |
596 FileOptions::LITE_RUNTIME) { | 625 FileOptions::LITE_RUNTIME) { |
597 return VERIFY; | 626 return VERIFY; |
598 } else { | 627 } else { |
599 return NONE; | 628 return NONE; |
600 } | 629 } |
601 } | 630 } |
602 | 631 |
603 static void GenerateUtf8CheckCode(const FieldDescriptor* field, | 632 static void GenerateUtf8CheckCode(const FieldDescriptor* field, |
604 const Options& options, bool for_parse, | 633 const Options& options, bool for_parse, |
605 const std::map<string, string>& variables, | 634 const map<string, string>& variables, |
606 const char* parameters, | 635 const char* parameters, |
607 const char* strict_function, | 636 const char* strict_function, |
608 const char* verify_function, | 637 const char* verify_function, |
609 io::Printer* printer) { | 638 io::Printer* printer) { |
610 switch (GetUtf8CheckMode(field, options)) { | 639 switch (GetUtf8CheckMode(field, options)) { |
611 case STRICT: { | 640 case STRICT: { |
612 if (for_parse) { | 641 if (for_parse) { |
613 printer->Print("DO_("); | 642 printer->Print("DO_("); |
614 } | 643 } |
615 printer->Print( | 644 printer->Print( |
(...skipping 29 matching lines...) Expand all Loading... |
645 printer->Outdent(); | 674 printer->Outdent(); |
646 break; | 675 break; |
647 } | 676 } |
648 case NONE: | 677 case NONE: |
649 break; | 678 break; |
650 } | 679 } |
651 } | 680 } |
652 | 681 |
653 void GenerateUtf8CheckCodeForString(const FieldDescriptor* field, | 682 void GenerateUtf8CheckCodeForString(const FieldDescriptor* field, |
654 const Options& options, bool for_parse, | 683 const Options& options, bool for_parse, |
655 const std::map<string, string>& variables, | 684 const map<string, string>& variables, |
656 const char* parameters, | 685 const char* parameters, |
657 io::Printer* printer) { | 686 io::Printer* printer) { |
658 GenerateUtf8CheckCode(field, options, for_parse, variables, parameters, | 687 GenerateUtf8CheckCode(field, options, for_parse, variables, parameters, |
659 "VerifyUtf8String", "VerifyUTF8StringNamedField", | 688 "VerifyUtf8String", "VerifyUTF8StringNamedField", |
660 printer); | 689 printer); |
661 } | 690 } |
662 | 691 |
663 void GenerateUtf8CheckCodeForCord(const FieldDescriptor* field, | 692 void GenerateUtf8CheckCodeForCord(const FieldDescriptor* field, |
664 const Options& options, bool for_parse, | 693 const Options& options, bool for_parse, |
665 const std::map<string, string>& variables, | 694 const map<string, string>& variables, |
666 const char* parameters, | 695 const char* parameters, |
667 io::Printer* printer) { | 696 io::Printer* printer) { |
668 GenerateUtf8CheckCode(field, options, for_parse, variables, parameters, | 697 GenerateUtf8CheckCode(field, options, for_parse, variables, parameters, |
669 "VerifyUtf8Cord", "VerifyUTF8CordNamedField", printer); | 698 "VerifyUtf8Cord", "VerifyUTF8CordNamedField", printer); |
670 } | 699 } |
671 | 700 |
672 } // namespace cpp | 701 } // namespace cpp |
673 } // namespace compiler | 702 } // namespace compiler |
674 } // namespace protobuf | 703 } // namespace protobuf |
675 } // namespace google | 704 } // namespace google |
OLD | NEW |