blob: 0119f122a18c34dada052dd5c1e3f97ae40ffb39 [file] [log] [blame]
Mark Lobodzinski85672672016-10-13 08:36:42 -06001#!/usr/bin/python3 -i
2#
Dave Houlton413a6782018-05-22 13:01:54 -06003# Copyright (c) 2015-2018 The Khronos Group Inc.
4# Copyright (c) 2015-2018 Valve Corporation
5# Copyright (c) 2015-2018 LunarG, Inc.
6# Copyright (c) 2015-2018 Google Inc.
Mark Lobodzinski85672672016-10-13 08:36:42 -06007#
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: Dustin Graves <dustin@lunarg.com>
Mark Lobodzinski26112592017-05-30 12:02:17 -060021# Author: Mark Lobodzinski <mark@lunarg.com>
Dave Houlton413a6782018-05-22 13:01:54 -060022# Author: Dave Houlton <daveh@lunarg.com>
Mark Lobodzinski85672672016-10-13 08:36:42 -060023
Dave Houlton413a6782018-05-22 13:01:54 -060024import os,re,sys,string,json
Mark Lobodzinski85672672016-10-13 08:36:42 -060025import xml.etree.ElementTree as etree
26from generator import *
27from collections import namedtuple
Mark Lobodzinski62f71562017-10-24 13:41:18 -060028from common_codegen import *
Mark Lobodzinski06954ea2017-06-21 12:21:45 -060029
Jamie Madill8d4cda22017-11-08 13:40:09 -050030# This is a workaround to use a Python 2.7 and 3.x compatible syntax.
31from io import open
Mark Lobodzinski85672672016-10-13 08:36:42 -060032
Mark Lobodzinskid4950072017-08-01 13:02:20 -060033# ParameterValidationGeneratorOptions - subclass of GeneratorOptions.
Mark Lobodzinski85672672016-10-13 08:36:42 -060034#
Mark Lobodzinskid4950072017-08-01 13:02:20 -060035# Adds options used by ParameterValidationOutputGenerator object during Parameter validation layer generation.
Mark Lobodzinski85672672016-10-13 08:36:42 -060036#
37# Additional members
38# prefixText - list of strings to prefix generated header with
39# (usually a copyright statement + calling convention macros).
40# protectFile - True if multiple inclusion protection should be
41# generated (based on the filename) around the entire header.
42# protectFeature - True if #ifndef..#endif protection should be
43# generated around a feature interface in the header file.
44# genFuncPointers - True if function pointer typedefs should be
45# generated
46# protectProto - If conditional protection should be generated
47# around prototype declarations, set to either '#ifdef'
48# to require opt-in (#ifdef protectProtoStr) or '#ifndef'
49# to require opt-out (#ifndef protectProtoStr). Otherwise
50# set to None.
51# protectProtoStr - #ifdef/#ifndef symbol to use around prototype
52# declarations, if protectProto is set
53# apicall - string to use for the function declaration prefix,
54# such as APICALL on Windows.
55# apientry - string to use for the calling convention macro,
56# in typedefs, such as APIENTRY.
57# apientryp - string to use for the calling convention macro
58# in function pointer typedefs, such as APIENTRYP.
59# indentFuncProto - True if prototype declarations should put each
60# parameter on a separate line
61# indentFuncPointer - True if typedefed function pointers should put each
62# parameter on a separate line
63# alignFuncParam - if nonzero and parameters are being put on a
64# separate line, align parameter names at the specified column
Mark Lobodzinskid4950072017-08-01 13:02:20 -060065class ParameterValidationGeneratorOptions(GeneratorOptions):
Mark Lobodzinski85672672016-10-13 08:36:42 -060066 def __init__(self,
67 filename = None,
68 directory = '.',
69 apiname = None,
70 profile = None,
71 versions = '.*',
72 emitversions = '.*',
73 defaultExtensions = None,
74 addExtensions = None,
75 removeExtensions = None,
Mark Lobodzinski62f71562017-10-24 13:41:18 -060076 emitExtensions = None,
Mark Lobodzinski85672672016-10-13 08:36:42 -060077 sortProcedure = regSortFeatures,
78 prefixText = "",
Mark Lobodzinski85672672016-10-13 08:36:42 -060079 apicall = '',
80 apientry = '',
81 apientryp = '',
82 indentFuncProto = True,
83 indentFuncPointer = False,
Mark Lobodzinski62f71562017-10-24 13:41:18 -060084 alignFuncParam = 0,
Mark Lobodzinski27a9e7c2018-05-31 16:01:57 -060085 expandEnumerants = True,
86 valid_usage_path = ''):
Mark Lobodzinski85672672016-10-13 08:36:42 -060087 GeneratorOptions.__init__(self, filename, directory, apiname, profile,
88 versions, emitversions, defaultExtensions,
Mark Lobodzinski62f71562017-10-24 13:41:18 -060089 addExtensions, removeExtensions, emitExtensions, sortProcedure)
Mark Lobodzinski85672672016-10-13 08:36:42 -060090 self.prefixText = prefixText
Mark Lobodzinski85672672016-10-13 08:36:42 -060091 self.apicall = apicall
92 self.apientry = apientry
93 self.apientryp = apientryp
94 self.indentFuncProto = indentFuncProto
95 self.indentFuncPointer = indentFuncPointer
96 self.alignFuncParam = alignFuncParam
Mark Lobodzinski62f71562017-10-24 13:41:18 -060097 self.expandEnumerants = expandEnumerants
Mark Lobodzinski27a9e7c2018-05-31 16:01:57 -060098 self.valid_usage_path = valid_usage_path
Mark Lobodzinski85672672016-10-13 08:36:42 -060099
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600100# ParameterValidationOutputGenerator - subclass of OutputGenerator.
Mark Lobodzinski85672672016-10-13 08:36:42 -0600101# Generates param checker layer code.
102#
103# ---- methods ----
104# ParamCheckerOutputGenerator(errFile, warnFile, diagFile) - args as for
105# OutputGenerator. Defines additional internal state.
106# ---- methods overriding base class ----
107# beginFile(genOpts)
108# endFile()
109# beginFeature(interface, emit)
110# endFeature()
111# genType(typeinfo,name)
112# genStruct(typeinfo,name)
113# genGroup(groupinfo,name)
114# genEnum(enuminfo, name)
115# genCmd(cmdinfo)
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600116class ParameterValidationOutputGenerator(OutputGenerator):
117 """Generate Parameter Validation code based on XML element attributes"""
Mark Lobodzinski85672672016-10-13 08:36:42 -0600118 # This is an ordered list of sections in the header file.
119 ALL_SECTIONS = ['command']
120 def __init__(self,
121 errFile = sys.stderr,
122 warnFile = sys.stderr,
123 diagFile = sys.stdout):
124 OutputGenerator.__init__(self, errFile, warnFile, diagFile)
125 self.INDENT_SPACES = 4
Mark Lobodzinskib6b8bbd2017-02-08 14:37:15 -0700126 self.intercepts = []
127 self.declarations = []
Mark Lobodzinski85672672016-10-13 08:36:42 -0600128 # Commands to ignore
129 self.blacklist = [
130 'vkGetInstanceProcAddr',
131 'vkGetDeviceProcAddr',
Mark Young6ba8abe2017-11-09 10:37:04 -0700132 'vkEnumerateInstanceVersion',
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600133 'vkEnumerateInstanceLayerProperties',
134 'vkEnumerateInstanceExtensionProperties',
135 'vkEnumerateDeviceLayerProperties',
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600136 'vkEnumerateDeviceExtensionProperties',
Mark Young6ba8abe2017-11-09 10:37:04 -0700137 'vkCmdDebugMarkerEndEXT',
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600138 ]
139 self.validate_only = [
140 'vkCreateInstance',
141 'vkDestroyInstance',
142 'vkCreateDevice',
143 'vkDestroyDevice',
144 'vkCreateQueryPool',
Mark Lobodzinski85672672016-10-13 08:36:42 -0600145 'vkCreateDebugReportCallbackEXT',
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600146 'vkDestroyDebugReportCallbackEXT',
147 'vkCreateCommandPool',
Petr Krause91f7a12017-12-14 20:57:36 +0100148 'vkCreateRenderPass',
149 'vkDestroyRenderPass',
Mark Young6ba8abe2017-11-09 10:37:04 -0700150 'vkCreateDebugUtilsMessengerEXT',
151 'vkDestroyDebugUtilsMessengerEXT',
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600152 ]
Dustin Gravesce68f082017-03-30 15:42:16 -0600153 # Structure fields to ignore
154 self.structMemberBlacklist = { 'VkWriteDescriptorSet' : ['dstSet'] }
Mark Lobodzinski85672672016-10-13 08:36:42 -0600155 # Validation conditions for some special case struct members that are conditionally validated
156 self.structMemberValidationConditions = { 'VkPipelineColorBlendStateCreateInfo' : { 'logicOp' : '{}logicOpEnable == VK_TRUE' } }
157 # Header version
158 self.headerVersion = None
159 # Internal state - accumulators for different inner block text
Mark Lobodzinskid8b7e022017-06-01 15:10:13 -0600160 self.validation = [] # Text comprising the main per-api parameter validation routines
Mark Lobodzinski85672672016-10-13 08:36:42 -0600161 self.stypes = [] # Values from the VkStructureType enumeration
162 self.structTypes = dict() # Map of Vulkan struct typename to required VkStructureType
163 self.handleTypes = set() # Set of handle type names
164 self.commands = [] # List of CommandData records for all Vulkan commands
165 self.structMembers = [] # List of StructMemberData records for all Vulkan structs
166 self.validatedStructs = dict() # Map of structs type names to generated validation code for that struct type
167 self.enumRanges = dict() # Map of enum name to BEGIN/END range values
Mark Lobodzinskif31e0422017-07-25 14:29:42 -0600168 self.enumValueLists = '' # String containing enumerated type map definitions
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600169 self.func_pointers = '' # String containing function pointers for manual PV functions
170 self.typedefs = '' # String containing function pointer typedefs
Mark Lobodzinski85672672016-10-13 08:36:42 -0600171 self.flags = set() # Map of flags typenames
172 self.flagBits = dict() # Map of flag bits typename to list of values
Chris Forbes78ea32d2016-11-28 11:14:17 +1300173 self.newFlags = set() # Map of flags typenames /defined in the current feature/
Mike Schuchardtafd00482017-08-24 15:15:02 -0600174 self.required_extensions = dict() # Dictionary of required extensions for each item in the current extension
Mark Lobodzinskid8b7e022017-06-01 15:10:13 -0600175 self.extension_type = '' # Type of active feature (extension), device or instance
176 self.extension_names = dict() # Dictionary of extension names to extension name defines
Mark Lobodzinskia1368552018-05-04 16:03:28 -0600177 self.structextends_list = [] # List of extensions which extend another struct
178 self.struct_feature_protect = dict() # Dictionary of structnames and FeatureExtraProtect strings
Mark Lobodzinski06954ea2017-06-21 12:21:45 -0600179 self.valid_vuids = set() # Set of all valid VUIDs
Dave Houlton413a6782018-05-22 13:01:54 -0600180 self.vuid_dict = dict() # VUID dictionary (from JSON)
181 self.alias_dict = dict() # Dict of cmd|struct aliases
Mark Lobodzinskia1368552018-05-04 16:03:28 -0600182 self.returnedonly_structs = []
Mark Lobodzinski85672672016-10-13 08:36:42 -0600183 # Named tuples to store struct and command data
184 self.StructType = namedtuple('StructType', ['name', 'value'])
185 self.CommandParam = namedtuple('CommandParam', ['type', 'name', 'ispointer', 'isstaticarray', 'isbool', 'israngedenum',
Mark Lobodzinskia1368552018-05-04 16:03:28 -0600186 'isconst', 'isoptional', 'iscount', 'noautovalidity',
187 'len', 'extstructs', 'condition', 'cdecl'])
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600188 self.CommandData = namedtuple('CommandData', ['name', 'params', 'cdecl', 'extension_type', 'result'])
Mark Lobodzinski85672672016-10-13 08:36:42 -0600189 self.StructMemberData = namedtuple('StructMemberData', ['name', 'members'])
Mark Lobodzinski06954ea2017-06-21 12:21:45 -0600190
Mark Lobodzinski85672672016-10-13 08:36:42 -0600191 #
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600192 # Generate Copyright comment block for file
193 def GenerateCopyright(self):
194 copyright = '/* *** THIS FILE IS GENERATED - DO NOT EDIT! ***\n'
195 copyright += ' * See parameter_validation_generator.py for modifications\n'
196 copyright += ' *\n'
Dave Houlton413a6782018-05-22 13:01:54 -0600197 copyright += ' * Copyright (c) 2015-2018 The Khronos Group Inc.\n'
198 copyright += ' * Copyright (c) 2015-2018 LunarG, Inc.\n'
199 copyright += ' * Copyright (C) 2015-2018 Google Inc.\n'
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600200 copyright += ' *\n'
201 copyright += ' * Licensed under the Apache License, Version 2.0 (the "License");\n'
202 copyright += ' * you may not use this file except in compliance with the License.\n'
203 copyright += ' * Copyright (c) 2015-2017 Valve Corporation\n'
204 copyright += ' * You may obtain a copy of the License at\n'
205 copyright += ' *\n'
206 copyright += ' * http://www.apache.org/licenses/LICENSE-2.0\n'
207 copyright += ' *\n'
208 copyright += ' * Unless required by applicable law or agreed to in writing, software\n'
209 copyright += ' * distributed under the License is distributed on an "AS IS" BASIS,\n'
210 copyright += ' * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n'
211 copyright += ' * See the License for the specific language governing permissions and\n'
212 copyright += ' * limitations under the License.\n'
213 copyright += ' *\n'
214 copyright += ' * Author: Mark Lobodzinski <mark@LunarG.com>\n'
Dave Houlton413a6782018-05-22 13:01:54 -0600215 copyright += ' * Author: Dave Houlton <daveh@LunarG.com>\n'
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600216 copyright += ' */\n\n'
217 return copyright
218 #
219 # Increases the global indent variable
Mark Lobodzinski85672672016-10-13 08:36:42 -0600220 def incIndent(self, indent):
221 inc = ' ' * self.INDENT_SPACES
222 if indent:
223 return indent + inc
224 return inc
225 #
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600226 # Decreases the global indent variable
Mark Lobodzinski85672672016-10-13 08:36:42 -0600227 def decIndent(self, indent):
228 if indent and (len(indent) > self.INDENT_SPACES):
229 return indent[:-self.INDENT_SPACES]
230 return ''
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600231 #
Dave Houlton413a6782018-05-22 13:01:54 -0600232 # Walk the JSON-derived dict and find all "vuid" key values
233 def ExtractVUIDs(self, d):
234 if hasattr(d, 'items'):
235 for k, v in d.items():
236 if k == "vuid":
237 yield v
238 elif isinstance(v, dict):
239 for s in self.ExtractVUIDs(v):
240 yield s
241 elif isinstance (v, list):
242 for l in v:
243 for s in self.ExtractVUIDs(l):
244 yield s
Mark Lobodzinski85672672016-10-13 08:36:42 -0600245 #
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600246 # Called at file creation time
Mark Lobodzinski85672672016-10-13 08:36:42 -0600247 def beginFile(self, genOpts):
248 OutputGenerator.beginFile(self, genOpts)
Mark Lobodzinski27a9e7c2018-05-31 16:01:57 -0600249
250 self.valid_usage_path = genOpts.valid_usage_path
251 vu_json_filename = os.path.join(self.valid_usage_path + os.sep, 'validusage.json')
252 if os.path.isfile(vu_json_filename):
253 json_file = open(vu_json_filename, 'r')
254 self.vuid_dict = json.load(json_file)
255 json_file.close()
256 if len(self.vuid_dict) == 0:
257 print("Error: Could not find, or error loading %s/validusage.json\n", vu_json_filename)
258 sys.exit(1)
259
Mark Lobodzinski85672672016-10-13 08:36:42 -0600260 # C-specific
261 #
Dave Houlton413a6782018-05-22 13:01:54 -0600262 # Build a set of all vuid text strings found in validusage.json
263 for json_vuid_string in self.ExtractVUIDs(self.vuid_dict):
264 self.valid_vuids.add(json_vuid_string)
Mark Lobodzinski06954ea2017-06-21 12:21:45 -0600265 #
Mark Lobodzinski85672672016-10-13 08:36:42 -0600266 # User-supplied prefix text, if any (list of strings)
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600267 s = self.GenerateCopyright()
268 write(s, file=self.outFile)
Mark Lobodzinski85672672016-10-13 08:36:42 -0600269 #
270 # Headers
271 write('#include <string>', file=self.outFile)
272 self.newline()
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600273 write('#include "vk_loader_platform.h"', file=self.outFile)
Mark Lobodzinski85672672016-10-13 08:36:42 -0600274 write('#include "vulkan/vulkan.h"', file=self.outFile)
275 write('#include "vk_layer_extension_utils.h"', file=self.outFile)
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600276 write('#include "parameter_validation.h"', file=self.outFile)
Mark Lobodzinski85672672016-10-13 08:36:42 -0600277 #
278 # Macros
279 self.newline()
280 write('#ifndef UNUSED_PARAMETER', file=self.outFile)
281 write('#define UNUSED_PARAMETER(x) (void)(x)', file=self.outFile)
282 write('#endif // UNUSED_PARAMETER', file=self.outFile)
283 #
284 # Namespace
285 self.newline()
286 write('namespace parameter_validation {', file = self.outFile)
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600287 self.newline()
288 write('extern std::mutex global_lock;', file = self.outFile)
289 write('extern std::unordered_map<void *, layer_data *> layer_data_map;', file = self.outFile)
290 write('extern std::unordered_map<void *, instance_layer_data *> instance_layer_data_map;', file = self.outFile)
291 self.newline()
Mark Lobodzinski78a12a92017-08-08 14:16:51 -0600292 #
293 # FuncPtrMap
294 self.func_pointers += 'std::unordered_map<std::string, void *> custom_functions = {\n'
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600295 #
296 # Called at end-time for final content output
Mark Lobodzinski85672672016-10-13 08:36:42 -0600297 def endFile(self):
298 # C-specific
299 self.newline()
Mark Lobodzinskif31e0422017-07-25 14:29:42 -0600300 write(self.enumValueLists, file=self.outFile)
301 self.newline()
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600302 write(self.typedefs, file=self.outFile)
303 self.newline()
Mark Lobodzinski78a12a92017-08-08 14:16:51 -0600304 self.func_pointers += '};\n'
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600305 write(self.func_pointers, file=self.outFile)
306 self.newline()
Mark Lobodzinskia1368552018-05-04 16:03:28 -0600307
308 pnext_handler = 'bool ValidatePnextStructContents(debug_report_data *report_data, const char *api_name, const ParameterName &parameter_name, const GenericHeader* header) {\n'
309 pnext_handler += ' bool skip = false;\n'
310 pnext_handler += ' switch(header->sType) {\n'
311
312 # Do some processing here to extract data from validatedstructs...
313 for item in self.structextends_list:
314 postProcSpec = {}
315 postProcSpec['ppp'] = '' if not item else '{postProcPrefix}'
316 postProcSpec['pps'] = '' if not item else '{postProcSuffix}'
317 postProcSpec['ppi'] = '' if not item else '{postProcInsert}'
318
319 pnext_case = '\n'
320 protect = ''
321 # Guard struct cases with feature ifdefs, if necessary
322 if item in self.struct_feature_protect.keys():
323 protect = self.struct_feature_protect[item]
324 pnext_case += '#ifdef %s\n' % protect
325 pnext_case += ' // Validation code for %s structure members\n' % item
326 pnext_case += ' case %s: {\n' % self.getStructType(item)
327 pnext_case += ' %s *structure = (%s *) header;\n' % (item, item)
Mark Lobodzinski554cf372018-05-24 11:06:00 -0600328 expr = self.expandStructCode(item, item, 'structure->', '', ' ', [], postProcSpec)
Mark Lobodzinskia1368552018-05-04 16:03:28 -0600329 struct_validation_source = self.ScrubStructCode(expr)
330 pnext_case += '%s' % struct_validation_source
331 pnext_case += ' } break;\n'
332 if protect is not '':
333 pnext_case += '#endif // %s\n' % protect
334 # Skip functions containing no validation
335 if struct_validation_source != '':
336 pnext_handler += pnext_case;
337 pnext_handler += ' default:\n'
338 pnext_handler += ' skip = false;\n'
339 pnext_handler += ' }\n'
340 pnext_handler += ' return skip;\n'
341 pnext_handler += '}\n'
342 write(pnext_handler, file=self.outFile)
343 self.newline()
344
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600345 ext_template = 'template <typename T>\n'
346 ext_template += 'bool OutputExtensionError(const T *layer_data, const std::string &api_name, const std::string &extension_name) {\n'
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600347 ext_template += ' return log_msg(layer_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,\n'
Dave Houlton413a6782018-05-22 13:01:54 -0600348 ext_template += ' kVUID_PVError_ExtensionNotEnabled, "Attemped to call %s() but its required extension %s has not been enabled\\n",\n'
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600349 ext_template += ' api_name.c_str(), extension_name.c_str());\n'
350 ext_template += '}\n'
351 write(ext_template, file=self.outFile)
352 self.newline()
Mark Lobodzinskid8b7e022017-06-01 15:10:13 -0600353 commands_text = '\n'.join(self.validation)
354 write(commands_text, file=self.outFile)
355 self.newline()
Mark Lobodzinskib6b8bbd2017-02-08 14:37:15 -0700356 # Output declarations and record intercepted procedures
357 write('// Declarations', file=self.outFile)
358 write('\n'.join(self.declarations), file=self.outFile)
Mark Lobodzinskide43e642017-06-07 14:00:31 -0600359 write('// Map of all APIs to be intercepted by this layer', file=self.outFile)
John Zulauf9652bf92018-10-02 16:56:28 -0600360 write('std::unordered_map<std::string, void*> name_to_funcptr_map = {', file=self.outFile)
Mark Lobodzinskib6b8bbd2017-02-08 14:37:15 -0700361 write('\n'.join(self.intercepts), file=self.outFile)
362 write('};\n', file=self.outFile)
363 self.newline()
Mark Lobodzinski85672672016-10-13 08:36:42 -0600364 # Namespace
365 write('} // namespace parameter_validation', file = self.outFile)
Mark Lobodzinski85672672016-10-13 08:36:42 -0600366 # Finish processing in superclass
367 OutputGenerator.endFile(self)
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600368 #
369 # Processing at beginning of each feature or extension
Mark Lobodzinski85672672016-10-13 08:36:42 -0600370 def beginFeature(self, interface, emit):
371 # Start processing in superclass
372 OutputGenerator.beginFeature(self, interface, emit)
373 # C-specific
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600374 # Accumulate includes, defines, types, enums, function pointer typedefs, end function prototypes separately for this
375 # feature. They're only printed in endFeature().
Mark Lobodzinski85672672016-10-13 08:36:42 -0600376 self.headerVersion = None
Mark Lobodzinski85672672016-10-13 08:36:42 -0600377 self.stypes = []
Mark Lobodzinski85672672016-10-13 08:36:42 -0600378 self.commands = []
379 self.structMembers = []
Chris Forbes78ea32d2016-11-28 11:14:17 +1300380 self.newFlags = set()
Mark Lobodzinski62f71562017-10-24 13:41:18 -0600381 self.featureExtraProtect = GetFeatureProtect(interface)
Mike Schuchardtafd00482017-08-24 15:15:02 -0600382 # Get base list of extension dependencies for all items in this extension
383 base_required_extensions = []
Mark Lobodzinski31964ca2017-09-18 14:15:09 -0600384 if "VK_VERSION_1" not in self.featureName:
Mark Lobodzinskid8b7e022017-06-01 15:10:13 -0600385 # Save Name Define to get correct enable name later
386 nameElem = interface[0][1]
387 name = nameElem.get('name')
388 self.extension_names[self.featureName] = name
389 # This extension is the first dependency for this command
Mike Schuchardtafd00482017-08-24 15:15:02 -0600390 base_required_extensions.append(self.featureName)
391 # Add any defined extension dependencies to the base dependency list for this extension
392 requires = interface.get('requires')
393 if requires is not None:
394 base_required_extensions.extend(requires.split(','))
Mike Schuchardtafd00482017-08-24 15:15:02 -0600395 # Build dictionary of extension dependencies for each item in this extension
396 self.required_extensions = dict()
397 for require_element in interface.findall('require'):
398 # Copy base extension dependency list
399 required_extensions = list(base_required_extensions)
400 # Add any additional extension dependencies specified in this require block
401 additional_extensions = require_element.get('extension')
402 if additional_extensions:
403 required_extensions.extend(additional_extensions.split(','))
404 # Save full extension list for all named items
405 for element in require_element.findall('*[@name]'):
406 self.required_extensions[element.get('name')] = required_extensions
407
Mark Lobodzinskid8b7e022017-06-01 15:10:13 -0600408 # And note if this is an Instance or Device extension
409 self.extension_type = interface.get('type')
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600410 #
411 # Called at the end of each extension (feature)
Mark Lobodzinski85672672016-10-13 08:36:42 -0600412 def endFeature(self):
413 # C-specific
414 # Actually write the interface to the output file.
415 if (self.emit):
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600416 # If type declarations are needed by other features based on this one, it may be necessary to suppress the ExtraProtect,
Mark Lobodzinski85672672016-10-13 08:36:42 -0600417 # or move it below the 'for section...' loop.
Mark Lobodzinskid8b7e022017-06-01 15:10:13 -0600418 ifdef = ''
Mark Lobodzinski85672672016-10-13 08:36:42 -0600419 if (self.featureExtraProtect != None):
Mark Lobodzinskid8b7e022017-06-01 15:10:13 -0600420 ifdef = '#ifdef %s\n' % self.featureExtraProtect
421 self.validation.append(ifdef)
Mark Lobodzinski85672672016-10-13 08:36:42 -0600422 # Generate the struct member checking code from the captured data
423 self.processStructMemberData()
424 # Generate the command parameter checking code from the captured data
425 self.processCmdData()
426 # Write the declaration for the HeaderVersion
427 if self.headerVersion:
428 write('const uint32_t GeneratedHeaderVersion = {};'.format(self.headerVersion), file=self.outFile)
429 self.newline()
430 # Write the declarations for the VkFlags values combining all flag bits
Chris Forbes78ea32d2016-11-28 11:14:17 +1300431 for flag in sorted(self.newFlags):
Mark Lobodzinski85672672016-10-13 08:36:42 -0600432 flagBits = flag.replace('Flags', 'FlagBits')
433 if flagBits in self.flagBits:
434 bits = self.flagBits[flagBits]
435 decl = 'const {} All{} = {}'.format(flag, flagBits, bits[0])
436 for bit in bits[1:]:
437 decl += '|' + bit
438 decl += ';'
439 write(decl, file=self.outFile)
Mark Lobodzinskid8b7e022017-06-01 15:10:13 -0600440 endif = '\n'
Mark Lobodzinski85672672016-10-13 08:36:42 -0600441 if (self.featureExtraProtect != None):
Mark Lobodzinskid8b7e022017-06-01 15:10:13 -0600442 endif = '#endif // %s\n' % self.featureExtraProtect
443 self.validation.append(endif)
Mark Lobodzinski85672672016-10-13 08:36:42 -0600444 # Finish processing in superclass
445 OutputGenerator.endFeature(self)
446 #
Mark Lobodzinski85672672016-10-13 08:36:42 -0600447 # Type generation
Mike Schuchardtf375c7c2017-12-28 11:23:48 -0700448 def genType(self, typeinfo, name, alias):
Dave Houlton413a6782018-05-22 13:01:54 -0600449 # record the name/alias pair
450 if alias != None:
451 self.alias_dict[name]=alias
Mike Schuchardtf375c7c2017-12-28 11:23:48 -0700452 OutputGenerator.genType(self, typeinfo, name, alias)
Mark Lobodzinski85672672016-10-13 08:36:42 -0600453 typeElem = typeinfo.elem
Mark Lobodzinski87017df2018-05-30 11:29:24 -0600454 # If the type is a struct type, traverse the embedded <member> tags generating a structure. Otherwise, emit the tag text.
Mark Lobodzinski85672672016-10-13 08:36:42 -0600455 category = typeElem.get('category')
456 if (category == 'struct' or category == 'union'):
Mike Schuchardtf375c7c2017-12-28 11:23:48 -0700457 self.genStruct(typeinfo, name, alias)
Mark Lobodzinski85672672016-10-13 08:36:42 -0600458 elif (category == 'handle'):
459 self.handleTypes.add(name)
460 elif (category == 'bitmask'):
461 self.flags.add(name)
Chris Forbes78ea32d2016-11-28 11:14:17 +1300462 self.newFlags.add(name)
Mark Lobodzinski85672672016-10-13 08:36:42 -0600463 elif (category == 'define'):
464 if name == 'VK_HEADER_VERSION':
465 nameElem = typeElem.find('name')
466 self.headerVersion = noneStr(nameElem.tail).strip()
467 #
468 # Struct parameter check generation.
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600469 # This is a special case of the <type> tag where the contents are interpreted as a set of <member> tags instead of freeform C
470 # type declarations. The <member> tags are just like <param> tags - they are a declaration of a struct or union member.
471 # Only simple member declarations are supported (no nested structs etc.)
Mike Schuchardtf375c7c2017-12-28 11:23:48 -0700472 def genStruct(self, typeinfo, typeName, alias):
Dave Houlton413a6782018-05-22 13:01:54 -0600473 # alias has already been recorded in genType, above
Mike Schuchardtf375c7c2017-12-28 11:23:48 -0700474 OutputGenerator.genStruct(self, typeinfo, typeName, alias)
Mark Lobodzinski85672672016-10-13 08:36:42 -0600475 conditions = self.structMemberValidationConditions[typeName] if typeName in self.structMemberValidationConditions else None
476 members = typeinfo.elem.findall('.//member')
Mark Lobodzinskia1368552018-05-04 16:03:28 -0600477 if self.featureExtraProtect is not None:
478 self.struct_feature_protect[typeName] = self.featureExtraProtect
Mark Lobodzinski85672672016-10-13 08:36:42 -0600479 #
480 # Iterate over members once to get length parameters for arrays
481 lens = set()
482 for member in members:
483 len = self.getLen(member)
484 if len:
485 lens.add(len)
486 #
487 # Generate member info
488 membersInfo = []
489 for member in members:
490 # Get the member's type and name
491 info = self.getTypeNameTuple(member)
492 type = info[0]
493 name = info[1]
494 stypeValue = ''
495 cdecl = self.makeCParamDecl(member, 0)
496 # Process VkStructureType
497 if type == 'VkStructureType':
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600498 # Extract the required struct type value from the comments embedded in the original text defining the
499 # 'typeinfo' element
Mark Lobodzinski85672672016-10-13 08:36:42 -0600500 rawXml = etree.tostring(typeinfo.elem).decode('ascii')
501 result = re.search(r'VK_STRUCTURE_TYPE_\w+', rawXml)
502 if result:
503 value = result.group(0)
504 else:
505 value = self.genVkStructureType(typeName)
506 # Store the required type value
507 self.structTypes[typeName] = self.StructType(name=name, value=value)
508 #
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600509 # Store pointer/array/string info -- Check for parameter name in lens set
Mark Lobodzinski85672672016-10-13 08:36:42 -0600510 iscount = False
511 if name in lens:
512 iscount = True
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600513 # The pNext members are not tagged as optional, but are treated as optional for parameter NULL checks. Static array
514 # members are also treated as optional to skip NULL pointer validation, as they won't be NULL.
Mark Lobodzinski85672672016-10-13 08:36:42 -0600515 isstaticarray = self.paramIsStaticArray(member)
516 isoptional = False
517 if self.paramIsOptional(member) or (name == 'pNext') or (isstaticarray):
518 isoptional = True
Dustin Gravesce68f082017-03-30 15:42:16 -0600519 # Determine if value should be ignored by code generation.
520 noautovalidity = False
521 if (member.attrib.get('noautovalidity') is not None) or ((typeName in self.structMemberBlacklist) and (name in self.structMemberBlacklist[typeName])):
522 noautovalidity = True
Mark Lobodzinskia1368552018-05-04 16:03:28 -0600523 structextends = False
Mark Lobodzinski85672672016-10-13 08:36:42 -0600524 membersInfo.append(self.CommandParam(type=type, name=name,
525 ispointer=self.paramIsPointer(member),
526 isstaticarray=isstaticarray,
527 isbool=True if type == 'VkBool32' else False,
528 israngedenum=True if type in self.enumRanges else False,
529 isconst=True if 'const' in cdecl else False,
530 isoptional=isoptional,
531 iscount=iscount,
Dustin Gravesce68f082017-03-30 15:42:16 -0600532 noautovalidity=noautovalidity,
Mark Lobodzinski85672672016-10-13 08:36:42 -0600533 len=self.getLen(member),
Mike Schuchardta40d0b02017-07-23 12:47:47 -0600534 extstructs=self.registry.validextensionstructs[typeName] if name == 'pNext' else None,
Mark Lobodzinski85672672016-10-13 08:36:42 -0600535 condition=conditions[name] if conditions and name in conditions else None,
536 cdecl=cdecl))
Mark Lobodzinskia1368552018-05-04 16:03:28 -0600537 # If this struct extends another, keep its name in list for further processing
538 if typeinfo.elem.attrib.get('structextends') is not None:
539 self.structextends_list.append(typeName)
540 # Returnedonly structs should have most of their members ignored -- on entry, we only care about validating the sType and
541 # pNext members. Everything else will be overwritten by the callee.
542 if typeinfo.elem.attrib.get('returnedonly') is not None:
Mark Lobodzinskia1368552018-05-04 16:03:28 -0600543 self.returnedonly_structs.append(typeName)
544 membersInfo = [m for m in membersInfo if m.name in ('sType', 'pNext')]
Mark Lobodzinski85672672016-10-13 08:36:42 -0600545 self.structMembers.append(self.StructMemberData(name=typeName, members=membersInfo))
546 #
Mark Lobodzinskif31e0422017-07-25 14:29:42 -0600547 # Capture group (e.g. C "enum" type) info to be used for param check code generation.
Mark Lobodzinski85672672016-10-13 08:36:42 -0600548 # These are concatenated together with other types.
Mike Schuchardtf375c7c2017-12-28 11:23:48 -0700549 def genGroup(self, groupinfo, groupName, alias):
Dave Houlton413a6782018-05-22 13:01:54 -0600550 # record the name/alias pair
551 if alias != None:
552 self.alias_dict[groupName]=alias
Mike Schuchardtf375c7c2017-12-28 11:23:48 -0700553 OutputGenerator.genGroup(self, groupinfo, groupName, alias)
Mark Lobodzinski85672672016-10-13 08:36:42 -0600554 groupElem = groupinfo.elem
Mark Lobodzinski85672672016-10-13 08:36:42 -0600555 # Store the sType values
556 if groupName == 'VkStructureType':
557 for elem in groupElem.findall('enum'):
558 self.stypes.append(elem.get('name'))
559 elif 'FlagBits' in groupName:
560 bits = []
561 for elem in groupElem.findall('enum'):
Shannon McPherson533a66c2018-08-21 12:09:25 -0600562 if elem.get('supported') != 'disabled':
563 bits.append(elem.get('name'))
Mark Lobodzinski85672672016-10-13 08:36:42 -0600564 if bits:
565 self.flagBits[groupName] = bits
566 else:
567 # Determine if begin/end ranges are needed (we don't do this for VkStructureType, which has a more finely grained check)
568 expandName = re.sub(r'([0-9a-z_])([A-Z0-9][^A-Z0-9]?)',r'\1_\2',groupName).upper()
569 expandPrefix = expandName
570 expandSuffix = ''
571 expandSuffixMatch = re.search(r'[A-Z][A-Z]+$',groupName)
572 if expandSuffixMatch:
573 expandSuffix = '_' + expandSuffixMatch.group()
574 # Strip off the suffix from the prefix
575 expandPrefix = expandName.rsplit(expandSuffix, 1)[0]
576 isEnum = ('FLAG_BITS' not in expandPrefix)
577 if isEnum:
578 self.enumRanges[groupName] = (expandPrefix + '_BEGIN_RANGE' + expandSuffix, expandPrefix + '_END_RANGE' + expandSuffix)
Mark Lobodzinskif31e0422017-07-25 14:29:42 -0600579 # Create definition for a list containing valid enum values for this enumerated type
580 enum_entry = 'const std::vector<%s> All%sEnums = {' % (groupName, groupName)
581 for enum in groupElem:
582 name = enum.get('name')
Mark Lobodzinski117d88f2017-07-27 12:09:08 -0600583 if name is not None and enum.get('supported') != 'disabled':
584 enum_entry += '%s, ' % name
Mark Lobodzinskif31e0422017-07-25 14:29:42 -0600585 enum_entry += '};\n'
586 self.enumValueLists += enum_entry
Mark Lobodzinski85672672016-10-13 08:36:42 -0600587 #
Mark Lobodzinskif31e0422017-07-25 14:29:42 -0600588 # Capture command parameter info to be used for param check code generation.
Mike Schuchardtf375c7c2017-12-28 11:23:48 -0700589 def genCmd(self, cmdinfo, name, alias):
Dave Houlton413a6782018-05-22 13:01:54 -0600590 # record the name/alias pair
591 if alias != None:
592 self.alias_dict[name]=alias
Mike Schuchardtf375c7c2017-12-28 11:23:48 -0700593 OutputGenerator.genCmd(self, cmdinfo, name, alias)
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600594 decls = self.makeCDecls(cmdinfo.elem)
595 typedef = decls[1]
596 typedef = typedef.split(')',1)[1]
597 if name not in self.blacklist:
Mark Lobodzinskib6b8bbd2017-02-08 14:37:15 -0700598 if (self.featureExtraProtect != None):
599 self.declarations += [ '#ifdef %s' % self.featureExtraProtect ]
600 self.intercepts += [ '#ifdef %s' % self.featureExtraProtect ]
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600601 if (name not in self.validate_only):
602 self.func_pointers += '#ifdef %s\n' % self.featureExtraProtect
603 self.typedefs += '#ifdef %s\n' % self.featureExtraProtect
604 if (name not in self.validate_only):
605 self.typedefs += 'typedef bool (*PFN_manual_%s)%s\n' % (name, typedef)
Mark Lobodzinski78a12a92017-08-08 14:16:51 -0600606 self.func_pointers += ' {"%s", nullptr},\n' % name
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600607 self.intercepts += [ ' {"%s", (void*)%s},' % (name,name) ]
Mark Lobodzinskib6b8bbd2017-02-08 14:37:15 -0700608 # Strip off 'vk' from API name
609 self.declarations += [ '%s' % decls[0].replace("VKAPI_CALL vk", "VKAPI_CALL ") ]
610 if (self.featureExtraProtect != None):
611 self.intercepts += [ '#endif' ]
612 self.declarations += [ '#endif' ]
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600613 if (name not in self.validate_only):
614 self.func_pointers += '#endif\n'
615 self.typedefs += '#endif\n'
Mark Lobodzinski85672672016-10-13 08:36:42 -0600616 if name not in self.blacklist:
617 params = cmdinfo.elem.findall('param')
618 # Get list of array lengths
619 lens = set()
620 for param in params:
621 len = self.getLen(param)
622 if len:
623 lens.add(len)
624 # Get param info
625 paramsInfo = []
626 for param in params:
627 paramInfo = self.getTypeNameTuple(param)
628 cdecl = self.makeCParamDecl(param, 0)
629 # Check for parameter name in lens set
630 iscount = False
631 if paramInfo[1] in lens:
632 iscount = True
633 paramsInfo.append(self.CommandParam(type=paramInfo[0], name=paramInfo[1],
634 ispointer=self.paramIsPointer(param),
635 isstaticarray=self.paramIsStaticArray(param),
636 isbool=True if paramInfo[0] == 'VkBool32' else False,
637 israngedenum=True if paramInfo[0] in self.enumRanges else False,
638 isconst=True if 'const' in cdecl else False,
639 isoptional=self.paramIsOptional(param),
640 iscount=iscount,
641 noautovalidity=True if param.attrib.get('noautovalidity') is not None else False,
642 len=self.getLen(param),
643 extstructs=None,
644 condition=None,
645 cdecl=cdecl))
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600646 # Save return value information, if any
647 result_type = ''
648 resultinfo = cmdinfo.elem.find('proto/type')
649 if (resultinfo != None and resultinfo.text != 'void'):
650 result_type = resultinfo.text
651 self.commands.append(self.CommandData(name=name, params=paramsInfo, cdecl=self.makeCDecls(cmdinfo.elem)[0], extension_type=self.extension_type, result=result_type))
Mark Lobodzinski85672672016-10-13 08:36:42 -0600652 #
653 # Check if the parameter passed in is a pointer
654 def paramIsPointer(self, param):
655 ispointer = 0
656 paramtype = param.find('type')
657 if (paramtype.tail is not None) and ('*' in paramtype.tail):
658 ispointer = paramtype.tail.count('*')
659 elif paramtype.text[:4] == 'PFN_':
660 # Treat function pointer typedefs as a pointer to a single value
661 ispointer = 1
662 return ispointer
663 #
664 # Check if the parameter passed in is a static array
665 def paramIsStaticArray(self, param):
666 isstaticarray = 0
667 paramname = param.find('name')
668 if (paramname.tail is not None) and ('[' in paramname.tail):
669 isstaticarray = paramname.tail.count('[')
670 return isstaticarray
671 #
672 # Check if the parameter passed in is optional
673 # Returns a list of Boolean values for comma separated len attributes (len='false,true')
674 def paramIsOptional(self, param):
675 # See if the handle is optional
676 isoptional = False
677 # Simple, if it's optional, return true
678 optString = param.attrib.get('optional')
679 if optString:
680 if optString == 'true':
681 isoptional = True
682 elif ',' in optString:
683 opts = []
684 for opt in optString.split(','):
685 val = opt.strip()
686 if val == 'true':
687 opts.append(True)
688 elif val == 'false':
689 opts.append(False)
690 else:
691 print('Unrecognized len attribute value',val)
692 isoptional = opts
693 return isoptional
694 #
695 # Check if the handle passed in is optional
696 # Uses the same logic as ValidityOutputGenerator.isHandleOptional
697 def isHandleOptional(self, param, lenParam):
698 # Simple, if it's optional, return true
699 if param.isoptional:
700 return True
701 # If no validity is being generated, it usually means that validity is complex and not absolute, so let's say yes.
702 if param.noautovalidity:
703 return True
704 # If the parameter is an array and we haven't already returned, find out if any of the len parameters are optional
705 if lenParam and lenParam.isoptional:
706 return True
707 return False
708 #
709 # Generate a VkStructureType based on a structure typename
710 def genVkStructureType(self, typename):
711 # Add underscore between lowercase then uppercase
712 value = re.sub('([a-z0-9])([A-Z])', r'\1_\2', typename)
Mark Young39389872017-01-19 21:10:49 -0700713 value = value.replace('D3_D12', 'D3D12')
Shannon McPherson7bd85752018-09-10 11:13:02 -0600714 value = value.replace('ASTCDecode', 'ASTC_Decode')
Mark Young39389872017-01-19 21:10:49 -0700715 value = value.replace('Device_IDProp', 'Device_ID_Prop')
Mark Lobodzinskia1368552018-05-04 16:03:28 -0600716 value = value.replace('LODGather', 'LOD_Gather')
Mark Lobodzinski9ddf9282018-05-31 13:59:59 -0600717 value = value.replace('Features2', 'FEATURES_2')
718 value = value.replace('e16_Bit', 'E_16BIT')
Mark Lobodzinskidbd5c0f2018-07-16 12:03:07 -0600719 value = value.replace('e8_Bit', 'E_8BIT')
Jeff Bolze54ae892018-09-08 12:16:29 -0500720 value = value.replace('ASTCDecode', 'ASTC_Decode')
Mark Lobodzinski85672672016-10-13 08:36:42 -0600721 # Change to uppercase
722 value = value.upper()
723 # Add STRUCTURE_TYPE_
724 return re.sub('VK_', 'VK_STRUCTURE_TYPE_', value)
725 #
726 # Get the cached VkStructureType value for the specified struct typename, or generate a VkStructureType
727 # value assuming the struct is defined by a different feature
Mark Lobodzinski9ddf9282018-05-31 13:59:59 -0600728 # TODO: The structTypes list gets built incrementally -- half the time, the sType you're looking for is not yet in the list.
729 # The list needs to be built up-front, probably by accessing the XML directly, or by rewriting the generator.
Mark Lobodzinski85672672016-10-13 08:36:42 -0600730 def getStructType(self, typename):
731 value = None
732 if typename in self.structTypes:
733 value = self.structTypes[typename].value
734 else:
735 value = self.genVkStructureType(typename)
736 self.logMsg('diag', 'ParameterValidation: Generating {} for {} structure type that was not defined by the current feature'.format(value, typename))
737 return value
738 #
739 # Retrieve the value of the len tag
740 def getLen(self, param):
741 result = None
742 len = param.attrib.get('len')
743 if len and len != 'null-terminated':
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600744 # For string arrays, 'len' can look like 'count,null-terminated', indicating that we have a null terminated array of
745 # strings. We strip the null-terminated from the 'len' field and only return the parameter specifying the string count
Mark Lobodzinski85672672016-10-13 08:36:42 -0600746 if 'null-terminated' in len:
747 result = len.split(',')[0]
748 else:
749 result = len
750 result = str(result).replace('::', '->')
751 return result
752 #
753 # Retrieve the type and name for a parameter
754 def getTypeNameTuple(self, param):
755 type = ''
756 name = ''
757 for elem in param:
758 if elem.tag == 'type':
759 type = noneStr(elem.text)
760 elif elem.tag == 'name':
761 name = noneStr(elem.text)
762 return (type, name)
763 #
764 # Find a named parameter in a parameter list
765 def getParamByName(self, params, name):
766 for param in params:
767 if param.name == name:
768 return param
769 return None
770 #
771 # Extract length values from latexmath. Currently an inflexible solution that looks for specific
772 # patterns that are found in vk.xml. Will need to be updated when new patterns are introduced.
773 def parseLateXMath(self, source):
774 name = 'ERROR'
775 decoratedName = 'ERROR'
776 if 'mathit' in source:
Mark Lobodzinski36c33862017-02-13 10:15:53 -0700777 # Matches expressions similar to 'latexmath:[\lceil{\mathit{rasterizationSamples} \over 32}\rceil]'
778 match = re.match(r'latexmath\s*\:\s*\[\s*\\l(\w+)\s*\{\s*\\mathit\s*\{\s*(\w+)\s*\}\s*\\over\s*(\d+)\s*\}\s*\\r(\w+)\s*\]', source)
Mark Lobodzinski85672672016-10-13 08:36:42 -0600779 if not match or match.group(1) != match.group(4):
780 raise 'Unrecognized latexmath expression'
781 name = match.group(2)
782 decoratedName = '{}({}/{})'.format(*match.group(1, 2, 3))
783 else:
Mark Lobodzinski36c33862017-02-13 10:15:53 -0700784 # Matches expressions similar to 'latexmath : [dataSize \over 4]'
Mark Young0f183a82017-02-28 09:58:04 -0700785 match = re.match(r'latexmath\s*\:\s*\[\s*(\w+)\s*\\over\s*(\d+)\s*\]', source)
Mark Lobodzinski85672672016-10-13 08:36:42 -0600786 name = match.group(1)
787 decoratedName = '{}/{}'.format(*match.group(1, 2))
788 return name, decoratedName
789 #
790 # Get the length paramater record for the specified parameter name
791 def getLenParam(self, params, name):
792 lenParam = None
793 if name:
794 if '->' in name:
795 # The count is obtained by dereferencing a member of a struct parameter
796 lenParam = self.CommandParam(name=name, iscount=True, ispointer=False, isbool=False, israngedenum=False, isconst=False,
Mark Lobodzinskia1368552018-05-04 16:03:28 -0600797 isstaticarray=None, isoptional=False, type=None, noautovalidity=False,
798 len=None, extstructs=None, condition=None, cdecl=None)
Mark Lobodzinski85672672016-10-13 08:36:42 -0600799 elif 'latexmath' in name:
800 lenName, decoratedName = self.parseLateXMath(name)
801 lenParam = self.getParamByName(params, lenName)
Mark Lobodzinski85672672016-10-13 08:36:42 -0600802 else:
803 lenParam = self.getParamByName(params, name)
804 return lenParam
805 #
806 # Convert a vulkan.h command declaration into a parameter_validation.h definition
807 def getCmdDef(self, cmd):
Mark Lobodzinski85672672016-10-13 08:36:42 -0600808 # Strip the trailing ';' and split into individual lines
809 lines = cmd.cdecl[:-1].split('\n')
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600810 cmd_hdr = '\n'.join(lines)
811 return cmd_hdr
Mark Lobodzinski85672672016-10-13 08:36:42 -0600812 #
813 # Generate the code to check for a NULL dereference before calling the
814 # validation function
815 def genCheckedLengthCall(self, name, exprs):
816 count = name.count('->')
817 if count:
818 checkedExpr = []
819 localIndent = ''
820 elements = name.split('->')
821 # Open the if expression blocks
822 for i in range(0, count):
823 checkedExpr.append(localIndent + 'if ({} != NULL) {{\n'.format('->'.join(elements[0:i+1])))
824 localIndent = self.incIndent(localIndent)
825 # Add the validation expression
826 for expr in exprs:
827 checkedExpr.append(localIndent + expr)
828 # Close the if blocks
829 for i in range(0, count):
830 localIndent = self.decIndent(localIndent)
831 checkedExpr.append(localIndent + '}\n')
832 return [checkedExpr]
833 # No if statements were required
834 return exprs
835 #
836 # Generate code to check for a specific condition before executing validation code
837 def genConditionalCall(self, prefix, condition, exprs):
838 checkedExpr = []
839 localIndent = ''
840 formattedCondition = condition.format(prefix)
841 checkedExpr.append(localIndent + 'if ({})\n'.format(formattedCondition))
842 checkedExpr.append(localIndent + '{\n')
843 localIndent = self.incIndent(localIndent)
844 for expr in exprs:
845 checkedExpr.append(localIndent + expr)
846 localIndent = self.decIndent(localIndent)
847 checkedExpr.append(localIndent + '}\n')
848 return [checkedExpr]
849 #
Mark Lobodzinski06954ea2017-06-21 12:21:45 -0600850 # Get VUID identifier from implicit VUID tag
Dave Houlton413a6782018-05-22 13:01:54 -0600851 def GetVuid(self, name, suffix):
852 vuid_string = 'VUID-%s-%s' % (name, suffix)
853 vuid = "kVUIDUndefined"
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600854 if '->' in vuid_string:
Dave Houlton413a6782018-05-22 13:01:54 -0600855 return vuid
856 if vuid_string in self.valid_vuids:
857 vuid = "\"%s\"" % vuid_string
Mark Lobodzinski06954ea2017-06-21 12:21:45 -0600858 else:
Dave Houlton413a6782018-05-22 13:01:54 -0600859 if name in self.alias_dict:
860 alias_string = 'VUID-%s-%s' % (self.alias_dict[name], suffix)
861 if alias_string in self.valid_vuids:
862 vuid = "\"%s\"" % vuid_string
Mark Lobodzinski06954ea2017-06-21 12:21:45 -0600863 return vuid
864 #
Mark Lobodzinski85672672016-10-13 08:36:42 -0600865 # Generate the sType check string
Mark Lobodzinski9cf24dd2017-06-28 14:23:22 -0600866 def makeStructTypeCheck(self, prefix, value, lenValue, valueRequired, lenValueRequired, lenPtrRequired, funcPrintName, lenPrintName, valuePrintName, postProcSpec, struct_type_name):
Mark Lobodzinski85672672016-10-13 08:36:42 -0600867 checkExpr = []
868 stype = self.structTypes[value.type]
Mark Lobodzinski59603552018-05-29 16:14:59 -0600869 vuid_name = struct_type_name if struct_type_name is not None else funcPrintName
870 stype_vuid = self.GetVuid(value.type, "sType-sType")
871 param_vuid = self.GetVuid(vuid_name, "%s-parameter" % value.name)
872
Mark Lobodzinski85672672016-10-13 08:36:42 -0600873 if lenValue:
874 # This is an array with a pointer to a count value
875 if lenValue.ispointer:
876 # When the length parameter is a pointer, there is an extra Boolean parameter in the function call to indicate if it is required
Mark Lobodzinski59603552018-05-29 16:14:59 -0600877 checkExpr.append('skip |= validate_struct_type_array(local_data->report_data, "{}", {ppp}"{ldn}"{pps}, {ppp}"{dn}"{pps}, "{sv}", {pf}{ln}, {pf}{vn}, {sv}, {}, {}, {}, {}, {});\n'.format(
878 funcPrintName, lenPtrRequired, lenValueRequired, valueRequired, stype_vuid, param_vuid, ln=lenValue.name, ldn=lenPrintName, dn=valuePrintName, vn=value.name, sv=stype.value, pf=prefix, **postProcSpec))
Mark Lobodzinski85672672016-10-13 08:36:42 -0600879 # This is an array with an integer count value
880 else:
Mark Lobodzinski59603552018-05-29 16:14:59 -0600881 checkExpr.append('skip |= validate_struct_type_array(local_data->report_data, "{}", {ppp}"{ldn}"{pps}, {ppp}"{dn}"{pps}, "{sv}", {pf}{ln}, {pf}{vn}, {sv}, {}, {}, {}, {});\n'.format(
882 funcPrintName, lenValueRequired, valueRequired, stype_vuid, param_vuid, ln=lenValue.name, ldn=lenPrintName, dn=valuePrintName, vn=value.name, sv=stype.value, pf=prefix, **postProcSpec))
Mark Lobodzinski85672672016-10-13 08:36:42 -0600883 # This is an individual struct
884 else:
Mark Lobodzinskia16ebc72018-06-15 14:47:39 -0600885 checkExpr.append('skip |= validate_struct_type(local_data->report_data, "{}", {ppp}"{}"{pps}, "{sv}", {}{vn}, {sv}, {}, {}, {});\n'.format(
886 funcPrintName, valuePrintName, prefix, valueRequired, param_vuid, stype_vuid, vn=value.name, sv=stype.value, vt=value.type, **postProcSpec))
Mark Lobodzinski85672672016-10-13 08:36:42 -0600887 return checkExpr
888 #
889 # Generate the handle check string
890 def makeHandleCheck(self, prefix, value, lenValue, valueRequired, lenValueRequired, funcPrintName, lenPrintName, valuePrintName, postProcSpec):
891 checkExpr = []
892 if lenValue:
893 if lenValue.ispointer:
894 # This is assumed to be an output array with a pointer to a count value
895 raise('Unsupported parameter validation case: Output handle array elements are not NULL checked')
896 else:
897 # This is an array with an integer count value
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600898 checkExpr.append('skip |= validate_handle_array(local_data->report_data, "{}", {ppp}"{ldn}"{pps}, {ppp}"{dn}"{pps}, {pf}{ln}, {pf}{vn}, {}, {});\n'.format(
Mark Lobodzinski85672672016-10-13 08:36:42 -0600899 funcPrintName, lenValueRequired, valueRequired, ln=lenValue.name, ldn=lenPrintName, dn=valuePrintName, vn=value.name, pf=prefix, **postProcSpec))
900 else:
901 # This is assumed to be an output handle pointer
902 raise('Unsupported parameter validation case: Output handles are not NULL checked')
903 return checkExpr
904 #
905 # Generate check string for an array of VkFlags values
906 def makeFlagsArrayCheck(self, prefix, value, lenValue, valueRequired, lenValueRequired, funcPrintName, lenPrintName, valuePrintName, postProcSpec):
907 checkExpr = []
908 flagBitsName = value.type.replace('Flags', 'FlagBits')
909 if not flagBitsName in self.flagBits:
910 raise('Unsupported parameter validation case: array of reserved VkFlags')
911 else:
912 allFlags = 'All' + flagBitsName
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600913 checkExpr.append('skip |= validate_flags_array(local_data->report_data, "{}", {ppp}"{}"{pps}, {ppp}"{}"{pps}, "{}", {}, {pf}{}, {pf}{}, {}, {});\n'.format(funcPrintName, lenPrintName, valuePrintName, flagBitsName, allFlags, lenValue.name, value.name, lenValueRequired, valueRequired, pf=prefix, **postProcSpec))
Mark Lobodzinski85672672016-10-13 08:36:42 -0600914 return checkExpr
915 #
916 # Generate pNext check string
Mark Lobodzinski3c828522017-06-26 13:05:57 -0600917 def makeStructNextCheck(self, prefix, value, funcPrintName, valuePrintName, postProcSpec, struct_type_name):
Mark Lobodzinski85672672016-10-13 08:36:42 -0600918 checkExpr = []
919 # Generate an array of acceptable VkStructureType values for pNext
920 extStructCount = 0
921 extStructVar = 'NULL'
922 extStructNames = 'NULL'
Dave Houlton413a6782018-05-22 13:01:54 -0600923 vuid = self.GetVuid(struct_type_name, "pNext-pNext")
Mark Lobodzinski85672672016-10-13 08:36:42 -0600924 if value.extstructs:
Mike Schuchardtc73d07e2017-07-12 10:10:01 -0600925 extStructVar = 'allowed_structs_{}'.format(struct_type_name)
926 extStructCount = 'ARRAY_SIZE({})'.format(extStructVar)
Mike Schuchardta40d0b02017-07-23 12:47:47 -0600927 extStructNames = '"' + ', '.join(value.extstructs) + '"'
928 checkExpr.append('const VkStructureType {}[] = {{ {} }};\n'.format(extStructVar, ', '.join([self.getStructType(s) for s in value.extstructs])))
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600929 checkExpr.append('skip |= validate_struct_pnext(local_data->report_data, "{}", {ppp}"{}"{pps}, {}, {}{}, {}, {}, GeneratedHeaderVersion, {});\n'.format(
Mark Lobodzinski3c828522017-06-26 13:05:57 -0600930 funcPrintName, valuePrintName, extStructNames, prefix, value.name, extStructCount, extStructVar, vuid, **postProcSpec))
Mark Lobodzinski85672672016-10-13 08:36:42 -0600931 return checkExpr
932 #
933 # Generate the pointer check string
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600934 def makePointerCheck(self, prefix, value, lenValue, valueRequired, lenValueRequired, lenPtrRequired, funcPrintName, lenPrintName, valuePrintName, postProcSpec, struct_type_name):
Mark Lobodzinski85672672016-10-13 08:36:42 -0600935 checkExpr = []
Mark Lobodzinskidead0b62017-06-28 13:22:03 -0600936 vuid_tag_name = struct_type_name if struct_type_name is not None else funcPrintName
Mark Lobodzinski85672672016-10-13 08:36:42 -0600937 if lenValue:
Dave Houlton413a6782018-05-22 13:01:54 -0600938 count_required_vuid = self.GetVuid(vuid_tag_name, "%s-arraylength" % (lenValue.name))
939 array_required_vuid = self.GetVuid(vuid_tag_name, "%s-parameter" % (value.name))
Mark Lobodzinski85672672016-10-13 08:36:42 -0600940 # This is an array with a pointer to a count value
941 if lenValue.ispointer:
942 # If count and array parameters are optional, there will be no validation
943 if valueRequired == 'true' or lenPtrRequired == 'true' or lenValueRequired == 'true':
944 # When the length parameter is a pointer, there is an extra Boolean parameter in the function call to indicate if it is required
Gabríel Arthúr Pétursson092b29b2018-03-21 22:44:11 +0000945 checkExpr.append('skip |= validate_array(local_data->report_data, "{}", {ppp}"{ldn}"{pps}, {ppp}"{dn}"{pps}, {pf}{ln}, &{pf}{vn}, {}, {}, {}, {}, {});\n'.format(
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600946 funcPrintName, lenPtrRequired, lenValueRequired, valueRequired, count_required_vuid, array_required_vuid, ln=lenValue.name, ldn=lenPrintName, dn=valuePrintName, vn=value.name, pf=prefix, **postProcSpec))
Mark Lobodzinski85672672016-10-13 08:36:42 -0600947 # This is an array with an integer count value
948 else:
949 # If count and array parameters are optional, there will be no validation
950 if valueRequired == 'true' or lenValueRequired == 'true':
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600951 if value.type != 'char':
Gabríel Arthúr Pétursson092b29b2018-03-21 22:44:11 +0000952 checkExpr.append('skip |= validate_array(local_data->report_data, "{}", {ppp}"{ldn}"{pps}, {ppp}"{dn}"{pps}, {pf}{ln}, &{pf}{vn}, {}, {}, {}, {});\n'.format(
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600953 funcPrintName, lenValueRequired, valueRequired, count_required_vuid, array_required_vuid, ln=lenValue.name, ldn=lenPrintName, dn=valuePrintName, vn=value.name, pf=prefix, **postProcSpec))
954 else:
955 # Arrays of strings receive special processing
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600956 checkExpr.append('skip |= validate_string_array(local_data->report_data, "{}", {ppp}"{ldn}"{pps}, {ppp}"{dn}"{pps}, {pf}{ln}, {pf}{vn}, {}, {}, {}, {});\n'.format(
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -0600957 funcPrintName, lenValueRequired, valueRequired, count_required_vuid, array_required_vuid, ln=lenValue.name, ldn=lenPrintName, dn=valuePrintName, vn=value.name, pf=prefix, **postProcSpec))
Mark Lobodzinski85672672016-10-13 08:36:42 -0600958 if checkExpr:
959 if lenValue and ('->' in lenValue.name):
960 # Add checks to ensure the validation call does not dereference a NULL pointer to obtain the count
961 checkExpr = self.genCheckedLengthCall(lenValue.name, checkExpr)
962 # This is an individual struct that is not allowed to be NULL
963 elif not value.isoptional:
964 # Function pointers need a reinterpret_cast to void*
Dave Houlton413a6782018-05-22 13:01:54 -0600965 ptr_required_vuid = self.GetVuid(vuid_tag_name, "%s-parameter" % (value.name))
Mark Lobodzinski85672672016-10-13 08:36:42 -0600966 if value.type[:4] == 'PFN_':
Dave Houlton413a6782018-05-22 13:01:54 -0600967 allocator_dict = {'pfnAllocation': '"VUID-VkAllocationCallbacks-pfnAllocation-00632"',
968 'pfnReallocation': '"VUID-VkAllocationCallbacks-pfnReallocation-00633"',
969 'pfnFree': '"VUID-VkAllocationCallbacks-pfnFree-00634"',
970 'pfnInternalAllocation': '"VUID-VkAllocationCallbacks-pfnInternalAllocation-00635"'
Mark Lobodzinski02fa1972017-06-28 14:46:14 -0600971 }
972 vuid = allocator_dict.get(value.name)
973 if vuid is not None:
Dave Houlton413a6782018-05-22 13:01:54 -0600974 ptr_required_vuid = vuid
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600975 checkExpr.append('skip |= validate_required_pointer(local_data->report_data, "{}", {ppp}"{}"{pps}, reinterpret_cast<const void*>({}{}), {});\n'.format(funcPrintName, valuePrintName, prefix, value.name, ptr_required_vuid, **postProcSpec))
Mark Lobodzinski85672672016-10-13 08:36:42 -0600976 else:
Mark Lobodzinskid4950072017-08-01 13:02:20 -0600977 checkExpr.append('skip |= validate_required_pointer(local_data->report_data, "{}", {ppp}"{}"{pps}, {}{}, {});\n'.format(funcPrintName, valuePrintName, prefix, value.name, ptr_required_vuid, **postProcSpec))
Mark Lobodzinski85672672016-10-13 08:36:42 -0600978 return checkExpr
979 #
Mark Lobodzinski87017df2018-05-30 11:29:24 -0600980 # Process struct member validation code, performing name substitution if required
Mark Lobodzinski85672672016-10-13 08:36:42 -0600981 def processStructMemberCode(self, line, funcName, memberNamePrefix, memberDisplayNamePrefix, postProcSpec):
982 # Build format specifier list
983 kwargs = {}
984 if '{postProcPrefix}' in line:
985 # If we have a tuple that includes a format string and format parameters, need to use ParameterName class
986 if type(memberDisplayNamePrefix) is tuple:
987 kwargs['postProcPrefix'] = 'ParameterName('
988 else:
989 kwargs['postProcPrefix'] = postProcSpec['ppp']
990 if '{postProcSuffix}' in line:
991 # If we have a tuple that includes a format string and format parameters, need to use ParameterName class
992 if type(memberDisplayNamePrefix) is tuple:
993 kwargs['postProcSuffix'] = ', ParameterName::IndexVector{{ {}{} }})'.format(postProcSpec['ppi'], memberDisplayNamePrefix[1])
994 else:
995 kwargs['postProcSuffix'] = postProcSpec['pps']
996 if '{postProcInsert}' in line:
997 # If we have a tuple that includes a format string and format parameters, need to use ParameterName class
998 if type(memberDisplayNamePrefix) is tuple:
999 kwargs['postProcInsert'] = '{}{}, '.format(postProcSpec['ppi'], memberDisplayNamePrefix[1])
1000 else:
1001 kwargs['postProcInsert'] = postProcSpec['ppi']
1002 if '{funcName}' in line:
1003 kwargs['funcName'] = funcName
1004 if '{valuePrefix}' in line:
1005 kwargs['valuePrefix'] = memberNamePrefix
1006 if '{displayNamePrefix}' in line:
1007 # Check for a tuple that includes a format string and format parameters to be used with the ParameterName class
1008 if type(memberDisplayNamePrefix) is tuple:
1009 kwargs['displayNamePrefix'] = memberDisplayNamePrefix[0]
1010 else:
1011 kwargs['displayNamePrefix'] = memberDisplayNamePrefix
1012
1013 if kwargs:
1014 # Need to escape the C++ curly braces
1015 if 'IndexVector' in line:
1016 line = line.replace('IndexVector{ ', 'IndexVector{{ ')
1017 line = line.replace(' }),', ' }}),')
1018 return line.format(**kwargs)
1019 return line
1020 #
Mark Lobodzinskia1368552018-05-04 16:03:28 -06001021 # Process struct member validation code, stripping metadata
1022 def ScrubStructCode(self, code):
1023 scrubbed_lines = ''
1024 for line in code:
1025 if 'validate_struct_pnext' in line:
1026 continue
1027 if 'allowed_structs' in line:
1028 continue
1029 if 'xml-driven validation' in line:
1030 continue
1031 line = line.replace('{postProcPrefix}', '')
1032 line = line.replace('{postProcSuffix}', '')
1033 line = line.replace('{postProcInsert}', '')
1034 line = line.replace('{funcName}', '')
1035 line = line.replace('{valuePrefix}', '')
1036 line = line.replace('{displayNamePrefix}', '')
1037 line = line.replace('{IndexVector}', '')
1038 line = line.replace('local_data->', '')
1039 scrubbed_lines += line
1040 return scrubbed_lines
1041 #
Mark Lobodzinski85672672016-10-13 08:36:42 -06001042 # Process struct validation code for inclusion in function or parent struct validation code
Mark Lobodzinski554cf372018-05-24 11:06:00 -06001043 def expandStructCode(self, item_type, funcName, memberNamePrefix, memberDisplayNamePrefix, indent, output, postProcSpec):
1044 lines = self.validatedStructs[item_type]
Mark Lobodzinski85672672016-10-13 08:36:42 -06001045 for line in lines:
1046 if output:
1047 output[-1] += '\n'
1048 if type(line) is list:
1049 for sub in line:
1050 output.append(self.processStructMemberCode(indent + sub, funcName, memberNamePrefix, memberDisplayNamePrefix, postProcSpec))
1051 else:
1052 output.append(self.processStructMemberCode(indent + line, funcName, memberNamePrefix, memberDisplayNamePrefix, postProcSpec))
1053 return output
1054 #
Mark Lobodzinski87017df2018-05-30 11:29:24 -06001055 # Process struct pointer/array validation code, performing name substitution if required
Mark Lobodzinski85672672016-10-13 08:36:42 -06001056 def expandStructPointerCode(self, prefix, value, lenValue, funcName, valueDisplayName, postProcSpec):
1057 expr = []
1058 expr.append('if ({}{} != NULL)\n'.format(prefix, value.name))
1059 expr.append('{')
1060 indent = self.incIndent(None)
1061 if lenValue:
1062 # Need to process all elements in the array
1063 indexName = lenValue.name.replace('Count', 'Index')
1064 expr[-1] += '\n'
Mark Young39389872017-01-19 21:10:49 -07001065 if lenValue.ispointer:
1066 # If the length value is a pointer, de-reference it for the count.
1067 expr.append(indent + 'for (uint32_t {iname} = 0; {iname} < *{}{}; ++{iname})\n'.format(prefix, lenValue.name, iname=indexName))
1068 else:
1069 expr.append(indent + 'for (uint32_t {iname} = 0; {iname} < {}{}; ++{iname})\n'.format(prefix, lenValue.name, iname=indexName))
Mark Lobodzinski85672672016-10-13 08:36:42 -06001070 expr.append(indent + '{')
1071 indent = self.incIndent(indent)
1072 # Prefix for value name to display in error message
Mark Lobodzinski6f82eb52016-12-05 07:38:41 -07001073 if value.ispointer == 2:
1074 memberNamePrefix = '{}{}[{}]->'.format(prefix, value.name, indexName)
1075 memberDisplayNamePrefix = ('{}[%i]->'.format(valueDisplayName), indexName)
1076 else:
1077 memberNamePrefix = '{}{}[{}].'.format(prefix, value.name, indexName)
1078 memberDisplayNamePrefix = ('{}[%i].'.format(valueDisplayName), indexName)
Mark Lobodzinski85672672016-10-13 08:36:42 -06001079 else:
1080 memberNamePrefix = '{}{}->'.format(prefix, value.name)
1081 memberDisplayNamePrefix = '{}->'.format(valueDisplayName)
Mark Lobodzinski85672672016-10-13 08:36:42 -06001082 # Expand the struct validation lines
Mark Lobodzinski554cf372018-05-24 11:06:00 -06001083 expr = self.expandStructCode(value.type, funcName, memberNamePrefix, memberDisplayNamePrefix, indent, expr, postProcSpec)
Mark Lobodzinski85672672016-10-13 08:36:42 -06001084 if lenValue:
1085 # Close if and for scopes
1086 indent = self.decIndent(indent)
1087 expr.append(indent + '}\n')
1088 expr.append('}\n')
1089 return expr
1090 #
1091 # Generate the parameter checking code
1092 def genFuncBody(self, funcName, values, valuePrefix, displayNamePrefix, structTypeName):
1093 lines = [] # Generated lines of code
1094 unused = [] # Unused variable names
1095 for value in values:
1096 usedLines = []
1097 lenParam = None
1098 #
1099 # Prefix and suffix for post processing of parameter names for struct members. Arrays of structures need special processing to include the array index in the full parameter name.
1100 postProcSpec = {}
1101 postProcSpec['ppp'] = '' if not structTypeName else '{postProcPrefix}'
1102 postProcSpec['pps'] = '' if not structTypeName else '{postProcSuffix}'
1103 postProcSpec['ppi'] = '' if not structTypeName else '{postProcInsert}'
1104 #
1105 # Generate the full name of the value, which will be printed in the error message, by adding the variable prefix to the value name
1106 valueDisplayName = '{}{}'.format(displayNamePrefix, value.name)
1107 #
Mark Lobodzinski87017df2018-05-30 11:29:24 -06001108 # Check for NULL pointers, ignore the in-out count parameters that
Mark Lobodzinski85672672016-10-13 08:36:42 -06001109 # will be validated with their associated array
1110 if (value.ispointer or value.isstaticarray) and not value.iscount:
Mark Lobodzinski85672672016-10-13 08:36:42 -06001111 # Parameters for function argument generation
Mark Lobodzinskia1368552018-05-04 16:03:28 -06001112 req = 'true' # Parameter cannot be NULL
Mark Lobodzinski85672672016-10-13 08:36:42 -06001113 cpReq = 'true' # Count pointer cannot be NULL
1114 cvReq = 'true' # Count value cannot be 0
1115 lenDisplayName = None # Name of length parameter to print with validation messages; parameter name with prefix applied
Mark Lobodzinski85672672016-10-13 08:36:42 -06001116 # Generate required/optional parameter strings for the pointer and count values
1117 if value.isoptional:
1118 req = 'false'
1119 if value.len:
1120 # The parameter is an array with an explicit count parameter
1121 lenParam = self.getLenParam(values, value.len)
1122 lenDisplayName = '{}{}'.format(displayNamePrefix, lenParam.name)
1123 if lenParam.ispointer:
1124 # Count parameters that are pointers are inout
1125 if type(lenParam.isoptional) is list:
1126 if lenParam.isoptional[0]:
1127 cpReq = 'false'
1128 if lenParam.isoptional[1]:
1129 cvReq = 'false'
1130 else:
1131 if lenParam.isoptional:
1132 cpReq = 'false'
1133 else:
1134 if lenParam.isoptional:
1135 cvReq = 'false'
1136 #
Mark Lobodzinski899338b2018-07-26 13:59:52 -06001137 # The parameter will not be processed when tagged as 'noautovalidity'
Mark Lobodzinski85672672016-10-13 08:36:42 -06001138 # For the pointer to struct case, the struct pointer will not be validated, but any
Mark Lobodzinski899338b2018-07-26 13:59:52 -06001139 # members not tagged as 'noautovalidity' will be validated
1140 # We special-case the custom allocator checks, as they are explicit but can be auto-generated.
1141 AllocatorFunctions = ['PFN_vkAllocationFunction', 'PFN_vkReallocationFunction', 'PFN_vkFreeFunction']
1142 if value.noautovalidity and value.type not in AllocatorFunctions:
Mark Lobodzinski85672672016-10-13 08:36:42 -06001143 # Log a diagnostic message when validation cannot be automatically generated and must be implemented manually
1144 self.logMsg('diag', 'ParameterValidation: No validation for {} {}'.format(structTypeName if structTypeName else funcName, value.name))
1145 else:
Mark Lobodzinski85672672016-10-13 08:36:42 -06001146 if value.type in self.structTypes:
Mark Lobodzinski87017df2018-05-30 11:29:24 -06001147 # If this is a pointer to a struct with an sType field, verify the type
Mark Lobodzinski9cf24dd2017-06-28 14:23:22 -06001148 usedLines += self.makeStructTypeCheck(valuePrefix, value, lenParam, req, cvReq, cpReq, funcName, lenDisplayName, valueDisplayName, postProcSpec, structTypeName)
Mark Lobodzinski85672672016-10-13 08:36:42 -06001149 # If this is an input handle array that is not allowed to contain NULL handles, verify that none of the handles are VK_NULL_HANDLE
1150 elif value.type in self.handleTypes and value.isconst and not self.isHandleOptional(value, lenParam):
1151 usedLines += self.makeHandleCheck(valuePrefix, value, lenParam, req, cvReq, funcName, lenDisplayName, valueDisplayName, postProcSpec)
1152 elif value.type in self.flags and value.isconst:
1153 usedLines += self.makeFlagsArrayCheck(valuePrefix, value, lenParam, req, cvReq, funcName, lenDisplayName, valueDisplayName, postProcSpec)
1154 elif value.isbool and value.isconst:
Mark Lobodzinskid4950072017-08-01 13:02:20 -06001155 usedLines.append('skip |= validate_bool32_array(local_data->report_data, "{}", {ppp}"{}"{pps}, {ppp}"{}"{pps}, {pf}{}, {pf}{}, {}, {});\n'.format(funcName, lenDisplayName, valueDisplayName, lenParam.name, value.name, cvReq, req, pf=valuePrefix, **postProcSpec))
Mark Lobodzinski85672672016-10-13 08:36:42 -06001156 elif value.israngedenum and value.isconst:
Mark Lobodzinskiaff801e2017-07-25 15:29:57 -06001157 enum_value_list = 'All%sEnums' % value.type
Mark Lobodzinskid4950072017-08-01 13:02:20 -06001158 usedLines.append('skip |= validate_ranged_enum_array(local_data->report_data, "{}", {ppp}"{}"{pps}, {ppp}"{}"{pps}, "{}", {}, {pf}{}, {pf}{}, {}, {});\n'.format(funcName, lenDisplayName, valueDisplayName, value.type, enum_value_list, lenParam.name, value.name, cvReq, req, pf=valuePrefix, **postProcSpec))
Mark Lobodzinski85672672016-10-13 08:36:42 -06001159 elif value.name == 'pNext':
Mark Lobodzinski9ddf9282018-05-31 13:59:59 -06001160 usedLines += self.makeStructNextCheck(valuePrefix, value, funcName, valueDisplayName, postProcSpec, structTypeName)
Mark Lobodzinski85672672016-10-13 08:36:42 -06001161 else:
Mark Lobodzinski1e707bd2017-06-28 10:54:55 -06001162 usedLines += self.makePointerCheck(valuePrefix, value, lenParam, req, cvReq, cpReq, funcName, lenDisplayName, valueDisplayName, postProcSpec, structTypeName)
Mark Lobodzinski85672672016-10-13 08:36:42 -06001163 # If this is a pointer to a struct (input), see if it contains members that need to be checked
Mark Lobodzinskia1368552018-05-04 16:03:28 -06001164 if value.type in self.validatedStructs:
1165 if value.isconst: # or value.type in self.returnedonly_structs:
1166 usedLines.append(self.expandStructPointerCode(valuePrefix, value, lenParam, funcName, valueDisplayName, postProcSpec))
1167 elif value.type in self.returnedonly_structs:
1168 usedLines.append(self.expandStructPointerCode(valuePrefix, value, lenParam, funcName, valueDisplayName, postProcSpec))
Mark Lobodzinski85672672016-10-13 08:36:42 -06001169 # Non-pointer types
1170 else:
Mark Lobodzinski85672672016-10-13 08:36:42 -06001171 # The parameter will not be processes when tagged as 'noautovalidity'
1172 # For the struct case, the struct type will not be validated, but any
Mark Lobodzinskia1368552018-05-04 16:03:28 -06001173 # members not tagged as 'noautovalidity' will be validated
Mark Lobodzinski85672672016-10-13 08:36:42 -06001174 if value.noautovalidity:
1175 # Log a diagnostic message when validation cannot be automatically generated and must be implemented manually
1176 self.logMsg('diag', 'ParameterValidation: No validation for {} {}'.format(structTypeName if structTypeName else funcName, value.name))
1177 else:
Mark Lobodzinski024b2822017-06-27 13:22:05 -06001178 vuid_name_tag = structTypeName if structTypeName is not None else funcName
Mark Lobodzinski85672672016-10-13 08:36:42 -06001179 if value.type in self.structTypes:
1180 stype = self.structTypes[value.type]
Dave Houlton413a6782018-05-22 13:01:54 -06001181 vuid = self.GetVuid(value.type, "sType-sType")
Mark Lobodzinskia16ebc72018-06-15 14:47:39 -06001182 undefined_vuid = '"kVUIDUndefined"'
1183 usedLines.append('skip |= validate_struct_type(local_data->report_data, "{}", {ppp}"{}"{pps}, "{sv}", &({}{vn}), {sv}, false, kVUIDUndefined, {});\n'.format(
Mark Lobodzinski06954ea2017-06-21 12:21:45 -06001184 funcName, valueDisplayName, valuePrefix, vuid, vn=value.name, sv=stype.value, vt=value.type, **postProcSpec))
Mark Lobodzinski85672672016-10-13 08:36:42 -06001185 elif value.type in self.handleTypes:
1186 if not self.isHandleOptional(value, None):
Mark Lobodzinskid4950072017-08-01 13:02:20 -06001187 usedLines.append('skip |= validate_required_handle(local_data->report_data, "{}", {ppp}"{}"{pps}, {}{});\n'.format(funcName, valueDisplayName, valuePrefix, value.name, **postProcSpec))
Mark Lobodzinski85672672016-10-13 08:36:42 -06001188 elif value.type in self.flags:
1189 flagBitsName = value.type.replace('Flags', 'FlagBits')
1190 if not flagBitsName in self.flagBits:
Dave Houlton413a6782018-05-22 13:01:54 -06001191 vuid = self.GetVuid(vuid_name_tag, "%s-zerobitmask" % (value.name))
Mark Lobodzinskid4950072017-08-01 13:02:20 -06001192 usedLines.append('skip |= validate_reserved_flags(local_data->report_data, "{}", {ppp}"{}"{pps}, {pf}{}, {});\n'.format(funcName, valueDisplayName, value.name, vuid, pf=valuePrefix, **postProcSpec))
Mark Lobodzinski85672672016-10-13 08:36:42 -06001193 else:
Mark Lobodzinskie643fcc2017-06-26 16:27:15 -06001194 if value.isoptional:
1195 flagsRequired = 'false'
Dave Houlton413a6782018-05-22 13:01:54 -06001196 vuid = self.GetVuid(vuid_name_tag, "%s-parameter" % (value.name))
Mark Lobodzinskie643fcc2017-06-26 16:27:15 -06001197 else:
1198 flagsRequired = 'true'
Dave Houlton413a6782018-05-22 13:01:54 -06001199 vuid = self.GetVuid(vuid_name_tag, "%s-requiredbitmask" % (value.name))
Mark Lobodzinski85672672016-10-13 08:36:42 -06001200 allFlagsName = 'All' + flagBitsName
Mark Lobodzinskid4950072017-08-01 13:02:20 -06001201 usedLines.append('skip |= validate_flags(local_data->report_data, "{}", {ppp}"{}"{pps}, "{}", {}, {pf}{}, {}, false, {});\n'.format(funcName, valueDisplayName, flagBitsName, allFlagsName, value.name, flagsRequired, vuid, pf=valuePrefix, **postProcSpec))
Mike Schuchardt47619c82017-05-31 09:14:22 -06001202 elif value.type in self.flagBits:
1203 flagsRequired = 'false' if value.isoptional else 'true'
1204 allFlagsName = 'All' + value.type
Dave Houlton413a6782018-05-22 13:01:54 -06001205 vuid = self.GetVuid(vuid_name_tag, "%s-parameter" % (value.name))
Mark Lobodzinskid4950072017-08-01 13:02:20 -06001206 usedLines.append('skip |= validate_flags(local_data->report_data, "{}", {ppp}"{}"{pps}, "{}", {}, {pf}{}, {}, true, {});\n'.format(funcName, valueDisplayName, value.type, allFlagsName, value.name, flagsRequired, vuid, pf=valuePrefix, **postProcSpec))
Mark Lobodzinski85672672016-10-13 08:36:42 -06001207 elif value.isbool:
Mark Lobodzinskid4950072017-08-01 13:02:20 -06001208 usedLines.append('skip |= validate_bool32(local_data->report_data, "{}", {ppp}"{}"{pps}, {}{});\n'.format(funcName, valueDisplayName, valuePrefix, value.name, **postProcSpec))
Mark Lobodzinski85672672016-10-13 08:36:42 -06001209 elif value.israngedenum:
Dave Houlton413a6782018-05-22 13:01:54 -06001210 vuid = self.GetVuid(vuid_name_tag, "%s-parameter" % (value.name))
Mark Lobodzinski74cb45f2017-07-25 15:10:29 -06001211 enum_value_list = 'All%sEnums' % value.type
Mark Lobodzinskid4950072017-08-01 13:02:20 -06001212 usedLines.append('skip |= validate_ranged_enum(local_data->report_data, "{}", {ppp}"{}"{pps}, "{}", {}, {}{}, {});\n'.format(funcName, valueDisplayName, value.type, enum_value_list, valuePrefix, value.name, vuid, **postProcSpec))
Mark Lobodzinski85672672016-10-13 08:36:42 -06001213 # If this is a struct, see if it contains members that need to be checked
1214 if value.type in self.validatedStructs:
1215 memberNamePrefix = '{}{}.'.format(valuePrefix, value.name)
1216 memberDisplayNamePrefix = '{}.'.format(valueDisplayName)
Mark Lobodzinski554cf372018-05-24 11:06:00 -06001217 usedLines.append(self.expandStructCode(value.type, funcName, memberNamePrefix, memberDisplayNamePrefix, '', [], postProcSpec))
Mark Lobodzinski85672672016-10-13 08:36:42 -06001218 # Append the parameter check to the function body for the current command
1219 if usedLines:
1220 # Apply special conditional checks
1221 if value.condition:
1222 usedLines = self.genConditionalCall(valuePrefix, value.condition, usedLines)
1223 lines += usedLines
1224 elif not value.iscount:
1225 # If no expression was generated for this value, it is unreferenced by the validation function, unless
1226 # it is an array count, which is indirectly referenced for array valiadation.
1227 unused.append(value.name)
Mark Lobodzinskid4950072017-08-01 13:02:20 -06001228 if not lines:
1229 lines.append('// No xml-driven validation\n')
Mark Lobodzinski85672672016-10-13 08:36:42 -06001230 return lines, unused
1231 #
1232 # Generate the struct member check code from the captured data
1233 def processStructMemberData(self):
1234 indent = self.incIndent(None)
1235 for struct in self.structMembers:
1236 #
1237 # The string returned by genFuncBody will be nested in an if check for a NULL pointer, so needs its indent incremented
1238 lines, unused = self.genFuncBody('{funcName}', struct.members, '{valuePrefix}', '{displayNamePrefix}', struct.name)
1239 if lines:
1240 self.validatedStructs[struct.name] = lines
1241 #
1242 # Generate the command param check code from the captured data
1243 def processCmdData(self):
1244 indent = self.incIndent(None)
1245 for command in self.commands:
Mark Lobodzinskid4950072017-08-01 13:02:20 -06001246 just_validate = False
1247 if command.name in self.validate_only:
1248 just_validate = True
Mark Lobodzinski85672672016-10-13 08:36:42 -06001249 # Skip first parameter if it is a dispatch handle (everything except vkCreateInstance)
1250 startIndex = 0 if command.name == 'vkCreateInstance' else 1
1251 lines, unused = self.genFuncBody(command.name, command.params[startIndex:], '', '', None)
Mark Lobodzinski3f10bfe2017-08-23 15:23:23 -06001252 # Cannot validate extension dependencies for device extension APIs having a physical device as their dispatchable object
Mike Schuchardtafd00482017-08-24 15:15:02 -06001253 if (command.name in self.required_extensions) and (self.extension_type != 'device' or command.params[0].type != 'VkPhysicalDevice'):
Mark Lobodzinskid8b7e022017-06-01 15:10:13 -06001254 ext_test = ''
Mike Schuchardtafd00482017-08-24 15:15:02 -06001255 for ext in self.required_extensions[command.name]:
Mark Lobodzinskid8b7e022017-06-01 15:10:13 -06001256 ext_name_define = ''
1257 ext_enable_name = ''
1258 for extension in self.registry.extensions:
1259 if extension.attrib['name'] == ext:
1260 ext_name_define = extension[0][1].get('name')
1261 ext_enable_name = ext_name_define.lower()
1262 ext_enable_name = re.sub('_extension_name', '', ext_enable_name)
1263 break
Mark Lobodzinskid4950072017-08-01 13:02:20 -06001264 ext_test = 'if (!local_data->extensions.%s) skip |= OutputExtensionError(local_data, "%s", %s);\n' % (ext_enable_name, command.name, ext_name_define)
Mark Lobodzinskid8b7e022017-06-01 15:10:13 -06001265 lines.insert(0, ext_test)
Mark Lobodzinski85672672016-10-13 08:36:42 -06001266 if lines:
1267 cmdDef = self.getCmdDef(command) + '\n'
Mark Lobodzinskid4950072017-08-01 13:02:20 -06001268 # For a validation-only routine, change the function declaration
1269 if just_validate:
1270 jv_def = '// Generated function handles validation only -- API definition is in non-generated source\n'
1271 jv_def += 'extern %s\n\n' % command.cdecl
1272 cmdDef = 'bool parameter_validation_' + cmdDef.split('VKAPI_CALL ',1)[1]
1273 if command.name == 'vkCreateInstance':
1274 cmdDef = cmdDef.replace('(\n', '(\n VkInstance instance,\n')
1275 cmdDef = jv_def + cmdDef
Mark Lobodzinski85672672016-10-13 08:36:42 -06001276 cmdDef += '{\n'
Mark Lobodzinski26112592017-05-30 12:02:17 -06001277
Gabríel Arthúr Péturssona3b5d672017-08-19 16:44:45 +00001278 # Add list of commands to skip -- just generate the routine signature and put the manual source in parameter_validation_utils.cpp
1279 if command.params[0].type in ["VkInstance", "VkPhysicalDevice"] or command.name == 'vkCreateInstance':
Mark Lobodzinskid4950072017-08-01 13:02:20 -06001280 map_name = 'instance_layer_data_map'
1281 map_type = 'instance_layer_data'
1282 else:
1283 map_name = 'layer_data_map'
1284 map_type = 'layer_data'
1285 instance_param = command.params[0].name
1286 if command.name == 'vkCreateInstance':
1287 instance_param = 'instance'
1288 layer_data = ' %s *local_data = GetLayerDataPtr(get_dispatch_key(%s), %s);\n' % (map_type, instance_param, map_name)
1289 cmdDef += layer_data
1290 cmdDef += '%sbool skip = false;\n' % indent
1291 if not just_validate:
1292 if command.result != '':
Jamie Madillfc315192017-11-08 14:11:26 -05001293 if command.result == "VkResult":
1294 cmdDef += indent + '%s result = VK_ERROR_VALIDATION_FAILED_EXT;\n' % command.result
1295 elif command.result == "VkBool32":
1296 cmdDef += indent + '%s result = VK_FALSE;\n' % command.result
1297 else:
1298 raise Exception("Unknown result type: " + command.result)
1299
Mark Lobodzinskid4950072017-08-01 13:02:20 -06001300 cmdDef += '%sstd::unique_lock<std::mutex> lock(global_lock);\n' % indent
Mark Lobodzinski85672672016-10-13 08:36:42 -06001301 for line in lines:
1302 cmdDef += '\n'
1303 if type(line) is list:
1304 for sub in line:
1305 cmdDef += indent + sub
1306 else:
1307 cmdDef += indent + line
1308 cmdDef += '\n'
Mark Lobodzinskid4950072017-08-01 13:02:20 -06001309 if not just_validate:
1310 # Generate parameter list for manual fcn and down-chain calls
1311 params_text = ''
1312 for param in command.params:
1313 params_text += '%s, ' % param.name
1314 params_text = params_text[:-2]
1315 # Generate call to manual function if its function pointer is non-null
Mark Lobodzinski78a12a92017-08-08 14:16:51 -06001316 cmdDef += '%sPFN_manual_%s custom_func = (PFN_manual_%s)custom_functions["%s"];\n' % (indent, command.name, command.name, command.name)
1317 cmdDef += '%sif (custom_func != nullptr) {\n' % indent
1318 cmdDef += ' %sskip |= custom_func(%s);\n' % (indent, params_text)
Mark Lobodzinskid4950072017-08-01 13:02:20 -06001319 cmdDef += '%s}\n\n' % indent
1320 # Release the validation lock
1321 cmdDef += '%slock.unlock();\n' % indent
1322 # Generate skip check and down-chain call
1323 cmdDef += '%sif (!skip) {\n' % indent
1324 down_chain_call = ' %s' % indent
1325 if command.result != '':
1326 down_chain_call += ' result = '
1327 # Generate down-chain API call
1328 api_call = '%s(%s);' % (command.name, params_text)
1329 down_chain_call += 'local_data->dispatch_table.%s\n' % api_call[2:]
1330 cmdDef += down_chain_call
1331 cmdDef += '%s}\n' % indent
1332 if command.result != '':
1333 cmdDef += '%sreturn result;\n' % indent
1334 else:
1335 cmdDef += '%sreturn skip;\n' % indent
Mark Lobodzinski85672672016-10-13 08:36:42 -06001336 cmdDef += '}\n'
Mark Lobodzinskid8b7e022017-06-01 15:10:13 -06001337 self.validation.append(cmdDef)