aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/symbol.h
blob: 24a8b60aea1cb51f42629a4d29e531a6b44db410 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#ifndef SEMANTIC_H
#define SEMANTIC_H
/*
 * Basic symbol and namespace definitions.
 *
 * Copyright (C) 2003 Linus Torvalds, all rights reserved.
 */

#include "token.h"

/*
 * An identifier with semantic meaning is a "symbol".
 *
 * There's a 1:n relationship: each symbol is always
 * associated with one identifier, while each identifier
 * can have one or more semantic meanings due to C scope
 * rules.
 *
 * The progression is symbol -> token -> identifier. The
 * token contains the information on where the symbol was
 * declared.
 */
enum namespace {
	NS_NONE,
	NS_PREPROCESSOR,
	NS_TYPEDEF,
	NS_STRUCT,
	NS_ENUM,
	NS_LABEL,
	NS_SYMBOL,
};

enum type {
	SYM_NONE,
	SYM_TYPE,
	SYM_PTR,
	SYM_FN,
	SYM_ARRAY,
	SYM_STRUCT,
	SYM_UNION,
	SYM_ENUM,
	SYM_TYPEDEF,
	SYM_MEMBER,
	SYM_BITFIELD,
};

struct ctype {
	unsigned long modifiers;
	struct symbol *base_type;
};

struct symbol {
	enum namespace namespace:8;
	enum type type:8;
	struct token *token;		/* Where this symbol was declared */
	struct token *ident;		/* What identifier this symbol is associated with */
	struct symbol *next_id;		/* Next semantic symbol that shares this identifier */
	struct symbol **id_list;	/* Backpointer to symbol list head */
	union {
		struct preprocessor_sym {
			int busy;
			struct token *expansion;
			struct token *arglist;
		};
		struct ctype_symbol {
			struct symbol *next;		/* Next symbol at this level */
			unsigned long size;
			struct ctype ctype;
			struct symbol_list *arguments;
			struct statement *stmt;
			struct symbol_list *symbol_list;
		};
	};
};

/* Modifiers */
#define SYM_AUTO	0x0001
#define SYM_REGISTER	0x0002
#define SYM_STATIC	0x0004
#define SYM_EXTERN	0x0008

#define SYM_CONST	0x0010
#define SYM_VOLATILE	0x0020
#define SYM_SIGNED	0x0040
#define SYM_UNSIGNED	0x0080

#define SYM_CHAR	0x0100
#define SYM_SHORT	0x0200
#define SYM_LONG	0x0400
#define SYM_LONGLONG	0x0800

#define SYM_TYPEDEF	0x1000
#define SYM_STRUCTOF	0x2000
#define SYM_UNIONOF	0x4000
#define SYM_ENUMOF	0x8000

#define SYM_TYPEOF	0x10000
#define SYM_ATTRIBUTE	0x20000

/* Basic types */
extern struct symbol	void_type,
			int_type,
			fp_type,
			vector_type,
			bad_type;

/* Basic identifiers */
extern struct ident	sizeof_ident,
			alignof_ident,
			__alignof_ident,
			__alignof___ident,
			if_ident,
			else_ident,
			switch_ident,
			case_ident,
			default_ident,
			break_ident,
			continue_ident,
			for_ident,
			while_ident,
			do_ident,
			goto_ident,
			return_ident;

extern struct ident	__asm___ident,
			__asm_ident,
			asm_ident,
			__volatile___ident,
			__volatile_ident,
			volatile_ident,
			__attribute___ident,
			__attribute_ident;

#define symbol_is_typename(sym) ((sym)->type == SYM_TYPE)

extern struct symbol *lookup_symbol(struct ident *, enum namespace);
extern void init_symbols(void);
extern struct symbol *alloc_symbol(struct token *, int type);
extern void show_type(struct symbol *);
extern const char *modifier_string(unsigned long mod);
extern void show_symbol(struct symbol *);
extern void show_type_list(struct symbol *);
extern void show_symbol_list(struct symbol_list *, const char *);
extern void add_symbol(struct symbol_list **, struct symbol *);
extern void bind_symbol(struct symbol *, struct ident *, enum namespace);

#endif /* SEMANTIC_H */