eolian: add support for opaque struct types (+ tests)

This commit is contained in:
Daniel Kolesa 2014-08-13 16:43:18 +01:00
parent 2df5eae338
commit 1688749ce9
8 changed files with 48 additions and 10 deletions

View File

@ -69,10 +69,11 @@ _type_generate(const Eolian_Type *tp, Eina_Bool in_typedef)
break; break;
} }
case EOLIAN_TYPE_STRUCT: case EOLIAN_TYPE_STRUCT:
case EOLIAN_TYPE_STRUCT_OPAQUE:
{ {
const char *member_name; const char *member_name;
char *name = _concat_name(tp); char *name = _concat_name(tp);
if (in_typedef && name) if ((in_typedef && name) || tp_type == EOLIAN_TYPE_STRUCT_OPAQUE)
{ {
eina_strbuf_append_printf(buf, "struct %s", name); eina_strbuf_append_printf(buf, "struct %s", name);
free(name); free(name);

View File

@ -136,6 +136,7 @@ typedef enum
EOLIAN_TYPE_POINTER, EOLIAN_TYPE_POINTER,
EOLIAN_TYPE_FUNCTION, EOLIAN_TYPE_FUNCTION,
EOLIAN_TYPE_STRUCT, EOLIAN_TYPE_STRUCT,
EOLIAN_TYPE_STRUCT_OPAQUE,
EOLIAN_TYPE_ENUM, EOLIAN_TYPE_ENUM,
EOLIAN_TYPE_ALIAS, EOLIAN_TYPE_ALIAS,
EOLIAN_TYPE_CLASS EOLIAN_TYPE_CLASS
@ -1143,7 +1144,8 @@ EAPI Eina_Stringshare *eolian_type_enum_legacy_prefix_get(const Eolian_Type *tp)
* @brief Get the description of a struct/alias type. * @brief Get the description of a struct/alias type.
* *
* @param[in] tp the type. * @param[in] tp the type.
* @return the description when @c tp is EOLIAN_TYPE_STRUCT, NULL otherwise. * @return the description when @c tp is EOLIAN_TYPE_STRUCT or
* EOLIAN_TYPE_STRUCT_OPAQUE, NULL otherwise.
* *
* @ingroup Eolian * @ingroup Eolian
*/ */
@ -1153,7 +1155,7 @@ EAPI Eina_Stringshare *eolian_type_description_get(const Eolian_Type *tp);
* @brief Get the filename of a struct/alias type. * @brief Get the filename of a struct/alias type.
* *
* @param[in] tp the type. * @param[in] tp the type.
* @return the filename when @c tp is EOLIAN_TYPE_STRUCT, NULL otherwise. * @return the filename.
* *
* @ingroup Eolian * @ingroup Eolian
*/ */
@ -1261,10 +1263,10 @@ EAPI Eina_Stringshare *eolian_type_c_type_get(const Eolian_Type *tp);
/* /*
* @brief Get the name of the given type. You have to manually delete * @brief Get the name of the given type. You have to manually delete
* the stringshare. For EOLIAN_TYPE_REGULAR and EOLIAN_TYPE_REGULAR_STRUCT, * the stringshare. For EOLIAN_TYPE_REGULAR and EOLIAN_TYPE_REGULAR_STRUCT,
* this is for example "int". For EOLIAN_TYPE_STRUCT and EOLIAN_TYPE_ALIAS, * this is for example "int". For EOLIAN_TYPE_STRUCT, EOLIAN_TYPE_STRUCT_OPAQUE
* this is the name of the alias or of the struct. For EOLIAN_TYPE_CLASS, * and EOLIAN_TYPE_ALIAS, this is the name of the alias or of the struct. For
* this can be "Button". Keep in mind that the name doesn't include * EOLIAN_TYPE_CLASS, this can be "Button". Keep in mind that the name doesn't
* namespaces for structs and aliases. * include namespaces for structs and aliases.
* *
* @param[in] tp the type. * @param[in] tp the type.
* @return the name. * @return the name.

View File

@ -34,7 +34,8 @@ database_typedef_del(Eolian_Type *tp)
{ {
if (btp->type == EOLIAN_TYPE_ENUM) if (btp->type == EOLIAN_TYPE_ENUM)
tp->base_type = NULL; tp->base_type = NULL;
else if (btp->type == EOLIAN_TYPE_STRUCT && btp->name) else if ((btp->type == EOLIAN_TYPE_STRUCT
|| btp->type == EOLIAN_TYPE_STRUCT_OPAQUE) && btp->name)
tp->base_type = NULL; tp->base_type = NULL;
} }
database_type_del(tp); database_type_del(tp);
@ -107,6 +108,8 @@ _stype_to_str(const Eolian_Type *tp, Eina_Strbuf *buf, const char *name)
eina_strbuf_append(buf, tp->name); eina_strbuf_append(buf, tp->name);
eina_strbuf_append_char(buf, ' '); eina_strbuf_append_char(buf, ' ');
} }
if (tp->type == EOLIAN_TYPE_STRUCT_OPAQUE)
goto append_name;
eina_strbuf_append(buf, "{ "); eina_strbuf_append(buf, "{ ");
EINA_LIST_FOREACH(tp->field_names, l, fname) EINA_LIST_FOREACH(tp->field_names, l, fname)
{ {
@ -115,6 +118,7 @@ _stype_to_str(const Eolian_Type *tp, Eina_Strbuf *buf, const char *name)
eina_strbuf_append(buf, "; "); eina_strbuf_append(buf, "; ");
} }
eina_strbuf_append(buf, "}"); eina_strbuf_append(buf, "}");
append_name:
if (name) if (name)
{ {
eina_strbuf_append_char(buf, ' '); eina_strbuf_append_char(buf, ' ');
@ -201,7 +205,8 @@ database_type_to_str(const Eolian_Type *tp, Eina_Strbuf *buf, const char *name)
_ftype_to_str(tp, buf, name); _ftype_to_str(tp, buf, name);
return; return;
} }
else if (tp->type == EOLIAN_TYPE_STRUCT) else if (tp->type == EOLIAN_TYPE_STRUCT
|| tp->type == EOLIAN_TYPE_STRUCT_OPAQUE)
{ {
_stype_to_str(tp, buf, name); _stype_to_str(tp, buf, name);
return; return;
@ -299,7 +304,8 @@ database_type_print(Eolian_Type *tp)
printf("%s", tp->full_name); printf("%s", tp->full_name);
else if (tp->type == EOLIAN_TYPE_VOID) else if (tp->type == EOLIAN_TYPE_VOID)
printf("void"); printf("void");
else if (tp->type == EOLIAN_TYPE_REGULAR_STRUCT) else if (tp->type == EOLIAN_TYPE_REGULAR_STRUCT
|| tp->type == EOLIAN_TYPE_STRUCT_OPAQUE)
printf("struct %s", tp->full_name); printf("struct %s", tp->full_name);
else if (tp->type == EOLIAN_TYPE_REGULAR_ENUM) else if (tp->type == EOLIAN_TYPE_REGULAR_ENUM)
printf("enum %s", tp->full_name); printf("enum %s", tp->full_name);

View File

@ -1774,6 +1774,25 @@ parse_unit(Eo_Lexer *ls, Eina_Bool eot)
} }
eo_lexer_context_pop(ls); eo_lexer_context_pop(ls);
pop_strbuf(ls); pop_strbuf(ls);
if (ls->t.token == ';')
{
Eolian_Type *def = push_type(ls);
def->is_extern = is_extern;
def->type = EOLIAN_TYPE_STRUCT_OPAQUE;
_fill_type_name(def, name);
eo_lexer_get(ls);
if (ls->t.token == TOK_COMMENT)
{
def->comment = eina_stringshare_ref(ls->t.value.s);
eo_lexer_get(ls);
}
def->base.file = eina_stringshare_ref(ls->filename);
def->base.line = line;
def->base.column = col;
database_struct_add(def);
pop_type(ls);
break;
}
if (is_enum) if (is_enum)
parse_enum(ls, name, is_extern, line, col); parse_enum(ls, name, is_extern, line, col);
else else

View File

@ -138,6 +138,7 @@ struct _Eolian_Type
Eina_Bool is_const :1; Eina_Bool is_const :1;
Eina_Bool is_own :1; Eina_Bool is_own :1;
Eina_Bool is_extern :1; Eina_Bool is_extern :1;
Eina_Bool is_opaque :1;
}; };
struct _Eolian_Implement struct _Eolian_Implement

View File

@ -19,6 +19,9 @@ type Bar: struct {
b: struct _Foo; b: struct _Foo;
}; };
/* opaque struct */
struct Opaque;
class Struct { class Struct {
methods { methods {
foo { foo {

View File

@ -32,6 +32,8 @@ struct _Foo {
float another; float another;
}; };
struct Opaque;
#endif #endif
#define STRUCT_CLASS struct_class_get() #define STRUCT_CLASS struct_class_get()

View File

@ -606,6 +606,10 @@ START_TEST(eolian_struct)
fail_if(!!(type_name = eolian_type_name_get(type))); fail_if(!!(type_name = eolian_type_name_get(type)));
fail_if(eolian_type_type_get(type) != EOLIAN_TYPE_STRUCT); fail_if(eolian_type_type_get(type) != EOLIAN_TYPE_STRUCT);
/* opaque struct */
fail_if(!(type = eolian_type_struct_get_by_name("Opaque")));
fail_if(eolian_type_type_get(type) != EOLIAN_TYPE_STRUCT_OPAQUE);
eolian_shutdown(); eolian_shutdown();
} }
END_TEST END_TEST