e004c9f95b9b045266482f5c299d527c5b72eb26
[mesa.git] / src / glsl / pp / sl_pp_define.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 static void
35 skip_whitespace(const struct sl_pp_token_info *input,
36 unsigned int *first,
37 unsigned int last)
38 {
39 while (*first < last && input[*first].token == SL_PP_WHITESPACE) {
40 (*first)++;
41 }
42 }
43
44
45 static int
46 _parse_formal_args(struct sl_pp_context *context,
47 const struct sl_pp_token_info *input,
48 unsigned int *first,
49 unsigned int last,
50 struct sl_pp_macro *macro)
51 {
52 struct sl_pp_macro_formal_arg **arg;
53
54 macro->num_args = 0;
55
56 skip_whitespace(input, first, last);
57 if (*first < last) {
58 if (input[*first].token == SL_PP_RPAREN) {
59 (*first)++;
60 return 0;
61 }
62 } else {
63 strcpy(context->error_msg, "expected either macro formal argument or `)'");
64 return -1;
65 }
66
67 arg = &macro->arg;
68
69 for (;;) {
70 if (*first < last && input[*first].token != SL_PP_IDENTIFIER) {
71 strcpy(context->error_msg, "expected macro formal argument");
72 return -1;
73 }
74
75 *arg = malloc(sizeof(struct sl_pp_macro_formal_arg));
76 if (!*arg) {
77 strcpy(context->error_msg, "out of memory");
78 return -1;
79 }
80
81 (**arg).name = input[*first].data.identifier;
82 (*first)++;
83
84 (**arg).next = NULL;
85 arg = &(**arg).next;
86
87 macro->num_args++;
88
89 skip_whitespace(input, first, last);
90 if (*first < last) {
91 if (input[*first].token == SL_PP_COMMA) {
92 (*first)++;
93 skip_whitespace(input, first, last);
94 } else if (input[*first].token == SL_PP_RPAREN) {
95 (*first)++;
96 return 0;
97 } else {
98 strcpy(context->error_msg, "expected either `,' or `)'");
99 return -1;
100 }
101 } else {
102 strcpy(context->error_msg, "expected either `,' or `)'");
103 return -1;
104 }
105 }
106
107 /* Should not gete here. */
108 }
109
110
111 int
112 sl_pp_process_define(struct sl_pp_context *context,
113 const struct sl_pp_token_info *input,
114 unsigned int first,
115 unsigned int last)
116 {
117 int macro_name = -1;
118 struct sl_pp_macro *macro;
119 unsigned int i;
120 unsigned int body_len;
121 unsigned int j;
122
123 if (first < last && input[first].token == SL_PP_IDENTIFIER) {
124 macro_name = input[first].data.identifier;
125 first++;
126 }
127 if (macro_name == -1) {
128 strcpy(context->error_msg, "expected macro name");
129 return -1;
130 }
131
132 /* Check for reserved macro names */
133 {
134 const char *name = sl_pp_context_cstr(context, macro_name);
135
136 if (strstr(name, "__")) {
137 strcpy(context->error_msg, "macro names containing `__' are reserved");
138 return 1;
139 }
140 if (name[0] == 'G' && name[1] == 'L' && name[2] == '_') {
141 strcpy(context->error_msg, "macro names prefixed with `GL_' are reserved");
142 return 1;
143 }
144 }
145
146 for (macro = context->macro; macro; macro = macro->next) {
147 if (macro->name == macro_name) {
148 break;
149 }
150 }
151
152 if (!macro) {
153 macro = sl_pp_macro_new();
154 if (!macro) {
155 strcpy(context->error_msg, "out of memory");
156 return -1;
157 }
158
159 *context->macro_tail = macro;
160 context->macro_tail = &macro->next;
161 } else {
162 sl_pp_macro_reset(macro);
163 }
164
165 macro->name = macro_name;
166
167 /*
168 * If there is no whitespace between macro name and left paren, a macro
169 * formal argument list follows. This is the only place where the presence
170 * of a whitespace matters and it's the only reason why we are dealing
171 * with whitespace at this level.
172 */
173 if (first < last && input[first].token == SL_PP_LPAREN) {
174 first++;
175 if (_parse_formal_args(context, input, &first, last, macro)) {
176 return -1;
177 }
178 }
179
180 /* Calculate body size, trim out whitespace, make room for EOF. */
181 body_len = 1;
182 for (i = first; i < last; i++) {
183 if (input[i].token != SL_PP_WHITESPACE) {
184 body_len++;
185 }
186 }
187
188 macro->body = malloc(sizeof(struct sl_pp_token_info) * body_len);
189 if (!macro->body) {
190 strcpy(context->error_msg, "out of memory");
191 return -1;
192 }
193
194 for (j = 0, i = first; i < last; i++) {
195 if (input[i].token != SL_PP_WHITESPACE) {
196 macro->body[j++] = input[i];
197 }
198 }
199 macro->body[j++].token = SL_PP_EOF;
200
201 return 0;
202 }
203
204
205 int
206 sl_pp_process_undef(struct sl_pp_context *context,
207 const struct sl_pp_token_info *input,
208 unsigned int first,
209 unsigned int last)
210 {
211 int macro_name = -1;
212 struct sl_pp_macro **pmacro;
213 struct sl_pp_macro *macro;
214
215 if (first < last && input[first].token == SL_PP_IDENTIFIER) {
216 macro_name = input[first].data.identifier;
217 }
218 if (macro_name == -1) {
219 return 0;
220 }
221
222 for (pmacro = &context->macro; *pmacro; pmacro = &(**pmacro).next) {
223 if ((**pmacro).name == macro_name) {
224 break;
225 }
226 }
227 if (!*pmacro) {
228 return 0;
229 }
230
231 macro = *pmacro;
232 *pmacro = macro->next;
233 macro->next = NULL;
234 sl_pp_macro_free(macro);
235
236 return 0;
237 }