1
/**************************************************************************
3
* Copyright 2009 VMware, Inc.
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:
14
* The above copyright notice and this permission notice (including the
15
* next paragraph) shall be included in all copies or substantial portions
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.
26
**************************************************************************/
31
#include "sl_pp_context.h"
32
#include "sl_pp_public.h"
33
#include "sl_pp_macro.h"
34
#include "sl_pp_process.h"
38
_macro_init(struct sl_pp_macro *macro)
49
struct sl_pp_macro *macro;
51
macro = calloc(1, sizeof(struct sl_pp_macro));
59
_macro_destroy(struct sl_pp_macro *macro)
61
struct sl_pp_macro_formal_arg *arg = macro->arg;
64
struct sl_pp_macro_formal_arg *next_arg = arg->next;
74
sl_pp_macro_free(struct sl_pp_macro *macro)
77
struct sl_pp_macro *next_macro = macro->next;
79
_macro_destroy(macro);
86
sl_pp_macro_reset(struct sl_pp_macro *macro)
88
_macro_destroy(macro);
93
_out_number(struct sl_pp_context *context,
94
struct sl_pp_process_state *state,
98
struct sl_pp_token_info ti;
100
sprintf(buf, "%u", number);
102
ti.token = SL_PP_UINT;
103
ti.data._uint = sl_pp_context_add_unique_str(context, buf);
104
if (sl_pp_process_out(state, &ti)) {
105
strcpy(context->error_msg, "out of memory");
113
sl_pp_macro_expand(struct sl_pp_context *context,
114
struct sl_pp_token_buffer *tokens,
115
struct sl_pp_macro *local,
116
struct sl_pp_process_state *state,
117
enum sl_pp_macro_expand_behaviour behaviour)
119
int mute = (behaviour == sl_pp_macro_expand_mute);
120
struct sl_pp_token_info input;
122
struct sl_pp_macro *macro = NULL;
123
struct sl_pp_macro *actual_arg = NULL;
126
if (sl_pp_token_buffer_get(tokens, &input)) {
130
if (input.token != SL_PP_IDENTIFIER) {
131
strcpy(context->error_msg, "expected an identifier");
135
macro_name = input.data.identifier;
137
/* First look for predefined macros.
140
if (macro_name == context->dict.___LINE__) {
141
if (!mute && _out_number(context, state, context->line)) {
146
if (macro_name == context->dict.___FILE__) {
147
if (!mute && _out_number(context, state, context->file)) {
152
if (macro_name == context->dict.___VERSION__) {
153
if (!mute && _out_number(context, state, 110)) {
159
for (j = 0; j < context->num_predefined; j++) {
160
if (macro_name == context->predefined[j].name) {
162
struct sl_pp_token_info ti;
164
ti.token = SL_PP_UINT;
165
ti.data._uint = context->predefined[j].value;
166
if (sl_pp_process_out(state, &ti)) {
167
strcpy(context->error_msg, "out of memory");
175
/* Replace extension names with 1.
177
for (j = 0; j < context->num_extensions; j++) {
178
if (macro_name == context->extensions[j].name) {
179
if (!mute && _out_number(context, state, 1)) {
187
for (macro = local; macro; macro = macro->next) {
188
if (macro->name == macro_name) {
195
for (macro = context->macro; macro; macro = macro->next) {
196
if (macro->name == macro_name) {
203
if (behaviour == sl_pp_macro_expand_unknown_to_0) {
204
if (_out_number(context, state, 0)) {
205
strcpy(context->error_msg, "out of memory");
209
if (sl_pp_process_out(state, &input)) {
210
strcpy(context->error_msg, "out of memory");
217
if (macro->num_args >= 0) {
218
if (sl_pp_token_buffer_skip_white(tokens, &input)) {
221
if (input.token != SL_PP_LPAREN) {
222
strcpy(context->error_msg, "expected `('");
225
if (sl_pp_token_buffer_skip_white(tokens, &input)) {
228
sl_pp_token_buffer_unget(tokens, &input);
231
if (macro->num_args > 0) {
232
struct sl_pp_macro_formal_arg *formal_arg = macro->arg;
233
struct sl_pp_macro **pmacro = &actual_arg;
235
for (j = 0; j < (unsigned int)macro->num_args; j++) {
236
struct sl_pp_process_state arg_state;
238
unsigned int paren_nesting = 0;
239
struct sl_pp_token_info eof;
241
memset(&arg_state, 0, sizeof(arg_state));
244
if (sl_pp_token_buffer_get(tokens, &input)) {
247
switch (input.token) {
248
case SL_PP_WHITESPACE:
252
if (!paren_nesting) {
253
if (j < (unsigned int)macro->num_args - 1) {
256
strcpy(context->error_msg, "too many actual macro arguments");
260
if (sl_pp_process_out(&arg_state, &input)) {
261
strcpy(context->error_msg, "out of memory");
269
if (sl_pp_process_out(&arg_state, &input)) {
275
if (!paren_nesting) {
276
if (j == (unsigned int)macro->num_args - 1) {
279
strcpy(context->error_msg, "too few actual macro arguments");
284
if (sl_pp_process_out(&arg_state, &input)) {
290
case SL_PP_IDENTIFIER:
291
sl_pp_token_buffer_unget(tokens, &input);
292
if (sl_pp_macro_expand(context, tokens, local, &arg_state, sl_pp_macro_expand_normal)) {
298
strcpy(context->error_msg, "too few actual macro arguments");
302
if (sl_pp_process_out(&arg_state, &input)) {
308
eof.token = SL_PP_EOF;
309
if (sl_pp_process_out(&arg_state, &eof)) {
313
*pmacro = sl_pp_macro_new();
318
(**pmacro).name = formal_arg->name;
319
(**pmacro).body = arg_state.out;
321
formal_arg = formal_arg->next;
322
pmacro = &(**pmacro).next;
327
strcpy(context->error_msg, "out of memory");
334
/* Right paren for non-empty argument list has already been eaten. */
335
if (macro->num_args == 0) {
336
if (sl_pp_token_buffer_skip_white(tokens, &input)) {
339
if (input.token != SL_PP_RPAREN) {
340
strcpy(context->error_msg, "expected `)'");
345
/* XXX: This is all wrong, we should be ungetting all tokens
346
* back to the main token buffer.
349
struct sl_pp_token_buffer buffer;
353
for (j = 0; macro->body[j].token != SL_PP_EOF; j++) {
357
/* Create a context-less token buffer since we are not going to underrun
358
* its internal buffer.
360
if (sl_pp_token_buffer_init(&buffer, NULL)) {
361
strcpy(context->error_msg, "out of memory");
365
/* Unget the tokens in reverse order so later they will be fetched correctly.
368
sl_pp_token_buffer_unget(&buffer, ¯o->body[j - 1]);
374
struct sl_pp_token_info input;
376
sl_pp_token_buffer_get(&buffer, &input);
377
switch (input.token) {
379
if (sl_pp_process_out(state, &input)) {
380
strcpy(context->error_msg, "out of memory");
381
sl_pp_token_buffer_destroy(&buffer);
386
case SL_PP_IDENTIFIER:
387
sl_pp_token_buffer_unget(&buffer, &input);
388
if (sl_pp_macro_expand(context, &buffer, actual_arg, state, behaviour)) {
389
sl_pp_token_buffer_destroy(&buffer);
395
sl_pp_token_buffer_destroy(&buffer);
396
sl_pp_macro_free(actual_arg);
401
if (sl_pp_process_out(state, &input)) {
402
strcpy(context->error_msg, "out of memory");
403
sl_pp_token_buffer_destroy(&buffer);
412
sl_pp_macro_free(actual_arg);