// Protocol Buffers - Google's data interchange format // Copyright 2008 Google Inc. All rights reserved. // https://developers.google.com/protocol-buffers/ // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // Author: kenton@google.com (Kenton Varda) // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #undef PACKAGE // autoheader #defines this. :( // Must be included last. #include namespace google { namespace protobuf { namespace { const int kPackageLimit = 100; // Note: I distrust ctype.h due to locales. char ToUpper(char ch) { return (ch >= 'a' && ch <= 'z') ? (ch - 'a' + 'A') : ch; } char ToLower(char ch) { return (ch >= 'A' && ch <= 'Z') ? (ch - 'A' + 'a') : ch; } std::string ToCamelCase(const std::string& input, bool lower_first) { bool capitalize_next = !lower_first; std::string result; result.reserve(input.size()); for (char character : input) { if (character == '_') { capitalize_next = true; } else if (capitalize_next) { result.push_back(ToUpper(character)); capitalize_next = false; } else { result.push_back(character); } } // Lower-case the first letter. if (lower_first && !result.empty()) { result[0] = ToLower(result[0]); } return result; } std::string ToJsonName(const std::string& input) { bool capitalize_next = false; std::string result; result.reserve(input.size()); for (char character : input) { if (character == '_') { capitalize_next = true; } else if (capitalize_next) { result.push_back(ToUpper(character)); capitalize_next = false; } else { result.push_back(character); } } return result; } // Backport of fold expressions for the comma operator to C++11. // Usage: Fold({expr...}); // Guaranteed to evaluate left-to-right struct ExpressionEater { template ExpressionEater(T&&) {} // NOLINT }; void Fold(std::initializer_list) {} template constexpr size_t RoundUpTo(size_t n) { static_assert((R & (R - 1)) == 0, "Must be power of two"); return (n + (R - 1)) & ~(R - 1); } constexpr size_t Max(size_t a, size_t b) { return a > b ? a : b; } template constexpr size_t Max(T a, Ts... b) { return Max(a, Max(b...)); } template constexpr size_t EffectiveAlignof() { // `char` is special in that it gets aligned to 8. It is where we drop the // trivial structs. return std::is_same::value ? 8 : alignof(T); } template using AppendIfAlign = typename std::conditional() == align, void (*)(T..., U), void (*)(T...)>::type; // Metafunction to sort types in descending order of alignment. // Useful for the flat allocator to ensure proper alignment of all elements // without having to add padding. // Instead of implementing a proper sort metafunction we just do a // filter+merge, which is much simpler to write as a metafunction. // We have a fixed set of alignments we can filter on. // For simplicity we use a function pointer as a type list. template struct TypeListSortImpl; template struct TypeListSortImpl { using type = void (*)(T16..., T8..., T4..., T2..., T1...); }; template struct TypeListSortImpl { using type = typename TypeListSortImpl< void (*)(Rest...), AppendIfAlign<16, First, T16...>, AppendIfAlign<8, First, T8...>, AppendIfAlign<4, First, T4...>, AppendIfAlign<2, First, T2...>, AppendIfAlign<1, First, T1...>>::type; }; template using SortByAlignment = typename TypeListSortImpl::type; template