Small cleanups. (White space).
[smatch.git] / scope.c
blob27e38bc9da5fb7be008ff38f5de407c2edd849d4
1 /*
2 * Symbol scoping.
4 * This is pretty trivial.
6 * Copyright (C) 2003 Transmeta Corp.
7 * 2003-2004 Linus Torvalds
9 * Licensed under the Open Software License version 1.1
11 #include <stdlib.h>
12 #include <string.h>
13 #include <stdio.h>
15 #include "lib.h"
16 #include "allocate.h"
17 #include "symbol.h"
18 #include "scope.h"
20 static struct scope builtin_scope = { .next = &builtin_scope };
22 struct scope *block_scope = &builtin_scope, // regular automatic variables etc
23 *function_scope = &builtin_scope, // labels, arguments etc
24 *file_scope = &builtin_scope, // static
25 *global_scope = &builtin_scope; // externally visible
27 void bind_scope(struct symbol *sym, struct scope *scope)
29 sym->scope = scope;
30 add_symbol(&scope->symbols, sym);
33 static void start_scope(struct scope **s)
35 struct scope *scope = __alloc_scope(0);
36 memset(scope, 0, sizeof(*scope));
37 scope->next = *s;
38 *s = scope;
41 void start_file_scope(void)
43 struct scope *scope = __alloc_scope(0);
45 memset(scope, 0, sizeof(*scope));
46 scope->next = &builtin_scope;
47 file_scope = scope;
49 /* top-level stuff defaults to file scope, "extern" etc will choose global scope */
50 function_scope = scope;
51 block_scope = scope;
54 void start_symbol_scope(void)
56 start_scope(&block_scope);
59 void start_function_scope(void)
61 start_scope(&function_scope);
62 start_scope(&block_scope);
65 static void remove_symbol_scope(struct symbol *sym)
67 struct symbol **ptr = &sym->ident->symbols;
69 while (*ptr != sym)
70 ptr = &(*ptr)->next_id;
71 *ptr = sym->next_id;
74 static void end_scope(struct scope **s)
76 struct scope *scope = *s;
77 struct symbol_list *symbols = scope->symbols;
78 struct symbol *sym;
80 *s = scope->next;
81 scope->symbols = NULL;
82 FOR_EACH_PTR(symbols, sym) {
83 remove_symbol_scope(sym);
84 } END_FOR_EACH_PTR(sym);
87 void end_file_scope(void)
89 end_scope(&file_scope);
92 void new_file_scope(void)
94 if (file_scope != &builtin_scope)
95 end_file_scope();
96 start_file_scope();
99 void end_symbol_scope(void)
101 end_scope(&block_scope);
104 void end_function_scope(void)
106 end_scope(&block_scope);
107 end_scope(&function_scope);
110 int is_outer_scope(struct scope *scope)
112 if (scope == block_scope)
113 return 0;
114 if (scope == &builtin_scope && block_scope->next == &builtin_scope)
115 return 0;
116 return 1;