[PATCH] parser.c cleanup
[smatch.git] / token.h
blobecb8ea0150c9e03a6d8ad6a815e26fe285d984c9
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;
44 dev_t dev;
45 ino_t ino;
48 extern int input_stream_nr;
49 extern struct stream *input_streams;
51 extern int ident_hit, ident_miss;
53 struct ident {
54 struct ident *next; /* Hash chain of identifiers */
55 struct symbol *symbols; /* Pointer to semantic meaning list */
56 unsigned char len; /* Length of identifier name */
57 unsigned char tainted;
58 char name[]; /* Actual identifier */
61 enum token_type {
62 TOKEN_EOF,
63 TOKEN_ERROR,
64 TOKEN_IDENT,
65 TOKEN_NUMBER,
66 TOKEN_CHAR,
67 TOKEN_STRING,
68 TOKEN_SPECIAL,
69 TOKEN_STREAMBEGIN,
70 TOKEN_STREAMEND,
71 TOKEN_MACRO_ARGUMENT,
72 TOKEN_STR_ARGUMENT,
73 TOKEN_QUOTED_ARGUMENT,
74 TOKEN_CONCAT,
75 TOKEN_GNU_KLUDGE,
76 TOKEN_UNTAINT,
77 TOKEN_ARG_COUNT,
80 /* Combination tokens */
81 #define COMBINATION_STRINGS { \
82 "+=", "++", \
83 "-=", "--", "->", \
84 "*=", \
85 "/=", \
86 "%=", \
87 "..", "...", \
88 "<=", "<<", "<<=", \
89 ">=", ">>", ">>=", \
90 "==", "!=", \
91 "&&", "&=", \
92 "||", "|=", \
93 "^=", "##", \
94 "", \
95 "<", ">", "<=", ">=" \
98 enum special_token {
99 SPECIAL_BASE = 256,
100 SPECIAL_ADD_ASSIGN = SPECIAL_BASE,
101 SPECIAL_INCREMENT,
102 SPECIAL_SUB_ASSIGN,
103 SPECIAL_DECREMENT,
104 SPECIAL_DEREFERENCE,
105 SPECIAL_MUL_ASSIGN,
106 SPECIAL_DIV_ASSIGN,
107 SPECIAL_MOD_ASSIGN,
108 SPECIAL_DOTDOT,
109 SPECIAL_ELLIPSIS,
110 SPECIAL_LTE,
111 SPECIAL_LEFTSHIFT,
112 SPECIAL_SHL_ASSIGN,
113 SPECIAL_GTE,
114 SPECIAL_RIGHTSHIFT,
115 SPECIAL_SHR_ASSIGN,
116 SPECIAL_EQUAL,
117 SPECIAL_NOTEQUAL,
118 SPECIAL_LOGICAL_AND,
119 SPECIAL_AND_ASSIGN,
120 SPECIAL_LOGICAL_OR,
121 SPECIAL_OR_ASSIGN,
122 SPECIAL_XOR_ASSIGN,
123 SPECIAL_HASHHASH,
124 SPECIAL_ARG_SEPARATOR,
125 SPECIAL_UNSIGNED_LT,
126 SPECIAL_UNSIGNED_GT,
127 SPECIAL_UNSIGNED_LTE,
128 SPECIAL_UNSIGNED_GTE,
131 struct string {
132 unsigned int length;
133 char data[];
136 /* will fit into 32 bits */
137 struct argcount {
138 unsigned normal:10;
139 unsigned quoted:10;
140 unsigned str:10;
141 unsigned vararg:1;
145 * This is a very common data structure, it should be kept
146 * as small as humanly possible. Big (rare) types go as
147 * pointers.
149 struct token {
150 struct position pos;
151 struct token *next;
152 union {
153 char *number;
154 struct ident *ident;
155 unsigned int special;
156 struct string *string;
157 int character;
158 int argnum;
159 struct argcount count;
163 #define MAX_STRING 4095
165 static inline struct token *containing_token(struct token **p)
167 void *addr = (char*)p - ((char*)&((struct token *)0)->next - (char*)0);
168 return addr;
171 #define token_type(x) ((x)->pos.type)
174 * Last token in the stream - points to itself.
175 * This allows us to not test for NULL pointers
176 * when following the token->next chain..
178 extern int preprocessing, verbose;
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 struct ident *hash_ident(struct ident *);
184 extern struct ident *built_in_ident(const char *);
185 extern struct token *built_in_token(int, const char *);
186 extern const char *show_special(int);
187 extern const char *show_ident(const struct ident *);
188 extern const char *show_string(const struct string *string);
189 extern const char *show_token(const struct token *);
190 extern struct token * tokenize(const char *, int, struct token *, const char **next_path);
191 extern struct token * tokenize_buffer(unsigned char *, unsigned long, struct token *);
193 extern void die(const char *, ...);
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