Get comparison sizes right.
[smatch.git] / token.h
blobc7bca7ab99c3b3a22f276560a22ec4eb95c39671
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 **next_path;
40 /* Use these to check for "already parsed" */
41 enum constantfile constant;
42 int nesting;
43 struct ident *protect;
46 extern int input_stream_nr;
47 extern struct stream *input_streams;
49 extern int ident_hit, ident_miss;
51 struct ident {
52 struct ident *next; /* Hash chain of identifiers */
53 struct symbol *symbols; /* Pointer to semantic meaning list */
54 unsigned char len; /* Length of identifier name */
55 unsigned char tainted:1,
56 reserved:1;
57 char name[]; /* Actual identifier */
60 enum token_type {
61 TOKEN_EOF,
62 TOKEN_ERROR,
63 TOKEN_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 enum special_token {
98 SPECIAL_BASE = 256,
99 SPECIAL_ADD_ASSIGN = SPECIAL_BASE,
100 SPECIAL_INCREMENT,
101 SPECIAL_SUB_ASSIGN,
102 SPECIAL_DECREMENT,
103 SPECIAL_DEREFERENCE,
104 SPECIAL_MUL_ASSIGN,
105 SPECIAL_DIV_ASSIGN,
106 SPECIAL_MOD_ASSIGN,
107 SPECIAL_DOTDOT,
108 SPECIAL_ELLIPSIS,
109 SPECIAL_LTE,
110 SPECIAL_LEFTSHIFT,
111 SPECIAL_SHL_ASSIGN,
112 SPECIAL_GTE,
113 SPECIAL_RIGHTSHIFT,
114 SPECIAL_SHR_ASSIGN,
115 SPECIAL_EQUAL,
116 SPECIAL_NOTEQUAL,
117 SPECIAL_LOGICAL_AND,
118 SPECIAL_AND_ASSIGN,
119 SPECIAL_LOGICAL_OR,
120 SPECIAL_OR_ASSIGN,
121 SPECIAL_XOR_ASSIGN,
122 SPECIAL_HASHHASH,
123 SPECIAL_ARG_SEPARATOR,
124 SPECIAL_UNSIGNED_LT,
125 SPECIAL_UNSIGNED_GT,
126 SPECIAL_UNSIGNED_LTE,
127 SPECIAL_UNSIGNED_GTE,
130 struct string {
131 unsigned int length;
132 char data[];
135 /* will fit into 32 bits */
136 struct argcount {
137 unsigned normal:10;
138 unsigned quoted:10;
139 unsigned str:10;
140 unsigned vararg:1;
144 * This is a very common data structure, it should be kept
145 * as small as humanly possible. Big (rare) types go as
146 * pointers.
148 struct token {
149 struct position pos;
150 struct token *next;
151 union {
152 const char *number;
153 struct ident *ident;
154 unsigned int special;
155 struct string *string;
156 int character;
157 int argnum;
158 struct argcount count;
162 #define MAX_STRING 4095
164 static inline struct token *containing_token(struct token **p)
166 void *addr = (char*)p - ((char*)&((struct token *)0)->next - (char*)0);
167 return addr;
170 #define token_type(x) ((x)->pos.type)
173 * Last token in the stream - points to itself.
174 * This allows us to not test for NULL pointers
175 * when following the token->next chain..
177 extern struct token eof_token_entry;
178 #define eof_token(x) ((x) == &eof_token_entry)
180 extern int init_stream(const char *, int fd, const char **next_path);
181 extern const char *stream_name(int stream);
182 extern struct ident *hash_ident(struct ident *);
183 extern struct ident *built_in_ident(const char *);
184 extern struct token *built_in_token(int, const char *);
185 extern const char *show_special(int);
186 extern const char *show_ident(const struct ident *);
187 extern const char *show_string(const struct string *string);
188 extern const char *show_token(const struct token *);
189 extern struct token * tokenize(const char *, int, struct token *, const char **next_path);
190 extern struct token * tokenize_buffer(unsigned char *, unsigned long, struct token *);
192 extern void show_identifier_stats(void);
193 extern struct token *preprocess(struct token *);
195 static inline int match_op(struct token *token, int op)
197 return token->pos.type == TOKEN_SPECIAL && token->special == op;
200 static inline int match_ident(struct token *token, struct ident *id)
202 return token->pos.type == TOKEN_IDENT && token->ident == id;
205 #endif