00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "bconfig.h"
00022 #include "system.h"
00023 #include "coretypes.h"
00024 #include "tm.h"
00025 #include "cpplib.h"
00026 #include "scan.h"
00027
00028 static void skip_to_closing_brace (cpp_reader *);
00029 static const cpp_token *get_a_token (cpp_reader *);
00030
00031 int brace_nesting = 0;
00032
00033
00034
00035
00036 int extern_C_braces_length = 0;
00037
00038 #define MAX_EXTERN_C_BRACES 200
00039 char extern_C_braces[MAX_EXTERN_C_BRACES];
00040 #define in_extern_C_brace (extern_C_braces_length>0)
00041
00042
00043
00044 int current_extern_C = 0;
00045
00046
00047 static const cpp_token *
00048 get_a_token (cpp_reader *pfile)
00049 {
00050 for (;;)
00051 {
00052 const cpp_token *result = cpp_get_token (pfile);
00053 if (result->type != CPP_PADDING)
00054 return result;
00055 }
00056 }
00057
00058 static void
00059 skip_to_closing_brace (cpp_reader *pfile)
00060 {
00061 int nesting = 1;
00062 for (;;)
00063 {
00064 enum cpp_ttype token = get_a_token (pfile)->type;
00065
00066 if (token == CPP_EOF)
00067 break;
00068 if (token == CPP_OPEN_BRACE)
00069 nesting++;
00070 if (token == CPP_CLOSE_BRACE && --nesting == 0)
00071 break;
00072 }
00073 }
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 int
00098 scan_decls (cpp_reader *pfile, int argc ATTRIBUTE_UNUSED,
00099 char **argv ATTRIBUTE_UNUSED)
00100 {
00101 int saw_extern, saw_inline;
00102 cpp_token prev_id;
00103 const cpp_token *token;
00104
00105 new_statement:
00106 token = get_a_token (pfile);
00107
00108 handle_statement:
00109 current_extern_C = 0;
00110 saw_extern = 0;
00111 saw_inline = 0;
00112 if (token->type == CPP_OPEN_BRACE)
00113 {
00114
00115 if (extern_C_braces_length
00116 && extern_C_braces[extern_C_braces_length - 1] == brace_nesting)
00117 extern_C_braces_length--;
00118 brace_nesting--;
00119 goto new_statement;
00120 }
00121 if (token->type == CPP_OPEN_BRACE)
00122 {
00123 brace_nesting++;
00124 goto new_statement;
00125 }
00126
00127 if (token->type == CPP_EOF)
00128 return 0;
00129
00130 if (token->type == CPP_SEMICOLON)
00131 goto new_statement;
00132 if (token->type != CPP_NAME)
00133 goto new_statement;
00134
00135 prev_id.type = CPP_EOF;
00136 for (;;)
00137 {
00138 switch (token->type)
00139 {
00140 default:
00141 goto handle_statement;
00142 case CPP_MULT:
00143 case CPP_AND:
00144
00145 break;
00146
00147 case CPP_COMMA:
00148 case CPP_SEMICOLON:
00149 if (prev_id.type != CPP_EOF && saw_extern)
00150 {
00151 recognized_extern (&prev_id);
00152 }
00153 if (token->type == CPP_COMMA)
00154 break;
00155
00156 case CPP_OPEN_BRACE: case CPP_CLOSE_BRACE:
00157 goto new_statement;
00158
00159 case CPP_EOF:
00160 return 0;
00161
00162 case CPP_OPEN_PAREN:
00163
00164 if (prev_id.type != CPP_EOF)
00165 {
00166 int nesting = 1;
00167 int have_arg_list = 0;
00168 const struct line_map *map;
00169 unsigned int line;
00170 for (;;)
00171 {
00172 token = get_a_token (pfile);
00173 if (token->type == CPP_OPEN_PAREN)
00174 nesting++;
00175 else if (token->type == CPP_CLOSE_PAREN)
00176 {
00177 nesting--;
00178 if (nesting == 0)
00179 break;
00180 }
00181 else if (token->type == CPP_EOF)
00182 break;
00183 else if (token->type == CPP_NAME
00184 || token->type == CPP_ELLIPSIS)
00185 have_arg_list = 1;
00186 }
00187 map = linemap_lookup (&line_table, token->src_loc);
00188 line = SOURCE_LINE (map, token->src_loc);
00189 recognized_function (&prev_id, line,
00190 (saw_inline ? 'I'
00191 : in_extern_C_brace || current_extern_C
00192 ? 'F' : 'f'), have_arg_list);
00193 token = get_a_token (pfile);
00194 if (token->type == CPP_OPEN_BRACE)
00195 {
00196
00197 skip_to_closing_brace (pfile);
00198 goto new_statement;
00199 }
00200
00201
00202
00203 while (token->type != CPP_SEMICOLON && token->type != CPP_EOF)
00204 token = get_a_token (pfile);
00205 goto new_statement;
00206 }
00207 break;
00208 case CPP_NAME:
00209
00210 if (cpp_ideq (token, "inline"))
00211 {
00212 saw_inline = 1;
00213 }
00214 else if (cpp_ideq (token, "extern"))
00215 {
00216 saw_extern = 1;
00217 token = get_a_token (pfile);
00218 if (token->type == CPP_STRING
00219 && token->val.str.len == 1
00220 && token->val.str.text[0] == 'C')
00221 {
00222 current_extern_C = 1;
00223 token = get_a_token (pfile);
00224 if (token->type == CPP_OPEN_BRACE)
00225 {
00226 brace_nesting++;
00227 extern_C_braces[extern_C_braces_length++]
00228 = brace_nesting;
00229 if (extern_C_braces_length >= MAX_EXTERN_C_BRACES)
00230 {
00231 fprintf (stderr,
00232 "Internal error: out-of-bounds index\n");
00233 exit (FATAL_EXIT_CODE);
00234 }
00235 goto new_statement;
00236 }
00237 }
00238 else
00239 continue;
00240 break;
00241 }
00242
00243 prev_id = *token;
00244 break;
00245 }
00246 token = get_a_token (pfile);
00247 }
00248 }