Handle template expressions that may use the << or >> operators
[arduino-ctags.git] / c.c
blobb975453952befcdf74a709ac6d866e5a73ec30cb
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 // watch out for '<<' in template arguments
1396 int x = cppGetc ();
1397 if(c == '<' && x == '<') {
1398 // we've found a << - do nothing
1399 } else {
1400 cppUngetc (x);
1401 ++matchLevel;
1402 if (braceFormatting && getDirectiveNestLevel () != initialLevel)
1404 skipToFormattedBraceMatch ();
1405 break;
1409 else if (c == end)
1411 // watch out for '>>' in template arguments
1412 int x = cppGetc ();
1413 if(c == '>' && x == '>') {
1414 // we've found a >> in a template - skip it
1415 } else {
1416 cppUngetc (x);
1417 --matchLevel;
1418 if (braceFormatting && getDirectiveNestLevel () != initialLevel)
1420 skipToFormattedBraceMatch ();
1421 break;
1426 if (c == EOF)
1428 verbose ("%s: failed to find match for '%c' at line %lu\n",
1429 getInputFileName (), begin, inputLineNumber);
1430 if (braceMatching)
1431 longjmp (Exception, (int) ExceptionBraceFormattingError);
1432 else
1433 longjmp (Exception, (int) ExceptionFormattingError);
1437 static void skipParens (void)
1439 const int c = skipToNonWhite ();
1441 if (c == '(')
1442 skipToMatch ("()");
1443 else
1444 cppUngetc (c);
1447 static void skipBraces (void)
1449 const int c = skipToNonWhite ();
1451 if (c == '{')
1452 skipToMatch ("{}");
1453 else
1454 cppUngetc (c);
1457 static keywordId analyzeKeyword (const char *const name)
1459 const keywordId id = (keywordId) lookupKeyword (name, getSourceLanguage ());
1460 return id;
1463 static void analyzeIdentifier (tokenInfo *const token)
1465 char *const name = vStringValue (token->name);
1466 const char *replacement = NULL;
1467 boolean parensToo = FALSE;
1469 if (isLanguage (Lang_java) ||
1470 ! isIgnoreToken (name, &parensToo, &replacement))
1472 if (replacement != NULL)
1473 token->keyword = analyzeKeyword (replacement);
1474 else
1475 token->keyword = analyzeKeyword (vStringValue (token->name));
1477 if (token->keyword == KEYWORD_NONE)
1478 token->type = TOKEN_NAME;
1479 else
1480 token->type = TOKEN_KEYWORD;
1482 else
1484 initToken (token);
1485 if (parensToo)
1487 int c = skipToNonWhite ();
1489 if (c == '(')
1490 skipToMatch ("()");
1495 static void readIdentifier (tokenInfo *const token, const int firstChar)
1497 vString *const name = token->name;
1498 int c = firstChar;
1499 boolean first = TRUE;
1501 initToken (token);
1503 /* Bug #1585745: strangely, C++ destructors allow whitespace between
1504 * the ~ and the class name. */
1505 if (isLanguage (Lang_cpp) && firstChar == '~')
1507 vStringPut (name, c);
1508 c = skipToNonWhite ();
1513 vStringPut (name, c);
1514 if (CollectingSignature)
1516 if (!first)
1517 vStringPut (Signature, c);
1518 first = FALSE;
1520 c = cppGetc ();
1521 } while (isident (c) || ((isLanguage (Lang_java) || isLanguage (Lang_csharp)) && (isHighChar (c) || c == '.')));
1522 vStringTerminate (name);
1523 cppUngetc (c); /* unget non-identifier character */
1525 analyzeIdentifier (token);
1528 static void readPackageName (tokenInfo *const token, const int firstChar)
1530 vString *const name = token->name;
1531 int c = firstChar;
1533 initToken (token);
1535 while (isident (c) || c == '.')
1537 vStringPut (name, c);
1538 c = cppGetc ();
1540 vStringTerminate (name);
1541 cppUngetc (c); /* unget non-package character */
1544 static void readPackageOrNamespace (statementInfo *const st, const declType declaration)
1546 st->declaration = declaration;
1548 if (declaration == DECL_NAMESPACE && !isLanguage (Lang_csharp))
1550 /* In C++ a namespace is specified one level at a time. */
1551 return;
1553 else
1555 /* In C#, a namespace can also be specified like a Java package name. */
1556 tokenInfo *const token = activeToken (st);
1557 Assert (isType (token, TOKEN_KEYWORD));
1558 readPackageName (token, skipToNonWhite ());
1559 token->type = TOKEN_NAME;
1560 st->gotName = TRUE;
1561 st->haveQualifyingName = TRUE;
1565 static void processName (statementInfo *const st)
1567 Assert (isType (activeToken (st), TOKEN_NAME));
1568 if (st->gotName && st->declaration == DECL_NONE)
1569 st->declaration = DECL_BASE;
1570 st->gotName = TRUE;
1571 st->haveQualifyingName = TRUE;
1574 static void readOperator (statementInfo *const st)
1576 const char *const acceptable = "+-*/%^&|~!=<>,[]";
1577 const tokenInfo* const prev = prevToken (st,1);
1578 tokenInfo *const token = activeToken (st);
1579 vString *const name = token->name;
1580 int c = skipToNonWhite ();
1582 /* When we arrive here, we have the keyword "operator" in 'name'.
1584 if (isType (prev, TOKEN_KEYWORD) && (prev->keyword == KEYWORD_ENUM ||
1585 prev->keyword == KEYWORD_STRUCT || prev->keyword == KEYWORD_UNION))
1586 ; /* ignore "operator" keyword if preceded by these keywords */
1587 else if (c == '(')
1589 /* Verify whether this is a valid function call (i.e. "()") operator.
1591 if (cppGetc () == ')')
1593 vStringPut (name, ' '); /* always separate operator from keyword */
1594 c = skipToNonWhite ();
1595 if (c == '(')
1596 vStringCatS (name, "()");
1598 else
1600 skipToMatch ("()");
1601 c = cppGetc ();
1604 else if (isident1 (c))
1606 /* Handle "new" and "delete" operators, and conversion functions
1607 * (per 13.3.1.1.2 [2] of the C++ spec).
1609 boolean whiteSpace = TRUE; /* default causes insertion of space */
1612 if (isspace (c))
1613 whiteSpace = TRUE;
1614 else
1616 if (whiteSpace)
1618 vStringPut (name, ' ');
1619 whiteSpace = FALSE;
1621 vStringPut (name, c);
1623 c = cppGetc ();
1624 } while (! isOneOf (c, "(;") && c != EOF);
1625 vStringTerminate (name);
1627 else if (isOneOf (c, acceptable))
1629 vStringPut (name, ' '); /* always separate operator from keyword */
1632 vStringPut (name, c);
1633 c = cppGetc ();
1634 } while (isOneOf (c, acceptable));
1635 vStringTerminate (name);
1638 cppUngetc (c);
1640 token->type = TOKEN_NAME;
1641 token->keyword = KEYWORD_NONE;
1642 processName (st);
1645 static void copyToken (tokenInfo *const dest, const tokenInfo *const src)
1647 dest->type = src->type;
1648 dest->keyword = src->keyword;
1649 dest->filePosition = src->filePosition;
1650 dest->lineNumber = src->lineNumber;
1651 vStringCopy (dest->name, src->name);
1654 static void setAccess (statementInfo *const st, const accessType access)
1656 if (isMember (st))
1658 if (isLanguage (Lang_cpp))
1660 int c = skipToNonWhite ();
1662 if (c == ':')
1663 reinitStatement (st, FALSE);
1664 else
1665 cppUngetc (c);
1667 st->member.accessDefault = access;
1669 st->member.access = access;
1673 static void discardTypeList (tokenInfo *const token)
1675 int c = skipToNonWhite ();
1676 while (isident1 (c))
1678 readIdentifier (token, c);
1679 c = skipToNonWhite ();
1680 if (c == '.' || c == ',')
1681 c = skipToNonWhite ();
1683 cppUngetc (c);
1686 static void addParentClass (statementInfo *const st, tokenInfo *const token)
1688 if (vStringLength (token->name) > 0 &&
1689 vStringLength (st->parentClasses) > 0)
1691 vStringPut (st->parentClasses, ',');
1693 vStringCat (st->parentClasses, token->name);
1696 static void readParents (statementInfo *const st, const int qualifier)
1698 tokenInfo *const token = newToken ();
1699 tokenInfo *const parent = newToken ();
1700 int c;
1704 c = skipToNonWhite ();
1705 if (isident1 (c))
1707 readIdentifier (token, c);
1708 if (isType (token, TOKEN_NAME))
1709 vStringCat (parent->name, token->name);
1710 else
1712 addParentClass (st, parent);
1713 initToken (parent);
1716 else if (c == qualifier)
1717 vStringPut (parent->name, c);
1718 else if (c == '<')
1719 skipToMatch ("<>");
1720 else if (isType (token, TOKEN_NAME))
1722 addParentClass (st, parent);
1723 initToken (parent);
1725 } while (c != '{' && c != EOF);
1726 cppUngetc (c);
1727 deleteToken (parent);
1728 deleteToken (token);
1731 static void skipStatement (statementInfo *const st)
1733 st->declaration = DECL_IGNORE;
1734 skipToOneOf (";");
1737 static void processInterface (statementInfo *const st)
1739 st->declaration = DECL_INTERFACE;
1742 static void checkIsClassEnum (statementInfo *const st, const declType decl)
1744 if (! isLanguage (Lang_cpp) || st->declaration != DECL_ENUM)
1745 st->declaration = decl;
1748 static void processToken (tokenInfo *const token, statementInfo *const st)
1750 switch (token->keyword) /* is it a reserved word? */
1752 default: break;
1754 case KEYWORD_NONE: processName (st); break;
1755 case KEYWORD_ABSTRACT: st->implementation = IMP_ABSTRACT; break;
1756 case KEYWORD_ATTRIBUTE:
1757 case KEYWORD_TYPENAME:
1758 case KEYWORD_INLINE: skipParens (); initToken (token); break;
1759 case KEYWORD_BIND: st->declaration = DECL_BASE; break;
1760 case KEYWORD_BIT: st->declaration = DECL_BASE; break;
1761 case KEYWORD_CATCH: skipParens (); skipBraces (); break;
1762 case KEYWORD_CHAR: st->declaration = DECL_BASE; break;
1763 case KEYWORD_CLASS: checkIsClassEnum (st, DECL_CLASS); break;
1764 case KEYWORD_CONST: st->declaration = DECL_BASE; break;
1765 case KEYWORD_DOUBLE: st->declaration = DECL_BASE; break;
1766 case KEYWORD_ENUM: st->declaration = DECL_ENUM; break;
1767 case KEYWORD_EXTENDS: readParents (st, '.');
1768 setToken (st, TOKEN_NONE); break;
1769 case KEYWORD_FLOAT: st->declaration = DECL_BASE; break;
1770 case KEYWORD_FUNCTION: st->declaration = DECL_BASE; break;
1771 case KEYWORD_FRIEND: st->scope = SCOPE_FRIEND; break;
1772 case KEYWORD_GOTO: skipStatement (st); break;
1773 case KEYWORD_IMPLEMENTS:readParents (st, '.');
1774 setToken (st, TOKEN_NONE); break;
1775 case KEYWORD_IMPORT: skipStatement (st); break;
1776 case KEYWORD_INT: st->declaration = DECL_BASE; break;
1777 case KEYWORD_INTEGER: st->declaration = DECL_BASE; break;
1778 case KEYWORD_INTERFACE: processInterface (st); break;
1779 case KEYWORD_LOCAL: setAccess (st, ACCESS_LOCAL); break;
1780 case KEYWORD_LONG: st->declaration = DECL_BASE; break;
1781 case KEYWORD_OPERATOR: readOperator (st); break;
1782 case KEYWORD_PRIVATE: setAccess (st, ACCESS_PRIVATE); break;
1783 case KEYWORD_PROGRAM: st->declaration = DECL_PROGRAM; break;
1784 case KEYWORD_PROTECTED: setAccess (st, ACCESS_PROTECTED); break;
1785 case KEYWORD_PUBLIC: setAccess (st, ACCESS_PUBLIC); break;
1786 case KEYWORD_RETURN: skipStatement (st); break;
1787 case KEYWORD_SHORT: st->declaration = DECL_BASE; break;
1788 case KEYWORD_SIGNED: st->declaration = DECL_BASE; break;
1789 case KEYWORD_STATIC_ASSERT: skipParens(); break;
1790 case KEYWORD_STRING: st->declaration = DECL_BASE; break;
1791 case KEYWORD_STRUCT: checkIsClassEnum (st, DECL_STRUCT); break;
1792 case KEYWORD_TASK: st->declaration = DECL_TASK; break;
1793 case KEYWORD_THROWS: discardTypeList (token); break;
1794 case KEYWORD_UNION: st->declaration = DECL_UNION; break;
1795 case KEYWORD_UNSIGNED: st->declaration = DECL_BASE; break;
1796 case KEYWORD_USING: skipStatement (st); break;
1797 case KEYWORD_VOID: st->declaration = DECL_BASE; break;
1798 case KEYWORD_VOLATILE: st->declaration = DECL_BASE; break;
1799 case KEYWORD_VIRTUAL: st->implementation = IMP_VIRTUAL; break;
1800 case KEYWORD_WCHAR_T: st->declaration = DECL_BASE; break;
1802 case KEYWORD_NAMESPACE: readPackageOrNamespace (st, DECL_NAMESPACE); break;
1803 case KEYWORD_PACKAGE: readPackageOrNamespace (st, DECL_PACKAGE); break;
1805 case KEYWORD_EVENT:
1806 if (isLanguage (Lang_csharp))
1807 st->declaration = DECL_EVENT;
1808 break;
1810 case KEYWORD_TYPEDEF:
1811 reinitStatement (st, FALSE);
1812 st->scope = SCOPE_TYPEDEF;
1813 break;
1815 case KEYWORD_EXTERN:
1816 if (! isLanguage (Lang_csharp) || !st->gotName)
1818 reinitStatement (st, FALSE);
1819 st->scope = SCOPE_EXTERN;
1820 st->declaration = DECL_BASE;
1822 break;
1824 case KEYWORD_STATIC:
1825 if (! (isLanguage (Lang_java) || isLanguage (Lang_csharp)))
1827 reinitStatement (st, FALSE);
1828 st->scope = SCOPE_STATIC;
1829 st->declaration = DECL_BASE;
1831 break;
1833 case KEYWORD_FOR:
1834 case KEYWORD_FOREACH:
1835 case KEYWORD_IF:
1836 case KEYWORD_SWITCH:
1837 case KEYWORD_WHILE:
1839 int c = skipToNonWhite ();
1840 if (c == '(')
1841 skipToMatch ("()");
1842 break;
1848 * Parenthesis handling functions
1851 static void restartStatement (statementInfo *const st)
1853 tokenInfo *const save = newToken ();
1854 tokenInfo *token = activeToken (st);
1856 copyToken (save, token);
1857 DebugStatement ( if (debug (DEBUG_PARSE)) printf ("<ES>");)
1858 reinitStatement (st, FALSE);
1859 token = activeToken (st);
1860 copyToken (token, save);
1861 deleteToken (save);
1862 processToken (token, st);
1865 /* Skips over a the mem-initializer-list of a ctor-initializer, defined as:
1867 * mem-initializer-list:
1868 * mem-initializer, mem-initializer-list
1870 * mem-initializer:
1871 * [::] [nested-name-spec] class-name (...)
1872 * identifier
1874 static void skipMemIntializerList (tokenInfo *const token)
1876 int c;
1880 c = skipToNonWhite ();
1881 while (isident1 (c) || c == ':')
1883 if (c != ':')
1884 readIdentifier (token, c);
1885 c = skipToNonWhite ();
1887 if (c == '<')
1889 skipToMatch ("<>");
1890 c = skipToNonWhite ();
1892 if (c == '(')
1894 skipToMatch ("()");
1895 c = skipToNonWhite ();
1897 } while (c == ',');
1898 cppUngetc (c);
1901 static void skipMacro (statementInfo *const st)
1903 tokenInfo *const prev2 = prevToken (st, 2);
1905 if (isType (prev2, TOKEN_NAME))
1906 retardToken (st);
1907 skipToMatch ("()");
1910 /* Skips over characters following the parameter list. This will be either
1911 * non-ANSI style function declarations or C++ stuff. Our choices:
1913 * C (K&R):
1914 * int func ();
1915 * int func (one, two) int one; float two; {...}
1916 * C (ANSI):
1917 * int func (int one, float two);
1918 * int func (int one, float two) {...}
1919 * C++:
1920 * int foo (...) [const|volatile] [throw (...)];
1921 * int foo (...) [const|volatile] [throw (...)] [ctor-initializer] {...}
1922 * int foo (...) [const|volatile] [throw (...)] try [ctor-initializer] {...}
1923 * catch (...) {...}
1925 static boolean skipPostArgumentStuff (
1926 statementInfo *const st, parenInfo *const info)
1928 tokenInfo *const token = activeToken (st);
1929 unsigned int parameters = info->parameterCount;
1930 unsigned int elementCount = 0;
1931 boolean restart = FALSE;
1932 boolean end = FALSE;
1933 int c = skipToNonWhite ();
1937 switch (c)
1939 case ')': break;
1940 case ':': skipMemIntializerList (token);break; /* ctor-initializer */
1941 case '[': skipToMatch ("[]"); break;
1942 case '=': cppUngetc (c); end = TRUE; break;
1943 case '{': cppUngetc (c); end = TRUE; break;
1944 case '}': cppUngetc (c); end = TRUE; break;
1946 case '(':
1947 if (elementCount > 0)
1948 ++elementCount;
1949 skipToMatch ("()");
1950 break;
1952 case ';':
1953 if (parameters == 0 || elementCount < 2)
1955 cppUngetc (c);
1956 end = TRUE;
1958 else if (--parameters == 0)
1959 end = TRUE;
1960 break;
1962 default:
1963 if (isident1 (c))
1965 readIdentifier (token, c);
1966 switch (token->keyword)
1968 case KEYWORD_ATTRIBUTE: skipParens (); break;
1969 case KEYWORD_THROW: skipParens (); break;
1970 case KEYWORD_TRY: break;
1972 case KEYWORD_CONST:
1973 case KEYWORD_VOLATILE:
1974 if (vStringLength (Signature) > 0)
1976 vStringPut (Signature, ' ');
1977 vStringCat (Signature, token->name);
1979 break;
1981 case KEYWORD_CATCH:
1982 case KEYWORD_CLASS:
1983 case KEYWORD_EXPLICIT:
1984 case KEYWORD_EXTERN:
1985 case KEYWORD_FRIEND:
1986 case KEYWORD_INLINE:
1987 case KEYWORD_MUTABLE:
1988 case KEYWORD_NAMESPACE:
1989 case KEYWORD_NEW:
1990 case KEYWORD_NEWCOV:
1991 case KEYWORD_NOEXCEPT:
1992 case KEYWORD_OPERATOR:
1993 case KEYWORD_OVERLOAD:
1994 case KEYWORD_PRIVATE:
1995 case KEYWORD_PROTECTED:
1996 case KEYWORD_PUBLIC:
1997 case KEYWORD_STATIC:
1998 case KEYWORD_TEMPLATE:
1999 case KEYWORD_TYPEDEF:
2000 case KEYWORD_TYPENAME:
2001 case KEYWORD_USING:
2002 case KEYWORD_VIRTUAL:
2003 /* Never allowed within parameter declarations. */
2004 restart = TRUE;
2005 end = TRUE;
2006 break;
2008 default:
2009 /* "override" and "final" are only keywords in the declaration of a virtual
2010 * member function, so need to be handled specially, not as keywords */
2011 if (isLanguage(Lang_cpp) && isType (token, TOKEN_NAME) &&
2012 (strcmp ("override", vStringValue (token->name)) == 0 ||
2013 strcmp ("final", vStringValue (token->name)) == 0))
2015 else if (isType (token, TOKEN_NONE))
2017 else if (info->isKnrParamList && info->parameterCount > 0)
2018 ++elementCount;
2019 else
2021 /* If we encounter any other identifier immediately
2022 * following an empty parameter list, this is almost
2023 * certainly one of those Microsoft macro "thingies"
2024 * that the automatic source code generation sticks
2025 * in. Terminate the current statement.
2027 restart = TRUE;
2028 end = TRUE;
2030 break;
2034 if (! end)
2036 c = skipToNonWhite ();
2037 if (c == EOF)
2038 end = TRUE;
2040 } while (! end);
2042 if (restart)
2043 restartStatement (st);
2044 else
2045 setToken (st, TOKEN_NONE);
2047 return (boolean) (c != EOF);
2050 static void skipJavaThrows (statementInfo *const st)
2052 tokenInfo *const token = activeToken (st);
2053 int c = skipToNonWhite ();
2055 if (isident1 (c))
2057 readIdentifier (token, c);
2058 if (token->keyword == KEYWORD_THROWS)
2062 c = skipToNonWhite ();
2063 if (isident1 (c))
2065 readIdentifier (token, c);
2066 c = skipToNonWhite ();
2068 } while (c == '.' || c == ',');
2071 cppUngetc (c);
2072 setToken (st, TOKEN_NONE);
2075 static void analyzePostParens (statementInfo *const st, parenInfo *const info)
2077 const unsigned long inputLineNumber = getInputLineNumber ();
2078 int c = skipToNonWhite ();
2080 cppUngetc (c);
2081 if (isOneOf (c, "{;,="))
2083 else if (isLanguage (Lang_java))
2084 skipJavaThrows (st);
2085 else
2087 if (! skipPostArgumentStuff (st, info))
2089 verbose (
2090 "%s: confusing argument declarations beginning at line %lu\n",
2091 getInputFileName (), inputLineNumber);
2092 longjmp (Exception, (int) ExceptionFormattingError);
2097 static boolean languageSupportsGenerics (void)
2099 return (boolean) (isLanguage (Lang_cpp) || isLanguage (Lang_csharp) ||
2100 isLanguage (Lang_java));
2103 static void processAngleBracket (void)
2105 int c = cppGetc ();
2106 if (c == '>') {
2107 /* already found match for template */
2108 } else if (languageSupportsGenerics () && c != '<' && c != '=') {
2109 /* this is a template */
2110 cppUngetc (c);
2111 skipToMatch ("<>");
2112 } else if (c == '<') {
2113 /* skip "<<" or "<<=". */
2114 c = cppGetc ();
2115 if (c != '=') {
2116 cppUngetc (c);
2118 } else {
2119 cppUngetc (c);
2123 static void parseJavaAnnotation (statementInfo *const st)
2126 * @Override
2127 * @Target(ElementType.METHOD)
2128 * @SuppressWarnings(value = "unchecked")
2130 * But watch out for "@interface"!
2132 tokenInfo *const token = activeToken (st);
2134 int c = skipToNonWhite ();
2135 readIdentifier (token, c);
2136 if (token->keyword == KEYWORD_INTERFACE)
2138 /* Oops. This was actually "@interface" defining a new annotation. */
2139 processInterface (st);
2141 else
2143 /* Bug #1691412: skip any annotation arguments. */
2144 skipParens ();
2148 static void parseReturnType (statementInfo *const st)
2150 int i;
2151 int lower_bound;
2152 int upper_bound;
2153 tokenInfo * finding_tok;
2155 /* FIXME TODO: if java language must be supported then impement this here
2156 * removing the current FIXME */
2157 if (!isLanguage (Lang_c) && !isLanguage (Lang_cpp))
2159 return;
2162 vStringClear (ReturnType);
2164 finding_tok = prevToken (st, 1);
2166 if (isType (finding_tok, TOKEN_NONE))
2167 return;
2169 finding_tok = prevToken (st, 2);
2171 if (finding_tok->type == TOKEN_DOUBLE_COLON)
2173 /* get the total number of double colons */
2174 int j;
2175 int num_colons = 0;
2177 /* we already are at 2nd token */
2178 /* the +=2 means that colons are usually found at even places */
2179 for (j = 2; j < NumTokens; j+=2)
2181 tokenInfo *curr_tok;
2182 curr_tok = prevToken (st, j);
2183 if (curr_tok->type == TOKEN_DOUBLE_COLON)
2184 num_colons++;
2185 else
2186 break;
2189 /*printf ("FOUND colons %d\n", num_colons);*/
2190 lower_bound = 2 * num_colons + 1;
2192 else
2193 lower_bound = 1;
2195 upper_bound = -1;
2196 for (i = 0; i < NumTokens; i++) {
2197 tokenInfo *curr_tok;
2198 curr_tok = prevToken (st, i);
2199 if (curr_tok->type == TOKEN_BRACE_CLOSE || curr_tok->type == TOKEN_BRACE_OPEN) {
2200 upper_bound = i - 1;
2201 break;
2204 if (upper_bound < 0) {
2205 upper_bound = NumTokens - 1;
2208 for (i = upper_bound; i > lower_bound; i--)
2210 tokenInfo * curr_tok;
2211 curr_tok = prevToken (st, i);
2213 switch (curr_tok->type)
2215 case TOKEN_PAREN_NAME:
2216 case TOKEN_NONE:
2217 continue;
2218 break;
2220 case TOKEN_DOUBLE_COLON:
2221 /* usually C++ class scope */
2222 vStringCatS (ReturnType, "::");
2223 break;
2225 case TOKEN_STAR:
2226 /* pointers */
2227 vStringPut (ReturnType, '*');
2228 break;
2230 case TOKEN_AMPERSAND:
2231 /* references */
2232 vStringPut (ReturnType, '&');
2233 break;
2235 default:
2236 vStringCat (ReturnType, curr_tok->name);
2237 if (curr_tok->type == TOKEN_KEYWORD) {
2238 vStringPut (ReturnType, ' ');
2240 break;
2244 /* clear any white space from the front */
2245 vStringStripLeading (ReturnType);
2247 /* .. and from the tail too */
2248 vStringStripTrailing (ReturnType);
2250 /* put and end marker */
2251 vStringTerminate (ReturnType);
2254 printf ("~~~~~ statement ---->\n");
2255 ps (st);
2256 printf ("NumTokens: %d\n", NumTokens);
2257 printf ("FOUND ReturnType: %s\n", vStringValue (ReturnType));
2258 printf ("<~~~~~\n");
2259 //*/
2262 static int parseParens (statementInfo *const st, parenInfo *const info)
2264 tokenInfo *const token = activeToken (st);
2265 unsigned int identifierCount = 0;
2266 unsigned int depth = 1;
2267 boolean firstChar = TRUE;
2268 int nextChar = '\0';
2270 CollectingSignature = TRUE;
2271 vStringClear (Signature);
2272 vStringPut (Signature, '(');
2273 info->parameterCount = 1;
2276 int c = skipToNonWhite ();
2277 vStringPut (Signature, c);
2279 switch (c)
2281 case '&':
2282 case '*':
2283 info->isPointer = TRUE;
2284 info->isKnrParamList = FALSE;
2285 if (identifierCount == 0)
2286 info->isParamList = FALSE;
2287 initToken (token);
2288 break;
2290 case ':':
2291 info->isKnrParamList = FALSE;
2292 break;
2294 case '.':
2295 info->isNameCandidate = FALSE;
2296 c = cppGetc ();
2297 if (c != '.')
2299 cppUngetc (c);
2300 info->isKnrParamList = FALSE;
2302 else
2304 c = cppGetc ();
2305 if (c != '.')
2307 cppUngetc (c);
2308 info->isKnrParamList = FALSE;
2310 else
2311 vStringCatS (Signature, "..."); /* variable arg list */
2313 break;
2315 case ',':
2316 info->isNameCandidate = FALSE;
2317 if (info->isKnrParamList)
2319 ++info->parameterCount;
2320 identifierCount = 0;
2322 break;
2324 case '=':
2325 info->isKnrParamList = FALSE;
2326 info->isNameCandidate = FALSE;
2327 if (firstChar)
2329 info->isParamList = FALSE;
2330 skipMacro (st);
2331 depth = 0;
2333 break;
2335 case '[':
2336 info->isKnrParamList = FALSE;
2337 skipToMatch ("[]");
2338 break;
2340 case '<':
2341 info->isKnrParamList = FALSE;
2342 processAngleBracket ();
2343 break;
2345 case ')':
2346 if (firstChar)
2347 info->parameterCount = 0;
2348 --depth;
2349 break;
2351 case '(':
2352 info->isKnrParamList = FALSE;
2353 if (firstChar)
2355 info->isNameCandidate = FALSE;
2356 cppUngetc (c);
2357 vStringClear (Signature);
2358 skipMacro (st);
2359 depth = 0;
2360 vStringChop (Signature);
2362 else if (isType (token, TOKEN_PAREN_NAME))
2364 c = skipToNonWhite ();
2365 if (c == '*') /* check for function pointer */
2367 skipToMatch ("()");
2368 c = skipToNonWhite ();
2369 if (c == '(')
2370 skipToMatch ("()");
2371 else
2372 cppUngetc (c);
2374 else
2376 cppUngetc (c);
2377 cppUngetc ('(');
2378 info->nestedArgs = TRUE;
2381 else
2382 ++depth;
2383 break;
2385 default:
2386 if (c == '@' && isLanguage (Lang_java))
2388 parseJavaAnnotation(st);
2390 else if (isident1 (c))
2392 if (++identifierCount > 1)
2393 info->isKnrParamList = FALSE;
2394 readIdentifier (token, c);
2395 if (isType (token, TOKEN_NAME) && info->isNameCandidate)
2396 token->type = TOKEN_PAREN_NAME;
2397 else if (isType (token, TOKEN_KEYWORD))
2399 if (token->keyword != KEYWORD_CONST &&
2400 token->keyword != KEYWORD_VOLATILE)
2402 info->isKnrParamList = FALSE;
2403 info->isNameCandidate = FALSE;
2407 else
2409 info->isParamList = FALSE;
2410 info->isKnrParamList = FALSE;
2411 info->isNameCandidate = FALSE;
2412 info->invalidContents = TRUE;
2414 break;
2416 firstChar = FALSE;
2417 } while (! info->nestedArgs && depth > 0 &&
2418 (info->isKnrParamList || info->isNameCandidate));
2420 if (! info->nestedArgs) while (depth > 0)
2422 skipToMatch ("()");
2423 --depth;
2426 if (! info->isNameCandidate)
2427 initToken (token);
2429 vStringTerminate (Signature);
2430 if (info->isKnrParamList)
2431 vStringClear (Signature);
2432 CollectingSignature = FALSE;
2433 return nextChar;
2436 static void initParenInfo (parenInfo *const info)
2438 info->isPointer = FALSE;
2439 info->isParamList = TRUE;
2440 info->isKnrParamList = isLanguage (Lang_c);
2441 info->isNameCandidate = TRUE;
2442 info->invalidContents = FALSE;
2443 info->nestedArgs = FALSE;
2444 info->parameterCount = 0;
2447 static void analyzeParens (statementInfo *const st)
2449 tokenInfo *const prev = prevToken (st, 1);
2451 if (st->inFunction && ! st->assignment)
2452 st->notVariable = TRUE;
2453 if (! isType (prev, TOKEN_NONE)) /* in case of ignored enclosing macros */
2455 tokenInfo *const token = activeToken (st);
2456 parenInfo info;
2457 int c;
2459 initParenInfo (&info);
2460 parseParens (st, &info);
2461 parseReturnType (st);
2462 c = skipToNonWhite ();
2463 cppUngetc (c);
2464 if (info.invalidContents)
2465 reinitStatement (st, FALSE);
2466 else if (info.isNameCandidate && isType (token, TOKEN_PAREN_NAME) &&
2467 ! st->gotParenName &&
2468 (! info.isParamList || ! st->haveQualifyingName ||
2469 c == '(' ||
2470 (c == '=' && st->implementation != IMP_VIRTUAL) ||
2471 (st->declaration == DECL_NONE && isOneOf (c, ",;"))))
2473 token->type = TOKEN_NAME;
2474 processName (st);
2475 st->gotParenName = TRUE;
2476 if (! (c == '(' && info.nestedArgs))
2477 st->isPointer = info.isPointer;
2479 else if (! st->gotArgs && info.isParamList)
2481 st->gotArgs = TRUE;
2482 setToken (st, TOKEN_ARGS);
2483 advanceToken (st);
2484 if (st->scope != SCOPE_TYPEDEF)
2485 analyzePostParens (st, &info);
2487 else
2488 setToken (st, TOKEN_NONE);
2493 * Token parsing functions
2496 static void addContext (statementInfo *const st, const tokenInfo* const token)
2498 if (isType (token, TOKEN_NAME))
2500 if (vStringLength (st->context->name) > 0)
2502 if (isLanguage (Lang_c) || isLanguage (Lang_cpp))
2503 vStringCatS (st->context->name, "::");
2504 else if (isLanguage (Lang_java) || isLanguage (Lang_csharp))
2505 vStringCatS (st->context->name, ".");
2507 vStringCat (st->context->name, token->name);
2508 st->context->type = TOKEN_NAME;
2512 static boolean inheritingDeclaration (declType decl)
2514 /* C# supports inheritance for enums. C++0x will too, but not yet. */
2515 if (decl == DECL_ENUM)
2517 return (boolean) (isLanguage (Lang_csharp));
2519 return (boolean) (
2520 decl == DECL_CLASS ||
2521 decl == DECL_STRUCT ||
2522 decl == DECL_INTERFACE);
2525 static void processColon (statementInfo *const st)
2527 int c = (isLanguage (Lang_cpp) ? cppGetc () : skipToNonWhite ());
2528 const boolean doubleColon = (boolean) (c == ':');
2530 if (doubleColon)
2532 setToken (st, TOKEN_DOUBLE_COLON);
2533 st->haveQualifyingName = FALSE;
2535 else
2537 cppUngetc (c);
2538 if ((isLanguage (Lang_cpp) || isLanguage (Lang_csharp)) &&
2539 inheritingDeclaration (st->declaration))
2541 readParents (st, ':');
2543 else if (parentDecl (st) == DECL_STRUCT)
2545 c = skipToOneOf (",;");
2546 if (c == ',')
2547 setToken (st, TOKEN_COMMA);
2548 else if (c == ';')
2549 setToken (st, TOKEN_SEMICOLON);
2551 else if (isLanguage (Lang_cpp) && st->declaration == DECL_ENUM)
2553 /* skip enum's base type */
2554 c = skipToOneOf ("{;");
2555 if (c == '{')
2556 setToken (st, TOKEN_BRACE_OPEN);
2557 else if (c == ';')
2558 setToken (st, TOKEN_SEMICOLON);
2560 else
2562 const tokenInfo *const prev = prevToken (st, 1);
2563 const tokenInfo *const prev2 = prevToken (st, 2);
2564 if (prev->keyword == KEYWORD_DEFAULT ||
2565 prev2->keyword == KEYWORD_CASE ||
2566 st->parent != NULL)
2568 reinitStatement (st, FALSE);
2574 /* Skips over any initializing value which may follow an '=' character in a
2575 * variable definition.
2577 static int skipInitializer (statementInfo *const st)
2579 boolean done = FALSE;
2580 int c;
2582 while (! done)
2584 c = skipToNonWhite ();
2586 if (c == EOF)
2587 longjmp (Exception, (int) ExceptionFormattingError);
2588 else switch (c)
2590 case ',':
2591 case ';': done = TRUE; break;
2593 case '0':
2594 if (st->implementation == IMP_VIRTUAL)
2595 st->implementation = IMP_PURE_VIRTUAL;
2596 break;
2598 case '[': skipToMatch ("[]"); break;
2599 case '(': skipToMatch ("()"); break;
2600 case '{': skipToMatch ("{}"); break;
2601 case '<': processAngleBracket(); break;
2603 case '}':
2604 if (insideEnumBody (st))
2605 done = TRUE;
2606 else if (! isBraceFormat ())
2608 verbose ("%s: unexpected closing brace at line %lu\n",
2609 getInputFileName (), getInputLineNumber ());
2610 longjmp (Exception, (int) ExceptionBraceFormattingError);
2612 break;
2614 default: break;
2617 return c;
2620 static void processInitializer (statementInfo *const st)
2622 const boolean inEnumBody = insideEnumBody (st);
2623 int c = cppGetc ();
2625 if (c != '=')
2627 cppUngetc (c);
2628 c = skipInitializer (st);
2629 st->assignment = TRUE;
2630 if (c == ';')
2631 setToken (st, TOKEN_SEMICOLON);
2632 else if (c == ',')
2633 setToken (st, TOKEN_COMMA);
2634 else if (c == '}' && inEnumBody)
2636 cppUngetc (c);
2637 setToken (st, TOKEN_COMMA);
2639 if (st->scope == SCOPE_EXTERN)
2640 st->scope = SCOPE_GLOBAL;
2644 static void parseIdentifier (statementInfo *const st, const int c)
2646 tokenInfo *const token = activeToken (st);
2648 readIdentifier (token, c);
2649 if (! isType (token, TOKEN_NONE))
2650 processToken (token, st);
2653 static void parseGeneralToken (statementInfo *const st, const int c)
2655 const tokenInfo *const prev = prevToken (st, 1);
2657 if (isident1 (c) || (isLanguage (Lang_java) && isHighChar (c)))
2659 parseIdentifier (st, c);
2660 if (isType (st->context, TOKEN_NAME) &&
2661 isType (activeToken (st), TOKEN_NAME) && isType (prev, TOKEN_NAME))
2663 initToken (st->context);
2666 else if (c == '.' || c == '-')
2668 if (! st->assignment)
2669 st->notVariable = TRUE;
2670 if (c == '-')
2672 int c2 = cppGetc ();
2673 if (c2 != '>')
2674 cppUngetc (c2);
2677 else if (c == '!' || c == '>')
2679 int c2 = cppGetc ();
2680 if (c2 != '=')
2681 cppUngetc (c2);
2683 else if (c == '@' && isLanguage (Lang_java))
2685 parseJavaAnnotation (st);
2687 else if (isExternCDecl (st, c))
2689 st->declaration = DECL_NOMANGLE;
2690 st->scope = SCOPE_GLOBAL;
2694 /* Reads characters from the pre-processor and assembles tokens, setting
2695 * the current statement state.
2697 static void nextToken (statementInfo *const st)
2699 tokenInfo *token;
2702 int c = skipToNonWhite ();
2703 switch (c)
2705 case EOF: longjmp (Exception, (int) ExceptionEOF); break;
2706 /* analyze functions and co */
2707 case '(': analyzeParens (st); break;
2708 case '<': processAngleBracket (); break;
2709 case '*':
2710 st->haveQualifyingName = FALSE;
2711 setToken (st, TOKEN_STAR);
2712 break;
2713 case '&': setToken (st, TOKEN_AMPERSAND); break;
2715 case ',': setToken (st, TOKEN_COMMA); break;
2716 case ':': processColon (st); break;
2717 case ';': setToken (st, TOKEN_SEMICOLON); break;
2718 case '=': processInitializer (st); break;
2719 case '[': skipToMatch ("[]"); break;
2720 case '{': setToken (st, TOKEN_BRACE_OPEN); break;
2721 case '}': setToken (st, TOKEN_BRACE_CLOSE); break;
2722 default: parseGeneralToken (st, c); break;
2724 token = activeToken (st);
2725 } while (isType (token, TOKEN_NONE));
2729 * Scanning support functions
2732 static statementInfo *CurrentStatement = NULL;
2734 static statementInfo *newStatement (statementInfo *const parent)
2736 statementInfo *const st = xMalloc (1, statementInfo);
2737 unsigned int i;
2739 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
2740 st->token [i] = newToken ();
2742 st->context = newToken ();
2743 st->blockName = newToken ();
2744 st->parentClasses = vStringNew ();
2746 initStatement (st, parent);
2747 CurrentStatement = st;
2749 return st;
2752 static void deleteStatement (void)
2754 statementInfo *const st = CurrentStatement;
2755 statementInfo *const parent = st->parent;
2756 unsigned int i;
2758 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
2760 deleteToken (st->token [i]); st->token [i] = NULL;
2762 deleteToken (st->blockName); st->blockName = NULL;
2763 deleteToken (st->context); st->context = NULL;
2764 vStringDelete (st->parentClasses); st->parentClasses = NULL;
2765 eFree (st);
2766 CurrentStatement = parent;
2769 static void deleteAllStatements (void)
2771 while (CurrentStatement != NULL)
2772 deleteStatement ();
2775 static boolean isStatementEnd (const statementInfo *const st)
2777 const tokenInfo *const token = activeToken (st);
2778 boolean isEnd;
2780 if (isType (token, TOKEN_SEMICOLON))
2781 isEnd = TRUE;
2782 else if (isType (token, TOKEN_BRACE_CLOSE))
2783 /* Java and C# do not require semicolons to end a block. Neither do C++
2784 * namespaces. All other blocks require a semicolon to terminate them.
2786 isEnd = (boolean) (isLanguage (Lang_java) || isLanguage (Lang_csharp) ||
2787 ! isContextualStatement (st));
2788 else
2789 isEnd = FALSE;
2791 return isEnd;
2794 static void checkStatementEnd (statementInfo *const st)
2796 const tokenInfo *const token = activeToken (st);
2798 if (isType (token, TOKEN_COMMA))
2799 reinitStatement (st, TRUE);
2800 else if (isStatementEnd (st))
2802 DebugStatement ( if (debug (DEBUG_PARSE)) printf ("<ES>"); )
2803 reinitStatement (st, FALSE);
2804 cppEndStatement ();
2806 else
2808 cppBeginStatement ();
2809 advanceToken (st);
2813 static void nest (statementInfo *const st, const unsigned int nestLevel)
2815 switch (st->declaration)
2817 case DECL_CLASS:
2818 case DECL_ENUM:
2819 case DECL_INTERFACE:
2820 case DECL_NAMESPACE:
2821 case DECL_NOMANGLE:
2822 case DECL_STRUCT:
2823 case DECL_UNION:
2824 createTags (nestLevel, st);
2825 break;
2827 case DECL_FUNCTION:
2828 case DECL_TASK:
2829 st->inFunction = TRUE;
2831 /* fall through */
2832 default:
2833 if (includeTag (TAG_LOCAL, FALSE))
2834 createTags (nestLevel, st);
2835 else
2836 skipToMatch ("{}");
2837 break;
2839 advanceToken (st);
2840 setToken (st, TOKEN_BRACE_CLOSE);
2843 static void tagCheck (statementInfo *const st)
2845 const tokenInfo *const token = activeToken (st);
2846 const tokenInfo *const prev = prevToken (st, 1);
2847 const tokenInfo *const prev2 = prevToken (st, 2);
2849 switch (token->type)
2851 case TOKEN_NAME:
2852 if (insideEnumBody (st))
2853 qualifyEnumeratorTag (st, token);
2854 break;
2855 #if 0
2856 case TOKEN_PACKAGE:
2857 if (st->haveQualifyingName)
2858 makeTag (token, st, FALSE, TAG_PACKAGE);
2859 break;
2860 #endif
2861 case TOKEN_BRACE_OPEN:
2862 if (isType (prev, TOKEN_ARGS))
2864 if (st->haveQualifyingName)
2866 if (! isLanguage (Lang_vera))
2867 st->declaration = DECL_FUNCTION;
2868 if (isType (prev2, TOKEN_NAME))
2869 copyToken (st->blockName, prev2);
2870 qualifyFunctionTag (st, prev2);
2873 else if (isContextualStatement (st) ||
2874 st->declaration == DECL_NAMESPACE ||
2875 st->declaration == DECL_PROGRAM)
2877 tokenInfo *name_token = (tokenInfo *)prev;
2879 /* C++ 11 allows class <name> final { ... } */
2880 if (isLanguage (Lang_cpp) && isType (prev, TOKEN_NAME) &&
2881 strcmp("final", vStringValue(prev->name)) == 0 &&
2882 isType(prev2, TOKEN_NAME))
2884 name_token = (tokenInfo *)prev2;
2885 copyToken (st->blockName, name_token);
2887 else if (isType (name_token, TOKEN_NAME))
2889 copyToken (st->blockName, prev);
2891 else
2893 /* For an anonymous struct or union we use a unique ID
2894 * a number, so that the members can be found.
2896 char buf [20]; /* length of "_anon" + digits + null */
2897 sprintf (buf, "__anon%d", ++AnonymousID);
2898 vStringCopyS (st->blockName->name, buf);
2899 st->blockName->type = TOKEN_NAME;
2900 st->blockName->keyword = KEYWORD_NONE;
2902 qualifyBlockTag (st, prev);
2904 else if (isLanguage (Lang_csharp))
2905 makeTag (prev, st, FALSE, TAG_PROPERTY);
2906 break;
2908 case TOKEN_SEMICOLON:
2909 case TOKEN_COMMA:
2910 if (insideEnumBody (st))
2912 else if (isType (prev, TOKEN_NAME))
2914 if (isContextualKeyword (prev2))
2915 makeTag (prev, st, TRUE, TAG_EXTERN_VAR);
2916 else
2917 qualifyVariableTag (st, prev);
2919 else if (isType (prev, TOKEN_ARGS) && isType (prev2, TOKEN_NAME))
2921 if (st->isPointer)
2922 qualifyVariableTag (st, prev2);
2923 else
2924 qualifyFunctionDeclTag (st, prev2);
2926 if (isLanguage (Lang_java) && token->type == TOKEN_SEMICOLON && insideEnumBody (st))
2928 /* In Java, after an initial enum-like part,
2929 * a semicolon introduces a class-like part.
2930 * See Bug #1730485 for the full rationale. */
2931 st->parent->declaration = DECL_CLASS;
2933 break;
2935 default: break;
2939 /* Parses the current file and decides whether to write out and tags that
2940 * are discovered.
2942 static void createTags (const unsigned int nestLevel,
2943 statementInfo *const parent)
2945 statementInfo *const st = newStatement (parent);
2947 DebugStatement ( if (nestLevel > 0) debugParseNest (TRUE, nestLevel); )
2948 while (TRUE)
2950 tokenInfo *token;
2952 nextToken (st);
2953 token = activeToken (st);
2955 if (isType (token, TOKEN_BRACE_CLOSE))
2957 if (nestLevel > 0)
2958 break;
2959 else
2961 verbose ("%s: unexpected closing brace at line %lu\n",
2962 getInputFileName (), getInputLineNumber ());
2963 longjmp (Exception, (int) ExceptionBraceFormattingError);
2966 else if (isType (token, TOKEN_DOUBLE_COLON))
2968 addContext (st, prevToken (st, 1));
2969 advanceToken (st);
2971 else
2973 tagCheck (st);
2974 if (isType (token, TOKEN_BRACE_OPEN))
2975 nest (st, nestLevel + 1);
2976 checkStatementEnd (st);
2979 deleteStatement ();
2980 DebugStatement ( if (nestLevel > 0) debugParseNest (FALSE, nestLevel - 1); )
2983 static boolean findCTags (const unsigned int passCount)
2985 exception_t exception;
2986 boolean retry;
2988 Assert (passCount < 3);
2989 cppInit ((boolean) (passCount > 1), isLanguage (Lang_csharp));
2990 Signature = vStringNew ();
2991 ReturnType = vStringNew ();
2993 exception = (exception_t) setjmp (Exception);
2994 retry = FALSE;
2995 if (exception == ExceptionNone)
2996 createTags (0, NULL);
2997 else
2999 deleteAllStatements ();
3000 if (exception == ExceptionBraceFormattingError && passCount == 1)
3002 retry = TRUE;
3003 verbose ("%s: retrying file with fallback brace matching algorithm\n",
3004 getInputFileName ());
3007 vStringDelete (Signature);
3008 vStringDelete (ReturnType);
3009 cppTerminate ();
3010 return retry;
3013 static void buildKeywordHash (const langType language, unsigned int idx)
3015 const size_t count = sizeof (KeywordTable) / sizeof (KeywordTable [0]);
3016 size_t i;
3017 for (i = 0 ; i < count ; ++i)
3019 const keywordDesc* const p = &KeywordTable [i];
3020 if (p->isValid [idx])
3021 addKeyword (p->name, language, (int) p->id);
3025 static void initializeCParser (const langType language)
3027 Lang_c = language;
3028 buildKeywordHash (language, 0);
3031 static void initializeCppParser (const langType language)
3033 Lang_cpp = language;
3034 buildKeywordHash (language, 1);
3037 static void initializeCsharpParser (const langType language)
3039 Lang_csharp = language;
3040 buildKeywordHash (language, 2);
3043 static void initializeJavaParser (const langType language)
3045 Lang_java = language;
3046 buildKeywordHash (language, 3);
3049 static void initializeVeraParser (const langType language)
3051 Lang_vera = language;
3052 buildKeywordHash (language, 4);
3055 extern parserDefinition* CParser (void)
3057 static const char *const extensions [] = { "c", NULL };
3058 parserDefinition* def = parserNew ("C");
3059 def->kinds = CKinds;
3060 def->kindCount = KIND_COUNT (CKinds);
3061 def->extensions = extensions;
3062 def->parser2 = findCTags;
3063 def->initialize = initializeCParser;
3064 return def;
3067 extern parserDefinition* CppParser (void)
3069 static const char *const extensions [] = {
3070 "c++", "cc", "cp", "cpp", "cxx", "h", "h++", "hh", "hp", "hpp", "hxx",
3071 #ifndef CASE_INSENSITIVE_FILENAMES
3072 "C", "H",
3073 #endif
3074 NULL
3076 parserDefinition* def = parserNew ("C++");
3077 def->kinds = CKinds;
3078 def->kindCount = KIND_COUNT (CKinds);
3079 def->extensions = extensions;
3080 def->parser2 = findCTags;
3081 def->initialize = initializeCppParser;
3082 return def;
3085 extern parserDefinition* CsharpParser (void)
3087 static const char *const extensions [] = { "cs", NULL };
3088 parserDefinition* def = parserNew ("C#");
3089 def->kinds = CsharpKinds;
3090 def->kindCount = KIND_COUNT (CsharpKinds);
3091 def->extensions = extensions;
3092 def->parser2 = findCTags;
3093 def->initialize = initializeCsharpParser;
3094 return def;
3097 extern parserDefinition* JavaParser (void)
3099 static const char *const extensions [] = { "java", NULL };
3100 parserDefinition* def = parserNew ("Java");
3101 def->kinds = JavaKinds;
3102 def->kindCount = KIND_COUNT (JavaKinds);
3103 def->extensions = extensions;
3104 def->parser2 = findCTags;
3105 def->initialize = initializeJavaParser;
3106 return def;
3109 extern parserDefinition* VeraParser (void)
3111 static const char *const extensions [] = { "vr", "vri", "vrh", NULL };
3112 parserDefinition* def = parserNew ("Vera");
3113 def->kinds = VeraKinds;
3114 def->kindCount = KIND_COUNT (VeraKinds);
3115 def->extensions = extensions;
3116 def->parser2 = findCTags;
3117 def->initialize = initializeVeraParser;
3118 return def;
3121 /* vi:set tabstop=4 shiftwidth=4 noexpandtab: */