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"
29 const char *get_entity_kind_name(entity_kind_t kind)
31 switch ((entity_kind_tag_t) kind) {
32 case ENTITY_FUNCTION: return "function";
33 case ENTITY_VARIABLE: return "variable";
34 case ENTITY_PARAMETER: return "parameter";
35 case ENTITY_COMPOUND_MEMBER: return "compound member";
36 case ENTITY_CLASS: return "class";
37 case ENTITY_STRUCT: return "struct";
38 case ENTITY_UNION: return "union";
39 case ENTITY_ENUM: return "enum";
40 case ENTITY_ENUM_VALUE: return "enum value";
41 case ENTITY_LABEL: return "label";
42 case ENTITY_LOCAL_LABEL: return "local label";
43 case ENTITY_TYPEDEF: return "typedef";
44 case ENTITY_NAMESPACE: return "namespace";
45 case ENTITY_INVALID: break;
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 kind)
85 size_t size = get_entity_struct_size(kind);
86 entity_t *entity = allocate_ast_zero(size);