Parse C++ references.
[cparser] / entity_t.h
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
4  *
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.
9  *
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.
14  *
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
18  * 02111-1307, USA.
19  */
20 #ifndef ENTITY_T_H
21 #define ENTITY_T_H
22
23 #include "lexer.h"
24 #include "symbol.h"
25 #include "entity.h"
26
27 typedef enum {
28         ENTITY_INVALID,
29         ENTITY_VARIABLE,
30         ENTITY_COMPOUND_MEMBER,
31         ENTITY_FUNCTION,
32         ENTITY_TYPEDEF,
33         ENTITY_STRUCT,
34         ENTITY_UNION,
35         ENTITY_ENUM,
36         ENTITY_ENUM_VALUE,
37         ENTITY_LABEL,
38         ENTITY_LOCAL_LABEL,
39         ENTITY_NAMESPACE
40 } entity_kind_tag_t;
41 typedef unsigned char entity_kind_t;
42
43 typedef enum namespace_tag_t {
44         NAMESPACE_INVALID,
45         NAMESPACE_NORMAL,
46         NAMESPACE_STRUCT,
47         NAMESPACE_UNION,
48         NAMESPACE_ENUM,
49         NAMESPACE_LABEL
50 } namespace_tag_t;
51 typedef unsigned char entity_namespace_t;
52
53 typedef enum storage_class_tag_t {
54         STORAGE_CLASS_NONE,
55         STORAGE_CLASS_EXTERN,
56         STORAGE_CLASS_STATIC,
57         STORAGE_CLASS_TYPEDEF,
58         STORAGE_CLASS_AUTO,
59         STORAGE_CLASS_REGISTER,
60         STORAGE_CLASS_THREAD,
61         STORAGE_CLASS_THREAD_EXTERN,
62         STORAGE_CLASS_THREAD_STATIC,
63 } storage_class_tag_t;
64 typedef unsigned char storage_class_t;
65
66 typedef enum decl_modifier_t {
67         DM_NONE              = 0,
68         DM_DLLIMPORT         = 1 <<  0,
69         DM_DLLEXPORT         = 1 <<  1,
70         DM_THREAD            = 1 <<  2,
71         DM_NAKED             = 1 <<  3,
72         DM_MICROSOFT_INLINE  = 1 <<  4,
73         DM_FORCEINLINE       = 1 <<  5,
74         DM_SELECTANY         = 1 <<  6,
75         DM_NOTHROW           = 1 <<  7,
76         DM_NOVTABLE          = 1 <<  8,
77         DM_NORETURN          = 1 <<  9,
78         DM_NOINLINE          = 1 << 10,
79         DM_RESTRICT          = 1 << 11,
80         DM_NOALIAS           = 1 << 12,
81         DM_PACKED            = 1 << 13,
82         DM_TRANSPARENT_UNION = 1 << 14,
83         DM_CONST             = 1 << 15,
84         DM_PURE              = 1 << 16,
85         DM_CONSTRUCTOR       = 1 << 17,
86         DM_DESTRUCTOR        = 1 << 18,
87         DM_UNUSED            = 1 << 19,
88         DM_USED              = 1 << 20,
89         DM_CDECL             = 1 << 21,
90         DM_FASTCALL          = 1 << 22,
91         DM_STDCALL           = 1 << 23,
92         DM_THISCALL          = 1 << 24,
93         DM_DEPRECATED        = 1 << 25
94 } decl_modifier_t;
95
96 typedef unsigned decl_modifiers_t;
97
98 /**
99  * A scope containing entities.
100  */
101 struct scope_t {
102         entity_t *entities;
103         entity_t *last_entity;
104         scope_t  *parent;       /**< points to the parent scope. */
105         unsigned  depth;        /**< while parsing, the depth of this scope in the
106                                      scope stack. */
107 };
108
109 /**
110  * a named entity is something which can be referenced by its name
111  * (a symbol)
112  */
113 struct entity_base_t {
114         entity_kind_t       kind;
115         entity_namespace_t  namespc;
116         symbol_t           *symbol;
117         source_position_t   source_position;
118         scope_t            *parent_scope;       /**< The parent scope where this declaration lives. */
119
120         /** next declaration in a scope */
121         entity_t           *next;
122         /** next declaration with same symbol */
123         entity_t           *symbol_next;
124 };
125
126 struct compound_t {
127         entity_base_t     base;
128         entity_t         *alias; /* used for name mangling of anonymous types */
129         scope_t           members;
130         decl_modifiers_t  modifiers;
131         bool              complete            : 1;
132         bool              has_flexible_member : 1;
133
134         /* ast2firm info */
135         ir_type          *irtype;
136         bool              irtype_complete : 1;
137 };
138
139 struct enum_t {
140         entity_base_t  base;
141         entity_t      *alias; /* used for name mangling of anonymous types */
142         bool           complete : 1;
143
144         /* ast2firm info */
145         ir_type       *irtype;
146 };
147
148 struct enum_value_t {
149         entity_base_t  base;
150         expression_t  *value;
151         type_t        *enum_type;
152
153         /* ast2firm info */
154         tarval        *tv;
155 };
156
157 struct label_t {
158         entity_base_t  base;
159         bool           used : 1;
160         bool           address_taken : 1;
161         statement_t   *statement;
162
163         /* ast2firm info */
164         ir_node       *block;
165 };
166
167 struct namespace_t {
168         entity_base_t  base;
169         scope_t        members;
170 };
171
172 struct typedef_t {
173         entity_base_t     base;
174         decl_modifiers_t  modifiers;
175         type_t           *type;
176         bool              builtin : 1;
177 };
178
179 struct declaration_t {
180         entity_base_t     base;
181         storage_class_t   declared_storage_class;
182         storage_class_t   storage_class;
183         decl_modifiers_t  modifiers;
184         const char       *deprecated_string;  /**< MS deprecated string if any. */
185         bool              used          : 1;  /**< Set if the declaration is used. */
186         bool              implicit      : 1;  /**< Set for implicit (not found in source code) declarations. */
187         type_t           *type;
188
189         /* ast2firm info */
190         unsigned char     kind;
191 };
192
193 struct compound_member_t {
194         declaration_t  base;
195         bool           read : 1;
196
197         /* ast2firm info */
198         ir_entity *entity;
199         il_size_t  offset;  /**< The offset of this member inside a compound. */
200 };
201
202 struct variable_t {
203         declaration_t  base;
204         bool           address_taken : 1;  /**< Set if the address of this declaration was taken. */
205         bool           read          : 1;
206         unsigned char  alignment;          /**< Alignment of the declaration, 0 for default. */
207         symbol_t      *get_property_sym;   /**< MS get property. */
208         symbol_t      *put_property_sym;   /**< MS put property. */
209
210         initializer_t *initializer;
211
212         /* ast2firm info */
213         union {
214                 unsigned int  value_number;
215                 ir_entity    *entity;
216                 ir_node      *vla_base;
217         } v;
218 };
219
220 struct function_t {
221         declaration_t  base;
222         bool           is_inline     : 1;
223         bool           need_closure  : 1;  /**< Inner function needs closure. */
224         bool           goto_to_outer : 1;  /**< Inner function has goto to outer function. */
225
226         scope_t        parameters;
227         statement_t   *statement;
228
229         /* ast2firm info */
230         ir_entity     *entity;
231 };
232
233 union entity_t {
234         entity_kind_t      kind;
235         entity_base_t      base;
236         compound_t         structe;
237         compound_t         unione;
238         compound_t         compound;
239         enum_t             enume;
240         enum_value_t       enum_value;
241         label_t            label;
242         namespace_t        namespacee;
243         typedef_t          typedefe;
244         declaration_t      declaration;
245         variable_t         variable;
246         function_t         function;
247         compound_member_t  compound_member;
248 };
249
250 static inline bool is_declaration(const entity_t *entity)
251 {
252         return entity->kind == ENTITY_FUNCTION || entity->kind == ENTITY_VARIABLE
253                 || entity->kind == ENTITY_COMPOUND_MEMBER;
254 }
255
256 #endif