• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3# Copyright (c) 2015-2016 The Khronos Group Inc.
4# Copyright (c) 2015-2016 Valve Corporation
5# Copyright (c) 2015-2016 LunarG, Inc.
6# Copyright (c) 2015-2016 Google Inc.
7#
8# Licensed under the Apache License, Version 2.0 (the "License");
9# you may not use this file except in compliance with the License.
10# You may obtain a copy of the License at
11#
12#     http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS,
16# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17# See the License for the specific language governing permissions and
18# limitations under the License.
19#
20# Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
21# Author: Tobin Ehlis <tobin@lunarg.com>
22
23import argparse
24import os
25import sys
26import re
27import vulkan
28from source_line_info import sourcelineinfo
29
30# vk_helper.py overview
31# This script generates code based on vulkan input header
32#  It generate wrappers functions that can be used to display
33#  structs in a human-readable txt format, as well as utility functions
34#  to print enum values as strings
35
36def handle_args():
37    parser = argparse.ArgumentParser(description='Perform analysis of vogl trace.')
38    parser.add_argument('input_file', help='The input header file from which code will be generated.')
39    parser.add_argument('--rel_out_dir', required=False, default='vktrace_gen', help='Path relative to exec path to write output files. Will be created if needed.')
40    parser.add_argument('--abs_out_dir', required=False, default=None, help='Absolute path to write output files. Will be created if needed.')
41    parser.add_argument('--gen_enum_string_helper', required=False, action='store_true', default=False, help='Enable generation of helper header file to print string versions of enums.')
42    parser.add_argument('--gen_struct_wrappers', required=False, action='store_true', default=False, help='Enable generation of struct wrapper classes.')
43    parser.add_argument('--gen_struct_sizes', required=False, action='store_true', default=False, help='Enable generation of struct sizes.')
44    parser.add_argument('--gen_cmake', required=False, action='store_true', default=False, help='Enable generation of cmake file for generated code.')
45    parser.add_argument('--gen_graphviz', required=False, action='store_true', default=False, help='Enable generation of graphviz dot file.')
46    #parser.add_argument('--test', action='store_true', default=False, help='Run simple test.')
47    return parser.parse_args()
48
49# TODO : Ideally these data structs would be opaque to user and could be wrapped
50#   in their own class(es) to make them friendly for data look-up
51# Dicts for Data storage
52# enum_val_dict[value_name] = dict keys are the string enum value names for all enums
53#    |-------->['type'] = the type of enum class into which the value falls
54#    |-------->['val'] = the value assigned to this particular value_name
55#    '-------->['unique'] = bool designating if this enum 'val' is unique within this enum 'type'
56enum_val_dict = {}
57# enum_type_dict['type'] = the type or class of of enum
58#  '----->['val_name1', 'val_name2'...] = each type references a list of val_names within this type
59enum_type_dict = {}
60# struct_dict['struct_basename'] = the base (non-typedef'd) name of the struct
61#  |->[<member_num>] = members are stored via their integer placement in the struct
62#  |    |->['name'] = string name of this struct member
63# ...   |->['full_type'] = complete type qualifier for this member
64#       |->['type'] = base type for this member
65#       |->['ptr'] = bool indicating if this member is ptr
66#       |->['const'] = bool indicating if this member is const
67#       |->['struct'] = bool indicating if this member is a struct type
68#       |->['array'] = bool indicating if this member is an array
69#       |->['dyn_array'] = bool indicating if member is a dynamically sized array
70#       '->['array_size'] = For dyn_array, member name used to size array, else int size for static array
71struct_dict = {}
72struct_order_list = [] # struct names in order they're declared
73# Store struct names that require #ifdef guards
74#  dict stores struct and enum definitions that are guarded by ifdef as the key
75#  and the txt of the ifdef is the value
76ifdef_dict = {}
77# typedef_fwd_dict stores mapping from orig_type_name -> new_type_name
78typedef_fwd_dict = {}
79# typedef_rev_dict stores mapping from new_type_name -> orig_type_name
80typedef_rev_dict = {} # store new_name -> orig_name mapping
81# types_dict['id_name'] = identifier name will map to it's type
82#              '---->'type' = currently either 'struct' or 'enum'
83types_dict = {}   # store orig_name -> type mapping
84
85
86# Class that parses header file and generates data structures that can
87#  Then be used for other tasks
88class HeaderFileParser:
89    def __init__(self, header_file=None):
90        self.header_file = header_file
91        # store header data in various formats, see above for more info
92        self.enum_val_dict = {}
93        self.enum_type_dict = {}
94        self.struct_dict = {}
95        self.typedef_fwd_dict = {}
96        self.typedef_rev_dict = {}
97        self.types_dict = {}
98        self.last_struct_count_name = ''
99
100    def setHeaderFile(self, header_file):
101        self.header_file = header_file
102
103    def get_enum_val_dict(self):
104        return self.enum_val_dict
105
106    def get_enum_type_dict(self):
107        return self.enum_type_dict
108
109    def get_struct_dict(self):
110        return self.struct_dict
111
112    def get_typedef_fwd_dict(self):
113        return self.typedef_fwd_dict
114
115    def get_typedef_rev_dict(self):
116        return self.typedef_rev_dict
117
118    def get_types_dict(self):
119        return self.types_dict
120
121    # Parse header file into data structures
122    def parse(self):
123        # parse through the file, identifying different sections
124        parse_enum = False
125        parse_struct = False
126        member_num = 0
127        # TODO : Comment parsing is very fragile but handles 2 known files
128        block_comment = False
129        prev_count_name = ''
130        ifdef_txt = ''
131        ifdef_active = 0
132        exclude_struct_list = ['VkPlatformHandleXcbKHR', 'VkPlatformHandleX11KHR']
133        with open(self.header_file) as f:
134            for line in f:
135                if True in [ifd_txt in line for ifd_txt in ['#ifdef ', '#ifndef ']]:
136                    ifdef_txt = line.split()[1]
137                    ifdef_active = ifdef_active + 1
138                    continue
139                if ifdef_active != 0 and '#endif' in line:
140                    ifdef_active = ifdef_active - 1
141                if block_comment:
142                    if '*/' in line:
143                        block_comment = False
144                    continue
145                if '/*' in line:
146                    if '*/' in line: # single line block comment
147                        continue
148                    block_comment = True
149                elif 0 == len(line.split()):
150                    #print("Skipping empty line")
151                    continue
152                elif line.split()[0].strip().startswith("//"):
153                    #print("Skipping commented line %s" % line)
154                    continue
155                elif 'typedef enum' in line:
156                    (ty_txt, en_txt, base_type) = line.strip().split(None, 2)
157                    #print("Found ENUM type %s" % base_type)
158                    if '{' == base_type:
159                        base_type = 'tmp_enum'
160                    parse_enum = True
161                    default_enum_val = 0
162                    self.types_dict[base_type] = 'enum'
163                elif 'typedef struct' in line or 'typedef union' in line:
164                    if True in [ex_type in line for ex_type in exclude_struct_list]:
165                        continue
166
167                    (ty_txt, st_txt, base_type) = line.strip().split(None, 2)
168                    if ' ' in base_type:
169                        (ignored, base_type) = base_type.strip().split(None, 1)
170
171                    #print("Found STRUCT type: %s" % base_type)
172                    # Note:  This really needs to be updated to handle one line struct definition, like
173                    #        typedef struct obj##_T { uint64_t handle; } obj;
174                    if ('{' == base_type or not (' ' in base_type)):
175                        base_type = 'tmp_struct'
176                        parse_struct = True
177                        self.types_dict[base_type] = 'struct'
178#                elif 'typedef union' in line:
179#                    (ty_txt, st_txt, base_type) = line.strip().split(None, 2)
180#                    print("Found UNION type: %s" % base_type)
181#                    parse_struct = True
182#                    self.types_dict[base_type] = 'struct'
183                elif '}' in line and (parse_enum or parse_struct):
184                    if len(line.split()) > 1: # deals with embedded union in one struct
185                        parse_enum = False
186                        parse_struct = False
187                        self.last_struct_count_name = ''
188                        member_num = 0
189                        (cur_char, targ_type) = line.strip().split(None, 1)
190                        if 'tmp_struct' == base_type:
191                            base_type = targ_type.strip(';')
192                            if True in [ex_type in base_type for ex_type in exclude_struct_list]:
193                                del self.struct_dict['tmp_struct']
194                                continue
195                            #print("Found Actual Struct type %s" % base_type)
196                            self.struct_dict[base_type] = self.struct_dict['tmp_struct']
197                            self.struct_dict.pop('tmp_struct', 0)
198                            struct_order_list.append(base_type)
199                            self.types_dict[base_type] = 'struct'
200                            self.types_dict.pop('tmp_struct', 0)
201                        elif 'tmp_enum' == base_type:
202                            base_type = targ_type.strip(';')
203                            #print("Found Actual ENUM type %s" % base_type)
204                            for n in self.enum_val_dict:
205                                if 'tmp_enum' == self.enum_val_dict[n]['type']:
206                                    self.enum_val_dict[n]['type'] = base_type
207#                            self.enum_val_dict[base_type] = self.enum_val_dict['tmp_enum']
208#                            self.enum_val_dict.pop('tmp_enum', 0)
209                            self.enum_type_dict[base_type] = self.enum_type_dict['tmp_enum']
210                            self.enum_type_dict.pop('tmp_enum', 0)
211                            self.types_dict[base_type] = 'enum'
212                            self.types_dict.pop('tmp_enum', 0)
213                        if ifdef_active:
214                            ifdef_dict[base_type] = ifdef_txt
215                        self.typedef_fwd_dict[base_type] = targ_type.strip(';')
216                        self.typedef_rev_dict[targ_type.strip(';')] = base_type
217                        #print("fwd_dict: %s = %s" % (base_type, targ_type))
218                elif parse_enum:
219                    #if 'VK_MAX_ENUM' not in line and '{' not in line:
220                    if True not in [ens in line for ens in ['{', '_MAX_ENUM', '_BEGIN_RANGE', '_END_RANGE', '_NUM = ', '_ENUM_RANGE']]:
221                        self._add_enum(line, base_type, default_enum_val)
222                        default_enum_val += 1
223                elif parse_struct:
224                    if ';' in line:
225                        self._add_struct(line, base_type, member_num)
226                        member_num = member_num + 1
227
228    # populate enum dicts based on enum lines
229    def _add_enum(self, line_txt, enum_type, def_enum_val):
230        #print("Parsing enum line %s" % line_txt)
231        if '=' in line_txt:
232            (enum_name, eq_char, enum_val) = line_txt.split(None, 2)
233        else:
234            enum_name = line_txt.split(',')[0]
235            enum_val = str(def_enum_val)
236        self.enum_val_dict[enum_name] = {}
237        self.enum_val_dict[enum_name]['type'] = enum_type
238        # strip comma and comment, then extra split in case of no comma w/ comments
239        enum_val = enum_val.strip().split(',', 1)[0]
240        self.enum_val_dict[enum_name]['val'] = enum_val.split()[0]
241        # Perform conversion of VK_BIT macro
242        if 'VK_BIT' in self.enum_val_dict[enum_name]['val']:
243            vk_bit_val = self.enum_val_dict[enum_name]['val']
244            bit_shift = int(vk_bit_val[vk_bit_val.find('(')+1:vk_bit_val.find(')')], 0)
245            self.enum_val_dict[enum_name]['val'] = str(1 << bit_shift)
246        else:
247            # account for negative values surrounded by parens
248            self.enum_val_dict[enum_name]['val'] = self.enum_val_dict[enum_name]['val'].strip(')').replace('-(', '-')
249        # Try to cast to int to determine if enum value is unique
250        try:
251            #print("ENUM val:", self.enum_val_dict[enum_name]['val'])
252            int(self.enum_val_dict[enum_name]['val'], 0)
253            self.enum_val_dict[enum_name]['unique'] = True
254            #print("ENUM has num value")
255        except ValueError:
256            self.enum_val_dict[enum_name]['unique'] = False
257            #print("ENUM is not a number value")
258        # Update enum_type_dict as well
259        if not enum_type in self.enum_type_dict:
260            self.enum_type_dict[enum_type] = []
261        self.enum_type_dict[enum_type].append(enum_name)
262
263    # Return True if struct member is a dynamic array
264    # RULES : This is a bit quirky based on the API
265    # NOTE : Changes in API spec may cause these rules to change
266    #  1. There must be a previous uint var w/ 'count' in the name in the struct
267    #  2. Dynam array must have 'const' and '*' qualifiers
268    #  3a. Name of dynam array must end in 's' char OR
269    #  3b. Name of count var minus 'count' must be contained in name of dynamic array
270    def _is_dynamic_array(self, full_type, name):
271        exceptions = ['pEnabledFeatures', 'pWaitDstStageMask', 'pSampleMask']
272        if name in exceptions:
273            return False
274        if '' != self.last_struct_count_name:
275            if 'const' in full_type and '*' in full_type:
276                if name.endswith('s') or self.last_struct_count_name.lower().replace('count', '') in name.lower():
277                    return True
278
279                # VkWriteDescriptorSet
280                if self.last_struct_count_name == "descriptorCount":
281                    return True
282
283        return False
284
285    # populate struct dicts based on struct lines
286    # TODO : Handle ":" bitfield, "**" ptr->ptr and "const type*const*"
287    def _add_struct(self, line_txt, struct_type, num):
288        #print("Parsing struct line %s" % line_txt)
289        if '{' == struct_type:
290            print("Parsing struct '{' w/ line %s" % line_txt)
291        if not struct_type in self.struct_dict:
292            self.struct_dict[struct_type] = {}
293        members = line_txt.strip().split(';', 1)[0] # first strip semicolon & comments
294        # TODO : Handle bitfields more correctly
295        members = members.strip().split(':', 1)[0] # strip bitfield element
296        (member_type, member_name) = members.rsplit(None, 1)
297        # Store counts to help recognize and size dynamic arrays
298        if 'count' in member_name.lower() and 'samplecount' != member_name.lower() and 'uint' in member_type:
299            self.last_struct_count_name = member_name
300        self.struct_dict[struct_type][num] = {}
301        self.struct_dict[struct_type][num]['full_type'] = member_type
302        self.struct_dict[struct_type][num]['dyn_array'] = False
303        if '*' in member_type:
304            self.struct_dict[struct_type][num]['ptr'] = True
305            # TODO : Need more general purpose way here to reduce down to basic type
306            member_type = member_type.replace(' const*', '')
307            member_type = member_type.strip('*')
308        else:
309            self.struct_dict[struct_type][num]['ptr'] = False
310        if 'const' in member_type:
311            self.struct_dict[struct_type][num]['const'] = True
312            member_type = member_type.replace('const', '').strip()
313        else:
314            self.struct_dict[struct_type][num]['const'] = False
315        # TODO : There is a bug here where it seems that at the time we do this check,
316        #    the data is not in the types or typedef_rev_dict, so we never pass this if check
317        if is_type(member_type, 'struct'):
318            self.struct_dict[struct_type][num]['struct'] = True
319        else:
320            self.struct_dict[struct_type][num]['struct'] = False
321        self.struct_dict[struct_type][num]['type'] = member_type
322        if '[' in member_name:
323            (member_name, array_size) = member_name.split('[', 1)
324            #if 'char' in member_type:
325            #    self.struct_dict[struct_type][num]['array'] = False
326            #    self.struct_dict[struct_type][num]['array_size'] = 0
327            #    self.struct_dict[struct_type][num]['ptr'] = True
328            #else:
329            self.struct_dict[struct_type][num]['array'] = True
330            self.struct_dict[struct_type][num]['array_size'] = array_size.strip(']')
331        elif self._is_dynamic_array(self.struct_dict[struct_type][num]['full_type'], member_name):
332            #print("Found dynamic array %s of size %s" % (member_name, self.last_struct_count_name))
333            self.struct_dict[struct_type][num]['array'] = True
334            self.struct_dict[struct_type][num]['dyn_array'] = True
335            self.struct_dict[struct_type][num]['array_size'] = self.last_struct_count_name
336        elif not 'array' in self.struct_dict[struct_type][num]:
337            self.struct_dict[struct_type][num]['array'] = False
338            self.struct_dict[struct_type][num]['array_size'] = 0
339        self.struct_dict[struct_type][num]['name'] = member_name
340
341# check if given identifier is of specified type_to_check
342def is_type(identifier, type_to_check):
343    if identifier in types_dict and type_to_check == types_dict[identifier]:
344        return True
345    if identifier in typedef_rev_dict:
346        new_id = typedef_rev_dict[identifier]
347        if new_id in types_dict and type_to_check == types_dict[new_id]:
348            return True
349    return False
350
351# This is a validation function to verify that we can reproduce the original structs
352def recreate_structs():
353    for struct_name in struct_dict:
354        sys.stdout.write("typedef struct %s\n{\n" % struct_name)
355        for mem_num in sorted(struct_dict[struct_name]):
356            sys.stdout.write("    ")
357            if struct_dict[struct_name][mem_num]['const']:
358                sys.stdout.write("const ")
359            #if struct_dict[struct_name][mem_num]['struct']:
360            #    sys.stdout.write("struct ")
361            sys.stdout.write (struct_dict[struct_name][mem_num]['type'])
362            if struct_dict[struct_name][mem_num]['ptr']:
363                sys.stdout.write("*")
364            sys.stdout.write(" ")
365            sys.stdout.write(struct_dict[struct_name][mem_num]['name'])
366            if struct_dict[struct_name][mem_num]['array']:
367                sys.stdout.write("[")
368                sys.stdout.write(struct_dict[struct_name][mem_num]['array_size'])
369                sys.stdout.write("]")
370            sys.stdout.write(";\n")
371        sys.stdout.write("} ")
372        sys.stdout.write(typedef_fwd_dict[struct_name])
373        sys.stdout.write(";\n\n")
374
375#
376# TODO: Fix construction of struct name
377def get_struct_name_from_struct_type(struct_type):
378    # Note: All struct types are now camel-case
379    # Debug Report has an inconsistency - so need special case.
380    if ("VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT" == struct_type):
381        return "VkDebugReportCallbackCreateInfoEXT"
382    caps_struct_name = struct_type.replace("_STRUCTURE_TYPE", "")
383    char_idx = 0
384    struct_name = ''
385    for char in caps_struct_name:
386        if (0 == char_idx) or (caps_struct_name[char_idx-1] == '_'):
387            struct_name += caps_struct_name[char_idx]
388        elif (caps_struct_name[char_idx] == '_'):
389            pass
390        else:
391            struct_name += caps_struct_name[char_idx].lower()
392        char_idx += 1
393
394    return struct_name
395
396# Emit an ifdef if incoming func matches a platform identifier
397def add_platform_wrapper_entry(list, func):
398    if (re.match(r'.*Xlib.*', func)):
399        list.append("#ifdef VK_USE_PLATFORM_XLIB_KHR")
400    if (re.match(r'.*Xcb.*', func)):
401        list.append("#ifdef VK_USE_PLATFORM_XCB_KHR")
402    if (re.match(r'.*Wayland.*', func)):
403        list.append("#ifdef VK_USE_PLATFORM_WAYLAND_KHR")
404    if (re.match(r'.*Mir.*', func)):
405        list.append("#ifdef VK_USE_PLATFORM_MIR_KHR")
406    if (re.match(r'.*Android.*', func)):
407        list.append("#ifdef VK_USE_PLATFORM_ANDROID_KHR")
408    if (re.match(r'.*Win32.*', func)):
409        list.append("#ifdef VK_USE_PLATFORM_WIN32_KHR")
410
411# Emit an endif if incoming func matches a platform identifier
412def add_platform_wrapper_exit(list, func):
413    if (re.match(r'.*Xlib.*', func)):
414        list.append("#endif //VK_USE_PLATFORM_XLIB_KHR")
415    if (re.match(r'.*Xcb.*', func)):
416        list.append("#endif //VK_USE_PLATFORM_XCB_KHR")
417    if (re.match(r'.*Wayland.*', func)):
418        list.append("#endif //VK_USE_PLATFORM_WAYLAND_KHR")
419    if (re.match(r'.*Mir.*', func)):
420        list.append("#endif //VK_USE_PLATFORM_MIR_KHR")
421    if (re.match(r'.*Android.*', func)):
422        list.append("#endif //VK_USE_PLATFORM_ANDROID_KHR")
423    if (re.match(r'.*Win32.*', func)):
424        list.append("#endif //VK_USE_PLATFORM_WIN32_KHR")
425
426# class for writing common file elements
427# Here's how this class lays out a file:
428#  COPYRIGHT
429#  HEADER
430#  BODY
431#  FOOTER
432#
433# For each of these sections, there's a "set*" function
434# The class as a whole has a generate function which will write each section in order
435class CommonFileGen:
436    def __init__(self, filename=None, copyright_txt="", header_txt="", body_txt="", footer_txt=""):
437        self.filename = filename
438        self.contents = {'copyright': copyright_txt, 'header': header_txt, 'body': body_txt, 'footer': footer_txt}
439        # TODO : Set a default copyright & footer at least
440
441    def setFilename(self, filename):
442        self.filename = filename
443
444    def setCopyright(self, c):
445        self.contents['copyright'] = c
446
447    def setHeader(self, h):
448        self.contents['header'] = h
449
450    def setBody(self, b):
451        self.contents['body'] = b
452
453    def setFooter(self, f):
454        self.contents['footer'] = f
455
456    def generate(self):
457        #print("Generate to file %s" % self.filename)
458        with open(self.filename, "w") as f:
459            f.write(self.contents['copyright'])
460            f.write(self.contents['header'])
461            f.write(self.contents['body'])
462            f.write(self.contents['footer'])
463
464# class for writing a wrapper class for structures
465# The wrapper class wraps the structs and includes utility functions for
466#  setting/getting member values and displaying the struct data in various formats
467class StructWrapperGen:
468    def __init__(self, in_struct_dict, prefix, out_dir):
469        self.struct_dict = in_struct_dict
470        self.include_headers = []
471        self.lineinfo = sourcelineinfo()
472        self.api = prefix
473        if prefix.lower() == "vulkan":
474            self.api_prefix = "vk"
475        else:
476            self.api_prefix = prefix
477        self.header_filename = os.path.join(out_dir, self.api_prefix+"_struct_wrappers.h")
478        self.class_filename = os.path.join(out_dir, self.api_prefix+"_struct_wrappers.cpp")
479        self.safe_struct_header_filename = os.path.join(out_dir, self.api_prefix+"_safe_struct.h")
480        self.safe_struct_source_filename = os.path.join(out_dir, self.api_prefix+"_safe_struct.cpp")
481        self.string_helper_filename = os.path.join(out_dir, self.api_prefix+"_struct_string_helper.h")
482        self.string_helper_no_addr_filename = os.path.join(out_dir, self.api_prefix+"_struct_string_helper_no_addr.h")
483        self.string_helper_cpp_filename = os.path.join(out_dir, self.api_prefix+"_struct_string_helper_cpp.h")
484        self.string_helper_no_addr_cpp_filename = os.path.join(out_dir, self.api_prefix+"_struct_string_helper_no_addr_cpp.h")
485        self.validate_helper_filename = os.path.join(out_dir, self.api_prefix+"_struct_validate_helper.h")
486        self.no_addr = False
487        # Safe Struct (ss) header and source files
488        self.ssh = CommonFileGen(self.safe_struct_header_filename)
489        self.sss = CommonFileGen(self.safe_struct_source_filename)
490        self.hfg = CommonFileGen(self.header_filename)
491        self.cfg = CommonFileGen(self.class_filename)
492        self.shg = CommonFileGen(self.string_helper_filename)
493        self.shcppg = CommonFileGen(self.string_helper_cpp_filename)
494        self.vhg = CommonFileGen(self.validate_helper_filename)
495        self.size_helper_filename = os.path.join(out_dir, self.api_prefix+"_struct_size_helper.h")
496        self.size_helper_c_filename = os.path.join(out_dir, self.api_prefix+"_struct_size_helper.c")
497        self.size_helper_gen = CommonFileGen(self.size_helper_filename)
498        self.size_helper_c_gen = CommonFileGen(self.size_helper_c_filename)
499        #print(self.header_filename)
500        self.header_txt = ""
501        self.definition_txt = ""
502
503    def set_include_headers(self, include_headers):
504        self.include_headers = include_headers
505
506    def set_no_addr(self, no_addr):
507        self.no_addr = no_addr
508        if self.no_addr:
509            self.shg = CommonFileGen(self.string_helper_no_addr_filename)
510            self.shcppg = CommonFileGen(self.string_helper_no_addr_cpp_filename)
511        else:
512            self.shg = CommonFileGen(self.string_helper_filename)
513            self.shcppg = CommonFileGen(self.string_helper_cpp_filename)
514
515    # Return class name for given struct name
516    def get_class_name(self, struct_name):
517        class_name = struct_name.strip('_').lower() + "_struct_wrapper"
518        return class_name
519
520    def get_file_list(self):
521        return [os.path.basename(self.header_filename), os.path.basename(self.class_filename), os.path.basename(self.string_helper_filename)]
522
523    # Generate class header file
524    def generateHeader(self):
525        self.hfg.setCopyright(self._generateCopyright())
526        self.hfg.setHeader(self._generateHeader())
527        self.hfg.setBody(self._generateClassDeclaration())
528        self.hfg.setFooter(self._generateFooter())
529        self.hfg.generate()
530
531    # Generate class definition
532    def generateBody(self):
533        self.cfg.setCopyright(self._generateCopyright())
534        self.cfg.setHeader(self._generateCppHeader())
535        self.cfg.setBody(self._generateClassDefinition())
536        self.cfg.setFooter(self._generateFooter())
537        self.cfg.generate()
538
539    # Safe Structs are versions of vulkan structs with non-const safe ptrs
540    #  that make shadowing structures and clean-up of shadowed structures very simple
541    def generateSafeStructHeader(self):
542        self.ssh.setCopyright(self._generateCopyright())
543        self.ssh.setHeader(self._generateSafeStructHeader())
544        self.ssh.setBody(self._generateSafeStructDecls())
545        self.ssh.generate()
546
547    def generateSafeStructs(self):
548        self.sss.setCopyright(self._generateCopyright())
549        self.sss.setHeader(self._generateSafeStructSourceHeader())
550        self.sss.setBody(self._generateSafeStructSource())
551        self.sss.generate()
552
553    # Generate c-style .h file that contains functions for printing structs
554    def generateStringHelper(self):
555        print("Generating struct string helper")
556        self.shg.setCopyright(self._generateCopyright())
557        self.shg.setHeader(self._generateStringHelperHeader())
558        self.shg.setBody(self._generateStringHelperFunctions())
559        self.shg.generate()
560
561    # Generate cpp-style .h file that contains functions for printing structs
562    def generateStringHelperCpp(self):
563        print("Generating struct string helper cpp")
564        self.shcppg.setCopyright(self._generateCopyright())
565        self.shcppg.setHeader(self._generateStringHelperHeaderCpp())
566        self.shcppg.setBody(self._generateStringHelperFunctionsCpp())
567        self.shcppg.generate()
568
569    # Generate c-style .h file that contains functions for printing structs
570    def generateValidateHelper(self):
571        print("Generating struct validate helper")
572        self.vhg.setCopyright(self._generateCopyright())
573        self.vhg.setHeader(self._generateValidateHelperHeader())
574        self.vhg.setBody(self._generateValidateHelperFunctions())
575        self.vhg.generate()
576
577    def generateSizeHelper(self):
578        print("Generating struct size helper")
579        self.size_helper_gen.setCopyright(self._generateCopyright())
580        self.size_helper_gen.setHeader(self._generateSizeHelperHeader())
581        self.size_helper_gen.setBody(self._generateSizeHelperFunctions())
582        self.size_helper_gen.setFooter(self._generateSizeHelperFooter())
583        self.size_helper_gen.generate()
584
585    def generateSizeHelperC(self):
586        print("Generating struct size helper c")
587        self.size_helper_c_gen.setCopyright(self._generateCopyright())
588        self.size_helper_c_gen.setHeader(self._generateSizeHelperHeaderC())
589        self.size_helper_c_gen.setBody(self._generateSizeHelperFunctionsC())
590        self.size_helper_c_gen.generate()
591
592    def _generateCopyright(self):
593        copyright = []
594        copyright.append('/* THIS FILE IS GENERATED.  DO NOT EDIT. */');
595        copyright.append('');
596        copyright.append('/*');
597        copyright.append(' * Vulkan');
598        copyright.append(' *');
599        copyright.append(' * Copyright (c) 2015-2016 The Khronos Group Inc.');
600        copyright.append(' * Copyright (c) 2015-2016 Valve Corporation.');
601        copyright.append(' * Copyright (c) 2015-2016 LunarG, Inc.');
602        copyright.append(' * Copyright (c) 2015-2016 Google Inc.');
603        copyright.append(' *');
604        copyright.append(' * Licensed under the Apache License, Version 2.0 (the "License");');
605        copyright.append(' * you may not use this file except in compliance with the License.');
606        copyright.append(' * You may obtain a copy of the License at');
607        copyright.append(' *');
608        copyright.append(' *     http://www.apache.org/licenses/LICENSE-2.0');
609        copyright.append(' *');
610        copyright.append(' * Unless required by applicable law or agreed to in writing, software');
611        copyright.append(' * distributed under the License is distributed on an "AS IS" BASIS,');
612        copyright.append(' * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.');
613        copyright.append(' * See the License for the specific language governing permissions and');
614        copyright.append(' * limitations under the License.');
615        copyright.append(' *');
616        copyright.append(' * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>');
617        copyright.append(' * Author: Tobin Ehlis <tobin@lunarg.com>');
618        copyright.append(' */');
619        copyright.append('');
620        return "\n".join(copyright)
621
622    def _generateCppHeader(self):
623        header = []
624        header.append("//#includes, #defines, globals and such...\n")
625        header.append("#include <stdio.h>\n#include <%s>\n#include <%s_enum_string_helper.h>\n" % (os.path.basename(self.header_filename), self.api_prefix))
626        return "".join(header)
627
628    def _generateClassDefinition(self):
629        class_def = []
630        if 'vk' == self.api:
631            class_def.append(self._generateDynamicPrintFunctions())
632        for s in sorted(self.struct_dict):
633            class_def.append("\n// %s class definition" % self.get_class_name(s))
634            class_def.append(self._generateConstructorDefinitions(s))
635            class_def.append(self._generateDestructorDefinitions(s))
636            class_def.append(self._generateDisplayDefinitions(s))
637        return "\n".join(class_def)
638
639    def _generateConstructorDefinitions(self, s):
640        con_defs = []
641        con_defs.append("%s::%s() : m_struct(), m_indent(0), m_dummy_prefix('\\0'), m_origStructAddr(NULL) {}" % (self.get_class_name(s), self.get_class_name(s)))
642        # TODO : This is a shallow copy of ptrs
643        con_defs.append("%s::%s(%s* pInStruct) : m_indent(0), m_dummy_prefix('\\0')\n{\n    m_struct = *pInStruct;\n    m_origStructAddr = pInStruct;\n}" % (self.get_class_name(s), self.get_class_name(s), typedef_fwd_dict[s]))
644        con_defs.append("%s::%s(const %s* pInStruct) : m_indent(0), m_dummy_prefix('\\0')\n{\n    m_struct = *pInStruct;\n    m_origStructAddr = pInStruct;\n}" % (self.get_class_name(s), self.get_class_name(s), typedef_fwd_dict[s]))
645        return "\n".join(con_defs)
646
647    def _generateDestructorDefinitions(self, s):
648        return "%s::~%s() {}" % (self.get_class_name(s), self.get_class_name(s))
649
650    def _generateDynamicPrintFunctions(self):
651        dp_funcs = []
652        dp_funcs.append("\nvoid dynamic_display_full_txt(const void* pStruct, uint32_t indent)\n{\n    // Cast to APP_INFO ptr initially just to pull sType off struct")
653        dp_funcs.append("    VkStructureType sType = ((VkApplicationInfo*)pStruct)->sType;\n")
654        dp_funcs.append("    switch (sType)\n    {")
655        for e in enum_type_dict:
656            class_num = 0
657            if "StructureType" in e:
658                for v in sorted(enum_type_dict[e]):
659                    struct_name = get_struct_name_from_struct_type(v)
660                    if struct_name not in self.struct_dict:
661                        continue
662
663                    class_name = self.get_class_name(struct_name)
664                    instance_name = "swc%i" % class_num
665                    dp_funcs.append("        case %s:\n        {" % (v))
666                    dp_funcs.append("            %s %s((%s*)pStruct);" % (class_name, instance_name, struct_name))
667                    dp_funcs.append("            %s.set_indent(indent);" % (instance_name))
668                    dp_funcs.append("            %s.display_full_txt();" % (instance_name))
669                    dp_funcs.append("        }")
670                    dp_funcs.append("        break;")
671                    class_num += 1
672                dp_funcs.append("    }")
673        dp_funcs.append("}\n")
674        return "\n".join(dp_funcs)
675
676    def _get_func_name(self, struct, mid_str):
677        return "%s_%s_%s" % (self.api_prefix, mid_str, struct.lower().strip("_"))
678
679    def _get_sh_func_name(self, struct):
680        return self._get_func_name(struct, 'print')
681
682    def _get_vh_func_name(self, struct):
683        return self._get_func_name(struct, 'validate')
684
685    def _get_size_helper_func_name(self, struct):
686        return self._get_func_name(struct, 'size')
687
688    # Return elements to create formatted string for given struct member
689    def _get_struct_print_formatted(self, struct_member, pre_var_name="prefix", postfix = "\\n", struct_var_name="pStruct", struct_ptr=True, print_array=False):
690        struct_op = "->"
691        if not struct_ptr:
692            struct_op = "."
693        member_name = struct_member['name']
694        print_type = "p"
695        cast_type = ""
696        member_post = ""
697        array_index = ""
698        member_print_post = ""
699        print_delimiter = "%"
700        if struct_member['array'] and 'char' in struct_member['type'].lower(): # just print char array as string
701            if member_name.startswith('pp'): # TODO : Only printing first element of dynam array of char* for now
702                member_post = "[0]"
703            print_type = "s"
704            print_array = False
705        elif struct_member['array'] and not print_array:
706            # Just print base address of array when not full print_array
707            print_delimiter = "0x%"
708            cast_type = "(void*)"
709        elif is_type(struct_member['type'], 'enum'):
710            cast_type = "string_%s" % struct_member['type']
711            if struct_member['ptr']:
712                struct_var_name = "*" + struct_var_name
713                print_delimiter = "0x%"
714            print_type = "s"
715        elif is_type(struct_member['type'], 'struct'): # print struct address for now
716            print_delimiter = "0x%"
717            cast_type = "(void*)"
718            if not struct_member['ptr']:
719                cast_type = "(void*)&"
720        elif 'bool' in struct_member['type'].lower():
721            print_type = "s"
722            member_post = ' ? "TRUE" : "FALSE"'
723        elif 'float' in struct_member['type']:
724            print_type = "f"
725        elif 'uint64' in struct_member['type'] or 'gpusize' in struct_member['type'].lower():
726            print_type = '" PRId64 "'
727        elif 'uint8' in struct_member['type']:
728            print_type = "hu"
729        elif 'size' in struct_member['type'].lower():
730            print_type = '" PRINTF_SIZE_T_SPECIFIER "'
731            print_delimiter = ""
732        elif True in [ui_str.lower() in struct_member['type'].lower() for ui_str in ['uint', 'flags', 'samplemask']]:
733            print_type = "u"
734        elif 'int' in struct_member['type']:
735            print_type = "i"
736        elif struct_member['ptr']:
737            print_delimiter = "0x%"
738            pass
739        else:
740            #print("Unhandled struct type: %s" % struct_member['type'])
741            print_delimiter = "0x%"
742            cast_type = "(void*)"
743        if print_array and struct_member['array']:
744            member_print_post = "[%u]"
745            array_index = " i,"
746            member_post = "[i]"
747        print_out = "%%s%s%s = %s%s%s" % (member_name, member_print_post, print_delimiter, print_type, postfix) # section of print that goes inside of quotes
748        print_arg = ", %s,%s %s(%s%s%s)%s" % (pre_var_name, array_index, cast_type, struct_var_name, struct_op, member_name, member_post) # section of print passed to portion in quotes
749        if self.no_addr and "p" == print_type:
750            print_out = "%%s%s%s = addr\\n" % (member_name, member_print_post) # section of print that goes inside of quotes
751            print_arg = ", %s" % (pre_var_name)
752        return (print_out, print_arg)
753
754    def _generateStringHelperFunctions(self):
755        sh_funcs = []
756        # We do two passes, first pass just generates prototypes for all the functsions
757        for s in sorted(self.struct_dict):
758            sh_funcs.append('char* %s(const %s* pStruct, const char* prefix);' % (self._get_sh_func_name(s), typedef_fwd_dict[s]))
759        sh_funcs.append('')
760        sh_funcs.append('#if defined(_WIN32)')
761        sh_funcs.append('// Microsoft did not implement C99 in Visual Studio; but started adding it with')
762        sh_funcs.append('// VS2013.  However, VS2013 still did not have snprintf().  The following is a')
763        sh_funcs.append('// work-around.')
764        sh_funcs.append('#define snprintf _snprintf')
765        sh_funcs.append('#endif // _WIN32\n')
766        for s in sorted(self.struct_dict):
767            p_out = ""
768            p_args = ""
769            stp_list = [] # stp == "struct to print" a list of structs for this API call that should be printed as structs
770            # This pre-pass flags embedded structs and pNext
771            for m in sorted(self.struct_dict[s]):
772                if 'pNext' == self.struct_dict[s][m]['name'] or is_type(self.struct_dict[s][m]['type'], 'struct'):
773                    stp_list.append(self.struct_dict[s][m])
774            sh_funcs.append('char* %s(const %s* pStruct, const char* prefix)\n{\n    char* str;' % (self._get_sh_func_name(s), typedef_fwd_dict[s]))
775            sh_funcs.append("    size_t len;")
776            num_stps = len(stp_list);
777            total_strlen_str = ''
778            if 0 != num_stps:
779                sh_funcs.append("    char* tmpStr;")
780                sh_funcs.append('    char* extra_indent = (char*)malloc(strlen(prefix) + 3);')
781                sh_funcs.append('    strcpy(extra_indent, "  ");')
782                sh_funcs.append('    strncat(extra_indent, prefix, strlen(prefix));')
783                sh_funcs.append('    char* stp_strs[%i];' % num_stps)
784                for index in range(num_stps):
785                    # If it's an array, print all of the elements
786                    # If it's a ptr, print thing it's pointing to
787                    # Non-ptr struct case. Print the struct using its address
788                    struct_deref = '&'
789                    if 1 < stp_list[index]['full_type'].count('*'):
790                        struct_deref = ''
791                    if (stp_list[index]['ptr']):
792                        sh_funcs.append('    if (pStruct->%s) {' % stp_list[index]['name'])
793                        if 'pNext' == stp_list[index]['name']:
794                            sh_funcs.append('        tmpStr = dynamic_display((void*)pStruct->pNext, prefix);')
795                            sh_funcs.append('        len = 256+strlen(tmpStr);')
796                            sh_funcs.append('        stp_strs[%i] = (char*)malloc(len);' % index)
797                            if self.no_addr:
798                                sh_funcs.append('        snprintf(stp_strs[%i], len, " %%spNext (addr)\\n%%s", prefix, tmpStr);' % index)
799                            else:
800                                sh_funcs.append('        snprintf(stp_strs[%i], len, " %%spNext (0x%%p)\\n%%s", prefix, (void*)pStruct->pNext, tmpStr);' % index)
801                            sh_funcs.append('        free(tmpStr);')
802                        else:
803                            if stp_list[index]['name'] in ['pImageViews', 'pBufferViews']:
804                                # TODO : This is a quick hack to handle these arrays of ptrs
805                                sh_funcs.append('        tmpStr = %s(&pStruct->%s[0], extra_indent);' % (self._get_sh_func_name(stp_list[index]['type']), stp_list[index]['name']))
806                            else:
807                                sh_funcs.append('        tmpStr = %s(pStruct->%s, extra_indent);' % (self._get_sh_func_name(stp_list[index]['type']), stp_list[index]['name']))
808                            sh_funcs.append('        len = 256+strlen(tmpStr)+strlen(prefix);')
809                            sh_funcs.append('        stp_strs[%i] = (char*)malloc(len);' % (index))
810                            if self.no_addr:
811                                sh_funcs.append('        snprintf(stp_strs[%i], len, " %%s%s (addr)\\n%%s", prefix, tmpStr);' % (index, stp_list[index]['name']))
812                            else:
813                                sh_funcs.append('        snprintf(stp_strs[%i], len, " %%s%s (0x%%p)\\n%%s", prefix, (void*)pStruct->%s, tmpStr);' % (index, stp_list[index]['name'], stp_list[index]['name']))
814                        sh_funcs.append('    }')
815                        sh_funcs.append("    else\n        stp_strs[%i] = \"\";" % (index))
816                    elif stp_list[index]['array']:
817                        sh_funcs.append('    tmpStr = %s(&pStruct->%s[0], extra_indent);' % (self._get_sh_func_name(stp_list[index]['type']), stp_list[index]['name']))
818                        sh_funcs.append('    len = 256+strlen(tmpStr);')
819                        sh_funcs.append('    stp_strs[%i] = (char*)malloc(len);' % (index))
820                        if self.no_addr:
821                            sh_funcs.append('    snprintf(stp_strs[%i], len, " %%s%s[0] (addr)\\n%%s", prefix, tmpStr);' % (index, stp_list[index]['name']))
822                        else:
823                            sh_funcs.append('    snprintf(stp_strs[%i], len, " %%s%s[0] (0x%%p)\\n%%s", prefix, (void*)&pStruct->%s[0], tmpStr);' % (index, stp_list[index]['name'], stp_list[index]['name']))
824                    else:
825                        sh_funcs.append('    tmpStr = %s(&pStruct->%s, extra_indent);' % (self._get_sh_func_name(stp_list[index]['type']), stp_list[index]['name']))
826                        sh_funcs.append('    len = 256+strlen(tmpStr);')
827                        sh_funcs.append('    stp_strs[%i] = (char*)malloc(len);' % (index))
828                        if self.no_addr:
829                            sh_funcs.append('    snprintf(stp_strs[%i], len, " %%s%s (addr)\\n%%s", prefix, tmpStr);' % (index, stp_list[index]['name']))
830                        else:
831                            sh_funcs.append('    snprintf(stp_strs[%i], len, " %%s%s (0x%%p)\\n%%s", prefix, (void*)&pStruct->%s, tmpStr);' % (index, stp_list[index]['name'], stp_list[index]['name']))
832                    total_strlen_str += 'strlen(stp_strs[%i]) + ' % index
833            sh_funcs.append('    len = %ssizeof(char)*1024;' % (total_strlen_str))
834            sh_funcs.append('    str = (char*)malloc(len);')
835            sh_funcs.append('    snprintf(str, len, "')
836            for m in sorted(self.struct_dict[s]):
837                (p_out1, p_args1) = self._get_struct_print_formatted(self.struct_dict[s][m])
838                p_out += p_out1
839                p_args += p_args1
840            p_out += '"'
841            p_args += ");"
842            sh_funcs[-1] = '%s%s%s' % (sh_funcs[-1], p_out, p_args)
843            if 0 != num_stps:
844                sh_funcs.append('    for (int32_t stp_index = %i; stp_index >= 0; stp_index--) {' % (num_stps-1))
845                sh_funcs.append('        if (0 < strlen(stp_strs[stp_index])) {')
846                sh_funcs.append('            strncat(str, stp_strs[stp_index], strlen(stp_strs[stp_index]));')
847                sh_funcs.append('            free(stp_strs[stp_index]);')
848                sh_funcs.append('        }')
849                sh_funcs.append('    }')
850                sh_funcs.append('    free(extra_indent);')
851            sh_funcs.append("    return str;\n}")
852        # Add function to dynamically print out unknown struct
853        sh_funcs.append("char* dynamic_display(const void* pStruct, const char* prefix)\n{")
854        sh_funcs.append("    // Cast to APP_INFO ptr initially just to pull sType off struct")
855        sh_funcs.append("    if (pStruct == NULL) {")
856        sh_funcs.append("        return NULL;")
857        sh_funcs.append("    }")
858        sh_funcs.append("    VkStructureType sType = ((VkApplicationInfo*)pStruct)->sType;")
859        sh_funcs.append('    char indent[100];\n    strcpy(indent, "    ");\n    strcat(indent, prefix);')
860        sh_funcs.append("    switch (sType)\n    {")
861        for e in enum_type_dict:
862            if "StructureType" in e:
863                for v in sorted(enum_type_dict[e]):
864                    struct_name = get_struct_name_from_struct_type(v)
865                    if struct_name not in self.struct_dict:
866                        continue
867                    print_func_name = self._get_sh_func_name(struct_name)
868                    sh_funcs.append('        case %s:\n        {' % (v))
869                    sh_funcs.append('            return %s((%s*)pStruct, indent);' % (print_func_name, struct_name))
870                    sh_funcs.append('        }')
871                    sh_funcs.append('        break;')
872                sh_funcs.append("        default:")
873                sh_funcs.append("        return NULL;")
874                sh_funcs.append("    }")
875        sh_funcs.append("}")
876        return "\n".join(sh_funcs)
877
878    def _generateStringHelperFunctionsCpp(self):
879        # declare str & tmp str
880        # declare array of stringstreams for every struct ptr in current struct
881        # declare array of stringstreams for every non-string element in current struct
882        # For every struct ptr, if non-Null, then set its string, else set to NULL str
883        # For every non-string element, set its string stream
884        # create and return final string
885        sh_funcs = []
886        # First generate prototypes for every struct
887        # XXX - REMOVE this comment
888        lineinfo = sourcelineinfo()
889        sh_funcs.append('%s' % lineinfo.get())
890        for s in sorted(self.struct_dict):
891            # Wrap this in platform check since it may contain undefined structs or functions
892            add_platform_wrapper_entry(sh_funcs, typedef_fwd_dict[s])
893            sh_funcs.append('std::string %s(const %s* pStruct, const std::string prefix);' % (self._get_sh_func_name(s), typedef_fwd_dict[s]))
894            add_platform_wrapper_exit(sh_funcs, typedef_fwd_dict[s])
895
896        sh_funcs.append('\n')
897        sh_funcs.append('%s' % lineinfo.get())
898        for s in sorted(self.struct_dict):
899            num_non_enum_elems = [(is_type(self.struct_dict[s][elem]['type'], 'enum') and not self.struct_dict[s][elem]['ptr']) for elem in self.struct_dict[s]].count(False)
900            stp_list = [] # stp == "struct to print" a list of structs for this API call that should be printed as structs
901            # This pre-pass flags embedded structs and pNext
902            for m in sorted(self.struct_dict[s]):
903                if 'pNext' == self.struct_dict[s][m]['name'] or is_type(self.struct_dict[s][m]['type'], 'struct') or self.struct_dict[s][m]['array']:
904                    # TODO: This is a tmp workaround
905                    if 'ppActiveLayerNames' not in self.struct_dict[s][m]['name']:
906                        stp_list.append(self.struct_dict[s][m])
907            sh_funcs.append('%s' % lineinfo.get())
908
909            # Wrap this in platform check since it may contain undefined structs or functions
910            add_platform_wrapper_entry(sh_funcs, typedef_fwd_dict[s])
911
912            sh_funcs.append('std::string %s(const %s* pStruct, const std::string prefix)\n{' % (self._get_sh_func_name(s), typedef_fwd_dict[s]))
913            sh_funcs.append('%s' % lineinfo.get())
914            indent = '    '
915            sh_funcs.append('%susing namespace StreamControl;' % (indent))
916            sh_funcs.append('%susing namespace std;' % (indent))
917            sh_funcs.append('%sstring final_str;' % (indent))
918            sh_funcs.append('%sstring tmp_str;' % (indent))
919            sh_funcs.append('%sstring extra_indent = "  " + prefix;' % (indent))
920            if (0 != num_non_enum_elems):
921                sh_funcs.append('%sstringstream ss[%u];' % (indent, num_non_enum_elems))
922            num_stps = len(stp_list)
923            # First generate code for any embedded structs or arrays
924            if 0 < num_stps:
925                sh_funcs.append('%sstring stp_strs[%u];' % (indent, num_stps))
926                idx_ss_decl = False # Make sure to only decl this once
927                for index in range(num_stps):
928                    addr_char = '&'
929                    if 1 < stp_list[index]['full_type'].count('*'):
930                        addr_char = ''
931                    if stp_list[index]['array']:
932                        sh_funcs.append('%s' % lineinfo.get())
933                        if stp_list[index]['dyn_array']:
934                            sh_funcs.append('%s' % lineinfo.get())
935                            array_count = 'pStruct->%s' % (stp_list[index]['array_size'])
936                        else:
937                            sh_funcs.append('%s' % lineinfo.get())
938                            array_count = '%s' % (stp_list[index]['array_size'])
939                        sh_funcs.append('%s' % lineinfo.get())
940                        sh_funcs.append('%sstp_strs[%u] = "";' % (indent, index))
941                        if not idx_ss_decl:
942                            sh_funcs.append('%sstringstream index_ss;' % (indent))
943                            idx_ss_decl = True
944                        if (stp_list[index]['name'] == 'pQueueFamilyIndices'):
945                            if (typedef_fwd_dict[s] == 'VkSwapchainCreateInfoKHR'):
946                                sh_funcs.append('%sif (pStruct->imageSharingMode == VK_SHARING_MODE_CONCURRENT) {' % (indent))
947                            else:
948                                sh_funcs.append('%sif (pStruct->sharingMode == VK_SHARING_MODE_CONCURRENT) {' % (indent))
949                            indent += '    '
950                        if (stp_list[index]['name'] == 'pImageInfo'):
951                            sh_funcs.append('%sif ((pStruct->descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER)                ||' % (indent))
952                            sh_funcs.append('%s    (pStruct->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) ||' % (indent))
953                            sh_funcs.append('%s    (pStruct->descriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE)          ||' % (indent))
954                            sh_funcs.append('%s    (pStruct->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE))           {' % (indent))
955                            indent += '    '
956                        elif (stp_list[index]['name'] == 'pBufferInfo'):
957                            sh_funcs.append('%sif ((pStruct->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)         ||' % (indent))
958                            sh_funcs.append('%s    (pStruct->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER)         ||' % (indent))
959                            sh_funcs.append('%s    (pStruct->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||' % (indent))
960                            sh_funcs.append('%s    (pStruct->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC))  {' % (indent))
961                            indent += '    '
962                        elif (stp_list[index]['name'] == 'pTexelBufferView'):
963                            sh_funcs.append('%sif ((pStruct->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER) ||' % (indent))
964                            sh_funcs.append('%s    (pStruct->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER))  {' % (indent))
965                            indent += '    '
966                        if stp_list[index]['dyn_array']:
967                            sh_funcs.append('%sif (pStruct->%s) {' % (indent, stp_list[index]['name']))
968                            indent += '    '
969                        sh_funcs.append('%sfor (uint32_t i = 0; i < %s; i++) {' % (indent, array_count))
970                        indent += '    '
971                        sh_funcs.append('%sindex_ss.str("");' % (indent))
972                        sh_funcs.append('%sindex_ss << i;' % (indent))
973                        if is_type(stp_list[index]['type'], 'enum'):
974                            sh_funcs.append('%s' % lineinfo.get())
975                            addr_char = ''
976                            #value_print = 'string_%s(%spStruct->%s)' % (self.struct_dict[s][m]['type'], deref, self.struct_dict[s][m]['name'])
977                            sh_funcs.append('%sss[%u] << string_%s(pStruct->%s[i]);' % (indent, index, stp_list[index]['type'], stp_list[index]['name']))
978                            sh_funcs.append('%sstp_strs[%u] += " " + prefix + "%s[" + index_ss.str() + "] = " + ss[%u].str() + "\\n";' % (indent, index, stp_list[index]['name'], index))
979                        elif is_type(stp_list[index]['type'], 'struct'):
980                            sh_funcs.append('%s' % lineinfo.get())
981                            sh_funcs.append('%sss[%u] << "0x" << %spStruct->%s[i];' % (indent, index, addr_char, stp_list[index]['name']))
982                            sh_funcs.append('%stmp_str = %s(%spStruct->%s[i], extra_indent);' % (indent, self._get_sh_func_name(stp_list[index]['type']), addr_char, stp_list[index]['name']))
983                            if self.no_addr:
984                                sh_funcs.append('%s' % lineinfo.get())
985                                sh_funcs.append('%sstp_strs[%u] += " " + prefix + "%s[" + index_ss.str() + "] (addr)\\n" + tmp_str;' % (indent, index, stp_list[index]['name']))
986                            else:
987                                sh_funcs.append('%s' % lineinfo.get())
988                                sh_funcs.append('%sstp_strs[%u] += " " + prefix + "%s[" + index_ss.str() + "] (" + ss[%u].str() + ")\\n" + tmp_str;' % (indent, index, stp_list[index]['name'], index))
989                        else:
990                            sh_funcs.append('%s' % lineinfo.get())
991                            addr_char = ''
992                            if stp_list[index]['ptr'] or 'UUID' in stp_list[index]['name']:
993                                sh_funcs.append('%sss[%u] << "0x" << %spStruct->%s[i];' % (indent, index, addr_char, stp_list[index]['name']))
994                            else:
995                                sh_funcs.append('%sss[%u] << %spStruct->%s[i];' % (indent, index, addr_char, stp_list[index]['name']))
996                            if stp_list[index]['type'] in vulkan.core.objects:
997                                sh_funcs.append('%sstp_strs[%u] += " " + prefix + "%s[" + index_ss.str() + "].handle = " + ss[%u].str() + "\\n";' % (indent, index, stp_list[index]['name'], index))
998                            else:
999                                sh_funcs.append('%sstp_strs[%u] += " " + prefix + "%s[" + index_ss.str() + "] = " + ss[%u].str() + "\\n";' % (indent, index, stp_list[index]['name'], index))
1000                        sh_funcs.append('%s' % lineinfo.get())
1001                        sh_funcs.append('%sss[%u].str("");' % (indent, index))
1002                        indent = indent[4:]
1003                        sh_funcs.append('%s}' % (indent))
1004                        if stp_list[index]['dyn_array']:
1005                            indent = indent[4:]
1006                            sh_funcs.append('%s}' % (indent))
1007                        #endif
1008                        if (stp_list[index]['name'] == 'pQueueFamilyIndices') or (stp_list[index]['name'] == 'pImageInfo') or (stp_list[index]['name'] == 'pBufferInfo') or (stp_list[index]['name'] == 'pTexelBufferView'):
1009                            indent = indent[4:]
1010                            sh_funcs.append('%s}' % (indent))
1011                    elif (stp_list[index]['ptr']):
1012                        sh_funcs.append('%s' % lineinfo.get())
1013                        sh_funcs.append('%sif (pStruct->%s) {' % (indent, stp_list[index]['name']))
1014                        indent += '    '
1015                        if 'pNext' == stp_list[index]['name']:
1016                            sh_funcs.append('%s' % lineinfo.get())
1017                            sh_funcs.append('        tmp_str = dynamic_display((void*)pStruct->pNext, prefix);')
1018                        else:
1019                            if stp_list[index]['name'] in ['pImageViews', 'pBufferViews']:
1020                                # TODO : This is a quick hack to handle these arrays of ptrs
1021                                sh_funcs.append('%s' % lineinfo.get())
1022                                sh_funcs.append('        tmp_str = %s(&pStruct->%s[0], extra_indent);' % (self._get_sh_func_name(stp_list[index]['type']), stp_list[index]['name']))
1023                            else:
1024                                sh_funcs.append('%s' % lineinfo.get())
1025                                sh_funcs.append('        tmp_str = %s(pStruct->%s, extra_indent);' % (self._get_sh_func_name(stp_list[index]['type']), stp_list[index]['name']))
1026                        sh_funcs.append('        ss[%u] << "0x" << %spStruct->%s;' % (index, addr_char, stp_list[index]['name']))
1027                        if self.no_addr:
1028                            sh_funcs.append('%s' % lineinfo.get())
1029                            sh_funcs.append('        stp_strs[%u] = " " + prefix + "%s (addr)\\n" + tmp_str;' % (index, stp_list[index]['name']))
1030                        else:
1031                            sh_funcs.append('%s' % lineinfo.get())
1032                            sh_funcs.append('        stp_strs[%u] = " " + prefix + "%s (" + ss[%u].str() + ")\\n" + tmp_str;' % (index, stp_list[index]['name'], index))
1033                        sh_funcs.append('        ss[%u].str("");' % (index))
1034                        sh_funcs.append('    }')
1035                        sh_funcs.append('    else')
1036                        sh_funcs.append('        stp_strs[%u] = "";' % index)
1037                    else:
1038                        sh_funcs.append('%s' % lineinfo.get())
1039                        sh_funcs.append('    tmp_str = %s(&pStruct->%s, extra_indent);' % (self._get_sh_func_name(stp_list[index]['type']), stp_list[index]['name']))
1040                        sh_funcs.append('    ss[%u] << "0x" << %spStruct->%s;' % (index, addr_char, stp_list[index]['name']))
1041                        if self.no_addr:
1042                            sh_funcs.append('    stp_strs[%u] = " " + prefix + "%s (addr)\\n" + tmp_str;' % (index, stp_list[index]['name']))
1043                            sh_funcs.append('%s' % lineinfo.get())
1044                        else:
1045                            sh_funcs.append('    stp_strs[%u] = " " + prefix + "%s (" + ss[%u].str() + ")\\n" + tmp_str;' % (index, stp_list[index]['name'], index))
1046                            sh_funcs.append('%s' % lineinfo.get())
1047                        sh_funcs.append('    ss[%u].str("");' % index)
1048            # Now print one-line info for all data members
1049            index = 0
1050            final_str = []
1051            for m in sorted(self.struct_dict[s]):
1052                if not is_type(self.struct_dict[s][m]['type'], 'enum'):
1053                    if is_type(self.struct_dict[s][m]['type'], 'struct') and not self.struct_dict[s][m]['ptr']:
1054                        if self.no_addr:
1055                            sh_funcs.append('%s' % lineinfo.get())
1056                            sh_funcs.append('    ss[%u].str("addr");' % (index))
1057                        else:
1058                            sh_funcs.append('%s' % lineinfo.get())
1059                            sh_funcs.append('    ss[%u] << "0x" << &pStruct->%s;' % (index, self.struct_dict[s][m]['name']))
1060                    elif self.struct_dict[s][m]['array']:
1061                        sh_funcs.append('%s' % lineinfo.get())
1062                        sh_funcs.append('    ss[%u] << "0x" << (void*)pStruct->%s;' % (index, self.struct_dict[s][m]['name']))
1063                    elif 'bool' in self.struct_dict[s][m]['type'].lower():
1064                        sh_funcs.append('%s' % lineinfo.get())
1065                        sh_funcs.append('    ss[%u].str(pStruct->%s ? "TRUE" : "FALSE");' % (index, self.struct_dict[s][m]['name']))
1066                    elif 'uint8' in self.struct_dict[s][m]['type'].lower():
1067                        sh_funcs.append('%s' % lineinfo.get())
1068                        sh_funcs.append('    ss[%u] << pStruct->%s;' % (index, self.struct_dict[s][m]['name']))
1069                    elif 'void' in self.struct_dict[s][m]['type'].lower() and self.struct_dict[s][m]['ptr']:
1070                        sh_funcs.append('%s' % lineinfo.get())
1071                        sh_funcs.append('    if (StreamControl::writeAddress)')
1072                        sh_funcs.append('        ss[%u] << "0x" << pStruct->%s;' % (index, self.struct_dict[s][m]['name']))
1073                        sh_funcs.append('    else')
1074                        sh_funcs.append('        ss[%u].str("address");' % (index))
1075                    elif 'char' in self.struct_dict[s][m]['type'].lower() and self.struct_dict[s][m]['ptr']:
1076                        sh_funcs.append('%s' % lineinfo.get())
1077                        sh_funcs.append('    if (pStruct->%s != NULL) {' % self.struct_dict[s][m]['name'])
1078                        sh_funcs.append('        ss[%u] << pStruct->%s;' % (index, self.struct_dict[s][m]['name']))
1079                        sh_funcs.append('     } else {')
1080                        sh_funcs.append('        ss[%u] << "";' % index)
1081                        sh_funcs.append('     }')
1082                    else:
1083                        if self.struct_dict[s][m]['ptr'] or \
1084                           'Vk' in self.struct_dict[s][m]['full_type'] or \
1085                           'PFN_vk' in self.struct_dict[s][m]['full_type']:
1086                            sh_funcs.append('%s' % lineinfo.get())
1087                            sh_funcs.append('    ss[%u] << "0x" << pStruct->%s;' % (index, self.struct_dict[s][m]['name']))
1088                        elif any (x in self.struct_dict[s][m]['name'].lower() for x in ("flag", "bit", "offset", "handle", "buffer", "object", "mask")) or \
1089                             'ID' in self.struct_dict[s][m]['name']:
1090                            sh_funcs.append('%s: NB: Edit here to choose hex vs dec output by variable name' % lineinfo.get())
1091                            sh_funcs.append('    ss[%u] << "0x" << pStruct->%s;' % (index, self.struct_dict[s][m]['name']))
1092                        else:
1093                            sh_funcs.append('%s: NB Edit this section to choose hex vs dec output by variable name' % lineinfo.get())
1094                            sh_funcs.append('    ss[%u] << pStruct->%s;' % (index, self.struct_dict[s][m]['name']))
1095                    value_print = 'ss[%u].str()' % index
1096                    index += 1
1097                else:
1098                    # For an non-empty array of enums just print address w/ note that array will be displayed below
1099                    if self.struct_dict[s][m]['ptr']:
1100                        sh_funcs.append('%s' % lineinfo.get())
1101                        sh_funcs.append('    if (pStruct->%s)' % (self.struct_dict[s][m]['name']))
1102                        sh_funcs.append('        ss[%u] << "0x" << pStruct->%s << " (See individual array values below)";' % (index, self.struct_dict[s][m]['name']))
1103                        sh_funcs.append('    else')
1104                        sh_funcs.append('        ss[%u].str("NULL");' % (index))
1105                        value_print = 'ss[%u].str()' % index
1106                        index += 1
1107                    # For single enum just print the string representation
1108                    else:
1109                        value_print = 'string_%s(pStruct->%s)' % (self.struct_dict[s][m]['type'], self.struct_dict[s][m]['name'])
1110                final_str.append('+ prefix + "%s = " + %s + "\\n"' % (self.struct_dict[s][m]['name'], value_print))
1111            if 0 != num_stps: # Append data for any embedded structs
1112                final_str.append("+ %s" % " + ".join(['stp_strs[%u]' % n for n in reversed(range(num_stps))]))
1113            sh_funcs.append('%s' % lineinfo.get())
1114            for final_str_part in final_str:
1115                sh_funcs.append('    final_str = final_str %s;' % final_str_part)
1116            sh_funcs.append('    return final_str;\n}')
1117
1118            # End of platform wrapped section
1119            add_platform_wrapper_exit(sh_funcs, typedef_fwd_dict[s])
1120
1121        # Add function to return a string value for input void*
1122        sh_funcs.append('%s' % lineinfo.get())
1123        sh_funcs.append("std::string string_convert_helper(const void* toString, const std::string prefix)\n{")
1124        sh_funcs.append("    using namespace StreamControl;")
1125        sh_funcs.append("    using namespace std;")
1126        sh_funcs.append("    stringstream ss;")
1127        sh_funcs.append('    ss << toString;')
1128        sh_funcs.append('    string final_str = prefix + ss.str();')
1129        sh_funcs.append("    return final_str;")
1130        sh_funcs.append("}")
1131        sh_funcs.append('%s' % lineinfo.get())
1132        # Add function to return a string value for input uint64_t
1133        sh_funcs.append("std::string string_convert_helper(const uint64_t toString, const std::string prefix)\n{")
1134        sh_funcs.append("    using namespace StreamControl;")
1135        sh_funcs.append("    using namespace std;")
1136        sh_funcs.append("    stringstream ss;")
1137        sh_funcs.append('    ss << toString;')
1138        sh_funcs.append('    string final_str = prefix + ss.str();')
1139        sh_funcs.append("    return final_str;")
1140        sh_funcs.append("}")
1141        sh_funcs.append('%s' % lineinfo.get())
1142        # Add function to return a string value for input VkSurfaceFormatKHR*
1143        sh_funcs.append("std::string string_convert_helper(VkSurfaceFormatKHR toString, const std::string prefix)\n{")
1144        sh_funcs.append("    using namespace std;")
1145        sh_funcs.append('    string final_str = prefix + "format = " + string_VkFormat(toString.format) + "format = " + string_VkColorSpaceKHR(toString.colorSpace);')
1146        sh_funcs.append("    return final_str;")
1147        sh_funcs.append("}")
1148        sh_funcs.append('%s' % lineinfo.get())
1149        # Add function to dynamically print out unknown struct
1150        sh_funcs.append("std::string dynamic_display(const void* pStruct, const std::string prefix)\n{")
1151        sh_funcs.append("    using namespace std;")
1152        sh_funcs.append("    // Cast to APP_INFO ptr initially just to pull sType off struct")
1153        sh_funcs.append("    if (pStruct == NULL) {\n")
1154        sh_funcs.append("        return string();")
1155        sh_funcs.append("    }\n")
1156        sh_funcs.append("    VkStructureType sType = ((VkApplicationInfo*)pStruct)->sType;")
1157        sh_funcs.append('    string indent = "    ";')
1158        sh_funcs.append('    indent += prefix;')
1159        sh_funcs.append("    switch (sType)\n    {")
1160        for e in enum_type_dict:
1161            if "StructureType" in e:
1162                for v in sorted(enum_type_dict[e]):
1163                    struct_name = get_struct_name_from_struct_type(v)
1164                    if struct_name not in self.struct_dict:
1165                        continue
1166                    print_func_name = self._get_sh_func_name(struct_name)
1167                    #sh_funcs.append('string %s(const %s* pStruct, const string prefix);' % (self._get_sh_func_name(s), typedef_fwd_dict[s]))
1168                    sh_funcs.append('        case %s:\n        {' % (v))
1169                    sh_funcs.append('            return %s((%s*)pStruct, indent);' % (print_func_name, struct_name))
1170                    sh_funcs.append('        }')
1171                    sh_funcs.append('        break;')
1172                sh_funcs.append("        default:")
1173                sh_funcs.append("        return string();")
1174        sh_funcs.append('%s' % lineinfo.get())
1175        sh_funcs.append("    }")
1176        sh_funcs.append("}")
1177        return "\n".join(sh_funcs)
1178
1179    def _genStructMemberPrint(self, member, s, array, struct_array):
1180        (p_out, p_arg) = self._get_struct_print_formatted(self.struct_dict[s][member], pre_var_name="&m_dummy_prefix", struct_var_name="m_struct", struct_ptr=False, print_array=True)
1181        extra_indent = ""
1182        if array:
1183            extra_indent = "    "
1184        if is_type(self.struct_dict[s][member]['type'], 'struct'): # print struct address for now
1185            struct_array.insert(0, self.struct_dict[s][member])
1186        elif self.struct_dict[s][member]['ptr']:
1187            # Special case for void* named "pNext"
1188            if "void" in self.struct_dict[s][member]['type'] and "pNext" == self.struct_dict[s][member]['name']:
1189                struct_array.insert(0, self.struct_dict[s][member])
1190        return ('    %sprintf("%%*s    %s", m_indent, ""%s);' % (extra_indent, p_out, p_arg), struct_array)
1191
1192    def _generateDisplayDefinitions(self, s):
1193        disp_def = []
1194        struct_array = []
1195        # Single-line struct print function
1196        disp_def.append("// Output 'structname = struct_address' on a single line")
1197        disp_def.append("void %s::display_single_txt()\n{" % self.get_class_name(s))
1198        disp_def.append('    printf(" %%*s%s = 0x%%p", m_indent, "", (void*)m_origStructAddr);' % typedef_fwd_dict[s])
1199        disp_def.append("}\n")
1200        # Private helper function to print struct members
1201        disp_def.append("// Private helper function that displays the members of the wrapped struct")
1202        disp_def.append("void %s::display_struct_members()\n{" % self.get_class_name(s))
1203        i_declared = False
1204        for member in sorted(self.struct_dict[s]):
1205            # TODO : Need to display each member based on its type
1206            # TODO : Need to handle pNext which are structs, but of void* type
1207            #   Can grab struct type off of header of struct pointed to
1208            # TODO : Handle Arrays
1209            if self.struct_dict[s][member]['array']:
1210                # Create for loop to print each element of array
1211                if not i_declared:
1212                    disp_def.append('    uint32_t i;')
1213                    i_declared = True
1214                disp_def.append('    for (i = 0; i<%s; i++) {' % self.struct_dict[s][member]['array_size'])
1215                (return_str, struct_array) = self._genStructMemberPrint(member, s, True, struct_array)
1216                disp_def.append(return_str)
1217                disp_def.append('    }')
1218            else:
1219                (return_str, struct_array) = self._genStructMemberPrint(member, s, False, struct_array)
1220                disp_def.append(return_str)
1221        disp_def.append("}\n")
1222        i_declared = False
1223        # Basic print function to display struct members
1224        disp_def.append("// Output all struct elements, each on their own line")
1225        disp_def.append("void %s::display_txt()\n{" % self.get_class_name(s))
1226        disp_def.append('    printf("%%*s%s struct contents at 0x%%p:\\n", m_indent, "", (void*)m_origStructAddr);' % typedef_fwd_dict[s])
1227        disp_def.append('    this->display_struct_members();')
1228        disp_def.append("}\n")
1229        # Advanced print function to display current struct and contents of any pointed-to structs
1230        disp_def.append("// Output all struct elements, and for any structs pointed to, print complete contents")
1231        disp_def.append("void %s::display_full_txt()\n{" % self.get_class_name(s))
1232        disp_def.append('    printf("%%*s%s struct contents at 0x%%p:\\n", m_indent, "", (void*)m_origStructAddr);' % typedef_fwd_dict[s])
1233        disp_def.append('    this->display_struct_members();')
1234        class_num = 0
1235        # TODO : Need to handle arrays of structs here
1236        for ms in struct_array:
1237            swc_name = "class%s" % str(class_num)
1238            if ms['array']:
1239                if not i_declared:
1240                    disp_def.append('    uint32_t i;')
1241                    i_declared = True
1242                disp_def.append('    for (i = 0; i<%s; i++) {' % ms['array_size'])
1243                #disp_def.append("        if (m_struct.%s[i]) {" % (ms['name']))
1244                disp_def.append("            %s %s(&(m_struct.%s[i]));" % (self.get_class_name(ms['type']), swc_name, ms['name']))
1245                disp_def.append("            %s.set_indent(m_indent + 4);" % (swc_name))
1246                disp_def.append("            %s.display_full_txt();" % (swc_name))
1247                #disp_def.append('        }')
1248                disp_def.append('    }')
1249            elif 'pNext' == ms['name']:
1250                # Need some code trickery here
1251                #  I'm thinking have a generated function that takes pNext ptr value
1252                #  then it checks sType and in large switch statement creates appropriate
1253                #  wrapper class type and then prints contents
1254                disp_def.append("    if (m_struct.%s) {" % (ms['name']))
1255                #disp_def.append('        printf("%*s    This is where we would call dynamic print function\\n", m_indent, "");')
1256                disp_def.append('        dynamic_display_full_txt(m_struct.%s, m_indent);' % (ms['name']))
1257                disp_def.append("    }")
1258            else:
1259                if ms['ptr']:
1260                    disp_def.append("    if (m_struct.%s) {" % (ms['name']))
1261                    disp_def.append("        %s %s(m_struct.%s);" % (self.get_class_name(ms['type']), swc_name, ms['name']))
1262                else:
1263                    disp_def.append("    if (&m_struct.%s) {" % (ms['name']))
1264                    disp_def.append("        %s %s(&m_struct.%s);" % (self.get_class_name(ms['type']), swc_name, ms['name']))
1265                disp_def.append("        %s.set_indent(m_indent + 4);" % (swc_name))
1266                disp_def.append("        %s.display_full_txt();\n    }" % (swc_name))
1267            class_num += 1
1268        disp_def.append("}\n")
1269        return "\n".join(disp_def)
1270
1271    def _generateStringHelperHeader(self):
1272        header = []
1273        header.append("//#includes, #defines, globals and such...\n")
1274        for f in self.include_headers:
1275            if 'vk_enum_string_helper' not in f:
1276                header.append("#include <%s>\n" % f)
1277        header.append('#include "vk_enum_string_helper.h"\n\n// Function Prototypes\n')
1278        header.append("char* dynamic_display(const void* pStruct, const char* prefix);\n")
1279        return "".join(header)
1280
1281    def _generateStringHelperHeaderCpp(self):
1282        header = []
1283        header.append("//#includes, #defines, globals and such...\n")
1284        for f in self.include_headers:
1285            if 'vk_enum_string_helper' not in f:
1286                header.append("#include <%s>\n" % f)
1287        header.append('#include "vk_enum_string_helper.h"\n')
1288        header.append('namespace StreamControl\n')
1289        header.append('{\n')
1290        header.append('bool writeAddress = true;\n')
1291        header.append('template <typename T>\n')
1292        header.append('std::ostream& operator<< (std::ostream &out, T const* pointer)\n')
1293        header.append('{\n')
1294        header.append('    if(writeAddress)\n')
1295        header.append('    {\n')
1296        header.append('        out.operator<<(pointer);\n')
1297        header.append('    }\n')
1298        header.append('    else\n')
1299        header.append('    {\n')
1300        header.append('        std::operator<<(out, "address");\n')
1301        header.append('    }\n')
1302        header.append('    return out;\n')
1303        header.append('}\n')
1304        header.append('std::ostream& operator<<(std::ostream &out, char const*const s)\n')
1305        header.append('{\n')
1306        header.append('    return std::operator<<(out, s);\n')
1307        header.append('}\n')
1308        header.append('}\n')
1309        header.append('\n')
1310        header.append("std::string dynamic_display(const void* pStruct, const std::string prefix);\n")
1311        return "".join(header)
1312
1313    def _generateValidateHelperFunctions(self):
1314        sh_funcs = []
1315        # We do two passes, first pass just generates prototypes for all the functsions
1316        for s in sorted(self.struct_dict):
1317
1318            # Wrap this in platform check since it may contain undefined structs or functions
1319            add_platform_wrapper_entry(sh_funcs, typedef_fwd_dict[s])
1320            sh_funcs.append('uint32_t %s(const %s* pStruct);' % (self._get_vh_func_name(s), typedef_fwd_dict[s]))
1321            add_platform_wrapper_exit(sh_funcs, typedef_fwd_dict[s])
1322
1323        sh_funcs.append('\n')
1324        for s in sorted(self.struct_dict):
1325
1326            # Wrap this in platform check since it may contain undefined structs or functions
1327            add_platform_wrapper_entry(sh_funcs, typedef_fwd_dict[s])
1328
1329            sh_funcs.append('uint32_t %s(const %s* pStruct)\n{' % (self._get_vh_func_name(s), typedef_fwd_dict[s]))
1330            for m in sorted(self.struct_dict[s]):
1331                # TODO : Need to handle arrays of enums like in VkRenderPassCreateInfo struct
1332                if is_type(self.struct_dict[s][m]['type'], 'enum') and not self.struct_dict[s][m]['ptr']:
1333                    sh_funcs.append('    if (!validate_%s(pStruct->%s))\n        return 0;' % (self.struct_dict[s][m]['type'], self.struct_dict[s][m]['name']))
1334                # TODO : Need a little refinement to this code to make sure type of struct matches expected input (ptr, const...)
1335                if is_type(self.struct_dict[s][m]['type'], 'struct'):
1336                    if (self.struct_dict[s][m]['ptr']):
1337                        sh_funcs.append('    if (pStruct->%s && !%s((const %s*)pStruct->%s))\n        return 0;' % (self.struct_dict[s][m]['name'], self._get_vh_func_name(self.struct_dict[s][m]['type']), self.struct_dict[s][m]['type'], self.struct_dict[s][m]['name']))
1338                    else:
1339                        sh_funcs.append('    if (!%s((const %s*)&pStruct->%s))\n        return 0;' % (self._get_vh_func_name(self.struct_dict[s][m]['type']), self.struct_dict[s][m]['type'], self.struct_dict[s][m]['name']))
1340            sh_funcs.append("    return 1;\n}")
1341
1342            # End of platform wrapped section
1343            add_platform_wrapper_exit(sh_funcs, typedef_fwd_dict[s])
1344
1345        return "\n".join(sh_funcs)
1346
1347    def _generateValidateHelperHeader(self):
1348        header = []
1349        header.append("//#includes, #defines, globals and such...\n")
1350        for f in self.include_headers:
1351            if 'vk_enum_validate_helper' not in f:
1352                header.append("#include <%s>\n" % f)
1353        header.append('#include "vk_enum_validate_helper.h"\n\n// Function Prototypes\n')
1354        #header.append("char* dynamic_display(const void* pStruct, const char* prefix);\n")
1355        return "".join(header)
1356
1357    def _generateSizeHelperFunctions(self):
1358        sh_funcs = []
1359        # just generates prototypes for all the functions
1360        for s in sorted(self.struct_dict):
1361
1362            # Wrap this in platform check since it may contain undefined structs or functions
1363            add_platform_wrapper_entry(sh_funcs, typedef_fwd_dict[s])
1364            sh_funcs.append('size_t %s(const %s* pStruct);' % (self._get_size_helper_func_name(s), typedef_fwd_dict[s]))
1365            add_platform_wrapper_exit(sh_funcs, typedef_fwd_dict[s])
1366
1367        return "\n".join(sh_funcs)
1368
1369
1370    def _generateSizeHelperFunctionsC(self):
1371        sh_funcs = []
1372        # generate function definitions
1373        for s in sorted(self.struct_dict):
1374
1375            # Wrap this in platform check since it may contain undefined structs or functions
1376            add_platform_wrapper_entry(sh_funcs, typedef_fwd_dict[s])
1377
1378            skip_list = [] # Used when struct elements need to be skipped because size already accounted for
1379            sh_funcs.append('size_t %s(const %s* pStruct)\n{' % (self._get_size_helper_func_name(s), typedef_fwd_dict[s]))
1380            indent = '    '
1381            sh_funcs.append('%ssize_t structSize = 0;' % (indent))
1382            sh_funcs.append('%sif (pStruct) {' % (indent))
1383            indent = '        '
1384            sh_funcs.append('%sstructSize = sizeof(%s);' % (indent, typedef_fwd_dict[s]))
1385            i_decl = False
1386            for m in sorted(self.struct_dict[s]):
1387                if m in skip_list:
1388                    continue
1389                if self.struct_dict[s][m]['dyn_array']:
1390                    if self.struct_dict[s][m]['full_type'].count('*') > 1:
1391                        if not is_type(self.struct_dict[s][m]['type'], 'struct') and not 'char' in self.struct_dict[s][m]['type'].lower():
1392                            if 'ppMemoryBarriers' == self.struct_dict[s][m]['name']:
1393                                # TODO : For now be conservative and consider all memBarrier ptrs as largest possible struct
1394                                sh_funcs.append('%sstructSize += pStruct->%s*(sizeof(%s*) + sizeof(VkImageMemoryBarrier));' % (indent, self.struct_dict[s][m]['array_size'], self.struct_dict[s][m]['type']))
1395                            else:
1396                                sh_funcs.append('%sstructSize += pStruct->%s*(sizeof(%s*) + sizeof(%s));' % (indent, self.struct_dict[s][m]['array_size'], self.struct_dict[s][m]['type'], self.struct_dict[s][m]['type']))
1397                        else: # This is an array of char* or array of struct ptrs
1398                            if not i_decl:
1399                                sh_funcs.append('%suint32_t i = 0;' % (indent))
1400                                i_decl = True
1401                            sh_funcs.append('%sfor (i = 0; i < pStruct->%s; i++) {' % (indent, self.struct_dict[s][m]['array_size']))
1402                            indent = '            '
1403                            if is_type(self.struct_dict[s][m]['type'], 'struct'):
1404                                sh_funcs.append('%sstructSize += (sizeof(%s*) + %s(pStruct->%s[i]));' % (indent, self.struct_dict[s][m]['type'], self._get_size_helper_func_name(self.struct_dict[s][m]['type']), self.struct_dict[s][m]['name']))
1405                            else:
1406                                sh_funcs.append('%sstructSize += (sizeof(char*) + (sizeof(char) * (1 + strlen(pStruct->%s[i]))));' % (indent, self.struct_dict[s][m]['name']))
1407                            indent = '        '
1408                            sh_funcs.append('%s}' % (indent))
1409                    else:
1410                        if is_type(self.struct_dict[s][m]['type'], 'struct'):
1411                            if not i_decl:
1412                                sh_funcs.append('%suint32_t i = 0;' % (indent))
1413                                i_decl = True
1414                            sh_funcs.append('%sfor (i = 0; i < pStruct->%s; i++) {' % (indent, self.struct_dict[s][m]['array_size']))
1415                            indent = '            '
1416                            sh_funcs.append('%sstructSize += %s(&pStruct->%s[i]);' % (indent, self._get_size_helper_func_name(self.struct_dict[s][m]['type']), self.struct_dict[s][m]['name']))
1417                            indent = '        '
1418                            sh_funcs.append('%s}' % (indent))
1419                        else:
1420                            sh_funcs.append('%sstructSize += pStruct->%s*sizeof(%s);' % (indent, self.struct_dict[s][m]['array_size'], self.struct_dict[s][m]['type']))
1421                elif self.struct_dict[s][m]['ptr'] and 'pNext' != self.struct_dict[s][m]['name']:
1422                    if 'char' in self.struct_dict[s][m]['type'].lower():
1423                        sh_funcs.append('%sstructSize += (pStruct->%s != NULL) ? sizeof(%s)*(1+strlen(pStruct->%s)) : 0;' % (indent, self.struct_dict[s][m]['name'], self.struct_dict[s][m]['type'], self.struct_dict[s][m]['name']))
1424                    elif is_type(self.struct_dict[s][m]['type'], 'struct'):
1425                        sh_funcs.append('%sstructSize += %s(pStruct->%s);' % (indent, self._get_size_helper_func_name(self.struct_dict[s][m]['type']), self.struct_dict[s][m]['name']))
1426                    elif 'void' not in self.struct_dict[s][m]['type'].lower():
1427                        if (self.struct_dict[s][m]['type'] != 'xcb_connection_t'):
1428                            sh_funcs.append('%sstructSize += sizeof(%s);' % (indent, self.struct_dict[s][m]['type']))
1429                elif 'size_t' == self.struct_dict[s][m]['type'].lower():
1430                    sh_funcs.append('%sstructSize += pStruct->%s;' % (indent, self.struct_dict[s][m]['name']))
1431                    skip_list.append(m+1)
1432            indent = '    '
1433            sh_funcs.append('%s}' % (indent))
1434            sh_funcs.append("%sreturn structSize;\n}" % (indent))
1435
1436            # End of platform wrapped section
1437            add_platform_wrapper_exit(sh_funcs, typedef_fwd_dict[s])
1438
1439        # Now generate generic functions to loop over entire struct chain (or just handle single generic structs)
1440        if '_debug_' not in self.header_filename:
1441            for follow_chain in [True, False]:
1442                sh_funcs.append('%s' % self.lineinfo.get())
1443                if follow_chain:
1444                    sh_funcs.append('size_t get_struct_chain_size(const void* pStruct)\n{')
1445                else:
1446                    sh_funcs.append('size_t get_dynamic_struct_size(const void* pStruct)\n{')
1447                indent = '    '
1448                sh_funcs.append('%s// Just use VkApplicationInfo as struct until actual type is resolved' % (indent))
1449                sh_funcs.append('%sVkApplicationInfo* pNext = (VkApplicationInfo*)pStruct;' % (indent))
1450                sh_funcs.append('%ssize_t structSize = 0;' % (indent))
1451                if follow_chain:
1452                    sh_funcs.append('%swhile (pNext) {' % (indent))
1453                    indent = '        '
1454                sh_funcs.append('%sswitch (pNext->sType) {' % (indent))
1455                indent += '    '
1456                for e in enum_type_dict:
1457                    if 'StructureType' in e:
1458                        for v in sorted(enum_type_dict[e]):
1459                            struct_name = get_struct_name_from_struct_type(v)
1460                            if struct_name not in self.struct_dict:
1461                                continue
1462
1463                            sh_funcs.append('%scase %s:' % (indent, v))
1464                            sh_funcs.append('%s{' % (indent))
1465                            indent += '    '
1466                            sh_funcs.append('%sstructSize += %s((%s*)pNext);' % (indent, self._get_size_helper_func_name(struct_name), struct_name))
1467                            sh_funcs.append('%sbreak;' % (indent))
1468                            indent = indent[:-4]
1469                            sh_funcs.append('%s}' % (indent))
1470                        sh_funcs.append('%sdefault:' % (indent))
1471                        indent += '    '
1472                        sh_funcs.append('%sassert(0);' % (indent))
1473                        sh_funcs.append('%sstructSize += 0;' % (indent))
1474                        indent = indent[:-4]
1475                indent = indent[:-4]
1476                sh_funcs.append('%s}' % (indent))
1477                if follow_chain:
1478                    sh_funcs.append('%spNext = (VkApplicationInfo*)pNext->pNext;' % (indent))
1479                    indent = indent[:-4]
1480                    sh_funcs.append('%s}' % (indent))
1481                sh_funcs.append('%sreturn structSize;\n}' % indent)
1482        return "\n".join(sh_funcs)
1483
1484    def _generateSizeHelperHeader(self):
1485        header = []
1486        header.append('\n#ifdef __cplusplus\n')
1487        header.append('extern "C" {\n')
1488        header.append('#endif\n')
1489        header.append("\n")
1490        header.append("//#includes, #defines, globals and such...\n")
1491        for f in self.include_headers:
1492            header.append("#include <%s>\n" % f)
1493        header.append('\n// Function Prototypes\n')
1494        header.append("size_t get_struct_chain_size(const void* pStruct);\n")
1495        header.append("size_t get_dynamic_struct_size(const void* pStruct);\n")
1496        return "".join(header)
1497
1498    def _generateSizeHelperHeaderC(self):
1499        header = []
1500        header.append('#include "vk_struct_size_helper.h"')
1501        header.append('#include <string.h>')
1502        header.append('#include <assert.h>')
1503        header.append('\n// Function definitions\n')
1504        return "\n".join(header)
1505
1506    def _generateSizeHelperFooter(self):
1507        footer = []
1508        footer.append('\n\n#ifdef __cplusplus')
1509        footer.append('}')
1510        footer.append('#endif')
1511        return "\n".join(footer)
1512
1513    def _generateHeader(self):
1514        header = []
1515        header.append("//#includes, #defines, globals and such...\n")
1516        for f in self.include_headers:
1517            header.append("#include <%s>\n" % f)
1518        return "".join(header)
1519
1520    # Declarations
1521    def _generateConstructorDeclarations(self, s):
1522        constructors = []
1523        constructors.append("    %s();\n" % self.get_class_name(s))
1524        constructors.append("    %s(%s* pInStruct);\n" % (self.get_class_name(s), typedef_fwd_dict[s]))
1525        constructors.append("    %s(const %s* pInStruct);\n" % (self.get_class_name(s), typedef_fwd_dict[s]))
1526        return "".join(constructors)
1527
1528    def _generateDestructorDeclarations(self, s):
1529        return "    virtual ~%s();\n" % self.get_class_name(s)
1530
1531    def _generateDisplayDeclarations(self, s):
1532        return "    void display_txt();\n    void display_single_txt();\n    void display_full_txt();\n"
1533
1534    def _generateGetSetDeclarations(self, s):
1535        get_set = []
1536        get_set.append("    void set_indent(uint32_t indent) { m_indent = indent; }\n")
1537        for member in sorted(self.struct_dict[s]):
1538            # TODO : Skipping array set/get funcs for now
1539            if self.struct_dict[s][member]['array']:
1540                continue
1541            get_set.append("    %s get_%s() { return m_struct.%s; }\n" % (self.struct_dict[s][member]['full_type'], self.struct_dict[s][member]['name'], self.struct_dict[s][member]['name']))
1542            if not self.struct_dict[s][member]['const']:
1543                get_set.append("    void set_%s(%s inValue) { m_struct.%s = inValue; }\n" % (self.struct_dict[s][member]['name'], self.struct_dict[s][member]['full_type'], self.struct_dict[s][member]['name']))
1544        return "".join(get_set)
1545
1546    def _generatePrivateMembers(self, s):
1547        priv = []
1548        priv.append("\nprivate:\n")
1549        priv.append("    %s m_struct;\n" % typedef_fwd_dict[s])
1550        priv.append("    const %s* m_origStructAddr;\n" % typedef_fwd_dict[s])
1551        priv.append("    uint32_t m_indent;\n")
1552        priv.append("    const char m_dummy_prefix;\n")
1553        priv.append("    void display_struct_members();\n")
1554        return "".join(priv)
1555
1556    def _generateClassDeclaration(self):
1557        class_decl = []
1558        for s in sorted(self.struct_dict):
1559            class_decl.append("\n//class declaration")
1560            class_decl.append("class %s\n{\npublic:" % self.get_class_name(s))
1561            class_decl.append(self._generateConstructorDeclarations(s))
1562            class_decl.append(self._generateDestructorDeclarations(s))
1563            class_decl.append(self._generateDisplayDeclarations(s))
1564            class_decl.append(self._generateGetSetDeclarations(s))
1565            class_decl.append(self._generatePrivateMembers(s))
1566            class_decl.append("};\n")
1567        return "\n".join(class_decl)
1568
1569    def _generateFooter(self):
1570        return "\n//any footer info for class\n"
1571
1572    def _getSafeStructName(self, struct):
1573        return "safe_%s" % (struct)
1574
1575    # If struct has sType or ptr members, generate safe type
1576    def _hasSafeStruct(self, s):
1577        exceptions = ['VkPhysicalDeviceFeatures']
1578        if s in exceptions:
1579            return False
1580        if 'sType' == self.struct_dict[s][0]['name']:
1581            return True
1582        for m in self.struct_dict[s]:
1583            if self.struct_dict[s][m]['ptr']:
1584                return True
1585        inclusions = ['VkDisplayPlanePropertiesKHR', 'VkDisplayModePropertiesKHR', 'VkDisplayPropertiesKHR']
1586        if s in inclusions:
1587            return True
1588        return False
1589
1590    def _generateSafeStructHeader(self):
1591        header = []
1592        header.append("//#includes, #defines, globals and such...\n")
1593        header.append('#pragma once\n')
1594        header.append('#include "vulkan/vulkan.h"')
1595        return "".join(header)
1596
1597    # If given ty is in obj list, or is a struct that contains anything in obj list, return True
1598    def _typeHasObject(self, ty, obj):
1599        if ty in obj:
1600            return True
1601        if is_type(ty, 'struct'):
1602            for m in self.struct_dict[ty]:
1603                if self.struct_dict[ty][m]['type'] in obj:
1604                    return True
1605        return False
1606
1607    def _generateSafeStructDecls(self):
1608        ss_decls = []
1609        for s in struct_order_list:
1610            if not self._hasSafeStruct(s):
1611                continue
1612            if s in ifdef_dict:
1613                ss_decls.append('#ifdef %s' % ifdef_dict[s])
1614            ss_name = self._getSafeStructName(s)
1615            ss_decls.append("\nstruct %s {" % (ss_name))
1616            for m in sorted(self.struct_dict[s]):
1617                m_type = self.struct_dict[s][m]['type']
1618                if is_type(m_type, 'struct') and self._hasSafeStruct(m_type):
1619                    m_type = self._getSafeStructName(m_type)
1620                if self.struct_dict[s][m]['array_size'] != 0 and not self.struct_dict[s][m]['dyn_array']:
1621                    ss_decls.append("    %s %s[%s];" % (m_type, self.struct_dict[s][m]['name'], self.struct_dict[s][m]['array_size']))
1622                elif self.struct_dict[s][m]['ptr'] and 'safe_' not in m_type and not self._typeHasObject(m_type, vulkan.object_non_dispatch_list):#m_type in ['char', 'float', 'uint32_t', 'void', 'VkPhysicalDeviceFeatures']: # We'll never overwrite char* so it can remain const
1623                    ss_decls.append("    %s %s;" % (self.struct_dict[s][m]['full_type'], self.struct_dict[s][m]['name']))
1624                elif self.struct_dict[s][m]['array']:
1625                    ss_decls.append("    %s* %s;" % (m_type, self.struct_dict[s][m]['name']))
1626                elif self.struct_dict[s][m]['ptr']:
1627                    ss_decls.append("    %s* %s;" % (m_type, self.struct_dict[s][m]['name']))
1628                else:
1629                    ss_decls.append("    %s %s;" % (m_type, self.struct_dict[s][m]['name']))
1630            ss_decls.append("    %s(const %s* pInStruct);" % (ss_name, s))
1631            ss_decls.append("    %s(const %s& src);" % (ss_name, ss_name)) # Copy constructor
1632            ss_decls.append("    %s();" % (ss_name)) # Default constructor
1633            ss_decls.append("    ~%s();" % (ss_name))
1634            ss_decls.append("    void initialize(const %s* pInStruct);" % (s))
1635            ss_decls.append("    void initialize(const %s* src);" % (ss_name))
1636            ss_decls.append("    %s *ptr() { return reinterpret_cast<%s *>(this); }" % (s, s))
1637            ss_decls.append("    %s const *ptr() const { return reinterpret_cast<%s const *>(this); }" % (s, s))
1638            ss_decls.append("};")
1639            if s in ifdef_dict:
1640                ss_decls.append('#endif')
1641        return "\n".join(ss_decls)
1642
1643    def _generateSafeStructSourceHeader(self):
1644        header = []
1645        header.append("//#includes, #defines, globals and such...\n")
1646        header.append('#include "vk_safe_struct.h"\n#include <string.h>\n\n')
1647        return "".join(header)
1648
1649    def _generateSafeStructSource(self):
1650        ss_src = []
1651        for s in struct_order_list:
1652            if not self._hasSafeStruct(s):
1653                continue
1654            if s in ifdef_dict:
1655                ss_src.append('#ifdef %s' % ifdef_dict[s])
1656            ss_name = self._getSafeStructName(s)
1657            init_list = '' # list of members in struct constructor initializer
1658            default_init_list = '' # Default constructor just inits ptrs to nullptr in initializer
1659            init_func_txt = '' # Txt for initialize() function that takes struct ptr and inits members
1660            construct_txt = '' # Body of constuctor as well as body of initialize() func following init_func_txt
1661            destruct_txt = ''
1662            # VkWriteDescriptorSet is special case because pointers may be non-null but ignored
1663            # TODO : This is ugly, figure out better way to do this
1664            custom_construct_txt = {'VkWriteDescriptorSet' :
1665                                    '    switch (descriptorType) {\n'
1666                                    '        case VK_DESCRIPTOR_TYPE_SAMPLER:\n'
1667                                    '        case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:\n'
1668                                    '        case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:\n'
1669                                    '        case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:\n'
1670                                    '        case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:\n'
1671                                    '        if (descriptorCount && pInStruct->pImageInfo) {\n'
1672                                    '            pImageInfo = new VkDescriptorImageInfo[descriptorCount];\n'
1673                                    '            for (uint32_t i=0; i<descriptorCount; ++i) {\n'
1674                                    '                pImageInfo[i] = pInStruct->pImageInfo[i];\n'
1675                                    '            }\n'
1676                                    '        }\n'
1677                                    '        break;\n'
1678                                    '        case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:\n'
1679                                    '        case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:\n'
1680                                    '        case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:\n'
1681                                    '        case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:\n'
1682                                    '        if (descriptorCount && pInStruct->pBufferInfo) {\n'
1683                                    '            pBufferInfo = new VkDescriptorBufferInfo[descriptorCount];\n'
1684                                    '            for (uint32_t i=0; i<descriptorCount; ++i) {\n'
1685                                    '                pBufferInfo[i] = pInStruct->pBufferInfo[i];\n'
1686                                    '            }\n'
1687                                    '        }\n'
1688                                    '        break;\n'
1689                                    '        case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:\n'
1690                                    '        case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:\n'
1691                                    '        if (descriptorCount && pInStruct->pTexelBufferView) {\n'
1692                                    '            pTexelBufferView = new VkBufferView[descriptorCount];\n'
1693                                    '            for (uint32_t i=0; i<descriptorCount; ++i) {\n'
1694                                    '                pTexelBufferView[i] = pInStruct->pTexelBufferView[i];\n'
1695                                    '            }\n'
1696                                    '        }\n'
1697                                    '        break;\n'
1698                                    '        default:\n'
1699                                    '        break;\n'
1700                                    '    }\n'}
1701            for m in self.struct_dict[s]:
1702                m_name = self.struct_dict[s][m]['name']
1703                m_type = self.struct_dict[s][m]['type']
1704                if is_type(m_type, 'struct') and self._hasSafeStruct(m_type):
1705                    m_type = self._getSafeStructName(m_type)
1706                if self.struct_dict[s][m]['ptr'] and 'safe_' not in m_type and not self._typeHasObject(m_type, vulkan.object_non_dispatch_list):# in ['char', 'float', 'uint32_t', 'void', 'VkPhysicalDeviceFeatures']) or 'pp' == self.struct_dict[s][m]['name'][0:1]:
1707                    # Ptr types w/o a safe_struct, for non-null case need to allocate new ptr and copy data in
1708                    if 'KHR' in ss_name or m_type in ['void', 'char']:
1709                        # For these exceptions just copy initial value over for now
1710                        init_list += '\n\t%s(pInStruct->%s),' % (m_name, m_name)
1711                        init_func_txt += '    %s = pInStruct->%s;\n' % (m_name, m_name)
1712                    else:
1713                        default_init_list += '\n\t%s(nullptr),' % (m_name)
1714                        init_list += '\n\t%s(nullptr),' % (m_name)
1715                        init_func_txt += '    %s = nullptr;\n' % (m_name)
1716                        if 'pNext' != m_name and 'void' not in m_type:
1717                            if not self.struct_dict[s][m]['array']:
1718                                construct_txt += '    if (pInStruct->%s) {\n' % (m_name)
1719                                construct_txt += '        %s = new %s(*pInStruct->%s);\n' % (m_name, m_type, m_name)
1720                                construct_txt += '    }\n'
1721                                destruct_txt += '    if (%s)\n' % (m_name)
1722                                destruct_txt += '        delete %s;\n' % (m_name)
1723                            else: # new array and then init each element
1724                                construct_txt += '    if (pInStruct->%s) {\n' % (m_name)
1725                                construct_txt += '        %s = new %s[pInStruct->%s];\n' % (m_name, m_type, self.struct_dict[s][m]['array_size'])
1726                                #construct_txt += '        std::copy (pInStruct->%s, pInStruct->%s+pInStruct->%s, %s);\n' % (m_name, m_name, self.struct_dict[s][m]['array_size'], m_name)
1727                                construct_txt += '        memcpy ((void *)%s, (void *)pInStruct->%s, sizeof(%s)*pInStruct->%s);\n' % (m_name, m_name, m_type, self.struct_dict[s][m]['array_size'])
1728                                construct_txt += '    }\n'
1729                                destruct_txt += '    if (%s)\n' % (m_name)
1730                                destruct_txt += '        delete[] %s;\n' % (m_name)
1731                elif self.struct_dict[s][m]['array']:
1732                    if not self.struct_dict[s][m]['dyn_array']:
1733                        # Handle static array case
1734                        construct_txt += '    for (uint32_t i=0; i<%s; ++i) {\n' % (self.struct_dict[s][m]['array_size'])
1735                        construct_txt += '        %s[i] = pInStruct->%s[i];\n' % (m_name, m_name)
1736                        construct_txt += '    }\n'
1737                    else:
1738                        # Init array ptr to NULL
1739                        default_init_list += '\n\t%s(nullptr),' % (m_name)
1740                        init_list += '\n\t%s(nullptr),' % (m_name)
1741                        init_func_txt += '    %s = nullptr;\n' % (m_name)
1742                        array_element = 'pInStruct->%s[i]' % (m_name)
1743                        if is_type(self.struct_dict[s][m]['type'], 'struct') and self._hasSafeStruct(self.struct_dict[s][m]['type']):
1744                            array_element = '%s(&pInStruct->%s[i])' % (self._getSafeStructName(self.struct_dict[s][m]['type']), m_name)
1745                        construct_txt += '    if (%s && pInStruct->%s) {\n' % (self.struct_dict[s][m]['array_size'], m_name)
1746                        construct_txt += '        %s = new %s[%s];\n' % (m_name, m_type, self.struct_dict[s][m]['array_size'])
1747                        destruct_txt += '    if (%s)\n' % (m_name)
1748                        destruct_txt += '        delete[] %s;\n' % (m_name)
1749                        construct_txt += '        for (uint32_t i=0; i<%s; ++i) {\n' % (self.struct_dict[s][m]['array_size'])
1750                        if 'safe_' in m_type:
1751                            construct_txt += '            %s[i].initialize(&pInStruct->%s[i]);\n' % (m_name, m_name)
1752                        else:
1753                            construct_txt += '            %s[i] = %s;\n' % (m_name, array_element)
1754                        construct_txt += '        }\n'
1755                        construct_txt += '    }\n'
1756                elif self.struct_dict[s][m]['ptr']:
1757                    construct_txt += '    if (pInStruct->%s)\n' % (m_name)
1758                    construct_txt += '        %s = new %s(pInStruct->%s);\n' % (m_name, m_type, m_name)
1759                    construct_txt += '    else\n'
1760                    construct_txt += '        %s = NULL;\n' % (m_name)
1761                    destruct_txt += '    if (%s)\n' % (m_name)
1762                    destruct_txt += '        delete %s;\n' % (m_name)
1763                elif 'safe_' in m_type: # inline struct, need to pass in reference for constructor
1764                    init_list += '\n\t%s(&pInStruct->%s),' % (m_name, m_name)
1765                    init_func_txt += '        %s.initialize(&pInStruct->%s);\n' % (m_name, m_name)
1766                else:
1767                    init_list += '\n\t%s(pInStruct->%s),' % (m_name, m_name)
1768                    init_func_txt += '    %s = pInStruct->%s;\n' % (m_name, m_name)
1769            if '' != init_list:
1770                init_list = init_list[:-1] # hack off final comma
1771            if s in custom_construct_txt:
1772                construct_txt = custom_construct_txt[s]
1773            ss_src.append("\n%s::%s(const %s* pInStruct) : %s\n{\n%s}" % (ss_name, ss_name, s, init_list, construct_txt))
1774            if '' != default_init_list:
1775                default_init_list = " : %s" % (default_init_list[:-1])
1776            ss_src.append("\n%s::%s()%s\n{}" % (ss_name, ss_name, default_init_list))
1777            # Create slight variation of init and construct txt for copy constructor that takes a src object reference vs. struct ptr
1778            copy_construct_init = init_func_txt.replace('pInStruct->', 'src.')
1779            copy_construct_txt = construct_txt.replace(' (pInStruct->', ' (src.') # Exclude 'if' blocks from next line
1780            copy_construct_txt = copy_construct_txt.replace('(pInStruct->', '(*src.') # Pass object to copy constructors
1781            copy_construct_txt = copy_construct_txt.replace('pInStruct->', 'src.') # Modify remaining struct refs for src object
1782            ss_src.append("\n%s::%s(const %s& src)\n{\n%s%s}" % (ss_name, ss_name, ss_name, copy_construct_init, copy_construct_txt)) # Copy constructor
1783            ss_src.append("\n%s::~%s()\n{\n%s}" % (ss_name, ss_name, destruct_txt))
1784            ss_src.append("\nvoid %s::initialize(const %s* pInStruct)\n{\n%s%s}" % (ss_name, s, init_func_txt, construct_txt))
1785            # Copy initializer uses same txt as copy constructor but has a ptr and not a reference
1786            init_copy = copy_construct_init.replace('src.', 'src->')
1787            init_construct = copy_construct_txt.replace('src.', 'src->')
1788            ss_src.append("\nvoid %s::initialize(const %s* src)\n{\n%s%s}" % (ss_name, ss_name, init_copy, init_construct))
1789            if s in ifdef_dict:
1790                ss_src.append('#endif')
1791        return "\n".join(ss_src)
1792
1793class EnumCodeGen:
1794    def __init__(self, enum_type_dict=None, enum_val_dict=None, typedef_fwd_dict=None, in_file=None, out_sh_file=None, out_vh_file=None):
1795        self.et_dict = enum_type_dict
1796        self.ev_dict = enum_val_dict
1797        self.tf_dict = typedef_fwd_dict
1798        self.in_file = in_file
1799        self.out_sh_file = out_sh_file
1800        self.eshfg = CommonFileGen(self.out_sh_file)
1801        self.out_vh_file = out_vh_file
1802        self.evhfg = CommonFileGen(self.out_vh_file)
1803
1804    def generateStringHelper(self):
1805        self.eshfg.setHeader(self._generateSHHeader())
1806        self.eshfg.setBody(self._generateSHBody())
1807        self.eshfg.generate()
1808
1809    def generateEnumValidate(self):
1810        self.evhfg.setHeader(self._generateSHHeader())
1811        self.evhfg.setBody(self._generateVHBody())
1812        self.evhfg.generate()
1813
1814    def _generateVHBody(self):
1815        body = []
1816        for bet in sorted(self.et_dict):
1817            fet = self.tf_dict[bet]
1818            body.append("static inline uint32_t validate_%s(%s input_value)\n{" % (fet, fet))
1819            # TODO : This is not ideal, but allows for flag combinations. Need more rigorous validation of realistic flag combinations
1820            if 'flagbits' in bet.lower():
1821                body.append('    if (input_value > (%s))' % (' | '.join(self.et_dict[bet])))
1822                body.append('        return 0;')
1823                body.append('    return 1;')
1824                body.append('}\n\n')
1825            else:
1826                body.append('    switch ((%s)input_value)\n    {' % (fet))
1827                for e in sorted(self.et_dict[bet]):
1828                    if (self.ev_dict[e]['unique']):
1829                        body.append('        case %s:' % (e))
1830                body.append('            return 1;\n        default:\n            return 0;\n    }\n}\n\n')
1831        return "\n".join(body)
1832
1833    def _generateSHBody(self):
1834        body = []
1835#        with open(self.out_file, "a") as hf:
1836            # bet == base_enum_type, fet == final_enum_type
1837        for bet in sorted(self.et_dict):
1838            fet = self.tf_dict[bet]
1839            body.append("static inline const char* string_%s(%s input_value)\n{\n    switch ((%s)input_value)\n    {" % (fet, fet, fet))
1840            for e in sorted(self.et_dict[bet]):
1841                if (self.ev_dict[e]['unique']):
1842                    body.append('        case %s:\n            return "%s";' % (e, e))
1843            body.append('        default:\n            return "Unhandled %s";\n    }\n}\n\n' % (fet))
1844        return "\n".join(body)
1845
1846    def _generateSHHeader(self):
1847        header = []
1848        header.append('#pragma once\n')
1849        header.append('#ifdef _WIN32\n')
1850        header.append('#pragma warning( disable : 4065 )\n')
1851        header.append('#endif\n')
1852        header.append('#include <vulkan/%s>\n\n\n' % self.in_file)
1853        return "\n".join(header)
1854
1855
1856class CMakeGen:
1857    def __init__(self, struct_wrapper=None, out_dir=None):
1858        self.sw = struct_wrapper
1859        self.include_headers = []
1860        self.add_lib_file_list = self.sw.get_file_list()
1861        self.out_dir = out_dir
1862        self.out_file = os.path.join(self.out_dir, "CMakeLists.txt")
1863        self.cmg = CommonFileGen(self.out_file)
1864
1865    def generate(self):
1866        self.cmg.setBody(self._generateBody())
1867        self.cmg.generate()
1868
1869    def _generateBody(self):
1870        body = []
1871        body.append("project(%s)" % os.path.basename(self.out_dir))
1872        body.append("cmake_minimum_required(VERSION 2.8)\n")
1873        body.append("add_library(${PROJECT_NAME} %s)\n" % " ".join(self.add_lib_file_list))
1874        body.append('set(COMPILE_FLAGS "-fpermissive")')
1875        body.append('set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMPILE_FLAGS}")\n')
1876        body.append("include_directories(${SRC_DIR}/thirdparty/${GEN_API}/inc/)\n")
1877        body.append("target_include_directories (%s PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})\n" % os.path.basename(self.out_dir))
1878        return "\n".join(body)
1879
1880class GraphVizGen:
1881    def __init__(self, struct_dict, prefix, out_dir):
1882        self.struct_dict = struct_dict
1883        self.api = prefix
1884        if prefix == "vulkan":
1885            self.api_prefix = "vk"
1886        else:
1887            self.api_prefix = prefix
1888        self.out_file = os.path.join(out_dir, self.api_prefix+"_struct_graphviz_helper.h")
1889        self.gvg = CommonFileGen(self.out_file)
1890
1891    def generate(self):
1892        self.gvg.setCopyright("//This is the copyright\n")
1893        self.gvg.setHeader(self._generateHeader())
1894        self.gvg.setBody(self._generateBody())
1895        #self.gvg.setFooter('}')
1896        self.gvg.generate()
1897
1898    def set_include_headers(self, include_headers):
1899        self.include_headers = include_headers
1900
1901    def _generateHeader(self):
1902        header = []
1903        header.append("//#includes, #defines, globals and such...\n")
1904        for f in self.include_headers:
1905            if 'vk_enum_string_helper' not in f:
1906                header.append("#include <%s>\n" % f)
1907        #header.append('#include "vk_enum_string_helper.h"\n\n// Function Prototypes\n')
1908        header.append("\nchar* dynamic_gv_display(const void* pStruct, const char* prefix);\n")
1909        return "".join(header)
1910
1911    def _get_gv_func_name(self, struct):
1912        return "%s_gv_print_%s" % (self.api_prefix, struct.lower().strip("_"))
1913
1914    # Return elements to create formatted string for given struct member
1915    def _get_struct_gv_print_formatted(self, struct_member, pre_var_name="", postfix = "\\n", struct_var_name="pStruct", struct_ptr=True, print_array=False, port_label=""):
1916        struct_op = "->"
1917        pre_var_name = '"%s "' % struct_member['full_type']
1918        if not struct_ptr:
1919            struct_op = "."
1920        member_name = struct_member['name']
1921        print_type = "p"
1922        cast_type = ""
1923        member_post = ""
1924        array_index = ""
1925        member_print_post = ""
1926        print_delimiter = "%"
1927        if struct_member['array'] and 'char' in struct_member['type'].lower(): # just print char array as string
1928            print_type = "p"
1929            print_array = False
1930        elif struct_member['array'] and not print_array:
1931            # Just print base address of array when not full print_array
1932            cast_type = "(void*)"
1933        elif is_type(struct_member['type'], 'enum'):
1934            if struct_member['ptr']:
1935                struct_var_name = "*" + struct_var_name
1936                print_delimiter = "0x%"
1937            cast_type = "string_%s" % struct_member['type']
1938            print_type = "s"
1939        elif is_type(struct_member['type'], 'struct'): # print struct address for now
1940            cast_type = "(void*)"
1941            print_delimiter = "0x%"
1942            if not struct_member['ptr']:
1943                cast_type = "(void*)&"
1944        elif 'bool' in struct_member['type'].lower():
1945            print_type = "s"
1946            member_post = ' ? "TRUE" : "FALSE"'
1947        elif 'float' in struct_member['type']:
1948            print_type = "f"
1949        elif 'uint64' in struct_member['type'] or 'gpusize' in struct_member['type'].lower():
1950            print_type = '" PRId64 "'
1951        elif 'uint8' in struct_member['type']:
1952            print_type = "hu"
1953        elif 'size' in struct_member['type'].lower():
1954            print_type = '" PRINTF_SIZE_T_SPECIFIER "'
1955            print_delimiter = ""
1956        elif True in [ui_str.lower() in struct_member['type'].lower() for ui_str in ['uint', 'flags', 'samplemask']]:
1957            print_type = "u"
1958        elif 'int' in struct_member['type']:
1959            print_type = "i"
1960        elif struct_member['ptr']:
1961            print_delimiter = "0x%"
1962            pass
1963        else:
1964            #print("Unhandled struct type: %s" % struct_member['type'])
1965            print_delimiter = "0x%"
1966            cast_type = "(void*)"
1967        if print_array and struct_member['array']:
1968            member_print_post = "[%u]"
1969            array_index = " i,"
1970            member_post = "[i]"
1971        print_out = "<TR><TD>%%s%s%s</TD><TD%s>%s%s%s</TD></TR>" % (member_name, member_print_post, port_label, print_delimiter, print_type, postfix) # section of print that goes inside of quotes
1972        print_arg = ", %s,%s %s(%s%s%s)%s\n" % (pre_var_name, array_index, cast_type, struct_var_name, struct_op, member_name, member_post) # section of print passed to portion in quotes
1973        return (print_out, print_arg)
1974
1975    def _generateBody(self):
1976        gv_funcs = []
1977        array_func_list = [] # structs for which we'll generate an array version of their print function
1978        array_func_list.append('vkbufferviewattachinfo')
1979        array_func_list.append('vkimageviewattachinfo')
1980        array_func_list.append('vksamplerimageviewinfo')
1981        array_func_list.append('vkdescriptortypecount')
1982        # For first pass, generate prototype
1983        for s in sorted(self.struct_dict):
1984            gv_funcs.append('char* %s(const %s* pStruct, const char* myNodeName);\n' % (self._get_gv_func_name(s), typedef_fwd_dict[s]))
1985            if s.lower().strip("_") in array_func_list:
1986                if s.lower().strip("_") in ['vkbufferviewattachinfo', 'vkimageviewattachinfo']:
1987                    gv_funcs.append('char* %s_array(uint32_t count, const %s* const* pStruct, const char* myNodeName);\n' % (self._get_gv_func_name(s), typedef_fwd_dict[s]))
1988                else:
1989                    gv_funcs.append('char* %s_array(uint32_t count, const %s* pStruct, const char* myNodeName);\n' % (self._get_gv_func_name(s), typedef_fwd_dict[s]))
1990        gv_funcs.append('\n')
1991        for s in sorted(self.struct_dict):
1992            p_out = ""
1993            p_args = ""
1994            stp_list = [] # stp == "struct to print" a list of structs for this API call that should be printed as structs
1995            # the fields below are a super-hacky way for now to get port labels into GV output, TODO : Clean this up!
1996            pl_dict = {}
1997            struct_num = 0
1998            # This isn't great but this pre-pass flags structs w/ pNext and other struct ptrs
1999            for m in sorted(self.struct_dict[s]):
2000                if 'pNext' == self.struct_dict[s][m]['name'] or is_type(self.struct_dict[s][m]['type'], 'struct'):
2001                    stp_list.append(self.struct_dict[s][m])
2002                    if 'pNext' == self.struct_dict[s][m]['name']:
2003                        pl_dict[m] = ' PORT=\\"pNext\\"'
2004                    else:
2005                        pl_dict[m] = ' PORT=\\"struct%i\\"' % struct_num
2006                    struct_num += 1
2007            gv_funcs.append('char* %s(const %s* pStruct, const char* myNodeName)\n{\n    char* str;\n' % (self._get_gv_func_name(s), typedef_fwd_dict[s]))
2008            num_stps = len(stp_list);
2009            total_strlen_str = ''
2010            if 0 != num_stps:
2011                gv_funcs.append("    char* tmpStr;\n")
2012                gv_funcs.append("    char nodeName[100];\n")
2013                gv_funcs.append('    char* stp_strs[%i];\n' % num_stps)
2014                for index in range(num_stps):
2015                    if (stp_list[index]['ptr']):
2016                        if 'pDescriptorInfo' == stp_list[index]['name']:
2017                            gv_funcs.append('    if (pStruct->pDescriptorInfo && (0 != pStruct->descriptorCount)) {\n')
2018                        else:
2019                            gv_funcs.append('    if (pStruct->%s) {\n' % stp_list[index]['name'])
2020                        if 'pNext' == stp_list[index]['name']:
2021                            gv_funcs.append('        sprintf(nodeName, "pNext_0x%p", (void*)pStruct->pNext);\n')
2022                            gv_funcs.append('        tmpStr = dynamic_gv_display((void*)pStruct->pNext, nodeName);\n')
2023                            gv_funcs.append('        stp_strs[%i] = (char*)malloc(256+strlen(tmpStr)+strlen(nodeName)+strlen(myNodeName));\n' % index)
2024                            gv_funcs.append('        sprintf(stp_strs[%i], "%%s\\n\\"%%s\\":pNext -> \\"%%s\\" [];\\n", tmpStr, myNodeName, nodeName);\n' % index)
2025                            gv_funcs.append('        free(tmpStr);\n')
2026                        else:
2027                            gv_funcs.append('        sprintf(nodeName, "%s_0x%%p", (void*)pStruct->%s);\n' % (stp_list[index]['name'], stp_list[index]['name']))
2028                            if stp_list[index]['name'] in ['pTypeCount', 'pSamplerImageViews']:
2029                                gv_funcs.append('        tmpStr = %s_array(pStruct->count, pStruct->%s, nodeName);\n' % (self._get_gv_func_name(stp_list[index]['type']), stp_list[index]['name']))
2030                            else:
2031                                gv_funcs.append('        tmpStr = %s(pStruct->%s, nodeName);\n' % (self._get_gv_func_name(stp_list[index]['type']), stp_list[index]['name']))
2032                            gv_funcs.append('        stp_strs[%i] = (char*)malloc(256+strlen(tmpStr)+strlen(nodeName)+strlen(myNodeName));\n' % (index))
2033                            gv_funcs.append('        sprintf(stp_strs[%i], "%%s\\n\\"%%s\\":struct%i -> \\"%%s\\" [];\\n", tmpStr, myNodeName, nodeName);\n' % (index, index))
2034                        gv_funcs.append('    }\n')
2035                        gv_funcs.append("    else\n        stp_strs[%i] = \"\";\n" % (index))
2036                    elif stp_list[index]['array']: # TODO : For now just printing first element of array
2037                        gv_funcs.append('    sprintf(nodeName, "%s_0x%%p", (void*)&pStruct->%s[0]);\n' % (stp_list[index]['name'], stp_list[index]['name']))
2038                        gv_funcs.append('    tmpStr = %s(&pStruct->%s[0], nodeName);\n' % (self._get_gv_func_name(stp_list[index]['type']), stp_list[index]['name']))
2039                        gv_funcs.append('    stp_strs[%i] = (char*)malloc(256+strlen(tmpStr)+strlen(nodeName)+strlen(myNodeName));\n' % (index))
2040                        gv_funcs.append('    sprintf(stp_strs[%i], "%%s\\n\\"%%s\\":struct%i -> \\"%%s\\" [];\\n", tmpStr, myNodeName, nodeName);\n' % (index, index))
2041                    else:
2042                        gv_funcs.append('    sprintf(nodeName, "%s_0x%%p", (void*)&pStruct->%s);\n' % (stp_list[index]['name'], stp_list[index]['name']))
2043                        gv_funcs.append('    tmpStr = %s(&pStruct->%s, nodeName);\n' % (self._get_gv_func_name(stp_list[index]['type']), stp_list[index]['name']))
2044                        gv_funcs.append('    stp_strs[%i] = (char*)malloc(256+strlen(tmpStr)+strlen(nodeName)+strlen(myNodeName));\n' % (index))
2045                        gv_funcs.append('    sprintf(stp_strs[%i], "%%s\\n\\"%%s\\":struct%i -> \\"%%s\\" [];\\n", tmpStr, myNodeName, nodeName);\n' % (index, index))
2046                    total_strlen_str += 'strlen(stp_strs[%i]) + ' % index
2047            gv_funcs.append('    str = (char*)malloc(%ssizeof(char)*2048);\n' % (total_strlen_str))
2048            gv_funcs.append('    sprintf(str, "\\"%s\\" [\\nlabel = <<TABLE BORDER=\\"0\\" CELLBORDER=\\"1\\" CELLSPACING=\\"0\\"><TR><TD COLSPAN=\\"2\\">%s (0x%p)</TD></TR>')
2049            p_args = ", myNodeName, myNodeName, pStruct"
2050            for m in sorted(self.struct_dict[s]):
2051                plabel = ""
2052                if m in pl_dict:
2053                    plabel = pl_dict[m]
2054                (p_out1, p_args1) = self._get_struct_gv_print_formatted(self.struct_dict[s][m], port_label=plabel)
2055                p_out += p_out1
2056                p_args += p_args1
2057            p_out += '</TABLE>>\\n];\\n\\n"'
2058            p_args += ");\n"
2059            gv_funcs.append(p_out)
2060            gv_funcs.append(p_args)
2061            if 0 != num_stps:
2062                gv_funcs.append('    for (int32_t stp_index = %i; stp_index >= 0; stp_index--) {\n' % (num_stps-1))
2063                gv_funcs.append('        if (0 < strlen(stp_strs[stp_index])) {\n')
2064                gv_funcs.append('            strncat(str, stp_strs[stp_index], strlen(stp_strs[stp_index]));\n')
2065                gv_funcs.append('            free(stp_strs[stp_index]);\n')
2066                gv_funcs.append('        }\n')
2067                gv_funcs.append('    }\n')
2068            gv_funcs.append("    return str;\n}\n")
2069            if s.lower().strip("_") in array_func_list:
2070                ptr_array = False
2071                if s.lower().strip("_") in ['vkbufferviewattachinfo', 'vkimageviewattachinfo']:
2072                    ptr_array = True
2073                    gv_funcs.append('char* %s_array(uint32_t count, const %s* const* pStruct, const char* myNodeName)\n{\n    char* str;\n    char tmpStr[1024];\n' % (self._get_gv_func_name(s), typedef_fwd_dict[s]))
2074                else:
2075                    gv_funcs.append('char* %s_array(uint32_t count, const %s* pStruct, const char* myNodeName)\n{\n    char* str;\n    char tmpStr[1024];\n' % (self._get_gv_func_name(s), typedef_fwd_dict[s]))
2076                gv_funcs.append('    str = (char*)malloc(sizeof(char)*1024*count);\n')
2077                gv_funcs.append('    sprintf(str, "\\"%s\\" [\\nlabel = <<TABLE BORDER=\\"0\\" CELLBORDER=\\"1\\" CELLSPACING=\\"0\\"><TR><TD COLSPAN=\\"3\\">%s (0x%p)</TD></TR>", myNodeName, myNodeName, pStruct);\n')
2078                gv_funcs.append('    for (uint32_t i=0; i < count; i++) {\n')
2079                gv_funcs.append('        sprintf(tmpStr, "');
2080                p_args = ""
2081                p_out = ""
2082                for m in sorted(self.struct_dict[s]):
2083                    plabel = ""
2084                    (p_out1, p_args1) = self._get_struct_gv_print_formatted(self.struct_dict[s][m], port_label=plabel)
2085                    if 0 == m: # Add array index notation at end of first row
2086                        p_out1 = '%s<TD ROWSPAN=\\"%i\\" PORT=\\"slot%%u\\">%%u</TD></TR>' % (p_out1[:-5], len(self.struct_dict[s]))
2087                        p_args1 += ', i, i'
2088                    p_out += p_out1
2089                    p_args += p_args1
2090                p_out += '"'
2091                p_args += ");\n"
2092                if ptr_array:
2093                    p_args = p_args.replace('->', '[i]->')
2094                else:
2095                    p_args = p_args.replace('->', '[i].')
2096                gv_funcs.append(p_out);
2097                gv_funcs.append(p_args);
2098                gv_funcs.append('        strncat(str, tmpStr, strlen(tmpStr));\n')
2099                gv_funcs.append('    }\n')
2100                gv_funcs.append('    strncat(str, "</TABLE>>\\n];\\n\\n", 20);\n')
2101                gv_funcs.append('    return str;\n}\n')
2102        # Add function to dynamically print out unknown struct
2103        gv_funcs.append("char* dynamic_gv_display(const void* pStruct, const char* nodeName)\n{\n")
2104        gv_funcs.append("    // Cast to APP_INFO ptr initially just to pull sType off struct\n")
2105        gv_funcs.append("    VkStructureType sType = ((VkApplicationInfo*)pStruct)->sType;\n")
2106        gv_funcs.append("    switch (sType)\n    {\n")
2107        for e in enum_type_dict:
2108            if "StructureType" in e:
2109                for v in sorted(enum_type_dict[e]):
2110                    struct_name = get_struct_name_from_struct_type(v)
2111                    if struct_name not in self.struct_dict:
2112                        continue
2113
2114                    print_func_name = self._get_gv_func_name(struct_name)
2115                    # TODO : Hand-coded fixes for some exceptions
2116                    #if 'VkPipelineCbStateCreateInfo' in struct_name:
2117                    #    struct_name = 'VK_PIPELINE_CB_STATE'
2118                    if 'VkSemaphoreCreateInfo' in struct_name:
2119                        struct_name = 'VkSemaphoreCreateInfo'
2120                        print_func_name = self._get_gv_func_name(struct_name)
2121                    elif 'VkSemaphoreOpenInfo' in struct_name:
2122                        struct_name = 'VkSemaphoreOpenInfo'
2123                        print_func_name = self._get_gv_func_name(struct_name)
2124                    gv_funcs.append('        case %s:\n' % (v))
2125                    gv_funcs.append('            return %s((%s*)pStruct, nodeName);\n' % (print_func_name, struct_name))
2126                    #gv_funcs.append('        }\n')
2127                    #gv_funcs.append('        break;\n')
2128                gv_funcs.append("        default:\n")
2129                gv_funcs.append("        return NULL;\n")
2130                gv_funcs.append("    }\n")
2131        gv_funcs.append("}")
2132        return "".join(gv_funcs)
2133
2134
2135
2136
2137
2138#    def _generateHeader(self):
2139#        hdr = []
2140#        hdr.append('digraph g {\ngraph [\nrankdir = "LR"\n];')
2141#        hdr.append('node [\nfontsize = "16"\nshape = "plaintext"\n];')
2142#        hdr.append('edge [\n];\n')
2143#        return "\n".join(hdr)
2144#
2145#    def _generateBody(self):
2146#        body = []
2147#        for s in sorted(self.struc_dict):
2148#            field_num = 1
2149#            body.append('"%s" [\nlabel = <<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"> <TR><TD COLSPAN="2" PORT="f0">%s</TD></TR>' % (s, typedef_fwd_dict[s]))
2150#            for m in sorted(self.struc_dict[s]):
2151#                body.append('<TR><TD PORT="f%i">%s</TD><TD PORT="f%i">%s</TD></TR>' % (field_num, self.struc_dict[s][m]['full_type'], field_num+1, self.struc_dict[s][m]['name']))
2152#                field_num += 2
2153#            body.append('</TABLE>>\n];\n')
2154#        return "".join(body)
2155
2156def main(argv=None):
2157    opts = handle_args()
2158    # Parse input file and fill out global dicts
2159    hfp = HeaderFileParser(opts.input_file)
2160    hfp.parse()
2161    # TODO : Don't want these to be global, see note at top about wrapper classes
2162    global enum_val_dict
2163    global enum_type_dict
2164    global struct_dict
2165    global typedef_fwd_dict
2166    global typedef_rev_dict
2167    global types_dict
2168    enum_val_dict = hfp.get_enum_val_dict()
2169    enum_type_dict = hfp.get_enum_type_dict()
2170    struct_dict = hfp.get_struct_dict()
2171    # TODO : Would like to validate struct data here to verify that all of the bools for struct members are correct at this point
2172    typedef_fwd_dict = hfp.get_typedef_fwd_dict()
2173    typedef_rev_dict = hfp.get_typedef_rev_dict()
2174    types_dict = hfp.get_types_dict()
2175    #print(enum_val_dict)
2176    #print(typedef_dict)
2177    #print(struct_dict)
2178    input_header = os.path.basename(opts.input_file)
2179    if 'vulkan.h' == input_header:
2180        input_header = "vulkan/vulkan.h"
2181
2182    prefix = os.path.basename(opts.input_file).strip(".h")
2183    if prefix == "vulkan":
2184        prefix = "vk"
2185    if (opts.abs_out_dir is not None):
2186        enum_sh_filename = os.path.join(opts.abs_out_dir, prefix+"_enum_string_helper.h")
2187    else:
2188        enum_sh_filename = os.path.join(os.getcwd(), opts.rel_out_dir, prefix+"_enum_string_helper.h")
2189    enum_sh_filename = os.path.abspath(enum_sh_filename)
2190    if not os.path.exists(os.path.dirname(enum_sh_filename)):
2191        print("Creating output dir %s" % os.path.dirname(enum_sh_filename))
2192        os.mkdir(os.path.dirname(enum_sh_filename))
2193    if opts.gen_enum_string_helper:
2194        print("Generating enum string helper to %s" % enum_sh_filename)
2195        enum_vh_filename = os.path.join(os.path.dirname(enum_sh_filename), prefix+"_enum_validate_helper.h")
2196        print("Generating enum validate helper to %s" % enum_vh_filename)
2197        eg = EnumCodeGen(enum_type_dict, enum_val_dict, typedef_fwd_dict, os.path.basename(opts.input_file), enum_sh_filename, enum_vh_filename)
2198        eg.generateStringHelper()
2199        eg.generateEnumValidate()
2200    #for struct in struct_dict:
2201    #print(struct)
2202    if opts.gen_struct_wrappers:
2203        sw = StructWrapperGen(struct_dict, os.path.basename(opts.input_file).strip(".h"), os.path.dirname(enum_sh_filename))
2204        #print(sw.get_class_name(struct))
2205        sw.set_include_headers([input_header,os.path.basename(enum_sh_filename),"stdint.h","cinttypes", "stdio.h","stdlib.h"])
2206        print("Generating struct wrapper header to %s" % sw.header_filename)
2207        sw.generateHeader()
2208        print("Generating struct wrapper class to %s" % sw.class_filename)
2209        sw.generateBody()
2210        sw.generateStringHelper()
2211        sw.generateValidateHelper()
2212        # Generate a 2nd helper file that excludes addrs
2213        sw.set_no_addr(True)
2214        sw.generateStringHelper()
2215        sw.set_no_addr(False)
2216        sw.set_include_headers([input_header,os.path.basename(enum_sh_filename),"stdint.h","stdio.h","stdlib.h","iostream","sstream","string"])
2217        sw.set_no_addr(True)
2218        sw.generateStringHelperCpp()
2219        sw.set_no_addr(False)
2220        sw.generateStringHelperCpp()
2221        sw.set_include_headers(["stdio.h", "stdlib.h", input_header])
2222        sw.generateSizeHelper()
2223        sw.generateSizeHelperC()
2224        sw.generateSafeStructHeader()
2225        sw.generateSafeStructs()
2226    if opts.gen_struct_sizes:
2227        st = StructWrapperGen(struct_dict, os.path.basename(opts.input_file).strip(".h"), os.path.dirname(enum_sh_filename))
2228        st.set_include_headers(["stdio.h", "stdlib.h", input_header])
2229        st.generateSizeHelper()
2230        st.generateSizeHelperC()
2231    if opts.gen_cmake:
2232        cmg = CMakeGen(sw, os.path.dirname(enum_sh_filename))
2233        cmg.generate()
2234    if opts.gen_graphviz:
2235        gv = GraphVizGen(struct_dict, os.path.basename(opts.input_file).strip(".h"), os.path.dirname(enum_sh_filename))
2236        gv.set_include_headers([input_header,os.path.basename(enum_sh_filename),"stdint.h","stdio.h","stdlib.h", "cinttypes"])
2237        gv.generate()
2238    print("DONE!")
2239    #print(typedef_rev_dict)
2240    #print(types_dict)
2241    #recreate_structs()
2242
2243if __name__ == "__main__":
2244    sys.exit(main())
2245