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