2 * $Id: c.c 689 2008-12-13 21:17:36Z elliotth $
4 * Copyright (c) 1996-2003, Darren Hiebert
6 * This source code is released for free distribution under the terms of the
7 * GNU General Public License.
9 * This module contains functions for parsing and scanning C, C++ and Java
16 #include "general.h" /* must always come first */
34 #define activeToken(st) ((st)->token [(int) (st)->tokenIndex])
35 #define parentDecl(st) ((st)->parent == NULL ? \
36 DECL_NONE : (st)->parent->declaration)
37 #define isType(token,t) (boolean) ((token)->type == (t))
38 #define insideEnumBody(st) ((st)->parent == NULL ? FALSE : \
39 (boolean) ((st)->parent->declaration == DECL_ENUM))
40 #define isExternCDecl(st,c) (boolean) ((c) == STRING_SYMBOL && \
41 ! (st)->haveQualifyingName && (st)->scope == SCOPE_EXTERN)
43 #define isOneOf(c,s) (boolean) (strchr ((s), (c)) != NULL)
45 #define isHighChar(c) ((c) != EOF && (unsigned char)(c) >= 0xc0)
51 enum { NumTokens
= 15 };
53 typedef enum eException
{
54 ExceptionNone
, ExceptionEOF
, ExceptionFormattingError
,
55 ExceptionBraceFormattingError
58 /* Used to specify type of keyword.
60 typedef enum eKeywordId
{
62 KEYWORD_ATTRIBUTE
, KEYWORD_ABSTRACT
,
63 KEYWORD_BOOLEAN
, KEYWORD_BYTE
, KEYWORD_BAD_STATE
, KEYWORD_BAD_TRANS
,
64 KEYWORD_BIND
, KEYWORD_BIND_VAR
, KEYWORD_BIT
,
65 KEYWORD_CASE
, KEYWORD_CATCH
, KEYWORD_CHAR
, KEYWORD_CLASS
, KEYWORD_CONST
,
66 KEYWORD_CONSTRAINT
, KEYWORD_COVERAGE_BLOCK
, KEYWORD_COVERAGE_DEF
,
67 KEYWORD_DEFAULT
, KEYWORD_DELEGATE
, KEYWORD_DELETE
, KEYWORD_DO
,
69 KEYWORD_ELSE
, KEYWORD_ENUM
, KEYWORD_EXPLICIT
, KEYWORD_EXTERN
,
70 KEYWORD_EXTENDS
, KEYWORD_EVENT
,
71 KEYWORD_FINAL
, KEYWORD_FLOAT
, KEYWORD_FOR
, KEYWORD_FOREACH
,
72 KEYWORD_FRIEND
, KEYWORD_FUNCTION
,
74 KEYWORD_IF
, KEYWORD_IMPLEMENTS
, KEYWORD_IMPORT
, KEYWORD_INLINE
, KEYWORD_INT
,
75 KEYWORD_INOUT
, KEYWORD_INPUT
, KEYWORD_INTEGER
, KEYWORD_INTERFACE
,
77 KEYWORD_LOCAL
, KEYWORD_LONG
,
78 KEYWORD_M_BAD_STATE
, KEYWORD_M_BAD_TRANS
, KEYWORD_M_STATE
, KEYWORD_M_TRANS
,
80 KEYWORD_NAMESPACE
, KEYWORD_NEW
, KEYWORD_NEWCOV
, KEYWORD_NATIVE
,
81 KEYWORD_OPERATOR
, KEYWORD_OUTPUT
, KEYWORD_OVERLOAD
, KEYWORD_OVERRIDE
,
82 KEYWORD_PACKED
, KEYWORD_PORT
, KEYWORD_PACKAGE
, KEYWORD_PRIVATE
,
83 KEYWORD_PROGRAM
, KEYWORD_PROTECTED
, KEYWORD_PUBLIC
,
84 KEYWORD_REGISTER
, KEYWORD_RETURN
,
85 KEYWORD_SHADOW
, KEYWORD_STATE
,
86 KEYWORD_SHORT
, KEYWORD_SIGNED
, KEYWORD_STATIC
, KEYWORD_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
,
93 KEYWORD_VIRTUAL
, KEYWORD_VOID
, KEYWORD_VOLATILE
,
94 KEYWORD_WCHAR_T
, KEYWORD_WHILE
97 /* Used to determine whether keyword is valid for the current language and
100 typedef struct sKeywordDesc
{
103 short isValid
[5]; /* indicates languages for which kw is valid */
106 /* Used for reporting the type of object parsed by nextToken ().
108 typedef enum eTokenType
{
109 TOKEN_NONE
, /* none */
110 TOKEN_ARGS
, /* a parenthetical pair and its contents */
113 TOKEN_COLON
, /* the colon character */
114 TOKEN_COMMA
, /* the comma character */
115 TOKEN_DOUBLE_COLON
, /* double colon indicates nested-name-specifier */
117 TOKEN_NAME
, /* an unknown name */
118 TOKEN_PACKAGE
, /* a Java package name */
119 TOKEN_PAREN_NAME
, /* a single name in parentheses */
120 TOKEN_SEMICOLON
, /* the semicolon character */
121 TOKEN_SPEC
, /* a storage class specifier, qualifier, type, etc. */
122 TOKEN_STAR
, /* pointer * detection */
123 TOKEN_AMPERSAND
, /* ampersand & detection */
127 /* This describes the scoping of the current statement.
129 typedef enum eTagScope
{
130 SCOPE_GLOBAL
, /* no storage class specified */
131 SCOPE_STATIC
, /* static storage class */
132 SCOPE_EXTERN
, /* external storage class */
133 SCOPE_FRIEND
, /* declares access only */
134 SCOPE_TYPEDEF
, /* scoping depends upon context */
138 typedef enum eDeclaration
{
140 DECL_BASE
, /* base type (default) */
145 DECL_IGNORE
, /* non-taggable "declaration" */
148 DECL_NOMANGLE
, /* C++ name demangling block */
150 DECL_PROGRAM
, /* Vera program */
152 DECL_TASK
, /* Vera task */
157 typedef enum eVisibilityType
{
163 ACCESS_DEFAULT
, /* Java-specific */
167 /* Information about the parent class of a member (if any).
169 typedef struct sMemberInfo
{
170 accessType access
; /* access of current statement */
171 accessType accessDefault
; /* access default for current statement */
174 typedef struct sTokenInfo
{
177 vString
* name
; /* the name of the token */
178 unsigned long lineNumber
; /* line number of tag */
179 fpos_t filePosition
; /* file position of line containing name */
182 typedef enum eImplementation
{
190 /* Describes the statement currently undergoing analysis.
192 typedef struct sStatementInfo
{
194 declType declaration
; /* specifier associated with TOKEN_SPEC */
195 boolean gotName
; /* was a name parsed yet? */
196 boolean haveQualifyingName
; /* do we have a name we are considering? */
197 boolean gotParenName
; /* was a name inside parentheses parsed yet? */
198 boolean gotArgs
; /* was a list of parameters parsed yet? */
199 boolean isPointer
; /* is 'name' a pointer? */
200 boolean inFunction
; /* are we inside of a function? */
201 boolean assignment
; /* have we handled an '='? */
202 boolean notVariable
; /* has a variable declaration been disqualified ? */
203 impType implementation
; /* abstract or concrete implementation? */
204 unsigned int tokenIndex
; /* currently active token */
205 tokenInfo
* token
[(int) NumTokens
];
206 tokenInfo
* context
; /* accumulated scope of current statement */
207 tokenInfo
* blockName
; /* name of current block */
208 memberInfo member
; /* information regarding parent class/struct */
209 vString
* parentClasses
; /* parent classes */
210 struct sStatementInfo
*parent
; /* statement we are nested within */
213 /* Describes the type of tag being generated.
215 typedef enum eTagType
{
217 TAG_CLASS
, /* class name */
218 TAG_ENUM
, /* enumeration name */
219 TAG_ENUMERATOR
, /* enumerator (enumeration value) */
220 TAG_EVENT
, /* event */
221 TAG_FIELD
, /* field (Java) */
222 TAG_FUNCTION
, /* function definition */
223 TAG_INTERFACE
, /* interface declaration */
224 TAG_LOCAL
, /* local variable definition */
225 TAG_MEMBER
, /* structure, class or interface member */
226 TAG_METHOD
, /* method declaration */
227 TAG_NAMESPACE
, /* namespace name */
228 TAG_PACKAGE
, /* package name */
229 TAG_PROGRAM
, /* program name */
230 TAG_PROPERTY
, /* property name */
231 TAG_PROTOTYPE
, /* function prototype or declaration */
232 TAG_STRUCT
, /* structure name */
233 TAG_TASK
, /* task name */
234 TAG_TYPEDEF
, /* typedef name */
235 TAG_UNION
, /* union name */
236 TAG_VARIABLE
, /* variable definition */
237 TAG_EXTERN_VAR
, /* external variable declaration */
238 TAG_COUNT
/* must be last */
241 typedef struct sParenInfo
{
244 boolean isKnrParamList
;
245 boolean isNameCandidate
;
246 boolean invalidContents
;
248 unsigned int parameterCount
;
255 static jmp_buf Exception
;
257 static langType Lang_c
;
258 static langType Lang_cpp
;
259 static langType Lang_csharp
;
260 static langType Lang_java
;
261 static langType Lang_vera
;
262 static vString
*Signature
;
263 static boolean CollectingSignature
;
264 static vString
*ReturnType
;
266 /* Number used to uniquely identify anonymous structs and unions. */
267 static int AnonymousID
= 0;
269 /* Used to index into the CKinds table. */
272 CK_CLASS
, CK_DEFINE
, CK_ENUMERATOR
, CK_FUNCTION
,
273 CK_ENUMERATION
, CK_LOCAL
, CK_MEMBER
, CK_NAMESPACE
, CK_PROTOTYPE
,
274 CK_STRUCT
, CK_TYPEDEF
, CK_UNION
, CK_VARIABLE
,
278 static kindOption CKinds
[] = {
279 { TRUE
, 'c', "class", "classes"},
280 { TRUE
, 'd', "macro", "macro definitions"},
281 { TRUE
, 'e', "enumerator", "enumerators (values inside an enumeration)"},
282 { TRUE
, 'f', "function", "function definitions"},
283 { TRUE
, 'g', "enum", "enumeration names"},
284 { FALSE
, 'l', "local", "local variables"},
285 { TRUE
, 'm', "member", "class, struct, and union members"},
286 { TRUE
, 'n', "namespace", "namespaces"},
287 { FALSE
, 'p', "prototype", "function prototypes"},
288 { TRUE
, 's', "struct", "structure names"},
289 { TRUE
, 't', "typedef", "typedefs"},
290 { TRUE
, 'u', "union", "union names"},
291 { TRUE
, 'v', "variable", "variable definitions"},
292 { FALSE
, 'x', "externvar", "external and forward variable declarations"},
297 CSK_CLASS
, CSK_DEFINE
, CSK_ENUMERATOR
, CSK_EVENT
, CSK_FIELD
,
298 CSK_ENUMERATION
, CSK_INTERFACE
, CSK_LOCAL
, CSK_METHOD
,
299 CSK_NAMESPACE
, CSK_PROPERTY
, CSK_STRUCT
, CSK_TYPEDEF
302 static kindOption CsharpKinds
[] = {
303 { TRUE
, 'c', "class", "classes"},
304 { TRUE
, 'd', "macro", "macro definitions"},
305 { TRUE
, 'e', "enumerator", "enumerators (values inside an enumeration)"},
306 { TRUE
, 'E', "event", "events"},
307 { TRUE
, 'f', "field", "fields"},
308 { TRUE
, 'g', "enum", "enumeration names"},
309 { TRUE
, 'i', "interface", "interfaces"},
310 { FALSE
, 'l', "local", "local variables"},
311 { TRUE
, 'm', "method", "methods"},
312 { TRUE
, 'n', "namespace", "namespaces"},
313 { TRUE
, 'p', "property", "properties"},
314 { TRUE
, 's', "struct", "structure names"},
315 { TRUE
, 't', "typedef", "typedefs"},
318 /* Used to index into the JavaKinds table. */
321 JK_CLASS
, JK_ENUM_CONSTANT
, JK_FIELD
, JK_ENUM
, JK_INTERFACE
,
322 JK_LOCAL
, JK_METHOD
, JK_PACKAGE
, JK_ACCESS
, JK_CLASS_PREFIX
325 static kindOption JavaKinds
[] = {
326 { TRUE
, 'c', "class", "classes"},
327 { TRUE
, 'e', "enum constant", "enum constants"},
328 { TRUE
, 'f', "field", "fields"},
329 { TRUE
, 'g', "enum", "enum types"},
330 { TRUE
, 'i', "interface", "interfaces"},
331 { FALSE
, 'l', "local", "local variables"},
332 { TRUE
, 'm', "method", "methods"},
333 { TRUE
, 'p', "package", "packages"},
336 /* Used to index into the VeraKinds table. */
339 VK_CLASS
, VK_DEFINE
, VK_ENUMERATOR
, VK_FUNCTION
,
340 VK_ENUMERATION
, VK_LOCAL
, VK_MEMBER
, VK_PROGRAM
, VK_PROTOTYPE
,
341 VK_TASK
, VK_TYPEDEF
, VK_VARIABLE
,
345 static kindOption VeraKinds
[] = {
346 { TRUE
, 'c', "class", "classes"},
347 { TRUE
, 'd', "macro", "macro definitions"},
348 { TRUE
, 'e', "enumerator", "enumerators (values inside an enumeration)"},
349 { TRUE
, 'f', "function", "function definitions"},
350 { TRUE
, 'g', "enum", "enumeration names"},
351 { FALSE
, 'l', "local", "local variables"},
352 { TRUE
, 'm', "member", "class, struct, and union members"},
353 { TRUE
, 'p', "program", "programs"},
354 { FALSE
, 'P', "prototype", "function prototypes"},
355 { TRUE
, 't', "task", "tasks"},
356 { TRUE
, 'T', "typedef", "typedefs"},
357 { TRUE
, 'v', "variable", "variable definitions"},
358 { FALSE
, 'x', "externvar", "external variable declarations"}
361 static const keywordDesc KeywordTable
[] = {
363 /* ANSI C | C# Java */
365 /* keyword keyword ID | | | | | */
366 { "__attribute__", KEYWORD_ATTRIBUTE
, { 1, 1, 1, 0, 0 } },
367 { "abstract", KEYWORD_ABSTRACT
, { 0, 0, 1, 1, 0 } },
368 { "bad_state", KEYWORD_BAD_STATE
, { 0, 0, 0, 0, 1 } },
369 { "bad_trans", KEYWORD_BAD_TRANS
, { 0, 0, 0, 0, 1 } },
370 { "bind", KEYWORD_BIND
, { 0, 0, 0, 0, 1 } },
371 { "bind_var", KEYWORD_BIND_VAR
, { 0, 0, 0, 0, 1 } },
372 { "bit", KEYWORD_BIT
, { 0, 0, 0, 0, 1 } },
373 { "boolean", KEYWORD_BOOLEAN
, { 0, 0, 0, 1, 0 } },
374 { "byte", KEYWORD_BYTE
, { 0, 0, 0, 1, 0 } },
375 { "case", KEYWORD_CASE
, { 1, 1, 1, 1, 0 } },
376 { "catch", KEYWORD_CATCH
, { 0, 1, 1, 0, 0 } },
377 { "char", KEYWORD_CHAR
, { 1, 1, 1, 1, 0 } },
378 { "class", KEYWORD_CLASS
, { 0, 1, 1, 1, 1 } },
379 { "const", KEYWORD_CONST
, { 1, 1, 1, 1, 0 } },
380 { "constraint", KEYWORD_CONSTRAINT
, { 0, 0, 0, 0, 1 } },
381 { "coverage_block", KEYWORD_COVERAGE_BLOCK
, { 0, 0, 0, 0, 1 } },
382 { "coverage_def", KEYWORD_COVERAGE_DEF
, { 0, 0, 0, 0, 1 } },
383 { "do", KEYWORD_DO
, { 1, 1, 1, 1, 0 } },
384 { "default", KEYWORD_DEFAULT
, { 1, 1, 1, 1, 0 } },
385 { "delegate", KEYWORD_DELEGATE
, { 0, 0, 1, 0, 0 } },
386 { "delete", KEYWORD_DELETE
, { 0, 1, 0, 0, 0 } },
387 { "double", KEYWORD_DOUBLE
, { 1, 1, 1, 1, 0 } },
388 { "else", KEYWORD_ELSE
, { 1, 1, 1, 1, 0 } },
389 { "enum", KEYWORD_ENUM
, { 1, 1, 1, 1, 1 } },
390 { "event", KEYWORD_EVENT
, { 0, 0, 1, 0, 1 } },
391 { "explicit", KEYWORD_EXPLICIT
, { 0, 1, 1, 0, 0 } },
392 { "extends", KEYWORD_EXTENDS
, { 0, 0, 0, 1, 1 } },
393 { "extern", KEYWORD_EXTERN
, { 1, 1, 1, 0, 1 } },
394 { "final", KEYWORD_FINAL
, { 0, 0, 0, 1, 0 } },
395 { "float", KEYWORD_FLOAT
, { 1, 1, 1, 1, 0 } },
396 { "for", KEYWORD_FOR
, { 1, 1, 1, 1, 0 } },
397 { "foreach", KEYWORD_FOREACH
, { 0, 0, 1, 0, 0 } },
398 { "friend", KEYWORD_FRIEND
, { 0, 1, 0, 0, 0 } },
399 { "function", KEYWORD_FUNCTION
, { 0, 0, 0, 0, 1 } },
400 { "goto", KEYWORD_GOTO
, { 1, 1, 1, 1, 0 } },
401 { "if", KEYWORD_IF
, { 1, 1, 1, 1, 0 } },
402 { "implements", KEYWORD_IMPLEMENTS
, { 0, 0, 0, 1, 0 } },
403 { "import", KEYWORD_IMPORT
, { 0, 0, 0, 1, 0 } },
404 { "inline", KEYWORD_INLINE
, { 0, 1, 0, 0, 0 } },
405 { "inout", KEYWORD_INOUT
, { 0, 0, 0, 0, 1 } },
406 { "input", KEYWORD_INPUT
, { 0, 0, 0, 0, 1 } },
407 { "int", KEYWORD_INT
, { 1, 1, 1, 1, 0 } },
408 { "integer", KEYWORD_INTEGER
, { 0, 0, 0, 0, 1 } },
409 { "interface", KEYWORD_INTERFACE
, { 0, 0, 1, 1, 1 } },
410 { "internal", KEYWORD_INTERNAL
, { 0, 0, 1, 0, 0 } },
411 { "local", KEYWORD_LOCAL
, { 0, 0, 0, 0, 1 } },
412 { "long", KEYWORD_LONG
, { 1, 1, 1, 1, 0 } },
413 { "m_bad_state", KEYWORD_M_BAD_STATE
, { 0, 0, 0, 0, 1 } },
414 { "m_bad_trans", KEYWORD_M_BAD_TRANS
, { 0, 0, 0, 0, 1 } },
415 { "m_state", KEYWORD_M_STATE
, { 0, 0, 0, 0, 1 } },
416 { "m_trans", KEYWORD_M_TRANS
, { 0, 0, 0, 0, 1 } },
417 { "mutable", KEYWORD_MUTABLE
, { 0, 1, 0, 0, 0 } },
418 { "namespace", KEYWORD_NAMESPACE
, { 0, 1, 1, 0, 0 } },
419 { "native", KEYWORD_NATIVE
, { 0, 0, 0, 1, 0 } },
420 { "new", KEYWORD_NEW
, { 0, 1, 1, 1, 0 } },
421 { "newcov", KEYWORD_NEWCOV
, { 0, 0, 0, 0, 1 } },
422 { "operator", KEYWORD_OPERATOR
, { 0, 1, 1, 0, 0 } },
423 { "output", KEYWORD_OUTPUT
, { 0, 0, 0, 0, 1 } },
424 { "overload", KEYWORD_OVERLOAD
, { 0, 1, 0, 0, 0 } },
425 { "override", KEYWORD_OVERRIDE
, { 0, 0, 1, 0, 0 } },
426 { "package", KEYWORD_PACKAGE
, { 0, 0, 0, 1, 0 } },
427 { "packed", KEYWORD_PACKED
, { 0, 0, 0, 0, 1 } },
428 { "port", KEYWORD_PORT
, { 0, 0, 0, 0, 1 } },
429 { "private", KEYWORD_PRIVATE
, { 0, 1, 1, 1, 0 } },
430 { "program", KEYWORD_PROGRAM
, { 0, 0, 0, 0, 1 } },
431 { "protected", KEYWORD_PROTECTED
, { 0, 1, 1, 1, 1 } },
432 { "public", KEYWORD_PUBLIC
, { 0, 1, 1, 1, 1 } },
433 { "register", KEYWORD_REGISTER
, { 1, 1, 0, 0, 0 } },
434 { "return", KEYWORD_RETURN
, { 1, 1, 1, 1, 0 } },
435 { "shadow", KEYWORD_SHADOW
, { 0, 0, 0, 0, 1 } },
436 { "short", KEYWORD_SHORT
, { 1, 1, 1, 1, 0 } },
437 { "signed", KEYWORD_SIGNED
, { 1, 1, 0, 0, 0 } },
438 { "state", KEYWORD_STATE
, { 0, 0, 0, 0, 1 } },
439 { "static", KEYWORD_STATIC
, { 1, 1, 1, 1, 1 } },
440 { "string", KEYWORD_STRING
, { 0, 0, 1, 0, 1 } },
441 { "struct", KEYWORD_STRUCT
, { 1, 1, 1, 0, 0 } },
442 { "switch", KEYWORD_SWITCH
, { 1, 1, 1, 1, 0 } },
443 { "synchronized", KEYWORD_SYNCHRONIZED
, { 0, 0, 0, 1, 0 } },
444 { "task", KEYWORD_TASK
, { 0, 0, 0, 0, 1 } },
445 { "template", KEYWORD_TEMPLATE
, { 0, 1, 0, 0, 0 } },
446 { "this", KEYWORD_THIS
, { 0, 1, 1, 1, 0 } },
447 { "throw", KEYWORD_THROW
, { 0, 1, 1, 1, 0 } },
448 { "throws", KEYWORD_THROWS
, { 0, 0, 0, 1, 0 } },
449 { "trans", KEYWORD_TRANS
, { 0, 0, 0, 0, 1 } },
450 { "transition", KEYWORD_TRANSITION
, { 0, 0, 0, 0, 1 } },
451 { "transient", KEYWORD_TRANSIENT
, { 0, 0, 0, 1, 0 } },
452 { "try", KEYWORD_TRY
, { 0, 1, 1, 0, 0 } },
453 { "typedef", KEYWORD_TYPEDEF
, { 1, 1, 1, 0, 1 } },
454 { "typename", KEYWORD_TYPENAME
, { 0, 1, 0, 0, 0 } },
455 { "uint", KEYWORD_UINT
, { 0, 0, 1, 0, 0 } },
456 { "ulong", KEYWORD_ULONG
, { 0, 0, 1, 0, 0 } },
457 { "union", KEYWORD_UNION
, { 1, 1, 0, 0, 0 } },
458 { "unsigned", KEYWORD_UNSIGNED
, { 1, 1, 1, 0, 0 } },
459 { "ushort", KEYWORD_USHORT
, { 0, 0, 1, 0, 0 } },
460 { "using", KEYWORD_USING
, { 0, 1, 1, 0, 0 } },
461 { "virtual", KEYWORD_VIRTUAL
, { 0, 1, 1, 0, 1 } },
462 { "void", KEYWORD_VOID
, { 1, 1, 1, 1, 1 } },
463 { "volatile", KEYWORD_VOLATILE
, { 1, 1, 1, 1, 0 } },
464 { "wchar_t", KEYWORD_WCHAR_T
, { 1, 1, 1, 0, 0 } },
465 { "while", KEYWORD_WHILE
, { 1, 1, 1, 1, 0 } }
469 * FUNCTION PROTOTYPES
471 static void createTags (const unsigned int nestLevel
, statementInfo
*const parent
);
474 * FUNCTION DEFINITIONS
477 extern boolean
includingDefineTags (void)
479 return CKinds
[CK_DEFINE
].enabled
;
486 static void initToken (tokenInfo
* const token
)
488 token
->type
= TOKEN_NONE
;
489 token
->keyword
= KEYWORD_NONE
;
490 token
->lineNumber
= getSourceLineNumber ();
491 token
->filePosition
= getInputFilePosition ();
492 vStringClear (token
->name
);
495 static void advanceToken (statementInfo
* const st
)
497 if (st
->tokenIndex
>= (unsigned int) NumTokens
- 1)
501 initToken (st
->token
[st
->tokenIndex
]);
504 static tokenInfo
*prevToken (const statementInfo
*const st
, unsigned int n
)
506 unsigned int tokenIndex
;
507 unsigned int num
= (unsigned int) NumTokens
;
509 tokenIndex
= (st
->tokenIndex
+ num
- n
) % num
;
510 return st
->token
[tokenIndex
];
513 static void setToken (statementInfo
*const st
, const tokenType type
)
516 token
= activeToken (st
);
521 static void retardToken (statementInfo
*const st
)
523 if (st
->tokenIndex
== 0)
524 st
->tokenIndex
= (unsigned int) NumTokens
- 1;
527 setToken (st
, TOKEN_NONE
);
530 static tokenInfo
*newToken (void)
532 tokenInfo
*const token
= xMalloc (1, tokenInfo
);
533 token
->name
= vStringNew ();
538 static void deleteToken (tokenInfo
*const token
)
542 vStringDelete (token
->name
);
547 static const char *accessString (const accessType access
)
549 static const char *const names
[] = {
550 "?", "local", "private", "protected", "public", "default"
552 Assert (sizeof (names
) / sizeof (names
[0]) == ACCESS_COUNT
);
553 Assert ((int) access
< ACCESS_COUNT
);
554 return names
[(int) access
];
557 static const char *implementationString (const impType imp
)
559 static const char *const names
[] ={
560 "?", "abstract", "virtual", "pure virtual"
562 Assert (sizeof (names
) / sizeof (names
[0]) == IMP_COUNT
);
563 Assert ((int) imp
< IMP_COUNT
);
564 return names
[(int) imp
];
568 * Debugging functions
573 #define boolString(c) ((c) ? "TRUE" : "FALSE")
575 static const char *tokenString (const tokenType type
)
577 static const char *const names
[] = {
578 "none", "args", "}", "{", "colon", "comma", "double colon", "keyword",
579 "name", "package", "paren-name", "semicolon", "specifier", "star", "ampersand"
581 Assert (sizeof (names
) / sizeof (names
[0]) == TOKEN_COUNT
);
582 Assert ((int) type
< TOKEN_COUNT
);
583 return names
[(int) type
];
586 static const char *scopeString (const tagScope scope
)
588 static const char *const names
[] = {
589 "global", "static", "extern", "friend", "typedef"
591 Assert (sizeof (names
) / sizeof (names
[0]) == SCOPE_COUNT
);
592 Assert ((int) scope
< SCOPE_COUNT
);
593 return names
[(int) scope
];
596 static const char *declString (const declType declaration
)
598 static const char *const names
[] = {
599 "?", "base", "class", "enum", "event", "function", "ignore",
600 "interface", "namespace", "no mangle", "package", "program",
601 "struct", "task", "union",
603 Assert (sizeof (names
) / sizeof (names
[0]) == DECL_COUNT
);
604 Assert ((int) declaration
< DECL_COUNT
);
605 return names
[(int) declaration
];
608 static const char *keywordString (const keywordId keyword
)
610 const size_t count
= sizeof (KeywordTable
) / sizeof (KeywordTable
[0]);
611 const char *name
= "none";
613 for (i
= 0 ; i
< count
; ++i
)
615 const keywordDesc
*p
= &KeywordTable
[i
];
616 if (p
->id
== keyword
)
625 static void __unused__
pt (tokenInfo
*const token
)
627 if (isType (token
, TOKEN_NAME
))
628 printf ("type: %-12s: %-13s line: %lu\n",
629 tokenString (token
->type
), vStringValue (token
->name
),
631 else if (isType (token
, TOKEN_KEYWORD
))
632 printf ("type: %-12s: %-13s line: %lu\n",
633 tokenString (token
->type
), keywordString (token
->keyword
),
636 printf ("type: %-12s line: %lu\n",
637 tokenString (token
->type
), token
->lineNumber
);
640 static void __unused__
ps (statementInfo
*const st
)
643 printf ("scope: %s decl: %s gotName: %s gotParenName: %s isPointer: %s\n",
644 scopeString (st
->scope
), declString (st
->declaration
),
645 boolString (st
->gotName
), boolString (st
->gotParenName
), boolString (st
->isPointer
));
646 printf ("haveQualifyingName: %s\n", boolString (st
->haveQualifyingName
));
647 printf ("access: %s default: %s\n", accessString (st
->member
.access
),
648 accessString (st
->member
.accessDefault
));
649 printf ("active token : ");
650 pt (activeToken (st
));
651 for (i
= 1 ; i
< (unsigned int) NumTokens
; ++i
)
653 printf ("prev %u : ", i
);
654 pt (prevToken (st
, i
));
656 printf ("context: ");
663 * Statement management
666 static boolean
isContextualKeyword (const tokenInfo
*const token
)
669 switch (token
->keyword
)
673 case KEYWORD_INTERFACE
:
674 case KEYWORD_NAMESPACE
:
680 default: result
= FALSE
; break;
685 static boolean
isContextualStatement (const statementInfo
*const st
)
687 boolean result
= FALSE
;
688 if (st
!= NULL
) switch (st
->declaration
)
699 default: result
= FALSE
; break;
704 static boolean
isMember (const statementInfo
*const st
)
707 if (isType (st
->context
, TOKEN_NAME
))
711 (st
->parent
!= NULL
&& isContextualStatement (st
->parent
));
715 static void initMemberInfo (statementInfo
*const st
)
717 accessType accessDefault
= ACCESS_UNDEFINED
;
719 if (st
->parent
!= NULL
) switch (st
->parent
->declaration
)
722 accessDefault
= (isLanguage (Lang_java
) ? ACCESS_PUBLIC
: ACCESS_UNDEFINED
);
725 accessDefault
= ACCESS_UNDEFINED
;
729 if (isLanguage (Lang_java
))
730 accessDefault
= ACCESS_DEFAULT
;
732 accessDefault
= ACCESS_PRIVATE
;
738 accessDefault
= ACCESS_PUBLIC
;
743 st
->member
.accessDefault
= accessDefault
;
744 st
->member
.access
= accessDefault
;
747 static void reinitStatement (statementInfo
*const st
, const boolean partial
)
753 st
->scope
= SCOPE_GLOBAL
;
754 if (isContextualStatement (st
->parent
))
755 st
->declaration
= DECL_BASE
;
757 st
->declaration
= DECL_NONE
;
759 st
->gotParenName
= FALSE
;
760 st
->isPointer
= FALSE
;
761 st
->inFunction
= FALSE
;
762 st
->assignment
= FALSE
;
763 st
->notVariable
= FALSE
;
764 st
->implementation
= IMP_DEFAULT
;
767 st
->haveQualifyingName
= FALSE
;
770 if (st
->parent
!= NULL
)
771 st
->inFunction
= st
->parent
->inFunction
;
773 for (i
= 0 ; i
< (unsigned int) NumTokens
; ++i
)
774 initToken (st
->token
[i
]);
776 initToken (st
->context
);
778 /* Keep the block name, so that a variable following after a comma will
779 * still have the structure name.
782 initToken (st
->blockName
);
784 vStringClear (st
->parentClasses
);
789 st
->member
.access
= st
->member
.accessDefault
;
792 static void initStatement (statementInfo
*const st
, statementInfo
*const parent
)
796 reinitStatement (st
, FALSE
);
800 * Tag generation functions
802 static cKind
cTagKind (const tagType type
)
804 cKind result
= CK_UNDEFINED
;
807 case TAG_CLASS
: result
= CK_CLASS
; break;
808 case TAG_ENUM
: result
= CK_ENUMERATION
; break;
809 case TAG_ENUMERATOR
: result
= CK_ENUMERATOR
; break;
810 case TAG_FUNCTION
: result
= CK_FUNCTION
; break;
811 case TAG_LOCAL
: result
= CK_LOCAL
; break;
812 case TAG_MEMBER
: result
= CK_MEMBER
; break;
813 case TAG_NAMESPACE
: result
= CK_NAMESPACE
; break;
814 case TAG_PROTOTYPE
: result
= CK_PROTOTYPE
; break;
815 case TAG_STRUCT
: result
= CK_STRUCT
; break;
816 case TAG_TYPEDEF
: result
= CK_TYPEDEF
; break;
817 case TAG_UNION
: result
= CK_UNION
; break;
818 case TAG_VARIABLE
: result
= CK_VARIABLE
; break;
819 case TAG_EXTERN_VAR
: result
= CK_EXTERN_VARIABLE
; break;
821 default: Assert ("Bad C tag type" == NULL
); break;
826 static csharpKind
csharpTagKind (const tagType type
)
828 csharpKind result
= CSK_UNDEFINED
;
831 case TAG_CLASS
: result
= CSK_CLASS
; break;
832 case TAG_ENUM
: result
= CSK_ENUMERATION
; break;
833 case TAG_ENUMERATOR
: result
= CSK_ENUMERATOR
; break;
834 case TAG_EVENT
: result
= CSK_EVENT
; break;
835 case TAG_FIELD
: result
= CSK_FIELD
; break;
836 case TAG_INTERFACE
: result
= CSK_INTERFACE
; break;
837 case TAG_LOCAL
: result
= CSK_LOCAL
; break;
838 case TAG_METHOD
: result
= CSK_METHOD
; break;
839 case TAG_NAMESPACE
: result
= CSK_NAMESPACE
; break;
840 case TAG_PROPERTY
: result
= CSK_PROPERTY
; break;
841 case TAG_STRUCT
: result
= CSK_STRUCT
; break;
842 case TAG_TYPEDEF
: result
= CSK_TYPEDEF
; break;
844 default: Assert ("Bad C# tag type" == NULL
); break;
849 static javaKind
javaTagKind (const tagType type
)
851 javaKind result
= JK_UNDEFINED
;
854 case TAG_CLASS
: result
= JK_CLASS
; break;
855 case TAG_ENUM
: result
= JK_ENUM
; break;
856 case TAG_ENUMERATOR
: result
= JK_ENUM_CONSTANT
; break;
857 case TAG_FIELD
: result
= JK_FIELD
; break;
858 case TAG_INTERFACE
: result
= JK_INTERFACE
; break;
859 case TAG_LOCAL
: result
= JK_LOCAL
; break;
860 case TAG_METHOD
: result
= JK_METHOD
; break;
861 case TAG_PACKAGE
: result
= JK_PACKAGE
; break;
863 default: Assert ("Bad Java tag type" == NULL
); break;
868 static veraKind
veraTagKind (const tagType type
) {
869 veraKind result
= VK_UNDEFINED
;
872 case TAG_CLASS
: result
= VK_CLASS
; break;
873 case TAG_ENUM
: result
= VK_ENUMERATION
; break;
874 case TAG_ENUMERATOR
: result
= VK_ENUMERATOR
; break;
875 case TAG_FUNCTION
: result
= VK_FUNCTION
; break;
876 case TAG_LOCAL
: result
= VK_LOCAL
; break;
877 case TAG_MEMBER
: result
= VK_MEMBER
; break;
878 case TAG_PROGRAM
: result
= VK_PROGRAM
; break;
879 case TAG_PROTOTYPE
: result
= VK_PROTOTYPE
; break;
880 case TAG_TASK
: result
= VK_TASK
; break;
881 case TAG_TYPEDEF
: result
= VK_TYPEDEF
; break;
882 case TAG_VARIABLE
: result
= VK_VARIABLE
; break;
883 case TAG_EXTERN_VAR
: result
= VK_EXTERN_VARIABLE
; break;
885 default: Assert ("Bad Vera tag type" == NULL
); break;
890 static const char *tagName (const tagType type
)
893 if (isLanguage (Lang_csharp
))
894 result
= CsharpKinds
[csharpTagKind (type
)].name
;
895 else if (isLanguage (Lang_java
))
896 result
= JavaKinds
[javaTagKind (type
)].name
;
897 else if (isLanguage (Lang_vera
))
898 result
= VeraKinds
[veraTagKind (type
)].name
;
900 result
= CKinds
[cTagKind (type
)].name
;
904 static int tagLetter (const tagType type
)
907 if (isLanguage (Lang_csharp
))
908 result
= CsharpKinds
[csharpTagKind (type
)].letter
;
909 else if (isLanguage (Lang_java
))
910 result
= JavaKinds
[javaTagKind (type
)].letter
;
911 else if (isLanguage (Lang_vera
))
912 result
= VeraKinds
[veraTagKind (type
)].letter
;
914 result
= CKinds
[cTagKind (type
)].letter
;
918 static boolean
includeTag (const tagType type
, const boolean isFileScope
)
921 if (isFileScope
&& ! Option
.include
.fileScope
)
923 else if (isLanguage (Lang_csharp
))
924 result
= CsharpKinds
[csharpTagKind (type
)].enabled
;
925 else if (isLanguage (Lang_java
))
926 result
= JavaKinds
[javaTagKind (type
)].enabled
;
927 else if (isLanguage (Lang_vera
))
928 result
= VeraKinds
[veraTagKind (type
)].enabled
;
930 result
= CKinds
[cTagKind (type
)].enabled
;
934 static tagType
declToTagType (const declType declaration
)
936 tagType type
= TAG_UNDEFINED
;
940 case DECL_CLASS
: type
= TAG_CLASS
; break;
941 case DECL_ENUM
: type
= TAG_ENUM
; break;
942 case DECL_EVENT
: type
= TAG_EVENT
; break;
943 case DECL_FUNCTION
: type
= TAG_FUNCTION
; break;
944 case DECL_INTERFACE
: type
= TAG_INTERFACE
; break;
945 case DECL_NAMESPACE
: type
= TAG_NAMESPACE
; break;
946 case DECL_PROGRAM
: type
= TAG_PROGRAM
; break;
947 case DECL_TASK
: type
= TAG_TASK
; break;
948 case DECL_STRUCT
: type
= TAG_STRUCT
; break;
949 case DECL_UNION
: type
= TAG_UNION
; break;
951 default: Assert ("Unexpected declaration" == NULL
); break;
956 static const char* accessField (const statementInfo
*const st
)
958 const char* result
= NULL
;
959 if (isLanguage (Lang_cpp
) && st
->scope
== SCOPE_FRIEND
)
961 else if (st
->member
.access
!= ACCESS_UNDEFINED
)
962 result
= accessString (st
->member
.access
);
966 static void addContextSeparator (vString
*const scope
)
968 if (isLanguage (Lang_c
) || isLanguage (Lang_cpp
))
969 vStringCatS (scope
, "::");
970 else if (isLanguage (Lang_java
) || isLanguage (Lang_csharp
))
971 vStringCatS (scope
, ".");
974 static void addOtherFields (tagEntryInfo
* const tag
, const tagType type
,
975 const statementInfo
*const st
,
976 vString
*const scope
, vString
*const typeRef
)
978 /* For selected tag types, append an extension flag designating the
979 * parent object in which the tag is defined.
988 if (vStringLength (Signature
) > 0)
990 tag
->extensionFields
.signature
= vStringValue (Signature
);
993 if (vStringLength (ReturnType
) > 0)
995 tag
->extensionFields
.returnType
= vStringValue (ReturnType
);
1010 if (vStringLength (scope
) > 0 &&
1011 (isMember (st
) || st
->parent
->declaration
== DECL_NAMESPACE
))
1013 if (isType (st
->context
, TOKEN_NAME
))
1014 tag
->extensionFields
.scope
[0] = tagName (TAG_CLASS
);
1016 tag
->extensionFields
.scope
[0] =
1017 tagName (declToTagType (parentDecl (st
)));
1018 tag
->extensionFields
.scope
[1] = vStringValue (scope
);
1020 if ((type
== TAG_CLASS
|| type
== TAG_INTERFACE
||
1021 type
== TAG_STRUCT
) && vStringLength (st
->parentClasses
) > 0)
1024 tag
->extensionFields
.inheritance
=
1025 vStringValue (st
->parentClasses
);
1027 if (st
->implementation
!= IMP_DEFAULT
&&
1028 (isLanguage (Lang_cpp
) || isLanguage (Lang_csharp
) ||
1029 isLanguage (Lang_java
)))
1031 tag
->extensionFields
.implementation
=
1032 implementationString (st
->implementation
);
1036 tag
->extensionFields
.access
= accessField (st
);
1041 /* Add typename info, type of the tag and name of struct/union/etc. */
1042 if ((type
== TAG_TYPEDEF
|| type
== TAG_VARIABLE
|| type
== TAG_MEMBER
)
1043 && isContextualStatement(st
))
1047 tag
->extensionFields
.typeRef
[0] =
1048 tagName (declToTagType (st
->declaration
));
1049 p
= vStringValue (st
->blockName
->name
);
1051 /* If there was no {} block get the name from the token before the
1052 * name (current token is ';' or ',', previous token is the name).
1054 if (p
== NULL
|| *p
== '\0')
1056 tokenInfo
*const prev2
= prevToken (st
, 2);
1057 if (isType (prev2
, TOKEN_NAME
))
1058 p
= vStringValue (prev2
->name
);
1061 /* Prepend the scope name if there is one. */
1062 if (vStringLength (scope
) > 0)
1064 vStringCopy(typeRef
, scope
);
1065 addContextSeparator (typeRef
);
1066 vStringCatS(typeRef
, p
);
1067 p
= vStringValue (typeRef
);
1069 tag
->extensionFields
.typeRef
[1] = p
;
1073 static void findScopeHierarchy (vString
*const string
,
1074 const statementInfo
*const st
)
1076 vStringClear (string
);
1077 if (isType (st
->context
, TOKEN_NAME
))
1078 vStringCopy (string
, st
->context
->name
);
1079 if (st
->parent
!= NULL
)
1081 vString
*temp
= vStringNew ();
1082 const statementInfo
*s
;
1083 for (s
= st
->parent
; s
!= NULL
; s
= s
->parent
)
1085 if (isContextualStatement (s
) ||
1086 s
->declaration
== DECL_NAMESPACE
||
1087 s
->declaration
== DECL_PROGRAM
)
1089 vStringCopy (temp
, string
);
1090 vStringClear (string
);
1091 Assert (isType (s
->blockName
, TOKEN_NAME
));
1092 if (isType (s
->context
, TOKEN_NAME
) &&
1093 vStringLength (s
->context
->name
) > 0)
1095 vStringCat (string
, s
->context
->name
);
1096 addContextSeparator (string
);
1098 vStringCat (string
, s
->blockName
->name
);
1099 if (vStringLength (temp
) > 0)
1100 addContextSeparator (string
);
1101 vStringCat (string
, temp
);
1104 vStringDelete (temp
);
1108 static void makeExtraTagEntry (const tagType type
, tagEntryInfo
*const e
,
1109 vString
*const scope
)
1111 if (Option
.include
.qualifiedTags
&&
1112 scope
!= NULL
&& vStringLength (scope
) > 0)
1114 vString
*const scopedName
= vStringNew ();
1116 if (type
!= TAG_ENUMERATOR
)
1117 vStringCopy (scopedName
, scope
);
1120 /* remove last component (i.e. enumeration name) from scope */
1121 const char* const sc
= vStringValue (scope
);
1122 const char* colon
= strrchr (sc
, ':');
1125 while (*colon
== ':' && colon
> sc
)
1127 vStringNCopy (scopedName
, scope
, colon
+ 1 - sc
);
1130 if (vStringLength (scopedName
) > 0)
1132 addContextSeparator (scopedName
);
1133 vStringCatS (scopedName
, e
->name
);
1134 e
->name
= vStringValue (scopedName
);
1137 vStringDelete (scopedName
);
1141 static void makeTag (const tokenInfo
*const token
,
1142 const statementInfo
*const st
,
1143 boolean isFileScope
, const tagType type
)
1145 /* Nothing is really of file scope when it appears in a header file.
1147 isFileScope
= (boolean
) (isFileScope
&& ! isHeaderFile ());
1149 if (isType (token
, TOKEN_NAME
) && vStringLength (token
->name
) > 0 &&
1150 includeTag (type
, isFileScope
))
1152 vString
*scope
= vStringNew ();
1153 /* Use "typeRef" to store the typename from addOtherFields() until
1154 * it's used in makeTagEntry().
1156 vString
*typeRef
= vStringNew ();
1159 initTagEntry (&e
, vStringValue (token
->name
));
1161 e
.lineNumber
= token
->lineNumber
;
1162 e
.filePosition
= token
->filePosition
;
1163 e
.isFileScope
= isFileScope
;
1164 e
.kindName
= tagName (type
);
1165 e
.kind
= tagLetter (type
);
1167 findScopeHierarchy (scope
, st
);
1168 addOtherFields (&e
, type
, st
, scope
, typeRef
);
1171 makeExtraTagEntry (type
, &e
, scope
);
1172 vStringDelete (scope
);
1173 vStringDelete (typeRef
);
1177 static boolean
isValidTypeSpecifier (const declType declaration
)
1180 switch (declaration
)
1198 static void qualifyEnumeratorTag (const statementInfo
*const st
,
1199 const tokenInfo
*const nameToken
)
1201 if (isType (nameToken
, TOKEN_NAME
))
1202 makeTag (nameToken
, st
, TRUE
, TAG_ENUMERATOR
);
1205 static void qualifyFunctionTag (const statementInfo
*const st
,
1206 const tokenInfo
*const nameToken
)
1208 if (isType (nameToken
, TOKEN_NAME
))
1211 const boolean isFileScope
=
1212 (boolean
) (st
->member
.access
== ACCESS_PRIVATE
||
1213 (!isMember (st
) && st
->scope
== SCOPE_STATIC
));
1214 if (isLanguage (Lang_java
) || isLanguage (Lang_csharp
))
1216 else if (isLanguage (Lang_vera
) && st
->declaration
== DECL_TASK
)
1219 type
= TAG_FUNCTION
;
1220 makeTag (nameToken
, st
, isFileScope
, type
);
1224 static void qualifyFunctionDeclTag (const statementInfo
*const st
,
1225 const tokenInfo
*const nameToken
)
1227 if (! isType (nameToken
, TOKEN_NAME
))
1229 else if (isLanguage (Lang_java
) || isLanguage (Lang_csharp
))
1230 qualifyFunctionTag (st
, nameToken
);
1231 else if (st
->scope
== SCOPE_TYPEDEF
)
1232 makeTag (nameToken
, st
, TRUE
, TAG_TYPEDEF
);
1233 else if (isValidTypeSpecifier (st
->declaration
) && ! isLanguage (Lang_csharp
))
1234 makeTag (nameToken
, st
, TRUE
, TAG_PROTOTYPE
);
1237 static void qualifyCompoundTag (const statementInfo
*const st
,
1238 const tokenInfo
*const nameToken
)
1240 if (isType (nameToken
, TOKEN_NAME
))
1242 const tagType type
= declToTagType (st
->declaration
);
1243 const boolean fileScoped
= (boolean
)
1244 (!(isLanguage (Lang_java
) ||
1245 isLanguage (Lang_csharp
) ||
1246 isLanguage (Lang_vera
)));
1248 if (type
!= TAG_UNDEFINED
)
1249 makeTag (nameToken
, st
, fileScoped
, type
);
1253 static void qualifyBlockTag (statementInfo
*const st
,
1254 const tokenInfo
*const nameToken
)
1256 switch (st
->declaration
)
1260 case DECL_INTERFACE
:
1261 case DECL_NAMESPACE
:
1265 qualifyCompoundTag (st
, nameToken
);
1271 static void qualifyVariableTag (const statementInfo
*const st
,
1272 const tokenInfo
*const nameToken
)
1274 /* We have to watch that we do not interpret a declaration of the
1275 * form "struct tag;" as a variable definition. In such a case, the
1276 * token preceding the name will be a keyword.
1278 if (! isType (nameToken
, TOKEN_NAME
))
1280 else if (st
->scope
== SCOPE_TYPEDEF
)
1281 makeTag (nameToken
, st
, TRUE
, TAG_TYPEDEF
);
1282 else if (st
->declaration
== DECL_EVENT
)
1283 makeTag (nameToken
, st
, (boolean
) (st
->member
.access
== ACCESS_PRIVATE
),
1285 else if (st
->declaration
== DECL_PACKAGE
)
1286 makeTag (nameToken
, st
, FALSE
, TAG_PACKAGE
);
1287 else if (isValidTypeSpecifier (st
->declaration
))
1289 if (st
->notVariable
)
1291 else if (isMember (st
))
1293 if (isLanguage (Lang_java
) || isLanguage (Lang_csharp
))
1294 makeTag (nameToken
, st
,
1295 (boolean
) (st
->member
.access
== ACCESS_PRIVATE
), TAG_FIELD
);
1296 else if (st
->scope
== SCOPE_GLOBAL
|| st
->scope
== SCOPE_STATIC
)
1297 makeTag (nameToken
, st
, TRUE
, TAG_MEMBER
);
1301 if (st
->scope
== SCOPE_EXTERN
|| ! st
->haveQualifyingName
)
1302 makeTag (nameToken
, st
, FALSE
, TAG_EXTERN_VAR
);
1303 else if (st
->inFunction
)
1304 makeTag (nameToken
, st
, (boolean
) (st
->scope
== SCOPE_STATIC
),
1307 makeTag (nameToken
, st
, (boolean
) (st
->scope
== SCOPE_STATIC
),
1317 static int skipToOneOf (const char *const chars
)
1322 while (c
!= EOF
&& c
!= '\0' && strchr (chars
, c
) == NULL
);
1326 /* Skip to the next non-white character.
1328 static int skipToNonWhite (void)
1330 boolean found
= FALSE
;
1336 while (isspace (c
));
1346 if (CollectingSignature
&& found
)
1347 vStringPut (Signature
, ' ');
1353 /* Skips to the next brace in column 1. This is intended for cases where
1354 * preprocessor constructs result in unbalanced braces.
1356 static void skipToFormattedBraceMatch (void)
1362 while (c
!= EOF
&& (c
!= '\n' || next
!= '}'))
1369 /* Skip to the matching character indicated by the pair string. If skipping
1370 * to a matching brace and any brace is found within a different level of a
1371 * #if conditional statement while brace formatting is in effect, we skip to
1372 * the brace matched by its formatting. It is assumed that we have already
1373 * read the character which starts the group (i.e. the first character of
1376 static void skipToMatch (const char *const pair
)
1378 const boolean braceMatching
= (boolean
) (strcmp ("{}", pair
) == 0);
1379 const boolean braceFormatting
= (boolean
) (isBraceFormat () && braceMatching
);
1380 const unsigned int initialLevel
= getDirectiveNestLevel ();
1381 const int begin
= pair
[0], end
= pair
[1];
1382 const unsigned long inputLineNumber
= getInputLineNumber ();
1386 while (matchLevel
> 0 && (c
= skipToNonWhite ()) != EOF
)
1388 if (CollectingSignature
)
1389 vStringPut (Signature
, c
);
1395 if (braceFormatting
&& getDirectiveNestLevel () != initialLevel
)
1397 skipToFormattedBraceMatch ();
1404 if (braceFormatting
&& getDirectiveNestLevel () != initialLevel
)
1406 skipToFormattedBraceMatch ();
1413 verbose ("%s: failed to find match for '%c' at line %lu\n",
1414 getInputFileName (), begin
, inputLineNumber
);
1416 longjmp (Exception
, (int) ExceptionBraceFormattingError
);
1418 longjmp (Exception
, (int) ExceptionFormattingError
);
1422 static void skipParens (void)
1424 const int c
= skipToNonWhite ();
1432 static void skipBraces (void)
1434 const int c
= skipToNonWhite ();
1442 static keywordId
analyzeKeyword (const char *const name
)
1444 const keywordId id
= (keywordId
) lookupKeyword (name
, getSourceLanguage ());
1448 static void analyzeIdentifier (tokenInfo
*const token
)
1450 char *const name
= vStringValue (token
->name
);
1451 const char *replacement
= NULL
;
1452 boolean parensToo
= FALSE
;
1454 if (isLanguage (Lang_java
) ||
1455 ! isIgnoreToken (name
, &parensToo
, &replacement
))
1457 if (replacement
!= NULL
)
1458 token
->keyword
= analyzeKeyword (replacement
);
1460 token
->keyword
= analyzeKeyword (vStringValue (token
->name
));
1462 if (token
->keyword
== KEYWORD_NONE
)
1463 token
->type
= TOKEN_NAME
;
1465 token
->type
= TOKEN_KEYWORD
;
1472 int c
= skipToNonWhite ();
1480 static void readIdentifier (tokenInfo
*const token
, const int firstChar
)
1482 vString
*const name
= token
->name
;
1484 boolean first
= TRUE
;
1488 /* Bug #1585745: strangely, C++ destructors allow whitespace between
1489 * the ~ and the class name. */
1490 if (isLanguage (Lang_cpp
) && firstChar
== '~')
1492 vStringPut (name
, c
);
1493 c
= skipToNonWhite ();
1498 vStringPut (name
, c
);
1499 if (CollectingSignature
)
1502 vStringPut (Signature
, c
);
1506 } while (isident (c
) || ((isLanguage (Lang_java
) || isLanguage (Lang_csharp
)) && (isHighChar (c
) || c
== '.')));
1507 vStringTerminate (name
);
1508 cppUngetc (c
); /* unget non-identifier character */
1510 analyzeIdentifier (token
);
1513 static void readPackageName (tokenInfo
*const token
, const int firstChar
)
1515 vString
*const name
= token
->name
;
1520 while (isident (c
) || c
== '.')
1522 vStringPut (name
, c
);
1525 vStringTerminate (name
);
1526 cppUngetc (c
); /* unget non-package character */
1529 static void readPackageOrNamespace (statementInfo
*const st
, const declType declaration
)
1531 st
->declaration
= declaration
;
1533 if (declaration
== DECL_NAMESPACE
&& !isLanguage (Lang_csharp
))
1535 /* In C++ a namespace is specified one level at a time. */
1540 /* In C#, a namespace can also be specified like a Java package name. */
1541 tokenInfo
*const token
= activeToken (st
);
1542 Assert (isType (token
, TOKEN_KEYWORD
));
1543 readPackageName (token
, skipToNonWhite ());
1544 token
->type
= TOKEN_NAME
;
1546 st
->haveQualifyingName
= TRUE
;
1550 static void processName (statementInfo
*const st
)
1552 Assert (isType (activeToken (st
), TOKEN_NAME
));
1553 if (st
->gotName
&& st
->declaration
== DECL_NONE
)
1554 st
->declaration
= DECL_BASE
;
1556 st
->haveQualifyingName
= TRUE
;
1559 static void readOperator (statementInfo
*const st
)
1561 const char *const acceptable
= "+-*/%^&|~!=<>,[]";
1562 const tokenInfo
* const prev
= prevToken (st
,1);
1563 tokenInfo
*const token
= activeToken (st
);
1564 vString
*const name
= token
->name
;
1565 int c
= skipToNonWhite ();
1567 /* When we arrive here, we have the keyword "operator" in 'name'.
1569 if (isType (prev
, TOKEN_KEYWORD
) && (prev
->keyword
== KEYWORD_ENUM
||
1570 prev
->keyword
== KEYWORD_STRUCT
|| prev
->keyword
== KEYWORD_UNION
))
1571 ; /* ignore "operator" keyword if preceded by these keywords */
1574 /* Verify whether this is a valid function call (i.e. "()") operator.
1576 if (cppGetc () == ')')
1578 vStringPut (name
, ' '); /* always separate operator from keyword */
1579 c
= skipToNonWhite ();
1581 vStringCatS (name
, "()");
1589 else if (isident1 (c
))
1591 /* Handle "new" and "delete" operators, and conversion functions
1592 * (per 13.3.1.1.2 [2] of the C++ spec).
1594 boolean whiteSpace
= TRUE
; /* default causes insertion of space */
1603 vStringPut (name
, ' ');
1606 vStringPut (name
, c
);
1609 } while (! isOneOf (c
, "(;") && c
!= EOF
);
1610 vStringTerminate (name
);
1612 else if (isOneOf (c
, acceptable
))
1614 vStringPut (name
, ' '); /* always separate operator from keyword */
1617 vStringPut (name
, c
);
1619 } while (isOneOf (c
, acceptable
));
1620 vStringTerminate (name
);
1625 token
->type
= TOKEN_NAME
;
1626 token
->keyword
= KEYWORD_NONE
;
1630 static void copyToken (tokenInfo
*const dest
, const tokenInfo
*const src
)
1632 dest
->type
= src
->type
;
1633 dest
->keyword
= src
->keyword
;
1634 dest
->filePosition
= src
->filePosition
;
1635 dest
->lineNumber
= src
->lineNumber
;
1636 vStringCopy (dest
->name
, src
->name
);
1639 static void setAccess (statementInfo
*const st
, const accessType access
)
1643 if (isLanguage (Lang_cpp
))
1645 int c
= skipToNonWhite ();
1648 reinitStatement (st
, FALSE
);
1652 st
->member
.accessDefault
= access
;
1654 st
->member
.access
= access
;
1658 static void discardTypeList (tokenInfo
*const token
)
1660 int c
= skipToNonWhite ();
1661 while (isident1 (c
))
1663 readIdentifier (token
, c
);
1664 c
= skipToNonWhite ();
1665 if (c
== '.' || c
== ',')
1666 c
= skipToNonWhite ();
1671 static void addParentClass (statementInfo
*const st
, tokenInfo
*const token
)
1673 if (vStringLength (token
->name
) > 0 &&
1674 vStringLength (st
->parentClasses
) > 0)
1676 vStringPut (st
->parentClasses
, ',');
1678 vStringCat (st
->parentClasses
, token
->name
);
1681 static void readParents (statementInfo
*const st
, const int qualifier
)
1683 tokenInfo
*const token
= newToken ();
1684 tokenInfo
*const parent
= newToken ();
1689 c
= skipToNonWhite ();
1692 readIdentifier (token
, c
);
1693 if (isType (token
, TOKEN_NAME
))
1694 vStringCat (parent
->name
, token
->name
);
1697 addParentClass (st
, parent
);
1701 else if (c
== qualifier
)
1702 vStringPut (parent
->name
, c
);
1705 else if (isType (token
, TOKEN_NAME
))
1707 addParentClass (st
, parent
);
1710 } while (c
!= '{' && c
!= EOF
);
1712 deleteToken (parent
);
1713 deleteToken (token
);
1716 static void skipStatement (statementInfo
*const st
)
1718 st
->declaration
= DECL_IGNORE
;
1722 static void processInterface (statementInfo
*const st
)
1724 st
->declaration
= DECL_INTERFACE
;
1727 static void processToken (tokenInfo
*const token
, statementInfo
*const st
)
1729 switch (token
->keyword
) /* is it a reserved word? */
1733 case KEYWORD_NONE
: processName (st
); break;
1734 case KEYWORD_ABSTRACT
: st
->implementation
= IMP_ABSTRACT
; break;
1735 case KEYWORD_ATTRIBUTE
:
1736 case KEYWORD_INLINE
: skipParens (); initToken (token
); break;
1737 case KEYWORD_BIND
: st
->declaration
= DECL_BASE
; break;
1738 case KEYWORD_BIT
: st
->declaration
= DECL_BASE
; break;
1739 case KEYWORD_CATCH
: skipParens (); skipBraces (); break;
1740 case KEYWORD_CHAR
: st
->declaration
= DECL_BASE
; break;
1741 case KEYWORD_CLASS
: st
->declaration
= DECL_CLASS
; break;
1742 case KEYWORD_CONST
: st
->declaration
= DECL_BASE
; break;
1743 case KEYWORD_DOUBLE
: st
->declaration
= DECL_BASE
; break;
1744 case KEYWORD_ENUM
: st
->declaration
= DECL_ENUM
; break;
1745 case KEYWORD_EXTENDS
: readParents (st
, '.');
1746 setToken (st
, TOKEN_NONE
); break;
1747 case KEYWORD_FLOAT
: st
->declaration
= DECL_BASE
; break;
1748 case KEYWORD_FUNCTION
: st
->declaration
= DECL_BASE
; break;
1749 case KEYWORD_FRIEND
: st
->scope
= SCOPE_FRIEND
; break;
1750 case KEYWORD_GOTO
: skipStatement (st
); break;
1751 case KEYWORD_IMPLEMENTS
:readParents (st
, '.');
1752 setToken (st
, TOKEN_NONE
); break;
1753 case KEYWORD_IMPORT
: skipStatement (st
); break;
1754 case KEYWORD_INT
: st
->declaration
= DECL_BASE
; break;
1755 case KEYWORD_INTEGER
: st
->declaration
= DECL_BASE
; break;
1756 case KEYWORD_INTERFACE
: processInterface (st
); break;
1757 case KEYWORD_LOCAL
: setAccess (st
, ACCESS_LOCAL
); break;
1758 case KEYWORD_LONG
: st
->declaration
= DECL_BASE
; break;
1759 case KEYWORD_OPERATOR
: readOperator (st
); break;
1760 case KEYWORD_PRIVATE
: setAccess (st
, ACCESS_PRIVATE
); break;
1761 case KEYWORD_PROGRAM
: st
->declaration
= DECL_PROGRAM
; break;
1762 case KEYWORD_PROTECTED
: setAccess (st
, ACCESS_PROTECTED
); break;
1763 case KEYWORD_PUBLIC
: setAccess (st
, ACCESS_PUBLIC
); break;
1764 case KEYWORD_RETURN
: skipStatement (st
); break;
1765 case KEYWORD_SHORT
: st
->declaration
= DECL_BASE
; break;
1766 case KEYWORD_SIGNED
: st
->declaration
= DECL_BASE
; break;
1767 case KEYWORD_STRING
: st
->declaration
= DECL_BASE
; break;
1768 case KEYWORD_STRUCT
: st
->declaration
= DECL_STRUCT
; break;
1769 case KEYWORD_TASK
: st
->declaration
= DECL_TASK
; break;
1770 case KEYWORD_THROWS
: discardTypeList (token
); break;
1771 case KEYWORD_UNION
: st
->declaration
= DECL_UNION
; break;
1772 case KEYWORD_UNSIGNED
: st
->declaration
= DECL_BASE
; break;
1773 case KEYWORD_USING
: skipStatement (st
); break;
1774 case KEYWORD_VOID
: st
->declaration
= DECL_BASE
; break;
1775 case KEYWORD_VOLATILE
: st
->declaration
= DECL_BASE
; break;
1776 case KEYWORD_VIRTUAL
: st
->implementation
= IMP_VIRTUAL
; break;
1777 case KEYWORD_WCHAR_T
: st
->declaration
= DECL_BASE
; break;
1779 case KEYWORD_NAMESPACE
: readPackageOrNamespace (st
, DECL_NAMESPACE
); break;
1780 case KEYWORD_PACKAGE
: readPackageOrNamespace (st
, DECL_PACKAGE
); break;
1783 if (isLanguage (Lang_csharp
))
1784 st
->declaration
= DECL_EVENT
;
1787 case KEYWORD_TYPEDEF
:
1788 reinitStatement (st
, FALSE
);
1789 st
->scope
= SCOPE_TYPEDEF
;
1792 case KEYWORD_EXTERN
:
1793 if (! isLanguage (Lang_csharp
) || !st
->gotName
)
1795 reinitStatement (st
, FALSE
);
1796 st
->scope
= SCOPE_EXTERN
;
1797 st
->declaration
= DECL_BASE
;
1801 case KEYWORD_STATIC
:
1802 if (! (isLanguage (Lang_java
) || isLanguage (Lang_csharp
)))
1804 reinitStatement (st
, FALSE
);
1805 st
->scope
= SCOPE_STATIC
;
1806 st
->declaration
= DECL_BASE
;
1811 case KEYWORD_FOREACH
:
1813 case KEYWORD_SWITCH
:
1816 int c
= skipToNonWhite ();
1825 * Parenthesis handling functions
1828 static void restartStatement (statementInfo
*const st
)
1830 tokenInfo
*const save
= newToken ();
1831 tokenInfo
*token
= activeToken (st
);
1833 copyToken (save
, token
);
1834 DebugStatement ( if (debug (DEBUG_PARSE
)) printf ("<ES>");)
1835 reinitStatement (st
, FALSE
);
1836 token
= activeToken (st
);
1837 copyToken (token
, save
);
1839 processToken (token
, st
);
1842 /* Skips over a the mem-initializer-list of a ctor-initializer, defined as:
1844 * mem-initializer-list:
1845 * mem-initializer, mem-initializer-list
1848 * [::] [nested-name-spec] class-name (...)
1851 static void skipMemIntializerList (tokenInfo
*const token
)
1857 c
= skipToNonWhite ();
1858 while (isident1 (c
) || c
== ':')
1861 readIdentifier (token
, c
);
1862 c
= skipToNonWhite ();
1867 c
= skipToNonWhite ();
1872 c
= skipToNonWhite ();
1878 static void skipMacro (statementInfo
*const st
)
1880 tokenInfo
*const prev2
= prevToken (st
, 2);
1882 if (isType (prev2
, TOKEN_NAME
))
1887 /* Skips over characters following the parameter list. This will be either
1888 * non-ANSI style function declarations or C++ stuff. Our choices:
1892 * int func (one, two) int one; float two; {...}
1894 * int func (int one, float two);
1895 * int func (int one, float two) {...}
1897 * int foo (...) [const|volatile] [throw (...)];
1898 * int foo (...) [const|volatile] [throw (...)] [ctor-initializer] {...}
1899 * int foo (...) [const|volatile] [throw (...)] try [ctor-initializer] {...}
1902 static boolean
skipPostArgumentStuff (
1903 statementInfo
*const st
, parenInfo
*const info
)
1905 tokenInfo
*const token
= activeToken (st
);
1906 unsigned int parameters
= info
->parameterCount
;
1907 unsigned int elementCount
= 0;
1908 boolean restart
= FALSE
;
1909 boolean end
= FALSE
;
1910 int c
= skipToNonWhite ();
1917 case ':': skipMemIntializerList (token
);break; /* ctor-initializer */
1918 case '[': skipToMatch ("[]"); break;
1919 case '=': cppUngetc (c
); end
= TRUE
; break;
1920 case '{': cppUngetc (c
); end
= TRUE
; break;
1921 case '}': cppUngetc (c
); end
= TRUE
; break;
1924 if (elementCount
> 0)
1930 if (parameters
== 0 || elementCount
< 2)
1935 else if (--parameters
== 0)
1942 readIdentifier (token
, c
);
1943 switch (token
->keyword
)
1945 case KEYWORD_ATTRIBUTE
: skipParens (); break;
1946 case KEYWORD_THROW
: skipParens (); break;
1947 case KEYWORD_TRY
: break;
1950 case KEYWORD_VOLATILE
:
1951 if (vStringLength (Signature
) > 0)
1953 vStringPut (Signature
, ' ');
1954 vStringCat (Signature
, token
->name
);
1960 case KEYWORD_EXPLICIT
:
1961 case KEYWORD_EXTERN
:
1962 case KEYWORD_FRIEND
:
1963 case KEYWORD_INLINE
:
1964 case KEYWORD_MUTABLE
:
1965 case KEYWORD_NAMESPACE
:
1967 case KEYWORD_NEWCOV
:
1968 case KEYWORD_OPERATOR
:
1969 case KEYWORD_OVERLOAD
:
1970 case KEYWORD_PRIVATE
:
1971 case KEYWORD_PROTECTED
:
1972 case KEYWORD_PUBLIC
:
1973 case KEYWORD_STATIC
:
1974 case KEYWORD_TEMPLATE
:
1975 case KEYWORD_TYPEDEF
:
1976 case KEYWORD_TYPENAME
:
1978 case KEYWORD_VIRTUAL
:
1979 /* Never allowed within parameter declarations. */
1985 if (isType (token
, TOKEN_NONE
))
1987 else if (info
->isKnrParamList
&& info
->parameterCount
> 0)
1991 /* If we encounter any other identifier immediately
1992 * following an empty parameter list, this is almost
1993 * certainly one of those Microsoft macro "thingies"
1994 * that the automatic source code generation sticks
1995 * in. Terminate the current statement.
2006 c
= skipToNonWhite ();
2013 restartStatement (st
);
2015 setToken (st
, TOKEN_NONE
);
2017 return (boolean
) (c
!= EOF
);
2020 static void skipJavaThrows (statementInfo
*const st
)
2022 tokenInfo
*const token
= activeToken (st
);
2023 int c
= skipToNonWhite ();
2027 readIdentifier (token
, c
);
2028 if (token
->keyword
== KEYWORD_THROWS
)
2032 c
= skipToNonWhite ();
2035 readIdentifier (token
, c
);
2036 c
= skipToNonWhite ();
2038 } while (c
== '.' || c
== ',');
2042 setToken (st
, TOKEN_NONE
);
2045 static void analyzePostParens (statementInfo
*const st
, parenInfo
*const info
)
2047 const unsigned long inputLineNumber
= getInputLineNumber ();
2048 int c
= skipToNonWhite ();
2051 if (isOneOf (c
, "{;,="))
2053 else if (isLanguage (Lang_java
))
2054 skipJavaThrows (st
);
2057 if (! skipPostArgumentStuff (st
, info
))
2060 "%s: confusing argument declarations beginning at line %lu\n",
2061 getInputFileName (), inputLineNumber
);
2062 longjmp (Exception
, (int) ExceptionFormattingError
);
2067 static boolean
languageSupportsGenerics (void)
2069 return (boolean
) (isLanguage (Lang_cpp
) || isLanguage (Lang_csharp
) ||
2070 isLanguage (Lang_java
));
2073 static void processAngleBracket (void)
2077 /* already found match for template */
2078 } else if (languageSupportsGenerics () && c
!= '<' && c
!= '=') {
2079 /* this is a template */
2082 } else if (c
== '<') {
2083 /* skip "<<" or "<<=". */
2093 static void parseJavaAnnotation (statementInfo
*const st
)
2097 * @Target(ElementType.METHOD)
2098 * @SuppressWarnings(value = "unchecked")
2100 * But watch out for "@interface"!
2102 tokenInfo
*const token
= activeToken (st
);
2104 int c
= skipToNonWhite ();
2105 readIdentifier (token
, c
);
2106 if (token
->keyword
== KEYWORD_INTERFACE
)
2108 /* Oops. This was actually "@interface" defining a new annotation. */
2109 processInterface (st
);
2113 /* Bug #1691412: skip any annotation arguments. */
2118 static void parseReturnType (statementInfo
*const st
)
2122 tokenInfo
* finding_tok
;
2124 /* FIXME TODO: if java language must be supported then impement this here
2125 * removing the current FIXME */
2126 if (!isLanguage (Lang_c
) && !isLanguage (Lang_cpp
))
2131 vStringClear (ReturnType
);
2133 finding_tok
= prevToken (st
, 1);
2135 if (isType (finding_tok
, TOKEN_NONE
))
2138 finding_tok
= prevToken (st
, 2);
2140 if (finding_tok
->type
== TOKEN_DOUBLE_COLON
)
2142 /* get the total number of double colons */
2146 /* we already are at 2nd token */
2147 /* the +=2 means that colons are usually found at even places */
2148 for (j
= 2; j
< NumTokens
; j
+=2)
2150 tokenInfo
*curr_tok
;
2151 curr_tok
= prevToken (st
, j
);
2152 if (curr_tok
->type
== TOKEN_DOUBLE_COLON
)
2158 /*printf ("FOUND colons %d\n", num_colons);*/
2159 lower_bound
= 2 * num_colons
+ 1;
2164 for (i
= (unsigned int) NumTokens
; i
> lower_bound
; i
--)
2166 tokenInfo
* curr_tok
;
2167 curr_tok
= prevToken (st
, i
);
2169 switch (curr_tok
->type
)
2171 case TOKEN_PAREN_NAME
:
2176 case TOKEN_DOUBLE_COLON
:
2177 /* usually C++ class scope */
2178 vStringCatS (ReturnType
, "::");
2183 vStringPut (ReturnType
, '*');
2186 case TOKEN_AMPERSAND
:
2188 vStringPut (ReturnType
, '&');
2192 vStringPut (ReturnType
, ' ');
2195 vStringCat (ReturnType
, curr_tok
->name
);
2200 /* clear any white space from the front */
2201 vStringStripLeading (ReturnType
);
2203 /* .. and from the tail too */
2204 vStringStripTrailing (ReturnType
);
2206 /* put and end marker */
2207 vStringTerminate (ReturnType
);
2210 printf ("~~~~~ statement ---->\n");
2212 printf ("NumTokens: %d\n", NumTokens);
2213 printf ("FOUND ReturnType: %s\n", vStringValue (ReturnType));
2214 printf ("<~~~~~\n");
2218 static int parseParens (statementInfo
*const st
, parenInfo
*const info
)
2220 tokenInfo
*const token
= activeToken (st
);
2221 unsigned int identifierCount
= 0;
2222 unsigned int depth
= 1;
2223 boolean firstChar
= TRUE
;
2224 int nextChar
= '\0';
2226 CollectingSignature
= TRUE
;
2227 vStringClear (Signature
);
2228 vStringPut (Signature
, '(');
2229 info
->parameterCount
= 1;
2232 int c
= skipToNonWhite ();
2233 vStringPut (Signature
, c
);
2239 info
->isPointer
= TRUE
;
2240 info
->isKnrParamList
= FALSE
;
2241 if (identifierCount
== 0)
2242 info
->isParamList
= FALSE
;
2247 info
->isKnrParamList
= FALSE
;
2251 info
->isNameCandidate
= FALSE
;
2256 info
->isKnrParamList
= FALSE
;
2264 info
->isKnrParamList
= FALSE
;
2267 vStringCatS (Signature
, "..."); /* variable arg list */
2272 info
->isNameCandidate
= FALSE
;
2273 if (info
->isKnrParamList
)
2275 ++info
->parameterCount
;
2276 identifierCount
= 0;
2281 info
->isKnrParamList
= FALSE
;
2282 info
->isNameCandidate
= FALSE
;
2285 info
->isParamList
= FALSE
;
2292 info
->isKnrParamList
= FALSE
;
2297 info
->isKnrParamList
= FALSE
;
2298 processAngleBracket ();
2303 info
->parameterCount
= 0;
2308 info
->isKnrParamList
= FALSE
;
2311 info
->isNameCandidate
= FALSE
;
2313 vStringClear (Signature
);
2316 vStringChop (Signature
);
2318 else if (isType (token
, TOKEN_PAREN_NAME
))
2320 c
= skipToNonWhite ();
2321 if (c
== '*') /* check for function pointer */
2324 c
= skipToNonWhite ();
2334 info
->nestedArgs
= TRUE
;
2342 if (c
== '@' && isLanguage (Lang_java
))
2344 parseJavaAnnotation(st
);
2346 else if (isident1 (c
))
2348 if (++identifierCount
> 1)
2349 info
->isKnrParamList
= FALSE
;
2350 readIdentifier (token
, c
);
2351 if (isType (token
, TOKEN_NAME
) && info
->isNameCandidate
)
2352 token
->type
= TOKEN_PAREN_NAME
;
2353 else if (isType (token
, TOKEN_KEYWORD
))
2355 if (token
->keyword
!= KEYWORD_CONST
&&
2356 token
->keyword
!= KEYWORD_VOLATILE
)
2358 info
->isKnrParamList
= FALSE
;
2359 info
->isNameCandidate
= FALSE
;
2365 info
->isParamList
= FALSE
;
2366 info
->isKnrParamList
= FALSE
;
2367 info
->isNameCandidate
= FALSE
;
2368 info
->invalidContents
= TRUE
;
2373 } while (! info
->nestedArgs
&& depth
> 0 &&
2374 (info
->isKnrParamList
|| info
->isNameCandidate
));
2376 if (! info
->nestedArgs
) while (depth
> 0)
2382 if (! info
->isNameCandidate
)
2385 vStringTerminate (Signature
);
2386 if (info
->isKnrParamList
)
2387 vStringClear (Signature
);
2388 CollectingSignature
= FALSE
;
2392 static void initParenInfo (parenInfo
*const info
)
2394 info
->isPointer
= FALSE
;
2395 info
->isParamList
= TRUE
;
2396 info
->isKnrParamList
= isLanguage (Lang_c
);
2397 info
->isNameCandidate
= TRUE
;
2398 info
->invalidContents
= FALSE
;
2399 info
->nestedArgs
= FALSE
;
2400 info
->parameterCount
= 0;
2403 static void analyzeParens (statementInfo
*const st
)
2405 tokenInfo
*const prev
= prevToken (st
, 1);
2407 if (st
->inFunction
&& ! st
->assignment
)
2408 st
->notVariable
= TRUE
;
2409 if (! isType (prev
, TOKEN_NONE
)) /* in case of ignored enclosing macros */
2411 tokenInfo
*const token
= activeToken (st
);
2415 initParenInfo (&info
);
2416 parseParens (st
, &info
);
2417 parseReturnType (st
);
2418 c
= skipToNonWhite ();
2420 if (info
.invalidContents
)
2421 reinitStatement (st
, FALSE
);
2422 else if (info
.isNameCandidate
&& isType (token
, TOKEN_PAREN_NAME
) &&
2423 ! st
->gotParenName
&&
2424 (! info
.isParamList
|| ! st
->haveQualifyingName
||
2426 (c
== '=' && st
->implementation
!= IMP_VIRTUAL
) ||
2427 (st
->declaration
== DECL_NONE
&& isOneOf (c
, ",;"))))
2429 token
->type
= TOKEN_NAME
;
2431 st
->gotParenName
= TRUE
;
2432 if (! (c
== '(' && info
.nestedArgs
))
2433 st
->isPointer
= info
.isPointer
;
2435 else if (! st
->gotArgs
&& info
.isParamList
)
2438 setToken (st
, TOKEN_ARGS
);
2440 if (st
->scope
!= SCOPE_TYPEDEF
)
2441 analyzePostParens (st
, &info
);
2444 setToken (st
, TOKEN_NONE
);
2449 * Token parsing functions
2452 static void addContext (statementInfo
*const st
, const tokenInfo
* const token
)
2454 if (isType (token
, TOKEN_NAME
))
2456 if (vStringLength (st
->context
->name
) > 0)
2458 if (isLanguage (Lang_c
) || isLanguage (Lang_cpp
))
2459 vStringCatS (st
->context
->name
, "::");
2460 else if (isLanguage (Lang_java
) || isLanguage (Lang_csharp
))
2461 vStringCatS (st
->context
->name
, ".");
2463 vStringCat (st
->context
->name
, token
->name
);
2464 st
->context
->type
= TOKEN_NAME
;
2468 static boolean
inheritingDeclaration (declType decl
)
2470 /* C# supports inheritance for enums. C++0x will too, but not yet. */
2471 if (decl
== DECL_ENUM
)
2473 return (boolean
) (isLanguage (Lang_csharp
));
2476 decl
== DECL_CLASS
||
2477 decl
== DECL_STRUCT
||
2478 decl
== DECL_INTERFACE
);
2481 static void processColon (statementInfo
*const st
)
2483 int c
= (isLanguage (Lang_cpp
) ? cppGetc () : skipToNonWhite ());
2484 const boolean doubleColon
= (boolean
) (c
== ':');
2488 setToken (st
, TOKEN_DOUBLE_COLON
);
2489 st
->haveQualifyingName
= FALSE
;
2494 if ((isLanguage (Lang_cpp
) || isLanguage (Lang_csharp
)) &&
2495 inheritingDeclaration (st
->declaration
))
2497 readParents (st
, ':');
2499 else if (parentDecl (st
) == DECL_STRUCT
)
2501 c
= skipToOneOf (",;");
2503 setToken (st
, TOKEN_COMMA
);
2505 setToken (st
, TOKEN_SEMICOLON
);
2509 const tokenInfo
*const prev
= prevToken (st
, 1);
2510 const tokenInfo
*const prev2
= prevToken (st
, 2);
2511 if (prev
->keyword
== KEYWORD_DEFAULT
||
2512 prev2
->keyword
== KEYWORD_CASE
||
2515 reinitStatement (st
, FALSE
);
2521 /* Skips over any initializing value which may follow an '=' character in a
2522 * variable definition.
2524 static int skipInitializer (statementInfo
*const st
)
2526 boolean done
= FALSE
;
2531 c
= skipToNonWhite ();
2534 longjmp (Exception
, (int) ExceptionFormattingError
);
2538 case ';': done
= TRUE
; break;
2541 if (st
->implementation
== IMP_VIRTUAL
)
2542 st
->implementation
= IMP_PURE_VIRTUAL
;
2545 case '[': skipToMatch ("[]"); break;
2546 case '(': skipToMatch ("()"); break;
2547 case '{': skipToMatch ("{}"); break;
2548 case '<': processAngleBracket(); break;
2551 if (insideEnumBody (st
))
2553 else if (! isBraceFormat ())
2555 verbose ("%s: unexpected closing brace at line %lu\n",
2556 getInputFileName (), getInputLineNumber ());
2557 longjmp (Exception
, (int) ExceptionBraceFormattingError
);
2567 static void processInitializer (statementInfo
*const st
)
2569 const boolean inEnumBody
= insideEnumBody (st
);
2575 c
= skipInitializer (st
);
2576 st
->assignment
= TRUE
;
2578 setToken (st
, TOKEN_SEMICOLON
);
2580 setToken (st
, TOKEN_COMMA
);
2581 else if (c
== '}' && inEnumBody
)
2584 setToken (st
, TOKEN_COMMA
);
2586 if (st
->scope
== SCOPE_EXTERN
)
2587 st
->scope
= SCOPE_GLOBAL
;
2591 static void parseIdentifier (statementInfo
*const st
, const int c
)
2593 tokenInfo
*const token
= activeToken (st
);
2595 readIdentifier (token
, c
);
2596 if (! isType (token
, TOKEN_NONE
))
2597 processToken (token
, st
);
2600 static void parseGeneralToken (statementInfo
*const st
, const int c
)
2602 const tokenInfo
*const prev
= prevToken (st
, 1);
2604 if (isident1 (c
) || (isLanguage (Lang_java
) && isHighChar (c
)))
2606 parseIdentifier (st
, c
);
2607 if (isType (st
->context
, TOKEN_NAME
) &&
2608 isType (activeToken (st
), TOKEN_NAME
) && isType (prev
, TOKEN_NAME
))
2610 initToken (st
->context
);
2613 else if (c
== '.' || c
== '-')
2615 if (! st
->assignment
)
2616 st
->notVariable
= TRUE
;
2619 int c2
= cppGetc ();
2624 else if (c
== '!' || c
== '>')
2626 int c2
= cppGetc ();
2630 else if (c
== '@' && isLanguage (Lang_java
))
2632 parseJavaAnnotation (st
);
2634 else if (isExternCDecl (st
, c
))
2636 st
->declaration
= DECL_NOMANGLE
;
2637 st
->scope
= SCOPE_GLOBAL
;
2641 /* Reads characters from the pre-processor and assembles tokens, setting
2642 * the current statement state.
2644 static void nextToken (statementInfo
*const st
)
2649 int c
= skipToNonWhite ();
2652 case EOF
: longjmp (Exception
, (int) ExceptionEOF
); break;
2653 /* analyze functions and co */
2654 case '(': analyzeParens (st
); break;
2655 case '<': processAngleBracket (); break;
2657 st
->haveQualifyingName
= FALSE
;
2658 setToken (st
, TOKEN_STAR
);
2660 case '&': setToken (st
, TOKEN_AMPERSAND
); break;
2662 case ',': setToken (st
, TOKEN_COMMA
); break;
2663 case ':': processColon (st
); break;
2664 case ';': setToken (st
, TOKEN_SEMICOLON
); break;
2665 case '=': processInitializer (st
); break;
2666 case '[': skipToMatch ("[]"); break;
2667 case '{': setToken (st
, TOKEN_BRACE_OPEN
); break;
2668 case '}': setToken (st
, TOKEN_BRACE_CLOSE
); break;
2669 default: parseGeneralToken (st
, c
); break;
2671 token
= activeToken (st
);
2672 } while (isType (token
, TOKEN_NONE
));
2676 * Scanning support functions
2679 static statementInfo
*CurrentStatement
= NULL
;
2681 static statementInfo
*newStatement (statementInfo
*const parent
)
2683 statementInfo
*const st
= xMalloc (1, statementInfo
);
2686 for (i
= 0 ; i
< (unsigned int) NumTokens
; ++i
)
2687 st
->token
[i
] = newToken ();
2689 st
->context
= newToken ();
2690 st
->blockName
= newToken ();
2691 st
->parentClasses
= vStringNew ();
2693 initStatement (st
, parent
);
2694 CurrentStatement
= st
;
2699 static void deleteStatement (void)
2701 statementInfo
*const st
= CurrentStatement
;
2702 statementInfo
*const parent
= st
->parent
;
2705 for (i
= 0 ; i
< (unsigned int) NumTokens
; ++i
)
2707 deleteToken (st
->token
[i
]); st
->token
[i
] = NULL
;
2709 deleteToken (st
->blockName
); st
->blockName
= NULL
;
2710 deleteToken (st
->context
); st
->context
= NULL
;
2711 vStringDelete (st
->parentClasses
); st
->parentClasses
= NULL
;
2713 CurrentStatement
= parent
;
2716 static void deleteAllStatements (void)
2718 while (CurrentStatement
!= NULL
)
2722 static boolean
isStatementEnd (const statementInfo
*const st
)
2724 const tokenInfo
*const token
= activeToken (st
);
2727 if (isType (token
, TOKEN_SEMICOLON
))
2729 else if (isType (token
, TOKEN_BRACE_CLOSE
))
2730 /* Java and C# do not require semicolons to end a block. Neither do C++
2731 * namespaces. All other blocks require a semicolon to terminate them.
2733 isEnd
= (boolean
) (isLanguage (Lang_java
) || isLanguage (Lang_csharp
) ||
2734 ! isContextualStatement (st
));
2741 static void checkStatementEnd (statementInfo
*const st
)
2743 const tokenInfo
*const token
= activeToken (st
);
2745 if (isType (token
, TOKEN_COMMA
))
2746 reinitStatement (st
, TRUE
);
2747 else if (isStatementEnd (st
))
2749 DebugStatement ( if (debug (DEBUG_PARSE
)) printf ("<ES>"); )
2750 reinitStatement (st
, FALSE
);
2755 cppBeginStatement ();
2760 static void nest (statementInfo
*const st
, const unsigned int nestLevel
)
2762 switch (st
->declaration
)
2766 case DECL_INTERFACE
:
2767 case DECL_NAMESPACE
:
2771 createTags (nestLevel
, st
);
2776 st
->inFunction
= TRUE
;
2780 if (includeTag (TAG_LOCAL
, FALSE
))
2781 createTags (nestLevel
, st
);
2787 setToken (st
, TOKEN_BRACE_CLOSE
);
2790 static void tagCheck (statementInfo
*const st
)
2792 const tokenInfo
*const token
= activeToken (st
);
2793 const tokenInfo
*const prev
= prevToken (st
, 1);
2794 const tokenInfo
*const prev2
= prevToken (st
, 2);
2796 switch (token
->type
)
2799 if (insideEnumBody (st
))
2800 qualifyEnumeratorTag (st
, token
);
2804 if (st
->haveQualifyingName
)
2805 makeTag (token
, st
, FALSE
, TAG_PACKAGE
);
2808 case TOKEN_BRACE_OPEN
:
2809 if (isType (prev
, TOKEN_ARGS
))
2811 if (st
->haveQualifyingName
)
2813 if (! isLanguage (Lang_vera
))
2814 st
->declaration
= DECL_FUNCTION
;
2815 if (isType (prev2
, TOKEN_NAME
))
2816 copyToken (st
->blockName
, prev2
);
2817 qualifyFunctionTag (st
, prev2
);
2820 else if (isContextualStatement (st
) ||
2821 st
->declaration
== DECL_NAMESPACE
||
2822 st
->declaration
== DECL_PROGRAM
)
2824 if (isType (prev
, TOKEN_NAME
))
2825 copyToken (st
->blockName
, prev
);
2828 /* For an anonymous struct or union we use a unique ID
2829 * a number, so that the members can be found.
2831 char buf
[20]; /* length of "_anon" + digits + null */
2832 sprintf (buf
, "__anon%d", ++AnonymousID
);
2833 vStringCopyS (st
->blockName
->name
, buf
);
2834 st
->blockName
->type
= TOKEN_NAME
;
2835 st
->blockName
->keyword
= KEYWORD_NONE
;
2837 qualifyBlockTag (st
, prev
);
2839 else if (isLanguage (Lang_csharp
))
2840 makeTag (prev
, st
, FALSE
, TAG_PROPERTY
);
2843 case TOKEN_SEMICOLON
:
2845 if (insideEnumBody (st
))
2847 else if (isType (prev
, TOKEN_NAME
))
2849 if (isContextualKeyword (prev2
))
2850 makeTag (prev
, st
, TRUE
, TAG_EXTERN_VAR
);
2852 qualifyVariableTag (st
, prev
);
2854 else if (isType (prev
, TOKEN_ARGS
) && isType (prev2
, TOKEN_NAME
))
2857 qualifyVariableTag (st
, prev2
);
2859 qualifyFunctionDeclTag (st
, prev2
);
2861 if (isLanguage (Lang_java
) && token
->type
== TOKEN_SEMICOLON
&& insideEnumBody (st
))
2863 /* In Java, after an initial enum-like part,
2864 * a semicolon introduces a class-like part.
2865 * See Bug #1730485 for the full rationale. */
2866 st
->parent
->declaration
= DECL_CLASS
;
2874 /* Parses the current file and decides whether to write out and tags that
2877 static void createTags (const unsigned int nestLevel
,
2878 statementInfo
*const parent
)
2880 statementInfo
*const st
= newStatement (parent
);
2882 DebugStatement ( if (nestLevel
> 0) debugParseNest (TRUE
, nestLevel
); )
2888 token
= activeToken (st
);
2890 if (isType (token
, TOKEN_BRACE_CLOSE
))
2896 verbose ("%s: unexpected closing brace at line %lu\n",
2897 getInputFileName (), getInputLineNumber ());
2898 longjmp (Exception
, (int) ExceptionBraceFormattingError
);
2901 else if (isType (token
, TOKEN_DOUBLE_COLON
))
2903 addContext (st
, prevToken (st
, 1));
2909 if (isType (token
, TOKEN_BRACE_OPEN
))
2910 nest (st
, nestLevel
+ 1);
2911 checkStatementEnd (st
);
2915 DebugStatement ( if (nestLevel
> 0) debugParseNest (FALSE
, nestLevel
- 1); )
2918 static boolean
findCTags (const unsigned int passCount
)
2920 exception_t exception
;
2923 Assert (passCount
< 3);
2924 cppInit ((boolean
) (passCount
> 1), isLanguage (Lang_csharp
));
2925 Signature
= vStringNew ();
2926 ReturnType
= vStringNew ();
2928 exception
= (exception_t
) setjmp (Exception
);
2930 if (exception
== ExceptionNone
)
2931 createTags (0, NULL
);
2934 deleteAllStatements ();
2935 if (exception
== ExceptionBraceFormattingError
&& passCount
== 1)
2938 verbose ("%s: retrying file with fallback brace matching algorithm\n",
2939 getInputFileName ());
2942 vStringDelete (Signature
);
2943 vStringDelete (ReturnType
);
2948 static void buildKeywordHash (const langType language
, unsigned int idx
)
2950 const size_t count
= sizeof (KeywordTable
) / sizeof (KeywordTable
[0]);
2952 for (i
= 0 ; i
< count
; ++i
)
2954 const keywordDesc
* const p
= &KeywordTable
[i
];
2955 if (p
->isValid
[idx
])
2956 addKeyword (p
->name
, language
, (int) p
->id
);
2960 static void initializeCParser (const langType language
)
2963 buildKeywordHash (language
, 0);
2966 static void initializeCppParser (const langType language
)
2968 Lang_cpp
= language
;
2969 buildKeywordHash (language
, 1);
2972 static void initializeCsharpParser (const langType language
)
2974 Lang_csharp
= language
;
2975 buildKeywordHash (language
, 2);
2978 static void initializeJavaParser (const langType language
)
2980 Lang_java
= language
;
2981 buildKeywordHash (language
, 3);
2984 static void initializeVeraParser (const langType language
)
2986 Lang_vera
= language
;
2987 buildKeywordHash (language
, 4);
2990 extern parserDefinition
* CParser (void)
2992 static const char *const extensions
[] = { "c", NULL
};
2993 parserDefinition
* def
= parserNew ("C");
2994 def
->kinds
= CKinds
;
2995 def
->kindCount
= KIND_COUNT (CKinds
);
2996 def
->extensions
= extensions
;
2997 def
->parser2
= findCTags
;
2998 def
->initialize
= initializeCParser
;
3002 extern parserDefinition
* CppParser (void)
3004 static const char *const extensions
[] = {
3005 "c++", "cc", "cp", "cpp", "cxx", "h", "h++", "hh", "hp", "hpp", "hxx",
3006 #ifndef CASE_INSENSITIVE_FILENAMES
3011 parserDefinition
* def
= parserNew ("C++");
3012 def
->kinds
= CKinds
;
3013 def
->kindCount
= KIND_COUNT (CKinds
);
3014 def
->extensions
= extensions
;
3015 def
->parser2
= findCTags
;
3016 def
->initialize
= initializeCppParser
;
3020 extern parserDefinition
* CsharpParser (void)
3022 static const char *const extensions
[] = { "cs", NULL
};
3023 parserDefinition
* def
= parserNew ("C#");
3024 def
->kinds
= CsharpKinds
;
3025 def
->kindCount
= KIND_COUNT (CsharpKinds
);
3026 def
->extensions
= extensions
;
3027 def
->parser2
= findCTags
;
3028 def
->initialize
= initializeCsharpParser
;
3032 extern parserDefinition
* JavaParser (void)
3034 static const char *const extensions
[] = { "java", NULL
};
3035 parserDefinition
* def
= parserNew ("Java");
3036 def
->kinds
= JavaKinds
;
3037 def
->kindCount
= KIND_COUNT (JavaKinds
);
3038 def
->extensions
= extensions
;
3039 def
->parser2
= findCTags
;
3040 def
->initialize
= initializeJavaParser
;
3044 extern parserDefinition
* VeraParser (void)
3046 static const char *const extensions
[] = { "vr", "vri", "vrh", NULL
};
3047 parserDefinition
* def
= parserNew ("Vera");
3048 def
->kinds
= VeraKinds
;
3049 def
->kindCount
= KIND_COUNT (VeraKinds
);
3050 def
->extensions
= extensions
;
3051 def
->parser2
= findCTags
;
3052 def
->initialize
= initializeVeraParser
;
3056 /* vi:set tabstop=4 shiftwidth=4 noexpandtab: */