esx_vi_generator.py 63.4 KB
Newer Older
1
#!/usr/bin/env python
2 3 4 5

#
# esx_vi_generator.py: generates most of the SOAP type mapping code
#
6
# Copyright (C) 2010-2011 Matthias Bolte <matthias.bolte@googlemail.com>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#

import sys
import os
import os.path



29 30 31 32 33
OCCURRENCE__REQUIRED_ITEM = "r"
OCCURRENCE__REQUIRED_LIST = "rl"
OCCURRENCE__OPTIONAL_ITEM = "o"
OCCURRENCE__OPTIONAL_LIST = "ol"
OCCURRENCE__IGNORED = "i"
34

35 36 37 38 39
valid_occurrences = [OCCURRENCE__REQUIRED_ITEM,
                     OCCURRENCE__REQUIRED_LIST,
                     OCCURRENCE__OPTIONAL_ITEM,
                     OCCURRENCE__OPTIONAL_LIST,
                     OCCURRENCE__IGNORED]
40

41 42 43 44 45 46 47 48 49
autobind_map = { "FileManager"        : "fileManager",
                 "PerformanceManager" : "perfManager",
                 "PropertyCollector"  : "propertyCollector",
                 "SearchIndex"        : "searchIndex",
                 "SessionManager"     : "sessionManager",
                 "VirtualDiskManager" : "virtualDiskManager" }

autobind_map_usage = set()

50 51


52 53
def aligned(left, right, length=59):
    while len(left) < length:
M
Matthias Bolte 已提交
54 55 56
        left += " "

    return left + right
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75



class Parameter:
    def __init__(self, type, name, occurrence):
        self.type = type
        self.occurrence = occurrence

        if ':' in name and name.startswith("_this"):
            self.name, self.autobind_type = name.split(":")
        else:
            self.name = name
            self.autobind_type = None


    def is_enum(self):
        return self.type in predefined_enums or self.type in enums_by_name


76 77 78 79 80 81 82 83
    def is_object(self):
        return self.type in predefined_objects or self.type in objects_by_name


    def is_type_generated(self):
        return self.type in enums_by_name or self.type in objects_by_name


84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    def generate_parameter(self, is_last = False, is_header = True, offset = 0):
        if self.occurrence == OCCURRENCE__IGNORED:
            raise ValueError("invalid function parameter occurrence value '%s'" % self.occurrence)
        elif self.autobind_type is not None:
            return ""
        else:
            string = "       "
            string += " " * offset
            string += "%s%s" % (self.get_type_string(), self.name)

            if is_last:
                if is_header:
                    string += "); "
                else:
                    string += "), "
            else:
                string += ", "

M
Matthias Bolte 已提交
102
            return aligned(string, self.get_occurrence_comment() + "\n")
103 104 105 106


    def generate_return(self, offset = 0, end_of_line = ";"):
        if self.occurrence == OCCURRENCE__IGNORED:
107
            raise ValueError("invalid function parameter occurrence value '%s'" % self.occurrence)
108 109 110
        else:
            string = "       "
            string += " " * offset
111
            string += "%s%s)%s" % (self.get_type_string(True), self.name, end_of_line)
112

M
Matthias Bolte 已提交
113
            return aligned(string, self.get_occurrence_comment() + "\n")
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133


    def generate_require_code(self):
        if self.occurrence in [OCCURRENCE__REQUIRED_ITEM,
                               OCCURRENCE__REQUIRED_LIST]:
            return "    ESX_VI__METHOD__PARAMETER__REQUIRE(%s)\n" % self.name
        else:
            return ""


    def generate_serialize_code(self):
        if self.occurrence in [OCCURRENCE__REQUIRED_LIST,
                               OCCURRENCE__OPTIONAL_LIST]:
            return "    ESX_VI__METHOD__PARAMETER__SERIALIZE_LIST(%s, %s)\n" % (self.type, self.name)
        elif self.type == "String":
            return "    ESX_VI__METHOD__PARAMETER__SERIALIZE_VALUE(String, %s)\n" % self.name
        else:
            return "    ESX_VI__METHOD__PARAMETER__SERIALIZE(%s, %s)\n" % (self.type, self.name)


134 135 136
    def get_type_string(self, as_return_value = False):
        string = ""

137 138 139
        if self.type == "String" and \
           self.occurrence not in [OCCURRENCE__REQUIRED_LIST,
                                   OCCURRENCE__OPTIONAL_LIST]:
140 141 142 143
            if as_return_value:
                string += "char *"
            else:
                string += "const char *"
144
        elif self.is_enum():
145
            string += "esxVI_%s " % self.type
146
        else:
147 148 149 150 151 152
            string += "esxVI_%s *" % self.type

        if as_return_value:
            string += "*"

        return string
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


    def get_occurrence_comment(self):
        if self.occurrence == OCCURRENCE__REQUIRED_ITEM:
            return "/* required */"
        elif self.occurrence == OCCURRENCE__REQUIRED_LIST:
            return "/* required, list */"
        elif self.occurrence == OCCURRENCE__OPTIONAL_ITEM:
            return "/* optional */"
        elif self.occurrence == OCCURRENCE__OPTIONAL_LIST:
            return "/* optional, list */"

        raise ValueError("unknown occurrence value '%s'" % self.occurrence)


    def get_occurrence_short_enum(self):
        if self.occurrence == OCCURRENCE__REQUIRED_ITEM:
            return "RequiredItem"
        elif self.occurrence == OCCURRENCE__REQUIRED_LIST:
            return "RequiredList"
        elif self.occurrence == OCCURRENCE__OPTIONAL_ITEM:
            return "OptionalItem"
        elif self.occurrence == OCCURRENCE__OPTIONAL_LIST:
            return "OptionalList"

        raise ValueError("unknown occurrence value '%s'" % self.occurrence)



class Method:
    def __init__(self, name, parameters, returns):
        self.name = name
        self.parameters = []
        self.autobind_parameter = None
        self.returns = returns

        for parameter in parameters:
            if parameter.autobind_type is None:
                self.parameters.append(parameter)
            else:
                self.autobind_parameter = parameter


    def generate_header(self):
        header = "int esxVI_%s\n" % self.name
        header += "      (esxVI_Context *ctx"

        if len(self.parameters) > 0 or self.returns is not None:
            header += ",\n"

            for parameter in self.parameters[:-1]:
                header += parameter.generate_parameter()

            if self.returns is None:
                header += self.parameters[-1].generate_parameter(is_last = True)
            else:
                header += self.parameters[-1].generate_parameter()
                header += self.returns.generate_return()
        else:
            header += ");\n"

        header += "\n"

        return header


    def generate_source(self):
        source = "/* esxVI_%s */\n" % self.name
        source += "ESX_VI__METHOD(%s," % self.name

        if self.autobind_parameter is not None:
224 225
            autobind_map_usage.add(self.autobind_parameter.autobind_type)
            source += " %s,\n" % autobind_map[self.autobind_parameter.autobind_type]
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
        else:
            source += " /* explicit _this */,\n"

        source += "               (esxVI_Context *ctx"

        if len(self.parameters) > 0 or self.returns is not None:
            source += ",\n"

            for parameter in self.parameters[:-1]:
                source += parameter.generate_parameter(is_header = False, offset = 9)

            if self.returns is None:
                source += self.parameters[-1].generate_parameter(is_last = True, is_header = False, offset = 9)
            else:
                source += self.parameters[-1].generate_parameter(is_header = False, offset = 9)
                source += self.returns.generate_return(offset = 9, end_of_line = ",")
        else:
            source += "),\n"

        if self.returns is None:
246 247 248
            source += "               void, /* nothing */, None,\n"
        elif self.returns.type == "String":
            source += "               String, Value, %s,\n" % self.returns.get_occurrence_short_enum()
249
        else:
250
            source += "               %s, /* nothing */, %s,\n" % (self.returns.type, self.returns.get_occurrence_short_enum())
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

        source += "{\n"

        if self.autobind_parameter is not None:
            source += self.autobind_parameter.generate_require_code()

        for parameter in self.parameters:
            source += parameter.generate_require_code()

        source += "},\n"
        source += "{\n"

        if self.autobind_parameter is not None:
            source += self.autobind_parameter.generate_serialize_code()

        for parameter in self.parameters:
            source += parameter.generate_serialize_code()

        source += "})\n\n\n\n"

        return source



class Property:
276 277 278 279 280
    def __init__(self, type, name, occurrence):
        self.type = type
        self.name = name
        self.occurrence = occurrence

281

282 283 284
    def is_enum(self):
        return self.type in predefined_enums or self.type in enums_by_name

285

286 287 288 289 290 291 292 293
    def is_object(self):
        return self.type in predefined_objects or self.type in objects_by_name


    def is_type_generated(self):
        return self.type in enums_by_name or self.type in objects_by_name


294
    def generate_struct_member(self):
295
        if self.occurrence == OCCURRENCE__IGNORED:
296 297 298 299
            return "    /* FIXME: %s is currently ignored */\n" % self.name
        else:
            string = "    %s%s; " % (self.get_type_string(), self.name)

M
Matthias Bolte 已提交
300
            return aligned(string, self.get_occurrence_comment() + "\n")
301

302 303 304

    def generate_free_code(self):
        if self.type == "String" and \
305 306 307
           self.occurrence not in [OCCURRENCE__REQUIRED_LIST,
                                   OCCURRENCE__OPTIONAL_LIST,
                                   OCCURRENCE__IGNORED]:
308 309 310 311
            return "    VIR_FREE(item->%s);\n" % self.name
        elif self.is_enum():
            return ""
        else:
312
            if self.occurrence == OCCURRENCE__IGNORED:
313 314 315 316
                return "    /* FIXME: %s is currently ignored */\n" % self.name
            else:
                return "    esxVI_%s_Free(&item->%s);\n" % (self.type, self.name)

317

318 319 320 321 322 323
    def generate_validate_code(self, managed=False):
        if managed:
            macro = "ESX_VI__TEMPLATE__PROPERTY__MANAGED_REQUIRE"
        else:
            macro = "ESX_VI__TEMPLATE__PROPERTY__REQUIRE"

324 325
        if self.occurrence in [OCCURRENCE__REQUIRED_ITEM,
                               OCCURRENCE__REQUIRED_LIST]:
326
            return "    %s(%s)\n" % (macro, self.name)
327
        elif self.occurrence == OCCURRENCE__IGNORED:
328 329 330 331
            return "    /* FIXME: %s is currently ignored */\n" % self.name
        else:
            return ""

332

333
    def generate_deep_copy_code(self):
334
        if self.occurrence == OCCURRENCE__IGNORED:
335
            return "    /* FIXME: %s is currently ignored */\n" % self.name
336 337
        elif self.occurrence in [OCCURRENCE__REQUIRED_LIST,
                                 OCCURRENCE__OPTIONAL_LIST]:
338 339 340
            return "    ESX_VI__TEMPLATE__PROPERTY__DEEP_COPY_LIST(%s, %s)\n" % (self.type, self.name)
        elif self.type == "String":
            return "    ESX_VI__TEMPLATE__PROPERTY__DEEP_COPY_VALUE(String, %s)\n" % self.name
341 342
        elif self.is_enum():
            return "    (*dest)->%s = src->%s;\n" % (self.name, self.name)
343 344 345
        else:
            return "    ESX_VI__TEMPLATE__PROPERTY__DEEP_COPY(%s, %s)\n" % (self.type, self.name)

346

347
    def generate_serialize_code(self):
348
        if self.occurrence == OCCURRENCE__IGNORED:
349
            return "    /* FIXME: %s is currently ignored */\n" % self.name
350 351
        elif self.occurrence in [OCCURRENCE__REQUIRED_LIST,
                                 OCCURRENCE__OPTIONAL_LIST]:
352 353 354 355 356 357
            return "    ESX_VI__TEMPLATE__PROPERTY__SERIALIZE_LIST(%s, %s)\n" % (self.type, self.name)
        elif self.type == "String":
            return "    ESX_VI__TEMPLATE__PROPERTY__SERIALIZE_VALUE(String, %s)\n" % self.name
        else:
            return "    ESX_VI__TEMPLATE__PROPERTY__SERIALIZE(%s, %s)\n" % (self.type, self.name)

358

359
    def generate_deserialize_code(self):
360
        if self.occurrence == OCCURRENCE__IGNORED:
361
            return "    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_IGNORE(%s) /* FIXME */\n" % self.name
362 363
        elif self.occurrence in [OCCURRENCE__REQUIRED_LIST,
                                 OCCURRENCE__OPTIONAL_LIST]:
364 365 366 367 368 369
            return "    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_LIST(%s, %s)\n" % (self.type, self.name)
        elif self.type == "String":
            return "    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_VALUE(String, %s)\n" % self.name
        else:
            return "    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE(%s, %s)\n" % (self.type, self.name)

370

371 372 373 374 375 376 377 378 379 380 381 382
    def generate_lookup_code(self):
        if self.occurrence == OCCURRENCE__IGNORED:
            return "    ESX_VI__TEMPLATE__PROPERTY__CAST_FROM_ANY_TYPE_IGNORE(%s) /* FIXME */\n" % self.name
        elif self.occurrence in [OCCURRENCE__REQUIRED_LIST,
                                 OCCURRENCE__OPTIONAL_LIST]:
            return "    ESX_VI__TEMPLATE__PROPERTY__CAST_LIST_FROM_ANY_TYPE(%s, %s)\n" % (self.type, self.name)
        elif self.type == "String":
            return "    ESX_VI__TEMPLATE__PROPERTY__CAST_VALUE_FROM_ANY_TYPE(String, %s)\n" % self.name
        else:
            return "    ESX_VI__TEMPLATE__PROPERTY__CAST_FROM_ANY_TYPE(%s, %s)\n" % (self.type, self.name)


383 384
    def get_type_string(self):
        if self.type == "String" and \
385 386
           self.occurrence not in [OCCURRENCE__REQUIRED_LIST,
                                   OCCURRENCE__OPTIONAL_LIST]:
387 388 389 390 391 392 393
            return "char *"
        elif self.is_enum():
            return "esxVI_%s " % self.type
        else:
            return "esxVI_%s *" % self.type


394 395 396 397 398 399 400 401 402
    def get_occurrence_comment(self):
        if self.occurrence == OCCURRENCE__REQUIRED_ITEM:
            return "/* required */"
        elif self.occurrence == OCCURRENCE__REQUIRED_LIST:
            return "/* required, list */"
        elif self.occurrence == OCCURRENCE__OPTIONAL_ITEM:
            return "/* optional */"
        elif self.occurrence == OCCURRENCE__OPTIONAL_LIST:
            return "/* optional, list */"
403

404
        raise ValueError("unknown occurrence value '%s'" % self.occurrence)
405 406 407



M
Matthias Bolte 已提交
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
class Base:
    def __init__(self, kind, name):
        self.kind = kind
        self.name = name


    def generate_typedef(self):
        return "typedef %s _esxVI_%s esxVI_%s;\n" % (self.kind, self.name, self.name)


    def generate_typeenum(self):
        return "    esxVI_Type_%s,\n" % self.name


    def generate_typetostring(self):
        string = "          case esxVI_Type_%s:\n" % self.name
        string += "            return \"%s\";\n\n" % self.name

        return string


    def generate_typefromstring(self):
        string =  "           else if (STREQ(type, \"%s\")) {\n" % self.name
        string += "               return esxVI_Type_%s;\n" % self.name
        string += "           }\n"

        return string



class Object(Base):
439 440 441 442 443 444 445 446
    FEATURE__DYNAMIC_CAST = (1 << 1)
    FEATURE__LIST         = (1 << 2)
    FEATURE__DEEP_COPY    = (1 << 3)
    FEATURE__ANY_TYPE     = (1 << 4)
    FEATURE__SERIALIZE    = (1 << 5)
    FEATURE__DESERIALIZE  = (1 << 6)

    def __init__(self, name, extends, properties, features = 0, extended_by = None):
M
Matthias Bolte 已提交
447
        Base.__init__(self, "struct", name)
448
        self.extends = extends
449
        self.features = features
450 451
        self.properties = properties
        self.extended_by = extended_by
452
        self.candidate_for_dynamic_cast = False
453

454
        if self.extended_by is not None:
M
Matthias Bolte 已提交
455
            self.extended_by.sort()
456

457

M
Matthias Bolte 已提交
458
    def generate_struct_members(self, add_banner=False, struct_gap=False):
459 460
        members = ""

M
Matthias Bolte 已提交
461
        if struct_gap:
462 463 464
            members += "\n"

        if self.extends is not None:
M
Matthias Bolte 已提交
465
            members += objects_by_name[self.extends].generate_struct_members(add_banner=True, struct_gap=False) + "\n"
466 467 468 469 470 471 472 473 474 475 476 477

        if self.extends is not None or add_banner:
            members += "    /* %s */\n" % self.name

        for property in self.properties:
            members += property.generate_struct_member()

        if len(self.properties) < 1:
            members += "    /* no properties */\n"

        return members

478

M
Matthias Bolte 已提交
479
    def generate_free_code(self, add_banner=False):
480 481 482
        source = ""

        if self.extends is not None:
M
Matthias Bolte 已提交
483
            source += objects_by_name[self.extends].generate_free_code(add_banner=True) + "\n"
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502

        if self.extends is not None or add_banner:
            source += "    /* %s */\n" % self.name

        if len(self.properties) < 1:
            source += "    /* no properties */\n"
        else:
            string = ""

            for property in self.properties:
                string += property.generate_free_code()

            if len(string) < 1:
                source += "    /* no properties to be freed */\n"
            else:
                source += string

        return source

503

M
Matthias Bolte 已提交
504
    def generate_validate_code(self, add_banner=False):
505 506 507
        source = ""

        if self.extends is not None:
M
Matthias Bolte 已提交
508
            source += objects_by_name[self.extends].generate_validate_code(add_banner=True) + "\n"
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527

        if self.extends is not None or add_banner:
            source += "    /* %s */\n" % self.name

        if len(self.properties) < 1:
            source += "    /* no properties */\n"
        else:
            string = ""

            for property in self.properties:
                string += property.generate_validate_code()

            if len(string) < 1:
                source += "    /* no required properties */\n"
            else:
                source += string

        return source

528

529
    def generate_dynamic_cast_code(self, is_first = True):
530 531 532
        source = ""

        if self.extended_by is not None:
533 534 535
            if not is_first:
                source += "\n"

536 537 538 539 540 541
            source += "    /* %s */\n" % self.name

            for extended_by in self.extended_by:
                source += "    ESX_VI__TEMPLATE__DYNAMIC_CAST__ACCEPT(%s)\n" % extended_by

            for extended_by in self.extended_by:
542
                source += objects_by_name[extended_by].generate_dynamic_cast_code(False)
543 544 545

        return source

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
    def generate_deep_copy_code(self, add_banner = False):
        source = ""

        if self.extends is not None:
            source += objects_by_name[self.extends].generate_deep_copy_code(add_banner = True) + "\n"

        if self.extends is not None or add_banner:
            source += "    /* %s */\n" % self.name

        if len(self.properties) < 1:
            source += "    /* no properties */\n"
        else:
            string = ""

            for property in self.properties:
                string += property.generate_deep_copy_code()

            if len(string) < 1:
                source += "    /* no properties to be deep copied */\n"
            else:
                source += string

        return source

571

572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
    def generate_serialize_code(self, add_banner = False):
        source = ""

        if self.extends is not None:
            source += objects_by_name[self.extends].generate_serialize_code(add_banner = True) + "\n"

        if self.extends is not None or add_banner:
            source += "    /* %s */\n" % self.name

        if len(self.properties) < 1:
            source += "    /* no properties */\n"
        else:
            for property in self.properties:
                source += property.generate_serialize_code()

        return source

589

590 591 592 593
    def generate_deserialize_code(self, add_banner = False):
        source = ""

        if self.extends is not None:
M
Matthias Bolte 已提交
594
            source += objects_by_name[self.extends].generate_deserialize_code(add_banner=True) + "\n"
595 596 597 598 599 600 601 602 603 604 605 606

        if self.extends is not None or add_banner:
            source += "    /* %s */\n" % self.name

        if len(self.properties) < 1:
            source += "    /* no properties */\n"
        else:
            for property in self.properties:
                source += property.generate_deserialize_code()

        return source

607

608 609
    def generate_header(self):
        header = "/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n"
610
        header += " * VI Object: %s\n" % self.name
611 612

        if self.extends is not None:
613
            header += " *            extends %s\n" % self.extends
614 615 616 617 618 619

        first = True

        if self.extended_by is not None:
            for extended_by in self.extended_by:
                if first:
620
                    header += " *            extended by %s\n" % extended_by
621 622
                    first = False
                else:
623
                    header += " *                        %s\n" % extended_by
624 625 626 627 628 629 630

        header += " */\n\n"

        # struct
        header += "struct _esxVI_%s {\n" % self.name

        if self.features & Object.FEATURE__LIST:
M
Matthias Bolte 已提交
631
            header += aligned("    esxVI_%s *_next; " % self.name, "/* optional */\n")
632
        else:
M
Matthias Bolte 已提交
633
            header += aligned("    esxVI_%s *_unused; " % self.name, "/* optional */\n")
634

M
Matthias Bolte 已提交
635 636
        header += aligned("    esxVI_Type _type; ", "/* required */\n")
        header += self.generate_struct_members(struct_gap=True)
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
        header += "};\n\n"

        # functions
        header += "int esxVI_%s_Alloc(esxVI_%s **item);\n" % (self.name, self.name)
        header += "void esxVI_%s_Free(esxVI_%s **item);\n" % (self.name, self.name)
        header += "int esxVI_%s_Validate(esxVI_%s *item);\n" % (self.name, self.name)

        if self.features & Object.FEATURE__DYNAMIC_CAST:
            if self.extended_by is not None or self.extends is not None:
                header += "esxVI_%s *esxVI_%s_DynamicCast(void *item);\n" % (self.name, self.name)
            else:
                report_error("cannot add dynamic cast support for an untyped object")

        if self.features & Object.FEATURE__LIST:
            header += "int esxVI_%s_AppendToList(esxVI_%s **list, esxVI_%s *item);\n" % (self.name, self.name, self.name)

        if self.features & Object.FEATURE__DEEP_COPY:
            header += "int esxVI_%s_DeepCopy(esxVI_%s **dst, esxVI_%s *src);\n" % (self.name, self.name, self.name)

            if self.features & Object.FEATURE__LIST:
                header += "int esxVI_%s_DeepCopyList(esxVI_%s **dstList, esxVI_%s *srcList);\n" % (self.name, self.name, self.name)

        if self.features & Object.FEATURE__ANY_TYPE:
            header += "int esxVI_%s_CastFromAnyType(esxVI_AnyType *anyType, esxVI_%s **item);\n" % (self.name, self.name)

            if self.features & Object.FEATURE__LIST:
                header += "int esxVI_%s_CastListFromAnyType(esxVI_AnyType *anyType, esxVI_%s **list);\n" % (self.name, self.name)

        if self.features & Object.FEATURE__SERIALIZE:
            header += "int esxVI_%s_Serialize(esxVI_%s *item, const char *element, virBufferPtr output);\n" % (self.name, self.name)

            if self.features & Object.FEATURE__LIST:
                header += "int esxVI_%s_SerializeList(esxVI_%s *list, const char *element, virBufferPtr output);\n" % (self.name, self.name)

        if self.features & Object.FEATURE__DESERIALIZE:
            header += "int esxVI_%s_Deserialize(xmlNodePtr node, esxVI_%s **item);\n" % (self.name, self.name)

            if self.features & Object.FEATURE__LIST:
                header += "int esxVI_%s_DeserializeList(xmlNodePtr node, esxVI_%s **list);\n" % (self.name, self.name)

        header += "\n\n\n"

        return header

681

682 683
    def generate_source(self):
        source = "/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n"
684
        source += " * VI Object: %s\n" % self.name
685 686

        if self.extends is not None:
687
            source += " *            extends %s\n" % self.extends
688 689 690 691 692 693

        first = True

        if self.extended_by is not None:
            for extended_by in self.extended_by:
                if first:
694
                    source += " *            extended by %s\n" % extended_by
695 696
                    first = False
                else:
697
                    source += " *                        %s\n" % extended_by
698 699 700 701 702 703 704 705 706 707 708 709 710 711

        source += " */\n\n"

        # functions
        source += "/* esxVI_%s_Alloc */\n" % self.name
        source += "ESX_VI__TEMPLATE__ALLOC(%s)\n\n" % self.name

        # free
        if self.extended_by is None:
            source += "/* esxVI_%s_Free */\n" % self.name
            source += "ESX_VI__TEMPLATE__FREE(%s,\n" % self.name
            source += "{\n"

            if self.features & Object.FEATURE__LIST:
712
                if self.extends is not None:
713 714 715 716
                    # avoid "dereferencing type-punned pointer will break strict-aliasing rules" warnings
                    source += "    esxVI_%s *next = (esxVI_%s *)item->_next;\n\n" % (self.extends, self.extends)
                    source += "    esxVI_%s_Free(&next);\n" % self.extends
                    source += "    item->_next = (esxVI_%s *)next;\n\n" % self.name
717 718
                else:
                    source += "    esxVI_%s_Free(&item->_next);\n\n" % self.name
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734

            source += self.generate_free_code()

            source += "})\n\n"
        else:
            source += "/* esxVI_%s_Free */\n" % self.name
            source += "ESX_VI__TEMPLATE__DYNAMIC_FREE(%s,\n" % self.name
            source += "{\n"

            for extended_by in self.extended_by:
                source += "    ESX_VI__TEMPLATE__DISPATCH__FREE(%s)\n" % extended_by

            source += "},\n"
            source += "{\n"

            if self.features & Object.FEATURE__LIST:
735
                if self.extends is not None:
736 737 738 739
                    # avoid "dereferencing type-punned pointer will break strict-aliasing rules" warnings
                    source += "    esxVI_%s *next = (esxVI_%s *)item->_next;\n\n" % (self.extends, self.extends)
                    source += "    esxVI_%s_Free(&next);\n" % self.extends
                    source += "    item->_next = (esxVI_%s *)next;\n\n" % self.name
740 741
                else:
                    source += "    esxVI_%s_Free(&item->_next);\n\n" % self.name
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 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810

            source += self.generate_free_code()

            source += "})\n\n"

        # validate
        source += "/* esxVI_%s_Validate */\n" % self.name
        source += "ESX_VI__TEMPLATE__VALIDATE(%s,\n" % self.name
        source += "{\n"

        source += self.generate_validate_code()

        source += "})\n\n"

        # dynamic cast
        if self.features & Object.FEATURE__DYNAMIC_CAST:
            if self.extended_by is not None or self.extends is not None:
                source += "/* esxVI_%s_DynamicCast */\n" % self.name
                source += "ESX_VI__TEMPLATE__DYNAMIC_CAST(%s,\n" % self.name
                source += "{\n"

                source += self.generate_dynamic_cast_code()

                source += "})\n\n"
            else:
                report_error("cannot add dynamic cast support for an untyped object")

        # append to list
        if self.features & Object.FEATURE__LIST:
            source += "/* esxVI_%s_AppendToList */\n" % self.name
            source += "ESX_VI__TEMPLATE__LIST__APPEND(%s)\n\n" % self.name

        # deep copy
        if self.extended_by is None:
            if self.features & Object.FEATURE__DEEP_COPY:
                source += "/* esxVI_%s_DeepCopy */\n" % self.name
                source += "ESX_VI__TEMPLATE__DEEP_COPY(%s,\n" % self.name
                source += "{\n"

                source += self.generate_deep_copy_code()

                source += "})\n\n"

                if self.features & Object.FEATURE__LIST:
                    source += "/* esxVI_%s_DeepCopyList */\n" % self.name
                    source += "ESX_VI__TEMPLATE__LIST__DEEP_COPY(%s)\n\n" % self.name
        else:
            if self.features & Object.FEATURE__DEEP_COPY:
                source += "/* esxVI_%s_DeepCopy */\n" % self.name
                source += "ESX_VI__TEMPLATE__DYNAMIC_DEEP_COPY(%s)\n" % self.name
                source += "{\n"

                for extended_by in self.extended_by:
                    source += "    ESX_VI__TEMPLATE__DISPATCH__DEEP_COPY(%s)\n" % extended_by

                source += "},\n"
                source += "{\n"

                source += self.generate_deep_copy_code()

                source += "})\n\n"

                if self.features & Object.FEATURE__LIST:
                    source += "/* esxVI_%s_DeepCopyList */\n" % self.name
                    source += "ESX_VI__TEMPLATE__LIST__DEEP_COPY(%s)\n\n" % self.name

        # cast from any type
        if self.features & Object.FEATURE__ANY_TYPE:
            source += "/* esxVI_%s_CastFromAnyType */\n" % self.name
811 812

            if self.extended_by is None:
813
                source += "ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE(%s)\n\n" % self.name
814
            else:
815
                source += "ESX_VI__TEMPLATE__DYNAMIC_CAST_FROM_ANY_TYPE(%s,\n" % self.name
816 817 818 819 820 821
                source += "{\n"

                for extended_by in self.extended_by:
                    source += "    ESX_VI__TEMPLATE__DISPATCH__CAST_FROM_ANY_TYPE(%s)\n" % extended_by

                source += "})\n\n"
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

            if self.features & Object.FEATURE__LIST:
                source += "/* esxVI_%s_CastListFromAnyType */\n" % self.name
                source += "ESX_VI__TEMPLATE__LIST__CAST_FROM_ANY_TYPE(%s)\n\n" % self.name

        # serialize
        if self.extended_by is None:
            if self.features & Object.FEATURE__SERIALIZE:
                source += "/* esxVI_%s_Serialize */\n" % self.name
                source += "ESX_VI__TEMPLATE__SERIALIZE(%s,\n" % self.name
                source += "{\n"

                source += self.generate_serialize_code()

                source += "})\n\n"

                if self.features & Object.FEATURE__LIST:
                    source += "/* esxVI_%s_SerializeList */\n" % self.name
                    source += "ESX_VI__TEMPLATE__LIST__SERIALIZE(%s)\n\n" % self.name
        else:
            if self.features & Object.FEATURE__SERIALIZE:
                source += "/* esxVI_%s_Serialize */\n" % self.name
                source += "ESX_VI__TEMPLATE__DYNAMIC_SERIALIZE(%s,\n" % self.name
                source += "{\n"

                for extended_by in self.extended_by:
                    source += "    ESX_VI__TEMPLATE__DISPATCH__SERIALIZE(%s)\n" % extended_by

                source += "},\n"
                source += "{\n"

                source += self.generate_serialize_code()

                source += "})\n\n"

                if self.features & Object.FEATURE__LIST:
                    source += "/* esxVI_%s_SerializeList */\n" % self.name
                    source += "ESX_VI__TEMPLATE__LIST__SERIALIZE(%s)\n\n" % self.name

        # deserilaize
862 863 864 865 866
        if self.extended_by is None:
            if self.features & Object.FEATURE__DESERIALIZE:
                source += "/* esxVI_%s_Deserialize */\n" % self.name
                source += "ESX_VI__TEMPLATE__DESERIALIZE(%s,\n" % self.name
                source += "{\n"
867

868
                source += self.generate_deserialize_code()
869

870
                source += "})\n\n"
871

872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
                if self.features & Object.FEATURE__LIST:
                    source += "/* esxVI_%s_DeserializeList */\n" % self.name
                    source += "ESX_VI__TEMPLATE__LIST__DESERIALIZE(%s)\n\n" % self.name
        else:
            if self.features & Object.FEATURE__DESERIALIZE:
                source += "/* esxVI_%s_Deserialize */\n" % self.name
                source += "ESX_VI__TEMPLATE__DYNAMIC_DESERIALIZE(%s,\n" % self.name
                source += "{\n"

                for extended_by in self.extended_by:
                    source += "    ESX_VI__TEMPLATE__DISPATCH__DESERIALIZE(%s)\n" % extended_by

                source += "},\n"
                source += "{\n"

                source += self.generate_deserialize_code()

                source += "})\n\n"

                if self.features & Object.FEATURE__LIST:
                    source += "/* esxVI_%s_DeserializeList */\n" % self.name
                    source += "ESX_VI__TEMPLATE__LIST__DESERIALIZE(%s)\n\n" % self.name
894 895 896 897 898 899 900

        source += "\n\n"

        return source



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
class ManagedObject(Base):
    FEATURE__LIST = (1 << 2)


    def __init__(self, name, extends, properties, features=0, extended_by=None):
        Base.__init__(self, "struct", name)
        self.extends = extends
        self.features = features
        self.properties = properties
        self.extended_by = extended_by

        if self.extended_by is not None:
            self.extended_by.sort()


    def generate_struct_members(self, add_banner=False, struct_gap=False):
        members = ""

        if struct_gap:
            members += "\n"

        if self.extends is not None:
            members += managed_objects_by_name[self.extends].generate_struct_members(add_banner=True) + "\n"

        if self.extends is not None or add_banner:
            members += "    /* %s */\n" % self.name

        for property in self.properties:
            members += property.generate_struct_member()

        if len(self.properties) < 1:
            members += "    /* no properties */\n"

        return members


    def generate_free_code(self, add_banner=False):
        source = ""
939

940 941
        if self.extends is not None:
            source += managed_objects_by_name[self.extends].generate_free_code(add_banner=True) + "\n"
942

943 944
        if self.extends is not None or add_banner:
            source += "    /* %s */\n" % self.name
945

946 947 948 949
        if len(self.properties) < 1:
            source += "    /* no properties */\n"
        else:
            string = ""
950

951 952
            for property in self.properties:
                string += property.generate_free_code()
953

954 955 956 957
            if len(string) < 1:
                source += "    /* no properties to be freed */\n"
            else:
                source += string
958

959
        return source
960

961

962 963 964 965 966 967 968 969 970 971 972 973 974
    def generate_validate_code(self, add_banner=False):
        source = ""

        if self.extends is not None:
            source += managed_objects_by_name[self.extends].generate_validate_code(add_banner=True) + "\n"

        if self.extends is not None or add_banner:
            source += "    /* %s */\n" % self.name

        if len(self.properties) < 1:
            source += "    /* no properties */\n"
        else:
            string = ""
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 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 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 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 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 1140 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
            for property in self.properties:
                string += property.generate_validate_code(managed=True)

            if len(string) < 1:
                source += "    /* no required properties */\n"
            else:
                source += string

        return source


    def generate_lookup_code1(self, add_banner=False):
        source = ""

        if self.extends is not None:
            source += managed_objects_by_name[self.extends].generate_lookup_code1(add_banner=True) + "\n"

        if self.extends is not None or add_banner:
            source += "    /* %s */\n" % self.name

        if len(self.properties) < 1:
            source += "    /* no properties */\n"
        else:
            string = ""

            for property in self.properties:
                string += "    \"%s\\0\"\n" % property.name

            if len(string) < 1:
                source += "    /* no properties */\n"
            else:
                source += string

        return source


    def generate_lookup_code2(self, add_banner=False):
        source = ""

        if self.extends is not None:
            source += managed_objects_by_name[self.extends].generate_lookup_code2(add_banner=True) + "\n"

        if self.extends is not None or add_banner:
            source += "    /* %s */\n" % self.name

        if len(self.properties) < 1:
            source += "    /* no properties */\n"
        else:
            string = ""

            for property in self.properties:
                string += property.generate_lookup_code()

            if len(string) < 1:
                source += "    /* no properties */\n"
            else:
                source += string

        return source


    def generate_comment(self):
        comment = "/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n"
        comment += " * VI Managed Object: %s\n" % self.name

        if self.extends is not None:
            comment += " *                    extends %s\n" % self.extends

        first = True

        if self.extended_by is not None:
            for extended_by in self.extended_by:
                if first:
                    comment += " *                    extended by %s\n" % extended_by
                    first = False
                else:
                    comment += " *                                %s\n" % extended_by

        comment += " */\n\n"

        return comment


    def generate_header(self):
        header = self.generate_comment()

        # struct
        header += "struct _esxVI_%s {\n" % self.name

        if self.features & Object.FEATURE__LIST:
            header += aligned("    esxVI_%s *_next; " % self.name, "/* optional */\n")
        else:
            header += aligned("    esxVI_%s *_unused; " % self.name, "/* optional */\n")

        header += aligned("    esxVI_Type _type; ", "/* required */\n")
        header += aligned("    esxVI_ManagedObjectReference *_reference; ", "/* required */\n")
        header += "\n"
        header += self.generate_struct_members()

        header += "};\n\n"

        # functions
        header += "int esxVI_%s_Alloc(esxVI_%s **item);\n" % (self.name, self.name)
        header += "void esxVI_%s_Free(esxVI_%s **item);\n" % (self.name, self.name)
        header += "int esxVI_%s_Validate(esxVI_%s *item, esxVI_String *selectedPropertyNameList);\n" % (self.name, self.name)

        if self.features & Object.FEATURE__LIST:
            header += "int esxVI_%s_AppendToList(esxVI_%s **list, esxVI_%s *item);\n" % (self.name, self.name, self.name)

        header += "\n\n\n"

        return header


    def generate_helper_header(self):
        header = ""

        # functions
        header += ("int esxVI_Lookup%s(esxVI_Context *ctx, " +
                                      "const char *name, " +
                                      "esxVI_ManagedObjectReference *root, " +
                                      "esxVI_String *selectedPropertyNameList, " +
                                      "esxVI_%s **item, " +
                                      "esxVI_Occurrence occurrence);\n") % (self.name, self.name)

        header += "\n"

        return header


    def generate_source(self):
        source = self.generate_comment()

        # functions
        source += "/* esxVI_%s_Alloc */\n" % self.name
        source += "ESX_VI__TEMPLATE__ALLOC(%s)\n\n" % self.name

        # free
        if self.extended_by is None:
            source += "/* esxVI_%s_Free */\n" % self.name
            source += "ESX_VI__TEMPLATE__FREE(%s,\n" % self.name
            source += "{\n"

            if self.features & ManagedObject.FEATURE__LIST:
                if self.extends is not None:
                    # avoid "dereferencing type-punned pointer will break strict-aliasing rules" warnings
                    source += "    esxVI_%s *next = (esxVI_%s *)item->_next;\n\n" % (self.extends, self.extends)
                    source += "    esxVI_%s_Free(&next);\n" % self.extends
                    source += "    item->_next = (esxVI_%s *)next;\n\n" % self.name
                else:
                    source += "    esxVI_%s_Free(&item->_next);\n" % self.name

            source += "    esxVI_ManagedObjectReference_Free(&item->_reference);\n\n"

            source += self.generate_free_code()

            source += "})\n\n"
        else:
            source += "/* esxVI_%s_Free */\n" % self.name
            source += "ESX_VI__TEMPLATE__DYNAMIC_FREE(%s,\n" % self.name
            source += "{\n"

            for extended_by in self.extended_by:
                source += "    ESX_VI__TEMPLATE__DISPATCH__FREE(%s)\n" % extended_by

            source += "},\n"
            source += "{\n"

            if self.features & Object.FEATURE__LIST:
                if self.extends is not None:
                    # avoid "dereferencing type-punned pointer will break strict-aliasing rules" warnings
                    source += "    esxVI_%s *next = (esxVI_%s *)item->_next;\n\n" % (self.extends, self.extends)
                    source += "    esxVI_%s_Free(&next);\n" % self.extends
                    source += "    item->_next = (esxVI_%s *)next;\n\n" % self.name
                else:
                    source += "    esxVI_%s_Free(&item->_next);\n" % self.name

            source += "    esxVI_ManagedObjectReference_Free(&item->_reference);\n\n"

            source += self.generate_free_code()

            source += "})\n\n"

        # validate
        source += "/* esxVI_%s_Validate */\n" % self.name
        source += "ESX_VI__TEMPLATE__MANAGED_VALIDATE(%s,\n" % self.name
        source += "{\n"

        source += self.generate_validate_code()

        source += "})\n\n"

        # append to list
        if self.features & ManagedObject.FEATURE__LIST:
            source += "/* esxVI_%s_AppendToList */\n" % self.name
            source += "ESX_VI__TEMPLATE__LIST__APPEND(%s)\n\n" % self.name

        source += "\n\n"

        return source


    def generate_helper_source(self):
        source = ""

        # lookup
        source += "/* esxVI_Lookup%s */\n" % self.name
        source += "ESX_VI__TEMPLATE__LOOKUP(%s,\n" % self.name
        source += "{\n"

        source += self.generate_lookup_code1()

        source += "},\n"
        source += "{\n"

        source += self.generate_lookup_code2()

        source += "})\n\n"

        source += "\n\n"

        return source
1198

1199

1200

M
Matthias Bolte 已提交
1201 1202 1203 1204 1205 1206 1207 1208 1209
class Enum(Base):
    FEATURE__ANY_TYPE = (1 << 1)
    FEATURE__SERIALIZE = (1 << 2)
    FEATURE__DESERIALIZE = (1 << 3)


    def __init__(self, name, values, features=0):
        Base.__init__(self, "enum", name)
        self.values = values
1210
        self.features = features
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
    def generate_header(self):
        header = "/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n"
        header += " * VI Enum: %s\n" % self.name
        header += " */\n\n"

        # enum
        header += "enum _esxVI_%s {\n" % self.name
        header += "    esxVI_%s_Undefined = 0,\n" % self.name

        for value in self.values:
            header += "    esxVI_%s_%s,\n" % (self.name, capitalize_first(value))

        header += "};\n\n"

        # functions
        if self.features & Enum.FEATURE__ANY_TYPE:
            header += "int esxVI_%s_CastFromAnyType(esxVI_AnyType *anyType, esxVI_%s *item);\n" % (self.name, self.name)

        if self.features & Enum.FEATURE__SERIALIZE:
            header += "int esxVI_%s_Serialize(esxVI_%s item, const char *element, virBufferPtr output);\n" % (self.name, self.name)

        if self.features & Enum.FEATURE__DESERIALIZE:
            header += "int esxVI_%s_Deserialize(xmlNodePtr node, esxVI_%s *item);\n" % (self.name, self.name)

        header += "\n\n\n"

        return header

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
    def generate_source(self):
        source = "/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n"
        source += " * VI Enum: %s\n" % self.name
        source += " */\n\n"

        source += "static const esxVI_Enumeration _esxVI_%s_Enumeration = {\n" % self.name
        source += "    esxVI_Type_%s, {\n" % self.name

        for value in self.values:
            source += "        { \"%s\", esxVI_%s_%s },\n" % (value, self.name, capitalize_first(value))

        source += "        { NULL, -1 },\n"
        source += "    },\n"
        source += "};\n\n"

        # functions
        if self.features & Enum.FEATURE__ANY_TYPE:
            source += "/* esxVI_%s_CastFromAnyType */\n" % self.name
            source += "ESX_VI__TEMPLATE__ENUMERATION__CAST_FROM_ANY_TYPE(%s)\n\n" % self.name

        if self.features & Enum.FEATURE__SERIALIZE:
            source += "/* esxVI_%s_Serialize */\n" % self.name
            source += "ESX_VI__TEMPLATE__ENUMERATION__SERIALIZE(%s)\n\n" % self.name

        if self.features & Enum.FEATURE__DESERIALIZE:
            source += "/* esxVI_%s_Deserialize */\n" % self.name
            source += "ESX_VI__TEMPLATE__ENUMERATION__DESERIALIZE(%s)\n\n" % self.name

        source += "\n\n"

        return source



def report_error(message):
    print "error: " + message
    sys.exit(1)



def capitalize_first(string):
    return string[:1].upper() + string[1:]



def parse_object(block):
1288
    # expected format: [managed] object <name> [extends <name>]
1289
    header_items = block[0][1].split()
1290 1291 1292 1293 1294
    managed = False

    if header_items[0] == "managed":
        managed = True
        del header_items[0]
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318

    if len(header_items) < 2:
        report_error("line %d: invalid block header" % (number))

    assert header_items[0] == "object"

    name = header_items[1]
    extends = None

    if len(header_items) > 2:
        if header_items[2] != "extends":
            report_error("line %d: invalid block header" % (number))
        else:
            extends = header_items[3]

    properties = []

    for line in block[1:]:
        # expected format: <type> <name> <occurrence>
        items = line[1].split()

        if len(items) != 3:
            report_error("line %d: invalid property" % line[0])

1319
        if items[2] not in valid_occurrences:
1320 1321
            report_error("line %d: invalid occurrence" % line[0])

M
Matthias Bolte 已提交
1322 1323
        properties.append(Property(type=items[0], name=items[1],
                                   occurrence=items[2]))
1324

1325 1326 1327 1328
    if managed:
        return ManagedObject(name=name, extends=extends, properties=properties)
    else:
        return Object(name=name, extends=extends, properties=properties)
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348



def parse_enum(block):
    # expected format: enum <name>
    header_items = block[0][1].split()

    if len(header_items) < 2:
        report_error("line %d: invalid block header" % (number))

    assert header_items[0] == "enum"

    name = header_items[1]

    values = []

    for line in block[1:]:
        # expected format: <value>
        values.append(line[1])

M
Matthias Bolte 已提交
1349
    return Enum(name=name, values=values)
1350 1351 1352



1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
def parse_method(block):
    # expected format: method <name> [returns <type> <occurrence>]
    header_items = block[0][1].split()

    if len(header_items) < 2:
        report_error("line %d: invalid block header" % (number))

    assert header_items[0] == "method"

    name = header_items[1]
    returns = None

    if len(header_items) > 2:
        if header_items[2] != "returns":
            report_error("line %d: invalid block header" % (number))
        else:
M
Matthias Bolte 已提交
1369 1370
            returns = Parameter(type=header_items[3], name="output",
                                occurrence=header_items[4])
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383

    parameters = []

    for line in block[1:]:
        # expected format: <type> <name> <occurrence>
        items = line[1].split()

        if len(items) != 3:
            report_error("line %d: invalid property" % line[0])

        if items[2] not in valid_occurrences:
            report_error("line %d: invalid occurrence" % line[0])

M
Matthias Bolte 已提交
1384 1385
        parameters.append(Parameter(type=items[0], name=items[1],
                                    occurrence=items[2]))
1386

M
Matthias Bolte 已提交
1387
    return Method(name=name, parameters=parameters, returns=returns)
1388 1389 1390



1391 1392 1393 1394
def is_known_type(type):
    return type in predefined_objects or \
           type in predefined_enums or \
           type in objects_by_name or \
1395
           type in managed_objects_by_name or \
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
           type in enums_by_name



def open_and_print(filename):
    if filename.startswith("./"):
        print "  GEN    " + filename[2:]
    else:
        print "  GEN    " + filename

    return open(filename, "wb")



predefined_enums = ["Boolean"]

predefined_objects = ["AnyType",
                      "Int",
                      "Long",
                      "String",
                      "DateTime",
1417
                      "MethodFault",
1418 1419 1420 1421 1422 1423
                      "ManagedObjectReference"]

additional_enum_features = { "ManagedEntityStatus"      : Enum.FEATURE__ANY_TYPE,
                             "TaskInfoState"            : Enum.FEATURE__ANY_TYPE,
                             "VirtualMachinePowerState" : Enum.FEATURE__ANY_TYPE }

1424
additional_object_features = { "AutoStartDefaults"          : Object.FEATURE__ANY_TYPE,
1425
                               "AutoStartPowerInfo"         : Object.FEATURE__ANY_TYPE,
1426
                               "DatastoreHostMount"         : Object.FEATURE__DEEP_COPY | Object.FEATURE__LIST | Object.FEATURE__ANY_TYPE,
1427
                               "DatastoreInfo"              : Object.FEATURE__ANY_TYPE | Object.FEATURE__DYNAMIC_CAST,
1428
                               "HostConfigManager"          : Object.FEATURE__ANY_TYPE,
1429
                               "HostCpuIdInfo"              : Object.FEATURE__ANY_TYPE | Object.FEATURE__LIST,
1430
                               "HostDatastoreBrowserSearchResults" : Object.FEATURE__LIST | Object.FEATURE__ANY_TYPE,
1431
                               "ManagedObjectReference"     : Object.FEATURE__ANY_TYPE,
1432
                               "ObjectContent"              : Object.FEATURE__DEEP_COPY,
1433
                               "ResourcePoolResourceUsage"  : Object.FEATURE__ANY_TYPE,
1434
                               "ServiceContent"             : Object.FEATURE__DESERIALIZE,
1435 1436 1437
                               "SharesInfo"                 : Object.FEATURE__ANY_TYPE,
                               "TaskInfo"                   : Object.FEATURE__ANY_TYPE | Object.FEATURE__LIST,
                               "UserSession"                : Object.FEATURE__ANY_TYPE,
1438
                               "VirtualMachineQuestionInfo" : Object.FEATURE__ANY_TYPE,
1439 1440
                               "VirtualMachineSnapshotTree" : Object.FEATURE__DEEP_COPY | Object.FEATURE__ANY_TYPE,
                               "VmEventArgument"            : Object.FEATURE__DESERIALIZE }
1441

1442
removed_object_features = {}
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454



if "srcdir" in os.environ:
    input_filename = os.path.join(os.environ["srcdir"], "esx/esx_vi_generator.input")
    output_dirname = os.path.join(os.environ["srcdir"], "esx")
else:
    input_filename = os.path.join(os.getcwd(), "esx_vi_generator.input")
    output_dirname = os.getcwd()



1455 1456 1457 1458 1459 1460 1461 1462
types_typedef = open_and_print(os.path.join(output_dirname, "esx_vi_types.generated.typedef"))
types_typeenum = open_and_print(os.path.join(output_dirname, "esx_vi_types.generated.typeenum"))
types_typetostring = open_and_print(os.path.join(output_dirname, "esx_vi_types.generated.typetostring"))
types_typefromstring = open_and_print(os.path.join(output_dirname, "esx_vi_types.generated.typefromstring"))
types_header = open_and_print(os.path.join(output_dirname, "esx_vi_types.generated.h"))
types_source = open_and_print(os.path.join(output_dirname, "esx_vi_types.generated.c"))
methods_header = open_and_print(os.path.join(output_dirname, "esx_vi_methods.generated.h"))
methods_source = open_and_print(os.path.join(output_dirname, "esx_vi_methods.generated.c"))
1463
methods_macro = open_and_print(os.path.join(output_dirname, "esx_vi_methods.generated.macro"))
1464 1465
helpers_header = open_and_print(os.path.join(output_dirname, "esx_vi.generated.h"))
helpers_source = open_and_print(os.path.join(output_dirname, "esx_vi.generated.c"))
1466 1467 1468 1469 1470



number = 0
objects_by_name = {}
1471
managed_objects_by_name = {}
1472
enums_by_name = {}
1473
methods_by_name = {}
1474 1475 1476 1477
block = None



1478
# parse input file
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
for line in file(input_filename, "rb").readlines():
    number += 1

    if "#" in line:
        line = line[:line.index("#")]

    line = line.lstrip().rstrip()

    if len(line) < 1:
        continue

1490 1491
    if line.startswith("object") or line.startswith("managed object") or \
       line.startswith("enum") or line.startswith("method"):
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
        if block is not None:
            report_error("line %d: nested block found" % (number))
        else:
            block = []

    if block is not None:
        if line == "end":
            if block[0][1].startswith("object"):
                obj = parse_object(block)
                objects_by_name[obj.name] = obj
1502 1503 1504
            elif block[0][1].startswith("managed object"):
                obj = parse_object(block)
                managed_objects_by_name[obj.name] = obj
1505
            elif block[0][1].startswith("enum"):
1506 1507
                enum = parse_enum(block)
                enums_by_name[enum.name] = enum
1508 1509 1510
            else:
                method = parse_method(block)
                methods_by_name[method.name] = method
1511 1512 1513 1514 1515 1516 1517

            block = None
        else:
            block.append((number, line))



1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
for method in methods_by_name.values():
    # method parameter types must be serializable
    for parameter in method.parameters:
        if not parameter.is_type_generated():
            continue

        if parameter.is_enum():
            enums_by_name[parameter.type].features |= Enum.FEATURE__SERIALIZE
        else:
            objects_by_name[parameter.type].features |= Object.FEATURE__SERIALIZE
1528
            objects_by_name[parameter.type].candidate_for_dynamic_cast = True
1529

1530 1531 1532 1533 1534 1535 1536 1537 1538
        # detect list usage
        if parameter.occurrence == OCCURRENCE__REQUIRED_LIST or \
           parameter.occurrence == OCCURRENCE__OPTIONAL_LIST:
            if parameter.is_enum():
                report_error("unsupported usage of enum '%s' as list in '%s'"
                             % (parameter.type, method.name))
            else:
                objects_by_name[parameter.type].features |= Object.FEATURE__LIST

1539 1540 1541 1542 1543 1544
    # method return types must be deserializable
    if method.returns and method.returns.is_type_generated():
        if method.returns.is_enum():
            enums_by_name[method.returns.type].features |= Enum.FEATURE__DESERIALIZE
        else:
            objects_by_name[method.returns.type].features |= Object.FEATURE__DESERIALIZE
1545
            objects_by_name[method.returns.type].candidate_for_dynamic_cast = True
1546

1547 1548 1549 1550 1551 1552 1553 1554 1555
        # detect list usage
        if method.returns.occurrence == OCCURRENCE__REQUIRED_LIST or \
           method.returns.occurrence == OCCURRENCE__OPTIONAL_LIST:
            if method.returns.is_enum():
                report_error("unsupported usage of enum '%s' as list in '%s'"
                             % (method.returns.type, method.name))
            else:
                objects_by_name[method.returns.type].features |= Object.FEATURE__LIST

1556 1557


1558 1559 1560 1561 1562
for enum in enums_by_name.values():
    # apply additional features
    if enum.name in additional_enum_features:
        enum.features |= additional_enum_features[enum.name]

1563 1564 1565
        if additional_enum_features[enum.name] & Enum.FEATURE__ANY_TYPE:
            enum.features |= Enum.FEATURE__DESERIALIZE

1566 1567 1568 1569


for obj in objects_by_name.values():
    for property in obj.properties:
1570
        if property.occurrence != OCCURRENCE__IGNORED and \
1571 1572 1573 1574 1575 1576 1577 1578
           not is_known_type(property.type):
            report_error("object '%s' contains unknown property type '%s'" % (obj.name, property.type))

    if obj.extends is not None:
        if not is_known_type(obj.extends):
            report_error("object '%s' extends unknown object '%s'" % (obj.name, obj.extends))

    for property in obj.properties:
1579 1580 1581
        if not property.is_type_generated():
            continue

1582 1583 1584 1585 1586 1587
        if property.is_enum():
            enums_by_name[property.type].candidate_for_dynamic_cast = True
        else:
            objects_by_name[property.type].candidate_for_dynamic_cast = True

        # detect list usage
1588 1589 1590 1591 1592 1593 1594
        if property.occurrence == OCCURRENCE__REQUIRED_LIST or \
           property.occurrence == OCCURRENCE__OPTIONAL_LIST:
            if property.is_enum():
                report_error("unsupported usage of enum '%s' as list in '%s'"
                             % (property.type, obj.type))
            else:
                objects_by_name[property.type].features |= Object.FEATURE__LIST
1595 1596 1597 1598 1599

    # apply/remove additional features
    if obj.name in additional_object_features:
        obj.features |= additional_object_features[obj.name]

1600 1601 1602
        if additional_object_features[obj.name] & Object.FEATURE__ANY_TYPE:
            obj.features |= Object.FEATURE__DESERIALIZE

1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
    if obj.name in removed_object_features:
        obj.features &= ~removed_object_features[obj.name]

    # detect extended_by relation
    if obj.extends is not None:
        extended_obj = objects_by_name[obj.extends]

        if extended_obj.extended_by is None:
            extended_obj.extended_by = [obj.name]
        else:
            extended_obj.extended_by.append(obj.name)
1614
            extended_obj.extended_by.sort()
1615 1616 1617



1618 1619 1620 1621 1622 1623 1624 1625 1626
for obj in objects_by_name.values():
    # if an object is a candidate (it is used directly as parameter or return
    # type or is a member of another object) and it is extended by another
    # object then this type needs the dynamic cast feature
    if obj.candidate_for_dynamic_cast and obj.extended_by:
        obj.features |= Object.FEATURE__DYNAMIC_CAST



1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
def propagate_feature(obj, feature):
    global features_have_changed

    if not (obj.features & feature):
        return

    for property in obj.properties:
        if property.occurrence == OCCURRENCE__IGNORED or \
           not property.is_type_generated():
            continue

        if property.is_enum():
            if feature == Object.FEATURE__SERIALIZE and \
               not (enums_by_name[property.type].features & Enum.FEATURE__SERIALIZE):
                enums_by_name[property.type].features |= Enum.FEATURE__SERIALIZE
                features_have_changed = True
            elif feature == Object.FEATURE__DESERIALIZE and \
               not (enums_by_name[property.type].features & Enum.FEATURE__DESERIALIZE):
                enums_by_name[property.type].features |= Enum.FEATURE__DESERIALIZE
                features_have_changed = True
        elif property.is_object():
            if not (objects_by_name[property.type].features & feature):
                objects_by_name[property.type].features |= feature
                features_have_changed = True

            if obj.name != property.type:
                propagate_feature(objects_by_name[property.type], feature)



def inherit_features(obj):
    global features_have_changed

    if obj.extended_by is not None:
        for extended_by in obj.extended_by:
            previous = objects_by_name[extended_by].features
            objects_by_name[extended_by].features |= obj.features

            if objects_by_name[extended_by].features != previous:
                features_have_changed = True

    if obj.extends is not None:
        previous = objects_by_name[obj.extends].features
        objects_by_name[obj.extends].features |= obj.features

        if objects_by_name[obj.extends].features != previous:
            features_have_changed = True

    if obj.extended_by is not None:
        for extended_by in obj.extended_by:
            inherit_features(objects_by_name[extended_by])



# there are two directions to spread features:
# 1) up and down the inheritance chain
# 2) from object types to their member property types
# spreading needs to be done alternating on both directions because they can
# affect each other
features_have_changed = True

while features_have_changed:
    features_have_changed = False

    for obj in objects_by_name.values():
        propagate_feature(obj, Object.FEATURE__DEEP_COPY)
        propagate_feature(obj, Object.FEATURE__SERIALIZE)
        propagate_feature(obj, Object.FEATURE__DESERIALIZE)

    for obj in objects_by_name.values():
        inherit_features(obj)



1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
for obj in managed_objects_by_name.values():
    for property in obj.properties:
        if property.occurrence != OCCURRENCE__IGNORED and \
           not is_known_type(property.type):
            report_error("object '%s' contains unknown property type '%s'" % (obj.name, property.type))

    if obj.extends is not None:
        if not is_known_type(obj.extends):
            report_error("object '%s' extends unknown object '%s'" % (obj.name, obj.extends))

    # detect extended_by relation
    if obj.extends is not None:
        extended_obj = managed_objects_by_name[obj.extends]

        if extended_obj.extended_by is None:
            extended_obj.extended_by = [obj.name]
        else:
            extended_obj.extended_by.append(obj.name)
            extended_obj.extended_by.sort()



1723 1724 1725 1726 1727 1728 1729 1730
types_typedef.write("/* Generated by esx_vi_generator.py */\n\n\n\n")
types_typeenum.write("/* Generated by esx_vi_generator.py */\n\n")
types_typetostring.write("/* Generated by esx_vi_generator.py */\n\n")
types_typefromstring.write("/* Generated by esx_vi_generator.py */\n\n")
types_header.write("/* Generated by esx_vi_generator.py */\n\n\n\n")
types_source.write("/* Generated by esx_vi_generator.py */\n\n\n\n")
methods_header.write("/* Generated by esx_vi_generator.py */\n\n\n\n")
methods_source.write("/* Generated by esx_vi_generator.py */\n\n\n\n")
1731
methods_macro.write("/* Generated by esx_vi_generator.py */\n\n\n\n")
1732 1733
helpers_header.write("/* Generated by esx_vi_generator.py */\n\n\n\n")
helpers_source.write("/* Generated by esx_vi_generator.py */\n\n\n\n")
1734 1735


1736

1737 1738 1739 1740
# output enums
types_typedef.write("/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n" +
                    " * VI Enums\n" +
                    " */\n\n")
1741 1742 1743 1744 1745

names = enums_by_name.keys()
names.sort()

for name in names:
1746 1747 1748 1749 1750 1751
    types_typedef.write(enums_by_name[name].generate_typedef())
    types_typeenum.write(enums_by_name[name].generate_typeenum())
    types_typetostring.write(enums_by_name[name].generate_typetostring())
    types_typefromstring.write(enums_by_name[name].generate_typefromstring())
    types_header.write(enums_by_name[name].generate_header())
    types_source.write(enums_by_name[name].generate_source())
1752 1753 1754



1755 1756 1757
# output objects
types_typedef.write("\n\n\n" +
                    "/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n" +
1758
                    " * VI Objects\n" +
1759 1760 1761 1762
                    " */\n\n")
types_typeenum.write("\n")
types_typetostring.write("\n")
types_typefromstring.write("\n")
1763

1764 1765
names = objects_by_name.keys()
names.sort()
1766

1767 1768 1769 1770 1771 1772 1773
for name in names:
    types_typedef.write(objects_by_name[name].generate_typedef())
    types_typeenum.write(objects_by_name[name].generate_typeenum())
    types_typetostring.write(objects_by_name[name].generate_typetostring())
    types_typefromstring.write(objects_by_name[name].generate_typefromstring())
    types_header.write(objects_by_name[name].generate_header())
    types_source.write(objects_by_name[name].generate_source())
1774 1775 1776



1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
# output managed objects
types_typedef.write("\n\n\n" +
                    "/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n" +
                    " * VI Managed Objects\n" +
                    " */\n\n")
types_typeenum.write("\n")
types_typetostring.write("\n")
types_typefromstring.write("\n")

names = managed_objects_by_name.keys()
names.sort()

for name in names:
    types_typedef.write(managed_objects_by_name[name].generate_typedef())
    types_typeenum.write(managed_objects_by_name[name].generate_typeenum())
    types_typetostring.write(managed_objects_by_name[name].generate_typetostring())
    types_typefromstring.write(managed_objects_by_name[name].generate_typefromstring())
    types_header.write(managed_objects_by_name[name].generate_header())
    types_source.write(managed_objects_by_name[name].generate_source())



1799 1800 1801
# output methods
names = methods_by_name.keys()
names.sort()
1802 1803

for name in names:
1804 1805
    methods_header.write(methods_by_name[name].generate_header())
    methods_source.write(methods_by_name[name].generate_source())
1806

1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817
sorted_usage = list(autobind_map_usage)
sorted_usage.sort()

for usage in sorted_usage:
    name = autobind_map[usage]
    string = aligned("#define ESX_VI__METHOD__PARAMETER__THIS__%s " % name, "\\\n", 78)
    string += "    ESX_VI__METHOD__PARAMETER__THIS_FROM_SERVICE(ManagedObjectReference,      \\\n"
    string += aligned("", "%s)\n\n\n\n" % name, 49)

    methods_macro.write(string)

1818 1819 1820 1821 1822 1823 1824 1825 1826


# output helpers
names = managed_objects_by_name.keys()
names.sort()

for name in names:
    helpers_header.write(managed_objects_by_name[name].generate_helper_header())
    helpers_source.write(managed_objects_by_name[name].generate_helper_source())