- char *copy;
- int n, num;
-
- copy = xmalloc (len + 1);
- memcpy (copy, p, len);
- copy[len] = 0;
-
- num = sscanf (copy, "%" DOUBLEST_SCAN_FORMAT "%n", d, &n);
- xfree (copy);
-
- /* The sscanf man page suggests not making any assumptions on the effect
- of %n on the result, so we don't.
- That is why we simply test num == 0. */
- if (num == 0)
- return 0;
-
- *suffix = p + n;
- return 1;
-}
-
-/* Parse floating point value P of length LEN, using the C syntax for floats.
- Return 0 (false) if invalid, 1 (true) if valid.
- The successfully parsed number is stored in *D.
- Its type is taken from builtin_type (gdbarch) and is stored in *T. */
-
-int
-parse_c_float (struct gdbarch *gdbarch, const char *p, int len,
- DOUBLEST *d, struct type **t)
-{
- const char *suffix;
- int suffix_len;
- const struct builtin_type *builtin_types = builtin_type (gdbarch);
-
- if (! parse_float (p, len, d, &suffix))
- return 0;
-
- suffix_len = p + len - suffix;
-
- if (suffix_len == 0)
- *t = builtin_types->builtin_double;
- else if (suffix_len == 1)
- {
- /* Handle suffixes: 'f' for float, 'l' for long double. */
- if (tolower (*suffix) == 'f')
- *t = builtin_types->builtin_float;
- else if (tolower (*suffix) == 'l')
- *t = builtin_types->builtin_long_double;
- else
- return 0;
- }
- else
- return 0;
-
- return 1;
-}
-\f
-/* Stuff for maintaining a stack of types. Currently just used by C, but
- probably useful for any language which declares its types "backwards". */
-
-/* Ensure that there are HOWMUCH open slots on the type stack STACK. */
-
-static void
-type_stack_reserve (struct type_stack *stack, int howmuch)
-{
- if (stack->depth + howmuch >= stack->size)
- {
- stack->size *= 2;
- if (stack->size < howmuch)
- stack->size = howmuch;
- stack->elements = xrealloc (stack->elements,
- stack->size * sizeof (union type_stack_elt));
- }
-}
-
-/* Ensure that there is a single open slot in the global type stack. */
-
-static void
-check_type_stack_depth (void)
-{
- type_stack_reserve (&type_stack, 1);
-}
-
-/* A helper function for insert_type and insert_type_address_space.
- This does work of expanding the type stack and inserting the new
- element, ELEMENT, into the stack at location SLOT. */
-
-static void
-insert_into_type_stack (int slot, union type_stack_elt element)
-{
- check_type_stack_depth ();
-
- if (slot < type_stack.depth)
- memmove (&type_stack.elements[slot + 1], &type_stack.elements[slot],
- (type_stack.depth - slot) * sizeof (union type_stack_elt));
- type_stack.elements[slot] = element;
- ++type_stack.depth;
-}
-
-/* Insert a new type, TP, at the bottom of the type stack. If TP is
- tp_pointer or tp_reference, it is inserted at the bottom. If TP is
- a qualifier, it is inserted at slot 1 (just above a previous
- tp_pointer) if there is anything on the stack, or simply pushed if
- the stack is empty. Other values for TP are invalid. */
-
-void
-insert_type (enum type_pieces tp)
-{
- union type_stack_elt element;
- int slot;
-
- gdb_assert (tp == tp_pointer || tp == tp_reference
- || tp == tp_const || tp == tp_volatile);
-
- /* If there is anything on the stack (we know it will be a
- tp_pointer), insert the qualifier above it. Otherwise, simply
- push this on the top of the stack. */
- if (type_stack.depth && (tp == tp_const || tp == tp_volatile))
- slot = 1;
- else
- slot = 0;
-
- element.piece = tp;
- insert_into_type_stack (slot, element);
-}
-
-void
-push_type (enum type_pieces tp)
-{
- check_type_stack_depth ();
- type_stack.elements[type_stack.depth++].piece = tp;
-}
-
-void
-push_type_int (int n)
-{
- check_type_stack_depth ();
- type_stack.elements[type_stack.depth++].int_val = n;
-}
-
-/* Insert a tp_space_identifier and the corresponding address space
- value into the stack. STRING is the name of an address space, as
- recognized by address_space_name_to_int. If the stack is empty,
- the new elements are simply pushed. If the stack is not empty,
- this function assumes that the first item on the stack is a
- tp_pointer, and the new values are inserted above the first
- item. */
-
-void
-insert_type_address_space (struct parser_state *pstate, char *string)
-{
- union type_stack_elt element;
- int slot;
-
- /* If there is anything on the stack (we know it will be a
- tp_pointer), insert the address space qualifier above it.
- Otherwise, simply push this on the top of the stack. */
- if (type_stack.depth)
- slot = 1;
- else
- slot = 0;
-
- element.piece = tp_space_identifier;
- insert_into_type_stack (slot, element);
- element.int_val = address_space_name_to_int (parse_gdbarch (pstate),
- string);
- insert_into_type_stack (slot, element);
-}
-
-enum type_pieces
-pop_type (void)
-{
- if (type_stack.depth)
- return type_stack.elements[--type_stack.depth].piece;
- return tp_end;
-}
-
-int
-pop_type_int (void)
-{
- if (type_stack.depth)
- return type_stack.elements[--type_stack.depth].int_val;
- /* "Can't happen". */
- return 0;
-}
-
-/* Pop a type list element from the global type stack. */
-
-static VEC (type_ptr) *
-pop_typelist (void)
-{
- gdb_assert (type_stack.depth);
- return type_stack.elements[--type_stack.depth].typelist_val;
-}
-
-/* Pop a type_stack element from the global type stack. */
-
-static struct type_stack *
-pop_type_stack (void)
-{
- gdb_assert (type_stack.depth);
- return type_stack.elements[--type_stack.depth].stack_val;
-}
-
-/* Append the elements of the type stack FROM to the type stack TO.
- Always returns TO. */
-
-struct type_stack *
-append_type_stack (struct type_stack *to, struct type_stack *from)
-{
- type_stack_reserve (to, from->depth);
-
- memcpy (&to->elements[to->depth], &from->elements[0],
- from->depth * sizeof (union type_stack_elt));
- to->depth += from->depth;
-
- return to;
-}
-
-/* Push the type stack STACK as an element on the global type stack. */
-
-void
-push_type_stack (struct type_stack *stack)
-{
- check_type_stack_depth ();
- type_stack.elements[type_stack.depth++].stack_val = stack;
- push_type (tp_type_stack);
-}
-
-/* Copy the global type stack into a newly allocated type stack and
- return it. The global stack is cleared. The returned type stack
- must be freed with type_stack_cleanup. */
-
-struct type_stack *
-get_type_stack (void)
-{
- struct type_stack *result = XNEW (struct type_stack);
-
- *result = type_stack;
- type_stack.depth = 0;
- type_stack.size = 0;
- type_stack.elements = NULL;
-
- return result;
-}
-
-/* A cleanup function that destroys a single type stack. */
-
-void
-type_stack_cleanup (void *arg)
-{
- struct type_stack *stack = arg;
-
- xfree (stack->elements);
- xfree (stack);
-}
-
-/* Push a function type with arguments onto the global type stack.
- LIST holds the argument types. If the final item in LIST is NULL,
- then the function will be varargs. */
-
-void
-push_typelist (VEC (type_ptr) *list)
-{
- check_type_stack_depth ();
- type_stack.elements[type_stack.depth++].typelist_val = list;
- push_type (tp_function_with_arguments);
-}
-
-/* Pop the type stack and return the type which corresponds to FOLLOW_TYPE
- as modified by all the stuff on the stack. */
-struct type *
-follow_types (struct type *follow_type)
-{
- int done = 0;
- int make_const = 0;
- int make_volatile = 0;
- int make_addr_space = 0;
- int array_size;
-
- while (!done)
- switch (pop_type ())
- {
- case tp_end:
- done = 1;
- if (make_const)
- follow_type = make_cv_type (make_const,
- TYPE_VOLATILE (follow_type),
- follow_type, 0);
- if (make_volatile)
- follow_type = make_cv_type (TYPE_CONST (follow_type),
- make_volatile,
- follow_type, 0);
- if (make_addr_space)
- follow_type = make_type_with_address_space (follow_type,
- make_addr_space);
- make_const = make_volatile = 0;
- make_addr_space = 0;
- break;
- case tp_const:
- make_const = 1;
- break;
- case tp_volatile:
- make_volatile = 1;
- break;
- case tp_space_identifier:
- make_addr_space = pop_type_int ();
- break;
- case tp_pointer:
- follow_type = lookup_pointer_type (follow_type);
- if (make_const)
- follow_type = make_cv_type (make_const,
- TYPE_VOLATILE (follow_type),
- follow_type, 0);
- if (make_volatile)
- follow_type = make_cv_type (TYPE_CONST (follow_type),
- make_volatile,
- follow_type, 0);
- if (make_addr_space)
- follow_type = make_type_with_address_space (follow_type,
- make_addr_space);
- make_const = make_volatile = 0;
- make_addr_space = 0;
- break;
- case tp_reference:
- follow_type = lookup_reference_type (follow_type);
- if (make_const)
- follow_type = make_cv_type (make_const,
- TYPE_VOLATILE (follow_type),
- follow_type, 0);
- if (make_volatile)
- follow_type = make_cv_type (TYPE_CONST (follow_type),
- make_volatile,
- follow_type, 0);
- if (make_addr_space)
- follow_type = make_type_with_address_space (follow_type,
- make_addr_space);
- make_const = make_volatile = 0;
- make_addr_space = 0;
- break;
- case tp_array:
- array_size = pop_type_int ();
- /* FIXME-type-allocation: need a way to free this type when we are
- done with it. */
- follow_type =
- lookup_array_range_type (follow_type,
- 0, array_size >= 0 ? array_size - 1 : 0);
- if (array_size < 0)
- TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (follow_type))
- = PROP_UNDEFINED;
- break;
- case tp_function:
- /* FIXME-type-allocation: need a way to free this type when we are
- done with it. */
- follow_type = lookup_function_type (follow_type);
- break;
-
- case tp_function_with_arguments:
- {
- VEC (type_ptr) *args = pop_typelist ();
-
- follow_type
- = lookup_function_type_with_arguments (follow_type,
- VEC_length (type_ptr, args),
- VEC_address (type_ptr,
- args));
- VEC_free (type_ptr, args);
- }
- break;
-
- case tp_type_stack:
- {
- struct type_stack *stack = pop_type_stack ();
- /* Sort of ugly, but not really much worse than the
- alternatives. */
- struct type_stack save = type_stack;
-
- type_stack = *stack;
- follow_type = follow_types (follow_type);
- gdb_assert (type_stack.depth == 0);
-
- type_stack = save;
- }
- break;
- default:
- gdb_assert_not_reached ("unrecognized tp_ value in follow_types");
- }
- return follow_type;