Skip to content

Commit

Permalink
Separated idl to c code generator off into own library (#1752)
Browse files Browse the repository at this point in the history
* Separated idl to c code generator off into own library

The C code generator is now no longer "included" in idlc, but
inside its own library just like all other code generators
This required the generation of extra export headers in addition
to the restructuring of the code
Some of the non-C generators will need to be modified slightly
and include the C generator library, as this now includes the
type_meta information generation functions

Signed-off-by: Martijn Reicher <[email protected]>

* Fixes for CodeQL requirements

Made function implementations exactly match their definitions
Changed function parameter names to prevent any possible shadowing
of static variables

Signed-off-by: Martijn Reicher <[email protected]>

* Further restructuring

Moved and renamed libidlc files to their own folder
Moved and renamed generator_common files to their own folder
Moved idlc files to their own folder

Signed-off-by: Martijn Reicher <[email protected]>

* Merged idlc_common library into idl

After a suggestion from @eboasson moved the functions (name
generation, typeinfo blobs) which will be shared between different
language bindings (C, C++, python, ...) into the idl library
This made the idl_common library superfluous, so that was deleted

Signed-off-by: Martijn Reicher <[email protected]>

* Renamed files

Renamed idlcs plugin.* files to generator.* to keep it compatible
with all other language bindings.
The contents of these files was previously split between other files
and the generator file was removed, by doing this other bindings
(C++/python/???) should not require any modifications.

Signed-off-by: Martijn Reicher <[email protected]>

* Fix declaration/definition mismatch

Signed-off-by: Erik Boasson <[email protected]>

* Clean up IDLC header guards

Signed-off-by: Erik Boasson <[email protected]>

---------

Signed-off-by: Martijn Reicher <[email protected]>
Signed-off-by: Erik Boasson <[email protected]>
Co-authored-by: Erik Boasson <[email protected]>
  • Loading branch information
reicheratwork and eboasson authored Aug 10, 2023
1 parent bde2009 commit d6cdd36
Show file tree
Hide file tree
Showing 28 changed files with 437 additions and 392 deletions.
7 changes: 6 additions & 1 deletion cmake/Modules/Generate.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ function(IDLC_GENERATE)
cmake_parse_arguments(
IDLC "${options}" "${one_value_keywords}" "${multi_value_keywords}" "" ${ARGN})

set(_idlc_shared_lib "$<TARGET_FILE:CycloneDDS::libidlc>")
set(_idlc_depends CycloneDDS::libidlc)

set(gen_args
BACKEND ${_idlc_shared_lib}
${IDLC_UNPARSED_ARGUMENTS}
TARGET ${IDLC_TARGET}
BASE_DIR ${IDLC_BASE_DIR}
Expand All @@ -26,7 +30,8 @@ function(IDLC_GENERATE)
INCLUDES ${IDLC_INCLUDES}
WARNINGS ${IDLC_WARNINGS}
OUTPUT_DIR ${IDLC_OUTPUT_DIR}
DEFAULT_EXTENSIBILITY ${IDLC_DEFAULT_EXTENSIBILITY})
DEFAULT_EXTENSIBILITY ${IDLC_DEFAULT_EXTENSIBILITY}
DEPENDS ${_idlc_depends})
if(${IDLC_NO_TYPE_INFO})
list(APPEND gen_args NO_TYPE_INFO)
endif()
Expand Down
11 changes: 10 additions & 1 deletion src/idl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ set(sources
src/tree.h
src/keylist.h)

if(ENABLE_TYPELIB)
list(APPEND headers include/idl/descriptor_type_meta.h)
list(APPEND sources src/descriptor_type_meta.c)
endif()

# import generic utility sources from ddsrt to avoid duplication
# NOTE: an additional INTERFACE library defined in ddsrt would be ideal, but
# the GENERATED property is not propagated before 3.20. using an OBJECT
Expand Down Expand Up @@ -157,6 +162,7 @@ set_target_properties(

generate_export_header(idl EXPORT_FILE_NAME include/idl/export.h)

target_link_libraries(idl PUBLIC ddsc)

target_include_directories(
idl
Expand All @@ -166,7 +172,10 @@ target_include_directories(
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>"
INTERFACE
"$<INSTALL_INTERFACE:include>")
"$<INSTALL_INTERFACE:include>"
PRIVATE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../core/ddsi/include>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../core/cdr/include>")

target_link_libraries(idl PRIVATE Threads::Threads)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@
//
// SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause

#ifndef DESCRIPTOR_TYPE_META_H
#define DESCRIPTOR_TYPE_META_H
#ifndef IDL_DESCRIPTOR_TYPE_META_H
#define IDL_DESCRIPTOR_TYPE_META_H

#include "generator.h"
#include "dds/ddsi/ddsi_xt_typeinfo.h"
#include "dds/ddsi/ddsi_xt_typemap.h"

#include "idl/export.h"
#include "idl/processor.h"
#include "idl/print.h"

#if defined (__cplusplus)
extern "C" {
#endif

struct type_meta {
bool finalized;
Expand All @@ -31,29 +39,33 @@ struct descriptor_type_meta {
struct type_meta *stack;
};

idl_retcode_t
IDL_EXPORT idl_retcode_t
get_type_hash (DDS_XTypes_EquivalenceHash hash, const DDS_XTypes_TypeObject *to);

idl_retcode_t
IDL_EXPORT idl_retcode_t
print_type_meta_ser (
FILE *fp,
const idl_pstate_t *state,
const idl_node_t *node);

IDL_EXPORT idl_retcode_t
generate_type_meta_ser (
const idl_pstate_t *state,
const idl_node_t *node,
idl_typeinfo_typemap_t *result);

IDL_EXPORT idl_retcode_t
generate_descriptor_type_meta (
const idl_pstate_t *pstate,
const idl_node_t *node,
struct descriptor_type_meta *dtm);

void
IDL_EXPORT void
descriptor_type_meta_fini (
struct descriptor_type_meta *dtm);

idl_retcode_t
print_type_meta_ser (
FILE *fp,
const idl_pstate_t *pstate,
const idl_node_t *node);

idl_retcode_t
generate_type_meta_ser (
const idl_pstate_t *pstate,
const idl_node_t *node,
idl_typeinfo_typemap_t *result);
#if defined(__cplusplus)
}
#endif

#endif /* DESCRIPTOR_TYPE_META_H */
#endif /* IDL_DESCRIPTOR_TYPE_META_H */
7 changes: 7 additions & 0 deletions src/idl/include/idl/print.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#endif

#include "idl/export.h"
#include "processor.h"

typedef int(*idl_print_t)(char *, size_t, const void *, void *);

Expand Down Expand Up @@ -77,4 +78,10 @@ IDL_EXPORT int idl_print__(
#define IDL_PRINT(strp_, print_, ...) \
IDL_PRINT__(strp_, print_, __VA_ARGS__, )

IDL_EXPORT int
print_type(char *str, size_t size, const void *ptr, void *user_data);

IDL_EXPORT int
print_scoped_name(char *str, size_t size, const void *ptr, void *user_data);

#endif /* IDL_PRINT_H */
6 changes: 3 additions & 3 deletions src/idl/include/idl/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
//
// SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause

#ifndef VECTOR_H
#define VECTOR_H
#ifndef IDL_VECTOR_H
#define IDL_VECTOR_H

#include <stddef.h>
#include <stdbool.h>
Expand All @@ -33,4 +33,4 @@ const void *idl_boxed_vector_next_c (struct idl_boxed_vector_iter *it);
const void *idl_boxed_vector_first_c (const struct idl_boxed_vector *v, struct idl_boxed_vector_iter *it);
void idl_boxed_vector_sort (struct idl_boxed_vector *v, int (*cmp) (const void *a, const void *b));

#endif
#endif /* IDL_VECTOR_H */
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <string.h>
#include <inttypes.h>

#include "dds/ddsrt/md5.h"
#include "idl/md5.h"
#include "dds/ddsi/ddsi_serdata.h"
#include "dds/ddsi/ddsi_typewrap.h"
#include "dds/ddsi/ddsi_xt_typeinfo.h"
Expand All @@ -33,8 +33,7 @@
#include "idl/stream.h"
#include "idl/string.h"
#include "idl/misc.h"
#include "generator.h"
#include "descriptor_type_meta.h"
#include "idl/descriptor_type_meta.h"

static struct dds_cdrstream_allocator idlc_cdrstream_default_allocator = { idl_malloc, idl_realloc, idl_free };

Expand Down Expand Up @@ -337,10 +336,10 @@ get_type_hash (DDS_XTypes_EquivalenceHash hash, const DDS_XTypes_TypeObject *to)

// get md5 of serialized cdr and store first 14 bytes in equivalence hash parameter
char buf[16];
ddsrt_md5_state_t md5st;
ddsrt_md5_init (&md5st);
ddsrt_md5_append (&md5st, (ddsrt_md5_byte_t *) os.m_buffer, os.m_index);
ddsrt_md5_finish (&md5st, (ddsrt_md5_byte_t *) buf);
idl_md5_state_t md5st;
idl_md5_init (&md5st);
idl_md5_append (&md5st, (idl_md5_byte_t *) os.m_buffer, os.m_index);
idl_md5_finish (&md5st, (idl_md5_byte_t *) buf);
memcpy (hash, buf, sizeof(DDS_XTypes_EquivalenceHash));
dds_ostream_fini (&os, &idlc_cdrstream_default_allocator);
return IDL_RETCODE_OK;
Expand Down Expand Up @@ -381,10 +380,10 @@ get_namehash (DDS_XTypes_NameHash name_hash, const char *name)
{
/* FIXME: multi byte utf8 chars? */
char buf[16];
ddsrt_md5_state_t md5st;
ddsrt_md5_init (&md5st);
ddsrt_md5_append (&md5st, (ddsrt_md5_byte_t *) name, (uint32_t) strlen (name));
ddsrt_md5_finish (&md5st, (ddsrt_md5_byte_t *) buf);
idl_md5_state_t md5st;
idl_md5_init (&md5st);
idl_md5_append (&md5st, (idl_md5_byte_t *) name, (uint32_t) strlen (name));
idl_md5_finish (&md5st, (idl_md5_byte_t *) buf);
memcpy (name_hash, buf, sizeof (DDS_XTypes_NameHash));
}

Expand Down Expand Up @@ -1804,7 +1803,7 @@ generate_type_meta_ser_impl (
idl_retcode_t
print_type_meta_ser (
FILE *fp,
const idl_pstate_t *pstate,
const idl_pstate_t *state,
const idl_node_t *node)
{
struct DDS_XTypes_TypeInformation type_information;
Expand All @@ -1816,7 +1815,7 @@ print_type_meta_ser (
if (IDL_PRINTA(&type_name, print_type, node) < 0)
return IDL_RETCODE_NO_MEMORY;

if ((rc = generate_type_meta_ser_impl (pstate, node, &type_information, &os_typeinfo, &os_typemap)))
if ((rc = generate_type_meta_ser_impl (state, node, &type_information, &os_typeinfo, &os_typemap)))
return rc;

if ((rc = print_typeinformation_comment (fp, &type_information)) != IDL_RETCODE_OK)
Expand All @@ -1833,7 +1832,7 @@ print_type_meta_ser (

idl_retcode_t
generate_type_meta_ser (
const idl_pstate_t *pstate,
const idl_pstate_t *state,
const idl_node_t *node,
idl_typeinfo_typemap_t *result)
{
Expand All @@ -1842,7 +1841,7 @@ generate_type_meta_ser (
dds_ostream_t os_typemap;
idl_retcode_t rc;

if ((rc = generate_type_meta_ser_impl (pstate, node, &type_information, &os_typeinfo, &os_typemap)))
if ((rc = generate_type_meta_ser_impl (state, node, &type_information, &os_typeinfo, &os_typemap)))
return rc;

result->typeinfo = NULL;
Expand Down
Loading

0 comments on commit d6cdd36

Please sign in to comment.