2 * This file is part of cparser.
3 * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
11 #include "adt/error.h"
13 #include "adt/strutil.h"
15 const char *get_entity_kind_name(entity_kind_t kind)
17 switch ((entity_kind_tag_t) kind) {
18 case ENTITY_FUNCTION: return "function";
19 case ENTITY_VARIABLE: return "variable";
20 case ENTITY_PARAMETER: return "parameter";
21 case ENTITY_COMPOUND_MEMBER: return "compound member";
22 case ENTITY_CLASS: return "class";
23 case ENTITY_STRUCT: return "struct";
24 case ENTITY_UNION: return "union";
25 case ENTITY_ENUM: return "enum";
26 case ENTITY_ENUM_VALUE: return "enum value";
27 case ENTITY_LABEL: return "label";
28 case ENTITY_LOCAL_LABEL: return "local label";
29 case ENTITY_TYPEDEF: return "typedef";
30 case ENTITY_NAMESPACE: return "namespace";
33 panic("invalid entity kind");
37 * Returns the size of an entity node.
39 * @param kind the entity kind
41 static size_t get_entity_struct_size(entity_kind_t kind)
43 static const size_t sizes[] = {
44 [ENTITY_VARIABLE] = sizeof(variable_t),
45 [ENTITY_PARAMETER] = sizeof(variable_t),
46 [ENTITY_COMPOUND_MEMBER] = sizeof(compound_member_t),
47 [ENTITY_FUNCTION] = sizeof(function_t),
48 [ENTITY_TYPEDEF] = sizeof(typedef_t),
49 [ENTITY_STRUCT] = sizeof(compound_t),
50 [ENTITY_UNION] = sizeof(compound_t),
51 [ENTITY_ENUM] = sizeof(enum_t),
52 [ENTITY_ENUM_VALUE] = sizeof(enum_value_t),
53 [ENTITY_LABEL] = sizeof(label_t),
54 [ENTITY_LOCAL_LABEL] = sizeof(label_t),
55 [ENTITY_NAMESPACE] = sizeof(namespace_t)
57 assert(kind < lengthof(sizes));
58 assert(sizes[kind] != 0);
63 * Allocate an entity of given kind and initialize all
66 * @param kind the kind of the entity to allocate
68 entity_t *allocate_entity_zero(entity_kind_t const kind, entity_namespace_t const namespc, symbol_t *const symbol, position_t const *const pos)
70 size_t const size = get_entity_struct_size(kind);
71 entity_t *const entity = allocate_ast_zero(size);
73 entity->base.namespc = namespc;
74 entity->base.symbol = symbol;
75 entity->base.pos = *pos;
79 elf_visibility_tag_t get_elf_visibility_from_string(const char *string)
81 if (streq(string, "default")) {
82 return ELF_VISIBILITY_DEFAULT;
83 } else if (streq(string, "hidden")) {
84 return ELF_VISIBILITY_HIDDEN;
85 } else if (streq(string, "internal")) {
86 return ELF_VISIBILITY_INTERNAL;
87 } else if (streq(string, "protected")) {
88 return ELF_VISIBILITY_PROTECTED;
90 return ELF_VISIBILITY_ERROR;
94 entity_t *skip_unnamed_bitfields(entity_t *entry)
96 for (; entry != NULL; entry = entry->base.next) {
97 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
98 if (!entry->compound_member.bitfield || entry->base.symbol != NULL)