summaryrefslogtreecommitdiff
path: root/src/mesa/glapi/typeexpr.py
blob: 8ba7de486273567ef5ed81511e7c1cda08058803 (plain)
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
#!/usr/bin/env python

# (C) Copyright IBM Corporation 2005
# All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# on the rights to use, copy, modify, merge, publish, distribute, sub
# license, and/or sell copies of the Software, and to permit persons to whom
# the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
# IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# Authors:
#    Ian Romanick <idr@us.ibm.com>

import string, copy

class type_node:
	def __init__(self):
		self.pointer = 0  # bool
		self.const = 0    # bool
		self.signed = 1   # bool
		self.integer = 1  # bool

		# If elements is set to non-zero, then field is an array.
		self.elements = 0

		self.name = None
		self.size = 0     # type's size in bytes
		return


	def string(self):
		"""Return string representation of this type_node."""
		s = ""
		
		if self.pointer:
			s = "* "

		if self.const:
			s += "const "

		if not self.pointer:
			if self.integer:
				if self.signed:
					s += "signed "
				else:
					s += "unsigned "

			if self.name:
				s += "%s " % (self.name)

		return s


class type_table:
	def __init__(self):
		self.types_by_name = {}
		return


	def add_type(self, type_expr):
		self.types_by_name[ type_expr.get_base_name() ] = type_expr
		return


	def find_type(self, name):
		if name in self.types_by_name:
			return self.types_by_name[ name ]
		else:
			return None


def create_initial_types():
	tt = type_table()

	basic_types = [
		("char",   1, 1),
		("short",  2, 1),
		("int",    4, 1),
		("long",   4, 1),
		("float",  4, 0),
		("double", 8, 0),
		("enum",   4, 1)
	]

	for (type_name, type_size, integer) in basic_types:
		te = type_expression(None)
		tn = type_node()
		tn.name = type_name
		tn.size = type_size
		tn.integer = integer
		te.expr.append(tn)
		tt.add_type( te )

	type_expression.built_in_types = tt
	return


class type_expression:
	built_in_types = None

	def __init__(self, type_string, extra_types = None):
		self.expr = []

		if not type_string:
			return

		self.original_string = type_string

		if not type_expression.built_in_types:
			raise RuntimeError("create_initial_types must be called before creating type_expression objects.")

		# Replace '*' with ' * ' in type_string.  Then, split the string
		# into tokens, separated by spaces.
		tokens = string.split( string.replace( type_string, "*", " * " ) )

		const = 0
		t = None
		signed = 0
		unsigned = 0

		for i in tokens:
			if i == "const":
				if t and t.pointer:
					t.const = 1
				else:
					const = 1
			elif i == "signed":
				signed = 1
			elif i == "unsigned":
				unsigned = 1
			elif i == "*":
				# This is a quirky special-case because of the
				# way the C works for types.  If 'unsigned' is
				# specified all by itself, it is treated the
				# same as "unsigned int".

				if unsigned:
					self.set_base_type( "int", signed, unsigned, const, extra_types )
					const = 0
					signed = 0
					unsigned = 0

				if not self.expr:
					raise RuntimeError("Invalid type expression (dangling pointer)")

				if signed:
					raise RuntimeError("Invalid type expression (signed / unsigned applied to pointer)")

				t = type_node()
				t.pointer = 1
				self.expr.append( t )
			else:
				if self.expr:
					raise RuntimeError('Invalid type expression (garbage after pointer qualifier -> "%s")' % (self.original_string))

				self.set_base_type( i, signed, unsigned, const, extra_types )
				const = 0
				signed = 0
				unsigned = 0

			if signed and unsigned:
				raise RuntimeError("Invalid type expression (both signed and unsigned specified)")
				

		if const:
			raise RuntimeError("Invalid type expression (dangling const)")

		if unsigned:
			raise RuntimeError("Invalid type expression (dangling signed)")

		if signed:
			raise RuntimeError("Invalid type expression (dangling unsigned)")

		return


	def set_base_type(self, type_name, signed, unsigned, const, extra_types):
		te = type_expression.built_in_types.find_type( type_name )
		if not te:
			te = extra_types.find_type( type_name )

		if not te:
			raise RuntimeError('Unknown base type "%s".' % (type_name))

		self.expr = copy.deepcopy(te.expr)

		t = self.expr[ len(self.expr) - 1 ]
		t.const = const
		if signed:
			t.signed = 1
		elif unsigned:
			t.signed = 0


	def set_base_type_node(self, tn):
		self.expr = [tn]
		return


	def set_elements(self, count):
		tn = self.expr[0]

		tn.elements = count
		return


	def string(self):
		s = ""
		for t in self.expr:
			s += t.string()

		return s


	def get_base_type_node(self):
		return self.expr[0]


	def get_base_name(self):
		if len(self.expr):
			return self.expr[0].name
		else:
			return None


	def get_element_size(self):
		tn = self.expr[0]

		if tn.elements:
			return tn.elements * tn.size
		else:
			return tn.size


	def get_element_count(self):
		tn = self.expr[0]
		return tn.elements


	def get_stack_size(self):
		tn = self.expr[ len(self.expr) - 1 ]

		if tn.elements or tn.pointer:
			return 4
		elif not tn.integer:
			return tn.size
		else:
			return 4


	def is_pointer(self):
		tn = self.expr[ len(self.expr) - 1 ]
		return tn.pointer


	def format_string(self):
		tn = self.expr[ len(self.expr) - 1 ]
		if tn.pointer:
			return "%p"
		elif not tn.integer:
			return "%f"
		else:
			return "%d"



if __name__ == '__main__':
	
	types_to_try = [ "int", "int *", "const int *", "int * const", "const int * const", \
	                 "unsigned * const *", \
			 "float", "const double", "double * const"]

	create_initial_types()

	for t in types_to_try:
		print 'Trying "%s"...' % (t)
		te = type_expression( t )
		print 'Got "%s" (%u, %u).' % (te.string(), te.get_stack_size(), te.get_element_size())