Sync whitespace in parsers
[geany-mirror.git] / ctags / parsers / c.c
blob696ba214700ff63c0f9272072cb5cbebdcf18a3a
1 /*
2 * Copyright (c) 1996-2003, Darren Hiebert
4 * This source code is released for free distribution under the terms of the
5 * GNU General Public License version 2 or (at your option) any later version.
7 * This module contains functions for parsing and scanning C, C++, C#, D and Java
8 * source files.
9 */
12 * INCLUDE FILES
14 #include "general.h" /* must always come first */
16 #include <string.h>
17 #include <setjmp.h>
19 #include "debug.h"
20 #include "mio.h"
21 #include "entry.h"
22 #include "lcpp.h"
23 #include "keyword.h"
24 #include "options.h"
25 #include "parse.h"
26 #include "read.h"
27 #include "routines.h"
28 #include "xtag.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) (boolean) ((st)->parent == NULL ? FALSE : \
39 ((st)->parent->declaration == DECL_ENUM))
40 #define isExternCDecl(st,c) (boolean) ((c) == STRING_SYMBOL && \
41 ! (st)->haveQualifyingName && \
42 (st)->scope == SCOPE_EXTERN)
44 #define isOneOf(c,s) (boolean) (strchr ((s), (c)) != NULL)
47 * DATA DECLARATIONS
50 enum { NumTokens = 12 };
52 typedef enum eException
54 ExceptionNone, ExceptionEOF, ExceptionFormattingError,
55 ExceptionBraceFormattingError
56 } exception_t;
58 /* Used to specify type of keyword.
60 typedef enum eKeywordId
62 KEYWORD_NONE = -1,
63 KEYWORD_ATTRIBUTE, KEYWORD_ABSTRACT, KEYWORD_ALIAS,
64 KEYWORD_BOOLEAN, KEYWORD_BYTE, KEYWORD_BAD_STATE, KEYWORD_BAD_TRANS,
65 KEYWORD_BIND, KEYWORD_BIND_VAR, KEYWORD_BIT, KEYWORD_BODY,
66 KEYWORD_CASE, KEYWORD_CATCH, KEYWORD_CHAR, KEYWORD_CLASS, KEYWORD_CONST,
67 KEYWORD_CONSTRAINT, KEYWORD_COVERAGE_BLOCK, KEYWORD_COVERAGE_DEF,
68 KEYWORD_DEFAULT, KEYWORD_DELEGATE, KEYWORD_DELETE, KEYWORD_DO,
69 KEYWORD_DOUBLE,
70 KEYWORD_ELSE, KEYWORD_ENUM, KEYWORD_EXPLICIT, KEYWORD_EXTERN,
71 KEYWORD_EXTENDS, KEYWORD_EVENT,
72 KEYWORD_FINAL, KEYWORD_FINALLY, KEYWORD_FLOAT, KEYWORD_FOR, KEYWORD_FRIEND, KEYWORD_FUNCTION,
73 KEYWORD_GET, KEYWORD_GOTO,
74 KEYWORD_IF, KEYWORD_IMPLEMENTS, KEYWORD_IMPORT, KEYWORD_IN, 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_MODULE, KEYWORD_MUTABLE,
80 KEYWORD_NAMESPACE, KEYWORD_NEW, KEYWORD_NEWCOV, KEYWORD_NATIVE, KEYWORD_NOEXCEPT,
81 KEYWORD_OPERATOR, KEYWORD_OUT, KEYWORD_OUTPUT, KEYWORD_OVERLOAD, KEYWORD_OVERRIDE,
82 KEYWORD_PACKED, KEYWORD_PORT, KEYWORD_PACKAGE, KEYWORD_PRIVATE,
83 KEYWORD_PROGRAM, KEYWORD_PROTECTED, KEYWORD_PUBLIC,
84 KEYWORD_REF, KEYWORD_REGISTER, KEYWORD_RETURN,
85 KEYWORD_SHADOW, KEYWORD_STATE,
86 KEYWORD_SET, KEYWORD_SHORT, KEYWORD_SIGNAL, KEYWORD_SIGNED, KEYWORD_SIZE_T, KEYWORD_STATIC,
87 KEYWORD_STATIC_ASSERT, KEYWORD_STRING,
88 KEYWORD_STRUCT, KEYWORD_SWITCH, KEYWORD_SYNCHRONIZED,
89 KEYWORD_TASK, KEYWORD_TEMPLATE, KEYWORD_THIS, KEYWORD_THROW,
90 KEYWORD_THROWS, KEYWORD_TRANSIENT, KEYWORD_TRANS, KEYWORD_TRANSITION,
91 KEYWORD_TRY, KEYWORD_TYPEDEF, KEYWORD_TYPENAME,
92 KEYWORD_UINT, KEYWORD_ULONG, KEYWORD_UNION, KEYWORD_UNSIGNED, KEYWORD_USHORT,
93 KEYWORD_USING,
94 KEYWORD_VIRTUAL, KEYWORD_VOID, KEYWORD_VOLATILE,
95 KEYWORD_WCHAR_T, KEYWORD_WEAK, KEYWORD_WHILE
96 } keywordId;
98 /* Used to determine whether keyword is valid for the current language and
99 * what its ID is.
101 typedef struct sKeywordDesc
103 const char *name;
104 keywordId id;
105 short isValid [7]; /* indicates languages for which kw is valid */
106 } keywordDesc;
108 /* Used for reporting the type of object parsed by nextToken ().
110 typedef enum eTokenType
112 TOKEN_NONE, /* none */
113 TOKEN_ARGS, /* a parenthetical pair and its contents */
114 TOKEN_BRACE_CLOSE,
115 TOKEN_BRACE_OPEN,
116 TOKEN_COMMA, /* the comma character */
117 TOKEN_DOUBLE_COLON, /* double colon indicates nested-name-specifier */
118 TOKEN_KEYWORD,
119 TOKEN_NAME, /* an unknown name */
120 TOKEN_PACKAGE, /* a Java package name */
121 TOKEN_PAREN_NAME, /* a single name in parentheses */
122 TOKEN_SEMICOLON, /* the semicolon character */
123 TOKEN_SPEC, /* a storage class specifier, qualifier, type, etc. */
124 TOKEN_STAR, /* pointer detection */
125 TOKEN_ARRAY, /* array detection */
126 TOKEN_COUNT
127 } tokenType;
129 /* This describes the scoping of the current statement.
131 typedef enum eTagScope
133 SCOPE_GLOBAL, /* no storage class specified */
134 SCOPE_STATIC, /* static storage class */
135 SCOPE_EXTERN, /* external storage class */
136 SCOPE_FRIEND, /* declares access only */
137 SCOPE_TYPEDEF, /* scoping depends upon context */
138 SCOPE_COUNT
139 } tagScope;
141 typedef enum eDeclaration
143 DECL_NONE,
144 DECL_BASE, /* base type (default) */
145 DECL_CLASS,
146 DECL_ENUM,
147 DECL_EVENT,
148 DECL_SIGNAL,
149 DECL_FUNCTION,
150 DECL_FUNCTION_TEMPLATE,
151 DECL_IGNORE, /* non-taggable "declaration" */
152 DECL_INTERFACE,
153 DECL_MODULE,
154 DECL_NAMESPACE,
155 DECL_NOMANGLE, /* C++ name demangling block */
156 DECL_PACKAGE,
157 DECL_STRUCT,
158 DECL_UNION,
159 DECL_COUNT
160 } declType;
162 typedef enum eVisibilityType
164 ACCESS_UNDEFINED,
165 ACCESS_PRIVATE,
166 ACCESS_PROTECTED,
167 ACCESS_PUBLIC,
168 ACCESS_DEFAULT, /* Java-specific */
169 ACCESS_COUNT
170 } accessType;
172 /* Information about the parent class of a member (if any).
174 typedef struct sMemberInfo
176 accessType access; /* access of current statement */
177 accessType accessDefault; /* access default for current statement */
178 } memberInfo;
180 typedef struct sTokenInfo
182 tokenType type;
183 keywordId keyword;
184 vString* name; /* the name of the token */
185 unsigned long lineNumber; /* line number of tag */
186 MIOPos filePosition; /* file position of line containing name */
187 } tokenInfo;
189 typedef enum eImplementation
191 IMP_DEFAULT,
192 IMP_ABSTRACT,
193 IMP_VIRTUAL,
194 IMP_PURE_VIRTUAL,
195 IMP_COUNT
196 } impType;
198 /* Describes the statement currently undergoing analysis.
200 typedef struct sStatementInfo
202 tagScope scope;
203 declType declaration; /* specifier associated with TOKEN_SPEC */
204 boolean gotName; /* was a name parsed yet? */
205 boolean haveQualifyingName; /* do we have a name we are considering? */
206 boolean gotParenName; /* was a name inside parentheses parsed yet? */
207 boolean gotArgs; /* was a list of parameters parsed yet? */
208 unsigned int nSemicolons; /* how many semicolons did we see in that statement */
209 impType implementation; /* abstract or concrete implementation? */
210 unsigned int tokenIndex; /* currently active token */
211 tokenInfo* token [((int) NumTokens)];
212 tokenInfo* context; /* accumulated scope of current statement */
213 tokenInfo* blockName; /* name of current block */
214 memberInfo member; /* information regarding parent class/struct */
215 vString* parentClasses; /* parent classes */
216 struct sStatementInfo *parent; /* statement we are nested within */
217 long argEndPosition; /* Position where argument list ended */
218 tokenInfo* firstToken; /* First token in the statement */
219 } statementInfo;
221 /* Describes the type of tag being generated.
223 typedef enum eTagType
225 TAG_UNDEFINED,
226 TAG_CLASS, /* class name */
227 TAG_ENUM, /* enumeration name */
228 TAG_ENUMERATOR, /* enumerator (enumeration value) */
229 TAG_FIELD, /* field (Java) */
230 TAG_FUNCTION, /* function definition */
231 TAG_INTERFACE, /* interface declaration */
232 TAG_MEMBER, /* structure, class or interface member */
233 TAG_METHOD, /* method declaration */
234 TAG_NAMESPACE, /* namespace name */
235 TAG_PACKAGE, /* package name / D module name */
236 TAG_PROTOTYPE, /* function prototype or declaration */
237 TAG_STRUCT, /* structure name */
238 TAG_TYPEDEF, /* typedef name */
239 TAG_UNION, /* union name */
240 TAG_VARIABLE, /* variable definition */
241 TAG_EXTERN_VAR, /* external variable declaration */
242 TAG_MACRO, /* #define s */
243 TAG_EVENT, /* event */
244 TAG_SIGNAL, /* signal */
245 TAG_LOCAL, /* local variable definition */
246 TAG_PROPERTY, /* property name */
247 TAG_COUNT /* must be last */
248 } tagType;
250 typedef struct sParenInfo
252 boolean isParamList;
253 boolean isKnrParamList;
254 boolean isNameCandidate;
255 boolean invalidContents;
256 boolean nestedArgs;
257 unsigned int parameterCount;
258 } parenInfo;
261 * DATA DEFINITIONS
264 static jmp_buf Exception;
266 static langType Lang_c;
267 static langType Lang_cpp;
268 static langType Lang_csharp;
269 static langType Lang_java;
270 static langType Lang_d;
271 static langType Lang_glsl;
272 static langType Lang_ferite;
273 static langType Lang_vala;
275 /* Used to index into the CKinds table. */
276 typedef enum
278 CK_UNDEFINED = -1,
279 CK_CLASS, CK_DEFINE, CK_ENUMERATOR, CK_FUNCTION,
280 CK_ENUMERATION, CK_MEMBER, CK_NAMESPACE, CK_PROTOTYPE,
281 CK_STRUCT, CK_TYPEDEF, CK_UNION, CK_VARIABLE,
282 CK_EXTERN_VARIABLE
283 } cKind;
285 static kindOption CKinds [] = {
286 { TRUE, 'c', "class", "classes"},
287 { TRUE, 'd', "macro", "macro definitions"},
288 { TRUE, 'e', "enumerator", "enumerators (values inside an enumeration)"},
289 { TRUE, 'f', "function", "function definitions"},
290 { TRUE, 'g', "enum", "enumeration names"},
291 { TRUE, 'm', "member", "class, struct, and union members"},
292 { TRUE, 'n', "namespace", "namespaces"},
293 { FALSE, 'p', "prototype", "function prototypes"},
294 { TRUE, 's', "struct", "structure names"},
295 { TRUE, 't', "typedef", "typedefs"},
296 { TRUE, 'u', "union", "union names"},
297 { TRUE, 'v', "variable", "variable definitions"},
298 { FALSE, 'x', "externvar", "external variable declarations"},
301 /* Used to index into the DKinds table. */
302 typedef enum
304 DK_UNDEFINED = -1,
305 DK_CLASS, DK_ENUMERATOR, DK_FUNCTION,
306 DK_ENUMERATION, DK_INTERFACE, DK_MEMBER, DK_NAMESPACE, DK_PROTOTYPE,
307 DK_STRUCT, DK_TYPEDEF, DK_UNION, DK_VARIABLE,
308 DK_EXTERN_VARIABLE
309 } dKind;
311 static kindOption DKinds [] = {
312 { TRUE, 'c', "class", "classes"},
313 { TRUE, 'e', "enumerator", "enumerators (values inside an enumeration)"},
314 { TRUE, 'f', "function", "function definitions"},
315 { TRUE, 'g', "enum", "enumeration names"},
316 { TRUE, 'i', "interface", "interfaces"},
317 { TRUE, 'm', "member", "class, struct, and union members"},
318 { TRUE, 'n', "namespace", "namespaces"},
319 { FALSE, 'p', "prototype", "function prototypes"},
320 { TRUE, 's', "struct", "structure names"},
321 { TRUE, 't', "typedef", "typedefs"},
322 { TRUE, 'u', "union", "union names"},
323 { TRUE, 'v', "variable", "variable definitions"},
324 { FALSE, 'x', "externvar", "external variable declarations"},
327 /* Used to index into the JavaKinds table. */
328 typedef enum
330 JK_UNDEFINED = -1,
331 JK_CLASS, JK_FIELD, JK_INTERFACE, JK_METHOD,
332 JK_PACKAGE, JK_ENUMERATOR, JK_ENUMERATION
333 } javaKind;
335 static kindOption JavaKinds [] = {
336 { TRUE, 'c', "class", "classes"},
337 { TRUE, 'f', "field", "fields"},
338 { TRUE, 'i', "interface", "interfaces"},
339 { TRUE, 'm', "method", "methods"},
340 { TRUE, 'p', "package", "packages"},
341 { TRUE, 'e', "enumConstant", "enum constants"},
342 { TRUE, 'g', "enum", "enum types"},
345 typedef enum
347 CSK_UNDEFINED = -1,
348 CSK_CLASS, CSK_DEFINE, CSK_ENUMERATOR, CSK_EVENT, CSK_FIELD,
349 CSK_ENUMERATION, CSK_INTERFACE, CSK_LOCAL, CSK_METHOD,
350 CSK_NAMESPACE, CSK_PROPERTY, CSK_STRUCT, CSK_TYPEDEF
351 } csharpKind;
353 static kindOption CsharpKinds [] = {
354 { TRUE, 'c', "class", "classes"},
355 { TRUE, 'd', "macro", "macro definitions"},
356 { TRUE, 'e', "enumerator", "enumerators (values inside an enumeration)"},
357 { TRUE, 'E', "event", "events"},
358 { TRUE, 'f', "field", "fields"},
359 { TRUE, 'g', "enum", "enumeration names"},
360 { TRUE, 'i', "interface", "interfaces"},
361 { FALSE, 'l', "local", "local variables"},
362 { TRUE, 'm', "method", "methods"},
363 { TRUE, 'n', "namespace", "namespaces"},
364 { TRUE, 'p', "property", "properties"},
365 { TRUE, 's', "struct", "structure names"},
366 { TRUE, 't', "typedef", "typedefs"},
369 typedef enum {
370 VK_UNDEFINED = -1,
371 VK_CLASS, VK_DEFINE, VK_ENUMERATOR, VK_FIELD,
372 VK_ENUMERATION, VK_INTERFACE, VK_LOCAL, VK_METHOD,
373 VK_NAMESPACE, VK_PROPERTY, VK_SIGNAL, VK_STRUCT
374 } valaKind;
376 static kindOption ValaKinds [] = {
377 { TRUE, 'c', "class", "classes"},
378 { TRUE, 'd', "macro", "macro definitions"},
379 { TRUE, 'e', "enumerator", "enumerators (values inside an enumeration)"},
380 { TRUE, 'f', "field", "fields"},
381 { TRUE, 'g', "enum", "enumeration names"},
382 { TRUE, 'i', "interface", "interfaces"},
383 { FALSE, 'l', "local", "local variables"},
384 { TRUE, 'm', "method", "methods"},
385 { TRUE, 'n', "namespace", "namespaces"},
386 { TRUE, 'p', "property", "properties"},
387 { TRUE, 'S', "signal", "signals"},
388 { TRUE, 's', "struct", "structure names"},
391 /* Note: some keyword aliases are added in initializeDParser, initializeValaParser */
392 static const keywordDesc KeywordTable [] = {
393 /* C++ */
394 /* ANSI C | C# Java */
395 /* | | | | Vera */
396 /* | | | | | Vala */
397 /* | | | | | | D */
398 /* keyword keyword ID | | | | | | | */
399 { "__attribute__", KEYWORD_ATTRIBUTE, { 1, 1, 1, 0, 0, 0, 1 } },
400 { "abstract", KEYWORD_ABSTRACT, { 0, 0, 1, 1, 0, 1, 1 } },
401 { "bad_state", KEYWORD_BAD_STATE, { 0, 0, 0, 0, 1, 0, 0 } },
402 { "bad_trans", KEYWORD_BAD_TRANS, { 0, 0, 0, 0, 1, 0, 0 } },
403 { "bind", KEYWORD_BIND, { 0, 0, 0, 0, 1, 0, 0 } },
404 { "bind_var", KEYWORD_BIND_VAR, { 0, 0, 0, 0, 1, 0, 0 } },
405 { "bit", KEYWORD_BIT, { 0, 0, 0, 0, 1, 0, 0 } },
406 { "body", KEYWORD_BODY, { 0, 0, 0, 0, 0, 0, 1 } },
407 { "boolean", KEYWORD_BOOLEAN, { 0, 0, 0, 1, 0, 0, 0 } },
408 { "byte", KEYWORD_BYTE, { 0, 0, 0, 1, 0, 0, 1 } },
409 { "case", KEYWORD_CASE, { 1, 1, 1, 1, 0, 1, 1 } },
410 { "catch", KEYWORD_CATCH, { 0, 1, 1, 0, 0, 1, 1 } },
411 { "char", KEYWORD_CHAR, { 1, 1, 1, 1, 0, 1, 1 } },
412 { "class", KEYWORD_CLASS, { 0, 1, 1, 1, 1, 1, 1 } },
413 { "const", KEYWORD_CONST, { 1, 1, 1, 1, 0, 1, 1 } },
414 { "constraint", KEYWORD_CONSTRAINT, { 0, 0, 0, 0, 1, 0, 0 } },
415 { "coverage_block", KEYWORD_COVERAGE_BLOCK, { 0, 0, 0, 0, 1, 0, 0 } },
416 { "coverage_def", KEYWORD_COVERAGE_DEF, { 0, 0, 0, 0, 1, 0, 0 } },
417 { "do", KEYWORD_DO, { 1, 1, 1, 1, 0, 1, 1 } },
418 { "default", KEYWORD_DEFAULT, { 1, 1, 1, 1, 0, 1, 1 } },
419 { "delegate", KEYWORD_DELEGATE, { 0, 0, 1, 0, 0, 1, 1 } },
420 { "delete", KEYWORD_DELETE, { 0, 1, 0, 0, 0, 1, 1 } },
421 { "double", KEYWORD_DOUBLE, { 1, 1, 1, 1, 0, 1, 1 } },
422 { "else", KEYWORD_ELSE, { 1, 1, 0, 1, 0, 1, 1 } },
423 { "enum", KEYWORD_ENUM, { 1, 1, 1, 1, 1, 1, 1 } },
424 { "event", KEYWORD_EVENT, { 0, 0, 1, 0, 1, 0, 0 } },
425 { "explicit", KEYWORD_EXPLICIT, { 0, 1, 1, 0, 0, 0, 1 } },
426 { "extends", KEYWORD_EXTENDS, { 0, 0, 0, 1, 1, 0, 0 } },
427 { "extern", KEYWORD_EXTERN, { 1, 1, 1, 0, 1, 1, 0 } },
428 { "extern", KEYWORD_NAMESPACE, { 0, 0, 0, 0, 0, 0, 1 } }, /* parse block */
429 { "final", KEYWORD_FINAL, { 0, 0, 0, 1, 0, 0, 1 } },
430 { "finally", KEYWORD_FINALLY, { 0, 0, 0, 0, 0, 1, 1 } },
431 { "float", KEYWORD_FLOAT, { 1, 1, 1, 1, 0, 1, 1 } },
432 { "for", KEYWORD_FOR, { 1, 1, 1, 1, 0, 1, 1 } },
433 { "friend", KEYWORD_FRIEND, { 0, 1, 0, 0, 0, 0, 0 } },
434 { "function", KEYWORD_FUNCTION, { 0, 0, 0, 0, 1, 0, 1 } },
435 { "get", KEYWORD_GET, { 0, 0, 0, 0, 0, 1, 0 } },
436 { "goto", KEYWORD_GOTO, { 1, 1, 1, 1, 0, 1, 1 } },
437 { "if", KEYWORD_IF, { 1, 1, 1, 1, 0, 1, 1 } },
438 { "implements", KEYWORD_IMPLEMENTS, { 0, 0, 0, 1, 0, 0, 0 } },
439 { "import", KEYWORD_IMPORT, { 0, 0, 0, 1, 0, 0, 1 } },
440 { "inline", KEYWORD_INLINE, { 0, 1, 0, 0, 0, 1, 0 } },
441 { "in", KEYWORD_IN, { 0, 0, 0, 0, 0, 0, 1 } },
442 { "inout", KEYWORD_INOUT, { 0, 0, 0, 0, 1, 0, 0 } },
443 { "inout", KEYWORD_CONST, { 0, 0, 0, 0, 0, 0, 1 } }, /* treat like const */
444 { "input", KEYWORD_INPUT, { 0, 0, 0, 0, 1, 0, 0 } },
445 { "int", KEYWORD_INT, { 1, 1, 1, 1, 0, 1, 1 } },
446 { "integer", KEYWORD_INTEGER, { 0, 0, 0, 0, 1, 0, 0 } },
447 { "interface", KEYWORD_INTERFACE, { 0, 0, 1, 1, 1, 1, 1 } },
448 { "internal", KEYWORD_INTERNAL, { 0, 0, 1, 0, 0, 0, 0 } },
449 { "local", KEYWORD_LOCAL, { 0, 0, 0, 0, 1, 0, 0 } },
450 { "long", KEYWORD_LONG, { 1, 1, 1, 1, 0, 1, 1 } },
451 { "m_bad_state", KEYWORD_M_BAD_STATE, { 0, 0, 0, 0, 1, 0, 0 } },
452 { "m_bad_trans", KEYWORD_M_BAD_TRANS, { 0, 0, 0, 0, 1, 0, 0 } },
453 { "m_state", KEYWORD_M_STATE, { 0, 0, 0, 0, 1, 0, 0 } },
454 { "m_trans", KEYWORD_M_TRANS, { 0, 0, 0, 0, 1, 0, 0 } },
455 { "mutable", KEYWORD_MUTABLE, { 0, 1, 0, 0, 0, 0, 0 } },
456 { "module", KEYWORD_MODULE, { 0, 0, 0, 0, 0, 0, 1 } },
457 { "namespace", KEYWORD_NAMESPACE, { 0, 1, 1, 0, 0, 1, 0 } },
458 { "native", KEYWORD_NATIVE, { 0, 0, 0, 1, 0, 0, 0 } },
459 { "new", KEYWORD_NEW, { 0, 1, 1, 1, 0, 1, 1 } },
460 { "newcov", KEYWORD_NEWCOV, { 0, 0, 0, 0, 1, 0, 0 } },
461 { "noexcept", KEYWORD_NOEXCEPT, { 0, 1, 0, 0, 0, 0, 0 } },
462 { "operator", KEYWORD_OPERATOR, { 0, 1, 1, 0, 0, 0, 0 } },
463 { "out", KEYWORD_OUT, { 0, 0, 0, 0, 0, 1, 1 } },
464 { "output", KEYWORD_OUTPUT, { 0, 0, 0, 0, 1, 0, 0 } },
465 { "overload", KEYWORD_OVERLOAD, { 0, 1, 0, 0, 0, 0, 0 } },
466 { "override", KEYWORD_OVERRIDE, { 0, 0, 1, 0, 0, 1, 1 } },
467 { "package", KEYWORD_PACKAGE, { 0, 0, 0, 1, 0, 0, 1 } },
468 { "packed", KEYWORD_PACKED, { 0, 0, 0, 0, 1, 0, 0 } },
469 { "port", KEYWORD_PORT, { 0, 0, 0, 0, 1, 0, 0 } },
470 { "private", KEYWORD_PRIVATE, { 0, 1, 1, 1, 0, 1, 1 } },
471 { "program", KEYWORD_PROGRAM, { 0, 0, 0, 0, 1, 0, 0 } },
472 { "protected", KEYWORD_PROTECTED, { 0, 1, 1, 1, 1, 1, 1 } },
473 { "public", KEYWORD_PUBLIC, { 0, 1, 1, 1, 1, 1, 1 } },
474 { "ref", KEYWORD_REF, { 0, 0, 0, 0, 0, 1, 1 } },
475 { "register", KEYWORD_REGISTER, { 1, 1, 0, 0, 0, 0, 0 } },
476 { "return", KEYWORD_RETURN, { 1, 1, 1, 1, 0, 1, 1 } },
477 { "set", KEYWORD_SET, { 0, 0, 0, 0, 0, 1, 0 } },
478 { "shadow", KEYWORD_SHADOW, { 0, 0, 0, 0, 1, 0, 0 } },
479 { "short", KEYWORD_SHORT, { 1, 1, 1, 1, 0, 1, 1 } },
480 { "signal", KEYWORD_SIGNAL, { 0, 0, 0, 0, 0, 1, 0 } },
481 { "signed", KEYWORD_SIGNED, { 1, 1, 0, 0, 0, 0, 0 } },
482 { "size_t", KEYWORD_SIZE_T, { 0, 0, 0, 0, 0, 1, 0 } },
483 { "state", KEYWORD_STATE, { 0, 0, 0, 0, 1, 0, 0 } },
484 { "static", KEYWORD_STATIC, { 1, 1, 1, 1, 1, 1, 1 } },
485 { "static_assert", KEYWORD_STATIC_ASSERT, { 0, 1, 0, 0, 0, 0, 0 } },
486 { "string", KEYWORD_STRING, { 0, 0, 1, 0, 1, 1, 0 } },
487 { "struct", KEYWORD_STRUCT, { 1, 1, 1, 0, 0, 1, 1 } },
488 { "switch", KEYWORD_SWITCH, { 1, 1, 1, 1, 0, 1, 1 } },
489 { "synchronized", KEYWORD_SYNCHRONIZED, { 0, 0, 0, 1, 0, 0, 1 } },
490 { "task", KEYWORD_TASK, { 0, 0, 0, 0, 1, 0, 0 } },
491 { "template", KEYWORD_TEMPLATE, { 0, 1, 0, 0, 0, 0, 0 } },
492 { "template", KEYWORD_NAMESPACE, { 0, 0, 0, 0, 0, 0, 1 } }, /* parse block */
493 { "this", KEYWORD_THIS, { 0, 0, 1, 1, 0, 1, 0 } }, /* 0 to allow D ctor tags */
494 { "throw", KEYWORD_THROW, { 0, 1, 1, 1, 0, 1, 1 } },
495 { "throws", KEYWORD_THROWS, { 0, 0, 0, 1, 0, 1, 0 } },
496 { "trans", KEYWORD_TRANS, { 0, 0, 0, 0, 1, 0, 0 } },
497 { "transition", KEYWORD_TRANSITION, { 0, 0, 0, 0, 1, 0, 0 } },
498 { "transient", KEYWORD_TRANSIENT, { 0, 0, 0, 1, 0, 0, 0 } },
499 { "try", KEYWORD_TRY, { 0, 1, 1, 0, 0, 1, 1 } },
500 { "typedef", KEYWORD_TYPEDEF, { 1, 1, 1, 0, 1, 0, 1 } },
501 { "typename", KEYWORD_TYPENAME, { 0, 1, 0, 0, 0, 0, 0 } },
502 { "uint", KEYWORD_UINT, { 0, 0, 1, 0, 0, 1, 1 } },
503 { "ulong", KEYWORD_ULONG, { 0, 0, 1, 0, 0, 1, 1 } },
504 { "union", KEYWORD_UNION, { 1, 1, 0, 0, 0, 0, 1 } },
505 { "unsigned", KEYWORD_UNSIGNED, { 1, 1, 1, 0, 0, 0, 1 } },
506 { "ushort", KEYWORD_USHORT, { 0, 0, 1, 0, 0, 1, 1 } },
507 { "using", KEYWORD_USING, { 0, 1, 1, 0, 0, 1, 0 } },
508 { "virtual", KEYWORD_VIRTUAL, { 0, 1, 1, 0, 1, 1, 0 } },
509 { "void", KEYWORD_VOID, { 1, 1, 1, 1, 1, 1, 1 } },
510 { "volatile", KEYWORD_VOLATILE, { 1, 1, 1, 1, 0, 0, 1 } },
511 { "wchar_t", KEYWORD_WCHAR_T, { 0, 1, 1, 0, 0, 0, 0 } },
512 { "weak", KEYWORD_WEAK, { 0, 0, 0, 0, 0, 1, 0 } },
513 { "while", KEYWORD_WHILE, { 1, 1, 1, 1, 0, 1, 1 } }
518 * FUNCTION PROTOTYPES
520 static void createTags (const unsigned int nestLevel, statementInfo *const parent);
521 static void copyToken (tokenInfo *const dest, const tokenInfo *const src);
522 static const char *getVarType (const statementInfo *const st,
523 const tokenInfo *const token);
526 * FUNCTION DEFINITIONS
529 /* Debugging functions added by Biswa */
530 #if defined(DEBUG_C) && DEBUG_C
531 static char *tokenTypeName[] = {
532 "none", "args", "'}'", "'{'", "','", "'::'", "keyword", "name",
533 "package", "paren-name", "';'", "spec", "*", "[]", "count"
536 static char *tagScopeNames[] = {
537 "global", "static", "extern", "friend", "typedef", "count"};
539 static char *declTypeNames[] = {
540 "none", "base", "class", "enum", "function", "ignore", "interface",
541 "namespace", "nomangle", "package", "struct", "union", "count"};
543 static char *impTypeNames[] = {
544 "default", "abstract", "virtual", "pure-virtual", "count"};
546 void printToken(const tokenInfo *const token)
548 fprintf(stderr, "Type: %s, Keyword: %d, name: %s\n", tokenTypeName[token->type],
549 token->keyword, vStringValue(token->name));
552 void printTagEntry(const tagEntryInfo *tag)
554 fprintf(stderr, "Tag: %s (%s) [ impl: %s, scope: %s, type: %s\n", tag->name,
555 tag->kindName, tag->extensionFields.implementation, tag->extensionFields.scope[1],
556 tag->extensionFields.varType);
559 void printStatement(const statementInfo *const statement)
561 int i;
562 statementInfo *st = (statementInfo *) statement;
563 while (NULL != st)
565 fprintf(stderr, "Statement Info:\n------------------------\n");
566 fprintf(stderr, "scope: %s, decl: %s, impl: %s\n", tagScopeNames[st->scope],
567 declTypeNames[st->declaration], impTypeNames[st->implementation]);
568 for (i=0; i < NumTokens; ++i)
570 fprintf(stderr, "Token %d %s: ", i, (i == st->tokenIndex)?"(current)":"");
571 printToken(st->token[i]);
573 fprintf(stderr, "Context: ");
574 printToken(st->context);
575 fprintf(stderr, "Block: ");
576 printToken(st->blockName);
577 fprintf(stderr, "Parent classes: %s\n", vStringValue(st->parentClasses));
578 fprintf(stderr, "First token: ");
579 printToken(st->firstToken);
580 if (NULL != st->parent)
581 fprintf(stderr, "Printing Parent:\n");
582 st = st->parent;
584 fprintf(stderr, "-----------------------------------------------\n");
586 #endif
588 extern boolean includingDefineTags (void)
590 if (isLanguage(Lang_c) ||
591 isLanguage(Lang_cpp) ||
592 isLanguage(Lang_csharp) ||
593 isLanguage(Lang_ferite) ||
594 isLanguage(Lang_glsl) ||
595 isLanguage(Lang_vala))
596 return CKinds [CK_DEFINE].enabled;
598 return FALSE;
602 * Token management
605 static void initToken (tokenInfo* const token)
607 token->type = TOKEN_NONE;
608 token->keyword = KEYWORD_NONE;
609 token->lineNumber = getInputLineNumber ();
610 token->filePosition = getInputFilePosition ();
611 vStringClear (token->name);
614 static void advanceToken (statementInfo* const st)
616 if (st->tokenIndex >= (unsigned int) NumTokens - 1)
617 st->tokenIndex = 0;
618 else
619 ++st->tokenIndex;
620 initToken (st->token [st->tokenIndex]);
623 static tokenInfo *prevToken (const statementInfo *const st, unsigned int n)
625 unsigned int tokenIndex;
626 unsigned int num = (unsigned int) NumTokens;
627 Assert (n < num);
628 tokenIndex = (st->tokenIndex + num - n) % num;
629 return st->token [tokenIndex];
632 static void setToken (statementInfo *const st, const tokenType type)
634 tokenInfo *token;
635 token = activeToken (st);
636 initToken (token);
637 token->type = type;
640 static void retardToken (statementInfo *const st)
642 if (st->tokenIndex == 0)
643 st->tokenIndex = (unsigned int) NumTokens - 1;
644 else
645 --st->tokenIndex;
646 setToken (st, TOKEN_NONE);
649 static tokenInfo *newToken (void)
651 tokenInfo *const token = xMalloc (1, tokenInfo);
652 token->name = vStringNew ();
653 initToken (token);
654 return token;
657 static void deleteToken (tokenInfo *const token)
659 if (token != NULL)
661 vStringDelete (token->name);
662 eFree (token);
666 static const char *accessString (const accessType laccess)
668 static const char *const names [] = {
669 "?", "private", "protected", "public", "default"
671 Assert (sizeof (names) / sizeof (names [0]) == ACCESS_COUNT);
672 Assert ((int) laccess < ACCESS_COUNT);
673 return names[(int) laccess];
676 static const char *implementationString (const impType imp)
678 static const char *const names [] = {
679 "?", "abstract", "virtual", "pure virtual"
681 Assert (sizeof (names) / sizeof (names [0]) == IMP_COUNT);
682 Assert ((int) imp < IMP_COUNT);
683 return names [(int) imp];
687 * Debugging functions
690 #ifdef TM_DEBUG
692 #define boolString(c) ((c) ? "TRUE" : "FALSE")
694 static const char *tokenString (const tokenType type)
696 static const char *const names [] = {
697 "none", "args", "}", "{", "comma", "double colon", "keyword", "name",
698 "package", "paren-name", "semicolon", "specifier", "*", "[]"
700 Assert (sizeof (names) / sizeof (names [0]) == TOKEN_COUNT);
701 Assert ((int) type < TOKEN_COUNT);
702 return names [(int) type];
705 static const char *scopeString (const tagScope scope)
707 static const char *const names [] = {
708 "global", "static", "extern", "friend", "typedef"
710 Assert (sizeof (names) / sizeof (names [0]) == SCOPE_COUNT);
711 Assert ((int) scope < SCOPE_COUNT);
712 return names [(int) scope];
715 static const char *declString (const declType declaration)
717 static const char *const names [] = {
718 "?", "base", "class", "enum", "event", "signal", "function",
719 "function template", "ignore", "interface", "module", "namespace",
720 "no mangle", "package", "struct", "union",
722 Assert (sizeof (names) / sizeof (names [0]) == DECL_COUNT);
723 Assert ((int) declaration < DECL_COUNT);
724 return names [(int) declaration];
727 static const char *keywordString (const keywordId keyword)
729 const size_t count = sizeof (KeywordTable) / sizeof (KeywordTable [0]);
730 const char *name = "none";
731 size_t i;
732 for (i = 0 ; i < count ; ++i)
734 const keywordDesc *p = &KeywordTable [i];
735 if (p->id == keyword)
737 name = p->name;
738 break;
741 return name;
744 static void UNUSED pt (tokenInfo *const token)
746 if (isType (token, TOKEN_NAME))
747 printf ("type: %-12s: %-13s line: %lu\n",
748 tokenString (token->type), vStringValue (token->name),
749 token->lineNumber);
750 else if (isType (token, TOKEN_KEYWORD))
751 printf ("type: %-12s: %-13s line: %lu\n",
752 tokenString (token->type), keywordString (token->keyword),
753 token->lineNumber);
754 else
755 printf ("type: %-12s line: %lu\n",
756 tokenString (token->type), token->lineNumber);
759 static void UNUSED ps (statementInfo *const st)
761 unsigned int i;
762 printf("scope: %s decl: %s gotName: %s gotParenName: %s\n",
763 scopeString (st->scope), declString (st->declaration),
764 boolString (st->gotName), boolString (st->gotParenName));
765 printf("haveQualifyingName: %s\n", boolString (st->haveQualifyingName));
766 printf("access: %s default: %s\n", accessString (st->member.access),
767 accessString (st->member.accessDefault));
768 printf("token : ");
769 pt(activeToken (st));
770 for (i = 1 ; i < (unsigned int) NumTokens ; ++i)
772 printf("prev %u : ", i);
773 pt(prevToken (st, i));
775 printf("context: ");
776 pt(st->context);
779 #endif
782 * Statement management
785 static boolean isDataTypeKeyword (const tokenInfo *const token)
787 switch (token->keyword)
789 case KEYWORD_BOOLEAN:
790 case KEYWORD_BYTE:
791 case KEYWORD_CHAR:
792 case KEYWORD_DOUBLE:
793 case KEYWORD_FLOAT:
794 case KEYWORD_INT:
795 case KEYWORD_LONG:
796 case KEYWORD_SHORT:
797 case KEYWORD_VOID:
798 case KEYWORD_WCHAR_T:
799 case KEYWORD_SIZE_T:
800 return TRUE;
801 default:
802 return FALSE;
806 #if 0
807 static boolean isVariableKeyword (const tokenInfo *const token)
809 switch (token->keyword)
811 case KEYWORD_CONST:
812 case KEYWORD_EXTERN:
813 case KEYWORD_REGISTER:
814 case KEYWORD_STATIC:
815 case KEYWORD_VIRTUAL:
816 case KEYWORD_SIGNED:
817 case KEYWORD_UNSIGNED:
818 return TRUE;
819 default:
820 return FALSE;
823 #endif
825 static boolean isContextualKeyword (const tokenInfo *const token)
827 boolean result;
828 switch (token->keyword)
830 case KEYWORD_CLASS:
831 case KEYWORD_ENUM:
832 case KEYWORD_INTERFACE:
833 case KEYWORD_NAMESPACE:
834 case KEYWORD_STRUCT:
835 case KEYWORD_UNION:
837 result = TRUE;
838 break;
841 default:
843 result = FALSE;
844 break;
847 return result;
850 static boolean isContextualStatement (const statementInfo *const st)
852 boolean result = FALSE;
854 if (st != NULL)
856 if (isLanguage (Lang_vala))
858 /* All can be a contextual statement as properties can be of any type */
859 result = TRUE;
861 else
863 switch (st->declaration)
865 case DECL_CLASS:
866 case DECL_ENUM:
867 case DECL_INTERFACE:
868 case DECL_NAMESPACE:
869 case DECL_STRUCT:
870 case DECL_UNION:
872 result = TRUE;
873 break;
876 default:
878 result = FALSE;
879 break;
884 return result;
887 static boolean isMember (const statementInfo *const st)
889 boolean result;
890 if (isType (st->context, TOKEN_NAME))
891 result = TRUE;
892 else
893 result = isContextualStatement (st->parent);
894 return result;
897 static void initMemberInfo (statementInfo *const st)
899 accessType accessDefault = ACCESS_UNDEFINED;
901 if (st->parent != NULL) switch (st->parent->declaration)
903 case DECL_ENUM:
904 case DECL_NAMESPACE:
906 accessDefault = ACCESS_UNDEFINED;
907 break;
909 case DECL_CLASS:
911 if (isLanguage (Lang_java))
912 accessDefault = ACCESS_DEFAULT;
913 else
914 accessDefault = ACCESS_PRIVATE;
915 break;
917 case DECL_INTERFACE:
918 case DECL_STRUCT:
919 case DECL_UNION:
921 accessDefault = ACCESS_PUBLIC;
922 break;
924 default:
925 break;
927 st->member.accessDefault = accessDefault;
928 st->member.access = accessDefault;
931 static void reinitStatement (statementInfo *const st, const boolean partial)
933 unsigned int i;
935 if (! partial)
937 st->scope = SCOPE_GLOBAL;
938 if (isContextualStatement (st->parent))
939 st->declaration = DECL_BASE;
940 else
941 st->declaration = DECL_NONE;
943 st->gotParenName = FALSE;
944 st->implementation = IMP_DEFAULT;
945 st->gotArgs = FALSE;
946 st->gotName = FALSE;
947 st->nSemicolons = 0;
948 st->haveQualifyingName = FALSE;
949 st->argEndPosition = 0;
951 st->tokenIndex = 0;
952 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
954 initToken (st->token [i]);
957 initToken (st->context);
958 initToken (st->blockName);
959 vStringClear (st->parentClasses);
961 /* Init member info. */
962 if (! partial)
963 st->member.access = st->member.accessDefault;
965 /* Init first token */
966 if (!partial)
967 initToken(st->firstToken);
970 static void reinitStatementWithToken (statementInfo *const st,
971 tokenInfo *token, const boolean partial)
973 tokenInfo *const save = newToken ();
974 /* given token can be part of reinit statementInfo */
975 copyToken (save, token);
976 reinitStatement (st, partial);
977 token = activeToken (st);
978 copyToken (token, save);
979 deleteToken (save);
980 ++st->tokenIndex; /* this is quite safe because current tokenIndex = 0 */
983 static void initStatement (statementInfo *const st, statementInfo *const parent)
985 st->parent = parent;
986 initMemberInfo (st);
987 reinitStatement (st, FALSE);
988 if (parent)
990 const tokenInfo *const src = activeToken (parent);
991 tokenInfo *const dst = activeToken (st);
992 copyToken (dst, src);
993 st->tokenIndex++;
998 * Tag generation functions
1000 static cKind cTagKind (const tagType type)
1002 cKind result = CK_UNDEFINED;
1003 switch (type)
1005 case TAG_CLASS: result = CK_CLASS; break;
1006 case TAG_ENUM: result = CK_ENUMERATION; break;
1007 case TAG_ENUMERATOR: result = CK_ENUMERATOR; break;
1008 case TAG_FUNCTION: result = CK_FUNCTION; break;
1009 case TAG_MEMBER: result = CK_MEMBER; break;
1010 case TAG_NAMESPACE: result = CK_NAMESPACE; break;
1011 case TAG_PROTOTYPE: result = CK_PROTOTYPE; break;
1012 case TAG_STRUCT: result = CK_STRUCT; break;
1013 case TAG_TYPEDEF: result = CK_TYPEDEF; break;
1014 case TAG_UNION: result = CK_UNION; break;
1015 case TAG_VARIABLE: result = CK_VARIABLE; break;
1016 case TAG_EXTERN_VAR: result = CK_EXTERN_VARIABLE; break;
1018 default: Assert ("Bad C tag type" == NULL); break;
1020 return result;
1023 static csharpKind csharpTagKind (const tagType type)
1025 csharpKind result = CSK_UNDEFINED;
1026 switch (type)
1028 case TAG_CLASS: result = CSK_CLASS; break;
1029 case TAG_ENUM: result = CSK_ENUMERATION; break;
1030 case TAG_ENUMERATOR: result = CSK_ENUMERATOR; break;
1031 case TAG_EVENT: result = CSK_EVENT; break;
1032 case TAG_FIELD: result = CSK_FIELD ; break;
1033 case TAG_INTERFACE: result = CSK_INTERFACE; break;
1034 case TAG_LOCAL: result = CSK_LOCAL; break;
1035 case TAG_METHOD: result = CSK_METHOD; break;
1036 case TAG_NAMESPACE: result = CSK_NAMESPACE; break;
1037 case TAG_PROPERTY: result = CSK_PROPERTY; break;
1038 case TAG_STRUCT: result = CSK_STRUCT; break;
1039 case TAG_TYPEDEF: result = CSK_TYPEDEF; break;
1041 default: Assert ("Bad C# tag type" == NULL); break;
1043 return result;
1046 static dKind dTagKind (const tagType type)
1048 dKind result = DK_UNDEFINED;
1049 switch (type)
1051 case TAG_CLASS: result = DK_CLASS; break;
1052 case TAG_ENUM: result = DK_ENUMERATION; break;
1053 case TAG_ENUMERATOR: result = DK_ENUMERATOR; break;
1054 case TAG_FUNCTION: result = DK_FUNCTION; break;
1055 case TAG_INTERFACE: result = DK_INTERFACE; break;
1056 case TAG_MEMBER: result = DK_MEMBER; break;
1057 case TAG_NAMESPACE: result = DK_NAMESPACE; break;
1058 case TAG_PROTOTYPE: result = DK_PROTOTYPE; break;
1059 case TAG_STRUCT: result = DK_STRUCT; break;
1060 case TAG_TYPEDEF: result = DK_TYPEDEF; break;
1061 case TAG_UNION: result = DK_UNION; break;
1062 case TAG_VARIABLE: result = DK_VARIABLE; break;
1063 case TAG_EXTERN_VAR: result = DK_EXTERN_VARIABLE; break;
1065 default: Assert ("Bad D tag type" == NULL); break;
1067 return result;
1070 static valaKind valaTagKind (const tagType type)
1072 valaKind result = VK_UNDEFINED;
1073 switch (type)
1075 case TAG_CLASS: result = VK_CLASS; break;
1076 case TAG_ENUM: result = VK_ENUMERATION; break;
1077 case TAG_ENUMERATOR: result = VK_ENUMERATOR; break;
1078 case TAG_SIGNAL: result = VK_SIGNAL; break;
1079 case TAG_FIELD: result = VK_FIELD ; break;
1080 case TAG_INTERFACE: result = VK_INTERFACE; break;
1081 case TAG_LOCAL: result = VK_LOCAL; break;
1082 case TAG_METHOD: result = VK_METHOD; break;
1083 case TAG_NAMESPACE: result = VK_NAMESPACE; break;
1084 case TAG_PROPERTY: result = VK_PROPERTY; break;
1085 case TAG_STRUCT: result = VK_STRUCT; break;
1087 default: Assert ("Bad Vala tag type" == NULL); break;
1089 return result;
1092 static javaKind javaTagKind (const tagType type)
1094 javaKind result = JK_UNDEFINED;
1095 switch (type)
1097 case TAG_CLASS: result = JK_CLASS; break;
1098 case TAG_FIELD: result = JK_FIELD; break;
1099 case TAG_INTERFACE: result = JK_INTERFACE; break;
1100 case TAG_METHOD: result = JK_METHOD; break;
1101 case TAG_PACKAGE: result = JK_PACKAGE; break;
1102 case TAG_ENUM: result = JK_ENUMERATION; break;
1103 case TAG_ENUMERATOR: result = JK_ENUMERATOR; break;
1105 default: Assert ("Bad Java tag type" == NULL); break;
1107 return result;
1110 static const kindOption *tagKind (const tagType type)
1112 const kindOption* result;
1113 if (isLanguage (Lang_java))
1114 result = &JavaKinds [javaTagKind (type)];
1115 else if (isLanguage (Lang_csharp))
1116 result = &CsharpKinds [csharpTagKind (type)];
1117 else if (isLanguage (Lang_d))
1118 result = &DKinds [dTagKind (type)];
1119 else if (isLanguage (Lang_vala))
1120 result = &ValaKinds [valaTagKind (type)];
1121 else
1122 result = &CKinds [cTagKind (type)];
1123 return result;
1127 static boolean includeTag (const tagType type, const boolean isFileScope)
1129 boolean result;
1130 if (isFileScope && ! Option.include.fileScope)
1131 result = FALSE;
1132 else if (isLanguage (Lang_java))
1133 result = JavaKinds [javaTagKind (type)].enabled;
1134 else
1135 result = CKinds [cTagKind (type)].enabled;
1136 return result;
1140 static tagType declToTagType (const declType declaration)
1142 tagType type = TAG_UNDEFINED;
1144 switch (declaration)
1146 case DECL_CLASS: type = TAG_CLASS; break;
1147 case DECL_ENUM: type = TAG_ENUM; break;
1148 case DECL_FUNCTION: type = TAG_FUNCTION; break;
1149 case DECL_FUNCTION_TEMPLATE: type = TAG_FUNCTION; break;
1150 case DECL_INTERFACE: type = TAG_INTERFACE; break;
1151 case DECL_NAMESPACE: type = TAG_NAMESPACE; break;
1152 case DECL_STRUCT: type = TAG_STRUCT; break;
1153 case DECL_UNION: type = TAG_UNION; break;
1155 default: Assert ("Unexpected declaration" == NULL); break;
1157 return type;
1160 static const char* accessField (const statementInfo *const st)
1162 const char* result = NULL;
1164 if ((isLanguage (Lang_cpp) || isLanguage (Lang_d) || isLanguage (Lang_ferite)) &&
1165 st->scope == SCOPE_FRIEND)
1166 result = "friend";
1167 else if (st->member.access != ACCESS_UNDEFINED)
1168 result = accessString (st->member.access);
1169 return result;
1172 static void addOtherFields (tagEntryInfo* const tag, const tagType type,
1173 const tokenInfo *const nameToken,
1174 const statementInfo *const st, vString *const scope)
1176 /* For selected tag types, append an extension flag designating the
1177 * parent object in which the tag is defined.
1179 switch (type)
1181 default: break;
1183 case TAG_NAMESPACE:
1184 case TAG_CLASS:
1185 case TAG_ENUM:
1186 case TAG_ENUMERATOR:
1187 case TAG_FIELD:
1188 case TAG_FUNCTION:
1189 case TAG_INTERFACE:
1190 case TAG_MEMBER:
1191 case TAG_METHOD:
1192 case TAG_PROTOTYPE:
1193 case TAG_STRUCT:
1194 case TAG_TYPEDEF:
1195 case TAG_UNION:
1197 if (vStringLength (scope) > 0 &&
1198 (isMember (st) || st->parent->declaration == DECL_NAMESPACE))
1200 if (isType (st->context, TOKEN_NAME))
1201 tag->extensionFields.scopeKind = tagKind (TAG_CLASS);
1202 else
1203 tag->extensionFields.scopeKind =
1204 tagKind (declToTagType (parentDecl (st)));
1205 tag->extensionFields.scopeName = vStringValue (scope);
1207 if ((type == TAG_CLASS || type == TAG_INTERFACE ||
1208 type == TAG_STRUCT) && vStringLength (st->parentClasses) > 0)
1210 tag->extensionFields.inheritance =
1211 vStringValue (st->parentClasses);
1213 if (st->implementation != IMP_DEFAULT &&
1214 (isLanguage (Lang_cpp) || isLanguage (Lang_csharp) || isLanguage (Lang_vala) ||
1215 isLanguage (Lang_java) || isLanguage (Lang_d) || isLanguage (Lang_ferite)))
1217 tag->extensionFields.implementation =
1218 implementationString (st->implementation);
1220 if (isMember (st))
1222 tag->extensionFields.access = accessField (st);
1224 if ((TRUE == st->gotArgs) && (TRUE == Option.extensionFields.argList) &&
1225 ((TAG_FUNCTION == type) || (TAG_METHOD == type) || (TAG_PROTOTYPE == type)))
1227 tag->extensionFields.signature = getArglistFromFilePos(
1228 tag->filePosition, tag->name);
1230 break;
1234 if ((TAG_FIELD == type) || (TAG_MEMBER == type) ||
1235 (TAG_EXTERN_VAR == type) || (TAG_TYPEDEF == type) ||
1236 (TAG_VARIABLE == type) || (TAG_METHOD == type) ||
1237 (TAG_PROTOTYPE == type) || (TAG_FUNCTION == type))
1239 if (((TOKEN_NAME == st->firstToken->type) || isDataTypeKeyword(st->firstToken))
1240 && (0 != strcmp(vStringValue(st->firstToken->name), tag->name)))
1242 tag->extensionFields.varType = getVarType(st, nameToken);
1247 static const char *getVarType (const statementInfo *const st,
1248 const tokenInfo *const nameToken)
1250 static vString *vt = NULL;
1251 unsigned int i;
1252 unsigned int end = st->tokenIndex;
1253 boolean seenType = FALSE;
1255 switch (st->declaration) {
1256 case DECL_BASE:
1257 case DECL_FUNCTION:
1258 case DECL_FUNCTION_TEMPLATE:
1259 break;
1260 default:
1261 return vStringValue(st->firstToken->name);
1264 if (vt == NULL)
1265 vt = vStringNew();
1266 else
1267 vStringClear(vt);
1269 /* find the end of the type signature in the token list */
1270 for (i = 0; i < st->tokenIndex; i++)
1272 const tokenInfo *const t = st->token[i];
1274 /* stop if we find the token used to generate the tag name, or
1275 * a name token in the middle yet not preceded by a scope separator */
1276 if ((t == nameToken ||
1277 (t->type == nameToken->type &&
1278 t->keyword == nameToken->keyword &&
1279 t->lineNumber == nameToken->lineNumber &&
1280 strcmp(vStringValue(t->name), vStringValue(nameToken->name)) == 0)) ||
1281 (t->type == TOKEN_NAME && seenType &&
1282 (i > 0 && st->token[i - 1]->type != TOKEN_DOUBLE_COLON)))
1284 break;
1286 if (t->type != TOKEN_DOUBLE_COLON)
1287 end = i + 1;
1288 if (t->type == TOKEN_NAME)
1289 seenType = TRUE;
1290 else if (t->type == TOKEN_KEYWORD && isDataTypeKeyword(t))
1291 seenType = TRUE;
1294 /* ugly historic workaround when we can't figure out the type */
1295 if (end < 2 && ! st->gotArgs)
1296 return vStringValue(st->firstToken->name);
1298 for (i = 0; i < end; i++)
1300 tokenInfo *t = st->token[i];
1302 switch (t->type)
1304 case TOKEN_NAME: /* user typename */
1305 break;
1306 case TOKEN_KEYWORD:
1307 if ((t->keyword != KEYWORD_EXTERN && t->keyword != KEYWORD_STATIC) && /* uninteresting keywords */
1308 (st->gotArgs ||
1309 /* ignore uninteresting keywords for non-functions */
1310 (t->keyword != KEYWORD_PUBLIC &&
1311 t->keyword != KEYWORD_PRIVATE &&
1312 t->keyword != KEYWORD_PROTECTED &&
1313 t->keyword != KEYWORD_FINAL &&
1314 t->keyword != KEYWORD_TYPEDEF &&
1315 /* hack for D static conditions */
1316 t->keyword != KEYWORD_IF)))
1318 break;
1320 continue;
1321 case TOKEN_STAR: vStringCatS(vt, " *"); continue;
1322 case TOKEN_ARRAY: vStringCatS(vt, "[]"); continue;
1323 case TOKEN_DOUBLE_COLON:
1324 vStringCatS(vt, "::");
1325 continue;
1326 default: continue;
1328 if (vStringLength(vt) > 0)
1329 if (isalpha(vStringValue(vt)[vStringLength(vt) - 1]))
1330 vStringPut(vt, ' ');
1331 vStringCat(vt, t->name);
1333 vStringTerminate(vt);
1334 return vStringValue(vt);
1337 static void addContextSeparator (vString *const scope)
1339 if (isLanguage (Lang_c) || isLanguage (Lang_cpp))
1340 vStringCatS (scope, "::");
1341 else if (isLanguage (Lang_java) || isLanguage (Lang_d) || isLanguage (Lang_ferite) ||
1342 isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1343 vStringCatS (scope, ".");
1346 static void findScopeHierarchy (vString *const string,
1347 const statementInfo *const st)
1349 const char* const anon = "<anonymous>";
1350 boolean nonAnonPresent = FALSE;
1352 vStringClear (string);
1353 if (isType (st->context, TOKEN_NAME))
1355 vStringCopy (string, st->context->name);
1356 nonAnonPresent = TRUE;
1358 if (st->parent != NULL)
1360 vString *temp = vStringNew ();
1361 const statementInfo *s;
1363 for (s = st->parent ; s != NULL ; s = s->parent)
1365 if (isContextualStatement (s) ||
1366 s->declaration == DECL_NAMESPACE)
1368 vStringCopy (temp, string);
1369 vStringClear (string);
1370 if (isType (s->blockName, TOKEN_NAME))
1372 if (isType (s->context, TOKEN_NAME) &&
1373 vStringLength (s->context->name) > 0)
1375 vStringCat (string, s->context->name);
1376 addContextSeparator (string);
1378 vStringCat (string, s->blockName->name);
1379 nonAnonPresent = TRUE;
1381 else
1382 vStringCopyS (string, anon);
1383 if (vStringLength (temp) > 0)
1384 addContextSeparator (string);
1385 vStringCat (string, temp);
1388 vStringDelete (temp);
1390 if (! nonAnonPresent)
1391 vStringClear (string);
1395 static void makeExtraTagEntry (const tagType type, tagEntryInfo *const e,
1396 vString *const scope)
1398 if (isXtagEnabled(XTAG_QUALIFIED_TAGS) &&
1399 scope != NULL && vStringLength (scope) > 0)
1401 vString *const scopedName = vStringNew ();
1403 if (type != TAG_ENUMERATOR)
1404 vStringCopy (scopedName, scope);
1405 else
1407 /* remove last component (i.e. enumeration name) from scope */
1408 const char* const sc = vStringValue (scope);
1409 const char* colon = strrchr (sc, ':');
1410 if (colon != NULL)
1412 while (*colon == ':' && colon > sc)
1413 --colon;
1414 vStringNCopy (scopedName, scope, colon + 1 - sc);
1417 if (vStringLength (scopedName) > 0)
1419 addContextSeparator (scopedName);
1420 vStringCatS (scopedName, e->name);
1421 e->name = vStringValue (scopedName);
1422 makeTagEntry (e);
1424 vStringDelete (scopedName);
1428 static void makeTag (const tokenInfo *const token,
1429 const statementInfo *const st,
1430 boolean isFileScope, const tagType type)
1432 #ifdef DEBUG_C
1433 printToken(token);
1434 fprintf(stderr, "<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>\n");
1435 printStatement(st);
1436 #endif
1437 /* Nothing is really of file scope when it appears in a header file.
1439 isFileScope = (boolean) (isFileScope && ! isHeaderFile ());
1441 if (isType (token, TOKEN_NAME) && vStringLength (token->name) > 0 /* &&
1442 includeTag (type, isFileScope) */)
1444 vString *scope = vStringNew ();
1445 tagEntryInfo e;
1447 /* take only functions which are introduced by "function ..." */
1448 if (type == TAG_FUNCTION && isLanguage (Lang_ferite) &&
1449 strncmp("function", st->firstToken->name->buffer, 8) != 0)
1451 return;
1454 initTagEntry (&e, vStringValue (token->name), tagKind (type));
1456 e.lineNumber = token->lineNumber;
1457 e.filePosition = token->filePosition;
1458 e.isFileScope = isFileScope;
1460 findScopeHierarchy (scope, st);
1461 addOtherFields (&e, type, token, st, scope);
1463 #ifdef DEBUG_C
1464 printTagEntry(&e);
1465 #endif
1466 makeTagEntry (&e);
1467 if (NULL != TagEntryFunction)
1468 makeExtraTagEntry (type, &e, scope);
1469 vStringDelete (scope);
1470 if (NULL != e.extensionFields.signature)
1471 free((char *) e.extensionFields.signature);
1475 static boolean isValidTypeSpecifier (const declType declaration)
1477 boolean result;
1478 switch (declaration)
1480 case DECL_BASE:
1481 case DECL_CLASS:
1482 case DECL_ENUM:
1483 case DECL_STRUCT:
1484 case DECL_UNION:
1485 result = TRUE;
1486 break;
1488 default:
1489 result = FALSE;
1490 break;
1492 return result;
1495 static void qualifyEnumeratorTag (const statementInfo *const st,
1496 const tokenInfo *const nameToken)
1498 if (isType (nameToken, TOKEN_NAME))
1499 makeTag (nameToken, st, TRUE, TAG_ENUMERATOR);
1502 static void qualifyFunctionTag (const statementInfo *const st,
1503 const tokenInfo *const nameToken)
1505 if (isType (nameToken, TOKEN_NAME))
1507 const tagType type = (isLanguage (Lang_java) || isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1508 ? TAG_METHOD : TAG_FUNCTION;
1509 const boolean isFileScope =
1510 (boolean) (st->member.access == ACCESS_PRIVATE ||
1511 (!isMember (st) && st->scope == SCOPE_STATIC));
1513 makeTag (nameToken, st, isFileScope, type);
1517 static void qualifyFunctionDeclTag (const statementInfo *const st,
1518 const tokenInfo *const nameToken)
1520 if (! isType (nameToken, TOKEN_NAME))
1522 else if (isLanguage (Lang_java) || isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1523 qualifyFunctionTag (st, nameToken);
1524 else if (st->scope == SCOPE_TYPEDEF)
1525 makeTag (nameToken, st, TRUE, TAG_TYPEDEF);
1526 else if (isValidTypeSpecifier (st->declaration) &&
1527 ! (isLanguage (Lang_csharp) || isLanguage (Lang_vala)))
1528 makeTag (nameToken, st, TRUE, TAG_PROTOTYPE);
1531 static void qualifyCompoundTag (const statementInfo *const st,
1532 const tokenInfo *const nameToken)
1534 if (isType (nameToken, TOKEN_NAME))
1536 const tagType type = declToTagType (st->declaration);
1538 if (type != TAG_UNDEFINED)
1539 makeTag (nameToken, st, (boolean) (! isLanguage (Lang_java) &&
1540 ! isLanguage (Lang_csharp) &&
1541 ! isLanguage (Lang_vala)), type);
1545 static void qualifyBlockTag (statementInfo *const st,
1546 const tokenInfo *const nameToken)
1548 switch (st->declaration)
1550 case DECL_CLASS:
1551 case DECL_ENUM:
1552 case DECL_INTERFACE:
1553 case DECL_NAMESPACE:
1554 case DECL_STRUCT:
1555 case DECL_UNION:
1556 qualifyCompoundTag (st, nameToken);
1557 break;
1558 default: break;
1562 static void qualifyVariableTag (const statementInfo *const st,
1563 const tokenInfo *const nameToken)
1565 /* We have to watch that we do not interpret a declaration of the
1566 * form "struct tag;" as a variable definition. In such a case, the
1567 * token preceding the name will be a keyword.
1569 if (! isType (nameToken, TOKEN_NAME))
1571 else if (st->declaration == DECL_IGNORE)
1573 else if (st->scope == SCOPE_TYPEDEF)
1574 makeTag (nameToken, st, TRUE, TAG_TYPEDEF);
1575 else if (st->declaration == DECL_PACKAGE)
1576 makeTag (nameToken, st, FALSE, TAG_PACKAGE);
1577 else if (st->declaration == DECL_MODULE) /* handle modules in D as namespaces */
1578 makeTag (nameToken, st, FALSE, TAG_NAMESPACE);
1579 else if (isValidTypeSpecifier (st->declaration))
1581 if (isMember (st))
1583 if (isLanguage (Lang_java) || isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1584 makeTag (nameToken, st, (boolean) (st->member.access == ACCESS_PRIVATE), TAG_FIELD);
1585 else if (st->scope == SCOPE_GLOBAL || st->scope == SCOPE_STATIC)
1586 makeTag (nameToken, st, TRUE, TAG_MEMBER);
1588 else if (isLanguage (Lang_java) || isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1590 else
1592 if (st->scope == SCOPE_EXTERN || ! st->haveQualifyingName)
1593 makeTag (nameToken, st, FALSE, TAG_EXTERN_VAR);
1594 else
1595 makeTag (nameToken, st, (boolean) (st->scope == SCOPE_STATIC), TAG_VARIABLE);
1601 * Parsing functions
1604 static int skipToOneOf (const char *const chars)
1606 int c;
1608 c = cppGetc ();
1609 while (c != EOF && c != '\0' && strchr (chars, c) == NULL);
1611 return c;
1614 /* Skip to the next non-white character.
1616 static int skipToNonWhite (void)
1618 int c;
1622 c = cppGetc ();
1624 while (isspace (c));
1626 return c;
1629 /* Skips to the next brace in column 1. This is intended for cases where
1630 * preprocessor constructs result in unbalanced braces.
1632 static void skipToFormattedBraceMatch (void)
1634 int c, next;
1636 c = cppGetc ();
1637 next = cppGetc ();
1638 while (c != EOF && (c != '\n' || next != '}'))
1640 c = next;
1641 next = cppGetc ();
1645 /* Skip to the matching character indicated by the pair string. If skipping
1646 * to a matching brace and any brace is found within a different level of a
1647 * #if conditional statement while brace formatting is in effect, we skip to
1648 * the brace matched by its formatting. It is assumed that we have already
1649 * read the character which starts the group (i.e. the first character of
1650 * "pair").
1652 static void skipToMatch (const char *const pair)
1654 const boolean braceMatching = (boolean) (strcmp ("{}", pair) == 0);
1655 const boolean braceFormatting = (boolean) (isBraceFormat () && braceMatching);
1656 const unsigned int initialLevel = getDirectiveNestLevel ();
1657 const int begin = pair [0], end = pair [1];
1658 const unsigned long inputLineNumber = getInputLineNumber ();
1659 int matchLevel = 1;
1660 int c = '\0';
1661 if (isLanguage(Lang_d) && pair[0] == '<')
1662 return; /* ignore e.g. Foo!(x < 2) */
1663 while (matchLevel > 0 && (c = cppGetc ()) != EOF)
1665 if (c == begin)
1667 ++matchLevel;
1668 if (braceFormatting && getDirectiveNestLevel () != initialLevel)
1670 skipToFormattedBraceMatch ();
1671 break;
1674 else if (c == end)
1676 --matchLevel;
1677 if (braceFormatting && getDirectiveNestLevel () != initialLevel)
1679 skipToFormattedBraceMatch ();
1680 break;
1683 /* early out if matching "<>" and we encounter a ";" or "{" to mitigate
1684 * match problems with C++ generics containing a static expression like
1685 * foo<X<Y> bar;
1686 * normally neither ";" nor "{" could appear inside "<>" anyway. */
1687 else if (isLanguage (Lang_cpp) && begin == '<' &&
1688 (c == ';' || c == '{'))
1690 cppUngetc (c);
1691 break;
1694 if (c == EOF)
1696 verbose ("%s: failed to find match for '%c' at line %lu\n",
1697 getInputFileName (), begin, inputLineNumber);
1698 if (braceMatching)
1699 longjmp (Exception, (int) ExceptionBraceFormattingError);
1700 else
1701 longjmp (Exception, (int) ExceptionFormattingError);
1705 static void skipParens (void)
1707 const int c = skipToNonWhite ();
1709 if (c == '(')
1710 skipToMatch ("()");
1711 else
1712 cppUngetc (c);
1715 static void skipBraces (void)
1717 const int c = skipToNonWhite ();
1719 if (c == '{')
1720 skipToMatch ("{}");
1721 else
1722 cppUngetc (c);
1725 static keywordId analyzeKeyword (const char *const name)
1727 const keywordId id = (keywordId) lookupKeyword (name, getSourceLanguage ());
1729 /* ignore D @attributes and Java @annotations(...), but show them in function signatures */
1730 if ((isLanguage(Lang_d) || isLanguage(Lang_java)) && id == KEYWORD_NONE && name[0] == '@')
1732 skipParens(); /* if annotation has parameters, skip them */
1733 return KEYWORD_CONST;
1735 return id;
1738 static void analyzeIdentifier (tokenInfo *const token)
1740 char *const name = vStringValue (token->name);
1741 const char *replacement = NULL;
1742 boolean parensToo = FALSE;
1744 if (isLanguage (Lang_java) ||
1745 ! isIgnoreToken (name, &parensToo, &replacement))
1747 if (replacement != NULL)
1748 token->keyword = analyzeKeyword (replacement);
1749 else
1750 token->keyword = analyzeKeyword (vStringValue (token->name));
1752 if (token->keyword == KEYWORD_NONE)
1753 token->type = TOKEN_NAME;
1754 else
1755 token->type = TOKEN_KEYWORD;
1757 else
1759 initToken (token);
1760 if (parensToo)
1762 int c = skipToNonWhite ();
1764 if (c == '(')
1765 skipToMatch ("()");
1770 static void readIdentifier (tokenInfo *const token, const int firstChar)
1772 vString *const name = token->name;
1773 int c = firstChar;
1775 initToken (token);
1777 /* Bug #1585745 (CTags): strangely, C++ destructors allow whitespace between
1778 * the ~ and the class name. */
1779 if (isLanguage (Lang_cpp) && firstChar == '~')
1781 vStringPut (name, c);
1782 c = skipToNonWhite ();
1787 vStringPut (name, c);
1788 c = cppGetc ();
1789 } while (isident (c) || (isLanguage (Lang_vala) && '.' == c));
1790 vStringTerminate (name);
1791 cppUngetc (c); /* unget non-identifier character */
1793 /* Vala supports '?' at end of a type (with or without whitespace before) for nullable types */
1794 if (isLanguage (Lang_vala))
1796 c = skipToNonWhite ();
1797 if ('?' == c)
1798 vStringPut (name, c);
1799 else
1800 cppUngetc (c);
1803 analyzeIdentifier (token);
1806 static void readPackageName (tokenInfo *const token, const int firstChar)
1808 vString *const name = token->name;
1809 int c = firstChar;
1811 initToken (token);
1813 while (isident (c) || c == '.')
1815 vStringPut (name, c);
1816 c = cppGetc ();
1818 vStringTerminate (name);
1819 cppUngetc (c); /* unget non-package character */
1822 static void readPackageOrNamespace (statementInfo *const st, const declType declaration)
1824 st->declaration = declaration;
1826 if (declaration == DECL_NAMESPACE && !(isLanguage (Lang_csharp) || isLanguage (Lang_vala)))
1828 /* In C++ a namespace is specified one level at a time. */
1829 return;
1831 else
1833 /* In C#, a namespace can also be specified like a Java package name. */
1834 tokenInfo *const token = activeToken (st);
1835 Assert (isType (token, TOKEN_KEYWORD));
1836 readPackageName (token, skipToNonWhite ());
1837 token->type = TOKEN_NAME;
1838 st->gotName = TRUE;
1839 st->haveQualifyingName = TRUE;
1843 static void readPackage (statementInfo *const st)
1845 tokenInfo *const token = activeToken (st);
1846 Assert (isType (token, TOKEN_KEYWORD));
1847 readPackageName (token, skipToNonWhite ());
1848 token->type = TOKEN_NAME;
1849 if (isLanguage (Lang_d))
1850 st->declaration = DECL_MODULE;
1851 else
1852 st->declaration = DECL_PACKAGE;
1853 st->gotName = TRUE;
1854 st->haveQualifyingName = TRUE;
1857 static void processName (statementInfo *const st)
1859 Assert (isType (activeToken (st), TOKEN_NAME));
1860 if (st->gotName && st->declaration == DECL_NONE)
1861 st->declaration = DECL_BASE;
1862 st->gotName = TRUE;
1863 st->haveQualifyingName = TRUE;
1866 static void readOperator (statementInfo *const st)
1868 const char *const acceptable = "+-*/%^&|~!=<>,[]";
1869 const tokenInfo* const prev = prevToken (st,1);
1870 tokenInfo *const token = activeToken (st);
1871 vString *const name = token->name;
1872 int c = skipToNonWhite ();
1874 /* When we arrive here, we have the keyword "operator" in 'name'.
1876 if (isType (prev, TOKEN_KEYWORD) && (prev->keyword == KEYWORD_ENUM ||
1877 prev->keyword == KEYWORD_STRUCT || prev->keyword == KEYWORD_UNION))
1878 ; /* ignore "operator" keyword if preceded by these keywords */
1879 else if (c == '(')
1881 /* Verify whether this is a valid function call (i.e. "()") operator.
1883 if (cppGetc () == ')')
1885 vStringPut (name, ' '); /* always separate operator from keyword */
1886 c = skipToNonWhite ();
1887 if (c == '(')
1888 vStringCatS (name, "()");
1890 else
1892 skipToMatch ("()");
1893 c = cppGetc ();
1896 else if (isident1 (c))
1898 /* Handle "new" and "delete" operators, and conversion functions
1899 * (per 13.3.1.1.2 [2] of the C++ spec).
1901 boolean whiteSpace = TRUE; /* default causes insertion of space */
1904 if (isspace (c))
1905 whiteSpace = TRUE;
1906 else
1908 if (whiteSpace)
1910 vStringPut (name, ' ');
1911 whiteSpace = FALSE;
1913 vStringPut (name, c);
1915 c = cppGetc ();
1916 } while (! isOneOf (c, "(;") && c != EOF);
1917 vStringTerminate (name);
1919 else if (isOneOf (c, acceptable))
1921 vStringPut (name, ' '); /* always separate operator from keyword */
1924 vStringPut (name, c);
1925 c = cppGetc ();
1926 } while (isOneOf (c, acceptable));
1927 vStringTerminate (name);
1930 cppUngetc (c);
1932 token->type = TOKEN_NAME;
1933 token->keyword = KEYWORD_NONE;
1934 processName (st);
1937 static void copyToken (tokenInfo *const dest, const tokenInfo *const src)
1939 dest->type = src->type;
1940 dest->keyword = src->keyword;
1941 dest->filePosition = src->filePosition;
1942 dest->lineNumber = src->lineNumber;
1943 vStringCopy (dest->name, src->name);
1946 static void setAccess (statementInfo *const st, const accessType laccess)
1948 if (isMember (st))
1950 if (isLanguage (Lang_cpp) || isLanguage (Lang_d) || isLanguage (Lang_ferite))
1952 int c = skipToNonWhite ();
1954 if (c == ':')
1955 reinitStatementWithToken (st, prevToken (st, 1), FALSE);
1956 else
1957 cppUngetc (c);
1959 st->member.accessDefault = laccess;
1961 st->member.access = laccess;
1965 static void discardTypeList (tokenInfo *const token)
1967 int c = skipToNonWhite ();
1968 while (isident1 (c))
1970 readIdentifier (token, c);
1971 c = skipToNonWhite ();
1972 if (c == '.' || c == ',')
1973 c = skipToNonWhite ();
1975 cppUngetc (c);
1978 static void addParentClass (statementInfo *const st, tokenInfo *const token)
1980 if (vStringLength (token->name) > 0 &&
1981 vStringLength (st->parentClasses) > 0)
1983 vStringPut (st->parentClasses, ',');
1985 vStringCat (st->parentClasses, token->name);
1988 static void readParents (statementInfo *const st, const int qualifier)
1990 tokenInfo *const token = newToken ();
1991 tokenInfo *const parent = newToken ();
1992 int c;
1996 c = skipToNonWhite ();
1997 if (isident1 (c))
1999 readIdentifier (token, c);
2000 if (isType (token, TOKEN_NAME))
2001 vStringCat (parent->name, token->name);
2002 else
2004 addParentClass (st, parent);
2005 initToken (parent);
2008 else if (c == qualifier)
2009 vStringPut (parent->name, c);
2010 else if (c == '<')
2011 skipToMatch ("<>");
2012 else if (isType (token, TOKEN_NAME))
2014 addParentClass (st, parent);
2015 initToken (parent);
2017 } while (c != '{' && c != EOF);
2018 cppUngetc (c);
2019 deleteToken (parent);
2020 deleteToken (token);
2023 static void checkIsClassEnum (statementInfo *const st, const declType decl)
2025 if (! isLanguage (Lang_cpp) || st->declaration != DECL_ENUM)
2026 st->declaration = decl;
2029 static void processToken (tokenInfo *const token, statementInfo *const st)
2031 switch (token->keyword) /* is it a reserved word? */
2033 default: break;
2035 case KEYWORD_NONE: processName (st); break;
2036 case KEYWORD_ABSTRACT: st->implementation = IMP_ABSTRACT; break;
2037 case KEYWORD_ATTRIBUTE: skipParens (); initToken (token); break;
2038 case KEYWORD_CATCH: skipParens (); skipBraces (); break;
2039 case KEYWORD_CHAR: st->declaration = DECL_BASE; break;
2040 case KEYWORD_CLASS: checkIsClassEnum (st, DECL_CLASS); break;
2041 case KEYWORD_CONST: st->declaration = DECL_BASE; break;
2042 case KEYWORD_DOUBLE: st->declaration = DECL_BASE; break;
2043 case KEYWORD_ENUM: st->declaration = DECL_ENUM; break;
2044 case KEYWORD_EXTENDS: readParents (st, '.');
2045 setToken (st, TOKEN_NONE); break;
2046 case KEYWORD_FLOAT: st->declaration = DECL_BASE; break;
2047 case KEYWORD_FRIEND: st->scope = SCOPE_FRIEND; break;
2048 case KEYWORD_IMPLEMENTS:readParents (st, '.');
2049 setToken (st, TOKEN_NONE); break;
2050 case KEYWORD_IMPORT: st->declaration = DECL_IGNORE; break;
2051 case KEYWORD_INT: st->declaration = DECL_BASE; break;
2052 case KEYWORD_BOOLEAN: st->declaration = DECL_BASE; break;
2053 case KEYWORD_WCHAR_T: st->declaration = DECL_BASE; break;
2054 case KEYWORD_SIZE_T: st->declaration = DECL_BASE; break;
2055 case KEYWORD_INTERFACE: st->declaration = DECL_INTERFACE; break;
2056 case KEYWORD_LONG: st->declaration = DECL_BASE; break;
2057 case KEYWORD_OPERATOR: readOperator (st); break;
2058 case KEYWORD_MODULE: readPackage (st); break;
2059 case KEYWORD_PRIVATE: setAccess (st, ACCESS_PRIVATE); break;
2060 case KEYWORD_PROTECTED: setAccess (st, ACCESS_PROTECTED); break;
2061 case KEYWORD_PUBLIC: setAccess (st, ACCESS_PUBLIC); break;
2062 case KEYWORD_SHORT: st->declaration = DECL_BASE; break;
2063 case KEYWORD_SIGNED: st->declaration = DECL_BASE; break;
2064 case KEYWORD_STRUCT: checkIsClassEnum (st, DECL_STRUCT); break;
2065 case KEYWORD_STATIC_ASSERT: skipParens (); break;
2066 case KEYWORD_THROWS: discardTypeList (token); break;
2067 case KEYWORD_TYPEDEF: st->scope = SCOPE_TYPEDEF; break;
2068 case KEYWORD_UNION: st->declaration = DECL_UNION; break;
2069 case KEYWORD_UNSIGNED: st->declaration = DECL_BASE; break;
2070 case KEYWORD_USING: st->declaration = DECL_IGNORE; break;
2071 case KEYWORD_VOID: st->declaration = DECL_BASE; break;
2072 case KEYWORD_VOLATILE: st->declaration = DECL_BASE; break;
2073 case KEYWORD_VIRTUAL: st->implementation = IMP_VIRTUAL; break;
2075 case KEYWORD_NAMESPACE: readPackageOrNamespace (st, DECL_NAMESPACE); break;
2076 case KEYWORD_PACKAGE: readPackageOrNamespace (st, DECL_PACKAGE); break;
2077 case KEYWORD_EVENT:
2079 if (isLanguage (Lang_csharp))
2080 st->declaration = DECL_EVENT;
2081 break;
2083 case KEYWORD_SIGNAL:
2085 if (isLanguage (Lang_vala))
2086 st->declaration = DECL_SIGNAL;
2087 break;
2089 case KEYWORD_EXTERN:
2091 if (! isLanguage (Lang_csharp) || !st->gotName)
2093 /*reinitStatement (st, FALSE);*/
2094 st->scope = SCOPE_EXTERN;
2095 st->declaration = DECL_BASE;
2097 break;
2099 case KEYWORD_STATIC:
2101 if (! isLanguage (Lang_java) && ! isLanguage (Lang_csharp) && ! isLanguage (Lang_vala))
2103 /*reinitStatement (st, FALSE);*/
2104 st->scope = SCOPE_STATIC;
2105 st->declaration = DECL_BASE;
2107 break;
2109 case KEYWORD_IF:
2110 if (isLanguage (Lang_d))
2111 { /* static if (is(typeof(__traits(getMember, a, name)) == function)) */
2112 int c = skipToNonWhite ();
2113 if (c == '(')
2114 skipToMatch ("()");
2116 break;
2121 * Parenthesis handling functions
2124 static void restartStatement (statementInfo *const st)
2126 tokenInfo *const save = newToken ();
2127 tokenInfo *token = activeToken (st);
2129 copyToken (save, token);
2130 DebugStatement ( if (debug (DEBUG_PARSE)) printf ("<ES>");)
2131 reinitStatement (st, FALSE);
2132 token = activeToken (st);
2133 copyToken (token, save);
2134 deleteToken (save);
2135 processToken (token, st);
2138 /* Skips over a mem-initializer-list of a ctor-initializer, defined as:
2140 * mem-initializer-list:
2141 * mem-initializer, mem-initializer-list
2143 * mem-initializer:
2144 * [::] [nested-name-spec] class-name (...)
2145 * identifier
2147 static void skipMemIntializerList (tokenInfo *const token)
2149 int c;
2153 c = skipToNonWhite ();
2154 while (isident1 (c) || c == ':')
2156 if (c != ':')
2157 readIdentifier (token, c);
2158 c = skipToNonWhite ();
2160 if (c == '<')
2162 skipToMatch ("<>");
2163 c = skipToNonWhite ();
2165 if (c == '(')
2167 skipToMatch ("()");
2168 c = skipToNonWhite ();
2170 } while (c == ',');
2171 cppUngetc (c);
2174 static void skipMacro (statementInfo *const st)
2176 tokenInfo *const prev2 = prevToken (st, 2);
2178 if (isType (prev2, TOKEN_NAME))
2179 retardToken (st);
2180 skipToMatch ("()");
2183 static boolean isDPostArgumentToken(tokenInfo *const token)
2185 switch (token->keyword)
2187 /* Note: some other keywords e.g. immutable are parsed as
2188 * KEYWORD_CONST - see initializeDParser */
2189 case KEYWORD_CONST:
2190 /* template constraint */
2191 case KEYWORD_IF:
2192 /* contracts */
2193 case KEYWORD_IN:
2194 case KEYWORD_OUT:
2195 case KEYWORD_BODY:
2196 return TRUE;
2197 default:
2198 break;
2200 /* @attributes */
2201 if (vStringValue(token->name)[0] == '@')
2202 return TRUE;
2203 return FALSE;
2206 /* Skips over characters following the parameter list. This will be either
2207 * non-ANSI style function declarations or C++ stuff. Our choices:
2209 * C (K&R):
2210 * int func ();
2211 * int func (one, two) int one; float two; {...}
2212 * C (ANSI):
2213 * int func (int one, float two);
2214 * int func (int one, float two) {...}
2215 * C++:
2216 * int foo (...) [const|volatile] [throw (...)];
2217 * int foo (...) [const|volatile] [throw (...)] [ctor-initializer] {...}
2218 * int foo (...) [const|volatile] [throw (...)] try [ctor-initializer] {...}
2219 * catch (...) {...}
2221 static boolean skipPostArgumentStuff (
2222 statementInfo *const st, parenInfo *const info)
2224 tokenInfo *const token = activeToken (st);
2225 unsigned int parameters = info->parameterCount;
2226 unsigned int elementCount = 0;
2227 boolean restart = FALSE;
2228 boolean end = FALSE;
2229 int c = skipToNonWhite ();
2233 switch (c)
2235 case ')': break;
2236 case ':': skipMemIntializerList (token);break; /* ctor-initializer */
2237 case '[': skipToMatch ("[]"); break;
2238 case '=': cppUngetc (c); end = TRUE; break;
2239 case '{': cppUngetc (c); end = TRUE; break;
2240 case '}': cppUngetc (c); end = TRUE; break;
2242 case '(':
2244 if (elementCount > 0)
2245 ++elementCount;
2246 skipToMatch ("()");
2247 break;
2250 case ';':
2252 if (parameters == 0 || elementCount < 2)
2254 cppUngetc (c);
2255 end = TRUE;
2257 else if (--parameters == 0)
2258 end = TRUE;
2259 break;
2262 default:
2264 if (isident1 (c))
2266 readIdentifier (token, c);
2267 if (isLanguage(Lang_d) && isDPostArgumentToken(token))
2268 token->keyword = KEYWORD_CONST;
2270 switch (token->keyword)
2272 case KEYWORD_ATTRIBUTE: skipParens (); break;
2273 case KEYWORD_THROW: skipParens (); break;
2274 case KEYWORD_CONST: break;
2275 case KEYWORD_NOEXCEPT: break;
2276 case KEYWORD_TRY: break;
2277 case KEYWORD_VOLATILE: break;
2279 case KEYWORD_CATCH: case KEYWORD_CLASS:
2280 case KEYWORD_EXPLICIT: case KEYWORD_EXTERN:
2281 case KEYWORD_FRIEND: case KEYWORD_INLINE:
2282 case KEYWORD_MUTABLE: case KEYWORD_NAMESPACE:
2283 case KEYWORD_NEW: case KEYWORD_OPERATOR:
2284 case KEYWORD_OVERLOAD: case KEYWORD_PRIVATE:
2285 case KEYWORD_PROTECTED: case KEYWORD_PUBLIC:
2286 case KEYWORD_STATIC: case KEYWORD_TEMPLATE:
2287 case KEYWORD_TYPEDEF: case KEYWORD_TYPENAME:
2288 case KEYWORD_USING: case KEYWORD_VIRTUAL:
2289 /* Never allowed within parameter declarations.
2291 restart = TRUE;
2292 end = TRUE;
2293 break;
2295 default:
2296 /* "override" and "final" are only keywords in the declaration of a virtual
2297 * member function, so need to be handled specially, not as keywords */
2298 if (isLanguage(Lang_cpp) && isType (token, TOKEN_NAME) &&
2299 (strcmp ("override", vStringValue (token->name)) == 0 ||
2300 strcmp ("final", vStringValue (token->name)) == 0))
2302 else if (isType (token, TOKEN_NONE))
2304 else if (info->isKnrParamList && info->parameterCount > 0)
2305 ++elementCount;
2306 else
2308 /* If we encounter any other identifier immediately
2309 * following an empty parameter list, this is almost
2310 * certainly one of those Microsoft macro "thingies"
2311 * that the automatic source code generation sticks
2312 * in. Terminate the current statement.
2314 restart = TRUE;
2315 end = TRUE;
2317 break;
2322 if (! end)
2324 c = skipToNonWhite ();
2325 if (c == EOF)
2326 end = TRUE;
2328 } while (! end);
2330 if (restart)
2331 restartStatement (st);
2332 else
2333 setToken (st, TOKEN_NONE);
2335 return (boolean) (c != EOF);
2338 static void skipJavaThrows (statementInfo *const st)
2340 tokenInfo *const token = activeToken (st);
2341 int c = skipToNonWhite ();
2343 if (isident1 (c))
2345 readIdentifier (token, c);
2346 if (token->keyword == KEYWORD_THROWS)
2350 c = skipToNonWhite ();
2351 if (isident1 (c))
2353 readIdentifier (token, c);
2354 c = skipToNonWhite ();
2356 } while (c == '.' || c == ',');
2359 cppUngetc (c);
2360 setToken (st, TOKEN_NONE);
2363 static void skipValaPostParens (statementInfo *const st)
2365 tokenInfo *const token = activeToken (st);
2366 int c = skipToNonWhite ();
2368 while (isident1 (c))
2370 readIdentifier (token, c);
2371 if (token->keyword == KEYWORD_ATTRIBUTE)
2373 /* parse contracts */
2374 skipParens ();
2375 c = skipToNonWhite ();
2377 else if (token->keyword == KEYWORD_THROWS)
2381 c = skipToNonWhite ();
2382 if (isident1 (c))
2384 readIdentifier (token, c);
2385 c = skipToNonWhite ();
2387 } while (c == '.' || c == ',');
2389 else
2390 break;
2392 cppUngetc (c);
2393 setToken (st, TOKEN_NONE);
2396 static void analyzePostParens (statementInfo *const st, parenInfo *const info)
2398 const unsigned long inputLineNumber = getInputLineNumber ();
2399 int c = skipToNonWhite ();
2401 cppUngetc (c);
2402 if (isOneOf (c, "{;,="))
2404 else if (isLanguage (Lang_java))
2405 skipJavaThrows (st);
2406 else if (isLanguage (Lang_vala))
2407 skipValaPostParens(st);
2408 else
2410 if (! skipPostArgumentStuff (st, info))
2412 verbose (
2413 "%s: confusing argument declarations beginning at line %lu\n",
2414 getInputFileName (), inputLineNumber);
2415 longjmp (Exception, (int) ExceptionFormattingError);
2420 static int parseParens (statementInfo *const st, parenInfo *const info)
2422 tokenInfo *const token = activeToken (st);
2423 unsigned int identifierCount = 0;
2424 unsigned int depth = 1;
2425 boolean firstChar = TRUE;
2426 int nextChar = '\0';
2428 info->parameterCount = 1;
2431 int c = skipToNonWhite ();
2433 switch (c)
2435 case '&':
2436 case '*':
2438 /* DEBUG_PRINT("parseParens, po++\n"); */
2439 info->isKnrParamList = FALSE;
2440 if (identifierCount == 0)
2441 info->isParamList = FALSE;
2442 initToken (token);
2443 break;
2445 case ':':
2447 info->isKnrParamList = FALSE;
2448 break;
2450 case '.':
2452 info->isNameCandidate = FALSE;
2453 info->isKnrParamList = FALSE;
2454 break;
2456 case ',':
2458 info->isNameCandidate = FALSE;
2459 if (info->isKnrParamList)
2461 ++info->parameterCount;
2462 identifierCount = 0;
2464 break;
2466 case '=':
2468 info->isKnrParamList = FALSE;
2469 info->isNameCandidate = FALSE;
2470 if (firstChar)
2472 info->isParamList = FALSE;
2473 skipMacro (st);
2474 depth = 0;
2476 break;
2478 case '[':
2480 info->isKnrParamList = FALSE;
2481 skipToMatch ("[]");
2482 break;
2484 case '<':
2486 info->isKnrParamList = FALSE;
2487 skipToMatch ("<>");
2488 break;
2490 case ')':
2492 if (firstChar)
2493 info->parameterCount = 0;
2494 --depth;
2495 break;
2497 case '(':
2499 info->isKnrParamList = FALSE;
2500 if (firstChar)
2502 info->isNameCandidate = FALSE;
2503 cppUngetc (c);
2504 skipMacro (st);
2505 depth = 0;
2507 else if (isType (token, TOKEN_PAREN_NAME))
2509 c = skipToNonWhite ();
2510 if (c == '*') /* check for function pointer */
2512 skipToMatch ("()");
2513 c = skipToNonWhite ();
2514 if (c == '(')
2515 skipToMatch ("()");
2517 else
2519 cppUngetc (c);
2520 cppUngetc ('(');
2521 info->nestedArgs = TRUE;
2524 else
2525 ++depth;
2526 break;
2529 default:
2531 if (isident1 (c))
2533 if (++identifierCount > 1)
2534 info->isKnrParamList = FALSE;
2535 readIdentifier (token, c);
2536 if (isType (token, TOKEN_NAME) && info->isNameCandidate)
2537 token->type = TOKEN_PAREN_NAME;
2538 else if (isType (token, TOKEN_KEYWORD))
2540 info->isKnrParamList = FALSE;
2541 info->isNameCandidate = FALSE;
2544 else if (isLanguage(Lang_d) && c == '!')
2545 { /* D template instantiation */
2546 info->isNameCandidate = FALSE;
2547 info->isKnrParamList = FALSE;
2549 else
2551 info->isParamList = FALSE;
2552 info->isKnrParamList = FALSE;
2553 info->isNameCandidate = FALSE;
2554 info->invalidContents = TRUE;
2556 break;
2559 firstChar = FALSE;
2560 } while (! info->nestedArgs && depth > 0 &&
2561 (info->isKnrParamList || info->isNameCandidate));
2563 if (! info->nestedArgs) while (depth > 0)
2565 skipToMatch ("()");
2566 --depth;
2568 if (st->argEndPosition == 0)
2569 st->argEndPosition = mio_tell (File.mio);
2571 if (! info->isNameCandidate)
2572 initToken (token);
2574 return nextChar;
2577 static void initParenInfo (parenInfo *const info)
2579 info->isParamList = TRUE;
2580 info->isKnrParamList = TRUE;
2581 info->isNameCandidate = TRUE;
2582 info->invalidContents = FALSE;
2583 info->nestedArgs = FALSE;
2584 info->parameterCount = 0;
2587 static void analyzeParens (statementInfo *const st)
2589 tokenInfo *const prev = prevToken (st, 1);
2591 if (! isType (prev, TOKEN_NONE)) /* in case of ignored enclosing macros */
2593 tokenInfo *const token = activeToken (st);
2594 parenInfo info;
2595 int c;
2597 initParenInfo (&info);
2598 parseParens (st, &info);
2599 c = skipToNonWhite ();
2600 cppUngetc (c);
2601 if (info.invalidContents)
2603 reinitStatement (st, FALSE);
2605 else if (info.isNameCandidate && isType (token, TOKEN_PAREN_NAME) &&
2606 ! st->gotParenName &&
2607 (! info.isParamList || ! st->haveQualifyingName ||
2608 c == '(' ||
2609 (c == '=' && st->implementation != IMP_VIRTUAL) ||
2610 (st->declaration == DECL_NONE && isOneOf (c, ",;"))))
2612 token->type = TOKEN_NAME;
2613 processName (st);
2614 st->gotParenName = TRUE;
2615 if (isLanguage(Lang_d) && c == '(' && isType (prev, TOKEN_NAME))
2617 st->declaration = DECL_FUNCTION_TEMPLATE;
2618 copyToken (st->blockName, prev);
2621 else if (! st->gotArgs && info.isParamList)
2623 st->gotArgs = TRUE;
2624 setToken (st, TOKEN_ARGS);
2625 advanceToken (st);
2626 analyzePostParens (st, &info);
2628 else
2629 setToken (st, TOKEN_NONE);
2634 * Token parsing functions
2637 static void addContext (statementInfo *const st, const tokenInfo* const token)
2639 if (isType (token, TOKEN_NAME))
2641 if (vStringLength (st->context->name) > 0)
2643 if (isLanguage (Lang_c) || isLanguage (Lang_cpp))
2644 vStringCatS (st->context->name, "::");
2645 else if (isLanguage (Lang_java) ||
2646 isLanguage (Lang_d) || isLanguage (Lang_ferite) ||
2647 isLanguage (Lang_csharp) || isLanguage (Lang_vala))
2648 vStringCatS (st->context->name, ".");
2650 vStringCat (st->context->name, token->name);
2651 st->context->type = TOKEN_NAME;
2655 static boolean inheritingDeclaration (declType decl)
2657 /* enum base types */
2658 if (decl == DECL_ENUM)
2660 return (boolean) (isLanguage (Lang_cpp) || isLanguage (Lang_csharp) ||
2661 isLanguage (Lang_d));
2663 return (boolean) (
2664 decl == DECL_CLASS ||
2665 decl == DECL_STRUCT ||
2666 decl == DECL_INTERFACE);
2669 static void processColon (statementInfo *const st)
2671 int c = cppGetc ();
2672 const boolean doubleColon = (boolean) (c == ':');
2674 if (doubleColon)
2676 setToken (st, TOKEN_DOUBLE_COLON);
2677 st->haveQualifyingName = FALSE;
2679 else
2681 cppUngetc (c);
2682 if ((isLanguage (Lang_cpp) || isLanguage (Lang_csharp) || isLanguage (Lang_d) ||
2683 isLanguage (Lang_vala)) &&
2684 inheritingDeclaration (st->declaration))
2686 readParents (st, ':');
2688 else if (parentDecl (st) == DECL_STRUCT || parentDecl (st) == DECL_CLASS)
2690 c = skipToOneOf (",;");
2691 if (c == ',')
2692 setToken (st, TOKEN_COMMA);
2693 else if (c == ';')
2694 setToken (st, TOKEN_SEMICOLON);
2696 else
2698 const tokenInfo *const prev = prevToken (st, 1);
2699 const tokenInfo *const prev2 = prevToken (st, 2);
2700 if (prev->keyword == KEYWORD_DEFAULT ||
2701 prev2->keyword == KEYWORD_CASE ||
2702 st->parent != NULL)
2704 reinitStatement (st, FALSE);
2710 /* Skips over any initializing value which may follow an '=' character in a
2711 * variable definition.
2713 static int skipInitializer (statementInfo *const st)
2715 boolean done = FALSE;
2716 int c;
2718 while (! done)
2720 c = skipToNonWhite ();
2722 if (c == EOF)
2723 longjmp (Exception, (int) ExceptionFormattingError);
2724 else switch (c)
2726 case ',':
2727 case ';': done = TRUE; break;
2729 case '0':
2730 if (st->implementation == IMP_VIRTUAL)
2731 st->implementation = IMP_PURE_VIRTUAL;
2732 break;
2734 case '[': skipToMatch ("[]"); break;
2735 case '(': skipToMatch ("()"); break;
2736 case '{': skipToMatch ("{}"); break;
2738 case '}':
2739 if (insideEnumBody (st))
2740 done = TRUE;
2741 else if (! isBraceFormat ())
2743 verbose ("%s: unexpected closing brace at line %lu\n",
2744 getInputFileName (), getInputLineNumber ());
2745 longjmp (Exception, (int) ExceptionBraceFormattingError);
2747 break;
2749 default: break;
2752 return c;
2755 static void processInitializer (statementInfo *const st)
2757 const boolean inEnumBody = insideEnumBody (st);
2758 const int c = skipInitializer (st);
2760 if (c == ';')
2761 setToken (st, TOKEN_SEMICOLON);
2762 else if (c == ',')
2763 setToken (st, TOKEN_COMMA);
2764 else if (c == '}' && inEnumBody)
2766 cppUngetc (c);
2767 setToken (st, TOKEN_COMMA);
2769 if (st->scope == SCOPE_EXTERN)
2770 st->scope = SCOPE_GLOBAL;
2773 static void parseIdentifier (statementInfo *const st, const int c)
2775 tokenInfo *const token = activeToken (st);
2777 readIdentifier (token, c);
2778 if (! isType (token, TOKEN_NONE))
2779 processToken (token, st);
2782 static void parseGeneralToken (statementInfo *const st, const int c)
2784 const tokenInfo *const prev = prevToken (st, 1);
2786 if (isident1(c))
2788 parseIdentifier (st, c);
2789 if (isType (st->context, TOKEN_NAME) &&
2790 isType (activeToken (st), TOKEN_NAME) && isType (prev, TOKEN_NAME))
2792 initToken (st->context);
2795 else if (isExternCDecl (st, c))
2797 st->declaration = DECL_NOMANGLE;
2798 st->scope = SCOPE_GLOBAL;
2802 /* Reads characters from the pre-processor and assembles tokens, setting
2803 * the current statement state.
2805 static void nextToken (statementInfo *const st)
2807 int c;
2808 tokenInfo *token = activeToken (st);
2811 c = skipToNonWhite();
2812 switch (c)
2814 case EOF: longjmp (Exception, (int) ExceptionEOF); break;
2815 case '(': analyzeParens (st); token = activeToken (st); break;
2816 case '*': setToken (st, TOKEN_STAR); break;
2817 case ',': setToken (st, TOKEN_COMMA); break;
2818 case ':': processColon (st); break;
2819 case ';': setToken (st, TOKEN_SEMICOLON); break;
2820 case '<': skipToMatch ("<>"); break;
2821 case '=': processInitializer (st); break;
2822 case '[':
2823 /* Hack for Vala: [..] can be a function attribute.
2824 * Seems not to have bad side effects, but have to test it more. */
2825 if (!isLanguage (Lang_vala))
2826 setToken (st, TOKEN_ARRAY);
2827 skipToMatch ("[]");
2828 break;
2829 case '{': setToken (st, TOKEN_BRACE_OPEN); break;
2830 case '}': setToken (st, TOKEN_BRACE_CLOSE); break;
2831 default: parseGeneralToken (st, c); break;
2833 } while (isType (token, TOKEN_NONE));
2835 if (isType (token, TOKEN_SEMICOLON) && st->parent)
2836 st->parent->nSemicolons ++;
2838 /* We want to know about non-keyword variable types */
2839 if (TOKEN_NONE == st->firstToken->type)
2841 if ((TOKEN_NAME == token->type) || isDataTypeKeyword(token))
2842 copyToken(st->firstToken, token);
2847 * Scanning support functions
2849 static unsigned int contextual_fake_count = 0;
2850 static statementInfo *CurrentStatement = NULL;
2852 static statementInfo *newStatement (statementInfo *const parent)
2854 statementInfo *const st = xMalloc (1, statementInfo);
2855 unsigned int i;
2857 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
2858 st->token [i] = newToken ();
2860 st->context = newToken ();
2861 st->blockName = newToken ();
2862 st->parentClasses = vStringNew ();
2863 st->firstToken = newToken();
2865 initStatement (st, parent);
2866 CurrentStatement = st;
2868 return st;
2871 static void deleteStatement (void)
2873 statementInfo *const st = CurrentStatement;
2874 statementInfo *const parent = st->parent;
2875 unsigned int i;
2877 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
2879 deleteToken (st->token [i]); st->token [i] = NULL;
2881 deleteToken (st->blockName); st->blockName = NULL;
2882 deleteToken (st->context); st->context = NULL;
2883 vStringDelete (st->parentClasses); st->parentClasses = NULL;
2884 deleteToken(st->firstToken);
2885 eFree (st);
2886 CurrentStatement = parent;
2889 static void deleteAllStatements (void)
2891 while (CurrentStatement != NULL)
2892 deleteStatement ();
2895 static boolean isStatementEnd (const statementInfo *const st)
2897 const tokenInfo *const token = activeToken (st);
2898 boolean isEnd;
2900 if (isType (token, TOKEN_SEMICOLON))
2901 isEnd = TRUE;
2902 else if (isType (token, TOKEN_BRACE_CLOSE))
2903 /* Java, D, C#, Vala do not require semicolons to end a block. Neither do
2904 * C++ namespaces. All other blocks require a semicolon to terminate them.
2906 isEnd = (boolean) (isLanguage (Lang_java) || isLanguage (Lang_d) ||
2907 isLanguage (Lang_csharp) || isLanguage (Lang_vala) ||
2908 ! isContextualStatement (st));
2909 else
2910 isEnd = FALSE;
2912 return isEnd;
2915 static void checkStatementEnd (statementInfo *const st)
2917 const tokenInfo *const token = activeToken (st);
2918 boolean comma = isType (token, TOKEN_COMMA);
2920 if (comma || isStatementEnd (st))
2922 reinitStatementWithToken (st, activeToken (st), comma);
2924 DebugStatement ( if (debug (DEBUG_PARSE)) printf ("<ES>"); )
2925 cppEndStatement ();
2927 else
2929 cppBeginStatement ();
2930 advanceToken (st);
2934 static void nest (statementInfo *const st, const unsigned int nestLevel)
2936 switch (st->declaration)
2938 case DECL_CLASS:
2939 case DECL_ENUM:
2940 case DECL_INTERFACE:
2941 case DECL_NAMESPACE:
2942 case DECL_NOMANGLE:
2943 case DECL_STRUCT:
2944 case DECL_UNION:
2945 createTags (nestLevel, st);
2946 break;
2947 default:
2948 skipToMatch ("{}");
2949 break;
2951 advanceToken (st);
2952 setToken (st, TOKEN_BRACE_CLOSE);
2955 static void tagCheck (statementInfo *const st)
2957 const tokenInfo *const token = activeToken (st);
2958 const tokenInfo *const prev = prevToken (st, 1);
2959 const tokenInfo *const prev2 = prevToken (st, 2);
2961 switch (token->type)
2963 case TOKEN_NAME:
2965 if (insideEnumBody (st) &&
2966 /* Java enumerations can contain members after a semicolon */
2967 (! isLanguage(Lang_java) || st->parent->nSemicolons < 1))
2968 qualifyEnumeratorTag (st, token);
2969 break;
2971 #if 0
2972 case TOKEN_PACKAGE:
2974 if (st->haveQualifyingName)
2975 makeTag (token, st, FALSE, TAG_PACKAGE);
2976 break;
2978 #endif
2979 case TOKEN_BRACE_OPEN:
2981 if (isType (prev, TOKEN_ARGS))
2983 if (st->declaration == DECL_FUNCTION_TEMPLATE)
2984 qualifyFunctionTag (st, st->blockName);
2985 else if (st->haveQualifyingName)
2987 if (isType (prev2, TOKEN_NAME))
2988 copyToken (st->blockName, prev2);
2989 /* D structure templates */
2990 if (isLanguage (Lang_d) &&
2991 (st->declaration == DECL_CLASS || st->declaration == DECL_STRUCT ||
2992 st->declaration == DECL_INTERFACE || st->declaration == DECL_NAMESPACE))
2993 qualifyBlockTag (st, prev2);
2994 else
2996 st->declaration = DECL_FUNCTION;
2997 qualifyFunctionTag (st, prev2);
3001 else if (isContextualStatement (st))
3003 tokenInfo *name_token = (tokenInfo *)prev;
3004 boolean free_name_token = FALSE;
3006 /* C++ 11 allows class <name> final { ... } */
3007 if (isLanguage (Lang_cpp) && isType (prev, TOKEN_NAME) &&
3008 strcmp("final", vStringValue(prev->name)) == 0 &&
3009 isType(prev2, TOKEN_NAME))
3011 name_token = (tokenInfo *)prev2;
3012 copyToken (st->blockName, name_token);
3014 else if (isType (name_token, TOKEN_NAME))
3016 if (!isLanguage (Lang_vala))
3017 copyToken (st->blockName, name_token);
3018 else
3020 switch (st->declaration)
3022 case DECL_CLASS:
3023 case DECL_ENUM:
3024 case DECL_INTERFACE:
3025 case DECL_NAMESPACE:
3026 case DECL_STRUCT:
3027 copyToken (st->blockName, name_token);
3028 break;
3030 /* anything else can be a property */
3031 default:
3032 /* makeTag (prev, st, FALSE, TAG_PROPERTY); */
3033 /* FIXME: temporary hack to get properties shown */
3034 makeTag (prev, st, FALSE, TAG_FIELD);
3035 break;
3039 else if (isLanguage (Lang_csharp))
3040 makeTag (prev, st, FALSE, TAG_PROPERTY);
3041 else
3043 tokenInfo *contextual_token = (tokenInfo *)prev;
3044 if(isContextualKeyword (contextual_token))
3046 char buffer[64];
3048 name_token = newToken ();
3049 free_name_token = TRUE;
3050 copyToken (name_token, contextual_token);
3052 sprintf(buffer, "anon_%s_%d", name_token->name->buffer, contextual_fake_count++);
3053 vStringClear(name_token->name);
3054 vStringCatS(name_token->name, buffer);
3056 name_token->type = TOKEN_NAME;
3057 name_token->keyword = KEYWORD_NONE;
3059 advanceToken (st);
3060 contextual_token = activeToken (st);
3061 copyToken (contextual_token, token);
3062 copyToken ((tokenInfo *const)token, name_token);
3063 copyToken (st->blockName, name_token);
3064 copyToken (st->firstToken, name_token);
3067 qualifyBlockTag (st, name_token);
3068 if (free_name_token)
3069 deleteToken (name_token);
3071 break;
3073 case TOKEN_ARRAY:
3074 case TOKEN_SEMICOLON:
3075 case TOKEN_COMMA:
3077 if (insideEnumBody (st) &&
3078 /* Java enumerations can contain members after a semicolon */
3079 (! isLanguage (Lang_java) || st->parent->nSemicolons < 2))
3081 else if (isType (prev, TOKEN_NAME))
3083 if (isContextualKeyword (prev2))
3084 makeTag (prev, st, TRUE, TAG_EXTERN_VAR);
3085 else
3086 qualifyVariableTag (st, prev);
3088 else if (isType (prev, TOKEN_ARGS) && isType (prev2, TOKEN_NAME))
3090 qualifyFunctionDeclTag (st, prev2);
3092 break;
3094 default:
3095 break;
3099 /* Parses the current file and decides whether to write out and tags that
3100 * are discovered.
3102 static void createTags (const unsigned int nestLevel,
3103 statementInfo *const parent)
3105 statementInfo *const st = newStatement (parent);
3107 DebugStatement ( if (nestLevel > 0) debugParseNest (TRUE, nestLevel); )
3108 while (TRUE)
3110 tokenInfo *token;
3112 nextToken (st);
3113 token = activeToken (st);
3114 if (isType (token, TOKEN_BRACE_CLOSE))
3116 if (nestLevel > 0)
3117 break;
3118 else
3120 verbose ("%s: unexpected closing brace at line %lu\n",
3121 getInputFileName (), getInputLineNumber ());
3122 longjmp (Exception, (int) ExceptionBraceFormattingError);
3125 else if (isType (token, TOKEN_DOUBLE_COLON))
3127 addContext (st, prevToken (st, 1));
3128 advanceToken (st);
3130 else
3132 tagCheck (st);/* this can add new token */
3133 if (isType (activeToken (st), TOKEN_BRACE_OPEN))
3134 nest (st, nestLevel + 1);
3135 checkStatementEnd (st);
3138 deleteStatement ();
3139 DebugStatement ( if (nestLevel > 0) debugParseNest (FALSE, nestLevel - 1); )
3142 static boolean findCTags (const unsigned int passCount)
3144 exception_t exception;
3145 boolean retry;
3147 contextual_fake_count = 0;
3149 Assert (passCount < 3);
3150 cppInit ((boolean) (passCount > 1), isLanguage (Lang_csharp), isLanguage (Lang_cpp), &(CKinds [CK_DEFINE]));
3152 exception = (exception_t) setjmp (Exception);
3153 retry = FALSE;
3155 if (exception == ExceptionNone)
3157 createTags (0, NULL);
3159 else
3161 deleteAllStatements ();
3162 if (exception == ExceptionBraceFormattingError && passCount == 1)
3164 retry = TRUE;
3165 verbose ("%s: retrying file with fallback brace matching algorithm\n",
3166 getInputFileName ());
3169 cppTerminate ();
3170 return retry;
3173 static void buildKeywordHash (const langType language, unsigned int idx)
3175 const size_t count = sizeof (KeywordTable) / sizeof (KeywordTable [0]);
3176 size_t i;
3177 for (i = 0 ; i < count ; ++i)
3179 const keywordDesc* const p = &KeywordTable [i];
3180 if (p->isValid [idx])
3181 addKeyword (p->name, language, (int) p->id);
3185 static void initializeCParser (const langType language)
3187 Lang_c = language;
3188 buildKeywordHash (language, 0);
3191 static void initializeCppParser (const langType language)
3193 Lang_cpp = language;
3194 buildKeywordHash (language, 1);
3197 static void initializeJavaParser (const langType language)
3199 Lang_java = language;
3200 buildKeywordHash (language, 3);
3203 static void initializeDParser (const langType language)
3205 /* treat these like const - some are for parsing like const(Type), some are just
3206 * function attributes */
3207 const char *const_aliases[] = {"immutable", "nothrow", "pure", "shared", NULL};
3208 const char **s;
3210 Lang_d = language;
3211 buildKeywordHash (language, 6);
3213 for (s = const_aliases; *s != NULL; s++)
3215 addKeyword (*s, language, KEYWORD_CONST);
3217 /* other keyword aliases */
3218 addKeyword ("alias", language, KEYWORD_TYPEDEF);
3219 /* skip 'static assert(...)' like 'static if (...)' */
3220 addKeyword ("assert", language, KEYWORD_IF);
3221 addKeyword ("unittest", language, KEYWORD_BODY); /* ignore */
3222 addKeyword ("version", language, KEYWORD_NAMESPACE); /* parse block */
3225 static void initializeGLSLParser (const langType language)
3227 Lang_glsl = language;
3228 buildKeywordHash (language, 0); /* C keywords */
3231 static void initializeFeriteParser (const langType language)
3233 Lang_ferite = language;
3234 buildKeywordHash (language, 1); /* C++ keywords */
3237 static void initializeCsharpParser (const langType language)
3239 Lang_csharp = language;
3240 buildKeywordHash (language, 2);
3243 static void initializeValaParser (const langType language)
3245 Lang_vala = language;
3246 buildKeywordHash (language, 5);
3248 /* keyword aliases */
3249 addKeyword ("ensures", language, KEYWORD_ATTRIBUTE); /* ignore */
3250 addKeyword ("errordomain", language, KEYWORD_ENUM); /* looks like enum */
3251 addKeyword ("requires", language, KEYWORD_ATTRIBUTE); /* ignore */
3254 extern parserDefinition* CParser (void)
3256 static const char *const extensions [] = { "c", "pc", "sc", NULL };
3257 parserDefinition* def = parserNew ("C");
3258 def->kinds = CKinds;
3259 def->kindCount = ARRAY_SIZE (CKinds);
3260 def->extensions = extensions;
3261 def->parser2 = findCTags;
3262 def->initialize = initializeCParser;
3263 return def;
3266 extern parserDefinition* CppParser (void)
3268 static const char *const extensions [] = {
3269 "c++", "cc", "cp", "cpp", "cxx", "h", "h++", "hh", "hp", "hpp", "hxx",
3270 "i",
3271 #ifndef CASE_INSENSITIVE_FILENAMES
3272 "C", "H",
3273 #endif
3274 NULL
3276 parserDefinition* def = parserNew ("C++");
3277 def->kinds = CKinds;
3278 def->kindCount = ARRAY_SIZE (CKinds);
3279 def->extensions = extensions;
3280 def->parser2 = findCTags;
3281 def->initialize = initializeCppParser;
3282 return def;
3285 extern parserDefinition* JavaParser (void)
3287 static const char *const extensions [] = { "java", NULL };
3288 parserDefinition* def = parserNew ("Java");
3289 def->kinds = JavaKinds;
3290 def->kindCount = ARRAY_SIZE (JavaKinds);
3291 def->extensions = extensions;
3292 def->parser2 = findCTags;
3293 def->initialize = initializeJavaParser;
3294 return def;
3297 extern parserDefinition* DParser (void)
3299 static const char *const extensions [] = { "d", "di", NULL };
3300 parserDefinition* def = parserNew ("D");
3301 def->kinds = DKinds;
3302 def->kindCount = ARRAY_SIZE (DKinds);
3303 def->extensions = extensions;
3304 def->parser2 = findCTags;
3305 def->initialize = initializeDParser;
3306 return def;
3309 extern parserDefinition* GLSLParser (void)
3311 static const char *const extensions [] = { "glsl", "frag", "vert", NULL };
3312 parserDefinition* def = parserNew ("GLSL");
3313 def->kinds = CKinds;
3314 def->kindCount = ARRAY_SIZE (CKinds);
3315 def->extensions = extensions;
3316 def->parser2 = findCTags;
3317 def->initialize = initializeGLSLParser;
3318 return def;
3321 extern parserDefinition* FeriteParser (void)
3323 static const char *const extensions [] = { "fe", NULL };
3324 parserDefinition* def = parserNew ("Ferite");
3325 def->kinds = CKinds;
3326 def->kindCount = ARRAY_SIZE (CKinds);
3327 def->extensions = extensions;
3328 def->parser2 = findCTags;
3329 def->initialize = initializeFeriteParser;
3330 return def;
3333 extern parserDefinition* CsharpParser (void)
3335 static const char *const extensions [] = { "cs", NULL };
3336 parserDefinition* def = parserNew ("C#");
3337 def->kinds = CsharpKinds;
3338 def->kindCount = ARRAY_SIZE (CsharpKinds);
3339 def->extensions = extensions;
3340 def->parser2 = findCTags;
3341 def->initialize = initializeCsharpParser;
3342 return def;
3345 extern parserDefinition* ValaParser (void)
3347 static const char *const extensions [] = { "vala", NULL };
3348 parserDefinition* def = parserNew ("Vala");
3349 def->kinds = ValaKinds;
3350 def->kindCount = ARRAY_SIZE (ValaKinds);
3351 def->extensions = extensions;
3352 def->parser2 = findCTags;
3353 def->initialize = initializeValaParser;
3354 return def;
3356 /* vi:set tabstop=4 shiftwidth=4: */