generator.cpp 73.8 KB
Newer Older
gejun's avatar
gejun committed
1
// mcpack2pb - Make protobuf be front-end of mcpack/compack
gejun's avatar
gejun committed
2
// Copyright (c) 2015 Baidu, Inc.
gejun's avatar
gejun committed
3 4 5 6 7 8 9 10 11 12 13 14
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
gejun's avatar
gejun committed
15

gejun's avatar
gejun committed
16
// Author: Ge,Jun (gejun@baidu.com)
gejun's avatar
gejun committed
17 18 19 20 21 22 23
// Date: Mon Oct 19 17:17:36 CST 2015

#include <set>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/io/printer.h>
#include <google/protobuf/compiler/code_generator.h>
#include <google/protobuf/compiler/plugin.h>
24 25
#include "butil/string_printf.h"
#include "butil/file_util.h"
gejun's avatar
gejun committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
#include "mcpack2pb/mcpack2pb.h"
#include "idl_options.pb.h"

namespace mcpack2pb {

const std::string& get_idl_name(const google::protobuf::FieldDescriptor* f) {
    const std::string& real_name = f->options().GetExtension(idl_name);
    return real_name.empty() ? f->name() : real_name;
}

bool is_integral_type(ConvertibleIdlType type) {
    switch (type) {
    case IDL_INT8:
    case IDL_INT16:
    case IDL_INT32:
    case IDL_INT64:
    case IDL_UINT8:
    case IDL_UINT16:
    case IDL_UINT32:
    case IDL_UINT64:
        return true;
    default:
        return false;
    }
}

const char* field_to_string(const google::protobuf::FieldDescriptor* f) {
    switch (f->type()) {
    case google::protobuf::FieldDescriptor::TYPE_DOUBLE:   return "double";
    case google::protobuf::FieldDescriptor::TYPE_FLOAT:    return "float";
    case google::protobuf::FieldDescriptor::TYPE_INT64:    return "int64";
    case google::protobuf::FieldDescriptor::TYPE_UINT64:   return "uint64";
    case google::protobuf::FieldDescriptor::TYPE_INT32:    return "int32";
    case google::protobuf::FieldDescriptor::TYPE_FIXED64:  return "fixed64";
    case google::protobuf::FieldDescriptor::TYPE_FIXED32:  return "fixed32";
    case google::protobuf::FieldDescriptor::TYPE_BOOL:     return "bool";
    case google::protobuf::FieldDescriptor::TYPE_STRING:   return "string";
    case google::protobuf::FieldDescriptor::TYPE_GROUP:
    case google::protobuf::FieldDescriptor::TYPE_MESSAGE:
        return f->message_type()->name().c_str();
    case google::protobuf::FieldDescriptor::TYPE_BYTES:    return "bytes";
    case google::protobuf::FieldDescriptor::TYPE_UINT32:   return "uint32";
    case google::protobuf::FieldDescriptor::TYPE_ENUM:
        return f->enum_type()->name().c_str();
    case google::protobuf::FieldDescriptor::TYPE_SFIXED32: return "sfixed32";
    case google::protobuf::FieldDescriptor::TYPE_SFIXED64: return "sfixed64";
    case google::protobuf::FieldDescriptor::TYPE_SINT32:   return "sint32";
    case google::protobuf::FieldDescriptor::TYPE_SINT64:   return "sint64";
    }
    return "unknown_protobuf_type";
}

const char* to_mcpack_typestr(const google::protobuf::FieldDescriptor* f) {
    switch (f->type()) {
    case google::protobuf::FieldDescriptor::TYPE_DOUBLE:   return "double";
    case google::protobuf::FieldDescriptor::TYPE_FLOAT:    return "float";
    case google::protobuf::FieldDescriptor::TYPE_INT64:    return "int64";
    case google::protobuf::FieldDescriptor::TYPE_UINT64:   return "uint64";
    case google::protobuf::FieldDescriptor::TYPE_INT32:    return "int32";
    case google::protobuf::FieldDescriptor::TYPE_FIXED64:  return "uint64";
    case google::protobuf::FieldDescriptor::TYPE_FIXED32:  return "uint32";
    case google::protobuf::FieldDescriptor::TYPE_BOOL:     return "bool";
    case google::protobuf::FieldDescriptor::TYPE_STRING:   return "string";
    case google::protobuf::FieldDescriptor::TYPE_GROUP:    return "object";
    case google::protobuf::FieldDescriptor::TYPE_MESSAGE:  return "object";
    case google::protobuf::FieldDescriptor::TYPE_BYTES:    return "binary";
    case google::protobuf::FieldDescriptor::TYPE_UINT32:   return "uint32";
    case google::protobuf::FieldDescriptor::TYPE_ENUM:     return "int32";
    case google::protobuf::FieldDescriptor::TYPE_SFIXED32: return "int32";
    case google::protobuf::FieldDescriptor::TYPE_SFIXED64: return "int64";
    case google::protobuf::FieldDescriptor::TYPE_SINT32:   return "int32";
    case google::protobuf::FieldDescriptor::TYPE_SINT64:   return "int64";
    }
    return "unknown_protobuf_type";
}

const char* to_mcpack_typestr(ConvertibleIdlType type,
                              const google::protobuf::FieldDescriptor* f) {
    switch (type) {
    case IDL_AUTO:   return to_mcpack_typestr(f);
    case IDL_INT8:   return "int8";
    case IDL_INT16:  return "int16";
    case IDL_INT32:  return "int32";
    case IDL_INT64:  return "int64";
    case IDL_UINT8:  return "uint8";
    case IDL_UINT16: return "uint16";
    case IDL_UINT32: return "uint32";
    case IDL_UINT64: return "uint64";
    case IDL_BOOL:   return "bool";
    case IDL_FLOAT:  return "float";
    case IDL_DOUBLE: return "double";
    case IDL_BINARY: return "binary";
    case IDL_STRING: return "string";
    }
    return "unknown";
}

const char* to_mcpack_typestr_uppercase(
    ConvertibleIdlType type,
    const google::protobuf::FieldDescriptor* f) {
    const char* s = to_mcpack_typestr(type, f);
    static char tempbuf[32];
    char* p = tempbuf;
    for (; *s; ++s, ++p) {
        *p = ::toupper(*s);
    }
    *p = 0;
    return tempbuf;
}

const char* describe_idl_type(ConvertibleIdlType type) {
    switch (type) {
    case IDL_AUTO:   return "IDL_AUTO";
    case IDL_INT8:   return "IDL_INT8";
    case IDL_INT16:  return "IDL_INT16";
    case IDL_INT32:  return "IDL_INT32";
    case IDL_INT64:  return "IDL_INT64";
    case IDL_UINT8:  return "IDL_UINT8";
    case IDL_UINT16: return "IDL_UINT16";
    case IDL_UINT32: return "IDL_UINT32";
    case IDL_UINT64: return "IDL_UINT64";
    case IDL_BOOL:   return "IDL_BOOL";
    case IDL_FLOAT:  return "IDL_FLOAT";
    case IDL_DOUBLE: return "IDL_DOUBLE";
    case IDL_BINARY: return "IDL_BINARY";
    case IDL_STRING: return "IDL_STRING";
    }
    return "Bad ConvertibleIdlType";
}

static std::string to_var_name(const std::string& name) {
    std::string result = name;
    for (size_t i = 0; i < result.size(); ++i) {
        if (result[i] == '.') {
            result[i] = '_';
        }
    }
    return result;
}

static std::string to_cpp_name(const std::string& full_name) {
    std::string cname;
    cname.reserve(full_name.size() + 8);
    for (size_t i = 0; i < full_name.size(); ++i) {
        if (full_name[i] == '.') {
            cname.append("::", 2);
        } else {
            cname.push_back(full_name[i]);
        }
    }
    return cname;
}

bool generate_declarations(const std::set<std::string>& ref_msgs,
                           const std::set<std::string>& ref_maps,
                           google::protobuf::io::Printer& decl) {
    for (std::set<std::string>::const_iterator
             it = ref_msgs.begin(); it != ref_msgs.end(); ++it) {
        decl.Print(
            "extern bool parse_$vmsg$_body_internal(\n"
            "    ::google::protobuf::Message* msg,\n"
            "    ::mcpack2pb::UnparsedValue& value);\n"
            "extern void serialize_$vmsg$_body(\n"
            "    const ::google::protobuf::Message& msg,\n"
            "    ::mcpack2pb::Serializer& serializer,\n"
            "    ::mcpack2pb::SerializationFormat format);\n"
            "extern ::mcpack2pb::FieldMap* g_$vmsg$_fields;\n"
            , "vmsg", *it);
    }
    for (std::set<std::string>::const_iterator
             it = ref_maps.begin(); it != ref_maps.end(); ++it) {
        decl.Print(
            "extern bool set_$vmsg$_value(::google::protobuf::Message* msg,\n"
            "                                 ::mcpack2pb::UnparsedValue& value);\n"
            , "vmsg", *it);
    }

    return !decl.failed();
}

#define TEMPLATE_OF_SET_FUNC_SIGNATURE()                                \
    "bool set_$vmsg$_$lcfield$(::google::protobuf::Message* msg_base,\n" \
    "                                ::mcpack2pb::UnparsedValue& value) "

#define TEMPLATE_OF_SET_FUNC_BODY(fntype)                               \
    "{\n"                                                               \
    "  static_cast<$msg$*>(msg_base)->set_$lcfield$(value.as_"#fntype"(\"$field$\"));\n" \
    "  return value.stream()->good();\n"                                \
    "}\n"                                                               

#define TEMPLATE_OF_ADD_FUNC_SIGNATURE()                                \
    "bool set_$vmsg$_$lcfield$(::google::protobuf::Message* msg_base,\n"     \
    "                                ::mcpack2pb::UnparsedValue& value) "  \

#define TEMPLATE_OF_ADD_FUNC_BODY(fntype, pbtype)                       \
    "{\n"                                                               \
    "  $msg$* const msg = static_cast<$msg$*>(msg_base);\n"             \
    "  if (value.type() == ::mcpack2pb::FIELD_ISOARRAY) {\n"               \
    "    ::mcpack2pb::ISOArrayIterator it(value);\n"                       \
    "    msg->mutable_$lcfield$()->Reserve(it.item_count());\n"         \
    "    for (; it != NULL; ++it) {\n"                                  \
    "      msg->add_$lcfield$(it.as_"#fntype "());\n"                   \
    "    }\n"                                                           \
    "    return value.stream()->good();\n"                              \
    "  } else if (value.type() == ::mcpack2pb::FIELD_ARRAY) {\n"           \
    "    ::mcpack2pb::ArrayIterator it(value);\n"                          \
    "    msg->mutable_$lcfield$()->Reserve(it.item_count());\n"         \
    "    for (; it != NULL; ++it) {\n"                                  \
    "      msg->add_$lcfield$(it->as_"#fntype "(\"$field$\"));\n" \
    "    }\n"                                                           \
    "    return value.stream()->good();\n"                              \
    "  }\n"                                                             \
    "  LOG(ERROR) << \"Can't set \" << value << \" to $field$ (repeated " #pbtype ")\";\n" \
    "  return false;\n"                                                 \
    "}\n"

static bool is_map_entry(const google::protobuf::Descriptor* d) {
    if (d->field_count() != 2 ||
        d->field(0)->name() != "key" ||
        d->field(0)->number() != 1 ||
        d->field(1)->name() != "value" ||
        d->field(1)->number() != 2) {
        return false;
    }
    if (d->field(0)->is_repeated()) {
        LOG(ERROR) << d->field(0)->full_name() << " must be required or optional";
        return false;
    }
    if (d->field(1)->is_repeated()) {
        LOG(ERROR) << d->field(1)->full_name() << " must be required or optional";
        return false;
    }
    if (d->field(0)->cpp_type() != google::protobuf::FieldDescriptor::CPPTYPE_STRING) {
        LOG(ERROR) << "key of idl map must be string";
        return false;
    }
    return true;
}

static bool generate_parsing(const google::protobuf::Descriptor* d,
                             std::set<std::string> & ref_msgs,
                             std::set<std::string> & ref_maps,
                             google::protobuf::io::Printer& impl) {
    std::string var_name = mcpack2pb::to_var_name(d->full_name());
    std::string cpp_name = mcpack2pb::to_cpp_name(d->full_name());
    ref_msgs.insert(var_name);

    impl.Print("\n// $msg$ from mcpack\n", "msg", d->full_name());
    for (int i = 0; i < d->field_count(); ++i) {
        const google::protobuf::FieldDescriptor* f = d->field(i);
        if (f->is_repeated()) {
            impl.Print("// repeated $type$ $name$ = $number$;\n"
                       , "type", field_to_string(f)
                       , "name", f->name()
280
                       , "number", butil::string_printf("%d", f->number()));
gejun's avatar
gejun committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
            impl.Print(TEMPLATE_OF_ADD_FUNC_SIGNATURE()
                       , "vmsg", var_name
                       , "lcfield", f->lowercase_name());
            switch (f->cpp_type()) {
            case google::protobuf::FieldDescriptor::CPPTYPE_INT32:
                impl.Print(TEMPLATE_OF_ADD_FUNC_BODY(int32, int32)
                           , "msg", cpp_name
                           , "field", f->full_name()
                           , "lcfield", f->lowercase_name());
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_INT64:
                impl.Print(TEMPLATE_OF_ADD_FUNC_BODY(int64, int64)
                           , "msg", cpp_name
                           , "field", f->full_name()
                           , "lcfield", f->lowercase_name());
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_UINT32:
                impl.Print(TEMPLATE_OF_ADD_FUNC_BODY(uint32, uint32)
                           , "msg", cpp_name
                           , "field", f->full_name()
                           , "lcfield", f->lowercase_name());
                break;                
            case google::protobuf::FieldDescriptor::CPPTYPE_UINT64:
                impl.Print(TEMPLATE_OF_ADD_FUNC_BODY(uint64, uint64)
                           , "msg", cpp_name
                           , "field", f->full_name()
                           , "lcfield", f->lowercase_name());
                break;                
            case google::protobuf::FieldDescriptor::CPPTYPE_BOOL:
                impl.Print(TEMPLATE_OF_ADD_FUNC_BODY(bool, bool)
                           , "msg", cpp_name
                           , "field", f->full_name()
                           , "lcfield", f->lowercase_name());
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_ENUM:
                impl.Print(
                    "{\n"
                    "  $msg$* const msg = static_cast<$msg$*>(msg_base);\n"
                    "  if (value.type() == ::mcpack2pb::FIELD_ISOARRAY) {\n"
                    "    ::mcpack2pb::ISOArrayIterator it(value);\n"
                    "    msg->mutable_$lcfield$()->Reserve(it.item_count());\n" 
                    "    for (; it != NULL; ++it) {\n"
                    "      msg->add_$lcfield$(($enum$)it.as_int32());\n"
                    "    }\n"
                    "    return value.stream()->good();\n"
                    "  } else if (value.type() == ::mcpack2pb::FIELD_ARRAY) {\n"            
                    "    ::mcpack2pb::ArrayIterator it(value);\n"
                    "    msg->mutable_$lcfield$()->Reserve(it.item_count());\n"
                    "    for (; it != NULL; ++it) {\n"
                    "      msg->add_$lcfield$(($enum$)it->as_int32(\"$enum$\"));\n"
                    "    }\n"
                    "    return value.stream()->good();\n"
                    "  }\n"                                                             
                    "  LOG(ERROR) << \"Can't set \" << value << \" to repeated $enum$\";\n"
                    "  return false;\n"                                                 
                    "}\n"
                    , "msg", cpp_name
                    , "enum", to_cpp_name(f->enum_type()->full_name())
                    , "lcfield", f->lowercase_name());
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
                impl.Print(TEMPLATE_OF_ADD_FUNC_BODY(float, float)
                           , "msg", cpp_name
                           , "field", f->full_name()
                           , "lcfield", f->lowercase_name());
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
                impl.Print(TEMPLATE_OF_ADD_FUNC_BODY(double, double)
                           , "msg", cpp_name
                           , "field", f->full_name()
                           , "lcfield", f->lowercase_name());
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_STRING:
                impl.Print(
                    "{\n"
                    "  $msg$* const msg = static_cast<$msg$*>(msg_base);\n"
                    "  if (value.type() == ::mcpack2pb::FIELD_ARRAY) {\n"
                    "    ::mcpack2pb::ArrayIterator it(value);\n"
                    "    msg->mutable_$lcfield$()->Reserve(it.item_count());\n"
                    "    for (; it != NULL; ++it) {\n"
                    "      if (it->type() == ::mcpack2pb::FIELD_STRING) {\n"
                    "        it->as_string(msg->add_$lcfield$(), \"$field$\");\n"
                    "      } else if (it->type() == ::mcpack2pb::FIELD_BINARY) {\n"
                    "        it->as_binary(msg->add_$lcfield$(), \"$field$\");\n"
                    "      } else {\n"
                    "        LOG(ERROR) << \"Can't add \" << *it << \" to $field$ (repeated string)\";\n"
                    "        return false;\n"    
                    "      }\n"
                    "    }\n"
                    "    return value.stream()->good();\n"
                    "  }\n"                                                             
                    "  LOG(ERROR) << \"Can't set \" << value << \" to $field$ (repeated string)\";\n"
                    "  return false;\n"    
                    "}\n"
                    , "msg", cpp_name
                    , "field", f->full_name()
                    , "lcfield", f->lowercase_name());
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {
                std::string var_name2 = mcpack2pb::to_var_name(f->message_type()->full_name());
                std::string cpp_name2 = mcpack2pb::to_cpp_name(f->message_type()->full_name());
                if (is_map_entry(f->message_type())) {
                    ref_maps.insert(var_name2);
                    impl.Print(
                        "{\n"
                        "  $msg$* const msg = static_cast<$msg$*>(msg_base);\n"
                        "  if (value.type() == ::mcpack2pb::FIELD_OBJECT) {\n"
                        "    ::mcpack2pb::ObjectIterator it(value);\n"
                        "    for (; it != NULL; ++it) {\n"
                        "      $msg2$* sub = msg->add_$lcfield$();\n"
                        "      sub->set_key(it->name.data(), it->name.size());\n"
                        , "msg", cpp_name
                        , "msg2", cpp_name2
                        , "lcfield", f->lowercase_name());
                    impl.Print(
                        "      if (!set_$vmsg2$_value(sub, it->value)) {\n"
                        "        return false;\n"
                        "      }\n"
                        "    }\n"
                        "    return value.stream()->good();\n"
                        "  }\n"
                        "  LOG(ERROR) << \"Can't set \" << value << \" to $field$ (repeated $msg2$)\";\n"
                        "  return false;\n"    
                        "}\n"
                        , "vmsg2", var_name2
                        , "msg2", f->message_type()->full_name()
                        , "field", f->full_name());
                    break;
                }
                ref_msgs.insert(var_name2);
                impl.Print(
                    "{\n"
                    "  $msg$* const msg = static_cast<$msg$*>(msg_base);\n"
                    "  if (value.type() == ::mcpack2pb::FIELD_OBJECTISOARRAY) {\n"
                    "    ::mcpack2pb::ObjectIterator it(value);\n"
                    "    for (; it != NULL; ++it) {\n"
                    "      ::mcpack2pb::SetFieldFn* fn = g_$vmsg2$_fields->seek(it->name);\n"
                    "      if (!fn) {\n"
                    "        if (!FLAGS_mcpack2pb_absent_field_is_error) {\n"
                    "          continue;\n"
                    "        } else {\n"
                    "          LOG(ERROR) << \"No field=\" << it->name << \" (\"\n"
                    "                     << value << \") in $msg$\";\n"
                    "          return false;\n"
                    "        }\n"
                    "      }\n"
                    "      if (it->value.type() == ::mcpack2pb::FIELD_ARRAY) {\n"
                    "        ::mcpack2pb::ArrayIterator it2(it->value);\n"
                    "        int i = 0;\n"
                    "        for (; it2 != NULL; ++it2, ++i) {\n"
                    "          ::google::protobuf::Message* sub_msg = NULL;\n"
                    "          if (i < msg->$lcfield$_size()) {\n"
                    "            sub_msg = msg->mutable_$lcfield$(i);\n"
                    "          } else {\n"
                    "            sub_msg = msg->add_$lcfield$();\n"
                    "          }\n"
                    "          if (it2->type() != ::mcpack2pb::FIELD_NULL) {\n"
                    "            if (!(*fn)(sub_msg, *it2)) {\n"
                    "              LOG(ERROR) << \"Fail to set item of \" << it->name;\n"
                    "              return false;\n"
                    "            }\n"
                    "          }\n"
                    "        }\n"
                    "      } else if (it->value.type() == ::mcpack2pb::FIELD_ISOARRAY) {\n"
                    "         LOG(ERROR) << \"Shouldn't be a iso array, name=\" << it->name;\n"
                    "         return false;\n"
                    "      } else {\n"
                    "         LOG(ERROR) << \"Can't add \" << value << \" to repeated $msg$\";\n"
                    "         return false;\n"
                    "      }\n"
                    "    }\n"
                    "    return value.stream()->good();\n"
                    "  } else if (value.type() == ::mcpack2pb::FIELD_ARRAY) {\n"
                    "    ::mcpack2pb::ArrayIterator it(value);\n"
                    "    msg->mutable_$lcfield$()->Reserve(it.item_count());\n"
                    "    for (; it != NULL; ++it) {\n"
                    "      if (it->type() == ::mcpack2pb::FIELD_OBJECT) {\n"
                    "        if (!parse_$vmsg2$_body_internal(msg->add_$lcfield$(), *it)) {\n"
                    "          return false;\n"
                    "        }\n"
                    "      } else {\n"
                    "        LOG(ERROR) << \"Can't add \" << *it << \" to repeated $msg$\";\n"
                    "        return false;\n"    
                    "      }\n"
                    "    }\n"
                    "    return value.stream()->good();\n"
                    "  }\n"
                    , "vmsg2", var_name2
                    , "msg", cpp_name
                    , "lcfield", f->lowercase_name());
                impl.Print(
                    "  LOG(ERROR) << \"Can't set \" << value << \" to $field$ (repeated $msg2$)\";\n"
                    "  return false;\n"    
                    "}\n"
                    , "msg2", f->message_type()->full_name()
                    , "field", f->full_name());
            } break;
            } // switch
        } else {
            if (f->is_optional()) {
                impl.Print("// optional $type$ $name$ = $number$;\n"
                           , "type", field_to_string(f)
                           , "name", f->name()
484
                           , "number", butil::string_printf("%d", f->number()));
gejun's avatar
gejun committed
485 486 487 488
            } else {
                impl.Print("// required $type$ $name$ = $number$;\n"
                           , "type", field_to_string(f)
                           , "name", f->name()
489
                           , "number", butil::string_printf("%d", f->number()));
gejun's avatar
gejun committed
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
            }
            impl.Print(TEMPLATE_OF_SET_FUNC_SIGNATURE()
                       , "vmsg", var_name
                       , "lcfield", f->lowercase_name());
            switch (f->cpp_type()) {
            case google::protobuf::FieldDescriptor::CPPTYPE_INT32:
                impl.Print(TEMPLATE_OF_SET_FUNC_BODY(int32)
                           , "msg", cpp_name
                           , "field", f->full_name()
                           , "lcfield", f->lowercase_name());
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_INT64:
                impl.Print(TEMPLATE_OF_SET_FUNC_BODY(int64)
                           , "msg", cpp_name
                           , "field", f->full_name()
                           , "lcfield", f->lowercase_name());
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_UINT32:
                impl.Print(TEMPLATE_OF_SET_FUNC_BODY(uint32)
                           , "msg", cpp_name
                           , "field", f->full_name()
                           , "lcfield", f->lowercase_name());
                break;                
            case google::protobuf::FieldDescriptor::CPPTYPE_UINT64:
                impl.Print(TEMPLATE_OF_SET_FUNC_BODY(uint64)
                           , "msg", cpp_name
                           , "field", f->full_name()
                           , "lcfield", f->lowercase_name());
                break;                
            case google::protobuf::FieldDescriptor::CPPTYPE_BOOL:
                impl.Print(TEMPLATE_OF_SET_FUNC_BODY(bool)
                           , "msg", cpp_name
                           , "field", f->full_name()
                           , "lcfield", f->lowercase_name());
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
                impl.Print(TEMPLATE_OF_SET_FUNC_BODY(float)
                           , "msg", cpp_name
                           , "field", f->full_name()
                           , "lcfield", f->lowercase_name());
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
                impl.Print(TEMPLATE_OF_SET_FUNC_BODY(double)
                           , "msg", cpp_name
                           , "field", f->full_name()
                           , "lcfield", f->lowercase_name());
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_ENUM:
                impl.Print(
                    "{\n"
                    "  static_cast<$msg$*>(msg_base)->set_$lcfield$(static_cast<$enum$>(value.as_int32(\"$enum$\")));\n"
                    "  return value.stream()->good();\n"
                    "}\n"
                    , "msg", cpp_name
                    , "enum", to_cpp_name(f->enum_type()->full_name())
                    , "lcfield", f->lowercase_name());
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_STRING:
                // TODO: Encoding checking/conversion for pb strings(utf8).
                impl.Print(
                    "{\n"
                    "  if (value.type() == ::mcpack2pb::FIELD_STRING) {\n"
                    "    value.as_string(static_cast<$msg$*>(msg_base)->mutable_$lcfield$(), \"$field$\");\n"
                    "    return value.stream()->good();\n"
                    "  } else if (value.type() == ::mcpack2pb::FIELD_BINARY) {\n"
                    "    value.as_binary(static_cast<$msg$*>(msg_base)->mutable_$lcfield$(), \"$field$\");\n"
                    "    return value.stream()->good();\n"
                    "  }\n"
                    "  LOG(ERROR) << \"Can't set \" << value << \" to $field$ (string)\";\n"
                    "  return false;\n"
                    "}\n"
                    , "msg", cpp_name
                    , "field", f->full_name()
                    , "lcfield", f->lowercase_name());
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {
                std::string var_name2 = mcpack2pb::to_var_name(f->message_type()->full_name());
                ref_msgs.insert(var_name2);
                impl.Print(
                    "{\n"
                    "  if (value.type() == ::mcpack2pb::FIELD_OBJECT) {\n"
                    "    return parse_$vmsg2$_body_internal(static_cast<$msg$*>(msg_base)->mutable_$lcfield$(), value);\n"
                    "  }\n"
                    , "vmsg2", var_name2
                    , "msg", cpp_name
                    , "lcfield", f->lowercase_name());
                impl.Print(
                    // FIXME: describe type.
                    "  LOG(ERROR) << \"Can't set \" << value << \" to $field$\";\n"
                    "  return false;\n"
                    "}\n"
                    , "field", f->full_name());
            } break;
            } // switch
        } // else
    }

    impl.Print(
        "bool parse_$vmsg$_body_internal(\n"
        "    ::google::protobuf::Message* msg,\n"
        "    ::mcpack2pb::UnparsedValue& value) {\n"
        "  ::mcpack2pb::ObjectIterator it(value);\n"
        "  for (; it != NULL; ++it) {\n"
        "    ::mcpack2pb::SetFieldFn* fn = g_$vmsg$_fields->seek(it->name);\n"
        "    if (!fn) {\n"
        "      if (!FLAGS_mcpack2pb_absent_field_is_error) {\n"
        "        continue;\n"
        "      } else {\n"
        "        LOG(ERROR) << \"No field=\" << it->name << \" (\"\n"
        "                   << it->value << \") in $msg$\";\n"
        "        return false;\n"
        "      }\n"
        "    }\n"
        "    if (!(*fn)(msg, it->value)) {\n"
        "      return false;\n"
        "    }\n"
        "  }\n"
        "  return value.stream()->good();\n"
        "}\n"
        "bool parse_$vmsg$_body(\n"
        "    ::google::protobuf::Message* msg,\n"
        "    ::google::protobuf::io::ZeroCopyInputStream* input,\n"
        "     size_t size) {\n"
        "  ::mcpack2pb::InputStream mc_stream(input);\n"
        "  ::mcpack2pb::UnparsedValue value(::mcpack2pb::FIELD_OBJECT, &mc_stream, size);\n"
        "  if (!parse_$vmsg$_body_internal(msg, value)) {\n"
        "    return false;\n"
        "  }\n"
        "  if (!msg->IsInitialized()) {\n"
        "    LOG(ERROR) << \"Missing required fields: \" << msg->InitializationErrorString();\n"
        "    return false;\n"
        "  }\n"
        "  return true;\n"
        "}\n"
        "size_t parse_$vmsg$(\n"
        "    ::google::protobuf::Message* msg,\n"
        "    ::google::protobuf::io::ZeroCopyInputStream* input) {\n"
        "  ::mcpack2pb::InputStream mc_stream(input);\n"
        "  const size_t value_size = ::mcpack2pb::unbox(&mc_stream);\n"
        "  if (!value_size) {\n"
        "    LOG(ERROR) << \"Fail to unbox\";\n"
        "    return 0;\n"
        "  }\n"
        "  ::mcpack2pb::UnparsedValue value(::mcpack2pb::FIELD_OBJECT, &mc_stream, value_size);\n"
        "  if (!parse_$vmsg$_body_internal(msg, value)) {\n"
        "    return 0;\n"
        "  }\n"
        "  if (!msg->IsInitialized()) {\n"
        "    LOG(ERROR) << \"Missing required fields: \" << msg->InitializationErrorString();\n"
        "    return 0;\n"
        "  }\n"
        "  return 6/*sizeof(FieldLongHead)*/ + value_size;\n"
        "}\n"
        , "vmsg", var_name
        , "msg", d->full_name());
    return !impl.failed();
}

#define TEMPLATE_SERIALIZE_REPEATED(cit, printer, field, strict_cond, looser_cond) \
    if (strict_cond) {                                                  \
        (printer).Print(                                                \
            "if (msg.$lcfield$_size()) {\n"                             \
            "  if (format == ::mcpack2pb::FORMAT_COMPACK) {\n"             \
            "    serializer.begin_compack_array(\"$field$\", ::mcpack2pb::FIELD_$TYPE$);\n" \
            "  } else {\n"                                              \
            "    serializer.begin_mcpack_array(\"$field$\", ::mcpack2pb::FIELD_$TYPE$);\n" \
            "  }\n"                                                     \
            , "lcfield", (field)->lowercase_name()                      \
            , "field", get_idl_name(field)                              \
            , "TYPE", to_mcpack_typestr_uppercase(cit, (field)));       \
        (printer).Print(                                                \
            "  serializer.add_multiple_$type$(msg.$lcfield$().data(), msg.$lcfield$_size());\n" \
            "  serializer.end_array();\n"                               \
            "}"                                                         \
            , "type", to_mcpack_typestr(cit, (field))                   \
            , "field", get_idl_name(field)                              \
            , "lcfield", (field)->lowercase_name());                    \
        if ((field)->options().GetExtension(idl_on)) {                  \
          (printer).Print(                                              \
            " else {\n"                                                 \
            "  serializer.add_empty_array(\"$field$\");\n"              \
            "}\n"                                                       \
            , "field", get_idl_name(field));                            \
        } else {                                                        \
          (printer).Print("\n");                                        \
        }                                                               \
    } else if (looser_cond) {                                           \
        (printer).Print(                                                \
            "if (msg.$lcfield$_size()) {\n"                             \
            "  if (format == ::mcpack2pb::FORMAT_COMPACK) {\n"             \
            "    serializer.begin_compack_array(\"$field$\", ::mcpack2pb::FIELD_$TYPE$);\n" \
            "  } else {\n"                                              \
            "    serializer.begin_mcpack_array(\"$field$\", ::mcpack2pb::FIELD_$TYPE$);\n" \
            "  }\n"                                                     \
            , "lcfield", (field)->lowercase_name()                      \
            , "field", get_idl_name(field)                              \
            , "TYPE", to_mcpack_typestr_uppercase(cit, (field)));       \
        (printer).Print(                                                \
            "  for (int i = 0; i < msg.$lcfield$_size(); ++i) {\n"      \
            "    serializer.add_$type$(msg.$lcfield$(i));\n"            \
            "  }\n"                                                     \
            "  serializer.end_array();\n"                               \
            "}"                                                         \
            , "type", to_mcpack_typestr(cit, (field))                   \
            , "field", get_idl_name(field)                              \
            , "lcfield", (field)->lowercase_name());                    \
        if ((field)->options().GetExtension(idl_on)) {                  \
          (printer).Print(                                              \
            " else {\n"                                                 \
            "  serializer.add_empty_array(\"$field$\");\n"              \
            "}\n"                                                       \
            , "field", get_idl_name(field));                            \
        } else {                                                        \
          (printer).Print("\n");                                        \
        }                                                               \
    } else {                                                            \
        if ((field)->type() == google::protobuf::FieldDescriptor::TYPE_ENUM) { \
            LOG(ERROR) << "Disallow converting " << (field)->full_name() \
                       << " (" << (field)->enum_type()->full_name() << ") to " \
                       << to_mcpack_typestr(cit, (field)) << " (idl)";  \
        } else {                                                        \
            LOG(ERROR) << "Disallow converting " << (field)->full_name() \
                       << " (" << field_to_string(field) << ") to "     \
                       << to_mcpack_typestr(cit, (field)) << " (idl)";  \
        }                                                               \
        return false;                                                   \
    }                                                                   \

#define TEMPLATE_SERIALIZE_REPEATED_INTEGRAL(cit, printer, field, strict_cond) \
    TEMPLATE_SERIALIZE_REPEATED(cit, printer, field, ((cit) == IDL_AUTO || (strict_cond)), \
                                is_integral_type(cit))

    
#define TEMPLATE_SERIALIZE(cit, printer, field, cond)                   \
    if (!(cond)) {                                                      \
        if ((field)->type() == google::protobuf::FieldDescriptor::TYPE_ENUM) { \
            LOG(ERROR) << "Disallow converting " << (field)->full_name() \
                       << " (" << (field)->enum_type()->full_name() << ") to " \
                       << to_mcpack_typestr(cit, (field)) << " (idl)";  \
        } else {                                                        \
            LOG(ERROR) << "Disallow converting " << (field)->full_name() \
                       << " (" << field_to_string(field) << ") to "     \
                       << to_mcpack_typestr(cit, (field)) << " (idl)";  \
        }                                                               \
        return false;                                                   \
    }                                                                   \
    (printer).Print(                                                    \
        "if (msg.has_$lcfield$()) {\n"                                  \
        "  serializer.add_$type$(\"$field$\", msg.$lcfield$());\n"      \
        "}"                                                             \
        , "type", to_mcpack_typestr(cit, (field))                       \
        , "field", get_idl_name(field)                                  \
        , "lcfield", (field)->lowercase_name());                        \
        if ((field)->options().GetExtension(idl_on)) {                  \
          (printer).Print(                                              \
            " else {\n"                                                 \
            "  serializer.add_empty_array(\"$field$\");\n"              \
            "}\n"                                                       \
            , "field", get_idl_name(field));                            \
        } 

#define TEMPLATE_SERIALIZE_INTEGRAL(cit, printer, field)                \
    TEMPLATE_SERIALIZE(cit, printer, field,                             \
                       ((cit) == IDL_AUTO || is_integral_type(cit)))


#define TEMPLATE_INNER_SERIALIZE_REPEATED(msg, cit, printer, field,     \
                                          strict_cond, looser_cond)     \
    if ((strict_cond)) {                                                \
        (printer).Print(                                                \
            "if ($msg$.$lcfield$_size()) {\n"                           \
            "  serializer.begin_compack_array(::mcpack2pb::FIELD_$TYPE$);\n" \
            , "msg", msg                                                \
            , "TYPE", to_mcpack_typestr_uppercase(cit, (field))         \
            , "lcfield", (field)->lowercase_name());                    \
        (printer).Print(                                                \
            "  serializer.add_multiple_$type$($msg$.$lcfield$().data(), $msg$.$lcfield$_size());\n" \
            "  serializer.end_array();\n"                               \
768
            "}"                                                         \
gejun's avatar
gejun committed
769 770 771
            , "msg", msg                                                \
            , "type", to_mcpack_typestr(cit, (field))                   \
            , "lcfield", (field)->lowercase_name());                    \
772 773 774 775 776 777 778 779 780 781 782
        if ((field)->options().GetExtension(idl_on)) {                  \
          (printer).Print(                                              \
            " else {\n"                                                 \
            "  serializer.add_empty_array();\n"                         \
            "}\n");                                                     \
        } else {                                                        \
          (printer).Print(                                              \
            " else {\n"                                                 \
            "  serializer.add_null();\n"                                \
            "}\n");                                                     \
        }                                                               \
gejun's avatar
gejun committed
783 784 785 786 787 788 789 790 791 792 793 794
    } else if (looser_cond) {                                           \
        (printer).Print(                                                \
            "if ($msg$.$lcfield$_size()) {\n"                           \
            "  serializer.begin_compack_array(::mcpack2pb::FIELD_$TYPE$);\n" \
            , "msg", msg                                                \
            , "TYPE", to_mcpack_typestr_uppercase(cit, (field))         \
            , "lcfield", (field)->lowercase_name());                    \
        (printer).Print(                                                \
            "  for (int j = 0; j < $msg$.$lcfield$_size(); ++j) {\n"    \
            "    serializer.add_$type$($msg$.$lcfield$(j));\n"          \
            "  }\n"                                                     \
            "  serializer.end_array();\n"                               \
795
            "}"                                                         \
gejun's avatar
gejun committed
796 797 798
            , "msg", msg                                                \
            , "type", to_mcpack_typestr(cit, (field))                   \
            , "lcfield", (field)->lowercase_name());                    \
799 800 801 802 803 804 805 806 807 808 809
        if ((field)->options().GetExtension(idl_on)) {                  \
          (printer).Print(                                              \
            " else {\n"                                                 \
            "  serializer.add_empty_array();\n"                         \
            "}\n");                                                     \
        } else {                                                        \
          (printer).Print(                                              \
            " else {\n"                                                 \
            "  serializer.add_null();\n"                                \
            "}\n");                                                     \
        }                                                               \
gejun's avatar
gejun committed
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
    } else {                                                            \
        if ((field)->type() == google::protobuf::FieldDescriptor::TYPE_ENUM) { \
            LOG(ERROR) << "Disallow converting " << (field)->full_name() \
                       << " (" << (field)->enum_type()->full_name() << ") to " \
                       << to_mcpack_typestr(cit, (field)) << " (idl)";  \
        } else {                                                        \
            LOG(ERROR) << "Disallow converting " << (field)->full_name() \
                       << " (" << field_to_string(field) << ") to "     \
                       << to_mcpack_typestr(cit, (field)) << " (idl)";  \
        }                                                               \
        return false;                                                   \
    }                                                                   \

#define TEMPLATE_INNER_SERIALIZE_REPEATED_INTEGRAL(msg, cit, printer, field, strict_cond) \
    TEMPLATE_INNER_SERIALIZE_REPEATED(msg, cit, printer, field, ((cit) == IDL_AUTO || (strict_cond)), \
                                      is_integral_type(cit))

    
#define TEMPLATE_INNER_SERIALIZE(msg, cit, printer, field, cond)        \
    if (!(cond)) {                                                      \
        if ((field)->type() == google::protobuf::FieldDescriptor::TYPE_ENUM) { \
            LOG(ERROR) << "Disallow converting " << (field)->full_name() \
                       << " (" << (field)->enum_type()->full_name() << ") to " \
                       << to_mcpack_typestr(cit, (field)) << " (idl)";  \
        } else {                                                        \
            LOG(ERROR) << "Disallow converting " << (field)->full_name() \
                       << " (" << field_to_string(field) << ") to "     \
                       << to_mcpack_typestr(cit, (field)) << " (idl)";  \
        }                                                               \
        return false;                                                   \
    }                                                                   \
    (printer).Print("if ($msg$.has_$lcfield$()) {\n"                    \
                    "  serializer.add_$type$($msg$.$lcfield$());\n"     \
                    "} else {\n"                                        \
                    "  serializer.add_null();\n"                        \
                    "}\n"                                               \
                    , "msg", msg                                        \
                    , "type", to_mcpack_typestr(cit, (field))           \
                    , "lcfield", (field)->lowercase_name())            

#define TEMPLATE_INNER_SERIALIZE_INTEGRAL(msg, cit, printer, field)     \
    TEMPLATE_INNER_SERIALIZE(msg, cit, printer, field,                  \
                             ((cit) == IDL_AUTO || is_integral_type(cit)))

static bool generate_serializing(const google::protobuf::Descriptor* d,
                                 std::set<std::string> & ref_msgs,
                                 std::set<std::string> & ref_maps,
                                 google::protobuf::io::Printer & impl) {
    std::string var_name = mcpack2pb::to_var_name(d->full_name());
    std::string cpp_name = mcpack2pb::to_cpp_name(d->full_name());
    ref_msgs.insert(var_name);
    impl.Print(
        "void serialize_$vmsg$_body(\n"
        "    const ::google::protobuf::Message& msg_base,\n"
        "    ::mcpack2pb::Serializer& serializer,\n"
        "    ::mcpack2pb::SerializationFormat format) {\n"
        "  (void)format;     // suppress compiler warning when it's not used\n"
        , "vmsg", var_name);
    if (d->field_count()) {
        impl.Print(
            "  const $msg$& msg = static_cast<const $msg$&>(msg_base);\n"
            , "msg", cpp_name);
    } else {
        impl.Print("  (void)msg_base;   // ^\n"
                   "  (void)serializer; // ^\n");
    }
    impl.Indent();
    for (int i = 0; i < d->field_count(); ++i) {
        const google::protobuf::FieldDescriptor* f = d->field(i);
        ConvertibleIdlType cit = f->options().GetExtension(idl_type);
        // Print the field as comment.
        std::string comment_template;
        if (cit == IDL_AUTO) {
883
            butil::string_printf(&comment_template,
gejun's avatar
gejun committed
884 885 886 887
                                "// %s $type$ $name$ = $number$;\n",
                                (f->is_repeated() ? "repeated" :
                                 (f->is_optional() ? "optional" : "required")));
        } else {
888
            butil::string_printf(&comment_template,
gejun's avatar
gejun committed
889 890 891 892 893 894 895 896
                                "// %s $type$ $name$ = $number$ [(idl_type)=%s];\n",
                                (f->is_repeated() ? "repeated" :
                                 (f->is_optional() ? "optional" : "required")),
                                describe_idl_type(cit));
        }
        impl.Print(comment_template.c_str()
                   , "type", field_to_string(f)
                   , "name", f->name()
897
                   , "number", butil::string_printf("%d", f->number()));
gejun's avatar
gejun committed
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
        if (f->is_repeated()) {
            switch (f->cpp_type()) {
            case google::protobuf::FieldDescriptor::CPPTYPE_INT32:
            case google::protobuf::FieldDescriptor::CPPTYPE_UINT32:
            case google::protobuf::FieldDescriptor::CPPTYPE_ENUM:
                TEMPLATE_SERIALIZE_REPEATED_INTEGRAL(
                    cit, impl, f, (cit == IDL_INT32 || cit == IDL_UINT32));
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_INT64:
            case google::protobuf::FieldDescriptor::CPPTYPE_UINT64:
                TEMPLATE_SERIALIZE_REPEATED_INTEGRAL(
                    cit, impl, f, (cit == IDL_INT64 || cit == IDL_UINT64));
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_BOOL:
                TEMPLATE_SERIALIZE_REPEATED(
                    cit, impl, f, (cit == IDL_AUTO || cit == IDL_BOOL), false);
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
                TEMPLATE_SERIALIZE_REPEATED(
                    cit, impl, f,
                    (cit == IDL_AUTO || cit == IDL_FLOAT), (cit == IDL_DOUBLE));
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
                TEMPLATE_SERIALIZE_REPEATED(
                    cit, impl, f,
                    (cit == IDL_AUTO || cit == IDL_DOUBLE), (cit == IDL_FLOAT));
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_STRING:
                if (f->type() == google::protobuf::FieldDescriptor::TYPE_STRING) {
                    TEMPLATE_SERIALIZE_REPEATED(
                        cit, impl, f, false, (cit == IDL_AUTO || cit == IDL_STRING));
                } else if (f->type() == google::protobuf::FieldDescriptor::TYPE_BYTES) {
                    TEMPLATE_SERIALIZE_REPEATED(
                        cit, impl, f, false,
                        (cit == IDL_AUTO || cit == IDL_BINARY || cit == IDL_STRING));
                } else {
                    LOG(ERROR) << "Unknown pb type=" << f->type();
                    return false;
                }
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {
                if (cit != IDL_AUTO) {
                    LOG(ERROR) << "Disallow converting " << f->full_name()
                               << " (" << f->message_type()->full_name() << ") to "
                               << to_mcpack_typestr(cit, f) << " (idl)";
                    return false;
                }
                const google::protobuf::Descriptor* msg2 = f->message_type();
                std::string var_name2 = mcpack2pb::to_var_name(msg2->full_name());
                std::string cpp_name2 = mcpack2pb::to_cpp_name(msg2->full_name());
                if (is_map_entry(msg2)) {
                    ref_maps.insert(var_name2);
                    impl.Print(
                        "serializer.begin_object(\"$field$\");\n"
                        "for (int i = 0; i < msg.$lcfield$_size(); ++i) {\n"
                        "  const $msg2$& pair = msg.$lcfield$(i);\n"
                        , "field", get_idl_name(f)
                        , "lcfield", f->lowercase_name()
                        , "msg2", cpp_name2);
                    const google::protobuf::FieldDescriptor* value_desc = msg2->field(1);
                    switch (value_desc->cpp_type()) {
                    case google::protobuf::FieldDescriptor::CPPTYPE_INT32:
                        impl.Print("  serializer.add_int32(pair.key(), pair.value());\n");
                        break;
                    case google::protobuf::FieldDescriptor::CPPTYPE_UINT32:
                        impl.Print("  serializer.add_uint32(pair.key(), pair.value());\n");
                        break;
                    case google::protobuf::FieldDescriptor::CPPTYPE_ENUM:
                        impl.Print("  serializer.add_int32(pair.key(), (int32_t)pair.value());\n");
                        break;
                    case google::protobuf::FieldDescriptor::CPPTYPE_INT64:
                        impl.Print("  serializer.add_int64(pair.key(), pair.value());\n");
                        break;
                    case google::protobuf::FieldDescriptor::CPPTYPE_UINT64:
                        impl.Print("  serializer.add_uint64(pair.key(), pair.value());\n");
                        break;
                    case google::protobuf::FieldDescriptor::CPPTYPE_BOOL:
                        impl.Print("  serializer.add_bool(pair.key(), pair.value());\n");
                        break;
                    case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
                        impl.Print("  serializer.add_float(pair.key(), pair.value());\n");
                        break;
                    case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
                        impl.Print("  serializer.add_double(pair.key(), pair.value());\n");
                        break;
                    case google::protobuf::FieldDescriptor::CPPTYPE_STRING:
                        impl.Print("  serializer.add_string(pair.key(), pair.value());\n");
                        break;
                    case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {
                        std::string var_name3 = mcpack2pb::to_var_name(
                            value_desc->message_type()->full_name());
                        ref_msgs.insert(var_name3);
                        impl.Print(
                            "  serializer.begin_object(pair.key());\n"
                            "  serialize_$vmsg3$_body(pair.value(), serializer, format);\n"
                            "  serializer.end_object();\n"
                            , "vmsg3", var_name3);
                    } break;
                    } // switch
                    impl.Print(
                        "}\n"
                        "serializer.end_object();\n");
                    break;
                }

                ref_msgs.insert(var_name2);
                impl.Print(
                    "if (format == ::mcpack2pb::FORMAT_MCPACK_V2) {\n"
                    "  if (msg.$lcfield$_size()) {\n"
                    "    serializer.begin_mcpack_array(\"$field$\", ::mcpack2pb::FIELD_OBJECT);\n"
                    "    for (int i = 0; i < msg.$lcfield$_size(); ++i) {\n"
                    "      serializer.begin_object();\n"
                    "      serialize_$vmsg2$_body(msg.$lcfield$(i), serializer, format);\n"
                    "      serializer.end_object();\n"
                    "    }\n"
                    "    serializer.end_array();\n"
1014
                    "  }"
gejun's avatar
gejun committed
1015 1016 1017
                    , "field", get_idl_name(f)
                    , "lcfield", f->lowercase_name()
                    , "vmsg2", var_name2);
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
                if (f->options().GetExtension(idl_on)) {
                    impl.Print(
                       " else {\n"
                       "    serializer.add_empty_array(\"$field$\");\n"
                       "  }\n", "field", get_idl_name(f));
                } else {
                    impl.Print("\n");
                }
                impl.Print("} else if (msg.$lcfield$_size()) {\n"
                    , "lcfield", f->lowercase_name());
gejun's avatar
gejun committed
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
                impl.Indent();
                impl.Print("serializer.begin_object(\"$field$\");\n"
                           , "field", get_idl_name(f));
                for (int j = 0; j < msg2->field_count(); ++j) {
                    const google::protobuf::FieldDescriptor* f2 = msg2->field(j);
                    ConvertibleIdlType cit2 = f2->options().GetExtension(idl_type);
                    impl.Print(
                        "serializer.begin_mcpack_array(\"$f2$\", ::mcpack2pb::FIELD_$TYPE$);\n"
                        "for (int i = 0; i < msg.$lcfield$_size(); ++i) {\n"
                        , "f2", get_idl_name(f2)
                        , "TYPE", (f2->is_repeated() ? "ARRAY" : to_mcpack_typestr_uppercase(cit2, (f2)))
                        , "lcfield", f->lowercase_name());
                    impl.Indent();
                    if (f2->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE) {
                        if (cit2 != IDL_AUTO) {
                            LOG(ERROR) << "Disallow converting " << f2->full_name()
                                       << " (" << f2->message_type()->full_name() << ") to "
                                       << to_mcpack_typestr(cit2, f2) << " (idl)";
                            return false;
                        }
                        std::string var_name3 = mcpack2pb::to_var_name(f2->message_type()->full_name());
                        ref_msgs.insert(var_name3);
                        if (f2->is_repeated()) {
                            impl.Print(
                                "const int $lcfield2$_size = msg.$lcfield$(i).$lcfield2$_size();\n"
                                "if ($lcfield2$_size) {\n"
                                "  serializer.begin_mcpack_array(::mcpack2pb::FIELD_OBJECT);\n"
                                "  for (int j = 0; j < $lcfield2$_size; ++j) {\n"
                                "    serializer.begin_object();\n"
                                "    serialize_$vmsg3$_body(msg.$lcfield$(i).$lcfield2$(j), serializer, format);\n"
                                "    serializer.end_object();\n"
                                "  }\n"
                                "  serializer.end_array();\n"
1061
                                "}"
gejun's avatar
gejun committed
1062 1063 1064
                                , "vmsg3", var_name3
                                , "lcfield", f->lowercase_name()
                                , "lcfield2", f2->lowercase_name());
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
                            if (f2->options().GetExtension(idl_on)) {
                                impl.Print(
                                " else {\n"
                                "  serializer.add_empty_array();\n"
                                "}\n");
                            } else {
                                impl.Print(
                                " else {\n"
                                "  serializer.add_null();\n"
                                "}\n");
                            }
gejun's avatar
gejun committed
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
                        } else {
                            impl.Print(
                                "if (msg.$lcfield$(i).has_$lcfield2$()) {\n"
                                "  serializer.begin_object();\n"
                                "  serialize_$vmsg3$_body(msg.$lcfield$(i).$lcfield2$(), serializer, format);\n"
                                "  serializer.end_object();\n"
                                "} else {\n"
                                "  serializer.add_null();\n"
                                "}\n"
                                , "vmsg3", var_name3
                                , "lcfield", f->lowercase_name()
                                , "lcfield2", f2->lowercase_name());
                        }
                    } else if (f2->is_repeated()) {
1090
                        const std::string msgstr = butil::string_printf(
gejun's avatar
gejun committed
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
                            "msg.%s(i)", f->lowercase_name().c_str());
                        switch (f2->cpp_type()) {
                        case google::protobuf::FieldDescriptor::CPPTYPE_INT32:
                        case google::protobuf::FieldDescriptor::CPPTYPE_UINT32:
                        case google::protobuf::FieldDescriptor::CPPTYPE_ENUM:
                            TEMPLATE_INNER_SERIALIZE_REPEATED_INTEGRAL(
                                msgstr.c_str(), cit2, impl, f2,
                                (cit2 == IDL_INT32 || cit2 == IDL_UINT32));
                            break;
                        case google::protobuf::FieldDescriptor::CPPTYPE_INT64:
                        case google::protobuf::FieldDescriptor::CPPTYPE_UINT64:
                            TEMPLATE_INNER_SERIALIZE_REPEATED_INTEGRAL(
                                msgstr.c_str(), cit2, impl, f2,
                                (cit2 == IDL_INT64 || cit2 == IDL_UINT64));
                            break;
                        case google::protobuf::FieldDescriptor::CPPTYPE_BOOL:
                            TEMPLATE_INNER_SERIALIZE_REPEATED(
                                msgstr.c_str(), cit2, impl, f2,
                                (cit2 == IDL_AUTO || cit2 == IDL_BOOL), false);
                            break;
                        case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
                            TEMPLATE_INNER_SERIALIZE_REPEATED(
                                msgstr.c_str(), cit2, impl, f2,
                                (cit2 == IDL_AUTO || cit2 == IDL_FLOAT), (cit2 == IDL_DOUBLE));
                            break;
                        case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
                            TEMPLATE_INNER_SERIALIZE_REPEATED(
                                msgstr.c_str(), cit2, impl, f2,
                                (cit2 == IDL_AUTO || cit2 == IDL_DOUBLE), (cit2 == IDL_FLOAT));
                            break;
                        case google::protobuf::FieldDescriptor::CPPTYPE_STRING:
                            if (f2->type() == google::protobuf::FieldDescriptor::TYPE_STRING) {
                                TEMPLATE_INNER_SERIALIZE_REPEATED(
                                    msgstr.c_str(), cit2, impl, f2, false,
                                    (cit2 == IDL_AUTO || cit2 == IDL_STRING));
                            } else if (f2->type() == google::protobuf::FieldDescriptor::TYPE_BYTES) {
                                TEMPLATE_INNER_SERIALIZE_REPEATED(
                                    msgstr.c_str(), cit2, impl, f2, false,
                                    (cit2 == IDL_AUTO || cit2 == IDL_BINARY || cit2 == IDL_STRING));
                            } else {
                                LOG(ERROR) << "Unknown pb type=" << f2->type();
                                return false;
                            }
                            break;
                        case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
                            CHECK(false) << "Impossible";
                            return false;
                        }
                    } else {
1140
                        const std::string msgstr = butil::string_printf(
gejun's avatar
gejun committed
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
                            "msg.%s(i)", f->lowercase_name().c_str());
                        switch (f2->cpp_type()) {
                        case google::protobuf::FieldDescriptor::CPPTYPE_INT32:
                        case google::protobuf::FieldDescriptor::CPPTYPE_INT64:
                        case google::protobuf::FieldDescriptor::CPPTYPE_UINT32:
                        case google::protobuf::FieldDescriptor::CPPTYPE_UINT64:
                        case google::protobuf::FieldDescriptor::CPPTYPE_ENUM:
                            TEMPLATE_INNER_SERIALIZE_INTEGRAL(msgstr.c_str(), cit2, impl, f2);
                            break;
                        case google::protobuf::FieldDescriptor::CPPTYPE_BOOL:
                            TEMPLATE_INNER_SERIALIZE(
                                msgstr.c_str(), cit2, impl, f2,
                                (cit2 == IDL_AUTO || cit2 == IDL_BOOL));
                            break;
                        case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
                            TEMPLATE_INNER_SERIALIZE(
                                msgstr.c_str(), cit2, impl, f2,
                                (cit2 == IDL_AUTO || cit2 == IDL_FLOAT));
                            break;
                        case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
                            TEMPLATE_INNER_SERIALIZE(
                                msgstr.c_str(), cit2, impl, f2,
                                (cit2 == IDL_AUTO || cit2 == IDL_FLOAT || cit2 == IDL_DOUBLE));
                            break;
                        case google::protobuf::FieldDescriptor::CPPTYPE_STRING:
                            if (f2->type() == google::protobuf::FieldDescriptor::TYPE_STRING) {
                                TEMPLATE_INNER_SERIALIZE(
                                    msgstr.c_str(), cit2, impl, f2,
                                    (cit2 == IDL_AUTO || cit2 == IDL_STRING));
                            } else if (f2->type() == google::protobuf::FieldDescriptor::TYPE_BYTES) {
                                TEMPLATE_INNER_SERIALIZE(
                                    msgstr.c_str(), cit2, impl, f2,
                                    (cit2 == IDL_AUTO || cit2 == IDL_BINARY || cit2 == IDL_STRING));
                            } else {
                                LOG(ERROR) << "Unknown pb type=" << f2->type();
                                return false;
                            }
                            break;
                        case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
                            LOG(ERROR) << "Impossible";
                            return false;
                        }
                    }
                    impl.Outdent();
                    impl.Print("}\n"
                               "serializer.end_array();\n");
                }
                impl.Print("serializer.end_object_iso();\n");
                impl.Outdent();
                impl.Print("} else {\n"
                           "  serializer.begin_object(\"$field$\");\n"
                           "  serializer.end_object_iso();\n"
                           "}\n"
                           , "field", get_idl_name(f));
            } break;
            } // switch
        } else {
            switch (f->cpp_type()) {
            case google::protobuf::FieldDescriptor::CPPTYPE_INT32:
            case google::protobuf::FieldDescriptor::CPPTYPE_INT64:
            case google::protobuf::FieldDescriptor::CPPTYPE_UINT32:
            case google::protobuf::FieldDescriptor::CPPTYPE_UINT64:
            case google::protobuf::FieldDescriptor::CPPTYPE_ENUM:
                TEMPLATE_SERIALIZE_INTEGRAL(cit, impl, f);
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_BOOL:
                TEMPLATE_SERIALIZE(cit, impl, f,
                                   (cit == IDL_AUTO || cit == IDL_BOOL));
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
                TEMPLATE_SERIALIZE(cit, impl, f,
                                   (cit == IDL_AUTO || cit == IDL_FLOAT));
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
                TEMPLATE_SERIALIZE(
                    cit, impl, f,
                    (cit == IDL_AUTO || cit == IDL_FLOAT || cit == IDL_DOUBLE));
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_STRING:
                if (f->type() == google::protobuf::FieldDescriptor::TYPE_STRING) {
                    TEMPLATE_SERIALIZE(
                        cit, impl, f, (cit == IDL_AUTO || cit == IDL_STRING));
                } else if (f->type() == google::protobuf::FieldDescriptor::TYPE_BYTES) {
                    TEMPLATE_SERIALIZE(
                        cit, impl, f,
                        (cit == IDL_AUTO || cit == IDL_BINARY || cit == IDL_STRING));
                } else {
                    LOG(ERROR) << "Unknown pb type=" << f->type();
                    return false;
                }
                break;
            case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {
                if (cit != IDL_AUTO) {
                    LOG(ERROR) << "Disallow converting " << f->full_name()
                               << " (" << f->message_type()->full_name() << ") to "
                               << to_mcpack_typestr(cit, f) << " (idl)";
                    return false;
                }
                std::string var_name2 = mcpack2pb::to_var_name(f->message_type()->full_name());
                ref_msgs.insert(var_name2);
                impl.Print("if (msg.has_$lcfield$()) {\n"
                           "  serializer.begin_object(\"$field$\");\n"
                           "  serialize_$vmsg$_body(msg.$lcfield$(), serializer, format);\n"
                           "  serializer.end_object();\n"
                           "}"
                           , "field", get_idl_name(f)
                           , "lcfield", f->lowercase_name()
                           , "vmsg", var_name2);
            } break;
            } // switch
            if (f->is_required()) {
                impl.Print(" else {\n"
                           "  LOG(ERROR) << \"Missing required $field$\";\n"
                           "  return serializer.set_bad();\n"
                           "}\n", "field", f->full_name());
            } else {
                impl.Print("\n");
            }
        } // else
    }
    impl.Outdent();
    impl.Print(
        "}\n"
        "bool serialize_$vmsg$(\n"
        "    const ::google::protobuf::Message& msg_base,\n"
        "    ::google::protobuf::io::ZeroCopyOutputStream* output,\n"
        "    ::mcpack2pb::SerializationFormat format) {\n"
        "  ::mcpack2pb::OutputStream ostream(output);\n"
        "  ::mcpack2pb::Serializer serializer(&ostream);\n"
        "  serializer.begin_object();\n"
        "  serialize_$vmsg$_body(msg_base, serializer, format);\n"
        "  serializer.end_object();\n"
        "  ostream.done();\n"
        "  return serializer.good();\n"
        "}\n"
        , "vmsg", var_name);
    return !impl.failed();
}

static std::string protobuf_style_normalize_filename(const std::string & fname) {
    std::string norm_fname;
    norm_fname.reserve(fname.size() + 10);
    for (size_t i = 0; i < fname.size(); ++i) {
        if (fname[i] == '_' || isdigit(fname[i]) || isalpha(fname[i])) {
            norm_fname.push_back(fname[i]);
        } else {
            char symbol[4];
            snprintf(symbol, sizeof(symbol), "_%02x", (int)fname[i]);
            norm_fname.append(symbol, 3);
        }
    }
    return norm_fname;
}

static bool generate_registration(
    const google::protobuf::FileDescriptor* file,
    google::protobuf::io::Printer & impl) {
    const std::string cpp_ns = to_cpp_name(file->package());
    std::string norm_fname = protobuf_style_normalize_filename(file->name());
    impl.Print(
        "\n// register all message handlers\n"
        "struct RegisterMcpackFunctions_$norm_fname$ {\n"
        "  RegisterMcpackFunctions_$norm_fname$() {\n"
        , "norm_fname", norm_fname);
    impl.Indent();
    impl.Indent();
    for (int i = 0; i < file->message_type_count(); ++i) {
        const google::protobuf::Descriptor* d = file->message_type(i);
        std::string var_name = mcpack2pb::to_var_name(d->full_name());

        impl.Print(
            "\n"
            "g_$vmsg$_fields = new ::mcpack2pb::FieldMap;\n"
            "CHECK_EQ(0, g_$vmsg$_fields->init(std::max($field_count$, 1), 30));\n"
            , "vmsg", var_name
1316
            , "field_count", ::butil::string_printf("%d", d->field_count())); 
gejun's avatar
gejun committed
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
        for (int i = 0; i < d->field_count(); ++i) {
            const google::protobuf::FieldDescriptor* f = d->field(i);
            impl.Print("(*g_$vmsg$_fields)[\"$field$\"] = ::set_$vmsg$_$lcfield$;\n"
                       , "vmsg", var_name
                       , "field", get_idl_name(f)
                       , "lcfield", f->lowercase_name());
        }
        impl.Print(
            "::mcpack2pb::MessageHandler $vmsg$_handler = {\n"
            "  parse_$vmsg$,\n"
            "  parse_$vmsg$_body,\n"
            "  serialize_$vmsg$,\n"
            "  serialize_$vmsg$_body\n"
            "};\n"
            "::mcpack2pb::register_message_handler_or_die(\"$fmsg$\", $vmsg$_handler);\n"
            , "fmsg", d->full_name()
            , "vmsg", var_name);
    }
    impl.Outdent();
    impl.Outdent();
    impl.Print("  }\n"
               "} static_init_mcpack_$suffix$;\n"
               , "suffix", norm_fname);
    return !impl.failed();
}

class McpackToProtobuf : public google::protobuf::compiler::CodeGenerator {
public:
    bool Generate(const google::protobuf::FileDescriptor* file,
                  const std::string& parameter,
                  google::protobuf::compiler::GeneratorContext*,
                  std::string* error) const;
};

bool McpackToProtobuf::Generate(const google::protobuf::FileDescriptor* file,
                                const std::string& /*parameter*/,
                                google::protobuf::compiler::GeneratorContext* ctx,
                                std::string* error) const {
    if (!file->options().GetExtension(idl_support)) {
        // skip the file.
        return true;
    }
    
    std::string cpp_name = file->name();
    const size_t pos = cpp_name.find_last_of('.');
    if (pos == std::string::npos) {
1363
        ::butil::string_printf(error, "Bad filename=%s", cpp_name.c_str());
gejun's avatar
gejun committed
1364 1365 1366 1367 1368 1369 1370
        return false;
    }
    cpp_name.resize(pos);
    cpp_name.append(".pb.cc");
    
    google::protobuf::io::Printer inc_printer(
        ctx->OpenForInsert(cpp_name, "includes"), '$');
1371
    inc_printer.Print("#include <butil/logging.h>\n"
gejun's avatar
gejun committed
1372 1373 1374 1375 1376 1377 1378 1379 1380
                      "#include <mcpack2pb/mcpack2pb.h>\n"
                      "#include <gflags/gflags.h>\n");

    google::protobuf::io::Printer gdecl_printer(
        ctx->OpenForInsert(cpp_name, "includes"), '$');
    google::protobuf::io::Printer gimpl_printer(
        ctx->OpenForInsert(cpp_name, "global_scope"), '$');

    gdecl_printer.Print(
gejun's avatar
gejun committed
1381
        "\n// ==== declarations generated by brpc/mcpack2pb/protoc-gen-mcpack ====\n"
gejun's avatar
gejun committed
1382 1383 1384 1385 1386 1387 1388
        "DECLARE_bool(mcpack2pb_absent_field_is_error);\n");

    std::set<std::string> ref_msgs;
    std::set<std::string> ref_maps;
    for (int i = 0; i < file->message_type_count(); ++i) {
        const google::protobuf::Descriptor* d = file->message_type(i);
        if (!generate_parsing(d, ref_msgs, ref_maps, gimpl_printer)) {
1389
            ::butil::string_printf(
gejun's avatar
gejun committed
1390 1391 1392 1393 1394
                error, "Fail to generate parsing code for %s",
                d->full_name().c_str());
            return false;
        }
        if (!generate_serializing(d, ref_msgs, ref_maps, gimpl_printer)) {
1395
            ::butil::string_printf(
gejun's avatar
gejun committed
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
                error, "Fail to generate serializing code for %s",
                d->full_name().c_str());
            return false;
        }
        std::string var_name = mcpack2pb::to_var_name(d->full_name());
        gdecl_printer.Print(
            "::mcpack2pb::FieldMap* g_$vmsg$_fields = NULL;\n"
            , "vmsg", var_name);
    }
    if (!generate_declarations(ref_msgs, ref_maps, gdecl_printer)) {
1406
        ::butil::string_printf(
gejun's avatar
gejun committed
1407 1408 1409 1410 1411
            error, "Fail to generate declarations for %s",
            cpp_name.c_str());
        return false;        
    }
    if (!generate_registration(file, gimpl_printer)) {
1412
        ::butil::string_printf(
gejun's avatar
gejun committed
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
            error, "Fail to generate registration code for %s",
            cpp_name.c_str());
        return false;
    }
    return true;
}
} // namespace mcpack2pb

int main(int argc, char* argv[]) {
    ::mcpack2pb::McpackToProtobuf generator;
    return google::protobuf::compiler::PluginMain(argc, argv, &generator);
}