glcpp: Add line locations to "reserved name" error messages.
[mesa.git] / glsl_types.h
1 /* -*- c++ -*- */
2 /*
3 * Copyright © 2009 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #pragma once
26 #ifndef GLSL_TYPES_H
27 #define GLSL_TYPES_H
28
29 #include <cstring>
30 #include <cassert>
31
32 #define GLSL_TYPE_UINT 0
33 #define GLSL_TYPE_INT 1
34 #define GLSL_TYPE_FLOAT 2
35 #define GLSL_TYPE_BOOL 3
36 #define GLSL_TYPE_SAMPLER 4
37 #define GLSL_TYPE_STRUCT 5
38 #define GLSL_TYPE_ARRAY 6
39 #define GLSL_TYPE_FUNCTION 7
40 #define GLSL_TYPE_VOID 8
41 #define GLSL_TYPE_ERROR 9
42
43 enum glsl_sampler_dim {
44 GLSL_SAMPLER_DIM_1D = 0,
45 GLSL_SAMPLER_DIM_2D,
46 GLSL_SAMPLER_DIM_3D,
47 GLSL_SAMPLER_DIM_CUBE,
48 GLSL_SAMPLER_DIM_RECT,
49 GLSL_SAMPLER_DIM_BUF
50 };
51
52
53 struct glsl_type {
54 unsigned base_type:4;
55
56 unsigned sampler_dimensionality:3;
57 unsigned sampler_shadow:1;
58 unsigned sampler_array:1;
59 unsigned sampler_type:2; /**< Type of data returned using this sampler.
60 * only \c GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
61 * and \c GLSL_TYPE_UINT are valid.
62 */
63
64 /**
65 * \name Vector and matrix element counts
66 *
67 * For scalars, each of these values will be 1. For non-numeric types
68 * these will be 0.
69 */
70 /*@{*/
71 unsigned vector_elements:3; /**< 1, 2, 3, or 4 vector elements. */
72 unsigned matrix_columns:3; /**< 1, 2, 3, or 4 matrix columns. */
73 /*@}*/
74
75 /**
76 * Name of the data type
77 *
78 * This may be \c NULL for anonymous structures, for arrays, or for
79 * function types.
80 */
81 const char *name;
82
83 /**
84 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
85 * \c GLSL_TYPE_STRUCT, it is the number of elements in the structure and
86 * the number of values pointed to by \c fields.structure (below).
87 *
88 * For \c GLSL_TYPE_FUNCTION, it is the number of parameters to the
89 * function. The return value from a function is implicitly the first
90 * parameter. The types of the parameters are stored in
91 * \c fields.parameters (below).
92 */
93 unsigned length;
94
95 /**
96 * Subtype of composite data types.
97 */
98 union {
99 const struct glsl_type *array; /**< Type of array elements. */
100 const struct glsl_type *parameters; /**< Parameters to function. */
101 const struct glsl_struct_field *structure;/**< List of struct fields. */
102 } fields;
103
104
105 /**
106 * \name Pointers to various public type singletons
107 */
108 /*@{*/
109 static const glsl_type *const error_type;
110 static const glsl_type *const int_type;
111 static const glsl_type *const ivec4_type;
112 static const glsl_type *const uint_type;
113 static const glsl_type *const uvec4_type;
114 static const glsl_type *const float_type;
115 static const glsl_type *const vec2_type;
116 static const glsl_type *const vec3_type;
117 static const glsl_type *const vec4_type;
118 static const glsl_type *const bool_type;
119 static const glsl_type *const mat2_type;
120 static const glsl_type *const mat2x3_type;
121 static const glsl_type *const mat2x4_type;
122 static const glsl_type *const mat3x2_type;
123 static const glsl_type *const mat3_type;
124 static const glsl_type *const mat3x4_type;
125 static const glsl_type *const mat4x2_type;
126 static const glsl_type *const mat4x3_type;
127 static const glsl_type *const mat4_type;
128 /*@}*/
129
130
131 glsl_type(unsigned base_type, unsigned vector_elements,
132 unsigned matrix_columns, const char *name) :
133 base_type(base_type),
134 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
135 sampler_type(0),
136 vector_elements(vector_elements), matrix_columns(matrix_columns),
137 name(name),
138 length(0)
139 {
140 /* Neither dimension is zero or both dimensions are zero.
141 */
142 assert((vector_elements == 0) == (matrix_columns == 0));
143 memset(& fields, 0, sizeof(fields));
144 }
145
146 glsl_type(enum glsl_sampler_dim dim, bool shadow, bool array,
147 unsigned type, const char *name) :
148 base_type(GLSL_TYPE_SAMPLER),
149 sampler_dimensionality(dim), sampler_shadow(shadow),
150 sampler_array(array), sampler_type(type),
151 vector_elements(0), matrix_columns(0),
152 name(name),
153 length(0)
154 {
155 memset(& fields, 0, sizeof(fields));
156 }
157
158 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
159 const char *name) :
160 base_type(GLSL_TYPE_STRUCT),
161 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
162 sampler_type(0),
163 vector_elements(0), matrix_columns(0),
164 name(name),
165 length(num_fields)
166 {
167 this->fields.structure = fields;
168 }
169
170 /**
171 * For numeric and boolean derrived types returns the basic scalar type
172 *
173 * If the type is a numeric or boolean scalar, vector, or matrix type,
174 * this function gets the scalar type of the individual components. For
175 * all other types, including arrays of numeric or boolean types, the
176 * error type is returned.
177 */
178 const glsl_type *get_base_type() const;
179
180 /**
181 * Query the type of elements in an array
182 *
183 * \return
184 * Pointer to the type of elements in the array for array types, or \c NULL
185 * for non-array types.
186 */
187 const glsl_type *element_type() const
188 {
189 return is_array() ? fields.array : NULL;
190 }
191
192 /**
193 * Get the instance of a built-in scalar, vector, or matrix type
194 */
195 static const glsl_type *get_instance(unsigned base_type, unsigned rows,
196 unsigned columns);
197
198 /**
199 * Get the instance of an array type
200 */
201 static const glsl_type *get_array_instance(const glsl_type *base,
202 unsigned elements);
203
204 /**
205 * Generate the constructor for this type and add it to the symbol table
206 */
207 class ir_function *generate_constructor(class glsl_symbol_table *) const;
208
209 /**
210 * Query the total number of scalars that make up a scalar, vector or matrix
211 */
212 unsigned components() const
213 {
214 return vector_elements * matrix_columns;
215 }
216
217 /**
218 * Query whether or not a type is a scalar (non-vector and non-matrix).
219 */
220 bool is_scalar() const
221 {
222 return (vector_elements == 1)
223 && (base_type >= GLSL_TYPE_UINT)
224 && (base_type <= GLSL_TYPE_BOOL);
225 }
226
227 /**
228 * Query whether or not a type is a vector
229 */
230 bool is_vector() const
231 {
232 return (vector_elements > 1)
233 && (matrix_columns == 1)
234 && (base_type >= GLSL_TYPE_UINT)
235 && (base_type <= GLSL_TYPE_BOOL);
236 }
237
238 /**
239 * Query whether or not a type is a matrix
240 */
241 bool is_matrix() const
242 {
243 /* GLSL only has float matrices. */
244 return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT);
245 }
246
247 /**
248 * Query whether or not a type is a non-array numeric type
249 */
250 bool is_numeric() const
251 {
252 return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_FLOAT);
253 }
254
255 /**
256 * Query whether or not a type is an integral type
257 */
258 bool is_integer() const
259 {
260 return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT);
261 }
262
263 /**
264 * Query whether or not a type is a float type
265 */
266 bool is_float() const
267 {
268 return base_type == GLSL_TYPE_FLOAT;
269 }
270
271 /**
272 * Query whether or not a type is a non-array boolean type
273 */
274 bool is_boolean() const
275 {
276 return base_type == GLSL_TYPE_BOOL;
277 }
278
279 /**
280 * Query whether or not a type is a sampler
281 */
282 bool is_sampler() const
283 {
284 return base_type == GLSL_TYPE_SAMPLER;
285 }
286
287 /**
288 * Query whether or not a type is an array
289 */
290 bool is_array() const
291 {
292 return base_type == GLSL_TYPE_ARRAY;
293 }
294
295 /**
296 * Query whether or not a type is a record
297 */
298 bool is_record() const
299 {
300 return base_type == GLSL_TYPE_STRUCT;
301 }
302
303 /**
304 * Query whether or not a type is the void type singleton.
305 */
306 bool is_void() const
307 {
308 return base_type == GLSL_TYPE_VOID;
309 }
310
311 /**
312 * Query whether or not a type is the error type singleton.
313 */
314 bool is_error() const
315 {
316 return base_type == GLSL_TYPE_ERROR;
317 }
318
319 /**
320 * Query the full type of a matrix row
321 *
322 * \return
323 * If the type is not a matrix, \c glsl_type::error_type is returned.
324 * Otherwise a type matching the rows of the matrix is returned.
325 */
326 const glsl_type *row_type() const
327 {
328 return is_matrix()
329 ? get_instance(base_type, matrix_columns, 1)
330 : error_type;
331 }
332
333 /**
334 * Query the full type of a matrix column
335 *
336 * \return
337 * If the type is not a matrix, \c glsl_type::error_type is returned.
338 * Otherwise a type matching the columns of the matrix is returned.
339 */
340 const glsl_type *column_type() const
341 {
342 return is_matrix()
343 ? get_instance(base_type, vector_elements, 1)
344 : error_type;
345 }
346
347
348 /**
349 * Get the type of a structure field
350 *
351 * \return
352 * Pointer to the type of the named field. If the type is not a structure
353 * or the named field does not exist, \c glsl_type::error_type is returned.
354 */
355 const glsl_type *field_type(const char *name) const;
356
357
358 /**
359 * Get the location of a filed within a record type
360 */
361 int field_index(const char *name) const;
362
363
364 /**
365 * Query the number of elements in an array type
366 *
367 * \return
368 * The number of elements in the array for array types or -1 for non-array
369 * types. If the number of elements in the array has not yet been declared,
370 * zero is returned.
371 */
372 int array_size() const
373 {
374 return is_array() ? length : -1;
375 }
376
377 private:
378 /**
379 * Constructor for array types
380 */
381 glsl_type(const glsl_type *array, unsigned length);
382
383 /** Hash table containing the known array types. */
384 static struct hash_table *array_types;
385
386 static int array_key_compare(const void *a, const void *b);
387 static unsigned array_key_hash(const void *key);
388 };
389
390 struct glsl_struct_field {
391 const struct glsl_type *type;
392 const char *name;
393 };
394
395 struct _mesa_glsl_parse_state;
396
397 #ifdef __cplusplus
398 extern "C" {
399 #endif
400
401 extern void
402 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state);
403
404 extern void
405 _mesa_glsl_initialize_constructors(struct exec_list *instructions,
406 struct _mesa_glsl_parse_state *state);
407
408 #ifdef __cplusplus
409 }
410 #endif
411
412 #endif /* GLSL_TYPES_H */