You need to sign in or sign up before continuing.
esx_vi_generator.py 65.8 KB
Newer Older
1
#!/usr/bin/env python
2 3 4 5

#
# esx_vi_generator.py: generates most of the SOAP type mapping code
#
M
Matthias Bolte 已提交
6
# Copyright (C) 2010-2012 Matthias Bolte <matthias.bolte@googlemail.com>
7 8 9 10 11 12 13 14 15 16 17 18
#
# 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
19
# License along with this library.  If not, see
20
# <http://www.gnu.org/licenses/>.
21 22 23 24 25 26 27 28
#

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
autobind_names = set()
42

43 44
separator = "/* " + ("* " * 37) + "*\n"

45 46


47 48
def aligned(left, right, length=59):
    while len(left) < length:
M
Matthias Bolte 已提交
49 50 51
        left += " "

    return left + right
52 53 54



55 56
class Member:
    def __init__(self, type, occurrence):
57 58 59 60 61 62 63 64
        self.type = type
        self.occurrence = occurrence


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


65 66 67 68 69 70 71 72
    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


73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    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)



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

        if ':' in name and name.startswith("_this"):
92
            self.name, self.autobind_name = name.split(":")
93 94
        else:
            self.name = name
95
            self.autobind_name = None
96 97


98
    def generate_parameter(self, is_last=False, is_header=True, offset=0):
99
        if self.occurrence == OCCURRENCE__IGNORED:
100 101
            raise ValueError("invalid function parameter occurrence value '%s'"
                             % self.occurrence)
102
        elif self.autobind_name is not None:
103 104 105 106 107 108 109 110 111 112 113 114 115 116
            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 已提交
117
            return aligned(string, self.get_occurrence_comment() + "\n")
118 119 120 121


    def generate_return(self, offset = 0, end_of_line = ";"):
        if self.occurrence == OCCURRENCE__IGNORED:
122 123
            raise ValueError("invalid function parameter occurrence value '%s'"
                             % self.occurrence)
124 125 126
        else:
            string = "       "
            string += " " * offset
127 128
            string += "%s%s)%s" \
                      % (self.get_type_string(True), self.name, end_of_line)
129

M
Matthias Bolte 已提交
130
            return aligned(string, self.get_occurrence_comment() + "\n")
131 132 133 134 135 136 137 138 139 140 141 142 143


    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]:
144 145
            return "    ESX_VI__METHOD__PARAMETER__SERIALIZE_LIST(%s, %s)\n" \
                   % (self.type, self.name)
146
        elif self.type == "String":
147 148
            return "    ESX_VI__METHOD__PARAMETER__SERIALIZE_VALUE(String, %s)\n" \
                   % self.name
149
        else:
150 151
            return "    ESX_VI__METHOD__PARAMETER__SERIALIZE(%s, %s)\n" \
                   % (self.type, self.name)
152 153


154
    def get_type_string(self, as_return_value=False):
155 156
        string = ""

157 158 159
        if self.type == "String" and \
           self.occurrence not in [OCCURRENCE__REQUIRED_LIST,
                                   OCCURRENCE__OPTIONAL_LIST]:
160 161 162 163
            if as_return_value:
                string += "char *"
            else:
                string += "const char *"
164
        elif self.is_enum():
165
            string += "esxVI_%s " % self.type
166
        else:
167 168 169 170 171 172
            string += "esxVI_%s *" % self.type

        if as_return_value:
            string += "*"

        return string
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196


    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:
197
            if parameter.autobind_name is None:
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
                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:
214
                header += self.parameters[-1].generate_parameter(is_last=True)
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
            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:
231 232
            autobind_names.add(self.autobind_parameter.autobind_name)
            source += " %s,\n" % self.autobind_parameter.autobind_name
233 234 235 236 237 238 239 240 241
        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]:
242 243
                source += parameter.generate_parameter(is_header=False,
                                                       offset=9)
244 245

            if self.returns is None:
246 247 248
                source += self.parameters[-1].generate_parameter(is_last=True,
                                                                 is_header=False,
                                                                 offset=9)
249
            else:
250 251 252 253
                source += self.parameters[-1].generate_parameter(is_header=False,
                                                                 offset=9)
                source += self.returns.generate_return(offset=9,
                                                       end_of_line=",")
254 255 256 257
        else:
            source += "),\n"

        if self.returns is None:
258 259
            source += "               void, /* nothing */, None,\n"
        elif self.returns.type == "String":
260 261
            source += "               String, Value, %s,\n" \
                      % self.returns.get_occurrence_short_enum()
262
        else:
263 264 265
            source += "               %s, /* nothing */, %s,\n" \
                      % (self.returns.type,
                         self.returns.get_occurrence_short_enum())
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

        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



290
class Property(Member):
291
    def __init__(self, type, name, occurrence):
292
        Member.__init__(self, type, occurrence)
293

294
        self.name = name
295 296


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

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

305 306 307

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

320

321 322 323 324 325 326
    def generate_validate_code(self, managed=False):
        if managed:
            macro = "ESX_VI__TEMPLATE__PROPERTY__MANAGED_REQUIRE"
        else:
            macro = "ESX_VI__TEMPLATE__PROPERTY__REQUIRE"

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

335

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

352

353
    def generate_serialize_code(self):
354
        if self.occurrence == OCCURRENCE__IGNORED:
355
            return "    /* FIXME: %s is currently ignored */\n" % self.name
356 357
        elif self.occurrence in [OCCURRENCE__REQUIRED_LIST,
                                 OCCURRENCE__OPTIONAL_LIST]:
358 359
            return "    ESX_VI__TEMPLATE__PROPERTY__SERIALIZE_LIST(%s, %s)\n" \
                   % (self.type, self.name)
360
        elif self.type == "String":
361 362
            return "    ESX_VI__TEMPLATE__PROPERTY__SERIALIZE_VALUE(String, %s)\n" \
                   % self.name
363
        else:
364 365
            return "    ESX_VI__TEMPLATE__PROPERTY__SERIALIZE(%s, %s)\n" \
                   % (self.type, self.name)
366

367

368
    def generate_deserialize_code(self):
369
        if self.occurrence == OCCURRENCE__IGNORED:
370 371
            return "    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_IGNORE(%s) /* FIXME */\n" \
                   % self.name
372 373
        elif self.occurrence in [OCCURRENCE__REQUIRED_LIST,
                                 OCCURRENCE__OPTIONAL_LIST]:
374 375
            return "    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_LIST(%s, %s)\n" \
                   % (self.type, self.name)
376
        elif self.type == "String":
377 378
            return "    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_VALUE(String, %s)\n" \
                   % self.name
379
        else:
380 381
            return "    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE(%s, %s)\n" \
                   % (self.type, self.name)
382

383

384 385
    def generate_lookup_code(self):
        if self.occurrence == OCCURRENCE__IGNORED:
386 387
            return "    ESX_VI__TEMPLATE__PROPERTY__CAST_FROM_ANY_TYPE_IGNORE(%s) /* FIXME */\n" \
                   % self.name
388 389
        elif self.occurrence in [OCCURRENCE__REQUIRED_LIST,
                                 OCCURRENCE__OPTIONAL_LIST]:
390 391
            return "    ESX_VI__TEMPLATE__PROPERTY__CAST_LIST_FROM_ANY_TYPE(%s, %s)\n" \
                   % (self.type, self.name)
392
        elif self.type == "String":
393 394
            return "    ESX_VI__TEMPLATE__PROPERTY__CAST_VALUE_FROM_ANY_TYPE(String, %s)\n" \
                   % self.name
395
        else:
396 397
            return "    ESX_VI__TEMPLATE__PROPERTY__CAST_FROM_ANY_TYPE(%s, %s)\n" \
                   % (self.type, self.name)
398 399


400 401
    def get_type_string(self):
        if self.type == "String" and \
402 403
           self.occurrence not in [OCCURRENCE__REQUIRED_LIST,
                                   OCCURRENCE__OPTIONAL_LIST]:
404 405 406 407 408 409 410 411
            return "char *"
        elif self.is_enum():
            return "esxVI_%s " % self.type
        else:
            return "esxVI_%s *" % self.type



412
class Type:
M
Matthias Bolte 已提交
413 414 415 416 417 418
    def __init__(self, kind, name):
        self.kind = kind
        self.name = name


    def generate_typedef(self):
419 420
        return "typedef %s _esxVI_%s esxVI_%s;\n" \
               % (self.kind, self.name, self.name)
M
Matthias Bolte 已提交
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442


    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



443
class Object(Type):
444 445 446 447 448 449 450
    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)

451 452

    def __init__(self, name, extends, properties, features=0, extended_by=None):
453
        Type.__init__(self, "struct", name)
454
        self.extends = extends
455
        self.features = features
456 457
        self.properties = properties
        self.extended_by = extended_by
458
        self.candidate_for_dynamic_cast = False
459

460
        if self.extended_by is not None:
M
Matthias Bolte 已提交
461
            self.extended_by.sort()
462

463

M
Matthias Bolte 已提交
464
    def generate_struct_members(self, add_banner=False, struct_gap=False):
465 466
        members = ""

M
Matthias Bolte 已提交
467
        if struct_gap:
468 469 470
            members += "\n"

        if self.extends is not None:
471 472 473
            members += objects_by_name[self.extends] \
                       .generate_struct_members(add_banner=True,
                                                struct_gap=False) + "\n"
474 475 476 477 478 479 480 481 482 483 484 485

        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

486

M
Matthias Bolte 已提交
487
    def generate_free_code(self, add_banner=False):
488 489 490
        source = ""

        if self.extends is not None:
491 492
            source += objects_by_name[self.extends] \
                      .generate_free_code(add_banner=True) + "\n"
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511

        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

512

M
Matthias Bolte 已提交
513
    def generate_validate_code(self, add_banner=False):
514 515 516
        source = ""

        if self.extends is not None:
517 518
            source += objects_by_name[self.extends] \
                      .generate_validate_code(add_banner=True) + "\n"
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537

        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

538

539
    def generate_dynamic_cast_code(self, is_first=True):
540 541 542
        source = ""

        if self.extended_by is not None:
543 544 545
            if not is_first:
                source += "\n"

546 547 548
            source += "    /* %s */\n" % self.name

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

            for extended_by in self.extended_by:
553 554
                source += objects_by_name[extended_by] \
                          .generate_dynamic_cast_code(False)
555 556 557

        return source

558

559 560 561 562
    def generate_deep_copy_code(self, add_banner = False):
        source = ""

        if self.extends is not None:
563 564
            source += objects_by_name[self.extends] \
                      .generate_deep_copy_code(add_banner=True) + "\n"
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583

        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

584

585
    def generate_serialize_code(self, add_banner=False):
586 587 588
        source = ""

        if self.extends is not None:
589 590
            source += objects_by_name[self.extends] \
                      .generate_serialize_code(add_banner=True) + "\n"
591 592 593 594 595 596 597 598 599 600 601 602

        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

603

604
    def generate_deserialize_code(self, add_banner=False):
605 606 607
        source = ""

        if self.extends is not None:
608 609
            source += objects_by_name[self.extends] \
                      .generate_deserialize_code(add_banner=True) + "\n"
610 611 612 613 614 615 616 617 618 619 620 621

        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

622

623
    def generate_header(self):
624
        header = separator
625
        header += " * VI Object: %s\n" % self.name
626 627

        if self.extends is not None:
628
            header += " *            extends %s\n" % self.extends
629 630 631 632 633 634

        first = True

        if self.extended_by is not None:
            for extended_by in self.extended_by:
                if first:
635
                    header += " *            extended by %s\n" % extended_by
636 637
                    first = False
                else:
638
                    header += " *                        %s\n" % extended_by
639 640 641 642 643 644 645

        header += " */\n\n"

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

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

M
Matthias Bolte 已提交
652 653
        header += aligned("    esxVI_Type _type; ", "/* required */\n")
        header += self.generate_struct_members(struct_gap=True)
654 655 656
        header += "};\n\n"

        # functions
657 658 659 660 661 662
        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)
663 664 665

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

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

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

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

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

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

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

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

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

            if self.features & Object.FEATURE__LIST:
711 712 713
                header += ("int esxVI_%s_DeserializeList(xmlNodePtr node, "
                                                        "esxVI_%s **list);\n") \
                          % (self.name, self.name)
714 715 716 717 718

        header += "\n\n\n"

        return header

719

720
    def generate_source(self):
721
        source = separator
722
        source += " * VI Object: %s\n" % self.name
723 724

        if self.extends is not None:
725
            source += " *            extends %s\n" % self.extends
726 727 728 729 730 731

        first = True

        if self.extended_by is not None:
            for extended_by in self.extended_by:
                if first:
732
                    source += " *            extended by %s\n" % extended_by
733 734
                    first = False
                else:
735
                    source += " *                        %s\n" % extended_by
736 737 738 739 740 741 742 743 744 745 746 747 748 749

        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:
750
                if self.extends is not None:
751 752 753 754
                    # 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)
755 756
                    source += "    esxVI_%s_Free(&next);\n" % self.extends
                    source += "    item->_next = (esxVI_%s *)next;\n\n" % self.name
757 758
                else:
                    source += "    esxVI_%s_Free(&item->_next);\n\n" % self.name
759 760 761 762 763 764 765 766 767 768

            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:
769 770
                source += "    ESX_VI__TEMPLATE__DISPATCH__FREE(%s)\n" \
                          % extended_by
771 772 773 774 775

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

            if self.features & Object.FEATURE__LIST:
776
                if self.extends is not None:
777 778 779 780
                    # avoid "dereferencing type-punned pointer will brea
                    # strict-aliasing rules" warnings
                    source += "    esxVI_%s *next = (esxVI_%s *)item->_next;\n\n" \
                              % (self.extends, self.extends)
781 782
                    source += "    esxVI_%s_Free(&next);\n" % self.extends
                    source += "    item->_next = (esxVI_%s *)next;\n\n" % self.name
783 784
                else:
                    source += "    esxVI_%s_Free(&item->_next);\n\n" % self.name
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 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829

            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
830 831
                    source += "ESX_VI__TEMPLATE__LIST__DEEP_COPY(%s)\n\n" \
                              % self.name
832 833 834
        else:
            if self.features & Object.FEATURE__DEEP_COPY:
                source += "/* esxVI_%s_DeepCopy */\n" % self.name
835
                source += "ESX_VI__TEMPLATE__DYNAMIC_DEEP_COPY(%s,\n" % self.name
836 837 838
                source += "{\n"

                for extended_by in self.extended_by:
839 840
                    source += "    ESX_VI__TEMPLATE__DISPATCH__DEEP_COPY(%s)\n" \
                              % extended_by
841 842 843 844 845 846 847 848 849 850

                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
851 852
                    source += "ESX_VI__TEMPLATE__LIST__DEEP_COPY(%s)\n\n" \
                              % self.name
853 854 855 856

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

            if self.extended_by is None:
859 860
                source += "ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE(%s)\n\n" \
                          % self.name
861
            else:
862 863
                source += "ESX_VI__TEMPLATE__DYNAMIC_CAST_FROM_ANY_TYPE(%s,\n" \
                          % self.name
864 865 866
                source += "{\n"

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

                source += "})\n\n"
871 872 873

            if self.features & Object.FEATURE__LIST:
                source += "/* esxVI_%s_CastListFromAnyType */\n" % self.name
874 875
                source += "ESX_VI__TEMPLATE__LIST__CAST_FROM_ANY_TYPE(%s)\n\n" \
                          % self.name
876 877 878 879 880 881 882 883 884 885 886 887 888 889

        # 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
890 891
                    source += "ESX_VI__TEMPLATE__LIST__SERIALIZE(%s)\n\n" \
                              % self.name
892 893 894 895 896 897 898
        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:
899 900
                    source += "    ESX_VI__TEMPLATE__DISPATCH__SERIALIZE(%s)\n" \
                              % extended_by
901 902 903 904 905 906 907 908 909 910

                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
911 912
                    source += "ESX_VI__TEMPLATE__LIST__SERIALIZE(%s)\n\n" \
                              % self.name
913

914
        # deserialize
915 916 917 918 919
        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"
920

921
                source += self.generate_deserialize_code()
922

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

925 926
                if self.features & Object.FEATURE__LIST:
                    source += "/* esxVI_%s_DeserializeList */\n" % self.name
927 928
                    source += "ESX_VI__TEMPLATE__LIST__DESERIALIZE(%s)\n\n" \
                              % self.name
929 930 931
        else:
            if self.features & Object.FEATURE__DESERIALIZE:
                source += "/* esxVI_%s_Deserialize */\n" % self.name
932 933
                source += "ESX_VI__TEMPLATE__DYNAMIC_DESERIALIZE(%s,\n" \
                          % self.name
934 935 936
                source += "{\n"

                for extended_by in self.extended_by:
937 938
                    source += "    ESX_VI__TEMPLATE__DISPATCH__DESERIALIZE(%s)\n" \
                              % extended_by
939 940 941 942 943 944 945 946 947 948

                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
949 950
                    source += "ESX_VI__TEMPLATE__LIST__DESERIALIZE(%s)\n\n" \
                              % self.name
951 952 953 954 955 956 957

        source += "\n\n"

        return source



958
class ManagedObject(Type):
959 960 961 962
    FEATURE__LIST = (1 << 2)


    def __init__(self, name, extends, properties, features=0, extended_by=None):
963
        Type.__init__(self, "struct", name)
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
        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:
980 981
            members += managed_objects_by_name[self.extends] \
                       .generate_struct_members(add_banner=True) + "\n"
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996

        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 = ""
997

998
        if self.extends is not None:
999 1000
            source += managed_objects_by_name[self.extends] \
                      .generate_free_code(add_banner=True) + "\n"
1001

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

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

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

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

1018
        return source
1019

1020

1021 1022 1023 1024
    def generate_validate_code(self, add_banner=False):
        source = ""

        if self.extends is not None:
1025 1026
            source += managed_objects_by_name[self.extends] \
                      .generate_validate_code(add_banner=True) + "\n"
1027 1028 1029 1030 1031 1032 1033 1034

        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 = ""
1035

1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
            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:
1051 1052
            source += managed_objects_by_name[self.extends] \
                      .generate_lookup_code1(add_banner=True) + "\n"
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076

        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:
1077 1078
            source += managed_objects_by_name[self.extends] \
                      .generate_lookup_code2(add_banner=True) + "\n"
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099

        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):
1100
        comment = separator
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
        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:
1111 1112
                    comment += " *                    extended by %s\n" \
                               % extended_by
1113 1114
                    first = False
                else:
1115 1116
                    comment += " *                                %s\n" \
                               % extended_by
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129

        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:
1130 1131
            header += aligned("    esxVI_%s *_next; " % self.name,
                              "/* optional */\n")
1132
        else:
1133 1134
            header += aligned("    esxVI_%s *_unused; " % self.name,
                              "/* optional */\n")
1135 1136

        header += aligned("    esxVI_Type _type; ", "/* required */\n")
1137 1138
        header += aligned("    esxVI_ManagedObjectReference *_reference; ",
                          "/* required */\n")
1139 1140 1141 1142 1143 1144 1145 1146
        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)
1147 1148 1149
        header += ("int esxVI_%s_Validate(esxVI_%s *item, "
                                         "esxVI_String *selectedPropertyNameList);\n") \
                  % (self.name, self.name)
1150 1151

        if self.features & Object.FEATURE__LIST:
1152 1153
            header += "int esxVI_%s_AppendToList(esxVI_%s **list, esxVI_%s *item);\n" \
                      % (self.name, self.name, self.name)
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163

        header += "\n\n\n"

        return header


    def generate_helper_header(self):
        header = ""

        # functions
1164 1165 1166 1167 1168 1169 1170
        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)
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191

        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:
1192 1193 1194 1195
                    # 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)
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
                    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:
1219 1220 1221 1222
                    # 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)
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
                    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
1273

1274

1275

1276
class Enum(Type):
M
Matthias Bolte 已提交
1277 1278 1279 1280 1281 1282
    FEATURE__ANY_TYPE = (1 << 1)
    FEATURE__SERIALIZE = (1 << 2)
    FEATURE__DESERIALIZE = (1 << 3)


    def __init__(self, name, values, features=0):
1283
        Type.__init__(self, "enum", name)
M
Matthias Bolte 已提交
1284
        self.values = values
1285
        self.features = features
1286

1287

1288
    def generate_header(self):
1289
        header = separator
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
        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:
1304 1305 1306
            header += ("int esxVI_%s_CastFromAnyType(esxVI_AnyType *anyType, "
                                                    "esxVI_%s *item);\n") \
                      % (self.name, self.name)
1307 1308

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

        if self.features & Enum.FEATURE__DESERIALIZE:
1314 1315 1316
            header += ("int esxVI_%s_Deserialize(xmlNodePtr node, "
                                                "esxVI_%s *item);\n") \
                      % (self.name, self.name)
1317 1318 1319 1320 1321

        header += "\n\n\n"

        return header

1322

1323
    def generate_source(self):
1324
        source = separator
1325 1326 1327
        source += " * VI Enum: %s\n" % self.name
        source += " */\n\n"

1328 1329
        source += "static const esxVI_Enumeration _esxVI_%s_Enumeration = {\n" \
                  % self.name
1330 1331 1332
        source += "    esxVI_Type_%s, {\n" % self.name

        for value in self.values:
1333 1334
            source += "        { \"%s\", esxVI_%s_%s },\n" \
                      % (value, self.name, capitalize_first(value))
1335 1336 1337 1338 1339 1340 1341 1342

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

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

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

        if self.features & Enum.FEATURE__DESERIALIZE:
            source += "/* esxVI_%s_Deserialize */\n" % self.name
1353 1354
            source += "ESX_VI__TEMPLATE__ENUMERATION__DESERIALIZE(%s)\n\n" \
                      % self.name
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373

        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):
1374
    # expected format: [managed] object <name> [extends <name>]
1375
    header_items = block[0][1].split()
1376 1377 1378 1379 1380
    managed = False

    if header_items[0] == "managed":
        managed = True
        del header_items[0]
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404

    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])

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

M
Matthias Bolte 已提交
1408 1409
        properties.append(Property(type=items[0], name=items[1],
                                   occurrence=items[2]))
1410

1411 1412 1413 1414
    if managed:
        return ManagedObject(name=name, extends=extends, properties=properties)
    else:
        return Object(name=name, extends=extends, properties=properties)
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434



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 已提交
1435
    return Enum(name=name, values=values)
1436 1437 1438



1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
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 已提交
1455 1456
            returns = Parameter(type=header_items[3], name="output",
                                occurrence=header_items[4])
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469

    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 已提交
1470 1471
        parameters.append(Parameter(type=items[0], name=items[1],
                                    occurrence=items[2]))
1472

M
Matthias Bolte 已提交
1473
    return Method(name=name, parameters=parameters, returns=returns)
1474 1475 1476



1477 1478 1479 1480
def is_known_type(type):
    return type in predefined_objects or \
           type in predefined_enums or \
           type in objects_by_name or \
1481
           type in managed_objects_by_name or \
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
           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",
A
Ata E Husain Bohra 已提交
1499
                      "Byte",
1500 1501 1502 1503
                      "Int",
                      "Long",
                      "String",
                      "DateTime",
1504
                      "MethodFault",
1505 1506 1507 1508 1509 1510
                      "ManagedObjectReference"]

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

1511
additional_object_features = { "AutoStartDefaults"          : Object.FEATURE__ANY_TYPE,
1512
                               "AutoStartPowerInfo"         : Object.FEATURE__ANY_TYPE,
1513 1514 1515 1516 1517
                               "DatastoreHostMount"         : Object.FEATURE__DEEP_COPY |
                                                              Object.FEATURE__LIST |
                                                              Object.FEATURE__ANY_TYPE,
                               "DatastoreInfo"              : Object.FEATURE__ANY_TYPE |
                                                              Object.FEATURE__DYNAMIC_CAST,
1518
                               "HostConfigManager"          : Object.FEATURE__ANY_TYPE,
1519 1520 1521 1522
                               "HostCpuIdInfo"              : Object.FEATURE__LIST |
                                                              Object.FEATURE__ANY_TYPE,
                               "HostDatastoreBrowserSearchResults" : Object.FEATURE__LIST |
                                                              Object.FEATURE__ANY_TYPE,
M
Matthias Bolte 已提交
1523 1524 1525 1526 1527
                               "HostPortGroup"              : Object.FEATURE__LIST |
                                                              Object.FEATURE__ANY_TYPE,
                               "HostVirtualSwitch"          : Object.FEATURE__DEEP_COPY |
                                                              Object.FEATURE__LIST |
                                                              Object.FEATURE__ANY_TYPE,
1528
                               "ManagedObjectReference"     : Object.FEATURE__ANY_TYPE,
1529
                               "ObjectContent"              : Object.FEATURE__DEEP_COPY,
M
Matthias Bolte 已提交
1530 1531 1532
                               "PhysicalNic"                : Object.FEATURE__DEEP_COPY |
                                                              Object.FEATURE__LIST |
                                                              Object.FEATURE__ANY_TYPE,
1533
                               "ResourcePoolResourceUsage"  : Object.FEATURE__ANY_TYPE,
1534
                               "ServiceContent"             : Object.FEATURE__DESERIALIZE,
1535
                               "SharesInfo"                 : Object.FEATURE__ANY_TYPE,
1536 1537
                               "TaskInfo"                   : Object.FEATURE__LIST |
                                                              Object.FEATURE__ANY_TYPE,
1538
                               "UserSession"                : Object.FEATURE__ANY_TYPE,
1539
                               "VirtualMachineQuestionInfo" : Object.FEATURE__ANY_TYPE,
1540 1541
                               "VirtualMachineSnapshotTree" : Object.FEATURE__DEEP_COPY |
                                                              Object.FEATURE__ANY_TYPE,
1542
                               "VmEventArgument"            : Object.FEATURE__DESERIALIZE }
1543

1544
removed_object_features = {}
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556



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()



1557 1558 1559 1560 1561 1562 1563 1564
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"))
1565
methods_macro = open_and_print(os.path.join(output_dirname, "esx_vi_methods.generated.macro"))
1566 1567
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"))
1568 1569 1570 1571 1572



number = 0
objects_by_name = {}
1573
managed_objects_by_name = {}
1574
enums_by_name = {}
1575
methods_by_name = {}
1576 1577 1578 1579
block = None



1580
# parse input file
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
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

1592 1593
    if line.startswith("object") or line.startswith("managed object") or \
       line.startswith("enum") or line.startswith("method"):
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
        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
1604 1605 1606
            elif block[0][1].startswith("managed object"):
                obj = parse_object(block)
                managed_objects_by_name[obj.name] = obj
1607
            elif block[0][1].startswith("enum"):
1608 1609
                enum = parse_enum(block)
                enums_by_name[enum.name] = enum
1610 1611 1612
            else:
                method = parse_method(block)
                methods_by_name[method.name] = method
1613 1614 1615 1616 1617 1618 1619

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



1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
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
1630
            objects_by_name[parameter.type].candidate_for_dynamic_cast = True
1631

1632 1633 1634 1635 1636 1637 1638 1639 1640
        # 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

1641 1642 1643 1644 1645 1646
    # 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
1647
            objects_by_name[method.returns.type].candidate_for_dynamic_cast = True
1648

1649 1650 1651 1652 1653 1654 1655 1656 1657
        # 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

1658 1659


1660 1661 1662 1663 1664
for enum in enums_by_name.values():
    # apply additional features
    if enum.name in additional_enum_features:
        enum.features |= additional_enum_features[enum.name]

1665 1666 1667
        if additional_enum_features[enum.name] & Enum.FEATURE__ANY_TYPE:
            enum.features |= Enum.FEATURE__DESERIALIZE

1668 1669 1670 1671


for obj in objects_by_name.values():
    for property in obj.properties:
1672
        if property.occurrence != OCCURRENCE__IGNORED and \
1673
           not is_known_type(property.type):
1674 1675
            report_error("object '%s' contains unknown property type '%s'"
                         % (obj.name, property.type))
1676 1677 1678

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

    for property in obj.properties:
1683 1684 1685
        if not property.is_type_generated():
            continue

1686 1687 1688 1689 1690 1691
        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
1692 1693 1694 1695 1696 1697 1698
        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
1699 1700 1701 1702 1703

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

1704 1705 1706
        if additional_object_features[obj.name] & Object.FEATURE__ANY_TYPE:
            obj.features |= Object.FEATURE__DESERIALIZE

1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
    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)
1718
            extended_obj.extended_by.sort()
1719 1720 1721



1722 1723 1724 1725 1726 1727 1728 1729 1730
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



1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 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 1799 1800 1801 1802 1803 1804
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)



1805 1806 1807 1808
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):
1809 1810
            report_error("object '%s' contains unknown property type '%s'"
                         % (obj.name, property.type))
1811 1812 1813

    if obj.extends is not None:
        if not is_known_type(obj.extends):
1814 1815
            report_error("object '%s' extends unknown object '%s'"
                         % (obj.name, obj.extends))
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828

    # 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()



1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841
notice = "/* Generated by esx_vi_generator.py */\n\n\n\n"

types_typedef.write(notice)
types_typeenum.write(notice)
types_typetostring.write(notice)
types_typefromstring.write(notice)
types_header.write(notice)
types_source.write(notice)
methods_header.write(notice)
methods_source.write(notice)
methods_macro.write(notice)
helpers_header.write(notice)
helpers_source.write(notice)
1842 1843


1844

1845
# output enums
1846
types_typedef.write(separator +
1847 1848
                    " * VI Enums\n" +
                    " */\n\n")
1849 1850 1851 1852 1853

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

for name in names:
1854 1855 1856 1857 1858 1859
    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())
1860 1861 1862



1863 1864
# output objects
types_typedef.write("\n\n\n" +
1865
                    separator +
1866
                    " * VI Objects\n" +
1867 1868 1869 1870
                    " */\n\n")
types_typeenum.write("\n")
types_typetostring.write("\n")
types_typefromstring.write("\n")
1871

1872 1873
names = objects_by_name.keys()
names.sort()
1874

1875 1876 1877 1878 1879 1880 1881
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())
1882 1883 1884



1885 1886
# output managed objects
types_typedef.write("\n\n\n" +
1887
                    separator +
1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906
                    " * 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())



1907 1908 1909
# output methods
names = methods_by_name.keys()
names.sort()
1910 1911

for name in names:
1912 1913
    methods_header.write(methods_by_name[name].generate_header())
    methods_source.write(methods_by_name[name].generate_source())
1914

1915 1916
names = list(autobind_names)
names.sort()
1917

1918
for name in names:
1919 1920 1921 1922 1923 1924
    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)

1925 1926 1927 1928 1929 1930 1931 1932 1933


# 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())