cp-demangle.c

来自「基于4个mips核的noc设计」· C语言 代码 · 共 2,161 行 · 第 1/5 页

C
2,161
字号
  return STATUS_OK;}/* Allocates and pushes a new string onto the demangled results stack   for DM.  Subsequent demangling with DM will emit to the new string.   Returns STATUS_OK on success, STATUS_ALLOCATION_FAILED on   allocation failure.  */static status_tresult_push (dm)     demangling_t dm;{  string_list_t new_string = string_list_new (0);  if (new_string == NULL)    /* Allocation failed.  */    return STATUS_ALLOCATION_FAILED;  /* Link the new string to the front of the list of result strings.  */  new_string->next = (string_list_t) dm->result;  dm->result = new_string;  return STATUS_OK;}/* Removes and returns the topmost element on the demangled results   stack for DM.  The caller assumes ownership for the returned   string.  */static string_list_tresult_pop (dm)     demangling_t dm;{  string_list_t top = dm->result;  dm->result = top->next;  return top;}/* Returns the current value of the caret for the result string.  The   value is an offet from the end of the result string.  */static intresult_get_caret (dm)     demangling_t dm;{  return ((string_list_t) result_string (dm))->caret_position;}/* Sets the value of the caret for the result string, counted as an   offet from the end of the result string.  */static voidresult_set_caret (dm, position)     demangling_t dm;     int position;{  ((string_list_t) result_string (dm))->caret_position = position;}/* Shifts the position of the next addition to the result by   POSITION_OFFSET.  A negative value shifts the caret to the left.  */static voidresult_shift_caret (dm, position_offset)     demangling_t dm;     int position_offset;{  ((string_list_t) result_string (dm))->caret_position += position_offset;}/* Returns non-zero if the character that comes right before the place   where text will be added to the result is a space.  In this case,   the caller should supress adding another space.  */static intresult_previous_char_is_space (dm)     demangling_t dm;{  char *result = dyn_string_buf (result_string (dm));  int pos = result_caret_pos (dm);  return pos > 0 && result[pos - 1] == ' ';}/* Returns the start position of a fragment of the demangled result   that will be a substitution candidate.  Should be called at the   start of productions that can add substitutions.  */static intsubstitution_start (dm)     demangling_t dm;{  return result_caret_pos (dm);}/* Adds the suffix of the current demangled result of DM starting at   START_POSITION as a potential substitution.  If TEMPLATE_P is   non-zero, this potential substitution is a template-id.  */static status_tsubstitution_add (dm, start_position, template_p)     demangling_t dm;     int start_position;     int template_p;{  dyn_string_t result = result_string (dm);  dyn_string_t substitution = dyn_string_new (0);  int i;  if (substitution == NULL)    return STATUS_ALLOCATION_FAILED;  /* Extract the substring of the current demangling result that     represents the subsitution candidate.  */  if (!dyn_string_substring (substitution, 			     result, start_position, result_caret_pos (dm)))    {      dyn_string_delete (substitution);      return STATUS_ALLOCATION_FAILED;    }  /* If there's no room for the new entry, grow the array.  */  if (dm->substitutions_allocated == dm->num_substitutions)    {      size_t new_array_size;      if (dm->substitutions_allocated > 0)	dm->substitutions_allocated *= 2;      else	dm->substitutions_allocated = 2;      new_array_size = 	sizeof (struct substitution_def) * dm->substitutions_allocated;      dm->substitutions = (struct substitution_def *)	realloc (dm->substitutions, new_array_size);      if (dm->substitutions == NULL)	/* Realloc failed.  */	{	  dyn_string_delete (substitution);	  return STATUS_ALLOCATION_FAILED;	}    }  /* Add the substitution to the array.  */  i = dm->num_substitutions++;  dm->substitutions[i].text = substitution;  dm->substitutions[i].template_p = template_p;#ifdef CP_DEMANGLE_DEBUG  substitutions_print (dm, stderr);#endif  return STATUS_OK;}/* Returns the Nth-most-recent substitution.  Sets *TEMPLATE_P to   non-zero if the substitution is a template-id, zero otherwise.     N is numbered from zero.  DM retains ownership of the returned   string.  If N is negative, or equal to or greater than the current   number of substitution candidates, returns NULL.  */static dyn_string_tsubstitution_get (dm, n, template_p)     demangling_t dm;     int n;     int *template_p;{  struct substitution_def *sub;  /* Make sure N is in the valid range.  */  if (n < 0 || n >= dm->num_substitutions)    return NULL;  sub = &(dm->substitutions[n]);  *template_p = sub->template_p;  return sub->text;}#ifdef CP_DEMANGLE_DEBUG/* Debugging routine to print the current substitutions to FP.  */static voidsubstitutions_print (dm, fp)     demangling_t dm;     FILE *fp;{  int seq_id;  int num = dm->num_substitutions;  fprintf (fp, "SUBSTITUTIONS:\n");  for (seq_id = -1; seq_id < num - 1; ++seq_id)    {      int template_p;      dyn_string_t text = substitution_get (dm, seq_id + 1, &template_p);      if (seq_id == -1)	fprintf (fp, " S_ ");      else	fprintf (fp, " S%d_", seq_id);      fprintf (fp, " %c: %s\n", template_p ? '*' : ' ', dyn_string_buf (text));    }}#endif /* CP_DEMANGLE_DEBUG *//* Creates a new template argument list.  Returns NULL if allocation   fails.  */static template_arg_list_ttemplate_arg_list_new (){  template_arg_list_t new_list =    (template_arg_list_t) malloc (sizeof (struct template_arg_list_def));  if (new_list == NULL)    return NULL;  /* Initialize the new list to have no arguments.  */  new_list->first_argument = NULL;  new_list->last_argument = NULL;  /* Return the new list.  */  return new_list;}/* Deletes a template argument list and the template arguments it   contains.  */static voidtemplate_arg_list_delete (list)     template_arg_list_t list;{  /* If there are any arguments on LIST, delete them.  */  if (list->first_argument != NULL)    string_list_delete (list->first_argument);  /* Delete LIST.  */  free (list);}/* Adds ARG to the template argument list ARG_LIST.  */static void template_arg_list_add_arg (arg_list, arg)     template_arg_list_t arg_list;     string_list_t arg;{  if (arg_list->first_argument == NULL)    /* If there were no arguments before, ARG is the first one.  */    arg_list->first_argument = arg;  else    /* Make ARG the last argument on the list.  */    arg_list->last_argument->next = arg;  /* Make ARG the last on the list.  */  arg_list->last_argument = arg;  arg->next = NULL;}/* Returns the template arugment at position INDEX in template   argument list ARG_LIST.  */static string_list_ttemplate_arg_list_get_arg (arg_list, index)     template_arg_list_t arg_list;     int index;{  string_list_t arg = arg_list->first_argument;  /* Scan down the list of arguments to find the one at position     INDEX.  */  while (index--)    {      arg = arg->next;      if (arg == NULL)	/* Ran out of arguments before INDEX hit zero.  That's an	   error.  */	return NULL;    }  /* Return the argument at position INDEX.  */  return arg;}/* Pushes ARG_LIST onto the top of the template argument list stack.  */static voidpush_template_arg_list (dm, arg_list)     demangling_t dm;     template_arg_list_t arg_list;{  arg_list->next = dm->template_arg_lists;  dm->template_arg_lists = arg_list;#ifdef CP_DEMANGLE_DEBUG  fprintf (stderr, " ** pushing template arg list\n");  template_arg_list_print (arg_list, stderr);#endif }/* Pops and deletes elements on the template argument list stack until   arg_list is the topmost element.  If arg_list is NULL, all elements   are popped and deleted.  */static voidpop_to_template_arg_list (dm, arg_list)     demangling_t dm;     template_arg_list_t arg_list;{  while (dm->template_arg_lists != arg_list)    {      template_arg_list_t top = dm->template_arg_lists;      /* Disconnect the topmost element from the list.  */      dm->template_arg_lists = top->next;      /* Delete the popped element.  */      template_arg_list_delete (top);#ifdef CP_DEMANGLE_DEBUG      fprintf (stderr, " ** removing template arg list\n");#endif    }}#ifdef CP_DEMANGLE_DEBUG/* Prints the contents of ARG_LIST to FP.  */static voidtemplate_arg_list_print (arg_list, fp)  template_arg_list_t arg_list;  FILE *fp;{  string_list_t arg;  int index = -1;  fprintf (fp, "TEMPLATE ARGUMENT LIST:\n");  for (arg = arg_list->first_argument; arg != NULL; arg = arg->next)    {      if (index == -1)	fprintf (fp, " T_  : ");      else	fprintf (fp, " T%d_ : ", index);      ++index;      fprintf (fp, "%s\n", dyn_string_buf ((dyn_string_t) arg));    }}#endif /* CP_DEMANGLE_DEBUG *//* Returns the topmost element on the stack of template argument   lists.  If there is no list of template arguments, returns NULL.  */static template_arg_list_tcurrent_template_arg_list (dm)     demangling_t dm;{  return dm->template_arg_lists;}/* Allocates a demangling_t object for demangling mangled NAME.  A new   result must be pushed before the returned object can be used.   Returns NULL if allocation fails.  */static demangling_tdemangling_new (name)     const char *name;{  demangling_t dm;  dm = (demangling_t) malloc (sizeof (struct demangling_def));  if (dm == NULL)    return NULL;  dm->name = name;  dm->next = name;  dm->result = NULL;  dm->num_substitutions = 0;  dm->substitutions_allocated = 10;  dm->template_arg_lists = NULL;  dm->last_source_name = dyn_string_new (0);  if (dm->last_source_name == NULL)    return NULL;  dm->substitutions = (struct substitution_def *)    malloc (dm->substitutions_allocated * sizeof (struct substitution_def));  if (dm->substitutions == NULL)    {      dyn_string_delete (dm->last_source_name);      return NULL;    }  return dm;}/* Deallocates a demangling_t object and all memory associated with   it.  */static voiddemangling_delete (dm)     demangling_t dm;{  int i;  template_arg_list_t arg_list = dm->template_arg_lists;  /* Delete the stack of template argument lists.  */  while (arg_list != NULL)    {      template_arg_list_t next = arg_list->next;      template_arg_list_delete (arg_list);      arg_list = next;    }  /* Delete the list of substitutions.  */  for (i = dm->num_substitutions; --i >= 0; )    dyn_string_delete (dm->substitutions[i].text);  free (dm->substitutions);  /* Delete the demangled result.  */  string_list_delete (dm->result);  /* Delete the stored identifier name.  */  dyn_string_delete (dm->last_source_name);  /* Delete the context object itself.  */  free (dm);}/* These functions demangle an alternative of the corresponding   production in the mangling spec.  The first argument of each is a   demangling context structure for the current demangling   operation.  Most emit demangled text directly to the topmost result   string on the result string stack in the demangling context   structure.  */static status_t demangle_char  PARAMS ((demangling_t, int));static status_t demangle_mangled_name   PARAMS ((demangling_t));static status_t demangle_encoding  PARAMS ((demangling_t));static status_t demangle_name  PARAMS ((demangling_t, int *));static status_t demangle_nested_name  PARAMS ((demangling_t, int *));static status_t demangle_prefix  PARAMS ((demangling_t, int *));static status_t demangle_unqualified_name  PARAMS ((demangling_t, int *));static status_t demangle_source_name  PARAMS ((demangling_t));static status_t demangle_number

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?