[PATCH] Add -Wno-cast-truncate
[smatch.git] / token.h
blob96b0c416c4a6b14253481f23204f6df2ecf3f8b4
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 dirty;
44 struct ident *protect;
45 struct token *ifndef;
46 struct token *top_if;
49 extern int input_stream_nr;
50 extern struct stream *input_streams;
52 struct ident {
53 struct ident *next; /* Hash chain of identifiers */
54 struct symbol *symbols; /* Pointer to semantic meaning list */
55 unsigned char len; /* Length of identifier name */
56 unsigned char tainted:1,
57 reserved:1;
58 char name[]; /* Actual identifier */
61 enum token_type {
62 TOKEN_EOF,
63 TOKEN_ERROR,
64 TOKEN_IDENT,
65 TOKEN_ZERO_IDENT,
66 TOKEN_NUMBER,
67 TOKEN_CHAR,
68 TOKEN_STRING,
69 TOKEN_SPECIAL,
70 TOKEN_STREAMBEGIN,
71 TOKEN_STREAMEND,
72 TOKEN_MACRO_ARGUMENT,
73 TOKEN_STR_ARGUMENT,
74 TOKEN_QUOTED_ARGUMENT,
75 TOKEN_CONCAT,
76 TOKEN_GNU_KLUDGE,
77 TOKEN_UNTAINT,
78 TOKEN_ARG_COUNT,
79 TOKEN_IF,
80 TOKEN_SKIP_GROUPS,
81 TOKEN_ELSE,
84 /* Combination tokens */
85 #define COMBINATION_STRINGS { \
86 "+=", "++", \
87 "-=", "--", "->", \
88 "*=", \
89 "/=", \
90 "%=", \
91 "..", "...", \
92 "<=", "<<", "<<=", \
93 ">=", ">>", ">>=", \
94 "==", "!=", \
95 "&&", "&=", \
96 "||", "|=", \
97 "^=", "##", \
98 "", \
99 "<", ">", "<=", ">=" \
102 extern unsigned char combinations[][3];
104 enum special_token {
105 SPECIAL_BASE = 256,
106 SPECIAL_ADD_ASSIGN = SPECIAL_BASE,
107 SPECIAL_INCREMENT,
108 SPECIAL_SUB_ASSIGN,
109 SPECIAL_DECREMENT,
110 SPECIAL_DEREFERENCE,
111 SPECIAL_MUL_ASSIGN,
112 SPECIAL_DIV_ASSIGN,
113 SPECIAL_MOD_ASSIGN,
114 SPECIAL_DOTDOT,
115 SPECIAL_ELLIPSIS,
116 SPECIAL_LTE,
117 SPECIAL_LEFTSHIFT,
118 SPECIAL_SHL_ASSIGN,
119 SPECIAL_GTE,
120 SPECIAL_RIGHTSHIFT,
121 SPECIAL_SHR_ASSIGN,
122 SPECIAL_EQUAL,
123 SPECIAL_NOTEQUAL,
124 SPECIAL_LOGICAL_AND,
125 SPECIAL_AND_ASSIGN,
126 SPECIAL_LOGICAL_OR,
127 SPECIAL_OR_ASSIGN,
128 SPECIAL_XOR_ASSIGN,
129 SPECIAL_HASHHASH,
130 SPECIAL_ARG_SEPARATOR,
131 SPECIAL_UNSIGNED_LT,
132 SPECIAL_UNSIGNED_GT,
133 SPECIAL_UNSIGNED_LTE,
134 SPECIAL_UNSIGNED_GTE,
137 struct string {
138 unsigned int length;
139 char data[];
142 /* will fit into 32 bits */
143 struct argcount {
144 unsigned normal:10;
145 unsigned quoted:10;
146 unsigned str:10;
147 unsigned vararg:1;
151 * This is a very common data structure, it should be kept
152 * as small as humanly possible. Big (rare) types go as
153 * pointers.
155 struct token {
156 struct position pos;
157 struct token *next;
158 union {
159 const char *number;
160 struct ident *ident;
161 unsigned int special;
162 struct string *string;
163 int character;
164 int argnum;
165 struct argcount count;
169 #define MAX_STRING 4095
171 static inline struct token *containing_token(struct token **p)
173 void *addr = (char*)p - ((char*)&((struct token *)0)->next - (char*)0);
174 return addr;
177 #define token_type(x) ((x)->pos.type)
180 * Last token in the stream - points to itself.
181 * This allows us to not test for NULL pointers
182 * when following the token->next chain..
184 extern struct token eof_token_entry;
185 #define eof_token(x) ((x) == &eof_token_entry)
187 extern int init_stream(const char *, int fd, const char **next_path);
188 extern const char *stream_name(int stream);
189 extern struct ident *hash_ident(struct ident *);
190 extern struct ident *built_in_ident(const char *);
191 extern struct token *built_in_token(int, const char *);
192 extern const char *show_special(int);
193 extern const char *show_ident(const struct ident *);
194 extern const char *show_string(const struct string *string);
195 extern const char *show_token(const struct token *);
196 extern struct token * tokenize(const char *, int, struct token *, const char **next_path);
197 extern struct token * tokenize_buffer(void *, unsigned long, struct token *);
199 extern void show_identifier_stats(void);
200 extern struct token *preprocess(struct token *);
202 static inline int match_op(struct token *token, int op)
204 return token->pos.type == TOKEN_SPECIAL && token->special == op;
207 static inline int match_ident(struct token *token, struct ident *id)
209 return token->pos.type == TOKEN_IDENT && token->ident == id;
212 #endif