Source

glLoadGen / modules / NoloadC_Style.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
local util = require "util"
local struct = require "NoloadC_Struct"
local common = require "CommonStyle"

--------------------------------------
-- Common functions.
local function GetIncludeGuard(spec, options)
	local temp = 
		options.prefix .. spec.GetIncludeGuardString() .. "_NOLOAD_STYLE_H"
	return temp:upper()
end

local function GetEnumName(enum, spec, options)
	return options.prefix .. spec.EnumNamePrefix() .. enum.name
end

local function GetFuncPtrName(func, spec, options)
	return options.prefix .. "_ptrc_".. spec.FuncNamePrefix() .. func.name
end

local function GetFuncName(func, spec, options)
	return options.prefix .. spec.FuncNamePrefix() .. func.name
end

local function GetFuncPtrTypedefName(func, spec, options)
	return "PFN" .. GetFuncPtrName(func, spec, options):upper() .. "PROC"
end

local function WriteFuncPtrTypedefStmt(hFile, func, typemap, spec, options)
	hFile:fmt("typedef %s (%s *%s)(%s);\n",
		common.GetFuncReturnType(func, typemap),
		spec.GetCodegenPtrType(),
		GetFuncPtrTypedefName(func, spec, options),
		common.GetFuncParamList(func, typemap))
end

local function GetFuncPtrDefDirect(func, typemap, spec, options)
	return string.format("%s (%s *%s)(%s)",
		common.GetFuncReturnType(func, typemap),
		spec.GetCodegenPtrType(),
		GetFuncPtrName(func, spec, options),
		common.GetFuncParamList(func, typemap, true))
end

local function GetFuncPtrDefTypedef(func, typemap, spec, options)
	return string.format("%s %s",
		GetFuncPtrTypedefName(func, spec, options),
		GetFuncPtrName(func, spec, options))
end

--------------------------------------
-- All style functions.
local my_style = {}

function my_style.WriteLargeHeader(hFile, value, options)
	local len = #value
	hFile:write("///", string.rep("/", len), "///\n")
	hFile:write("// ", value, "\n")
end

function my_style.WriteSmallHeader(hFile, value, options)
	hFile:write("// ", value, "\n")
end

function my_style.WriteBlockBeginExtVariables(hFile, spec, options)
end

function my_style.WriteBlockEndExtVariables(hFile, spec, options)
end

function my_style.WriteBlockBeginSystem(hFile, spec, options)
end

function my_style.WriteBlockEndSystem(hFile, spec, options)
end


---------------------------------------------
-- Header functions.
local hdr = {}
my_style.hdr = hdr

function hdr.GetFilename(basename, spec, options)
	return basename .. ".h"
end

function hdr.WriteBlockBeginIncludeGuard(hFile, spec, options)
	local guard = GetIncludeGuard(spec, options)
	hFile:fmt("#ifndef %s\n", guard)
	hFile:fmt("#define %s\n", guard)
end

function hdr.WriteBlockEndIncludeGuard(hFile, spec, options)
	hFile:fmt("#endif //%s\n", GetIncludeGuard(spec, options))
end

function hdr.WriteGuards(hFile, spec, options)
	hFile:rawwrite(spec.GetHeaderInit())
end

function hdr.WriteTypedefs(hFile, specData, spec, options)
	local defArray = common.GetStdTypedefs()
	
	--Use include-guards for the typedefs, since they're common among
	--headers in this style.
	hFile:write("#ifndef GL_LOAD_GEN_BASIC_OPENGL_TYPEDEFS\n")
	hFile:write("#define GL_LOAD_GEN_BASIC_OPENGL_TYPEDEFS\n")
	hFile:write("\n")
	hFile:inc()
	
	for _, def in ipairs(defArray) do
		hFile:write(def)
	end
	
	hFile:dec()
	hFile:write("\n")
	hFile:write("#endif /*GL_LOAD_GEN_BASIC_OPENGL_TYPEDEFS*/\n")
	hFile:write("\n")

	common.WritePassthruData(hFile, specData.funcData.passthru)
end

function hdr.WriteExtension(hFile, extName, spec, options)
	hFile:fmt("extern int %s%sext_%s;\n", options.prefix, spec.DeclPrefix(), extName)
end

function hdr.WriteBlockBeginEnumerators(hFile, spec, options)
end

function hdr.WriteBlockEndEnumerators(hFile, spec, options)
end

function hdr.WriteEnumerator(hFile, enum, enumTable, spec, options, enumSeen)
	local name = GetEnumName(enum, spec, options)
	if(enumSeen[enum.name]) then
		hFile:fmt("//%s seen in %s\n", name, enumSeen[enum.name])
	else
		hFile:fmt("#define %s%s%s\n",
			name,
			common.GetNameLengthPadding(name, 33),
			common.ResolveEnumValue(enum, enumTable))
	end
end

function hdr.WriteBlockBeginExternC(hFile, spec, options)
	common.WriteExternCStart(hFile)
end

function hdr.WriteBlockEndExternC(hFile, spec, options)
	common.WriteExternCEnd(hFile)
end

function hdr.WriteFunction(hFile, func, typemap, spec, options, funcSeen)
	if(funcSeen[func.name]) then return end
	
	hFile:write("extern ", GetFuncPtrDefDirect(func, typemap, spec, options), ";\n")
	hFile:fmt("#define %s %s\n", GetFuncName(func, spec, options),
		GetFuncPtrName(func, spec, options))
end

function hdr.WriteSetupFunction(hFile, specData, spec, options)
	hFile:fmt("void %sCheckExtensions(%s);\n", spec.DeclPrefix(), spec.GetLoaderParams())
end

function hdr.WriteVersionFunctions(hFile, specData, spec, options)
end


----------------------------------------
-- Source file.
local src = {}
my_style.src = src

function src.GetFilename(basename, spec, options)
	return basename .. ".c"
end

function src.WriteIncludes(hFile, basename, spec, options)
	hFile:writeblock([[
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
]])

	local base = util.ParsePath(hdr.GetFilename(basename, spec, options))
	hFile:fmt('#include "%s"\n', base)

end

function src.WriteLoaderFunc(hFile, spec, options)
	hFile:writeblock(spec.GetLoaderFunc())
end

function src.WriteExtension(hFile, extName, spec, options)
	hFile:fmt("int %s%sext_%s = 0;\n", options.prefix, spec.DeclPrefix(), extName)
end

function src.WriteSetupFunction(hFile, specData, spec, options)
	hFile:write "static void ClearExtensionVariables()\n"
	hFile:write "{\n"
	hFile:inc()
	
	for _, extName in ipairs(options.extensions) do
		hFile:fmt("%s%sext_%s = 0;\n", options.prefix, spec.DeclPrefix(), extName)
	end
	
	hFile:dec()
	hFile:write "}\n"
	hFile:write "\n"
	
	local mapTableName = options.prefix .. spec.DeclPrefix() .. "MapTable"
	
	hFile:writeblock([[
typedef struct ]] .. mapTableName .. [[_s
{
	char *extName;
	int *extVariable;
}]] .. mapTableName .. [[;

]])

	local arrayLength = #options.extensions
	if(arrayLength == 0) then arrayLength = 1 end

	hFile:fmt("static %s g_mappingTable[%i] =\n", mapTableName, arrayLength)
	hFile:write "{\n"
	hFile:inc()
	for _, extName in ipairs(options.extensions) do
		hFile:fmt('{"%s%s", &%s%sext_%s},\n',
			spec.ExtNamePrefix(),
			extName,
			options.prefix,
			spec.DeclPrefix(),
			extName)
	end
	hFile:dec()
	hFile:write "};\n"
	
	hFile:write "\n"
	hFile:fmtblock([[
static void LoadExtByName(const char *extensionName)
{
	%s *tableEnd = &g_mappingTable[%i];
	%s *entry = &g_mappingTable[0];
	for(; entry != tableEnd; ++entry)
	{
		if(strcmp(entry->extName, extensionName) == 0)
			break;
	}
	
	if(entry != tableEnd)
		*(entry->extVariable) = 1;
}
]], mapTableName, #options.extensions, mapTableName)

	hFile:write "\n"
	
	local indexed = spec.GetIndexedExtStringFunc(options);
	if(indexed) then
		indexed[1] = specData.functable[indexed[1]]
		indexed[3] = specData.functable[indexed[3]]
		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

		hFile:writeblock([[
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);
	}
}
]])
	else
		hFile:writeblock(
			common.GetProcessExtsFromStringFunc("LoadExtByName(%s)"))
	end

	hFile:write "\n"
	hFile:fmt("void %sCheckExtensions(%s)\n", spec.DeclPrefix(), spec.GetLoaderParams())
	hFile:write "{\n"
	hFile:inc()
	hFile:write "ClearExtensionVariables();\n"
	hFile:write "\n"
	if(indexed) then
		hFile:write("ProcExtsFromExtList();\n")
	else
		--First, check if the GetExtStringFuncName is in the specData.
		hFile:write "{\n"
		hFile:inc()
		
		local funcName = spec.GetExtStringFuncName()
		if(specData.functable[funcName]) then
			--Create a function pointer and load it.
			local func = specData.functable[funcName]
			local typemap = specData.typemap
			funcName = "InternalGetExtensionString"

				hFile:fmt("typedef %s (%s *MYGETEXTSTRINGPROC)(%s);\n",
					common.GetFuncReturnType(func, typemap),
					spec.GetCodegenPtrType(),
					common.GetFuncParamList(func, typemap))
				hFile:fmt('MYGETEXTSTRINGPROC %s = (MYGETEXTSTRINGPROC)%s("%s%s");\n',
					funcName,
					spec.GetPtrLoaderFuncName(),
					spec.FuncNamePrefix(),
					func.name)
				hFile:fmt("if(!%s) return;\n", funcName)
		end
		
		hFile:fmt("ProcExtsFromExtString((const char *)%s(%s));\n",
			funcName,
			spec.GetExtStringParamList(
				function (name) return options.prefix .. spec.EnumNamePrefix() .. name end))
		hFile:dec()
		hFile:write "}\n"
	end
	hFile:dec()
	hFile:write "}\n"
end

function src.WriteVersionFunctions(hFile, specData, spec, options)
end

local typedefs = {}
src.typedefs = typedefs

function typedefs.WriteFunction(hFile, func, typemap, spec, options, funcSeen)
	WriteFuncPtrTypedefStmt(hFile, func, typemap, spec, options)
	hFile:fmt("static %s %s Switch_%s(%s);\n",
		common.GetFuncReturnType(func, typemap),
		spec.GetCodegenPtrType(),
		func.name,
		common.GetFuncParamList(func, typemap, true))
end

local defs = {}
src.defs = defs

function defs.WriteFunction(hFile, func, typemap, spec, options, funcSeen)
	hFile:fmt("%s = Switch_%s;\n",
		GetFuncPtrDefTypedef(func, typemap, spec, options),
		func.name)
end

local switch = {}
src.switch = switch

function switch.WriteFunction(hFile, func, typemap, spec, options, funcSeen)
	hFile:fmt("static %s %s Switch_%s(%s)\n",
		common.GetFuncReturnType(func, typemap),
		spec.GetCodegenPtrType(),
		func.name,
		common.GetFuncParamList(func, typemap, true))
	hFile:write "{\n"
	hFile:inc()
	hFile:fmt('%s = (%s)%s("%s%s");\n',
		GetFuncPtrName(func, spec, options),
		GetFuncPtrTypedefName(func, spec, options),
		spec.GetPtrLoaderFuncName(),
		spec.FuncNamePrefix(),
		func.name)
		
	if(common.DoesFuncReturnSomething(func, typemap)) then
		hFile:fmt('%s(%s);\n',
			GetFuncPtrName(func, spec, options),
			common.GetFuncParamCallList(func, typemap))
	else
		hFile:fmt('return %s(%s);\n',
			GetFuncPtrName(func, spec, options),
			common.GetFuncParamCallList(func, typemap))
	end
	hFile:dec()
	hFile:write "}\n\n"
end

function switch.WriteGetExtString(hFile, specData, spec, options, funcSeen)
	if(funcSeen[spec.GetExtStringFuncName()]) then
		return
	end

	local func = specData.funcdefs[spec.GetExtStringFuncName()]
	if(func) then
		hFile:write "\n"
		hFile:fmt("static %s %s(%s)\n",
			common.GetFuncReturnType(func, typemap),
			func.name,
			common.GetFuncParamList(func, specData.funcData.typemap, true))
		hFile:write "{\n"
		hFile:inc()
		hFile:fmt('%s = (%s)%s("%s%s");\n',
			GetFuncPtrName(func, spec, options),
			GetFuncPtrTypedefName(func, spec, options),
			spec.GetPtrLoaderFuncName(),
			spec.FuncNamePrefix(),
			func.name)
			
		if(common.DoesFuncReturnSomething(func, typemap)) then
			hFile:fmt('%s(%s);\n',
				GetFuncPtrName(func, spec, options),
				common.GetFuncParamCallList(func, typemap))
		else
			hFile:fmt('return %s(%s);\n',
				GetFuncPtrName(func, spec, options),
				common.GetFuncParamCallList(func, typemap))
		end
		hFile:dec()
		hFile:write "}\n\n"
	end
end

local init = {}
src.init = init

function init.WriteBlockBeginStruct(hFile, spec, options)
	hFile:write("struct InitializeVariables\n")
	hFile:write "{\n"
	hFile:inc()

	hFile:write("InitializeVariables()\n")
	hFile:write "{\n"
	hFile:inc()
end

function init.WriteBlockEndStruct(hFile, spec, options)
	hFile:dec()
	hFile:write "}\n"
	hFile:dec()
	hFile:write "};\n\n"
	hFile:write("InitializeVariables g_initVariables;\n")
end

function init.WriteFunction(hFile, func, typemap, spec, options, funcSeen)
	hFile:fmt("%s = Switch_%s;\n", func.name, func.name)
end


local function Create()
	return util.DeepCopyTable(my_style), struct
end

return { Create = Create }
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.