Added some helper functions to show the variability, volatility and peculirity.
[libfirm] / ir / ident / ident.c
1 /* Ident --- unique handles for identifiers
2    Copyright (C) 1995, 1996 Markus Armbruster
3    All rights reserved. */
4
5 /* $Id$ */
6
7 #ifdef HAVE_CONFIG_H
8 # include <config.h>
9 #endif
10
11 #include <assert.h>
12 #include <ctype.h>
13 #include <string.h>
14 #include <stddef.h>
15 #include <stdlib.h>
16
17 #include "ident_t.h"
18 #include "array.h"
19 #include "tune.h"
20 #include "misc.h"
21 #include "set.h"
22
23 #define ID_TO_STR(id) ((const char *)&(id)->dptr[0])
24 #define ID_TO_STRLEN(id) ((id)->size)
25 #define ID_TO_HASH(id) ((long)(id) + (id)->hash)
26
27 /* Vormals Debugunterstuetzung, entfernt (debug.h). */
28 # define ID_VRFY(id) ((void)0)
29 # define IDS_VRFY(id) ((void)0)
30
31 #ifdef STATS
32 # define id_stats() set_stats (id_set)
33 #else
34 # define id_stats() ((void)0)
35 #endif
36
37
38 static set *id_set;
39
40 void id_init(void)
41 {
42   id_set = new_set(memcmp, TUNE_NIDENTS);
43 }
44
45 INLINE ident *id_from_str (const char *str, int len)
46 {
47   assert(len > 0);
48   return set_hinsert0(id_set, str, len, ID_HASH(str, len));
49 }
50
51 ident *new_id_from_str(const char *str)
52 {
53   assert(str);
54   return id_from_str(str, strlen(str));
55 }
56
57 INLINE const char *id_to_str(ident *id)
58 {
59   return (const char *)id->dptr;
60 }
61
62 INLINE int id_to_strlen(ident *id)
63 {
64   return id->size;
65 }
66
67 int id_is_prefix(ident *prefix, ident *id)
68 {
69   if (id_to_strlen(prefix) > id_to_strlen(id)) return 0;
70   return 0 == memcmp(prefix->dptr, id->dptr, id_to_strlen(prefix));
71 }
72
73 int id_is_suffix(ident *suffix, ident *id)
74 {
75   int suflen = id_to_strlen(suffix);
76   int idlen  = id_to_strlen(id);
77   char *part;
78
79   if (suflen > idlen) return 0;
80
81   part = (char *)id->dptr;
82   part = part + (idlen - suflen);
83
84   return 0 == memcmp(suffix->dptr, part, suflen);
85 }
86
87 int id_contains_char(ident *id, char c)
88 {
89   return strchr(id_to_str(id), c) != NULL;
90 }
91
92 int print_id (ident *id)
93 {
94   return printf("%s", id_to_str(id));
95 }
96
97 int fprint_id (FILE *F, ident *id)
98 {
99   return fprintf(F, "%s", id_to_str(id));
100 }
101
102 int
103 ident_print (XP_PAR1, const xprintf_info *info ATTRIBUTE((unused)), XP_PARN)
104 {
105   ident *id = XP_GETARG (ident *, 0);
106   return XPMR (ID_TO_STR (id), ID_TO_STRLEN (id));
107 }