ctags: Rename Geany-specific tagEntryInfo::arglist to upstream's ::signature
[geany-mirror.git] / ctags / parsers / c.c
blobcf577494adc744cb4b7691dacc2c057eae7c8e47
1 /*
3 * Copyright (c) 1996-2001, Darren Hiebert
5 * This source code is released for free distribution under the terms of the
6 * GNU General Public License.
8 * This module contains functions for parsing and scanning C, C++, D and Java
9 * source files.
13 * INCLUDE FILES
15 #include "general.h" /* must always come first */
17 #include <string.h>
18 #include <setjmp.h>
20 #include "mio.h"
21 #include "entry.h"
22 #include "get.h"
23 #include "keyword.h"
24 #include "main.h"
25 #include "options.h"
26 #include "parse.h"
27 #include "read.h"
30 * MACROS
33 #define activeToken(st) ((st)->token [(int) (st)->tokenIndex])
34 #define parentDecl(st) ((st)->parent == NULL ? \
35 DECL_NONE : (st)->parent->declaration)
36 #define isType(token,t) (boolean) ((token)->type == (t))
37 #define insideEnumBody(st) (boolean) ((st)->parent == NULL ? FALSE : \
38 ((st)->parent->declaration == DECL_ENUM))
39 #define isExternCDecl(st,c) (boolean) ((c) == STRING_SYMBOL && \
40 ! (st)->haveQualifyingName && \
41 (st)->scope == SCOPE_EXTERN)
43 #define isOneOf(c,s) (boolean) (strchr ((s), (c)) != NULL)
46 * DATA DECLARATIONS
49 enum { NumTokens = 12 };
51 typedef enum eException
53 ExceptionNone, ExceptionEOF, ExceptionFormattingError,
54 ExceptionBraceFormattingError
55 } exception_t;
57 /* Used to specify type of keyword.
59 typedef enum eKeywordId
61 KEYWORD_NONE = -1,
62 KEYWORD_ATTRIBUTE, KEYWORD_ABSTRACT, KEYWORD_ALIAS,
63 KEYWORD_BOOLEAN, KEYWORD_BYTE, KEYWORD_BAD_STATE, KEYWORD_BAD_TRANS,
64 KEYWORD_BIND, KEYWORD_BIND_VAR, KEYWORD_BIT, KEYWORD_BODY,
65 KEYWORD_CASE, KEYWORD_CATCH, KEYWORD_CHAR, KEYWORD_CLASS, KEYWORD_CONST,
66 KEYWORD_CONSTRAINT, KEYWORD_COVERAGE_BLOCK, KEYWORD_COVERAGE_DEF,
67 KEYWORD_DEFAULT, KEYWORD_DELEGATE, KEYWORD_DELETE, KEYWORD_DO,
68 KEYWORD_DOUBLE,
69 KEYWORD_ELSE, KEYWORD_ENUM, KEYWORD_EXPLICIT, KEYWORD_EXTERN,
70 KEYWORD_EXTENDS, KEYWORD_EVENT,
71 KEYWORD_FINAL, KEYWORD_FINALLY, KEYWORD_FLOAT, KEYWORD_FOR, KEYWORD_FRIEND, KEYWORD_FUNCTION,
72 KEYWORD_GET, KEYWORD_GOTO,
73 KEYWORD_IF, KEYWORD_IMPLEMENTS, KEYWORD_IMPORT, KEYWORD_IN, KEYWORD_INLINE, KEYWORD_INT,
74 KEYWORD_INOUT, KEYWORD_INPUT, KEYWORD_INTEGER, KEYWORD_INTERFACE,
75 KEYWORD_INTERNAL,
76 KEYWORD_LOCAL, KEYWORD_LONG,
77 KEYWORD_M_BAD_STATE, KEYWORD_M_BAD_TRANS, KEYWORD_M_STATE, KEYWORD_M_TRANS,
78 KEYWORD_MODULE, KEYWORD_MUTABLE,
79 KEYWORD_NAMESPACE, KEYWORD_NEW, KEYWORD_NEWCOV, KEYWORD_NATIVE, KEYWORD_NOEXCEPT,
80 KEYWORD_OPERATOR, KEYWORD_OUT, KEYWORD_OUTPUT, KEYWORD_OVERLOAD, KEYWORD_OVERRIDE,
81 KEYWORD_PACKED, KEYWORD_PORT, KEYWORD_PACKAGE, KEYWORD_PRIVATE,
82 KEYWORD_PROGRAM, KEYWORD_PROTECTED, KEYWORD_PUBLIC,
83 KEYWORD_REF, KEYWORD_REGISTER, KEYWORD_RETURN,
84 KEYWORD_SHADOW, KEYWORD_STATE,
85 KEYWORD_SET, KEYWORD_SHORT, KEYWORD_SIGNAL, KEYWORD_SIGNED, KEYWORD_SIZE_T, KEYWORD_STATIC,
86 KEYWORD_STATIC_ASSERT, KEYWORD_STRING,
87 KEYWORD_STRUCT, KEYWORD_SWITCH, KEYWORD_SYNCHRONIZED,
88 KEYWORD_TASK, KEYWORD_TEMPLATE, KEYWORD_THIS, KEYWORD_THROW,
89 KEYWORD_THROWS, KEYWORD_TRANSIENT, KEYWORD_TRANS, KEYWORD_TRANSITION,
90 KEYWORD_TRY, KEYWORD_TYPEDEF, KEYWORD_TYPENAME,
91 KEYWORD_UINT, KEYWORD_ULONG, KEYWORD_UNION, KEYWORD_UNSIGNED, KEYWORD_USHORT,
92 KEYWORD_USING,
93 KEYWORD_VIRTUAL, KEYWORD_VOID, KEYWORD_VOLATILE,
94 KEYWORD_WCHAR_T, KEYWORD_WEAK, KEYWORD_WHILE
95 } keywordId;
97 /* Used to determine whether keyword is valid for the current language and
98 * what its ID is.
100 typedef struct sKeywordDesc
102 const char *name;
103 keywordId id;
104 short isValid [7]; /* indicates languages for which kw is valid */
105 } keywordDesc;
107 /* Used for reporting the type of object parsed by nextToken ().
109 typedef enum eTokenType
111 TOKEN_NONE, /* none */
112 TOKEN_ARGS, /* a parenthetical pair and its contents */
113 TOKEN_BRACE_CLOSE,
114 TOKEN_BRACE_OPEN,
115 TOKEN_COMMA, /* the comma character */
116 TOKEN_DOUBLE_COLON, /* double colon indicates nested-name-specifier */
117 TOKEN_KEYWORD,
118 TOKEN_NAME, /* an unknown name */
119 TOKEN_PACKAGE, /* a Java package name */
120 TOKEN_PAREN_NAME, /* a single name in parentheses */
121 TOKEN_SEMICOLON, /* the semicolon character */
122 TOKEN_SPEC, /* a storage class specifier, qualifier, type, etc. */
123 TOKEN_STAR, /* pointer detection */
124 TOKEN_ARRAY, /* array detection */
125 TOKEN_COUNT
126 } tokenType;
128 /* This describes the scoping of the current statement.
130 typedef enum eTagScope
132 SCOPE_GLOBAL, /* no storage class specified */
133 SCOPE_STATIC, /* static storage class */
134 SCOPE_EXTERN, /* external storage class */
135 SCOPE_FRIEND, /* declares access only */
136 SCOPE_TYPEDEF, /* scoping depends upon context */
137 SCOPE_COUNT
138 } tagScope;
140 typedef enum eDeclaration
142 DECL_NONE,
143 DECL_BASE, /* base type (default) */
144 DECL_CLASS,
145 DECL_ENUM,
146 DECL_EVENT,
147 DECL_SIGNAL,
148 DECL_FUNCTION,
149 DECL_FUNCTION_TEMPLATE,
150 DECL_IGNORE, /* non-taggable "declaration" */
151 DECL_INTERFACE,
152 DECL_MODULE,
153 DECL_NAMESPACE,
154 DECL_NOMANGLE, /* C++ name demangling block */
155 DECL_PACKAGE,
156 DECL_STRUCT,
157 DECL_UNION,
158 DECL_COUNT
159 } declType;
161 typedef enum eVisibilityType
163 ACCESS_UNDEFINED,
164 ACCESS_PRIVATE,
165 ACCESS_PROTECTED,
166 ACCESS_PUBLIC,
167 ACCESS_DEFAULT, /* Java-specific */
168 ACCESS_COUNT
169 } accessType;
171 /* Information about the parent class of a member (if any).
173 typedef struct sMemberInfo
175 accessType access; /* access of current statement */
176 accessType accessDefault; /* access default for current statement */
177 } memberInfo;
179 typedef struct sTokenInfo
181 tokenType type;
182 keywordId keyword;
183 vString* name; /* the name of the token */
184 unsigned long lineNumber; /* line number of tag */
185 MIOPos filePosition; /* file position of line containing name */
186 } tokenInfo;
188 typedef enum eImplementation
190 IMP_DEFAULT,
191 IMP_ABSTRACT,
192 IMP_VIRTUAL,
193 IMP_PURE_VIRTUAL,
194 IMP_COUNT
195 } impType;
197 /* Describes the statement currently undergoing analysis.
199 typedef struct sStatementInfo
201 tagScope scope;
202 declType declaration; /* specifier associated with TOKEN_SPEC */
203 boolean gotName; /* was a name parsed yet? */
204 boolean haveQualifyingName; /* do we have a name we are considering? */
205 boolean gotParenName; /* was a name inside parentheses parsed yet? */
206 boolean gotArgs; /* was a list of parameters parsed yet? */
207 unsigned int nSemicolons; /* how many semicolons did we see in that statement */
208 impType implementation; /* abstract or concrete implementation? */
209 unsigned int tokenIndex; /* currently active token */
210 tokenInfo* token [((int) NumTokens)];
211 tokenInfo* context; /* accumulated scope of current statement */
212 tokenInfo* blockName; /* name of current block */
213 memberInfo member; /* information regarding parent class/struct */
214 vString* parentClasses; /* parent classes */
215 struct sStatementInfo *parent; /* statement we are nested within */
216 long argEndPosition; /* Position where argument list ended */
217 tokenInfo* firstToken; /* First token in the statement */
218 } statementInfo;
220 /* Describes the type of tag being generated.
222 typedef enum eTagType
224 TAG_UNDEFINED,
225 TAG_CLASS, /* class name */
226 TAG_ENUM, /* enumeration name */
227 TAG_ENUMERATOR, /* enumerator (enumeration value) */
228 TAG_FIELD, /* field (Java) */
229 TAG_FUNCTION, /* function definition */
230 TAG_INTERFACE, /* interface declaration */
231 TAG_MEMBER, /* structure, class or interface member */
232 TAG_METHOD, /* method declaration */
233 TAG_NAMESPACE, /* namespace name */
234 TAG_PACKAGE, /* package name */
235 TAG_PROTOTYPE, /* function prototype or declaration */
236 TAG_STRUCT, /* structure name */
237 TAG_TYPEDEF, /* typedef name */
238 TAG_UNION, /* union name */
239 TAG_VARIABLE, /* variable definition */
240 TAG_EXTERN_VAR, /* external variable declaration */
241 TAG_MACRO, /* #define s */
242 TAG_EVENT, /* event */
243 TAG_SIGNAL, /* signal */
244 TAG_LOCAL, /* local variable definition */
245 TAG_PROPERTY, /* property name */
246 TAG_COUNT /* must be last */
247 } tagType;
249 typedef struct sParenInfo
251 boolean isParamList;
252 boolean isKnrParamList;
253 boolean isNameCandidate;
254 boolean invalidContents;
255 boolean nestedArgs;
256 unsigned int parameterCount;
257 } parenInfo;
260 * DATA DEFINITIONS
263 static jmp_buf Exception;
265 static langType Lang_c;
266 static langType Lang_cpp;
267 static langType Lang_csharp;
268 static langType Lang_java;
269 static langType Lang_d;
270 static langType Lang_glsl;
271 static langType Lang_ferite;
272 static langType Lang_vala;
274 /* Used to index into the CKinds table. */
275 typedef enum
277 CK_UNDEFINED = -1,
278 CK_CLASS, CK_DEFINE, CK_ENUMERATOR, CK_FUNCTION,
279 CK_ENUMERATION, CK_MEMBER, CK_NAMESPACE, CK_PROTOTYPE,
280 CK_STRUCT, CK_TYPEDEF, CK_UNION, CK_VARIABLE,
281 CK_EXTERN_VARIABLE
282 } cKind;
284 static kindOption CKinds [] = {
285 { TRUE, 'c', "class", "classes"},
286 { TRUE, 'd', "macro", "macro definitions"},
287 { TRUE, 'e', "enumerator", "enumerators (values inside an enumeration)"},
288 { TRUE, 'f', "function", "function definitions"},
289 { TRUE, 'g', "enum", "enumeration names"},
290 { TRUE, 'm', "member", "class, struct, and union members"},
291 { TRUE, 'n', "namespace", "namespaces"},
292 { FALSE, 'p', "prototype", "function prototypes"},
293 { TRUE, 's', "struct", "structure names"},
294 { TRUE, 't', "typedef", "typedefs"},
295 { TRUE, 'u', "union", "union names"},
296 { TRUE, 'v', "variable", "variable definitions"},
297 { FALSE, 'x', "externvar", "external variable declarations"},
300 /* Used to index into the DKinds table. */
301 typedef enum
303 DK_UNDEFINED = -1,
304 DK_CLASS, DK_ENUMERATOR, DK_FUNCTION,
305 DK_ENUMERATION, DK_INTERFACE, DK_MEMBER, DK_NAMESPACE, DK_PROTOTYPE,
306 DK_STRUCT, DK_TYPEDEF, DK_UNION, DK_VARIABLE,
307 DK_EXTERN_VARIABLE
308 } dKind;
310 static kindOption DKinds [] = {
311 { TRUE, 'c', "class", "classes"},
312 { TRUE, 'e', "enumerator", "enumerators (values inside an enumeration)"},
313 { TRUE, 'f', "function", "function definitions"},
314 { TRUE, 'g', "enum", "enumeration names"},
315 { TRUE, 'i', "interface", "interfaces"},
316 { TRUE, 'm', "member", "class, struct, and union members"},
317 { TRUE, 'n', "namespace", "namespaces"},
318 { FALSE, 'p', "prototype", "function prototypes"},
319 { TRUE, 's', "struct", "structure names"},
320 { TRUE, 't', "typedef", "typedefs"},
321 { TRUE, 'u', "union", "union names"},
322 { TRUE, 'v', "variable", "variable definitions"},
323 { FALSE, 'x', "externvar", "external variable declarations"},
326 /* Used to index into the JavaKinds table. */
327 typedef enum
329 JK_UNDEFINED = -1,
330 JK_CLASS, JK_FIELD, JK_INTERFACE, JK_METHOD,
331 JK_PACKAGE, JK_ENUMERATOR, JK_ENUMERATION
332 } javaKind;
334 static kindOption JavaKinds [] = {
335 { TRUE, 'c', "class", "classes"},
336 { TRUE, 'f', "field", "fields"},
337 { TRUE, 'i', "interface", "interfaces"},
338 { TRUE, 'm', "method", "methods"},
339 { TRUE, 'p', "package", "packages"},
340 { TRUE, 'e', "enumConstant", "enum constants"},
341 { TRUE, 'g', "enum", "enum types"},
344 typedef enum
346 CSK_UNDEFINED = -1,
347 CSK_CLASS, CSK_DEFINE, CSK_ENUMERATOR, CSK_EVENT, CSK_FIELD,
348 CSK_ENUMERATION, CSK_INTERFACE, CSK_LOCAL, CSK_METHOD,
349 CSK_NAMESPACE, CSK_PROPERTY, CSK_STRUCT, CSK_TYPEDEF
350 } csharpKind;
352 static kindOption CsharpKinds [] = {
353 { TRUE, 'c', "class", "classes"},
354 { TRUE, 'd', "macro", "macro definitions"},
355 { TRUE, 'e', "enumerator", "enumerators (values inside an enumeration)"},
356 { TRUE, 'E', "event", "events"},
357 { TRUE, 'f', "field", "fields"},
358 { TRUE, 'g', "enum", "enumeration names"},
359 { TRUE, 'i', "interface", "interfaces"},
360 { FALSE, 'l', "local", "local variables"},
361 { TRUE, 'm', "method", "methods"},
362 { TRUE, 'n', "namespace", "namespaces"},
363 { TRUE, 'p', "property", "properties"},
364 { TRUE, 's', "struct", "structure names"},
365 { TRUE, 't', "typedef", "typedefs"},
368 typedef enum {
369 VK_UNDEFINED = -1,
370 VK_CLASS, VK_DEFINE, VK_ENUMERATOR, VK_FIELD,
371 VK_ENUMERATION, VK_INTERFACE, VK_LOCAL, VK_METHOD,
372 VK_NAMESPACE, VK_PROPERTY, VK_SIGNAL, VK_STRUCT
373 } valaKind;
375 static kindOption ValaKinds [] = {
376 { TRUE, 'c', "class", "classes"},
377 { TRUE, 'd', "macro", "macro definitions"},
378 { TRUE, 'e', "enumerator", "enumerators (values inside an enumeration)"},
379 { TRUE, 'f', "field", "fields"},
380 { TRUE, 'g', "enum", "enumeration names"},
381 { TRUE, 'i', "interface", "interfaces"},
382 { FALSE, 'l', "local", "local variables"},
383 { TRUE, 'm', "method", "methods"},
384 { TRUE, 'n', "namespace", "namespaces"},
385 { TRUE, 'p', "property", "properties"},
386 { TRUE, 'S', "signal", "signals"},
387 { TRUE, 's', "struct", "structure names"},
390 /* Note: some keyword aliases are added in initializeDParser, initializeValaParser */
391 static const keywordDesc KeywordTable [] = {
392 /* C++ */
393 /* ANSI C | C# Java */
394 /* | | | | Vera */
395 /* | | | | | Vala */
396 /* | | | | | | D */
397 /* keyword keyword ID | | | | | | | */
398 { "__attribute__", KEYWORD_ATTRIBUTE, { 1, 1, 1, 0, 0, 0, 1 } },
399 { "abstract", KEYWORD_ABSTRACT, { 0, 0, 1, 1, 0, 1, 1 } },
400 { "bad_state", KEYWORD_BAD_STATE, { 0, 0, 0, 0, 1, 0, 0 } },
401 { "bad_trans", KEYWORD_BAD_TRANS, { 0, 0, 0, 0, 1, 0, 0 } },
402 { "bind", KEYWORD_BIND, { 0, 0, 0, 0, 1, 0, 0 } },
403 { "bind_var", KEYWORD_BIND_VAR, { 0, 0, 0, 0, 1, 0, 0 } },
404 { "bit", KEYWORD_BIT, { 0, 0, 0, 0, 1, 0, 0 } },
405 { "body", KEYWORD_BODY, { 0, 0, 0, 0, 0, 0, 1 } },
406 { "boolean", KEYWORD_BOOLEAN, { 0, 0, 0, 1, 0, 0, 0 } },
407 { "byte", KEYWORD_BYTE, { 0, 0, 0, 1, 0, 0, 1 } },
408 { "case", KEYWORD_CASE, { 1, 1, 1, 1, 0, 1, 1 } },
409 { "catch", KEYWORD_CATCH, { 0, 1, 1, 0, 0, 1, 1 } },
410 { "char", KEYWORD_CHAR, { 1, 1, 1, 1, 0, 1, 1 } },
411 { "class", KEYWORD_CLASS, { 0, 1, 1, 1, 1, 1, 1 } },
412 { "const", KEYWORD_CONST, { 1, 1, 1, 1, 0, 1, 1 } },
413 { "constraint", KEYWORD_CONSTRAINT, { 0, 0, 0, 0, 1, 0, 0 } },
414 { "coverage_block", KEYWORD_COVERAGE_BLOCK, { 0, 0, 0, 0, 1, 0, 0 } },
415 { "coverage_def", KEYWORD_COVERAGE_DEF, { 0, 0, 0, 0, 1, 0, 0 } },
416 { "do", KEYWORD_DO, { 1, 1, 1, 1, 0, 1, 1 } },
417 { "default", KEYWORD_DEFAULT, { 1, 1, 1, 1, 0, 1, 1 } },
418 { "delegate", KEYWORD_DELEGATE, { 0, 0, 1, 0, 0, 1, 1 } },
419 { "delete", KEYWORD_DELETE, { 0, 1, 0, 0, 0, 1, 1 } },
420 { "double", KEYWORD_DOUBLE, { 1, 1, 1, 1, 0, 1, 1 } },
421 { "else", KEYWORD_ELSE, { 1, 1, 0, 1, 0, 1, 1 } },
422 { "enum", KEYWORD_ENUM, { 1, 1, 1, 1, 1, 1, 1 } },
423 { "event", KEYWORD_EVENT, { 0, 0, 1, 0, 1, 0, 0 } },
424 { "explicit", KEYWORD_EXPLICIT, { 0, 1, 1, 0, 0, 0, 1 } },
425 { "extends", KEYWORD_EXTENDS, { 0, 0, 0, 1, 1, 0, 0 } },
426 { "extern", KEYWORD_EXTERN, { 1, 1, 1, 0, 1, 1, 0 } },
427 { "extern", KEYWORD_NAMESPACE, { 0, 0, 0, 0, 0, 0, 1 } }, /* parse block */
428 { "final", KEYWORD_FINAL, { 0, 0, 0, 1, 0, 0, 1 } },
429 { "finally", KEYWORD_FINALLY, { 0, 0, 0, 0, 0, 1, 1 } },
430 { "float", KEYWORD_FLOAT, { 1, 1, 1, 1, 0, 1, 1 } },
431 { "for", KEYWORD_FOR, { 1, 1, 1, 1, 0, 1, 1 } },
432 { "friend", KEYWORD_FRIEND, { 0, 1, 0, 0, 0, 0, 0 } },
433 { "function", KEYWORD_FUNCTION, { 0, 0, 0, 0, 1, 0, 1 } },
434 { "get", KEYWORD_GET, { 0, 0, 0, 0, 0, 1, 0 } },
435 { "goto", KEYWORD_GOTO, { 1, 1, 1, 1, 0, 1, 1 } },
436 { "if", KEYWORD_IF, { 1, 1, 1, 1, 0, 1, 1 } },
437 { "implements", KEYWORD_IMPLEMENTS, { 0, 0, 0, 1, 0, 0, 0 } },
438 { "import", KEYWORD_IMPORT, { 0, 0, 0, 1, 0, 0, 1 } },
439 { "inline", KEYWORD_INLINE, { 0, 1, 0, 0, 0, 1, 0 } },
440 { "in", KEYWORD_IN, { 0, 0, 0, 0, 0, 0, 1 } },
441 { "inout", KEYWORD_INOUT, { 0, 0, 0, 0, 1, 0, 0 } },
442 { "inout", KEYWORD_CONST, { 0, 0, 0, 0, 0, 0, 1 } }, /* treat like const */
443 { "input", KEYWORD_INPUT, { 0, 0, 0, 0, 1, 0, 0 } },
444 { "int", KEYWORD_INT, { 1, 1, 1, 1, 0, 1, 1 } },
445 { "integer", KEYWORD_INTEGER, { 0, 0, 0, 0, 1, 0, 0 } },
446 { "interface", KEYWORD_INTERFACE, { 0, 0, 1, 1, 1, 1, 1 } },
447 { "internal", KEYWORD_INTERNAL, { 0, 0, 1, 0, 0, 0, 0 } },
448 { "local", KEYWORD_LOCAL, { 0, 0, 0, 0, 1, 0, 0 } },
449 { "long", KEYWORD_LONG, { 1, 1, 1, 1, 0, 1, 1 } },
450 { "m_bad_state", KEYWORD_M_BAD_STATE, { 0, 0, 0, 0, 1, 0, 0 } },
451 { "m_bad_trans", KEYWORD_M_BAD_TRANS, { 0, 0, 0, 0, 1, 0, 0 } },
452 { "m_state", KEYWORD_M_STATE, { 0, 0, 0, 0, 1, 0, 0 } },
453 { "m_trans", KEYWORD_M_TRANS, { 0, 0, 0, 0, 1, 0, 0 } },
454 { "mutable", KEYWORD_MUTABLE, { 0, 1, 0, 0, 0, 0, 0 } },
455 { "module", KEYWORD_MODULE, { 0, 0, 0, 0, 0, 0, 1 } },
456 { "namespace", KEYWORD_NAMESPACE, { 0, 1, 1, 0, 0, 1, 0 } },
457 { "native", KEYWORD_NATIVE, { 0, 0, 0, 1, 0, 0, 0 } },
458 { "new", KEYWORD_NEW, { 0, 1, 1, 1, 0, 1, 1 } },
459 { "newcov", KEYWORD_NEWCOV, { 0, 0, 0, 0, 1, 0, 0 } },
460 { "noexcept", KEYWORD_NOEXCEPT, { 0, 1, 0, 0, 0, 0, 0 } },
461 { "operator", KEYWORD_OPERATOR, { 0, 1, 1, 0, 0, 0, 0 } },
462 { "out", KEYWORD_OUT, { 0, 0, 0, 0, 0, 1, 1 } },
463 { "output", KEYWORD_OUTPUT, { 0, 0, 0, 0, 1, 0, 0 } },
464 { "overload", KEYWORD_OVERLOAD, { 0, 1, 0, 0, 0, 0, 0 } },
465 { "override", KEYWORD_OVERRIDE, { 0, 0, 1, 0, 0, 1, 1 } },
466 { "package", KEYWORD_PACKAGE, { 0, 0, 0, 1, 0, 0, 1 } },
467 { "packed", KEYWORD_PACKED, { 0, 0, 0, 0, 1, 0, 0 } },
468 { "port", KEYWORD_PORT, { 0, 0, 0, 0, 1, 0, 0 } },
469 { "private", KEYWORD_PRIVATE, { 0, 1, 1, 1, 0, 1, 1 } },
470 { "program", KEYWORD_PROGRAM, { 0, 0, 0, 0, 1, 0, 0 } },
471 { "protected", KEYWORD_PROTECTED, { 0, 1, 1, 1, 1, 1, 1 } },
472 { "public", KEYWORD_PUBLIC, { 0, 1, 1, 1, 1, 1, 1 } },
473 { "ref", KEYWORD_REF, { 0, 0, 0, 0, 0, 1, 1 } },
474 { "register", KEYWORD_REGISTER, { 1, 1, 0, 0, 0, 0, 0 } },
475 { "return", KEYWORD_RETURN, { 1, 1, 1, 1, 0, 1, 1 } },
476 { "set", KEYWORD_SET, { 0, 0, 0, 0, 0, 1, 0 } },
477 { "shadow", KEYWORD_SHADOW, { 0, 0, 0, 0, 1, 0, 0 } },
478 { "short", KEYWORD_SHORT, { 1, 1, 1, 1, 0, 1, 1 } },
479 { "signal", KEYWORD_SIGNAL, { 0, 0, 0, 0, 0, 1, 0 } },
480 { "signed", KEYWORD_SIGNED, { 1, 1, 0, 0, 0, 0, 0 } },
481 { "size_t", KEYWORD_SIZE_T, { 0, 0, 0, 0, 0, 1, 0 } },
482 { "state", KEYWORD_STATE, { 0, 0, 0, 0, 1, 0, 0 } },
483 { "static", KEYWORD_STATIC, { 1, 1, 1, 1, 1, 1, 1 } },
484 { "static_assert", KEYWORD_STATIC_ASSERT, { 0, 1, 0, 0, 0, 0, 0 } },
485 { "string", KEYWORD_STRING, { 0, 0, 1, 0, 1, 1, 0 } },
486 { "struct", KEYWORD_STRUCT, { 1, 1, 1, 0, 0, 1, 1 } },
487 { "switch", KEYWORD_SWITCH, { 1, 1, 1, 1, 0, 1, 1 } },
488 { "synchronized", KEYWORD_SYNCHRONIZED, { 0, 0, 0, 1, 0, 0, 1 } },
489 { "task", KEYWORD_TASK, { 0, 0, 0, 0, 1, 0, 0 } },
490 { "template", KEYWORD_TEMPLATE, { 0, 1, 0, 0, 0, 0, 0 } },
491 { "template", KEYWORD_NAMESPACE, { 0, 0, 0, 0, 0, 0, 1 } }, /* parse block */
492 { "this", KEYWORD_THIS, { 0, 0, 1, 1, 0, 1, 0 } }, /* 0 to allow D ctor tags */
493 { "throw", KEYWORD_THROW, { 0, 1, 1, 1, 0, 1, 1 } },
494 { "throws", KEYWORD_THROWS, { 0, 0, 0, 1, 0, 1, 0 } },
495 { "trans", KEYWORD_TRANS, { 0, 0, 0, 0, 1, 0, 0 } },
496 { "transition", KEYWORD_TRANSITION, { 0, 0, 0, 0, 1, 0, 0 } },
497 { "transient", KEYWORD_TRANSIENT, { 0, 0, 0, 1, 0, 0, 0 } },
498 { "try", KEYWORD_TRY, { 0, 1, 1, 0, 0, 1, 1 } },
499 { "typedef", KEYWORD_TYPEDEF, { 1, 1, 1, 0, 1, 0, 1 } },
500 { "typename", KEYWORD_TYPENAME, { 0, 1, 0, 0, 0, 0, 0 } },
501 { "uint", KEYWORD_UINT, { 0, 0, 1, 0, 0, 1, 1 } },
502 { "ulong", KEYWORD_ULONG, { 0, 0, 1, 0, 0, 1, 1 } },
503 { "union", KEYWORD_UNION, { 1, 1, 0, 0, 0, 0, 1 } },
504 { "unsigned", KEYWORD_UNSIGNED, { 1, 1, 1, 0, 0, 0, 1 } },
505 { "ushort", KEYWORD_USHORT, { 0, 0, 1, 0, 0, 1, 1 } },
506 { "using", KEYWORD_USING, { 0, 1, 1, 0, 0, 1, 0 } },
507 { "virtual", KEYWORD_VIRTUAL, { 0, 1, 1, 0, 1, 1, 0 } },
508 { "void", KEYWORD_VOID, { 1, 1, 1, 1, 1, 1, 1 } },
509 { "volatile", KEYWORD_VOLATILE, { 1, 1, 1, 1, 0, 0, 1 } },
510 { "wchar_t", KEYWORD_WCHAR_T, { 0, 1, 1, 0, 0, 0, 0 } },
511 { "weak", KEYWORD_WEAK, { 0, 0, 0, 0, 0, 1, 0 } },
512 { "while", KEYWORD_WHILE, { 1, 1, 1, 1, 0, 1, 1 } }
517 * FUNCTION PROTOTYPES
519 static void createTags (const unsigned int nestLevel, statementInfo *const parent);
520 static void copyToken (tokenInfo *const dest, const tokenInfo *const src);
521 static const char *getVarType (const statementInfo *const st,
522 const tokenInfo *const token);
525 * FUNCTION DEFINITIONS
528 /* Debugging functions added by Biswa */
529 #if defined(DEBUG_C) && DEBUG_C
530 static char *tokenTypeName[] = {
531 "none", "args", "'}'", "'{'", "','", "'::'", "keyword", "name",
532 "package", "paren-name", "';'", "spec", "*", "[]", "count"
535 static char *tagScopeNames[] = {
536 "global", "static", "extern", "friend", "typedef", "count"};
538 static char *declTypeNames[] = {
539 "none", "base", "class", "enum", "function", "ignore", "interface",
540 "namespace", "nomangle", "package", "struct", "union", "count"};
542 static char *impTypeNames[] = {
543 "default", "abstract", "virtual", "pure-virtual", "count"};
545 void printToken(const tokenInfo *const token)
547 fprintf(stderr, "Type: %s, Keyword: %d, name: %s\n", tokenTypeName[token->type],
548 token->keyword, vStringValue(token->name));
551 void printTagEntry(const tagEntryInfo *tag)
553 fprintf(stderr, "Tag: %s (%s) [ impl: %s, scope: %s, type: %s\n", tag->name,
554 tag->kindName, tag->extensionFields.implementation, tag->extensionFields.scope[1],
555 tag->extensionFields.varType);
558 void printStatement(const statementInfo *const statement)
560 int i;
561 statementInfo *st = (statementInfo *) statement;
562 while (NULL != st)
564 fprintf(stderr, "Statement Info:\n------------------------\n");
565 fprintf(stderr, "scope: %s, decl: %s, impl: %s\n", tagScopeNames[st->scope],
566 declTypeNames[st->declaration], impTypeNames[st->implementation]);
567 for (i=0; i < NumTokens; ++i)
569 fprintf(stderr, "Token %d %s: ", i, (i == st->tokenIndex)?"(current)":"");
570 printToken(st->token[i]);
572 fprintf(stderr, "Context: ");
573 printToken(st->context);
574 fprintf(stderr, "Block: ");
575 printToken(st->blockName);
576 fprintf(stderr, "Parent classes: %s\n", vStringValue(st->parentClasses));
577 fprintf(stderr, "First token: ");
578 printToken(st->firstToken);
579 if (NULL != st->parent)
580 fprintf(stderr, "Printing Parent:\n");
581 st = st->parent;
583 fprintf(stderr, "-----------------------------------------------\n");
585 #endif
587 extern boolean includingDefineTags (void)
589 if (isLanguage(Lang_c) ||
590 isLanguage(Lang_cpp) ||
591 isLanguage(Lang_csharp) ||
592 isLanguage(Lang_ferite) ||
593 isLanguage(Lang_glsl) ||
594 isLanguage(Lang_vala))
595 return CKinds [CK_DEFINE].enabled;
597 return FALSE;
601 * Token management
604 static void initToken (tokenInfo* const token)
606 token->type = TOKEN_NONE;
607 token->keyword = KEYWORD_NONE;
608 token->lineNumber = getSourceLineNumber();
609 token->filePosition = getInputFilePosition();
610 vStringClear(token->name);
613 static void advanceToken (statementInfo* const st)
615 if (st->tokenIndex >= (unsigned int) NumTokens - 1)
616 st->tokenIndex = 0;
617 else
618 ++st->tokenIndex;
619 initToken(st->token[st->tokenIndex]);
622 static tokenInfo *prevToken (const statementInfo *const st, unsigned int n)
624 unsigned int tokenIndex;
625 unsigned int num = (unsigned int) NumTokens;
626 Assert(n < num);
627 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];
736 if (p->id == keyword)
738 name = p->name;
739 break;
742 return name;
745 static void UNUSED pt (tokenInfo *const token)
747 if (isType (token, TOKEN_NAME))
748 printf("type: %-12s: %-13s line: %lu\n",
749 tokenString (token->type), vStringValue (token->name),
750 token->lineNumber);
751 else if (isType (token, TOKEN_KEYWORD))
752 printf("type: %-12s: %-13s line: %lu\n",
753 tokenString (token->type), keywordString (token->keyword),
754 token->lineNumber);
755 else
756 printf("type: %-12s line: %lu\n",
757 tokenString (token->type), token->lineNumber);
760 static void UNUSED ps (statementInfo *const st)
762 unsigned int i;
763 printf("scope: %s decl: %s gotName: %s gotParenName: %s\n",
764 scopeString (st->scope), declString (st->declaration),
765 boolString (st->gotName), boolString (st->gotParenName));
766 printf("haveQualifyingName: %s\n", boolString (st->haveQualifyingName));
767 printf("access: %s default: %s\n", accessString (st->member.access),
768 accessString (st->member.accessDefault));
769 printf("token : ");
770 pt(activeToken (st));
771 for (i = 1 ; i < (unsigned int) NumTokens ; ++i)
773 printf("prev %u : ", i);
774 pt(prevToken (st, i));
776 printf("context: ");
777 pt(st->context);
780 #endif
783 * Statement management
786 static boolean isDataTypeKeyword (const tokenInfo *const token)
788 switch (token->keyword)
790 case KEYWORD_BOOLEAN:
791 case KEYWORD_BYTE:
792 case KEYWORD_CHAR:
793 case KEYWORD_DOUBLE:
794 case KEYWORD_FLOAT:
795 case KEYWORD_INT:
796 case KEYWORD_LONG:
797 case KEYWORD_SHORT:
798 case KEYWORD_VOID:
799 case KEYWORD_WCHAR_T:
800 case KEYWORD_SIZE_T:
801 return TRUE;
802 default:
803 return FALSE;
807 #if 0
808 static boolean isVariableKeyword (const tokenInfo *const token)
810 switch (token->keyword)
812 case KEYWORD_CONST:
813 case KEYWORD_EXTERN:
814 case KEYWORD_REGISTER:
815 case KEYWORD_STATIC:
816 case KEYWORD_VIRTUAL:
817 case KEYWORD_SIGNED:
818 case KEYWORD_UNSIGNED:
819 return TRUE;
820 default:
821 return FALSE;
824 #endif
826 static boolean isContextualKeyword (const tokenInfo *const token)
828 boolean result;
829 switch (token->keyword)
831 case KEYWORD_CLASS:
832 case KEYWORD_ENUM:
833 case KEYWORD_INTERFACE:
834 case KEYWORD_NAMESPACE:
835 case KEYWORD_STRUCT:
836 case KEYWORD_UNION:
838 result = TRUE;
839 break;
842 default:
844 result = FALSE;
845 break;
848 return result;
851 static boolean isContextualStatement (const statementInfo *const st)
853 boolean result = FALSE;
855 if (st != NULL)
857 if (isLanguage (Lang_vala))
859 /* All can be a contextual statement as properties can be of any type */
860 result = TRUE;
862 else
864 switch (st->declaration)
866 case DECL_CLASS:
867 case DECL_ENUM:
868 case DECL_INTERFACE:
869 case DECL_NAMESPACE:
870 case DECL_STRUCT:
871 case DECL_UNION:
873 result = TRUE;
874 break;
877 default:
879 result = FALSE;
880 break;
885 return result;
888 static boolean isMember (const statementInfo *const st)
890 boolean result;
891 if (isType (st->context, TOKEN_NAME))
892 result = TRUE;
893 else
894 result = isContextualStatement (st->parent);
895 return result;
898 static void initMemberInfo (statementInfo *const st)
900 accessType accessDefault = ACCESS_UNDEFINED;
902 if (st->parent != NULL) switch (st->parent->declaration)
904 case DECL_ENUM:
905 case DECL_NAMESPACE:
907 accessDefault = ACCESS_UNDEFINED;
908 break;
910 case DECL_CLASS:
912 if (isLanguage (Lang_java))
913 accessDefault = ACCESS_DEFAULT;
914 else
915 accessDefault = ACCESS_PRIVATE;
916 break;
918 case DECL_INTERFACE:
919 case DECL_STRUCT:
920 case DECL_UNION:
922 accessDefault = ACCESS_PUBLIC;
923 break;
925 default:
926 break;
928 st->member.accessDefault = accessDefault;
929 st->member.access = accessDefault;
932 static void reinitStatement (statementInfo *const st, const boolean partial)
934 unsigned int i;
936 if (! partial)
938 st->scope = SCOPE_GLOBAL;
939 if (isContextualStatement (st->parent))
940 st->declaration = DECL_BASE;
941 else
942 st->declaration = DECL_NONE;
944 st->gotParenName = FALSE;
945 st->implementation = IMP_DEFAULT;
946 st->gotArgs = FALSE;
947 st->gotName = FALSE;
948 st->nSemicolons = 0;
949 st->haveQualifyingName = FALSE;
950 st->argEndPosition = 0;
952 st->tokenIndex = 0;
953 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
955 initToken (st->token [i]);
958 initToken (st->context);
959 initToken (st->blockName);
960 vStringClear (st->parentClasses);
962 /* Init member info. */
963 if (! partial)
964 st->member.access = st->member.accessDefault;
966 /* Init first token */
967 if (!partial)
968 initToken(st->firstToken);
971 static void reinitStatementWithToken (statementInfo *const st,
972 tokenInfo *token, const boolean partial)
974 tokenInfo *const save = newToken ();
975 /* given token can be part of reinit statementInfo */
976 copyToken (save, token);
977 reinitStatement (st, partial);
978 token = activeToken (st);
979 copyToken (token, save);
980 deleteToken (save);
981 ++st->tokenIndex; /* this is quite safe because current tokenIndex = 0 */
984 static void initStatement (statementInfo *const st, statementInfo *const parent)
986 st->parent = parent;
987 initMemberInfo (st);
988 reinitStatement (st, FALSE);
989 if (parent)
991 const tokenInfo *const src = activeToken (parent);
992 tokenInfo *const dst = activeToken (st);
993 copyToken (dst, src);
994 st->tokenIndex++;
999 * Tag generation functions
1001 static cKind cTagKind (const tagType type)
1003 cKind result = CK_UNDEFINED;
1004 switch (type)
1006 case TAG_CLASS: result = CK_CLASS; break;
1007 case TAG_ENUM: result = CK_ENUMERATION; break;
1008 case TAG_ENUMERATOR: result = CK_ENUMERATOR; break;
1009 case TAG_FUNCTION: result = CK_FUNCTION; break;
1010 case TAG_MEMBER: result = CK_MEMBER; break;
1011 case TAG_NAMESPACE: result = CK_NAMESPACE; break;
1012 case TAG_PROTOTYPE: result = CK_PROTOTYPE; break;
1013 case TAG_STRUCT: result = CK_STRUCT; break;
1014 case TAG_TYPEDEF: result = CK_TYPEDEF; break;
1015 case TAG_UNION: result = CK_UNION; break;
1016 case TAG_VARIABLE: result = CK_VARIABLE; break;
1017 case TAG_EXTERN_VAR: result = CK_EXTERN_VARIABLE; break;
1019 default: Assert ("Bad C tag type" == NULL); break;
1021 return result;
1024 static csharpKind csharpTagKind (const tagType type)
1026 csharpKind result = CSK_UNDEFINED;
1027 switch (type)
1029 case TAG_CLASS: result = CSK_CLASS; break;
1030 case TAG_ENUM: result = CSK_ENUMERATION; break;
1031 case TAG_ENUMERATOR: result = CSK_ENUMERATOR; break;
1032 case TAG_EVENT: result = CSK_EVENT; break;
1033 case TAG_FIELD: result = CSK_FIELD ; break;
1034 case TAG_INTERFACE: result = CSK_INTERFACE; break;
1035 case TAG_LOCAL: result = CSK_LOCAL; break;
1036 case TAG_METHOD: result = CSK_METHOD; break;
1037 case TAG_NAMESPACE: result = CSK_NAMESPACE; break;
1038 case TAG_PROPERTY: result = CSK_PROPERTY; break;
1039 case TAG_STRUCT: result = CSK_STRUCT; break;
1040 case TAG_TYPEDEF: result = CSK_TYPEDEF; break;
1042 default: Assert ("Bad C# tag type" == NULL); break;
1044 return result;
1047 static dKind dTagKind (const tagType type)
1049 dKind result = DK_UNDEFINED;
1050 switch (type)
1052 case TAG_CLASS: result = DK_CLASS; break;
1053 case TAG_ENUM: result = DK_ENUMERATION; break;
1054 case TAG_ENUMERATOR: result = DK_ENUMERATOR; break;
1055 case TAG_FUNCTION: result = DK_FUNCTION; break;
1056 case TAG_INTERFACE: result = DK_INTERFACE; break;
1057 case TAG_MEMBER: result = DK_MEMBER; break;
1058 case TAG_NAMESPACE: result = DK_NAMESPACE; break;
1059 case TAG_PROTOTYPE: result = DK_PROTOTYPE; break;
1060 case TAG_STRUCT: result = DK_STRUCT; break;
1061 case TAG_TYPEDEF: result = DK_TYPEDEF; break;
1062 case TAG_UNION: result = DK_UNION; break;
1063 case TAG_VARIABLE: result = DK_VARIABLE; break;
1064 case TAG_EXTERN_VAR: result = DK_EXTERN_VARIABLE; break;
1066 default: Assert ("Bad D tag type" == NULL); break;
1068 return result;
1071 static valaKind valaTagKind (const tagType type)
1073 valaKind result = VK_UNDEFINED;
1074 switch (type)
1076 case TAG_CLASS: result = VK_CLASS; break;
1077 case TAG_ENUM: result = VK_ENUMERATION; break;
1078 case TAG_ENUMERATOR: result = VK_ENUMERATOR; break;
1079 case TAG_SIGNAL: result = VK_SIGNAL; break;
1080 case TAG_FIELD: result = VK_FIELD ; break;
1081 case TAG_INTERFACE: result = VK_INTERFACE; break;
1082 case TAG_LOCAL: result = VK_LOCAL; break;
1083 case TAG_METHOD: result = VK_METHOD; break;
1084 case TAG_NAMESPACE: result = VK_NAMESPACE; break;
1085 case TAG_PROPERTY: result = VK_PROPERTY; break;
1086 case TAG_STRUCT: result = VK_STRUCT; break;
1088 default: Assert ("Bad Vala tag type" == NULL); break;
1090 return result;
1093 static javaKind javaTagKind (const tagType type)
1095 javaKind result = JK_UNDEFINED;
1096 switch (type)
1098 case TAG_CLASS: result = JK_CLASS; break;
1099 case TAG_FIELD: result = JK_FIELD; break;
1100 case TAG_INTERFACE: result = JK_INTERFACE; break;
1101 case TAG_METHOD: result = JK_METHOD; break;
1102 case TAG_PACKAGE: result = JK_PACKAGE; break;
1103 case TAG_ENUM: result = JK_ENUMERATION; break;
1104 case TAG_ENUMERATOR: result = JK_ENUMERATOR; break;
1106 default: Assert ("Bad Java tag type" == NULL); break;
1108 return result;
1111 static const char *tagName (const tagType type)
1113 const char* result;
1114 if (isLanguage (Lang_java))
1115 result = JavaKinds [javaTagKind (type)].name;
1116 else if (isLanguage (Lang_csharp))
1117 result = CsharpKinds [csharpTagKind (type)].name;
1118 else if (isLanguage (Lang_d))
1119 result = DKinds [dTagKind (type)].name;
1120 else if (isLanguage (Lang_vala))
1121 result = ValaKinds [valaTagKind (type)].name;
1122 else
1123 result = CKinds [cTagKind (type)].name;
1124 return result;
1127 static int tagLetter (const tagType type)
1129 int result;
1130 if (isLanguage (Lang_csharp))
1131 result = CsharpKinds [csharpTagKind (type)].letter;
1132 else if (isLanguage (Lang_d))
1133 result = DKinds [dTagKind (type)].letter;
1134 else if (isLanguage (Lang_java))
1135 result = JavaKinds [javaTagKind (type)].letter;
1136 else if (isLanguage (Lang_vala))
1137 result = ValaKinds [valaTagKind (type)].letter;
1138 else
1139 result = CKinds [cTagKind (type)].letter;
1140 return result;
1144 static boolean includeTag (const tagType type, const boolean isFileScope)
1146 boolean result;
1147 if (isFileScope && ! Option.include.fileScope)
1148 result = FALSE;
1149 else if (isLanguage (Lang_java))
1150 result = JavaKinds [javaTagKind (type)].enabled;
1151 else
1152 result = CKinds [cTagKind (type)].enabled;
1153 return result;
1157 static tagType declToTagType (const declType declaration)
1159 tagType type = TAG_UNDEFINED;
1161 switch (declaration)
1163 case DECL_CLASS: type = TAG_CLASS; break;
1164 case DECL_ENUM: type = TAG_ENUM; break;
1165 case DECL_FUNCTION: type = TAG_FUNCTION; break;
1166 case DECL_FUNCTION_TEMPLATE: type = TAG_FUNCTION; break;
1167 case DECL_INTERFACE:type = TAG_INTERFACE; break;
1168 case DECL_NAMESPACE:type = TAG_NAMESPACE; break;
1169 case DECL_STRUCT: type = TAG_STRUCT; break;
1170 case DECL_UNION: type = TAG_UNION; break;
1172 default: Assert ("Unexpected declaration" == NULL); break;
1174 return type;
1177 static const char* accessField (const statementInfo *const st)
1179 const char* result = NULL;
1181 if ((isLanguage (Lang_cpp) || isLanguage (Lang_d) || isLanguage (Lang_ferite)) &&
1182 st->scope == SCOPE_FRIEND)
1183 result = "friend";
1184 else if (st->member.access != ACCESS_UNDEFINED)
1185 result = accessString (st->member.access);
1186 return result;
1189 static void addOtherFields (tagEntryInfo* const tag, const tagType type,
1190 const tokenInfo *const nameToken,
1191 const statementInfo *const st, vString *const scope)
1193 /* For selected tag types, append an extension flag designating the
1194 * parent object in which the tag is defined.
1196 switch (type)
1198 default: break;
1200 case TAG_NAMESPACE:
1201 case TAG_CLASS:
1202 case TAG_ENUM:
1203 case TAG_ENUMERATOR:
1204 case TAG_FIELD:
1205 case TAG_FUNCTION:
1206 case TAG_INTERFACE:
1207 case TAG_MEMBER:
1208 case TAG_METHOD:
1209 case TAG_PROTOTYPE:
1210 case TAG_STRUCT:
1211 case TAG_TYPEDEF:
1212 case TAG_UNION:
1214 if (vStringLength (scope) > 0 &&
1215 (isMember (st) || st->parent->declaration == DECL_NAMESPACE))
1217 if (isType (st->context, TOKEN_NAME))
1218 tag->extensionFields.scope [0] = tagName (TAG_CLASS);
1219 else
1220 tag->extensionFields.scope [0] =
1221 tagName (declToTagType (parentDecl (st)));
1222 tag->extensionFields.scope [1] = vStringValue (scope);
1224 if ((type == TAG_CLASS || type == TAG_INTERFACE ||
1225 type == TAG_STRUCT) && vStringLength (st->parentClasses) > 0)
1227 tag->extensionFields.inheritance =
1228 vStringValue (st->parentClasses);
1230 if (st->implementation != IMP_DEFAULT &&
1231 (isLanguage (Lang_cpp) || isLanguage (Lang_csharp) || isLanguage (Lang_vala) ||
1232 isLanguage (Lang_java) || isLanguage (Lang_d) || isLanguage (Lang_ferite)))
1234 tag->extensionFields.implementation =
1235 implementationString (st->implementation);
1237 if (isMember (st))
1239 tag->extensionFields.access = accessField (st);
1241 if ((TRUE == st->gotArgs) && (TRUE == Option.extensionFields.argList) &&
1242 ((TAG_FUNCTION == type) || (TAG_METHOD == type) || (TAG_PROTOTYPE == type)))
1244 tag->extensionFields.signature = getArglistFromFilePos(
1245 tag->filePosition, tag->name);
1247 break;
1251 if ((TAG_FIELD == type) || (TAG_MEMBER == type) ||
1252 (TAG_EXTERN_VAR == type) || (TAG_TYPEDEF == type) ||
1253 (TAG_VARIABLE == type) || (TAG_METHOD == type) ||
1254 (TAG_PROTOTYPE == type) || (TAG_FUNCTION == type))
1256 if (((TOKEN_NAME == st->firstToken->type) || isDataTypeKeyword(st->firstToken))
1257 && (0 != strcmp(vStringValue(st->firstToken->name), tag->name)))
1259 tag->extensionFields.varType = getVarType(st, nameToken);
1264 static const char *getVarType (const statementInfo *const st,
1265 const tokenInfo *const nameToken)
1267 static vString *vt = NULL;
1268 unsigned int i;
1269 unsigned int end = st->tokenIndex;
1270 boolean seenType = FALSE;
1272 switch (st->declaration) {
1273 case DECL_BASE:
1274 case DECL_FUNCTION:
1275 case DECL_FUNCTION_TEMPLATE:
1276 break;
1277 default:
1278 return vStringValue(st->firstToken->name);
1281 if (vt == NULL)
1282 vt = vStringNew();
1283 else
1284 vStringClear(vt);
1286 /* find the end of the type signature in the token list */
1287 for (i = 0; i < st->tokenIndex; i++)
1289 const tokenInfo *const t = st->token[i];
1291 /* stop if we find the token used to generate the tag name, or
1292 * a name token in the middle yet not preceded by a scope separator */
1293 if ((t == nameToken ||
1294 (t->type == nameToken->type &&
1295 t->keyword == nameToken->keyword &&
1296 t->lineNumber == nameToken->lineNumber &&
1297 strcmp(vStringValue(t->name), vStringValue(nameToken->name)) == 0)) ||
1298 (t->type == TOKEN_NAME && seenType &&
1299 (i > 0 && st->token[i - 1]->type != TOKEN_DOUBLE_COLON)))
1301 break;
1303 if (t->type != TOKEN_DOUBLE_COLON)
1304 end = i + 1;
1305 if (t->type == TOKEN_NAME)
1306 seenType = TRUE;
1307 else if (t->type == TOKEN_KEYWORD && isDataTypeKeyword(t))
1308 seenType = TRUE;
1311 /* ugly historic workaround when we can't figure out the type */
1312 if (end < 2 && ! st->gotArgs)
1313 return vStringValue(st->firstToken->name);
1315 for (i = 0; i < end; i++)
1317 tokenInfo *t = st->token[i];
1319 switch (t->type)
1321 case TOKEN_NAME: /* user typename */
1322 break;
1323 case TOKEN_KEYWORD:
1324 if ((t->keyword != KEYWORD_EXTERN && t->keyword != KEYWORD_STATIC) && /* uninteresting keywords */
1325 (st->gotArgs ||
1326 /* ignore uninteresting keywords for non-functions */
1327 (t->keyword != KEYWORD_PUBLIC &&
1328 t->keyword != KEYWORD_PRIVATE &&
1329 t->keyword != KEYWORD_PROTECTED &&
1330 t->keyword != KEYWORD_FINAL &&
1331 t->keyword != KEYWORD_TYPEDEF &&
1332 /* hack for D static conditions */
1333 t->keyword != KEYWORD_IF)))
1335 break;
1337 continue;
1338 case TOKEN_STAR: vStringCatS(vt, " *"); continue;
1339 case TOKEN_ARRAY: vStringCatS(vt, "[]"); continue;
1340 case TOKEN_DOUBLE_COLON:
1341 vStringCatS(vt, "::");
1342 continue;
1343 default: continue;
1345 if (vStringLength(vt) > 0)
1346 if (isalpha(vStringValue(vt)[vStringLength(vt) - 1]))
1347 vStringPut(vt, ' ');
1348 vStringCat(vt, t->name);
1350 vStringTerminate(vt);
1351 return vStringValue(vt);
1354 static void addContextSeparator (vString *const scope)
1356 if (isLanguage (Lang_c) || isLanguage (Lang_cpp))
1357 vStringCatS (scope, "::");
1358 else if (isLanguage (Lang_java) || isLanguage (Lang_d) || isLanguage (Lang_ferite) ||
1359 isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1360 vStringCatS (scope, ".");
1363 static void findScopeHierarchy (vString *const string,
1364 const statementInfo *const st)
1366 const char* const anon = "<anonymous>";
1367 boolean nonAnonPresent = FALSE;
1369 vStringClear (string);
1370 if (isType (st->context, TOKEN_NAME))
1372 vStringCopy (string, st->context->name);
1373 nonAnonPresent = TRUE;
1375 if (st->parent != NULL)
1377 vString *temp = vStringNew ();
1378 const statementInfo *s;
1380 for (s = st->parent ; s != NULL ; s = s->parent)
1382 if (isContextualStatement (s) ||
1383 s->declaration == DECL_NAMESPACE)
1385 vStringCopy (temp, string);
1386 vStringClear (string);
1387 if (isType (s->blockName, TOKEN_NAME))
1389 if (isType (s->context, TOKEN_NAME) &&
1390 vStringLength (s->context->name) > 0)
1392 vStringCat (string, s->context->name);
1393 addContextSeparator (string);
1395 vStringCat (string, s->blockName->name);
1396 nonAnonPresent = TRUE;
1398 else
1399 vStringCopyS (string, anon);
1400 if (vStringLength (temp) > 0)
1401 addContextSeparator (string);
1402 vStringCat (string, temp);
1405 vStringDelete (temp);
1407 if (! nonAnonPresent)
1408 vStringClear (string);
1412 static void makeExtraTagEntry (const tagType type, tagEntryInfo *const e,
1413 vString *const scope)
1415 if (Option.include.qualifiedTags &&
1416 scope != NULL && vStringLength (scope) > 0)
1418 vString *const scopedName = vStringNew ();
1420 if (type != TAG_ENUMERATOR)
1421 vStringCopy (scopedName, scope);
1422 else
1424 /* remove last component (i.e. enumeration name) from scope */
1425 const char* const sc = vStringValue (scope);
1426 const char* colon = strrchr (sc, ':');
1427 if (colon != NULL)
1429 while (*colon == ':' && colon > sc)
1430 --colon;
1431 vStringNCopy (scopedName, scope, colon + 1 - sc);
1434 if (vStringLength (scopedName) > 0)
1436 addContextSeparator (scopedName);
1437 vStringCatS (scopedName, e->name);
1438 e->name = vStringValue (scopedName);
1439 makeTagEntry (e);
1441 vStringDelete (scopedName);
1445 static void makeTag (const tokenInfo *const token,
1446 const statementInfo *const st,
1447 boolean isFileScope, const tagType type)
1449 #ifdef DEBUG_C
1450 printToken(token);
1451 fprintf(stderr, "<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>\n");
1452 printStatement(st);
1453 #endif
1454 /* Nothing is really of file scope when it appears in a header file.
1456 isFileScope = (boolean) (isFileScope && ! isHeaderFile ());
1458 if (isType (token, TOKEN_NAME) && vStringLength (token->name) > 0 /* &&
1459 includeTag (type, isFileScope) */)
1461 vString *scope = vStringNew ();
1462 tagEntryInfo e;
1464 /* take only functions which are introduced by "function ..." */
1465 if (type == TAG_FUNCTION && isLanguage (Lang_ferite) &&
1466 strncmp("function", st->firstToken->name->buffer, 8) != 0)
1468 return;
1471 initTagEntry (&e, vStringValue (token->name));
1473 e.lineNumber = token->lineNumber;
1474 e.filePosition = token->filePosition;
1475 e.isFileScope = isFileScope;
1476 e.kindName = tagName (type);
1477 e.kind = tagLetter (type);
1479 findScopeHierarchy (scope, st);
1480 addOtherFields (&e, type, token, st, scope);
1482 #ifdef DEBUG_C
1483 printTagEntry(&e);
1484 #endif
1485 makeTagEntry (&e);
1486 if (NULL != TagEntryFunction)
1487 makeExtraTagEntry (type, &e, scope);
1488 vStringDelete (scope);
1489 if (NULL != e.extensionFields.signature)
1490 free((char *) e.extensionFields.signature);
1494 static boolean isValidTypeSpecifier (const declType declaration)
1496 boolean result;
1497 switch (declaration)
1499 case DECL_BASE:
1500 case DECL_CLASS:
1501 case DECL_ENUM:
1502 case DECL_STRUCT:
1503 case DECL_UNION:
1504 result = TRUE;
1505 break;
1507 default:
1508 result = FALSE;
1509 break;
1511 return result;
1514 static void qualifyEnumeratorTag (const statementInfo *const st,
1515 const tokenInfo *const nameToken)
1517 if (isType (nameToken, TOKEN_NAME))
1518 makeTag (nameToken, st, TRUE, TAG_ENUMERATOR);
1521 static void qualifyFunctionTag (const statementInfo *const st,
1522 const tokenInfo *const nameToken)
1524 if (isType (nameToken, TOKEN_NAME))
1526 const tagType type = (isLanguage (Lang_java) || isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1527 ? TAG_METHOD : TAG_FUNCTION;
1528 const boolean isFileScope =
1529 (boolean) (st->member.access == ACCESS_PRIVATE ||
1530 (!isMember (st) && st->scope == SCOPE_STATIC));
1532 makeTag (nameToken, st, isFileScope, type);
1536 static void qualifyFunctionDeclTag (const statementInfo *const st,
1537 const tokenInfo *const nameToken)
1539 if (! isType (nameToken, TOKEN_NAME))
1541 else if (isLanguage (Lang_java) || isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1542 qualifyFunctionTag (st, nameToken);
1543 else if (st->scope == SCOPE_TYPEDEF)
1544 makeTag (nameToken, st, TRUE, TAG_TYPEDEF);
1545 else if (isValidTypeSpecifier (st->declaration) &&
1546 ! (isLanguage (Lang_csharp) || isLanguage (Lang_vala)))
1547 makeTag (nameToken, st, TRUE, TAG_PROTOTYPE);
1550 static void qualifyCompoundTag (const statementInfo *const st,
1551 const tokenInfo *const nameToken)
1553 if (isType (nameToken, TOKEN_NAME))
1555 const tagType type = declToTagType (st->declaration);
1557 if (type != TAG_UNDEFINED)
1558 makeTag (nameToken, st, (boolean) (! isLanguage (Lang_java) &&
1559 ! isLanguage (Lang_csharp) &&
1560 ! isLanguage (Lang_vala)), type);
1564 static void qualifyBlockTag (statementInfo *const st,
1565 const tokenInfo *const nameToken)
1567 switch (st->declaration)
1569 case DECL_CLASS:
1570 case DECL_ENUM:
1571 case DECL_INTERFACE:
1572 case DECL_NAMESPACE:
1573 case DECL_STRUCT:
1574 case DECL_UNION:
1575 qualifyCompoundTag (st, nameToken);
1576 break;
1577 default: break;
1581 static void qualifyVariableTag (const statementInfo *const st,
1582 const tokenInfo *const nameToken)
1584 /* We have to watch that we do not interpret a declaration of the
1585 * form "struct tag;" as a variable definition. In such a case, the
1586 * token preceding the name will be a keyword.
1588 if (! isType (nameToken, TOKEN_NAME))
1590 else if (st->declaration == DECL_IGNORE)
1592 else if (st->scope == SCOPE_TYPEDEF)
1593 makeTag (nameToken, st, TRUE, TAG_TYPEDEF);
1594 else if (st->declaration == DECL_PACKAGE)
1595 makeTag (nameToken, st, FALSE, TAG_PACKAGE);
1596 else if (st->declaration == DECL_MODULE) /* handle modules in D as namespaces */
1597 makeTag (nameToken, st, FALSE, TAG_NAMESPACE);
1598 else if (isValidTypeSpecifier (st->declaration))
1600 if (isMember (st))
1602 if (isLanguage (Lang_java) || isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1603 makeTag (nameToken, st, (boolean) (st->member.access == ACCESS_PRIVATE), TAG_FIELD);
1604 else if (st->scope == SCOPE_GLOBAL || st->scope == SCOPE_STATIC)
1605 makeTag (nameToken, st, TRUE, TAG_MEMBER);
1607 else if (isLanguage (Lang_java) || isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1609 else
1611 if (st->scope == SCOPE_EXTERN || ! st->haveQualifyingName)
1612 makeTag (nameToken, st, FALSE, TAG_EXTERN_VAR);
1613 else
1614 makeTag (nameToken, st, (boolean) (st->scope == SCOPE_STATIC), TAG_VARIABLE);
1620 * Parsing functions
1623 static int skipToOneOf (const char *const chars)
1625 int c;
1627 c = cppGetc ();
1628 while (c != EOF && c != '\0' && strchr (chars, c) == NULL);
1630 return c;
1633 /* Skip to the next non-white character.
1635 static int skipToNonWhite (void)
1637 int c;
1641 c = cppGetc ();
1643 while (isspace (c));
1645 return c;
1648 /* Skips to the next brace in column 1. This is intended for cases where
1649 * preprocessor constructs result in unbalanced braces.
1651 static void skipToFormattedBraceMatch (void)
1653 int c, next;
1655 c = cppGetc ();
1656 next = cppGetc ();
1657 while (c != EOF && (c != '\n' || next != '}'))
1659 c = next;
1660 next = cppGetc ();
1664 /* Skip to the matching character indicated by the pair string. If skipping
1665 * to a matching brace and any brace is found within a different level of a
1666 * #if conditional statement while brace formatting is in effect, we skip to
1667 * the brace matched by its formatting. It is assumed that we have already
1668 * read the character which starts the group (i.e. the first character of
1669 * "pair").
1671 static void skipToMatch (const char *const pair)
1673 const boolean braceMatching = (boolean) (strcmp ("{}", pair) == 0);
1674 const boolean braceFormatting = (boolean) (isBraceFormat () && braceMatching);
1675 const unsigned int initialLevel = getDirectiveNestLevel ();
1676 const int begin = pair [0], end = pair [1];
1677 const unsigned long inputLineNumber = getInputLineNumber ();
1678 int matchLevel = 1;
1679 int c = '\0';
1680 if (isLanguage(Lang_d) && pair[0] == '<')
1681 return; /* ignore e.g. Foo!(x < 2) */
1682 while (matchLevel > 0 && (c = cppGetc ()) != EOF)
1684 if (c == begin)
1686 ++matchLevel;
1687 if (braceFormatting && getDirectiveNestLevel () != initialLevel)
1689 skipToFormattedBraceMatch ();
1690 break;
1693 else if (c == end)
1695 --matchLevel;
1696 if (braceFormatting && getDirectiveNestLevel () != initialLevel)
1698 skipToFormattedBraceMatch ();
1699 break;
1702 /* early out if matching "<>" and we encounter a ";" or "{" to mitigate
1703 * match problems with C++ generics containing a static expression like
1704 * foo<X<Y> bar;
1705 * normally neither ";" nor "{" could appear inside "<>" anyway. */
1706 else if (isLanguage (Lang_cpp) && begin == '<' &&
1707 (c == ';' || c == '{'))
1709 cppUngetc (c);
1710 break;
1713 if (c == EOF)
1715 verbose ("%s: failed to find match for '%c' at line %lu\n",
1716 getInputFileName (), begin, inputLineNumber);
1717 if (braceMatching)
1718 longjmp (Exception, (int) ExceptionBraceFormattingError);
1719 else
1720 longjmp (Exception, (int) ExceptionFormattingError);
1724 static void skipParens (void)
1726 const int c = skipToNonWhite ();
1728 if (c == '(')
1729 skipToMatch ("()");
1730 else
1731 cppUngetc (c);
1734 static void skipBraces (void)
1736 const int c = skipToNonWhite ();
1738 if (c == '{')
1739 skipToMatch ("{}");
1740 else
1741 cppUngetc (c);
1744 static keywordId analyzeKeyword (const char *const name)
1746 const keywordId id = (keywordId) lookupKeyword (name, getSourceLanguage ());
1748 /* ignore D @attributes and Java @annotations(...), but show them in function signatures */
1749 if ((isLanguage(Lang_d) || isLanguage(Lang_java)) && id == KEYWORD_NONE && name[0] == '@')
1751 skipParens(); /* if annotation has parameters, skip them */
1752 return KEYWORD_CONST;
1754 return id;
1757 static void analyzeIdentifier (tokenInfo *const token)
1759 char *const name = vStringValue (token->name);
1760 const char *replacement = NULL;
1761 boolean parensToo = FALSE;
1763 if (isLanguage (Lang_java) ||
1764 ! isIgnoreToken (name, &parensToo, &replacement))
1766 if (replacement != NULL)
1767 token->keyword = analyzeKeyword (replacement);
1768 else
1769 token->keyword = analyzeKeyword (vStringValue (token->name));
1771 if (token->keyword == KEYWORD_NONE)
1772 token->type = TOKEN_NAME;
1773 else
1774 token->type = TOKEN_KEYWORD;
1776 else
1778 initToken (token);
1779 if (parensToo)
1781 int c = skipToNonWhite ();
1783 if (c == '(')
1784 skipToMatch ("()");
1789 static void readIdentifier (tokenInfo *const token, const int firstChar)
1791 vString *const name = token->name;
1792 int c = firstChar;
1794 initToken (token);
1796 /* Bug #1585745 (CTags): strangely, C++ destructors allow whitespace between
1797 * the ~ and the class name. */
1798 if (isLanguage (Lang_cpp) && firstChar == '~')
1800 vStringPut (name, c);
1801 c = skipToNonWhite ();
1806 vStringPut (name, c);
1807 c = cppGetc ();
1808 } while (isident (c) || (isLanguage (Lang_vala) && '.' == c));
1809 vStringTerminate (name);
1810 cppUngetc (c); /* unget non-identifier character */
1812 /* Vala supports '?' at end of a type (with or without whitespace before) for nullable types */
1813 if (isLanguage (Lang_vala))
1815 c = skipToNonWhite ();
1816 if ('?' == c)
1817 vStringPut (name, c);
1818 else
1819 cppUngetc (c);
1822 analyzeIdentifier (token);
1825 static void readPackageName (tokenInfo *const token, const int firstChar)
1827 vString *const name = token->name;
1828 int c = firstChar;
1830 initToken (token);
1832 while (isident (c) || c == '.')
1834 vStringPut (name, c);
1835 c = cppGetc ();
1837 vStringTerminate (name);
1838 cppUngetc (c); /* unget non-package character */
1841 static void readPackageOrNamespace (statementInfo *const st, const declType declaration)
1843 st->declaration = declaration;
1845 if (declaration == DECL_NAMESPACE && !(isLanguage (Lang_csharp) || isLanguage (Lang_vala)))
1847 /* In C++ a namespace is specified one level at a time. */
1848 return;
1850 else
1852 /* In C#, a namespace can also be specified like a Java package name. */
1853 tokenInfo *const token = activeToken (st);
1854 Assert (isType (token, TOKEN_KEYWORD));
1855 readPackageName (token, skipToNonWhite ());
1856 token->type = TOKEN_NAME;
1857 st->gotName = TRUE;
1858 st->haveQualifyingName = TRUE;
1862 static void readPackage (statementInfo *const st)
1864 tokenInfo *const token = activeToken (st);
1865 Assert (isType (token, TOKEN_KEYWORD));
1866 readPackageName (token, skipToNonWhite ());
1867 token->type = TOKEN_NAME;
1868 if (isLanguage (Lang_d))
1869 st->declaration = DECL_MODULE;
1870 else
1871 st->declaration = DECL_PACKAGE;
1872 st->gotName = TRUE;
1873 st->haveQualifyingName = TRUE;
1876 static void processName (statementInfo *const st)
1878 Assert (isType (activeToken (st), TOKEN_NAME));
1879 if (st->gotName && st->declaration == DECL_NONE)
1880 st->declaration = DECL_BASE;
1881 st->gotName = TRUE;
1882 st->haveQualifyingName = TRUE;
1885 static void readOperator (statementInfo *const st)
1887 const char *const acceptable = "+-*/%^&|~!=<>,[]";
1888 const tokenInfo* const prev = prevToken (st,1);
1889 tokenInfo *const token = activeToken (st);
1890 vString *const name = token->name;
1891 int c = skipToNonWhite ();
1893 /* When we arrive here, we have the keyword "operator" in 'name'.
1895 if (isType (prev, TOKEN_KEYWORD) && (prev->keyword == KEYWORD_ENUM ||
1896 prev->keyword == KEYWORD_STRUCT || prev->keyword == KEYWORD_UNION))
1897 ; /* ignore "operator" keyword if preceded by these keywords */
1898 else if (c == '(')
1900 /* Verify whether this is a valid function call (i.e. "()") operator.
1902 if (cppGetc () == ')')
1904 vStringPut (name, ' '); /* always separate operator from keyword */
1905 c = skipToNonWhite ();
1906 if (c == '(')
1907 vStringCatS (name, "()");
1909 else
1911 skipToMatch ("()");
1912 c = cppGetc ();
1915 else if (isident1 (c))
1917 /* Handle "new" and "delete" operators, and conversion functions
1918 * (per 13.3.1.1.2 [2] of the C++ spec).
1920 boolean whiteSpace = TRUE; /* default causes insertion of space */
1923 if (isspace (c))
1924 whiteSpace = TRUE;
1925 else
1927 if (whiteSpace)
1929 vStringPut (name, ' ');
1930 whiteSpace = FALSE;
1932 vStringPut (name, c);
1934 c = cppGetc ();
1935 } while (! isOneOf (c, "(;") && c != EOF);
1936 vStringTerminate (name);
1938 else if (isOneOf (c, acceptable))
1940 vStringPut (name, ' '); /* always separate operator from keyword */
1943 vStringPut (name, c);
1944 c = cppGetc ();
1945 } while (isOneOf (c, acceptable));
1946 vStringTerminate (name);
1949 cppUngetc (c);
1951 token->type = TOKEN_NAME;
1952 token->keyword = KEYWORD_NONE;
1953 processName (st);
1956 static void copyToken (tokenInfo *const dest, const tokenInfo *const src)
1958 dest->type = src->type;
1959 dest->keyword = src->keyword;
1960 dest->filePosition = src->filePosition;
1961 dest->lineNumber = src->lineNumber;
1962 vStringCopy (dest->name, src->name);
1965 static void setAccess (statementInfo *const st, const accessType laccess)
1967 if (isMember (st))
1969 if (isLanguage (Lang_cpp) || isLanguage (Lang_d) || isLanguage (Lang_ferite))
1971 int c = skipToNonWhite ();
1973 if (c == ':')
1974 reinitStatementWithToken (st, prevToken (st, 1), FALSE);
1975 else
1976 cppUngetc (c);
1978 st->member.accessDefault = laccess;
1980 st->member.access = laccess;
1984 static void discardTypeList (tokenInfo *const token)
1986 int c = skipToNonWhite ();
1987 while (isident1 (c))
1989 readIdentifier (token, c);
1990 c = skipToNonWhite ();
1991 if (c == '.' || c == ',')
1992 c = skipToNonWhite ();
1994 cppUngetc (c);
1997 static void addParentClass (statementInfo *const st, tokenInfo *const token)
1999 if (vStringLength (token->name) > 0 &&
2000 vStringLength (st->parentClasses) > 0)
2002 vStringPut (st->parentClasses, ',');
2004 vStringCat (st->parentClasses, token->name);
2007 static void readParents (statementInfo *const st, const int qualifier)
2009 tokenInfo *const token = newToken ();
2010 tokenInfo *const parent = newToken ();
2011 int c;
2015 c = skipToNonWhite ();
2016 if (isident1 (c))
2018 readIdentifier (token, c);
2019 if (isType (token, TOKEN_NAME))
2020 vStringCat (parent->name, token->name);
2021 else
2023 addParentClass (st, parent);
2024 initToken (parent);
2027 else if (c == qualifier)
2028 vStringPut (parent->name, c);
2029 else if (c == '<')
2030 skipToMatch ("<>");
2031 else if (isType (token, TOKEN_NAME))
2033 addParentClass (st, parent);
2034 initToken (parent);
2036 } while (c != '{' && c != EOF);
2037 cppUngetc (c);
2038 deleteToken (parent);
2039 deleteToken (token);
2042 static void checkIsClassEnum (statementInfo *const st, const declType decl)
2044 if (! isLanguage (Lang_cpp) || st->declaration != DECL_ENUM)
2045 st->declaration = decl;
2048 static void processToken (tokenInfo *const token, statementInfo *const st)
2050 switch (token->keyword) /* is it a reserved word? */
2052 default: break;
2054 case KEYWORD_NONE: processName (st); break;
2055 case KEYWORD_ABSTRACT: st->implementation = IMP_ABSTRACT; break;
2056 case KEYWORD_ATTRIBUTE: skipParens (); initToken (token); break;
2057 case KEYWORD_CATCH: skipParens (); skipBraces (); break;
2058 case KEYWORD_CHAR: st->declaration = DECL_BASE; break;
2059 case KEYWORD_CLASS: checkIsClassEnum (st, DECL_CLASS); break;
2060 case KEYWORD_CONST: st->declaration = DECL_BASE; break;
2061 case KEYWORD_DOUBLE: st->declaration = DECL_BASE; break;
2062 case KEYWORD_ENUM: st->declaration = DECL_ENUM; break;
2063 case KEYWORD_EXTENDS: readParents (st, '.');
2064 setToken (st, TOKEN_NONE); break;
2065 case KEYWORD_FLOAT: st->declaration = DECL_BASE; break;
2066 case KEYWORD_FRIEND: st->scope = SCOPE_FRIEND; break;
2067 case KEYWORD_IMPLEMENTS:readParents (st, '.');
2068 setToken (st, TOKEN_NONE); break;
2069 case KEYWORD_IMPORT: st->declaration = DECL_IGNORE; break;
2070 case KEYWORD_INT: st->declaration = DECL_BASE; break;
2071 case KEYWORD_BOOLEAN: st->declaration = DECL_BASE; break;
2072 case KEYWORD_WCHAR_T: st->declaration = DECL_BASE; break;
2073 case KEYWORD_SIZE_T: st->declaration = DECL_BASE; break;
2074 case KEYWORD_INTERFACE: st->declaration = DECL_INTERFACE; break;
2075 case KEYWORD_LONG: st->declaration = DECL_BASE; break;
2076 case KEYWORD_OPERATOR: readOperator (st); break;
2077 case KEYWORD_MODULE: readPackage (st); break;
2078 case KEYWORD_PRIVATE: setAccess (st, ACCESS_PRIVATE); break;
2079 case KEYWORD_PROTECTED: setAccess (st, ACCESS_PROTECTED); break;
2080 case KEYWORD_PUBLIC: setAccess (st, ACCESS_PUBLIC); break;
2081 case KEYWORD_SHORT: st->declaration = DECL_BASE; break;
2082 case KEYWORD_SIGNED: st->declaration = DECL_BASE; break;
2083 case KEYWORD_STRUCT: checkIsClassEnum (st, DECL_STRUCT); break;
2084 case KEYWORD_STATIC_ASSERT: skipParens (); break;
2085 case KEYWORD_THROWS: discardTypeList (token); break;
2086 case KEYWORD_TYPEDEF: st->scope = SCOPE_TYPEDEF; break;
2087 case KEYWORD_UNION: st->declaration = DECL_UNION; break;
2088 case KEYWORD_UNSIGNED: st->declaration = DECL_BASE; break;
2089 case KEYWORD_USING: st->declaration = DECL_IGNORE; break;
2090 case KEYWORD_VOID: st->declaration = DECL_BASE; break;
2091 case KEYWORD_VOLATILE: st->declaration = DECL_BASE; break;
2092 case KEYWORD_VIRTUAL: st->implementation = IMP_VIRTUAL; break;
2094 case KEYWORD_NAMESPACE: readPackageOrNamespace (st, DECL_NAMESPACE); break;
2095 case KEYWORD_PACKAGE: readPackageOrNamespace (st, DECL_PACKAGE); break;
2096 case KEYWORD_EVENT:
2098 if (isLanguage (Lang_csharp))
2099 st->declaration = DECL_EVENT;
2100 break;
2102 case KEYWORD_SIGNAL:
2104 if (isLanguage (Lang_vala))
2105 st->declaration = DECL_SIGNAL;
2106 break;
2108 case KEYWORD_EXTERN:
2110 if (! isLanguage (Lang_csharp) || !st->gotName)
2112 /*reinitStatement (st, FALSE);*/
2113 st->scope = SCOPE_EXTERN;
2114 st->declaration = DECL_BASE;
2116 break;
2118 case KEYWORD_STATIC:
2120 if (! isLanguage (Lang_java) && ! isLanguage (Lang_csharp) && ! isLanguage (Lang_vala))
2122 /*reinitStatement (st, FALSE);*/
2123 st->scope = SCOPE_STATIC;
2124 st->declaration = DECL_BASE;
2126 break;
2128 case KEYWORD_IF:
2129 if (isLanguage (Lang_d))
2130 { /* static if (is(typeof(__traits(getMember, a, name)) == function)) */
2131 int c = skipToNonWhite ();
2132 if (c == '(')
2133 skipToMatch ("()");
2135 break;
2140 * Parenthesis handling functions
2143 static void restartStatement (statementInfo *const st)
2145 tokenInfo *const save = newToken ();
2146 tokenInfo *token = activeToken (st);
2148 copyToken (save, token);
2149 DebugStatement ( if (debug (DEBUG_PARSE)) printf ("<ES>");)
2150 reinitStatement (st, FALSE);
2151 token = activeToken (st);
2152 copyToken (token, save);
2153 deleteToken (save);
2154 processToken (token, st);
2157 /* Skips over a the mem-initializer-list of a ctor-initializer, defined as:
2159 * mem-initializer-list:
2160 * mem-initializer, mem-initializer-list
2162 * mem-initializer:
2163 * [::] [nested-name-spec] class-name (...)
2164 * identifier
2166 static void skipMemIntializerList (tokenInfo *const token)
2168 int c;
2172 c = skipToNonWhite ();
2173 while (isident1 (c) || c == ':')
2175 if (c != ':')
2176 readIdentifier (token, c);
2177 c = skipToNonWhite ();
2179 if (c == '<')
2181 skipToMatch ("<>");
2182 c = skipToNonWhite ();
2184 if (c == '(')
2186 skipToMatch ("()");
2187 c = skipToNonWhite ();
2189 } while (c == ',');
2190 cppUngetc (c);
2193 static void skipMacro (statementInfo *const st)
2195 tokenInfo *const prev2 = prevToken (st, 2);
2197 if (isType (prev2, TOKEN_NAME))
2198 retardToken (st);
2199 skipToMatch ("()");
2202 static boolean isDPostArgumentToken(tokenInfo *const token)
2204 switch (token->keyword)
2206 /* Note: some other keywords e.g. immutable are parsed as
2207 * KEYWORD_CONST - see initializeDParser */
2208 case KEYWORD_CONST:
2209 /* template constraint */
2210 case KEYWORD_IF:
2211 /* contracts */
2212 case KEYWORD_IN:
2213 case KEYWORD_OUT:
2214 case KEYWORD_BODY:
2215 return TRUE;
2216 default:
2217 break;
2219 /* @attributes */
2220 if (vStringValue(token->name)[0] == '@')
2221 return TRUE;
2222 return FALSE;
2225 /* Skips over characters following the parameter list. This will be either
2226 * non-ANSI style function declarations or C++ stuff. Our choices:
2228 * C (K&R):
2229 * int func ();
2230 * int func (one, two) int one; float two; {...}
2231 * C (ANSI):
2232 * int func (int one, float two);
2233 * int func (int one, float two) {...}
2234 * C++:
2235 * int foo (...) [const|volatile] [throw (...)];
2236 * int foo (...) [const|volatile] [throw (...)] [ctor-initializer] {...}
2237 * int foo (...) [const|volatile] [throw (...)] try [ctor-initializer] {...}
2238 * catch (...) {...}
2240 static boolean skipPostArgumentStuff (statementInfo *const st,
2241 parenInfo *const info)
2243 tokenInfo *const token = activeToken (st);
2244 unsigned int parameters = info->parameterCount;
2245 unsigned int elementCount = 0;
2246 boolean restart = FALSE;
2247 boolean end = FALSE;
2248 int c = skipToNonWhite ();
2252 switch (c)
2254 case ')': break;
2255 case ':': skipMemIntializerList (token);break; /* ctor-initializer */
2256 case '[': skipToMatch ("[]"); break;
2257 case '=': cppUngetc (c); end = TRUE; break;
2258 case '{': cppUngetc (c); end = TRUE; break;
2259 case '}': cppUngetc (c); end = TRUE; break;
2261 case '(':
2263 if (elementCount > 0)
2264 ++elementCount;
2265 skipToMatch ("()");
2266 break;
2269 case ';':
2271 if (parameters == 0 || elementCount < 2)
2273 cppUngetc (c);
2274 end = TRUE;
2276 else if (--parameters == 0)
2277 end = TRUE;
2278 break;
2281 default:
2283 if (isident1 (c))
2285 readIdentifier (token, c);
2286 if (isLanguage(Lang_d) && isDPostArgumentToken(token))
2287 token->keyword = KEYWORD_CONST;
2289 switch (token->keyword)
2291 case KEYWORD_ATTRIBUTE: skipParens (); break;
2292 case KEYWORD_THROW: skipParens (); break;
2293 case KEYWORD_CONST: break;
2294 case KEYWORD_NOEXCEPT: break;
2295 case KEYWORD_TRY: break;
2296 case KEYWORD_VOLATILE: break;
2298 case KEYWORD_CATCH: case KEYWORD_CLASS:
2299 case KEYWORD_EXPLICIT: case KEYWORD_EXTERN:
2300 case KEYWORD_FRIEND: case KEYWORD_INLINE:
2301 case KEYWORD_MUTABLE: case KEYWORD_NAMESPACE:
2302 case KEYWORD_NEW: case KEYWORD_OPERATOR:
2303 case KEYWORD_OVERLOAD: case KEYWORD_PRIVATE:
2304 case KEYWORD_PROTECTED: case KEYWORD_PUBLIC:
2305 case KEYWORD_STATIC: case KEYWORD_TEMPLATE:
2306 case KEYWORD_TYPEDEF: case KEYWORD_TYPENAME:
2307 case KEYWORD_USING: case KEYWORD_VIRTUAL:
2308 /* Never allowed within parameter declarations.
2310 restart = TRUE;
2311 end = TRUE;
2312 break;
2314 default:
2315 /* "override" and "final" are only keywords in the declaration of a virtual
2316 * member function, so need to be handled specially, not as keywords */
2317 if (isLanguage(Lang_cpp) && isType (token, TOKEN_NAME) &&
2318 (strcmp ("override", vStringValue (token->name)) == 0 ||
2319 strcmp ("final", vStringValue (token->name)) == 0))
2321 else if (isType (token, TOKEN_NONE))
2323 else if (info->isKnrParamList && info->parameterCount > 0)
2324 ++elementCount;
2325 else
2327 /* If we encounter any other identifier immediately
2328 * following an empty parameter list, this is almost
2329 * certainly one of those Microsoft macro "thingies"
2330 * that the automatic source code generation sticks
2331 * in. Terminate the current statement.
2333 restart = TRUE;
2334 end = TRUE;
2336 break;
2341 if (! end)
2343 c = skipToNonWhite ();
2344 if (c == EOF)
2345 end = TRUE;
2347 } while (! end);
2349 if (restart)
2350 restartStatement (st);
2351 else
2352 setToken (st, TOKEN_NONE);
2353 return (boolean) (c != EOF);
2356 static void skipJavaThrows (statementInfo *const st)
2358 tokenInfo *const token = activeToken (st);
2359 int c = skipToNonWhite ();
2361 if (isident1 (c))
2363 readIdentifier (token, c);
2364 if (token->keyword == KEYWORD_THROWS)
2368 c = skipToNonWhite ();
2369 if (isident1 (c))
2371 readIdentifier (token, c);
2372 c = skipToNonWhite ();
2374 } while (c == '.' || c == ',');
2377 cppUngetc (c);
2378 setToken (st, TOKEN_NONE);
2381 static void skipValaPostParens (statementInfo *const st)
2383 tokenInfo *const token = activeToken (st);
2384 int c = skipToNonWhite ();
2386 while (isident1 (c))
2388 readIdentifier (token, c);
2389 if (token->keyword == KEYWORD_ATTRIBUTE)
2391 /* parse contracts */
2392 skipParens ();
2393 c = skipToNonWhite ();
2395 else if (token->keyword == KEYWORD_THROWS)
2399 c = skipToNonWhite ();
2400 if (isident1 (c))
2402 readIdentifier (token, c);
2403 c = skipToNonWhite ();
2405 } while (c == '.' || c == ',');
2407 else
2408 break;
2410 cppUngetc (c);
2411 setToken (st, TOKEN_NONE);
2414 static void analyzePostParens (statementInfo *const st, parenInfo *const info)
2416 const unsigned long inputLineNumber = getInputLineNumber ();
2417 int c = skipToNonWhite ();
2419 cppUngetc (c);
2420 if (isOneOf (c, "{;,="))
2422 else if (isLanguage (Lang_java))
2423 skipJavaThrows (st);
2424 else if (isLanguage (Lang_vala))
2425 skipValaPostParens(st);
2426 else
2428 if (! skipPostArgumentStuff (st, info))
2430 verbose (
2431 "%s: confusing argument declarations beginning at line %lu\n",
2432 getInputFileName (), inputLineNumber);
2433 longjmp (Exception, (int) ExceptionFormattingError);
2438 static int parseParens (statementInfo *const st, parenInfo *const info)
2440 tokenInfo *const token = activeToken (st);
2441 unsigned int identifierCount = 0;
2442 unsigned int depth = 1;
2443 boolean firstChar = TRUE;
2444 int nextChar = '\0';
2446 info->parameterCount = 1;
2449 int c = skipToNonWhite ();
2451 switch (c)
2453 case '&':
2454 case '*':
2456 /* DEBUG_PRINT("parseParens, po++\n"); */
2457 info->isKnrParamList = FALSE;
2458 if (identifierCount == 0)
2459 info->isParamList = FALSE;
2460 initToken (token);
2461 break;
2463 case ':':
2465 info->isKnrParamList = FALSE;
2466 break;
2468 case '.':
2470 info->isNameCandidate = FALSE;
2471 info->isKnrParamList = FALSE;
2472 break;
2474 case ',':
2476 info->isNameCandidate = FALSE;
2477 if (info->isKnrParamList)
2479 ++info->parameterCount;
2480 identifierCount = 0;
2482 break;
2484 case '=':
2486 info->isKnrParamList = FALSE;
2487 info->isNameCandidate = FALSE;
2488 if (firstChar)
2490 info->isParamList = FALSE;
2491 skipMacro (st);
2492 depth = 0;
2494 break;
2496 case '[':
2498 info->isKnrParamList = FALSE;
2499 skipToMatch ("[]");
2500 break;
2502 case '<':
2504 info->isKnrParamList = FALSE;
2505 skipToMatch ("<>");
2506 break;
2508 case ')':
2510 if (firstChar)
2511 info->parameterCount = 0;
2512 --depth;
2513 break;
2515 case '(':
2517 info->isKnrParamList = FALSE;
2518 if (firstChar)
2520 info->isNameCandidate = FALSE;
2521 cppUngetc (c);
2522 skipMacro (st);
2523 depth = 0;
2525 else if (isType (token, TOKEN_PAREN_NAME))
2527 c = skipToNonWhite ();
2528 if (c == '*') /* check for function pointer */
2530 skipToMatch ("()");
2531 c = skipToNonWhite ();
2532 if (c == '(')
2533 skipToMatch ("()");
2535 else
2537 cppUngetc (c);
2538 cppUngetc ('(');
2539 info->nestedArgs = TRUE;
2542 else
2543 ++depth;
2544 break;
2547 default:
2549 if (isident1 (c))
2551 if (++identifierCount > 1)
2552 info->isKnrParamList = FALSE;
2553 readIdentifier (token, c);
2554 if (isType (token, TOKEN_NAME) && info->isNameCandidate)
2555 token->type = TOKEN_PAREN_NAME;
2556 else if (isType (token, TOKEN_KEYWORD))
2558 info->isKnrParamList = FALSE;
2559 info->isNameCandidate = FALSE;
2562 else if (isLanguage(Lang_d) && c == '!')
2563 { /* D template instantiation */
2564 info->isNameCandidate = FALSE;
2565 info->isKnrParamList = FALSE;
2567 else
2569 info->isParamList = FALSE;
2570 info->isKnrParamList = FALSE;
2571 info->isNameCandidate = FALSE;
2572 info->invalidContents = TRUE;
2574 break;
2577 firstChar = FALSE;
2578 } while (! info->nestedArgs && depth > 0 &&
2579 (info->isKnrParamList || info->isNameCandidate));
2581 if (! info->nestedArgs) while (depth > 0)
2583 skipToMatch ("()");
2584 --depth;
2586 if (st->argEndPosition == 0)
2587 st->argEndPosition = mio_tell (File.mio);
2589 if (! info->isNameCandidate)
2590 initToken (token);
2592 return nextChar;
2595 static void initParenInfo (parenInfo *const info)
2597 info->isParamList = TRUE;
2598 info->isKnrParamList = TRUE;
2599 info->isNameCandidate = TRUE;
2600 info->invalidContents = FALSE;
2601 info->nestedArgs = FALSE;
2602 info->parameterCount = 0;
2605 static void analyzeParens (statementInfo *const st)
2607 tokenInfo *const prev = prevToken (st, 1);
2609 if (! isType (prev, TOKEN_NONE)) /* in case of ignored enclosing macros */
2611 tokenInfo *const token = activeToken (st);
2612 parenInfo info;
2613 int c;
2615 initParenInfo (&info);
2616 parseParens (st, &info);
2618 c = skipToNonWhite ();
2620 cppUngetc (c);
2621 if (info.invalidContents)
2623 reinitStatement (st, FALSE);
2625 else if (info.isNameCandidate && isType (token, TOKEN_PAREN_NAME) &&
2626 ! st->gotParenName &&
2627 (! info.isParamList || ! st->haveQualifyingName ||
2628 c == '(' ||
2629 (c == '=' && st->implementation != IMP_VIRTUAL) ||
2630 (st->declaration == DECL_NONE && isOneOf (c, ",;"))))
2632 token->type = TOKEN_NAME;
2633 processName (st);
2634 st->gotParenName = TRUE;
2635 if (isLanguage(Lang_d) && c == '(' && isType (prev, TOKEN_NAME))
2637 st->declaration = DECL_FUNCTION_TEMPLATE;
2638 copyToken (st->blockName, prev);
2641 else if (! st->gotArgs && info.isParamList)
2643 st->gotArgs = TRUE;
2644 setToken (st, TOKEN_ARGS);
2645 advanceToken (st);
2646 analyzePostParens (st, &info);
2648 else
2650 setToken (st, TOKEN_NONE);
2656 * Token parsing functions
2659 static void addContext (statementInfo *const st, const tokenInfo* const token)
2661 if (isType (token, TOKEN_NAME))
2663 if (vStringLength (st->context->name) > 0)
2665 if (isLanguage (Lang_c) || isLanguage (Lang_cpp))
2666 vStringCatS (st->context->name, "::");
2667 else if (isLanguage (Lang_java) ||
2668 isLanguage (Lang_d) || isLanguage (Lang_ferite) ||
2669 isLanguage (Lang_csharp) || isLanguage (Lang_vala))
2670 vStringCatS (st->context->name, ".");
2672 vStringCat (st->context->name, token->name);
2673 st->context->type = TOKEN_NAME;
2677 static boolean inheritingDeclaration (declType decl)
2679 /* enum base types */
2680 if (decl == DECL_ENUM)
2682 return (boolean) (isLanguage (Lang_cpp) || isLanguage (Lang_csharp) ||
2683 isLanguage (Lang_d));
2685 return (boolean) (
2686 decl == DECL_CLASS ||
2687 decl == DECL_STRUCT ||
2688 decl == DECL_INTERFACE);
2691 static void processColon (statementInfo *const st)
2693 int c = cppGetc ();
2694 const boolean doubleColon = (boolean) (c == ':');
2696 if (doubleColon)
2698 setToken (st, TOKEN_DOUBLE_COLON);
2699 st->haveQualifyingName = FALSE;
2701 else
2703 cppUngetc (c);
2704 if ((isLanguage (Lang_cpp) || isLanguage (Lang_csharp) || isLanguage (Lang_d) ||
2705 isLanguage (Lang_vala)) &&
2706 inheritingDeclaration (st->declaration))
2708 readParents (st, ':');
2710 else if (parentDecl (st) == DECL_STRUCT || parentDecl (st) == DECL_CLASS)
2712 c = skipToOneOf (",;");
2713 if (c == ',')
2714 setToken (st, TOKEN_COMMA);
2715 else if (c == ';')
2716 setToken (st, TOKEN_SEMICOLON);
2718 else
2720 const tokenInfo *const prev = prevToken (st, 1);
2721 const tokenInfo *const prev2 = prevToken (st, 2);
2722 if (prev->keyword == KEYWORD_DEFAULT ||
2723 prev2->keyword == KEYWORD_CASE ||
2724 st->parent != NULL)
2726 reinitStatement (st, FALSE);
2732 /* Skips over any initializing value which may follow an '=' character in a
2733 * variable definition.
2735 static int skipInitializer (statementInfo *const st)
2737 boolean done = FALSE;
2738 int c;
2740 while (! done)
2742 c = skipToNonWhite ();
2744 if (c == EOF)
2745 longjmp (Exception, (int) ExceptionFormattingError);
2746 else switch (c)
2748 case ',':
2749 case ';': done = TRUE; break;
2751 case '0':
2752 if (st->implementation == IMP_VIRTUAL)
2753 st->implementation = IMP_PURE_VIRTUAL;
2754 break;
2756 case '[': skipToMatch ("[]"); break;
2757 case '(': skipToMatch ("()"); break;
2758 case '{': skipToMatch ("{}"); break;
2760 case '}':
2761 if (insideEnumBody (st))
2762 done = TRUE;
2763 else if (! isBraceFormat ())
2765 verbose ("%s: unexpected closing brace at line %lu\n",
2766 getInputFileName (), getInputLineNumber ());
2767 longjmp (Exception, (int) ExceptionBraceFormattingError);
2769 break;
2771 default: break;
2774 return c;
2777 static void processInitializer (statementInfo *const st)
2779 const boolean inEnumBody = insideEnumBody (st);
2780 const int c = skipInitializer (st);
2782 if (c == ';')
2783 setToken (st, TOKEN_SEMICOLON);
2784 else if (c == ',')
2785 setToken (st, TOKEN_COMMA);
2786 else if (c == '}' && inEnumBody)
2788 cppUngetc (c);
2789 setToken (st, TOKEN_COMMA);
2791 if (st->scope == SCOPE_EXTERN)
2792 st->scope = SCOPE_GLOBAL;
2795 static void parseIdentifier (statementInfo *const st, const int c)
2797 tokenInfo *const token = activeToken (st);
2799 readIdentifier (token, c);
2800 if (! isType (token, TOKEN_NONE))
2801 processToken (token, st);
2804 static void parseGeneralToken (statementInfo *const st, const int c)
2806 const tokenInfo *const prev = prevToken (st, 1);
2808 if (isident1(c))
2810 parseIdentifier (st, c);
2811 if (isType (st->context, TOKEN_NAME) &&
2812 isType (activeToken (st), TOKEN_NAME) && isType (prev, TOKEN_NAME))
2814 initToken (st->context);
2817 else if (isExternCDecl (st, c))
2819 st->declaration = DECL_NOMANGLE;
2820 st->scope = SCOPE_GLOBAL;
2824 /* Reads characters from the pre-processor and assembles tokens, setting
2825 * the current statement state.
2827 static void nextToken (statementInfo *const st)
2829 int c;
2830 tokenInfo *token = activeToken (st);
2833 c = skipToNonWhite();
2834 switch (c)
2836 case EOF: longjmp (Exception, (int) ExceptionEOF); break;
2837 case '(': analyzeParens (st); token = activeToken (st); break;
2838 case '*': setToken (st, TOKEN_STAR); break;
2839 case ',': setToken (st, TOKEN_COMMA); break;
2840 case ':': processColon (st); break;
2841 case ';': setToken (st, TOKEN_SEMICOLON); break;
2842 case '<': skipToMatch ("<>"); break;
2843 case '=': processInitializer (st); break;
2844 case '[':
2845 /* Hack for Vala: [..] can be a function attribute.
2846 * Seems not to have bad side effects, but have to test it more. */
2847 if (!isLanguage (Lang_vala))
2848 setToken (st, TOKEN_ARRAY);
2849 skipToMatch ("[]");
2850 break;
2851 case '{': setToken (st, TOKEN_BRACE_OPEN); break;
2852 case '}': setToken (st, TOKEN_BRACE_CLOSE); break;
2853 default: parseGeneralToken (st, c); break;
2855 } while (isType (token, TOKEN_NONE));
2857 if (isType (token, TOKEN_SEMICOLON) && st->parent)
2858 st->parent->nSemicolons ++;
2860 /* We want to know about non-keyword variable types */
2861 if (TOKEN_NONE == st->firstToken->type)
2863 if ((TOKEN_NAME == token->type) || isDataTypeKeyword(token))
2864 copyToken(st->firstToken, token);
2869 * Scanning support functions
2871 static unsigned int contextual_fake_count = 0;
2872 static statementInfo *CurrentStatement = NULL;
2874 static statementInfo *newStatement (statementInfo *const parent)
2876 statementInfo *const st = xMalloc (1, statementInfo);
2877 unsigned int i;
2879 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
2880 st->token [i] = newToken ();
2882 st->context = newToken ();
2883 st->blockName = newToken ();
2884 st->parentClasses = vStringNew ();
2885 st->firstToken = newToken();
2887 initStatement (st, parent);
2888 CurrentStatement = st;
2890 return st;
2893 static void deleteStatement (void)
2895 statementInfo *const st = CurrentStatement;
2896 statementInfo *const parent = st->parent;
2897 unsigned int i;
2899 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
2901 deleteToken(st->token[i]); st->token[i] = NULL;
2903 deleteToken(st->blockName); st->blockName = NULL;
2904 deleteToken(st->context); st->context = NULL;
2905 vStringDelete(st->parentClasses); st->parentClasses = NULL;
2906 deleteToken(st->firstToken);
2907 eFree (st);
2908 CurrentStatement = parent;
2911 static void deleteAllStatements (void)
2913 while (CurrentStatement != NULL)
2914 deleteStatement ();
2917 static boolean isStatementEnd (const statementInfo *const st)
2919 const tokenInfo *const token = activeToken (st);
2920 boolean isEnd;
2922 if (isType (token, TOKEN_SEMICOLON))
2923 isEnd = TRUE;
2924 else if (isType (token, TOKEN_BRACE_CLOSE))
2925 /* Java, D, C#, Vala do not require semicolons to end a block. Neither do
2926 * C++ namespaces. All other blocks require a semicolon to terminate them.
2928 isEnd = (boolean) (isLanguage (Lang_java) || isLanguage (Lang_d) ||
2929 isLanguage (Lang_csharp) || isLanguage (Lang_vala) ||
2930 ! isContextualStatement (st));
2931 else
2932 isEnd = FALSE;
2934 return isEnd;
2937 static void checkStatementEnd (statementInfo *const st)
2939 const tokenInfo *const token = activeToken (st);
2940 boolean comma = isType (token, TOKEN_COMMA);
2942 if (comma || isStatementEnd (st))
2944 reinitStatementWithToken (st, activeToken (st), comma);
2946 DebugStatement ( if (debug (DEBUG_PARSE)) printf ("<ES>"); )
2947 cppEndStatement ();
2949 else
2951 cppBeginStatement ();
2952 advanceToken (st);
2956 static void nest (statementInfo *const st, const unsigned int nestLevel)
2958 switch (st->declaration)
2960 case DECL_CLASS:
2961 case DECL_ENUM:
2962 case DECL_INTERFACE:
2963 case DECL_NAMESPACE:
2964 case DECL_NOMANGLE:
2965 case DECL_STRUCT:
2966 case DECL_UNION:
2967 createTags (nestLevel, st);
2968 break;
2969 default:
2970 skipToMatch ("{}");
2971 break;
2973 advanceToken (st);
2974 setToken (st, TOKEN_BRACE_CLOSE);
2977 static void tagCheck (statementInfo *const st)
2979 const tokenInfo *const token = activeToken (st);
2980 const tokenInfo *const prev = prevToken (st, 1);
2981 const tokenInfo *const prev2 = prevToken (st, 2);
2983 switch (token->type)
2985 case TOKEN_NAME:
2987 if (insideEnumBody (st) &&
2988 /* Java enumerations can contain members after a semicolon */
2989 (! isLanguage(Lang_java) || st->parent->nSemicolons < 1))
2990 qualifyEnumeratorTag (st, token);
2991 break;
2993 #if 0
2994 case TOKEN_PACKAGE:
2996 if (st->haveQualifyingName)
2997 makeTag (token, st, FALSE, TAG_PACKAGE);
2998 break;
3000 #endif
3001 case TOKEN_BRACE_OPEN:
3003 if (isType (prev, TOKEN_ARGS))
3005 if (st->declaration == DECL_FUNCTION_TEMPLATE)
3006 qualifyFunctionTag (st, st->blockName);
3007 else if (st->haveQualifyingName)
3009 if (isType (prev2, TOKEN_NAME))
3010 copyToken (st->blockName, prev2);
3011 /* D structure templates */
3012 if (isLanguage (Lang_d) &&
3013 (st->declaration == DECL_CLASS || st->declaration == DECL_STRUCT ||
3014 st->declaration == DECL_INTERFACE || st->declaration == DECL_NAMESPACE))
3015 qualifyBlockTag (st, prev2);
3016 else
3018 st->declaration = DECL_FUNCTION;
3019 qualifyFunctionTag (st, prev2);
3023 else if (isContextualStatement (st))
3025 tokenInfo *name_token = (tokenInfo *)prev;
3026 boolean free_name_token = FALSE;
3028 /* C++ 11 allows class <name> final { ... } */
3029 if (isLanguage (Lang_cpp) && isType (prev, TOKEN_NAME) &&
3030 strcmp("final", vStringValue(prev->name)) == 0 &&
3031 isType(prev2, TOKEN_NAME))
3033 name_token = (tokenInfo *)prev2;
3034 copyToken (st->blockName, name_token);
3036 else if (isType (name_token, TOKEN_NAME))
3038 if (!isLanguage (Lang_vala))
3039 copyToken (st->blockName, name_token);
3040 else
3042 switch (st->declaration)
3044 case DECL_CLASS:
3045 case DECL_ENUM:
3046 case DECL_INTERFACE:
3047 case DECL_NAMESPACE:
3048 case DECL_STRUCT:
3049 copyToken (st->blockName, name_token);
3050 break;
3052 /* anything else can be a property */
3053 default:
3054 /* makeTag (prev, st, FALSE, TAG_PROPERTY); */
3055 /* FIXME: temporary hack to get properties shown */
3056 makeTag (prev, st, FALSE, TAG_FIELD);
3057 break;
3061 else if (isLanguage (Lang_csharp))
3062 makeTag (prev, st, FALSE, TAG_PROPERTY);
3063 else
3065 tokenInfo *contextual_token = (tokenInfo *)prev;
3066 if(isContextualKeyword (contextual_token))
3068 char buffer[64];
3070 name_token = newToken ();
3071 free_name_token = TRUE;
3072 copyToken (name_token, contextual_token);
3074 sprintf(buffer, "anon_%s_%d", name_token->name->buffer, contextual_fake_count++);
3075 vStringClear(name_token->name);
3076 vStringCatS(name_token->name, buffer);
3078 name_token->type = TOKEN_NAME;
3079 name_token->keyword = KEYWORD_NONE;
3081 advanceToken (st);
3082 contextual_token = activeToken (st);
3083 copyToken (contextual_token, token);
3084 copyToken ((tokenInfo *const)token, name_token);
3085 copyToken (st->blockName, name_token);
3086 copyToken (st->firstToken, name_token);
3089 qualifyBlockTag (st, name_token);
3090 if (free_name_token)
3091 deleteToken (name_token);
3093 break;
3095 case TOKEN_ARRAY:
3096 case TOKEN_SEMICOLON:
3097 case TOKEN_COMMA:
3099 if (insideEnumBody (st) &&
3100 /* Java enumerations can contain members after a semicolon */
3101 (! isLanguage (Lang_java) || st->parent->nSemicolons < 2))
3103 else if (isType (prev, TOKEN_NAME))
3105 if (isContextualKeyword (prev2))
3106 makeTag (prev, st, TRUE, TAG_EXTERN_VAR);
3107 else
3108 qualifyVariableTag (st, prev);
3110 else if (isType (prev, TOKEN_ARGS) && isType (prev2, TOKEN_NAME))
3112 qualifyFunctionDeclTag (st, prev2);
3114 break;
3116 default:
3117 break;
3121 /* Parses the current file and decides whether to write out and tags that
3122 * are discovered.
3124 static void createTags (const unsigned int nestLevel,
3125 statementInfo *const parent)
3127 statementInfo *const st = newStatement (parent);
3129 DebugStatement ( if (nestLevel > 0) debugParseNest (TRUE, nestLevel); )
3130 while (TRUE)
3132 tokenInfo *token;
3134 nextToken (st);
3136 token = activeToken (st);
3138 if (isType (token, TOKEN_BRACE_CLOSE))
3140 if (nestLevel > 0)
3141 break;
3142 else
3144 verbose ("%s: unexpected closing brace at line %lu\n",
3145 getInputFileName (), getInputLineNumber ());
3146 longjmp (Exception, (int) ExceptionBraceFormattingError);
3149 else if (isType (token, TOKEN_DOUBLE_COLON))
3151 addContext (st, prevToken (st, 1));
3152 advanceToken (st);
3154 else
3156 tagCheck (st);/* this can add new token */
3157 if (isType (activeToken (st), TOKEN_BRACE_OPEN))
3158 nest (st, nestLevel + 1);
3159 checkStatementEnd (st);
3162 deleteStatement ();
3163 DebugStatement ( if (nestLevel > 0) debugParseNest (FALSE, nestLevel - 1); )
3166 static boolean findCTags (const unsigned int passCount)
3168 exception_t exception;
3169 boolean retry;
3171 contextual_fake_count = 0;
3173 Assert (passCount < 3);
3174 cppInit ((boolean) (passCount > 1), isLanguage (Lang_csharp), isLanguage (Lang_cpp));
3176 exception = (exception_t) setjmp (Exception);
3177 retry = FALSE;
3179 if (exception == ExceptionNone)
3181 createTags (0, NULL);
3183 else
3185 deleteAllStatements ();
3186 if (exception == ExceptionBraceFormattingError && passCount == 1)
3188 retry = TRUE;
3189 verbose ("%s: retrying file with fallback brace matching algorithm\n",
3190 getInputFileName ());
3193 cppTerminate ();
3194 return retry;
3197 static void buildKeywordHash (const langType language, unsigned int idx)
3199 const size_t count = sizeof (KeywordTable) / sizeof (KeywordTable [0]);
3200 size_t i;
3201 for (i = 0 ; i < count ; ++i)
3203 const keywordDesc* const p = &KeywordTable [i];
3204 if (p->isValid [idx])
3205 addKeyword (p->name, language, (int) p->id);
3209 static void initializeCParser (const langType language)
3211 Lang_c = language;
3212 buildKeywordHash (language, 0);
3215 static void initializeCppParser (const langType language)
3217 Lang_cpp = language;
3218 buildKeywordHash (language, 1);
3221 static void initializeJavaParser (const langType language)
3223 Lang_java = language;
3224 buildKeywordHash (language, 3);
3227 static void initializeDParser (const langType language)
3229 /* treat these like const - some are for parsing like const(Type), some are just
3230 * function attributes */
3231 const char *const_aliases[] = {"immutable", "nothrow", "pure", "shared", NULL};
3232 const char **s;
3234 Lang_d = language;
3235 buildKeywordHash (language, 6);
3237 for (s = const_aliases; *s != NULL; s++)
3239 addKeyword (*s, language, KEYWORD_CONST);
3241 /* other keyword aliases */
3242 addKeyword ("alias", language, KEYWORD_TYPEDEF);
3243 /* skip 'static assert(...)' like 'static if (...)' */
3244 addKeyword ("assert", language, KEYWORD_IF);
3245 addKeyword ("unittest", language, KEYWORD_BODY); /* ignore */
3246 addKeyword ("version", language, KEYWORD_NAMESPACE); /* parse block */
3249 static void initializeGLSLParser (const langType language)
3251 Lang_glsl = language;
3252 buildKeywordHash (language, 0); /* C keywords */
3255 static void initializeFeriteParser (const langType language)
3257 Lang_ferite = language;
3258 buildKeywordHash (language, 1); /* C++ keywords */
3261 static void initializeCsharpParser (const langType language)
3263 Lang_csharp = language;
3264 buildKeywordHash (language, 2);
3267 static void initializeValaParser (const langType language)
3269 Lang_vala = language;
3270 buildKeywordHash (language, 5);
3272 /* keyword aliases */
3273 addKeyword ("ensures", language, KEYWORD_ATTRIBUTE); /* ignore */
3274 addKeyword ("errordomain", language, KEYWORD_ENUM); /* looks like enum */
3275 addKeyword ("requires", language, KEYWORD_ATTRIBUTE); /* ignore */
3278 extern parserDefinition* CParser (void)
3280 static const char *const extensions [] = { "c", "pc", "sc", NULL };
3281 parserDefinition* def = parserNew ("C");
3282 def->kinds = CKinds;
3283 def->kindCount = KIND_COUNT (CKinds);
3284 def->extensions = extensions;
3285 def->parser2 = findCTags;
3286 def->initialize = initializeCParser;
3287 return def;
3290 extern parserDefinition* CppParser (void)
3292 static const char *const extensions [] = {
3293 "c++", "cc", "cp", "cpp", "cxx", "h", "h++", "hh", "hp", "hpp", "hxx",
3294 "i",
3295 #ifndef CASE_INSENSITIVE_FILENAMES
3296 "C", "H",
3297 #endif
3298 NULL
3300 parserDefinition* def = parserNew ("C++");
3301 def->kinds = CKinds;
3302 def->kindCount = KIND_COUNT (CKinds);
3303 def->extensions = extensions;
3304 def->parser2 = findCTags;
3305 def->initialize = initializeCppParser;
3306 return def;
3309 extern parserDefinition* JavaParser (void)
3311 static const char *const extensions [] = { "java", NULL };
3312 parserDefinition* def = parserNew ("Java");
3313 def->kinds = JavaKinds;
3314 def->kindCount = KIND_COUNT (JavaKinds);
3315 def->extensions = extensions;
3316 def->parser2 = findCTags;
3317 def->initialize = initializeJavaParser;
3318 return def;
3321 extern parserDefinition* DParser (void)
3323 static const char *const extensions [] = { "d", "di", NULL };
3324 parserDefinition* def = parserNew ("D");
3325 def->kinds = DKinds;
3326 def->kindCount = KIND_COUNT (DKinds);
3327 def->extensions = extensions;
3328 def->parser2 = findCTags;
3329 def->initialize = initializeDParser;
3330 return def;
3333 extern parserDefinition* GLSLParser (void)
3335 static const char *const extensions [] = { "glsl", "frag", "vert", NULL };
3336 parserDefinition* def = parserNew ("GLSL");
3337 def->kinds = CKinds;
3338 def->kindCount = KIND_COUNT (CKinds);
3339 def->extensions = extensions;
3340 def->parser2 = findCTags;
3341 def->initialize = initializeGLSLParser;
3342 return def;
3345 extern parserDefinition* FeriteParser (void)
3347 static const char *const extensions [] = { "fe", NULL };
3348 parserDefinition* def = parserNew ("Ferite");
3349 def->kinds = CKinds;
3350 def->kindCount = KIND_COUNT (CKinds);
3351 def->extensions = extensions;
3352 def->parser2 = findCTags;
3353 def->initialize = initializeFeriteParser;
3354 return def;
3357 extern parserDefinition* CsharpParser (void)
3359 static const char *const extensions [] = { "cs", NULL };
3360 parserDefinition* def = parserNew ("C#");
3361 def->kinds = CsharpKinds;
3362 def->kindCount = KIND_COUNT (CsharpKinds);
3363 def->extensions = extensions;
3364 def->parser2 = findCTags;
3365 def->initialize = initializeCsharpParser;
3366 return def;
3369 extern parserDefinition* ValaParser (void)
3371 static const char *const extensions [] = { "vala", NULL };
3372 parserDefinition* def = parserNew ("Vala");
3373 def->kinds = ValaKinds;
3374 def->kindCount = KIND_COUNT (ValaKinds);
3375 def->extensions = extensions;
3376 def->parser2 = findCTags;
3377 def->initialize = initializeValaParser;
3378 return def;
3380 /* vi:set tabstop=8 shiftwidth=4: */