xref: /openbsd-src/gnu/usr.bin/binutils/gdb/macroscope.c (revision b725ae7711052a2233e31a66fefb8a752c388d7a)
1*b725ae77Skettenis /* Functions for deciding which macros are currently in scope.
2*b725ae77Skettenis    Copyright 2002 Free Software Foundation, Inc.
3*b725ae77Skettenis    Contributed by Red Hat, Inc.
4*b725ae77Skettenis 
5*b725ae77Skettenis    This file is part of GDB.
6*b725ae77Skettenis 
7*b725ae77Skettenis    This program is free software; you can redistribute it and/or modify
8*b725ae77Skettenis    it under the terms of the GNU General Public License as published by
9*b725ae77Skettenis    the Free Software Foundation; either version 2 of the License, or
10*b725ae77Skettenis    (at your option) any later version.
11*b725ae77Skettenis 
12*b725ae77Skettenis    This program is distributed in the hope that it will be useful,
13*b725ae77Skettenis    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*b725ae77Skettenis    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*b725ae77Skettenis    GNU General Public License for more details.
16*b725ae77Skettenis 
17*b725ae77Skettenis    You should have received a copy of the GNU General Public License
18*b725ae77Skettenis    along with this program; if not, write to the Free Software
19*b725ae77Skettenis    Foundation, Inc., 59 Temple Place - Suite 330,
20*b725ae77Skettenis    Boston, MA 02111-1307, USA.  */
21*b725ae77Skettenis 
22*b725ae77Skettenis #include "defs.h"
23*b725ae77Skettenis 
24*b725ae77Skettenis #include "macroscope.h"
25*b725ae77Skettenis #include "symtab.h"
26*b725ae77Skettenis #include "source.h"
27*b725ae77Skettenis #include "target.h"
28*b725ae77Skettenis #include "frame.h"
29*b725ae77Skettenis #include "inferior.h"
30*b725ae77Skettenis #include "complaints.h"
31*b725ae77Skettenis 
32*b725ae77Skettenis 
33*b725ae77Skettenis struct macro_scope *
sal_macro_scope(struct symtab_and_line sal)34*b725ae77Skettenis sal_macro_scope (struct symtab_and_line sal)
35*b725ae77Skettenis {
36*b725ae77Skettenis   struct macro_source_file *main, *inclusion;
37*b725ae77Skettenis   struct macro_scope *ms;
38*b725ae77Skettenis 
39*b725ae77Skettenis   if (! sal.symtab
40*b725ae77Skettenis       || ! sal.symtab->macro_table)
41*b725ae77Skettenis     return 0;
42*b725ae77Skettenis 
43*b725ae77Skettenis   ms = (struct macro_scope *) xmalloc (sizeof (*ms));
44*b725ae77Skettenis 
45*b725ae77Skettenis   main = macro_main (sal.symtab->macro_table);
46*b725ae77Skettenis   inclusion = macro_lookup_inclusion (main, sal.symtab->filename);
47*b725ae77Skettenis 
48*b725ae77Skettenis   if (inclusion)
49*b725ae77Skettenis     {
50*b725ae77Skettenis       ms->file = inclusion;
51*b725ae77Skettenis       ms->line = sal.line;
52*b725ae77Skettenis     }
53*b725ae77Skettenis   else
54*b725ae77Skettenis     {
55*b725ae77Skettenis       /* There are, unfortunately, cases where a compilation unit can
56*b725ae77Skettenis          have a symtab for a source file that doesn't appear in the
57*b725ae77Skettenis          macro table.  For example, at the moment, Dwarf doesn't have
58*b725ae77Skettenis          any way in the .debug_macinfo section to describe the effect
59*b725ae77Skettenis          of #line directives, so if you debug a YACC parser you'll get
60*b725ae77Skettenis          a macro table which only mentions the .c files generated by
61*b725ae77Skettenis          YACC, but symtabs that mention the .y files consumed by YACC.
62*b725ae77Skettenis 
63*b725ae77Skettenis          In the long run, we should extend the Dwarf macro info
64*b725ae77Skettenis          representation to handle #line directives, and get GCC to
65*b725ae77Skettenis          emit it.
66*b725ae77Skettenis 
67*b725ae77Skettenis          For the time being, though, we'll just treat these as
68*b725ae77Skettenis          occurring at the end of the main source file.  */
69*b725ae77Skettenis       ms->file = main;
70*b725ae77Skettenis       ms->line = -1;
71*b725ae77Skettenis 
72*b725ae77Skettenis       complaint (&symfile_complaints,
73*b725ae77Skettenis                  "symtab found for `%s', but that file\n"
74*b725ae77Skettenis                  "is not covered in the compilation unit's macro information",
75*b725ae77Skettenis                  sal.symtab->filename);
76*b725ae77Skettenis     }
77*b725ae77Skettenis 
78*b725ae77Skettenis   return ms;
79*b725ae77Skettenis }
80*b725ae77Skettenis 
81*b725ae77Skettenis 
82*b725ae77Skettenis struct macro_scope *
default_macro_scope(void)83*b725ae77Skettenis default_macro_scope (void)
84*b725ae77Skettenis {
85*b725ae77Skettenis   struct symtab_and_line sal;
86*b725ae77Skettenis   struct macro_source_file *main;
87*b725ae77Skettenis   struct macro_scope *ms;
88*b725ae77Skettenis 
89*b725ae77Skettenis   /* If there's a selected frame, use its PC.  */
90*b725ae77Skettenis   if (deprecated_selected_frame)
91*b725ae77Skettenis     sal = find_pc_line (get_frame_pc (deprecated_selected_frame), 0);
92*b725ae77Skettenis 
93*b725ae77Skettenis   /* If the target has any registers at all, then use its PC.  Why we
94*b725ae77Skettenis      would have registers but no stack, I'm not sure.  */
95*b725ae77Skettenis   else if (target_has_registers)
96*b725ae77Skettenis     sal = find_pc_line (read_pc (), 0);
97*b725ae77Skettenis 
98*b725ae77Skettenis   /* If all else fails, fall back to the current listing position.  */
99*b725ae77Skettenis   else
100*b725ae77Skettenis     {
101*b725ae77Skettenis       /* Don't call select_source_symtab here.  That can raise an
102*b725ae77Skettenis          error if symbols aren't loaded, but GDB calls the expression
103*b725ae77Skettenis          evaluator in all sorts of contexts.
104*b725ae77Skettenis 
105*b725ae77Skettenis          For example, commands like `set width' call the expression
106*b725ae77Skettenis          evaluator to evaluate their numeric arguments.  If the
107*b725ae77Skettenis          current language is C, then that may call this function to
108*b725ae77Skettenis          choose a scope for macro expansion.  If you don't have any
109*b725ae77Skettenis          symbol files loaded, then get_current_or_default would raise an
110*b725ae77Skettenis          error.  But `set width' shouldn't raise an error just because
111*b725ae77Skettenis          it can't decide which scope to macro-expand its argument in.  */
112*b725ae77Skettenis       struct symtab_and_line cursal =
113*b725ae77Skettenis       			get_current_source_symtab_and_line ();
114*b725ae77Skettenis 
115*b725ae77Skettenis       sal.symtab = cursal.symtab;
116*b725ae77Skettenis       sal.line = cursal.line;
117*b725ae77Skettenis     }
118*b725ae77Skettenis 
119*b725ae77Skettenis   return sal_macro_scope (sal);
120*b725ae77Skettenis }
121*b725ae77Skettenis 
122*b725ae77Skettenis 
123*b725ae77Skettenis /* Look up the definition of the macro named NAME in scope at the source
124*b725ae77Skettenis    location given by BATON, which must be a pointer to a `struct
125*b725ae77Skettenis    macro_scope' structure.  */
126*b725ae77Skettenis struct macro_definition *
standard_macro_lookup(const char * name,void * baton)127*b725ae77Skettenis standard_macro_lookup (const char *name, void *baton)
128*b725ae77Skettenis {
129*b725ae77Skettenis   struct macro_scope *ms = (struct macro_scope *) baton;
130*b725ae77Skettenis 
131*b725ae77Skettenis   return macro_lookup_definition (ms->file, ms->line, name);
132*b725ae77Skettenis }
133