Don't die on unknown expressions at linearization time.
[smatch.git] / token.h
blob1c9f2ab6ce9c01af77c43047b917999fb53fa4da
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 struct stream {
27 int fd;
28 const char *name;
30 /* Use these to check for "already parsed" */
31 int constant, nesting;
32 struct ident *protect;
33 dev_t dev;
34 ino_t ino;
37 extern int input_stream_nr;
38 extern struct stream *input_streams;
40 extern int ident_hit, ident_miss;
42 struct ident {
43 struct ident *next; /* Hash chain of identifiers */
44 struct symbol *symbols; /* Pointer to semantic meaning list */
45 unsigned char len; /* Length of identifier name */
46 unsigned char tainted;
47 char name[]; /* Actual identifier */
50 enum token_type {
51 TOKEN_EOF,
52 TOKEN_ERROR,
53 TOKEN_IDENT,
54 TOKEN_NUMBER,
55 TOKEN_CHAR,
56 TOKEN_STRING,
57 TOKEN_SPECIAL,
58 TOKEN_STREAMBEGIN,
59 TOKEN_STREAMEND,
60 TOKEN_MACRO_ARGUMENT,
61 TOKEN_STR_ARGUMENT,
62 TOKEN_QUOTED_ARGUMENT,
63 TOKEN_CONCAT,
64 TOKEN_GNU_KLUDGE,
65 TOKEN_UNTAINT,
66 TOKEN_ARG_COUNT,
69 /* Combination tokens */
70 #define COMBINATION_STRINGS { \
71 "+=", "++", \
72 "-=", "--", "->", \
73 "*=", \
74 "/=", \
75 "%=", \
76 "..", "...", \
77 "<=", "<<", "<<=", \
78 ">=", ">>", ">>=", \
79 "==", "!=", \
80 "&&", "&=", \
81 "||", "|=", \
82 "^=", "##", \
83 " @ ", \
86 enum special_token {
87 SPECIAL_BASE = 256,
88 SPECIAL_ADD_ASSIGN = 256,
89 SPECIAL_INCREMENT,
90 SPECIAL_SUB_ASSIGN,
91 SPECIAL_DECREMENT,
92 SPECIAL_DEREFERENCE,
93 SPECIAL_MUL_ASSIGN,
94 SPECIAL_DIV_ASSIGN,
95 SPECIAL_MOD_ASSIGN,
96 SPECIAL_DOTDOT,
97 SPECIAL_ELLIPSIS,
98 SPECIAL_LTE,
99 SPECIAL_LEFTSHIFT,
100 SPECIAL_SHL_ASSIGN,
101 SPECIAL_GTE,
102 SPECIAL_RIGHTSHIFT,
103 SPECIAL_SHR_ASSIGN,
104 SPECIAL_EQUAL,
105 SPECIAL_NOTEQUAL,
106 SPECIAL_LOGICAL_AND,
107 SPECIAL_AND_ASSIGN,
108 SPECIAL_LOGICAL_OR,
109 SPECIAL_OR_ASSIGN,
110 SPECIAL_XOR_ASSIGN,
111 SPECIAL_HASHHASH,
112 SPECIAL_ARG_SEPARATOR,
113 SPECIAL_UNSIGNED_LT,
114 SPECIAL_UNSIGNED_GT,
115 SPECIAL_UNSIGNED_LTE,
116 SPECIAL_UNSIGNED_GTE,
119 struct string {
120 unsigned int length;
121 char data[];
124 /* will fit into 32 bits */
125 struct argcount {
126 unsigned normal:10;
127 unsigned quoted:10;
128 unsigned str:10;
129 unsigned vararg:1;
133 * This is a very common data structure, it should be kept
134 * as small as humanly possible. Big (rare) types go as
135 * pointers.
137 struct token {
138 struct position pos;
139 struct token *next;
140 union {
141 char *number;
142 struct ident *ident;
143 unsigned int special;
144 struct string *string;
145 int character;
146 int argnum;
147 struct argcount count;
151 #define MAX_STRING 4095
153 static inline struct token *containing_token(struct token **p)
155 void *addr = (char*)p - ((char*)&((struct token *)0)->next - (char*)0);
156 return addr;
159 #define token_type(x) ((x)->pos.type)
162 * Last token in the stream - points to itself.
163 * This allows us to not test for NULL pointers
164 * when following the token->next chain..
166 extern int preprocessing, verbose;
167 extern struct token eof_token_entry;
168 #define eof_token(x) ((x) == &eof_token_entry)
170 extern int init_stream(const char *, int fd);
171 extern struct ident *hash_ident(struct ident *);
172 extern struct ident *built_in_ident(const char *);
173 extern struct token *built_in_token(int, const char *);
174 extern const char *show_special(int);
175 extern const char *show_ident(const struct ident *);
176 extern const char *show_string(const struct string *string);
177 extern const char *show_token(const struct token *);
178 extern struct token * tokenize(const char *, int, struct token *);
179 extern struct token * tokenize_buffer(unsigned char *, unsigned long, struct token *);
181 extern void die(const char *, ...);
182 extern void show_identifier_stats(void);
183 extern struct token *preprocess(struct token *);
185 static inline int match_op(struct token *token, int op)
187 return token->pos.type == TOKEN_SPECIAL && token->special == op;
190 static inline int match_ident(struct token *token, struct ident *id)
192 return token->pos.type == TOKEN_IDENT && token->ident == id;
195 #endif