glsl/pp: fix extension enable/disable options
[mesa.git] / src / glsl / pp / sl_pp_extension.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include "sl_pp_context.h"
31 #include "sl_pp_process.h"
32 #include "sl_pp_public.h"
33
34
35 /**
36 * Declare an extension to the preprocessor. This tells the preprocessor
37 * which extensions are supported by Mesa.
38 * The shader still needs to have a "#extension name: behavior" line to enable
39 * the extension.
40 */
41 int
42 sl_pp_context_add_extension(struct sl_pp_context *context,
43 const char *name)
44 {
45 struct sl_pp_extension ext;
46
47 if (context->num_extensions == SL_PP_MAX_EXTENSIONS) {
48 return -1;
49 }
50
51 ext.name = sl_pp_context_add_unique_str(context, name);
52 if (ext.name == -1) {
53 return -1;
54 }
55
56 ext.enabled = 0;
57
58 context->extensions[context->num_extensions++] = ext;
59
60 assert(context->num_extensions <= sizeof(context->extensions));
61
62 return 0;
63 }
64
65 /**
66 * Process a "#extension name: behavior" directive.
67 */
68 int
69 sl_pp_process_extension(struct sl_pp_context *context,
70 const struct sl_pp_token_info *input,
71 unsigned int first,
72 unsigned int last,
73 struct sl_pp_process_state *state)
74 {
75 int extension_name = -1;
76 int behavior = -1;
77 struct sl_pp_token_info out;
78 struct sl_pp_extension *extension = NULL;
79
80 /* Grab the extension name. */
81 if (first < last && input[first].token == SL_PP_IDENTIFIER) {
82 extension_name = input[first].data.identifier;
83 first++;
84 }
85 if (extension_name == -1) {
86 strcpy(context->error_msg, "expected identifier after `#extension'");
87 return -1;
88 }
89
90 /* Make sure the extension is supported. */
91 if (extension_name == context->dict.all) {
92 out.data.extension = extension_name;
93 } else {
94 unsigned int i;
95
96 out.data.extension = -1;
97 for (i = 0; i < context->num_extensions; i++) {
98 if (extension_name == context->extensions[i].name) {
99 out.data.extension = extension_name;
100 extension = &context->extensions[i];
101 break;
102 }
103 }
104 }
105
106 /* Grab the colon separating the extension name and behavior. */
107 while (first < last && input[first].token == SL_PP_WHITESPACE) {
108 first++;
109 }
110 if (first < last && input[first].token == SL_PP_COLON) {
111 first++;
112 } else {
113 strcpy(context->error_msg, "expected `:' after extension name");
114 return -1;
115 }
116 while (first < last && input[first].token == SL_PP_WHITESPACE) {
117 first++;
118 }
119
120 /* Grab the behavior name. */
121 if (first < last && input[first].token == SL_PP_IDENTIFIER) {
122 behavior = input[first].data.identifier;
123 first++;
124 }
125 if (behavior == -1) {
126 strcpy(context->error_msg, "expected identifier after `:'");
127 return -1;
128 }
129
130 if (behavior == context->dict.require) {
131 if (out.data.extension == -1) {
132 strcpy(context->error_msg, "the required extension is not supported");
133 return -1;
134 }
135 if (out.data.extension == context->dict.all) {
136 strcpy(context->error_msg, "invalid behavior for `all' extension: `require'");
137 return -1;
138 }
139 out.token = SL_PP_EXTENSION_REQUIRE;
140 extension->enabled = 1;
141 } else if (behavior == context->dict.enable) {
142 if (out.data.extension == -1) {
143 /* Warning: the extension cannot be enabled. */
144 return 0;
145 }
146 if (out.data.extension == context->dict.all) {
147 strcpy(context->error_msg, "invalid behavior for `all' extension: `enable'");
148 return -1;
149 }
150 out.token = SL_PP_EXTENSION_ENABLE;
151 extension->enabled = 1;
152 } else if (behavior == context->dict.warn) {
153 if (out.data.extension == -1) {
154 /* Warning: the extension is not supported. */
155 return 0;
156 }
157 out.token = SL_PP_EXTENSION_WARN;
158 extension->enabled = 0;
159 } else if (behavior == context->dict.disable) {
160 if (out.data.extension == -1) {
161 /* Warning: the extension is not supported. */
162 return 0;
163 }
164 out.token = SL_PP_EXTENSION_DISABLE;
165 extension->enabled = 0;
166 } else {
167 strcpy(context->error_msg, "unrecognised behavior name");
168 return -1;
169 }
170
171 /* Grab the end of line. */
172 while (first < last && input[first].token == SL_PP_WHITESPACE) {
173 first++;
174 }
175 if (first < last) {
176 strcpy(context->error_msg, "expected end of line after behavior name");
177 return -1;
178 }
179
180 if (sl_pp_process_out(state, &out)) {
181 return -1;
182 }
183
184 return 0;
185 }