fix -Wunreachable-code warnings
[cparser] / token.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #include <config.h>
21
22 #include "token_t.h"
23 #include "symbol_t.h"
24
25 #include <assert.h>
26 #include <stdio.h>
27
28 #include "symbol.h"
29 #include "lang_features.h"
30 #include "adt/array.h"
31
32 static symbol_t *token_symbols[T_LAST_TOKEN];
33 static symbol_t *pp_token_symbols[TP_LAST_TOKEN];
34
35 const source_position_t builtin_source_position = { "<built-in>", 0 };
36
37 static int last_id;
38
39 static symbol_t *intern_register_token(token_type_t id, const char *string)
40 {
41         assert(0 <= id && id < T_LAST_TOKEN);
42         symbol_t *symbol = symbol_table_insert(string);
43         if (token_symbols[id] == NULL)
44                 token_symbols[id] = symbol;
45         return symbol;
46 }
47
48 static symbol_t *intern_register_pp_token(token_type_t id, const char *string)
49 {
50         assert(0 <= id && id < TP_LAST_TOKEN);
51         symbol_t *symbol = symbol_table_insert(string);
52         if (pp_token_symbols[id] == NULL)
53                 pp_token_symbols[id] = symbol;
54         return symbol;
55 }
56
57 static void register_token(unsigned mode, token_type_t id, const char *string)
58 {
59         if (id > 255) {
60                 assert(id >= last_id);
61                 last_id = id;
62         }
63         if (c_mode & mode) {
64                 symbol_t *symbol = intern_register_token(id, string);
65                 symbol->ID = id;
66         }
67 }
68
69 static void register_pp_token(unsigned mode, token_type_t id,
70                               const char *string)
71 {
72         if (! (c_mode & mode))
73                 return;
74
75         symbol_t *symbol = intern_register_token(id, string);
76         symbol->pp_ID = id;
77 }
78
79 void init_tokens(void)
80 {
81         memset(token_symbols, 0, T_LAST_TOKEN * sizeof(token_symbols[0]));
82         memset(pp_token_symbols, 0, TP_LAST_TOKEN * sizeof(pp_token_symbols[0]));
83
84         last_id = -2;
85
86 #define T(mode,x,str,val)  register_token(mode, T_##x, str);
87 #define TS(x,str,val)      intern_register_token(T_##x, str);
88 #include "tokens.inc"
89 #undef TS
90 #undef T
91
92 #define T(mode,x,str,val)  register_pp_token(mode, TP_##x, str);
93 #define TS(x,str,val)      intern_register_pp_token(TP_##x, str);
94 #include "tokens_preprocessor.inc"
95 #undef TS
96 #undef T
97 }
98
99 void exit_tokens(void)
100 {
101 }
102
103 void print_token_type(FILE *f, token_type_t token_type)
104 {
105         if(token_type == T_EOF) {
106                 fputs("end of file", f);
107                 return;
108         }
109         if(token_type == T_ERROR) {
110                 fputs("error", f);
111                 return;
112         }
113
114         int token_symbols_len = T_LAST_TOKEN;
115         if(token_type < 0 || token_type >= token_symbols_len) {
116                 fputs("invalid token", f);
117                 return;
118         }
119
120         const symbol_t *symbol = token_symbols[token_type];
121         if(symbol != NULL) {
122                 fputs(symbol->string, f);
123         } else {
124                 if(token_type >= 0 && token_type < 256) {
125                         fputc(token_type, f);
126                         return;
127                 }
128                 fputs("unknown token", f);
129         }
130 }
131
132 symbol_t *get_token_symbol(const token_t *token)
133 {
134         return token_symbols[token->type];
135 }
136
137 static void print_stringrep(const string_t *string, FILE *f)
138 {
139         for (size_t i = 0; i < string->size; ++i) {
140                 fputc(string->begin[i], f);
141         }
142 }
143
144 void print_token(FILE *f, const token_t *token)
145 {
146         switch(token->type) {
147         case T_IDENTIFIER:
148                 fprintf(f, "identifier '%s'", token->symbol->string);
149                 break;
150         case T_INTEGER:
151         case T_INTEGER_OCTAL:
152         case T_INTEGER_HEXADECIMAL:
153         case T_FLOATINGPOINT:
154         case T_FLOATINGPOINT_HEXADECIMAL:
155                 print_token_type(f, (token_type_t)token->type);
156                 fputs(" '", f);
157                 print_stringrep(&token->literal, f);
158                 if (token->symbol != NULL)
159                         fputs(token->symbol->string, f);
160                 fputc('\'', f);
161                 break;
162         case T_WIDE_STRING_LITERAL:
163         case T_STRING_LITERAL:
164                 print_token_type(f, (token_type_t)token->type);
165                 fprintf(f, " \"%s\"", token->literal.begin);
166                 break;
167         case T_CHARACTER_CONSTANT:
168         case T_WIDE_CHARACTER_CONSTANT:
169                 print_token_type(f, (token_type_t)token->type);
170                 fputs(" \'", f);
171                 print_stringrep(&token->literal, f);
172                 fputs("'", f);
173                 break;
174         default:
175                 fputc('\'', f);
176                 print_token_type(f, (token_type_t)token->type);
177                 fputc('\'', f);
178                 break;
179         }
180 }
181
182 void print_pp_token_type(FILE *f, int token_type)
183 {
184         if (token_type == TP_EOF) {
185                 fputs("end of file", f);
186                 return;
187         }
188         if (token_type == TP_ERROR) {
189                 fputs("error", f);
190                 return;
191         }
192
193         int token_symbols_len = TP_LAST_TOKEN;
194         if (token_type < 0 || token_type >= token_symbols_len) {
195                 fputs("invalid token", f);
196                 return;
197         }
198
199         const symbol_t *symbol = pp_token_symbols[token_type];
200         if (symbol != NULL) {
201                 fputs(symbol->string, f);
202         } else {
203                 if(token_type >= 0 && token_type < 256) {
204                         fputc(token_type, f);
205                         return;
206                 }
207                 fputs("unknown token", f);
208         }
209 }
210
211 void print_pp_token(FILE *f, const token_t *token)
212 {
213         switch((preprocessor_token_type_t) token->type) {
214         case TP_IDENTIFIER:
215                 fprintf(f, "identifier '%s'", token->symbol->string);
216                 break;
217         case TP_NUMBER:
218                 fprintf(f, "number '%s'", token->literal.begin);
219                 break;
220         case TP_STRING_LITERAL:
221                 fprintf(f, "string \"%s\"", token->literal.begin);
222                 break;
223         default:
224                 print_pp_token_type(f, (preprocessor_token_type_t) token->type);
225                 break;
226         }
227 }