eolian: return Eolian_Object_Type from ref resolver

This needs only one enum and allows for better interop.
This commit is contained in:
Daniel Kolesa 2018-03-09 15:19:53 +01:00
parent edda05e4d0
commit c7fd43ad4e
8 changed files with 32 additions and 96 deletions

View File

@ -276,19 +276,6 @@ ffi.cdef [[
EOLIAN_DOC_TOKEN_MARKUP_MONOSPACE EOLIAN_DOC_TOKEN_MARKUP_MONOSPACE
} Eolian_Doc_Token_Type; } Eolian_Doc_Token_Type;
typedef enum {
EOLIAN_DOC_REF_INVALID = 0,
EOLIAN_DOC_REF_CLASS,
EOLIAN_DOC_REF_FUNC,
EOLIAN_DOC_REF_EVENT,
EOLIAN_DOC_REF_ALIAS,
EOLIAN_DOC_REF_STRUCT,
EOLIAN_DOC_REF_STRUCT_FIELD,
EOLIAN_DOC_REF_ENUM,
EOLIAN_DOC_REF_ENUM_FIELD,
EOLIAN_DOC_REF_VAR
} Eolian_Doc_Ref_Type;
typedef struct _Eolian_Doc_Token { typedef struct _Eolian_Doc_Token {
Eolian_Doc_Token_Type type; Eolian_Doc_Token_Type type;
const char *text, *text_end; const char *text, *text_end;
@ -494,7 +481,7 @@ ffi.cdef [[
void eolian_doc_token_init(Eolian_Doc_Token *tok); void eolian_doc_token_init(Eolian_Doc_Token *tok);
Eolian_Doc_Token_Type eolian_doc_token_type_get(const Eolian_Doc_Token *tok); Eolian_Doc_Token_Type eolian_doc_token_type_get(const Eolian_Doc_Token *tok);
char *eolian_doc_token_text_get(const Eolian_Doc_Token *tok); char *eolian_doc_token_text_get(const Eolian_Doc_Token *tok);
Eolian_Doc_Ref_Type eolian_doc_token_ref_get(const Eolian_Unit *unit, const Eolian_Doc_Token *tok, const void **data, const void **data2); Eolian_Object_Type eolian_doc_token_ref_get(const Eolian_Unit *unit, const Eolian_Doc_Token *tok, const void **data, const void **data2);
]] ]]
local cutil = require("cutil") local cutil = require("cutil")
@ -1815,19 +1802,6 @@ M.doc_token_type = {
MARKUP_MONOSPACE = 7 MARKUP_MONOSPACE = 7
} }
M.doc_ref_type = {
INVALID = 0,
CLASS = 1,
FUNC = 2,
EVENT = 3,
ALIAS = 4,
STRUCT = 5,
STRUCT_FIELD = 6,
ENUM = 7,
ENUM_FIELD = 8,
VAR = 9
}
M.documentation_string_split = function(str) M.documentation_string_split = function(str)
if not str then if not str then
return {} return {}
@ -1881,7 +1855,7 @@ M.Eolian_Doc_Token = ffi.metatype("Eolian_Doc_Token", {
ref_get = function(self, unit) ref_get = function(self, unit)
local stor = ffi.new("const void *[2]") local stor = ffi.new("const void *[2]")
local tp = tonumber(eolian.eolian_doc_token_ref_get(unit, self, stor, stor + 1)) local tp = tonumber(eolian.eolian_doc_token_ref_get(unit, self, stor, stor + 1))
local reft = M.doc_ref_type local reft = M.object_type
if tp == reft.CLASS then if tp == reft.CLASS then
return tp, ffi.cast("const Eolian_Class *", stor[0]) return tp, ffi.cast("const Eolian_Class *", stor[0])
elseif tp == reft.FUNC then elseif tp == reft.FUNC then
@ -1905,7 +1879,7 @@ M.Eolian_Doc_Token = ffi.metatype("Eolian_Doc_Token", {
elseif tp == reft.VAR then elseif tp == reft.VAR then
return tp, ffi.cast("const Eolian_Variable *", stor[0]) return tp, ffi.cast("const Eolian_Variable *", stor[0])
else else
return reft.INVALID return reft.UNKNOWN
end end
end end
} }

View File

@ -449,20 +449,6 @@ typedef enum
EOLIAN_DOC_TOKEN_MARKUP_MONOSPACE EOLIAN_DOC_TOKEN_MARKUP_MONOSPACE
} Eolian_Doc_Token_Type; } Eolian_Doc_Token_Type;
typedef enum
{
EOLIAN_DOC_REF_INVALID = 0,
EOLIAN_DOC_REF_CLASS,
EOLIAN_DOC_REF_FUNC,
EOLIAN_DOC_REF_EVENT,
EOLIAN_DOC_REF_ALIAS,
EOLIAN_DOC_REF_STRUCT,
EOLIAN_DOC_REF_STRUCT_FIELD,
EOLIAN_DOC_REF_ENUM,
EOLIAN_DOC_REF_ENUM_FIELD,
EOLIAN_DOC_REF_VAR
} Eolian_Doc_Ref_Type;
typedef struct _Eolian_Doc_Token typedef struct _Eolian_Doc_Token
{ {
Eolian_Doc_Token_Type type; Eolian_Doc_Token_Type type;
@ -2779,7 +2765,7 @@ EAPI char *eolian_doc_token_text_get(const Eolian_Doc_Token *tok);
/* /*
* @brief Get the thing that a reference token references. * @brief Get the thing that a reference token references.
* *
* Returns EOLIAN_DOC_REF_INVALID on failure (when not ref token or * Returns EOLIAN_OBJECT_UNKNOWN on failure (when not ref token or
* invalid ref, but invalid refs don't happen when database is valid). * invalid ref, but invalid refs don't happen when database is valid).
* *
* When the reference is a class, alias, struct, enum or var, the first data arg * When the reference is a class, alias, struct, enum or var, the first data arg
@ -2794,7 +2780,7 @@ EAPI char *eolian_doc_token_text_get(const Eolian_Doc_Token *tok);
* @param[out] data2 the secondary data * @param[out] data2 the secondary data
* @return the kind of reference this is * @return the kind of reference this is
*/ */
EAPI Eolian_Doc_Ref_Type eolian_doc_token_ref_get(const Eolian_Unit *unit, const Eolian_Doc_Token *tok, const void **data, const void **data2); EAPI Eolian_Object_Type eolian_doc_token_ref_get(const Eolian_Unit *unit, const Eolian_Doc_Token *tok, const void **data, const void **data2);
#endif #endif

View File

@ -35,7 +35,7 @@ _validate_docstr(const Eolian_Unit *src, Eina_Stringshare *str, const Eolian_Obj
eolian_doc_token_init(&tok); eolian_doc_token_init(&tok);
while (ret && (doc = eolian_documentation_tokenize(doc, &tok))) while (ret && (doc = eolian_documentation_tokenize(doc, &tok)))
if (eolian_doc_token_type_get(&tok) == EOLIAN_DOC_TOKEN_REF) if (eolian_doc_token_type_get(&tok) == EOLIAN_DOC_TOKEN_REF)
if (eolian_doc_token_ref_get(src, &tok, NULL, NULL) == EOLIAN_DOC_REF_INVALID) if (eolian_doc_token_ref_get(src, &tok, NULL, NULL) == EOLIAN_OBJECT_UNKNOWN)
{ {
char *refn = eolian_doc_token_text_get(&tok); char *refn = eolian_doc_token_text_get(&tok);
_eolian_log_line(info->file, info->line, info->column, _eolian_log_line(info->file, info->line, info->column,

View File

@ -289,38 +289,38 @@ eolian_doc_token_text_get(const Eolian_Doc_Token *tok)
return ptr; return ptr;
} }
static Eolian_Doc_Ref_Type static Eolian_Object_Type
_resolve_event(const Eolian_Unit *src, char *name, const void **data, _resolve_event(const Eolian_Unit *src, char *name, const void **data,
const void **data2) const void **data2)
{ {
/* never trust the user */ /* never trust the user */
if (name[0] == ',') if (name[0] == ',')
return EOLIAN_DOC_REF_INVALID; return EOLIAN_OBJECT_UNKNOWN;
char *evname = strrchr(name, '.'); char *evname = strrchr(name, '.');
if (!evname) if (!evname)
return EOLIAN_DOC_REF_INVALID; return EOLIAN_OBJECT_UNKNOWN;
*evname++ = '\0'; *evname++ = '\0';
const Eolian_Class *cl = eolian_unit_class_by_name_get(src, name); const Eolian_Class *cl = eolian_unit_class_by_name_get(src, name);
if (!cl) if (!cl)
return EOLIAN_DOC_REF_INVALID; return EOLIAN_OBJECT_UNKNOWN;
const Eolian_Event *ev = eolian_class_event_get_by_name(cl, evname); const Eolian_Event *ev = eolian_class_event_get_by_name(cl, evname);
if (!ev) if (!ev)
return EOLIAN_DOC_REF_INVALID; return EOLIAN_OBJECT_UNKNOWN;
if (data) *data = cl; if (data) *data = cl;
if (data2) *data2 = ev; if (data2) *data2 = ev;
return EOLIAN_DOC_REF_EVENT; return EOLIAN_OBJECT_EVENT;
} }
EAPI Eolian_Doc_Ref_Type EAPI Eolian_Object_Type
eolian_doc_token_ref_get(const Eolian_Unit *unit, const Eolian_Doc_Token *tok, eolian_doc_token_ref_get(const Eolian_Unit *unit, const Eolian_Doc_Token *tok,
const void **data, const void **data2) const void **data, const void **data2)
{ {
if (tok->type != EOLIAN_DOC_TOKEN_REF) if (tok->type != EOLIAN_DOC_TOKEN_REF)
return EOLIAN_DOC_REF_INVALID; return EOLIAN_OBJECT_UNKNOWN;
size_t nlen = tok->text_end - tok->text; size_t nlen = tok->text_end - tok->text;
@ -343,27 +343,16 @@ eolian_doc_token_ref_get(const Eolian_Unit *unit, const Eolian_Doc_Token *tok,
if (decl) if (decl)
{ {
if (data) *data = decl; if (data) *data = decl;
switch (eolian_object_type_get(decl)) Eolian_Object_Type tp = eolian_object_type_get(decl);
switch (tp)
{ {
case EOLIAN_OBJECT_CLASS: case EOLIAN_OBJECT_CLASS:
return EOLIAN_DOC_REF_CLASS;
case EOLIAN_OBJECT_TYPEDECL: case EOLIAN_OBJECT_TYPEDECL:
switch (eolian_typedecl_type_get((Eolian_Typedecl *)decl))
{
case EOLIAN_TYPEDECL_ALIAS:
return EOLIAN_DOC_REF_ALIAS;
case EOLIAN_TYPEDECL_STRUCT:
case EOLIAN_TYPEDECL_STRUCT_OPAQUE:
return EOLIAN_DOC_REF_STRUCT;
case EOLIAN_TYPEDECL_ENUM:
return EOLIAN_DOC_REF_ENUM;
default:
return EOLIAN_DOC_REF_INVALID;
}
case EOLIAN_OBJECT_VARIABLE: case EOLIAN_OBJECT_VARIABLE:
return EOLIAN_DOC_REF_VAR; /* we only allow certain types to be referenced */
return tp;
default: default:
return EOLIAN_DOC_REF_INVALID; return EOLIAN_OBJECT_UNKNOWN;
} }
} }
@ -372,7 +361,7 @@ eolian_doc_token_ref_get(const Eolian_Unit *unit, const Eolian_Doc_Token *tok,
char *suffix = strrchr(name, '.'); char *suffix = strrchr(name, '.');
/* no suffix, therefore invalid */ /* no suffix, therefore invalid */
if (!suffix) if (!suffix)
return EOLIAN_DOC_REF_INVALID; return EOLIAN_OBJECT_UNKNOWN;
/* name will terminate before suffix, suffix will be standalone */ /* name will terminate before suffix, suffix will be standalone */
*suffix++ = '\0'; *suffix++ = '\0';
@ -384,10 +373,10 @@ eolian_doc_token_ref_get(const Eolian_Unit *unit, const Eolian_Doc_Token *tok,
const Eolian_Struct_Type_Field *fld = eolian_typedecl_struct_field_get(tpd, suffix); const Eolian_Struct_Type_Field *fld = eolian_typedecl_struct_field_get(tpd, suffix);
/* field itself is invalid */ /* field itself is invalid */
if (!fld) if (!fld)
return EOLIAN_DOC_REF_INVALID; return EOLIAN_OBJECT_UNKNOWN;
if (data) *data = tpd; if (data) *data = tpd;
if (data2) *data2 = fld; if (data2) *data2 = fld;
return EOLIAN_DOC_REF_STRUCT_FIELD; return EOLIAN_OBJECT_STRUCT_FIELD;
} }
/* try an enum field */ /* try an enum field */
@ -397,10 +386,10 @@ eolian_doc_token_ref_get(const Eolian_Unit *unit, const Eolian_Doc_Token *tok,
const Eolian_Enum_Type_Field *fld = eolian_typedecl_enum_field_get(tpd, suffix); const Eolian_Enum_Type_Field *fld = eolian_typedecl_enum_field_get(tpd, suffix);
/* field itself is invalid */ /* field itself is invalid */
if (!fld) if (!fld)
return EOLIAN_DOC_REF_INVALID; return EOLIAN_OBJECT_UNKNOWN;
if (data) *data = tpd; if (data) *data = tpd;
if (data2) *data2 = fld; if (data2) *data2 = fld;
return EOLIAN_DOC_REF_ENUM_FIELD; return EOLIAN_OBJECT_ENUM_FIELD;
} }
/* now it can only be a function or invalid */ /* now it can only be a function or invalid */
@ -416,23 +405,23 @@ eolian_doc_token_ref_get(const Eolian_Unit *unit, const Eolian_Doc_Token *tok,
suffix = strrchr(name, '.'); suffix = strrchr(name, '.');
/* wrong suffix, therefore invalid */ /* wrong suffix, therefore invalid */
if (!suffix) if (!suffix)
return EOLIAN_DOC_REF_INVALID; return EOLIAN_OBJECT_UNKNOWN;
/* re-terminate */ /* re-terminate */
*suffix++ = '\0'; *suffix++ = '\0';
} }
const Eolian_Class *cl = eolian_unit_class_by_name_get(unit, name); const Eolian_Class *cl = eolian_unit_class_by_name_get(unit, name);
if (!cl) if (!cl)
return EOLIAN_DOC_REF_INVALID; return EOLIAN_OBJECT_UNKNOWN;
const Eolian_Function *fid = eolian_class_function_get_by_name(cl, suffix, ftype); const Eolian_Function *fid = eolian_class_function_get_by_name(cl, suffix, ftype);
if (!fid) if (!fid)
return EOLIAN_DOC_REF_INVALID; return EOLIAN_OBJECT_UNKNOWN;
/* got a func */ /* got a func */
if (data) *data = cl; if (data) *data = cl;
if (data2) *data2 = fid; if (data2) *data2 = fid;
return EOLIAN_DOC_REF_FUNC; return EOLIAN_OBJECT_FUNCTION;
} }
void void

View File

@ -234,18 +234,6 @@ class Eolian_Doc_Token_Type(IntEnum):
MARK_TODO = 6 MARK_TODO = 6
MARKUP_MONOSPACE = 7 MARKUP_MONOSPACE = 7
class Eolian_Doc_Ref_Type(IntEnum):
INVALID = 0
CLASS = 1
FUNC = 2
EVENT = 3
ALIAS = 4
STRUCT = 5
STRUCT_FIELD = 6
ENUM = 7
ENUM_FIELD = 8
VAR = 9
### internal Classes ######################################################## ### internal Classes ########################################################

View File

@ -831,7 +831,7 @@ lib.eolian_doc_token_type_get.restype = c_int
lib.eolian_doc_token_text_get.argtypes = [c_void_p,] lib.eolian_doc_token_text_get.argtypes = [c_void_p,]
lib.eolian_doc_token_text_get.restype = c_void_p # char* TO BE FREED lib.eolian_doc_token_text_get.restype = c_void_p # char* TO BE FREED
# EAPI Eolian_Doc_Ref_Type eolian_doc_token_ref_get(const Eolian_Unit *unit, const Eolian_Doc_Token *tok, const void **data, const void **data2); # EAPI Eolian_Object_Type eolian_doc_token_ref_get(const Eolian_Unit *unit, const Eolian_Doc_Token *tok, const void **data, const void **data2);
# lib.eolian_doc_token_ref_get.argtypes = [c_void_p, c_void_p, ???, ???] # lib.eolian_doc_token_ref_get.argtypes = [c_void_p, c_void_p, ???, ???]
# lib.eolian_doc_token_ref_get.restype = c_int # lib.eolian_doc_token_ref_get.restype = c_int

View File

@ -129,6 +129,7 @@ class Template(pyratemp.Template):
'Documentation': eolian.Documentation, 'Documentation': eolian.Documentation,
'Documentation_Token': eolian.Documentation_Token, 'Documentation_Token': eolian.Documentation_Token,
# Eolian Enums # Eolian Enums
'Eolian_Object_Type': eolian.Eolian_Object_Type,
'Eolian_Function_Type': eolian.Eolian_Function_Type, 'Eolian_Function_Type': eolian.Eolian_Function_Type,
'Eolian_Parameter_Dir': eolian.Eolian_Parameter_Dir, 'Eolian_Parameter_Dir': eolian.Eolian_Parameter_Dir,
'Eolian_Class_Type': eolian.Eolian_Class_Type, 'Eolian_Class_Type': eolian.Eolian_Class_Type,
@ -142,9 +143,7 @@ class Template(pyratemp.Template):
'Eolian_Variable_Type': eolian.Eolian_Variable_Type, 'Eolian_Variable_Type': eolian.Eolian_Variable_Type,
'Eolian_Binary_Operator': eolian.Eolian_Binary_Operator, 'Eolian_Binary_Operator': eolian.Eolian_Binary_Operator,
'Eolian_Unary_Operator': eolian.Eolian_Unary_Operator, 'Eolian_Unary_Operator': eolian.Eolian_Unary_Operator,
'Eolian_Declaration_Type': eolian.Eolian_Declaration_Type,
'Eolian_Doc_Token_Type': eolian.Eolian_Doc_Token_Type, 'Eolian_Doc_Token_Type': eolian.Eolian_Doc_Token_Type,
'Eolian_Doc_Ref_Type': eolian.Eolian_Doc_Ref_Type,
}) })
# Call the parent __init__ func # Call the parent __init__ func

View File

@ -1256,7 +1256,7 @@ START_TEST(eolian_docs)
fail_if(eolian_doc_token_type_get(&tok) != EOLIAN_DOC_TOKEN_REF); fail_if(eolian_doc_token_type_get(&tok) != EOLIAN_DOC_TOKEN_REF);
txt = eolian_doc_token_text_get(&tok); txt = eolian_doc_token_text_get(&tok);
fail_if(strcmp(txt, "pants")); fail_if(strcmp(txt, "pants"));
fail_if(eolian_doc_token_ref_get(unit, &tok, NULL, NULL) != EOLIAN_DOC_REF_VAR); fail_if(eolian_doc_token_ref_get(unit, &tok, NULL, NULL) != EOLIAN_OBJECT_VARIABLE);
free(txt); free(txt);
tdoc = eolian_documentation_tokenize(tdoc, &tok); tdoc = eolian_documentation_tokenize(tdoc, &tok);
fail_if(eolian_doc_token_type_get(&tok) != EOLIAN_DOC_TOKEN_TEXT); fail_if(eolian_doc_token_type_get(&tok) != EOLIAN_DOC_TOKEN_TEXT);
@ -1267,7 +1267,7 @@ START_TEST(eolian_docs)
fail_if(eolian_doc_token_type_get(&tok) != EOLIAN_DOC_TOKEN_REF); fail_if(eolian_doc_token_type_get(&tok) != EOLIAN_DOC_TOKEN_REF);
txt = eolian_doc_token_text_get(&tok); txt = eolian_doc_token_text_get(&tok);
fail_if(strcmp(txt, "Docs.meth")); fail_if(strcmp(txt, "Docs.meth"));
fail_if(eolian_doc_token_ref_get(unit, &tok, NULL, NULL) != EOLIAN_DOC_REF_FUNC); fail_if(eolian_doc_token_ref_get(unit, &tok, NULL, NULL) != EOLIAN_OBJECT_FUNCTION);
free(txt); free(txt);
tdoc = eolian_documentation_tokenize(tdoc, &tok); tdoc = eolian_documentation_tokenize(tdoc, &tok);
fail_if(eolian_doc_token_type_get(&tok) != EOLIAN_DOC_TOKEN_TEXT); fail_if(eolian_doc_token_type_get(&tok) != EOLIAN_DOC_TOKEN_TEXT);