Make sure to be more careful about marking symbols assigned
[smatch.git] / token.h
blobbbf193c6ee7c69cfa6a25611539ea3a248272d93
1 #ifndef TOKEN_H
2 #define TOKEN_H
3 /*
4 * Basic tokenization structures. NOTE! Those tokens had better
5 * be pretty small, since we're going to keep them all in memory
6 * indefinitely.
8 * Copyright (C) 2003 Transmeta Corp.
9 * 2003 Linus Torvalds
11 * Licensed under the Open Software License version 1.1
14 #include <sys/types.h>
15 #include "lib.h"
18 * This describes the pure lexical elements (tokens), with
19 * no semantic meaning. In other words, an identifier doesn't
20 * have a type or meaning, it is only a specific string in
21 * the input stream.
23 * Semantic meaning is handled elsewhere.
26 enum constantfile {
27 CONSTANT_FILE_MAYBE, // To be determined, not inside any #ifs in this file
28 CONSTANT_FILE_IFNDEF, // To be determined, currently inside #ifndef
29 CONSTANT_FILE_NOPE, // No
30 CONSTANT_FILE_YES // Yes
33 extern const char *includepath[];
35 struct stream {
36 int fd;
37 const char *name;
38 const char *path; // inputfile path - see set_stream_include_path()
39 const char **next_path;
41 /* Use these to check for "already parsed" */
42 enum constantfile constant;
43 int nesting;
44 struct ident *protect;
47 extern int input_stream_nr;
48 extern struct stream *input_streams;
50 struct ident {
51 struct ident *next; /* Hash chain of identifiers */
52 struct symbol *symbols; /* Pointer to semantic meaning list */
53 unsigned char len; /* Length of identifier name */
54 unsigned char tainted:1,
55 reserved:1;
56 char name[]; /* Actual identifier */
59 enum token_type {
60 TOKEN_EOF,
61 TOKEN_ERROR,
62 TOKEN_IDENT,
63 TOKEN_ZERO_IDENT,
64 TOKEN_NUMBER,
65 TOKEN_CHAR,
66 TOKEN_STRING,
67 TOKEN_SPECIAL,
68 TOKEN_STREAMBEGIN,
69 TOKEN_STREAMEND,
70 TOKEN_MACRO_ARGUMENT,
71 TOKEN_STR_ARGUMENT,
72 TOKEN_QUOTED_ARGUMENT,
73 TOKEN_CONCAT,
74 TOKEN_GNU_KLUDGE,
75 TOKEN_UNTAINT,
76 TOKEN_ARG_COUNT,
79 /* Combination tokens */
80 #define COMBINATION_STRINGS { \
81 "+=", "++", \
82 "-=", "--", "->", \
83 "*=", \
84 "/=", \
85 "%=", \
86 "..", "...", \
87 "<=", "<<", "<<=", \
88 ">=", ">>", ">>=", \
89 "==", "!=", \
90 "&&", "&=", \
91 "||", "|=", \
92 "^=", "##", \
93 "", \
94 "<", ">", "<=", ">=" \
97 extern unsigned char combinations[][3];
99 enum special_token {
100 SPECIAL_BASE = 256,
101 SPECIAL_ADD_ASSIGN = SPECIAL_BASE,
102 SPECIAL_INCREMENT,
103 SPECIAL_SUB_ASSIGN,
104 SPECIAL_DECREMENT,
105 SPECIAL_DEREFERENCE,
106 SPECIAL_MUL_ASSIGN,
107 SPECIAL_DIV_ASSIGN,
108 SPECIAL_MOD_ASSIGN,
109 SPECIAL_DOTDOT,
110 SPECIAL_ELLIPSIS,
111 SPECIAL_LTE,
112 SPECIAL_LEFTSHIFT,
113 SPECIAL_SHL_ASSIGN,
114 SPECIAL_GTE,
115 SPECIAL_RIGHTSHIFT,
116 SPECIAL_SHR_ASSIGN,
117 SPECIAL_EQUAL,
118 SPECIAL_NOTEQUAL,
119 SPECIAL_LOGICAL_AND,
120 SPECIAL_AND_ASSIGN,
121 SPECIAL_LOGICAL_OR,
122 SPECIAL_OR_ASSIGN,
123 SPECIAL_XOR_ASSIGN,
124 SPECIAL_HASHHASH,
125 SPECIAL_ARG_SEPARATOR,
126 SPECIAL_UNSIGNED_LT,
127 SPECIAL_UNSIGNED_GT,
128 SPECIAL_UNSIGNED_LTE,
129 SPECIAL_UNSIGNED_GTE,
132 struct string {
133 unsigned int length;
134 char data[];
137 /* will fit into 32 bits */
138 struct argcount {
139 unsigned normal:10;
140 unsigned quoted:10;
141 unsigned str:10;
142 unsigned vararg:1;
146 * This is a very common data structure, it should be kept
147 * as small as humanly possible. Big (rare) types go as
148 * pointers.
150 struct token {
151 struct position pos;
152 struct token *next;
153 union {
154 const char *number;
155 struct ident *ident;
156 unsigned int special;
157 struct string *string;
158 int character;
159 int argnum;
160 struct argcount count;
164 #define MAX_STRING 4095
166 static inline struct token *containing_token(struct token **p)
168 void *addr = (char*)p - ((char*)&((struct token *)0)->next - (char*)0);
169 return addr;
172 #define token_type(x) ((x)->pos.type)
175 * Last token in the stream - points to itself.
176 * This allows us to not test for NULL pointers
177 * when following the token->next chain..
179 extern struct token eof_token_entry;
180 #define eof_token(x) ((x) == &eof_token_entry)
182 extern int init_stream(const char *, int fd, const char **next_path);
183 extern const char *stream_name(int stream);
184 extern struct ident *hash_ident(struct ident *);
185 extern struct ident *built_in_ident(const char *);
186 extern struct token *built_in_token(int, const char *);
187 extern const char *show_special(int);
188 extern const char *show_ident(const struct ident *);
189 extern const char *show_string(const struct string *string);
190 extern const char *show_token(const struct token *);
191 extern struct token * tokenize(const char *, int, struct token *, const char **next_path);
192 extern struct token * tokenize_buffer(void *, unsigned long, struct token *);
194 extern void show_identifier_stats(void);
195 extern struct token *preprocess(struct token *);
197 static inline int match_op(struct token *token, int op)
199 return token->pos.type == TOKEN_SPECIAL && token->special == op;
202 static inline int match_ident(struct token *token, struct ident *id)
204 return token->pos.type == TOKEN_IDENT && token->ident == id;
207 #endif