1
0
mirror of https://git.savannah.gnu.org/git/gperf.git synced 2025-12-02 13:09:22 +00:00

Remove all occurrences of reinterpret_cast<>.

* src/keyword-list.h (Keyword_List): Add template parameter list <KT>.
(KeywordExt_List): Define as a typedef, not as a subclass.
(copy_list, delete_list, mergesort_list): Templatize accordingly.
* src/keyword-list.icc (Keyword_List): Add template parameter list <KT>.
(KeywordExt_List): Remove method definitions.
* src/keyword-list.cc (Keyword_List): Add template parameter list <KT>.
(copy_list, mergesort_list): Remove definitions on subclass.
Add explicit template instantiation.
* src/keyword.h (Keyword_Factory): Add template parameter list <KT>.
* src/keyword.cc (Keyword_Factory): Likewise.
Add explicit template instantiation.
* src/input.h (Input): Add template parameter list <KT>.
* src/input.cc (Input): Likewise. Add explicit template instantiation.
* src/main.cc (KeywordExt_Factory): Define as a typedef, not as a
subclass.
(main): Update.
This commit is contained in:
Bruno Haible
2025-04-05 14:18:27 +02:00
parent 67c697622f
commit a041291c80
9 changed files with 1060 additions and 1019 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -2,7 +2,7 @@
/* Input routines.
Copyright (C) 1989-1998, 2002-2003 Free Software Foundation, Inc.
Copyright (C) 1989-1998, 2002-2003, 2025 Free Software Foundation, Inc.
Written by Douglas C. Schmidt <schmidt@ics.uci.edu>
and Bruno Haible <bruno@clisp.org>.
@@ -27,41 +27,42 @@
#include <stdio.h>
#include "keyword-list.h"
class Input
{
public:
Input (FILE *stream, Keyword_Factory *keyword_factory);
~Input ();
void read_input ();
private:
/* Input stream. */
FILE * _stream;
/* Creates the keywords. */
Keyword_Factory * const _factory;
public:
/* Memory block containing the entire input. */
char * _input;
char * _input_end;
/* The C code from the declarations section. */
const char * _verbatim_declarations;
const char * _verbatim_declarations_end;
unsigned int _verbatim_declarations_lineno;
/* The C code from the end of the file. */
const char * _verbatim_code;
const char * _verbatim_code_end;
unsigned int _verbatim_code_lineno;
/* Declaration of struct type for a keyword and its attributes. */
const char * _struct_decl;
unsigned int _struct_decl_lineno;
/* Return type of the lookup function. */
const char * _return_type;
/* Shorthand for user-defined struct tag type. */
const char * _struct_tag;
/* List of all keywords. */
Keyword_List * _head;
/* Whether the keyword chars would have different values in a different
character set. */
bool _charset_dependent;
};
template <class KT>
class Input
{
public:
Input (FILE *stream, Keyword_Factory<KT> *keyword_factory);
~Input ();
void read_input ();
private:
/* Input stream. */
FILE * _stream;
/* Creates the keywords. */
Keyword_Factory<KT> * const _factory;
public:
/* Memory block containing the entire input. */
char * _input;
char * _input_end;
/* The C code from the declarations section. */
const char * _verbatim_declarations;
const char * _verbatim_declarations_end;
unsigned int _verbatim_declarations_lineno;
/* The C code from the end of the file. */
const char * _verbatim_code;
const char * _verbatim_code_end;
unsigned int _verbatim_code_lineno;
/* Declaration of struct type for a keyword and its attributes. */
const char * _struct_decl;
unsigned int _struct_decl_lineno;
/* Return type of the lookup function. */
const char * _return_type;
/* Shorthand for user-defined struct tag type. */
const char * _struct_tag;
/* List of all keywords. */
Keyword_List<KT> * _head;
/* Whether the keyword chars would have different values in a different
character set. */
bool _charset_dependent;
};
#endif

View File

@@ -1,6 +1,6 @@
/* Keyword list.
Copyright (C) 2002-2003 Free Software Foundation, Inc.
Copyright (C) 2002-2003, 2025 Free Software Foundation, Inc.
Written by Bruno Haible <bruno@clisp.org>.
This file is part of GNU GPERF.
@@ -23,145 +23,130 @@
#include <stddef.h>
/* -------------------------- Keyword_List class --------------------------- */
/* ------------------------ Keyword_List<KT> class ------------------------- */
/* Constructor. */
Keyword_List::Keyword_List (Keyword *car)
: _cdr (NULL), _car (car)
{
}
template <class KT>
Keyword_List<KT>::Keyword_List (KT *car)
: _cdr (NULL), _car (car)
{
}
/* ------------------------- KeywordExt_List class ------------------------- */
/* Constructor. */
KeywordExt_List::KeywordExt_List (KeywordExt *car)
: Keyword_List (car)
{
}
/* ------------------------ Keyword_List functions ------------------------- */
/* ---------------------- Keyword_List<KT> functions ----------------------- */
/* Copies a linear list, sharing the list elements. */
Keyword_List *
copy_list (Keyword_List *list)
{
Keyword_List *result;
Keyword_List **lastp = &result;
while (list != NULL)
{
Keyword_List *new_cons = new Keyword_List (list->first());
*lastp = new_cons;
lastp = &new_cons->rest();
list = list->rest();
}
*lastp = NULL;
return result;
}
/* Copies a linear list, sharing the list elements. */
KeywordExt_List *
copy_list (KeywordExt_List *list)
{
return static_cast<KeywordExt_List *> (copy_list (static_cast<Keyword_List *> (list)));
}
template <class KT>
Keyword_List<KT> *
copy_list (Keyword_List<KT> *list)
{
Keyword_List<KT> *result;
Keyword_List<KT> **lastp = &result;
while (list != NULL)
{
Keyword_List<KT> *new_cons = new Keyword_List<KT> (list->first());
*lastp = new_cons;
lastp = &new_cons->rest();
list = list->rest();
}
*lastp = NULL;
return result;
}
/* Deletes a linear list, keeping the list elements in memory. */
void
delete_list (Keyword_List *list)
{
while (list != NULL)
{
Keyword_List *rest = list->rest();
delete list;
list = rest;
}
}
template <class KT>
void
delete_list (Keyword_List<KT> *list)
{
while (list != NULL)
{
Keyword_List<KT> *rest = list->rest();
delete list;
list = rest;
}
}
/* Type of a comparison function. */
typedef bool (*Keyword_Comparison) (Keyword *keyword1, Keyword *keyword2);
#if 0 /* wishful thinking */
template <class KT>
typedef bool (*Keyword_Comparison) (KT *keyword1, KT *keyword2);
#endif
/* Merges two sorted lists together to form one sorted list. */
static Keyword_List *
merge (Keyword_List *list1, Keyword_List *list2, Keyword_Comparison less)
{
Keyword_List *result;
Keyword_List **resultp = &result;
for (;;)
{
if (!list1)
{
*resultp = list2;
break;
}
if (!list2)
{
*resultp = list1;
break;
}
if (less (list2->first(), list1->first()))
{
*resultp = list2;
resultp = &list2->rest();
/* We would have a stable sorting if the next line would read:
list2 = *resultp; */
list2 = list1; list1 = *resultp;
}
else
{
*resultp = list1;
resultp = &list1->rest();
list1 = *resultp;
}
}
return result;
}
template <class KT>
static Keyword_List<KT> *
merge (Keyword_List<KT> *list1, Keyword_List<KT> *list2,
bool (*less) (KT *keyword1, KT *keyword2))
{
Keyword_List<KT> *result;
Keyword_List<KT> **resultp = &result;
for (;;)
{
if (!list1)
{
*resultp = list2;
break;
}
if (!list2)
{
*resultp = list1;
break;
}
if (less (list2->first(), list1->first()))
{
*resultp = list2;
resultp = &list2->rest();
/* We would have a stable sorting if the next line would read:
list2 = *resultp; */
list2 = list1; list1 = *resultp;
}
else
{
*resultp = list1;
resultp = &list1->rest();
list1 = *resultp;
}
}
return result;
}
/* Sorts a linear list, given a comparison function.
Note: This uses a variant of mergesort that is *not* a stable sorting
algorithm. */
Keyword_List *
mergesort_list (Keyword_List *list, Keyword_Comparison less)
{
if (list == NULL || list->rest() == NULL)
/* List of length 0 or 1. Nothing to do. */
return list;
else
{
/* Determine a list node in the middle. */
Keyword_List *middle = list;
for (Keyword_List *temp = list->rest();;)
{
temp = temp->rest();
if (temp == NULL)
break;
temp = temp->rest();
middle = middle->rest();
if (temp == NULL)
break;
}
template <class KT>
Keyword_List<KT> *
mergesort_list (Keyword_List<KT> *list,
bool (*less) (KT *keyword1, KT *keyword2))
{
if (list == NULL || list->rest() == NULL)
/* List of length 0 or 1. Nothing to do. */
return list;
else
{
/* Determine a list node in the middle. */
Keyword_List<KT> *middle = list;
for (Keyword_List<KT> *temp = list->rest();;)
{
temp = temp->rest();
if (temp == NULL)
break;
temp = temp->rest();
middle = middle->rest();
if (temp == NULL)
break;
}
/* Cut the list into two halves.
If the list has n elements, the left half has ceiling(n/2) elements
and the right half has floor(n/2) elements. */
Keyword_List *right_half = middle->rest();
middle->rest() = NULL;
/* Cut the list into two halves.
If the list has n elements, the left half has ceiling(n/2) elements
and the right half has floor(n/2) elements. */
Keyword_List<KT> *right_half = middle->rest();
middle->rest() = NULL;
/* Sort the two halves, then merge them. */
return merge (mergesort_list (list, less),
mergesort_list (right_half, less),
less);
}
}
KeywordExt_List *
mergesort_list (KeywordExt_List *list,
bool (*less) (KeywordExt *keyword1, KeywordExt *keyword2))
{
return
static_cast<KeywordExt_List *>
(mergesort_list (static_cast<Keyword_List *> (list),
reinterpret_cast<Keyword_Comparison> (less)));
}
/* Sort the two halves, then merge them. */
return merge (mergesort_list (list, less),
mergesort_list (right_half, less),
less);
}
}
#ifndef __OPTIMIZE__
@@ -171,3 +156,27 @@ mergesort_list (KeywordExt_List *list,
#undef INLINE
#endif /* not defined __OPTIMIZE__ */
/* ------------------------------------------------------------------------- */
/* Explicit template instantiations. Needed to avoid link-time errors.
C++ is just misdesigned: The most important aspect in building large
software packages is information hiding. (That's the point of having the
implementation of a .h file in a .cc file, isn't it? And of having
classes with private fields and methods, isn't it?) The fact that we
need the instantiation of the Keyword_List<KT> class and associate functions
only for KT = KeywordExt comes from the code in main.cc. It is ugly that
implementation details of main.cc have an influence into this file here. */
template class Keyword_List<KeywordExt>;
template Keyword_List<KeywordExt> *
copy_list<KeywordExt> (Keyword_List<KeywordExt> *list);
template void
delete_list<KeywordExt> (Keyword_List<KeywordExt> *list);
template Keyword_List<KeywordExt> *
mergesort_list<KeywordExt> (Keyword_List<KeywordExt> *list,
bool (*less) (KeywordExt *keyword1,
KeywordExt *keyword2));
/* ------------------------------------------------------------------------- */

View File

@@ -2,7 +2,7 @@
/* Keyword list.
Copyright (C) 2002-2003 Free Software Foundation, Inc.
Copyright (C) 2002-2003, 2025 Free Software Foundation, Inc.
Written by Bruno Haible <bruno@clisp.org>.
This file is part of GNU GPERF.
@@ -25,52 +25,42 @@
#include "keyword.h"
/* List node of a linear list of Keyword. */
class Keyword_List
{
public:
/* Constructor. */
Keyword_List (Keyword *car);
/* List node of a linear list of KT, where KT is a subclass of Keyword. */
template <class KT>
class Keyword_List
{
public:
/* Constructor. */
Keyword_List (KT *car);
/* Access to first element of list. */
Keyword * first () const;
/* Access to next element of list. */
Keyword_List *& rest ();
/* Access to first element of list. */
KT * first () const;
/* Access to next element of list. */
Keyword_List<KT> *& rest ();
protected:
Keyword_List * _cdr;
Keyword * const _car;
};
protected:
Keyword_List<KT> * _cdr;
KT * const _car;
};
/* List node of a linear list of KeywordExt. */
class KeywordExt_List : public Keyword_List
{
public:
/* Constructor. */
KeywordExt_List (KeywordExt *car);
/* Access to first element of list. */
KeywordExt * first () const;
/* Access to next element of list. */
KeywordExt_List *& rest ();
};
typedef Keyword_List<KeywordExt> KeywordExt_List;
/* Copies a linear list, sharing the list elements. */
extern Keyword_List * copy_list (Keyword_List *list);
extern KeywordExt_List * copy_list (KeywordExt_List *list);
template <class KT>
extern Keyword_List<KT> * copy_list (Keyword_List<KT> *list);
/* Deletes a linear list, keeping the list elements in memory. */
extern void delete_list (Keyword_List *list);
template <class KT>
extern void delete_list (Keyword_List<KT> *list);
/* Sorts a linear list, given a comparison function.
Note: This uses a variant of mergesort that is *not* a stable sorting
algorithm. */
extern Keyword_List * mergesort_list (Keyword_List *list,
bool (*less) (Keyword *keyword1,
Keyword *keyword2));
extern KeywordExt_List * mergesort_list (KeywordExt_List *list,
bool (*less) (KeywordExt *keyword1,
KeywordExt *keyword2));
template <class KT>
extern Keyword_List<KT> * mergesort_list (Keyword_List<KT> *list,
bool (*less) (KT *keyword1,
KT *keyword2));
#ifdef __OPTIMIZE__

View File

@@ -1,6 +1,6 @@
/* Inline Functions for keyword-list.{h,cc}.
Copyright (C) 2002-2003 Free Software Foundation, Inc.
Copyright (C) 2002-2003, 2025 Free Software Foundation, Inc.
Written by Bruno Haible <bruno@clisp.org>.
This file is part of GNU GPERF.
@@ -18,34 +18,20 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* -------------------------- Keyword_List class --------------------------- */
/* ------------------------ Keyword_List<KT> class ------------------------- */
/* Access to first element of list. */
INLINE Keyword *
Keyword_List::first () const
{
return _car;
}
template <class KT>
INLINE KT *
Keyword_List<KT>::first () const
{
return _car;
}
/* Access to next element of list. */
INLINE Keyword_List *&
Keyword_List::rest ()
{
return _cdr;
}
/* ------------------------- KeywordExt_List class ------------------------- */
/* Access to first element of list. */
INLINE KeywordExt *
KeywordExt_List::first () const
{
return static_cast<KeywordExt*>(_car);
}
/* Access to next element of list. */
INLINE KeywordExt_List *&
KeywordExt_List::rest ()
{
return *reinterpret_cast<KeywordExt_List**>(&_cdr);
}
template <class KT>
INLINE Keyword_List<KT> *&
Keyword_List<KT>::rest ()
{
return _cdr;
}

View File

@@ -1,5 +1,5 @@
/* Keyword data.
Copyright (C) 1989-1998, 2000, 2002-2003 Free Software Foundation, Inc.
Copyright (C) 1989-1998, 2000, 2002-2003, 2025 Free Software Foundation, Inc.
Written by Douglas C. Schmidt <schmidt@ics.uci.edu>
and Bruno Haible <bruno@clisp.org>.
@@ -136,13 +136,15 @@ KeywordExt::delete_selchars ()
/* ------------------------- Keyword_Factory class ------------------------- */
Keyword_Factory::Keyword_Factory ()
{
}
template <class KT>
Keyword_Factory<KT>::Keyword_Factory ()
{
}
Keyword_Factory::~Keyword_Factory ()
{
}
template <class KT>
Keyword_Factory<KT>::~Keyword_Factory ()
{
}
/* ------------------------------------------------------------------------- */
@@ -157,3 +159,19 @@ char empty_string[1] = "";
#undef INLINE
#endif /* not defined __OPTIMIZE__ */
/* ------------------------------------------------------------------------- */
/* Explicit template instantiations. Needed to avoid link-time errors.
C++ is just misdesigned: The most important aspect in building large
software packages is information hiding. (That's the point of having the
implementation of a .h file in a .cc file, isn't it? And of having
classes with private fields and methods, isn't it?) The fact that we
need the instantiation of the Keyword_Factory<KT> class only for
KT = KeywordExt comes from the code in main.cc. It is ugly that
implementation details of main.cc have an influence into this file here. */
template class Keyword_Factory<KeywordExt>;
/* ------------------------------------------------------------------------- */

View File

@@ -2,7 +2,7 @@
/* Keyword data.
Copyright (C) 1989-1998, 2000, 2002-2003, 2017 Free Software Foundation, Inc.
Copyright (C) 1989-1998, 2000, 2002-2003, 2017, 2025 Free Software Foundation, Inc.
Written by Douglas C. Schmidt <schmidt@ics.uci.edu>
and Bruno Haible <bruno@clisp.org>.
@@ -86,19 +86,20 @@ private:
This factory is used to make the Input class independent of the concrete
class KeywordExt. */
class Keyword_Factory
{
public:
/* Constructor. */
Keyword_Factory ();
/* Destructor. */
virtual ~Keyword_Factory ();
template <class KT>
class Keyword_Factory
{
public:
/* Constructor. */
Keyword_Factory ();
/* Destructor. */
virtual ~Keyword_Factory ();
/* Creates a new Keyword. */
virtual /*abstract*/ Keyword *
create_keyword (const char *allchars, int allchars_length,
const char *rest, unsigned int lineno) = 0;
};
/* Creates a new Keyword. */
virtual /*abstract*/ KT *
create_keyword (const char *allchars, int allchars_length,
const char *rest, unsigned int lineno);
};
/* A statically allocated empty string. */
extern char empty_string[1];

View File

@@ -1,5 +1,5 @@
/* Driver program for the hash function generator
Copyright (C) 1989-1998, 2000, 2002-2003, 2009, 2017 Free Software Foundation, Inc.
Copyright (C) 1989-1998, 2000, 2002-2003, 2009, 2017, 2025 Free Software Foundation, Inc.
Written by Douglas C. Schmidt <schmidt@ics.uci.edu>
and Bruno Haible <bruno@clisp.org>.
@@ -31,15 +31,12 @@
/* This Keyword factory produces KeywordExt instances. */
class KeywordExt_Factory : public Keyword_Factory
{
virtual Keyword * create_keyword (const char *allchars, int allchars_length,
const char *rest, unsigned int lineno);
};
typedef Keyword_Factory<KeywordExt> KeywordExt_Factory;
Keyword *
KeywordExt_Factory::create_keyword (const char *allchars, int allchars_length,
const char *rest, unsigned int lineno)
template <>
KeywordExt *
Keyword_Factory<KeywordExt>::create_keyword (const char *allchars, int allchars_length,
const char *rest, unsigned int lineno)
{
return new KeywordExt (allchars, allchars_length, rest, lineno);
}
@@ -66,11 +63,11 @@ main (int argc, char *argv[])
{
/* Initialize the keyword list. */
KeywordExt_Factory factory;
Input inputter (stdin, &factory);
Input<KeywordExt> inputter (stdin, &factory);
inputter.read_input ();
/* We can cast the keyword list to KeywordExt_List* because its list
elements were created by KeywordExt_Factory. */
KeywordExt_List* list = static_cast<KeywordExt_List*>(inputter._head);
KeywordExt_List* list = inputter._head;
{
/* Search for a good hash function. */