* source.c: Make global variables current_source_symtab and
[platform/upstream/binutils.git] / gdb / macroscope.c
1 /* Functions for deciding which macros are currently in scope.
2    Copyright 2002 Free Software Foundation, Inc.
3    Contributed by Red Hat, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23
24 #include "macroscope.h"
25 #include "symtab.h"
26 #include "source.h"
27 #include "target.h"
28 #include "frame.h"
29 #include "inferior.h"
30
31
32 struct macro_scope *
33 sal_macro_scope (struct symtab_and_line sal)
34 {
35   struct macro_source_file *main;
36   struct macro_scope *ms;
37
38   if (! sal.symtab
39       || ! sal.symtab->macro_table)
40     return 0;
41
42   ms = (struct macro_scope *) xmalloc (sizeof (*ms));
43
44   main = macro_main (sal.symtab->macro_table);
45   ms->file = macro_lookup_inclusion (main, sal.symtab->filename);
46
47   if (! ms->file)
48     internal_error
49       (__FILE__, __LINE__,
50        "\n"
51        "the symtab `%s' refers to a preprocessor macro table which doesn't\n"
52        "have any record of processing a file by that name.\n",
53        sal.symtab->filename);
54
55   ms->line = sal.line;
56
57   return ms;
58 }
59
60
61 struct macro_scope *
62 default_macro_scope (void)
63 {
64   struct symtab_and_line sal;
65   struct macro_source_file *main;
66   struct macro_scope *ms;
67
68   /* If there's a selected frame, use its PC.  */ 
69   if (selected_frame)
70     sal = find_pc_line (selected_frame->pc, 0);
71   
72   /* If the target has any registers at all, then use its PC.  Why we
73      would have registers but no stack, I'm not sure.  */
74   else if (target_has_registers)
75     sal = find_pc_line (read_pc (), 0);
76
77   /* If all else fails, fall back to the current listing position.  */
78   else
79     {
80       /* Don't call select_source_symtab here.  That can raise an
81          error if symbols aren't loaded, but GDB calls the expression
82          evaluator in all sorts of contexts.
83
84          For example, commands like `set width' call the expression
85          evaluator to evaluate their numeric arguments.  If the
86          current language is C, then that may call this function to
87          choose a scope for macro expansion.  If you don't have any
88          symbol files loaded, then get_current_or_default would raise an
89          error.  But `set width' shouldn't raise an error just because
90          it can't decide which scope to macro-expand its argument in.  */
91       struct symtab_and_line cursal = 
92                         get_current_source_symtab_and_line ();
93       
94       sal.symtab = cursal.symtab;
95       sal.line = cursal.line;
96     }
97
98   return sal_macro_scope (sal);
99 }
100
101
102 /* Look up the definition of the macro named NAME in scope at the source
103    location given by BATON, which must be a pointer to a `struct
104    macro_scope' structure.  */
105 struct macro_definition *
106 standard_macro_lookup (const char *name, void *baton)
107 {
108   struct macro_scope *ms = (struct macro_scope *) baton;
109
110   return macro_lookup_definition (ms->file, ms->line, name);
111 }