nv50: fix build-predicate function
[mesa.git] / src / glsl / pp / sl_pp_line.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_macro.h"
32 #include "sl_pp_public.h"
33 #include "sl_pp_process.h"
34 #include "sl_pp_token.h"
35
36
37 int
38 sl_pp_process_line(struct sl_pp_context *context,
39 struct sl_pp_token_buffer *buffer,
40 struct sl_pp_process_state *pstate)
41 {
42 struct sl_pp_process_state state;
43 int found_end = 0;
44 int line_number = -1;
45 int file_number = -1;
46 unsigned int line;
47 unsigned int file;
48
49 memset(&state, 0, sizeof(state));
50 while (!found_end) {
51 struct sl_pp_token_info input;
52
53 sl_pp_token_buffer_get(buffer, &input);
54 switch (input.token) {
55 case SL_PP_WHITESPACE:
56 break;
57
58 case SL_PP_IDENTIFIER:
59 sl_pp_token_buffer_unget(buffer, &input);
60 if (sl_pp_macro_expand(context, buffer, NULL, &state, sl_pp_macro_expand_normal)) {
61 free(state.out);
62 return -1;
63 }
64 break;
65
66 case SL_PP_NEWLINE:
67 case SL_PP_EOF:
68 found_end = 1;
69 break;
70
71 default:
72 if (sl_pp_process_out(&state, &input)) {
73 strcpy(context->error_msg, "out of memory");
74 free(state.out);
75 return -1;
76 }
77 }
78 }
79
80 if (state.out_len > 0 && state.out[0].token == SL_PP_UINT) {
81 line_number = state.out[0].data._uint;
82 } else {
83 strcpy(context->error_msg, "expected a number after `#line'");
84 free(state.out);
85 return -1;
86 }
87
88 if (state.out_len > 1) {
89 if (state.out[1].token == SL_PP_UINT) {
90 file_number = state.out[1].data._uint;
91 } else {
92 strcpy(context->error_msg, "expected a number after line number");
93 free(state.out);
94 return -1;
95 }
96
97 if (state.out_len > 2) {
98 strcpy(context->error_msg, "expected an end of line after file number");
99 free(state.out);
100 return -1;
101 }
102 }
103
104 free(state.out);
105
106 line = atoi(sl_pp_context_cstr(context, line_number));
107 if (file_number != -1) {
108 file = atoi(sl_pp_context_cstr(context, file_number));
109 } else {
110 file = context->file;
111 }
112
113 if (context->line != line || context->file != file) {
114 struct sl_pp_token_info ti;
115
116 ti.token = SL_PP_LINE;
117 ti.data.line.lineno = line;
118 ti.data.line.fileno = file;
119 if (sl_pp_process_out(pstate, &ti)) {
120 strcpy(context->error_msg, "out of memory");
121 return -1;
122 }
123
124 context->line = line;
125 context->file = file;
126 }
127
128 return 0;
129 }