scan-decls.c revision 96489
1/* scan-decls.c - Extracts declarations from cpp output. 2 Copyright (C) 1993, 1995, 1997, 1998, 3 1999, 2000 Free Software Foundation, Inc. 4 5This program is free software; you can redistribute it and/or modify it 6under the terms of the GNU General Public License as published by the 7Free Software Foundation; either version 2, or (at your option) any 8later version. 9 10This program is distributed in the hope that it will be useful, 11but WITHOUT ANY WARRANTY; without even the implied warranty of 12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13GNU General Public License for more details. 14 15You should have received a copy of the GNU General Public License 16along with this program; if not, write to the Free Software 17Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18 19 Written by Per Bothner <bothner@cygnus.com>, July 1993. */ 20 21#include "hconfig.h" 22#include "system.h" 23#include "cpplib.h" 24#include "scan.h" 25 26static void skip_to_closing_brace PARAMS ((cpp_reader *)); 27static const cpp_token *get_a_token PARAMS ((cpp_reader *)); 28 29int brace_nesting = 0; 30 31/* The first extern_C_braces_length elements of extern_C_braces 32 indicate the (brace nesting levels of) left braces that were 33 prefixed by extern "C". */ 34int extern_C_braces_length = 0; 35char extern_C_braces[20]; 36#define in_extern_C_brace (extern_C_braces_length>0) 37 38/* True if the function declaration currently being scanned is 39 prefixed by extern "C". */ 40int current_extern_C = 0; 41 42/* Get a token but skip padding. */ 43static const cpp_token * 44get_a_token (pfile) 45 cpp_reader *pfile; 46{ 47 for (;;) 48 { 49 const cpp_token *result = cpp_get_token (pfile); 50 if (result->type != CPP_PADDING) 51 return result; 52 } 53} 54 55static void 56skip_to_closing_brace (pfile) 57 cpp_reader *pfile; 58{ 59 int nesting = 1; 60 for (;;) 61 { 62 enum cpp_ttype token = get_a_token (pfile)->type; 63 64 if (token == CPP_EOF) 65 break; 66 if (token == CPP_OPEN_BRACE) 67 nesting++; 68 if (token == CPP_CLOSE_BRACE && --nesting == 0) 69 break; 70 } 71} 72 73/* This function scans a C source file (actually, the output of cpp), 74 reading from FP. It looks for function declarations, and 75 external variable declarations. 76 77 The following grammar (as well as some extra stuff) is recognized: 78 79 declaration: 80 (decl-specifier)* declarator ("," declarator)* ";" 81 decl-specifier: 82 identifier 83 keyword 84 extern "C" 85 declarator: 86 (ptr-operator)* dname [ "(" argument-declaration-list ")" ] 87 ptr-operator: 88 ("*" | "&") ("const" | "volatile")* 89 dname: 90 identifier 91 92Here dname is the actual name being declared. 93*/ 94 95int 96scan_decls (pfile, argc, argv) 97 cpp_reader *pfile; 98 int argc ATTRIBUTE_UNUSED; 99 char **argv ATTRIBUTE_UNUSED; 100{ 101 int saw_extern, saw_inline; 102 cpp_token prev_id; 103 const cpp_token *token; 104 105 new_statement: 106 token = get_a_token (pfile); 107 108 handle_statement: 109 current_extern_C = 0; 110 saw_extern = 0; 111 saw_inline = 0; 112 if (token->type == CPP_OPEN_BRACE) 113 { 114 /* Pop an 'extern "C"' nesting level, if appropriate. */ 115 if (extern_C_braces_length 116 && extern_C_braces[extern_C_braces_length - 1] == brace_nesting) 117 extern_C_braces_length--; 118 brace_nesting--; 119 goto new_statement; 120 } 121 if (token->type == CPP_OPEN_BRACE) 122 { 123 brace_nesting++; 124 goto new_statement; 125 } 126 127 if (token->type == CPP_EOF) 128 return 0; 129 130 if (token->type == CPP_SEMICOLON) 131 goto new_statement; 132 if (token->type != CPP_NAME) 133 goto new_statement; 134 135 prev_id.type = CPP_EOF; 136 for (;;) 137 { 138 switch (token->type) 139 { 140 default: 141 goto handle_statement; 142 case CPP_MULT: 143 case CPP_AND: 144 /* skip */ 145 break; 146 147 case CPP_COMMA: 148 case CPP_SEMICOLON: 149 if (prev_id.type != CPP_EOF && saw_extern) 150 { 151 recognized_extern (&prev_id); 152 } 153 if (token->type == CPP_COMMA) 154 break; 155 /* ... fall through ... */ 156 case CPP_OPEN_BRACE: case CPP_CLOSE_BRACE: 157 goto new_statement; 158 159 case CPP_EOF: 160 return 0; 161 162 case CPP_OPEN_PAREN: 163 /* Looks like this is the start of a formal parameter list. */ 164 if (prev_id.type != CPP_EOF) 165 { 166 int nesting = 1; 167 int have_arg_list = 0; 168 for (;;) 169 { 170 token = get_a_token (pfile); 171 if (token->type == CPP_OPEN_PAREN) 172 nesting++; 173 else if (token->type == CPP_CLOSE_PAREN) 174 { 175 nesting--; 176 if (nesting == 0) 177 break; 178 } 179 else if (token->type == CPP_EOF) 180 break; 181 else if (token->type == CPP_NAME 182 || token->type == CPP_ELLIPSIS) 183 have_arg_list = 1; 184 } 185 recognized_function (&prev_id, token->line, 186 (saw_inline ? 'I' 187 : in_extern_C_brace || current_extern_C 188 ? 'F' : 'f'), have_arg_list); 189 token = get_a_token (pfile); 190 if (token->type == CPP_OPEN_BRACE) 191 { 192 /* skip body of (normally) inline function */ 193 skip_to_closing_brace (pfile); 194 goto new_statement; 195 } 196 197 /* skip a possible __attribute__ or throw expression after the 198 parameter list */ 199 while (token->type != CPP_SEMICOLON && token->type != CPP_EOF) 200 token = get_a_token (pfile); 201 goto new_statement; 202 } 203 break; 204 case CPP_NAME: 205 /* "inline" and "extern" are recognized but skipped */ 206 if (cpp_ideq (token, "inline")) 207 { 208 saw_inline = 1; 209 } 210 else if (cpp_ideq (token, "extern")) 211 { 212 saw_extern = 1; 213 token = get_a_token (pfile); 214 if (token->type == CPP_STRING 215 && token->val.str.len == 1 216 && token->val.str.text[0] == 'C') 217 { 218 current_extern_C = 1; 219 token = get_a_token (pfile); 220 if (token->type == CPP_OPEN_BRACE) 221 { 222 brace_nesting++; 223 extern_C_braces[extern_C_braces_length++] 224 = brace_nesting; 225 goto new_statement; 226 } 227 } 228 else 229 continue; 230 break; 231 } 232 /* This may be the name of a variable or function. */ 233 prev_id = *token; 234 break; 235 } 236 token = get_a_token (pfile); 237 } 238} 239