glLoadGen / modules / CommonStyle.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
--[[Useful style utility functions. This file will contain commonly useful strings and functions that generate useful data.]]

local util = require "util"
local struct = require "Structure"

local common = {}

--Creates a tabbed file.
function common.CreateFile(filename, indent)
	return util.CreateFile(filename, indent)
end

--Retrieves the common typedefs used by OpenGL 1.1.
function common.GetStdTypedefs()
	return dofile(util.GetDataFilePath() .. "style_commontypedefs.lua")
end

--Writes passthru data from the specData, with proper indentation.
function common.WritePassthruData(hFile, strArray)
	for _, str in ipairs(strArray) do
		--unindent after #endif
		if(str:match("^#endif") or str:match("^#elif")) then
			hFile:dec()
		end
	
		hFile:write(str, "\n")
		
		--Indent after #if.
		if(str:match("^#if") or str:match("^#elif")) then
			hFile:inc()
		end
	end
end

function common.WriteExternCStart(hFile)
	hFile:write("#ifdef __cplusplus\n")
	hFile:write('extern "C" {\n')
	hFile:write("#endif /*__cplusplus*/\n")
	hFile:write("\n")
end

function common.WriteExternCEnd(hFile)
	hFile:write("#ifdef __cplusplus\n")
	hFile:write('}\n')
	hFile:write("#endif /*__cplusplus*/\n")
	hFile:write("\n")
end

--Determines the value of the enumeration.
local function ResolveEnumValue(enum, enumTable)
	if(enum.copy) then
		return common.ResolveEnumValue(enumTable[enum.value], enumTable),
			enum.value;
	else
		return enum.value;
	end
end
common.ResolveEnumValue = ResolveEnumValue

function common.GetCppEnumName(enum)
	--Note: some enumerators start with characters C++ forbids as initial
	--identifiers. If we detect such an enum, prefix it with `_`.
	local enumName = enum.name
	if(not enumName:match("^[a-zA-Z_]")) then
		enumName = "_" .. enumName
	end
	
	--Also, certain identifiers can need it because they conflict.
	local badIdent = {"TRUE", "FALSE", "NO_ERROR", "WAIT_FAILED", "DOMAIN"}
	for _, ident in ipairs(badIdent) do
		if(enumName == ident) then
			enumName = enumName .. "_"
			break
		end
	end
	
	return enumName
end

function common.GetNameLengthPadding(name, length)
	local numSpaces = length - #name
	if(numSpaces < 1) then
		numSpaces = 1
	end
	
	return string.rep(" ", numSpaces)
end

--Gets the return type for a function.
function common.GetFuncReturnType(func, typemap)
	return typemap[func["return"]] or func["return"]
end

function common.DoesFuncReturnSomething(func, typemap)
	local returnType = typemap[func["return"]] or func["return"]
	return (returnType == "void") or (returnType == "GLvoid")
end

local bIsKindPtr ={
	value = false,
	array = true,
	reference = true,
};

--Important due to name conflicts. Some names have to re-mapped to others.
--Won't really affect things.
local paramNameRemap = {
	near = "ren_near",
	far = "ren_far",
	array = "ren_array",
};

--Returns the parameter list as a string.
--Parameter list does not include parenthesis.
function common.GetFuncParamList(func, typemap, bWriteVarNames)
	local paramList = {}
	for i, param in ipairs(func.params) do
		local parameter = ""
		local paramType = typemap[param.type] or param.type;
		local paramName = "";
		if(bWriteVarNames) then paramName = param.name end
		if(paramNameRemap[paramName]) then paramName = paramNameRemap[paramName]end
		
		if(bIsKindPtr[param.kind]) then
			if(param.input) then
				--Input arrays are ALWAYS const.
				parameter = parameter .. "const ";
			end
			parameter = parameter .. string.format("%s *%s",
				paramType, paramName);
		else
			parameter = parameter .. string.format("%s %s",
				paramType, paramName);
		end
		paramList[#paramList + 1] = parameter
	end
	
	return table.concat(paramList, ", ");
end

--Get the list of parameter names, as a string ready to be put into ().
function common.GetFuncParamCallList(func, typemap)
	local paramList = {}
	for i, param in ipairs(func.params) do
		local paramName = param.name
		if(paramNameRemap[paramName]) then
			paramName = paramNameRemap[paramName]
		end

		paramList[#paramList + 1] = paramName
	end
	
	return table.concat(paramList, ", ");
end

--Retrieves the name of the function according to OpenGL.
function common.GetOpenGLFuncName(func, spec)
	return spec.FuncNamePrefix() .. func.name
end

function common.GetProcAddressName(spec)
	return spec.GetPtrLoaderFuncName()
end

function common.FixupIndexedList(specData, indexed)
	assert(indexed)
	for _, func in ipairs(specData.funcData.functions) do
		if(indexed[1] == func.name) then
			indexed[1] = func
		end
		if(indexed[3] == func.name) then
			indexed[3] = func
		end
	end
	for _, enum in ipairs(specData.enumerations) do
		if(indexed[2] == enum.name) then
			indexed[2] = enum
		end
		if(indexed[4] == enum.name) then
			indexed[4] = enum
		end
	end
end

function common.GetProcExtsFromExtListFunc(hFile, specData, spec, options,
							indexed, GetFuncPtrName, GetEnumName)
	return [[
static void ProcExtsFromExtList()
{
	GLint iLoop;
	GLint iNumExtensions = 0;
	]] .. GetFuncPtrName(indexed[1], spec, options)
	.. [[(]] .. GetEnumName(indexed[2], spec, options)
	.. [[, &iNumExtensions);

	for(iLoop = 0; iLoop < iNumExtensions; iLoop++)
	{
		const char *strExtensionName = (const char *)]] ..
		GetFuncPtrName(indexed[3], spec, options) ..
		[[(]] .. GetEnumName(indexed[4], spec, options) .. [[, iLoop);
		LoadExtByName(strExtensionName);
	}
}
]]
end

--You give it a function that takes a const char*.
function common.GetProcessExtsFromStringFunc(funcFormat, arguments)
	return [[
static void ProcExtsFromExtString(const char *strExtList]] .. (arguments or "") .. [[)
{
	size_t iExtListLen = strlen(strExtList);
	const char *strExtListEnd = strExtList + iExtListLen;
	const char *strCurrPos = strExtList;
	char strWorkBuff[256];

	while(*strCurrPos)
	{
		/*Get the extension at our position.*/
		int iStrLen = 0;
		const char *strEndStr = strchr(strCurrPos, ' ');
		int iStop = 0;
		if(strEndStr == NULL)
		{
			strEndStr = strExtListEnd;
			iStop = 1;
		}

		iStrLen = (int)((ptrdiff_t)strEndStr - (ptrdiff_t)strCurrPos);

		if(iStrLen > 255)
			return;

		strncpy(strWorkBuff, strCurrPos, iStrLen);
		strWorkBuff[iStrLen] = '\0';

		]] .. funcFormat:format("strWorkBuff") ..[[;

		strCurrPos = strEndStr + 1;
		if(iStop) break;
	}
}
]]
end

function common.WriteProcessExtsFromStringFunc(hFile, ...)
	hFile:writeblock(common.GetProcessExtsFromStringFunc(...))
end

function common.GetParseVersionFromString()
	return [[
static void ParseVersionFromString(int *pOutMajor, int *pOutMinor, const char *strVersion)
{
	const char *strDotPos = NULL;
	int iLength = 0;
	char strWorkBuff[10];
	*pOutMinor = 0;
	*pOutMajor = 0;

	strDotPos = strchr(strVersion, '.');
	if(!strDotPos)
		return;

	iLength = (int)((ptrdiff_t)strDotPos - (ptrdiff_t)strVersion);
	strncpy(strWorkBuff, strVersion, iLength);
	strWorkBuff[iLength] = '\0';

	*pOutMajor = atoi(strWorkBuff);
	strDotPos = strchr(strVersion + iLength + 1, ' ');
	if(!strDotPos)
	{
		/*No extra data. Take the whole rest of the string.*/
		strcpy(strWorkBuff, strVersion + iLength + 1);
	}
	else
	{
		/*Copy only up until the space.*/
		int iLengthMinor = (int)((ptrdiff_t)strDotPos - (ptrdiff_t)strVersion);
		iLengthMinor = iLengthMinor - (iLength + 1);
		strncpy(strWorkBuff, strVersion + iLength + 1, iLengthMinor);
		strWorkBuff[iLengthMinor] = '\0';
	}

	*pOutMinor = atoi(strWorkBuff);
}
]]
end

local function DeepCopyTable(tbl)
	local ret = {}
	for key, value in pairs(tbl) do
		if(type(value) == "table") then
			ret[key] = DeepCopyTable(value)
		else
			ret[key] = value
		end
	end
	return ret
end

function common.WriteCMappingTable(hFile, specData, spec,
							options, structName, varName, GetExtVariableName, GetExtLoaderFuncName)
	--Write the struct for the mapping table.
	hFile:write("typedef int (*PFN_LOADFUNCPOINTERS)();\n")
	hFile:fmt("typedef struct %s%sStrToExtMap_s\n",
		options.prefix, spec.DeclPrefix())
	hFile:write("{\n")
	hFile:inc()
	hFile:write("char *extensionName;\n")
	hFile:write("int *extensionVariable;\n")
	hFile:write("PFN_LOADFUNCPOINTERS LoadExtension;\n")
	hFile:dec()
	hFile:fmt("} %s;\n", structName)
	hFile:write "\n"
	
	--Write the mapping table itself.
	local arrayLength = #options.extensions
	if(arrayLength == 0) then arrayLength = 1 end
	hFile:fmt("static %s %s[%i] = {\n",
		structName,
		varName,
		arrayLength)
	hFile:inc()
	for _, extName in ipairs(options.extensions) do
		if(#specData.extdefs[extName].funcs > 0) then
			hFile:fmt('{"%s", &%s, %s},\n',
				spec.ExtNamePrefix() .. extName,
				GetExtVariableName(extName, spec, options),
				GetExtLoaderFuncName(extName, spec, options))
		else
			hFile:fmt('{"%s", &%s, NULL},\n',
				spec.ExtNamePrefix() .. extName,
				GetExtVariableName(extName, spec, options))
		end
	end
	--Because C is stupid, write bogus entry.
	if(#options.extensions == 0) then
		hFile:fmt('{"", NULL, NULL},\n')
	end
	hFile:dec()
	hFile:write("};\n")

	hFile:write "\n"
	hFile:fmt("static int g_extensionMapSize = %i;\n", #options.extensions);
end

function common.WriteCFindExtEntryFunc(hFile, specData, spec,
							options, structName, varName, sizeName)
	hFile:fmt("static %s *FindExtEntry(const char *extensionName)\n",
		structName)
	hFile:write("{\n")
	hFile:inc()
	hFile:write("int loop;\n")
	hFile:fmt("%s *currLoc = %s;\n",
		structName,
		varName)
	hFile:writeblock([[
for(loop = 0; loop < g_extensionMapSize; ++loop, ++currLoc)
{
	if(strcmp(extensionName, currLoc->extensionName) == 0)
		return currLoc;
}

return NULL;
]])
	hFile:dec()
	hFile:write("}\n")
end

function common.WriteCClearExtensionVarsFunc(hFile, specData, spec,
							options, GetExtVariableName, clearValue)
	hFile:fmt("static void ClearExtensionVars()\n")
	hFile:write("{\n")
	hFile:inc()
	for _, extName in ipairs(options.extensions) do
		hFile:fmt('%s = %s;\n',
			GetExtVariableName(extName, spec, options),
			clearValue)
	end
	hFile:dec()
	hFile:write("}\n")
	hFile:write "\n"
end

--Write a function that loads an extension by name. It is called when
--processing, so it should also set the extension variable based on the load.
function common.WriteCLoadExtByNameFunc(hFile, specData, spec,
							options, structName, successValue)
	hFile:writeblock([[
static void LoadExtByName(const char *extensionName)
{
	]] .. structName .. [[ *entry = NULL;
	entry = FindExtEntry(extensionName);
	if(entry)
	{
		if(entry->LoadExtension)
		{
			int numFailed = entry->LoadExtension();
			if(numFailed == 0)
			{
				*(entry->extensionVariable) = ]] ..
				successValue ..
				[[;
			}
			else
			{
				*(entry->extensionVariable) = ]] ..
				successValue ..
				[[ + numFailed;
			}
		}
		else
		{
			*(entry->extensionVariable) = ]] ..
			successValue ..
			[[;
		}
	}
}
]])
end

function common.WriteNamespaceBegin(hFile, namespace)
	hFile:fmt("namespace %s\n", namespace)
	hFile:write("{\n")
	hFile:inc()
end

function common.WriteNamespaceEnd(hFile)
	hFile:dec()
	hFile:write("}\n")
end


common.DeepCopyTable = DeepCopyTable

local my_struct =
{
	{ type="file", style="header", name="GetFilename",
		{ type="write", name="FilePreamble", optional=true} ,
		{ type="block", name="IncludeGuard(hFile, spec, options)",
			{ type="blank"},
			{ type="write", name="Init(hFile, spec, options)", },
			{ type="blank"},
			{ type="write", name="StdTypedefs(hFile, specData, options)",},
			{ type="blank"},
			{ type="write", name="SpecTypedefs(hFile, specData, options)",},
			{ type="blank"},
			{ type="block", name="Decl(hFile, spec, options)",
				{ type="block", name="ExtVarDecl(hFile, spec, options)",
					{ type="ext-iter",
						{ type="write", name="ExtVariableDecl(hFile, extName, specData, spec, options)" },
						{ type="blank", last=true },
					},
				},
				{ type="block", name="EnumDecl(hFile, spec, options)",
					{ type="enum-seen",
						{ type="ext-iter",
							{type="enum-iter",
								{ type="write", name="EnumDecl(hFile, enum, enumTable, spec, options, enumSeen)", },
								{ type="blank", last=true },
							},
						},
						{ type="version-iter",
							{ type="core-ext-cull-iter",
								{type="enum-iter",
									{ type="write", name="EnumDecl(hFile, enum, enumTable, spec, options, enumSeen)", },
									{ type="blank", last=true },
								},
							},
							{type="enum-iter",
								{ type="write", name="EnumDecl(hFile, enum, enumTable, spec, options, enumSeen)", },
								{ type="blank", last=true },
							},
						},
					},
				},
				{ type="block", name="FuncDecl(hFile, spec, options)",
					{ type="func-seen",
						{ type="ext-iter",
							{ type="block", name="ExtFuncDecl(hFile, extName, spec, options)", cond="func-iter",
								{type="func-iter",
									{ type="write", name="FuncDecl(hFile, func, typemap, spec, options, funcSeen)", },
								},
							},
							{ type="blank"},
						},
						{ type="version-iter",
							{ type="core-ext-cull-iter",
								{ type="block", name="ExtFuncDecl(hFile, extName, spec, options)", cond="func-iter",
									{type="func-iter",
										{ type="write", name="FuncDecl(hFile, func, typemap, spec, options, funcSeen)", },
									},
								},
								{ type="blank"},
							},
							{type="func-iter",
								{ type="write", name="FuncDecl(hFile, func, typemap, spec, options, funcSeen)", },
								{ type="blank", last=true },
							},
						},
					},
				},
				{ type="block", name="SysDecl(hFile, spec, options)",
					{ type="write", name="UtilityDecls(hFile, spec, options)",},
					{ type="blank" },
					{ type="write", name="MainLoaderFuncDecl(hFile, spec, options)",},
					{ type="blank" },
					{ type="write", name="VersioningFuncDecls(hFile, spec, options)",},
					{ type="blank" },
				},
			},
		},
	},
	{ type="file", style="source", name="GetFilename",
		{ type="write", name="FilePreamble", optional=true} ,
		{ type="write", name="Includes(hFile, basename, spec, options)",},
		{ type="blank"},
		{ type="write", name="LoaderData(hFile, spec, options)",},
		{ type="blank"},
		{ type="block", name="Def(hFile, spec, options)",
			{ type="block", name="ExtVarDef(hFile, spec, options)",
				{ type="ext-iter",
					{ type="write", name="ExtVariableDef(hFile, extName, specData, spec, options)",},
					{ type="blank", last=true},
				},
			},
			{ type="func-seen",
				{ type="ext-iter",
					{ type="block", name="ExtFuncDef(hFile, extName, spec, options)", cond="func-iter",
						{ type="func-iter",
							{ type="write", name="FuncDef(hFile, func, typemap, spec, options, funcSeen)", },
						},
						{ type="blank"},
						{ type="block", name="ExtLoader(hFile, extName, spec, options)",
							{ type="func-iter",
								{ type="write", name="ExtFuncLoader(hFile, func, typemap, spec, options)", }
							}
						},
						{ type="blank"},
					},
				},
				{ type="block", name="CoreFuncDef(hFile, spec, options)",
					cond="core-funcs",
					{ type="version-iter",
						{ type="core-ext-cull-iter",
							{ type="block", name="ExtFuncDef(hFile, extName, spec, options)", cond="func-iter",
								{type="func-iter",
									{ type="write", name="FuncDef(hFile, func, typemap, spec, options, funcSeen)", },
								},
							},
							{ type="blank"},
						},
						{type="func-iter",
							{ type="write", name="FuncDef(hFile, func, typemap, spec, options, funcSeen)", },
							{ type="blank", last=true },
						},
					},
					{ type="block", name="CoreLoader(hFile, spec, options)",
						{ type="version-iter",
							{ type="core-ext-iter",
								{type="func-iter",
									{ type="write", name="CoreFuncLoader(hFile, func, typemap, spec, options)", },
								},
							},
							{type="func-iter",
								{ type="write", name="CoreFuncLoader(hFile, func, typemap, spec, options)", },
							},
						},
					},
					{ type="blank"},
				},
				{ type="write", name="ExtStringFuncDef(hFile, specData, spec, options, funcSeen)"},
			},
			{ type="block", name="SysDef(hFile, spec, options)",
				{ type="write", name="UtilityDefs(hFile, specData, spec, options)",},
				{ type="blank" },
				{ type="write", name="MainLoaderFunc(hFile, specData, spec, options)",},
				{ type="blank" },
				{ type="write", name="VersioningFuncs(hFile, specData, spec, options)", cond="version-iter"},
				{ type="blank", cond="version-iter" },
			},
		},
	},
}


my_struct = struct.BuildStructure(my_struct)

function common.GetStandardStructure()
	return my_struct
end

return common
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.