diff options
| -rw-r--r-- | ident-list.h | 62 | ||||
| -rw-r--r-- | symbol.c | 13 | ||||
| -rw-r--r-- | symbol.h | 2 | ||||
| -rw-r--r-- | token.h | 3 |
4 files changed, 56 insertions, 24 deletions
diff --git a/ident-list.h b/ident-list.h index a57e0f16..e7eed0c2 100644 --- a/ident-list.h +++ b/ident-list.h @@ -1,17 +1,41 @@ -#ifndef IDENT -#define IDENT(n) __IDENT(n## _ident, #n) -#endif +#define IDENT(n) __IDENT(n## _ident, #n, 0) +#define IDENT_RESERVED(n) __IDENT(n## _ident, #n, 1) -IDENT(struct); IDENT(union); IDENT(enum); -IDENT(sizeof); IDENT(alignof); IDENT(__alignof); -IDENT(__alignof__); IDENT(if); IDENT(else); -IDENT(return); IDENT(switch); IDENT(case); -IDENT(default); IDENT(break); IDENT(continue); -IDENT(for); IDENT(while); IDENT(do); -IDENT(goto); IDENT(__asm__); IDENT(__asm); -IDENT(asm); IDENT(__volatile__); IDENT(__volatile); -IDENT(volatile); IDENT(__attribute__); IDENT(__attribute); +/* Basic C reserved words.. */ +IDENT_RESERVED(sizeof); +IDENT_RESERVED(if); +IDENT_RESERVED(else); +IDENT_RESERVED(return); +IDENT_RESERVED(switch); +IDENT_RESERVED(case); +IDENT_RESERVED(default); +IDENT_RESERVED(break); +IDENT_RESERVED(continue); +IDENT_RESERVED(for); +IDENT_RESERVED(while); +IDENT_RESERVED(do); +IDENT_RESERVED(goto); + +/* C typenames. They get marked as reserved when initialized */ +IDENT(struct); +IDENT(union); +IDENT(enum); +IDENT(__attribute__); +IDENT(__attribute); +IDENT(volatile); + +/* Extended gcc identifiers */ +IDENT(asm); +IDENT(__asm__); +IDENT(__asm); +IDENT(alignof); +IDENT(__alignof); +IDENT(__alignof__); +IDENT(__volatile__); +IDENT(__volatile); + +/* Attribute names */ IDENT(defined); IDENT(packed); IDENT(__packed__); IDENT(aligned); IDENT(__aligned__); IDENT(nocast); IDENT(noderef); IDENT(safe); IDENT(force); @@ -29,11 +53,13 @@ IDENT(syscall_linkage); IDENT(visibility); IDENT(bitwise); IDENT(model); IDENT(__model__); -__IDENT(pragma_ident, "__pragma__"); -__IDENT(__VA_ARGS___ident, "__VA_ARGS__"); -__IDENT(__LINE___ident, "__LINE__"); -__IDENT(__FILE___ident, "__FILE__"); -__IDENT(__func___ident, "__func__"); +/* Preprocessor idents */ +__IDENT(pragma_ident, "__pragma__", 0); +__IDENT(__VA_ARGS___ident, "__VA_ARGS__", 0); +__IDENT(__LINE___ident, "__LINE__", 0); +__IDENT(__FILE___ident, "__FILE__", 0); +__IDENT(__func___ident, "__func__", 0); #undef __IDENT - +#undef IDENT +#undef IDENT_RESERVED @@ -357,6 +357,10 @@ void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns) warning(sym->pos, "internal error: symbol type already bound"); return; } + if (ident->reserved && (ns & (NS_TYPEDEF | NS_STRUCT | NS_LABEL | NS_SYMBOL))) { + warning(sym->pos, "Trying to use reserved word '%s' as identifier", show_ident(ident)); + return; + } sym->namespace = ns; sym->next_id = ident->symbols; ident->symbols = sym; @@ -572,9 +576,9 @@ struct symbol bool_ctype, void_ctype, type_ctype, incomplete_ctype, label_ctype, bad_enum_ctype; -#define __INIT_IDENT(str) { .len = sizeof(str)-1, .name = str } -#define __IDENT(n,str) \ - struct ident n = __INIT_IDENT(str) +#define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res } +#define __IDENT(n,str,res) \ + struct ident n = __INIT_IDENT(str,res) #include "ident-list.h" @@ -583,13 +587,14 @@ void init_symbols(void) int stream = init_stream("builtin", -1, includepath); struct sym_init *ptr; -#define __IDENT(n,str) \ +#define __IDENT(n,str,res) \ hash_ident(&n) #include "ident-list.h" for (ptr = symbol_init_table; ptr->name; ptr++) { struct symbol *sym; sym = create_symbol(stream, ptr->name, SYM_NODE, NS_TYPEDEF); + sym->ident->reserved = 1; sym->ctype.base_type = ptr->base_type; sym->ctype.modifiers = ptr->modifiers; } @@ -175,7 +175,7 @@ extern struct symbol bool_ctype, void_ctype, type_ctype, incomplete_ctype, label_ctype, bad_enum_ctype; -#define __IDENT(n,str) \ +#define __IDENT(n,str,res) \ extern struct ident n #include "ident-list.h" @@ -54,7 +54,8 @@ struct ident { struct ident *next; /* Hash chain of identifiers */ struct symbol *symbols; /* Pointer to semantic meaning list */ unsigned char len; /* Length of identifier name */ - unsigned char tainted; + unsigned char tainted:1, + reserved:1; char name[]; /* Actual identifier */ }; |
