entity.c 22.7 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
4
5
 */

Michael Beck's avatar
Michael Beck committed
6
7
8
9
/**
 * @file
 * @brief   Representation of all program known entities.
 * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
10
 */
11
#include <stdlib.h>
12

Michael Beck's avatar
Michael Beck committed
13
14
15
#include "xmalloc.h"
#include "entity_t.h"
#include "array.h"
16
#include "util.h"
Michael Beck's avatar
Michael Beck committed
17
18
#include "irhooks.h"
#include "irprog_t.h"
19
#include "ircons_t.h"
Michael Beck's avatar
Michael Beck committed
20
#include "tv_t.h"
21
22
23
#include "irdump.h"
#include "irgraph_t.h"
#include "callgraph.h"
Matthias Braun's avatar
Matthias Braun committed
24
#include "panic.h"
Götz Lindenmaier's avatar
Götz Lindenmaier committed
25

26
/** The name of the unknown entity. */
27
#define UNKNOWN_ENTITY_NAME "unknown_entity"
28

29
30
31
32
33
ir_entity *get_unknown_entity(void)
{
	return irp->unknown_entity;
}

Matthias Braun's avatar
Matthias Braun committed
34
static ir_entity *intern_new_entity(ir_type *owner, ir_entity_kind kind,
35
36
                                    ident *name, ir_type *type,
                                    ir_visibility visibility)
Christian Schäfer's avatar
Christian Schäfer committed
37
{
Matthias Braun's avatar
Matthias Braun committed
38
	assert(owner != NULL);
39

40
	ir_entity *res = XMALLOCZ(ir_entity);
Matthias Braun's avatar
Matthias Braun committed
41
42
	res->kind        = k_entity;
	res->name        = name;
43
	res->ld_name     = name;
Matthias Braun's avatar
Matthias Braun committed
44
45
46
47
48
49
	res->type        = type;
	res->owner       = owner;
	res->entity_kind = kind;
	res->volatility  = volatility_non_volatile;
	res->aligned     = align_is_aligned;
	res->usage       = ir_usage_unknown;
50
	res->visibility  = visibility;
Matthias Braun's avatar
Matthias Braun committed
51
	res->nr          = get_irp_new_node_nr();
52
53

	/* Remember entity in its owner. */
54
	add_compound_member(owner, res);
55
56
57
58
59

	res->visit = 0;
	return res;
}

60
61
static ir_entity *new_entity_vis(ir_type *owner, ident *name, ir_type *type,
                                 ir_visibility vis)
62
{
Matthias Braun's avatar
Matthias Braun committed
63
	ir_entity *res;
64
	if (is_Method_type(type)) {
65
		res = intern_new_entity(owner, IR_ENTITY_METHOD, name, type, vis);
66
67
		res->linkage                     = IR_LINKAGE_CONSTANT;
		res->attr.mtd_attr.properties    = get_method_additional_properties(type);
Matthias Braun's avatar
Matthias Braun committed
68
69
70
71
		res->attr.mtd_attr.vtable_number = IR_VTABLE_NUM_NOT_SET;
		res->attr.mtd_attr.param_access  = NULL;
		res->attr.mtd_attr.param_weight  = NULL;
		res->attr.mtd_attr.irg           = NULL;
72
	} else if (is_compound_type(owner) && !is_segment_type(owner)) {
73
74
		res = intern_new_entity(owner, IR_ENTITY_COMPOUND_MEMBER, name, type,
		                        vis);
75
		res->attr.compound_member.offset = -1;
Matthias Braun's avatar
Matthias Braun committed
76
	} else {
77
		res = intern_new_entity(owner, IR_ENTITY_NORMAL, name, type, vis);
78
79
	}

80
81
82
	hook_new_entity(res);
	return res;
}
83

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
ir_entity *new_entity(ir_type *owner, ident *name, ir_type *type)
{
	return new_entity_vis(owner, name, type, ir_visibility_external);
}

ir_entity *new_global_entity(ir_type *segment, ident *ld_name, ir_type *type,
                             ir_visibility visibility, ir_linkage linkage)
{
	assert(is_segment_type(segment) || segment == irp->dummy_owner);
	ir_entity *res = new_entity_vis(segment, ld_name, type, visibility);
	add_entity_linkage(res, linkage);
	hook_new_entity(res);
	return res;
}

99
ir_entity *new_parameter_entity(ir_type *owner, size_t pos, ir_type *type)
100
{
101
	ident     *name = new_id_fmt("parameter.%lu", (unsigned long)pos);
102
103
	ir_entity *res  = intern_new_entity(owner, IR_ENTITY_PARAMETER, name, type,
	                                    ir_visibility_private);
104
	res->attr.compound_member.offset = -1;
105
	res->attr.parameter.number = pos;
106
107
	hook_new_entity(res);
	return res;
108
}
109

110
ir_entity *new_label_entity(ir_label_t label)
Matthias Braun's avatar
Matthias Braun committed
111
112
113
{
	ident *name = id_unique("label_%u");
	ir_type *global_type = get_glob_type();
Matthias Braun's avatar
Matthias Braun committed
114
	ir_entity *res = intern_new_entity(global_type, IR_ENTITY_LABEL, name,
115
	                                   get_code_type(), ir_visibility_private);
Matthias Braun's avatar
Matthias Braun committed
116
117
118
119
120
	res->attr.code_attr.label = label;
	hook_new_entity(res);
	return res;
}

121
ir_entity *new_alias_entity(ir_type *owner, ident *name, ir_entity *aliased,
122
                            ir_type *type, ir_visibility visibility)
123
{
124
125
	ir_entity *res = intern_new_entity(owner, IR_ENTITY_ALIAS, name, type,
	                                   visibility);
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
	res->attr.alias.aliased = aliased;
	hook_new_entity(res);
	return res;
}

void set_entity_alias(ir_entity *entity, ir_entity *aliased)
{
	assert(get_entity_kind(entity) == IR_ENTITY_ALIAS);
	entity->attr.alias.aliased = aliased;
}

ir_entity *get_entity_alias(const ir_entity *entity)
{
	assert(get_entity_kind(entity) == IR_ENTITY_ALIAS);
	return entity->attr.alias.aliased;
}

143
144
145
/**
 * Free entity attributes.
 */
Matthias Braun's avatar
Matthias Braun committed
146
147
static void free_entity_attrs(ir_entity *ent)
{
148
149
150
	if (ent->overwrites != NULL) {
		DEL_ARR_F(ent->overwrites);
		ent->overwrites = NULL;
151
	}
152
153
154
155
156
	if (ent->overwrittenby != NULL) {
		DEL_ARR_F(ent->overwrittenby);
		ent->overwrittenby = NULL;
	}

157
158
	/* TODO: free initializers */

159
	if (ent->entity_kind == IR_ENTITY_METHOD) {
160
161
162
163
164
165
166
167
168
		if (ent->attr.mtd_attr.param_access) {
			DEL_ARR_F(ent->attr.mtd_attr.param_access);
			ent->attr.mtd_attr.param_access = NULL;
		}
		if (ent->attr.mtd_attr.param_weight) {
			DEL_ARR_F(ent->attr.mtd_attr.param_weight);
			ent->attr.mtd_attr.param_weight = NULL;
		}
	}
169
}
Christian Schäfer's avatar
Christian Schäfer committed
170

171
172
173
174
175
176
177
/**
 * Creates a deep copy of an entity.
 */
static ir_entity *deep_entity_copy(ir_entity *old)
{
	ir_entity *newe = XMALLOC(ir_entity);

178
	*newe = *old;
179
180
	/* FIXME: the initializers are NOT copied */
	if (is_method_entity(old)) {
181
182
183
184
		/* do NOT copy them, reanalyze. This might be the best solution */
		newe->attr.mtd_attr.param_access = NULL;
		newe->attr.mtd_attr.param_weight = NULL;
	}
185
186
	newe->overwrites    = NULL;
	newe->overwrittenby = NULL;
187
188

	newe->nr = get_irp_new_node_nr();
189
	hook_new_entity(newe);
190
191
	return newe;
}
192

193
ir_entity *copy_entity_own(ir_entity *old, ir_type *new_owner)
194
{
195
	assert(old->kind == k_entity);
196
	assert(is_compound_type(new_owner));
197
	assert(get_type_state(new_owner) != layout_fixed);
198
199
200
201
	if (old->owner == new_owner)
		return old;

	/* create a deep copy so we are safe of aliasing and double-freeing. */
Matthias Braun's avatar
Matthias Braun committed
202
	ir_entity *newe = deep_entity_copy(old);
203
	newe->owner = new_owner;
204
	add_compound_member(new_owner, newe);
205

206
	return newe;
207
}
208

209
ir_entity *copy_entity_name(ir_entity *old, ident *new_name)
210
{
Matthias Braun's avatar
Matthias Braun committed
211
	assert(old->kind == k_entity);
212
213
214
	if (old->name == new_name)
		return old;

Matthias Braun's avatar
Matthias Braun committed
215
216
	ir_entity *newe = deep_entity_copy(old);
	newe->name    = new_name;
217
	newe->ld_name = NULL;
218
	add_compound_member(old->owner, newe);
219

220
	return newe;
221
}
222

223
void free_entity(ir_entity *ent)
224
{
225
	remove_compound_member(ent->owner, ent);
226

Matthias Braun's avatar
Matthias Braun committed
227
	assert(ent->kind == k_entity);
228
	free_entity_attrs(ent);
229
#ifdef DEBUG_libfirm
230
	ent->kind = k_BAD;
231
#endif
232
	free(ent);
233
}
234

235
long get_entity_nr(const ir_entity *ent)
236
{
Matthias Braun's avatar
Matthias Braun committed
237
	assert(ent->kind == k_entity);
238
	return ent->nr;
239
}
240

241
242
const char *(get_entity_name)(const ir_entity *ent)
{
243
	return _get_entity_name(ent);
Matthias Braun's avatar
Matthias Braun committed
244
}
Christian Schäfer's avatar
Christian Schäfer committed
245

246
247
ident *(get_entity_ident)(const ir_entity *ent)
{
248
	return _get_entity_ident(ent);
Matthias Braun's avatar
Matthias Braun committed
249
}
Michael Beck's avatar
Michael Beck committed
250

251
252
void (set_entity_ident)(ir_entity *ent, ident *id)
{
253
	_set_entity_ident(ent, id);
Matthias Braun's avatar
Matthias Braun committed
254
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
255

256
257
ir_type *(get_entity_owner)(const ir_entity *ent)
{
258
	return _get_entity_owner(ent);
Matthias Braun's avatar
Matthias Braun committed
259
}
Christian Schäfer's avatar
Christian Schäfer committed
260

261
void set_entity_owner(ir_entity *ent, ir_type *owner)
262
{
263
	assert(ent->kind == k_entity);
264
	assert(is_compound_type(owner));
265
266
267

	remove_compound_member(ent->owner, ent);
	add_compound_member(owner, ent);
268
	ent->owner = owner;
Matthias Braun's avatar
Matthias Braun committed
269
}
Christian Schäfer's avatar
Christian Schäfer committed
270

Matthias Braun's avatar
Matthias Braun committed
271
272
ident *(get_entity_ld_ident)(const ir_entity *ent)
{
273
	return _get_entity_ld_ident(ent);
Matthias Braun's avatar
Matthias Braun committed
274
}
275

Matthias Braun's avatar
Matthias Braun committed
276
void set_entity_ld_ident(ir_entity *const ent, ident *const ld_ident)
277
{
Matthias Braun's avatar
Matthias Braun committed
278
279
280
281
	ident *old_ident = get_entity_ld_ident(ent);
	ent->ld_name = ld_ident;
	if (old_ident != ld_ident) {
		ir_type *owner = get_entity_owner(ent);
282
283
		if (is_segment_type(owner) && !(owner->flags & tf_info)
		 && get_entity_visibility(ent) != ir_visibility_private) {
Matthias Braun's avatar
Matthias Braun committed
284
285
			pmap *globals = irp->globals;
			pmap_insert(globals, old_ident, NULL);
Andreas Zwinkau's avatar
Andreas Zwinkau committed
286
			assert(NULL == pmap_get(ir_entity, globals, ld_ident));
Matthias Braun's avatar
Matthias Braun committed
287
288
289
			pmap_insert(globals, ld_ident, ent);
		}
	}
Matthias Braun's avatar
Matthias Braun committed
290
}
Christian Schäfer's avatar
Christian Schäfer committed
291

Matthias Braun's avatar
Matthias Braun committed
292
293
const char *(get_entity_ld_name)(const ir_entity *ent)
{
294
	return _get_entity_ld_name(ent);
Matthias Braun's avatar
Matthias Braun committed
295
}
296

297
298
299
300
301
int entity_has_ld_ident(const ir_entity *entity)
{
	return entity->ld_name != NULL;
}

302
303
ir_type *(get_entity_type)(const ir_entity *ent)
{
304
	return _get_entity_type(ent);
Matthias Braun's avatar
Matthias Braun committed
305
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
306

Matthias Braun's avatar
Matthias Braun committed
307
308
309
310
311
312
313
314
315
316
void set_entity_type(ir_entity *ent, ir_type *type)
{
	switch (ent->entity_kind) {
	case IR_ENTITY_METHOD:
		assert(is_Method_type(type));
		break;
	case IR_ENTITY_NORMAL:
		assert(!is_Method_type(type));
		break;
	case IR_ENTITY_LABEL:
317
		assert(type == get_code_type());
Matthias Braun's avatar
Matthias Braun committed
318
		break;
Matthias Braun's avatar
Matthias Braun committed
319
320
321
	case IR_ENTITY_ALIAS:
	case IR_ENTITY_PARAMETER:
	case IR_ENTITY_UNKNOWN:
Matthias Braun's avatar
Matthias Braun committed
322
323
324
325
	case IR_ENTITY_COMPOUND_MEMBER:
		break;
	}
	ent->type = type;
Matthias Braun's avatar
Matthias Braun committed
326
}
Götz Lindenmaier's avatar
a    
Götz Lindenmaier committed
327

328
329
ir_volatility (get_entity_volatility)(const ir_entity *ent)
{
330
	return _get_entity_volatility(ent);
Matthias Braun's avatar
Matthias Braun committed
331
}
Götz Lindenmaier's avatar
a    
Götz Lindenmaier committed
332

333
334
void (set_entity_volatility)(ir_entity *ent, ir_volatility vol)
{
335
	_set_entity_volatility(ent, vol);
Matthias Braun's avatar
Matthias Braun committed
336
}
Götz Lindenmaier's avatar
a    
Götz Lindenmaier committed
337

338
const char *get_volatility_name(ir_volatility var)
339
{
340
#define X(a)    case a: return #a
341
342
343
344
	switch (var) {
	X(volatility_non_volatile);
	X(volatility_is_volatile);
	}
345
#undef X
Matthias Braun's avatar
Matthias Braun committed
346
	return "BAD VALUE";
347
}
348

349
350
ir_align (get_entity_aligned)(const ir_entity *ent)
{
Matthias Braun's avatar
Matthias Braun committed
351
352
	return _get_entity_aligned(ent);
}
353

354
355
void (set_entity_aligned)(ir_entity *ent, ir_align a)
{
Matthias Braun's avatar
Matthias Braun committed
356
357
	_set_entity_aligned(ent, a);
}
358

359
360
unsigned (get_entity_alignment)(const ir_entity *ent)
{
361
362
363
	return _get_entity_alignment(ent);
}

364
365
void (set_entity_alignment)(ir_entity *ent, unsigned alignment)
{
366
367
368
	_set_entity_alignment(ent, alignment);
}

Michael Beck's avatar
Michael Beck committed
369
370
371
372
373
374
375
376
const char *get_align_name(ir_align a)
{
#define X(a)    case a: return #a
	switch (a) {
	X(align_non_aligned);
	X(align_is_aligned);
	}
#undef X
Matthias Braun's avatar
Matthias Braun committed
377
	return "BAD VALUE";
378
}
Michael Beck's avatar
Michael Beck committed
379

380
void set_entity_label(ir_entity *ent, ir_label_t label)
381
{
Matthias Braun's avatar
Matthias Braun committed
382
	assert(ent->entity_kind == IR_ENTITY_LABEL);
383
384
385
386
387
	ent->attr.code_attr.label = label;
}

ir_label_t get_entity_label(const ir_entity *ent)
{
Matthias Braun's avatar
Matthias Braun committed
388
	assert(ent->entity_kind == IR_ENTITY_LABEL);
389
390
391
	return ent->attr.code_attr.label;
}

392
393
394
395
396
397
398
void set_entity_visibility(ir_entity *entity, ir_visibility visibility)
{
	entity->visibility = visibility;
}

ir_visibility get_entity_visibility(const ir_entity *entity)
{
399
	return (ir_visibility)entity->visibility;
400
401
}

Matthias Braun's avatar
Matthias Braun committed
402
403
404
405
406
407
408
409
410
void set_entity_linkage(ir_entity *entity, ir_linkage linkage)
{
	entity->linkage = linkage;
}

ir_linkage (get_entity_linkage)(const ir_entity *entity)
{
	return get_entity_linkage(entity);
}
411

Matthias Braun's avatar
Matthias Braun committed
412
413
414
415
void add_entity_linkage(ir_entity *entity, ir_linkage linkage)
{
	entity->linkage |= linkage;
}
416

Matthias Braun's avatar
Matthias Braun committed
417
418
419
420
void remove_entity_linkage(ir_entity *entity, ir_linkage linkage)
{
	entity->linkage &= ~linkage;
}
421

422
423
ir_entity_usage (get_entity_usage)(const ir_entity *ent)
{
424
425
	return _get_entity_usage(ent);
}
Michael Beck's avatar
Michael Beck committed
426

427
428
void (set_entity_usage)(ir_entity *ent, ir_entity_usage flags)
{
429
430
	_set_entity_usage(ent, flags);
}
431

432
ir_node *get_atomic_ent_value(const ir_entity *entity)
Matthias Braun's avatar
Matthias Braun committed
433
{
Matthias Braun's avatar
Matthias Braun committed
434
	assert(is_atomic_entity(entity));
Matthias Braun's avatar
Matthias Braun committed
435
436

	ir_initializer_t *initializer = get_entity_initializer(entity);
Matthias Braun's avatar
Matthias Braun committed
437
438
439
440
	if (initializer == NULL) {
		ir_type *type = get_entity_type(entity);
		return new_r_Unknown(get_const_code_irg(), get_type_mode(type));
	}
441

Matthias Braun's avatar
Matthias Braun committed
442
443
444
445
	switch (get_initializer_kind(initializer)) {
	case IR_INITIALIZER_NULL: {
		ir_type *type = get_entity_type(entity);
		ir_mode *mode = get_type_mode(type);
446
		return new_r_Const_null(get_const_code_irg(), mode);
Matthias Braun's avatar
Matthias Braun committed
447
448
	}
	case IR_INITIALIZER_TARVAL: {
Matthias Braun's avatar
Matthias Braun committed
449
		ir_tarval *tv = get_initializer_tarval_value(initializer);
Matthias Braun's avatar
Matthias Braun committed
450
451
452
453
454
455
456
457
		return new_r_Const(get_const_code_irg(), tv);
	}
	case IR_INITIALIZER_CONST:
		return get_initializer_const_value(initializer);
	case IR_INITIALIZER_COMPOUND:
		panic("compound initializer in atomic entity not allowed (%+F)", entity);
	}

458
	panic("invalid initializer kind (%+F)", entity);
Matthias Braun's avatar
Matthias Braun committed
459
460
461
}

void set_atomic_ent_value(ir_entity *entity, ir_node *val)
462
{
Matthias Braun's avatar
Matthias Braun committed
463
464
	assert(is_atomic_entity(entity));
	assert(is_Dummy(val) || get_irn_mode(val) == get_type_mode(entity->type));
Matthias Braun's avatar
Matthias Braun committed
465
	ir_initializer_t *initializer = create_initializer_const(val);
466
	set_entity_initializer(entity, initializer);
Matthias Braun's avatar
Matthias Braun committed
467
}
468

469
470
471
472
473
474
475
476
477
478
const char *get_initializer_kind_name(ir_initializer_kind_t ini)
{
#define X(a)    case a: return #a
	switch (ini) {
	X(IR_INITIALIZER_CONST);
	X(IR_INITIALIZER_TARVAL);
	X(IR_INITIALIZER_NULL);
	X(IR_INITIALIZER_COMPOUND);
	}
#undef X
Matthias Braun's avatar
Matthias Braun committed
479
	return "BAD VALUE";
480
481
}

482
483
484
485
486
487
488
489
490
491
492
493
static ir_initializer_t null_initializer = { IR_INITIALIZER_NULL };

ir_initializer_t *get_initializer_null(void)
{
	return &null_initializer;
}

ir_initializer_t *create_initializer_const(ir_node *value)
{
	struct obstack *obst = get_irg_obstack(get_const_code_irg());

	ir_initializer_t *initializer
494
		= (ir_initializer_t*)OALLOC(obst, ir_initializer_const_t);
495
496
497
498
499
500
	initializer->kind         = IR_INITIALIZER_CONST;
	initializer->consti.value = value;

	return initializer;
}

Matthias Braun's avatar
Matthias Braun committed
501
ir_initializer_t *create_initializer_tarval(ir_tarval *tv)
502
503
504
505
{
	struct obstack *obst = get_irg_obstack(get_const_code_irg());

	ir_initializer_t *initializer
506
		= (ir_initializer_t*)OALLOC(obst, ir_initializer_tarval_t);
507
508
509
510
511
512
	initializer->kind         = IR_INITIALIZER_TARVAL;
	initializer->tarval.value = tv;

	return initializer;
}

513
ir_initializer_t *create_initializer_compound(size_t n_entries)
514
515
516
{
	struct obstack *obst = get_irg_obstack(get_const_code_irg());

Matthias Braun's avatar
Matthias Braun committed
517
518
519
	size_t size = sizeof(ir_initializer_compound_t)
	            + n_entries * sizeof(ir_initializer_t*)
	            - sizeof(ir_initializer_t*);
520

521
522
	ir_initializer_t *initializer
		= (ir_initializer_t*)obstack_alloc(obst, size);
523
524
525
	initializer->kind                    = IR_INITIALIZER_COMPOUND;
	initializer->compound.n_initializers = n_entries;

Matthias Braun's avatar
Matthias Braun committed
526
	for (size_t i = 0; i < n_entries; ++i) {
527
528
529
530
531
532
533
534
535
		initializer->compound.initializers[i] = get_initializer_null();
	}

	return initializer;
}

ir_node *get_initializer_const_value(const ir_initializer_t *initializer)
{
	assert(initializer->kind == IR_INITIALIZER_CONST);
536
	return skip_Id(initializer->consti.value);
537
538
}

Matthias Braun's avatar
Matthias Braun committed
539
ir_tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
540
541
542
543
544
{
	assert(initializer->kind == IR_INITIALIZER_TARVAL);
	return initializer->tarval.value;
}

545
size_t get_initializer_compound_n_entries(const ir_initializer_t *initializer)
546
547
548
549
550
551
{
	assert(initializer->kind == IR_INITIALIZER_COMPOUND);
	return initializer->compound.n_initializers;
}

void set_initializer_compound_value(ir_initializer_t *initializer,
552
                                    size_t index, ir_initializer_t *value)
553
554
555
556
557
558
559
560
{
	assert(initializer->kind == IR_INITIALIZER_COMPOUND);
	assert(index < initializer->compound.n_initializers);

	initializer->compound.initializers[index] = value;
}

ir_initializer_t *get_initializer_compound_value(
561
		const ir_initializer_t *initializer, size_t index)
562
563
564
565
566
567
568
569
570
571
572
573
574
575
{
	assert(initializer->kind == IR_INITIALIZER_COMPOUND);
	assert(index < initializer->compound.n_initializers);

	return initializer->compound.initializers[index];
}

ir_initializer_kind_t get_initializer_kind(const ir_initializer_t *initializer)
{
	return initializer->kind;
}

static void check_entity_initializer(ir_entity *entity)
{
Matthias Braun's avatar
Matthias Braun committed
576
#ifndef NDEBUG
577
	ir_initializer_t *initializer = get_entity_initializer(entity);
578
579
	if (initializer == NULL)
		return;
580
	ir_type          *entity_tp   = get_entity_type(entity);
Matthias Braun's avatar
Matthias Braun committed
581
582
	switch (initializer->kind) {
	case IR_INITIALIZER_COMPOUND:
583
		assert(is_aggregate_type(entity_tp));
Matthias Braun's avatar
Matthias Braun committed
584
		break;
585

Matthias Braun's avatar
Matthias Braun committed
586
587
	case IR_INITIALIZER_CONST:
	case IR_INITIALIZER_TARVAL:
588
		assert(is_atomic_type(entity_tp));
Matthias Braun's avatar
Matthias Braun committed
589
		break;
590

Matthias Braun's avatar
Matthias Braun committed
591
592
593
	case IR_INITIALIZER_NULL:
		break;
	}
yb9976's avatar
yb9976 committed
594
595
#else
	(void)entity;
Matthias Braun's avatar
Matthias Braun committed
596
#endif
597
598
599
600
}

void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
{
601
	assert(get_entity_kind(entity) == IR_ENTITY_NORMAL);
602
	entity->attr.normal.initializer = initializer;
603
604
605
	check_entity_initializer(entity);
}

606
ir_initializer_t *(get_entity_initializer)(const ir_entity *entity)
607
{
608
	return _get_entity_initializer(entity);
609
610
}

611
612
int (get_entity_offset)(const ir_entity *ent)
{
613
	return _get_entity_offset(ent);
614
}
Michael Beck's avatar
Michael Beck committed
615

616
617
void (set_entity_offset)(ir_entity *ent, int offset)
{
618
	_set_entity_offset(ent, offset);
619
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
620

621
unsigned (get_entity_bitfield_offset)(const ir_entity *ent)
622
{
623
	return _get_entity_bitfield_offset(ent);
624
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
625

626
void (set_entity_bitfield_offset)(ir_entity *ent, unsigned offset)
627
{
628
629
630
631
632
633
634
635
636
637
638
	_set_entity_bitfield_offset(ent, offset);
}

unsigned (get_entity_bitfield_size)(const ir_entity *ent)
{
	return _get_entity_bitfield_size(ent);
}

void (set_entity_bitfield_size)(ir_entity *ent, unsigned size)
{
	_set_entity_bitfield_size(ent, size);
639
}
Michael Beck's avatar
Michael Beck committed
640

641
642
void add_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
{
643
644
645
	if (ent->overwrites == NULL) {
		ent->overwrites = NEW_ARR_F(ir_entity*, 0);
	}
646
	ARR_APP1(ir_entity *, ent->overwrites, overwritten);
647
648
649
	if (overwritten->overwrittenby == NULL) {
		overwritten->overwrittenby = NEW_ARR_F(ir_entity*, 0);
	}
650
	ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
651
}
652

653
size_t get_entity_n_overwrites(const ir_entity *ent)
654
{
655
656
657
	if (ent->overwrites == NULL)
		return 0;
	return ARR_LEN(ent->overwrites);
658
}
659

660
size_t get_entity_overwrites_index(const ir_entity *ent, ir_entity *overwritten)
661
{
Matthias Braun's avatar
Matthias Braun committed
662
	for (size_t i = 0, n = get_entity_n_overwrites(ent); i < n; ++i) {
663
664
		if (get_entity_overwrites(ent, i) == overwritten)
			return i;
665
	}
666
	return (size_t)-1;
667
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
668

669
ir_entity *get_entity_overwrites(const ir_entity *ent, size_t pos)
670
{
671
672
	assert(pos < get_entity_n_overwrites(ent));
	return ent->overwrites[pos];
673
}
674

675
void set_entity_overwrites(ir_entity *ent, size_t pos, ir_entity *overwritten)
676
{
677
678
	assert(pos < get_entity_n_overwrites(ent));
	ent->overwrites[pos] = overwritten;
679
}
680

681
682
void remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
{
Matthias Braun's avatar
Matthias Braun committed
683
	for (size_t i = 0, n = get_entity_n_overwrites(ent); i < n; ++i) {
684
		if (ent->overwrites[i] == overwritten) {
685
			for (; i < n - 1; i++)
686
				ent->overwrites[i] = ent->overwrites[i+1];
687
			ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
688
689
			break;
		}
690
	}
691
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
692

693

694
size_t get_entity_n_overwrittenby(const ir_entity *ent)
695
{
696
697
	if (ent->overwrittenby == NULL)
		return 0;
698
	return ARR_LEN(ent->overwrittenby);
699
}
700

701
702
size_t get_entity_overwrittenby_index(const ir_entity *ent,
                                      ir_entity *overwrites)
703
{
Matthias Braun's avatar
Matthias Braun committed
704
	for (size_t i = 0, n = get_entity_n_overwrittenby(ent); i < n; ++i) {
705
706
		if (get_entity_overwrittenby(ent, i) == overwrites)
			return i;
707
	}
708
	return (size_t)-1;
709
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
710

711
ir_entity *get_entity_overwrittenby(const ir_entity *ent, size_t pos)
712
{
713
714
	assert(pos < get_entity_n_overwrittenby(ent));
	return ent->overwrittenby[pos];
715
}
716

717
void set_entity_overwrittenby(ir_entity *ent, size_t pos, ir_entity *overwrites)
718
{
719
720
	assert(pos < get_entity_n_overwrittenby(ent));
	ent->overwrittenby[pos] = overwrites;
721
}
722

723
724
void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites)
{
Matthias Braun's avatar
Matthias Braun committed
725
	for (size_t i = 0, n = get_entity_n_overwrittenby(ent); i < n; ++i) {
726
		if (ent->overwrittenby[i] == overwrites) {
727
			for (; i < n - 1; ++i)
728
				ent->overwrittenby[i] = ent->overwrittenby[i+1];
729
			ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
730
731
			break;
		}
732
	}
733
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
734

735
736
void *(get_entity_link)(const ir_entity *ent)
{
737
	return _get_entity_link(ent);
738
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
739

740
741
void (set_entity_link)(ir_entity *ent, void *l)
{
742
	_set_entity_link(ent, l);
743
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
744

745
746
ir_graph *(get_entity_irg)(const ir_entity *ent)
{
747
	return _get_entity_irg(ent);
748
}
Christian Schäfer's avatar
Christian Schäfer committed
749

750
751
752
753
754
ir_graph *(get_entity_linktime_irg)(const ir_entity *ent)
{
	return _get_entity_linktime_irg(ent);
}

755
756
void set_entity_irg(ir_entity *ent, ir_graph *irg)
{
757
758
	assert(is_method_entity(ent));
	ent->attr.mtd_attr.irg = irg;
759
}
760

761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
int (is_parameter_entity)(const ir_entity *entity)
{
	return _is_parameter_entity(entity);
}

size_t (get_entity_parameter_number)(const ir_entity *entity)
{
	return _get_entity_parameter_number(entity);
}

void set_entity_parameter_number(ir_entity *entity, size_t n)
{
	assert(is_parameter_entity(entity));
	entity->attr.parameter.number = n;
}

777
778
unsigned get_entity_vtable_number(const ir_entity *ent)
{
yb9976's avatar
yb9976 committed
779
	assert(is_method_entity(ent));
780
	return ent->attr.mtd_attr.vtable_number;
781
}
782

783
784
void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number)
{
785
786
	assert(is_method_entity(ent));
	ent->attr.mtd_attr.vtable_number = vtable_number;
787
}
788

789
790
791
792
793
int is_unknown_entity(const ir_entity *entity)
{
	return entity->entity_kind == IR_ENTITY_UNKNOWN;
}

Matthias Braun's avatar
Matthias Braun committed
794
int is_atomic_entity(const ir_entity *ent)
795
{
796
797
798
799
	ir_type const *const type   = get_entity_type(ent);
	tp_opcode      const opcode = get_type_opcode(type);
	return opcode == tpo_primitive || opcode == tpo_pointer
	    || opcode == tpo_method;
800
}
801

Matthias Braun's avatar
Matthias Braun committed
802
int is_compound_entity(const ir_entity *ent)
803
{
804
805
	ir_type const *const type = get_entity_type(ent);
	return is_compound_type(type);
806
}
807

Matthias Braun's avatar
Matthias Braun committed
808
int is_method_entity(const ir_entity *ent)
809
{
810
811
812
813
814
815
	return ent->entity_kind == IR_ENTITY_METHOD;
}

int is_alias_entity(const ir_entity *entity)
{
	return entity->entity_kind == IR_ENTITY_ALIAS;
816
}
817

Matthias Braun's avatar
Matthias Braun committed
818
ir_visited_t (get_entity_visited)(const ir_entity *ent)
819
{
820
	return _get_entity_visited(ent);
821
}
Michael Beck's avatar
Michael Beck committed
822

823
824
void (set_entity_visited)(ir_entity *ent, ir_visited_t num)
{
825
	_set_entity_visited(ent, num);
826
}
Michael Beck's avatar
Michael Beck committed
827

828
829
void (mark_entity_visited)(ir_entity *ent)
{
830
	_mark_entity_visited(ent);
831
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
832

Matthias Braun's avatar
Matthias Braun committed
833
int (entity_visited)(const ir_entity *ent)
834
{
835
	return _entity_visited(ent);
836
}
837

Matthias Braun's avatar
Matthias Braun committed
838
int (entity_not_visited)(const ir_entity *ent)
839
{
840
	return _entity_not_visited(ent);
841
}
842

843
844
845
846
847
848
int entity_has_additional_properties(const ir_entity *entity)
{
	return entity->entity_kind == IR_ENTITY_METHOD
	    || entity->entity_kind == IR_ENTITY_ALIAS;
}

849
mtp_additional_properties get_entity_additional_properties(const ir_entity *ent)
850
{