OSDN Git Service

r600g: add POW instruction
[android-x86/external-mesa.git] / src / glsl / pp / sl_pp_version.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 #include "sl_pp_token.h"
33
34
35 int
36 sl_pp_version(struct sl_pp_context *context,
37               unsigned int *version)
38 {
39    struct sl_pp_token_peek peek;
40    unsigned int line = context->line;
41
42    /* Default values if `#version' is not present. */
43    *version = 110;
44
45    if (sl_pp_token_peek_init(&peek, &context->tokens)) {
46       return -1;
47    }
48
49    /* There can be multiple `#version' directives present.
50     * Accept the value of the last one.
51     */
52    for (;;) {
53       struct sl_pp_token_info input;
54       int found_hash = 0;
55       int found_version = 0;
56       int found_number = 0;
57       int found_end = 0;
58
59       /* Skip whitespace and newlines and seek for hash. */
60       while (!found_hash) {
61          if (sl_pp_token_peek_get(&peek, &input)) {
62             sl_pp_token_peek_destroy(&peek);
63             return -1;
64          }
65
66          switch (input.token) {
67          case SL_PP_NEWLINE:
68             line++;
69             break;
70
71          case SL_PP_WHITESPACE:
72             break;
73
74          case SL_PP_HASH:
75             found_hash = 1;
76             break;
77
78          default:
79             sl_pp_token_peek_destroy(&peek);
80             return 0;
81          }
82       }
83
84       /* Skip whitespace and seek for `version'. */
85       while (!found_version) {
86          if (sl_pp_token_peek_get(&peek, &input)) {
87             sl_pp_token_peek_destroy(&peek);
88             return -1;
89          }
90
91          switch (input.token) {
92          case SL_PP_WHITESPACE:
93             break;
94
95          case SL_PP_IDENTIFIER:
96             if (input.data.identifier != context->dict.version) {
97                sl_pp_token_peek_destroy(&peek);
98                return 0;
99             }
100             found_version = 1;
101             break;
102
103          default:
104             sl_pp_token_peek_destroy(&peek);
105             return 0;
106          }
107       }
108
109       sl_pp_token_peek_commit(&peek);
110
111       /* Skip whitespace and seek for version number. */
112       while (!found_number) {
113          if (sl_pp_token_buffer_get(&context->tokens, &input)) {
114             sl_pp_token_peek_destroy(&peek);
115             return -1;
116          }
117
118          switch (input.token) {
119          case SL_PP_WHITESPACE:
120             break;
121
122          case SL_PP_UINT:
123             *version = atoi(sl_pp_context_cstr(context, input.data._uint));
124             found_number = 1;
125             break;
126
127          default:
128             strcpy(context->error_msg, "expected version number after `#version'");
129             sl_pp_token_peek_destroy(&peek);
130             return -1;
131          }
132       }
133
134       /* Skip whitespace and seek for either newline or eof. */
135       while (!found_end) {
136          if (sl_pp_token_buffer_get(&context->tokens, &input)) {
137             sl_pp_token_peek_destroy(&peek);
138             return -1;
139          }
140
141          switch (input.token) {
142          case SL_PP_WHITESPACE:
143             break;
144
145          case SL_PP_NEWLINE:
146             line++;
147             /* pass thru */
148          case SL_PP_EOF:
149             context->line = line;
150             found_end = 1;
151             break;
152
153          default:
154             strcpy(context->error_msg, "expected end of line after version number");
155             sl_pp_token_peek_destroy(&peek);
156             return -1;
157          }
158       }
159    }
160
161    /* Should not get here. */
162 }