simple support for __attribute__((alias("symbol")))
[cparser] / entity.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
4  */
5 #include <config.h>
6
7 #include <assert.h>
8
9 #include "entity_t.h"
10 #include "ast_t.h"
11 #include "adt/error.h"
12 #include "adt/util.h"
13 #include "adt/strutil.h"
14
15 const char *get_entity_kind_name(entity_kind_t kind)
16 {
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";
31         }
32
33         panic("invalid entity kind");
34 }
35
36 /**
37  * Returns the size of an entity node.
38  *
39  * @param kind  the entity kind
40  */
41 static size_t get_entity_struct_size(entity_kind_t kind)
42 {
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)
56         };
57         assert(kind < lengthof(sizes));
58         assert(sizes[kind] != 0);
59         return sizes[kind];
60 }
61
62 /**
63  * Allocate an entity of given kind and initialize all
64  * fields with zero.
65  *
66  * @param kind   the kind of the entity to allocate
67  */
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)
69 {
70         size_t    const size   = get_entity_struct_size(kind);
71         entity_t *const entity = allocate_ast_zero(size);
72         entity->kind           = kind;
73         entity->base.namespc   = namespc;
74         entity->base.symbol    = symbol;
75         entity->base.pos       = *pos;
76         return entity;
77 }
78
79 elf_visibility_tag_t get_elf_visibility_from_string(const char *string)
80 {
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;
89         } else {
90                 return ELF_VISIBILITY_ERROR;
91         }
92 }
93
94 entity_t *skip_unnamed_bitfields(entity_t *entry)
95 {
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)
99                         break;
100         }
101         return entry;
102 }