4 * This is pretty trivial.
6 * Copyright (C) 2003 Transmeta Corp.
7 * 2003-2004 Linus Torvalds
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
36 static struct scope builtin_scope
= { .next
= &builtin_scope
};
38 struct scope
*block_scope
= &builtin_scope
, // regular automatic variables etc
39 *function_scope
= &builtin_scope
, // labels, arguments etc
40 *file_scope
= &builtin_scope
, // static
41 *global_scope
= &builtin_scope
; // externally visible
43 void bind_scope(struct symbol
*sym
, struct scope
*scope
)
46 add_symbol(&scope
->symbols
, sym
);
50 void rebind_scope(struct symbol
*sym
, struct scope
*new)
52 struct scope
*old
= sym
->scope
;
58 delete_ptr_list_entry((struct ptr_list
**) &old
->symbols
, sym
, 1);
63 static void start_scope(struct scope
**s
)
65 struct scope
*scope
= __alloc_scope(0);
66 memset(scope
, 0, sizeof(*scope
));
71 void start_file_scope(void)
73 struct scope
*scope
= __alloc_scope(0);
75 memset(scope
, 0, sizeof(*scope
));
76 scope
->next
= &builtin_scope
;
79 /* top-level stuff defaults to file scope, "extern" etc will choose global scope */
80 function_scope
= scope
;
84 void start_symbol_scope(void)
86 start_scope(&block_scope
);
89 void start_function_scope(void)
91 start_scope(&function_scope
);
92 start_scope(&block_scope
);
95 static void remove_symbol_scope(struct symbol
*sym
)
97 struct symbol
**ptr
= &sym
->ident
->symbols
;
100 ptr
= &(*ptr
)->next_id
;
104 static void end_scope(struct scope
**s
)
106 struct scope
*scope
= *s
;
107 struct symbol_list
*symbols
= scope
->symbols
;
111 scope
->symbols
= NULL
;
112 FOR_EACH_PTR(symbols
, sym
) {
113 remove_symbol_scope(sym
);
114 } END_FOR_EACH_PTR(sym
);
117 void end_file_scope(void)
119 end_scope(&file_scope
);
122 void new_file_scope(void)
124 if (file_scope
!= &builtin_scope
)
129 void end_symbol_scope(void)
131 end_scope(&block_scope
);
134 void end_function_scope(void)
136 end_scope(&block_scope
);
137 end_scope(&function_scope
);
140 int is_outer_scope(struct scope
*scope
)
142 if (scope
== block_scope
)
144 if (scope
== &builtin_scope
&& block_scope
->next
== &builtin_scope
)