#include "lang_features.h"
#include "adt/error.h"
-static ident *id_underscore;
-static struct obstack obst;
+static struct obstack obst;
static void mangle_type(type_t *type);
mangle_type(type->points_to);
}
+static void mangle_parameters(const function_type_t *type)
+{
+ if (type->unspecified_parameters)
+ panic("can't mangle unspecified parameter types");
+ if (type->kr_style_parameters)
+ panic("can't mangle kr_style_parameters type");
+
+ const function_parameter_t *parameter = type->parameters;
+ if (parameter != NULL) {
+ for ( ; parameter != NULL; parameter = parameter->next) {
+ mangle_type(parameter->type);
+ }
+ if (type->variadic) {
+ obstack_1grow(&obst, 'z');
+ }
+ } else {
+ obstack_1grow(&obst, 'v');
+ }
+}
+
static void mangle_function_type(const function_type_t *type)
{
obstack_1grow(&obst, 'F');
}
mangle_type(type->return_type);
-
- function_parameter_t *parameter = type->parameters;
- for ( ; parameter != NULL; parameter = parameter->next) {
- mangle_type(parameter->type);
- }
- if (type->variadic) {
- obstack_1grow(&obst, 'z');
- }
- if (type->unspecified_parameters)
- panic("can't mangle unspecified parameter types");
- if (type->kr_style_parameters)
- panic("can't mangle kr_style_parameters type");
+ mangle_parameters(type);
obstack_1grow(&obst, 'E');
}
static void mangle_qualifiers(type_qualifiers_t qualifiers)
{
- if (qualifiers & TYPE_QUALIFIER_CONST)
- obstack_1grow(&obst, 'K');
- if (qualifiers & TYPE_QUALIFIER_VOLATILE)
- obstack_1grow(&obst, 'V');
if (qualifiers & TYPE_QUALIFIER_RESTRICT)
obstack_1grow(&obst, 'r');
+ if (qualifiers & TYPE_QUALIFIER_VOLATILE)
+ obstack_1grow(&obst, 'V');
+ if (qualifiers & TYPE_QUALIFIER_CONST)
+ obstack_1grow(&obst, 'K');
/* handle MS extended qualifiers? */
}
/* TODO: mangle scope */
- symbol_t *symbol = entity->base.symbol;
- obstack_printf(&obst, "%u%s", strlen(symbol->string), symbol->string);
+ const char *name = entity->base.symbol->string;
+ obstack_printf(&obst, "%zu%s", strlen(name), name);
if (entity->kind == ENTITY_FUNCTION) {
- mangle_type(entity->declaration.type);
+ mangle_parameters(&entity->declaration.type->function);
}
}
+static ident *make_id_from_obst(void)
+{
+ size_t size = obstack_object_size(&obst);
+ char *str = obstack_finish(&obst);
+ ident *id = new_id_from_chars(str, size);
+ obstack_free(&obst, str);
+ return id;
+}
+
/**
* Mangles an entity linker (ld) name for win32 usage.
*
obstack_printf(o, "_%s", entity->base.symbol->string);
}
- size_t size = obstack_object_size(o);
- char *str = obstack_finish(o);
- ident *id = new_id_from_chars(str, size);
- obstack_free(o, str);
- return id;
+ return make_id_from_obst();
}
/**
if (needs_mangling) {
mangle_entity(entity);
- size_t size = obstack_object_size(&obst);
- char *str = obstack_finish(&obst);
- ident *id = new_id_from_chars(str, size);
- obstack_free(&obst, str);
- return id;
+ return make_id_from_obst();
}
return new_id_from_str(entity->base.symbol->string);
*/
ident *create_name_macho(entity_t *entity)
{
- ident *id = new_id_from_str(entity->base.symbol->string);
- return id_mangle(id_underscore, id);
+ obstack_printf(&obst, "_%s", entity->base.symbol->string);
+ return make_id_from_obst();
}
void init_mangle(void)
{
- id_underscore = new_id_from_chars("_", 1);
-
obstack_init(&obst);
}