Use separate namespaces for types, variables, and functions
[mesa.git] / glsl_types.cpp
1 /*
2 * Copyright © 2009 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <stdlib.h>
25 #include "glsl_symbol_table.h"
26 #include "glsl_parser_extras.h"
27 #include "glsl_types.h"
28 #include "builtin_types.h"
29
30
31 static void
32 add_types_to_symbol_table(glsl_symbol_table *symtab,
33 const struct glsl_type *types,
34 unsigned num_types)
35 {
36 unsigned i;
37
38 for (i = 0; i < num_types; i++) {
39 symtab->add_type(types[i].name, & types[i]);
40 }
41 }
42
43
44 static void
45 generate_110_types(glsl_symbol_table *symtab)
46 {
47 add_types_to_symbol_table(symtab, builtin_core_types,
48 Elements(builtin_core_types));
49 add_types_to_symbol_table(symtab, builtin_structure_types,
50 Elements(builtin_structure_types));
51 add_types_to_symbol_table(symtab, builtin_110_deprecated_structure_types,
52 Elements(builtin_110_deprecated_structure_types));
53 }
54
55
56 static void
57 generate_120_types(glsl_symbol_table *symtab)
58 {
59 generate_110_types(symtab);
60
61 add_types_to_symbol_table(symtab, builtin_120_types,
62 Elements(builtin_120_types));
63 }
64
65
66 static void
67 generate_130_types(glsl_symbol_table *symtab)
68 {
69 generate_120_types(symtab);
70
71 add_types_to_symbol_table(symtab, builtin_130_types,
72 Elements(builtin_130_types));
73 }
74
75
76 void
77 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state)
78 {
79 switch (state->language_version) {
80 case 110:
81 generate_110_types(state->symbols);
82 break;
83 case 120:
84 generate_120_types(state->symbols);
85 break;
86 case 130:
87 generate_130_types(state->symbols);
88 break;
89 default:
90 /* error */
91 break;
92 }
93 }
94
95
96 const struct glsl_type *
97 _mesa_glsl_get_vector_type(unsigned base_type, unsigned vector_length)
98 {
99 switch (base_type) {
100 case GLSL_TYPE_UINT:
101 switch (vector_length) {
102 case 1:
103 case 2:
104 case 3:
105 case 4:
106 return glsl_uint_type + (vector_length - 1);
107 default:
108 return glsl_error_type;
109 }
110 case GLSL_TYPE_INT:
111 switch (vector_length) {
112 case 1:
113 case 2:
114 case 3:
115 case 4:
116 return glsl_int_type + (vector_length - 1);
117 default:
118 return glsl_error_type;
119 }
120 case GLSL_TYPE_FLOAT:
121 switch (vector_length) {
122 case 1:
123 case 2:
124 case 3:
125 case 4:
126 return glsl_float_type + (vector_length - 1);
127 default:
128 return glsl_error_type;
129 }
130 case GLSL_TYPE_BOOL:
131 switch (vector_length) {
132 case 1:
133 case 2:
134 case 3:
135 case 4:
136 return glsl_bool_type + (vector_length - 1);
137 default:
138 return glsl_error_type;
139 }
140 default:
141 return glsl_error_type;
142 }
143 }