glsl/pp: Add sl_pp_context_add_extension().
[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_process.h"
31 #include "sl_pp_public.h"
32
33
34 int
35 sl_pp_context_add_extension(struct sl_pp_context *context,
36 const char *name,
37 const char *name_string)
38 {
39 struct sl_pp_extension ext;
40
41 if (context->num_extensions == SL_PP_MAX_EXTENSIONS) {
42 return -1;
43 }
44
45 ext.name = sl_pp_context_add_unique_str(context, name);
46 if (ext.name == -1) {
47 return -1;
48 }
49
50 ext.name_string = sl_pp_context_add_unique_str(context, name_string);
51 if (ext.name_string == -1) {
52 return -1;
53 }
54
55 context->extensions[context->num_extensions++] = ext;
56 return 0;
57 }
58
59 int
60 sl_pp_process_extension(struct sl_pp_context *context,
61 const struct sl_pp_token_info *input,
62 unsigned int first,
63 unsigned int last,
64 struct sl_pp_process_state *state)
65 {
66 int extension_name = -1;
67 int behavior = -1;
68 struct sl_pp_token_info out;
69
70 /* Grab the extension name. */
71 if (first < last && input[first].token == SL_PP_IDENTIFIER) {
72 extension_name = input[first].data.identifier;
73 first++;
74 }
75 if (extension_name == -1) {
76 strcpy(context->error_msg, "expected identifier after `#extension'");
77 return -1;
78 }
79
80 /* Make sure the extension is supported. */
81 if (extension_name == context->dict.all) {
82 out.data.extension = extension_name;
83 } else {
84 unsigned int i;
85
86 out.data.extension = -1;
87 for (i = 0; i < context->num_extensions; i++) {
88 if (extension_name == context->extensions[i].name_string) {
89 out.data.extension = extension_name;
90 break;
91 }
92 }
93 }
94
95 /* Grab the colon separating the extension name and behavior. */
96 while (first < last && input[first].token == SL_PP_WHITESPACE) {
97 first++;
98 }
99 if (first < last && input[first].token == SL_PP_COLON) {
100 first++;
101 } else {
102 strcpy(context->error_msg, "expected `:' after extension name");
103 return -1;
104 }
105 while (first < last && input[first].token == SL_PP_WHITESPACE) {
106 first++;
107 }
108
109 /* Grab the behavior name. */
110 if (first < last && input[first].token == SL_PP_IDENTIFIER) {
111 behavior = input[first].data.identifier;
112 first++;
113 }
114 if (behavior == -1) {
115 strcpy(context->error_msg, "expected identifier after `:'");
116 return -1;
117 }
118
119 if (behavior == context->dict.require) {
120 if (out.data.extension == -1) {
121 strcpy(context->error_msg, "the required extension is not supported");
122 return -1;
123 }
124 if (out.data.extension == context->dict.all) {
125 strcpy(context->error_msg, "invalid behavior for `all' extension: `require'");
126 return -1;
127 }
128 out.token = SL_PP_EXTENSION_REQUIRE;
129 } else if (behavior == context->dict.enable) {
130 if (out.data.extension == -1) {
131 /* Warning: the extension cannot be enabled. */
132 return 0;
133 }
134 if (out.data.extension == context->dict.all) {
135 strcpy(context->error_msg, "invalid behavior for `all' extension: `enable'");
136 return -1;
137 }
138 out.token = SL_PP_EXTENSION_ENABLE;
139 } else if (behavior == context->dict.warn) {
140 if (out.data.extension == -1) {
141 /* Warning: the extension is not supported. */
142 return 0;
143 }
144 out.token = SL_PP_EXTENSION_WARN;
145 } else if (behavior == context->dict.disable) {
146 if (out.data.extension == -1) {
147 /* Warning: the extension is not supported. */
148 return 0;
149 }
150 out.token = SL_PP_EXTENSION_DISABLE;
151 } else {
152 strcpy(context->error_msg, "unrecognised behavior name");
153 return -1;
154 }
155
156 /* Grab the end of line. */
157 while (first < last && input[first].token == SL_PP_WHITESPACE) {
158 first++;
159 }
160 if (first < last) {
161 strcpy(context->error_msg, "expected end of line after behavior name");
162 return -1;
163 }
164
165 if (sl_pp_process_out(state, &out)) {
166 return -1;
167 }
168
169 return 0;
170 }