Skip to content

Commit 8ae29ee

Browse files
committed
Let create_symbol check for previous same symbol
Avoid create a new one if same symbol exists. Signed-off-By: Christopher Li <[email protected]>
1 parent d8925f1 commit 8ae29ee

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

parse.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,10 @@ void init_parser(int stream)
671671
const char * name = ignored_attributes[i];
672672
struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD,
673673
NS_KEYWORD);
674-
sym->ident->keyword = 1;
675-
sym->op = &ignore_attr_op;
674+
if (!sym->op) {
675+
sym->ident->keyword = 1;
676+
sym->op = &ignore_attr_op;
677+
}
676678
}
677679
}
678680

symbol.c

+12-3
Original file line numberDiff line numberDiff line change
@@ -639,10 +639,19 @@ void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
639639

640640
struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
641641
{
642-
struct token *token = built_in_token(stream, name);
643-
struct symbol *sym = alloc_symbol(token->pos, type);
642+
struct ident *ident = built_in_ident(name);
643+
struct symbol *sym = lookup_symbol(ident, namespace);
644644

645-
bind_symbol(sym, token->ident, namespace);
645+
if (sym && sym->type != type)
646+
die("symbol %s created with different types: %d old %d", name,
647+
type, sym->type);
648+
649+
if (!sym) {
650+
struct token *token = built_in_token(stream, ident);
651+
652+
sym = alloc_symbol(token->pos, type);
653+
bind_symbol(sym, token->ident, namespace);
654+
}
646655
return sym;
647656
}
648657

token.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ extern int init_stream(const char *, int fd, const char **next_path);
218218
extern const char *stream_name(int stream);
219219
extern struct ident *hash_ident(struct ident *);
220220
extern struct ident *built_in_ident(const char *);
221-
extern struct token *built_in_token(int, const char *);
221+
extern struct token *built_in_token(int, struct ident *);
222222
extern const char *show_special(int);
223223
extern const char *show_ident(const struct ident *);
224224
extern const char *show_string(const struct string *string);

tokenize.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -904,14 +904,14 @@ struct ident *built_in_ident(const char *name)
904904
return create_hashed_ident(name, len, hash_name(name, len));
905905
}
906906

907-
struct token *built_in_token(int stream, const char *name)
907+
struct token *built_in_token(int stream, struct ident *ident)
908908
{
909909
struct token *token;
910910

911911
token = __alloc_token(0);
912912
token->pos.stream = stream;
913913
token_type(token) = TOKEN_IDENT;
914-
token->ident = built_in_ident(name);
914+
token->ident = ident;
915915
return token;
916916
}
917917

0 commit comments

Comments
 (0)