Merge branch 'mesa_7_7_branch'
[mesa.git] / src / glsl / pp / sl_pp_context.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_public.h"
31 #include "sl_pp_context.h"
32
33
34 struct sl_pp_context *
35 sl_pp_context_create(const char *input,
36 const struct sl_pp_purify_options *options)
37 {
38 struct sl_pp_context *context;
39
40 context = calloc(1, sizeof(struct sl_pp_context));
41 if (!context) {
42 return NULL;
43 }
44
45 if (sl_pp_dict_init(context)) {
46 sl_pp_context_destroy(context);
47 return NULL;
48 }
49
50 context->getc_buf_capacity = 64;
51 context->getc_buf = malloc(context->getc_buf_capacity * sizeof(char));
52 if (!context->getc_buf) {
53 sl_pp_context_destroy(context);
54 return NULL;
55 }
56
57 if (sl_pp_token_buffer_init(&context->tokens, context)) {
58 sl_pp_context_destroy(context);
59 return NULL;
60 }
61
62 context->macro_tail = &context->macro;
63 context->if_ptr = SL_PP_MAX_IF_NESTING;
64 context->if_value = 1;
65 memset(context->error_msg, 0, sizeof(context->error_msg));
66 context->error_line = 1;
67 context->line = 1;
68 context->file = 0;
69
70 sl_pp_purify_state_init(&context->pure, input, options);
71
72 memset(&context->process_state, 0, sizeof(context->process_state));
73
74 return context;
75 }
76
77 void
78 sl_pp_context_destroy(struct sl_pp_context *context)
79 {
80 if (context) {
81 free(context->cstr_pool);
82 sl_pp_macro_free(context->macro);
83 free(context->getc_buf);
84 sl_pp_token_buffer_destroy(&context->tokens);
85 free(context->process_state.out);
86 free(context);
87 }
88 }
89
90 const char *
91 sl_pp_context_error_message(const struct sl_pp_context *context)
92 {
93 return context->error_msg;
94 }
95
96 void
97 sl_pp_context_error_position(const struct sl_pp_context *context,
98 unsigned int *file,
99 unsigned int *line)
100 {
101 if (file) {
102 *file = 0;
103 }
104 if (line) {
105 *line = context->error_line;
106 }
107 }
108
109 int
110 sl_pp_context_add_predefined(struct sl_pp_context *context,
111 const char *name,
112 const char *value)
113 {
114 struct sl_pp_predefined pre;
115
116 if (context->num_predefined == SL_PP_MAX_PREDEFINED) {
117 return -1;
118 }
119
120 pre.name = sl_pp_context_add_unique_str(context, name);
121 if (pre.name == -1) {
122 return -1;
123 }
124
125 pre.value = sl_pp_context_add_unique_str(context, value);
126 if (pre.value == -1) {
127 return -1;
128 }
129
130 context->predefined[context->num_predefined++] = pre;
131 return 0;
132 }
133
134 int
135 sl_pp_context_add_unique_str(struct sl_pp_context *context,
136 const char *str)
137 {
138 unsigned int size;
139 unsigned int offset = 0;
140
141 size = strlen(str) + 1;
142
143 /* Find out if this is a unique string. */
144 while (offset < context->cstr_pool_len) {
145 const char *str2;
146 unsigned int size2;
147
148 str2 = &context->cstr_pool[offset];
149 size2 = strlen(str2) + 1;
150 if (size == size2 && !memcmp(str, str2, size - 1)) {
151 return offset;
152 }
153
154 offset += size2;
155 }
156
157 if (context->cstr_pool_len + size > context->cstr_pool_max) {
158 context->cstr_pool_max = (context->cstr_pool_len + size + 0xffff) & ~0xffff;
159 context->cstr_pool = realloc(context->cstr_pool, context->cstr_pool_max);
160 }
161
162 if (!context->cstr_pool) {
163 strcpy(context->error_msg, "out of memory");
164 return -1;
165 }
166
167 offset = context->cstr_pool_len;
168 memcpy(&context->cstr_pool[offset], str, size);
169 context->cstr_pool_len += size;
170
171 return offset;
172 }
173
174 const char *
175 sl_pp_context_cstr(const struct sl_pp_context *context,
176 int offset)
177 {
178 if (offset == -1) {
179 return NULL;
180 }
181 return &context->cstr_pool[offset];
182 }