1130803Smarcel/* Functions for deciding which macros are currently in scope.
2130803Smarcel   Copyright 2002 Free Software Foundation, Inc.
3130803Smarcel   Contributed by Red Hat, Inc.
4130803Smarcel
5130803Smarcel   This file is part of GDB.
6130803Smarcel
7130803Smarcel   This program is free software; you can redistribute it and/or modify
8130803Smarcel   it under the terms of the GNU General Public License as published by
9130803Smarcel   the Free Software Foundation; either version 2 of the License, or
10130803Smarcel   (at your option) any later version.
11130803Smarcel
12130803Smarcel   This program is distributed in the hope that it will be useful,
13130803Smarcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
14130803Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15130803Smarcel   GNU General Public License for more details.
16130803Smarcel
17130803Smarcel   You should have received a copy of the GNU General Public License
18130803Smarcel   along with this program; if not, write to the Free Software
19130803Smarcel   Foundation, Inc., 59 Temple Place - Suite 330,
20130803Smarcel   Boston, MA 02111-1307, USA.  */
21130803Smarcel
22130803Smarcel#include "defs.h"
23130803Smarcel
24130803Smarcel#include "macroscope.h"
25130803Smarcel#include "symtab.h"
26130803Smarcel#include "source.h"
27130803Smarcel#include "target.h"
28130803Smarcel#include "frame.h"
29130803Smarcel#include "inferior.h"
30130803Smarcel#include "complaints.h"
31130803Smarcel
32130803Smarcel
33130803Smarcelstruct macro_scope *
34130803Smarcelsal_macro_scope (struct symtab_and_line sal)
35130803Smarcel{
36130803Smarcel  struct macro_source_file *main, *inclusion;
37130803Smarcel  struct macro_scope *ms;
38130803Smarcel
39130803Smarcel  if (! sal.symtab
40130803Smarcel      || ! sal.symtab->macro_table)
41130803Smarcel    return 0;
42130803Smarcel
43130803Smarcel  ms = (struct macro_scope *) xmalloc (sizeof (*ms));
44130803Smarcel
45130803Smarcel  main = macro_main (sal.symtab->macro_table);
46130803Smarcel  inclusion = macro_lookup_inclusion (main, sal.symtab->filename);
47130803Smarcel
48130803Smarcel  if (inclusion)
49130803Smarcel    {
50130803Smarcel      ms->file = inclusion;
51130803Smarcel      ms->line = sal.line;
52130803Smarcel    }
53130803Smarcel  else
54130803Smarcel    {
55130803Smarcel      /* There are, unfortunately, cases where a compilation unit can
56130803Smarcel         have a symtab for a source file that doesn't appear in the
57130803Smarcel         macro table.  For example, at the moment, Dwarf doesn't have
58130803Smarcel         any way in the .debug_macinfo section to describe the effect
59130803Smarcel         of #line directives, so if you debug a YACC parser you'll get
60130803Smarcel         a macro table which only mentions the .c files generated by
61130803Smarcel         YACC, but symtabs that mention the .y files consumed by YACC.
62130803Smarcel
63130803Smarcel         In the long run, we should extend the Dwarf macro info
64130803Smarcel         representation to handle #line directives, and get GCC to
65130803Smarcel         emit it.
66130803Smarcel
67130803Smarcel         For the time being, though, we'll just treat these as
68130803Smarcel         occurring at the end of the main source file.  */
69130803Smarcel      ms->file = main;
70130803Smarcel      ms->line = -1;
71130803Smarcel
72130803Smarcel      complaint (&symfile_complaints,
73130803Smarcel                 "symtab found for `%s', but that file\n"
74130803Smarcel                 "is not covered in the compilation unit's macro information",
75130803Smarcel                 sal.symtab->filename);
76130803Smarcel    }
77130803Smarcel
78130803Smarcel  return ms;
79130803Smarcel}
80130803Smarcel
81130803Smarcel
82130803Smarcelstruct macro_scope *
83130803Smarceldefault_macro_scope (void)
84130803Smarcel{
85130803Smarcel  struct symtab_and_line sal;
86130803Smarcel  struct macro_source_file *main;
87130803Smarcel  struct macro_scope *ms;
88130803Smarcel
89130803Smarcel  /* If there's a selected frame, use its PC.  */
90130803Smarcel  if (deprecated_selected_frame)
91130803Smarcel    sal = find_pc_line (get_frame_pc (deprecated_selected_frame), 0);
92130803Smarcel
93130803Smarcel  /* If the target has any registers at all, then use its PC.  Why we
94130803Smarcel     would have registers but no stack, I'm not sure.  */
95130803Smarcel  else if (target_has_registers)
96130803Smarcel    sal = find_pc_line (read_pc (), 0);
97130803Smarcel
98130803Smarcel  /* If all else fails, fall back to the current listing position.  */
99130803Smarcel  else
100130803Smarcel    {
101130803Smarcel      /* Don't call select_source_symtab here.  That can raise an
102130803Smarcel         error if symbols aren't loaded, but GDB calls the expression
103130803Smarcel         evaluator in all sorts of contexts.
104130803Smarcel
105130803Smarcel         For example, commands like `set width' call the expression
106130803Smarcel         evaluator to evaluate their numeric arguments.  If the
107130803Smarcel         current language is C, then that may call this function to
108130803Smarcel         choose a scope for macro expansion.  If you don't have any
109130803Smarcel         symbol files loaded, then get_current_or_default would raise an
110130803Smarcel         error.  But `set width' shouldn't raise an error just because
111130803Smarcel         it can't decide which scope to macro-expand its argument in.  */
112130803Smarcel      struct symtab_and_line cursal =
113130803Smarcel      			get_current_source_symtab_and_line ();
114130803Smarcel
115130803Smarcel      sal.symtab = cursal.symtab;
116130803Smarcel      sal.line = cursal.line;
117130803Smarcel    }
118130803Smarcel
119130803Smarcel  return sal_macro_scope (sal);
120130803Smarcel}
121130803Smarcel
122130803Smarcel
123130803Smarcel/* Look up the definition of the macro named NAME in scope at the source
124130803Smarcel   location given by BATON, which must be a pointer to a `struct
125130803Smarcel   macro_scope' structure.  */
126130803Smarcelstruct macro_definition *
127130803Smarcelstandard_macro_lookup (const char *name, void *baton)
128130803Smarcel{
129130803Smarcel  struct macro_scope *ms = (struct macro_scope *) baton;
130130803Smarcel
131130803Smarcel  return macro_lookup_definition (ms->file, ms->line, name);
132130803Smarcel}
133