bearch: Disallow passing Projs to get_irn_ops().
[libfirm] / ir / ident / mangle.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief   Methods to manipulate names.
9  * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
10  */
11 #include "config.h"
12
13 #include <stdio.h>
14
15 #include "ident_t.h"
16 #include "obst.h"
17 #include "irprintf.h"
18
19 /* Make types visible to allow most efficient access */
20 #include "entity_t.h"
21 #include "type_t.h"
22 #include "tpop_t.h"
23
24 /** An obstack used for temporary space */
25 static struct obstack mangle_obst;
26
27 /** returned a mangled type name, currently no mangling */
28 static inline ident *mangle_type(const ir_type *tp)
29 {
30         assert(tp->kind == k_type);
31         return tp->name;
32 }
33
34 /* Returns a new ident that represents 'firstscnd'. */
35 ident *id_mangle(ident *first, ident *scnd)
36 {
37         char *cp;
38         int len;
39         ident *res;
40
41         obstack_grow(&mangle_obst, get_id_str(first), get_id_strlen(first));
42         obstack_grow(&mangle_obst, get_id_str(scnd), get_id_strlen(scnd));
43         len = obstack_object_size(&mangle_obst);
44         cp = (char*)obstack_finish(&mangle_obst);
45         res = new_id_from_chars(cp, len);
46         obstack_free(&mangle_obst, cp);
47         return res;
48 }
49
50 /** Returns a new ident that represents 'prefixscndsuffix'. */
51 ident *id_mangle3(const char *prefix, ident *scnd, const char *suffix)
52 {
53         char *cp;
54         int len;
55         ident *res;
56
57         obstack_grow(&mangle_obst, prefix, strlen(prefix));
58         obstack_grow(&mangle_obst, get_id_str(scnd), get_id_strlen(scnd));
59         obstack_grow(&mangle_obst, suffix, strlen(suffix));
60         len = obstack_object_size(&mangle_obst);
61         cp  = (char*)obstack_finish(&mangle_obst);
62         res = new_id_from_chars(cp, len);
63         obstack_free(&mangle_obst, cp);
64         return res;
65 }
66
67 /** Returns a new ident that represents first<c>scnd. */
68 static ident *id_mangle_3(ident *first, char c, ident *scnd)
69 {
70         char *cp;
71         int len;
72         ident *res;
73
74         obstack_grow(&mangle_obst, get_id_str(first), get_id_strlen(first));
75         obstack_1grow(&mangle_obst, c);
76         obstack_grow(&mangle_obst,get_id_str(scnd),get_id_strlen(scnd));
77         len = obstack_object_size(&mangle_obst);
78         cp = (char*)obstack_finish(&mangle_obst);
79         res = new_id_from_chars(cp, len);
80         obstack_free(&mangle_obst, cp);
81         return res;
82 }
83
84 /* Returns a new ident that represents first_scnd. */
85 ident *id_mangle_u(ident *first, ident* scnd)
86 {
87         return id_mangle_3(first, '_', scnd);
88 }
89
90 /* Returns a new ident that represents first.scnd. */
91 ident *id_mangle_dot(ident *first, ident *scnd)
92 {
93         return id_mangle_3(first, '.', scnd);
94 }
95
96 void firm_init_mangle(void)
97 {
98         obstack_init(&mangle_obst);
99 }
100
101 void firm_finish_mangle(void)
102 {
103         obstack_free(&mangle_obst, NULL);
104 }