eolian: add API to retrieve filename for each typedef and struct

This commit is contained in:
Daniel Kolesa 2014-07-21 12:03:51 +01:00
parent 6caf41e288
commit c4fd68f08a
6 changed files with 70 additions and 4 deletions

View File

@ -779,6 +779,16 @@ EAPI const Eolian_Type *eolian_type_find_by_alias(const char *alias);
*/
EAPI Eina_Bool eolian_typedef_is_extern(const char *alias);
/*
* @brief Find the filename for a certain alias
*
* @param[in] alias alias of the type definition
* @return the filename or NULL if @c alias is not found
*
* @ingroup Eolian
*/
EAPI const Eina_Stringshare *eolian_typedef_file_get(const char *alias);
/*
* @brief Find a struct by name
*
@ -865,6 +875,16 @@ EAPI Eina_Stringshare *eolian_type_struct_field_description_get(const Eolian_Typ
*/
EAPI Eina_Stringshare *eolian_type_struct_description_get(const Eolian_Type *tp);
/*
* @brief Get the filename of a struct type.
*
* @param[in] tp the type.
* @return the filename when @c tp is EOLIAN_TYPE_STRUCT, NULL otherwise.
*
* @ingroup Eolian
*/
EAPI Eina_Stringshare *eolian_type_struct_file_get(const Eolian_Type *tp);
/*
* @brief Get the return type of a function type.
*

View File

@ -11,6 +11,7 @@ database_type_del(Eolian_Type *tp)
if (tp->type == EOLIAN_TYPE_STRUCT)
{
eina_hash_free(tp->fields);
eina_stringshare_del(tp->file);
free(tp);
return;
}
@ -27,6 +28,7 @@ database_typedef_del(Eolian_Typedef *def)
{
if (!def) return;
eina_stringshare_del(def->alias);
eina_stringshare_del(def->file);
/* prevent deletion of named structs: stored in another hash */
if (def->type->type != EOLIAN_TYPE_STRUCT || !def->type->name)
database_type_del(def->type);

View File

@ -7,9 +7,9 @@ eolian_type_find_by_alias(const char *alias)
{
if (!_types) return NULL;
Eina_Stringshare *shr = eina_stringshare_add(alias);
Eolian_Typedef *cl = eina_hash_find(_types, shr);
Eolian_Typedef *def = eina_hash_find(_types, shr);
eina_stringshare_del(shr);
return cl ? cl->type : NULL;
return def ? def->type : NULL;
}
EAPI Eina_Bool
@ -17,11 +17,20 @@ eolian_typedef_is_extern(const char *alias)
{
if (!_types) return EINA_FALSE;
Eina_Stringshare *shr = eina_stringshare_add(alias);
Eolian_Typedef *cl = eina_hash_find(_types, shr);
Eolian_Typedef *def = eina_hash_find(_types, shr);
eina_stringshare_del(shr);
return cl ? cl->is_extern : EINA_FALSE;
return def ? def->is_extern : EINA_FALSE;
}
EAPI Eina_Stringshare *
eolian_typedef_file_get(const char *alias)
{
if (!_types) return EINA_FALSE;
Eina_Stringshare *shr = eina_stringshare_add(alias);
Eolian_Typedef *def = eina_hash_find(_types, shr);
eina_stringshare_del(shr);
return def ? eina_stringshare_ref(def->file) : NULL;
}
EAPI const Eolian_Type *
eolian_type_struct_find_by_name(const char *name)
@ -102,6 +111,14 @@ eolian_type_struct_description_get(const Eolian_Type *tp)
return tp->comment;
}
EAPI Eina_Stringshare *
eolian_type_struct_file_get(const Eolian_Type *tp)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(tp, NULL);
EINA_SAFETY_ON_FALSE_RETURN_VAL(tp->type == EOLIAN_TYPE_STRUCT, NULL);
return eina_stringshare_ref(tp->file);
}
EAPI const Eolian_Type *
eolian_type_return_type_get(const Eolian_Type *tp)
{

View File

@ -123,6 +123,16 @@ append_node(Eo_Lexer *ls, int type, void *def)
ls->nodes = eina_list_append(ls->nodes, nd);
}
static const char *
get_filename(Eo_Lexer *ls)
{
Eina_Array *arr = eina_file_split(strdup(ls->source));
const char *file = eina_stringshare_add(eina_array_data_get(arr,
eina_array_count_get(arr) - 1));
eina_array_free(arr);
return file;
}
static Eina_Strbuf *
parse_name(Eo_Lexer *ls, Eina_Strbuf *buf)
{
@ -234,6 +244,7 @@ parse_struct(Eo_Lexer *ls, const char *name, Eina_Bool is_extern)
int line = ls->line_number, column = ls->column;
Eolian_Type *def = push_type(ls);
def->is_extern = is_extern;
def->file = get_filename(ls);
def->name = name;
def->type = EOLIAN_TYPE_STRUCT;
def->fields = eina_hash_string_small_new(EINA_FREE_CB(_struct_field_free));
@ -421,6 +432,7 @@ parse_typedef(Eo_Lexer *ls)
if (eina_hash_find(_types, ls->t.value))
eo_lexer_syntax_error(ls, "typedef redefinition");
ls->tmp.typedef_def->alias = eina_stringshare_ref(ls->t.value);
ls->tmp.typedef_def->file = get_filename(ls);
eo_lexer_get(ls);
(void)!!test_next(ls, ':');
ls->tmp.typedef_def->type = parse_type_struct_nonvoid(ls, EINA_TRUE,

View File

@ -112,6 +112,7 @@ struct _Eolian_Type
struct {
Eina_Hash *fields;
Eina_Stringshare *comment;
Eina_Stringshare *file;
};
};
Eina_Bool is_const :1;
@ -134,6 +135,7 @@ struct _Eolian_Event
typedef struct _Eolian_Typedef
{
Eina_Stringshare *alias;
Eina_Stringshare *file;
Eolian_Type *type;
Eina_Bool is_extern :1;
} Eolian_Typedef;

View File

@ -225,6 +225,7 @@ START_TEST(eolian_typedef)
const char *type_name = NULL;
Eina_Iterator *iter = NULL;
const Eolian_Class *class;
const char *file;
eolian_init();
/* Parsing */
@ -243,6 +244,11 @@ START_TEST(eolian_typedef)
fail_if(strcmp(type_name, "int"));
eina_stringshare_del(type_name);
/* File */
fail_if(!(file = eolian_typedef_file_get("Evas_Coord")));
fail_if(strcmp(file, "typedef.eo"));
eina_stringshare_del(file);
/* Complex type */
fail_if(!(type = eolian_type_find_by_alias("List_Objects")));
fail_if(!(type_name = eolian_type_c_type_get(type)));
@ -488,6 +494,7 @@ START_TEST(eolian_struct)
const Eolian_Type *type = NULL, *field = NULL;
const Eolian_Class *class;
const char *type_name;
const char *file;
eolian_init();
@ -501,11 +508,14 @@ START_TEST(eolian_struct)
/* named struct */
fail_if(!(type = eolian_type_struct_find_by_name("Named")));
fail_if(!(type_name = eolian_type_name_get(type)));
fail_if(!(file = eolian_type_struct_file_get(type)));
fail_if(eolian_type_type_get(type) != EOLIAN_TYPE_STRUCT);
fail_if(eolian_type_is_own(type));
fail_if(eolian_type_is_const(type));
fail_if(strcmp(type_name, "Named"));
eina_stringshare_del(type_name);
fail_if(strcmp(file, "struct.eo"));
eina_stringshare_del(file);
fail_if(!(field = eolian_type_struct_field_get(type, "field")));
fail_if(!(type_name = eolian_type_name_get(field)));
fail_if(strcmp(type_name, "int"));
@ -518,9 +528,12 @@ START_TEST(eolian_struct)
/* referencing */
fail_if(!(type = eolian_type_struct_find_by_name("Another")));
fail_if(!(type_name = eolian_type_name_get(type)));
fail_if(!(file = eolian_type_struct_file_get(type)));
fail_if(eolian_type_type_get(type) != EOLIAN_TYPE_STRUCT);
fail_if(strcmp(type_name, "Another"));
eina_stringshare_del(type_name);
fail_if(strcmp(file, "struct.eo"));
eina_stringshare_del(file);
fail_if(!(field = eolian_type_struct_field_get(type, "field")));
fail_if(!(type_name = eolian_type_name_get(field)));
fail_if(strcmp(type_name, "Named"));