From 867b0f8278dc01a0b9036578855645c539c5c430 Mon Sep 17 00:00:00 2001 From: Jacek Caban Date: Fri, 31 Jul 2015 13:37:50 +0200 Subject: [PATCH] widl: Use proper macro name for forward declarations of interfaces inside a namespace. --- tools/widl/header.c | 4 ++-- tools/widl/parser.y | 7 +++++++ tools/widl/typetree.c | 31 +++++++++++++++++++++++++++++++ tools/widl/widltypes.h | 9 +++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/tools/widl/header.c b/tools/widl/header.c index bf824d8bc3e..a71ad1a7f6a 100644 --- a/tools/widl/header.c +++ b/tools/widl/header.c @@ -1219,8 +1219,8 @@ static void write_function_proto(FILE *header, const type_t *iface, const var_t static void write_forward(FILE *header, type_t *iface) { - fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name); - fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name); + fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->c_name); + fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->c_name); fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name); fprintf(header, "#endif\n\n" ); } diff --git a/tools/widl/parser.y b/tools/widl/parser.y index f3c250a99a1..b9234a44a23 100644 --- a/tools/widl/parser.y +++ b/tools/widl/parser.y @@ -1971,6 +1971,8 @@ int is_type(const char *name) type_t *get_type(enum type_type type, char *name, struct namespace *namespace, int t) { type_t *tp; + if (!namespace) + namespace = &global_namespace; if (name) { tp = find_type(name, namespace, t); if (tp) { @@ -1980,6 +1982,11 @@ type_t *get_type(enum type_type type, char *name, struct namespace *namespace, i } tp = make_type(type); tp->name = name; + tp->namespace = namespace; + if (is_global_namespace(namespace)) + tp->c_name = name; + else + tp->c_name = format_namespace(namespace, "__x_", "_C", name); if (!name) return tp; return reg_type(tp, name, namespace, t); } diff --git a/tools/widl/typetree.c b/tools/widl/typetree.c index 6c09806b6ef..d27a92a0222 100644 --- a/tools/widl/typetree.c +++ b/tools/widl/typetree.c @@ -45,8 +45,10 @@ type_t *make_type(enum type_type type) { type_t *t = alloc_type(); t->name = NULL; + t->namespace = NULL; t->type_type = type; t->attrs = NULL; + t->c_name = NULL; t->orig = NULL; memset(&t->details, 0, sizeof(t->details)); t->typestring_offset = 0; @@ -76,6 +78,35 @@ static const var_t *find_arg(const var_list_t *args, const char *name) return NULL; } +static char *append_namespace(char *ptr, struct namespace *namespace, const char *separator) +{ + if(is_global_namespace(namespace)) + return ptr; + + ptr = append_namespace(ptr, namespace->parent, separator); + strcpy(ptr, namespace->name); + strcat(ptr, separator); + return ptr + strlen(ptr); +} + +char *format_namespace(struct namespace *namespace, const char *prefix, const char *separator, const char *suffix) +{ + unsigned len = strlen(prefix) + strlen(suffix); + unsigned sep_len = strlen(separator); + struct namespace *iter; + char *ret, *ptr; + + for(iter = namespace; !is_global_namespace(iter); iter = iter->parent) + len += strlen(iter->name) + sep_len; + + ret = xmalloc(len+1); + strcpy(ret, prefix); + ptr = append_namespace(ret + strlen(ret), namespace, separator); + strcpy(ptr, suffix); + + return ret; +} + type_t *type_new_function(var_list_t *args) { var_t *arg; diff --git a/tools/widl/widltypes.h b/tools/widl/widltypes.h index 2e89ddbcf61..4d513121cd7 100644 --- a/tools/widl/widltypes.h +++ b/tools/widl/widltypes.h @@ -412,6 +412,7 @@ enum type_type struct _type_t { const char *name; + struct namespace *namespace; enum type_type type_type; attr_list_t *attrs; union @@ -427,6 +428,7 @@ struct _type_t { struct pointer_details pointer; struct bitfield_details bitfield; } details; + const char *c_name; type_t *orig; /* dup'd types */ unsigned int typestring_offset; unsigned int ptrdesc; /* used for complex structs */ @@ -570,6 +572,8 @@ var_list_t *append_var(var_list_t *list, var_t *var); void init_loc_info(loc_info_t *); +char *format_namespace(struct namespace *namespace, const char *prefix, const char *separator, const char *suffix); + static inline var_list_t *type_get_function_args(const type_t *func_type) { return func_type->details.function->args; @@ -599,4 +603,9 @@ static inline int statements_has_func(const statement_list_t *stmts) return has_func; } +static inline int is_global_namespace(const struct namespace *namespace) +{ + return !namespace->name; +} + #endif