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