2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #include "adt/error.h"
28 #include "adt/strutil.h"
30 const char *get_entity_kind_name(entity_kind_t kind)
32 switch ((entity_kind_tag_t) kind) {
33 case ENTITY_FUNCTION: return "function";
34 case ENTITY_VARIABLE: return "variable";
35 case ENTITY_PARAMETER: return "parameter";
36 case ENTITY_COMPOUND_MEMBER: return "compound member";
37 case ENTITY_CLASS: return "class";
38 case ENTITY_STRUCT: return "struct";
39 case ENTITY_UNION: return "union";
40 case ENTITY_ENUM: return "enum";
41 case ENTITY_ENUM_VALUE: return "enum value";
42 case ENTITY_LABEL: return "label";
43 case ENTITY_LOCAL_LABEL: return "local label";
44 case ENTITY_TYPEDEF: return "typedef";
45 case ENTITY_NAMESPACE: return "namespace";
48 panic("Invalid entity kind encountered in get_entity_kind_name");
52 * Returns the size of an entity node.
54 * @param kind the entity kind
56 static size_t get_entity_struct_size(entity_kind_t kind)
58 static const size_t sizes[] = {
59 [ENTITY_VARIABLE] = sizeof(variable_t),
60 [ENTITY_PARAMETER] = sizeof(parameter_t),
61 [ENTITY_COMPOUND_MEMBER] = sizeof(compound_member_t),
62 [ENTITY_FUNCTION] = sizeof(function_t),
63 [ENTITY_TYPEDEF] = sizeof(typedef_t),
64 [ENTITY_STRUCT] = sizeof(compound_t),
65 [ENTITY_UNION] = sizeof(compound_t),
66 [ENTITY_ENUM] = sizeof(enum_t),
67 [ENTITY_ENUM_VALUE] = sizeof(enum_value_t),
68 [ENTITY_LABEL] = sizeof(label_t),
69 [ENTITY_LOCAL_LABEL] = sizeof(label_t),
70 [ENTITY_NAMESPACE] = sizeof(namespace_t)
72 assert(kind < lengthof(sizes));
73 assert(sizes[kind] != 0);
78 * Allocate an entity of given kind and initialize all
81 * @param kind the kind of the entity to allocate
83 entity_t *allocate_entity_zero(entity_kind_t const kind, entity_namespace_t const namespc, symbol_t *const symbol)
85 size_t size = get_entity_struct_size(kind);
86 entity_t *entity = allocate_ast_zero(size);
88 entity->base.namespc = namespc;
89 entity->base.symbol = symbol;
93 elf_visibility_tag_t get_elf_visibility_from_string(const char *string)
95 if (streq(string, "default")) {
96 return ELF_VISIBILITY_DEFAULT;
97 } else if (streq(string, "hidden")) {
98 return ELF_VISIBILITY_HIDDEN;
99 } else if (streq(string, "internal")) {
100 return ELF_VISIBILITY_INTERNAL;
101 } else if (streq(string, "protected")) {
102 return ELF_VISIBILITY_PROTECTED;
104 return ELF_VISIBILITY_ERROR;
108 entity_t *skip_unnamed_bitfields(entity_t *entry)
110 for (; entry != NULL; entry = entry->base.next) {
111 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
112 if (!entry->compound_member.bitfield || entry->base.symbol != NULL)