Source

glLoadGen / modules / Generate.lua

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
--[[ Exports a table containing the following functions:

---- Generate

It creates a header/source file pair in accord with a standard algorithm and with the options it is given. It is given the options generated from GetOptions.
]]

local Specs = require "Specs"
local Styles = require "Styles"
local LoadSpec = require "LoadLuaSpec"
local util = require "util"

local function WriteEnumsFromList(hFile, enumList, enumSeen, listName,
	options, spec, style, specData)
	local header = style.header
	
	for _, enum in ipairs(enumList) do
		if(not enumSeen[enum.name]) then
			header.WriteEnumDecl(hFile, enum,
				specData.enumtable, spec, options)
			enumSeen[enum.name] = listName
		else
			header.WriteEnumPrevDecl(hFile, enum,
				specData.enumtable, spec, options,
				enumSeen[enum.name])
		end
	end
end

local function WriteEnumsForExt(hFile, extName, enumSeen, options, spec,
	style, specData)
	local header = style.header
	
	if(#specData.extdefs[extName].enums > 0) then
		style.WriteSmallHeading(hFile, spec.ExtNamePrefix() .. extName)
		
		WriteEnumsFromList(hFile, specData.extdefs[extName].enums,
			enumSeen, extName, options, spec, style, specData)
		
		hFile:write("\n")
		return true
	end
	
	return false
end

local function GetCoreEnumerators(core, specData, spec, options, version)
	--Only remove from core profile.
	if(options.profile ~= "core") then
		return core.enums
	end

	local targetVersion = tonumber(options.version)
	local enumList = {};
	for i, enum in ipairs(core.enums) do
		local bShouldWrite = true;
		
		if(enum.removed and tonumber(enum.removed) <= targetVersion) then
			bShouldWrite = false
		end

		--Very oddball logic to handle ARB_tessellation_shader's
		--ability to bring back GL_QUADS.
		--Remove the enumeration if all of the following
		--	The enum is from a core extension
		--	That extension is not core for the version we're writing
		if(enum.extensions) then
			for _, ext in ipairs(enum.extensions) do
				if(specData.coreexts[ext] and
					tonumber(specData.coreexts[ext].version) <= targetVersion) then
					bShouldWrite = false
				end
			end
		end

		if(bShouldWrite) then
			enumList[#enumList + 1] = enum;
		end
	end
	return enumList
end

local function WriteEnumerators(hFile, options, spec, style, specData)
	local header = style.header

	local extSeen = {}
	local enumSeen = {}
	
	--For each extension, write its enumerators.
	for _, extName in ipairs(options.extensions) do
		if(not extSeen[extName]) then
			extSeen[extName] = true
			WriteEnumsForExt(hFile, extName, enumSeen,
				options, spec, style, specData)
		end
	end
	
	hFile:write("\n")
	
	--For each version we are told to export, write the enumerators.
	if(options.version) then
		style.WriteSmallHeading(hFile, "Core Enumerators")
	end
	local coreExts = spec.GetCoreExts()
	for _, version in ipairs(spec.GetVersions()) do
		if(tonumber(version) <= tonumber(options.version)) then
			--Write any core extensions for that version.
			if(coreExts[version]) then
				for _, extName in ipairs(coreExts[version]) do
					if(not extSeen[extName]) then
						extSeen[extName] = true
						WriteEnumsForExt(
							hFile, extName, enumSeen,
							options, spec, style, specData)
					end
				end
			end
			
			--Write the actual core enumerators.
			local enumList = GetCoreEnumerators(specData.coredefs[version],
				specData, spec, options, version)
				
			if(#enumList > 0) then
				style.WriteSmallHeading(hFile, "Version " .. version)
				
				WriteEnumsFromList(hFile, enumList, enumSeen,
					version, options, spec, style, specData)

				hFile:write("\n")
			end
		end
	end
end

local function WriteFuncDeclsFromList(hFile, funcList, funcSeen,
	listName, options, spec, style, specData)
	local header = style.header
	
	for _, func in ipairs(funcList) do
		if(not funcSeen[func.name]) then
			header.WriteFuncDecl(hFile, func, specData.typemap, spec, options)
			funcSeen[func.name] = listName
		end
	end
end

local function WriteFuncDeclsForExt(hFile, extName, funcSeen, options, spec,
	style, specData)
	local header = style.header
	if(#specData.extdefs[extName].funcs > 0) then
		style.WriteSmallHeading(hFile, spec.ExtNamePrefix() .. extName)
		
		header.WriteBeginExtFuncDeclBlock(hFile, extName, spec, options)
		WriteFuncDeclsFromList(hFile, specData.extdefs[extName].funcs,
			funcSeen, extName, options, spec, style, specData)
		header.WriteEndExtFuncDeclBlock(hFile, extName, spec, options)
		hFile:write("\n")
	end
end

local function GetCoreFunctions(core, specData, spec, options, version)
	--Only remove from core profile.
	if(options.profile ~= "core") then
		return core.funcs
	end

	local targetVersion = tonumber(options.version)
	local funcList = {};
	for i, func in ipairs(core.funcs) do
		local bShouldWrite = true;
		
		if(func.deprecated and tonumber(func.deprecated) <= targetVersion) then
			bShouldWrite = false
		end

		--Fortuantely, a function can't be both from a version and an extension
		if(func.category and not string.match(func.category, "^VERSION")) then
			bShouldWrite = false
		end

		if(bShouldWrite) then
			funcList[#funcList + 1] = func;
		end
	end
	return funcList
end

local function WriteFunctionDecls(hFile, options, spec, style, specData)
	local header = style.header
	style.WriteSmallHeading(hFile, "Extension Functions")

	local extSeen = {}
	local funcSeen = {}
	
	--For each extension, write their function declarations.
	for _, extName in ipairs(options.extensions) do
		if(not extSeen[extName]) then
			extSeen[extName] = true
			WriteFuncDeclsForExt(hFile, extName, funcSeen, options,
				spec, style, specData)
		end
	end
	
	--For each version we are told to export, write the Functions.
	if(options.version) then
		hFile:write("\n")
		style.WriteSmallHeading(hFile, "Core Functions")
	else
		--No version to export
		return
	end
	
	local coreExts = spec.GetCoreExts()
	for _, version in ipairs(spec.GetVersions()) do
		if(tonumber(version) <= tonumber(options.version)) then
			--Write any core extensions for that version.
			if(coreExts[version]) then
				for _, extName in ipairs(coreExts[version]) do
					if(not extSeen[extName]) then
						extSeen[extName] = true
						WriteFuncDeclsForExt(hFile, extName, funcSeen,
							options, spec, style, specData)
					end
				end
			end
			
			--Write the actual core functions, if any.
			local funcList = GetCoreFunctions(specData.coredefs[version],
				specData, spec, options, version)
				
			if(#funcList > 0) then
				style.WriteSmallHeading(hFile, "Version " .. version)
				
				WriteFuncDeclsFromList(hFile, funcList, funcSeen,
					version, options, spec, style, specData)

				hFile:write("\n")
			end
		end
	end
end

local function BuildHeader(options, spec, style, specData, basename)
	local header = style.header
	local hFile, filename = header.CreateFile(basename, options)
	
	--Start include-guards.
	header.WriteBeginIncludeGuard(hFile, spec, options)
	hFile:write("\n")
	
	--Spec-specific initialization comes next. Generally macros and #includes.
	hFile:rawwrite(spec.GetHeaderInit())
	hFile:write("\n")
	
	--Write the standard typedefs.
	header.WriteStdTypedefs(hFile, specData, spec, options)
	hFile:write("\n")
	
	--Write the typedefs from the spec.
	header.WriteSpecTypedefs(hFile, specData, spec, options)
	hFile:write("\n")
	
	--Write any declaration scoping start.
	header.WriteBeginDecl(hFile, spec, options)
	
	--Write the extension variable declarations.
	style.WriteLargeHeading(hFile, "Extension variable declarations")
	header.WriteBeginExtVarDeclBlock(hFile, spec, options)
	for _, ext in ipairs(options.extensions) do
		header.WriteExtVariableDecl(hFile, ext, specData, spec, options)
	end
	header.WriteEndExtVarDeclBlock(hFile, spec, options)
	hFile:write("\n")
	
	--Write all enumerators.
	style.WriteLargeHeading(hFile, "Enumerators")
	header.WriteBeginEnumDeclBlock(hFile, spec, options)
	WriteEnumerators(hFile, options, spec, style, specData)
	header.WriteEndEnumDeclBlock(hFile, spec, options)
	hFile:write("\n")
	
	--Write all function declarations
	style.WriteLargeHeading(hFile, "Functions")
	header.WriteBeginFuncDeclBlock(hFile, spec, options)
	WriteFunctionDecls(hFile, options, spec, style, specData)
	header.WriteEndFuncDeclBlock(hFile, spec, options)
	hFile:write("\n")
	
	--Write the function loading stuff.
	style.WriteLargeHeading(hFile, "Loading Functions")
	header.WriteBeginSysDeclBlock(hFile, spec, options)
	header.WriteUtilityDecls(hFile, spec, options)
	hFile:write("\n")
	header.WriteMainLoaderFuncDecl(hFile, spec, options)
	if(options.version) then
		hFile:write("\n")
		header.WriteVersioningFuncDecls(hFile, spec, options)
	end
	header.WriteEndSysDeclBlock(hFile, spec, options)

	
	--Write any declaration scoping end.
	header.WriteEndDecl(hFile, spec, options)
	
	--Ending includeguard.
	header.WriteEndIncludeGuard(hFile, spec, options)
	hFile:write("\n")
	hFile:close()
	
	return filename
end



local function WriteFuncDefsFromList(hFile, funcList, funcSeen,
	listName, options, spec, style, specData)
	local source = style.source
	
	local loaded = {}
	for _, func in ipairs(funcList) do
		if(not funcSeen[func.name]) then
			source.WriteFuncDef(hFile, func, specData.typemap, spec, options)
			funcSeen[func.name] = listName
			loaded[#loaded + 1] = func
		end
	end
	
	return loaded
end

local function WriteExtFuncLoaderFromList(hFile, funcList,
	options, spec, style, specData)
	local source = style.source
	
	for _, func in ipairs(funcList) do
		source.WriteExtFuncLoader(hFile, func, specData.typemap, spec, options)
	end
end

local function WriteCoreFuncLoaderFromList(hFile, funcList,
	options, spec, style, specData)
	local source = style.source
	
	for _, func in ipairs(funcList) do
		source.WriteCoreFuncLoader(hFile, func, specData.typemap, spec, options)
	end
end

local function WriteFuncDefsForExt(hFile, extName, funcSeen, options, spec,
	style, specData)
	local source = style.source

	if(#specData.extdefs[extName].funcs > 0) then
		style.WriteSmallHeading(hFile, spec.ExtNamePrefix() .. extName)
		
		source.WriteBeginExtFuncDefBlock(hFile, extName, spec, options)
		local loaded = WriteFuncDefsFromList(hFile,
			specData.extdefs[extName].funcs, funcSeen, extName,
			options, spec, style, specData)
		
		hFile:write("\n")
		source.WriteBeginExtLoaderBlock(hFile, extName, spec, options)
		WriteExtFuncLoaderFromList(hFile, loaded, options,
			spec, style, specData)
		source.WriteEndExtLoaderBlock(hFile, extName, spec, options)
		
		source.WriteEndExtFuncDefBlock(hFile, extName, spec, options)
		hFile:write("\n")
	end
end

local function WriteFuncDefsForCoreExt(hFile, extName, funcSeen, options, spec,
	style, specData)
	local source = style.source

	if(#specData.extdefs[extName].funcs > 0) then
		style.WriteSmallHeading(hFile, spec.ExtNamePrefix() .. extName)
		
		local loaded = WriteFuncDefsFromList(hFile,
			specData.extdefs[extName].funcs, funcSeen, extName,
			options, spec, style, specData)
		hFile:write("\n")
	end
end

local function WriteFunctionDefs(hFile, options, spec, style, specData)
	local source = style.source
	
	style.WriteSmallHeading(hFile, "Extension Functions")

	local extSeen = {}
	local funcSeen = {}
	
	--For each extension, write their function pointer definitions.
	for _, extName in ipairs(options.extensions) do
		if(not extSeen[extName]) then
			extSeen[extName] = true
			WriteFuncDefsForExt(hFile, extName, funcSeen, options,
				spec, style, specData)
		end
	end
	
	--For each version we are told to export, write the function pointer definitions.
	if(options.version) then
		hFile:write("\n")
		style.WriteSmallHeading(hFile, "Core Functions")
	
		--Write the core function definitions, maintaining a list of everything
		--that was written.
		local coreExts = spec.GetCoreExts()
		local bWrittenBeginCore = false
		for _, version in ipairs(spec.GetVersions()) do
			if(tonumber(version) <= tonumber(options.version)) then
				--Write any core extensions for that version.
				if(coreExts[version]) then
					for _, extName in ipairs(coreExts[version]) do
						if(not extSeen[extName]) then
							if(not bWrittenBeginCore) then
								source.WriteBeginCoreFuncDefBlock(hFile, options.version, spec, options)
								bWrittenBeginCore = true
							end
							extSeen[extName] = true
							WriteFuncDefsForCoreExt(hFile, extName, funcSeen,
								options, spec, style, specData)
						end
					end
				end
				
				--Write the actual core functions, if any.
				local funcList = GetCoreFunctions(specData.coredefs[version],
					specData, spec, options, version)
					
				if(#funcList > 0) then
					if(not bWrittenBeginCore) then
						source.WriteBeginCoreFuncDefBlock(hFile, options.version, spec, options)
						bWrittenBeginCore = true
					end
					style.WriteSmallHeading(hFile, "Version " .. version)
					
					WriteFuncDefsFromList(hFile, funcList, funcSeen,
						version, options, spec, style, specData)

					hFile:write("\n")
				end
			end
		end
		
		if(bWrittenBeginCore) then
			--Now, write the function that loads the core version. Include
			--ALL core extensions, not just the ones we wrote.
			--This allows us to build an accurate count of what core stuff is missing.
			source.WriteBeginCoreLoaderBlock(hFile, options.version, spec, options)
			for _, version in ipairs(spec.GetVersions()) do
				if(tonumber(version) <= tonumber(options.version)) then
					if(coreExts[version]) then
						source.WriteBeginExtFuncDefBlock(hFile, extName, spec, options)

						for _, extName in ipairs(coreExts[version]) do
							WriteCoreFuncLoaderFromList(hFile,
								specData.extdefs[extName].funcs,
								options, spec, style, specData)
						end
						
						source.WriteEndExtFuncDefBlock(hFile, extName, spec, options)

					end
					
					--Write the actual core functions, if any.
					local funcList = GetCoreFunctions(specData.coredefs[version],
						specData, spec, options, version)
						
					if(#funcList > 0) then
						WriteCoreFuncLoaderFromList(hFile,
							funcList, options, spec, style, specData)
					end
				end
			end
			source.WriteEndCoreLoaderBlock(hFile, options.version, spec, options)
			
			source.WriteEndCoreFuncDefBlock(hFile, options.version, spec, options)
		end
	end
	
	local function FindFuncName(funcName)
		for _, func in ipairs(specData.funcData.functions) do
			if(func.name == funcName) then
				return func
			end
		end
		
		return nil
	end
	
	--Write the function declaration needed to load the extension string.
	--But only if it's available in the spec data and hasn't already been written.
	local funcExtString = FindFuncName(spec.GetExtStringFuncName())
	if(funcExtString and not funcSeen[spec.GetExtStringFuncName()]) then
		hFile:write("\n")
		source.WriteGetExtStringFuncDef(hFile, funcExtString,
			specData.typemap, spec, options)
	end
end

local function BuildSource(options, spec, style, specData, basename,
	hdrFilename)
	local source = style.source
	local hFile, filename = source.CreateFile(basename, options)
	
	--Write the header inclusions
	source.WriteIncludes(hFile, spec, options)
	hFile:fmt('#include "%s"\n', hdrFilename:match("([^\\/]+)$"))
	hFile:write("\n")

	--Write the function that loads a function pointer, given a name.
	hFile:writeblock(spec.GetLoaderFunc())
	hFile:write("\n")
	
	--Write any definitions scoping start.
	source.WriteBeginDef(hFile, spec, options)
	
	--Write the extension variable definitions.
	style.WriteLargeHeading(hFile, "Extension variable definitions")
	source.WriteBeginExtVarDefBlock(hFile, spec, options)
	for _, ext in ipairs(options.extensions) do
		source.WriteExtVariableDef(hFile, ext, specData, spec, options)
	end
	source.WriteEndExtVarDefBlock(hFile, spec, options)
	hFile:write("\n")
	
	--Write all of the loader definitions.
	style.WriteLargeHeading(hFile, "Function Definitions and Loaders")
	WriteFunctionDefs(hFile, options, spec, style, specData)
	hFile:write("\n")
	

	source.WriteBeginSysDefBlock(hFile, spec, options)
	--Write utility definitions needed by the loader.
	source.WriteUtilityDefs(hFile, specData, spec, options)
	hFile:write "\n"
	
	--Write the main loading function which loads everything.
	source.WriteMainLoaderFunc(hFile, specData, spec, options)
	hFile:write "\n"
	
	--Write any additional functions that load other things.
   	if(options.version) then
        source.WriteVersioningFuncs(hFile, specData, spec, options)
        hFile:write "\n"
	end

	source.WriteEndSysDefBlock(hFile, spec, options)
	
	--Write any definitions scoping end.
	source.WriteEndDef(hFile, spec, options)

	return filename
end

local function Generate(options)
	--Load the spec data.
	local spec = Specs.GetSpec(options.spec)
	local specData = spec.LoadSpec()
	
	--Verify that every extension in `options.extensions` is a real extension.
	local badExts = {}
	for _, extName in ipairs(options.extensions) do
		if(not specData.extdefs[extName]) then
			badExts[#badExts + 1] = extName
		end
	end
	
	if(#badExts > 0) then
		io.stdout:write("The following extensions are not in the spec ", options.spec, ":\n")
		for _, extName in ipairs(badExts) do
			io.stdout:write("\t", extName, "\n")
		end
		return
	end

	--Extract the path and base-filename from the options.
	local simplename = options.outname:match("([^\\/]+)$")
	local dir = options.outname:match("^(.*[\\/])")
	dir = dir or "./"
	
	assert(simplename,
		"There is no filename in the path '" .. options.outname .. "'")

	local style = Styles.GetStyle(options.style)

	--Compute the filename, minus style-specific suffix.
	local basename = dir .. spec:FilePrefix() .. simplename
	
	local hdrFilename = BuildHeader(options, spec, style, specData, basename)
	local srcFilename = BuildSource(options, spec, style, specData, basename,
		hdrFilename)
end

return
{
	Generate = Generate,
}