c++: Handle C++11 noexcept
[arduino-ctags.git] / c.c
blob0dd84d730ba23a7bc2e79d50cd44aabcb36bfa31
1 /*
2 * $Id: c.c 689 2008-12-13 21:17:36Z elliotth $
4 * Copyright (c) 1996-2003, Darren Hiebert
6 * This source code is released for free distribution under the terms of the
7 * GNU General Public License.
9 * This module contains functions for parsing and scanning C, C++ and Java
10 * source files.
14 * INCLUDE FILES
16 #include "general.h" /* must always come first */
18 #include <string.h>
19 #include <setjmp.h>
21 #include "debug.h"
22 #include "entry.h"
23 #include "get.h"
24 #include "keyword.h"
25 #include "options.h"
26 #include "parse.h"
27 #include "read.h"
28 #include "routines.h"
31 * MACROS
34 #define activeToken(st) ((st)->token [(int) (st)->tokenIndex])
35 #define parentDecl(st) ((st)->parent == NULL ? \
36 DECL_NONE : (st)->parent->declaration)
37 #define isType(token,t) (boolean) ((token)->type == (t))
38 #define insideEnumBody(st) ((st)->parent == NULL ? FALSE : \
39 (boolean) ((st)->parent->declaration == DECL_ENUM))
40 #define isExternCDecl(st,c) (boolean) ((c) == STRING_SYMBOL && \
41 ! (st)->haveQualifyingName && (st)->scope == SCOPE_EXTERN)
43 #define isOneOf(c,s) (boolean) (strchr ((s), (c)) != NULL)
45 #define isHighChar(c) ((c) != EOF && (unsigned char)(c) >= 0xc0)
48 * DATA DECLARATIONS
51 enum { NumTokens = 15 };
53 typedef enum eException {
54 ExceptionNone, ExceptionEOF, ExceptionFormattingError,
55 ExceptionBraceFormattingError
56 } exception_t;
58 /* Used to specify type of keyword.
60 typedef enum eKeywordId {
61 KEYWORD_NONE = -1,
62 KEYWORD_ATTRIBUTE, KEYWORD_ABSTRACT,
63 KEYWORD_BOOLEAN, KEYWORD_BYTE, KEYWORD_BAD_STATE, KEYWORD_BAD_TRANS,
64 KEYWORD_BIND, KEYWORD_BIND_VAR, KEYWORD_BIT,
65 KEYWORD_CASE, KEYWORD_CATCH, KEYWORD_CHAR, KEYWORD_CLASS, KEYWORD_CONST,
66 KEYWORD_CONSTRAINT, KEYWORD_COVERAGE_BLOCK, KEYWORD_COVERAGE_DEF,
67 KEYWORD_DEFAULT, KEYWORD_DELEGATE, KEYWORD_DELETE, KEYWORD_DO,
68 KEYWORD_DOUBLE,
69 KEYWORD_ELSE, KEYWORD_ENUM, KEYWORD_EXPLICIT, KEYWORD_EXTERN,
70 KEYWORD_EXTENDS, KEYWORD_EVENT,
71 KEYWORD_FINAL, KEYWORD_FLOAT, KEYWORD_FOR, KEYWORD_FOREACH,
72 KEYWORD_FRIEND, KEYWORD_FUNCTION,
73 KEYWORD_GOTO,
74 KEYWORD_IF, KEYWORD_IMPLEMENTS, KEYWORD_IMPORT, KEYWORD_INLINE, KEYWORD_INT,
75 KEYWORD_INOUT, KEYWORD_INPUT, KEYWORD_INTEGER, KEYWORD_INTERFACE,
76 KEYWORD_INTERNAL,
77 KEYWORD_LOCAL, KEYWORD_LONG,
78 KEYWORD_M_BAD_STATE, KEYWORD_M_BAD_TRANS, KEYWORD_M_STATE, KEYWORD_M_TRANS,
79 KEYWORD_MUTABLE,
80 KEYWORD_NAMESPACE, KEYWORD_NEW, KEYWORD_NEWCOV, KEYWORD_NATIVE, KEYWORD_NOEXCEPT,
81 KEYWORD_OPERATOR, KEYWORD_OUTPUT, KEYWORD_OVERLOAD, KEYWORD_OVERRIDE,
82 KEYWORD_PACKED, KEYWORD_PORT, KEYWORD_PACKAGE, KEYWORD_PRIVATE,
83 KEYWORD_PROGRAM, KEYWORD_PROTECTED, KEYWORD_PUBLIC,
84 KEYWORD_REGISTER, KEYWORD_RETURN,
85 KEYWORD_SHADOW, KEYWORD_STATE,
86 KEYWORD_SHORT, KEYWORD_SIGNED, KEYWORD_STATIC, KEYWORD_STATIC_ASSERT, KEYWORD_STRING,
87 KEYWORD_STRUCT, KEYWORD_SWITCH, KEYWORD_SYNCHRONIZED,
88 KEYWORD_TASK, KEYWORD_TEMPLATE, KEYWORD_THIS, KEYWORD_THROW,
89 KEYWORD_THROWS, KEYWORD_TRANSIENT, KEYWORD_TRANS, KEYWORD_TRANSITION,
90 KEYWORD_TRY, KEYWORD_TYPEDEF, KEYWORD_TYPENAME,
91 KEYWORD_UINT, KEYWORD_ULONG, KEYWORD_UNION, KEYWORD_UNSIGNED, KEYWORD_USHORT,
92 KEYWORD_USING,
93 KEYWORD_VIRTUAL, KEYWORD_VOID, KEYWORD_VOLATILE,
94 KEYWORD_WCHAR_T, KEYWORD_WHILE
95 } keywordId;
97 /* Used to determine whether keyword is valid for the current language and
98 * what its ID is.
100 typedef struct sKeywordDesc {
101 const char *name;
102 keywordId id;
103 short isValid [5]; /* indicates languages for which kw is valid */
104 } keywordDesc;
106 /* Used for reporting the type of object parsed by nextToken ().
108 typedef enum eTokenType {
109 TOKEN_NONE, /* none */
110 TOKEN_ARGS, /* a parenthetical pair and its contents */
111 TOKEN_BRACE_CLOSE,
112 TOKEN_BRACE_OPEN,
113 TOKEN_COLON, /* the colon character */
114 TOKEN_COMMA, /* the comma character */
115 TOKEN_DOUBLE_COLON, /* double colon indicates nested-name-specifier */
116 TOKEN_KEYWORD,
117 TOKEN_NAME, /* an unknown name */
118 TOKEN_PACKAGE, /* a Java package name */
119 TOKEN_PAREN_NAME, /* a single name in parentheses */
120 TOKEN_SEMICOLON, /* the semicolon character */
121 TOKEN_SPEC, /* a storage class specifier, qualifier, type, etc. */
122 TOKEN_STAR, /* pointer * detection */
123 TOKEN_AMPERSAND, /* ampersand & detection */
124 TOKEN_COUNT
125 } tokenType;
127 /* This describes the scoping of the current statement.
129 typedef enum eTagScope {
130 SCOPE_GLOBAL, /* no storage class specified */
131 SCOPE_STATIC, /* static storage class */
132 SCOPE_EXTERN, /* external storage class */
133 SCOPE_FRIEND, /* declares access only */
134 SCOPE_TYPEDEF, /* scoping depends upon context */
135 SCOPE_COUNT
136 } tagScope;
138 typedef enum eDeclaration {
139 DECL_NONE,
140 DECL_BASE, /* base type (default) */
141 DECL_CLASS,
142 DECL_ENUM,
143 DECL_EVENT,
144 DECL_FUNCTION,
145 DECL_IGNORE, /* non-taggable "declaration" */
146 DECL_INTERFACE,
147 DECL_NAMESPACE,
148 DECL_NOMANGLE, /* C++ name demangling block */
149 DECL_PACKAGE,
150 DECL_PROGRAM, /* Vera program */
151 DECL_STRUCT,
152 DECL_TASK, /* Vera task */
153 DECL_UNION,
154 DECL_COUNT
155 } declType;
157 typedef enum eVisibilityType {
158 ACCESS_UNDEFINED,
159 ACCESS_LOCAL,
160 ACCESS_PRIVATE,
161 ACCESS_PROTECTED,
162 ACCESS_PUBLIC,
163 ACCESS_DEFAULT, /* Java-specific */
164 ACCESS_COUNT
165 } accessType;
167 /* Information about the parent class of a member (if any).
169 typedef struct sMemberInfo {
170 accessType access; /* access of current statement */
171 accessType accessDefault; /* access default for current statement */
172 } memberInfo;
174 typedef struct sTokenInfo {
175 tokenType type;
176 keywordId keyword;
177 vString* name; /* the name of the token */
178 unsigned long lineNumber; /* line number of tag */
179 fpos_t filePosition; /* file position of line containing name */
180 } tokenInfo;
182 typedef enum eImplementation {
183 IMP_DEFAULT,
184 IMP_ABSTRACT,
185 IMP_VIRTUAL,
186 IMP_PURE_VIRTUAL,
187 IMP_COUNT
188 } impType;
190 /* Describes the statement currently undergoing analysis.
192 typedef struct sStatementInfo {
193 tagScope scope;
194 declType declaration; /* specifier associated with TOKEN_SPEC */
195 boolean gotName; /* was a name parsed yet? */
196 boolean haveQualifyingName; /* do we have a name we are considering? */
197 boolean gotParenName; /* was a name inside parentheses parsed yet? */
198 boolean gotArgs; /* was a list of parameters parsed yet? */
199 boolean isPointer; /* is 'name' a pointer? */
200 boolean inFunction; /* are we inside of a function? */
201 boolean assignment; /* have we handled an '='? */
202 boolean notVariable; /* has a variable declaration been disqualified ? */
203 impType implementation; /* abstract or concrete implementation? */
204 unsigned int tokenIndex; /* currently active token */
205 tokenInfo* token [(int) NumTokens];
206 tokenInfo* context; /* accumulated scope of current statement */
207 tokenInfo* blockName; /* name of current block */
208 memberInfo member; /* information regarding parent class/struct */
209 vString* parentClasses; /* parent classes */
210 struct sStatementInfo *parent; /* statement we are nested within */
211 } statementInfo;
213 /* Describes the type of tag being generated.
215 typedef enum eTagType {
216 TAG_UNDEFINED,
217 TAG_CLASS, /* class name */
218 TAG_ENUM, /* enumeration name */
219 TAG_ENUMERATOR, /* enumerator (enumeration value) */
220 TAG_EVENT, /* event */
221 TAG_FIELD, /* field (Java) */
222 TAG_FUNCTION, /* function definition */
223 TAG_INTERFACE, /* interface declaration */
224 TAG_LOCAL, /* local variable definition */
225 TAG_MEMBER, /* structure, class or interface member */
226 TAG_METHOD, /* method declaration */
227 TAG_NAMESPACE, /* namespace name */
228 TAG_PACKAGE, /* package name */
229 TAG_PROGRAM, /* program name */
230 TAG_PROPERTY, /* property name */
231 TAG_PROTOTYPE, /* function prototype or declaration */
232 TAG_STRUCT, /* structure name */
233 TAG_TASK, /* task name */
234 TAG_TYPEDEF, /* typedef name */
235 TAG_UNION, /* union name */
236 TAG_VARIABLE, /* variable definition */
237 TAG_EXTERN_VAR, /* external variable declaration */
238 TAG_COUNT /* must be last */
239 } tagType;
241 typedef struct sParenInfo {
242 boolean isPointer;
243 boolean isParamList;
244 boolean isKnrParamList;
245 boolean isNameCandidate;
246 boolean invalidContents;
247 boolean nestedArgs;
248 unsigned int parameterCount;
249 } parenInfo;
252 * DATA DEFINITIONS
255 static jmp_buf Exception;
257 static langType Lang_c;
258 static langType Lang_cpp;
259 static langType Lang_csharp;
260 static langType Lang_java;
261 static langType Lang_vera;
262 static vString *Signature;
263 static boolean CollectingSignature;
264 static vString *ReturnType;
266 /* Number used to uniquely identify anonymous structs and unions. */
267 static int AnonymousID = 0;
269 /* Used to index into the CKinds table. */
270 typedef enum {
271 CK_UNDEFINED = -1,
272 CK_CLASS, CK_DEFINE, CK_ENUMERATOR, CK_FUNCTION,
273 CK_ENUMERATION, CK_LOCAL, CK_MEMBER, CK_NAMESPACE, CK_PROTOTYPE,
274 CK_STRUCT, CK_TYPEDEF, CK_UNION, CK_VARIABLE,
275 CK_EXTERN_VARIABLE
276 } cKind;
278 static kindOption CKinds [] = {
279 { TRUE, 'c', "class", "classes"},
280 { TRUE, 'd', "macro", "macro definitions"},
281 { TRUE, 'e', "enumerator", "enumerators (values inside an enumeration)"},
282 { TRUE, 'f', "function", "function definitions"},
283 { TRUE, 'g', "enum", "enumeration names"},
284 { FALSE, 'l', "local", "local variables"},
285 { TRUE, 'm', "member", "class, struct, and union members"},
286 { TRUE, 'n', "namespace", "namespaces"},
287 { FALSE, 'p', "prototype", "function prototypes"},
288 { TRUE, 's', "struct", "structure names"},
289 { TRUE, 't', "typedef", "typedefs"},
290 { TRUE, 'u', "union", "union names"},
291 { TRUE, 'v', "variable", "variable definitions"},
292 { FALSE, 'x', "externvar", "external and forward variable declarations"},
295 typedef enum {
296 CSK_UNDEFINED = -1,
297 CSK_CLASS, CSK_DEFINE, CSK_ENUMERATOR, CSK_EVENT, CSK_FIELD,
298 CSK_ENUMERATION, CSK_INTERFACE, CSK_LOCAL, CSK_METHOD,
299 CSK_NAMESPACE, CSK_PROPERTY, CSK_STRUCT, CSK_TYPEDEF
300 } csharpKind;
302 static kindOption CsharpKinds [] = {
303 { TRUE, 'c', "class", "classes"},
304 { TRUE, 'd', "macro", "macro definitions"},
305 { TRUE, 'e', "enumerator", "enumerators (values inside an enumeration)"},
306 { TRUE, 'E', "event", "events"},
307 { TRUE, 'f', "field", "fields"},
308 { TRUE, 'g', "enum", "enumeration names"},
309 { TRUE, 'i', "interface", "interfaces"},
310 { FALSE, 'l', "local", "local variables"},
311 { TRUE, 'm', "method", "methods"},
312 { TRUE, 'n', "namespace", "namespaces"},
313 { TRUE, 'p', "property", "properties"},
314 { TRUE, 's', "struct", "structure names"},
315 { TRUE, 't', "typedef", "typedefs"},
318 /* Used to index into the JavaKinds table. */
319 typedef enum {
320 JK_UNDEFINED = -1,
321 JK_CLASS, JK_ENUM_CONSTANT, JK_FIELD, JK_ENUM, JK_INTERFACE,
322 JK_LOCAL, JK_METHOD, JK_PACKAGE, JK_ACCESS, JK_CLASS_PREFIX
323 } javaKind;
325 static kindOption JavaKinds [] = {
326 { TRUE, 'c', "class", "classes"},
327 { TRUE, 'e', "enum constant", "enum constants"},
328 { TRUE, 'f', "field", "fields"},
329 { TRUE, 'g', "enum", "enum types"},
330 { TRUE, 'i', "interface", "interfaces"},
331 { FALSE, 'l', "local", "local variables"},
332 { TRUE, 'm', "method", "methods"},
333 { TRUE, 'p', "package", "packages"},
336 /* Used to index into the VeraKinds table. */
337 typedef enum {
338 VK_UNDEFINED = -1,
339 VK_CLASS, VK_DEFINE, VK_ENUMERATOR, VK_FUNCTION,
340 VK_ENUMERATION, VK_LOCAL, VK_MEMBER, VK_PROGRAM, VK_PROTOTYPE,
341 VK_TASK, VK_TYPEDEF, VK_VARIABLE,
342 VK_EXTERN_VARIABLE
343 } veraKind;
345 static kindOption VeraKinds [] = {
346 { TRUE, 'c', "class", "classes"},
347 { TRUE, 'd', "macro", "macro definitions"},
348 { TRUE, 'e', "enumerator", "enumerators (values inside an enumeration)"},
349 { TRUE, 'f', "function", "function definitions"},
350 { TRUE, 'g', "enum", "enumeration names"},
351 { FALSE, 'l', "local", "local variables"},
352 { TRUE, 'm', "member", "class, struct, and union members"},
353 { TRUE, 'p', "program", "programs"},
354 { FALSE, 'P', "prototype", "function prototypes"},
355 { TRUE, 't', "task", "tasks"},
356 { TRUE, 'T', "typedef", "typedefs"},
357 { TRUE, 'v', "variable", "variable definitions"},
358 { FALSE, 'x', "externvar", "external variable declarations"}
361 static const keywordDesc KeywordTable [] = {
362 /* C++ */
363 /* ANSI C | C# Java */
364 /* | | | | Vera */
365 /* keyword keyword ID | | | | | */
366 { "__attribute__", KEYWORD_ATTRIBUTE, { 1, 1, 1, 0, 0 } },
367 { "abstract", KEYWORD_ABSTRACT, { 0, 0, 1, 1, 0 } },
368 { "bad_state", KEYWORD_BAD_STATE, { 0, 0, 0, 0, 1 } },
369 { "bad_trans", KEYWORD_BAD_TRANS, { 0, 0, 0, 0, 1 } },
370 { "bind", KEYWORD_BIND, { 0, 0, 0, 0, 1 } },
371 { "bind_var", KEYWORD_BIND_VAR, { 0, 0, 0, 0, 1 } },
372 { "bit", KEYWORD_BIT, { 0, 0, 0, 0, 1 } },
373 { "boolean", KEYWORD_BOOLEAN, { 0, 0, 0, 1, 0 } },
374 { "byte", KEYWORD_BYTE, { 0, 0, 0, 1, 0 } },
375 { "case", KEYWORD_CASE, { 1, 1, 1, 1, 0 } },
376 { "catch", KEYWORD_CATCH, { 0, 1, 1, 0, 0 } },
377 { "char", KEYWORD_CHAR, { 1, 1, 1, 1, 0 } },
378 { "class", KEYWORD_CLASS, { 0, 1, 1, 1, 1 } },
379 { "const", KEYWORD_CONST, { 1, 1, 1, 1, 0 } },
380 { "constraint", KEYWORD_CONSTRAINT, { 0, 0, 0, 0, 1 } },
381 { "coverage_block", KEYWORD_COVERAGE_BLOCK, { 0, 0, 0, 0, 1 } },
382 { "coverage_def", KEYWORD_COVERAGE_DEF, { 0, 0, 0, 0, 1 } },
383 { "do", KEYWORD_DO, { 1, 1, 1, 1, 0 } },
384 { "default", KEYWORD_DEFAULT, { 1, 1, 1, 1, 0 } },
385 { "delegate", KEYWORD_DELEGATE, { 0, 0, 1, 0, 0 } },
386 { "delete", KEYWORD_DELETE, { 0, 1, 0, 0, 0 } },
387 { "double", KEYWORD_DOUBLE, { 1, 1, 1, 1, 0 } },
388 { "else", KEYWORD_ELSE, { 1, 1, 1, 1, 0 } },
389 { "enum", KEYWORD_ENUM, { 1, 1, 1, 1, 1 } },
390 { "event", KEYWORD_EVENT, { 0, 0, 1, 0, 1 } },
391 { "explicit", KEYWORD_EXPLICIT, { 0, 1, 1, 0, 0 } },
392 { "extends", KEYWORD_EXTENDS, { 0, 0, 0, 1, 1 } },
393 { "extern", KEYWORD_EXTERN, { 1, 1, 1, 0, 1 } },
394 { "final", KEYWORD_FINAL, { 0, 0, 0, 1, 0 } },
395 { "float", KEYWORD_FLOAT, { 1, 1, 1, 1, 0 } },
396 { "for", KEYWORD_FOR, { 1, 1, 1, 1, 0 } },
397 { "foreach", KEYWORD_FOREACH, { 0, 0, 1, 0, 0 } },
398 { "friend", KEYWORD_FRIEND, { 0, 1, 0, 0, 0 } },
399 { "function", KEYWORD_FUNCTION, { 0, 0, 0, 0, 1 } },
400 { "goto", KEYWORD_GOTO, { 1, 1, 1, 1, 0 } },
401 { "if", KEYWORD_IF, { 1, 1, 1, 1, 0 } },
402 { "implements", KEYWORD_IMPLEMENTS, { 0, 0, 0, 1, 0 } },
403 { "import", KEYWORD_IMPORT, { 0, 0, 0, 1, 0 } },
404 { "inline", KEYWORD_INLINE, { 0, 1, 0, 0, 0 } },
405 { "inout", KEYWORD_INOUT, { 0, 0, 0, 0, 1 } },
406 { "input", KEYWORD_INPUT, { 0, 0, 0, 0, 1 } },
407 { "int", KEYWORD_INT, { 1, 1, 1, 1, 0 } },
408 { "integer", KEYWORD_INTEGER, { 0, 0, 0, 0, 1 } },
409 { "interface", KEYWORD_INTERFACE, { 0, 0, 1, 1, 1 } },
410 { "internal", KEYWORD_INTERNAL, { 0, 0, 1, 0, 0 } },
411 { "local", KEYWORD_LOCAL, { 0, 0, 0, 0, 1 } },
412 { "long", KEYWORD_LONG, { 1, 1, 1, 1, 0 } },
413 { "m_bad_state", KEYWORD_M_BAD_STATE, { 0, 0, 0, 0, 1 } },
414 { "m_bad_trans", KEYWORD_M_BAD_TRANS, { 0, 0, 0, 0, 1 } },
415 { "m_state", KEYWORD_M_STATE, { 0, 0, 0, 0, 1 } },
416 { "m_trans", KEYWORD_M_TRANS, { 0, 0, 0, 0, 1 } },
417 { "mutable", KEYWORD_MUTABLE, { 0, 1, 0, 0, 0 } },
418 { "namespace", KEYWORD_NAMESPACE, { 0, 1, 1, 0, 0 } },
419 { "native", KEYWORD_NATIVE, { 0, 0, 0, 1, 0 } },
420 { "new", KEYWORD_NEW, { 0, 1, 1, 1, 0 } },
421 { "newcov", KEYWORD_NEWCOV, { 0, 0, 0, 0, 1 } },
422 { "noexcept", KEYWORD_NOEXCEPT, { 0, 1, 0, 0, 0 } },
423 { "operator", KEYWORD_OPERATOR, { 0, 1, 1, 0, 0 } },
424 { "output", KEYWORD_OUTPUT, { 0, 0, 0, 0, 1 } },
425 { "overload", KEYWORD_OVERLOAD, { 0, 1, 0, 0, 0 } },
426 { "override", KEYWORD_OVERRIDE, { 0, 0, 1, 0, 0 } },
427 { "package", KEYWORD_PACKAGE, { 0, 0, 0, 1, 0 } },
428 { "packed", KEYWORD_PACKED, { 0, 0, 0, 0, 1 } },
429 { "port", KEYWORD_PORT, { 0, 0, 0, 0, 1 } },
430 { "private", KEYWORD_PRIVATE, { 0, 1, 1, 1, 0 } },
431 { "program", KEYWORD_PROGRAM, { 0, 0, 0, 0, 1 } },
432 { "protected", KEYWORD_PROTECTED, { 0, 1, 1, 1, 1 } },
433 { "public", KEYWORD_PUBLIC, { 0, 1, 1, 1, 1 } },
434 { "register", KEYWORD_REGISTER, { 1, 1, 0, 0, 0 } },
435 { "return", KEYWORD_RETURN, { 1, 1, 1, 1, 0 } },
436 { "shadow", KEYWORD_SHADOW, { 0, 0, 0, 0, 1 } },
437 { "short", KEYWORD_SHORT, { 1, 1, 1, 1, 0 } },
438 { "signed", KEYWORD_SIGNED, { 1, 1, 0, 0, 0 } },
439 { "state", KEYWORD_STATE, { 0, 0, 0, 0, 1 } },
440 { "static", KEYWORD_STATIC, { 1, 1, 1, 1, 1 } },
441 { "static_assert", KEYWORD_STATIC_ASSERT, { 0, 1, 0, 0, 0} },
442 { "string", KEYWORD_STRING, { 0, 0, 1, 0, 1 } },
443 { "struct", KEYWORD_STRUCT, { 1, 1, 1, 0, 0 } },
444 { "switch", KEYWORD_SWITCH, { 1, 1, 1, 1, 0 } },
445 { "synchronized", KEYWORD_SYNCHRONIZED, { 0, 0, 0, 1, 0 } },
446 { "task", KEYWORD_TASK, { 0, 0, 0, 0, 1 } },
447 { "template", KEYWORD_TEMPLATE, { 0, 1, 0, 0, 0 } },
448 { "this", KEYWORD_THIS, { 0, 1, 1, 1, 0 } },
449 { "throw", KEYWORD_THROW, { 0, 1, 1, 1, 0 } },
450 { "throws", KEYWORD_THROWS, { 0, 0, 0, 1, 0 } },
451 { "trans", KEYWORD_TRANS, { 0, 0, 0, 0, 1 } },
452 { "transition", KEYWORD_TRANSITION, { 0, 0, 0, 0, 1 } },
453 { "transient", KEYWORD_TRANSIENT, { 0, 0, 0, 1, 0 } },
454 { "try", KEYWORD_TRY, { 0, 1, 1, 0, 0 } },
455 { "typedef", KEYWORD_TYPEDEF, { 1, 1, 1, 0, 1 } },
456 { "typename", KEYWORD_TYPENAME, { 0, 1, 0, 0, 0 } },
457 { "uint", KEYWORD_UINT, { 0, 0, 1, 0, 0 } },
458 { "ulong", KEYWORD_ULONG, { 0, 0, 1, 0, 0 } },
459 { "union", KEYWORD_UNION, { 1, 1, 0, 0, 0 } },
460 { "unsigned", KEYWORD_UNSIGNED, { 1, 1, 1, 0, 0 } },
461 { "ushort", KEYWORD_USHORT, { 0, 0, 1, 0, 0 } },
462 { "using", KEYWORD_USING, { 0, 1, 1, 0, 0 } },
463 { "virtual", KEYWORD_VIRTUAL, { 0, 1, 1, 0, 1 } },
464 { "void", KEYWORD_VOID, { 1, 1, 1, 1, 1 } },
465 { "volatile", KEYWORD_VOLATILE, { 1, 1, 1, 1, 0 } },
466 { "wchar_t", KEYWORD_WCHAR_T, { 1, 1, 1, 0, 0 } },
467 { "while", KEYWORD_WHILE, { 1, 1, 1, 1, 0 } }
471 * FUNCTION PROTOTYPES
473 static void createTags (const unsigned int nestLevel, statementInfo *const parent);
476 * FUNCTION DEFINITIONS
479 extern boolean includingDefineTags (void)
481 return CKinds [CK_DEFINE].enabled;
485 * Token management
488 static void initToken (tokenInfo* const token)
490 token->type = TOKEN_NONE;
491 token->keyword = KEYWORD_NONE;
492 token->lineNumber = getSourceLineNumber ();
493 token->filePosition = getInputFilePosition ();
494 vStringClear (token->name);
497 static void advanceToken (statementInfo* const st)
499 if (st->tokenIndex >= (unsigned int) NumTokens - 1)
500 st->tokenIndex = 0;
501 else
502 ++st->tokenIndex;
503 initToken (st->token [st->tokenIndex]);
506 static tokenInfo *prevToken (const statementInfo *const st, unsigned int n)
508 unsigned int tokenIndex;
509 unsigned int num = (unsigned int) NumTokens;
510 Assert (n < num);
511 tokenIndex = (st->tokenIndex + num - n) % num;
512 return st->token [tokenIndex];
515 static void setToken (statementInfo *const st, const tokenType type)
517 tokenInfo *token;
518 token = activeToken (st);
519 initToken (token);
520 token->type = type;
523 static void retardToken (statementInfo *const st)
525 if (st->tokenIndex == 0)
526 st->tokenIndex = (unsigned int) NumTokens - 1;
527 else
528 --st->tokenIndex;
529 setToken (st, TOKEN_NONE);
532 static tokenInfo *newToken (void)
534 tokenInfo *const token = xMalloc (1, tokenInfo);
535 token->name = vStringNew ();
536 initToken (token);
537 return token;
540 static void deleteToken (tokenInfo *const token)
542 if (token != NULL)
544 vStringDelete (token->name);
545 eFree (token);
549 static const char *accessString (const accessType access)
551 static const char *const names [] = {
552 "?", "local", "private", "protected", "public", "default"
554 Assert (sizeof (names) / sizeof (names [0]) == ACCESS_COUNT);
555 Assert ((int) access < ACCESS_COUNT);
556 return names [(int) access];
559 static const char *implementationString (const impType imp)
561 static const char *const names [] ={
562 "?", "abstract", "virtual", "pure virtual"
564 Assert (sizeof (names) / sizeof (names [0]) == IMP_COUNT);
565 Assert ((int) imp < IMP_COUNT);
566 return names [(int) imp];
570 * Debugging functions
572 #ifdef DEBUG
574 #define boolString(c) ((c) ? "TRUE" : "FALSE")
576 static const char *tokenString (const tokenType type)
578 static const char *const names [] = {
579 "none", "args", "}", "{", "colon", "comma", "double colon", "keyword",
580 "name", "package", "paren-name", "semicolon", "specifier", "star", "ampersand"
582 Assert (sizeof (names) / sizeof (names [0]) == TOKEN_COUNT);
583 Assert ((int) type < TOKEN_COUNT);
584 return names [(int) type];
587 static const char *scopeString (const tagScope scope)
589 static const char *const names [] = {
590 "global", "static", "extern", "friend", "typedef"
592 Assert (sizeof (names) / sizeof (names [0]) == SCOPE_COUNT);
593 Assert ((int) scope < SCOPE_COUNT);
594 return names [(int) scope];
597 static const char *declString (const declType declaration)
599 static const char *const names [] = {
600 "?", "base", "class", "enum", "event", "function", "ignore",
601 "interface", "namespace", "no mangle", "package", "program",
602 "struct", "task", "union",
604 Assert (sizeof (names) / sizeof (names [0]) == DECL_COUNT);
605 Assert ((int) declaration < DECL_COUNT);
606 return names [(int) declaration];
609 static const char *keywordString (const keywordId keyword)
611 const size_t count = sizeof (KeywordTable) / sizeof (KeywordTable [0]);
612 const char *name = "none";
613 size_t i;
614 for (i = 0 ; i < count ; ++i)
616 const keywordDesc *p = &KeywordTable [i];
617 if (p->id == keyword)
619 name = p->name;
620 break;
623 return name;
626 static void __unused__ pt (tokenInfo *const token)
628 if (isType (token, TOKEN_NAME))
629 printf ("type: %-12s: %-13s line: %lu\n",
630 tokenString (token->type), vStringValue (token->name),
631 token->lineNumber);
632 else if (isType (token, TOKEN_KEYWORD))
633 printf ("type: %-12s: %-13s line: %lu\n",
634 tokenString (token->type), keywordString (token->keyword),
635 token->lineNumber);
636 else
637 printf ("type: %-12s line: %lu\n",
638 tokenString (token->type), token->lineNumber);
641 static void __unused__ ps (statementInfo *const st)
643 unsigned int i;
644 printf ("scope: %s decl: %s gotName: %s gotParenName: %s isPointer: %s\n",
645 scopeString (st->scope), declString (st->declaration),
646 boolString (st->gotName), boolString (st->gotParenName), boolString (st->isPointer));
647 printf ("haveQualifyingName: %s\n", boolString (st->haveQualifyingName));
648 printf ("access: %s default: %s\n", accessString (st->member.access),
649 accessString (st->member.accessDefault));
650 printf ("active token : ");
651 pt (activeToken (st));
652 for (i = 1 ; i < (unsigned int) NumTokens ; ++i)
654 printf ("prev %u : ", i);
655 pt (prevToken (st, i));
657 printf ("context: ");
658 pt (st->context);
661 #endif
664 * Statement management
667 static boolean isContextualKeyword (const tokenInfo *const token)
669 boolean result;
670 switch (token->keyword)
672 case KEYWORD_CLASS:
673 case KEYWORD_ENUM:
674 case KEYWORD_INTERFACE:
675 case KEYWORD_NAMESPACE:
676 case KEYWORD_STRUCT:
677 case KEYWORD_UNION:
678 result = TRUE;
679 break;
681 default: result = FALSE; break;
683 return result;
686 static boolean isContextualStatement (const statementInfo *const st)
688 boolean result = FALSE;
689 if (st != NULL) switch (st->declaration)
691 case DECL_CLASS:
692 case DECL_ENUM:
693 case DECL_INTERFACE:
694 case DECL_NAMESPACE:
695 case DECL_STRUCT:
696 case DECL_UNION:
697 result = TRUE;
698 break;
700 default: result = FALSE; break;
702 return result;
705 static boolean isMember (const statementInfo *const st)
707 boolean result;
708 if (isType (st->context, TOKEN_NAME))
709 result = TRUE;
710 else
711 result = (boolean)
712 (st->parent != NULL && isContextualStatement (st->parent));
713 return result;
716 static void initMemberInfo (statementInfo *const st)
718 accessType accessDefault = ACCESS_UNDEFINED;
720 if (st->parent != NULL) switch (st->parent->declaration)
722 case DECL_ENUM:
723 accessDefault = (isLanguage (Lang_java) ? ACCESS_PUBLIC : ACCESS_UNDEFINED);
724 break;
725 case DECL_NAMESPACE:
726 accessDefault = ACCESS_UNDEFINED;
727 break;
729 case DECL_CLASS:
730 if (isLanguage (Lang_java))
731 accessDefault = ACCESS_DEFAULT;
732 else
733 accessDefault = ACCESS_PRIVATE;
734 break;
736 case DECL_INTERFACE:
737 case DECL_STRUCT:
738 case DECL_UNION:
739 accessDefault = ACCESS_PUBLIC;
740 break;
742 default: break;
744 st->member.accessDefault = accessDefault;
745 st->member.access = accessDefault;
748 static void reinitStatement (statementInfo *const st, const boolean partial)
750 unsigned int i;
752 if (! partial)
754 st->scope = SCOPE_GLOBAL;
755 if (isContextualStatement (st->parent))
756 st->declaration = DECL_BASE;
757 else
758 st->declaration = DECL_NONE;
760 st->gotParenName = FALSE;
761 st->isPointer = FALSE;
762 st->inFunction = FALSE;
763 st->assignment = FALSE;
764 st->notVariable = FALSE;
765 st->implementation = IMP_DEFAULT;
766 st->gotArgs = FALSE;
767 st->gotName = FALSE;
768 st->haveQualifyingName = FALSE;
769 st->tokenIndex = 0;
771 if (st->parent != NULL)
772 st->inFunction = st->parent->inFunction;
774 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
775 initToken (st->token [i]);
777 initToken (st->context);
779 /* Keep the block name, so that a variable following after a comma will
780 * still have the structure name.
782 if (! partial)
783 initToken (st->blockName);
785 vStringClear (st->parentClasses);
787 /* Init member info.
789 if (! partial)
790 st->member.access = st->member.accessDefault;
793 static void initStatement (statementInfo *const st, statementInfo *const parent)
795 st->parent = parent;
796 initMemberInfo (st);
797 reinitStatement (st, FALSE);
801 * Tag generation functions
803 static cKind cTagKind (const tagType type)
805 cKind result = CK_UNDEFINED;
806 switch (type)
808 case TAG_CLASS: result = CK_CLASS; break;
809 case TAG_ENUM: result = CK_ENUMERATION; break;
810 case TAG_ENUMERATOR: result = CK_ENUMERATOR; break;
811 case TAG_FUNCTION: result = CK_FUNCTION; break;
812 case TAG_LOCAL: result = CK_LOCAL; break;
813 case TAG_MEMBER: result = CK_MEMBER; break;
814 case TAG_NAMESPACE: result = CK_NAMESPACE; break;
815 case TAG_PROTOTYPE: result = CK_PROTOTYPE; break;
816 case TAG_STRUCT: result = CK_STRUCT; break;
817 case TAG_TYPEDEF: result = CK_TYPEDEF; break;
818 case TAG_UNION: result = CK_UNION; break;
819 case TAG_VARIABLE: result = CK_VARIABLE; break;
820 case TAG_EXTERN_VAR: result = CK_EXTERN_VARIABLE; break;
822 default: Assert ("Bad C tag type" == NULL); break;
824 return result;
827 static csharpKind csharpTagKind (const tagType type)
829 csharpKind result = CSK_UNDEFINED;
830 switch (type)
832 case TAG_CLASS: result = CSK_CLASS; break;
833 case TAG_ENUM: result = CSK_ENUMERATION; break;
834 case TAG_ENUMERATOR: result = CSK_ENUMERATOR; break;
835 case TAG_EVENT: result = CSK_EVENT; break;
836 case TAG_FIELD: result = CSK_FIELD ; break;
837 case TAG_INTERFACE: result = CSK_INTERFACE; break;
838 case TAG_LOCAL: result = CSK_LOCAL; break;
839 case TAG_METHOD: result = CSK_METHOD; break;
840 case TAG_NAMESPACE: result = CSK_NAMESPACE; break;
841 case TAG_PROPERTY: result = CSK_PROPERTY; break;
842 case TAG_STRUCT: result = CSK_STRUCT; break;
843 case TAG_TYPEDEF: result = CSK_TYPEDEF; break;
845 default: Assert ("Bad C# tag type" == NULL); break;
847 return result;
850 static javaKind javaTagKind (const tagType type)
852 javaKind result = JK_UNDEFINED;
853 switch (type)
855 case TAG_CLASS: result = JK_CLASS; break;
856 case TAG_ENUM: result = JK_ENUM; break;
857 case TAG_ENUMERATOR: result = JK_ENUM_CONSTANT; break;
858 case TAG_FIELD: result = JK_FIELD; break;
859 case TAG_INTERFACE: result = JK_INTERFACE; break;
860 case TAG_LOCAL: result = JK_LOCAL; break;
861 case TAG_METHOD: result = JK_METHOD; break;
862 case TAG_PACKAGE: result = JK_PACKAGE; break;
864 default: Assert ("Bad Java tag type" == NULL); break;
866 return result;
869 static veraKind veraTagKind (const tagType type) {
870 veraKind result = VK_UNDEFINED;
871 switch (type)
873 case TAG_CLASS: result = VK_CLASS; break;
874 case TAG_ENUM: result = VK_ENUMERATION; break;
875 case TAG_ENUMERATOR: result = VK_ENUMERATOR; break;
876 case TAG_FUNCTION: result = VK_FUNCTION; break;
877 case TAG_LOCAL: result = VK_LOCAL; break;
878 case TAG_MEMBER: result = VK_MEMBER; break;
879 case TAG_PROGRAM: result = VK_PROGRAM; break;
880 case TAG_PROTOTYPE: result = VK_PROTOTYPE; break;
881 case TAG_TASK: result = VK_TASK; break;
882 case TAG_TYPEDEF: result = VK_TYPEDEF; break;
883 case TAG_VARIABLE: result = VK_VARIABLE; break;
884 case TAG_EXTERN_VAR: result = VK_EXTERN_VARIABLE; break;
886 default: Assert ("Bad Vera tag type" == NULL); break;
888 return result;
891 static const char *tagName (const tagType type)
893 const char* result;
894 if (isLanguage (Lang_csharp))
895 result = CsharpKinds [csharpTagKind (type)].name;
896 else if (isLanguage (Lang_java))
897 result = JavaKinds [javaTagKind (type)].name;
898 else if (isLanguage (Lang_vera))
899 result = VeraKinds [veraTagKind (type)].name;
900 else
901 result = CKinds [cTagKind (type)].name;
902 return result;
905 static int tagLetter (const tagType type)
907 int result;
908 if (isLanguage (Lang_csharp))
909 result = CsharpKinds [csharpTagKind (type)].letter;
910 else if (isLanguage (Lang_java))
911 result = JavaKinds [javaTagKind (type)].letter;
912 else if (isLanguage (Lang_vera))
913 result = VeraKinds [veraTagKind (type)].letter;
914 else
915 result = CKinds [cTagKind (type)].letter;
916 return result;
919 static boolean includeTag (const tagType type, const boolean isFileScope)
921 boolean result;
922 if (isFileScope && ! Option.include.fileScope)
923 result = FALSE;
924 else if (isLanguage (Lang_csharp))
925 result = CsharpKinds [csharpTagKind (type)].enabled;
926 else if (isLanguage (Lang_java))
927 result = JavaKinds [javaTagKind (type)].enabled;
928 else if (isLanguage (Lang_vera))
929 result = VeraKinds [veraTagKind (type)].enabled;
930 else
931 result = CKinds [cTagKind (type)].enabled;
932 return result;
935 static tagType declToTagType (const declType declaration)
937 tagType type = TAG_UNDEFINED;
939 switch (declaration)
941 case DECL_CLASS: type = TAG_CLASS; break;
942 case DECL_ENUM: type = TAG_ENUM; break;
943 case DECL_EVENT: type = TAG_EVENT; break;
944 case DECL_FUNCTION: type = TAG_FUNCTION; break;
945 case DECL_INTERFACE: type = TAG_INTERFACE; break;
946 case DECL_NAMESPACE: type = TAG_NAMESPACE; break;
947 case DECL_PROGRAM: type = TAG_PROGRAM; break;
948 case DECL_TASK: type = TAG_TASK; break;
949 case DECL_STRUCT: type = TAG_STRUCT; break;
950 case DECL_UNION: type = TAG_UNION; break;
952 default: Assert ("Unexpected declaration" == NULL); break;
954 return type;
957 static const char* accessField (const statementInfo *const st)
959 const char* result = NULL;
960 if (isLanguage (Lang_cpp) && st->scope == SCOPE_FRIEND)
961 result = "friend";
962 else if (st->member.access != ACCESS_UNDEFINED)
963 result = accessString (st->member.access);
964 return result;
967 static void addContextSeparator (vString *const scope)
969 if (isLanguage (Lang_c) || isLanguage (Lang_cpp))
970 vStringCatS (scope, "::");
971 else if (isLanguage (Lang_java) || isLanguage (Lang_csharp))
972 vStringCatS (scope, ".");
975 static void addOtherFields (tagEntryInfo* const tag, const tagType type,
976 const statementInfo *const st,
977 vString *const scope, vString *const typeRef)
979 /* For selected tag types, append an extension flag designating the
980 * parent object in which the tag is defined.
982 switch (type)
984 default: break;
986 case TAG_FUNCTION:
987 case TAG_METHOD:
988 case TAG_PROTOTYPE:
989 if (vStringLength (Signature) > 0)
991 tag->extensionFields.signature = vStringValue (Signature);
994 if (vStringLength (ReturnType) > 0)
996 tag->extensionFields.returnType = vStringValue (ReturnType);
998 case TAG_CLASS:
999 case TAG_ENUM:
1000 case TAG_ENUMERATOR:
1001 case TAG_EVENT:
1002 case TAG_FIELD:
1003 case TAG_INTERFACE:
1004 case TAG_MEMBER:
1005 case TAG_NAMESPACE:
1006 case TAG_PROPERTY:
1007 case TAG_STRUCT:
1008 case TAG_TASK:
1009 case TAG_TYPEDEF:
1010 case TAG_UNION:
1011 if (vStringLength (scope) > 0 &&
1012 (isMember (st) || st->parent->declaration == DECL_NAMESPACE))
1014 if (isType (st->context, TOKEN_NAME))
1015 tag->extensionFields.scope [0] = tagName (TAG_CLASS);
1016 else
1017 tag->extensionFields.scope [0] =
1018 tagName (declToTagType (parentDecl (st)));
1019 tag->extensionFields.scope [1] = vStringValue (scope);
1021 if ((type == TAG_CLASS || type == TAG_INTERFACE ||
1022 type == TAG_STRUCT) && vStringLength (st->parentClasses) > 0)
1025 tag->extensionFields.inheritance =
1026 vStringValue (st->parentClasses);
1028 if (st->implementation != IMP_DEFAULT &&
1029 (isLanguage (Lang_cpp) || isLanguage (Lang_csharp) ||
1030 isLanguage (Lang_java)))
1032 tag->extensionFields.implementation =
1033 implementationString (st->implementation);
1035 if (isMember (st))
1037 tag->extensionFields.access = accessField (st);
1039 break;
1042 /* Add typename info, type of the tag and name of struct/union/etc. */
1043 if ((type == TAG_TYPEDEF || type == TAG_VARIABLE || type == TAG_MEMBER)
1044 && isContextualStatement(st))
1046 char *p;
1048 tag->extensionFields.typeRef [0] =
1049 tagName (declToTagType (st->declaration));
1050 p = vStringValue (st->blockName->name);
1052 /* If there was no {} block get the name from the token before the
1053 * name (current token is ';' or ',', previous token is the name).
1055 if (p == NULL || *p == '\0')
1057 tokenInfo *const prev2 = prevToken (st, 2);
1058 if (isType (prev2, TOKEN_NAME))
1059 p = vStringValue (prev2->name);
1062 /* Prepend the scope name if there is one. */
1063 if (vStringLength (scope) > 0)
1065 vStringCopy(typeRef, scope);
1066 addContextSeparator (typeRef);
1067 vStringCatS(typeRef, p);
1068 p = vStringValue (typeRef);
1070 tag->extensionFields.typeRef [1] = p;
1074 static void findScopeHierarchy (vString *const string,
1075 const statementInfo *const st)
1077 vStringClear (string);
1078 if (isType (st->context, TOKEN_NAME))
1079 vStringCopy (string, st->context->name);
1080 if (st->parent != NULL)
1082 vString *temp = vStringNew ();
1083 const statementInfo *s;
1084 for (s = st->parent ; s != NULL ; s = s->parent)
1086 if (isContextualStatement (s) ||
1087 s->declaration == DECL_NAMESPACE ||
1088 s->declaration == DECL_PROGRAM)
1090 vStringCopy (temp, string);
1091 vStringClear (string);
1092 Assert (isType (s->blockName, TOKEN_NAME));
1093 if (isType (s->context, TOKEN_NAME) &&
1094 vStringLength (s->context->name) > 0)
1096 vStringCat (string, s->context->name);
1097 addContextSeparator (string);
1099 vStringCat (string, s->blockName->name);
1100 if (vStringLength (temp) > 0)
1101 addContextSeparator (string);
1102 vStringCat (string, temp);
1105 vStringDelete (temp);
1109 static void makeExtraTagEntry (const tagType type, tagEntryInfo *const e,
1110 vString *const scope)
1112 if (Option.include.qualifiedTags &&
1113 scope != NULL && vStringLength (scope) > 0)
1115 vString *const scopedName = vStringNew ();
1117 if (type != TAG_ENUMERATOR)
1118 vStringCopy (scopedName, scope);
1119 else
1121 /* remove last component (i.e. enumeration name) from scope */
1122 const char* const sc = vStringValue (scope);
1123 const char* colon = strrchr (sc, ':');
1124 if (colon != NULL)
1126 while (*colon == ':' && colon > sc)
1127 --colon;
1128 vStringNCopy (scopedName, scope, colon + 1 - sc);
1131 if (vStringLength (scopedName) > 0)
1133 addContextSeparator (scopedName);
1134 vStringCatS (scopedName, e->name);
1135 e->name = vStringValue (scopedName);
1136 makeTagEntry (e);
1138 vStringDelete (scopedName);
1142 static void makeTag (const tokenInfo *const token,
1143 const statementInfo *const st,
1144 boolean isFileScope, const tagType type)
1146 /* Nothing is really of file scope when it appears in a header file.
1148 isFileScope = (boolean) (isFileScope && ! isHeaderFile ());
1150 if (isType (token, TOKEN_NAME) && vStringLength (token->name) > 0 &&
1151 includeTag (type, isFileScope))
1153 vString *scope = vStringNew ();
1154 /* Use "typeRef" to store the typename from addOtherFields() until
1155 * it's used in makeTagEntry().
1157 vString *typeRef = vStringNew ();
1158 tagEntryInfo e;
1160 initTagEntry (&e, vStringValue (token->name));
1162 e.lineNumber = token->lineNumber;
1163 e.filePosition = token->filePosition;
1164 e.isFileScope = isFileScope;
1165 e.kindName = tagName (type);
1166 e.kind = tagLetter (type);
1168 findScopeHierarchy (scope, st);
1169 addOtherFields (&e, type, st, scope, typeRef);
1171 makeTagEntry (&e);
1172 makeExtraTagEntry (type, &e, scope);
1173 vStringDelete (scope);
1174 vStringDelete (typeRef);
1178 static boolean isValidTypeSpecifier (const declType declaration)
1180 boolean result;
1181 switch (declaration)
1183 case DECL_BASE:
1184 case DECL_CLASS:
1185 case DECL_ENUM:
1186 case DECL_EVENT:
1187 case DECL_STRUCT:
1188 case DECL_UNION:
1189 result = TRUE;
1190 break;
1192 default:
1193 result = FALSE;
1194 break;
1196 return result;
1199 static void qualifyEnumeratorTag (const statementInfo *const st,
1200 const tokenInfo *const nameToken)
1202 if (isType (nameToken, TOKEN_NAME))
1203 makeTag (nameToken, st, TRUE, TAG_ENUMERATOR);
1206 static void qualifyFunctionTag (const statementInfo *const st,
1207 const tokenInfo *const nameToken)
1209 if (isType (nameToken, TOKEN_NAME))
1211 tagType type;
1212 const boolean isFileScope =
1213 (boolean) (st->member.access == ACCESS_PRIVATE ||
1214 (!isMember (st) && st->scope == SCOPE_STATIC));
1215 if (isLanguage (Lang_java) || isLanguage (Lang_csharp))
1216 type = TAG_METHOD;
1217 else if (isLanguage (Lang_vera) && st->declaration == DECL_TASK)
1218 type = TAG_TASK;
1219 else
1220 type = TAG_FUNCTION;
1221 makeTag (nameToken, st, isFileScope, type);
1225 static void qualifyFunctionDeclTag (const statementInfo *const st,
1226 const tokenInfo *const nameToken)
1228 if (! isType (nameToken, TOKEN_NAME))
1230 else if (isLanguage (Lang_java) || isLanguage (Lang_csharp))
1231 qualifyFunctionTag (st, nameToken);
1232 else if (st->scope == SCOPE_TYPEDEF)
1233 makeTag (nameToken, st, TRUE, TAG_TYPEDEF);
1234 else if (isValidTypeSpecifier (st->declaration) && ! isLanguage (Lang_csharp))
1235 makeTag (nameToken, st, TRUE, TAG_PROTOTYPE);
1238 static void qualifyCompoundTag (const statementInfo *const st,
1239 const tokenInfo *const nameToken)
1241 if (isType (nameToken, TOKEN_NAME))
1243 const tagType type = declToTagType (st->declaration);
1244 const boolean fileScoped = (boolean)
1245 (!(isLanguage (Lang_java) ||
1246 isLanguage (Lang_csharp) ||
1247 isLanguage (Lang_vera)));
1249 if (type != TAG_UNDEFINED)
1250 makeTag (nameToken, st, fileScoped, type);
1254 static void qualifyBlockTag (statementInfo *const st,
1255 const tokenInfo *const nameToken)
1257 switch (st->declaration)
1259 case DECL_CLASS:
1260 case DECL_ENUM:
1261 case DECL_INTERFACE:
1262 case DECL_NAMESPACE:
1263 case DECL_PROGRAM:
1264 case DECL_STRUCT:
1265 case DECL_UNION:
1266 qualifyCompoundTag (st, nameToken);
1267 break;
1268 default: break;
1272 static void qualifyVariableTag (const statementInfo *const st,
1273 const tokenInfo *const nameToken)
1275 /* We have to watch that we do not interpret a declaration of the
1276 * form "struct tag;" as a variable definition. In such a case, the
1277 * token preceding the name will be a keyword.
1279 if (! isType (nameToken, TOKEN_NAME))
1281 else if (st->scope == SCOPE_TYPEDEF)
1282 makeTag (nameToken, st, TRUE, TAG_TYPEDEF);
1283 else if (st->declaration == DECL_EVENT)
1284 makeTag (nameToken, st, (boolean) (st->member.access == ACCESS_PRIVATE),
1285 TAG_EVENT);
1286 else if (st->declaration == DECL_PACKAGE)
1287 makeTag (nameToken, st, FALSE, TAG_PACKAGE);
1288 else if (isValidTypeSpecifier (st->declaration))
1290 if (st->notVariable)
1292 else if (isMember (st))
1294 if (isLanguage (Lang_java) || isLanguage (Lang_csharp))
1295 makeTag (nameToken, st,
1296 (boolean) (st->member.access == ACCESS_PRIVATE), TAG_FIELD);
1297 else if (st->scope == SCOPE_GLOBAL || st->scope == SCOPE_STATIC)
1298 makeTag (nameToken, st, TRUE, TAG_MEMBER);
1300 else
1302 if (st->scope == SCOPE_EXTERN || ! st->haveQualifyingName)
1303 makeTag (nameToken, st, FALSE, TAG_EXTERN_VAR);
1304 else if (st->inFunction)
1305 makeTag (nameToken, st, (boolean) (st->scope == SCOPE_STATIC),
1306 TAG_LOCAL);
1307 else
1308 makeTag (nameToken, st, (boolean) (st->scope == SCOPE_STATIC),
1309 TAG_VARIABLE);
1315 * Parsing functions
1318 static int skipToOneOf (const char *const chars)
1320 int c;
1322 c = cppGetc ();
1323 while (c != EOF && c != '\0' && strchr (chars, c) == NULL);
1324 return c;
1327 /* Skip to the next non-white character.
1329 static int skipToNonWhite (void)
1331 boolean found = FALSE;
1332 int c;
1334 #if 0
1336 c = cppGetc ();
1337 while (isspace (c));
1338 #else
1339 while (1)
1341 c = cppGetc ();
1342 if (isspace (c))
1343 found = TRUE;
1344 else
1345 break;
1347 if (CollectingSignature && found)
1348 vStringPut (Signature, ' ');
1349 #endif
1351 return c;
1354 /* Skips to the next brace in column 1. This is intended for cases where
1355 * preprocessor constructs result in unbalanced braces.
1357 static void skipToFormattedBraceMatch (void)
1359 int c, next;
1361 c = cppGetc ();
1362 next = cppGetc ();
1363 while (c != EOF && (c != '\n' || next != '}'))
1365 c = next;
1366 next = cppGetc ();
1370 /* Skip to the matching character indicated by the pair string. If skipping
1371 * to a matching brace and any brace is found within a different level of a
1372 * #if conditional statement while brace formatting is in effect, we skip to
1373 * the brace matched by its formatting. It is assumed that we have already
1374 * read the character which starts the group (i.e. the first character of
1375 * "pair").
1377 static void skipToMatch (const char *const pair)
1379 const boolean braceMatching = (boolean) (strcmp ("{}", pair) == 0);
1380 const boolean braceFormatting = (boolean) (isBraceFormat () && braceMatching);
1381 const unsigned int initialLevel = getDirectiveNestLevel ();
1382 const int begin = pair [0], end = pair [1];
1383 const unsigned long inputLineNumber = getInputLineNumber ();
1384 int matchLevel = 1;
1385 int c = '\0';
1387 while (matchLevel > 0 && (c = skipToNonWhite ()) != EOF)
1389 if (CollectingSignature)
1390 vStringPut (Signature, c);
1393 if (c == begin)
1395 ++matchLevel;
1396 if (braceFormatting && getDirectiveNestLevel () != initialLevel)
1398 skipToFormattedBraceMatch ();
1399 break;
1402 else if (c == end)
1404 --matchLevel;
1405 if (braceFormatting && getDirectiveNestLevel () != initialLevel)
1407 skipToFormattedBraceMatch ();
1408 break;
1412 if (c == EOF)
1414 verbose ("%s: failed to find match for '%c' at line %lu\n",
1415 getInputFileName (), begin, inputLineNumber);
1416 if (braceMatching)
1417 longjmp (Exception, (int) ExceptionBraceFormattingError);
1418 else
1419 longjmp (Exception, (int) ExceptionFormattingError);
1423 static void skipParens (void)
1425 const int c = skipToNonWhite ();
1427 if (c == '(')
1428 skipToMatch ("()");
1429 else
1430 cppUngetc (c);
1433 static void skipBraces (void)
1435 const int c = skipToNonWhite ();
1437 if (c == '{')
1438 skipToMatch ("{}");
1439 else
1440 cppUngetc (c);
1443 static keywordId analyzeKeyword (const char *const name)
1445 const keywordId id = (keywordId) lookupKeyword (name, getSourceLanguage ());
1446 return id;
1449 static void analyzeIdentifier (tokenInfo *const token)
1451 char *const name = vStringValue (token->name);
1452 const char *replacement = NULL;
1453 boolean parensToo = FALSE;
1455 if (isLanguage (Lang_java) ||
1456 ! isIgnoreToken (name, &parensToo, &replacement))
1458 if (replacement != NULL)
1459 token->keyword = analyzeKeyword (replacement);
1460 else
1461 token->keyword = analyzeKeyword (vStringValue (token->name));
1463 if (token->keyword == KEYWORD_NONE)
1464 token->type = TOKEN_NAME;
1465 else
1466 token->type = TOKEN_KEYWORD;
1468 else
1470 initToken (token);
1471 if (parensToo)
1473 int c = skipToNonWhite ();
1475 if (c == '(')
1476 skipToMatch ("()");
1481 static void readIdentifier (tokenInfo *const token, const int firstChar)
1483 vString *const name = token->name;
1484 int c = firstChar;
1485 boolean first = TRUE;
1487 initToken (token);
1489 /* Bug #1585745: strangely, C++ destructors allow whitespace between
1490 * the ~ and the class name. */
1491 if (isLanguage (Lang_cpp) && firstChar == '~')
1493 vStringPut (name, c);
1494 c = skipToNonWhite ();
1499 vStringPut (name, c);
1500 if (CollectingSignature)
1502 if (!first)
1503 vStringPut (Signature, c);
1504 first = FALSE;
1506 c = cppGetc ();
1507 } while (isident (c) || ((isLanguage (Lang_java) || isLanguage (Lang_csharp)) && (isHighChar (c) || c == '.')));
1508 vStringTerminate (name);
1509 cppUngetc (c); /* unget non-identifier character */
1511 analyzeIdentifier (token);
1514 static void readPackageName (tokenInfo *const token, const int firstChar)
1516 vString *const name = token->name;
1517 int c = firstChar;
1519 initToken (token);
1521 while (isident (c) || c == '.')
1523 vStringPut (name, c);
1524 c = cppGetc ();
1526 vStringTerminate (name);
1527 cppUngetc (c); /* unget non-package character */
1530 static void readPackageOrNamespace (statementInfo *const st, const declType declaration)
1532 st->declaration = declaration;
1534 if (declaration == DECL_NAMESPACE && !isLanguage (Lang_csharp))
1536 /* In C++ a namespace is specified one level at a time. */
1537 return;
1539 else
1541 /* In C#, a namespace can also be specified like a Java package name. */
1542 tokenInfo *const token = activeToken (st);
1543 Assert (isType (token, TOKEN_KEYWORD));
1544 readPackageName (token, skipToNonWhite ());
1545 token->type = TOKEN_NAME;
1546 st->gotName = TRUE;
1547 st->haveQualifyingName = TRUE;
1551 static void processName (statementInfo *const st)
1553 Assert (isType (activeToken (st), TOKEN_NAME));
1554 if (st->gotName && st->declaration == DECL_NONE)
1555 st->declaration = DECL_BASE;
1556 st->gotName = TRUE;
1557 st->haveQualifyingName = TRUE;
1560 static void readOperator (statementInfo *const st)
1562 const char *const acceptable = "+-*/%^&|~!=<>,[]";
1563 const tokenInfo* const prev = prevToken (st,1);
1564 tokenInfo *const token = activeToken (st);
1565 vString *const name = token->name;
1566 int c = skipToNonWhite ();
1568 /* When we arrive here, we have the keyword "operator" in 'name'.
1570 if (isType (prev, TOKEN_KEYWORD) && (prev->keyword == KEYWORD_ENUM ||
1571 prev->keyword == KEYWORD_STRUCT || prev->keyword == KEYWORD_UNION))
1572 ; /* ignore "operator" keyword if preceded by these keywords */
1573 else if (c == '(')
1575 /* Verify whether this is a valid function call (i.e. "()") operator.
1577 if (cppGetc () == ')')
1579 vStringPut (name, ' '); /* always separate operator from keyword */
1580 c = skipToNonWhite ();
1581 if (c == '(')
1582 vStringCatS (name, "()");
1584 else
1586 skipToMatch ("()");
1587 c = cppGetc ();
1590 else if (isident1 (c))
1592 /* Handle "new" and "delete" operators, and conversion functions
1593 * (per 13.3.1.1.2 [2] of the C++ spec).
1595 boolean whiteSpace = TRUE; /* default causes insertion of space */
1598 if (isspace (c))
1599 whiteSpace = TRUE;
1600 else
1602 if (whiteSpace)
1604 vStringPut (name, ' ');
1605 whiteSpace = FALSE;
1607 vStringPut (name, c);
1609 c = cppGetc ();
1610 } while (! isOneOf (c, "(;") && c != EOF);
1611 vStringTerminate (name);
1613 else if (isOneOf (c, acceptable))
1615 vStringPut (name, ' '); /* always separate operator from keyword */
1618 vStringPut (name, c);
1619 c = cppGetc ();
1620 } while (isOneOf (c, acceptable));
1621 vStringTerminate (name);
1624 cppUngetc (c);
1626 token->type = TOKEN_NAME;
1627 token->keyword = KEYWORD_NONE;
1628 processName (st);
1631 static void copyToken (tokenInfo *const dest, const tokenInfo *const src)
1633 dest->type = src->type;
1634 dest->keyword = src->keyword;
1635 dest->filePosition = src->filePosition;
1636 dest->lineNumber = src->lineNumber;
1637 vStringCopy (dest->name, src->name);
1640 static void setAccess (statementInfo *const st, const accessType access)
1642 if (isMember (st))
1644 if (isLanguage (Lang_cpp))
1646 int c = skipToNonWhite ();
1648 if (c == ':')
1649 reinitStatement (st, FALSE);
1650 else
1651 cppUngetc (c);
1653 st->member.accessDefault = access;
1655 st->member.access = access;
1659 static void discardTypeList (tokenInfo *const token)
1661 int c = skipToNonWhite ();
1662 while (isident1 (c))
1664 readIdentifier (token, c);
1665 c = skipToNonWhite ();
1666 if (c == '.' || c == ',')
1667 c = skipToNonWhite ();
1669 cppUngetc (c);
1672 static void addParentClass (statementInfo *const st, tokenInfo *const token)
1674 if (vStringLength (token->name) > 0 &&
1675 vStringLength (st->parentClasses) > 0)
1677 vStringPut (st->parentClasses, ',');
1679 vStringCat (st->parentClasses, token->name);
1682 static void readParents (statementInfo *const st, const int qualifier)
1684 tokenInfo *const token = newToken ();
1685 tokenInfo *const parent = newToken ();
1686 int c;
1690 c = skipToNonWhite ();
1691 if (isident1 (c))
1693 readIdentifier (token, c);
1694 if (isType (token, TOKEN_NAME))
1695 vStringCat (parent->name, token->name);
1696 else
1698 addParentClass (st, parent);
1699 initToken (parent);
1702 else if (c == qualifier)
1703 vStringPut (parent->name, c);
1704 else if (c == '<')
1705 skipToMatch ("<>");
1706 else if (isType (token, TOKEN_NAME))
1708 addParentClass (st, parent);
1709 initToken (parent);
1711 } while (c != '{' && c != EOF);
1712 cppUngetc (c);
1713 deleteToken (parent);
1714 deleteToken (token);
1717 static void skipStatement (statementInfo *const st)
1719 st->declaration = DECL_IGNORE;
1720 skipToOneOf (";");
1723 static void processInterface (statementInfo *const st)
1725 st->declaration = DECL_INTERFACE;
1728 static void processToken (tokenInfo *const token, statementInfo *const st)
1730 switch (token->keyword) /* is it a reserved word? */
1732 default: break;
1734 case KEYWORD_NONE: processName (st); break;
1735 case KEYWORD_ABSTRACT: st->implementation = IMP_ABSTRACT; break;
1736 case KEYWORD_ATTRIBUTE:
1737 case KEYWORD_TYPENAME:
1738 case KEYWORD_INLINE: skipParens (); initToken (token); break;
1739 case KEYWORD_BIND: st->declaration = DECL_BASE; break;
1740 case KEYWORD_BIT: st->declaration = DECL_BASE; break;
1741 case KEYWORD_CATCH: skipParens (); skipBraces (); break;
1742 case KEYWORD_CHAR: st->declaration = DECL_BASE; break;
1743 case KEYWORD_CLASS: st->declaration = DECL_CLASS; break;
1744 case KEYWORD_CONST: st->declaration = DECL_BASE; break;
1745 case KEYWORD_DOUBLE: st->declaration = DECL_BASE; break;
1746 case KEYWORD_ENUM: st->declaration = DECL_ENUM; break;
1747 case KEYWORD_EXTENDS: readParents (st, '.');
1748 setToken (st, TOKEN_NONE); break;
1749 case KEYWORD_FLOAT: st->declaration = DECL_BASE; break;
1750 case KEYWORD_FUNCTION: st->declaration = DECL_BASE; break;
1751 case KEYWORD_FRIEND: st->scope = SCOPE_FRIEND; break;
1752 case KEYWORD_GOTO: skipStatement (st); break;
1753 case KEYWORD_IMPLEMENTS:readParents (st, '.');
1754 setToken (st, TOKEN_NONE); break;
1755 case KEYWORD_IMPORT: skipStatement (st); break;
1756 case KEYWORD_INT: st->declaration = DECL_BASE; break;
1757 case KEYWORD_INTEGER: st->declaration = DECL_BASE; break;
1758 case KEYWORD_INTERFACE: processInterface (st); break;
1759 case KEYWORD_LOCAL: setAccess (st, ACCESS_LOCAL); break;
1760 case KEYWORD_LONG: st->declaration = DECL_BASE; break;
1761 case KEYWORD_OPERATOR: readOperator (st); break;
1762 case KEYWORD_PRIVATE: setAccess (st, ACCESS_PRIVATE); break;
1763 case KEYWORD_PROGRAM: st->declaration = DECL_PROGRAM; break;
1764 case KEYWORD_PROTECTED: setAccess (st, ACCESS_PROTECTED); break;
1765 case KEYWORD_PUBLIC: setAccess (st, ACCESS_PUBLIC); break;
1766 case KEYWORD_RETURN: skipStatement (st); break;
1767 case KEYWORD_SHORT: st->declaration = DECL_BASE; break;
1768 case KEYWORD_SIGNED: st->declaration = DECL_BASE; break;
1769 case KEYWORD_STATIC_ASSERT: skipParens(); break;
1770 case KEYWORD_STRING: st->declaration = DECL_BASE; break;
1771 case KEYWORD_STRUCT: st->declaration = DECL_STRUCT; break;
1772 case KEYWORD_TASK: st->declaration = DECL_TASK; break;
1773 case KEYWORD_THROWS: discardTypeList (token); break;
1774 case KEYWORD_UNION: st->declaration = DECL_UNION; break;
1775 case KEYWORD_UNSIGNED: st->declaration = DECL_BASE; break;
1776 case KEYWORD_USING: skipStatement (st); break;
1777 case KEYWORD_VOID: st->declaration = DECL_BASE; break;
1778 case KEYWORD_VOLATILE: st->declaration = DECL_BASE; break;
1779 case KEYWORD_VIRTUAL: st->implementation = IMP_VIRTUAL; break;
1780 case KEYWORD_WCHAR_T: st->declaration = DECL_BASE; break;
1782 case KEYWORD_NAMESPACE: readPackageOrNamespace (st, DECL_NAMESPACE); break;
1783 case KEYWORD_PACKAGE: readPackageOrNamespace (st, DECL_PACKAGE); break;
1785 case KEYWORD_EVENT:
1786 if (isLanguage (Lang_csharp))
1787 st->declaration = DECL_EVENT;
1788 break;
1790 case KEYWORD_TYPEDEF:
1791 reinitStatement (st, FALSE);
1792 st->scope = SCOPE_TYPEDEF;
1793 break;
1795 case KEYWORD_EXTERN:
1796 if (! isLanguage (Lang_csharp) || !st->gotName)
1798 reinitStatement (st, FALSE);
1799 st->scope = SCOPE_EXTERN;
1800 st->declaration = DECL_BASE;
1802 break;
1804 case KEYWORD_STATIC:
1805 if (! (isLanguage (Lang_java) || isLanguage (Lang_csharp)))
1807 reinitStatement (st, FALSE);
1808 st->scope = SCOPE_STATIC;
1809 st->declaration = DECL_BASE;
1811 break;
1813 case KEYWORD_FOR:
1814 case KEYWORD_FOREACH:
1815 case KEYWORD_IF:
1816 case KEYWORD_SWITCH:
1817 case KEYWORD_WHILE:
1819 int c = skipToNonWhite ();
1820 if (c == '(')
1821 skipToMatch ("()");
1822 break;
1828 * Parenthesis handling functions
1831 static void restartStatement (statementInfo *const st)
1833 tokenInfo *const save = newToken ();
1834 tokenInfo *token = activeToken (st);
1836 copyToken (save, token);
1837 DebugStatement ( if (debug (DEBUG_PARSE)) printf ("<ES>");)
1838 reinitStatement (st, FALSE);
1839 token = activeToken (st);
1840 copyToken (token, save);
1841 deleteToken (save);
1842 processToken (token, st);
1845 /* Skips over a the mem-initializer-list of a ctor-initializer, defined as:
1847 * mem-initializer-list:
1848 * mem-initializer, mem-initializer-list
1850 * mem-initializer:
1851 * [::] [nested-name-spec] class-name (...)
1852 * identifier
1854 static void skipMemIntializerList (tokenInfo *const token)
1856 int c;
1860 c = skipToNonWhite ();
1861 while (isident1 (c) || c == ':')
1863 if (c != ':')
1864 readIdentifier (token, c);
1865 c = skipToNonWhite ();
1867 if (c == '<')
1869 skipToMatch ("<>");
1870 c = skipToNonWhite ();
1872 if (c == '(')
1874 skipToMatch ("()");
1875 c = skipToNonWhite ();
1877 } while (c == ',');
1878 cppUngetc (c);
1881 static void skipMacro (statementInfo *const st)
1883 tokenInfo *const prev2 = prevToken (st, 2);
1885 if (isType (prev2, TOKEN_NAME))
1886 retardToken (st);
1887 skipToMatch ("()");
1890 /* Skips over characters following the parameter list. This will be either
1891 * non-ANSI style function declarations or C++ stuff. Our choices:
1893 * C (K&R):
1894 * int func ();
1895 * int func (one, two) int one; float two; {...}
1896 * C (ANSI):
1897 * int func (int one, float two);
1898 * int func (int one, float two) {...}
1899 * C++:
1900 * int foo (...) [const|volatile] [throw (...)];
1901 * int foo (...) [const|volatile] [throw (...)] [ctor-initializer] {...}
1902 * int foo (...) [const|volatile] [throw (...)] try [ctor-initializer] {...}
1903 * catch (...) {...}
1905 static boolean skipPostArgumentStuff (
1906 statementInfo *const st, parenInfo *const info)
1908 tokenInfo *const token = activeToken (st);
1909 unsigned int parameters = info->parameterCount;
1910 unsigned int elementCount = 0;
1911 boolean restart = FALSE;
1912 boolean end = FALSE;
1913 int c = skipToNonWhite ();
1917 switch (c)
1919 case ')': break;
1920 case ':': skipMemIntializerList (token);break; /* ctor-initializer */
1921 case '[': skipToMatch ("[]"); break;
1922 case '=': cppUngetc (c); end = TRUE; break;
1923 case '{': cppUngetc (c); end = TRUE; break;
1924 case '}': cppUngetc (c); end = TRUE; break;
1926 case '(':
1927 if (elementCount > 0)
1928 ++elementCount;
1929 skipToMatch ("()");
1930 break;
1932 case ';':
1933 if (parameters == 0 || elementCount < 2)
1935 cppUngetc (c);
1936 end = TRUE;
1938 else if (--parameters == 0)
1939 end = TRUE;
1940 break;
1942 default:
1943 if (isident1 (c))
1945 readIdentifier (token, c);
1946 switch (token->keyword)
1948 case KEYWORD_ATTRIBUTE: skipParens (); break;
1949 case KEYWORD_THROW: skipParens (); break;
1950 case KEYWORD_TRY: break;
1952 case KEYWORD_CONST:
1953 case KEYWORD_VOLATILE:
1954 if (vStringLength (Signature) > 0)
1956 vStringPut (Signature, ' ');
1957 vStringCat (Signature, token->name);
1959 break;
1961 case KEYWORD_CATCH:
1962 case KEYWORD_CLASS:
1963 case KEYWORD_EXPLICIT:
1964 case KEYWORD_EXTERN:
1965 case KEYWORD_FRIEND:
1966 case KEYWORD_INLINE:
1967 case KEYWORD_MUTABLE:
1968 case KEYWORD_NAMESPACE:
1969 case KEYWORD_NEW:
1970 case KEYWORD_NEWCOV:
1971 case KEYWORD_NOEXCEPT:
1972 case KEYWORD_OPERATOR:
1973 case KEYWORD_OVERLOAD:
1974 case KEYWORD_PRIVATE:
1975 case KEYWORD_PROTECTED:
1976 case KEYWORD_PUBLIC:
1977 case KEYWORD_STATIC:
1978 case KEYWORD_TEMPLATE:
1979 case KEYWORD_TYPEDEF:
1980 case KEYWORD_TYPENAME:
1981 case KEYWORD_USING:
1982 case KEYWORD_VIRTUAL:
1983 /* Never allowed within parameter declarations. */
1984 restart = TRUE;
1985 end = TRUE;
1986 break;
1988 default:
1989 if (isType (token, TOKEN_NONE))
1991 else if (info->isKnrParamList && info->parameterCount > 0)
1992 ++elementCount;
1993 else
1995 /* If we encounter any other identifier immediately
1996 * following an empty parameter list, this is almost
1997 * certainly one of those Microsoft macro "thingies"
1998 * that the automatic source code generation sticks
1999 * in. Terminate the current statement.
2001 restart = TRUE;
2002 end = TRUE;
2004 break;
2008 if (! end)
2010 c = skipToNonWhite ();
2011 if (c == EOF)
2012 end = TRUE;
2014 } while (! end);
2016 if (restart)
2017 restartStatement (st);
2018 else
2019 setToken (st, TOKEN_NONE);
2021 return (boolean) (c != EOF);
2024 static void skipJavaThrows (statementInfo *const st)
2026 tokenInfo *const token = activeToken (st);
2027 int c = skipToNonWhite ();
2029 if (isident1 (c))
2031 readIdentifier (token, c);
2032 if (token->keyword == KEYWORD_THROWS)
2036 c = skipToNonWhite ();
2037 if (isident1 (c))
2039 readIdentifier (token, c);
2040 c = skipToNonWhite ();
2042 } while (c == '.' || c == ',');
2045 cppUngetc (c);
2046 setToken (st, TOKEN_NONE);
2049 static void analyzePostParens (statementInfo *const st, parenInfo *const info)
2051 const unsigned long inputLineNumber = getInputLineNumber ();
2052 int c = skipToNonWhite ();
2054 cppUngetc (c);
2055 if (isOneOf (c, "{;,="))
2057 else if (isLanguage (Lang_java))
2058 skipJavaThrows (st);
2059 else
2061 if (! skipPostArgumentStuff (st, info))
2063 verbose (
2064 "%s: confusing argument declarations beginning at line %lu\n",
2065 getInputFileName (), inputLineNumber);
2066 longjmp (Exception, (int) ExceptionFormattingError);
2071 static boolean languageSupportsGenerics (void)
2073 return (boolean) (isLanguage (Lang_cpp) || isLanguage (Lang_csharp) ||
2074 isLanguage (Lang_java));
2077 static void processAngleBracket (void)
2079 int c = cppGetc ();
2080 if (c == '>') {
2081 /* already found match for template */
2082 } else if (languageSupportsGenerics () && c != '<' && c != '=') {
2083 /* this is a template */
2084 cppUngetc (c);
2085 skipToMatch ("<>");
2086 } else if (c == '<') {
2087 /* skip "<<" or "<<=". */
2088 c = cppGetc ();
2089 if (c != '=') {
2090 cppUngetc (c);
2092 } else {
2093 cppUngetc (c);
2097 static void parseJavaAnnotation (statementInfo *const st)
2100 * @Override
2101 * @Target(ElementType.METHOD)
2102 * @SuppressWarnings(value = "unchecked")
2104 * But watch out for "@interface"!
2106 tokenInfo *const token = activeToken (st);
2108 int c = skipToNonWhite ();
2109 readIdentifier (token, c);
2110 if (token->keyword == KEYWORD_INTERFACE)
2112 /* Oops. This was actually "@interface" defining a new annotation. */
2113 processInterface (st);
2115 else
2117 /* Bug #1691412: skip any annotation arguments. */
2118 skipParens ();
2122 static void parseReturnType (statementInfo *const st)
2124 int i;
2125 int lower_bound;
2126 int upper_bound;
2127 tokenInfo * finding_tok;
2129 /* FIXME TODO: if java language must be supported then impement this here
2130 * removing the current FIXME */
2131 if (!isLanguage (Lang_c) && !isLanguage (Lang_cpp))
2133 return;
2136 vStringClear (ReturnType);
2138 finding_tok = prevToken (st, 1);
2140 if (isType (finding_tok, TOKEN_NONE))
2141 return;
2143 finding_tok = prevToken (st, 2);
2145 if (finding_tok->type == TOKEN_DOUBLE_COLON)
2147 /* get the total number of double colons */
2148 int j;
2149 int num_colons = 0;
2151 /* we already are at 2nd token */
2152 /* the +=2 means that colons are usually found at even places */
2153 for (j = 2; j < NumTokens; j+=2)
2155 tokenInfo *curr_tok;
2156 curr_tok = prevToken (st, j);
2157 if (curr_tok->type == TOKEN_DOUBLE_COLON)
2158 num_colons++;
2159 else
2160 break;
2163 /*printf ("FOUND colons %d\n", num_colons);*/
2164 lower_bound = 2 * num_colons + 1;
2166 else
2167 lower_bound = 1;
2169 upper_bound = -1;
2170 for (i = 0; i < NumTokens; i++) {
2171 tokenInfo *curr_tok;
2172 curr_tok = prevToken (st, i);
2173 if (curr_tok->type == TOKEN_BRACE_CLOSE || curr_tok->type == TOKEN_BRACE_OPEN) {
2174 upper_bound = i - 1;
2175 break;
2178 if (upper_bound < 0) {
2179 upper_bound = NumTokens - 1;
2182 for (i = upper_bound; i > lower_bound; i--)
2184 tokenInfo * curr_tok;
2185 curr_tok = prevToken (st, i);
2187 switch (curr_tok->type)
2189 case TOKEN_PAREN_NAME:
2190 case TOKEN_NONE:
2191 continue;
2192 break;
2194 case TOKEN_DOUBLE_COLON:
2195 /* usually C++ class scope */
2196 vStringCatS (ReturnType, "::");
2197 break;
2199 case TOKEN_STAR:
2200 /* pointers */
2201 vStringPut (ReturnType, '*');
2202 break;
2204 case TOKEN_AMPERSAND:
2205 /* references */
2206 vStringPut (ReturnType, '&');
2207 break;
2209 default:
2210 vStringCat (ReturnType, curr_tok->name);
2211 if (curr_tok->type == TOKEN_KEYWORD) {
2212 vStringPut (ReturnType, ' ');
2214 break;
2218 /* clear any white space from the front */
2219 vStringStripLeading (ReturnType);
2221 /* .. and from the tail too */
2222 vStringStripTrailing (ReturnType);
2224 /* put and end marker */
2225 vStringTerminate (ReturnType);
2228 printf ("~~~~~ statement ---->\n");
2229 ps (st);
2230 printf ("NumTokens: %d\n", NumTokens);
2231 printf ("FOUND ReturnType: %s\n", vStringValue (ReturnType));
2232 printf ("<~~~~~\n");
2233 //*/
2236 static int parseParens (statementInfo *const st, parenInfo *const info)
2238 tokenInfo *const token = activeToken (st);
2239 unsigned int identifierCount = 0;
2240 unsigned int depth = 1;
2241 boolean firstChar = TRUE;
2242 int nextChar = '\0';
2244 CollectingSignature = TRUE;
2245 vStringClear (Signature);
2246 vStringPut (Signature, '(');
2247 info->parameterCount = 1;
2250 int c = skipToNonWhite ();
2251 vStringPut (Signature, c);
2253 switch (c)
2255 case '&':
2256 case '*':
2257 info->isPointer = TRUE;
2258 info->isKnrParamList = FALSE;
2259 if (identifierCount == 0)
2260 info->isParamList = FALSE;
2261 initToken (token);
2262 break;
2264 case ':':
2265 info->isKnrParamList = FALSE;
2266 break;
2268 case '.':
2269 info->isNameCandidate = FALSE;
2270 c = cppGetc ();
2271 if (c != '.')
2273 cppUngetc (c);
2274 info->isKnrParamList = FALSE;
2276 else
2278 c = cppGetc ();
2279 if (c != '.')
2281 cppUngetc (c);
2282 info->isKnrParamList = FALSE;
2284 else
2285 vStringCatS (Signature, "..."); /* variable arg list */
2287 break;
2289 case ',':
2290 info->isNameCandidate = FALSE;
2291 if (info->isKnrParamList)
2293 ++info->parameterCount;
2294 identifierCount = 0;
2296 break;
2298 case '=':
2299 info->isKnrParamList = FALSE;
2300 info->isNameCandidate = FALSE;
2301 if (firstChar)
2303 info->isParamList = FALSE;
2304 skipMacro (st);
2305 depth = 0;
2307 break;
2309 case '[':
2310 info->isKnrParamList = FALSE;
2311 skipToMatch ("[]");
2312 break;
2314 case '<':
2315 info->isKnrParamList = FALSE;
2316 processAngleBracket ();
2317 break;
2319 case ')':
2320 if (firstChar)
2321 info->parameterCount = 0;
2322 --depth;
2323 break;
2325 case '(':
2326 info->isKnrParamList = FALSE;
2327 if (firstChar)
2329 info->isNameCandidate = FALSE;
2330 cppUngetc (c);
2331 vStringClear (Signature);
2332 skipMacro (st);
2333 depth = 0;
2334 vStringChop (Signature);
2336 else if (isType (token, TOKEN_PAREN_NAME))
2338 c = skipToNonWhite ();
2339 if (c == '*') /* check for function pointer */
2341 skipToMatch ("()");
2342 c = skipToNonWhite ();
2343 if (c == '(')
2344 skipToMatch ("()");
2345 else
2346 cppUngetc (c);
2348 else
2350 cppUngetc (c);
2351 cppUngetc ('(');
2352 info->nestedArgs = TRUE;
2355 else
2356 ++depth;
2357 break;
2359 default:
2360 if (c == '@' && isLanguage (Lang_java))
2362 parseJavaAnnotation(st);
2364 else if (isident1 (c))
2366 if (++identifierCount > 1)
2367 info->isKnrParamList = FALSE;
2368 readIdentifier (token, c);
2369 if (isType (token, TOKEN_NAME) && info->isNameCandidate)
2370 token->type = TOKEN_PAREN_NAME;
2371 else if (isType (token, TOKEN_KEYWORD))
2373 if (token->keyword != KEYWORD_CONST &&
2374 token->keyword != KEYWORD_VOLATILE)
2376 info->isKnrParamList = FALSE;
2377 info->isNameCandidate = FALSE;
2381 else
2383 info->isParamList = FALSE;
2384 info->isKnrParamList = FALSE;
2385 info->isNameCandidate = FALSE;
2386 info->invalidContents = TRUE;
2388 break;
2390 firstChar = FALSE;
2391 } while (! info->nestedArgs && depth > 0 &&
2392 (info->isKnrParamList || info->isNameCandidate));
2394 if (! info->nestedArgs) while (depth > 0)
2396 skipToMatch ("()");
2397 --depth;
2400 if (! info->isNameCandidate)
2401 initToken (token);
2403 vStringTerminate (Signature);
2404 if (info->isKnrParamList)
2405 vStringClear (Signature);
2406 CollectingSignature = FALSE;
2407 return nextChar;
2410 static void initParenInfo (parenInfo *const info)
2412 info->isPointer = FALSE;
2413 info->isParamList = TRUE;
2414 info->isKnrParamList = isLanguage (Lang_c);
2415 info->isNameCandidate = TRUE;
2416 info->invalidContents = FALSE;
2417 info->nestedArgs = FALSE;
2418 info->parameterCount = 0;
2421 static void analyzeParens (statementInfo *const st)
2423 tokenInfo *const prev = prevToken (st, 1);
2425 if (st->inFunction && ! st->assignment)
2426 st->notVariable = TRUE;
2427 if (! isType (prev, TOKEN_NONE)) /* in case of ignored enclosing macros */
2429 tokenInfo *const token = activeToken (st);
2430 parenInfo info;
2431 int c;
2433 initParenInfo (&info);
2434 parseParens (st, &info);
2435 parseReturnType (st);
2436 c = skipToNonWhite ();
2437 cppUngetc (c);
2438 if (info.invalidContents)
2439 reinitStatement (st, FALSE);
2440 else if (info.isNameCandidate && isType (token, TOKEN_PAREN_NAME) &&
2441 ! st->gotParenName &&
2442 (! info.isParamList || ! st->haveQualifyingName ||
2443 c == '(' ||
2444 (c == '=' && st->implementation != IMP_VIRTUAL) ||
2445 (st->declaration == DECL_NONE && isOneOf (c, ",;"))))
2447 token->type = TOKEN_NAME;
2448 processName (st);
2449 st->gotParenName = TRUE;
2450 if (! (c == '(' && info.nestedArgs))
2451 st->isPointer = info.isPointer;
2453 else if (! st->gotArgs && info.isParamList)
2455 st->gotArgs = TRUE;
2456 setToken (st, TOKEN_ARGS);
2457 advanceToken (st);
2458 if (st->scope != SCOPE_TYPEDEF)
2459 analyzePostParens (st, &info);
2461 else
2462 setToken (st, TOKEN_NONE);
2467 * Token parsing functions
2470 static void addContext (statementInfo *const st, const tokenInfo* const token)
2472 if (isType (token, TOKEN_NAME))
2474 if (vStringLength (st->context->name) > 0)
2476 if (isLanguage (Lang_c) || isLanguage (Lang_cpp))
2477 vStringCatS (st->context->name, "::");
2478 else if (isLanguage (Lang_java) || isLanguage (Lang_csharp))
2479 vStringCatS (st->context->name, ".");
2481 vStringCat (st->context->name, token->name);
2482 st->context->type = TOKEN_NAME;
2486 static boolean inheritingDeclaration (declType decl)
2488 /* C# supports inheritance for enums. C++0x will too, but not yet. */
2489 if (decl == DECL_ENUM)
2491 return (boolean) (isLanguage (Lang_csharp));
2493 return (boolean) (
2494 decl == DECL_CLASS ||
2495 decl == DECL_STRUCT ||
2496 decl == DECL_INTERFACE);
2499 static void processColon (statementInfo *const st)
2501 int c = (isLanguage (Lang_cpp) ? cppGetc () : skipToNonWhite ());
2502 const boolean doubleColon = (boolean) (c == ':');
2504 if (doubleColon)
2506 setToken (st, TOKEN_DOUBLE_COLON);
2507 st->haveQualifyingName = FALSE;
2509 else
2511 cppUngetc (c);
2512 if ((isLanguage (Lang_cpp) || isLanguage (Lang_csharp)) &&
2513 inheritingDeclaration (st->declaration))
2515 readParents (st, ':');
2517 else if (parentDecl (st) == DECL_STRUCT)
2519 c = skipToOneOf (",;");
2520 if (c == ',')
2521 setToken (st, TOKEN_COMMA);
2522 else if (c == ';')
2523 setToken (st, TOKEN_SEMICOLON);
2525 else
2527 const tokenInfo *const prev = prevToken (st, 1);
2528 const tokenInfo *const prev2 = prevToken (st, 2);
2529 if (prev->keyword == KEYWORD_DEFAULT ||
2530 prev2->keyword == KEYWORD_CASE ||
2531 st->parent != NULL)
2533 reinitStatement (st, FALSE);
2539 /* Skips over any initializing value which may follow an '=' character in a
2540 * variable definition.
2542 static int skipInitializer (statementInfo *const st)
2544 boolean done = FALSE;
2545 int c;
2547 while (! done)
2549 c = skipToNonWhite ();
2551 if (c == EOF)
2552 longjmp (Exception, (int) ExceptionFormattingError);
2553 else switch (c)
2555 case ',':
2556 case ';': done = TRUE; break;
2558 case '0':
2559 if (st->implementation == IMP_VIRTUAL)
2560 st->implementation = IMP_PURE_VIRTUAL;
2561 break;
2563 case '[': skipToMatch ("[]"); break;
2564 case '(': skipToMatch ("()"); break;
2565 case '{': skipToMatch ("{}"); break;
2566 case '<': processAngleBracket(); break;
2568 case '}':
2569 if (insideEnumBody (st))
2570 done = TRUE;
2571 else if (! isBraceFormat ())
2573 verbose ("%s: unexpected closing brace at line %lu\n",
2574 getInputFileName (), getInputLineNumber ());
2575 longjmp (Exception, (int) ExceptionBraceFormattingError);
2577 break;
2579 default: break;
2582 return c;
2585 static void processInitializer (statementInfo *const st)
2587 const boolean inEnumBody = insideEnumBody (st);
2588 int c = cppGetc ();
2590 if (c != '=')
2592 cppUngetc (c);
2593 c = skipInitializer (st);
2594 st->assignment = TRUE;
2595 if (c == ';')
2596 setToken (st, TOKEN_SEMICOLON);
2597 else if (c == ',')
2598 setToken (st, TOKEN_COMMA);
2599 else if (c == '}' && inEnumBody)
2601 cppUngetc (c);
2602 setToken (st, TOKEN_COMMA);
2604 if (st->scope == SCOPE_EXTERN)
2605 st->scope = SCOPE_GLOBAL;
2609 static void parseIdentifier (statementInfo *const st, const int c)
2611 tokenInfo *const token = activeToken (st);
2613 readIdentifier (token, c);
2614 if (! isType (token, TOKEN_NONE))
2615 processToken (token, st);
2618 static void parseGeneralToken (statementInfo *const st, const int c)
2620 const tokenInfo *const prev = prevToken (st, 1);
2622 if (isident1 (c) || (isLanguage (Lang_java) && isHighChar (c)))
2624 parseIdentifier (st, c);
2625 if (isType (st->context, TOKEN_NAME) &&
2626 isType (activeToken (st), TOKEN_NAME) && isType (prev, TOKEN_NAME))
2628 initToken (st->context);
2631 else if (c == '.' || c == '-')
2633 if (! st->assignment)
2634 st->notVariable = TRUE;
2635 if (c == '-')
2637 int c2 = cppGetc ();
2638 if (c2 != '>')
2639 cppUngetc (c2);
2642 else if (c == '!' || c == '>')
2644 int c2 = cppGetc ();
2645 if (c2 != '=')
2646 cppUngetc (c2);
2648 else if (c == '@' && isLanguage (Lang_java))
2650 parseJavaAnnotation (st);
2652 else if (isExternCDecl (st, c))
2654 st->declaration = DECL_NOMANGLE;
2655 st->scope = SCOPE_GLOBAL;
2659 /* Reads characters from the pre-processor and assembles tokens, setting
2660 * the current statement state.
2662 static void nextToken (statementInfo *const st)
2664 tokenInfo *token;
2667 int c = skipToNonWhite ();
2668 switch (c)
2670 case EOF: longjmp (Exception, (int) ExceptionEOF); break;
2671 /* analyze functions and co */
2672 case '(': analyzeParens (st); break;
2673 case '<': processAngleBracket (); break;
2674 case '*':
2675 st->haveQualifyingName = FALSE;
2676 setToken (st, TOKEN_STAR);
2677 break;
2678 case '&': setToken (st, TOKEN_AMPERSAND); break;
2680 case ',': setToken (st, TOKEN_COMMA); break;
2681 case ':': processColon (st); break;
2682 case ';': setToken (st, TOKEN_SEMICOLON); break;
2683 case '=': processInitializer (st); break;
2684 case '[': skipToMatch ("[]"); break;
2685 case '{': setToken (st, TOKEN_BRACE_OPEN); break;
2686 case '}': setToken (st, TOKEN_BRACE_CLOSE); break;
2687 default: parseGeneralToken (st, c); break;
2689 token = activeToken (st);
2690 } while (isType (token, TOKEN_NONE));
2694 * Scanning support functions
2697 static statementInfo *CurrentStatement = NULL;
2699 static statementInfo *newStatement (statementInfo *const parent)
2701 statementInfo *const st = xMalloc (1, statementInfo);
2702 unsigned int i;
2704 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
2705 st->token [i] = newToken ();
2707 st->context = newToken ();
2708 st->blockName = newToken ();
2709 st->parentClasses = vStringNew ();
2711 initStatement (st, parent);
2712 CurrentStatement = st;
2714 return st;
2717 static void deleteStatement (void)
2719 statementInfo *const st = CurrentStatement;
2720 statementInfo *const parent = st->parent;
2721 unsigned int i;
2723 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
2725 deleteToken (st->token [i]); st->token [i] = NULL;
2727 deleteToken (st->blockName); st->blockName = NULL;
2728 deleteToken (st->context); st->context = NULL;
2729 vStringDelete (st->parentClasses); st->parentClasses = NULL;
2730 eFree (st);
2731 CurrentStatement = parent;
2734 static void deleteAllStatements (void)
2736 while (CurrentStatement != NULL)
2737 deleteStatement ();
2740 static boolean isStatementEnd (const statementInfo *const st)
2742 const tokenInfo *const token = activeToken (st);
2743 boolean isEnd;
2745 if (isType (token, TOKEN_SEMICOLON))
2746 isEnd = TRUE;
2747 else if (isType (token, TOKEN_BRACE_CLOSE))
2748 /* Java and C# do not require semicolons to end a block. Neither do C++
2749 * namespaces. All other blocks require a semicolon to terminate them.
2751 isEnd = (boolean) (isLanguage (Lang_java) || isLanguage (Lang_csharp) ||
2752 ! isContextualStatement (st));
2753 else
2754 isEnd = FALSE;
2756 return isEnd;
2759 static void checkStatementEnd (statementInfo *const st)
2761 const tokenInfo *const token = activeToken (st);
2763 if (isType (token, TOKEN_COMMA))
2764 reinitStatement (st, TRUE);
2765 else if (isStatementEnd (st))
2767 DebugStatement ( if (debug (DEBUG_PARSE)) printf ("<ES>"); )
2768 reinitStatement (st, FALSE);
2769 cppEndStatement ();
2771 else
2773 cppBeginStatement ();
2774 advanceToken (st);
2778 static void nest (statementInfo *const st, const unsigned int nestLevel)
2780 switch (st->declaration)
2782 case DECL_CLASS:
2783 case DECL_ENUM:
2784 case DECL_INTERFACE:
2785 case DECL_NAMESPACE:
2786 case DECL_NOMANGLE:
2787 case DECL_STRUCT:
2788 case DECL_UNION:
2789 createTags (nestLevel, st);
2790 break;
2792 case DECL_FUNCTION:
2793 case DECL_TASK:
2794 st->inFunction = TRUE;
2796 /* fall through */
2797 default:
2798 if (includeTag (TAG_LOCAL, FALSE))
2799 createTags (nestLevel, st);
2800 else
2801 skipToMatch ("{}");
2802 break;
2804 advanceToken (st);
2805 setToken (st, TOKEN_BRACE_CLOSE);
2808 static void tagCheck (statementInfo *const st)
2810 const tokenInfo *const token = activeToken (st);
2811 const tokenInfo *const prev = prevToken (st, 1);
2812 const tokenInfo *const prev2 = prevToken (st, 2);
2814 switch (token->type)
2816 case TOKEN_NAME:
2817 if (insideEnumBody (st))
2818 qualifyEnumeratorTag (st, token);
2819 break;
2820 #if 0
2821 case TOKEN_PACKAGE:
2822 if (st->haveQualifyingName)
2823 makeTag (token, st, FALSE, TAG_PACKAGE);
2824 break;
2825 #endif
2826 case TOKEN_BRACE_OPEN:
2827 if (isType (prev, TOKEN_ARGS))
2829 if (st->haveQualifyingName)
2831 if (! isLanguage (Lang_vera))
2832 st->declaration = DECL_FUNCTION;
2833 if (isType (prev2, TOKEN_NAME))
2834 copyToken (st->blockName, prev2);
2835 qualifyFunctionTag (st, prev2);
2838 else if (isContextualStatement (st) ||
2839 st->declaration == DECL_NAMESPACE ||
2840 st->declaration == DECL_PROGRAM)
2842 if (isType (prev, TOKEN_NAME))
2843 copyToken (st->blockName, prev);
2844 else
2846 /* For an anonymous struct or union we use a unique ID
2847 * a number, so that the members can be found.
2849 char buf [20]; /* length of "_anon" + digits + null */
2850 sprintf (buf, "__anon%d", ++AnonymousID);
2851 vStringCopyS (st->blockName->name, buf);
2852 st->blockName->type = TOKEN_NAME;
2853 st->blockName->keyword = KEYWORD_NONE;
2855 qualifyBlockTag (st, prev);
2857 else if (isLanguage (Lang_csharp))
2858 makeTag (prev, st, FALSE, TAG_PROPERTY);
2859 break;
2861 case TOKEN_SEMICOLON:
2862 case TOKEN_COMMA:
2863 if (insideEnumBody (st))
2865 else if (isType (prev, TOKEN_NAME))
2867 if (isContextualKeyword (prev2))
2868 makeTag (prev, st, TRUE, TAG_EXTERN_VAR);
2869 else
2870 qualifyVariableTag (st, prev);
2872 else if (isType (prev, TOKEN_ARGS) && isType (prev2, TOKEN_NAME))
2874 if (st->isPointer)
2875 qualifyVariableTag (st, prev2);
2876 else
2877 qualifyFunctionDeclTag (st, prev2);
2879 if (isLanguage (Lang_java) && token->type == TOKEN_SEMICOLON && insideEnumBody (st))
2881 /* In Java, after an initial enum-like part,
2882 * a semicolon introduces a class-like part.
2883 * See Bug #1730485 for the full rationale. */
2884 st->parent->declaration = DECL_CLASS;
2886 break;
2888 default: break;
2892 /* Parses the current file and decides whether to write out and tags that
2893 * are discovered.
2895 static void createTags (const unsigned int nestLevel,
2896 statementInfo *const parent)
2898 statementInfo *const st = newStatement (parent);
2900 DebugStatement ( if (nestLevel > 0) debugParseNest (TRUE, nestLevel); )
2901 while (TRUE)
2903 tokenInfo *token;
2905 nextToken (st);
2906 token = activeToken (st);
2908 if (isType (token, TOKEN_BRACE_CLOSE))
2910 if (nestLevel > 0)
2911 break;
2912 else
2914 verbose ("%s: unexpected closing brace at line %lu\n",
2915 getInputFileName (), getInputLineNumber ());
2916 longjmp (Exception, (int) ExceptionBraceFormattingError);
2919 else if (isType (token, TOKEN_DOUBLE_COLON))
2921 addContext (st, prevToken (st, 1));
2922 advanceToken (st);
2924 else
2926 tagCheck (st);
2927 if (isType (token, TOKEN_BRACE_OPEN))
2928 nest (st, nestLevel + 1);
2929 checkStatementEnd (st);
2932 deleteStatement ();
2933 DebugStatement ( if (nestLevel > 0) debugParseNest (FALSE, nestLevel - 1); )
2936 static boolean findCTags (const unsigned int passCount)
2938 exception_t exception;
2939 boolean retry;
2941 Assert (passCount < 3);
2942 cppInit ((boolean) (passCount > 1), isLanguage (Lang_csharp));
2943 Signature = vStringNew ();
2944 ReturnType = vStringNew ();
2946 exception = (exception_t) setjmp (Exception);
2947 retry = FALSE;
2948 if (exception == ExceptionNone)
2949 createTags (0, NULL);
2950 else
2952 deleteAllStatements ();
2953 if (exception == ExceptionBraceFormattingError && passCount == 1)
2955 retry = TRUE;
2956 verbose ("%s: retrying file with fallback brace matching algorithm\n",
2957 getInputFileName ());
2960 vStringDelete (Signature);
2961 vStringDelete (ReturnType);
2962 cppTerminate ();
2963 return retry;
2966 static void buildKeywordHash (const langType language, unsigned int idx)
2968 const size_t count = sizeof (KeywordTable) / sizeof (KeywordTable [0]);
2969 size_t i;
2970 for (i = 0 ; i < count ; ++i)
2972 const keywordDesc* const p = &KeywordTable [i];
2973 if (p->isValid [idx])
2974 addKeyword (p->name, language, (int) p->id);
2978 static void initializeCParser (const langType language)
2980 Lang_c = language;
2981 buildKeywordHash (language, 0);
2984 static void initializeCppParser (const langType language)
2986 Lang_cpp = language;
2987 buildKeywordHash (language, 1);
2990 static void initializeCsharpParser (const langType language)
2992 Lang_csharp = language;
2993 buildKeywordHash (language, 2);
2996 static void initializeJavaParser (const langType language)
2998 Lang_java = language;
2999 buildKeywordHash (language, 3);
3002 static void initializeVeraParser (const langType language)
3004 Lang_vera = language;
3005 buildKeywordHash (language, 4);
3008 extern parserDefinition* CParser (void)
3010 static const char *const extensions [] = { "c", NULL };
3011 parserDefinition* def = parserNew ("C");
3012 def->kinds = CKinds;
3013 def->kindCount = KIND_COUNT (CKinds);
3014 def->extensions = extensions;
3015 def->parser2 = findCTags;
3016 def->initialize = initializeCParser;
3017 return def;
3020 extern parserDefinition* CppParser (void)
3022 static const char *const extensions [] = {
3023 "c++", "cc", "cp", "cpp", "cxx", "h", "h++", "hh", "hp", "hpp", "hxx",
3024 #ifndef CASE_INSENSITIVE_FILENAMES
3025 "C", "H",
3026 #endif
3027 NULL
3029 parserDefinition* def = parserNew ("C++");
3030 def->kinds = CKinds;
3031 def->kindCount = KIND_COUNT (CKinds);
3032 def->extensions = extensions;
3033 def->parser2 = findCTags;
3034 def->initialize = initializeCppParser;
3035 return def;
3038 extern parserDefinition* CsharpParser (void)
3040 static const char *const extensions [] = { "cs", NULL };
3041 parserDefinition* def = parserNew ("C#");
3042 def->kinds = CsharpKinds;
3043 def->kindCount = KIND_COUNT (CsharpKinds);
3044 def->extensions = extensions;
3045 def->parser2 = findCTags;
3046 def->initialize = initializeCsharpParser;
3047 return def;
3050 extern parserDefinition* JavaParser (void)
3052 static const char *const extensions [] = { "java", NULL };
3053 parserDefinition* def = parserNew ("Java");
3054 def->kinds = JavaKinds;
3055 def->kindCount = KIND_COUNT (JavaKinds);
3056 def->extensions = extensions;
3057 def->parser2 = findCTags;
3058 def->initialize = initializeJavaParser;
3059 return def;
3062 extern parserDefinition* VeraParser (void)
3064 static const char *const extensions [] = { "vr", "vri", "vrh", NULL };
3065 parserDefinition* def = parserNew ("Vera");
3066 def->kinds = VeraKinds;
3067 def->kindCount = KIND_COUNT (VeraKinds);
3068 def->extensions = extensions;
3069 def->parser2 = findCTags;
3070 def->initialize = initializeVeraParser;
3071 return def;
3074 /* vi:set tabstop=4 shiftwidth=4 noexpandtab: */