[PATCH] Fix address space ordering problem
[smatch.git] / token.h
blobeb0180bcda0cb95f35ac16c665180ed92afa43c5
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_NUMBER,
64 TOKEN_CHAR,
65 TOKEN_STRING,
66 TOKEN_SPECIAL,
67 TOKEN_STREAMBEGIN,
68 TOKEN_STREAMEND,
69 TOKEN_MACRO_ARGUMENT,
70 TOKEN_STR_ARGUMENT,
71 TOKEN_QUOTED_ARGUMENT,
72 TOKEN_CONCAT,
73 TOKEN_GNU_KLUDGE,
74 TOKEN_UNTAINT,
75 TOKEN_ARG_COUNT,
78 /* Combination tokens */
79 #define COMBINATION_STRINGS { \
80 "+=", "++", \
81 "-=", "--", "->", \
82 "*=", \
83 "/=", \
84 "%=", \
85 "..", "...", \
86 "<=", "<<", "<<=", \
87 ">=", ">>", ">>=", \
88 "==", "!=", \
89 "&&", "&=", \
90 "||", "|=", \
91 "^=", "##", \
92 "", \
93 "<", ">", "<=", ">=" \
96 extern unsigned char combinations[][3];
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 const 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 struct token eof_token_entry;
179 #define eof_token(x) ((x) == &eof_token_entry)
181 extern int init_stream(const char *, int fd, const char **next_path);
182 extern const char *stream_name(int stream);
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(void *, unsigned long, struct token *);
193 extern void show_identifier_stats(void);
194 extern struct token *preprocess(struct token *);
196 static inline int match_op(struct token *token, int op)
198 return token->pos.type == TOKEN_SPECIAL && token->special == op;
201 static inline int match_ident(struct token *token, struct ident *id)
203 return token->pos.type == TOKEN_IDENT && token->ident == id;
206 #endif