Fix off-by-one error in cplus name demangling.
[deliverable/binutils-gdb.git] / gdb / symtab.c
CommitLineData
bd5635a1 1/* Symbol table lookup for the GNU debugger, GDB.
997a978c 2 Copyright 1986, 1987, 1988, 1989, 1990, 1991 Free Software Foundation, Inc.
bd5635a1
RP
3
4This file is part of GDB.
5
997a978c 6This program is free software; you can redistribute it and/or modify
bd5635a1 7it under the terms of the GNU General Public License as published by
997a978c
JG
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
bd5635a1 10
997a978c 11This program is distributed in the hope that it will be useful,
bd5635a1
RP
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
997a978c
JG
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1
RP
19
20#include <stdio.h>
21#include "defs.h"
22#include "symtab.h"
bd5635a1
RP
23#include "gdbcore.h"
24#include "frame.h"
25#include "target.h"
26#include "value.h"
27#include "symfile.h"
28#include "gdbcmd.h"
5594d534 29#include "regex.h"
997a978c 30#include "language.h"
bd5635a1
RP
31
32#include <obstack.h>
33#include <assert.h>
34
35#include <sys/types.h>
36#include <fcntl.h>
37#include <string.h>
38#include <sys/stat.h>
39
bd5635a1
RP
40extern char *getenv ();
41
42extern char *cplus_demangle ();
bcccec8c 43extern char *cplus_mangle_opname ();
bd5635a1
RP
44extern struct value *value_of_this ();
45extern void break_command ();
46extern void select_source_symtab ();
47
48/* Functions this file defines */
49static int find_line_common ();
50struct partial_symtab *lookup_partial_symtab ();
51static struct partial_symbol *lookup_partial_symbol ();
b039ac3a
JK
52static struct partial_symbol *lookup_demangled_partial_symbol ();
53static struct symbol *lookup_demangled_block_symbol ();
bd5635a1 54
997a978c 55/* The single non-language-specific builtin type */
bd5635a1
RP
56struct type *builtin_type_error;
57
58/* Block in which the most recently searched-for symbol was found.
59 Might be better to make this a parameter to lookup_symbol and
60 value_of_this. */
61struct block *block_found;
62
63char no_symtab_msg[] = "No symbol table is loaded. Use the \"file\" command.";
64
65/* Check for a symtab of a specific name; first in symtabs, then in
66 psymtabs. *If* there is no '/' in the name, a match after a '/'
67 in the symtab filename will also work. */
68
69static struct symtab *
70lookup_symtab_1 (name)
71 char *name;
72{
73 register struct symtab *s;
74 register struct partial_symtab *ps;
75 register char *slash = strchr (name, '/');
76 register int len = strlen (name);
77
78 for (s = symtab_list; s; s = s->next)
79 if (!strcmp (name, s->filename))
80 return s;
81
82 for (ps = partial_symtab_list; ps; ps = ps->next)
83 if (!strcmp (name, ps->filename))
84 {
85 if (ps->readin)
eaa1ef1d 86 error ("Internal: readin pst for `%s' found when no symtab found.", name);
bd5635a1
RP
87 return PSYMTAB_TO_SYMTAB (ps);
88 }
89
90 if (!slash)
91 {
92 for (s = symtab_list; s; s = s->next)
93 {
94 int l = strlen (s->filename);
95
96 if (s->filename[l - len -1] == '/'
97 && !strcmp (s->filename + l - len, name))
98 return s;
99 }
100
101 for (ps = partial_symtab_list; ps; ps = ps->next)
102 {
103 int l = strlen (ps->filename);
104
105 if (ps->filename[l - len - 1] == '/'
106 && !strcmp (ps->filename + l - len, name))
107 {
108 if (ps->readin)
eaa1ef1d 109 error ("Internal: readin pst for `%s' found when no symtab found.", name);
bd5635a1
RP
110 return PSYMTAB_TO_SYMTAB (ps);
111 }
112 }
113 }
114 return 0;
115}
116
117/* Lookup the symbol table of a source file named NAME. Try a couple
118 of variations if the first lookup doesn't work. */
119
120struct symtab *
121lookup_symtab (name)
122 char *name;
123{
124 register struct symtab *s;
125 register char *copy;
126
127 s = lookup_symtab_1 (name);
128 if (s) return s;
129
130 /* If name not found as specified, see if adding ".c" helps. */
131
132 copy = (char *) alloca (strlen (name) + 3);
133 strcpy (copy, name);
134 strcat (copy, ".c");
135 s = lookup_symtab_1 (copy);
136 if (s) return s;
137
138 /* We didn't find anything; die. */
139 return 0;
140}
141
142/* Lookup the partial symbol table of a source file named NAME. This
143 only returns true on an exact match (ie. this semantics are
144 different from lookup_symtab. */
145
146struct partial_symtab *
147lookup_partial_symtab (name)
148char *name;
149{
150 register struct partial_symtab *s;
151
152 for (s = partial_symtab_list; s; s = s->next)
153 if (!strcmp (name, s->filename))
154 return s;
155
156 return 0;
157}
158\f
159/* Return a typename for a struct/union/enum type
160 without the tag qualifier. If the type has a NULL name,
161 NULL is returned. */
162char *
163type_name_no_tag (type)
164 register struct type *type;
165{
166 register char *name = TYPE_NAME (type);
58050209 167
bd5635a1
RP
168 if (name == 0)
169 return 0;
170
bd5635a1
RP
171 switch (TYPE_CODE (type))
172 {
173 case TYPE_CODE_STRUCT:
58050209
DHW
174 if(!strncmp(name,"struct ",7))
175 return name + 7;
176 else return name;
bd5635a1 177 case TYPE_CODE_UNION:
58050209 178 if(!strncmp(name,"union ",6))
bd5635a1 179 return name + 6;
58050209 180 else return name;
bd5635a1 181 case TYPE_CODE_ENUM:
58050209 182 if(!strncmp(name,"enum ",5))
bd5635a1 183 return name + 5;
58050209 184 else return name;
7d9884b9
JG
185 default:
186 return name;
bd5635a1 187 }
bd5635a1
RP
188}
189
190/* Added by Bryan Boreham, Kewill, Sun Sep 17 18:07:17 1989.
191
192 If this is a stubbed struct (i.e. declared as struct foo *), see if
193 we can find a full definition in some other file. If so, copy this
194 definition, so we can use it in future. If not, set a flag so we
c2536607
JG
195 don't waste too much time in future. (FIXME, this doesn't seem
196 to be happening...)
bd5635a1
RP
197
198 This used to be coded as a macro, but I don't think it is called
199 often enough to merit such treatment.
200*/
201
202struct complaint stub_noname_complaint =
203 {"stub type has NULL name", 0, 0};
204
205void
206check_stub_type(type)
207 struct type *type;
208{
209 if (TYPE_FLAGS(type) & TYPE_FLAG_STUB)
210 {
211 char* name= type_name_no_tag (type);
212 struct symbol *sym;
213 if (name == 0)
214 {
e1ce8aa5 215 complain (&stub_noname_complaint, 0);
bd5635a1
RP
216 return;
217 }
5594d534
JG
218 sym = lookup_symbol (name, 0, STRUCT_NAMESPACE, 0,
219 (struct symtab **)NULL);
220 if (sym)
bd5635a1
RP
221 bcopy (SYMBOL_TYPE(sym), type, sizeof (struct type));
222 }
223}
224
225/* Demangle a GDB method stub type. */
226char *
bcccec8c 227gdb_mangle_name (type, i, j)
bd5635a1 228 struct type *type;
bcccec8c 229 int i, j;
bd5635a1 230{
bcccec8c
PB
231 int mangled_name_len;
232 char *mangled_name;
233 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
234 struct fn_field *method = &f[j];
235 char *field_name = TYPE_FN_FIELDLIST_NAME (type, i);
bd5635a1 236
bcccec8c
PB
237 /* Need a new type prefix. */
238 char *strchr ();
239 char *const_prefix = method->is_const ? "C" : "";
240 char *volatile_prefix = method->is_volatile ? "V" : "";
241 char *newname = type_name_no_tag (type);
242 char buf[20];
243 int len = strlen (newname);
244
245 sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
246 mangled_name_len = (strlen (field_name)
247 + strlen (buf) + len
248 + strlen (TYPE_FN_FIELD_PHYSNAME (f, j))
249 + 1);
250
7d9884b9
JG
251 /* Only needed for GNU-mangled names. ANSI-mangled names
252 work with the normal mechanisms. */
bcccec8c
PB
253 if (OPNAME_PREFIX_P (field_name))
254 {
7d9884b9 255 char *opname = cplus_mangle_opname (field_name + 3, 0);
bcccec8c
PB
256 if (opname == NULL)
257 error ("No mangling for \"%s\"", field_name);
258 mangled_name_len += strlen (opname);
259 mangled_name = (char *)xmalloc (mangled_name_len);
260
261 strncpy (mangled_name, field_name, 3);
262 mangled_name[3] = '\0';
263 strcat (mangled_name, opname);
264 }
265 else
bd5635a1 266 {
bcccec8c
PB
267 mangled_name = (char *)xmalloc (mangled_name_len);
268 strcpy (mangled_name, TYPE_FN_FIELDLIST_NAME (type, i));
bd5635a1 269 }
bcccec8c
PB
270 strcat (mangled_name, buf);
271 strcat (mangled_name, newname);
272 strcat (mangled_name, TYPE_FN_FIELD_PHYSNAME (f, j));
273
274 return mangled_name;
bd5635a1
RP
275}
276
277/* Lookup a primitive type named NAME.
278 Return zero if NAME is not a primitive type.*/
279
280struct type *
281lookup_primitive_typename (name)
282 char *name;
283{
c2536607 284 struct type ** const *p;
997a978c
JG
285
286 for (p = current_language->la_builtin_type_vector; *p; p++)
287 if(!strcmp((**p)->name, name))
288 return **p;
289 return 0;
bd5635a1
RP
290}
291
292/* Lookup a typedef or primitive type named NAME,
293 visible in lexical block BLOCK.
294 If NOERR is nonzero, return zero if NAME is not suitably defined. */
295
296struct type *
297lookup_typename (name, block, noerr)
298 char *name;
299 struct block *block;
300 int noerr;
301{
302 register struct symbol *sym =
303 lookup_symbol (name, block, VAR_NAMESPACE, 0, (struct symtab **)NULL);
304 if (sym == 0 || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
305 {
306 struct type *tmp;
307 tmp = lookup_primitive_typename (name);
997a978c
JG
308 if(tmp)
309 return tmp;
310 else if (!tmp && noerr)
bd5635a1 311 return 0;
997a978c
JG
312 else
313 error ("No type named %s.", name);
bd5635a1
RP
314 }
315 return SYMBOL_TYPE (sym);
316}
317
318struct type *
319lookup_unsigned_typename (name)
320 char *name;
321{
997a978c
JG
322 char *uns = alloca (strlen(name) + 10);
323
324 strcpy (uns, "unsigned ");
325 strcpy (uns+9, name);
326 return lookup_typename (uns, (struct block *)0, 0);
bd5635a1
RP
327}
328
329/* Lookup a structure type named "struct NAME",
330 visible in lexical block BLOCK. */
331
332struct type *
333lookup_struct (name, block)
334 char *name;
335 struct block *block;
336{
337 register struct symbol *sym
338 = lookup_symbol (name, block, STRUCT_NAMESPACE, 0, (struct symtab **)NULL);
339
340 if (sym == 0)
341 error ("No struct type named %s.", name);
342 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
343 error ("This context has class, union or enum %s, not a struct.", name);
344 return SYMBOL_TYPE (sym);
345}
346
347/* Lookup a union type named "union NAME",
348 visible in lexical block BLOCK. */
349
350struct type *
351lookup_union (name, block)
352 char *name;
353 struct block *block;
354{
355 register struct symbol *sym
356 = lookup_symbol (name, block, STRUCT_NAMESPACE, 0, (struct symtab **)NULL);
357
358 if (sym == 0)
359 error ("No union type named %s.", name);
360 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_UNION)
361 error ("This context has class, struct or enum %s, not a union.", name);
362 return SYMBOL_TYPE (sym);
363}
364
365/* Lookup an enum type named "enum NAME",
366 visible in lexical block BLOCK. */
367
368struct type *
369lookup_enum (name, block)
370 char *name;
371 struct block *block;
372{
373 register struct symbol *sym
374 = lookup_symbol (name, block, STRUCT_NAMESPACE, 0, (struct symtab **)NULL);
375 if (sym == 0)
376 error ("No enum type named %s.", name);
377 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
378 error ("This context has class, struct or union %s, not an enum.", name);
379 return SYMBOL_TYPE (sym);
380}
381
58050209
DHW
382/* Lookup a template type named "template NAME<TYPE>",
383 visible in lexical block BLOCK. */
384
385struct type *
386lookup_template_type (name, type, block)
387 char *name;
388 struct type *type;
389 struct block *block;
390{
391 struct symbol *sym ;
392 char *nam = (char*) alloca(strlen(name) + strlen(type->name) + 4);
393 strcpy(nam, name);
394 strcat(nam, "<");
395 strcat(nam, type->name);
f1d77e90 396 strcat(nam, " >"); /* FIXME, extra space still introduced in gcc? */
58050209
DHW
397
398 sym = lookup_symbol (nam, block, VAR_NAMESPACE, 0, (struct symtab **)NULL);
399
400 if (sym == 0)
401 error ("No template type named %s.", name);
402 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
403 error ("This context has class, union or enum %s, not a struct.", name);
404 return SYMBOL_TYPE (sym);
405}
406
bd5635a1 407/* Given a type TYPE, lookup the type of the component of type named
d96b54ea
JK
408 NAME.
409 If NOERR is nonzero, return zero if NAME is not suitably defined. */
bd5635a1
RP
410
411struct type *
d96b54ea 412lookup_struct_elt_type (type, name, noerr)
bd5635a1
RP
413 struct type *type;
414 char *name;
d96b54ea 415 int noerr;
bd5635a1
RP
416{
417 int i;
418
f1d77e90 419 if ( TYPE_CODE (type) != TYPE_CODE_STRUCT
bd5635a1
RP
420 && TYPE_CODE (type) != TYPE_CODE_UNION)
421 {
422 target_terminal_ours ();
423 fflush (stdout);
424 fprintf (stderr, "Type ");
425 type_print (type, "", stderr, -1);
426 error (" is not a structure or union type.");
427 }
428
d96b54ea
JK
429 check_stub_type (type);
430
bd5635a1 431 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
d96b54ea
JK
432 {
433 char *t_field_name = TYPE_FIELD_NAME (type, i);
bd5635a1 434
d96b54ea
JK
435 if (t_field_name && !strcmp (t_field_name, name))
436 return TYPE_FIELD_TYPE (type, i);
437 }
438 /* OK, it's not in this class. Recursively check the baseclasses. */
439 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
440 {
441 struct type *t = lookup_struct_elt_type (TYPE_BASECLASS (type, i),
442 name, 0);
443 if (t != NULL)
444 return t;
445 }
446
447 if (noerr)
448 return NULL;
449
bd5635a1
RP
450 target_terminal_ours ();
451 fflush (stdout);
452 fprintf (stderr, "Type ");
453 type_print (type, "", stderr, -1);
454 fprintf (stderr, " has no component named ");
455 fputs_filtered (name, stderr);
456 error (".");
457 return (struct type *)-1; /* For lint */
458}
459
460/* Given a type TYPE, return a type of pointers to that type.
7d9884b9 461 May need to construct such a type if this is the first use. */
bd5635a1
RP
462
463struct type *
464lookup_pointer_type (type)
465 struct type *type;
466{
467 register struct type *ptype = TYPE_POINTER_TYPE (type);
7d9884b9 468 if (ptype) return ptype;
bd5635a1
RP
469
470 /* This is the first time anyone wanted a pointer to a TYPE. */
471 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
472 ptype = (struct type *) xmalloc (sizeof (struct type));
473 else
474 ptype = (struct type *) obstack_alloc (symbol_obstack,
475 sizeof (struct type));
476
477 bzero (ptype, sizeof (struct type));
bd5635a1
RP
478 TYPE_TARGET_TYPE (ptype) = type;
479 TYPE_POINTER_TYPE (type) = ptype;
480 /* New type is permanent if type pointed to is permanent. */
481 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
482 TYPE_FLAGS (ptype) |= TYPE_FLAG_PERM;
483 /* We assume the machine has only one representation for pointers! */
dc9894c8
JG
484 /* FIXME: This confuses host<->target data representations, and is a
485 poor assumption besides. */
bd5635a1
RP
486 TYPE_LENGTH (ptype) = sizeof (char *);
487 TYPE_CODE (ptype) = TYPE_CODE_PTR;
488 return ptype;
489}
490
491struct type *
492lookup_reference_type (type)
493 struct type *type;
494{
495 register struct type *rtype = TYPE_REFERENCE_TYPE (type);
7d9884b9 496 if (rtype) return rtype;
bd5635a1
RP
497
498 /* This is the first time anyone wanted a pointer to a TYPE. */
499 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
500 rtype = (struct type *) xmalloc (sizeof (struct type));
501 else
502 rtype = (struct type *) obstack_alloc (symbol_obstack,
503 sizeof (struct type));
504
505 bzero (rtype, sizeof (struct type));
bd5635a1
RP
506 TYPE_TARGET_TYPE (rtype) = type;
507 TYPE_REFERENCE_TYPE (type) = rtype;
508 /* New type is permanent if type pointed to is permanent. */
509 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
510 TYPE_FLAGS (rtype) |= TYPE_FLAG_PERM;
511 /* We assume the machine has only one representation for pointers! */
512 TYPE_LENGTH (rtype) = sizeof (char *);
513 TYPE_CODE (rtype) = TYPE_CODE_REF;
514 return rtype;
515}
516
517
518/* Implement direct support for MEMBER_TYPE in GNU C++.
519 May need to construct such a type if this is the first use.
520 The TYPE is the type of the member. The DOMAIN is the type
521 of the aggregate that the member belongs to. */
522
523struct type *
524lookup_member_type (type, domain)
525 struct type *type, *domain;
526{
f1d77e90 527 register struct type *mtype;
bd5635a1 528
f1d77e90
JG
529 mtype = (struct type *) obstack_alloc (symbol_obstack,
530 sizeof (struct type));
531 smash_to_member_type (mtype, domain, type);
bd5635a1
RP
532 return mtype;
533}
534
f1d77e90
JG
535/* Allocate a stub method whose return type is TYPE.
536 This apparently happens for speed of symbol reading, since parsing
537 out the arguments to the method is cpu-intensive, the way we are doing
538 it. So, we will fill in arguments later.
539 This always returns a fresh type. */
540
d96b54ea
JK
541struct type *
542allocate_stub_method (type)
543 struct type *type;
544{
f1d77e90
JG
545 struct type *mtype = (struct type *) obstack_alloc (symbol_obstack,
546 sizeof (struct type));
d96b54ea 547 bzero (mtype, sizeof (struct type));
d96b54ea 548 TYPE_TARGET_TYPE (mtype) = type;
f1d77e90
JG
549 /* _DOMAIN_TYPE (mtype) = unknown yet */
550 /* _ARG_TYPES (mtype) = unknown yet */
d96b54ea
JK
551 TYPE_FLAGS (mtype) = TYPE_FLAG_STUB;
552 TYPE_CODE (mtype) = TYPE_CODE_METHOD;
553 TYPE_LENGTH (mtype) = 1;
554 return mtype;
555}
556
f1d77e90 557/* Ugly hack to convert method stubs into method types.
d96b54ea 558
f1d77e90
JG
559 He ain't kiddin'. This demangles the name of the method into a string
560 including argument types, parses out each argument type, generates
561 a string casting a zero to that type, evaluates the string, and stuffs
562 the resulting type into an argtype vector!!! Then it knows the type
563 of the whole function (including argument types for overloading),
564 which info used to be in the stab's but was removed to hack back
565 the space required for them. */
566void
567check_stub_method (type, i, j)
568 struct type *type;
569 int i, j;
bd5635a1 570{
f1d77e90
JG
571 extern char *gdb_mangle_name (), *strchr ();
572 struct fn_field *f;
573 char *mangled_name = gdb_mangle_name (type, i, j);
574 char *demangled_name = cplus_demangle (mangled_name, 0);
575 char *argtypetext, *p;
576 int depth = 0, argcount = 1;
577 struct type **argtypes;
578 struct type *mtype;
579
580 /* Now, read in the parameters that define this type. */
581 argtypetext = strchr (demangled_name, '(') + 1;
582 p = argtypetext;
583 while (*p)
bd5635a1 584 {
f1d77e90
JG
585 if (*p == '(')
586 depth += 1;
587 else if (*p == ')')
588 depth -= 1;
589 else if (*p == ',' && depth == 0)
590 argcount += 1;
591
592 p += 1;
593 }
a0a6174a
JG
594 /* We need two more slots: one for the THIS pointer, and one for the
595 NULL [...] or void [end of arglist]. */
f1d77e90 596 argtypes = (struct type **) obstack_alloc (symbol_obstack,
a0a6174a 597 (argcount+2) * sizeof (struct type *));
f1d77e90
JG
598 p = argtypetext;
599 argtypes[0] = lookup_pointer_type (type);
600 argcount = 1;
601
602 if (*p != ')') /* () means no args, skip while */
603 {
604 depth = 0;
605 while (*p)
bd5635a1 606 {
f1d77e90 607 if (depth <= 0 && (*p == ',' || *p == ')'))
bd5635a1 608 {
f1d77e90
JG
609 argtypes[argcount] =
610 parse_and_eval_type (argtypetext, p - argtypetext);
611 argcount += 1;
612 argtypetext = p + 1;
bd5635a1 613 }
bd5635a1 614
f1d77e90
JG
615 if (*p == '(')
616 depth += 1;
617 else if (*p == ')')
618 depth -= 1;
bd5635a1 619
f1d77e90
JG
620 p += 1;
621 }
bd5635a1
RP
622 }
623
f1d77e90
JG
624 if (p[-2] != '.') /* ... */
625 argtypes[argcount] = builtin_type_void; /* Ellist terminator */
bd5635a1 626 else
f1d77e90 627 argtypes[argcount] = NULL; /* List terminator */
bd5635a1 628
f1d77e90 629 free (demangled_name);
bd5635a1 630
f1d77e90
JG
631 f = TYPE_FN_FIELDLIST1 (type, i);
632 TYPE_FN_FIELD_PHYSNAME (f, j) = mangled_name;
bd5635a1 633
f1d77e90
JG
634 /* Now update the old "stub" type into a real type. */
635 mtype = TYPE_FN_FIELD_TYPE (f, j);
636 TYPE_DOMAIN_TYPE (mtype) = type;
637 TYPE_ARG_TYPES (mtype) = argtypes;
638 TYPE_FLAGS (mtype) &= ~TYPE_FLAG_STUB;
bd5635a1 639}
bd5635a1
RP
640
641/* Given a type TYPE, return a type of functions that return that type.
642 May need to construct such a type if this is the first use. */
643
644struct type *
645lookup_function_type (type)
646 struct type *type;
647{
648 register struct type *ptype = TYPE_FUNCTION_TYPE (type);
649 if (ptype) return ptype;
650
651 /* This is the first time anyone wanted a function returning a TYPE. */
652 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
653 ptype = (struct type *) xmalloc (sizeof (struct type));
654 else
655 ptype = (struct type *) obstack_alloc (symbol_obstack,
656 sizeof (struct type));
657
658 bzero (ptype, sizeof (struct type));
659 TYPE_TARGET_TYPE (ptype) = type;
660 TYPE_FUNCTION_TYPE (type) = ptype;
661 /* New type is permanent if type returned is permanent. */
662 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
663 TYPE_FLAGS (ptype) |= TYPE_FLAG_PERM;
664 TYPE_LENGTH (ptype) = 1;
665 TYPE_CODE (ptype) = TYPE_CODE_FUNC;
666 TYPE_NFIELDS (ptype) = 0;
667 return ptype;
668}
669\f
670/* Create an array type. Elements will be of type TYPE, and there will
671 be NUM of them.
672
673 Eventually this should be extended to take two more arguments which
674 specify the bounds of the array and the type of the index.
675 It should also be changed to be a "lookup" function, with the
676 appropriate data structures added to the type field.
677 Then read array type should call here. */
678
679struct type *
680create_array_type (element_type, number)
681 struct type *element_type;
682 int number;
683{
684 struct type *result_type = (struct type *)
685 obstack_alloc (symbol_obstack, sizeof (struct type));
997a978c 686 struct type *range_type;
bd5635a1
RP
687
688 bzero (result_type, sizeof (struct type));
689
690 TYPE_CODE (result_type) = TYPE_CODE_ARRAY;
691 TYPE_TARGET_TYPE (result_type) = element_type;
692 TYPE_LENGTH (result_type) = number * TYPE_LENGTH (element_type);
693 TYPE_NFIELDS (result_type) = 1;
694 TYPE_FIELDS (result_type) =
695 (struct field *) obstack_alloc (symbol_obstack, sizeof (struct field));
997a978c
JG
696
697 {
698 /* Create range type. */
699 range_type = (struct type *) obstack_alloc (symbol_obstack,
700 sizeof (struct type));
701 TYPE_CODE (range_type) = TYPE_CODE_RANGE;
702 TYPE_TARGET_TYPE (range_type) = builtin_type_int; /* FIXME */
703
704 /* This should never be needed. */
705 TYPE_LENGTH (range_type) = sizeof (int);
706
707 TYPE_NFIELDS (range_type) = 2;
708 TYPE_FIELDS (range_type) =
709 (struct field *) obstack_alloc (symbol_obstack,
710 2 * sizeof (struct field));
711 TYPE_FIELD_BITPOS (range_type, 0) = 0; /* FIXME */
712 TYPE_FIELD_BITPOS (range_type, 1) = number-1; /* FIXME */
713 TYPE_FIELD_TYPE (range_type, 0) = builtin_type_int; /* FIXME */
714 TYPE_FIELD_TYPE (range_type, 1) = builtin_type_int; /* FIXME */
715 }
716 TYPE_FIELD_TYPE(result_type,0)=range_type;
bd5635a1
RP
717 TYPE_VPTR_FIELDNO (result_type) = -1;
718
719 return result_type;
720}
721
722\f
f1d77e90
JG
723/* Smash TYPE to be a type of members of DOMAIN with type TO_TYPE.
724 A MEMBER is a wierd thing -- it amounts to a typed offset into
725 a struct, e.g. "an int at offset 8". A MEMBER TYPE doesn't
726 include the offset (that's the value of the MEMBER itself), but does
727 include the structure type into which it points (for some reason). */
bd5635a1
RP
728
729void
730smash_to_member_type (type, domain, to_type)
731 struct type *type, *domain, *to_type;
732{
733 bzero (type, sizeof (struct type));
734 TYPE_TARGET_TYPE (type) = to_type;
735 TYPE_DOMAIN_TYPE (type) = domain;
f1d77e90 736 TYPE_LENGTH (type) = 1; /* In practice, this is never needed. */
bd5635a1 737 TYPE_CODE (type) = TYPE_CODE_MEMBER;
bd5635a1
RP
738}
739
f1d77e90
JG
740/* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE.
741 METHOD just means `function that gets an extra "this" argument'. */
bd5635a1
RP
742
743void
744smash_to_method_type (type, domain, to_type, args)
745 struct type *type, *domain, *to_type, **args;
746{
747 bzero (type, sizeof (struct type));
748 TYPE_TARGET_TYPE (type) = to_type;
749 TYPE_DOMAIN_TYPE (type) = domain;
750 TYPE_ARG_TYPES (type) = args;
f1d77e90 751 TYPE_LENGTH (type) = 1; /* In practice, this is never needed. */
bd5635a1 752 TYPE_CODE (type) = TYPE_CODE_METHOD;
bd5635a1
RP
753}
754\f
755/* Find which partial symtab on the partial_symtab_list contains
756 PC. Return 0 if none. */
757
758struct partial_symtab *
759find_pc_psymtab (pc)
760 register CORE_ADDR pc;
761{
762 register struct partial_symtab *ps;
763
764 for (ps = partial_symtab_list; ps; ps = ps->next)
765 if (pc >= ps->textlow && pc < ps->texthigh)
766 return ps;
767
768 return 0;
769}
770
771/* Find which partial symbol within a psymtab contains PC. Return 0
772 if none. Check all psymtabs if PSYMTAB is 0. */
773struct partial_symbol *
774find_pc_psymbol (psymtab, pc)
775 struct partial_symtab *psymtab;
776 CORE_ADDR pc;
777{
778 struct partial_symbol *best, *p;
779 CORE_ADDR best_pc;
780
781 if (!psymtab)
782 psymtab = find_pc_psymtab (pc);
783 if (!psymtab)
784 return 0;
785
786 best_pc = psymtab->textlow - 1;
787
788 for (p = static_psymbols.list + psymtab->statics_offset;
789 (p - (static_psymbols.list + psymtab->statics_offset)
790 < psymtab->n_static_syms);
791 p++)
792 if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE
793 && SYMBOL_CLASS (p) == LOC_BLOCK
794 && pc >= SYMBOL_VALUE_ADDRESS (p)
795 && SYMBOL_VALUE_ADDRESS (p) > best_pc)
796 {
797 best_pc = SYMBOL_VALUE_ADDRESS (p);
798 best = p;
799 }
800 if (best_pc == psymtab->textlow - 1)
801 return 0;
802 return best;
803}
804
805\f
806/* Find the definition for a specified symbol name NAME
807 in namespace NAMESPACE, visible from lexical block BLOCK.
808 Returns the struct symbol pointer, or zero if no symbol is found.
809 If SYMTAB is non-NULL, store the symbol table in which the
810 symbol was found there, or NULL if not found.
811 C++: if IS_A_FIELD_OF_THIS is nonzero on entry, check to see if
812 NAME is a field of the current implied argument `this'. If so set
813 *IS_A_FIELD_OF_THIS to 1, otherwise set it to zero.
814 BLOCK_FOUND is set to the block in which NAME is found (in the case of
815 a field of `this', value_of_this sets BLOCK_FOUND to the proper value.) */
816
817struct symbol *
818lookup_symbol (name, block, namespace, is_a_field_of_this, symtab)
819 char *name;
820 register struct block *block;
821 enum namespace namespace;
822 int *is_a_field_of_this;
823 struct symtab **symtab;
824{
825 register struct symbol *sym;
826 register struct symtab *s;
827 register struct partial_symtab *ps;
828 struct blockvector *bv;
829
830 /* Search specified block and its superiors. */
831
832 while (block != 0)
833 {
834 sym = lookup_block_symbol (block, name, namespace);
835 if (sym)
836 {
837 block_found = block;
838 if (symtab != NULL)
839 {
840 /* Search the list of symtabs for one which contains the
841 address of the start of this block. */
842 struct block *b;
843 for (s = symtab_list; s; s = s->next)
844 {
845 bv = BLOCKVECTOR (s);
3ba6a043 846 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
bd5635a1
RP
847 if (BLOCK_START (b) <= BLOCK_START (block)
848 && BLOCK_END (b) > BLOCK_START (block))
849 break;
850 }
851 *symtab = s;
852 }
853
854 return sym;
855 }
856 block = BLOCK_SUPERBLOCK (block);
857 }
858
b039ac3a
JK
859 /* But that doesn't do any demangling for the STATIC_BLOCK.
860 I'm not sure whether demangling is needed in the case of
861 nested function in inner blocks; if so this needs to be changed.
862
863 Don't need to mess with the psymtabs; if we have a block,
864 that file is read in. If we don't, then we deal later with
865 all the psymtab stuff that needs checking. */
866 if (namespace == VAR_NAMESPACE && block != NULL)
867 {
868 struct block *b;
869 /* Find the right symtab. */
870 for (s = symtab_list; s; s = s->next)
871 {
872 bv = BLOCKVECTOR (s);
873 b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
874 if (BLOCK_START (b) <= BLOCK_START (block)
875 && BLOCK_END (b) > BLOCK_START (block))
876 {
877 sym = lookup_demangled_block_symbol (b, name);
878 if (sym)
879 {
880 block_found = b;
881 if (symtab != NULL)
882 *symtab = s;
883 return sym;
884 }
885 }
886 }
887 }
888
889
bd5635a1
RP
890 /* C++: If requested to do so by the caller,
891 check to see if NAME is a field of `this'. */
892 if (is_a_field_of_this)
893 {
894 struct value *v = value_of_this (0);
895
896 *is_a_field_of_this = 0;
897 if (v && check_field (v, name))
898 {
899 *is_a_field_of_this = 1;
900 if (symtab != NULL)
901 *symtab = NULL;
902 return 0;
903 }
904 }
905
906 /* Now search all global blocks. Do the symtab's first, then
907 check the psymtab's */
908
909 for (s = symtab_list; s; s = s->next)
910 {
911 bv = BLOCKVECTOR (s);
3ba6a043 912 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
bd5635a1
RP
913 sym = lookup_block_symbol (block, name, namespace);
914 if (sym)
915 {
916 block_found = block;
917 if (symtab != NULL)
918 *symtab = s;
919 return sym;
920 }
921 }
922
923 /* Check for the possibility of the symbol being a global function
924 that is stored on the misc function vector. Eventually, all
925 global symbols might be resolved in this way. */
926
927 if (namespace == VAR_NAMESPACE)
928 {
929 int ind = lookup_misc_func (name);
930
931 /* Look for a mangled C++ name for NAME. */
932 if (ind == -1)
933 {
934 int name_len = strlen (name);
935
936 for (ind = misc_function_count; --ind >= 0; )
937 /* Assume orginal name is prefix of mangled name. */
938 if (!strncmp (misc_function_vector[ind].name, name, name_len))
939 {
940 char *demangled =
941 cplus_demangle(misc_function_vector[ind].name, -1);
942 if (demangled != NULL)
943 {
944 int cond = strcmp (demangled, name);
945 free (demangled);
946 if (!cond)
947 break;
948 }
949 }
950 /* Loop terminates on no match with ind == -1. */
951 }
952
953 if (ind != -1)
954 {
955 s = find_pc_symtab (misc_function_vector[ind].address);
997a978c
JG
956 /* If S is zero, there are no debug symbols for this file.
957 Skip this stuff and check for matching static symbols below. */
bd5635a1
RP
958 if (s)
959 {
960 bv = BLOCKVECTOR (s);
3ba6a043 961 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
bd5635a1
RP
962 sym = lookup_block_symbol (block, misc_function_vector[ind].name,
963 namespace);
964 /* sym == 0 if symbol was found in the misc_function_vector
965 but not in the symtab.
966 Return 0 to use the misc_function definition of "foo_".
967
968 This happens for Fortran "foo_" symbols,
969 which are "foo" in the symtab.
970
971 This can also happen if "asm" is used to make a
972 regular symbol but not a debugging symbol, e.g.
973 asm(".globl _main");
974 asm("_main:");
975 */
976
977 if (symtab != NULL)
978 *symtab = s;
979 return sym;
980 }
981 }
982 }
983
984 for (ps = partial_symtab_list; ps; ps = ps->next)
985 if (!ps->readin && lookup_partial_symbol (ps, name, 1, namespace))
986 {
987 s = PSYMTAB_TO_SYMTAB(ps);
988 bv = BLOCKVECTOR (s);
3ba6a043 989 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
bd5635a1
RP
990 sym = lookup_block_symbol (block, name, namespace);
991 if (!sym)
eaa1ef1d 992 error ("Internal: global symbol `%s' found in psymtab but not in symtab", name);
bd5635a1
RP
993 if (symtab != NULL)
994 *symtab = s;
995 return sym;
996 }
997
998 /* Now search all per-file blocks.
999 Not strictly correct, but more useful than an error.
1000 Do the symtabs first, then check the psymtabs */
1001
1002 for (s = symtab_list; s; s = s->next)
1003 {
1004 bv = BLOCKVECTOR (s);
3ba6a043 1005 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
bd5635a1
RP
1006 sym = lookup_block_symbol (block, name, namespace);
1007 if (sym)
1008 {
1009 block_found = block;
1010 if (symtab != NULL)
1011 *symtab = s;
1012 return sym;
1013 }
1014 }
1015
1016 for (ps = partial_symtab_list; ps; ps = ps->next)
1017 if (!ps->readin && lookup_partial_symbol (ps, name, 0, namespace))
1018 {
1019 s = PSYMTAB_TO_SYMTAB(ps);
1020 bv = BLOCKVECTOR (s);
3ba6a043 1021 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
bd5635a1
RP
1022 sym = lookup_block_symbol (block, name, namespace);
1023 if (!sym)
eaa1ef1d 1024 error ("Internal: static symbol `%s' found in psymtab but not in symtab", name);
bd5635a1
RP
1025 if (symtab != NULL)
1026 *symtab = s;
1027 return sym;
1028 }
1029
b039ac3a
JK
1030 /* Now search all per-file blocks for static mangled symbols.
1031 Do the symtabs first, then check the psymtabs. */
1032
1033 if (namespace == VAR_NAMESPACE)
1034 {
1035 for (s = symtab_list; s; s = s->next)
1036 {
1037 bv = BLOCKVECTOR (s);
1038 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1039 sym = lookup_demangled_block_symbol (block, name);
1040 if (sym)
1041 {
1042 block_found = block;
1043 if (symtab != NULL)
1044 *symtab = s;
1045 return sym;
1046 }
1047 }
1048
1049 for (ps = partial_symtab_list; ps; ps = ps->next)
1050 if (!ps->readin && lookup_demangled_partial_symbol (ps, name))
1051 {
1052 s = PSYMTAB_TO_SYMTAB(ps);
1053 bv = BLOCKVECTOR (s);
1054 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1055 sym = lookup_demangled_block_symbol (block, name);
1056 if (!sym)
eaa1ef1d 1057 error ("Internal: mangled static symbol `%s' found in psymtab but not in symtab", name);
b039ac3a
JK
1058 if (symtab != NULL)
1059 *symtab = s;
1060 return sym;
1061 }
1062 }
1063
bd5635a1
RP
1064 if (symtab != NULL)
1065 *symtab = NULL;
1066 return 0;
1067}
1068
b039ac3a
JK
1069/* Look for a static demangled symbol in block BLOCK. */
1070
1071static struct symbol *
1072lookup_demangled_block_symbol (block, name)
1073 register struct block *block;
1074 char *name;
1075{
1076 register int bot, top, inc;
1077 register struct symbol *sym;
1078
1079 bot = 0;
1080 top = BLOCK_NSYMS (block);
1081 inc = name[0];
1082
1083 while (bot < top)
1084 {
1085 sym = BLOCK_SYM (block, bot);
1086 if (SYMBOL_NAME (sym)[0] == inc
1087 && SYMBOL_NAMESPACE (sym) == VAR_NAMESPACE)
1088 {
1089 char *demangled = cplus_demangle(SYMBOL_NAME (sym), -1);
1090 if (demangled != NULL)
1091 {
1092 int cond = strcmp (demangled, name);
1093 free (demangled);
1094 if (!cond)
1095 return sym;
1096 }
1097 }
1098 bot++;
1099 }
1100
1101 return 0;
1102}
1103
1104/* Look, in partial_symtab PST, for static mangled symbol NAME. */
1105
1106static struct partial_symbol *
1107lookup_demangled_partial_symbol (pst, name)
1108 struct partial_symtab *pst;
1109 char *name;
1110{
1111 struct partial_symbol *start, *psym;
1112 int length = pst->n_static_syms;
1113 register int inc = name[0];
1114
1115 if (!length)
1116 return (struct partial_symbol *) 0;
1117
1118 start = static_psymbols.list + pst->statics_offset;
1119 for (psym = start; psym < start + length; psym++)
1120 {
1121 if (SYMBOL_NAME (psym)[0] == inc
1122 && SYMBOL_NAMESPACE (psym) == VAR_NAMESPACE)
1123 {
1124 char *demangled = cplus_demangle(SYMBOL_NAME (psym), -1);
1125 if (demangled != NULL)
1126 {
1127 int cond = strcmp (demangled, name);
1128 free (demangled);
1129 if (!cond)
1130 return psym;
1131 }
1132 }
1133 }
1134
1135 return (struct partial_symbol *) 0;
1136}
1137
bd5635a1
RP
1138/* Look, in partial_symtab PST, for symbol NAME. Check the global
1139 symbols if GLOBAL, the static symbols if not */
1140
1141static struct partial_symbol *
1142lookup_partial_symbol (pst, name, global, namespace)
1143 struct partial_symtab *pst;
1144 char *name;
1145 int global;
1146 enum namespace namespace;
1147{
1148 struct partial_symbol *start, *psym;
1149 int length = (global ? pst->n_global_syms : pst->n_static_syms);
1150
1151 if (!length)
1152 return (struct partial_symbol *) 0;
1153
1154 start = (global ?
1155 global_psymbols.list + pst->globals_offset :
1156 static_psymbols.list + pst->statics_offset );
1157
1158 if (global) /* This means we can use a binary */
1159 /* search. */
1160 {
1161 struct partial_symbol *top, *bottom, *center;
1162
1163 /* Binary search. This search is guaranteed to end with center
1164 pointing at the earliest partial symbol with the correct
1165 name. At that point *all* partial symbols with that name
1166 will be checked against the correct namespace. */
1167 bottom = start;
1168 top = start + length - 1;
1169 while (top > bottom)
1170 {
1171 center = bottom + (top - bottom) / 2;
1172
1173 assert (center < top);
1174
1175 if (strcmp (SYMBOL_NAME (center), name) >= 0)
1176 top = center;
1177 else
1178 bottom = center + 1;
1179 }
1180 assert (top == bottom);
1181
1182 while (!strcmp (SYMBOL_NAME (top), name))
1183 {
1184 if (SYMBOL_NAMESPACE (top) == namespace)
1185 return top;
1186 top ++;
1187 }
1188 }
1189 else
1190 {
1191 /* Can't use a binary search */
1192 for (psym = start; psym < start + length; psym++)
1193 if (namespace == SYMBOL_NAMESPACE (psym)
1194 && !strcmp (name, SYMBOL_NAME (psym)))
1195 return psym;
1196 }
1197
1198 return (struct partial_symbol *) 0;
1199}
1200
0e2a896c
PB
1201/* Find the psymtab containing main(). */
1202
1203struct partial_symtab *
1204find_main_psymtab ()
1205{
1206 register struct partial_symtab *pst;
1207 for (pst = partial_symtab_list; pst; pst = pst->next)
1208 if (lookup_partial_symbol (pst, "main", 1, VAR_NAMESPACE))
1209 return pst;
1210 return NULL;
1211}
1212
bd5635a1
RP
1213/* Look for a symbol in block BLOCK. */
1214
1215struct symbol *
1216lookup_block_symbol (block, name, namespace)
1217 register struct block *block;
1218 char *name;
1219 enum namespace namespace;
1220{
1221 register int bot, top, inc;
1222 register struct symbol *sym, *parameter_sym;
1223
1224 top = BLOCK_NSYMS (block);
1225 bot = 0;
1226
1227 /* If the blocks's symbols were sorted, start with a binary search. */
1228
1229 if (BLOCK_SHOULD_SORT (block))
1230 {
1231 /* First, advance BOT to not far before
1232 the first symbol whose name is NAME. */
1233
1234 while (1)
1235 {
1236 inc = (top - bot + 1);
1237 /* No need to keep binary searching for the last few bits worth. */
1238 if (inc < 4)
1239 break;
1240 inc = (inc >> 1) + bot;
1241 sym = BLOCK_SYM (block, inc);
1242 if (SYMBOL_NAME (sym)[0] < name[0])
1243 bot = inc;
1244 else if (SYMBOL_NAME (sym)[0] > name[0])
1245 top = inc;
1246 else if (strcmp (SYMBOL_NAME (sym), name) < 0)
1247 bot = inc;
1248 else
1249 top = inc;
1250 }
1251
1252 /* Now scan forward until we run out of symbols,
1253 find one whose name is greater than NAME,
1254 or find one we want.
1255 If there is more than one symbol with the right name and namespace,
1256 we return the first one. dbxread.c is careful to make sure
1257 that if one is a register then it comes first. */
1258
1259 top = BLOCK_NSYMS (block);
1260 while (bot < top)
1261 {
1262 sym = BLOCK_SYM (block, bot);
1263 inc = SYMBOL_NAME (sym)[0] - name[0];
1264 if (inc == 0)
1265 inc = strcmp (SYMBOL_NAME (sym), name);
1266 if (inc == 0 && SYMBOL_NAMESPACE (sym) == namespace)
1267 return sym;
1268 if (inc > 0)
1269 return 0;
1270 bot++;
1271 }
1272 return 0;
1273 }
1274
1275 /* Here if block isn't sorted.
1276 This loop is equivalent to the loop above,
1277 but hacked greatly for speed.
1278
1279 Note that parameter symbols do not always show up last in the
1280 list; this loop makes sure to take anything else other than
1281 parameter symbols first; it only uses parameter symbols as a
1282 last resort. Note that this only takes up extra computation
1283 time on a match. */
1284
1285 parameter_sym = (struct symbol *) 0;
1286 top = BLOCK_NSYMS (block);
1287 inc = name[0];
1288 while (bot < top)
1289 {
1290 sym = BLOCK_SYM (block, bot);
1291 if (SYMBOL_NAME (sym)[0] == inc
1292 && !strcmp (SYMBOL_NAME (sym), name)
1293 && SYMBOL_NAMESPACE (sym) == namespace)
1294 {
1295 if (SYMBOL_CLASS (sym) == LOC_ARG
1296 || SYMBOL_CLASS (sym) == LOC_LOCAL_ARG
1297 || SYMBOL_CLASS (sym) == LOC_REF_ARG
1298 || SYMBOL_CLASS (sym) == LOC_REGPARM)
1299 parameter_sym = sym;
1300 else
1301 return sym;
1302 }
1303 bot++;
1304 }
1305 return parameter_sym; /* Will be 0 if not found. */
1306}
1307\f
1308/* Return the symbol for the function which contains a specified
1309 lexical block, described by a struct block BL. */
1310
1311struct symbol *
1312block_function (bl)
1313 struct block *bl;
1314{
1315 while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
1316 bl = BLOCK_SUPERBLOCK (bl);
1317
1318 return BLOCK_FUNCTION (bl);
1319}
1320
1321/* Subroutine of find_pc_line */
1322
1323struct symtab *
1324find_pc_symtab (pc)
1325 register CORE_ADDR pc;
1326{
1327 register struct block *b;
1328 struct blockvector *bv;
1329 register struct symtab *s;
1330 register struct partial_symtab *ps;
1331
1332 /* Search all symtabs for one whose file contains our pc */
1333
1334 for (s = symtab_list; s; s = s->next)
1335 {
1336 bv = BLOCKVECTOR (s);
3ba6a043 1337 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
bd5635a1
RP
1338 if (BLOCK_START (b) <= pc
1339 && BLOCK_END (b) > pc)
1340 break;
1341 }
1342
1343 if (!s)
1344 {
1345 ps = find_pc_psymtab (pc);
1346 if (ps && ps->readin)
997a978c 1347 printf_filtered (
eaa1ef1d 1348 "(Internal error: pc 0x%x in read in psymtab, but not in symtab.)\n", pc);
bd5635a1
RP
1349
1350 if (ps)
1351 s = PSYMTAB_TO_SYMTAB (ps);
1352 }
1353
1354 return s;
1355}
1356
1357/* Find the source file and line number for a given PC value.
1358 Return a structure containing a symtab pointer, a line number,
1359 and a pc range for the entire source line.
1360 The value's .pc field is NOT the specified pc.
1361 NOTCURRENT nonzero means, if specified pc is on a line boundary,
1362 use the line that ends there. Otherwise, in that case, the line
1363 that begins there is used. */
1364
1365struct symtab_and_line
1366find_pc_line (pc, notcurrent)
1367 CORE_ADDR pc;
1368 int notcurrent;
1369{
1370 struct symtab *s;
1371 register struct linetable *l;
1372 register int len;
1373 register int i;
1374 register struct linetable_entry *item;
1375 struct symtab_and_line val;
1376 struct blockvector *bv;
1377
1378 /* Info on best line seen so far, and where it starts, and its file. */
1379
1380 int best_line = 0;
1381 CORE_ADDR best_pc = 0;
1382 CORE_ADDR best_end = 0;
1383 struct symtab *best_symtab = 0;
1384
1385 /* Store here the first line number
1386 of a file which contains the line at the smallest pc after PC.
1387 If we don't find a line whose range contains PC,
1388 we will use a line one less than this,
1389 with a range from the start of that file to the first line's pc. */
1390 int alt_line = 0;
1391 CORE_ADDR alt_pc = 0;
1392 struct symtab *alt_symtab = 0;
1393
1394 /* Info on best line seen in this file. */
1395
1396 int prev_line;
1397 CORE_ADDR prev_pc;
1398
1399 /* Info on first line of this file. */
1400
1401 int first_line;
1402 CORE_ADDR first_pc;
1403
1404 /* If this pc is not from the current frame,
1405 it is the address of the end of a call instruction.
1406 Quite likely that is the start of the following statement.
1407 But what we want is the statement containing the instruction.
1408 Fudge the pc to make sure we get that. */
1409
1410 if (notcurrent) pc -= 1;
1411
1412 s = find_pc_symtab (pc);
1413 if (s == 0)
1414 {
1415 val.symtab = 0;
1416 val.line = 0;
1417 val.pc = pc;
1418 val.end = 0;
1419 return val;
1420 }
1421
1422 bv = BLOCKVECTOR (s);
1423
1424 /* Look at all the symtabs that share this blockvector.
1425 They all have the same apriori range, that we found was right;
1426 but they have different line tables. */
1427
1428 for (; s && BLOCKVECTOR (s) == bv; s = s->next)
1429 {
1430 /* Find the best line in this symtab. */
1431 l = LINETABLE (s);
4137c5fc
JG
1432 if (!l)
1433 continue;
bd5635a1
RP
1434 len = l->nitems;
1435 prev_line = -1;
1436 first_line = -1;
1437 for (i = 0; i < len; i++)
1438 {
1439 item = &(l->item[i]);
1440
1441 if (first_line < 0)
1442 {
1443 first_line = item->line;
1444 first_pc = item->pc;
1445 }
1446 /* Return the last line that did not start after PC. */
1447 if (pc >= item->pc)
1448 {
1449 prev_line = item->line;
1450 prev_pc = item->pc;
1451 }
1452 else
1453 break;
1454 }
1455
1456 /* Is this file's best line closer than the best in the other files?
1457 If so, record this file, and its best line, as best so far. */
1458 if (prev_line >= 0 && prev_pc > best_pc)
1459 {
1460 best_pc = prev_pc;
1461 best_line = prev_line;
1462 best_symtab = s;
1463 if (i < len)
1464 best_end = item->pc;
1465 else
1466 best_end = 0;
1467 }
1468 /* Is this file's first line closer than the first lines of other files?
1469 If so, record this file, and its first line, as best alternate. */
1470 if (first_line >= 0 && first_pc > pc
1471 && (alt_pc == 0 || first_pc < alt_pc))
1472 {
1473 alt_pc = first_pc;
1474 alt_line = first_line;
1475 alt_symtab = s;
1476 }
1477 }
1478 if (best_symtab == 0)
1479 {
1480 val.symtab = alt_symtab;
1481 val.line = alt_line - 1;
3ba6a043 1482 val.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
bd5635a1
RP
1483 val.end = alt_pc;
1484 }
1485 else
1486 {
1487 val.symtab = best_symtab;
1488 val.line = best_line;
1489 val.pc = best_pc;
1490 val.end = (best_end ? best_end
1491 : (alt_pc ? alt_pc
3ba6a043 1492 : BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))));
bd5635a1
RP
1493 }
1494 return val;
1495}
1496\f
1497/* Find the PC value for a given source file and line number.
1498 Returns zero for invalid line number.
1499 The source file is specified with a struct symtab. */
1500
1501CORE_ADDR
1502find_line_pc (symtab, line)
1503 struct symtab *symtab;
1504 int line;
1505{
1506 register struct linetable *l;
1507 register int ind;
1508 int dummy;
1509
1510 if (symtab == 0)
1511 return 0;
1512 l = LINETABLE (symtab);
1513 ind = find_line_common(l, line, &dummy);
b203fc18 1514 return (ind >= 0) ? l->item[ind].pc : 0;
bd5635a1
RP
1515}
1516
1517/* Find the range of pc values in a line.
1518 Store the starting pc of the line into *STARTPTR
1519 and the ending pc (start of next line) into *ENDPTR.
1520 Returns 1 to indicate success.
1521 Returns 0 if could not find the specified line. */
1522
1523int
1524find_line_pc_range (symtab, thisline, startptr, endptr)
1525 struct symtab *symtab;
1526 int thisline;
1527 CORE_ADDR *startptr, *endptr;
1528{
1529 register struct linetable *l;
1530 register int ind;
1531 int exact_match; /* did we get an exact linenumber match */
1532
1533 if (symtab == 0)
1534 return 0;
1535
1536 l = LINETABLE (symtab);
1537 ind = find_line_common (l, thisline, &exact_match);
b203fc18 1538 if (ind >= 0)
bd5635a1
RP
1539 {
1540 *startptr = l->item[ind].pc;
1541 /* If we have not seen an entry for the specified line,
1542 assume that means the specified line has zero bytes. */
1543 if (!exact_match || ind == l->nitems-1)
1544 *endptr = *startptr;
1545 else
1546 /* Perhaps the following entry is for the following line.
1547 It's worth a try. */
1548 if (ind+1 < l->nitems
1549 && l->item[ind+1].line == thisline + 1)
1550 *endptr = l->item[ind+1].pc;
1551 else
1552 *endptr = find_line_pc (symtab, thisline+1);
1553 return 1;
1554 }
1555
1556 return 0;
1557}
1558
1559/* Given a line table and a line number, return the index into the line
1560 table for the pc of the nearest line whose number is >= the specified one.
b203fc18 1561 Return -1 if none is found. The value is >= 0 if it is an index.
bd5635a1
RP
1562
1563 Set *EXACT_MATCH nonzero if the value returned is an exact match. */
1564
1565static int
1566find_line_common (l, lineno, exact_match)
1567 register struct linetable *l;
1568 register int lineno;
1569 int *exact_match;
1570{
1571 register int i;
1572 register int len;
1573
1574 /* BEST is the smallest linenumber > LINENO so far seen,
1575 or 0 if none has been seen so far.
1576 BEST_INDEX identifies the item for it. */
1577
b203fc18 1578 int best_index = -1;
bd5635a1
RP
1579 int best = 0;
1580
1581 if (lineno <= 0)
b203fc18 1582 return -1;
4137c5fc
JG
1583 if (l == 0)
1584 return -1;
bd5635a1
RP
1585
1586 len = l->nitems;
1587 for (i = 0; i < len; i++)
1588 {
1589 register struct linetable_entry *item = &(l->item[i]);
1590
1591 if (item->line == lineno)
1592 {
1593 *exact_match = 1;
1594 return i;
1595 }
1596
1597 if (item->line > lineno && (best == 0 || item->line < best))
1598 {
1599 best = item->line;
1600 best_index = i;
1601 }
1602 }
1603
1604 /* If we got here, we didn't get an exact match. */
1605
1606 *exact_match = 0;
1607 return best_index;
1608}
1609
1610int
1611find_pc_line_pc_range (pc, startptr, endptr)
1612 CORE_ADDR pc;
1613 CORE_ADDR *startptr, *endptr;
1614{
1615 struct symtab_and_line sal;
1616 sal = find_pc_line (pc, 0);
1617 *startptr = sal.pc;
1618 *endptr = sal.end;
1619 return sal.symtab != 0;
1620}
1621\f
d96b54ea
JK
1622/* If P is of the form "operator[ \t]+..." where `...' is
1623 some legitimate operator text, return a pointer to the
1624 beginning of the substring of the operator text.
1625 Otherwise, return "". */
1626static char *
1627operator_chars (p, end)
1628 char *p;
1629 char **end;
1630{
1631 *end = "";
1632 if (strncmp (p, "operator", 8))
1633 return *end;
1634 p += 8;
1635
1636 /* Don't get faked out by `operator' being part of a longer
1637 identifier. */
1638 if ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z')
1639 || *p == '_' || *p == '$' || *p == '\0')
1640 return *end;
1641
1642 /* Allow some whitespace between `operator' and the operator symbol. */
1643 while (*p == ' ' || *p == '\t')
1644 p++;
1645
1646 switch (*p)
1647 {
1648 case '!':
1649 case '=':
1650 case '*':
1651 case '/':
1652 case '%':
1653 case '^':
1654 if (p[1] == '=')
1655 *end = p+2;
1656 else
1657 *end = p+1;
1658 return p;
1659 case '<':
1660 case '>':
1661 case '+':
1662 case '-':
1663 case '&':
1664 case '|':
1665 if (p[1] == '=' || p[1] == p[0])
1666 *end = p+2;
1667 else
1668 *end = p+1;
1669 return p;
1670 case '~':
1671 case ',':
1672 *end = p+1;
1673 return p;
1674 case '(':
1675 if (p[1] != ')')
1676 error ("`operator ()' must be specified without whitespace in `()'");
1677 *end = p+2;
1678 return p;
1679 case '?':
1680 if (p[1] != ':')
1681 error ("`operator ?:' must be specified without whitespace in `?:'");
1682 *end = p+2;
1683 return p;
1684 case '[':
1685 if (p[1] != ']')
1686 error ("`operator []' must be specified without whitespace in `[]'");
1687 *end = p+2;
1688 return p;
1689 default:
1690 error ("`operator %s' not supported", p);
1691 break;
1692 }
1693 *end = "";
1694 return *end;
1695}
1696
bd5635a1
RP
1697/* Recursive helper function for decode_line_1.
1698 * Look for methods named NAME in type T.
1699 * Return number of matches.
1700 * Put matches in PHYSNAMES and SYM_ARR (which better be big enough!).
1701 * These allocations seem to define "big enough":
1702 * sym_arr = (struct symbol **) alloca(TYPE_NFN_FIELDS_TOTAL (t) * sizeof(struct symbol*));
1703 * physnames = (char **) alloca (TYPE_NFN_FIELDS_TOTAL (t) * sizeof(char*));
1704 */
1705
1706int
1707find_methods(t, name, physnames, sym_arr)
1708 struct type *t;
1709 char *name;
1710 char **physnames;
1711 struct symbol **sym_arr;
1712{
1713 int i1 = 0;
1714 int ibase;
1715 struct symbol *sym_class;
1716 char *class_name = type_name_no_tag (t);
1717 /* Ignore this class if it doesn't have a name.
1718 This prevents core dumps, but is just a workaround
1719 because we might not find the function in
1720 certain cases, such as
1721 struct D {virtual int f();}
1722 struct C : D {virtual int g();}
1723 (in this case g++ 1.35.1- does not put out a name
1724 for D as such, it defines type 19 (for example) in
1725 the same stab as C, and then does a
1726 .stabs "D:T19" and a .stabs "D:t19".
1727 Thus
1728 "break C::f" should not be looking for field f in
1729 the class named D,
1730 but just for the field f in the baseclasses of C
1731 (no matter what their names).
1732
1733 However, I don't know how to replace the code below
1734 that depends on knowing the name of D. */
1735 if (class_name
1736 && (sym_class = lookup_symbol (class_name,
1737 (struct block *)NULL,
1738 STRUCT_NAMESPACE,
1739 (int *)NULL,
1740 (struct symtab **)NULL)))
1741 {
1742 int method_counter;
1743 t = SYMBOL_TYPE (sym_class);
1744 for (method_counter = TYPE_NFN_FIELDS (t) - 1;
1745 method_counter >= 0;
1746 --method_counter)
1747 {
1748 int field_counter;
1749 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, method_counter);
1750
1751 char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
1752 if (!strcmp (name, method_name))
1753 /* Find all the fields with that name. */
1754 for (field_counter = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1;
1755 field_counter >= 0;
1756 --field_counter)
1757 {
1758 char *phys_name;
1759 if (TYPE_FLAGS (TYPE_FN_FIELD_TYPE (f, field_counter)) & TYPE_FLAG_STUB)
1760 check_stub_method (t, method_counter, field_counter);
1761 phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
1762 physnames[i1] = (char*) alloca (strlen (phys_name) + 1);
1763 strcpy (physnames[i1], phys_name);
1764 sym_arr[i1] = lookup_symbol (phys_name,
1765 SYMBOL_BLOCK_VALUE (sym_class),
1766 VAR_NAMESPACE,
1767 (int *) NULL,
1768 (struct symtab **) NULL);
1769 if (sym_arr[i1]) i1++;
1770 }
1771 }
1772 }
1773 /* Only search baseclasses if there is no match yet,
1774 * since names in derived classes override those in baseclasses.
1775 */
1776 if (i1)
1777 return i1;
1778 for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
1779 i1 += find_methods(TYPE_BASECLASS(t, ibase), name,
1780 physnames + i1, sym_arr + i1);
1781 return i1;
1782}
1783
1784/* Parse a string that specifies a line number.
1785 Pass the address of a char * variable; that variable will be
1786 advanced over the characters actually parsed.
1787
1788 The string can be:
1789
1790 LINENUM -- that line number in current file. PC returned is 0.
1791 FILE:LINENUM -- that line in that file. PC returned is 0.
1792 FUNCTION -- line number of openbrace of that function.
1793 PC returned is the start of the function.
1794 VARIABLE -- line number of definition of that variable.
1795 PC returned is 0.
1796 FILE:FUNCTION -- likewise, but prefer functions in that file.
1797 *EXPR -- line in which address EXPR appears.
1798
1799 FUNCTION may be an undebuggable function found in misc_function_vector.
1800
1801 If the argument FUNFIRSTLINE is nonzero, we want the first line
1802 of real code inside a function when a function is specified.
1803
1804 DEFAULT_SYMTAB specifies the file to use if none is specified.
1805 It defaults to current_source_symtab.
1806 DEFAULT_LINE specifies the line number to use for relative
1807 line numbers (that start with signs). Defaults to current_source_line.
1808
1809 Note that it is possible to return zero for the symtab
1810 if no file is validly specified. Callers must check that.
1811 Also, the line number returned may be invalid. */
1812
1813struct symtabs_and_lines
1814decode_line_1 (argptr, funfirstline, default_symtab, default_line)
1815 char **argptr;
1816 int funfirstline;
1817 struct symtab *default_symtab;
1818 int default_line;
1819{
1820 struct symtabs_and_lines decode_line_2 ();
1821 struct symtabs_and_lines values;
1822 struct symtab_and_line val;
1823 register char *p, *p1;
d96b54ea 1824 char *q, *q1;
bd5635a1
RP
1825 register struct symtab *s;
1826
1827 register struct symbol *sym;
1828 /* The symtab that SYM was found in. */
1829 struct symtab *sym_symtab;
1830
1831 register CORE_ADDR pc;
1832 register int i;
1833 char *copy;
1834 struct symbol *sym_class;
1835 int i1;
1836 struct symbol **sym_arr;
1837 struct type *t;
1838 char **physnames;
1839
1840 /* Defaults have defaults. */
1841
1842 if (default_symtab == 0)
1843 {
1844 default_symtab = current_source_symtab;
1845 default_line = current_source_line;
1846 }
1847
1848 /* See if arg is *PC */
1849
1850 if (**argptr == '*')
1851 {
1852 (*argptr)++;
1853 pc = parse_and_eval_address_1 (argptr);
1854 values.sals = (struct symtab_and_line *)
1855 xmalloc (sizeof (struct symtab_and_line));
1856 values.nelts = 1;
1857 values.sals[0] = find_pc_line (pc, 0);
1858 values.sals[0].pc = pc;
1859 return values;
1860 }
1861
1862 /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
1863
1864 s = 0;
1865
1866 for (p = *argptr; *p; p++)
1867 {
1868 if (p[0] == ':' || p[0] == ' ' || p[0] == '\t')
1869 break;
1870 }
1871 while (p[0] == ' ' || p[0] == '\t') p++;
1872
1873 if (p[0] == ':')
1874 {
1875
1876 /* C++ */
1877 if (p[1] ==':')
1878 {
1879 /* Extract the class name. */
1880 p1 = p;
1881 while (p != *argptr && p[-1] == ' ') --p;
1882 copy = (char *) alloca (p - *argptr + 1);
1883 bcopy (*argptr, copy, p - *argptr);
1884 copy[p - *argptr] = 0;
1885
1886 /* Discard the class name from the arg. */
1887 p = p1 + 2;
1888 while (*p == ' ' || *p == '\t') p++;
1889 *argptr = p;
1890
1891 sym_class = lookup_symbol (copy, 0, STRUCT_NAMESPACE, 0,
1892 (struct symtab **)NULL);
1893
1894 if (sym_class &&
f1d77e90 1895 ( TYPE_CODE (SYMBOL_TYPE (sym_class)) == TYPE_CODE_STRUCT
bd5635a1
RP
1896 || TYPE_CODE (SYMBOL_TYPE (sym_class)) == TYPE_CODE_UNION))
1897 {
1898 /* Arg token is not digits => try it as a function name
1899 Find the next token (everything up to end or next whitespace). */
1900 p = *argptr;
1901 while (*p && *p != ' ' && *p != '\t' && *p != ',' && *p !=':') p++;
d96b54ea 1902 q = operator_chars (*argptr, &q1);
aec4cb91 1903
d96b54ea
JK
1904 copy = (char *) alloca (p - *argptr + 1 + (q1 - q));
1905 if (q1 - q)
1906 {
1907 copy[0] = 'o';
1908 copy[1] = 'p';
1909 copy[2] = CPLUS_MARKER;
1910 bcopy (q, copy + 3, q1 - q);
1911 copy[3 + (q1 - q)] = '\0';
1912 p = q1;
1913 }
1914 else
1915 {
1916 bcopy (*argptr, copy, p - *argptr);
1917 copy[p - *argptr] = '\0';
1918 }
bd5635a1
RP
1919
1920 /* no line number may be specified */
1921 while (*p == ' ' || *p == '\t') p++;
1922 *argptr = p;
1923
1924 sym = 0;
1925 i1 = 0; /* counter for the symbol array */
1926 t = SYMBOL_TYPE (sym_class);
1927 sym_arr = (struct symbol **) alloca(TYPE_NFN_FIELDS_TOTAL (t) * sizeof(struct symbol*));
1928 physnames = (char **) alloca (TYPE_NFN_FIELDS_TOTAL (t) * sizeof(char*));
1929
1930 if (destructor_name_p (copy, t))
1931 {
1932 /* destructors are a special case. */
1933 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, 0);
1934 int len = TYPE_FN_FIELDLIST_LENGTH (t, 0) - 1;
1935 char *phys_name = TYPE_FN_FIELD_PHYSNAME (f, len);
1936 physnames[i1] = (char *)alloca (strlen (phys_name) + 1);
1937 strcpy (physnames[i1], phys_name);
1938 sym_arr[i1] =
1939 lookup_symbol (phys_name, SYMBOL_BLOCK_VALUE (sym_class),
1940 VAR_NAMESPACE, 0, (struct symtab **)NULL);
1941 if (sym_arr[i1]) i1++;
1942 }
1943 else
1944 i1 = find_methods (t, copy, physnames, sym_arr);
1945 if (i1 == 1)
1946 {
1947 /* There is exactly one field with that name. */
1948 sym = sym_arr[0];
1949
1950 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1951 {
1952 /* Arg is the name of a function */
1953 pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET;
1954 if (funfirstline)
1955 SKIP_PROLOGUE (pc);
1956 values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
1957 values.nelts = 1;
1958 values.sals[0] = find_pc_line (pc, 0);
1959 values.sals[0].pc = (values.sals[0].end && values.sals[0].pc != pc) ? values.sals[0].end : pc;
1960 }
1961 else
1962 {
1963 values.nelts = 0;
1964 }
1965 return values;
1966 }
1967 if (i1 > 0)
1968 {
1969 /* There is more than one field with that name
1970 (overloaded). Ask the user which one to use. */
1971 return decode_line_2 (sym_arr, i1, funfirstline);
1972 }
1973 else
d96b54ea
JK
1974 {
1975 char *tmp;
1976
1977 if (OPNAME_PREFIX_P (copy))
1978 {
1979 tmp = (char *)alloca (strlen (copy+3) + 9);
1980 strcpy (tmp, "operator ");
1981 strcat (tmp, copy+3);
1982 }
1983 else
1984 tmp = copy;
0e2a896c
PB
1985 if (tmp[0] == '~')
1986 error ("The class `%s' does not have destructor defined",
1987 sym_class->name);
1988 else
1989 error ("The class %s does not have any method named %s",
1990 sym_class->name, tmp);
d96b54ea 1991 }
bd5635a1
RP
1992 }
1993 else
1994 /* The quotes are important if copy is empty. */
1995 error("No class, struct, or union named \"%s\"", copy );
1996 }
1997 /* end of C++ */
1998
1999
2000 /* Extract the file name. */
2001 p1 = p;
2002 while (p != *argptr && p[-1] == ' ') --p;
58050209
DHW
2003 copy = (char *) alloca (p - *argptr + 1);
2004 bcopy (*argptr, copy, p - *argptr);
2005 copy[p - *argptr] = 0;
bd5635a1
RP
2006
2007 /* Find that file's data. */
2008 s = lookup_symtab (copy);
2009 if (s == 0)
2010 {
2011 if (symtab_list == 0 && partial_symtab_list == 0)
2012 error (no_symtab_msg);
2013 error ("No source file named %s.", copy);
2014 }
2015
2016 /* Discard the file name from the arg. */
2017 p = p1 + 1;
2018 while (*p == ' ' || *p == '\t') p++;
2019 *argptr = p;
2020 }
2021
2022 /* S is specified file's symtab, or 0 if no file specified.
2023 arg no longer contains the file name. */
2024
2025 /* Check whether arg is all digits (and sign) */
2026
2027 p = *argptr;
2028 if (*p == '-' || *p == '+') p++;
2029 while (*p >= '0' && *p <= '9')
2030 p++;
2031
2032 if (p != *argptr && (*p == 0 || *p == ' ' || *p == '\t' || *p == ','))
2033 {
2034 /* We found a token consisting of all digits -- at least one digit. */
2035 enum sign {none, plus, minus} sign = none;
2036
2037 /* This is where we need to make sure that we have good defaults.
2038 We must guarantee that this section of code is never executed
2039 when we are called with just a function name, since
2040 select_source_symtab calls us with such an argument */
2041
2042 if (s == 0 && default_symtab == 0)
2043 {
bd5635a1
RP
2044 select_source_symtab (0);
2045 default_symtab = current_source_symtab;
2046 default_line = current_source_line;
2047 }
2048
2049 if (**argptr == '+')
2050 sign = plus, (*argptr)++;
2051 else if (**argptr == '-')
2052 sign = minus, (*argptr)++;
2053 val.line = atoi (*argptr);
2054 switch (sign)
2055 {
2056 case plus:
2057 if (p == *argptr)
2058 val.line = 5;
2059 if (s == 0)
2060 val.line = default_line + val.line;
2061 break;
2062 case minus:
2063 if (p == *argptr)
2064 val.line = 15;
2065 if (s == 0)
2066 val.line = default_line - val.line;
2067 else
2068 val.line = 1;
2069 break;
2070 case none:
2071 break; /* No need to adjust val.line. */
2072 }
2073
2074 while (*p == ' ' || *p == '\t') p++;
2075 *argptr = p;
2076 if (s == 0)
2077 s = default_symtab;
2078 val.symtab = s;
2079 val.pc = 0;
2080 values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
2081 values.sals[0] = val;
2082 values.nelts = 1;
2083 return values;
2084 }
2085
2086 /* Arg token is not digits => try it as a variable name
2087 Find the next token (everything up to end or next whitespace). */
2088 p = *argptr;
2089 while (*p && *p != ' ' && *p != '\t' && *p != ',') p++;
2090 copy = (char *) alloca (p - *argptr + 1);
2091 bcopy (*argptr, copy, p - *argptr);
2092 copy[p - *argptr] = 0;
2093 while (*p == ' ' || *p == '\t') p++;
2094 *argptr = p;
2095
2096 /* Look up that token as a variable.
2097 If file specified, use that file's per-file block to start with. */
2098
2099 sym = lookup_symbol (copy,
3ba6a043 2100 (s ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK)
bd5635a1
RP
2101 : get_selected_block ()),
2102 VAR_NAMESPACE, 0, &sym_symtab);
2103
2104 if (sym != NULL)
2105 {
2106 if (SYMBOL_CLASS (sym) == LOC_BLOCK)
2107 {
2108 /* Arg is the name of a function */
2109 pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET;
2110 if (funfirstline)
2111 SKIP_PROLOGUE (pc);
2112 val = find_pc_line (pc, 0);
2113#ifdef PROLOGUE_FIRSTLINE_OVERLAP
2114 /* Convex: no need to suppress code on first line, if any */
2115 val.pc = pc;
2116#else
2117 val.pc = (val.end && val.pc != pc) ? val.end : pc;
2118#endif
2119 values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
2120 values.sals[0] = val;
2121 values.nelts = 1;
2122
2123 /* I think this is always the same as the line that
2124 we calculate above, but the general principle is
2125 "trust the symbols more than stuff like
2126 SKIP_PROLOGUE". */
2127 if (SYMBOL_LINE (sym) != 0)
2128 values.sals[0].line = SYMBOL_LINE (sym);
2129
2130 return values;
2131 }
2132 else if (SYMBOL_LINE (sym) != 0)
2133 {
2134 /* We know its line number. */
2135 values.sals = (struct symtab_and_line *)
2136 xmalloc (sizeof (struct symtab_and_line));
2137 values.nelts = 1;
2138 bzero (&values.sals[0], sizeof (values.sals[0]));
2139 values.sals[0].symtab = sym_symtab;
2140 values.sals[0].line = SYMBOL_LINE (sym);
2141 return values;
2142 }
2143 else
2144 /* This can happen if it is compiled with a compiler which doesn't
2145 put out line numbers for variables. */
2146 error ("Line number not known for symbol \"%s\"", copy);
2147 }
2148
bd5635a1
RP
2149 if ((i = lookup_misc_func (copy)) >= 0)
2150 {
2151 val.symtab = 0;
2152 val.line = 0;
2153 val.pc = misc_function_vector[i].address + FUNCTION_START_OFFSET;
2154 if (funfirstline)
2155 SKIP_PROLOGUE (val.pc);
2156 values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
2157 values.sals[0] = val;
2158 values.nelts = 1;
2159 return values;
2160 }
2161
997a978c
JG
2162 if (symtab_list == 0 && partial_symtab_list == 0 && misc_function_count == 0)
2163 error (no_symtab_msg);
2164
bd5635a1
RP
2165 error ("Function %s not defined.", copy);
2166 return values; /* for lint */
2167}
2168
2169struct symtabs_and_lines
2170decode_line_spec (string, funfirstline)
2171 char *string;
2172 int funfirstline;
2173{
2174 struct symtabs_and_lines sals;
2175 if (string == 0)
2176 error ("Empty line specification.");
2177 sals = decode_line_1 (&string, funfirstline,
2178 current_source_symtab, current_source_line);
2179 if (*string)
2180 error ("Junk at end of line specification: %s", string);
2181 return sals;
2182}
2183
2184/* Given a list of NELTS symbols in sym_arr (with corresponding
2185 mangled names in physnames), return a list of lines to operate on
2186 (ask user if necessary). */
2187struct symtabs_and_lines
2188decode_line_2 (sym_arr, nelts, funfirstline)
2189 struct symbol *sym_arr[];
2190 int nelts;
2191 int funfirstline;
2192{
bd5635a1
RP
2193 struct symtabs_and_lines values, return_values;
2194 register CORE_ADDR pc;
2195 char *args, *arg1, *command_line_input ();
2196 int i;
2197 char *prompt;
2198
2199 values.sals = (struct symtab_and_line *) alloca (nelts * sizeof(struct symtab_and_line));
2200 return_values.sals = (struct symtab_and_line *) xmalloc (nelts * sizeof(struct symtab_and_line));
2201
2202 i = 0;
2203 printf("[0] cancel\n[1] all\n");
2204 while (i < nelts)
2205 {
2206 if (sym_arr[i] && SYMBOL_CLASS (sym_arr[i]) == LOC_BLOCK)
2207 {
2208 /* Arg is the name of a function */
2209 pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym_arr[i]))
2210 + FUNCTION_START_OFFSET;
2211 if (funfirstline)
2212 SKIP_PROLOGUE (pc);
2213 values.sals[i] = find_pc_line (pc, 0);
2214 values.sals[i].pc = (values.sals[i].end && values.sals[i].pc != pc) ?
2215 values.sals[i].end : pc;
2216 printf("[%d] file:%s; line number:%d\n",
2217 (i+2), values.sals[i].symtab->filename, values.sals[i].line);
2218 }
2219 else printf ("?HERE\n");
2220 i++;
2221 }
2222
2223 if ((prompt = getenv ("PS2")) == NULL)
2224 {
2225 prompt = ">";
2226 }
2227 printf("%s ",prompt);
2228 fflush(stdout);
2229
2230 args = command_line_input (0, 0);
2231
2232 if (args == 0)
2233 error_no_arg ("one or more choice numbers");
2234
2235 i = 0;
2236 while (*args)
2237 {
2238 int num;
2239
2240 arg1 = args;
2241 while (*arg1 >= '0' && *arg1 <= '9') arg1++;
2242 if (*arg1 && *arg1 != ' ' && *arg1 != '\t')
2243 error ("Arguments must be choice numbers.");
2244
2245 num = atoi (args);
2246
2247 if (num == 0)
2248 error ("cancelled");
2249 else if (num == 1)
2250 {
2251 bcopy (values.sals, return_values.sals, (nelts * sizeof(struct symtab_and_line)));
2252 return_values.nelts = nelts;
2253 return return_values;
2254 }
2255
2256 if (num > nelts + 2)
2257 {
2258 printf ("No choice number %d.\n", num);
2259 }
2260 else
2261 {
2262 num -= 2;
2263 if (values.sals[num].pc)
2264 {
2265 return_values.sals[i++] = values.sals[num];
2266 values.sals[num].pc = 0;
2267 }
2268 else
2269 {
2270 printf ("duplicate request for %d ignored.\n", num);
2271 }
2272 }
2273
2274 args = arg1;
2275 while (*args == ' ' || *args == '\t') args++;
2276 }
2277 return_values.nelts = i;
2278 return return_values;
2279}
2280
2281/* Return the index of misc function named NAME. */
2282
2283int
2284lookup_misc_func (name)
2285 register char *name;
2286{
2287 register int i;
2288
2289 for (i = 0; i < misc_function_count; i++)
2290 if (!strcmp (misc_function_vector[i].name, name))
2291 return i;
2292 return -1; /* not found */
2293}
2294\f
2295/* Slave routine for sources_info. Force line breaks at ,'s.
2296 NAME is the name to print and *FIRST is nonzero if this is the first
2297 name printed. Set *FIRST to zero. */
2298static void
2299output_source_filename (name, first)
2300 char *name;
2301 int *first;
2302{
2303 static int column;
2304 /* Table of files printed so far. Since a single source file can
2305 result in several partial symbol tables, we need to avoid printing
2306 it more than once. Note: if some of the psymtabs are read in and
2307 some are not, it gets printed both under "Source files for which
2308 symbols have been read" and "Source files for which symbols will
2309 be read in on demand". I consider this a reasonable way to deal
2310 with the situation. I'm not sure whether this can also happen for
2311 symtabs; it doesn't hurt to check. */
2312 static char **tab = NULL;
2313 /* Allocated size of tab in elements.
2314 Start with one 256-byte block (when using GNU malloc.c).
2315 24 is the malloc overhead when range checking is in effect. */
2316 static int tab_alloc_size = (256 - 24) / sizeof (char *);
2317 /* Current size of tab in elements. */
2318 static int tab_cur_size;
2319
2320 char **p;
2321
2322 if (*first)
2323 {
2324 if (tab == NULL)
2325 tab = (char **) xmalloc (tab_alloc_size * sizeof (*tab));
2326 tab_cur_size = 0;
2327 }
2328
2329 /* Is NAME in tab? */
2330 for (p = tab; p < tab + tab_cur_size; p++)
2331 if (strcmp (*p, name) == 0)
2332 /* Yes; don't print it again. */
2333 return;
2334 /* No; add it to tab. */
2335 if (tab_cur_size == tab_alloc_size)
2336 {
2337 tab_alloc_size *= 2;
2338 tab = (char **) xrealloc (tab, tab_alloc_size * sizeof (*tab));
2339 }
2340 tab[tab_cur_size++] = name;
2341
2342 if (*first)
2343 {
2344 column = 0;
2345 *first = 0;
2346 }
2347 else
2348 {
2349 printf_filtered (",");
2350 column++;
2351 }
2352
2353 if (column != 0 && column + strlen (name) >= 70)
2354 {
2355 printf_filtered ("\n");
2356 column = 0;
2357 }
2358 else if (column != 0)
2359 {
2360 printf_filtered (" ");
2361 column++;
2362 }
2363 fputs_filtered (name, stdout);
2364 column += strlen (name);
2365}
2366
2367static void
2368sources_info ()
2369{
2370 register struct symtab *s;
2371 register struct partial_symtab *ps;
2372 int first;
2373
2374 if (symtab_list == 0 && partial_symtab_list == 0)
2375 {
3053b9f2 2376 error (no_symtab_msg);
bd5635a1
RP
2377 }
2378
2379 printf_filtered ("Source files for which symbols have been read in:\n\n");
2380
2381 first = 1;
2382 for (s = symtab_list; s; s = s->next)
2383 output_source_filename (s->filename, &first);
2384 printf_filtered ("\n\n");
2385
2386 printf_filtered ("Source files for which symbols will be read in on demand:\n\n");
2387
2388 first = 1;
2389 for (ps = partial_symtab_list; ps; ps = ps->next)
2390 if (!ps->readin)
2391 output_source_filename (ps->filename, &first);
2392 printf_filtered ("\n");
2393}
2394
2395/* List all symbols (if REGEXP is 0) or all symbols matching REGEXP.
2396 If CLASS is zero, list all symbols except functions and type names.
2397 If CLASS is 1, list only functions.
2398 If CLASS is 2, list only type names.
997a978c 2399 If CLASS is 3, list only method names.
bd5635a1
RP
2400
2401 BPT is non-zero if we should set a breakpoint at the functions
2402 we find. */
2403
2404static void
2405list_symbols (regexp, class, bpt)
2406 char *regexp;
2407 int class;
2408 int bpt;
2409{
2410 register struct symtab *s;
2411 register struct partial_symtab *ps;
2412 register struct blockvector *bv;
2413 struct blockvector *prev_bv = 0;
2414 register struct block *b;
2415 register int i, j;
2416 register struct symbol *sym;
2417 struct partial_symbol *psym;
2418 char *val;
2419 static char *classnames[]
2420 = {"variable", "function", "type", "method"};
2421 int found_in_file = 0;
997a978c
JG
2422 int found_misc = 0;
2423 static enum misc_function_type types[]
2424 = {mf_data, mf_text, mf_abs, mf_unknown};
2425 static enum misc_function_type types2[]
2426 = {mf_bss, mf_text, mf_abs, mf_unknown};
2427 enum misc_function_type ourtype = types[class];
2428 enum misc_function_type ourtype2 = types2[class];
bd5635a1
RP
2429
2430 if (regexp)
d11c44f1 2431 if (0 != (val = re_comp (regexp)))
bd5635a1
RP
2432 error ("Invalid regexp (%s): %s", val, regexp);
2433
2434 /* Search through the partial_symtab_list *first* for all symbols
2435 matching the regexp. That way we don't have to reproduce all of
2436 the machinery below. */
2437 for (ps = partial_symtab_list; ps; ps = ps->next)
2438 {
2439 struct partial_symbol *bound, *gbound, *sbound;
2440 int keep_going = 1;
2441
2442 if (ps->readin) continue;
2443
2444 gbound = global_psymbols.list + ps->globals_offset + ps->n_global_syms;
2445 sbound = static_psymbols.list + ps->statics_offset + ps->n_static_syms;
2446 bound = gbound;
2447
2448 /* Go through all of the symbols stored in a partial
2449 symtab in one loop. */
2450 psym = global_psymbols.list + ps->globals_offset;
2451 while (keep_going)
2452 {
2453 if (psym >= bound)
2454 {
2455 if (bound == gbound && ps->n_static_syms != 0)
2456 {
2457 psym = static_psymbols.list + ps->statics_offset;
2458 bound = sbound;
2459 }
2460 else
2461 keep_going = 0;
3ba6a043 2462 continue;
bd5635a1
RP
2463 }
2464 else
2465 {
2466 QUIT;
2467
2468 /* If it would match (logic taken from loop below)
2469 load the file and go on to the next one */
2470 if ((regexp == 0 || re_exec (SYMBOL_NAME (psym)))
2471 && ((class == 0 && SYMBOL_CLASS (psym) != LOC_TYPEDEF
997a978c 2472 && SYMBOL_CLASS (psym) != LOC_BLOCK)
bd5635a1
RP
2473 || (class == 1 && SYMBOL_CLASS (psym) == LOC_BLOCK)
2474 || (class == 2 && SYMBOL_CLASS (psym) == LOC_TYPEDEF)
2475 || (class == 3 && SYMBOL_CLASS (psym) == LOC_BLOCK)))
2476 {
2477 (void) PSYMTAB_TO_SYMTAB(ps);
2478 keep_going = 0;
2479 }
2480 }
2481 psym++;
2482 }
2483 }
2484
997a978c 2485 /* Here, we search through the misc function vector for functions that
bd5635a1
RP
2486 match, and call find_pc_symtab on them to force their symbols to
2487 be read. The symbol will then be found during the scan of symtabs
997a978c
JG
2488 below. If find_pc_symtab fails, set found_misc so that we will
2489 rescan to print any matching symbols without debug info. */
2490
2491 if (class == 1) {
2492 for (i = 0; i < misc_function_count; i++) {
2493 if (misc_function_vector[i].type != ourtype
2494 && misc_function_vector[i].type != ourtype2)
2495 continue;
2496 if (regexp == 0 || re_exec (misc_function_vector[i].name)) {
2497 if (0 == find_pc_symtab (misc_function_vector[i].address))
2498 found_misc = 1;
2499 }
bd5635a1 2500 }
997a978c 2501 }
bd5635a1
RP
2502
2503 /* Printout here so as to get after the "Reading in symbols"
2504 messages which will be generated above. */
2505 if (!bpt)
2506 printf_filtered (regexp
2507 ? "All %ss matching regular expression \"%s\":\n"
2508 : "All defined %ss:\n",
2509 classnames[class],
2510 regexp);
2511
2512 for (s = symtab_list; s; s = s->next)
2513 {
2514 found_in_file = 0;
2515 bv = BLOCKVECTOR (s);
2516 /* Often many files share a blockvector.
2517 Scan each blockvector only once so that
2518 we don't get every symbol many times.
2519 It happens that the first symtab in the list
2520 for any given blockvector is the main file. */
2521 if (bv != prev_bv)
3ba6a043 2522 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
bd5635a1
RP
2523 {
2524 b = BLOCKVECTOR_BLOCK (bv, i);
2525 /* Skip the sort if this block is always sorted. */
2526 if (!BLOCK_SHOULD_SORT (b))
2527 sort_block_syms (b);
2528 for (j = 0; j < BLOCK_NSYMS (b); j++)
2529 {
2530 QUIT;
2531 sym = BLOCK_SYM (b, j);
2532 if ((regexp == 0 || re_exec (SYMBOL_NAME (sym)))
2533 && ((class == 0 && SYMBOL_CLASS (sym) != LOC_TYPEDEF
997a978c 2534 && SYMBOL_CLASS (sym) != LOC_BLOCK)
bd5635a1
RP
2535 || (class == 1 && SYMBOL_CLASS (sym) == LOC_BLOCK)
2536 || (class == 2 && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2537 || (class == 3 && SYMBOL_CLASS (sym) == LOC_BLOCK)))
2538 {
2539 if (bpt)
2540 {
2541 /* Set a breakpoint here, if it's a function */
2542 if (class == 1)
2543 break_command (SYMBOL_NAME(sym), 0);
2544 }
2545 else if (!found_in_file)
2546 {
2547 fputs_filtered ("\nFile ", stdout);
2548 fputs_filtered (s->filename, stdout);
2549 fputs_filtered (":\n", stdout);
2550 }
2551 found_in_file = 1;
2552
3ba6a043 2553 if (class != 2 && i == STATIC_BLOCK)
bd5635a1 2554 printf_filtered ("static ");
997a978c
JG
2555
2556 /* Typedef that is not a C++ class */
bd5635a1
RP
2557 if (class == 2
2558 && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE)
dc9894c8 2559 typedef_print (SYMBOL_TYPE(sym), sym, stdout);
997a978c
JG
2560 /* variable, func, or typedef-that-is-c++-class */
2561 else if (class < 2 ||
2562 (class == 2 &&
2563 SYMBOL_NAMESPACE(sym) == STRUCT_NAMESPACE))
bd5635a1 2564 {
997a978c
JG
2565 type_print (SYMBOL_TYPE (sym),
2566 (SYMBOL_CLASS (sym) == LOC_TYPEDEF
2567 ? "" : SYMBOL_NAME (sym)),
2568 stdout, 0);
2569
2570 printf_filtered (";\n");
bd5635a1
RP
2571 }
2572 else
2573 {
2574# if 0
2575 char buf[1024];
2576 type_print_base (TYPE_FN_FIELD_TYPE(t, i), stdout, 0, 0);
2577 type_print_varspec_prefix (TYPE_FN_FIELD_TYPE(t, i), stdout, 0);
2578 sprintf (buf, " %s::", type_name_no_tag (t));
2579 type_print_method_args (TYPE_FN_FIELD_ARGS (t, i), buf, name, stdout);
2580# endif
2581 }
2582 }
2583 }
2584 }
2585 prev_bv = bv;
2586 }
997a978c
JG
2587
2588
2589 /* If there are no eyes, avoid all contact. I mean, if there are
2590 no debug symbols, then print directly from the misc_function_vector. */
2591
2592 if (found_misc || class != 1) {
2593 found_in_file = 0;
2594 for (i = 0; i < misc_function_count; i++) {
2595 if (misc_function_vector[i].type != ourtype
2596 && misc_function_vector[i].type != ourtype2)
2597 continue;
2598 if (regexp == 0 || re_exec (misc_function_vector[i].name)) {
2599 /* Functions: Look up by address. */
2600 if (class == 1)
2601 if (0 != find_pc_symtab (misc_function_vector[i].address))
2602 continue;
2603 /* Variables/Absolutes: Look up by name */
2604 if (0 != lookup_symbol (misc_function_vector[i].name,
2605 (struct block *)0, VAR_NAMESPACE, 0, (struct symtab **)0))
2606 continue;
2607 if (!found_in_file) {
2608 printf_filtered ("\nNon-debugging symbols:\n");
2609 found_in_file = 1;
2610 }
2611 printf_filtered (" %08x %s\n",
2612 misc_function_vector[i].address,
2613 misc_function_vector[i].name);
2614 }
2615 }
2616 }
bd5635a1
RP
2617}
2618
2619static void
2620variables_info (regexp)
2621 char *regexp;
2622{
2623 list_symbols (regexp, 0, 0);
2624}
2625
2626static void
2627functions_info (regexp)
2628 char *regexp;
2629{
2630 list_symbols (regexp, 1, 0);
2631}
2632
bd5635a1
RP
2633static void
2634types_info (regexp)
2635 char *regexp;
2636{
2637 list_symbols (regexp, 2, 0);
2638}
bd5635a1
RP
2639
2640#if 0
2641/* Tiemann says: "info methods was never implemented." */
2642static void
2643methods_info (regexp)
2644 char *regexp;
2645{
2646 list_symbols (regexp, 3, 0);
2647}
2648#endif /* 0 */
2649
2650/* Breakpoint all functions matching regular expression. */
2651static void
2652rbreak_command (regexp)
2653 char *regexp;
2654{
2655 list_symbols (regexp, 1, 1);
2656}
2657\f
997a978c 2658/* Helper function to initialize the standard scalar types. */
bd5635a1 2659
bd5635a1
RP
2660struct type *
2661init_type (code, length, uns, name)
2662 enum type_code code;
2663 int length, uns;
2664 char *name;
2665{
2666 register struct type *type;
2667
2668 type = (struct type *) xmalloc (sizeof (struct type));
2669 bzero (type, sizeof *type);
bd5635a1
RP
2670 TYPE_CODE (type) = code;
2671 TYPE_LENGTH (type) = length;
2672 TYPE_FLAGS (type) = uns ? TYPE_FLAG_UNSIGNED : 0;
2673 TYPE_FLAGS (type) |= TYPE_FLAG_PERM;
2674 TYPE_NFIELDS (type) = 0;
2675 TYPE_NAME (type) = name;
2676
2677 /* C++ fancies. */
f1d77e90 2678 if (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION)
7d9884b9
JG
2679 {
2680 TYPE_CPLUS_SPECIFIC (type)
2681 = (struct cplus_struct_type *) xmalloc (sizeof (struct cplus_struct_type));
7d9884b9
JG
2682 TYPE_NFN_FIELDS (type) = 0;
2683 TYPE_N_BASECLASSES (type) = 0;
2684 }
bd5635a1
RP
2685 return type;
2686}
2687
2688/* Return Nonzero if block a is lexically nested within block b,
2689 or if a and b have the same pc range.
2690 Return zero otherwise. */
2691int
2692contained_in (a, b)
2693 struct block *a, *b;
2694{
2695 if (!a || !b)
2696 return 0;
2697 return BLOCK_START (a) >= BLOCK_START (b)
2698 && BLOCK_END (a) <= BLOCK_END (b);
2699}
2700
2701\f
2702/* Helper routine for make_symbol_completion_list. */
2703
2704int return_val_size, return_val_index;
2705char **return_val;
2706
2707void
2708completion_list_add_symbol (symname)
2709 char *symname;
2710{
2711 if (return_val_index + 3 > return_val_size)
2712 return_val =
2713 (char **)xrealloc (return_val,
2714 (return_val_size *= 2) * sizeof (char *));
2715
2716 return_val[return_val_index] =
2717 (char *)xmalloc (1 + strlen (symname));
2718
2719 strcpy (return_val[return_val_index], symname);
2720
2721 return_val[++return_val_index] = (char *)NULL;
2722}
2723
2724/* Return a NULL terminated array of all symbols (regardless of class) which
2725 begin by matching TEXT. If the answer is no symbols, then the return value
2726 is an array which contains only a NULL pointer.
2727
2728 Problem: All of the symbols have to be copied because readline
2729 frees them. I'm not going to worry about this; hopefully there
2730 won't be that many. */
2731
2732char **
2733make_symbol_completion_list (text)
2734 char *text;
2735{
2736 register struct symtab *s;
2737 register struct partial_symtab *ps;
2738 register struct block *b, *surrounding_static_block = 0;
2739 extern struct block *get_selected_block ();
2740 register int i, j;
2741 struct partial_symbol *psym;
2742
2743 int text_len = strlen (text);
2744 return_val_size = 100;
2745 return_val_index = 0;
2746 return_val =
2747 (char **)xmalloc ((1 + return_val_size) *sizeof (char *));
2748 return_val[0] = (char *)NULL;
2749
2750 /* Look through the partial symtabs for all symbols which begin
2751 by matching TEXT. Add each one that you find to the list. */
2752
2753 for (ps = partial_symtab_list; ps; ps = ps->next)
2754 {
2755 /* If the psymtab's been read in we'll get it when we search
2756 through the blockvector. */
2757 if (ps->readin) continue;
2758
2759 for (psym = global_psymbols.list + ps->globals_offset;
2760 psym < (global_psymbols.list + ps->globals_offset
2761 + ps->n_global_syms);
2762 psym++)
2763 {
2764 QUIT; /* If interrupted, then quit. */
2765 if ((strncmp (SYMBOL_NAME (psym), text, text_len) == 0))
2766 completion_list_add_symbol (SYMBOL_NAME (psym));
2767 }
2768
2769 for (psym = static_psymbols.list + ps->statics_offset;
2770 psym < (static_psymbols.list + ps->statics_offset
2771 + ps->n_static_syms);
2772 psym++)
2773 {
2774 QUIT;
2775 if ((strncmp (SYMBOL_NAME (psym), text, text_len) == 0))
2776 completion_list_add_symbol (SYMBOL_NAME (psym));
2777 }
2778 }
2779
2780 /* At this point scan through the misc function vector and add each
2781 symbol you find to the list. Eventually we want to ignore
2782 anything that isn't a text symbol (everything else will be
2783 handled by the psymtab code above). */
2784
2785 for (i = 0; i < misc_function_count; i++)
2786 if (!strncmp (text, misc_function_vector[i].name, text_len))
2787 completion_list_add_symbol (misc_function_vector[i].name);
2788
2789 /* Search upwards from currently selected frame (so that we can
2790 complete on local vars. */
2791 for (b = get_selected_block (); b; b = BLOCK_SUPERBLOCK (b))
2792 {
2793 if (!BLOCK_SUPERBLOCK (b))
2794 surrounding_static_block = b; /* For elmin of dups */
2795
2796 /* Also catch fields of types defined in this places which
2797 match our text string. Only complete on types visible
2798 from current context. */
2799 for (i = 0; i < BLOCK_NSYMS (b); i++)
2800 {
2801 register struct symbol *sym = BLOCK_SYM (b, i);
2802
2803 if (!strncmp (SYMBOL_NAME (sym), text, text_len))
2804 completion_list_add_symbol (SYMBOL_NAME (sym));
2805
2806 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2807 {
2808 struct type *t = SYMBOL_TYPE (sym);
2809 enum type_code c = TYPE_CODE (t);
2810
2811 if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
2812 for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
2813 if (TYPE_FIELD_NAME (t, j) &&
2814 !strncmp (TYPE_FIELD_NAME (t, j), text, text_len))
2815 completion_list_add_symbol (TYPE_FIELD_NAME (t, j));
2816 }
2817 }
2818 }
2819
2820 /* Go through the symtabs and check the externs and statics for
2821 symbols which match. */
2822
2823 for (s = symtab_list; s; s = s->next)
2824 {
3ba6a043 2825 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
bd5635a1
RP
2826
2827 for (i = 0; i < BLOCK_NSYMS (b); i++)
2828 if (!strncmp (SYMBOL_NAME (BLOCK_SYM (b, i)), text, text_len))
2829 completion_list_add_symbol (SYMBOL_NAME (BLOCK_SYM (b, i)));
2830 }
2831
2832 for (s = symtab_list; s; s = s->next)
2833 {
3ba6a043 2834 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
bd5635a1
RP
2835
2836 /* Don't do this block twice. */
2837 if (b == surrounding_static_block) continue;
2838
2839 for (i = 0; i < BLOCK_NSYMS (b); i++)
2840 if (!strncmp (SYMBOL_NAME (BLOCK_SYM (b, i)), text, text_len))
2841 completion_list_add_symbol (SYMBOL_NAME (BLOCK_SYM (b, i)));
2842 }
2843
2844 return (return_val);
2845}
2846\f
997a978c
JG
2847#if 0
2848/* Add the type of the symbol sym to the type of the current
2849 function whose block we are in (assumed). The type of
2850 this current function is contained in *TYPE.
2851
2852 This basically works as follows: When we find a function
2853 symbol (N_FUNC with a 'f' or 'F' in the symbol name), we record
2854 a pointer to its type in the global in_function_type. Every
2855 time we come across a parameter symbol ('p' in its name), then
2856 this procedure adds the name and type of that parameter
2857 to the function type pointed to by *TYPE. (Which should correspond
2858 to in_function_type if it was called correctly).
2859
2860 Note that since we are modifying a type, the result of
2861 lookup_function_type() should be bcopy()ed before calling
2862 this. When not in strict typing mode, the expression
2863 evaluator can choose to ignore this.
2864
2865 Assumption: All of a function's parameter symbols will
2866 appear before another function symbol is found. The parameters
2867 appear in the same order in the argument list as they do in the
2868 symbol table. */
2869
2870void
2871add_param_to_type (type,sym)
2872 struct type **type;
2873 struct symbol *sym;
2874{
2875 int num = ++(TYPE_NFIELDS(*type));
2876
2877 if(TYPE_NFIELDS(*type)-1)
2878 TYPE_FIELDS(*type) =
2879 (struct field *)xrealloc((char *)(TYPE_FIELDS(*type)),
2880 num*sizeof(struct field));
2881 else
2882 TYPE_FIELDS(*type) =
2883 (struct field *)xmalloc(num*sizeof(struct field));
2884
2885 TYPE_FIELD_BITPOS(*type,num-1) = num-1;
2886 TYPE_FIELD_BITSIZE(*type,num-1) = 0;
2887 TYPE_FIELD_TYPE(*type,num-1) = SYMBOL_TYPE(sym);
2888 TYPE_FIELD_NAME(*type,num-1) = SYMBOL_NAME(sym);
2889}
2890#endif
2891\f
bd5635a1
RP
2892void
2893_initialize_symtab ()
2894{
2895 add_info ("variables", variables_info,
2896 "All global and static variable names, or those matching REGEXP.");
2897 add_info ("functions", functions_info,
2898 "All function names, or those matching REGEXP.");
3ba6a043
JG
2899
2900 /* FIXME: This command has at least the following problems:
bd5635a1
RP
2901 1. It prints builtin types (in a very strange and confusing fashion).
2902 2. It doesn't print right, e.g. with
2903 typedef struct foo *FOO
2904 type_print prints "FOO" when we want to make it (in this situation)
2905 print "struct foo *".
2906 I also think "ptype" or "whatis" is more likely to be useful (but if
2907 there is much disagreement "info types" can be fixed). */
2908 add_info ("types", types_info,
a0a6174a 2909 "All type names, or those matching REGEXP.");
3ba6a043 2910
bd5635a1
RP
2911#if 0
2912 add_info ("methods", methods_info,
2913 "All method names, or those matching REGEXP::REGEXP.\n\
2914If the class qualifier is ommited, it is assumed to be the current scope.\n\
2915If the first REGEXP is ommited, then all methods matching the second REGEXP\n\
2916are listed.");
2917#endif
2918 add_info ("sources", sources_info,
2919 "Source files in the program.");
2920
2921 add_com ("rbreak", no_class, rbreak_command,
2922 "Set a breakpoint for all functions matching REGEXP.");
2923
997a978c 2924 /* Initialize the one built-in type that isn't language dependent... */
bd5635a1
RP
2925 builtin_type_error = init_type (TYPE_CODE_ERROR, 0, 0, "<unknown type>");
2926}
This page took 0.159872 seconds and 4 git commands to generate.